fix: add fallback trim point for tool-heavy conversations in SlidingWindowConversationManager#2174
Merged
JackYPCOnline merged 1 commit intoApr 21, 2026
Conversation
Contributor
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
|
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.
56c31c2 to
71e428d
Compare
|
Assessment: Approve All three items from the prior review have been addressed. The fix is correct, well-tested, and follows repository conventions. Verification Details
|
JackYPCOnline
approved these changes
Apr 21, 2026
afarntrog
approved these changes
Apr 21, 2026
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.
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
hatch run prepareChecklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.