Fix ClaudeSDKClient.receive_response() returning before background agent work finishes - #1224
Open
dineshyadav03 wants to merge 1 commit into
Open
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1138
ClaudeSDKClient.receive_response()stopped on the firstResultMessageit saw, but aresultframe 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, laterResultMessage.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.Queryalready 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: eachresultframe sent while a delegated task is in flight has itsuuidrecorded, andQuery.is_deferred_result()letsreceive_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/— cleanpython -m mypy src/ scripts/— cleanpython -m pytest tests/— 1402 passed, 14 skippedGenerated by Claude Code