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
This commit is contained in:
1 parent
454d97f0cf
commit
df5009c70b
7 files changed
+141
-37
No files matched your search
@@ -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();
|
||||
|
||||
@@ -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<unknown>): 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<CodexAppServerManagerEve
|
||||
return;
|
||||
}
|
||||
|
||||
const message =
|
||||
cause instanceof Error ? cause.message : "codex app-server process errored.";
|
||||
const message = messageFromCodexProcessCause(cause);
|
||||
this.updateSession(context, {
|
||||
status: "error",
|
||||
activeTurnId: undefined,
|
||||
|
||||
@@ -3,7 +3,7 @@ import { it, assert } from "@effect/vitest";
|
||||
import { Effect, Layer, Schema } from "effect";
|
||||
|
||||
import { GitCommandError } from "../Errors.ts";
|
||||
import { GitServiceLive } from "./GitService.ts";
|
||||
import { GitServiceLive, normalizeGitProcessResult } from "./GitService.ts";
|
||||
import { GitService } from "../Services/GitService.ts";
|
||||
|
||||
const layer = it.layer(Layer.provideMerge(GitServiceLive, NodeServices.layer));
|
||||
@@ -56,4 +56,31 @@ layer("GitServiceLive", (it) => {
|
||||
}
|
||||
}),
|
||||
);
|
||||
|
||||
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.");
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -39,6 +39,59 @@ function toGitCommandError(
|
||||
});
|
||||
}
|
||||
|
||||
export function normalizeGitProcessResult(
|
||||
commandInput: Pick<ExecuteGitInput, "operation" | "cwd" | "args">,
|
||||
input: Pick<ExecuteGitInput, "allowNonZeroExit">,
|
||||
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."),
|
||||
});
|
||||
|
||||
@@ -127,7 +127,7 @@ export const spawnManagedCommand = (command: ChildProcess.Command) =>
|
||||
} satisfies ManagedChildProcess;
|
||||
});
|
||||
|
||||
export function spawnProcess(
|
||||
function spawnProcess(
|
||||
command: string,
|
||||
args: readonly string[],
|
||||
options: ProcessSpawnOptions = {},
|
||||
|
||||
@@ -7,7 +7,12 @@ import { checkCodexProviderStatus, parseAuthStatusFromOutput } from "./ProviderH
|
||||
// ── Test helpers ────────────────────────────────────────────────────
|
||||
|
||||
function mockRunner(
|
||||
handler: (args: ReadonlyArray<string>) => { stdout: string; stderr: string; code: number },
|
||||
handler: (args: ReadonlyArray<string>) => {
|
||||
stdout: string;
|
||||
stderr: string;
|
||||
code: number | null;
|
||||
timedOut?: boolean;
|
||||
},
|
||||
) {
|
||||
return (args: ReadonlyArray<string>) => 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", () => {
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in new issue
Block a user