fix: reorder tool_result blocks to match tool_use order (#11804)#11806
Draft
roomote[bot] wants to merge 1 commit intomainfrom
Draft
fix: reorder tool_result blocks to match tool_use order (#11804)#11806roomote[bot] wants to merge 1 commit intomainfrom
roomote[bot] wants to merge 1 commit intomainfrom
Conversation
When the Anthropic API returns multiple tool_use blocks, tool_result blocks must appear in the same order. The existing validateAndFixToolResultIds() already handled missing/duplicate results but did not reorder them. This adds a reorderToolResults() helper that sorts tool_result blocks by their corresponding tool_use index while keeping non-tool-result blocks (text, image) in their original positions.
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.
This PR attempts to address Issue #11804 -- Claude requests failing with HTTP 400 when multiple tool calls run concurrently due to tool_result order mismatch.
Changes
Root cause: When the Anthropic API returns multiple
tool_useblocks in a single assistant turn,tool_resultblocks could be pushed intouserMessageContentin completion order rather than the originaltool_useorder. The Anthropic Messages API requires them to match, so out-of-order results trigger HTTP 400.Fix: Added a
reorderToolResults()helper at the end ofvalidateAndFixToolResultIds()insrc/core/task/validateToolResultIds.ts. After all existing validation (deduplication, ID correction, missing result injection), tool_result blocks are sorted to match the tool_use order from the preceding assistant message, matched bytool_use_id.Key design decisions per the approved plan and reviewer feedback:
Tests
Added 4 new test cases covering:
Updated 2 existing tests whose expected order changed due to the new reordering behavior.
Feedback and guidance are welcome.
Interactively review PR in Roo Code Cloud