Skip to content

Fix ClaudeSDKClient.receive_response() returning before background agent work finishes - #1224

Open
dineshyadav03 wants to merge 1 commit into
anthropics:mainfrom
dineshyadav03:fix-1138-receive-response-deferred-result
Open

Fix ClaudeSDKClient.receive_response() returning before background agent work finishes#1224
dineshyadav03 wants to merge 1 commit into
anthropics:mainfrom
dineshyadav03:fix-1138-receive-response-deferred-result

Conversation

@dineshyadav03

Copy link
Copy Markdown

Fixes #1138

ClaudeSDKClient.receive_response() stopped on the first ResultMessage it saw, but a result frame only marks the end of one turn, not necessarily the run: when a delegated background agent/workflow task (local_agent/local_workflow) is still in flight, the CLI emits that result to close out the current turn, then continues with a follow-up turn once the task completes, ending in a second, later ResultMessage. receive_response() had no way to tell the two apart, so it returned on the first (intermediate) one and silently missed everything from the follow-up turn — including the real final result.

Query already tracks in-flight delegated agent tasks via _track_task_lifecycle()/_inflight_tasks (added for #1088, to avoid closing stdin too early). This reuses that same signal: each result frame sent while a delegated task is in flight has its uuid recorded, and Query.is_deferred_result() lets receive_response() recognize such a frame and keep reading instead of returning early.

Adds a regression test (test_receive_response_waits_for_deferred_result) reproducing the exact turn-boundary scenario: an intermediate result while a background agent task is in flight, followed by the task completing and a follow-up turn ending in the real final result.

Test plan

  • python -m ruff check src/ tests/ scripts/ — clean
  • python -m mypy src/ scripts/ — clean
  • python -m pytest tests/ — 1402 passed, 14 skipped

Generated by Claude Code

…ent work finishes

receive_response() stopped on the first ResultMessage it saw, but a result
frame only marks the end of one turn, not necessarily the run: when a
delegated background agent/workflow task is still in flight, the CLI emits
that result to close out the current turn and then continues with a
follow-up turn once the task completes, ending in a second, later
ResultMessage. receive_response() had no way to tell the two apart, so it
returned on the first (intermediate) one and silently missed everything
from the follow-up turn, including the real final result (anthropics#1138).

Query already tracks in-flight delegated agent tasks via
_track_task_lifecycle()/_inflight_tasks (added for anthropics#1088, to avoid closing
stdin too early), so this reuses that same signal: each "result" frame sent
while a delegated task is in flight has its uuid recorded, and
Query.is_deferred_result() lets receive_response() recognize such a frame
and keep reading instead of returning early.

Fixes anthropics#1138.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ResultMessage is being sent while background agent is still working and turn is finished

2 participants