Skip to content

fix(memory): strip injected user context from restored messages to prevent O(turns) accumulation#554

Open
citizen204 wants to merge 1 commit into
aws:mainfrom
citizen204:fix-420-strip-user-context-on-restore
Open

fix(memory): strip injected user context from restored messages to prevent O(turns) accumulation#554
citizen204 wants to merge 1 commit into
aws:mainfrom
citizen204:fix-420-strip-user-context-on-restore

Conversation

@citizen204

Copy link
Copy Markdown
Contributor

Summary

When retrieval_config is 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:

Turn 1: [user+XML, assistant]
Turn 2: [user+XML, assistant, user+XML, assistant]
Turn 3: [user+XML, assistant, user+XML, assistant, user+XML, assistant]

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 in list_messages() whenever retrieval_config is configured. Fresh context is re-injected on each new turn by retrieve_customer_context(), so no context is lost.

Fixes #420

Changes

  • src/bedrock_agentcore/memory/integrations/strands/session_manager.py:
    • Added _filter_restored_user_context() method (36 lines, analogous to _filter_restored_tool_context())
    • Called in list_messages() when self.config.retrieval_config is set

…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AgentCoreMemorySessionManager: retrieval_config context accumulates O(turns × records) across multi-turn conversations

1 participant