fix(memory): strip injected user context from restored messages to prevent O(turns) accumulation#554
Open
citizen204 wants to merge 1 commit into
Open
Conversation
…event O(turns) accumulation When retrieval_config is set, retrieve_customer_context() prepends a <context_tag> block to each user message and the message is persisted with that block in storage. On every subsequent turn list_messages() reloads all historical messages verbatim, so the injected block reappears in every restored user message — O(turns × records) tokens. Add _filter_restored_user_context() (mirroring the existing _filter_restored_tool_context pattern) and call it from list_messages() whenever retrieval_config is configured. The filter removes any content block whose text starts with the opening context tag before the messages are handed to the Strands runtime. Fresh context is re-injected on each new turn by retrieve_customer_context(), so no context is lost. Fixes aws#420
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.
Summary
When
retrieval_configis configured,retrieve_customer_context()prepends a<context_tag>XML block to each user message, and the message is persisted to AgentCore Memory with that block embedded. On every subsequent turn,list_messages()reloads all historical messages verbatim — so every restored user message re-appears with its injected context block intact. The result is O(turns × records) token accumulation:Observed with 5 turns and 3 preference records: 3,655 chars of repeated context (83% of total message content).
Fix
Added
_filter_restored_user_context()— mirroring the existing_filter_restored_tool_context()pattern — which strips any content block whose text starts with the opening<context_tag>before the messages are handed to the Strands runtime. The filter is applied inlist_messages()wheneverretrieval_configis configured. Fresh context is re-injected on each new turn byretrieve_customer_context(), so no context is lost.Fixes #420
Changes
src/bedrock_agentcore/memory/integrations/strands/session_manager.py:_filter_restored_user_context()method (36 lines, analogous to_filter_restored_tool_context())list_messages()whenself.config.retrieval_configis set