Skip to content

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

Closed

Description

@littlebird69

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

Activity

  1. added a commit that references this issue on Oct 20, 2026
    86ef22b
  2. added a commit that references this issue on Oct 22, 2026
    168647c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions