@devries-cameron20 / t3code-1 / commits / 5b1b35c773
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

[codex] Preserve desktop shell environment probe failures (#3383)

Co-authored-by: codex <codex@users.noreply.github.com>

Julius Marminge committed Jun 20, 2026 at 11:28 UTC 5b1b35c773e8ad4a85ea2f3dd277b698bf329f21
2 files changed +136 -5
apps/desktop/src/shell/DesktopShellEnvironment.test.ts
+55 -2
@@ -1,15 +1,23 @@
1 import { assert, describe, it } from "@effect/vitest";
2 import * as Effect from "effect/Effect";
3 import * as Layer from "effect/Layer";
4 +import * as Logger from "effect/Logger";
5 +import * as PlatformError from "effect/PlatformError";
6 +import * as Schema from "effect/Schema";
7 import * as Sink from "effect/Sink";
8 import * as Stream from "effect/Stream";
6 -import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process";
9 +import * as ChildProcess from "effect/unstable/process/ChildProcess";
10 +import * as ChildProcessSpawner from "effect/unstable/process/ChildProcessSpawner";
11
12 import * as DesktopEnvironment from "../app/DesktopEnvironment.ts";
13 import * as DesktopShellEnvironment from "./DesktopShellEnvironment.ts";
14
15 const textEncoder = new TextEncoder();
16
17 +const isDesktopShellEnvironmentCommandError = Schema.is(
18 + DesktopShellEnvironment.DesktopShellEnvironmentCommandError,
19 +);
20 +
21 function envOutput(values: Readonly<Record<string, string>>): string {
22 return Object.entries(values)
23 .flatMap(([name, value]) => [
@@ -59,6 +67,7 @@ function runShellEnvironment(input: {
67 readonly env: NodeJS.ProcessEnv;
68 readonly platform: NodeJS.Platform;
69 readonly handler: (command: ChildProcess.Command) => string;
70 + readonly failure?: PlatformError.PlatformError;
71 }) {
72 const environmentLayer = Layer.succeed(
73 DesktopEnvironment.DesktopEnvironment,
@@ -68,7 +77,11 @@ function runShellEnvironment(input: {
77 );
78 const spawnerLayer = Layer.succeed(
79 ChildProcessSpawner.ChildProcessSpawner,
71 - ChildProcessSpawner.make((command) => Effect.succeed(makeProcess(input.handler(command)))),
80 + ChildProcessSpawner.make((command) =>
81 + input.failure === undefined
82 + ? Effect.succeed(makeProcess(input.handler(command)))
83 + : Effect.fail(input.failure),
84 + ),
85 );
86
87 const program = Effect.gen(function* () {
@@ -229,4 +242,44 @@ describe("DesktopShellEnvironment", () => {
242 );
243 }),
244 );
245 +
246 + it.effect("logs command failures with safe probe context and the exact cause", () => {
247 + const env: NodeJS.ProcessEnv = {
248 + SHELL: "/bin/bash",
249 + PATH: "/usr/bin",
250 + };
251 + const cause = PlatformError.systemError({
252 + _tag: "PermissionDenied",
253 + module: "ChildProcess",
254 + method: "spawn",
255 + pathOrDescriptor: "/bin/bash",
256 + });
257 + const messages: Array<unknown> = [];
258 + const logger = Logger.make(({ message }) => {
259 + messages.push(message);
260 + });
261 +
262 + return runShellEnvironment({
263 + env,
264 + platform: "linux",
265 + handler: () => "",
266 + failure: cause,
267 + }).pipe(
268 + Effect.andThen(
269 + Effect.sync(() => {
270 + const errors = messages
271 + .flatMap((message) => (Array.isArray(message) ? message : [message]))
272 + .filter(isDesktopShellEnvironmentCommandError);
273 + assert.lengthOf(errors, 1);
274 + assert.equal(errors[0]?.probe, "login-shell");
275 + assert.equal(errors[0]?.executable, "bash");
276 + assert.equal(errors[0]?.argumentCount, 2);
277 + assert.notProperty(errors[0] ?? {}, "args");
278 + assert.equal(errors[0]?.cause, cause);
279 + assert.notInclude(errors[0]?.message ?? "", cause.message);
280 + }),
281 + ),
282 + Effect.provide(Logger.layer([logger], { mergeWithExisting: false })),
283 + );
284 + });
285 });
apps/desktop/src/shell/DesktopShellEnvironment.ts
+81 -3
@@ -3,6 +3,7 @@ import * as Duration from "effect/Duration";
3 import * as Effect from "effect/Effect";
4 import * as Layer from "effect/Layer";
5 import * as Option from "effect/Option";
6 +import * as Schema from "effect/Schema";
7 import * as ChildProcess from "effect/unstable/process/ChildProcess";
8 import * as ChildProcessSpawner from "effect/unstable/process/ChildProcessSpawner";
9
@@ -20,6 +21,44 @@ interface WindowsProbeOptions {
21 readonly loadProfile: boolean;
22 }
23
24 +const DesktopShellEnvironmentProbe = Schema.Literals([
25 + "login-shell",
26 + "launchctl-path",
27 + "powershell-profile",
28 + "powershell-no-profile",
29 +]);
30 +type DesktopShellEnvironmentProbe = typeof DesktopShellEnvironmentProbe.Type;
31 +
32 +const desktopShellEnvironmentCommandFields = {
33 + probe: DesktopShellEnvironmentProbe,
34 + executable: Schema.String,
35 + argumentCount: Schema.Number,
36 +};
37 +
38 +export class DesktopShellEnvironmentCommandError extends Schema.TaggedErrorClass<DesktopShellEnvironmentCommandError>()(
39 + "DesktopShellEnvironmentCommandError",
40 + {
41 + ...desktopShellEnvironmentCommandFields,
42 + cause: Schema.Defect(),
43 + },
44 +) {
45 + override get message(): string {
46 + return `Desktop shell environment ${this.probe} probe (${this.executable}) failed.`;
47 + }
48 +}
49 +
50 +export class DesktopShellEnvironmentCommandTimeoutError extends Schema.TaggedErrorClass<DesktopShellEnvironmentCommandTimeoutError>()(
51 + "DesktopShellEnvironmentCommandTimeoutError",
52 + {
53 + ...desktopShellEnvironmentCommandFields,
54 + timeoutMs: Schema.Number,
55 + },
56 +) {
57 + override get message(): string {
58 + return `Desktop shell environment ${this.probe} probe (${this.executable}) timed out after ${this.timeoutMs}ms.`;
59 + }
60 +}
61 +
62 export class DesktopShellEnvironment extends Context.Service<
63 DesktopShellEnvironment,
64 {
@@ -127,6 +166,18 @@ const knownWindowsCliDirs = (env: NodeJS.ProcessEnv): ReadonlyArray<string> => [
166 const startMarker = (name: string) => `__T3CODE_ENV_${name}_START__`;
167 const endMarker = (name: string) => `__T3CODE_ENV_${name}_END__`;
168
169 +const executableName = (command: string): string => command.split(/[\\/]/u).at(-1) ?? command;
170 +
171 +const logShellEnvironmentCommandError = (
172 + error: DesktopShellEnvironmentCommandError | DesktopShellEnvironmentCommandTimeoutError,
173 +) =>
174 + Effect.logWarning(error).pipe(
175 + Effect.annotateLogs({
176 + component: "desktop-shell-environment",
177 + error,
178 + }),
179 + );
180 +
181 const capturePosixEnvironmentCommand = (names: ReadonlyArray<string>) =>
182 names
183 .map((name) => {
@@ -175,13 +226,14 @@ const extractEnvironment = (output: string, names: ReadonlyArray<string>): Envir
226 };
227
228 const runCommandOutput = Effect.fn("desktop.shellEnvironment.runCommandOutput")(function* (input: {
229 + readonly probe: DesktopShellEnvironmentProbe;
230 readonly command: string;
231 readonly args: ReadonlyArray<string>;
232 readonly timeout: Duration.Duration;
233 readonly shell?: boolean;
234 }): Effect.fn.Return<string, never, ChildProcessSpawner.ChildProcessSpawner> {
235 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
184 - return yield* spawner
236 + const output = yield* spawner
237 .string(
238 ChildProcess.make(input.command, input.args, {
239 shell: input.shell ?? false,
@@ -193,10 +245,33 @@ const runCommandOutput = Effect.fn("desktop.shellEnvironment.runCommandOutput")(
245 }),
246 )
247 .pipe(
248 + Effect.mapError(
249 + (cause) =>
250 + new DesktopShellEnvironmentCommandError({
251 + probe: input.probe,
252 + executable: executableName(input.command),
253 + argumentCount: input.args.length,
254 + cause,
255 + }),
256 + ),
257 + Effect.catchTags({
258 + DesktopShellEnvironmentCommandError: (error) =>
259 + logShellEnvironmentCommandError(error).pipe(Effect.as("")),
260 + }),
261 Effect.timeoutOption(input.timeout),
197 - Effect.map(Option.getOrElse(() => "")),
198 - Effect.orElseSucceed(() => ""),
262 );
263 + if (Option.isSome(output)) {
264 + return output.value;
265 + }
266 +
267 + const error = new DesktopShellEnvironmentCommandTimeoutError({
268 + probe: input.probe,
269 + executable: executableName(input.command),
270 + argumentCount: input.args.length,
271 + timeoutMs: Duration.toMillis(input.timeout),
272 + });
273 + yield* logShellEnvironmentCommandError(error);
274 + return "";
275 });
276
277 const readLoginShellEnvironment = (
@@ -206,12 +281,14 @@ const readLoginShellEnvironment = (
281 names.length === 0
282 ? Effect.succeed({})
283 : runCommandOutput({
284 + probe: "login-shell",
285 command: shell,
286 args: ["-ilc", capturePosixEnvironmentCommand(names)],
287 timeout: LOGIN_SHELL_TIMEOUT,
288 }).pipe(Effect.map((output) => extractEnvironment(output, names)));
289
290 const readLaunchctlPath = runCommandOutput({
291 + probe: "launchctl-path",
292 command: "/bin/launchctl",
293 args: ["getenv", "PATH"],
294 timeout: LAUNCHCTL_TIMEOUT,
@@ -234,6 +311,7 @@ const readWindowsEnvironment = Effect.fn("desktop.shellEnvironment.readWindowsEn
311
312 for (const command of WINDOWS_SHELL_CANDIDATES) {
313 const output = yield* runCommandOutput({
314 + probe: options.loadProfile ? "powershell-profile" : "powershell-no-profile",
315 command,
316 args,
317 timeout: LOGIN_SHELL_TIMEOUT,