Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 33 additions & 22 deletions apps/desktop/src/backend/DesktopBackendConfiguration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,12 @@ describe("DesktopBackendConfiguration", () => {
},
ensureNodePty: (distro) => {
observedDistros.push(distro);
return { ok: true, nodePath: "/usr/bin/node", resolvedPath: "/usr/bin:/bin" };
return {
ok: true,
nodePath: "/usr/bin/node",
resolvedPath: "/usr/bin:/bin",
t3Home: null,
};
},
getDistroIp: (distro) => {
observedDistros.push(distro);
Expand Down Expand Up @@ -385,10 +390,12 @@ describe("DesktopBackendConfiguration", () => {
windowsArchivePath: string;
runtimeId: string;
sha256: string;
t3Home: string | null;
}> = [];
let probeCount = 0;
let legacyCleanupCount = 0;
const linuxAppRoot = "/home/test/.t3/wsl-runtime/1.2.3-x64";
const t3Home = "/home/test/.config/t3";
const linuxAppRoot = `${t3Home}/wsl-runtime/1.2.3-x64`;
const resolvedPath = "/home/test/.local/bin:/usr/bin:/bin";

return withPackagedWslHarness(
Expand All @@ -399,17 +406,18 @@ describe("DesktopBackendConfiguration", () => {
legacyCleanupCount += 1;
}),
wsl: () => ({
prepareRuntime: (_distro, archive) => {
prepareRuntime: (_distro, archive, archiveT3Home) => {
observedArchives.push({
windowsArchivePath: archive.windowsPath,
runtimeId: archive.runtimeId,
sha256: archive.sha256,
t3Home: archiveT3Home,
});
return { ok: true, linuxAppRoot };
},
probeRuntime: () => {
probeCount += 1;
return { ok: true, resolvedPath };
return { ok: true, resolvedPath, t3Home };
},
ensureNodePty: () => {
throw new Error("the staged runtime must not probe for node-pty");
Expand All @@ -427,6 +435,7 @@ describe("DesktopBackendConfiguration", () => {
windowsArchivePath: archivePath,
runtimeId: `sha256-${archiveHash}`,
sha256: archiveHash,
t3Home,
},
]);
assert.equal(probeCount, 1);
Expand All @@ -442,12 +451,14 @@ describe("DesktopBackendConfiguration", () => {
"--exec",
"env",
`PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:${resolvedPath}`,
`T3CODE_HOME=${t3Home}`,
`${linuxAppRoot}/t3`,
"--bootstrap-fd",
"0",
]);
assert.notInclude(config.args, "/usr/bin/node");
assert.equal(config.wslRuntimeId, `sha256-${archiveHash}`);
assert.equal(config.wslT3Home, t3Home);
assert.equal(legacyCleanupCount, 1);
assert.isTrue(Option.isNone(config.preflightFailure));
}),
Expand All @@ -470,6 +481,7 @@ describe("DesktopBackendConfiguration", () => {
ok: true,
nodePath: "/usr/bin/node",
resolvedPath: "/usr/bin:/bin",
t3Home: null,
}),
}),
},
Expand Down Expand Up @@ -502,10 +514,14 @@ describe("DesktopBackendConfiguration", () => {
archiveHash: "b".repeat(64),
wsl: () => ({
prepareRuntime: () => ({ ok: false, reason: "archive is corrupt" }),
invalidateRuntime: () => Effect.die("failed staging must not invalidate a runtime"),
ensureNodePty: (_distro, root) => {
observedNodePtyRoots.push(root);
return { ok: true, nodePath: "/usr/bin/node", resolvedPath: "/usr/bin:/bin" };
return {
ok: true,
nodePath: "/usr/bin/node",
resolvedPath: "/usr/bin:/bin",
t3Home: null,
};
},
}),
},
Expand All @@ -524,28 +540,30 @@ describe("DesktopBackendConfiguration", () => {
);
});

it.effect("resolveWsl retires a staged runtime whose launch probe fails", () => {
it.effect("resolveWsl does not stage a runtime when the login-shell probe fails", () => {
const archiveHash = "c".repeat(64);
const stagedAppRoot = `/home/test/.t3/wsl-runtime/sha256-${archiveHash}`;
let probeCount = 0;
const observedNodePtyRoots: string[] = [];
const invalidatedRuntimeIds: string[] = [];
return withPackagedWslHarness(
{
archiveHash,
wsl: () => ({
prepareRuntime: () => ({ ok: true, linuxAppRoot: stagedAppRoot }),
invalidateRuntime: (_distro, runtimeId) =>
Effect.sync(() => {
invalidatedRuntimeIds.push(runtimeId);
}),
prepareRuntime: () => {
throw new Error("a failed probe must not stage the runtime");
},
probeRuntime: () => {
probeCount += 1;
return { ok: false, reason: "WSL login-shell PATH could not be resolved" };
},
ensureNodePty: (_distro, root) => {
observedNodePtyRoots.push(root);
return { ok: true, nodePath: "/usr/bin/node", resolvedPath: "/usr/bin:/bin" };
return {
ok: true,
nodePath: "/usr/bin/node",
resolvedPath: "/usr/bin:/bin",
t3Home: null,
};
},
}),
},
Expand All @@ -562,23 +580,17 @@ describe("DesktopBackendConfiguration", () => {
assert.equal(config.entryPath, mountedEntryPath);
assert.isUndefined(config.wslRuntimeId);
assert.isTrue(Option.isNone(config.preflightFailure));
assert.deepEqual(invalidatedRuntimeIds, [`sha256-${archiveHash}`]);
}),
);
});

it.effect("resolveWsl keeps WSL retryable when the mounted fallback fails transiently", () => {
const stagedAppRoot = "/home/test/.t3/wsl-runtime/cache";
const invalidatedRuntimeIds: string[] = [];
return withPackagedWslHarness(
{
archiveHash: "f".repeat(64),
wsl: () => ({
prepareRuntime: () => ({ ok: true, linuxAppRoot: stagedAppRoot }),
invalidateRuntime: (_distro, runtimeId) =>
Effect.sync(() => {
invalidatedRuntimeIds.push(runtimeId);
}),
probeRuntime: () => ({ ok: false, reason: "WSL login-shell PATH could not be resolved" }),
ensureNodePty: () => ({
ok: false,
Expand All @@ -596,7 +608,6 @@ describe("DesktopBackendConfiguration", () => {
assert.isFalse(failure.fatal);
assert.equal(failure.retryLimit, 12);
assert.include(failure.reason, "timed out");
assert.deepEqual(invalidatedRuntimeIds, []);
}),
);
});
Expand Down Expand Up @@ -633,7 +644,7 @@ describe("DesktopBackendConfiguration", () => {
isAvailable: true,
distros: [{ name: "Ubuntu", isDefault: true, version: 2 }],
windowsToWslPath: () => Option.some(linuxAppRoot),
ensureNodePty: () => ({ ok: true, nodePath, resolvedPath }),
ensureNodePty: () => ({ ok: true, nodePath, resolvedPath, t3Home: null }),
getDistroIp: () => Option.some("172.27.0.99"),
}),
),
Expand Down
41 changes: 24 additions & 17 deletions apps/desktop/src/backend/DesktopBackendConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ interface WslPreflightSuccess {
readonly windowsEntryPath: string;
readonly runtime: WslPreflightRuntime;
readonly resolvedPath: string;
readonly t3Home: string | null;
readonly runtimeId?: string;
}

Expand Down Expand Up @@ -412,7 +413,7 @@ const runWslPreflight = Effect.fn("desktop.backendConfiguration.wslPreflight")(f
: ({ ok: true, windowsEntryPath, linuxAppRoot: mountedAppRoot.value } as const);
});

let stagedFailure: { readonly runtimeId?: string; readonly reason: string } | undefined;
let stagedFailure: { readonly reason: string } | undefined;
const failedStaged = (failure: { readonly reason: string }) =>
({
_tag: "Failed",
Expand All @@ -421,32 +422,38 @@ const runWslPreflight = Effect.fn("desktop.backendConfiguration.wslPreflight")(f
}) as const;

if (input.runtimeArchive !== null) {
const runtime = yield* wslEnv.prepareRuntime(runningDistro, input.runtimeArchive);
if (runtime.ok) {
// Staged runtimes bundle Node and node-pty; provider PATH discovery must not require them.
const stagedProbe = yield* wslEnv.probeRuntime(runningDistro);
if (stagedProbe.ok) {
// Staged runtimes bundle Node and node-pty; provider PATH discovery must not require them.
// Probe first: the runtime cache lives under the login shell's T3CODE_HOME.
const stagedProbe = yield* wslEnv.probeRuntime(runningDistro);
if (stagedProbe.ok) {
const runtime = yield* wslEnv.prepareRuntime(
runningDistro,
input.runtimeArchive,
stagedProbe.t3Home,
);
if (runtime.ok) {
yield* wslServerTree.cleanupLegacy;
return {
_tag: "Ready",
runningDistro,
windowsEntryPath: environment.backendEntryPath,
runtime: { kind: "executable", entryPath: `${runtime.linuxAppRoot}/t3` },
resolvedPath: stagedProbe.resolvedPath,
t3Home: stagedProbe.t3Home,
runtimeId: input.runtimeArchive.runtimeId,
} as const;
}
yield* Effect.logWarning(
"The staged WSL runtime probe failed; retrying from the mounted server tree.",
{ reason: stagedProbe.reason },
);
stagedFailure = { runtimeId: input.runtimeArchive.runtimeId, reason: stagedProbe.reason };
} else {
stagedFailure = { reason: runtime.reason };
yield* Effect.logWarning(
"Could not stage the WSL runtime; launching from the mounted server tree instead.",
{ reason: runtime.reason },
);
} else {
yield* Effect.logWarning(
"The staged WSL runtime probe failed; retrying from the mounted server tree.",
{ reason: stagedProbe.reason },
);
stagedFailure = { reason: stagedProbe.reason };
}
}

Expand All @@ -469,10 +476,6 @@ const runWslPreflight = Effect.fn("desktop.backendConfiguration.wslPreflight")(f
: failedNodePty(nodePtyResult);
}

if (stagedFailure?.runtimeId) {
yield* wslEnv.invalidateRuntime(runningDistro, stagedFailure.runtimeId);
}

return {
_tag: "Ready",
runningDistro,
Expand All @@ -483,6 +486,7 @@ const runWslPreflight = Effect.fn("desktop.backendConfiguration.wslPreflight")(f
linuxEntryPath: `${mounted.linuxAppRoot}/apps/server/dist/bin.mjs`,
},
resolvedPath: nodePtyResult.resolvedPath,
t3Home: nodePtyResult.t3Home,
} as const;
});

Expand Down Expand Up @@ -745,13 +749,16 @@ const resolveWslStartConfig = Effect.fn("desktop.backendConfiguration.resolveWsl
"--exec",
"env",
`PATH=${launchPath}`,
...(preflight.t3Home === null ? [] : [`T3CODE_HOME=${preflight.t3Home}`]),
...command,
"--bootstrap-fd",
"0",
...devUrlArgs,
],
preflightFailure: Option.none(),
...(preflight.runtimeId === undefined ? {} : { wslRuntimeId: preflight.runtimeId }),
...(preflight.runtimeId === undefined
? {}
: { wslRuntimeId: preflight.runtimeId, wslT3Home: preflight.t3Home }),
} satisfies DesktopBackendManager.DesktopBackendStartConfig;
});

Expand Down
2 changes: 2 additions & 0 deletions apps/desktop/src/backend/DesktopBackendManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export interface DesktopBackendStartConfig extends BackendProcessContext {
readonly preflightFailure: Option.Option<PreflightFailure>;
readonly runningDistro?: string;
readonly wslRuntimeId?: string;
readonly wslT3Home?: string | null;
}

export interface PreflightFailure {
Expand Down Expand Up @@ -992,6 +993,7 @@ export const makeBackendInstance = Effect.fn("makeBackendInstance")(function* (
yield* wslEnvironment.pruneRuntimes(
config.value.runningDistro,
config.value.wslRuntimeId,
config.value.wslT3Home ?? null,
);
}
}),
Expand Down
Loading
Loading