diff --git a/apps/desktop/src/backend/DesktopBackendConfiguration.test.ts b/apps/desktop/src/backend/DesktopBackendConfiguration.test.ts index 4a781c3f1d..b2063b8f53 100644 --- a/apps/desktop/src/backend/DesktopBackendConfiguration.test.ts +++ b/apps/desktop/src/backend/DesktopBackendConfiguration.test.ts @@ -243,9 +243,14 @@ describe("DesktopBackendConfiguration", () => { assert.equal(config.executablePath, "wsl.exe"); assert.equal(config.bootstrap.port, 5050); - assert.equal(config.bootstrap.host, "127.0.0.1"); + // Binds to 0.0.0.0 inside WSL so the backend is reachable via + // both wslhost-forwarded localhost and the distro's eth0 IP. + assert.equal(config.bootstrap.host, "0.0.0.0"); assert.equal(config.bootstrap.tailscaleServeEnabled, false); - assert.equal(config.httpBaseUrl.href, "http://127.0.0.1:5050/"); + // httpBaseUrl uses the resolved distro IP from the test stub, + // not localhost — the renderer reaches the backend directly to + // avoid relying on wslhost forwarding. + assert.equal(config.httpBaseUrl.href, "http://172.27.0.99:5050/"); assert.equal(config.env.OPENAI_API_KEY, "openai-key"); assert.equal(config.env.ANTHROPIC_API_KEY, "anthropic-key"); assert.equal( @@ -261,6 +266,7 @@ describe("DesktopBackendConfiguration", () => { DesktopWslEnvironment.layerTest({ isAvailable: true, windowsToWslPath: () => Option.some("/mnt/c/repo/apps/server/src/index.ts"), + getDistroIp: () => Option.some("172.27.0.99"), }), ), Layer.provideMerge(makeEnvironmentLayer(baseDir, { platform: "win32" })), diff --git a/apps/desktop/src/backend/DesktopBackendConfiguration.ts b/apps/desktop/src/backend/DesktopBackendConfiguration.ts index e75be07eaf..559692f0ce 100644 --- a/apps/desktop/src/backend/DesktopBackendConfiguration.ts +++ b/apps/desktop/src/backend/DesktopBackendConfiguration.ts @@ -283,13 +283,28 @@ const resolveWslStartConfig = Effect.fn("desktop.backendConfiguration.resolveWsl | FileSystem.FileSystem > { const environment = yield* DesktopEnvironment.DesktopEnvironment; + const wslEnvironment = yield* DesktopWslEnvironment.DesktopWslEnvironment; - // WSL backend is always loopback-only; the primary owns LAN exposure - // when the user opts in. Hardcode 127.0.0.1 + drop the tailscale flags - // so a second tailscale serve forwarder doesn't try to bind the same - // port from inside WSL. - const wslHost = "127.0.0.1"; - const httpBaseUrl = new URL(`http://${wslHost}:${input.port}`); + // Bind to 0.0.0.0 inside WSL so the backend is reachable both via + // WSL2's automatic localhost forwarding (wslhost: Windows 127.0.0.1 + // -> WSL 127.0.0.1) AND via the distro's eth0 IP directly from + // Windows. wslhost forwarding is unreliable on some Windows hosts: + // the desktop's readiness probe and the renderer's saved-env-style + // fetch both saw "Failed to fetch" when the backend only bound to + // 127.0.0.1 inside WSL. Binding to 0.0.0.0 plus advertising the + // WSL IP as the renderer-visible URL avoids that dependency. + // Security-wise this is acceptable for the local-only WSL backend: + // the network it exposes on is the WSL-vEthernet network, not the + // LAN; the primary owns LAN exposure when the user opts in. + const wslBindHost = "0.0.0.0"; + + // Resolve the WSL distro's IPv4 address (eth0). Falls back to + // 127.0.0.1 + wslhost forwarding when the IP probe fails: that + // gives us the same behavior as before this change, so a missing + // WSL setup doesn't regress instead of just degrading. + const distroIp = yield* wslEnvironment.getDistroIp(input.distro); + const rendererHost = Option.getOrElse(distroIp, () => "127.0.0.1"); + const httpBaseUrl = new URL(`http://${rendererHost}:${input.port}`); const bootstrap = { mode: "desktop" as const, @@ -298,7 +313,7 @@ const resolveWslStartConfig = Effect.fn("desktop.backendConfiguration.resolveWsl // Omit t3Home so the Linux backend uses its own home dir instead of // the Windows-side baseDir (which would be a /mnt/c path and share // the SQLite file with the primary). - host: wslHost, + host: wslBindHost, desktopBootstrapToken: input.bootstrapToken, // PortSchema rejects 0, so when tailscale serve is disabled we still // need a valid number in this slot. The backend reads tailscaleServePort diff --git a/apps/desktop/src/wsl/DesktopWslEnvironment.ts b/apps/desktop/src/wsl/DesktopWslEnvironment.ts index a501b969f3..924f0a6720 100644 --- a/apps/desktop/src/wsl/DesktopWslEnvironment.ts +++ b/apps/desktop/src/wsl/DesktopWslEnvironment.ts @@ -42,6 +42,13 @@ export interface DesktopWslEnvironmentShape { // Resolves the user's Linux home dir inside the chosen distro (e.g. // "/home/josh"). Used by the folder picker to expand `~` correctly. readonly getUserHome: (distro: string | null) => Effect.Effect>; + // Resolves the WSL distro's IPv4 address on the WSL vEthernet adapter + // (e.g. "172.x.x.x"). The orchestrator uses this for the WSL backend's + // httpBaseUrl so the renderer can reach it without relying on wslhost's + // localhost→WSL automatic forwarding, which is flaky in practice + // (the backend can be listening for 30+ seconds before wslhost starts + // forwarding 127.0.0.1:port to WSL-side localhost). + readonly getDistroIp: (distro: string | null) => Effect.Effect>; readonly ensureNodePty: ( distro: string | null, windowsRepoRoot: string, @@ -379,6 +386,43 @@ const windowsToWslPathImpl = ( ); }; +const IPV4_PATTERN = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/; + +const getDistroIpImpl = ( + distro: string | null, +): Effect.Effect, never, ChildProcessSpawner.ChildProcessSpawner> => + Effect.scoped( + Effect.gen(function* () { + const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; + // `hostname -I` prints a space-separated list of all non-loopback + // IPs the distro has bound. The first entry on the WSL2 default + // network is always the eth0 vEthernet address Windows can reach + // directly (no wslhost forwarding required). + const command = ChildProcess.make( + "wsl.exe", + [...buildDistroArgs(distro), "--", "sh", "-c", "hostname -I"], + { + stdin: "ignore", + stdout: "pipe", + stderr: "ignore", + killSignal: "SIGTERM", + forceKillAfter: PROCESS_TERMINATE_GRACE, + }, + ); + const handle = yield* spawner.spawn(command); + const stdoutBytes = yield* Stream.runCollect(handle.stdout); + const exitCode = yield* handle.exitCode; + if ((exitCode as unknown as number) !== 0) return Option.none(); + const raw = decodeUtf8(concatChunks(stdoutBytes)).trim(); + const candidate = raw.split(/\s+/).find((part) => IPV4_PATTERN.test(part)); + return candidate ? Option.some(candidate) : Option.none(); + }), + ).pipe( + Effect.timeoutOption(USER_HOME_TIMEOUT), + Effect.map(Option.flatten), + Effect.catch(() => Effect.succeed(Option.none())), + ); + const getUserHomeImpl = ( distro: string | null, ): Effect.Effect, never, ChildProcessSpawner.ChildProcessSpawner> => @@ -434,6 +478,7 @@ export interface DesktopWslEnvironmentTestStub { readonly distros?: ReadonlyArray; readonly windowsToWslPath?: (distro: string | null, windowsPath: string) => Option.Option; readonly getUserHome?: (distro: string | null) => Option.Option; + readonly getDistroIp?: (distro: string | null) => Option.Option; readonly ensureNodePty?: ( distro: string | null, windowsRepoRoot: string, @@ -451,6 +496,7 @@ export const layerTest = (stub: DesktopWslEnvironmentTestStub = {}) => windowsToWslPath: (distro, windowsPath) => Effect.succeed(stub.windowsToWslPath?.(distro, windowsPath) ?? Option.none()), getUserHome: (distro) => Effect.succeed(stub.getUserHome?.(distro) ?? Option.none()), + getDistroIp: (distro) => Effect.succeed(stub.getDistroIp?.(distro) ?? Option.none()), ensureNodePty: (distro, windowsRepoRoot, options) => Effect.succeed( stub.ensureNodePty?.(distro, windowsRepoRoot, options) ?? { @@ -500,6 +546,21 @@ export const layer = Layer.effect( return resolved; }).pipe(Effect.withSpan("desktop.wsl.getUserHome")); + // Cache the distro IP per distro for the life of the desktop + // process; WSL's eth0 IP only changes on full WSL restarts (which + // also restart the orchestrator's instance), so we don't need to + // re-probe on every config resolve. + const distroIpCache = new Map(); + const getDistroIp = (distro: string | null) => + Effect.gen(function* () { + const key = distro ?? "__default__"; + const cached = distroIpCache.get(key); + if (cached !== undefined) return Option.some(cached); + const resolved = yield* provideSpawner(getDistroIpImpl(distro)); + if (Option.isSome(resolved)) distroIpCache.set(key, resolved.value); + return resolved; + }).pipe(Effect.withSpan("desktop.wsl.getDistroIp")); + return DesktopWslEnvironment.of({ isAvailable, listDistros: provideSpawner(listDistrosImpl).pipe(Effect.withSpan("desktop.wsl.listDistros")), @@ -507,6 +568,7 @@ export const layer = Layer.effect( provideSpawner(preWarmImpl(distro)).pipe(Effect.withSpan("desktop.wsl.preWarm")), windowsToWslPath, getUserHome, + getDistroIp, ensureNodePty: (distro, windowsRepoRoot, options) => provideSpawner(ensureNodePtyImpl(distro, windowsRepoRoot, windowsToWslPath, options)).pipe( Effect.withSpan("desktop.wsl.ensureNodePty"),