Skip to content

fix: add fallback trim point for tool-heavy conversations in SlidingWindowConversationManager#2174

Merged
JackYPCOnline merged 1 commit into
strands-agents:mainfrom
lufecadu:fix/reduce-context-tool-heavy-fallback
Apr 21, 2026
Merged

fix: add fallback trim point for tool-heavy conversations in SlidingWindowConversationManager#2174
JackYPCOnline merged 1 commit into
strands-agents:mainfrom
lufecadu:fix/reduce-context-tool-heavy-fallback

Conversation

@lufecadu
Copy link
Copy Markdown
Contributor

@lufecadu lufecadu commented Apr 21, 2026

Description

PR #2087 introduced user-first enforcement in reduce_context, which correctly prevents conversations from starting with an assistant message. However, in tool-heavy agent loops where every user message after the initial prompt contains toolResult, there are no valid plain user message trim points. This causes ContextWindowOverflowException on conversations that previously trimmed successfully.

This adds a fallback that trims to an assistant(toolUse) + user(toolResult) boundary when no plain user message exists within the trim range. This preserves valid message ordering for all providers while allowing tool-heavy conversations to be trimmed.

Related Issues

#2173

Type of Change

Bug fix

Testing

How have you tested the change? Verify that the changes do not break functionality or introduce warnings in consuming repositories: agents-docs, agents-tools, agents-cli

  • I ran hatch run prepare

Checklist

  • I have read the CONTRIBUTING document
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@afarntrog
Copy link
Copy Markdown
Contributor

cc @JackYPCOnline

@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 21, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Comment thread tests/strands/agent/test_conversation_manager.py
Comment thread tests/strands/agent/test_conversation_manager.py Outdated
@github-actions
Copy link
Copy Markdown

Assessment: Comment

Good bug fix that addresses a real regression in tool-heavy conversations. The fallback approach is sound and the core logic is correct.

Review Categories

The fix is well-scoped and the fallback logic handles the edge cases correctly.

…indowConversationManager

PR#2087 introduced user-first enforcement in reduce_context, which correctly
prevents conversations from starting with an assistant message. However, in
tool-heavy agent loops where every user message after the initial prompt
contains toolResult, there are no valid plain user message trim points. This
causes ContextWindowOverflowException on conversations that previously
trimmed successfully.

Add a fallback that trims to an assistant(toolUse) + user(toolResult)
boundary when no plain user message exists within the trim range. This
preserves valid message ordering for all providers while allowing
tool-heavy conversations to be trimmed.
@lufecadu lufecadu force-pushed the fix/reduce-context-tool-heavy-fallback branch from 56c31c2 to 71e428d Compare April 21, 2026 18:02
@github-actions github-actions Bot added size/s and removed size/s labels Apr 21, 2026
@lufecadu lufecadu temporarily deployed to manual-approval April 21, 2026 18:02 — with GitHub Actions Inactive
@github-actions
Copy link
Copy Markdown

Assessment: Approve

All three items from the prior review have been addressed. The fix is correct, well-tested, and follows repository conventions.

Verification Details
  • Prior feedback addressed: Tests properly split into separate functions, assertions specify exact trim point (count + toolUseId), and provider compatibility rationale documented in code comments.
  • Algorithm correctness: Traced through the main scenario (8-message tool-heavy conversation) and edge cases (untrimmable history, last-message boundary, mismatched IDs). All paths behave correctly.
  • Existing tests unaffected: The untrimmable_history test still raises ContextWindowOverflowException because the mismatched toolUseIds prevent fallback activation, and trim_index starts past the assistant message at index 0.

@JackYPCOnline JackYPCOnline enabled auto-merge (squash) April 21, 2026 19:11
@JackYPCOnline JackYPCOnline merged commit 7b0337b into strands-agents:main Apr 21, 2026
19 of 20 checks passed
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.

3 participants