fix(desktop): cap WSL preflight failures, surface them, fall back to Windows
A fatal WSL preflight failure (no node, wrong version, missing build tools) used to reschedule the backend forever with no cap and no UI, so in wsl-only mode the app sat invisible (no onReady, no window) retrying a setup that can't fix itself. Classify preflight failures as fatal vs transient. Transient ones (WSL cold- starting, wslpath while the VM boots) keep retrying so the backend self-heals. Fatal ones stop after MAX_PREFLIGHT_FAILURE_ATTEMPTS and fire onPreflightFailed. On the primary the pool wires that to show an error dialog and persist Windows mode, so the manager's next restart re-resolves the primary as Windows and a window opens instead of hanging.
This commit is contained in:
1 parent
19c2146453
commit
383d41c1d1
5 files changed
+193
-6
No files matched your search
@@ -164,6 +164,11 @@ interface WslPreflightOutcome {
|
||||
interface WslPreflightFailure {
|
||||
readonly _tag: "Failed";
|
||||
readonly reason: string;
|
||||
// Fatal: the WSL distro is misconfigured (no node, wrong version, missing
|
||||
// build tools) and retrying won't help — surface it and (wsl-only) fall back
|
||||
// to Windows. Non-fatal: transient (WSL not ready yet, wslpath while it
|
||||
// boots) so the manager keeps retrying until it self-heals.
|
||||
readonly fatal: boolean;
|
||||
}
|
||||
|
||||
const runWslPreflight = Effect.fn("desktop.backendConfiguration.wslPreflight")(function* (input: {
|
||||
@@ -181,7 +186,11 @@ const runWslPreflight = Effect.fn("desktop.backendConfiguration.wslPreflight")(f
|
||||
|
||||
const wslAvailable = yield* wslEnv.isAvailable;
|
||||
if (!wslAvailable) {
|
||||
return { _tag: "Failed", reason: "WSL is not available on this system" } as const;
|
||||
return {
|
||||
_tag: "Failed",
|
||||
reason: "WSL is not available on this system",
|
||||
fatal: false,
|
||||
} as const;
|
||||
}
|
||||
|
||||
const entryExists = yield* fileSystem
|
||||
@@ -191,6 +200,7 @@ const runWslPreflight = Effect.fn("desktop.backendConfiguration.wslPreflight")(f
|
||||
return {
|
||||
_tag: "Failed",
|
||||
reason: `missing server entry at ${input.windowsEntryPath}`,
|
||||
fatal: false,
|
||||
} as const;
|
||||
}
|
||||
|
||||
@@ -199,6 +209,7 @@ const runWslPreflight = Effect.fn("desktop.backendConfiguration.wslPreflight")(f
|
||||
return {
|
||||
_tag: "Failed",
|
||||
reason: `wslpath conversion failed for ${input.windowsEntryPath}`,
|
||||
fatal: false,
|
||||
} as const;
|
||||
}
|
||||
|
||||
@@ -210,6 +221,7 @@ const runWslPreflight = Effect.fn("desktop.backendConfiguration.wslPreflight")(f
|
||||
return {
|
||||
_tag: "Failed",
|
||||
reason: `WSL node-pty unavailable: ${nodePtyResult.reason}`,
|
||||
fatal: true,
|
||||
} as const;
|
||||
}
|
||||
|
||||
@@ -427,7 +439,7 @@ const resolveWslStartConfig = Effect.fn("desktop.backendConfiguration.resolveWsl
|
||||
return {
|
||||
...baseConfig,
|
||||
args: [...distroArgs, "--", "node", "--version"],
|
||||
preflightFailure: Option.some(preflight.reason),
|
||||
preflightFailure: Option.some({ reason: preflight.reason, fatal: preflight.fatal }),
|
||||
} satisfies DesktopBackendManager.DesktopBackendStartConfig;
|
||||
}
|
||||
|
||||
|
||||
@@ -108,6 +108,7 @@ interface MakeInstanceInput {
|
||||
readonly backendOutputLog?: Partial<DesktopObservability.DesktopBackendOutputLogShape>;
|
||||
readonly onReady?: Effect.Effect<void>;
|
||||
readonly onShutdown?: Effect.Effect<void>;
|
||||
readonly onPreflightFailed?: (reason: string) => Effect.Effect<void>;
|
||||
readonly config?: DesktopBackendManager.DesktopBackendStartConfig;
|
||||
}
|
||||
|
||||
@@ -139,6 +140,7 @@ function makeTestInstance(input: MakeInstanceInput) {
|
||||
configResolve: Effect.succeed(input.config ?? baseConfig),
|
||||
...(input.onReady ? { onReady: () => input.onReady! } : {}),
|
||||
...(input.onShutdown ? { onShutdown: () => input.onShutdown! } : {}),
|
||||
...(input.onPreflightFailed ? { onPreflightFailed: input.onPreflightFailed } : {}),
|
||||
});
|
||||
|
||||
return instance.pipe(Effect.provide(servicesLayer));
|
||||
@@ -430,7 +432,7 @@ describe("DesktopBackendManager", () => {
|
||||
spawnerLayer,
|
||||
config: {
|
||||
...baseConfig,
|
||||
preflightFailure: Option.some("preflight failed"),
|
||||
preflightFailure: Option.some({ reason: "preflight failed", fatal: false }),
|
||||
},
|
||||
onShutdown: Effect.sync(() => {
|
||||
shutdownCount += 1;
|
||||
@@ -446,6 +448,76 @@ describe("DesktopBackendManager", () => {
|
||||
),
|
||||
);
|
||||
|
||||
it.effect("surfaces a fatal preflight failure once and stops looping after the cap", () =>
|
||||
Effect.scoped(
|
||||
Effect.gen(function* () {
|
||||
const failures: string[] = [];
|
||||
const spawnerLayer = Layer.succeed(
|
||||
ChildProcessSpawner.ChildProcessSpawner,
|
||||
ChildProcessSpawner.make(() => Effect.die("unexpected backend spawn")),
|
||||
);
|
||||
|
||||
const instance = yield* makeTestInstance({
|
||||
spawnerLayer,
|
||||
config: {
|
||||
...baseConfig,
|
||||
preflightFailure: Option.some({ reason: "Node.js not found", fatal: true }),
|
||||
},
|
||||
onPreflightFailed: (reason) =>
|
||||
Effect.sync(() => {
|
||||
failures.push(reason);
|
||||
}),
|
||||
});
|
||||
|
||||
yield* instance.start;
|
||||
assert.deepEqual(failures, []);
|
||||
|
||||
// Five fatal attempts with exponential backoff (500ms, 1s, 2s, 4s) reach
|
||||
// the cap, at which point the failure is surfaced exactly once.
|
||||
yield* TestClock.adjust(Duration.millis(500));
|
||||
yield* TestClock.adjust(Duration.seconds(1));
|
||||
yield* TestClock.adjust(Duration.seconds(2));
|
||||
yield* TestClock.adjust(Duration.seconds(4));
|
||||
assert.deepEqual(failures, ["Node.js not found"]);
|
||||
|
||||
// Past the cap the loop stops and nothing else is surfaced.
|
||||
yield* TestClock.adjust(Duration.seconds(8));
|
||||
yield* TestClock.adjust(Duration.seconds(30));
|
||||
assert.deepEqual(failures, ["Node.js not found"]);
|
||||
}).pipe(Effect.provide(TestClock.layer())),
|
||||
),
|
||||
);
|
||||
|
||||
it.effect("keeps retrying a transient (non-fatal) preflight failure without surfacing", () =>
|
||||
Effect.scoped(
|
||||
Effect.gen(function* () {
|
||||
const failures: string[] = [];
|
||||
const spawnerLayer = Layer.succeed(
|
||||
ChildProcessSpawner.ChildProcessSpawner,
|
||||
ChildProcessSpawner.make(() => Effect.die("unexpected backend spawn")),
|
||||
);
|
||||
|
||||
const instance = yield* makeTestInstance({
|
||||
spawnerLayer,
|
||||
config: {
|
||||
...baseConfig,
|
||||
preflightFailure: Option.some({ reason: "wslpath conversion failed", fatal: false }),
|
||||
},
|
||||
onPreflightFailed: (reason) =>
|
||||
Effect.sync(() => {
|
||||
failures.push(reason);
|
||||
}),
|
||||
});
|
||||
|
||||
yield* instance.start;
|
||||
// Well beyond the fatal cap's worth of time: a transient failure must
|
||||
// keep retrying (self-heal) and never surface.
|
||||
yield* TestClock.adjust(Duration.minutes(2));
|
||||
assert.deepEqual(failures, []);
|
||||
}).pipe(Effect.provide(TestClock.layer())),
|
||||
),
|
||||
);
|
||||
|
||||
it.effect("cancels a scheduled restart when start is requested manually", () =>
|
||||
Effect.scoped(
|
||||
Effect.gen(function* () {
|
||||
|
||||
@@ -53,6 +53,10 @@ import * as DesktopObservability from "../app/DesktopObservability.ts";
|
||||
|
||||
const INITIAL_RESTART_DELAY = Duration.millis(500);
|
||||
const MAX_RESTART_DELAY = Duration.seconds(10);
|
||||
// After this many consecutive *fatal* preflight failures, stop the silent
|
||||
// restart loop and surface the reason via onPreflightFailed. Transient
|
||||
// failures are not counted, so they keep retrying and can still self-heal.
|
||||
const MAX_PREFLIGHT_FAILURE_ATTEMPTS = 5;
|
||||
const DEFAULT_BACKEND_READINESS_TIMEOUT = Duration.minutes(1);
|
||||
const DEFAULT_BACKEND_READINESS_INTERVAL = Duration.millis(100);
|
||||
const DEFAULT_BACKEND_READINESS_REQUEST_TIMEOUT = Duration.seconds(1);
|
||||
@@ -81,7 +85,17 @@ export interface DesktopBackendStartConfig {
|
||||
readonly bootstrapDelivery: DesktopBackendBootstrapDelivery;
|
||||
readonly httpBaseUrl: URL;
|
||||
readonly captureOutput: boolean;
|
||||
readonly preflightFailure: Option.Option<string>;
|
||||
readonly preflightFailure: Option.Option<PreflightFailure>;
|
||||
}
|
||||
|
||||
// A preflight failure records whether it is fatal. Transient failures (WSL
|
||||
// cold-starting, wslpath while the VM boots) keep retrying so the backend can
|
||||
// self-heal; fatal ones (no node, wrong version, missing build tools) are
|
||||
// surfaced via onPreflightFailed and stop the restart loop after
|
||||
// MAX_PREFLIGHT_FAILURE_ATTEMPTS.
|
||||
export interface PreflightFailure {
|
||||
readonly reason: string;
|
||||
readonly fatal: boolean;
|
||||
}
|
||||
|
||||
interface BackendProcessExit {
|
||||
@@ -186,6 +200,11 @@ export interface BackendInstanceSpec {
|
||||
// between "fired onReady" and "currentConfig already advanced".
|
||||
readonly onReady?: (httpBaseUrl: URL) => Effect.Effect<void>;
|
||||
readonly onShutdown?: () => Effect.Effect<void>;
|
||||
// Fired once when a fatal preflight failure has exhausted its retries. The
|
||||
// pool wires this on the primary to surface the reason and, in wsl-only mode,
|
||||
// fall back to the Windows backend so a window can still open instead of the
|
||||
// app silently retrying forever with no window.
|
||||
readonly onPreflightFailed?: (reason: string) => Effect.Effect<void>;
|
||||
}
|
||||
|
||||
interface ActiveBackendRun {
|
||||
@@ -201,6 +220,9 @@ interface BackendManagerState {
|
||||
readonly config: Option.Option<DesktopBackendStartConfig>;
|
||||
readonly active: Option.Option<ActiveBackendRun>;
|
||||
readonly restartAttempt: number;
|
||||
// Consecutive fatal preflight failures, reset on a clean preflight. Drives
|
||||
// the MAX_PREFLIGHT_FAILURE_ATTEMPTS cap; restartAttempt counts all restarts.
|
||||
readonly preflightFailureAttempt: number;
|
||||
readonly restartFiber: Option.Option<Fiber.Fiber<void, never>>;
|
||||
readonly nextRunId: number;
|
||||
}
|
||||
@@ -211,6 +233,7 @@ const initialState: BackendManagerState = {
|
||||
config: Option.none(),
|
||||
active: Option.none(),
|
||||
restartAttempt: 0,
|
||||
preflightFailureAttempt: 0,
|
||||
restartFiber: Option.none(),
|
||||
nextRunId: 1,
|
||||
};
|
||||
@@ -434,10 +457,53 @@ export const makeBackendInstance = Effect.fn("makeBackendInstance")(function* (
|
||||
config: Option.some(config.value),
|
||||
}));
|
||||
|
||||
if (Option.isSome(config.value.preflightFailure)) {
|
||||
yield* scheduleRestart(config.value.preflightFailure.value);
|
||||
const preflightFailure = config.value.preflightFailure;
|
||||
if (Option.isSome(preflightFailure)) {
|
||||
const { reason, fatal } = preflightFailure.value;
|
||||
if (!fatal) {
|
||||
// Transient (WSL cold-starting, wslpath while the VM boots). Keep
|
||||
// retrying so the backend self-heals once WSL is ready; don't count
|
||||
// it toward the fatal cap.
|
||||
yield* scheduleRestart(reason);
|
||||
return;
|
||||
}
|
||||
const attempt = yield* Ref.modify(state, (latest) => {
|
||||
const next = latest.preflightFailureAttempt + 1;
|
||||
return [next, { ...latest, preflightFailureAttempt: next }] as const;
|
||||
});
|
||||
if (attempt > MAX_PREFLIGHT_FAILURE_ATTEMPTS) {
|
||||
// We already surfaced and asked for the Windows fallback, yet we're
|
||||
// still resolving the WSL primary — the fallback didn't take (e.g.
|
||||
// the settings write failed). Stop rather than loop forever.
|
||||
yield* logInstanceError("backend preflight still failing after fallback; stopping", {
|
||||
reason,
|
||||
attempt,
|
||||
});
|
||||
return;
|
||||
}
|
||||
if (attempt === MAX_PREFLIGHT_FAILURE_ATTEMPTS) {
|
||||
// Fatal and out of retries. Surface the reason (onPreflightFailed,
|
||||
// on the primary, shows a dialog and persists Windows mode), then
|
||||
// schedule one more restart so the next resolve picks up the Windows
|
||||
// primary and a window can open.
|
||||
yield* logInstanceError(
|
||||
"backend preflight failed repeatedly; surfacing and falling back",
|
||||
{ reason, attempt },
|
||||
);
|
||||
yield* spec.onPreflightFailed?.(reason) ?? Effect.void;
|
||||
yield* scheduleRestart(reason);
|
||||
return;
|
||||
}
|
||||
yield* scheduleRestart(reason);
|
||||
return;
|
||||
}
|
||||
// Clean preflight — reset the fatal counter so a later failure gets a
|
||||
// fresh allowance.
|
||||
yield* Ref.update(state, (latest) =>
|
||||
latest.preflightFailureAttempt === 0
|
||||
? latest
|
||||
: { ...latest, preflightFailureAttempt: 0 },
|
||||
);
|
||||
|
||||
if (!entryExists) {
|
||||
yield* scheduleRestart(`missing server entry at ${config.value.entryPath}`);
|
||||
|
||||
@@ -9,6 +9,8 @@ import { HttpClient } from "effect/unstable/http";
|
||||
import { ChildProcessSpawner } from "effect/unstable/process";
|
||||
|
||||
import * as DesktopObservability from "../app/DesktopObservability.ts";
|
||||
import * as DesktopAppSettings from "../settings/DesktopAppSettings.ts";
|
||||
import * as ElectronDialog from "../electron/ElectronDialog.ts";
|
||||
import * as DesktopWindow from "../window/DesktopWindow.ts";
|
||||
import * as DesktopBackendConfiguration from "./DesktopBackendConfiguration.ts";
|
||||
import * as DesktopBackendPool from "./DesktopBackendPool.ts";
|
||||
@@ -63,6 +65,8 @@ function makePoolLayer(
|
||||
resolvePrimaryLabel: Ref.get(labelRef),
|
||||
resolveWsl: () => Effect.die("unexpected WSL config resolve"),
|
||||
} satisfies DesktopBackendConfiguration.DesktopBackendConfigurationShape),
|
||||
DesktopAppSettings.layerTest(),
|
||||
ElectronDialog.layer,
|
||||
Layer.succeed(DesktopWindow.DesktopWindow, {
|
||||
createMain: Effect.die("unexpected window create"),
|
||||
ensureMain: Effect.die("unexpected window ensure"),
|
||||
|
||||
@@ -95,7 +95,9 @@ import { ChildProcessSpawner } from "effect/unstable/process";
|
||||
import * as DesktopBackendConfiguration from "./DesktopBackendConfiguration.ts";
|
||||
import * as DesktopBackendManager from "./DesktopBackendManager.ts";
|
||||
import * as DesktopObservability from "../app/DesktopObservability.ts";
|
||||
import * as DesktopAppSettings from "../settings/DesktopAppSettings.ts";
|
||||
import * as DesktopWindow from "../window/DesktopWindow.ts";
|
||||
import * as ElectronDialog from "../electron/ElectronDialog.ts";
|
||||
|
||||
const { logWarning: logBackendPoolWarning } =
|
||||
DesktopObservability.makeComponentLogger("desktop-backend-pool");
|
||||
@@ -188,6 +190,8 @@ export const layer = Layer.effect(
|
||||
Effect.gen(function* () {
|
||||
const configuration = yield* DesktopBackendConfiguration.DesktopBackendConfiguration;
|
||||
const desktopWindow = yield* DesktopWindow.DesktopWindow;
|
||||
const electronDialog = yield* ElectronDialog.ElectronDialog;
|
||||
const appSettings = 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()
|
||||
@@ -203,6 +207,34 @@ export const layer = Layer.effect(
|
||||
// primary instance uses.
|
||||
const factoryContext = yield* Effect.context<BackendInstanceFactoryRequirements>();
|
||||
|
||||
// A fatal WSL preflight failure on the *primary* only happens in wsl-only
|
||||
// mode (the primary is Windows otherwise, and Windows has no preflight).
|
||||
// Surface the reason and fall back to the Windows backend so a window can
|
||||
// still open instead of the app sitting invisible, retrying a node setup
|
||||
// that won't fix itself. The user re-enables "Run in WSL only" from Settings
|
||||
// once the distro is fixed.
|
||||
const handlePrimaryPreflightFailure = Effect.fn(
|
||||
"desktop.backendPool.primaryPreflightFailed",
|
||||
)(function* (reason: string) {
|
||||
yield* logBackendPoolWarning("primary WSL preflight failed; falling back to Windows", {
|
||||
reason,
|
||||
});
|
||||
yield* electronDialog.showErrorBox(
|
||||
"WSL backend couldn't start",
|
||||
`${reason}\n\nFalling back to the Windows backend so T3 Code can open. Re-enable "Run in WSL only" from Settings > Connections once the WSL distro is fixed.`,
|
||||
);
|
||||
// Persist Windows mode so the manager's next restart re-resolves the
|
||||
// primary as Windows (describePrimary reads wslOnly) and a window opens.
|
||||
// Swallow a write failure — we still logged and surfaced the reason.
|
||||
yield* appSettings.setWslOnly(false).pipe(
|
||||
Effect.catch((error) =>
|
||||
logBackendPoolWarning("failed to persist Windows fallback after WSL preflight failure", {
|
||||
error: error.message,
|
||||
}),
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
const primary = yield* DesktopBackendManager.makeBackendInstance({
|
||||
id: DesktopBackendManager.PRIMARY_INSTANCE_ID,
|
||||
// Keep this lazy. The pool layer is initialized before startup loads
|
||||
@@ -226,6 +258,7 @@ export const layer = Layer.effect(
|
||||
),
|
||||
),
|
||||
onShutdown: () => desktopWindow.handleBackendNotReady,
|
||||
onPreflightFailed: handlePrimaryPreflightFailure,
|
||||
});
|
||||
|
||||
const instancesRef = yield* SynchronizedRef.make<
|
||||
|
||||
Reference in new issue
Block a user