fix(conversation-manager): handle window_size=0 and reject negative values#2208
fix(conversation-manager): handle window_size=0 and reject negative values#2208SuperMarioYL wants to merge 1 commit intostrands-agents:mainfrom
Conversation
…alues SlidingWindowConversationManager.reduce_context computed trim_index as len(messages) - window_size. With window_size=0 that equals len(messages), so the while-loop guard fired immediately and the else-branch either silently returned (routine management) or raised ContextWindowOverflowException, leaving messages unchanged instead of clearing them. TypeScript SDK explicitly supports window_size=0 as "clear all messages" and has a test for it. Align Python behaviour: - Add a fast-path in reduce_context: if window_size == 0, empty the messages list in-place and update removed_message_count, then return. - Add a ValueError guard in __init__ for negative window_size (parallel to the existing per_turn validation). Fixes strands-agents#2205.
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
| test_agent = Agent(messages=messages) | ||
| manager.reduce_context(test_agent, e=Exception("overflow")) | ||
|
|
||
| assert messages == [] |
There was a problem hiding this comment.
Issue: test_window_size_zero_clears_on_overflow is missing the removed_message_count assertion that the other two window_size=0 tests include. This means the test doesn't verify that the bookkeeping counter is updated in the overflow path.
Suggestion: Add assert manager.removed_message_count == 2 after assert messages == [] for consistency with the other tests and to fully verify the behavior.
|
Assessment: Comment (leans Approve) Clean, well-scoped fix that addresses a real bug with |
Problem
SlidingWindowConversationManagerwithwindow_size=0silently fails to clear messages — the opposite of the intended behaviour documented in the TypeScript SDK.Root cause in
reduce_context:With
window_size=0this setstrim_index = len(messages).The
while trim_index < len(messages)guard is immediatelyFalse, so theelsebranch fires:e is None): logs a warning and returns — messages unchanged.e is not None): raisesContextWindowOverflowException— also wrong.Negative
window_sizevalues weren't validated at construction time either.Closes #2205.
Fix
__init__— raiseValueErrorforwindow_size < 0(parallel to the existingper_turnguard).reduce_context— add a fast-path forwindow_size == 0: emptymessagesin-place, incrementremoved_message_count, and return. This matches the TypeScript SDK's explicitwindow_size=0→ "clear all messages" semantics (sdk-typescript#789).Tests
Four new tests added to
tests/strands/agent/test_conversation_manager.py:test_window_size_negative_raises_value_errorwindow_size=-1raisesValueErrorat constructiontest_window_size_zero_clears_all_messages_on_apply_managementapply_managementwithwindow_size=0empties messages and updatesremoved_message_counttest_window_size_zero_clears_all_messages_on_reduce_contextreduce_contextwithwindow_size=0empties messages without overflowtest_window_size_zero_clears_on_overflowreduce_contextwithwindow_size=0still clears messages even wheneis providedAll 44 existing + new tests pass.