perf(desktop): reuse the prepared shell environment in the local backend (#17384)

Co-authored-by: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
Yash SinghandClaude Sonnet 5.5 authored and GitHub committed 2026-10-09 23:08:57 -05:00
1 parent aacefcd7e9
commit 03bd839554
11 files changed
+304 -66

No files matched your search

+45
View File
@@ -26,6 +26,21 @@ import { resolveServerConfig } from "./config.ts";
const deriveExplicitServerPaths = (baseDir: string, devUrl: URL | undefined) =>
deriveServerPaths(baseDir, devUrl, { baseDirIsExplicit: true });
const minimalDesktopFlags = (baseDir: string) => ({
mode: Option.some("desktop" as const),
port: Option.some(4888),
host: Option.none<string>(),
baseDir: Option.some(baseDir),
cwd: Option.none<string>(),
devUrl: Option.none<URL>(),
noBrowser: Option.none<boolean>(),
bootstrapFd: Option.none<number>(),
autoBootstrapProjectFromCwd: Option.none<boolean>(),
logWebSocketEvents: Option.none<boolean>(),
tailscaleServeEnabled: Option.none<boolean>(),
tailscaleServePort: Option.none<number>(),
});
const encodeDesktopBootstrap = Schema.encodeEffect(Schema.fromJsonString(DesktopBackendBootstrap));
const encodeUnknownJson = Schema.encodeEffect(Schema.fromJsonString(Schema.Unknown));
@@ -515,6 +530,36 @@ it.layer(NodeServices.layer)("cli config resolution", (it) => {
}),
);
it.effect("carries the desktop's shell environment handoff only when the envelope sets it", () =>
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-cli-config-shell-env-" });
const resolveWith = Effect.fn(function* (overrides: Partial<DesktopBackendBootstrapValue>) {
const fd = yield* openBootstrapFd(makeDesktopBootstrap(overrides));
return yield* resolveServerConfig(
{
...minimalDesktopFlags(baseDir),
bootstrapFd: Option.some(fd),
},
Option.none(),
).pipe(
Effect.provide(
Layer.mergeAll(
ConfigProvider.layer(ConfigProvider.fromEnv({ env: {} })),
NetService.layer,
),
),
);
});
assert.equal((yield* resolveWith({})).shellEnvironmentPrepared, undefined);
assert.equal(
(yield* resolveWith({ shellEnvironmentPrepared: true })).shellEnvironmentPrepared,
true,
);
}),
);
it.effect("creates derived runtime directories during config resolution", () =>
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
+2
View File
@@ -360,6 +360,7 @@ export const resolveServerConfig = (
);
const desktopBootstrapToken = bootstrap?.desktopBootstrapToken;
const desktopBootstrapSecret = bootstrap?.desktopBootstrapSecret;
const shellEnvironmentPrepared = bootstrap?.shellEnvironmentPrepared;
const desktopTelemetryFd = bootstrap?.desktopTelemetryFd;
const desktopTelemetryControlFd = bootstrap?.desktopTelemetryControlFd;
const desktopBrowserFd = bootstrap?.desktopBrowserFd;
@@ -468,6 +469,7 @@ export const resolveServerConfig = (
startupPresentation,
desktopBootstrapToken,
...(desktopBootstrapSecret === undefined ? {} : { desktopBootstrapSecret }),
...(shellEnvironmentPrepared === undefined ? {} : { shellEnvironmentPrepared }),
desktopTelemetryFd,
desktopTelemetryControlFd,
desktopBrowserFd,
+1
View File
@@ -95,6 +95,7 @@ export class ServerConfig extends Context.Service<
readonly startupPresentation: StartupPresentation;
readonly desktopBootstrapToken: string | undefined;
readonly desktopBootstrapSecret?: string | undefined;
readonly shellEnvironmentPrepared?: boolean | undefined;
readonly desktopTelemetryFd?: number | undefined;
readonly desktopTelemetryControlFd?: number | undefined;
readonly desktopBrowserFd?: number | undefined;
+84 -2
View File
@@ -1,7 +1,16 @@
import * as NodeServices from "@effect/platform-node/NodeServices";
import { it as effectIt } from "@effect/vitest";
import * as HostProcess from "@t3tools/shared/HostProcess";
import { CommandAvailability, WindowsShellEnvironment } from "@t3tools/shared/shell";
import * as Effect from "effect/Effect";
// @effect-diagnostics-next-line nodeBuiltinImport:off -- Observe the existing synchronous shell probe without launching the user's login shell.
import * as NodeChildProcess from "node:child_process";
import * as NodeOS from "node:os";
import { assert, it } from "vite-plus/test";
import { assert, it, vi } from "vite-plus/test";
import { hydratePosixHome } from "./os-jank.ts";
import { fixPath, hydratePosixHome } from "./os-jank.ts";
vi.mock("node:child_process", { spy: true });
it("hydrates HOME for minimal service environments from the user account", () => {
const env: NodeJS.ProcessEnv = {};
@@ -38,3 +47,76 @@ it("preserves an explicitly configured HOME", () => {
assert.equal(env.HOME, "/custom/home");
});
effectIt.effect.each(["darwin", "linux"] as const)(
"keeps the prepared PATH and still hydrates HOME on %s",
(platform) =>
Effect.acquireUseRelease(
Effect.sync(() => vi.spyOn(NodeChildProcess, "execFileSync")),
(probe) =>
Effect.gen(function* () {
probe.mockImplementation(() => {
throw new Error("The prepared environment must not launch another shell");
});
const env: NodeJS.ProcessEnv = { PATH: "/prepared/bin:/usr/bin" };
yield* fixPath({ shellEnvironmentPrepared: true }).pipe(
Effect.provideService(HostProcess.Platform, platform),
Effect.provideService(HostProcess.Environment, env),
Effect.provide(NodeServices.layer),
);
assert.equal(probe.mock.calls.length, 0);
assert.equal(env.PATH, "/prepared/bin:/usr/bin");
assert.equal(env.HOME, NodeOS.userInfo().homedir);
}),
(probe) => Effect.sync(() => probe.mockRestore()),
),
);
effectIt.effect("hydrates PATH when the desktop handoff is absent", () =>
Effect.acquireUseRelease(
Effect.sync(() =>
vi
.spyOn(NodeChildProcess, "execFileSync")
.mockReturnValue("__T3CODE_ENV_PATH_START__\n/opt/tools/bin\n__T3CODE_ENV_PATH_END__\n"),
),
(probe) =>
Effect.gen(function* () {
const env: NodeJS.ProcessEnv = { SHELL: "/bin/bash", PATH: "/usr/bin" };
yield* fixPath().pipe(
Effect.provideService(HostProcess.Platform, "linux"),
Effect.provideService(HostProcess.Environment, env),
Effect.provide(NodeServices.layer),
);
assert.equal(probe.mock.calls.length, 1);
assert.equal(env.PATH, "/opt/tools/bin:/usr/bin");
}),
(probe) => Effect.sync(() => probe.mockRestore()),
),
);
effectIt.effect("repairs Windows PATH even when the desktop marks it prepared", () =>
Effect.gen(function* () {
const env: NodeJS.ProcessEnv = { PATH: "C:\\Windows" };
const commands: string[] = [];
yield* fixPath({ shellEnvironmentPrepared: true }).pipe(
Effect.provideService(HostProcess.Platform, "win32"),
Effect.provideService(HostProcess.Environment, env),
Effect.provideService(WindowsShellEnvironment, () => ({ PATH: "C:\\Tools" })),
Effect.provideService(CommandAvailability, (command) =>
Effect.sync(() => {
commands.push(command);
return true;
}),
),
Effect.provide(NodeServices.layer),
);
assert.deepEqual(commands, ["node"]);
assert.equal(env.PATH, "C:\\Tools;C:\\Windows");
}),
);
+4 -5
View File
@@ -48,11 +48,9 @@ export function hydratePosixHome(
}
}
export const fixPath = Effect.fn("fixPath")(function* (): Effect.fn.Return<
void,
never,
FileSystem.FileSystem | Path.Path
> {
export const fixPath = Effect.fn("fixPath")(function* (options?: {
readonly shellEnvironmentPrepared?: boolean | undefined;
}): Effect.fn.Return<void, never, FileSystem.FileSystem | Path.Path> {
const platform = yield* HostProcess.Platform;
const env = yield* HostProcess.Environment;
@@ -82,6 +80,7 @@ export const fixPath = Effect.fn("fixPath")(function* (): Effect.fn.Return<
}),
),
);
if (options?.shellEnvironmentPrepared === true) return;
yield* Effect.sync(() => hydratePosixPath(env, platform)).pipe(
Effect.catchDefect((defect) =>
Effect.sync(() => {
+1 -1
View File
@@ -741,7 +741,7 @@ const layerMakeServer = Layer.unwrap(
const routesReady = yield* Deferred.make<void>();
const layerLauncher = ServiceLauncherClient.layer;
yield* fixPath();
yield* fixPath({ shellEnvironmentPrepared: config.shellEnvironmentPrepared });
const layerHttpListening = Layer.effectDiscard(
Effect.gen(function* () {