From f185b72a6629d6b55bdfcbeadf803575bbaafefa Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 5 Mar 2026 11:44:07 -0800 Subject: [PATCH] Handle null-exit and timeout failures in Codex and Git flows - squash Effect causes to surface real Codex process error messages - normalize Git process results to error on signal/null exit and keep details - treat timed-out Codex health probes/auth checks as unavailable or warning with clear messages - add tests for cause extraction, Git signal termination, and provider timeout paths --- apps/server/src/codexAppServerManager.test.ts | 8 ++ apps/server/src/codexAppServerManager.ts | 10 ++- apps/server/src/git/Layers/GitService.test.ts | 29 ++++++- apps/server/src/git/Layers/GitService.ts | 82 ++++++++++++------- apps/server/src/processRunner.ts | 2 +- .../provider/Layers/ProviderHealth.test.ts | 33 +++++++- .../src/provider/Layers/ProviderHealth.ts | 14 +++- 7 files changed, 141 insertions(+), 37 deletions(-) diff --git a/apps/server/src/codexAppServerManager.test.ts b/apps/server/src/codexAppServerManager.test.ts index a476d4e49c..e88ded4a13 100644 --- a/apps/server/src/codexAppServerManager.test.ts +++ b/apps/server/src/codexAppServerManager.test.ts @@ -1,4 +1,5 @@ import * as NodeServices from "@effect/platform-node/NodeServices"; +import { Cause } from "effect"; import { ManagedRuntime, ServiceMap } from "effect"; import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import { ProviderSessionId } from "@t3tools/contracts"; @@ -7,6 +8,7 @@ import { CodexAppServerManager, classifyCodexStderrLine, isRecoverableThreadResumeError, + messageFromCodexProcessCause, normalizeCodexModelSlug, } from "./codexAppServerManager"; @@ -171,6 +173,12 @@ describe("isRecoverableThreadResumeError", () => { }); }); +describe("messageFromCodexProcessCause", () => { + it("extracts the underlying error message from an effect cause", () => { + expect(messageFromCodexProcessCause(Cause.fail(new Error("boom")))).toBe("boom"); + }); +}); + describe("startSession", () => { it("emits session/startFailed when resolving cwd throws before process launch", async () => { const manager = createManager(); diff --git a/apps/server/src/codexAppServerManager.ts b/apps/server/src/codexAppServerManager.ts index b57b44b7a5..5a1ed26dee 100644 --- a/apps/server/src/codexAppServerManager.ts +++ b/apps/server/src/codexAppServerManager.ts @@ -17,7 +17,7 @@ import { type ProviderTurnStartResult, } from "@t3tools/contracts"; import type * as NodeServices from "@effect/platform-node/NodeServices"; -import { Effect, Exit, ServiceMap, Scope, Stream } from "effect"; +import { Cause, Effect, Exit, ServiceMap, Scope, Stream } from "effect"; import type { ChildProcessSpawner } from "effect/unstable/process"; import { makeRuntimeCommand, spawnManagedCommand } from "./processRunner"; @@ -153,6 +153,11 @@ export function isRecoverableThreadResumeError(error: unknown): boolean { return RECOVERABLE_THREAD_RESUME_ERROR_SNIPPETS.some((snippet) => message.includes(snippet)); } +export function messageFromCodexProcessCause(cause: Cause.Cause): string { + const squashed = Cause.squash(cause); + return squashed instanceof Error ? squashed.message : "codex app-server process errored."; +} + export interface CodexAppServerManagerEvents { event: [event: ProviderEvent]; } @@ -566,8 +571,7 @@ export class CodexAppServerManager extends EventEmitter { } }), ); + + it("normalizeGitProcessResult fails when git exits without a code", () => { + try { + void normalizeGitProcessResult( + { + operation: "GitProcess.test.signal", + cwd: process.cwd(), + args: ["status"], + }, + {}, + { + code: null, + signal: "SIGTERM", + stdout: "", + stderr: "", + timedOut: false, + }, + ); + assert.fail("Expected normalizeGitProcessResult to throw."); + } catch (error) { + assert.equal(Schema.is(GitCommandError)(error), true); + if (!Schema.is(GitCommandError)(error)) { + return; + } + assert.equal(error.detail, "git status terminated by signal SIGTERM."); + } + }); }); diff --git a/apps/server/src/git/Layers/GitService.ts b/apps/server/src/git/Layers/GitService.ts index 8cf00c969f..8df2bee3c9 100644 --- a/apps/server/src/git/Layers/GitService.ts +++ b/apps/server/src/git/Layers/GitService.ts @@ -39,6 +39,59 @@ function toGitCommandError( }); } +export function normalizeGitProcessResult( + commandInput: Pick, + input: Pick, + result: { + readonly stdout: string; + readonly stderr: string; + readonly code: number | null; + readonly signal: NodeJS.Signals | null; + readonly timedOut: boolean; + }, +): ExecuteGitResult { + if (result.timedOut) { + throw new GitCommandError({ + operation: commandInput.operation, + command: quoteGitCommand(commandInput.args), + cwd: commandInput.cwd, + detail: `${quoteGitCommand(commandInput.args)} timed out.`, + }); + } + + if (result.code === null) { + throw new GitCommandError({ + operation: commandInput.operation, + command: quoteGitCommand(commandInput.args), + cwd: commandInput.cwd, + detail: + result.signal !== null + ? `${quoteGitCommand(commandInput.args)} terminated by signal ${result.signal}.` + : `${quoteGitCommand(commandInput.args)} terminated before reporting an exit code.`, + }); + } + + const exitCode = result.code; + if (!input.allowNonZeroExit && exitCode !== 0) { + const trimmedStderr = result.stderr.trim(); + throw new GitCommandError({ + operation: commandInput.operation, + command: quoteGitCommand(commandInput.args), + cwd: commandInput.cwd, + detail: + trimmedStderr.length > 0 + ? `${quoteGitCommand(commandInput.args)} failed: ${trimmedStderr}` + : `${quoteGitCommand(commandInput.args)} failed with code ${exitCode}.`, + }); + } + + return { + code: exitCode, + stdout: result.stdout, + stderr: result.stderr, + } satisfies ExecuteGitResult; +} + const makeGitService = Effect.sync(() => { const execute: GitServiceShape["execute"] = Effect.fnUntraced(function* (input) { const commandInput = { @@ -59,34 +112,7 @@ const makeGitService = Effect.sync(() => { outputMode: "error", }); - if (result.timedOut) { - throw new GitCommandError({ - operation: commandInput.operation, - command: quoteGitCommand(commandInput.args), - cwd: commandInput.cwd, - detail: `${quoteGitCommand(commandInput.args)} timed out.`, - }); - } - - const exitCode = result.code ?? 0; - if (!input.allowNonZeroExit && exitCode !== 0) { - const trimmedStderr = result.stderr.trim(); - throw new GitCommandError({ - operation: commandInput.operation, - command: quoteGitCommand(commandInput.args), - cwd: commandInput.cwd, - detail: - trimmedStderr.length > 0 - ? `${quoteGitCommand(commandInput.args)} failed: ${trimmedStderr}` - : `${quoteGitCommand(commandInput.args)} failed with code ${exitCode}.`, - }); - } - - return { - code: exitCode, - stdout: result.stdout, - stderr: result.stderr, - } satisfies ExecuteGitResult; + return normalizeGitProcessResult(commandInput, input, result); }, catch: toGitCommandError(commandInput, "failed to run."), }); diff --git a/apps/server/src/processRunner.ts b/apps/server/src/processRunner.ts index 63eac85805..6bd3162faf 100644 --- a/apps/server/src/processRunner.ts +++ b/apps/server/src/processRunner.ts @@ -127,7 +127,7 @@ export const spawnManagedCommand = (command: ChildProcess.Command) => } satisfies ManagedChildProcess; }); -export function spawnProcess( +function spawnProcess( command: string, args: readonly string[], options: ProcessSpawnOptions = {}, diff --git a/apps/server/src/provider/Layers/ProviderHealth.test.ts b/apps/server/src/provider/Layers/ProviderHealth.test.ts index e663b07055..7ad8eea5f7 100644 --- a/apps/server/src/provider/Layers/ProviderHealth.test.ts +++ b/apps/server/src/provider/Layers/ProviderHealth.test.ts @@ -7,7 +7,12 @@ import { checkCodexProviderStatus, parseAuthStatusFromOutput } from "./ProviderH // ── Test helpers ──────────────────────────────────────────────────── function mockRunner( - handler: (args: ReadonlyArray) => { stdout: string; stderr: string; code: number }, + handler: (args: ReadonlyArray) => { + stdout: string; + stderr: string; + code: number | null; + timedOut?: boolean; + }, ) { return (args: ReadonlyArray) => Promise.resolve(handler(args)); } @@ -116,6 +121,32 @@ it.effect("returns warning when login status command is unsupported", () => }), ); +it.effect("returns unavailable when codex version probe times out", () => + Effect.gen(function* () { + const status = yield* checkCodexProviderStatus( + mockRunner((args) => { + const joined = args.join(" "); + if (joined === "--version") { + return { stdout: "", stderr: "", code: null, timedOut: true }; + } + throw new Error(`Unexpected args: ${joined}`); + }), + ); + assert.strictEqual(status.provider, "codex"); + assert.strictEqual(status.status, "error"); + assert.strictEqual(status.available, false); + assert.strictEqual(status.authStatus, "unknown"); + assert.strictEqual(status.message, "Codex CLI is installed but failed to run. Timed out while running command."); + }), +); + +it("parseAuthStatusFromOutput: timed out auth check is warning", () => { + const parsed = parseAuthStatusFromOutput({ stdout: "", stderr: "", code: null, timedOut: true }); + assert.strictEqual(parsed.status, "warning"); + assert.strictEqual(parsed.authStatus, "unknown"); + assert.strictEqual(parsed.message, "Timed out while checking Codex authentication status."); +}); + // ── Pure function tests ───────────────────────────────────────────── it("parseAuthStatusFromOutput: exit code 0 with no auth markers is ready", () => { diff --git a/apps/server/src/provider/Layers/ProviderHealth.ts b/apps/server/src/provider/Layers/ProviderHealth.ts index 75e1ab40ae..183d63661e 100644 --- a/apps/server/src/provider/Layers/ProviderHealth.ts +++ b/apps/server/src/provider/Layers/ProviderHealth.ts @@ -31,7 +31,7 @@ class ProviderHealthCommandError extends Data.TaggedError("ProviderHealthCommand export interface CommandResult { readonly stdout: string; readonly stderr: string; - readonly code: number; + readonly code: number | null; readonly timedOut?: boolean; } @@ -109,6 +109,14 @@ export function parseAuthStatusFromOutput(result: CommandResult): { readonly authStatus: ServerProviderAuthStatus; readonly message?: string; } { + if (result.timedOut) { + return { + status: "warning", + authStatus: "unknown", + message: "Timed out while checking Codex authentication status.", + }; + } + const lowerOutput = `${result.stdout}\n${result.stderr}`.toLowerCase(); if ( @@ -199,7 +207,7 @@ const defaultRunCodexCommand: RunProviderHealthCommand = async (args) => { return { stdout: result.stdout, stderr: result.stderr, - code: result.code ?? 0, + code: result.code, timedOut: result.timedOut, } satisfies CommandResult; }; @@ -237,7 +245,7 @@ export const checkCodexProviderStatus = ( } const version = versionProbe.success; - if (version.code !== 0) { + if (version.timedOut || version.code !== 0) { const detail = detailFromResult(version); return { provider: CODEX_PROVIDER,