fix: preserve tool_use blocks during condensation regardless of position #10669
+49
−35
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.
Summary
Fixes orphaned
tool_useblocks error during conversation condensation.Error:
ApiProviderError: messages.50: 'tool_use' ids were found without 'tool_result' blocks immediately after: toolu_011CmkW5Ho9AqFuiC84xuYBbPostHog issue: https://us.posthog.com/error_tracking/019ba58c-53b6-7592-8988-d7518c59512c
Root Cause
The Anthropic API requires every
tool_useblock in an assistant message to have a correspondingtool_resultblock in the immediately following user message. During conversation condensation, the bounded search (only last 3 messages) failed to findtool_useblocks that were further back in history, causing orphanedtool_resultblocks.Solution
Replaced the bounded linear search with a performance-optimized indexed lookup:
Performance Analysis
Memory footprint is minimal - only stores references to messages we're looking for.
Testing
All 44 condensation tests pass.
Fixes ROO-516
Important
Replaces linear search with indexed lookup in
getKeepMessagesWithToolBlocks()to preservetool_useblocks, preventing orphanedtool_resultblocks and improving performance.getKeepMessagesWithToolBlocks()inindex.tsto preservetool_useblocks regardless of position.tool_useblocks are preserved even if far back in history, preventing orphanedtool_resultblocks.tool_useblocks, improving from O(n) linear searches.tool_resultsin kept messages.index.spec.tsto verify preservation oftool_useblocks and reasoning blocks.This description was created by
for e00e0c7. You can customize this summary. It will automatically update as commits are pushed.