agentscope-java icon indicating copy to clipboard operation
agentscope-java copied to clipboard

[Bug]: Hook message extraction conflict when using long-term memory and RAG together

Open laeni opened this issue 6 months ago • 4 comments

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 StaticLongTermMemoryHook to inject memory as a USER message at the end of the message list (previously it was SYSTEM at the end)
  • PR #941 changed GenericRAGHook to inject retrieved knowledge as a USER message (previously it was SYSTEM)

Both hooks use "find the last USER role message" to extract the user's query:

  • StaticLongTermMemoryHook#extractLastUserMessageIndex — scans from end to start, returns the first USER role message
  • GenericRAGHook#extractQueryFromMessages — scans from end to start, returns the first USER role 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):

  1. User sends: USER: "What is the refund policy?"
  2. StaticLongTermMemoryHook runs first:
    • extractLastUserMessageIndex finds "What is the refund policy?" ✓
    • Appends USER (name="long_term_memory"): <long_term_memory>...</long_term_memory>
  3. GenericRAGHook runs next:
    • extractQueryFromMessages finds <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

  1. Configure a ReActAgent with both StaticLongTermMemoryHook and GenericRAGHook
  2. Send a user query
  3. 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:

  • StaticLongTermMemoryHook already uses name="long_term_memory" — update extractLastUserMessageIndex to skip messages where name is "retrieved_knowledge"
  • GenericRAGHook should use name="retrieved_knowledge" (instead of the default "user") — update extractQueryFromMessages to skip messages where name is "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 USER role injection)
  • #1164 / #1167 — Changed StaticLongTermMemoryHook to use user message (introduced USER role injection at end)
  • #1262 — Multiple system messages from StaticLongTermMemoryHook (related but different problem)

laeni avatar May 15 '26 11:05 laeni

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.

laeni avatar May 15 '26 11:05 laeni

Can assign this bug to me ?

hexly666 avatar May 18 '26 16:05 hexly666

Thanks! Assigned this to you. Let me know if you have any questions.

laeni avatar May 19 '26 08:05 laeni

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.

hexly666 avatar May 19 '26 14:05 hexly666