Skip to content

feat(auto-contextMemory): implement AutoContextMemory with multi-strategy intelligent context compression and offloading - #1523

Closed
BEASTSHRIRAM wants to merge 6 commits into
agentscope-ai:mainfrom
BEASTSHRIRAM:feat/autocontext-async-perstategy-allsystem
Closed

BEASTSHRIRAM wants to merge 6 commits into
agentscope-ai:mainfrom
BEASTSHRIRAM:feat/autocontext-async-perstategy-allsystem

Conversation

@BEASTSHRIRAM

Copy link
Copy Markdown
Contributor

AgentScope-Java Version

1.0.13-SNAPSHOT

Description

This PR solves issue #1350 by addressing structural and behavioral limitations within AutoContextMemory and AutoContextHook.

Background & Purpose:
While extending AutoContextMemory, several limitations restricted flexibility and latency:

  1. It was impossible to toggle individual compression strategies (e.g., dropping short messages, summarizing previous rounds) independently.
  2. The core compression loop used synchronous .block() calls, breaking the reactive chain and degrading performance under heavy compression scenarios.
  3. AutoContextHook only preserved the first SYSTEM message, accidentally wiping out any subsequent system instructions (e.g., roleplay directives alongside system prompts).

Changes Made:

  • AutoContextConfig: Introduced strategy1Enabled through strategy6Enabled (defaulting to true) to allow fine-grained control over which compression rules execute.
  • AutoContextMemory: Refactored the core implementation to be completely asynchronous using Spring Reactor types (Mono and Flux). All synchronous .block() wrappers were eliminated internally, and the primary entry point is now compressIfNeededAsync(). (The synchronous compressIfNeeded() is kept as a @Deprecated wrapper for backwards compatibility).
  • AutoContextHook: Modified handlePreReasoning to collect and preserve all SYSTEM messages. The memory-offloading instruction is now correctly appended to only the first SYSTEM message without discarding the others.
  • Testing: Comprehensive tests were added to AutoContextHookTest, AutoContextMemoryTest, and AutoContextConfigTest to verify that concurrent evaluation logic resolves correctly and that multiple system prompts remain intact.

How to test this PR:

  • Verify tests via mvn clean test -pl agentscope-extensions/agentscope-extensions-autocontext-memory -am.
  • Spotless formatting can be verified using mvn spotless:check.

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

@BEASTSHRIRAM
BEASTSHRIRAM requested review from a team and Copilot May 28, 2026 18:55

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

This PR makes AutoContext context compression fully reactive (non-blocking), adds per-strategy enable/disable switches, and improves SYSTEM message handling so multiple SYSTEM prompts are preserved while appending the offload instruction only to the first.

Changes:

  • Refactor compression pipeline to compressIfNeededAsync() using Reactor (Mono/Flux) and deprecate the blocking compressIfNeeded().
  • Add configuration flags to enable/disable compression strategies 1–6.
  • Preserve multiple SYSTEM messages in AutoContextHook and add a unit test covering this behavior.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 12 comments.

File Description
agentscope-extensions/agentscope-extensions-autocontext-memory/src/main/java/io/agentscope/core/memory/autocontext/AutoContextMemory.java Converts compression strategies to a reactive pipeline; adds concurrency for some summarization paths; keeps a deprecated blocking wrapper.
agentscope-extensions/agentscope-extensions-autocontext-memory/src/main/java/io/agentscope/core/memory/autocontext/AutoContextHook.java Switches PreReasoning handling to the async compressor and preserves multiple SYSTEM messages.
agentscope-extensions/agentscope-extensions-autocontext-memory/src/main/java/io/agentscope/core/memory/autocontext/AutoContextConfig.java Adds per-strategy enable/disable switches and exposes them via getters + builder.
agentscope-extensions/agentscope-extensions-autocontext-memory/src/test/java/io/agentscope/core/memory/autocontext/AutoContextHookTest.java Adds test to ensure multiple SYSTEM messages are preserved and instruction is appended only to the first.

@BEASTSHRIRAM BEASTSHRIRAM changed the title feat: implement AutoContextMemory with multi-strategy intelligent context compression and offloading feat(auto-contextMemory): implement AutoContextMemory with multi-strategy intelligent context compression and offloading May 28, 2026
@codecov

codecov Bot commented May 28, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 87.20472% with 65 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...ope/core/memory/autocontext/AutoContextMemory.java 85.11% 25 Missing and 39 partials ⚠️
...scope/core/memory/autocontext/AutoContextHook.java 97.61% 0 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

@AgentScopeJavaBot AgentScopeJavaBot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 AI Review

This PR makes three valuable changes: per-strategy enable/disable flags, multi-SYSTEM message preservation in the hook, and conversion of the compression pipeline from blocking .block() calls to a Reactor chain. The refactor is comprehensive and well-documented, with new tests covering the SYSTEM-preservation and disabled-strategy cases. However, the new concurrent execution in Strategy 4 and Strategy 5 introduces a real thread-safety regression: offloadContext is a plain HashMap and is now mutated from multiple boundedElastic worker threads simultaneously. There are also a few smaller issues (loss of subscribeOn(boundedElastic) in the hook, hard-coded block(60s) in the deprecated wrapper, raw Object[] tuple in Strategy 4, misleading "exhausted" log when all strategies are disabled). Recommend changes before merge.

@BEASTSHRIRAM

Copy link
Copy Markdown
Contributor Author

@LearningGp Could you please review the PR?

AgentScopeJavaBot

This comment was marked as duplicate.

@LearningGp

Copy link
Copy Markdown
Member

@shiyiyue1102 PTAL

@BEASTSHRIRAM

Copy link
Copy Markdown
Contributor Author

@shiyiyue1102 could you please review it?

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

Labels

area/ext/memory Memory/session extension implementations enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants