[BUG] CLI emits task_notification before subagent's ResultMessage — causes premature completion signal in multi-agent pipelines
Preflight Checklist
- [ ] I have searched existing issues and this hasn't been reported yet
- [x] This is a single bug report (please file separate reports for different bugs)
- [ ] I am using the latest version of Claude Code
What's Wrong?
Summary
In a multi-agent pipeline using claude_agent_sdk, the CLI emits task_notification (status=completed) for a subagent before the subagent has finished executing its tool calls
and sending its ResultMessage. This causes the orchestrating (lead) agent to act on a false completion signal — dispatching downstream tasks that depend on the subagent's output
files, which don't exist yet.
Observed behavior (from trace msg logs with timestamps):
10:07:11 [TASK_START] R1 researcher
10:08:36 Lead receives Agent R1 completed
10:08:36 Lead dispatches Reviewer-1 (trusting the notification)
10:08:36 R1 TASK_PROG: last_tool=WebSearch, tools=23 ← STILL RUNNING
10:09:29 R1 TASK_PROG: last_tool=WebSearch, tools=31 ← STILL RUNNING
10:12:36 R1 TASK_PROG: last_tool=Write, tools=38 ← ACTUALLY writes file now (4 min later)
The lead agent receives task_notification at tools=23, but the researcher continues to execute 15 more tool calls over 4 minutes before actually writing its output file.
Broader impact across 5 parallel researchers:
| Researcher | Notification time | Actual Write time | Gap |
|---|---|---|---|
| R1 | 10:08:36 (tools≈23) | 10:12:36 (tools=38) | +4 min |
| R2 | 10:08:39 (tools≈17) | 10:14:27 (tools=40) | +5.8 min |
| R3 | 10:09:02 (tools≈13) | Never (tools=61+) | ∞ |
[Reference:]https://github.com/anthropics/claude-agent-sdk-python/issues/882
What Should Happen?
Expected behavior
task_notification with status=completed should only be emitted after the subagent's ResultMessage has been written to the transport stream — i.e., after the subagent has
fully finished execution (all tool calls completed, final response generated).
## Actual behavior
task_notification is emitted while the subagent subprocess is still actively executing tool calls. The Python SDK relays it immediately (confirmed by SDK maintainer in
anthropics/claude-agent-sdk-python#882).
Steps to Reproduce
## Environment
- Claude Code version: v2.1.92
- claude-agent-sdk-python: 0.1.56
- OS: Linux (WSL2, kernel 5.15.167.4-microsoft-standard-WSL2)
- Python: 3.10
- Architecture: Lead agent (sonnet4.6) → 5 parallel researcher subagents → reviewer subagents
## Reproduction
Setup: A lead agent spawns researcher subagents via the Task tool. Each researcher performs web searches, then writes a file to disk as its final step. A reviewer subagent is
dispatched only after the corresponding researcher "completes."
Claude Model
Sonnet (default)
Is this a regression?
I don't know
Last Working Version
_No response_
Claude Code Version
v2.1.92
Platform
Other
Operating System
Ubuntu/Debian Linux
Terminal/Shell
WSL (Windows Subsystem for Linux)
Additional Information
_No response_
This issue has 7 comments on GitHub. Read the full discussion on GitHub ↗