Background agents complete but output files are empty
Summary
When launching Task agents with run_in_background: true, the agents complete successfully (status shows "completed") but their output files are empty. This requires re-running agents without the background flag to get actual results.
Environment
- Product: Claude Code CLI
- Platform: Windows 11
Steps to Reproduce
- Launch multiple Task agents with
run_in_background: true - Wait for all agents to complete (TaskOutput returns status: "completed")
- Attempt to read the output files using the Read tool or
catcommand
Expected Behavior
The output files at the paths provided in the Task tool response should contain the agent's output/results.
Example expected output file path:
C:\Users\...\AppData\Local\Temp\claude\...\tasks\a10faa3.output
This file should contain the agent's analysis, findings, and conclusions.
Actual Behavior
- Task tool returns successfully with output_file path:
Async agent launched successfully.
agentId: a10faa3
output_file: C:\Users\...\tasks\a10faa3.output
- TaskOutput confirms completion:
<retrieval_status>success</retrieval_status>
<task_id>a10faa3</task_id>
<task_type>local_agent</task_type>
<status>completed</status>
- But reading the output file returns empty or minimal content:
# Using Read tool:
Warning: the file exists but is shorter than the provided offset (1). The file has 1 lines.
# Using cat:
(no output)
- Eventually, task-notification messages arrive with the actual results, but by then the workflow has been interrupted.
Workaround
Re-run the agents without run_in_background: true to get results synchronously. This defeats the purpose of parallel agent execution.
Impact
- Workflow disruption: Cannot process parallel agent results efficiently
- Wasted time: Must re-run agents synchronously after discovering empty outputs
- Unreliable parallel execution: Background agents cannot be trusted to produce usable output
Session Details
During a PR review workflow:
- Launched 5 agents in parallel (code-reviewer, pr-test-analyzer, silent-failure-hunter, comment-analyzer, code-simplifier)
- All 5 returned "completed" status via TaskOutput
- All 5 output files were empty when read
- Had to re-run each agent synchronously to get actual results
Additional Context
This has happened multiple times, suggesting this is a recurring issue rather than a one-time failure.
Suggested Investigation Areas
- Race condition between agent completion and output file write
- Output file being written to wrong location
- Output stream not being flushed before marking task complete
- Windows-specific file path or encoding issue
- Task completion notification sent before output fully written
This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗