fix: SlidingWindowConversationManager no longer falsely truncates tool results in window#1837
Open
giulio-leone wants to merge 1 commit intostrands-agents:mainfrom
Open
Conversation
…l results in window apply_management() called reduce_context() which prioritizes tool result truncation over sliding window trimming. When message count exceeded window_size, tool results inside the kept window were unnecessarily truncated or marked as errors instead of simply removing old messages. Fix: apply_management() now calls _slide_window() directly to remove old messages first. reduce_context() (used for model context overflow) retains its truncation-first strategy since content size reduction is the priority in that case. Extracted _slide_window() as a shared method for the window trimming logic. Fixes strands-agents#702
e6beadf to
7d43103
Compare
Author
|
Friendly ping — CI is green, tests pass, rebased on latest. Ready for review whenever convenient. Happy to address any feedback. 🙏 |
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 the message count exceeds
window_size,apply_management()callsreduce_context()which prioritizes tool result truncation over sliding window trimming. This causes tool results inside the kept window to be unnecessarily truncated or marked as errors, even though they would be retained after the sliding window trims old messages.For example, with
window_size=5and 8 messages, the manager should remove the 3 oldest messages via sliding. Instead, it finds the oldest tool result (which may be at index 6 — inside the window) and truncates its content first.Reported in #702.
Root Cause
apply_management()delegates entirely toreduce_context(), which has a truncation-first strategy designed for context overflow recovery (when the model reports the context is too large). This strategy is correct for context overflow, but wrong for routine window size management where the goal is to remove old messages, not reduce content size.Fix
apply_management()now calls_slide_window()directly to remove old messages. Tool results inside the kept window are never touched.reduce_context()retains its original truncation-first strategy for context overflow recovery (called by the agent when the model raisesContextWindowOverflowException).Extracted
_slide_window()as a shared method that encapsulates the sliding window trim logic (finding a valid trim index that preserves toolUse/toolResult pairs).Testing
test_apply_management_does_not_falsely_truncate_tool_results_in_window: verifies that withwindow_size=5and 8 messages, tool results inside the window remain untouched withstatus='success'Changes
src/strands/agent/conversation_manager/sliding_window_conversation_manager.py: Refactoredapply_management()and extracted_slide_window()tests/strands/agent/test_conversation_manager.py: Added regression test