diff --git a/apps/desktop/src/backend/DesktopBackendManager.test.ts b/apps/desktop/src/backend/DesktopBackendManager.test.ts index 0c2239ba50..79d958e53c 100644 --- a/apps/desktop/src/backend/DesktopBackendManager.test.ts +++ b/apps/desktop/src/backend/DesktopBackendManager.test.ts @@ -135,7 +135,7 @@ function makeTestInstance(input: MakeInstanceInput) { const instance = DesktopBackendManager.makeBackendInstance({ id: DesktopBackendManager.PRIMARY_INSTANCE_ID, - label: "Windows", + label: Effect.succeed("Windows"), configResolve: Effect.succeed(input.config ?? baseConfig), ...(input.onReady ? { onReady: () => input.onReady! } : {}), ...(input.onShutdown ? { onShutdown: () => input.onShutdown! } : {}), diff --git a/apps/desktop/src/backend/DesktopBackendManager.ts b/apps/desktop/src/backend/DesktopBackendManager.ts index 276f7c51f9..7698f5af1b 100644 --- a/apps/desktop/src/backend/DesktopBackendManager.ts +++ b/apps/desktop/src/backend/DesktopBackendManager.ts @@ -154,7 +154,7 @@ export const PRIMARY_INSTANCE_ID: BackendInstanceId = BackendInstanceId( // + UI something to route on. export interface DesktopBackendInstance { readonly id: BackendInstanceId; - readonly label: string; + readonly label: Effect.Effect; readonly start: Effect.Effect; readonly stop: (options?: { readonly timeout?: Duration.Duration }) => Effect.Effect; readonly currentConfig: Effect.Effect>; @@ -174,7 +174,7 @@ export interface DesktopBackendInstance { // concerns; other instances pass them as undefined. export interface BackendInstanceSpec { readonly id: BackendInstanceId; - readonly label: string; + readonly label: Effect.Effect; // configResolve can now fail with PlatformError because the // bootstrap-token closure inside DesktopBackendConfiguration uses // crypto.randomBytes (Effect 4 beta.73 migration). diff --git a/apps/desktop/src/backend/DesktopBackendPool.test.ts b/apps/desktop/src/backend/DesktopBackendPool.test.ts index 6fdf2d31f6..b66330842b 100644 --- a/apps/desktop/src/backend/DesktopBackendPool.test.ts +++ b/apps/desktop/src/backend/DesktopBackendPool.test.ts @@ -1,8 +1,16 @@ import { assert, describe, it } from "@effect/vitest"; import * as Duration from "effect/Duration"; import * as Effect from "effect/Effect"; +import * as FileSystem from "effect/FileSystem"; +import * as Layer from "effect/Layer"; import * as Option from "effect/Option"; +import * as Ref from "effect/Ref"; +import { HttpClient } from "effect/unstable/http"; +import { ChildProcessSpawner } from "effect/unstable/process"; +import * as DesktopObservability from "../app/DesktopObservability.ts"; +import * as DesktopWindow from "../window/DesktopWindow.ts"; +import * as DesktopBackendConfiguration from "./DesktopBackendConfiguration.ts"; import * as DesktopBackendPool from "./DesktopBackendPool.ts"; import type { DesktopBackendSnapshot, DesktopBackendStartConfig } from "./DesktopBackendManager.ts"; @@ -19,7 +27,7 @@ function makeStubInstance( }; return { id, - label, + label: Effect.succeed(label), start: Effect.void, stop: () => Effect.void, currentConfig: Effect.succeed(Option.none()), @@ -28,6 +36,49 @@ function makeStubInstance( }; } +function makePoolLayer( + labelRef: Ref.Ref, +): Layer.Layer { + return DesktopBackendPool.layer.pipe( + Layer.provideMerge( + Layer.mergeAll( + FileSystem.layerNoop({}), + Layer.succeed( + ChildProcessSpawner.ChildProcessSpawner, + ChildProcessSpawner.make(() => Effect.die("unexpected child process spawn")), + ), + Layer.succeed( + HttpClient.HttpClient, + HttpClient.make(() => Effect.die("unexpected HTTP request")), + ), + Layer.succeed(DesktopObservability.DesktopBackendOutputLogFactory, { + forInstance: () => + Effect.succeed({ + writeSessionBoundary: () => Effect.void, + writeOutputChunk: () => Effect.void, + } satisfies DesktopObservability.DesktopBackendOutputLogShape), + } satisfies DesktopObservability.DesktopBackendOutputLogFactoryShape), + Layer.succeed(DesktopBackendConfiguration.DesktopBackendConfiguration, { + resolvePrimary: Effect.die("unexpected primary config resolve"), + resolvePrimaryLabel: Ref.get(labelRef), + resolveWsl: () => Effect.die("unexpected WSL config resolve"), + } satisfies DesktopBackendConfiguration.DesktopBackendConfigurationShape), + Layer.succeed(DesktopWindow.DesktopWindow, { + createMain: Effect.die("unexpected window create"), + ensureMain: Effect.die("unexpected window ensure"), + revealOrCreateMain: Effect.die("unexpected window reveal"), + activate: Effect.die("unexpected window activate"), + createMainIfBackendReady: Effect.die("unexpected window create"), + handleBackendReady: () => Effect.void, + handleBackendNotReady: Effect.void, + dispatchMenuAction: () => Effect.die("unexpected menu action"), + syncAppearance: Effect.void, + } satisfies DesktopWindow.DesktopWindowShape), + ), + ), + ); +} + describe("DesktopBackendPool", () => { it.effect("layerTest exposes registered instances by id", () => Effect.gen(function* () { @@ -38,8 +89,8 @@ describe("DesktopBackendPool", () => { const all = yield* pool.list; const resolvedPrimary = yield* pool.primary; - assert.equal(Option.getOrNull(fetchedPrimary)?.label, "Windows"); - assert.equal(Option.getOrNull(fetchedWsl)?.label, "WSL (Ubuntu)"); + assert.equal(yield* Option.getOrThrow(fetchedPrimary).label, "Windows"); + assert.equal(yield* Option.getOrThrow(fetchedWsl).label, "WSL (Ubuntu)"); assert.isTrue(Option.isNone(fetchedMissing)); assert.lengthOf(all, 2); // First instance becomes primary in layerTest so single-instance @@ -62,4 +113,20 @@ describe("DesktopBackendPool", () => { }).pipe(Effect.provide(DesktopBackendPool.layerTest([]))), ).pipe(Effect.map((exit) => assert.equal(exit._tag, "Failure"))), ); + + it.effect("resolves the primary label lazily after pool layer construction", () => + Effect.scoped( + Effect.gen(function* () { + const labelRef = yield* Ref.make("Windows"); + const pool = yield* DesktopBackendPool.DesktopBackendPool.pipe( + Effect.provide(makePoolLayer(labelRef)), + ); + const primary = yield* pool.primary; + + yield* Ref.set(labelRef, "WSL (Ubuntu)"); + + assert.equal(yield* primary.label, "WSL (Ubuntu)"); + }), + ), + ); }); diff --git a/apps/desktop/src/backend/DesktopBackendPool.ts b/apps/desktop/src/backend/DesktopBackendPool.ts index 06f8e54f35..9396c7c6d3 100644 --- a/apps/desktop/src/backend/DesktopBackendPool.ts +++ b/apps/desktop/src/backend/DesktopBackendPool.ts @@ -203,17 +203,13 @@ export const layer = Layer.effect( // primary instance uses. const factoryContext = yield* Effect.context(); - // Primary label is captured once per process. The configuration derives - // it from the same decision resolvePrimary makes (wsl-only + WSL actually - // available, else Windows), so the label always matches the backend that - // resolves — including the fall-back to Windows when WSL is unavailable. - // We don't react to runtime setting changes here: the wsl-only switch - // requires an app restart, so this reflects the mode the user is in. - const primaryLabel = yield* configuration.resolvePrimaryLabel; - const primary = yield* DesktopBackendManager.makeBackendInstance({ id: DesktopBackendManager.PRIMARY_INSTANCE_ID, - label: primaryLabel, + // Keep this lazy. The pool layer is initialized before startup loads + // persisted desktop settings, so resolving the primary label here would + // permanently capture DEFAULT_DESKTOP_SETTINGS and mislabel WSL-only + // primaries as Windows. + label: configuration.resolvePrimaryLabel, configResolve: configuration.resolvePrimary, // Window creation errors propagating out of handleBackendReady must // not block the readiness callback (that would prevent restartAttempt diff --git a/apps/desktop/src/ipc/methods/window.ts b/apps/desktop/src/ipc/methods/window.ts index 429aac8484..7d5ffd0ab9 100644 --- a/apps/desktop/src/ipc/methods/window.ts +++ b/apps/desktop/src/ipc/methods/window.ts @@ -77,7 +77,7 @@ export const getLocalEnvironmentBootstraps = makeSyncIpcMethod({ const { bootstrap, httpBaseUrl } = config.value; bootstraps.push({ id: instance.id, - label: instance.label, + label: yield* instance.label, httpBaseUrl: httpBaseUrl.href, wsBaseUrl: toWebSocketBaseUrl(httpBaseUrl), ...(bootstrap.desktopBootstrapToken diff --git a/apps/desktop/src/updates/DesktopUpdates.test.ts b/apps/desktop/src/updates/DesktopUpdates.test.ts index d73235abf9..b2a3c9d826 100644 --- a/apps/desktop/src/updates/DesktopUpdates.test.ts +++ b/apps/desktop/src/updates/DesktopUpdates.test.ts @@ -103,7 +103,7 @@ function makeHarness(options: UpdatesHarnessOptions = {}) { const stubBackendInstance: DesktopBackendPool.DesktopBackendInstance = { id: DesktopBackendPool.PRIMARY_INSTANCE_ID, - label: "Windows", + label: Effect.succeed("Windows"), start: Effect.void, stop: () => Effect.void, currentConfig: Effect.succeed(Option.none()), diff --git a/apps/desktop/src/wsl/DesktopWslBackend.ts b/apps/desktop/src/wsl/DesktopWslBackend.ts index 78ff56a4f8..104bf6294a 100644 --- a/apps/desktop/src/wsl/DesktopWslBackend.ts +++ b/apps/desktop/src/wsl/DesktopWslBackend.ts @@ -148,7 +148,7 @@ export const layer = Layer.effect( const instance = yield* pool .register({ id: targetId, - label: buildLabel(input.distro), + label: Effect.succeed(buildLabel(input.distro)), configResolve: configuration.resolveWsl({ port: allocatedPort, distro: input.distro }), }) .pipe(