fix: prevent orphaned tool results during interrupted tool execution#2
Open
michaeljabbour wants to merge 1 commit intomicrosoft:mainfrom
Open
fix: prevent orphaned tool results during interrupted tool execution#2michaeljabbour wants to merge 1 commit intomicrosoft:mainfrom
michaeljabbour wants to merge 1 commit intomicrosoft:mainfrom
Conversation
The _check_tool_pair_removable() method was incorrectly marking tool pairs as removable even when not all tool_results had been received yet. This caused the assistant message with tool_calls to be removed prematurely, leaving orphaned tool_result messages that triggered API errors. Root cause: - Method only checked if tool_results existed in the block range - Did not verify that EVERY tool_call had a corresponding tool_result - Returned all_removable=True after finding just one result Bug trigger scenario: 1. Assistant makes tool_calls [A, B, C] 2. Result A arrives 3. User interrupts with 'continue' message 4. Compaction runs, finds only result A 5. BUG: Returns all_removable=True anyway 6. Removes assistant message containing all tool_calls 7. Results B and C arrive later as orphans 8. API error: 'tool_use ids were found without tool_result blocks' The fix: - Added 'found' flag to track each individual tool_call - Only set all_removable=True if ALL tool_calls have results - Added detailed docstring explaining the critical validation - Added regression tests to prevent reintroduction of this bug Impact: - Fixes session crashes when users interrupt tool execution - Prevents data loss from dropped tool results - No API changes, backward compatible
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.
Summary
Fixes a critical bug in context compaction that caused orphaned tool results and API errors when users interrupted tool execution.
Bug Description
The
_check_tool_pair_removable()method was incorrectly marking tool pairs as removable even when not all tool_results had been received yet. This caused the assistant message with tool_calls to be removed prematurely, leaving orphaned tool_result messages that triggered Anthropic API errors.Root Cause
all_removable=Trueafter finding just one resultTrigger Scenario
all_removable=Trueanywaytool_use ids were found without tool_result blocksThe Fix
all_removable=Trueif ALL tool_calls have resultsFiles Changed
amplifier_module_context_simple/__init__.py: Fixed_check_tool_pair_removable()method (18 lines)tests/test_tool_pair_compaction.py: Added 2 regression tests (108 lines)Impact
Testing
Added two comprehensive regression tests:
test_incomplete_tool_results_not_removed()- Verifies bug is fixedtest_complete_tool_results_can_be_removed()- Verifies normal case still worksType
fix