fix: synthesize tool_result placeholders for unanswered tool_use blocks#5
Merged
Merged
Conversation
When a turn dies mid-tool-call (network error, upstream failure, user
cancellation), VS Code's chat history keeps the assistant's tool_use but
its tool_result never lands. Anthropic requires every tool_use to be
answered by a matching tool_result in the immediately-following message,
so every subsequent request built from that history was rejected with a
Vertex 400 ("tool_use ids were found without tool_result blocks
immediately after"), permanently wedging the conversation.
Add a repair pass in buildClaudeBody that synthesizes error placeholder
tool_result blocks for any unanswered tool_use ids: completing a partial
result turn in place, or inserting a new user result turn when the next
message carries no results (or doesn't exist). Covers both the VS Code
chat path and the CLI proxy path.
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.
Problem
When a turn dies mid-tool-call (network error, upstream failure, cancellation), VS Code's chat history keeps the assistant's
tool_usebut itstool_resultnever lands. Anthropic requires everytool_useto be answered by a matchingtool_resultin the immediately-following message, so every subsequent request built from that history was rejected with:Worse, each failed retry appends another dangling tool call, so the conversation wedges permanently. ef4c7bd fixed the trimming path and the reverse case (orphaned
tool_result), but never repaired an unansweredtool_use.Fix
Add a
repairUnansweredToolUsepass inbuildClaudeBodythat synthesizesis_errorplaceholdertool_resultblocks for any unansweredtool_useids — completing a partial result turn in place, or inserting a new user result turn when the next message carries no results (or doesn't exist). Covers both the VS Code chat path and the CLI proxy path, since both build bodies here.Tests
New pairing-invariant helper plus cases for: plain user turn after a failed tool call, partially-answered multi-tool turns, trailing dangling tool call, consecutive failed turns (the wedged-conversation repro), and untouched well-formed sequences. 84/84 passing; typecheck/lint/build green.