[Bug]: Hook message extraction conflict when using long-term memory and RAG together
Bug Description
When both StaticLongTermMemoryHook and GenericRAGHook are attached to the same agent, they interfere with each other's message extraction logic. Each hook searches for "the last user message" to use as its query, but since both hooks inject their results as USER role messages at the end of the message list, one hook may pick up the other hook's injected message instead of the original user input.
Root Cause
This is a side effect of previous fixes:
- PR #1167 (fixing #1164) changed
StaticLongTermMemoryHookto inject memory as aUSERmessage at the end of the message list (previously it wasSYSTEMat the end) - PR #941 changed
GenericRAGHookto inject retrieved knowledge as aUSERmessage (previously it wasSYSTEM)
Both hooks use "find the last USER role message" to extract the user's query:
StaticLongTermMemoryHook#extractLastUserMessageIndex— scans from end to start, returns the firstUSERrole messageGenericRAGHook#extractQueryFromMessages— scans from end to start, returns the firstUSERrole message's text
Since hooks execute in a chain, when the second hook runs, the first hook has already appended its USER message. The second hook then picks up that injected message as the "user query", not the actual user input.
Example flow (hooks execute by priority; both have priority 50):
- User sends:
USER: "What is the refund policy?" StaticLongTermMemoryHookruns first:extractLastUserMessageIndexfinds"What is the refund policy?"✓- Appends
USER (name="long_term_memory"): <long_term_memory>...</long_term_memory>
GenericRAGHookruns next:extractQueryFromMessagesfinds<long_term_memory>...</long_term_memory>✗ — this is the memory hook's output, not the user's query!- Uses this as the RAG retrieval query, producing irrelevant or incorrect results
The reverse scenario is equally problematic if the RAG hook runs first.
Steps to Reproduce
- Configure a
ReActAgentwith bothStaticLongTermMemoryHookandGenericRAGHook - Send a user query
- Observe that the second-executing hook uses the first hook's injected message as its retrieval query
Expected Behavior
Each hook should extract the original user input message, not the other hook's injected context.
Suggested Fix
Give injected messages distinct name identifiers so that each hook can skip them when extracting the user query:
StaticLongTermMemoryHookalready usesname="long_term_memory"— updateextractLastUserMessageIndexto skip messages wherenameis"retrieved_knowledge"GenericRAGHookshould usename="retrieved_knowledge"(instead of the default"user") — updateextractQueryFromMessagesto skip messages wherenameis"long_term_memory"
This way each hook only considers genuine user input messages, regardless of execution order.
Related Issues
- #941 — Changed GenericRAGHook to use user message (introduced
USERrole injection) - #1164 / #1167 — Changed StaticLongTermMemoryHook to use user message (introduced
USERrole injection at end) - #1262 — Multiple system messages from StaticLongTermMemoryHook (related but different problem)
Another approach: always inject context before the real user message, rather than at the end.
The message list then naturally looks like:
[system, ..., USER(long_term_memory), USER(retrieved_knowledge), USER(real query)]
The real user input is always the last USER message, so extractLastUserMessageIndex / extractQueryFromMessages can find it correctly without any name-based filtering — regardless of which hook runs first.
The name-skipping approach requires hooks to agree on naming conventions and update their skip lists whenever a new hook is added. Injecting before the user message only requires each hook to follow the same rule, no coordination needed.
Either way, the RAG message name should be changed from the default "user" to "retrieved_knowledge" to clearly identify injected content.
Can assign this bug to me ?
Thanks! Assigned this to you. Let me know if you have any questions.
No other questions, and I have solved this issue. I'm a agentscope heavy user, In our team we developed an enterprise framework(Including Agent Admin ) based on agentscope, I'm glad to join agentscope community.