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.
This commit is contained in:
Josh Gratton committed 2026-05-16 17:17:29 -04:00
1 parent a8fc78459a
commit 425c7d0b32
6 files changed
+38 -29

No files matched your search

-2
View File
@@ -4,7 +4,6 @@ import * as Layer from "effect/Layer";
import * as Ref from "effect/Ref";
export interface DesktopStateShape {
readonly backendReady: Ref.Ref<boolean>;
readonly quitting: Ref.Ref<boolean>;
}
@@ -15,7 +14,6 @@ export class DesktopState extends Context.Service<DesktopState, DesktopStateShap
export const layer = Layer.effect(
DesktopState,
Effect.all({
backendReady: Ref.make(false),
quitting: Ref.make(false),
}),
);
@@ -14,9 +14,10 @@
// - configResolve replaces `DesktopBackendConfiguration.resolve` so each
// instance can resolve its own start config (Windows-native, WSL via
// wsl.exe, etc.).
// - onReady / onShutdown replace direct writes to
// `DesktopState.backendReady` and `DesktopWindow.handleBackendReady`
// so only the primary's spec wires those side effects.
// - onReady / onShutdown drive UI side effects (window auto-open,
// readiness latch) only for instances that want them — the primary's
// spec passes the window's handleBackendReady/handleBackendNotReady,
// other pool instances pass nothing.
// - log writes still go through the shared `DesktopBackendOutputLog`
// service for now; step 3 of the migration will split that per
// instance.
+17 -17
View File
@@ -2,14 +2,14 @@
// point for the concurrent-Windows+WSL-backend feature; see the design
// notes below before extending it.
//
// Current state (step 1):
// Current state (step 2):
// - `DesktopBackendManager.ts` no longer exposes a Context.Service. It
// is a per-instance factory (`makeBackendInstance(spec)`); the pool
// calls it once for the Windows primary at startup.
// - The primary spec wires `configResolve` to `DesktopBackendConfiguration`
// and the `onReady`/`onShutdown` callbacks to the legacy global side
// effects (`DesktopState.backendReady`, `DesktopWindow.handleBackendReady`).
// Those couplings move per-instance in steps 2-3.
// and the `onReady`/`onShutdown` callbacks to the window service's
// `handleBackendReady` / `handleBackendNotReady`. Readiness is no
// longer in `DesktopState`; the window owns its own latch.
// - Consumers (window/wsl IPC, lifecycle hooks, telemetry) read the
// primary instance off `pool.primary`. There is no longer a separate
// `DesktopBackendManager` service in the layer graph.
@@ -18,10 +18,9 @@
// - The pool layer constructs N instances — at minimum the Windows
// primary; the WSL instance is added when the user enables the WSL
// backend (with the selected distro).
// - Per-instance state (readiness, restart fiber, active run) already
// lives on each `DesktopBackendInstance` after step 1. Step 2 moves
// the global `DesktopState.backendReady` flag onto the pool's primary
// readiness; step 3 splits backend log routing per instance.
// - Per-instance state (readiness, restart fiber, active run) lives on
// each `DesktopBackendInstance`. Step 3 splits backend log routing
// per instance.
// - `getLocalEnvironmentBootstrap()` widens to
// `getLocalEnvironmentBootstraps()` returning one bootstrap per pool
// instance; the frontend env runtime registers each as a local
@@ -31,12 +30,11 @@
// pool holds. No more swap-mode, no more rollback-on-restart.
//
// Migration sequence (each step is its own commit):
// 1. (this commit) Reshape `DesktopBackendManager` into an instance
// factory and route consumers through the pool. Pool still holds a
// single instance.
// 2. Drop `DesktopState.backendReady`. UI subscribes to
// `pool.primary.snapshot.ready` (or the multi-instance readiness
// signal once it exists).
// 1. Reshape `DesktopBackendManager` into an instance factory and route
// consumers through the pool. Pool still holds a single instance.
// 2. (this commit) Drop `DesktopState.backendReady`. The window owns
// its own readiness latch, driven by the primary instance's
// onReady/onShutdown callbacks.
// 3. Per-instance log routing: split `DesktopBackendOutputLog` into a
// factory keyed by instance id.
// 4. Add register/unregister so WSL backend can be added on demand.
@@ -55,7 +53,6 @@ import * as Ref from "effect/Ref";
import * as DesktopBackendConfiguration from "./DesktopBackendConfiguration.ts";
import * as DesktopBackendManager from "./DesktopBackendManager.ts";
import * as DesktopState from "../app/DesktopState.ts";
import * as DesktopWindow from "../window/DesktopWindow.ts";
export type BackendInstanceId = DesktopBackendManager.BackendInstanceId;
@@ -86,15 +83,18 @@ export const layer = Layer.effect(
DesktopBackendPool,
Effect.gen(function* () {
const configuration = yield* DesktopBackendConfiguration.DesktopBackendConfiguration;
const desktopState = yield* DesktopState.DesktopState;
const desktopWindow = yield* DesktopWindow.DesktopWindow;
const primary = yield* DesktopBackendManager.makeBackendInstance({
id: DesktopBackendManager.PRIMARY_INSTANCE_ID,
label: "Windows",
configResolve: configuration.resolve,
// Window creation errors propagating out of handleBackendReady are
// swallowed here on purpose: they're logged by the window service
// and we don't want a stuck splash window to block the readiness
// callback (which would prevent restartAttempt from being reset).
onReady: () => desktopWindow.handleBackendReady.pipe(Effect.catch(() => Effect.void)),
onShutdown: () => Ref.set(desktopState.backendReady, false),
onShutdown: () => desktopWindow.handleBackendNotReady,
});
const instancesRef = yield* Ref.make<ReadonlyMap<BackendInstanceId, DesktopBackendInstance>>(
@@ -71,6 +71,7 @@ const makeDesktopWindowLayer = (selectedAction: Deferred.Deferred<string>) =>
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);
@@ -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,
+16 -5
View File
@@ -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<void, DesktopWindowError>;
readonly createMainIfBackendReady: Effect.Effect<void, DesktopWindowError>;
readonly handleBackendReady: Effect.Effect<void, DesktopWindowError>;
// 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<void>;
readonly dispatchMenuAction: (action: string) => Effect.Effect<void, DesktopWindowError>;
readonly syncAppearance: Effect.Effect<void>;
}
@@ -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<DesktopWindowRuntimeServices>();
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;