fix(examples): make MemoryCompactionExample show memory files and fire compaction#1978
Open
147cai wants to merge 1 commit into
Open
fix(examples): make MemoryCompactionExample show memory files and fire compaction#1978147cai wants to merge 1 commit into
147cai wants to merge 1 commit into
Conversation
…e compaction 1) Memory files are written under the runtime-data namespace (default IsolationScope.USER falls back to sessionId when no userId is set), so they live under <workspace>/<sessionId>/. The file-check read them from the workspace root and always printed '(memory/ directory not yet created)'. Resolve paths via WorkspaceManager (getMemoryDir / resolveRuntimeDataPath) and move the async-flush wait before the check. 2) keepTokens defaults to dynamic mode and resolves to a large token budget (~8k) exceeding this short demo conversation, so keepMessages was never used and compaction never fired. Set keepTokens(0) to force message-count keep mode so compaction triggers after a few turns as the Javadoc states.
6d9692a to
ed1aa54
Compare
|
|
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
MemoryCompactionExampleis meant to demonstrate two things (per its Javadoc):conversation compaction, and the two-layer memory files it produces. In practice
neither was observable when running the example:
(memory/ directory not yet created)and never showed any file content.★ compaction fired!was never reached — despite the Javadoc statingtriggerMessages=6, keepMessages=2should fire compaction after a few turns.Root cause
(1) Memory files are written through
WorkspaceManager, which applies aruntime-data namespace from the
RuntimeContext. The defaultIsolationScope.USERfalls back to the
sessionIdwhen nouserIdis set, so files land under<workspace>/<sessionId>/memory/and<workspace>/<sessionId>/MEMORY.md. Theexample read them from the workspace root, so the lookup never matched. The check
also ran before the asynchronous flush was awaited.
(2) Compaction uses
keepTokens, which defaults to dynamic mode (-1).CompactionMiddlewareresolves it from the model context window (qwen-plus =131072) to
min(8000, max(2000, usable * 0.25)) = 8000tokens. BecausekeepTokens > 0, the cutoff logic uses the token-based keep window and ignoreskeepMessages. This short demo conversation is only ~1,000 tokens — far below the8,000-token keep window — so the cutoff resolves to 0 and compaction is skipped
every time.
Fix
WorkspaceManager(
getMemoryDir(ctx)/resolveRuntimeDataPath(ctx, "MEMORY.md")), and move theexisting
Thread.sleep(3000)before the file check..keepTokens(0)to force message-count keep mode, so compaction triggersafter a few turns as the Javadoc describes.
Verification
Ran the example with
qwen-plusbefore and after.Memory files — after:
── Memory files on disk ──
2026-07-01.md
── MEMORY.md content ──
Compaction — after:
Compaction triggered: total=25 msgs / 1087 tokens, cutoff=23, keeping=2 msgs
Compaction complete: 25 msgs → 1 summary + 2 tail = 3 total
context: 24 → 4 messages ★ compaction fired!