Skip to content

fix: SlidingWindowConversationManager no longer falsely truncates tool results in window#1837

Open
giulio-leone wants to merge 1 commit intostrands-agents:mainfrom
giulio-leone:fix/sliding-window-false-truncation-702
Open

fix: SlidingWindowConversationManager no longer falsely truncates tool results in window#1837
giulio-leone wants to merge 1 commit intostrands-agents:mainfrom
giulio-leone:fix/sliding-window-false-truncation-702

Conversation

@giulio-leone
Copy link

Problem

When the message count exceeds window_size, apply_management() calls reduce_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=5 and 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 to reduce_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

  1. apply_management() now calls _slide_window() directly to remove old messages. Tool results inside the kept window are never touched.

  2. reduce_context() retains its original truncation-first strategy for context overflow recovery (called by the agent when the model raises ContextWindowOverflowException).

  3. 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

  • Added test_apply_management_does_not_falsely_truncate_tool_results_in_window: verifies that with window_size=5 and 8 messages, tool results inside the window remain untouched with status='success'
  • All 36 existing conversation manager tests continue to pass

Changes

  • src/strands/agent/conversation_manager/sliding_window_conversation_manager.py: Refactored apply_management() and extracted _slide_window()
  • tests/strands/agent/test_conversation_manager.py: Added regression test

…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
@giulio-leone
Copy link
Author

Friendly ping — CI is green, tests pass, rebased on latest. Ready for review whenever convenient. Happy to address any feedback. 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant