fix(desktop): primary label follows the resolved backend, not just settings

Bugbot (medium): the pool computed primaryLabel once at init purely from
persisted settings (wslOnly && wslBackendEnabled -> "WSL"), but
resolvePrimary now falls back to the Windows config when WSL is
unavailable. So the pool's instance.label could read "WSL" while the
backend actually ran Windows-native, and getLocalEnvironmentBootstraps
surfaced that stale label to the sidebar/env switcher.

Centralize the decision: DesktopBackendConfiguration gains a
resolvePrimaryLabel derived from the same describePrimary helper that
resolvePrimary now uses (wsl-only + WSL actually available, else Windows),
so the label and the start-config can't disagree. The pool consumes
configuration.resolvePrimaryLabel and no longer reads settings directly.
Added label regression tests (available -> WSL distro, unavailable ->
Windows).
This commit is contained in:
Josh Gratton committed 2026-06-01 08:26:36 -04:00
1 parent 3be6625708
commit d729b00b01
3 files changed
+107 -32

No files matched your search

@@ -339,4 +339,69 @@ describe("DesktopBackendConfiguration", () => {
);
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
);
it.effect("resolvePrimaryLabel reports the WSL distro when wsl-only and WSL is available", () =>
Effect.gen(function* () {
const fileSystem = yield* FileSystem.FileSystem;
const baseDir = yield* fileSystem.makeTempDirectoryScoped({
prefix: "t3-desktop-backend-config-test-",
});
yield* Effect.gen(function* () {
const configuration = yield* DesktopBackendConfiguration.DesktopBackendConfiguration;
const label = yield* configuration.resolvePrimaryLabel;
assert.equal(label, "WSL (Ubuntu)");
}).pipe(
Effect.provide(
DesktopBackendConfiguration.layer.pipe(
Layer.provideMerge(serverExposureLayer),
Layer.provideMerge(
DesktopAppSettings.layerTest({
...DesktopAppSettings.DEFAULT_DESKTOP_SETTINGS,
wslBackendEnabled: true,
wslOnly: true,
wslDistro: "Ubuntu",
}),
),
Layer.provideMerge(DesktopWslEnvironment.layerTest({ isAvailable: true })),
Layer.provideMerge(makeEnvironmentLayer(baseDir, { platform: "win32" })),
),
),
);
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
);
it.effect("resolvePrimaryLabel reports Windows when wsl-only but WSL is unavailable", () =>
Effect.gen(function* () {
const fileSystem = yield* FileSystem.FileSystem;
const baseDir = yield* fileSystem.makeTempDirectoryScoped({
prefix: "t3-desktop-backend-config-test-",
});
yield* Effect.gen(function* () {
const configuration = yield* DesktopBackendConfiguration.DesktopBackendConfiguration;
// Mirrors the resolvePrimary fall-back: the label must follow the
// backend that actually resolves, not the persisted preference, so the
// env switcher can't show "WSL" for a Windows backend.
const label = yield* configuration.resolvePrimaryLabel;
assert.equal(label, "Windows");
}).pipe(
Effect.provide(
DesktopBackendConfiguration.layer.pipe(
Layer.provideMerge(serverExposureLayer),
Layer.provideMerge(
DesktopAppSettings.layerTest({
...DesktopAppSettings.DEFAULT_DESKTOP_SETTINGS,
wslBackendEnabled: true,
wslOnly: true,
wslDistro: "Ubuntu",
}),
),
Layer.provideMerge(DesktopWslEnvironment.layerTest({ isAvailable: false })),
Layer.provideMerge(makeEnvironmentLayer(baseDir, { platform: "win32" })),
),
),
);
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
);
});
@@ -38,6 +38,11 @@ export interface DesktopBackendConfigurationShape {
readonly port: number;
readonly distro: string | null;
}) => Effect.Effect<DesktopBackendManager.DesktopBackendStartConfig, PlatformError.PlatformError>;
// The renderer-facing label for the primary instance, derived from the
// same decision resolvePrimary makes (including the WSL-availability
// fall-back to Windows), so the env switcher can't show "WSL" for a
// backend that actually resolved to Windows.
readonly resolvePrimaryLabel: Effect.Effect<string>;
}
export class DesktopBackendConfiguration extends Context.Service<
@@ -501,31 +506,44 @@ export const layer = Layer.effect(
);
});
// Single source of truth for what the primary actually runs as. Both
// the start-config dispatch and the renderer-facing label derive from
// this, so they can't disagree — e.g. the label reading "WSL" while the
// config silently fell back to Windows because WSL is unavailable.
// Dispatch happens at resolve time so toggling wsl-only between restarts
// is picked up on the next start cycle (the pool's primary instance is
// created once at layer init, but configResolve fires on each restart).
const describePrimary = Effect.gen(function* () {
const persistedSettings = yield* settings.get;
const wslRequested = persistedSettings.wslOnly && persistedSettings.wslBackendEnabled;
// Only honor wsl-only when WSL is actually usable. If the user
// persisted wsl-only but WSL has since become unavailable (wsl.exe
// removed, no distro), fall back to the Windows primary instead of
// looping forever on preflight failures: the Connections backend
// control is hidden while WSL is unavailable, so a stuck WSL primary
// would otherwise leave no in-app way back to Windows.
const useWsl = wslRequested && (yield* wslEnvironment.isAvailable);
return { useWsl, wslRequested, distro: persistedSettings.wslDistro };
});
return DesktopBackendConfiguration.of({
resolvePrimary: Effect.gen(function* () {
// 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) {
// Only honor wsl-only when WSL is actually usable. If the user
// persisted wsl-only but WSL has since become unavailable (wsl.exe
// removed, no distro), fall back to the Windows primary instead of
// looping forever on preflight failures: the Connections backend
// control is hidden while WSL is unavailable, so a stuck WSL
// primary would otherwise leave no in-app way back to Windows.
const wslAvailable = yield* wslEnvironment.isAvailable;
if (wslAvailable) {
return yield* buildWslPrimaryConfig;
}
const { useWsl, wslRequested } = yield* describePrimary;
if (useWsl) {
return yield* buildWslPrimaryConfig;
}
if (wslRequested) {
yield* Effect.logWarning(
"WSL-only backend requested but WSL is unavailable; starting the Windows primary instead.",
);
}
return yield* buildWindowsPrimaryConfig;
}).pipe(Effect.withSpan("desktop.backendConfiguration.resolvePrimary")),
resolvePrimaryLabel: Effect.gen(function* () {
const { useWsl, distro } = yield* describePrimary;
if (!useWsl) return "Windows";
return distro ? `WSL (${distro})` : "WSL";
}).pipe(Effect.withSpan("desktop.backendConfiguration.resolvePrimaryLabel")),
resolveWsl: (input) =>
Effect.gen(function* () {
const shared = yield* sharedInputs;
+7 -15
View File
@@ -93,7 +93,6 @@ 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";
@@ -189,7 +188,6 @@ export const layer = Layer.effect(
Effect.gen(function* () {
const configuration = yield* DesktopBackendConfiguration.DesktopBackendConfiguration;
const desktopWindow = yield* DesktopWindow.DesktopWindow;
const settings = yield* DesktopAppSettings.DesktopAppSettings;
// Anchor the pool's lifetime to its layer scope so registered
// instance scopes can be forked off it. Without this, instance
// scopes are orphaned: they only close via explicit unregister()
@@ -205,19 +203,13 @@ export const layer = Layer.effect(
// primary instance uses.
const factoryContext = yield* Effect.context<BackendInstanceFactoryRequirements>();
// 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";
// 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,