Skip to content

fix(harness): persist compacted state and avoid duplicate input on overflow retry - #3661

Merged
jujn merged 1 commit into
agentscope-ai:mainfrom
littlebird69:fix/harness-overflow-recovery-duplicate-input
Oct 22, 2026
Merged

jujn merged 1 commit into
agentscope-ai:mainfrom
littlebird69:fix/harness-overflow-recovery-duplicate-input

Conversation

@littlebird69

@littlebird69 littlebird69 commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

AgentScope-Java Version

2.1.0-SNAPSHOT (main @ 2c6d0eb)

Description

Fixes #3626.

Since #2799 a failed call persists its state, and that state already contains the turn's input. HarnessAgent#forceCompactAndRetry compacted only the in-memory state and then retried with the same msgs. On the retry, activateSlotForContext reloads the session from the state store, so:

  1. the input was appended a second time (addToContext(msgs)), and
  2. with a store that returns a copy on load (e.g. JsonFileAgentStateStore), the compacted context was replaced by the persisted, uncompacted one, so the retry ran against the original oversized context.

Both were reproduced with the new tests. Consequence 2 was only inferred in the issue; on the unfixed code the JSON-file variant fails on it while the in-memory variant (which returns the same reference) does not, as predicted.

On current main the native Session Log mode is the default and skips this recovery path, so the bug affects legacySessionHistory(true) / SessionHistoryMode.LEGACY, and the 2.0.x line, where legacy is the default.

Changes

  • Persist the compacted context through ReActAgent#updateAgentState (on boundedElastic, since stores may block) before retrying, so the reload picks it up. Without a state store the cached state is updated in place, as before.
  • If the turn's input is already in the context (matched by message id, checked before compaction since compaction may fold it into the summary), retry with an empty input. The retry then continues from the persisted progress instead of appending the input again. Otherwise the input is passed as before.

Retrying with an empty input matches what the docs describe for the native Session Log mode ("does not resubmit the user task and does not re-run tools that already completed"). It also follows the empty-input continuation doCallInner already uses for shutdown recovery. If the overflow happens after a tool step, the tool result is kept and the tool is not executed again.

Tests

New HarnessAgentOverflowRecoveryTest (legacy mode, scripted model):

  • recoveredOverflowKeepsTheInputOnceAndRetriesWithTheCompactedContext, parameterized over JsonFileAgentStateStore and InMemoryAgentStateStore: the input is persisted once and sent to the model once, and the retry request no longer contains the folded prefix.
  • overflowAfterAToolStepContinuesWithoutRerunningTheTool: the tool runs once, and the input and tool result are each persisted once.

Each half of the fix was checked by reverting it: removing the persist step fails the compaction assertion for both stores; removing the input check fails the "input once" assertion. All three cases fail on the unfixed code.

Validation (JDK 25, Windows 11):

  • mvn -pl agentscope-harness spotless:check test: 1,257 tests, 0 failures/errors, 18 skipped; Spotless check passed. The new test class was also run against the unfixed code (all 3 cases fail there). Full-project clean verify was not run locally.

Checklist

  • Code has been formatted with mvn spotless:apply
  • All tests are passing (mvn test) (harness module, see above)
  • Javadoc comments are complete and follow project conventions
  • Related documentation has been updated (no doc change needed: this aligns legacy mode with the behavior the docs already describe)
  • Code is ready for review

…erflow retry

Since agentscope-ai#2799 a failed call persists its state, including the turn's input.
forceCompactAndRetry compacted only the in-memory state and retried with the
same msgs; the retry reloads the session from the state store, so the input
was appended twice and, with stores that return a copy, the compaction was
lost.

Persist the compacted context via updateAgentState before retrying, and retry
with an empty input when the turn's input is already in the context, so the
retry continues from the persisted progress without re-running tools.

Fixes agentscope-ai#3626
@littlebird69
littlebird69 force-pushed the fix/harness-overflow-recovery-duplicate-input branch from fdcfbbc to 86ef22b Compare October 20, 2026 03:40
@littlebird69

Copy link
Copy Markdown
Contributor Author

Hi @jujn @xzxiaoshan, could one of you take a look when you have time? CI is green and the branch merges cleanly into current main. The PR fixes #3626 (duplicate input / lost compaction on the legacy overflow retry). The tests and the revert checks are in the description. Thanks!

@jujn jujn self-assigned this Oct 22, 2026
@jujn
jujn merged commit 168647c into agentscope-ai:main Oct 22, 2026
5 checks passed
xzxiaoshan added a commit to xzxiaoshan/agentscope-java that referenced this pull request Oct 23, 2026
Conflict resolution in HarnessAgent#forceCompactAndRetry: keep main's
overflow-retry rework (agentscope-ai#3661 — persist compacted state via
updateAgentState, retry with retryInput to avoid duplicate input) and
re-apply the capability-surface re-prepare on the retry call so the
grant surface survives the overflow path.

Adapt HarnessSkillMiddlewareGrantTest to main's agentscope-ai#3691 change: the
skills block now rides a tail runtime material (harness:skills) instead
of the system prompt, so grant-visibility assertions move there.
chickenlj added a commit that referenced this pull request Nov 13, 2026
chickenlj added a commit that referenced this pull request Nov 13, 2026
chickenlj added a commit that referenced this pull request Nov 13, 2026
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.

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

2 participants