From c61ea48549d23406945dba5152cb6d2925f024cc Mon Sep 17 00:00:00 2001 From: Josh Gratton Date: Mon, 1 Jun 2026 15:41:27 -0400 Subject: [PATCH] Fix WSL picker and backend restart edge cases --- apps/desktop/src/app/DesktopApp.ts | 2 +- apps/desktop/src/ipc/methods/window.ts | 24 +++++++++++++++++-- apps/desktop/src/wsl/DesktopWslEnvironment.ts | 14 +---------- apps/desktop/src/wsl/wslPathParsing.test.ts | 19 +++++++++++++++ apps/desktop/src/wsl/wslPathParsing.ts | 13 ++++++++++ apps/web/src/environments/local/index.ts | 15 ++++++------ apps/web/src/environments/runtime/service.ts | 5 ++-- 7 files changed, 65 insertions(+), 27 deletions(-) diff --git a/apps/desktop/src/app/DesktopApp.ts b/apps/desktop/src/app/DesktopApp.ts index 7c4659c13d..19f96cb5ad 100644 --- a/apps/desktop/src/app/DesktopApp.ts +++ b/apps/desktop/src/app/DesktopApp.ts @@ -235,10 +235,10 @@ const scopedProgram = Effect.scoped( yield* Effect.annotateCurrentSpan({ scope: "desktop", runId }); const shutdown = yield* DesktopLifecycle.DesktopShutdown; - const pool = yield* DesktopBackendPool.DesktopBackendPool; yield* Effect.addFinalizer(() => Effect.gen(function* () { + const pool = yield* DesktopBackendPool.DesktopBackendPool; // Stop every backend in the pool, not just the primary. The // electronApp.quit() path can race ahead of the layer-scope // cascade, so leaving the WSL instance for its parent scope diff --git a/apps/desktop/src/ipc/methods/window.ts b/apps/desktop/src/ipc/methods/window.ts index b50c7f673e..429aac8484 100644 --- a/apps/desktop/src/ipc/methods/window.ts +++ b/apps/desktop/src/ipc/methods/window.ts @@ -23,7 +23,11 @@ import * as ElectronTheme from "../../electron/ElectronTheme.ts"; import * as ElectronWindow from "../../electron/ElectronWindow.ts"; import * as IpcChannels from "../channels.ts"; import { makeIpcMethod, makeSyncIpcMethod } from "../DesktopIpc.ts"; -import { resolveWslPickFolderDefaultPath } from "../../wsl/wslPathParsing.ts"; +import { + extractDistroFromUncPath, + resolveWslPickFolderDefaultPath, + wslUncPathToLinuxPath, +} from "../../wsl/wslPathParsing.ts"; const ContextMenuPosition = Schema.Struct({ x: Schema.Number, @@ -144,7 +148,23 @@ export const pickFolder = makeIpcMethod({ owner: yield* electronWindow.focusedMainOrFirst, defaultPath, }); - return Option.getOrNull(selectedPath); + if (Option.isNone(selectedPath)) { + return null; + } + if (!useWsl) { + return selectedPath.value; + } + + const linuxUncPath = wslUncPathToLinuxPath(selectedPath.value); + if (linuxUncPath !== null) { + return linuxUncPath; + } + + const converted = yield* wslEnvironment.windowsToWslPath( + extractDistroFromUncPath(selectedPath.value) ?? wslDistro, + selectedPath.value, + ); + return Option.getOrElse(converted, () => selectedPath.value); }), }); diff --git a/apps/desktop/src/wsl/DesktopWslEnvironment.ts b/apps/desktop/src/wsl/DesktopWslEnvironment.ts index c81b219807..a44819e97b 100644 --- a/apps/desktop/src/wsl/DesktopWslEnvironment.ts +++ b/apps/desktop/src/wsl/DesktopWslEnvironment.ts @@ -546,20 +546,8 @@ 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")); + provideSpawner(getDistroIpImpl(distro)).pipe(Effect.withSpan("desktop.wsl.getDistroIp")); return DesktopWslEnvironment.of({ isAvailable, diff --git a/apps/desktop/src/wsl/wslPathParsing.test.ts b/apps/desktop/src/wsl/wslPathParsing.test.ts index 732b70da8a..08df540773 100644 --- a/apps/desktop/src/wsl/wslPathParsing.test.ts +++ b/apps/desktop/src/wsl/wslPathParsing.test.ts @@ -7,6 +7,7 @@ import { parseWslDistroList, resolveWslHomeUncPath, resolveWslPickFolderDefaultPath, + wslUncPathToLinuxPath, } from "./wslPathParsing.ts"; function makeUtf16LeBuffer(text: string): Buffer { @@ -97,6 +98,24 @@ describe("extractDistroFromUncPath", () => { }); }); +describe("wslUncPathToLinuxPath", () => { + it("maps WSL UNC paths back to Linux absolute paths", () => { + expect(wslUncPathToLinuxPath("\\\\wsl.localhost\\Ubuntu-22.04\\home\\josh\\repo")).toBe( + "/home/josh/repo", + ); + }); + + it("maps a distro UNC root to Linux root", () => { + expect(wslUncPathToLinuxPath("\\\\wsl$\\Debian")).toBe("/"); + expect(wslUncPathToLinuxPath("\\\\wsl.localhost\\Debian\\")).toBe("/"); + }); + + it("rejects invalid distro names and non-WSL paths", () => { + expect(wslUncPathToLinuxPath("\\\\wsl.localhost\\bad!name\\home")).toBeNull(); + expect(wslUncPathToLinuxPath("C:\\Users\\Josh\\repo")).toBeNull(); + }); +}); + describe("resolveWslHomeUncPath", () => { const distros = [ { name: "Debian", isDefault: true, version: 2 as const }, diff --git a/apps/desktop/src/wsl/wslPathParsing.ts b/apps/desktop/src/wsl/wslPathParsing.ts index 3a06e11e4a..edbab81f6d 100644 --- a/apps/desktop/src/wsl/wslPathParsing.ts +++ b/apps/desktop/src/wsl/wslPathParsing.ts @@ -48,6 +48,19 @@ export function extractDistroFromUncPath(windowsPath: string): string | null { return DISTRO_NAME_PATTERN.test(candidate) ? candidate : null; } +export function wslUncPathToLinuxPath(windowsPath: string): string | null { + const match = /^\\\\(?:wsl\.localhost|wsl\$)\\([^\\]+)(?:\\(.*))?$/i.exec(windowsPath.trim()); + if (!match) return null; + + const distro = match[1]!; + if (!DISTRO_NAME_PATTERN.test(distro)) return null; + + const rest = match[2] ?? ""; + if (rest.length === 0) return "/"; + + return `/${rest.split("\\").filter(Boolean).join("/")}`; +} + export function resolveWslHomeUncPath( config: WslConfig, distros: readonly WslDistro[], diff --git a/apps/web/src/environments/local/index.ts b/apps/web/src/environments/local/index.ts index bb31fcc7c9..9106f598ea 100644 --- a/apps/web/src/environments/local/index.ts +++ b/apps/web/src/environments/local/index.ts @@ -63,18 +63,18 @@ let pendingReconcileRun: Promise | null = null; // Backoff schedule for the auto-retry loop. WSL cold boot routinely // takes 30-60 seconds (distro spin-up + node-pty preflight + node -// startup + migrations), and the backend's desktop-bootstrap grant -// has a 5-minute TTL after seeding. This schedule comfortably covers -// the cold-boot window while leaving headroom inside the TTL. +// startup + migrations). The backend's desktop-bootstrap grant lives +// for the backend process, so this budget is about user feedback rather +// than credential expiry. const AUTO_RETRY_DELAYS_MS = [2_000, 4_000, 8_000, 16_000, 30_000, 45_000, 60_000, 60_000] as const; let autoRetryHandle: ReturnType | null = null; let autoRetryAttempt = 0; // Latched once we've confirmed the user has no WSL backend configured // on this host. The auto-retry loop polls getLocalEnvironmentBootstraps -// every few seconds for up to ~4 minutes (the desktop-bootstrap TTL -// window); on machines where the user will never enable WSL that's -// just wasted IPC. setWslBackendEnabled IPC handlers in the settings +// every few seconds for a bounded window; on machines where the user +// will never enable WSL that's just wasted IPC. setWslBackendEnabled +// IPC handlers in the settings // page already call reconcile with resetBudget: true, so flipping the // switch on later resumes the loop. We default to false (keep // retrying) so any pre-existing WSL setup still gets retried during @@ -474,8 +474,7 @@ function runReconcile(options: { readonly resetBudget: boolean }): Promise // concurrent calls share a single underlying reconcile pass. When a // secondary's registration fails (typical cause: WSL backend still // cold-booting), an internal backoff loop keeps retrying until either -// the secondary lands in the registry or the desktop-bootstrap TTL -// runs out and we give up. +// the secondary lands in the registry or the retry budget runs out. export function reconcileLocalSecondaryEnvironments(): Promise { return runReconcile({ resetBudget: true }); } diff --git a/apps/web/src/environments/runtime/service.ts b/apps/web/src/environments/runtime/service.ts index 666b286d1e..e2e33e605d 100644 --- a/apps/web/src/environments/runtime/service.ts +++ b/apps/web/src/environments/runtime/service.ts @@ -1819,10 +1819,9 @@ export function startEnvironmentConnectionService(queryClient: QueryClient): () // Bring up the desktop-managed secondary local environments (today // just the WSL backend, if enabled). The reconcile call owns its - // own retry loop: WSL cold boot routinely takes 30-60 seconds, and - // the desktop-bootstrap grant on each backend has a 5-minute TTL. + // own retry loop: WSL cold boot routinely takes 30-60 seconds. // reconcileLocalSecondaryEnvironments() backs off internally until - // every secondary lands or the TTL window closes. + // every secondary lands or the retry budget closes. void reconcileLocalSecondaryEnvironments(); const unsubscribeSavedEnvironments = useSavedEnvironmentRegistryStore.subscribe(() => {