Skip to content

fix(observability): turns_attempted can exceed max_turns and the CLI shows a third value — three disagreeing turn counters #919

Open

Description

@isadeks

Component

CLI (cli/src/format.ts), agent runtime (agent/src/pipeline.py, agent/src/models.py)

Describe the bug

One task carries three different turn numbers, and the pair shown to an operator — turns_attempted / max_turns — compares two quantities that are not the same thing. So turns_attempted can exceed max_turns on a task that terminated COMPLETED and never breached the cap.

Observed on a live lambda-microvm run of three tasks:

Task max_turns persisted turns_attempted bgagent status displayed terminal status
01M35TNKCWKFFE9T1WS8BPG2M5 80 90 171 COMPLETED
01M35TNMAYD0T8BM8GTM1EBGR9 60 47 83 COMPLETED
01M35SWBR3RNY6WZAQ9H1MJWDQ 12 5 9 COMPLETED

Every displayed value disagrees with the persisted one, and in the middle row the display invents an overrun (83 / ~60) for a task that used 47 of 60.

The cap itself is fine. agent/src/pipeline.py:409 passes config.max_turns into the Claude Agent SDK, which terminates with error_max_turns; _compute_turns_completed (pipeline.py:730-750) exists precisely because the SDK reports num_turns = max_turns + 1 on that path. This is not a containment failure and not substrate-specific — the same container image runs on agentcore, ECS and lambda-microvm. Budget enforcement, by contrast, is exact: a deliberate probe with --max-budget 0.25 failed with error_max_budget_usd as designed.

Two distinct defects

1. turns_attempted and max_turns are not commensurable, but are printed as a ratio.

pipeline.py:397 sets turns_attempted = agent_result.num_turns or agent_result.turns, documented at models.py:400 as "the SDK's authoritative counter". The first row above ran a single agent session — 1 session_started, 1 step:implement:start, so this is not several sessions summing — reported 90, and still ended COMPLETED rather than error_max_turns. Whatever num_turns counts there, it is not the quantity the SDK caps at 80.

Consequence: an operator who sets --max-turns 80 and is shown 90 / ~80 concludes the cap leaks. It does not. The number we display simply is not the number we bound.

2. The CLI prefers a live counter that matches neither.

cli/src/format.ts:606-617:

// Prefer the live ``turn`` from the most recent ``agent_turn`` event
// over the persisted ``turns_attempted`` — the former updates mid-task
const liveTurn = readNumberField(turnEvent?.metadata, 'turn');
const currentTurn = liveTurn ?? task.turns_attempted ?? null;
...
return `${left} / ~${right}`;

For the first row this renders 171, while the persisted record says 90 and the visible agent_turn events top out at 19. Separately, agent_cost_update events carry their own turn field that disagrees with the neighbouring agent_turn (observed agent_turn turn=7 immediately followed by agent_cost_update turn=4 in another task), so "the live turn" is not one well-defined series.

The ~ in / ~80 hints that something is approximate without saying what, which is how this stayed invisible.

Expected behavior

  • A displayed ratio compares like with like: either show the counter the SDK caps, or stop presenting them as a ratio and label them separately.
  • turns_attempted exceeding max_turns on a non-error_max_turns task should be impossible by construction, or documented at the field with the reason it is possible.
  • One turn series. If agent_turn.turn and agent_cost_update.turn are different counters, they need different names.
  • Replace the unexplained ~ with either an exact value or a stated reason for the estimate.

Additional context

  • Found while stress-testing the lambda-microvm substrate on a fork; substrate is incidental, the code path is shared.
  • Related-but-separate finding from the same run: platform doctor reports a GitHub token as healthy when it is expired, revoked, or blocked by enterprise policy, because it checks the secret's presence rather than making a repo-scoped call. Filing separately.

Activity

  1. added
    bug
    agent-runtime
    cli
    observability
    P2
    v1
    on Sep 23, 2026
  2. krokoko commented on Nov 24, 2026

    @krokoko
    Contributor

    What is left of this issue after #1075.

    With #1075, turns_completed is the runner's count of the model's answers, which is what max_turns limits. bgagent status now shows it, as the web console already did.

    The completion comments on Linear and Jira still print turns_attempted, so one task can show two counts:

    • cdk/src/handlers/fanout-task-events.ts, the two places that read task.turns_attempted for the Linear and the Jira comment;
    • cdk/src/handlers/shared/jira-iteration-comment.ts, the turns metric of the iteration comment;
    • cdk/src/handlers/orchestration-reconciler.ts, where parseTerminalTaskRecord reads turns_attempted for the final status that the reconciler writes into the Jira iteration comment.

    Switching those four to turns_completed, with the fallback the CLI uses for a task record that has none, would close the issue.

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

    P2agent-runtimebugcliobservabilityv1

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions