@devries-cameron20 / t3code-1 / commits / d8bf307d22
Fork 0

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

[codex] Enrich process runner errors (#3268)

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

Julius Marminge committed Jun 20, 2026 at 11:37 UTC d8bf307d22663a3c458dc521a014fd21bdb60b98
3 files changed +135 -24
apps/server/src/environment/ServerEnvironmentLabel.test.ts
+1 -1
@@ -138,7 +138,7 @@ describe("resolveServerEnvironmentLabel", () => {
138 Effect.fail(
139 new ProcessRunner.ProcessSpawnError({
140 command: "scutil",
141 - args: ["--get", "ComputerName"],
141 + argumentCount: 2,
142 cause: new Error("spawn scutil ENOENT"),
143 }),
144 ),
apps/server/src/processRunner.test.ts
+89 -3
@@ -4,6 +4,7 @@ import * as Duration from "effect/Duration";
4 import * as Effect from "effect/Effect";
5 import * as Fiber from "effect/Fiber";
6 import * as Layer from "effect/Layer";
7 +import * as PlatformError from "effect/PlatformError";
8 import * as Sink from "effect/Sink";
9 import * as Stream from "effect/Stream";
10 import { TestClock } from "effect/testing";
@@ -55,7 +56,9 @@ function makeHandle(input: {
56 }
57
58 function makeSpawner(
58 - f: (command: ChildProcessCommand) => Effect.Effect<ChildProcessSpawner.ChildProcessHandle>,
59 + f: (
60 + command: ChildProcessCommand,
61 + ) => Effect.Effect<ChildProcessSpawner.ChildProcessHandle, PlatformError.PlatformError>,
62 ) {
63 return ChildProcessSpawner.make((command) => f(asChildProcessCommand(command)));
64 }
@@ -159,6 +162,44 @@ describe("runProcess", () => {
162 );
163 });
164
165 + it.effect("preserves resolved spawn context and cause", () =>
166 + Effect.gen(function* () {
167 + const cause = PlatformError.systemError({
168 + _tag: "PermissionDenied",
169 + module: "ChildProcessSpawner",
170 + method: "spawn",
171 + pathOrDescriptor: "/actual/fake",
172 + });
173 + const spawner = makeSpawner(() => Effect.fail(cause));
174 +
175 + const error = yield* runWith(spawner)({
176 + command: "fake",
177 + args: ["--flag", "secret-token-value"],
178 + cwd: "/logical",
179 + spawnCwd: "/actual",
180 + }).pipe(Effect.flip);
181 +
182 + expect(error._tag).toBe("ProcessSpawnError");
183 + if (error._tag !== "ProcessSpawnError") {
184 + return expect.fail("Expected ProcessSpawnError");
185 + }
186 + expect(error).toMatchObject({
187 + command: "fake",
188 + argumentCount: 2,
189 + cwd: "/logical",
190 + spawnCwd: "/actual",
191 + resolvedCommand: "fake",
192 + resolvedArgumentCount: 2,
193 + shell: false,
194 + });
195 + expect(error.cause).toBe(cause);
196 + expect(error.message).toBe("Failed to spawn process 'fake' in '/actual'");
197 + expect(error).not.toHaveProperty("args");
198 + expect(error).not.toHaveProperty("resolvedArgs");
199 + expect(error.message).not.toContain("secret-token-value");
200 + }),
201 + );
202 +
203 it.effect("fails when output exceeds max buffer in default mode", () =>
204 Effect.gen(function* () {
205 const spawner = makeSpawner(() => Effect.succeed(makeHandle({ stdout: "x".repeat(2048) })));
@@ -169,7 +210,39 @@ describe("runProcess", () => {
210 maxOutputBytes: 128,
211 }).pipe(Effect.flip);
212
172 - expect(error).toBeInstanceOf(ProcessRunner.ProcessOutputLimitError);
213 + expect(error._tag).toBe("ProcessOutputLimitError");
214 + if (error._tag !== "ProcessOutputLimitError") {
215 + return expect.fail("Expected ProcessOutputLimitError");
216 + }
217 + expect(error).toMatchObject({
218 + stream: "stdout",
219 + maxBytes: 128,
220 + observedBytes: 2048,
221 + });
222 + expect(error.message).toBe(
223 + "Process 'fake' stdout produced 2048 bytes, exceeding the 128 byte limit",
224 + );
225 + }),
226 + );
227 +
228 + it.effect("accepts output at the byte limit followed by an empty chunk", () =>
229 + Effect.gen(function* () {
230 + const output = new TextEncoder().encode("exactly");
231 + const spawner = makeSpawner(() =>
232 + Effect.succeed(
233 + makeHandle({
234 + stdout: Stream.make(output, new Uint8Array()),
235 + }),
236 + ),
237 + );
238 +
239 + const result = yield* runWith(spawner)({
240 + command: "fake",
241 + args: ["exact-limit"],
242 + maxOutputBytes: output.byteLength,
243 + });
244 +
245 + expect(result.stdout).toBe("exactly");
246 }),
247 );
248
@@ -272,6 +345,8 @@ describe("runProcess", () => {
345 const errorFiber = yield* runWith(spawner)({
346 command: "fake",
347 args: ["sleep"],
348 + cwd: "/logical",
349 + spawnCwd: "/actual",
350 timeout: "50 millis",
351 }).pipe(Effect.flip, Effect.forkScoped);
352
@@ -279,7 +354,18 @@ describe("runProcess", () => {
354 yield* TestClock.adjust(Duration.millis(50));
355 const error = yield* Fiber.join(errorFiber);
356
282 - expect(error).toBeInstanceOf(ProcessRunner.ProcessTimeoutError);
357 + expect(error._tag).toBe("ProcessTimeoutError");
358 + if (error._tag !== "ProcessTimeoutError") {
359 + return expect.fail("Expected ProcessTimeoutError");
360 + }
361 + expect(error).toMatchObject({
362 + command: "fake",
363 + argumentCount: 1,
364 + cwd: "/logical",
365 + spawnCwd: "/actual",
366 + timeoutMs: 50,
367 + });
368 + expect(error.message).toBe("Process 'fake' in '/actual' timed out after 50ms");
369 }),
370 );
371
apps/server/src/processRunner.ts
+45 -20
@@ -45,23 +45,29 @@ export interface ProcessRunOutput {
45
46 const ProcessInvocationFields = {
47 command: Schema.String,
48 - args: Schema.Array(Schema.String),
48 + argumentCount: Schema.Number,
49 cwd: Schema.optional(Schema.String),
50 + spawnCwd: Schema.optional(Schema.String),
51 };
52
53 const formatProcessInvocation = (input: {
54 readonly command: string;
54 - readonly args: ReadonlyArray<string>;
55 readonly cwd?: string | undefined;
56 + readonly spawnCwd?: string | undefined;
57 }): string => {
57 - const command = [input.command, ...input.args].join(" ");
58 - return input.cwd === undefined ? `'${command}'` : `'${command}' in '${input.cwd}'`;
58 + const executionCwd = input.spawnCwd ?? input.cwd;
59 + return executionCwd === undefined
60 + ? `'${input.command}'`
61 + : `'${input.command}' in '${executionCwd}'`;
62 };
63
64 export class ProcessSpawnError extends Schema.TaggedErrorClass<ProcessSpawnError>()(
65 "ProcessSpawnError",
66 {
67 ...ProcessInvocationFields,
68 + resolvedCommand: Schema.optional(Schema.String),
69 + resolvedArgumentCount: Schema.optional(Schema.Number),
70 + shell: Schema.optional(Schema.Boolean),
71 cause: Schema.Defect(),
72 },
73 ) {
@@ -74,6 +80,7 @@ export class ProcessStdinError extends Schema.TaggedErrorClass<ProcessStdinError
80 "ProcessStdinError",
81 {
82 ...ProcessInvocationFields,
83 + stdinBytes: Schema.Number,
84 cause: Schema.Defect(),
85 },
86 ) {
@@ -88,10 +95,11 @@ export class ProcessOutputLimitError extends Schema.TaggedErrorClass<ProcessOutp
95 ...ProcessInvocationFields,
96 stream: Schema.Literals(["stdout", "stderr"]),
97 maxBytes: Schema.Number,
98 + observedBytes: Schema.Number,
99 },
100 ) {
101 override get message(): string {
94 - return `Process ${formatProcessInvocation(this)} ${this.stream} exceeded ${this.maxBytes} bytes`;
102 + return `Process ${formatProcessInvocation(this)} ${this.stream} produced ${this.observedBytes} bytes, exceeding the ${this.maxBytes} byte limit`;
103 }
104 }
105
@@ -120,12 +128,14 @@ export class ProcessTimeoutError extends Schema.TaggedErrorClass<ProcessTimeoutE
128 }
129 }
130
123 -export type ProcessRunError =
124 - | ProcessSpawnError
125 - | ProcessStdinError
126 - | ProcessOutputLimitError
127 - | ProcessReadError
128 - | ProcessTimeoutError;
131 +export const ProcessRunError = Schema.Union([
132 + ProcessSpawnError,
133 + ProcessStdinError,
134 + ProcessOutputLimitError,
135 + ProcessReadError,
136 + ProcessTimeoutError,
137 +]);
138 +export type ProcessRunError = typeof ProcessRunError.Type;
139
140 export class ProcessRunner extends Context.Service<
141 ProcessRunner,
@@ -163,6 +173,7 @@ const collectText = Effect.fn("processRunner.collectText")(function* (input: {
173 readonly command: string;
174 readonly args: ReadonlyArray<string>;
175 readonly cwd?: string | undefined;
176 + readonly spawnCwd?: string | undefined;
177 readonly streamName: "stdout" | "stderr";
178 readonly stream: Stream.Stream<Uint8Array, PlatformError.PlatformError>;
179 readonly maxOutputBytes: number;
@@ -174,8 +185,9 @@ const collectText = Effect.fn("processRunner.collectText")(function* (input: {
185 (cause) =>
186 new ProcessReadError({
187 command: input.command,
177 - args: input.args,
188 + argumentCount: input.args.length,
189 cwd: input.cwd,
190 + spawnCwd: input.spawnCwd,
191 stream: input.streamName,
192 cause,
193 }),
@@ -203,14 +215,16 @@ const collectText = Effect.fn("processRunner.collectText")(function* (input: {
215 () => ({ chunks: [], bytes: 0 }),
216 (state, chunk) => {
217 const remainingBytes = input.maxOutputBytes - state.bytes;
206 - if (remainingBytes <= 0 || chunk.byteLength > remainingBytes) {
218 + if (chunk.byteLength > remainingBytes) {
219 return Effect.fail(
220 new ProcessOutputLimitError({
221 command: input.command,
210 - args: input.args,
222 + argumentCount: input.args.length,
223 cwd: input.cwd,
224 + spawnCwd: input.spawnCwd,
225 stream: input.streamName,
226 maxBytes: input.maxOutputBytes,
227 + observedBytes: state.bytes + chunk.byteLength,
228 }),
229 );
230 }
@@ -259,8 +273,9 @@ function finalizeRunProcess<R>(
273 return Effect.fail(
274 new ProcessTimeoutError({
275 command: input.command,
262 - args: input.args,
276 + argumentCount: input.args.length,
277 cwd: input.cwd,
278 + spawnCwd: input.spawnCwd,
279 timeoutMs: Duration.toMillis(timeout),
280 }),
281 );
@@ -300,23 +315,30 @@ const runProcessCore = Effect.fn("processRunner.runProcessCore")(function* (
315 (cause) =>
316 new ProcessSpawnError({
317 command: input.command,
303 - args: input.args,
318 + argumentCount: input.args.length,
319 cwd: input.cwd,
320 + spawnCwd: input.spawnCwd,
321 + resolvedCommand: spawnCommand.command,
322 + resolvedArgumentCount: spawnCommand.args.length,
323 + shell: spawnCommand.shell,
324 cause,
325 }),
326 ),
327 );
328
329 + const stdin = input.stdin;
330 const writeStdin =
311 - input.stdin === undefined
331 + stdin === undefined
332 ? Effect.void
313 - : Stream.run(Stream.encodeText(Stream.make(input.stdin)), child.stdin).pipe(
333 + : Stream.run(Stream.encodeText(Stream.make(stdin)), child.stdin).pipe(
334 Effect.mapError(
335 (cause) =>
336 new ProcessStdinError({
337 command: input.command,
318 - args: input.args,
338 + argumentCount: input.args.length,
339 cwd: input.cwd,
340 + spawnCwd: input.spawnCwd,
341 + stdinBytes: Buffer.byteLength(stdin),
342 cause,
343 }),
344 ),
@@ -328,6 +350,7 @@ const runProcessCore = Effect.fn("processRunner.runProcessCore")(function* (
350 command: input.command,
351 args: input.args,
352 cwd: input.cwd,
353 + spawnCwd: input.spawnCwd,
354 streamName: "stdout",
355 stream: child.stdout,
356 maxOutputBytes,
@@ -338,6 +361,7 @@ const runProcessCore = Effect.fn("processRunner.runProcessCore")(function* (
361 command: input.command,
362 args: input.args,
363 cwd: input.cwd,
364 + spawnCwd: input.spawnCwd,
365 streamName: "stderr",
366 stream: child.stderr,
367 maxOutputBytes,
@@ -354,8 +378,9 @@ const runProcessCore = Effect.fn("processRunner.runProcessCore")(function* (
378 (cause) =>
379 new ProcessReadError({
380 command: input.command,
357 - args: input.args,
381 + argumentCount: input.args.length,
382 cwd: input.cwd,
383 + spawnCwd: input.spawnCwd,
384 stream: "exitCode",
385 cause,
386 }),