feat(conversation-manager): improve tool result truncation strategy#1756
feat(conversation-manager): improve tool result truncation strategy#1756kevmyung wants to merge 3 commits intostrands-agents:mainfrom
Conversation
|
@JackYPCOnline Tagging you for review! |
src/strands/agent/conversation_manager/sliding_window_conversation_manager.py
Show resolved
Hide resolved
| @@ -197,10 +209,15 @@ def reduce_context(self, agent: "Agent", e: Exception | None = None, **kwargs: A | |||
| messages[:] = messages[trim_index:] | |||
|
|
|||
| def _truncate_tool_results(self, messages: Messages, msg_idx: int) -> bool: | |||
There was a problem hiding this comment.
Issue: This method is quite long (~70 lines) with multiple nested loops handling three different scenarios (top-level images, nested images in tool results, and text truncation).
Suggestion: Consider extracting helper methods for improved readability:
_replace_image_with_placeholder(image_block)→ returns the text placeholder_truncate_text_block(text, preserve_chars)→ returns truncated text or None if not needed
This would simplify the main loop logic and make the code easier to maintain.
| _PRESERVE_CHARS = 200 | ||
|
|
||
|
|
||
| class SlidingWindowConversationManager(ConversationManager): |
There was a problem hiding this comment.
Issue: The class docstring doesn't mention the new partial truncation behavior. Users looking at the class docs wouldn't know about the improved truncation strategy.
Suggestion: Add a brief note in the class docstring about the truncation behavior, e.g.:
"""Implements a sliding window strategy for managing conversation history.
...
When truncation is enabled, tool results are partially truncated (preserving
first and last 200 characters) rather than completely replaced, and image
blocks are replaced with descriptive placeholders.
"""|
Assessment: Comment This PR improves the truncation strategy with a solid implementation that preserves context rather than completely replacing tool results. The test coverage is comprehensive. Review Summary
Good work on maintaining backward compatibility and implementing oldest-first ordering. |
src/strands/agent/conversation_manager/sliding_window_conversation_manager.py
Outdated
Show resolved
Hide resolved
7e9b87d to
1942cf8
Compare
Partially truncate large tool results (preserve first/last 200 chars) instead of replacing with error. Replace image blocks with placeholders, and target oldest tool results first.
1942cf8 to
fb64029
Compare
|
Assessment: Comment Thanks for updating the class docstring! The truncation behavior is now well-documented (lines 26-29). Review StatusAddressed:
Still Open (from previous review threads):
The implementation is solid and test coverage is comprehensive. The configurability question is worth addressing to fully align with #1545's requirements, but could also be a follow-up PR if preferred. |
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
Description
Improves the
SlidingWindowConversationManagertruncation strategy to reducecontext size more gracefully without misleading the agent.
instead of replacing entire content with an error message
ValueErrorfor invalidper_turnvalues (0 or negative integer)_find_last_message_with_tool_results→_find_oldest_message_with_tool_resultsto accurately reflect iteration order
Related Issues
Closes #1545
Documentation PR
N/A
Type of Change
Other: Enhancement - improve existing truncation behavior
Testing
hatch run prepareAdded unit tests covering partial truncation, image block replacement,
oldest-first ordering, idempotency guard, and boundary conditions.
Checklist