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

[Bug]: HarnessAgent context-overflow recovery appends the user input twice since #2799 (and likely loses the emergency compaction)

Open littlebird69 opened this issue 1 month ago • 0 comments

Describe the bug

Since #2799, a failed call persists the state, and that state already contains the current turn's input (ReActAgent.java#L1227 → saveStateAfterCallFailure).

HarnessAgent recovers from a context-overflow error with forceCompactAndRetry (HarnessAgent.java#L1058-L1104). It compacts the in-memory state (state.contextMutable().clear(); addAll(compacted), L1090-1091) and then retries with the same msgs via delegate.call(msgs, effective) (L1092).

On the retry:

  1. activateSlotForContext (ReActAgent.java#L659-L681) reloads the state from the state store. HarnessAgent always configures one (JsonFileAgentStateStore by default). The reloaded state already contains the input, because the failed attempt persisted it.
  2. doCallInner → addToContext(msgs) (ReActAgent.java#L1838) appends the same input again.

So after a recovered overflow, the user's input appears twice in the context.

There is also a second consequence, inferred from reading the code and not verified at runtime: the reload replaces the in-memory compacted context with the persisted, uncompacted one. That would make the retry run against the original oversized context and likely overflow again. This happens with any store that returns a copy. A store that returns the same reference would keep the compaction, but the input would still be duplicated.

To Reproduce

  1. Build a HarnessAgent with compaction enabled and the default state store.
  2. Make the model throw a context-overflow error on the first call and succeed on the second.
  3. Call the agent with a single user message, then inspect the persisted context: that user message appears twice.

Expected behavior

Overflow recovery retries once with the compacted context, and the turn's input appears exactly once.

Suggested fix

  • Persist the compacted state before retrying, so that the reload picks it up.
  • Do not re-send input that is already in the context. For example, remove the trailing occurrence of msgs (matched by id) from the context before compacting, or retry with an empty input when msgs is already the tail of the context.

Environment

  • AgentScope-Java Version: 2.0.4 (also on main @035487e)
  • Java Version: 25
  • OS: any

littlebird69 avatar Oct 17 '26 11:10 littlebird69