Automation mode stops at the first `result`; background subagents leave the review silently empty

Author: VPetrovskiCreated Sep 23, 2026Updated Sep 23, 2026

Describe the bug

In automation mode (a prompt: input, pull_request trigger), the action's SDK runner stops consuming messages at the first result message and treats it as the end of the run:

typescript
// base-action/src/run-claude-sdk.ts (v1.0.183 and v1.0.231)
if (message.type === "result") {
  resultMessage = message as SDKResultMessage;
  // ... Break explicitly: by SDK contract no further messages follow a
  // result, so the break is safe.
  break;
}

That contract no longer holds once Claude launches a subagent with run_in_background: true. Recent Claude Code versions keep a headless session alive until background tasks finish, deliver the completion as a task_notification, run another turn, and emit a second result. The action only ever sees the first one.

The practical effect: on a large PR (100 files, ~12k diff lines) the model split the review across eight background agents, then ended its turn with "I'll wait for their completion notifications before posting inline comments". The runner took that as the final result, the step finished green, the job tore down the agents, and the PR got zero review comments with no error anywhere. A silent no-op that looks like a clean review.

To Reproduce

Minimal reproduction with the CLI alone (Claude Code 2.1.280), showing the two-result stream the runner does not expect:

bash
printf '%s' "Do exactly this. Step 1: call the Agent tool ONCE with run_in_background set to true, subagent_type general-purpose, prompt: 'Reply with exactly the text SECRET-NUMBER-4471 and nothing else.' Step 2: immediately end your turn with a plain text message saying only: WAITING FOR THE BACKGROUND AGENT. Do not call any other tool and do not poll. Step 3: if and when you are later notified that the agent finished, reply with exactly: AGENT REPORTED: <the text it returned>." \
  | claude -p --output-format stream-json --verbose --max-turns 8

Observed message sequence:

system init
assistant tool_use Agent (run_in_background=true)
system task_started
user tool_result "Async agent launched successfully..."
assistant text "WAITING FOR THE BACKGROUND AGENT"
result success  num_turns=2   result="WAITING FOR THE BACKGROUND AGENT"   <-- the action breaks here
system task_notification
system init
assistant text "AGENT REPORTED: SECRET-NUMBER-4471"
result success  num_turns=1   result="AGENT REPORTED: SECRET-NUMBER-4471"

In the action, the same pattern was observed on a real run: Claude Code 2.1.220 via anthropics/[email protected], pull_request trigger, --allowedTools "Read,mcp__github_inline_comment__create_inline_comment,Bash(gh pr diff:*),Bash(gh pr view:*)". The execution log ends with the "waiting for the agents" message and "subtype": "success"; no inline comment was ever created.

Why the existing break and this bug are the same problem

The comment on the break explains that without it, the for await sometimes hangs in pull_request runs until timeout-minutes kills the job, and that "by SDK contract no further messages follow a result". That contract describes a turn, not a session. A result closes a turn; a headless session whose background tasks are still running stays alive, delivers a task_notification, starts a new turn and emits another result, as the stream above shows. The iterator that "stays open indefinitely" is most likely this same mechanism: it is waiting for background work the runner does not know about, and it does close once that work finishes (it did in the reproduction). It only looks infinite when the background task never completes, or when nothing is consuming the messages that would say it did.

So the current code produces one of two bad outcomes depending on which side you stand on: wait for the iterator and risk a hang, or break at the first result and get a silent success with the delegated work thrown away. The runner needs to know whether the session is actually done, rather than guessing from the first result.

Expected behavior

  1. Track pending background tasks from the task_started / background_tasks_changed system messages, and break on a result only when none are pending.
  2. Keep a guard against the original hang, for example an inactivity timeout after the last message, and log a clear warning when it fires instead of letting timeout-minutes cancel the job.
  3. Warn loudly when a result arrives while tasks are still pending, so a run that would otherwise pass silently at least says so.
  4. Optionally, disable background subagents (Agent / Task with run_in_background) by default in automation mode, since a one-shot run has no later turn to act on their results.

Independently, the SDK contract the comment relies on ("no further messages follow a result") should either be enforced by the SDK or documented as one-result-per-turn, with sessions able to contain several turns when background tasks exist.

Screenshots

N/A. Execution log excerpt (final assistant message and result) available on request.

Workflow yml file

Reusable workflow, trimmed to the relevant step:

yaml
- name: Automatic PR Review
  uses: anthropics/[email protected]
  with:
    anthropic_api_key: ${{ secrets.anthropic_api_key }}
    track_progress: false
    claude_args: |
      --allowedTools "Read,mcp__github_inline_comment__create_inline_comment,Bash(gh pr diff:*),Bash(gh pr view:*)"
      --model claude-sonnet-5
    prompt: |
      REPO: ${{ github.repository }}
      PR NUMBER: ${{ github.event.pull_request.number }}
      ... review instructions ...

API Provider

[x] Anthropic First-Party API (default) [ ] AWS Bedrock [ ] GCP Vertex

Additional context

  • The break was added to work around the SDK iterator not closing in pull_request runs (per the code comment). Removing it alone would reintroduce that hang; the fix needs the pending-task check.
  • Claude Code changelog entries 2.1.269 ("Fixed remote and headless sessions reporting 'waiting for your input' while background agents were still running") and 2.1.274 ("Fixed headless and SDK sessions making a separate model call for every background task that finished") describe the CLI side of this behaviour. v1.0.231 of the action pins 2.1.278 and still has the early break, so bumping does not help.
  • Workaround we are applying: --disallowedTools Agent,Task in claude_args.

Source: anthropics/claude-code-action