Repository navigation
Description
Summary
A turn.failed event is only treated as a failure when its payload is an object with a string message. Any other payload shape falls through every branch of the event loop, and if a turn.completed was already observed the failed turn is reported to the caller as a successful scan and recorded in the workbench with complete-scan.
The branch requires both isRecord(event["error"]) and a string message:
codex-security/sdk/typescript/src/api.ts
Lines 1187 to 1192 in f22d4a3
| event.type === "turn.failed" && | |
| isRecord(event["error"]) && | |
| typeof event["error"]["message"] === "string" | |
| ) { | |
| throw new CodexSecurityError(event["error"]["message"]); | |
| } else if ( |
There is no else, so error: null, error as a plain string, or a non-string error.message all leave status as "completed" and the loop continues to onFinalize.
Affected version and environment
- Released package:
@openai/[email protected] - Confirmed on current
mainatf22d4a36f26d16287bcdfd707b369116e02a08c3 - macOS 26.5.2 (build 25F84)
- Node.js v24.5.0
- Bun 1.3.14
Steps to reproduce
No API call is required. Using the injected-dependency pattern from tests-ts/api.test.ts, drive the event stream with thread.started, turn.completed, then a turn.failed whose error payload varies. The scan directory holds a valid artifact bundle so the only variable is the failure event.
createCodex: () => ({
startThread: () => ({
id: null,
async runStreamed() {
async function* stream() {
yield { type: "thread.started", thread_id: "t" };
yield { type: "turn.completed", usage: {} };
yield { type: "turn.failed", error: null }; // <- payload under test
}
return { events: stream() };
},
}),
}),Observed results:
[A] turn.failed with a string message (handled correctly)
outcome: rejected: CodexSecurityError: model refused the turn
workbench: ["register-cli-scan","fail-scan"]
[B] turn.failed with error: null
outcome: RESOLVED (scan treated as successful)
workbench: ["register-cli-scan","complete-scan"]
[B] turn.failed with error as a plain string
outcome: RESOLVED (scan treated as successful)
workbench: ["register-cli-scan","complete-scan"]
[B] turn.failed with error.message not a string
outcome: RESOLVED (scan treated as successful)
workbench: ["register-cli-scan","complete-scan"]
[B] turn.failed with error.code only
outcome: RESOLVED (scan treated as successful)
workbench: ["register-cli-scan","complete-scan"]
The well-formed case behaves correctly, which isolates the payload shape as the only difference.
Expected behavior
turn.failed should be terminal regardless of the shape of its error payload. When the message cannot be extracted, the scan should still fail, with a generic message rather than silent success.
Actual behavior
run() resolves with a ScanResult, the CLI exits 0, and the workbench records complete-scan. A user running with --fail-on-severity in CI gets a passing build from a turn the model reported as failed.
Impact
This is a silent failure rather than a visible one. The scan directory can hold a stale or partial artifact bundle from an earlier phase while the run is reported as a clean pass, so neither the exit code nor scans list shows anything wrong.
Whether this is reachable in practice depends on what the Codex SDK can emit for turn.failed; the SDK types it loosely enough that the SDK cannot rely on the shape, and the defensive isRecord check in this very branch suggests the same conclusion.
Suggested direction
Make turn.failed terminal in all cases, extracting the message when possible:
} else if (event.type === "turn.failed") {
const error = event["error"];
const message =
isRecord(error) && typeof error["message"] === "string"
? error["message"]
: "Codex Security reported a failed turn without a usable error message.";
throw new CodexSecurityError(message);
}
Activity
Daewooox commentedon Jul 29, 2026
I independently reproduced this on current
mainatf22d4a36f26d16287bcdfd707b369116e02a08c3with Node26.5.0.Minimal event-stream result:
turn.failedwith{ "message": "boom" }turn.failedwithnullerrorturn.failedwith string errorturn.failedwith{ "code": "boom" }turn.failedwith{}The false-success cases resolve when a malformed
turn.failedis followed byturn.completed.I am building ScopeLock, a small local safety layer for AI coding tools, and this is exactly the kind of problem it is meant to help with: do not trust an external tool's success signal alone. In my repro, the simulated external tool exited
0, but ScopeLock's independent required validation detected the malformed-failure acceptance and blocked promotion before applying changes to the repository.So ScopeLock can mitigate this downstream, but the SDK-side fix still looks like: any
turn.failed, even with an unexpected payload shape, should fail closed.mldangelo-oai commentedon Aug 3, 2026
Fixed in #152.
Scan execution now fails closed for every turn.failed payload shape, including malformed error objects. I verified the regression on the current main. Thank you for reporting this.