diff --git a/apps/desktop/src/backend/DesktopBackendConfiguration.ts b/apps/desktop/src/backend/DesktopBackendConfiguration.ts index 559692f0ce..3ec76f53bf 100644 --- a/apps/desktop/src/backend/DesktopBackendConfiguration.ts +++ b/apps/desktop/src/backend/DesktopBackendConfiguration.ts @@ -13,6 +13,7 @@ import * as DesktopBackendManager from "./DesktopBackendManager.ts"; import * as DesktopEnvironment from "../app/DesktopEnvironment.ts"; import * as DesktopObservability from "../app/DesktopObservability.ts"; import * as DesktopServerExposure from "./DesktopServerExposure.ts"; +import * as DesktopAppSettings from "../settings/DesktopAppSettings.ts"; import * as DesktopWslEnvironment from "../wsl/DesktopWslEnvironment.ts"; export interface DesktopBackendConfigurationShape { @@ -413,6 +414,7 @@ export const layer = Layer.effect( const fileSystem = yield* FileSystem.FileSystem; const serverExposure = yield* DesktopServerExposure.DesktopServerExposure; const wslEnvironment = yield* DesktopWslEnvironment.DesktopWslEnvironment; + const settings = yield* DesktopAppSettings.DesktopAppSettings; const tokenRef = yield* Ref.make(Option.none()); // Both resolvers share the same bootstrap token: the renderer holds a @@ -429,13 +431,47 @@ export const layer = Layer.effect( return { bootstrapToken, observabilitySettings } satisfies SharedBootstrapInput; }); + const buildWslPrimaryConfig = Effect.gen(function* () { + // wsl-only mode pipes the WSL backend through the same port the + // Windows primary would normally take. That way the renderer + // still loads from the local-only endpoint advertised by + // DesktopServerExposure, and primary-aware code paths (cookie + // auth, the env switcher's "primary" id) keep working without + // a parallel "secondary" registration. + const backendExposure = yield* serverExposure.backendConfig; + const persistedSettings = yield* settings.get; + const shared = yield* sharedInputs; + return yield* resolveWslStartConfig({ + ...shared, + port: backendExposure.port, + distro: persistedSettings.wslDistro, + }).pipe( + Effect.provideService(DesktopEnvironment.DesktopEnvironment, environment), + Effect.provideService(DesktopWslEnvironment.DesktopWslEnvironment, wslEnvironment), + Effect.provideService(FileSystem.FileSystem, fileSystem), + ); + }); + + const buildWindowsPrimaryConfig = Effect.gen(function* () { + const shared = yield* sharedInputs; + return yield* resolvePrimaryStartConfig(shared).pipe( + Effect.provideService(DesktopEnvironment.DesktopEnvironment, environment), + Effect.provideService(DesktopServerExposure.DesktopServerExposure, serverExposure), + ); + }); + return DesktopBackendConfiguration.of({ resolvePrimary: Effect.gen(function* () { - const shared = yield* sharedInputs; - return yield* resolvePrimaryStartConfig(shared).pipe( - Effect.provideService(DesktopEnvironment.DesktopEnvironment, environment), - Effect.provideService(DesktopServerExposure.DesktopServerExposure, serverExposure), - ); + // Dispatch on the wsl-only setting at resolve time so the + // user toggling the mode between restarts picks up the new + // path on the next start cycle. The pool's primary + // BackendInstance is created once at layer init, but its + // configResolve fires each time the backend (re)starts. + const persistedSettings = yield* settings.get; + if (persistedSettings.wslOnly && persistedSettings.wslBackendEnabled) { + return yield* buildWslPrimaryConfig; + } + return yield* buildWindowsPrimaryConfig; }).pipe(Effect.withSpan("desktop.backendConfiguration.resolvePrimary")), resolveWsl: (input) => Effect.gen(function* () { diff --git a/apps/desktop/src/backend/DesktopBackendPool.ts b/apps/desktop/src/backend/DesktopBackendPool.ts index 029e5487b7..cb9e941601 100644 --- a/apps/desktop/src/backend/DesktopBackendPool.ts +++ b/apps/desktop/src/backend/DesktopBackendPool.ts @@ -93,6 +93,7 @@ import { HttpClient } from "effect/unstable/http"; import { ChildProcessSpawner } from "effect/unstable/process"; import * as DesktopBackendConfiguration from "./DesktopBackendConfiguration.ts"; +import * as DesktopAppSettings from "../settings/DesktopAppSettings.ts"; import * as DesktopBackendManager from "./DesktopBackendManager.ts"; import * as DesktopObservability from "../app/DesktopObservability.ts"; import * as DesktopWindow from "../window/DesktopWindow.ts"; @@ -185,6 +186,7 @@ export const layer = Layer.effect( Effect.gen(function* () { const configuration = yield* DesktopBackendConfiguration.DesktopBackendConfiguration; const desktopWindow = yield* DesktopWindow.DesktopWindow; + const settings = yield* DesktopAppSettings.DesktopAppSettings; // Capture the services needed to build any future instance from the // pool's layer scope. register() runs `makeBackendInstance` against // a fresh child scope but reuses these services so the instance gets @@ -192,9 +194,23 @@ export const layer = Layer.effect( // primary instance uses. const factoryContext = yield* Effect.context(); + // Primary label is captured once per process. When wsl-only mode + // is active, the primary's configResolve actually returns the WSL + // backend config (see DesktopBackendConfiguration.resolvePrimary), + // so the label needs to match. We don't try to react to runtime + // setting changes here: the wsl-only switch requires an app + // restart so the label reflects the mode the user is actually in. + const initialSettings = yield* settings.get; + const primaryLabel = + initialSettings.wslOnly && initialSettings.wslBackendEnabled + ? initialSettings.wslDistro + ? `WSL (${initialSettings.wslDistro})` + : "WSL" + : "Windows"; + const primary = yield* DesktopBackendManager.makeBackendInstance({ id: DesktopBackendManager.PRIMARY_INSTANCE_ID, - label: "Windows", + label: primaryLabel, configResolve: configuration.resolvePrimary, // Window creation errors propagating out of handleBackendReady are // swallowed here on purpose: they're logged by the window service diff --git a/apps/desktop/src/ipc/DesktopIpcHandlers.ts b/apps/desktop/src/ipc/DesktopIpcHandlers.ts index fe2ebebc55..8a0a704f1b 100644 --- a/apps/desktop/src/ipc/DesktopIpcHandlers.ts +++ b/apps/desktop/src/ipc/DesktopIpcHandlers.ts @@ -41,7 +41,7 @@ import { setTheme, showContextMenu, } from "./methods/window.ts"; -import { getWslState, setWslBackendEnabled, setWslDistro } from "./methods/wsl.ts"; +import { getWslState, setWslBackendEnabled, setWslDistro, setWslOnly } from "./methods/wsl.ts"; export const installDesktopIpcHandlers = Effect.gen(function* () { const ipc = yield* DesktopIpc.DesktopIpc; @@ -74,6 +74,7 @@ export const installDesktopIpcHandlers = Effect.gen(function* () { yield* ipc.handle(getWslState); yield* ipc.handle(setWslBackendEnabled); yield* ipc.handle(setWslDistro); + yield* ipc.handle(setWslOnly); yield* ipc.handle(pickFolder); yield* ipc.handle(confirm); diff --git a/apps/desktop/src/ipc/channels.ts b/apps/desktop/src/ipc/channels.ts index 9109a04e21..43a9f742dc 100644 --- a/apps/desktop/src/ipc/channels.ts +++ b/apps/desktop/src/ipc/channels.ts @@ -35,4 +35,5 @@ export const GET_ADVERTISED_ENDPOINTS_CHANNEL = "desktop:get-advertised-endpoint export const GET_WSL_STATE_CHANNEL = "desktop:get-wsl-state"; export const SET_WSL_BACKEND_ENABLED_CHANNEL = "desktop:set-wsl-backend-enabled"; export const SET_WSL_DISTRO_CHANNEL = "desktop:set-wsl-distro"; +export const SET_WSL_ONLY_CHANNEL = "desktop:set-wsl-only"; export const SSH_PASSWORD_PROMPT_CANCELLED_RESULT = "ssh-password-prompt-cancelled"; diff --git a/apps/desktop/src/ipc/methods/wsl.ts b/apps/desktop/src/ipc/methods/wsl.ts index 25e3953b41..5135a46919 100644 --- a/apps/desktop/src/ipc/methods/wsl.ts +++ b/apps/desktop/src/ipc/methods/wsl.ts @@ -24,6 +24,7 @@ const readWslState: Effect.Effect< enabled: settings.wslBackendEnabled, distro: settings.wslDistro, available, + wslOnly: settings.wslOnly, distros, }; }); @@ -66,3 +67,19 @@ export const setWslDistro = makeIpcMethod({ return yield* readWslState; }), }); + +export const setWslOnly = makeIpcMethod({ + channel: IpcChannels.SET_WSL_ONLY_CHANNEL, + payload: Schema.Boolean, + result: DesktopWslStateSchema, + handler: Effect.fn("desktop.ipc.wsl.setOnly")(function* (enabled) { + // wsl-only changes the pool's primary spec, which is captured + // once at layer init. Persist the new setting here; the actual + // mode flip lands on the next desktop launch. The renderer + // surfaces "T3 Code will restart" in the confirmation dialog so + // the user knows what to expect. + const appSettings = yield* DesktopAppSettings.DesktopAppSettings; + yield* appSettings.setWslOnly(enabled); + return yield* readWslState; + }), +}); diff --git a/apps/desktop/src/preload.ts b/apps/desktop/src/preload.ts index b47e741cd9..ed47adcc37 100644 --- a/apps/desktop/src/preload.ts +++ b/apps/desktop/src/preload.ts @@ -91,6 +91,7 @@ contextBridge.exposeInMainWorld("desktopBridge", { setWslBackendEnabled: (enabled) => ipcRenderer.invoke(IpcChannels.SET_WSL_BACKEND_ENABLED_CHANNEL, enabled), setWslDistro: (distro) => ipcRenderer.invoke(IpcChannels.SET_WSL_DISTRO_CHANNEL, distro), + setWslOnly: (enabled) => ipcRenderer.invoke(IpcChannels.SET_WSL_ONLY_CHANNEL, enabled), pickFolder: (options) => ipcRenderer.invoke(IpcChannels.PICK_FOLDER_CHANNEL, options), confirm: (message) => ipcRenderer.invoke(IpcChannels.CONFIRM_CHANNEL, message), setTheme: (theme) => ipcRenderer.invoke(IpcChannels.SET_THEME_CHANNEL, theme), diff --git a/apps/desktop/src/settings/DesktopAppSettings.test.ts b/apps/desktop/src/settings/DesktopAppSettings.test.ts index ffcff21582..fee2bd32ef 100644 --- a/apps/desktop/src/settings/DesktopAppSettings.test.ts +++ b/apps/desktop/src/settings/DesktopAppSettings.test.ts @@ -99,6 +99,7 @@ describe("DesktopSettings", () => { updateChannel: "nightly", updateChannelConfiguredByUser: false, wslBackendEnabled: false, + wslOnly: false, wslDistro: null, } satisfies DesktopSettingsValue); }); @@ -122,6 +123,7 @@ describe("DesktopSettings", () => { updateChannel: "latest", updateChannelConfiguredByUser: true, wslBackendEnabled: false, + wslOnly: false, wslDistro: null, } satisfies DesktopSettingsValue); @@ -203,6 +205,7 @@ describe("DesktopSettings", () => { updateChannel: "latest", updateChannelConfiguredByUser: false, wslBackendEnabled: false, + wslOnly: false, wslDistro: null, } satisfies DesktopSettingsValue); }), @@ -244,6 +247,7 @@ describe("DesktopSettings", () => { updateChannel: "nightly", updateChannelConfiguredByUser: false, wslBackendEnabled: false, + wslOnly: false, wslDistro: null, } satisfies DesktopSettingsValue); }), @@ -268,6 +272,7 @@ describe("DesktopSettings", () => { updateChannel: "latest", updateChannelConfiguredByUser: true, wslBackendEnabled: false, + wslOnly: false, wslDistro: null, } satisfies DesktopSettingsValue); }), @@ -291,6 +296,7 @@ describe("DesktopSettings", () => { updateChannel: "latest", updateChannelConfiguredByUser: false, wslBackendEnabled: false, + wslOnly: false, wslDistro: null, } satisfies DesktopSettingsValue); }), diff --git a/apps/desktop/src/settings/DesktopAppSettings.ts b/apps/desktop/src/settings/DesktopAppSettings.ts index f1f2d290d9..c40c22ffba 100644 --- a/apps/desktop/src/settings/DesktopAppSettings.ts +++ b/apps/desktop/src/settings/DesktopAppSettings.ts @@ -34,6 +34,14 @@ export interface DesktopSettings { // value are migrated to `wslBackendEnabled: true` on load. readonly wslBackendEnabled: boolean; readonly wslDistro: string | null; + // When true (and wslBackendEnabled is also true) the desktop runs only + // the WSL backend as the primary, and the Windows-side Node backend is + // not started. Designed for users who develop entirely inside WSL and + // don't want a second backend process running. Defaults to false so + // existing setups stay on the parallel-backends behavior. Changing + // this requires a desktop restart because the pool's primary spec is + // chosen once at layer init. + readonly wslOnly: boolean; } export interface DesktopSettingsChange { @@ -51,6 +59,7 @@ export const DEFAULT_DESKTOP_SETTINGS: DesktopSettings = { updateChannelConfiguredByUser: false, wslBackendEnabled: false, wslDistro: null, + wslOnly: false, }; const DesktopSettingsDocument = Schema.Struct({ @@ -65,6 +74,7 @@ const DesktopSettingsDocument = Schema.Struct({ wslBackendEnabled: Schema.optionalKey(Schema.Boolean), wslMode: Schema.optionalKey(Schema.Literals(["local", "wsl"])), wslDistro: Schema.optionalKey(Schema.NullOr(Schema.String)), + wslOnly: Schema.optionalKey(Schema.Boolean), }); type DesktopSettingsDocument = typeof DesktopSettingsDocument.Type; @@ -106,6 +116,9 @@ export interface DesktopAppSettingsShape { readonly setWslDistro: ( distro: string | null, ) => Effect.Effect; + readonly setWslOnly: ( + enabled: boolean, + ) => Effect.Effect; } export class DesktopAppSettings extends Context.Service< @@ -159,6 +172,7 @@ function normalizeDesktopSettingsDocument( updateChannelConfiguredByUser, wslBackendEnabled, wslDistro: normalizeWslDistro(parsed.wslDistro), + wslOnly: parsed.wslOnly === true, }; } @@ -189,6 +203,9 @@ function toDesktopSettingsDocument( if (settings.wslDistro !== defaults.wslDistro) { document.wslDistro = settings.wslDistro; } + if (settings.wslOnly !== defaults.wslOnly) { + document.wslOnly = settings.wslOnly; + } return document; } @@ -254,6 +271,15 @@ function setWslDistro(settings: DesktopSettings, distro: string | null): Desktop }; } +function setWslOnly(settings: DesktopSettings, enabled: boolean): DesktopSettings { + return settings.wslOnly === enabled + ? settings + : { + ...settings, + wslOnly: enabled, + }; +} + function readSettings( fileSystem: FileSystem.FileSystem, settingsPath: string, @@ -355,6 +381,10 @@ export const layer = Layer.effect( attributes: { distro: distro ?? null }, }), ), + setWslOnly: (enabled) => + persist((settings) => setWslOnly(settings, enabled)).pipe( + Effect.withSpan("desktop.settings.setWslOnly", { attributes: { enabled } }), + ), }); }), ); @@ -386,6 +416,7 @@ export const layerTest = (initialSettings: DesktopSettings = DEFAULT_DESKTOP_SET setWslBackendEnabled: (enabled) => update((settings) => setWslBackendEnabled(settings, enabled)), setWslDistro: (distro) => update((settings) => setWslDistro(settings, distro)), + setWslOnly: (enabled) => update((settings) => setWslOnly(settings, enabled)), }); }), ); diff --git a/apps/desktop/src/wsl/DesktopWslBackend.ts b/apps/desktop/src/wsl/DesktopWslBackend.ts index fe4a19714f..e6b3f2cdfa 100644 --- a/apps/desktop/src/wsl/DesktopWslBackend.ts +++ b/apps/desktop/src/wsl/DesktopWslBackend.ts @@ -161,7 +161,12 @@ export const layer = Layer.effect( const existing = yield* findExistingWslInstance; const existingId = Option.map(existing, (instance) => instance.id); - const shouldRun = settings.wslBackendEnabled && available; + // In wsl-only mode the pool's primary IS the WSL backend (see + // DesktopBackendConfiguration.resolvePrimary), so the + // orchestrator skips registering a parallel "wsl:" + // secondary. Without this skip we'd spin up two WSL processes + // on the same distro for users who explicitly asked for one. + const shouldRun = settings.wslBackendEnabled && available && !settings.wslOnly; const targetId = shouldRun ? Option.some(resolveTargetInstanceId(settings.wslDistro)) : Option.none(); diff --git a/apps/web/src/components/settings/ConnectionsSettings.tsx b/apps/web/src/components/settings/ConnectionsSettings.tsx index 13b0c2b229..77d2c92ecd 100644 --- a/apps/web/src/components/settings/ConnectionsSettings.tsx +++ b/apps/web/src/components/settings/ConnectionsSettings.tsx @@ -1505,13 +1505,15 @@ export function ConnectionsSettings() { const [isUpdatingWslBackend, setIsUpdatingWslBackend] = useState(false); const [desktopWslError, setDesktopWslError] = useState(null); // Pending WSL setting change waiting on user confirmation. Set when - // the user tries a destructive change (disable, switch distro) while - // the WSL backend has saved-env state on this machine. Confirming - // applies the change; cancelling drops it without touching the - // persisted setting. Null when nothing is pending. + // the user tries a destructive change (disable, switch distro, + // toggle wsl-only) while the WSL backend has saved-env state on this + // machine. Confirming applies the change; cancelling drops it + // without touching the persisted setting. Null when nothing is + // pending. type PendingWslChange = | { readonly kind: "disable" } - | { readonly kind: "distro"; readonly nextDistro: string | null }; + | { readonly kind: "distro"; readonly nextDistro: string | null } + | { readonly kind: "wsl-only"; readonly nextValue: boolean }; const [pendingWslChange, setPendingWslChange] = useState(null); const isWslConfirmDialogOpen = pendingWslChange !== null; const [pendingTailscaleServeEndpoint, setPendingTailscaleServeEndpoint] = @@ -2459,6 +2461,20 @@ export function ConnectionsSettings() { [applyWslSettingChange, desktopBridge, desktopWslState, hasWslRegistrationToLose], ); + const handleToggleWslOnly = useCallback( + (enabled: boolean) => { + if (!desktopBridge || !desktopWslState || desktopWslState.wslOnly === enabled) return; + // wsl-only changes which backend the pool uses as "primary", + // which is decided once at app launch. The desktop side persists + // the setting immediately but doesn't tear down or restart + // anything itself; the renderer warns the user to expect a + // restart and (in a follow-up) can trigger it automatically. + // Always prompt — even enabling is consequential here. + setPendingWslChange({ kind: "wsl-only", nextValue: enabled }); + }, + [desktopBridge, desktopWslState], + ); + const handleConfirmWslChange = useCallback(() => { if (!desktopBridge || !pendingWslChange) return; const change = pendingWslChange; @@ -2467,7 +2483,11 @@ export function ConnectionsSettings() { void applyWslSettingChange(() => desktopBridge.setWslBackendEnabled(false)); return; } - void applyWslSettingChange(() => desktopBridge.setWslDistro(change.nextDistro)); + if (change.kind === "distro") { + void applyWslSettingChange(() => desktopBridge.setWslDistro(change.nextDistro)); + return; + } + void applyWslSettingChange(() => desktopBridge.setWslOnly(change.nextValue)); }, [applyWslSettingChange, desktopBridge, pendingWslChange]); const renderWslRow = () => { @@ -2540,6 +2560,20 @@ export function ConnectionsSettings() { } /> ) : null} + {desktopWslState.enabled ? ( + handleToggleWslOnly(checked)} + aria-label="Run WSL only" + /> + } + /> + ) : null} ); }; @@ -2765,12 +2799,20 @@ export function ConnectionsSettings() { {pendingWslChange?.kind === "disable" ? "Disable WSL backend?" - : "Switch WSL distro?"} + : pendingWslChange?.kind === "distro" + ? "Switch WSL distro?" + : pendingWslChange?.nextValue + ? "Run only the WSL backend?" + : "Re-enable the Windows backend?"} {pendingWslChange?.kind === "disable" ? "The WSL backend will stop. Threads and projects opened against WSL stay safe inside the distro, but they'll be unavailable in T3 Code until you re-enable WSL." - : "T3 Code will restart the WSL backend on the new distro. Sessions still running on the current distro will be interrupted."} + : pendingWslChange?.kind === "distro" + ? "T3 Code will restart the WSL backend on the new distro. Sessions still running on the current distro will be interrupted." + : pendingWslChange?.nextValue + ? "T3 Code will save this preference and switch to running only the WSL backend on the next app launch. Your Windows-side projects won't be accessible until you turn this off again." + : "T3 Code will save this preference and bring the Windows backend back up alongside WSL on the next app launch."} @@ -2781,7 +2823,12 @@ export function ConnectionsSettings() { Cancel diff --git a/apps/web/src/components/settings/SettingsPanels.browser.tsx b/apps/web/src/components/settings/SettingsPanels.browser.tsx index c98075fd59..071ba75a10 100644 --- a/apps/web/src/components/settings/SettingsPanels.browser.tsx +++ b/apps/web/src/components/settings/SettingsPanels.browser.tsx @@ -451,18 +451,28 @@ const createDesktopBridgeStub = (overrides?: { enabled: false, distro: null, available: false, + wslOnly: false, distros: [], }), setWslBackendEnabled: vi.fn().mockResolvedValue({ enabled: false, distro: null, available: false, + wslOnly: false, distros: [], }), setWslDistro: vi.fn().mockResolvedValue({ enabled: false, distro: null, available: false, + wslOnly: false, + distros: [], + }), + setWslOnly: vi.fn().mockResolvedValue({ + enabled: false, + distro: null, + available: false, + wslOnly: false, distros: [], }), pickFolder: vi.fn().mockResolvedValue(null), diff --git a/apps/web/src/localApi.test.ts b/apps/web/src/localApi.test.ts index 39f3690da4..2e6bd97e5c 100644 --- a/apps/web/src/localApi.test.ts +++ b/apps/web/src/localApi.test.ts @@ -224,18 +224,28 @@ function makeDesktopBridge(overrides: Partial = {}): DesktopBridg enabled: false, distro: null, available: false, + wslOnly: false, distros: [], }), setWslBackendEnabled: async () => ({ enabled: false, distro: null, available: false, + wslOnly: false, distros: [], }), setWslDistro: async () => ({ enabled: false, distro: null, available: false, + wslOnly: false, + distros: [], + }), + setWslOnly: async () => ({ + enabled: false, + distro: null, + available: false, + wslOnly: false, distros: [], }), pickFolder: async () => null, diff --git a/packages/contracts/src/ipc.ts b/packages/contracts/src/ipc.ts index b59fb8e4d8..c461e4da42 100644 --- a/packages/contracts/src/ipc.ts +++ b/packages/contracts/src/ipc.ts @@ -407,6 +407,11 @@ export interface DesktopWslState { // null means "track the current WSL default distro". distro: string | null; available: boolean; + // When true (and `enabled` is also true) the desktop runs only the + // WSL backend as the primary; the Windows-side Node backend is not + // started. Toggling this requires an app restart because the + // primary backend's spec is captured once at layer init. + wslOnly: boolean; distros: readonly DesktopWslDistro[]; } @@ -414,6 +419,7 @@ export const DesktopWslStateSchema = Schema.Struct({ enabled: Schema.Boolean, distro: Schema.NullOr(Schema.String), available: Schema.Boolean, + wslOnly: Schema.Boolean, distros: Schema.Array(DesktopWslDistroSchema), }); @@ -460,6 +466,7 @@ export interface DesktopBridge { getWslState: () => Promise; setWslBackendEnabled: (enabled: boolean) => Promise; setWslDistro: (distro: string | null) => Promise; + setWslOnly: (enabled: boolean) => Promise; pickFolder: (options?: PickFolderOptions) => Promise; confirm: (message: string) => Promise; setTheme: (theme: DesktopTheme) => Promise;