From ed0a71ecc29befe03d4960926fb8ffcaf349d05e Mon Sep 17 00:00:00 2001 From: Muse Sweep Date: Fri, 23 Oct 2026 21:59:51 +0000 Subject: [PATCH] fix(codex-proxy): report turn.failed events as errors, not successful stops (#680) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit codex exec --json reports a fatal turn failure in-band as a turn.failed event, and that event can arrive on a run that exits 0. The proxy judged failure only by the child's exit code (#677), so an exit-0 failed turn was reported as a successful 'stop' — empty when nothing was produced, or carrying the truncated partial text. Track turn.failed in the event parser; on close, a run that emitted it is a failure even with exit code 0. The error detail prefers the event's error message, falling back to stderr/signal/exit-code. Non-stream returns 500 and stream mode writes a data:-framed error event + [DONE], mirroring the #677 framing — no chunk claims finish_reason 'stop'. New INBANDFAIL / INBANDPARTIAL stub modes pin the no-output and partial-output turn.failed + exit-0 shapes in both modes (marker names avoid the FAILPARTIAL substring collision). With only the proxy reverted, exactly those two tests fail with the issue's 200-instead-of-500 symptom; full bin suites pass 37/37. --- bin/codex-proxy.mjs | 31 +++++++++++++++++++++++-------- bin/codex-proxy.test.mjs | 21 ++++++++++++++++++++- 2 files changed, 43 insertions(+), 9 deletions(-) diff --git a/bin/codex-proxy.mjs b/bin/codex-proxy.mjs index a62299d553..1e6696b90d 100755 --- a/bin/codex-proxy.mjs +++ b/bin/codex-proxy.mjs @@ -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(); @@ -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') { @@ -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 diff --git a/bin/codex-proxy.test.mjs b/bin/codex-proxy.test.mjs index 9168b0976b..dc83c5c7a1 100644 --- a/bin/codex-proxy.test.mjs +++ b/bin/codex-proxy.test.mjs @@ -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 @@ -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 $$ ;; *) @@ -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);