@devries-cameron20 / t3code Mirror · read-only / commits / e01b1903de
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 24, 22:45 UTC

[codex] Structure process diagnostics failures (#3389)

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

Julius Marminge committed Jun 20, 2026 at 11:30 UTC e01b1903de403c720792a896d8c50ebcd8163b44
2 files changed +117 -33
apps/server/src/diagnostics/ProcessDiagnostics.test.ts
+39
@@ -6,6 +6,7 @@ import * as Option from "effect/Option";
6 import * as Sink from "effect/Sink";
7 import * as Stream from "effect/Stream";
8 import { ChildProcessSpawner } from "effect/unstable/process";
9 +import { HostProcessPlatform } from "@t3tools/shared/hostProcess";
10
11 import * as ProcessDiagnostics from "./ProcessDiagnostics.ts";
12
@@ -219,6 +220,44 @@ describe("ProcessDiagnostics", () => {
220 }),
221 );
222
223 + it.effect("keeps bounded command diagnostics when the process query exits unsuccessfully", () =>
224 + Effect.gen(function* () {
225 + const spawnerLayer = Layer.succeed(
226 + ChildProcessSpawner.ChildProcessSpawner,
227 + ChildProcessSpawner.make(() =>
228 + Effect.succeed(
229 + mockHandle({
230 + code: 17,
231 + stdout: "partial process output",
232 + stderr: "process access denied",
233 + }),
234 + ),
235 + ),
236 + );
237 +
238 + const error = yield* ProcessDiagnostics.readProcessRows.pipe(
239 + Effect.provide(spawnerLayer),
240 + Effect.provideService(HostProcessPlatform, "linux"),
241 + Effect.flip,
242 + );
243 +
244 + expect(error).toMatchObject({
245 + _tag: "ProcessDiagnosticsQueryFailedError",
246 + command: "ps",
247 + argCount: 2,
248 + cwd: process.cwd(),
249 + exitCode: 17,
250 + stdoutBytes: 22,
251 + stderrBytes: 21,
252 + stdoutTruncated: false,
253 + stderrTruncated: false,
254 + });
255 + expect(error.message).toBe(
256 + `Process diagnostics query 'ps' failed with exit code 17 in '${process.cwd()}'.`,
257 + );
258 + }),
259 + );
260 +
261 it.effect("does not allow signaling the diagnostics query process", () =>
262 Effect.gen(function* () {
263 const spawnerLayer = Layer.succeed(
apps/server/src/diagnostics/ProcessDiagnostics.ts
+78 -33
@@ -45,10 +45,15 @@ export class ProcessDiagnostics extends Context.Service<
45
46 class ProcessDiagnosticsQueryTimeoutError extends Schema.TaggedErrorClass<ProcessDiagnosticsQueryTimeoutError>()(
47 "ProcessDiagnosticsQueryTimeoutError",
48 - { command: Schema.String },
48 + {
49 + command: Schema.String,
50 + argCount: Schema.Number,
51 + cwd: Schema.String,
52 + timeoutMillis: Schema.Number,
53 + },
54 ) {
55 override get message(): string {
51 - return `Process diagnostics query '${this.command}' timed out.`;
56 + return `Process diagnostics query '${this.command}' timed out after ${this.timeoutMillis}ms in '${this.cwd}'.`;
57 }
58 }
59
@@ -56,12 +61,19 @@ class ProcessDiagnosticsQueryFailedError extends Schema.TaggedErrorClass<Process
61 "ProcessDiagnosticsQueryFailedError",
62 {
63 command: Schema.String,
59 - stderr: Schema.optional(Schema.String),
64 + argCount: Schema.Number,
65 + cwd: Schema.String,
66 + exitCode: Schema.optional(Schema.Number),
67 + stdoutBytes: Schema.optional(Schema.Number),
68 + stderrBytes: Schema.optional(Schema.Number),
69 + stdoutTruncated: Schema.optional(Schema.Boolean),
70 + stderrTruncated: Schema.optional(Schema.Boolean),
71 cause: Schema.optional(Schema.Defect()),
72 },
73 ) {
74 override get message(): string {
64 - return this.stderr?.trim() || `Failed to query process diagnostics with '${this.command}'.`;
75 + const exitCode = this.exitCode === undefined ? "" : ` with exit code ${this.exitCode}`;
76 + return `Process diagnostics query '${this.command}' failed${exitCode} in '${this.cwd}'.`;
77 }
78 }
79
@@ -76,7 +88,10 @@ class ProcessDiagnosticsServerProcessSignalError extends Schema.TaggedErrorClass
88
89 class ProcessDiagnosticsNotDescendantError extends Schema.TaggedErrorClass<ProcessDiagnosticsNotDescendantError>()(
90 "ProcessDiagnosticsNotDescendantError",
79 - { pid: Schema.Number },
91 + {
92 + pid: Schema.Number,
93 + serverPid: Schema.Number,
94 + },
95 ) {
96 override get message(): string {
97 return `Process ${this.pid} is not a live descendant of the T3 server.`;
@@ -312,20 +327,29 @@ function makeResult(input: {
327 }
328
329 interface ProcessOutput {
330 + readonly cwd: string;
331 readonly exitCode: number;
332 readonly stdout: string;
333 + readonly stdoutBytes: number;
334 + readonly stdoutTruncated: boolean;
335 readonly stderr: string;
336 + readonly stderrBytes: number;
337 + readonly stderrTruncated: boolean;
338 }
339
320 -const runProcess = Effect.fn("runProcess")(
321 - function* (input: { readonly command: string; readonly args: ReadonlyArray<string> }) {
340 +const runProcess = Effect.fn("runProcess")(function* (input: {
341 + readonly command: string;
342 + readonly args: ReadonlyArray<string>;
343 +}) {
344 + const cwd = process.cwd();
345 + return yield* Effect.gen(function* () {
346 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
347 // `ps` and `powershell.exe` are real executables; spawning through cmd.exe
348 // shell mode would re-tokenize the PowerShell `-Command` payload (which
349 // contains pipes) before PowerShell ever sees it.
350 const child = yield* spawner.spawn(
351 ChildProcess.make(input.command, input.args, {
328 - cwd: process.cwd(),
352 + cwd,
353 }),
354 );
355 const [stdout, stderr, exitCode] = yield* Effect.all(
@@ -346,36 +370,44 @@ const runProcess = Effect.fn("runProcess")(
370 );
371
372 return {
373 + cwd,
374 exitCode,
375 stdout: stdout.text,
376 + stdoutBytes: stdout.bytes,
377 + stdoutTruncated: stdout.truncated,
378 stderr: stderr.text,
379 + stderrBytes: stderr.bytes,
380 + stderrTruncated: stderr.truncated,
381 } satisfies ProcessOutput;
353 - },
354 - (effect, input) =>
355 - effect.pipe(
356 - Effect.scoped,
357 - Effect.timeoutOption(Duration.millis(PROCESS_QUERY_TIMEOUT_MS)),
358 - Effect.flatMap((result) =>
359 - Option.match(result, {
360 - onNone: () =>
361 - Effect.fail(
362 - new ProcessDiagnosticsQueryTimeoutError({
363 - command: input.command,
364 - }),
365 - ),
366 - onSome: Effect.succeed,
367 - }),
368 - ),
369 - Effect.mapError((cause) =>
370 - isProcessDiagnosticsError(cause)
371 - ? cause
372 - : new ProcessDiagnosticsQueryFailedError({
382 + }).pipe(
383 + Effect.scoped,
384 + Effect.timeoutOption(Duration.millis(PROCESS_QUERY_TIMEOUT_MS)),
385 + Effect.flatMap((result) =>
386 + Option.match(result, {
387 + onNone: () =>
388 + Effect.fail(
389 + new ProcessDiagnosticsQueryTimeoutError({
390 command: input.command,
374 - cause,
391 + argCount: input.args.length,
392 + cwd,
393 + timeoutMillis: PROCESS_QUERY_TIMEOUT_MS,
394 }),
376 - ),
395 + ),
396 + onSome: Effect.succeed,
397 + }),
398 + ),
399 + Effect.mapError((cause) =>
400 + isProcessDiagnosticsError(cause)
401 + ? cause
402 + : new ProcessDiagnosticsQueryFailedError({
403 + command: input.command,
404 + argCount: input.args.length,
405 + cwd,
406 + cause,
407 + }),
408 ),
378 -);
409 + );
410 +});
411
412 function readPosixProcessRows(): Effect.Effect<
413 ReadonlyArray<ProcessRow>,
@@ -391,7 +423,13 @@ function readPosixProcessRows(): Effect.Effect<
423 ? Effect.fail(
424 new ProcessDiagnosticsQueryFailedError({
425 command: "ps",
394 - stderr: result.stderr.trim() || "ps failed.",
426 + argCount: 2,
427 + cwd: result.cwd,
428 + exitCode: result.exitCode,
429 + stdoutBytes: result.stdoutBytes,
430 + stderrBytes: result.stderrBytes,
431 + stdoutTruncated: result.stdoutTruncated,
432 + stderrTruncated: result.stderrTruncated,
433 }),
434 )
435 : Effect.succeed(parsePosixProcessRows(result.stdout)),
@@ -421,7 +459,13 @@ function readWindowsProcessRows(): Effect.Effect<
459 ? Effect.fail(
460 new ProcessDiagnosticsQueryFailedError({
461 command: "powershell.exe",
424 - stderr: result.stderr.trim() || "PowerShell process query failed.",
462 + argCount: 4,
463 + cwd: result.cwd,
464 + exitCode: result.exitCode,
465 + stdoutBytes: result.stdoutBytes,
466 + stderrBytes: result.stderrBytes,
467 + stdoutTruncated: result.stdoutTruncated,
468 + stderrTruncated: result.stderrTruncated,
469 }),
470 )
471 : Effect.succeed(parseWindowsProcessRows(result.stdout)),
@@ -464,6 +508,7 @@ function assertDescendantPid(
508 : Effect.fail(
509 new ProcessDiagnosticsNotDescendantError({
510 pid,
511 + serverPid: process.pid,
512 }),
513 );
514 }),