fix: filter orphan tool_result blocks after sliding window truncation #11030
+166
−1
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 #11029
When sliding window truncation removes assistant messages containing
tool_useblocks, user messages withtool_resultblocks referencing those removedtool_use_ids were still being sent to the API. Anthropic's API requires everytool_resultto have a correspondingtool_usein the immediately preceding assistant message, so these orphaned references cause the request to be rejected with:Root Cause
The existing orphan
tool_resultfiltering logic ingetEffectiveApiHistory()only ran in the summary (condensation) code path. When there was no summary and only sliding window truncation had been applied, orphantool_resultblocks were not filtered out.Fix
Added the same orphan
tool_resultfiltering to the no-summary code path ingetEffectiveApiHistory():tool_useIDs from visible assistant messagestool_resultblocks in visible user messages that referencetool_use_ids not in the visible setThis mirrors the existing pattern already used in the condensation code path.
Tests
Added 3 test cases:
tool_resultfiltered when both assistant and user messages are truncated bytruncationParenttool_resultfiltered when user message survives truncation but referenced assistant is truncatedtool_resultblocks are strippedAll 5161 existing tests continue to pass.
Important
Adds orphan
tool_resultfiltering togetEffectiveApiHistory()for sliding window truncation inindex.ts.tool_resultfiltering to the no-summary path ingetEffectiveApiHistory()inindex.ts.tool_resultblocks referencing non-visibletool_useIDs and removes empty user messages.index.spec.tsfor orphantool_resultfiltering when assistant messages are truncated, user messages survive truncation, and mixed content in user messages.This description was created by
for ac1d172. You can customize this summary. It will automatically update as commits are pushed.