Skip to content
TypeScript

fix(desktop): discover shell environment without blocking startup (#28556)

Yash Singh8b4de61b8b4f
diff --git a/apps/desktop/src/backend/DesktopBackendConfiguration.test.ts b/apps/desktop/src/backend/DesktopBackendConfiguration.test.ts
index 7464d8fbe9e..26f6dbe0268 100644
--- a/apps/desktop/src/backend/DesktopBackendConfiguration.test.ts
+++ b/apps/desktop/src/backend/DesktopBackendConfiguration.test.ts
@@ -8,6 +8,7 @@ import * as Layer from "effect/Layer";
import * as ManagedRuntime from "effect/ManagedRuntime";
import * as Option from "effect/Option";
import * as Path from "effect/Path";
+import * as Stream from "effect/Stream";
import { ChildProcessSpawner } from "effect/process";
import * as DesktopEnvironment from "../app/DesktopEnvironment.ts";
@@ -22,6 +23,8 @@ import * as DesktopWslServerTree from "../wsl/DesktopWslServerTree.ts";
const layerShellEnvironment = (prepared = true) =>
Layer.succeed(DesktopShellEnvironment.DesktopShellEnvironment, {
installIntoProcess: Effect.succeed(prepared),
+ awaitDiscovery: Effect.succeed(prepared),
+ updates: Stream.empty,
});
const layerBackendConfiguration = (prepared = true) =>
@@ -237,6 +240,7 @@ describe("DesktopBackendConfiguration", () => {
assert.equal(first.cwd, environment.backendCwd);
assert.equal(first.captureOutput, true);
assert.equal(first.env.ELECTRON_RUN_AS_NODE, "1");
+ assert.equal(first.env.T3CODE_DESKTOP_SHELL_DISCOVERY_PENDING, "1");
assert.isUndefined(first.env.T3CODE_PORT);
assert.isUndefined(first.env.T3CODE_MODE);
assert.isUndefined(first.env.T3CODE_DESKTOP_LAN_HOST);
diff --git a/apps/desktop/src/backend/DesktopBackendConfiguration.ts b/apps/desktop/src/backend/DesktopBackendConfiguration.ts
index 49290313954..9c8186711f6 100644
--- a/apps/desktop/src/backend/DesktopBackendConfiguration.ts
+++ b/apps/desktop/src/backend/DesktopBackendConfiguration.ts
@@ -532,6 +532,7 @@ const resolvePrimaryStartConfig = Effect.fn("desktop.backendConfiguration.resolv
env: {
...backendChildEnvPatch(),
ELECTRON_RUN_AS_NODE: "1",
+ T3CODE_DESKTOP_SHELL_DISCOVERY_PENDING: "1",
T3CODE_CLI_PATH: Option.getOrUndefined(input.cliPath),
},
extendEnv: true,
diff --git a/apps/desktop/src/main.ts b/apps/desktop/src/main.ts
index 9a3c327e6b8..2890f164f6f 100644
--- a/apps/desktop/src/main.ts
+++ b/apps/desktop/src/main.ts
@@ -186,10 +186,10 @@ const layerDesktopAppActivation = DesktopAppActivation.layer.pipe(
const layerDesktopBackend = DesktopBackendPool.layer.pipe(
Layer.provideMerge(DesktopAppIdentity.layer),
Layer.provideMerge(DesktopBackendConfiguration.layer),
- Layer.provideMerge(DesktopShellEnvironment.layer),
Layer.provideMerge(DesktopWslEnvironment.layer),
Layer.provideMerge(DesktopWslServerTree.layer),
Layer.provideMerge(DesktopTelemetryPublisher.layer),
+ Layer.provideMerge(DesktopShellEnvironment.layer),
Layer.provideMerge(layerDesktopWindow),
);
diff --git a/apps/desktop/src/shell/DesktopShellEnvironment.test.ts b/apps/desktop/src/shell/DesktopShellEnvironment.test.ts
index 33325d9de99..79344567fcd 100644
--- a/apps/desktop/src/shell/DesktopShellEnvironment.test.ts
+++ b/apps/desktop/src/shell/DesktopShellEnvironment.test.ts
@@ -3,11 +3,15 @@ import * as NodeServices from "@effect/platform-node/NodeServices";
import { assert, describe, it } from "@effect/vitest";
import * as HostProcess from "@t3tools/shared/HostProcess";
import * as Effect from "effect/Effect";
+import * as Deferred from "effect/Deferred";
+import * as FileSystem from "effect/FileSystem";
import * as Layer from "effect/Layer";
import * as Logger from "effect/Logger";
import * as PlatformError from "effect/PlatformError";
import * as Sink from "effect/Sink";
import * as Stream from "effect/Stream";
+import * as Schema from "effect/Schema";
+import * as Option from "effect/Option";
import * as ChildProcess from "effect/process/ChildProcess";
import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
import * as NodeOS from "node:os";
@@ -19,6 +23,15 @@ import * as DesktopShellEnvironment from "./DesktopShellEnvironment.ts";
vi.mock("node:os", { spy: true });
const textEncoder = new TextEncoder();
+const cacheCodec = Schema.fromJsonString(
+ Schema.Struct({
+ version: Schema.Number,
+ key: Schema.String,
+ environment: Schema.Record(Schema.String, Schema.NullOr(Schema.String)),
+ }),
+);
+const encodeCache = Schema.encodeSync(cacheCodec);
+const decodeCache = Schema.decodeUnknownEffect(cacheCodec);
function envOutput(values: Readonly<Record<string, string>>): string {
return Object.entries(values)
@@ -69,18 +82,28 @@ function runShellEnvironment(input: {
readonly env: NodeJS.ProcessEnv;
readonly platform: NodeJS.Platform;
readonly userShell?: string;
+ readonly stateDir?: string;
+ readonly use?: (
+ service: DesktopShellEnvironment.DesktopShellEnvironment["Service"],
+ ) => Effect.Effect<boolean, never, import("effect/Scope").Scope>;
readonly handler: (
command: ChildProcess.Command,
) => string | { output: string; exitCode: number };
readonly failure?: PlatformError.PlatformError;
readonly processSpawner?: ChildProcessSpawner.ChildProcessSpawner["Service"];
}) {
- const layerEnvironment = Layer.succeed(
+ const layerEnvironment = Layer.effect(
DesktopEnvironment.DesktopEnvironment,
- DesktopEnvironment.DesktopEnvironment.of({
- platform: input.platform,
- } as DesktopEnvironment.DesktopEnvironment["Service"]),
- );
+ Effect.gen(function* () {
+ const fs = yield* FileSystem.FileSystem;
+ const stateDir = input.stateDir ?? (yield* fs.makeTempDirectoryScoped());
+ return DesktopEnvironment.DesktopEnvironment.of({
+ platform: input.platform,
+ stateDir,
+ homeDirectory: "/test-home",
+ } as DesktopEnvironment.DesktopEnvironment["Service"]);
+ }),
+ ).pipe(Layer.provide(NodeServices.layer));
const layerSpawner = Layer.succeed(
ChildProcessSpawner.ChildProcessSpawner,
ChildProcessSpawner.make((command) => {
@@ -97,8 +120,9 @@ function runShellEnvironment(input: {
const program = Effect.gen(function* () {
const shellEnvironment = yield* DesktopShellEnvironment.DesktopShellEnvironment;
- const prepared = yield* shellEnvironment.installIntoProcess;
- assert.equal(yield* shellEnvironment.installIntoProcess, prepared);
+ if (input.use) return yield* input.use(shellEnvironment);
+ const prepared = yield* shellEnvironment.awaitDiscovery;
+ assert.equal(yield* shellEnvironment.awaitDiscovery, prepared);
return prepared;
}).pipe(
Effect.provide(
@@ -121,6 +145,247 @@ function runShellEnvironment(input: {
}
describe("DesktopShellEnvironment", () => {
+ it.effect("does not wait for discovery when the cache is missing", () =>
+ Effect.gen(function* () {
+ const started = yield* Deferred.make<void>();
+ const release = yield* Deferred.make<void>();
+ yield* runShellEnvironment({
+ env: { SHELL: "/test-shell", PATH: "/usr/bin" },
+ platform: "darwin",
+ handler: () => "",
+ processSpawner: ChildProcessSpawner.make(() =>
+ Deferred.succeed(started, undefined).pipe(
+ Effect.andThen(Deferred.await(release)),
+ Effect.as(makeProcess(envOutput({ PATH: "/fresh/bin" }))),
+ ),
+ ),
+ use: (service) =>
+ Effect.gen(function* () {
+ const startup = yield* service.installIntoProcess.pipe(Effect.forkChild);
+ yield* Deferred.await(started);
+ assert.isDefined(startup.pollUnsafe());
+ yield* Deferred.succeed(release, undefined);
+ return yield* service.awaitDiscovery;
+ }),
+ });
+ }),
+ );
+
+ it.effect(
+ "reuses cached paths immediately and streams fresh discovery without keeping removed paths",
+ () =>
+ Effect.gen(function* () {
+ const fs = yield* FileSystem.FileSystem;
+ const stateDir = yield* fs.makeTempDirectoryScoped();
+ const spawner = yield* ChildProcessSpawner.ChildProcessSpawner.pipe(
+ Effect.provide(NodeServices.layer),
+ );
+ const executable = yield* HostProcess.ExecutablePath;
+ const first = { SHELL: "/bin/zsh", PATH: "/usr/bin" };
+ yield* runShellEnvironment({
+ env: first,
+ stateDir,
+ platform: "darwin",
+ handler: () =>
+ envOutput({
+ PATH: "/old/bin:/usr/bin",
+ __MISE_DIFF: "old-mise",
+ SSH_AUTH_SOCK: "/old-session.sock",
+ JAVA_HOME: "/old/java",
+ SSL_CERT_FILE: "/old/cert.pem",
+ }),
+ });
+ const cachePath = `${stateDir}/shell-environment-cache.json`;
+ const cached = yield* fs.readFileString(cachePath).pipe(Effect.flatMap(decodeCache));
+ assert.equal(cached.environment.PATH, "/old/bin:/usr/bin");
+ assert.notProperty(cached.environment, "SSH_AUTH_SOCK");
+ const started = yield* Deferred.make<void>();
+ const release = yield* Deferred.make<void>();
+ const env: NodeJS.ProcessEnv = {
+ SHELL: "/bin/zsh",
+ PATH: "/launcher/bin",
+ SSH_AUTH_SOCK: "/current-session.sock",
+ };
+ const source = [
+ "const values = { PATH: '/fresh/bin:' + process.env.PATH, __MISE_DIFF: 'fresh-mise', JAVA_HOME: process.env.JAVA_HOME, SSL_CERT_FILE: process.env.SSL_CERT_FILE };",
+ "for (const [name, value] of Object.entries(values)) {",
+ " if (value !== undefined) process.stdout.write('__T3CODE_ENV_' + name + '_START__\\n' + value + '\\n__T3CODE_ENV_' + name + '_END__\\n');",
+ "}",
+ ].join("\n");
+ let calls = 0;
+ yield* runShellEnvironment({
+ env,
+ stateDir,
+ platform: "darwin",
+ handler: () => "",
+ processSpawner: ChildProcessSpawner.make((command) => {
+ calls++;
+ return Deferred.succeed(started, undefined).pipe(
+ Effect.andThen(Deferred.await(release)),
+ Effect.andThen(
+ command._tag === "StandardCommand"
+ ? spawner.spawn(ChildProcess.make(executable, ["-e", source], command.options))
+ : Effect.succeed(makeProcess("")),
+ ),
+ );
+ }),
+ use: (service) =>
+ Effect.gen(function* () {
+ yield* service.installIntoProcess;
+ yield* service.installIntoProcess;
+ assert.equal(env.PATH, "/old/bin:/usr/bin:/launcher/bin");
+ assert.equal(env.SSH_AUTH_SOCK, "/current-session.sock");
+ const replay = yield* Stream.runHead(service.updates);
+ assert.isTrue(Option.isSome(replay));
+ if (Option.isSome(replay))
+ assert.equal(replay.value.environment.__MISE_DIFF, "old-mise");
+ yield* Deferred.await(started);
+ assert.equal(calls, 1);
+ yield* Deferred.succeed(release, undefined);
+ yield* service.awaitDiscovery;
+ assert.equal(env.PATH, "/fresh/bin:/launcher/bin");
+ assert.equal(env.__MISE_DIFF, "fresh-mise");
+ assert.isUndefined(env.JAVA_HOME);
+ assert.isUndefined(env.SSL_CERT_FILE);
+ const fresh = yield* Stream.runHead(service.updates);
+ assert.isTrue(Option.isSome(fresh));
+ if (Option.isSome(fresh)) {
+ assert.equal(fresh.value.environment.PATH, env.PATH);
+ assert.isTrue(fresh.value.discovered);
+ }
+ return true;
+ }),
+ });
+ const refreshed = yield* fs.readFileString(cachePath).pipe(Effect.flatMap(decodeCache));
+ assert.equal(refreshed.environment.PATH, "/fresh/bin:/launcher/bin");
+ assert.notProperty(refreshed.environment, "JAVA_HOME");
+ assert.notProperty(refreshed.environment, "SSL_CERT_FILE");
+ }).pipe(Effect.provide(NodeServices.layer)),
+ );
+
+ it.effect.each([
+ "not json",
+ '{"version":2,"key":"old","environment":{"PATH":"/bad/bin"}}',
+ '{"version":1,"key":"other-shell","environment":{"PATH":"/bad/bin"}}',
+ ])("ignores an unusable cache and still starts before discovery: %s", (contents) =>
+ Effect.gen(function* () {
+ const fs = yield* FileSystem.FileSystem;
+ const stateDir = yield* fs.makeTempDirectoryScoped();
+ yield* fs.writeFileString(`${stateDir}/shell-environment-cache.json`, contents);
+ const started = yield* Deferred.make<void>();
+ const release = yield* Deferred.make<void>();
+ const env: NodeJS.ProcessEnv = { SHELL: "/bin/zsh", PATH: "/usr/bin" };
+ yield* runShellEnvironment({
+ env,
+ stateDir,
+ platform: "darwin",
+ handler: () => "",
+ processSpawner: ChildProcessSpawner.make(() =>
+ Deferred.succeed(started, undefined).pipe(
+ Effect.andThen(Deferred.await(release)),
+ Effect.as(makeProcess(envOutput({ PATH: "/fresh/bin" }))),
+ ),
+ ),
+ use: (service) =>
+ Effect.gen(function* () {
+ const startup = yield* service.installIntoProcess.pipe(Effect.forkChild);
+ yield* Deferred.await(started);
+ assert.isDefined(startup.pollUnsafe());
+ assert.equal(env.PATH, "/usr/bin");
+ yield* Deferred.succeed(release, undefined);
+ return yield* service.awaitDiscovery;
+ }),
+ });
+ assert.equal(env.PATH, "/fresh/bin:/usr/bin");
+ }).pipe(Effect.provide(NodeServices.layer)),
+ );
+
+ it.effect(
+ "retains the last successful cache on refresh failure and ignores session values in the cache",
+ () =>
+ Effect.gen(function* () {
+ const fs = yield* FileSystem.FileSystem;
+ const stateDir = yield* fs.makeTempDirectoryScoped();
+ const contents = encodeCache({
+ version: 1,
+ key: '["darwin","/test-home","/bin/zsh",""]',
+ environment: {
+ PATH: "/cached/bin",
+ __MISE_DIFF: "cached-mise",
+ JAVA_HOME: "/cached/java",
+ NODE_EXTRA_CA_CERTS: "/cached/ca.pem",
+ SSL_CERT_FILE: "/cached/cert.pem",
+ SSL_CERT_DIR: "/cached/certs",
+ SSH_AUTH_SOCK: "/stale.sock",
+ NODE_OPTIONS: "--bad",
+ },
+ });
+ const cachePath = `${stateDir}/shell-environment-cache.json`;
+ yield* fs.writeFileString(cachePath, contents);
+ const env: NodeJS.ProcessEnv = {
+ SHELL: "/bin/zsh",
+ PATH: "/usr/bin",
+ __MISE_DIFF: "launcher-mise",
+ JAVA_HOME: "/launcher/java",
+ NODE_EXTRA_CA_CERTS: "/launcher/ca.pem",
+ SSL_CERT_FILE: "",
+ SSH_AUTH_SOCK: "/current.sock",
+ };
+ const prepared = yield* runShellEnvironment({
+ env,
+ stateDir,
+ platform: "darwin",
+ handler: () => "",
+ use: (service) =>
+ Effect.gen(function* () {
+ yield* service.installIntoProcess;
+ const replay = yield* Stream.runHead(service.updates);
+ assert.isTrue(Option.isSome(replay));
+ if (Option.isSome(replay)) {
+ assert.equal(replay.value.environment.JAVA_HOME, "/launcher/java");
+ assert.equal(replay.value.environment.NODE_EXTRA_CA_CERTS, "/launcher/ca.pem");
+ }
+ return yield* service.awaitDiscovery;
+ }),
+ });
+ assert.isFalse(prepared);
+ assert.equal(env.PATH, "/cached/bin:/usr/bin");
+ assert.equal(env.__MISE_DIFF, "cached-mise");
+ assert.equal(env.JAVA_HOME, "/launcher/java");
+ assert.equal(env.NODE_EXTRA_CA_CERTS, "/launcher/ca.pem");
+ assert.equal(env.SSL_CERT_FILE, "");
+ assert.equal(env.SSL_CERT_DIR, "/cached/certs");
+ assert.equal(env.SSH_AUTH_SOCK, "/current.sock");
+ assert.isUndefined(env.NODE_OPTIONS);
+ assert.equal(yield* fs.readFileString(cachePath), contents);
+ }).pipe(Effect.provide(NodeServices.layer)),
+ );
+
+ it.effect("cancels background discovery when the application scope closes", () =>
+ Effect.gen(function* () {
+ const started = yield* Deferred.make<void>();
+ const stopped = yield* Deferred.make<void>();
+ yield* runShellEnvironment({
+ env: { SHELL: "/bin/zsh", PATH: "/usr/bin" },
+ platform: "darwin",
+ handler: () => "",
+ processSpawner: ChildProcessSpawner.make(() =>
+ Deferred.succeed(started, undefined).pipe(
+ Effect.andThen(Effect.never),
+ Effect.ensuring(Deferred.succeed(stopped, undefined)),
+ ),
+ ),
+ use: (service) =>
+ Effect.gen(function* () {
+ yield* service.installIntoProcess;
+ yield* Deferred.await(started);
+ return true;
+ }),
+ });
+ yield* Deferred.await(stopped);
+ }),
+ );
+
it.effect(
"captures the login-shell PATH when startup writes more than a pipe buffer to stderr",
() =>
diff --git a/apps/desktop/src/shell/DesktopShellEnvironment.ts b/apps/desktop/src/shell/DesktopShellEnvironment.ts
index 255eb202df6..556f7090b42 100644
--- a/apps/desktop/src/shell/DesktopShellEnvironment.ts
+++ b/apps/desktop/src/shell/DesktopShellEnvironment.ts
@@ -1,4 +1,6 @@
+import { DesktopShellEnvironmentSnapshot, ShellEnvironmentPatch } from "@t3tools/contracts";
import {
+ applyShellEnvironment,
buildEnvironmentCaptureCommand,
listLoginShellCandidates,
setWindowsPath,
@@ -6,6 +8,11 @@ import {
import * as Context from "effect/Context";
import * as Duration from "effect/Duration";
import * as Effect from "effect/Effect";
+import * as Deferred from "effect/Deferred";
+import * as Path from "effect/Path";
+import * as PubSub from "effect/PubSub";
+import * as Ref from "effect/Ref";
+import * as Scope from "effect/Scope";
import * as FileSystem from "effect/FileSystem";
import * as Layer from "effect/Layer";
import * as Option from "effect/Option";
@@ -68,8 +75,10 @@ export class DesktopShellEnvironmentCommandTimeoutError extends Schema.TaggedErr
export class DesktopShellEnvironment extends Context.Service<
DesktopShellEnvironment,
{
- // Reuses the installation result; false leaves server hydration enabled.
+ // Desktop owns discovery, including on cache misses; the backend receives updates over fd4.
readonly installIntoProcess: Effect.Effect<boolean>;
+ readonly awaitDiscovery: Effect.Effect<boolean>;
+ readonly updates: Stream.Stream<DesktopShellEnvironmentSnapshot>;
}
>()("@t3tools/desktop/shell/DesktopShellEnvironment") {}
@@ -97,6 +106,7 @@ const LOGIN_SHELL_ENV_NAMES = [
"T3CODE_TELEMETRY_ENABLED",
"T3CODE_GITHUB_APP_CLIENT_ID",
"__MISE_DIFF",
+ "JAVA_HOME",
] as const;
const WINDOWS_PROFILE_ENV_NAMES = ["PATH", "FNM_DIR", "FNM_MULTISHELL_PATH"] as const;
const LOCALE_ENV_NAMES = ["LANG", "LC_ALL", "LC_CTYPE"] as const;
@@ -244,6 +254,7 @@ const runCommandOutput = Effect.fn("desktop.shellEnvironment.runCommandOutput")(
readonly probe: DesktopShellEnvironmentProbe;
readonly command: string;
readonly args: ReadonlyArray<string>;
+ readonly env: NodeJS.ProcessEnv;
readonly timeout: Duration.Duration;
readonly shell?: boolean;
}): Effect.fn.Return<string, never, ChildProcessSpawner.ChildProcessSpawner> {
@@ -253,6 +264,8 @@ const runCommandOutput = Effect.fn("desktop.shellEnvironment.runCommandOutput")(
const child = yield* spawner.spawn(
ChildProcess.make(input.command, input.args, {
shell: input.shell ?? false,
+ env: input.env,
+ extendEnv: false,
stdin: "ignore",
stdout: "pipe",
stderr: "ignore",
@@ -308,6 +321,7 @@ const runCommandOutput = Effect.fn("desktop.shellEnvironment.runCommandOutput")(
const readLoginShellEnvironment = (
shell: string,
names: ReadonlyArray<string>,
+ env: NodeJS.ProcessEnv,
): Effect.Effect<EnvironmentPatch, never, ChildProcessSpawner.ChildProcessSpawner> =>
names.length === 0
? Effect.succeed({})
@@ -315,20 +329,24 @@ const readLoginShellEnvironment = (
probe: "login-shell",
command: shell,
args: ["-ilc", buildEnvironmentCaptureCommand(shell, names)],
+ env,
timeout: LOGIN_SHELL_TIMEOUT,
}).pipe(Effect.map((output) => extractEnvironment(output, names)));
-const readLaunchctlPath = runCommandOutput({
- probe: "launchctl-path",
- command: "/bin/launchctl",
- args: ["getenv", "PATH"],
- timeout: LAUNCHCTL_TIMEOUT,
-}).pipe(Effect.map(trimNonEmpty));
+const readLaunchctlPath = (env: NodeJS.ProcessEnv) =>
+ runCommandOutput({
+ env,
+ probe: "launchctl-path",
+ command: "/bin/launchctl",
+ args: ["getenv", "PATH"],
+ timeout: LAUNCHCTL_TIMEOUT,
+ }).pipe(Effect.map(trimNonEmpty));
const readWindowsEnvironment = Effect.fn("desktop.shellEnvironment.readWindowsEnvironment")(
function* (
names: ReadonlyArray<string>,
options: WindowsProbeOptions,
+ env: NodeJS.ProcessEnv,
): Effect.fn.Return<EnvironmentPatch, never, ChildProcessSpawner.ChildProcessSpawner> {
if (names.length === 0) return {};
@@ -345,6 +363,7 @@ const readWindowsEnvironment = Effect.fn("desktop.shellEnvironment.readWindowsEn
probe: options.loadProfile ? "powershell-profile" : "powershell-no-profile",
command,
args,
+ env,
timeout: LOGIN_SHELL_TIMEOUT,
});
const environment = extractEnvironment(output, names);
@@ -363,8 +382,8 @@ const installWindowsEnvironment = Effect.fn("desktop.shellEnvironment.installWin
): Effect.fn.Return<boolean, never, ChildProcessSpawner.ChildProcessSpawner> {
const [noProfile, profile] = yield* Effect.all(
[
- readWindowsEnvironment(["PATH"], { loadProfile: false }),
- readWindowsEnvironment(WINDOWS_PROFILE_ENV_NAMES, { loadProfile: true }),
+ readWindowsEnvironment(["PATH"], { loadProfile: false }, config.env),
+ readWindowsEnvironment(WINDOWS_PROFILE_ENV_NAMES, { loadProfile: true }, config.env),
],
{ concurrency: 2 },
);
@@ -402,14 +421,14 @@ const installPosixEnvironment = Effect.fn("desktop.shellEnvironment.installPosix
for (const shell of listLoginShellCandidates(config.platform, config.env.SHELL)) {
Object.assign(
shellEnvironment,
- yield* readLoginShellEnvironment(shell, LOGIN_SHELL_ENV_NAMES),
+ yield* readLoginShellEnvironment(shell, LOGIN_SHELL_ENV_NAMES, config.env),
);
if (shellEnvironment.PATH) break;
}
const launchctlPath =
config.platform === "darwin" && !shellEnvironment.PATH
- ? yield* readLaunchctlPath
+ ? yield* readLaunchctlPath(config.env)
: Option.none<string>();
const mergedPath = mergePaths(config.platform, [
trimNonEmpty(shellEnvironment.PATH).pipe(Option.orElse(() => launchctlPath)),
@@ -450,6 +469,7 @@ const installPosixEnvironment = Effect.fn("desktop.shellEnvironment.installPosix
"SSL_CERT_FILE",
"SSL_CERT_DIR",
"NODE_EXTRA_CA_CERTS",
+ "JAVA_HOME",
"T3CODE_TELEMETRY_ENABLED",
"T3CODE_GITHUB_APP_CLIENT_ID",
] as const) {
@@ -512,24 +532,140 @@ const installShellEnvironment = (
return Effect.succeed(false);
};
+const ShellEnvironmentCache = Schema.fromJsonString(
+ Schema.Struct({
+ version: Schema.Literal(1),
+ key: Schema.String,
+ environment: ShellEnvironmentPatch,
+ }),
+);
+const encodeCacheKey = Schema.encodeSync(Schema.fromJsonString(Schema.Array(Schema.String)));
+const decodeCache = Schema.decodeUnknownEffect(ShellEnvironmentCache);
+const encodeCache = Schema.encodeEffect(ShellEnvironmentCache);
+const CACHED_ENV_NAMES = [
+ "PATH",
+ "__MISE_DIFF",
+ "HOMEBREW_PREFIX",
+ "HOMEBREW_CELLAR",
+ "HOMEBREW_REPOSITORY",
+ "JAVA_HOME",
+ "SSL_CERT_FILE",
+ "SSL_CERT_DIR",
+ "NODE_EXTRA_CA_CERTS",
+] as const;
+
/** @public */
export const make = Effect.gen(function* () {
const environment = yield* DesktopEnvironment.DesktopEnvironment;
const fileSystem = yield* FileSystem.FileSystem;
+ const path = yield* Path.Path;
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
- const installIntoProcess: DesktopShellEnvironment["Service"]["installIntoProcess"] =
- yield* Effect.cached(
- installShellEnvironment({
- env: process.env,
- platform: environment.platform,
- }).pipe(
- Effect.provideService(FileSystem.FileSystem, fileSystem),
- Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
- Effect.withSpan("desktop.shellEnvironment.installIntoProcess"),
- ),
+ const scope = yield* Scope.Scope;
+ const env = process.env;
+ const inherited = { ...env };
+ const key = encodeCacheKey([
+ environment.platform,
+ environment.homeDirectory,
+ inherited.SHELL ?? "",
+ inherited.ZDOTDIR ?? "",
+ ]);
+ const cachePath = path.join(environment.stateDir, "shell-environment-cache.json");
+ const changes = yield* PubSub.unbounded<DesktopShellEnvironmentSnapshot>();
+ const latest = yield* Ref.make<Option.Option<DesktopShellEnvironmentSnapshot>>(Option.none());
+ const discovered = yield* Deferred.make<boolean>();
+ const publish = Effect.fn("desktop.shellEnvironment.publish")(function* (
+ patch: ShellEnvironmentPatch,
+ discovered = false,
+ ) {
+ applyShellEnvironment(env, patch, environment.platform);
+ const message: DesktopShellEnvironmentSnapshot = {
+ version: 1,
+ type: "shellEnvironment",
+ discovered,
+ platform: environment.platform,
+ environment: patch,
+ };
+ yield* Ref.set(latest, Option.some(message));
+ yield* PubSub.publish(changes, message);
+ });
+ const refresh = Effect.gen(function* () {
+ // Refresh from the inherited baseline so removed cached paths and values do not survive.
+ const fresh = { ...inherited };
+ const prepared = yield* installShellEnvironment({ env: fresh, platform: environment.platform });
+ const patch = Object.fromEntries(
+ Object.keys(ShellEnvironmentPatch.fields).map((name) => [name, fresh[name] ?? null]),
);
-
- return DesktopShellEnvironment.of({ installIntoProcess });
+ if (prepared || Option.isNone(yield* Ref.get(latest))) yield* publish(patch, prepared);
+ if (prepared) {
+ const cached = Object.fromEntries(
+ CACHED_ENV_NAMES.flatMap((name) => (fresh[name] ? [[name, fresh[name]]] : [])),
+ );
+ const persist = Effect.gen(function* () {
+ const encoded = yield* encodeCache({ version: 1, key, environment: cached });
+ yield* fileSystem.makeDirectory(environment.stateDir, { recursive: true });
+ yield* fileSystem.writeFileString(cachePath + ".tmp", encoded, { mode: 0o600 });
+ yield* fileSystem.rename(cachePath + ".tmp", cachePath);
+ });
+ yield* persist.pipe(
+ Effect.catch(() => Effect.logWarning("Could not cache shell environment")),
+ );
+ }
+ return prepared;
+ }).pipe(
+ Effect.provideService(FileSystem.FileSystem, fileSystem),
+ Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
+ Effect.withSpan("desktop.shellEnvironment.refresh"),
+ Effect.onExit((exit) => Deferred.done(discovered, exit)),
+ );
+ const installIntoProcess = yield* Effect.cached(
+ Effect.gen(function* () {
+ const cache = yield* fileSystem
+ .readFileString(cachePath)
+ .pipe(Effect.flatMap(decodeCache), Effect.option);
+ if (Option.isSome(cache) && cache.value.key === key) {
+ const cached = Object.fromEntries(
+ CACHED_ENV_NAMES.flatMap((name) => {
+ const value = cache.value.environment[name];
+ // mise's metadata must describe the cached PATH entries restored alongside it.
+ if (name !== "PATH" && name !== "__MISE_DIFF" && inherited[name] !== undefined)
+ return [];
+ return typeof value === "string" ? [[name, value]] : [];
+ }),
+ );
+ const baseline = { ...inherited };
+ const cachedPath = cached.PATH;
+ if (cachedPath) {
+ cached.PATH = Option.getOrElse(
+ mergePaths(environment.platform, [Option.some(cachedPath), readEnvPath(inherited)]),
+ () => cachedPath,
+ );
+ }
+ applyShellEnvironment(baseline, cached, environment.platform);
+ yield* publish(
+ Object.fromEntries(
+ Object.keys(ShellEnvironmentPatch.fields).map((name) => [name, baseline[name] ?? null]),
+ ),
+ );
+ }
+ yield* Effect.forkIn(refresh, scope);
+ return true;
+ }).pipe(Effect.withSpan("desktop.shellEnvironment.installIntoProcess")),
+ );
+ const updates = Stream.unwrap(
+ Effect.gen(function* () {
+ const subscription = yield* PubSub.subscribe(changes);
+ const snapshot = yield* Ref.get(latest);
+ return Stream.concat(
+ Option.isSome(snapshot) ? Stream.make(snapshot.value) : Stream.empty,
+ Stream.fromSubscription(subscription),
+ );
+ }),
+ );
+ return DesktopShellEnvironment.of({
+ installIntoProcess,
+ awaitDiscovery: installIntoProcess.pipe(Effect.andThen(Deferred.await(discovered))),
+ updates,
+ });
});
export const layer = Layer.effect(DesktopShellEnvironment, make);
diff --git a/apps/desktop/src/telemetry/DesktopTelemetryPublisher.test.ts b/apps/desktop/src/telemetry/DesktopTelemetryPublisher.test.ts
index f3f0163401a..1180c0fa709 100644
--- a/apps/desktop/src/telemetry/DesktopTelemetryPublisher.test.ts
+++ b/apps/desktop/src/telemetry/DesktopTelemetryPublisher.test.ts
@@ -1,6 +1,8 @@
import * as NodeServices from "@effect/platform-node/NodeServices";
import { assert, describe, it } from "@effect/vitest";
import * as Deferred from "effect/Deferred";
+import * as Context from "effect/Context";
+import * as Option from "effect/Option";
import * as Duration from "effect/Duration";
import * as Effect from "effect/Effect";
import * as Exit from "effect/Exit";
@@ -8,6 +10,7 @@ import * as FileSystem from "effect/FileSystem";
import * as Layer from "effect/Layer";
import * as Schema from "effect/Schema";
import * as Scope from "effect/Scope";
+import * as Stream from "effect/Stream";
import * as TestClock from "effect/testing/TestClock";
import type * as Electron from "electron";
@@ -18,6 +21,7 @@ import * as ElectronApp from "../electron/ElectronApp.ts";
import * as ElectronPowerMonitor from "../electron/ElectronPowerMonitor.ts";
import * as DesktopTelemetryPublisher from "./DesktopTelemetryPublisher.ts";
import * as DesktopRendererHistory from "./DesktopRendererHistory.ts";
+import * as DesktopShellEnvironment from "../shell/DesktopShellEnvironment.ts";
const layerHistory = Layer.succeed(DesktopRendererHistory.DesktopRendererHistory, {
register: () => Effect.void,
@@ -57,39 +61,66 @@ function layerElectronApp(
}
describe("DesktopTelemetryPublisher", () => {
- it.effect("stops when its scope closes during an Electron telemetry sample", () =>
- Effect.gen(function* () {
- const pollStarted = yield* Deferred.make<void>();
- const blockPoll = yield* Deferred.make<void>();
- const layerPower = Layer.succeed(
- ElectronPowerMonitor.ElectronPowerMonitor,
- ElectronPowerMonitor.ElectronPowerMonitor.of({
- isOnBatteryPower: Effect.succeed(false),
- getSystemIdleTime: Deferred.succeed(pollStarted, undefined).pipe(
- Effect.andThen(Deferred.await(blockPoll)),
- Effect.as(0),
+ it.effect(
+ "forwards shell discovery while a telemetry sample is pending and stops on scope close",
+ () =>
+ Effect.gen(function* () {
+ const pollStarted = yield* Deferred.make<void>();
+ const blockPoll = yield* Deferred.make<void>();
+ const layerPower = Layer.succeed(
+ ElectronPowerMonitor.ElectronPowerMonitor,
+ ElectronPowerMonitor.ElectronPowerMonitor.of({
+ isOnBatteryPower: Effect.succeed(false),
+ getSystemIdleTime: Deferred.succeed(pollStarted, undefined).pipe(
+ Effect.andThen(Deferred.await(blockPoll)),
+ Effect.as(0),
+ ),
+ getSystemIdleState: () => Effect.succeed("active"),
+ getCurrentThermalState: Effect.succeed("nominal"),
+ onSimpleEvent: () => Effect.void,
+ onThermalStateChange: () => Effect.void,
+ onSpeedLimitChange: () => Effect.void,
+ }),
+ );
+ const layer = DesktopTelemetryPublisher.layer.pipe(
+ Layer.provide(
+ Layer.mergeAll(
+ layerElectronApp([]),
+ layerPower,
+ layerHistory,
+ Layer.succeed(DesktopShellEnvironment.DesktopShellEnvironment, {
+ installIntoProcess: Effect.succeed(true),
+ awaitDiscovery: Effect.succeed(true),
+ updates: Stream.make({
+ version: 1,
+ type: "shellEnvironment",
+ discovered: true,
+ platform: "darwin",
+ environment: { PATH: "/discovered/bin" },
+ }),
+ }),
+ ),
),
- getSystemIdleState: () => Effect.succeed("active"),
- getCurrentThermalState: Effect.succeed("nominal"),
- onSimpleEvent: () => Effect.void,
- onThermalStateChange: () => Effect.void,
- onSpeedLimitChange: () => Effect.void,
- }),
- );
- const layer = DesktopTelemetryPublisher.layer.pipe(
- Layer.provide(Layer.mergeAll(layerElectronApp([]), layerPower, layerHistory)),
- );
- const scope = yield* Scope.make();
+ );
+ const scope = yield* Scope.make();
- yield* Layer.buildWithScope(layer, scope);
- yield* Deferred.await(pollStarted);
+ const context = yield* Layer.buildWithScope(layer, scope);
+ const publisher = Context.get(context, DesktopTelemetryPublisher.DesktopTelemetryPublisher);
+ const update = yield* publisher.encoded.pipe(
+ Stream.filter((bytes) => new TextDecoder().decode(bytes).includes("shellEnvironment")),
+ Stream.runHead,
+ );
+ assert.isTrue(Option.isSome(update));
+ if (Option.isSome(update))
+ assert.include(new TextDecoder().decode(update.value), "/discovered/bin");
+ yield* Deferred.await(pollStarted);
- const closeFiber = yield* Scope.close(scope, Exit.void).pipe(Effect.forkDetach);
- yield* Effect.yieldNow;
- yield* Effect.yieldNow;
+ const closeFiber = yield* Scope.close(scope, Exit.void).pipe(Effect.forkDetach);
+ yield* Effect.yieldNow;
+ yield* Effect.yieldNow;
- assert.isDefined(closeFiber.pollUnsafe());
- }),
+ assert.isDefined(closeFiber.pollUnsafe());
+ }),
);
});
diff --git a/apps/desktop/src/telemetry/DesktopTelemetryPublisher.ts b/apps/desktop/src/telemetry/DesktopTelemetryPublisher.ts
index 8800c4f4283..0a2b10e08c5 100644
--- a/apps/desktop/src/telemetry/DesktopTelemetryPublisher.ts
+++ b/apps/desktop/src/telemetry/DesktopTelemetryPublisher.ts
@@ -24,6 +24,7 @@ import * as Stream from "effect/Stream";
import * as ElectronApp from "../electron/ElectronApp.ts";
import * as ElectronPowerMonitor from "../electron/ElectronPowerMonitor.ts";
import * as DesktopRendererHistory from "./DesktopRendererHistory.ts";
+import * as DesktopShellEnvironment from "../shell/DesktopShellEnvironment.ts";
const LIVE_SAMPLE_INTERVAL = Duration.seconds(1);
const BATTERY_SAMPLE_INTERVAL = Duration.seconds(5);
@@ -146,6 +147,7 @@ export const make = Effect.fn("desktop.telemetryPublisher.make")(function* () {
const electronApp = yield* ElectronApp.ElectronApp;
const powerMonitor = yield* ElectronPowerMonitor.ElectronPowerMonitor;
const rendererHistory = yield* DesktopRendererHistory.DesktopRendererHistory;
+ const shellEnvironment = yield* DesktopShellEnvironment.DesktopShellEnvironment;
yield* electronApp.whenReady;
const initialPowerState: PowerState = {
@@ -400,7 +402,7 @@ export const make = Effect.fn("desktop.telemetryPublisher.make")(function* () {
type: "desktopTelemetryHello",
electronPid: process.pid,
} as const),
- Stream.merge(snapshots, updateReports),
+ Stream.merge(Stream.merge(snapshots, updateReports), shellEnvironment.updates),
).pipe(Stream.map((message) => textEncoder.encode(`${encodeMessage(message)}\n`)));
const publishUpdateReport: DesktopTelemetryPublisher["Service"]["publishUpdateReport"] = (
diff --git a/apps/server/src/resourceTelemetry/DesktopTelemetryReceiver.test.ts b/apps/server/src/resourceTelemetry/DesktopTelemetryReceiver.test.ts
index 616643c6e1e..fe014b82e2d 100644
--- a/apps/server/src/resourceTelemetry/DesktopTelemetryReceiver.test.ts
+++ b/apps/server/src/resourceTelemetry/DesktopTelemetryReceiver.test.ts
@@ -1,12 +1,195 @@
// @effect-diagnostics nodeBuiltinImport:off
import { it } from "@effect/vitest";
+import * as NodeTls from "node:tls";
+import * as NodeCrypto from "node:crypto";
+import * as NodeServices from "@effect/platform-node/NodeServices";
import * as Effect from "effect/Effect";
+import * as Cause from "effect/Cause";
+import * as FileSystem from "effect/FileSystem";
+import * as Logger from "effect/Logger";
+import * as Path from "effect/Path";
+import * as Stream from "effect/Stream";
+import { ChildProcess, ChildProcessSpawner } from "effect/process";
import { describe, expect } from "vite-plus/test";
-import { requireDesktopTelemetryWriteProgress } from "./DesktopTelemetryReceiver.ts";
+import {
+ makeApplyDesktopShellEnvironmentSnapshot,
+ requireDesktopTelemetryWriteProgress,
+} from "./DesktopTelemetryReceiver.ts";
describe("DesktopTelemetryReceiver", () => {
+ it.live(
+ "preserves system and bundled roots when the first snapshot withdraws overlapping extras",
+ () =>
+ Effect.gen(function* () {
+ const fs = yield* FileSystem.FileSystem;
+ const path = yield* Path.Path;
+ const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
+ const directory = yield* fs.makeTempDirectoryScoped();
+ const certificatePath = path.join(directory, "startup-extra-ca.pem");
+ yield* fs.writeFileString(
+ certificatePath,
+ [...NodeTls.getCACertificates("system"), ...NodeTls.rootCertificates].join("\n"),
+ );
+ const child = yield* spawner.spawn(
+ ChildProcess.make(
+ process.execPath,
+ [
+ "--use-bundled-ca",
+ "--use-system-ca",
+ "--input-type=module",
+ "--eval",
+ `
+import * as NodeAssert from "node:assert/strict";
+import * as NodeCrypto from "node:crypto";
+import * as NodeTls from "node:tls";
+import * as Effect from "effect/Effect";
+import * as NodeServices from "@effect/platform-node/NodeServices";
+import { makeApplyDesktopShellEnvironmentSnapshot } from ${JSON.stringify(new URL("./DesktopTelemetryReceiver.ts", import.meta.url).href)};
+const fingerprints = (certificates) => [...new Set(certificates.map((pem) => new NodeCrypto.X509Certificate(pem).fingerprint256))].sort();
+const expected = fingerprints([...NodeTls.getCACertificates("bundled"), ...NodeTls.getCACertificates("system")]);
+const applySnapshot = makeApplyDesktopShellEnvironmentSnapshot(process.env, process.platform);
+await Effect.runPromise(applySnapshot({
+ version: 1,
+ type: "shellEnvironment",
+ platform: process.platform,
+ discovered: true,
+ environment: { NODE_EXTRA_CA_CERTS: null },
+}).pipe(Effect.provide(NodeServices.layer)));
+NodeAssert.equal(process.env.NODE_EXTRA_CA_CERTS, undefined);
+NodeAssert.deepEqual(fingerprints(NodeTls.getCACertificates("default")), expected);
+`,
+ ],
+ {
+ cwd: path.resolve(import.meta.dirname, "../.."),
+ env: { NODE_OPTIONS: "", NODE_EXTRA_CA_CERTS: certificatePath },
+ },
+ ),
+ );
+ const result = yield* Effect.all(
+ {
+ stderr: child.stderr.pipe(Stream.decodeText(), Stream.mkString),
+ exitCode: child.exitCode.pipe(Effect.map(Number)),
+ },
+ { concurrency: "unbounded" },
+ );
+ expect(result.exitCode, result.stderr).toBe(0);
+ }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
+ );
+
+ it.effect("updates live TLS trust when shell certificates arrive after startup", () =>
+ Effect.gen(function* () {
+ const fs = yield* FileSystem.FileSystem;
+ const path = yield* Path.Path;
+ const directory = yield* fs.makeTempDirectoryScoped();
+ const certificatePath = path.join(directory, "extra-ca.pem");
+ const original = NodeTls.getCACertificates("default");
+ yield* Effect.addFinalizer(() =>
+ Effect.sync(() => NodeTls.setDefaultCACertificates(original)),
+ );
+ const baseline = NodeTls.rootCertificates[0]!;
+ const extra = NodeTls.rootCertificates[1]!;
+ const replacement = NodeTls.rootCertificates[2]!;
+ NodeTls.setDefaultCACertificates([baseline]);
+ yield* fs.writeFileString(certificatePath, `${extra}\n${replacement}`);
+ const environment: NodeJS.ProcessEnv = { T3CODE_DESKTOP_SHELL_DISCOVERY_PENDING: "1" };
+ const applySnapshot = makeApplyDesktopShellEnvironmentSnapshot(environment, "darwin");
+ const message = {
+ version: 1,
+ type: "shellEnvironment",
+ platform: "darwin",
+ discovered: true,
+ environment: { NODE_EXTRA_CA_CERTS: certificatePath },
+ } as const;
+ const fingerprints = (certificates: ReadonlyArray<string>) =>
+ certificates.map((pem) => new NodeCrypto.X509Certificate(pem).fingerprint256).sort();
+ yield* applySnapshot({ ...message, platform: "win32" });
+ expect(fingerprints(NodeTls.getCACertificates("default"))).toEqual(fingerprints([baseline]));
+ expect(environment.T3CODE_DESKTOP_SHELL_DISCOVERY_PENDING).toBe("1");
+ yield* applySnapshot(message);
+ expect(fingerprints(NodeTls.getCACertificates("default"))).toEqual(
+ fingerprints([baseline, extra, replacement]),
+ );
+ expect(environment.NODE_EXTRA_CA_CERTS).toBe(certificatePath);
+ expect(environment.T3CODE_DESKTOP_SHELL_DISCOVERY_PENDING).toBeUndefined();
+
+ yield* fs.writeFileString(certificatePath, replacement);
+ yield* applySnapshot(message);
+ expect(fingerprints(NodeTls.getCACertificates("default"))).toEqual(
+ fingerprints([baseline, replacement]),
+ );
+
+ yield* fs.writeFileString(
+ certificatePath,
+ "-----BEGIN CERTIFICATE-----\ninvalid\n-----END CERTIFICATE-----",
+ );
+ yield* applySnapshot(message);
+ expect(fingerprints(NodeTls.getCACertificates("default"))).toEqual(
+ fingerprints([baseline, replacement]),
+ );
+ yield* fs.remove(certificatePath);
+ const warnings: Array<Logger.Options<unknown>> = [];
+ yield* applySnapshot(message).pipe(
+ Effect.provide(
+ Logger.layer([Logger.make((entry) => warnings.push(entry))], {
+ mergeWithExisting: false,
+ }),
+ ),
+ );
+ expect(warnings).toHaveLength(1);
+ expect(warnings[0]!.message).toEqual(["Failed to update desktop shell TLS certificates."]);
+ expect(Cause.squash(warnings[0]!.cause)).toMatchObject({ _tag: "PlatformError" });
+ expect(fingerprints(NodeTls.getCACertificates("default"))).toEqual(
+ fingerprints([baseline, replacement]),
+ );
+
+ yield* applySnapshot({ ...message, environment: { NODE_EXTRA_CA_CERTS: null } });
+ expect(fingerprints(NodeTls.getCACertificates("default"))).toEqual(fingerprints([baseline]));
+ expect(environment.NODE_EXTRA_CA_CERTS).toBeUndefined();
+
+ NodeTls.setDefaultCACertificates([]);
+ yield* fs.writeFileString(certificatePath, extra);
+ const applyOpaqueTrust = makeApplyDesktopShellEnvironmentSnapshot(environment, "darwin");
+ yield* applyOpaqueTrust(message);
+ expect(NodeTls.getCACertificates("default")).toEqual([]);
+ expect(environment.NODE_EXTRA_CA_CERTS).toBe(certificatePath);
+ }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
+ );
+
+ it.effect.each([
+ { platform: "darwin", discovered: true, applied: true, pending: false },
+ { platform: "win32", discovered: true, applied: false, pending: true },
+ { platform: "darwin", discovered: false, applied: true, pending: true },
+ ])(
+ "applies platform=$platform discovered=$discovered with pending=$pending",
+ ({ platform, discovered, applied, pending }) =>
+ Effect.gen(function* () {
+ const environment: NodeJS.ProcessEnv = {
+ PATH: "/old/bin",
+ JAVA_HOME: "/old/java",
+ UNRELATED: "keep",
+ T3CODE_DESKTOP_SHELL_DISCOVERY_PENDING: "1",
+ };
+ yield* makeApplyDesktopShellEnvironmentSnapshot(
+ environment,
+ "darwin",
+ )({
+ version: 1,
+ type: "shellEnvironment",
+ platform,
+ discovered,
+ environment: { PATH: "/fresh/bin", JAVA_HOME: null },
+ });
+ expect(environment).toEqual({
+ PATH: applied ? "/fresh/bin" : "/old/bin",
+ ...(applied ? {} : { JAVA_HOME: "/old/java" }),
+ UNRELATED: "keep",
+ ...(pending ? { T3CODE_DESKTOP_SHELL_DISCOVERY_PENDING: "1" } : {}),
+ });
+ }).pipe(Effect.provide(NodeServices.layer)),
+ );
+
it.effect("models a zero-byte control write as a stalled descriptor", () =>
Effect.gen(function* () {
const error = yield* requireDesktopTelemetryWriteProgress(7, 42, 0).pipe(Effect.flip);
diff --git a/apps/server/src/resourceTelemetry/DesktopTelemetryReceiver.ts b/apps/server/src/resourceTelemetry/DesktopTelemetryReceiver.ts
index 70dc2e8d750..7d9de87ecfd 100644
--- a/apps/server/src/resourceTelemetry/DesktopTelemetryReceiver.ts
+++ b/apps/server/src/resourceTelemetry/DesktopTelemetryReceiver.ts
@@ -1,22 +1,28 @@
// @effect-diagnostics nodeBuiltinImport:off
import * as NodeFS from "node:fs";
import * as NodeNet from "node:net";
+import * as NodeTls from "node:tls";
import * as NodeStream from "@effect/platform-node/NodeStream";
import {
DesktopHostTelemetryMessage,
type DesktopHostTelemetryMessage as DesktopHostTelemetryMessageValue,
type DesktopHostTelemetrySnapshot,
+ type DesktopShellEnvironmentSnapshot,
DesktopTelemetryControlMessage,
type DesktopUpdateChannel,
type DesktopUpdateStatusReport,
type ResourceTelemetrySourceStatus,
} from "@t3tools/contracts";
import { resolveServerBackgroundActivitySettings } from "@t3tools/shared/backgroundActivitySettings";
+import * as HostProcess from "@t3tools/shared/HostProcess";
+import { applyShellEnvironment } from "@t3tools/shared/shell";
+import * as Cause from "effect/Cause";
import * as Context from "effect/Context";
import * as DateTime from "effect/DateTime";
import * as Duration from "effect/Duration";
import * as Effect from "effect/Effect";
+import * as FileSystem from "effect/FileSystem";
import * as Layer from "effect/Layer";
import * as Option from "effect/Option";
import * as PubSub from "effect/PubSub";
@@ -202,6 +208,70 @@ export class DesktopTelemetryReceiver extends Context.Service<
}
>()("t3/resourceTelemetry/DesktopTelemetryReceiver") {}
+class DesktopShellCertificatesUpdateFailed extends Schema.TaggedError<DesktopShellCertificatesUpdateFailed>()(
+ "DesktopShellCertificatesUpdateFailed",
+ { cause: Schema.Defect() },
+) {
+ override get message(): string {
+ return "Failed to update desktop shell TLS certificates.";
+ }
+}
+
+export function makeApplyDesktopShellEnvironmentSnapshot(
+ environment: NodeJS.ProcessEnv,
+ platform: NodeJS.Platform,
+) {
+ let baseCertificates: Array<string> | undefined;
+ return Effect.fn("resourceTelemetry.desktopTelemetryReceiver.applyShellEnvironment")(function* (
+ message: DesktopShellEnvironmentSnapshot,
+ ) {
+ if (message.platform !== platform) return;
+ // Node's first default-CA enumeration consults process.env, so capture it before the patch.
+ if (message.environment.NODE_EXTRA_CA_CERTS !== undefined && baseCertificates === undefined) {
+ // Remove only the startup extras, preserving duplicates from bundled or system trust.
+ const extras = [...NodeTls.getCACertificates("extra")];
+ baseCertificates = NodeTls.getCACertificates("default").filter((certificate) => {
+ const index = extras.indexOf(certificate);
+ if (index === -1) return true;
+ extras.splice(index, 1);
+ return false;
+ });
+ }
+ applyShellEnvironment(environment, message.environment, platform);
+ if (message.environment.NODE_EXTRA_CA_CERTS !== undefined) {
+ yield* Effect.gen(function* () {
+ const certificates = baseCertificates;
+ // --use-openssl-ca roots are not enumerable; replacing the list would discard them.
+ if (certificates === undefined || certificates.length === 0) {
+ yield* Effect.logWarning(
+ "Preserving non-enumerable TLS trust. Restart to apply updated desktop shell certificates.",
+ );
+ return;
+ }
+ const fs = yield* FileSystem.FileSystem;
+ const file = message.environment.NODE_EXTRA_CA_CERTS;
+ const bundle = file ? yield* fs.readFileString(file) : undefined;
+ yield* Effect.try({
+ try: () => {
+ const extras =
+ bundle?.match(/-----BEGIN CERTIFICATE-----[\s\S]*?-----END CERTIFICATE-----/g) ?? [];
+ if (bundle?.trim() && extras.length === 0) {
+ throw new Error("No PEM certificates found.");
+ }
+ NodeTls.setDefaultCACertificates([...certificates, ...extras]);
+ },
+ catch: (cause) => new DesktopShellCertificatesUpdateFailed({ cause }),
+ });
+ }).pipe(
+ Effect.catch((cause) =>
+ Effect.logWarning("Failed to update desktop shell TLS certificates.", Cause.fail(cause)),
+ ),
+ );
+ }
+ if (message.discovered) delete environment.T3CODE_DESKTOP_SHELL_DISCOVERY_PENDING;
+ });
+}
+
const decodeMessage = Schema.decodeUnknownEffect(DesktopHostTelemetryMessage);
const encodeControlMessage = Schema.encodeEffect(
Schema.fromJsonString(DesktopTelemetryControlMessage),
@@ -328,6 +398,9 @@ export function requireDesktopTelemetryWriteProgress(
/** @public */
export const make = Effect.fn("resourceTelemetry.desktopTelemetryReceiver.make")(function* () {
const config = yield* ServerConfig.ServerConfig;
+ const environment = yield* HostProcess.Environment;
+ const platform = yield* HostProcess.Platform;
+ const applyShellSnapshot = makeApplyDesktopShellEnvironmentSnapshot(environment, platform);
const serverSettings = yield* ServerSettings.ServerSettingsService;
const latest = yield* Ref.make(Option.none<DesktopHostTelemetrySnapshot>());
const receiverStartedAt = yield* DateTime.now;
@@ -523,6 +596,10 @@ export const make = Effect.fn("resourceTelemetry.desktopTelemetryReceiver.make")
);
}
+ if (message.type === "shellEnvironment") {
+ return recordContact.pipe(Effect.andThen(applyShellSnapshot(message)));
+ }
+
if (message.type === "desktopUpdateStatus") {
return recordContact.pipe(
Effect.andThen(Ref.set(latestUpdateReport, Option.some(message))),
diff --git a/apps/server/src/telemetry/AnalyticsService.test.ts b/apps/server/src/telemetry/AnalyticsService.test.ts
index 417841cb53e..eb5858c4ba4 100644
--- a/apps/server/src/telemetry/AnalyticsService.test.ts
+++ b/apps/server/src/telemetry/AnalyticsService.test.ts
@@ -165,6 +165,32 @@ const sendFor = (
});
it.layer(NodeServices.layer)("AnalyticsService test", (it) => {
+ it.effect("waits for desktop shell consent and honors a late telemetry opt-out", () =>
+ Effect.gen(function* () {
+ const env: NodeJS.ProcessEnv = { T3CODE_DESKTOP_SHELL_DISCOVERY_PENDING: "1" };
+ const batches = yield* sendFor(
+ 0,
+ respondWithStatus(204),
+ Effect.gen(function* () {
+ const analytics = yield* AnalyticsService.AnalyticsService;
+ yield* analytics.record("test.before-discovery");
+ yield* analytics.flush;
+ env.T3CODE_TELEMETRY_ENABLED = "false";
+ delete env.T3CODE_DESKTOP_SHELL_DISCOVERY_PENDING;
+ yield* analytics.record("test.opted-out");
+ yield* analytics.flush;
+ env.T3CODE_TELEMETRY_ENABLED = "true";
+ yield* analytics.record("test.allowed");
+ yield* analytics.flush;
+ }),
+ ).pipe(Effect.provideService(HostProcess.Environment, env));
+ assert.deepEqual(
+ batches.flat().map((event) => event.event),
+ ["test.allowed"],
+ );
+ }),
+ );
+
it.effect("client owner reports cannot use up the server's owner cap", () =>
Effect.gen(function* () {
const batches = yield* sendFor(
diff --git a/apps/server/src/telemetry/AnalyticsService.ts b/apps/server/src/telemetry/AnalyticsService.ts
index 4edc6621b0c..07b6f604e08 100644
--- a/apps/server/src/telemetry/AnalyticsService.ts
+++ b/apps/server/src/telemetry/AnalyticsService.ts
@@ -3,6 +3,7 @@ import * as HostProcess from "@t3tools/shared/HostProcess";
import type { ClientOs } from "@t3tools/contracts";
import * as Clock from "effect/Clock";
import * as Config from "effect/Config";
+import * as ConfigProvider from "effect/ConfigProvider";
import * as Context from "effect/Context";
import * as Crypto from "effect/Crypto";
import * as DateTime from "effect/DateTime";
@@ -116,6 +117,26 @@ function serverOsFromNodePlatform(platform: string): ClientOs {
/** @public */
export const make = Effect.gen(function* () {
const telemetryConfig = yield* TelemetryEnvConfig;
+ const hostEnvironment = yield* HostProcess.Environment;
+ const awaitingShellConsent = hostEnvironment.T3CODE_DESKTOP_SHELL_DISCOVERY_PENDING === "1";
+ // A cached PATH does not establish consent from the current login shell.
+ const telemetryEnabled = Effect.suspend(() => {
+ if (!telemetryConfig.enabled) return Effect.succeed(false);
+ if (!awaitingShellConsent) return Effect.succeed(true);
+ if (hostEnvironment.T3CODE_DESKTOP_SHELL_DISCOVERY_PENDING === "1")
+ return Effect.succeed(false);
+ const preference = hostEnvironment.T3CODE_TELEMETRY_ENABLED;
+ return Config.Boolean("T3CODE_TELEMETRY_ENABLED").pipe(
+ Config.withDefault(true),
+ Effect.provideService(
+ ConfigProvider.ConfigProvider,
+ ConfigProvider.fromEnv({
+ env: preference === undefined ? {} : { T3CODE_TELEMETRY_ENABLED: preference },
+ }),
+ ),
+ Effect.orElseSucceed(() => false),
+ );
+ });
const httpClient = yield* HttpClient.HttpClient;
const serverConfig = yield* ServerConfig.ServerConfig;
const { distinctId, installId } = yield* getTelemetryIdentity;
@@ -172,7 +193,7 @@ export const make = Effect.gen(function* () {
const sendBatch = Effect.fn("AnalyticsService.sendBatch")(function* (
events: ReadonlyArray<BufferedAnalyticsEvent>,
) {
- if (!telemetryConfig.enabled || !distinctId) return;
+ if (!(yield* telemetryEnabled) || !distinctId) return;
const payload = {
api_key: telemetryConfig.posthogKey,
@@ -284,7 +305,7 @@ export const make = Effect.gen(function* () {
const record: AnalyticsService["Service"]["record"] = Effect.fn("AnalyticsService.record")(
function* (event, properties) {
- if (!telemetryConfig.enabled || !distinctId) return;
+ if (!(yield* telemetryEnabled) || !distinctId) return;
// Catch Node UUID defects too; best-effort telemetry drops events without UUIDs.
const uuid = yield* Effect.exit(crypto.randomUUIDv7());
diff --git a/apps/server/src/usage/UsageService.test.ts b/apps/server/src/usage/UsageService.test.ts
index fbb1aaf8726..32ef901326f 100644
--- a/apps/server/src/usage/UsageService.test.ts
+++ b/apps/server/src/usage/UsageService.test.ts
@@ -2639,6 +2639,7 @@ describe("UsageService", () => {
it.live("reports saved usage of a removed directory only for windows it reaches", () =>
Effect.gen(function* () {
+ yield* TestClock.setTime(Date.parse("2026-08-12T00:00:00Z"));
const { transcript, settings, home } = yield* setup;
// Saved usage is pruned after the retention window, so pin "now" near the write.
yield* TestClock.setTime(Date.parse("2026-08-15T00:00:00Z"));
diff --git a/packages/contracts/src/resourceTelemetry.ts b/packages/contracts/src/resourceTelemetry.ts
index 5a004f93a2a..5e7045515f1 100644
--- a/packages/contracts/src/resourceTelemetry.ts
+++ b/packages/contracts/src/resourceTelemetry.ts
@@ -295,8 +295,48 @@ export const DesktopUpdateStatusReport = Schema.Struct({
});
export type DesktopUpdateStatusReport = typeof DesktopUpdateStatusReport.Type;
+export const ShellEnvironmentPatch = Schema.Struct({
+ PATH: Schema.optionalKey(Schema.NullOr(Schema.String)),
+ DBUS_SESSION_BUS_ADDRESS: Schema.optionalKey(Schema.NullOr(Schema.String)),
+ DISPLAY: Schema.optionalKey(Schema.NullOr(Schema.String)),
+ LANG: Schema.optionalKey(Schema.NullOr(Schema.String)),
+ LC_ALL: Schema.optionalKey(Schema.NullOr(Schema.String)),
+ LC_CTYPE: Schema.optionalKey(Schema.NullOr(Schema.String)),
+ SSH_AUTH_SOCK: Schema.optionalKey(Schema.NullOr(Schema.String)),
+ HOMEBREW_PREFIX: Schema.optionalKey(Schema.NullOr(Schema.String)),
+ HOMEBREW_CELLAR: Schema.optionalKey(Schema.NullOr(Schema.String)),
+ HOMEBREW_REPOSITORY: Schema.optionalKey(Schema.NullOr(Schema.String)),
+ XDG_CONFIG_HOME: Schema.optionalKey(Schema.NullOr(Schema.String)),
+ XDG_CURRENT_DESKTOP: Schema.optionalKey(Schema.NullOr(Schema.String)),
+ XDG_DATA_HOME: Schema.optionalKey(Schema.NullOr(Schema.String)),
+ XDG_RUNTIME_DIR: Schema.optionalKey(Schema.NullOr(Schema.String)),
+ XDG_SESSION_DESKTOP: Schema.optionalKey(Schema.NullOr(Schema.String)),
+ XDG_SESSION_TYPE: Schema.optionalKey(Schema.NullOr(Schema.String)),
+ WAYLAND_DISPLAY: Schema.optionalKey(Schema.NullOr(Schema.String)),
+ SSL_CERT_FILE: Schema.optionalKey(Schema.NullOr(Schema.String)),
+ SSL_CERT_DIR: Schema.optionalKey(Schema.NullOr(Schema.String)),
+ NODE_EXTRA_CA_CERTS: Schema.optionalKey(Schema.NullOr(Schema.String)),
+ T3CODE_TELEMETRY_ENABLED: Schema.optionalKey(Schema.NullOr(Schema.String)),
+ T3CODE_GITHUB_APP_CLIENT_ID: Schema.optionalKey(Schema.NullOr(Schema.String)),
+ __MISE_DIFF: Schema.optionalKey(Schema.NullOr(Schema.String)),
+ JAVA_HOME: Schema.optionalKey(Schema.NullOr(Schema.String)),
+ FNM_DIR: Schema.optionalKey(Schema.NullOr(Schema.String)),
+ FNM_MULTISHELL_PATH: Schema.optionalKey(Schema.NullOr(Schema.String)),
+});
+export type ShellEnvironmentPatch = typeof ShellEnvironmentPatch.Type;
+
+export const DesktopShellEnvironmentSnapshot = Schema.Struct({
+ version: Schema.Literal(1),
+ type: Schema.Literal("shellEnvironment"),
+ discovered: Schema.Boolean,
+ platform: Schema.String,
+ environment: ShellEnvironmentPatch,
+});
+export type DesktopShellEnvironmentSnapshot = typeof DesktopShellEnvironmentSnapshot.Type;
+
export const DesktopHostTelemetryMessage = Schema.Union([
DesktopHostTelemetryHello,
+ DesktopShellEnvironmentSnapshot,
DesktopHostTelemetrySnapshot,
DesktopUpdateStatusReport,
]);
diff --git a/packages/shared/src/shell.test.ts b/packages/shared/src/shell.test.ts
index 690e6d94523..a4a17cda2cd 100644
--- a/packages/shared/src/shell.test.ts
+++ b/packages/shared/src/shell.test.ts
@@ -2,6 +2,8 @@
import * as NodeServices from "@effect/platform-node/NodeServices";
import { it as effectIt } from "@effect/vitest";
import * as HostProcess from "@t3tools/shared/HostProcess";
+import { DesktopHostTelemetryMessage } from "@t3tools/contracts";
+import * as Schema from "effect/Schema";
import * as Effect from "effect/Effect";
import * as Fiber from "effect/Fiber";
import * as FileSystem from "effect/FileSystem";
@@ -14,6 +16,7 @@ import * as NodePath from "node:path";
import { describe, expect, it, vi } from "vite-plus/test";
import {
+ applyShellEnvironment,
CommandAvailability,
CommandResolutionCache,
type CommandAvailabilityChecker,
@@ -35,6 +38,8 @@ import {
XcodeGitLocation,
} from "./shell.ts";
+const decodeDesktopHostMessage = Schema.decodeUnknownSync(DesktopHostTelemetryMessage);
+
const nativeProbe = vi.hoisted(() => ({
execFile: vi.fn<typeof import("node:child_process").execFile>(),
}));
@@ -1045,3 +1050,46 @@ effectIt.layer(NodeServices.layer)("resolveWindowsEnvironment", (it) => {
}),
);
});
+
+describe("desktop shell environment updates", () => {
+ it.each(["win32", "linux", "darwin"] as const)(
+ "applies shell updates with %s PATH semantics",
+ (platform) => {
+ const message = decodeDesktopHostMessage({
+ version: 1,
+ type: "shellEnvironment",
+ discovered: true,
+ platform,
+ environment: {
+ PATH: "C:\\fresh",
+ FNM_DIR: null,
+ __MISE_DIFF: null,
+ SSH_AUTH_SOCK: "/current.sock",
+ NODE_OPTIONS: "--bad",
+ },
+ });
+ expect(message.type).toBe("shellEnvironment");
+ if (message.type !== "shellEnvironment") throw new Error("Expected shell environment update");
+ const env: NodeJS.ProcessEnv = {
+ Path: "C:\\stale",
+ FNM_DIR: "stale",
+ __MISE_DIFF: "stale",
+ UNRELATED: "keep",
+ };
+ applyShellEnvironment(env, message.environment, platform);
+ expect(env).toEqual({
+ PATH: "C:\\fresh",
+ ...(platform === "win32" ? {} : { Path: "C:\\stale" }),
+ SSH_AUTH_SOCK: "/current.sock",
+ UNRELATED: "keep",
+ });
+ env.Path = "C:\\stale";
+ env.path = "C:\\other";
+ applyShellEnvironment(env, { PATH: null, SSH_AUTH_SOCK: null }, platform);
+ expect(env).toEqual({
+ ...(platform === "win32" ? {} : { Path: "C:\\stale", path: "C:\\other" }),
+ UNRELATED: "keep",
+ });
+ },
+ );
+});
diff --git a/packages/shared/src/shell.ts b/packages/shared/src/shell.ts
index 73c54d1f8b8..c5e6a7346fb 100644
--- a/packages/shared/src/shell.ts
+++ b/packages/shared/src/shell.ts
@@ -10,6 +10,7 @@ import * as FileSystem from "effect/FileSystem";
import * as Option from "effect/Option";
import * as Path from "effect/Path";
+import type { ShellEnvironmentPatch } from "@t3tools/contracts";
import * as HostProcess from "./HostProcess.ts";
import * as Context from "effect/Context";
const SHELL_ENV_NAME_PATTERN = /^[A-Z0-9_]+$/;
@@ -908,13 +909,13 @@ function resolveKnownWindowsCliDirs(env: NodeJS.ProcessEnv): ReadonlyArray<strin
}
/** Keep one PATH spelling in copied Windows environment records. */
-export function setWindowsPath(env: NodeJS.ProcessEnv, value: string): void {
+export function setWindowsPath(env: NodeJS.ProcessEnv, value: string | null): void {
for (const key of Object.keys(env)) {
if (key.toUpperCase() === "PATH") {
delete env[key];
}
}
- env.PATH = value;
+ if (value !== null) env.PATH = value;
}
function mergeWindowsEnv(
@@ -965,3 +966,15 @@ export const resolveWindowsEnvironment = Effect.fn("shell.resolveWindowsEnvironm
? { ...baselinePatch, ...profiledPatch }
: baselinePatch;
});
+
+export function applyShellEnvironment(
+ env: NodeJS.ProcessEnv,
+ patch: ShellEnvironmentPatch,
+ platform: NodeJS.Platform,
+): void {
+ for (const [key, value] of Object.entries(patch)) {
+ if (key === "PATH" && platform === "win32") setWindowsPath(env, value);
+ else if (value === null) delete env[key];
+ else env[key] = value;
+ }
+}