Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 23 additions & 8 deletions bin/codex-proxy.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,11 @@ const server = http.createServer((req, res) => {
let completionTokens = 0;
let stderrTail = '';
let spawnError = null;
// In-band failure state: `codex exec --json` can report a fatal
// turn failure as a turn.failed event on a run that still exits 0
// (#680). null = no turn.failed seen; otherwise its error message
// ('' when the event carried none).
let turnFailedMessage = null;

const processJsonLine = (line) => {
const trimmed = line.trim();
Expand Down Expand Up @@ -364,6 +369,15 @@ const server = http.createServer((req, res) => {
}
}

// Turn failed in-band: a fatal error during the turn. The
// exit code cannot be trusted to reflect it (#680).
if (event.type === 'turn.failed') {
const msg = typeof event.error === 'string'
? event.error
: (event.error && typeof event.error.message === 'string' ? event.error.message : '');
turnFailedMessage = msg;
}

// Turn completed with usage metrics
if (event.type === 'turn.completed' && event.usage) {
if (typeof event.usage.input_tokens === 'number') {
Expand Down Expand Up @@ -403,20 +417,21 @@ const server = http.createServer((req, res) => {

if (res.writableEnded) return;

// Failure is judged by the child's exit: a non-zero code or a
// killing signal means the run failed, however much content was
// already produced (#676). Partial content must not mask a
// failure — it used to be reported as a successful 'stop'
// carrying the truncated text (or an empty 200 when nothing was
// produced at all).
if (code !== 0) {
// Failure is judged by the run's authoritative signals: a
// turn.failed event (#680), or failing that the child's exit —
// a non-zero code or a killing signal means the run failed,
// however much content was already produced (#676). Partial
// content must not mask a failure — it used to be reported as
// a successful 'stop' carrying the truncated text (or an
// empty 200 when nothing was produced at all).
if (code !== 0 || turnFailedMessage !== null) {
const stderrLine = stderrTail.trim().split('\n').pop();
const fallback = spawnError
? spawnError.message
: signal
? `codex exec terminated by signal ${signal}`
: `codex exec exited with code ${code}`;
const detail = (stderrLine || fallback).trim();
const detail = (turnFailedMessage || stderrLine || fallback).trim();
const errorBody = { error: { message: `Codex execution failed: ${detail}` } };
if (stream) {
// The 200 + text/event-stream headers are already on the
Expand Down
21 changes: 20 additions & 1 deletion bin/codex-proxy.test.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Tests for codex-proxy's failure detection (#676).
// Tests for codex-proxy's failure detection (#676, #680).
// Run: node --test bin/codex-proxy.test.mjs
//
// The proxy spawns one `codex exec` child per request, with the prompt
Expand Down Expand Up @@ -29,6 +29,13 @@ case "$prompt" in
printf '%s\\n' '{"type":"item.completed","item":{"type":"agent_message","text":"partial answer"}}'
echo "stub codex: run failed midway" >&2
exit 3 ;;
*INBANDPARTIAL*)
printf '%s\\n' '{"type":"item.completed","item":{"type":"agent_message","text":"partial answer"}}'
printf '%s\\n' '{"type":"turn.failed","error":{"message":"stub codex: run failed midway"}}'
exit 0 ;;
*INBANDFAIL*)
printf '%s\\n' '{"type":"turn.failed","error":{"message":"stub codex: model overloaded"}}'
exit 0 ;;
*SIGKILLME*)
kill -9 $$ ;;
*)
Expand Down Expand Up @@ -137,6 +144,18 @@ test('partial output then failure: masked success becomes an error in both modes
await assertFailure(port, 'FAILPARTIAL', /Codex execution failed: stub codex: run failed midway/);
});

test('turn.failed with exit 0 and no output: 500 non-stream, SSE error + [DONE] stream (#680)', async (t) => {
const { port, stop } = await startProxy();
t.after(stop);
await assertFailure(port, 'INBANDFAIL', /Codex execution failed: stub codex: model overloaded/);
});

test('partial output then turn.failed with exit 0: masked success becomes an error in both modes (#680)', async (t) => {
const { port, stop } = await startProxy();
t.after(stop);
await assertFailure(port, 'INBANDPARTIAL', /Codex execution failed: stub codex: run failed midway/);
});

test('empty-stderr fallback names the killing signal, not code null (#676)', async (t) => {
const { port, stop } = await startProxy();
t.after(stop);
Expand Down
Loading