Skip to content

A failed turn is reported as a successful scan when turn.failed has an unexpected error payload #37

Closed

Description

@mariohercules

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:

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 main at f22d4a36f26d16287bcdfd707b369116e02a08c3
  • 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

  1. Daewooox commented on Jul 29, 2026

    @Daewooox

    I independently reproduced this on current main at f22d4a36f26d16287bcdfd707b369116e02a08c3 with Node 26.5.0.

    Minimal event-stream result:

    case expected actual
    turn.failed with { "message": "boom" } rejected rejected
    turn.failed with null error rejected resolved
    turn.failed with string error rejected resolved
    turn.failed with { "code": "boom" } rejected resolved
    turn.failed with {} rejected resolved

    The false-success cases resolve when a malformed turn.failed is followed by turn.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.

  2. added
    bug
    on Aug 3, 2026
  3. mldangelo-oai commented on Aug 3, 2026

    @mldangelo-oai
    Collaborator

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions