agentscope-ai / agentscope-ai/agentscope-java

[Feature]: Add iterative compression strategy to AutoContextMemory for handling large contexts

Aperta
#1,305 3 commenti 0 reazioni 0 assegnatari Vedi su GitHub
area/ext/memory enhancement
Lingua principale
Java
Stelle
5.6k
Fork
1.3k
Merge medio
4g 12h
PR unite (30g)
77

Descrizione

AgentScope-Java is an open-source project. To involve a broader community, we recommend asking your questions in English.

Is your feature request related to a problem? Please describe.

When using AutoContextMemory with long conversations, the current single-pass compression strategy (V1) has a significant limitation: after applying one compression strategy (strategies 1-6), if the context still exceeds the configured thresholds (message count or token count), the compression stops. This can result in the context still being too large for the LLM's input window (e.g., 128K tokens).

For example, in a conversation with many tool invocations and large messages:

Strategy 1 compresses tool invocations — context reduced but still above threshold
V1 stops here because one strategy was already applied
The remaining context may still exceed 128K, causing LLM API failures
Describe the solution you'd like

Implement a V2 iterative compression strategy that continues applying compression strategies 1-6 in a loop until either:

The context falls below both message count and token count thresholds, OR
The maximum number of compression iterations is reached (prevents infinite loops), OR
All compression strategies are exhausted (no more compressible content)
Implementation details:

New config fields in AutoContextConfig:

enableIterativeCompression (default: false) — enables V2 iterative compression
maxCompressionIterations (default: 3) — maximum number of compression iterations
Usage:

AutoContextConfig config = AutoContextConfig.builder()
.enableIterativeCompression(true)
.maxCompressionIterations(3)
.msgThreshold(100)
.maxToken(128 * 1024)
.tokenRatio(0.75)
.build();
Behavior change:
When enableIterativeCompression is set to true, compressIfNeeded() automatically delegates to compressIfNeededV2(), which iteratively applies strategies 1-6:

while (iteration < maxIterations && needsCompression(messages)) {
iteration++
compressedThisIteration = applyStrategies1to6(messages)
if (!compressedThisIteration) break // strategies exhausted
}
Backward compatibility: V1 single-pass compression remains the default behavior (enableIterativeCompression = false), so existing users are not affected.

Test coverage: 18 unit tests covering configuration, basic functionality, iterative compression, threshold checks, strategy ordering, edge cases, and V1 vs V2 comparison.

Describe alternatives you'd like to consider

Increase single-pass compression aggressiveness: Instead of iterating, make each strategy more aggressive (e.g., compress more messages per pass). This approach is harder to tune and may over-compress.

Add more compression strategies: Introduce additional strategies beyond the existing 6. This increases complexity without guaranteeing the context will fall below thresholds.

Pre-emptive compression: Compress after every N messages regardless of threshold. This wastes LLM tokens on unnecessary compression and doesn't adapt to actual context size.

The iterative approach (V2) is preferred because it:

Applies compression progressively (lightweight strategies first)
Stops as soon as the context is within bounds
Has a clear termination condition (max iterations)
Is backward compatible with existing V1 behavior
Additional context

Modified files:

AutoContextConfig.java — Added enableIterativeCompression, maxCompressionIterations fields with builder methods and getters
AutoContextMemory.java — Added compressIfNeededV2(), needsCompression(), applySingleCompressionPass(), copyMessages() methods; modified compressIfNeeded() to delegate to V2 when enabled
AutoContextMemoryV2Test.java — New test class with 18 test cases
Online testing: This feature has been validated in production environments with positive feedback on context size reduction effectiveness.

Guida per i contributori

Apri la guida per i contributori

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.