feat(conversation-manager): add protected_messages to SlidingWindowConversationManager#2196
Open
ghgcm24 wants to merge 1 commit intostrands-agents:mainfrom
Open
Conversation
…nversationManager The sliding window can trim the first user message during context overflow recovery, causing the agent to lose its task prompt. Add a protected_messages parameter that preserves the first N messages from being removed during trimming. Before trimming, the first protected_messages messages are snapshotted. The trim index is clamped to never fall inside the protected region, and any protected messages removed by aggressive overflow trimming are re-inserted afterward. Default is 0 (no protection), preserving full backward compatibility.
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.
The sliding window can trim the first user message during context overflow recovery, causing the agent to lose its task prompt. Add a protected_messages parameter that preserves the first N messages from being removed during trimming.
Before trimming, the first protected_messages messages are snapshot-ted. The trim index is clamped to never fall inside the protected region, and any protected messages removed by aggressive overflow trimming are re-inserted afterward.
Default is 0 (no protection), preserving full backward compatibility.
Description
Motivation
SlidingWindowConversationManagercan trim the first user message during context overflow recovery. When a tool returns a response large enough to overflow the context window, the manager removesthe oldest messages — including the initial task prompt — to make room. The model then sees tool results with no originating prompt and produces meaningless output.
PR #2087 fixed the structural issue (trimmed conversations now start with a
role: "user"message), but the semantic issue remains: that user message may be atoolResult, not the original taskprompt. This is critical for batch/report workloads where the initial prompt defines the entire task.
Public API Changes
SlidingWindowConversationManageraccepts a new optionalprotected_messageskeyword argument: