From 425c7d0b327ce8d23a0cb831c0f96c96457bcaa7 Mon Sep 17 00:00:00 2001 From: Josh Gratton Date: Sat, 16 May 2026 17:17:29 -0400 Subject: [PATCH] feat(desktop): move backend-ready latch from DesktopState onto the window DesktopState.backendReady was the last global coupling tying backend lifecycle to app-wide state. With the pool owning per-instance readiness (instance.snapshot.ready), the only remaining consumer of the global latch is the window's auto-create-on-ready path. Move ownership of the latch into DesktopWindow's own internals so DesktopState only carries truly app-wide state (the quitting flag). DesktopWindow gains handleBackendNotReady, called by the primary instance's onShutdown callback so the latch clears on clean stop, restart, or crash. Without it the macOS dock-click activation path could produce a window pointing at a backend that is no longer up. The pool spec wires both callbacks against the window service instead of the state Ref. Test stubs for DesktopWindowShape pick up the new handleBackendNotReady field; DesktopWindow.test.ts drops its DesktopState dependency. --- apps/desktop/src/app/DesktopState.ts | 2 -- .../src/backend/DesktopBackendManager.ts | 7 ++-- .../desktop/src/backend/DesktopBackendPool.ts | 34 +++++++++---------- .../src/window/DesktopApplicationMenu.test.ts | 1 + apps/desktop/src/window/DesktopWindow.test.ts | 2 -- apps/desktop/src/window/DesktopWindow.ts | 21 +++++++++--- 6 files changed, 38 insertions(+), 29 deletions(-) diff --git a/apps/desktop/src/app/DesktopState.ts b/apps/desktop/src/app/DesktopState.ts index 43960ada65..2a1deeb66c 100644 --- a/apps/desktop/src/app/DesktopState.ts +++ b/apps/desktop/src/app/DesktopState.ts @@ -4,7 +4,6 @@ import * as Layer from "effect/Layer"; import * as Ref from "effect/Ref"; export interface DesktopStateShape { - readonly backendReady: Ref.Ref; readonly quitting: Ref.Ref; } @@ -15,7 +14,6 @@ export class DesktopState extends Context.Service desktopWindow.handleBackendReady.pipe(Effect.catch(() => Effect.void)), - onShutdown: () => Ref.set(desktopState.backendReady, false), + onShutdown: () => desktopWindow.handleBackendNotReady, }); const instancesRef = yield* Ref.make>( diff --git a/apps/desktop/src/window/DesktopApplicationMenu.test.ts b/apps/desktop/src/window/DesktopApplicationMenu.test.ts index fc589b3e39..3d8532f731 100644 --- a/apps/desktop/src/window/DesktopApplicationMenu.test.ts +++ b/apps/desktop/src/window/DesktopApplicationMenu.test.ts @@ -71,6 +71,7 @@ const makeDesktopWindowLayer = (selectedAction: Deferred.Deferred) => activate: Effect.void, createMainIfBackendReady: Effect.void, handleBackendReady: Effect.void, + handleBackendNotReady: Effect.void, dispatchMenuAction: (action) => Deferred.succeed(selectedAction, action).pipe(Effect.asVoid), syncAppearance: Effect.void, } satisfies DesktopWindow.DesktopWindowShape); diff --git a/apps/desktop/src/window/DesktopWindow.test.ts b/apps/desktop/src/window/DesktopWindow.test.ts index fbcc60934a..f2b6b1e9a2 100644 --- a/apps/desktop/src/window/DesktopWindow.test.ts +++ b/apps/desktop/src/window/DesktopWindow.test.ts @@ -11,7 +11,6 @@ import { vi } from "vitest"; import * as DesktopAssets from "../app/DesktopAssets.ts"; import * as DesktopConfig from "../app/DesktopConfig.ts"; import * as DesktopEnvironment from "../app/DesktopEnvironment.ts"; -import * as DesktopState from "../app/DesktopState.ts"; import * as ElectronMenu from "../electron/ElectronMenu.ts"; import * as ElectronShell from "../electron/ElectronShell.ts"; import * as ElectronTheme from "../electron/ElectronTheme.ts"; @@ -143,7 +142,6 @@ function makeTestLayer(input: { desktopAssetsLayer, desktopEnvironmentLayer, desktopServerExposureLayer, - DesktopState.layer, electronMenuLayer, electronShellLayer, electronThemeLayer, diff --git a/apps/desktop/src/window/DesktopWindow.ts b/apps/desktop/src/window/DesktopWindow.ts index 8ebd1041c6..8d40b8cb8e 100644 --- a/apps/desktop/src/window/DesktopWindow.ts +++ b/apps/desktop/src/window/DesktopWindow.ts @@ -10,7 +10,6 @@ import type * as Electron from "electron"; import * as DesktopAssets from "../app/DesktopAssets.ts"; import * as DesktopEnvironment from "../app/DesktopEnvironment.ts"; import * as DesktopObservability from "../app/DesktopObservability.ts"; -import * as DesktopState from "../app/DesktopState.ts"; import * as ElectronMenu from "../electron/ElectronMenu.ts"; import * as ElectronShell from "../electron/ElectronShell.ts"; import * as ElectronTheme from "../electron/ElectronTheme.ts"; @@ -32,7 +31,6 @@ type DesktopWindowRuntimeServices = | DesktopEnvironment.DesktopEnvironment | DesktopAssets.DesktopAssets | DesktopServerExposure.DesktopServerExposure - | DesktopState.DesktopState | ElectronMenu.ElectronMenu | ElectronShell.ElectronShell | ElectronTheme.ElectronTheme @@ -57,6 +55,11 @@ export interface DesktopWindowShape { readonly activate: Effect.Effect; readonly createMainIfBackendReady: Effect.Effect; readonly handleBackendReady: Effect.Effect; + // Called when the backend transitions back to "not ready" (clean stop, + // restart, crash). Clears the latch that lets `activate` auto-create a + // window so a "macOS dock click" while the backend is down doesn't + // produce a stranded window pointing at nothing. + readonly handleBackendNotReady: Effect.Effect; readonly dispatchMenuAction: (action: string) => Effect.Effect; readonly syncAppearance: Effect.Effect; } @@ -152,7 +155,12 @@ const make = Effect.gen(function* () { const electronTheme = yield* ElectronTheme.ElectronTheme; const electronWindow = yield* ElectronWindow.ElectronWindow; const serverExposure = yield* DesktopServerExposure.DesktopServerExposure; - const state = yield* DesktopState.DesktopState; + // Window-side latch for the primary backend's readiness. Set by + // handleBackendReady (driven by the pool's onReady callback), cleared + // by handleBackendNotReady (driven by onShutdown). Only consumed by + // createMainIfBackendReady, which gates the post-readiness window + // open in development and the macOS "activate without windows" path. + const backendReadyRef = yield* Ref.make(false); const context = yield* Effect.context(); const runPromise = Effect.runPromiseWith(context); @@ -313,7 +321,7 @@ const make = Effect.gen(function* () { }).pipe(Effect.withSpan("desktop.window.revealOrCreateMain")); const createMainIfBackendReady = Effect.gen(function* () { - const backendReady = yield* Ref.get(state.backendReady); + const backendReady = yield* Ref.get(backendReadyRef); if (!backendReady) return; const existingWindow = yield* electronWindow.currentMainOrFirst; if (Option.isSome(existingWindow)) return; @@ -334,10 +342,13 @@ const make = Effect.gen(function* () { }).pipe(Effect.withSpan("desktop.window.activate")), createMainIfBackendReady, handleBackendReady: Effect.gen(function* () { - yield* Ref.set(state.backendReady, true); + yield* Ref.set(backendReadyRef, true); yield* logWindowInfo("backend ready", { source: "http" }); yield* createMainIfBackendReady; }).pipe(Effect.withSpan("desktop.window.handleBackendReady")), + handleBackendNotReady: Ref.set(backendReadyRef, false).pipe( + Effect.withSpan("desktop.window.handleBackendNotReady"), + ), dispatchMenuAction: Effect.fn("desktop.window.dispatchMenuAction")(function* (action) { yield* Effect.annotateCurrentSpan({ action }); const existingWindow = yield* electronWindow.focusedMainOrFirst;