Skip to content

fix(memory): ensure auto context memory tool message compression works correctly#1279

Open
benym wants to merge 2 commits intoagentscope-ai:mainfrom
benym:fix_automemory
Open

fix(memory): ensure auto context memory tool message compression works correctly#1279
benym wants to merge 2 commits intoagentscope-ai:mainfrom
benym:fix_automemory

Conversation

@benym
Copy link
Copy Markdown
Contributor

@benym benym commented Apr 24, 2026

AgentScope-Java Version

[1.0.11]

Description

[Closes #1260]

Background

AutoContextMemory.compressIfNeeded() implements a multi-strategy compression pipeline to keep the working memory context within token limits. Two logic bugs were found that reduce compression effectiveness:

Bug 1: Strategy 1 cursor/index mismatch

In the Strategy 1 while loop, currentMsgs was re-created from workingMemoryStorage on every iteration, but cursorStartIndex was carried over from the previous iteration's index calculation (based on the old list structure).

After each compression, summaryToolsMessages() replaces multiple tool messages with a single summary via MsgUtils.replaceMsg(), shrinking the list. The next iteration then creates a fresh list from the updated workingMemoryStorage, but applies the stale cursorStartIndex — which was computed against the pre-compression list — to this new list, causing index mismatch.

Impact: Only the first group of consecutive tool messages gets compressed per compressIfNeeded() call; subsequent groups are silently missed.

Fix: Create currentMsgs once before the loop. Since summaryToolsMessages() modifies the list in-place, cursorStartIndex stays consistent with the actual list structure across iterations. replaceWorkingMessage() is called once after the loop completes, which also provides better atomicity.

Bug 2: offloadingLargePayload() lastKeep guard blocks Strategy 3

The early-return guard at the top of offloadingLargePayload():

if (rawMessages.size() < autoContextConfig.getLastKeep()) {
    return false;
}

applies unconditionally to both Strategy 2 (lastKeep=true) and Strategy 3 (lastKeep=false). By design, Strategy 3 should not be subject to lastKeep protection — it only protects messages after the latest assistant response.

Impact: When total message count is less than lastKeep, Strategy 3 is incorrectly skipped, leaving large historical payloads uncompressed in the working memory.

Fix: Add lastKeep && condition so the guard only applies when lastKeep protection is enabled:

if (lastKeep && rawMessages.size() < autoContextConfig.getLastKeep()) {
    return false;
}

How to Test

  1. Strategy 1 fix: io.agentscope.core.memory.autocontext.AutoContextMemoryTest#testMultipleToolGroupsCompressedInSingleCall

  2. Strategy 3 fix:
    io.agentscope.core.memory.autocontext.AutoContextMemoryTest#testStrategy3NotBlockedByLastKeepGuard

Checklist

Please check the following items before code is ready to be reviewed.

  • Code has been formatted with mvn spotless:apply
  • All tests are passing (mvn test)
  • Javadoc comments are complete and follow project conventions
  • Related documentation has been updated (e.g. links, examples, etc.)
  • Code is ready for review

@benym benym requested a review from a team April 24, 2026 09:10
@benym benym changed the title fix(memory): ensure auto memory tool message compression works correctly fix(memory): ensure auto context memory tool message compression works correctly Apr 24, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 24, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant