diff --git a/apps/desktop/src/backend/DesktopBackendManager.ts b/apps/desktop/src/backend/DesktopBackendManager.ts index 84ccefd00a..610e9c03f3 100644 --- a/apps/desktop/src/backend/DesktopBackendManager.ts +++ b/apps/desktop/src/backend/DesktopBackendManager.ts @@ -176,7 +176,12 @@ export interface BackendInstanceSpec { readonly id: BackendInstanceId; readonly label: string; readonly configResolve: Effect.Effect; - readonly onReady?: () => Effect.Effect; + // Receives the *resolved* httpBaseUrl of the run that just became + // ready. The window service uses this to decide what URL to load + // (the WSL backend reports its distro IP, the Windows backend reports + // 127.0.0.1). Splitting this off from configResolve avoids races + // between "fired onReady" and "currentConfig already advanced". + readonly onReady?: (httpBaseUrl: URL) => Effect.Effect; readonly onShutdown?: () => Effect.Effect; } @@ -533,7 +538,7 @@ export const makeBackendInstance = Effect.fn("makeBackendInstance")(function* ( return; } - yield* spec.onReady?.() ?? Effect.void; + yield* spec.onReady?.(config.httpBaseUrl) ?? Effect.void; }), onReadinessFailure: (error) => logInstanceWarning("backend readiness check failed during bootstrap", { diff --git a/apps/desktop/src/backend/DesktopBackendPool.ts b/apps/desktop/src/backend/DesktopBackendPool.ts index cb9e941601..7b6d591d18 100644 --- a/apps/desktop/src/backend/DesktopBackendPool.ts +++ b/apps/desktop/src/backend/DesktopBackendPool.ts @@ -216,7 +216,8 @@ export const layer = Layer.effect( // 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)), + onReady: (httpBaseUrl) => + desktopWindow.handleBackendReady(httpBaseUrl).pipe(Effect.catch(() => Effect.void)), onShutdown: () => desktopWindow.handleBackendNotReady, }); diff --git a/apps/desktop/src/window/DesktopApplicationMenu.test.ts b/apps/desktop/src/window/DesktopApplicationMenu.test.ts index 3d8532f731..ec881f03df 100644 --- a/apps/desktop/src/window/DesktopApplicationMenu.test.ts +++ b/apps/desktop/src/window/DesktopApplicationMenu.test.ts @@ -70,7 +70,7 @@ const makeDesktopWindowLayer = (selectedAction: Deferred.Deferred) => revealOrCreateMain: Effect.die("unexpected revealOrCreateMain"), activate: Effect.void, createMainIfBackendReady: Effect.void, - handleBackendReady: Effect.void, + handleBackendReady: () => Effect.void, handleBackendNotReady: Effect.void, dispatchMenuAction: (action) => Deferred.succeed(selectedAction, action).pipe(Effect.asVoid), syncAppearance: Effect.void, diff --git a/apps/desktop/src/window/DesktopWindow.test.ts b/apps/desktop/src/window/DesktopWindow.test.ts index f2b6b1e9a2..cccd6e0eff 100644 --- a/apps/desktop/src/window/DesktopWindow.test.ts +++ b/apps/desktop/src/window/DesktopWindow.test.ts @@ -168,7 +168,7 @@ describe("DesktopWindow", () => { yield* desktopWindow.activate; assert.equal(yield* Ref.get(createCount), 0); - yield* desktopWindow.handleBackendReady; + yield* desktopWindow.handleBackendReady(new URL("http://127.0.0.1:3773")); assert.equal(yield* Ref.get(createCount), 1); assert.deepEqual(fakeWindow.loadURL.mock.calls[0], ["http://127.0.0.1:5733/"]); assert.equal(fakeWindow.openDevTools.mock.calls.length, 1); diff --git a/apps/desktop/src/window/DesktopWindow.ts b/apps/desktop/src/window/DesktopWindow.ts index 8d40b8cb8e..81cb2aac17 100644 --- a/apps/desktop/src/window/DesktopWindow.ts +++ b/apps/desktop/src/window/DesktopWindow.ts @@ -54,7 +54,14 @@ export interface DesktopWindowShape { readonly revealOrCreateMain: Effect.Effect; readonly activate: Effect.Effect; readonly createMainIfBackendReady: Effect.Effect; - readonly handleBackendReady: Effect.Effect; + // The pool tells us not just "primary backend is ready" but also + // *where* the renderer should load from. In wsl-only mode that's the + // WSL distro IP (e.g. http://172.27.152.141:3773), not the local + // exposure URL — wslhost localhost forwarding is unreliable enough + // that pointing loadURL at 127.0.0.1 breaks the renderer on hosts + // where the forward isn't set up. The Windows-primary path passes + // the same URL serverExposure would have given us. + readonly handleBackendReady: (httpBaseUrl: URL) => 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 @@ -161,6 +168,11 @@ const make = Effect.gen(function* () { // createMainIfBackendReady, which gates the post-readiness window // open in development and the macOS "activate without windows" path. const backendReadyRef = yield* Ref.make(false); + // Renderer URL the primary backend told us to load. Populated by + // handleBackendReady. createMain prefers this over serverExposure's + // backendConfig because in wsl-only mode the primary doesn't bind on + // the local exposure URL — the WSL backend listens on the distro IP. + const backendHttpUrlRef = yield* Ref.make>(Option.none()); const context = yield* Effect.context(); const runPromise = Effect.runPromiseWith(context); @@ -299,8 +311,11 @@ const make = Effect.gen(function* () { }); const createMain = Effect.gen(function* () { - const backendConfig = yield* serverExposure.backendConfig; - const window = yield* createWindow(backendConfig.httpBaseUrl); + const reportedHttpUrl = yield* Ref.get(backendHttpUrlRef); + const httpUrl = Option.isSome(reportedHttpUrl) + ? reportedHttpUrl.value + : (yield* serverExposure.backendConfig).httpBaseUrl; + const window = yield* createWindow(httpUrl); yield* electronWindow.setMain(window); yield* logWindowInfo("main window created"); return window; @@ -341,14 +356,16 @@ const make = Effect.gen(function* () { } }).pipe(Effect.withSpan("desktop.window.activate")), createMainIfBackendReady, - handleBackendReady: Effect.gen(function* () { + handleBackendReady: Effect.fn("desktop.window.handleBackendReady")(function* (httpBaseUrl) { + yield* Ref.set(backendHttpUrlRef, Option.some(httpBaseUrl)); yield* Ref.set(backendReadyRef, true); - yield* logWindowInfo("backend ready", { source: "http" }); + yield* logWindowInfo("backend ready", { source: "http", url: httpBaseUrl.href }); yield* createMainIfBackendReady; - }).pipe(Effect.withSpan("desktop.window.handleBackendReady")), - handleBackendNotReady: Ref.set(backendReadyRef, false).pipe( - Effect.withSpan("desktop.window.handleBackendNotReady"), - ), + }), + handleBackendNotReady: Effect.gen(function* () { + yield* Ref.set(backendReadyRef, false); + yield* Ref.set(backendHttpUrlRef, Option.none()); + }).pipe(Effect.withSpan("desktop.window.handleBackendNotReady")), dispatchMenuAction: Effect.fn("desktop.window.dispatchMenuAction")(function* (action) { yield* Effect.annotateCurrentSpan({ action }); const existingWindow = yield* electronWindow.focusedMainOrFirst;