Max turns from claude_args now stops the run

1 month ago

A user set max turns to 60 in claude args, and a long review ran for 73 turns. That's thirteen turns past the cap. The result came back as a success, with no error, so the action went green. Max turns is the main cost guardrail for unattended runs, and here it failed open. This pull request makes it fail closed.

Ask about this presentation

Chapters

Show transcript
  1. A user set max turns to 60 in claude args, and a long review ran for 73 turns. That's thirteen turns past the cap. The result came back as a success, with no error, so the action went green. Max turns is the main cost guardrail for unattended runs, and here it failed open. This pull request makes it fail closed.

    Slide text
    turn budget claude-code-action · pull request 1607

    A 60-turn cap let a run
    go to 73 and pass

    turn 1 cap: 60 73
    subtype: success
  2. Here's where the number went. Until now, the max turns flag in claude args was passed through to the CLI as a loose extra argument, and the Agent SDK's typed max turns option stayed empty. Now the flag is pulled out of claude args and set as that typed option. This matters because the main action doesn't have a separate max turns input, so every user who sets a cap sets it this way.

    Slide text
    turn budget the path

    The cap now goes to the SDK's own option

    claude_args --max-turns 60
    →
    Before: CLI pass-through extraArgs["max-turns"]
    sdkOptions.maxTurns = undefined
    →
    After: typed option sdkOptions.maxTurns = 60
    flag removed from extraArgs

    The top-level action has no max-turns input, so claude_args is the only way to set it.

  3. The first change copies a pattern that already exists for the model flag. It reads max turns out of the parsed claude args and deletes it, so the CLI never sees it twice. Then the typed option takes the direct max turns input first, and falls back to the claude args value. The base action still exposes that direct input, so it wins when both are set.

    Slide text
    turn budget change one

    Pull the flag out, then use it

    base-action/src/parse-sdk-options.ts
    const maxTurnsFromClaudeArgs = extraArgs["max-turns"] || undefined;
    delete extraArgs["max-turns"];
    // …
    maxTurns: options.maxTurns
      ? parseInt(options.maxTurns, 10)
      : maxTurnsFromClaudeArgs
        ? parseInt(maxTurnsFromClaudeArgs, 10)
        : undefined,
  4. The second change is a backstop. The author's reasoning is that a bug in the SDK or the CLI shouldn't be able to produce a green action. So after the run, if the result says success but the reported turn count is above the cap, the action logs an error and throws. Results that already failed, like a max turns error, are left alone.

    Slide text
    turn budget change two

    A success past the cap now throws

    base-action/src/run-claude-sdk.ts
    if (
      resultMessage.subtype === "success" &&
      !resultMessage.is_error &&
      sdkOptions.maxTurns !== undefined &&
      resultMessage.num_turns > sdkOptions.maxTurns
    ) {
      const message = `Claude reported a successful result after ${…} turns, …`;
      core.error(message);
      throw new Error(message);
    }
  5. The new test feeds the exact numbers from the bug report through a mocked SDK: a cap of 60 and a successful result at 73 turns. The run rejects with an error naming both numbers. The execution log is still written first, so the transcript survives for debugging. Two parser tests cover the flag mapping and the precedence rule.

    Slide text
    turn budget tests

    The 60-to-73 run, replayed in a test

    maxTurns: 60 num_turns: 73
    subtype: "success"
    →
    Rejects error message names 73 and 60
    +
    Log kept execution file still written

    Two parser tests: the flag maps to 60 and leaves extraArgs, and a direct 25 beats a claude_args 60.

  6. The backstop only runs once the session ends. Comments, commits and spend from the extra turns have already happened; the check just makes the run red. Then there's turn counting. In the bug report, a cap of one reported two turns. If a run that ends cleanly at the cap can report one more, this check would fail a good run. A typo like "sixty" parses to not a number. The SDK is handed that value, and the backstop comparison is always false, so it never fires. And a throw skips setting the conclusion and session outputs, which downstream steps may read.

    Slide text
    turn budget for the reviewer

    Check these before trusting the backstop

    • The backstop runs after the work All 73 turns, and their cost and side effects, already happened. It turns green to red.
    • How turns are counted The issue's cap of 1 reported 2 turns. Can a clean finish at the cap read one over?
    • A non-numeric value parseInt gives NaN, the SDK gets NaN, and the backstop never fires.
    • What a throw skips The conclusion and session id outputs aren't set; the tracking comment shows a failure.
  7. One limit to keep in mind. This change doesn't find out why the CLI flag let the run go past 60. It stops relying on that flag, and adds a check in case the SDK slips too. So the same 73-turn run from the start now ends red, with an error naming both numbers. Max turns from claude args now stops the run, or fails it.

    Slide text
    turn budget anthropics/claude-code-action · 1607

    The cap is enforced, and an overrun goes red

    It doesn't explain why the CLI flag slipped past 60.

    turn 1 cap: 60 73
    action failed

What happened in AI today, in your inbox

One short email each evening. Read it and you’re caught up.

Built with ❤️ in New York