fix(claude_agent_sdk): preserve tool span output across back-to-back tool_use messages#322
Open
Abhijeet Prasad (AbhiPrasad) wants to merge 1 commit intomainfrom
Conversation
…tool_use messages In claude-agent-sdk 0.1.61 a delegated subagent can emit multiple tool_use AssistantMessages (e.g. Bash then Read) inside one LLM turn before any tool_result arrives. ContextTracker._handle_assistant was unconditionally calling cleanup_context whenever the context already had an llm_span, which closed the in-flight Bash tool span with no output. When the matching tool_result finally arrived two events later, the span was already gone and its output was dropped (issue #318). Only run cleanup_context when ctx.next_llm_start is set, which is the real LLM-turn boundary (set in _handle_user when a tool_result arrives). Back-to- back tool_use messages now merge into the existing LLM span without closing sibling tool spans. - Red/green regression: new ContextTracker unit test simulating the 0.1.61 event stream (Bash tool_use -> Read tool_use -> Read tool_result -> Bash tool_result, all under one subagent parent). - Re-records cassettes under claude-agent-sdk 0.1.61 and bumps the matrix latest pin to 0.1.61. test_interleaved_subagent_tool_spans_preserve_output and test_interleaved_subagent_tool_spans_parent_to_correct_llm now pass. - Full test_claude_agent_sdk(latest) and test_claude_agent_sdk(0.1.10) sessions green.
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.
resolves #318
AI Summary
In claude-agent-sdk 0.1.61 a delegated subagent can emit multiple tool_use AssistantMessages (e.g. Bash then Read) inside one LLM turn before any tool_result arrives. ContextTracker._handle_assistant was unconditionally calling cleanup_context whenever the context already had an llm_span, which closed the in-flight Bash tool span with no output. When the matching tool_result finally arrived two events later, the span was already gone and its output was dropped (issue #318).
Only run cleanup_context when ctx.next_llm_start is set, which is the real LLM-turn boundary (set in _handle_user when a tool_result arrives). Back-to- back tool_use messages now merge into the existing LLM span without closing sibling tool spans.