Skip to content

fix(examples): make MemoryCompactionExample show memory files and fire compaction#1978

Open
147cai wants to merge 1 commit into
agentscope-ai:mainfrom
147cai:fix/memory-compaction-example-paths
Open

fix(examples): make MemoryCompactionExample show memory files and fire compaction#1978
147cai wants to merge 1 commit into
agentscope-ai:mainfrom
147cai:fix/memory-compaction-example-paths

Conversation

@147cai

@147cai 147cai commented Jul 1, 2026

Copy link
Copy Markdown

What

MemoryCompactionExample is 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:

  1. The "Check generated memory files" section always printed
    (memory/ directory not yet created) and never showed any file content.
  2. Compaction never fired — the printed context size kept growing across turns and
    ★ compaction fired! was never reached — despite the Javadoc stating
    triggerMessages=6, keepMessages=2 should fire compaction after a few turns.

Root cause

(1) Memory files are written through WorkspaceManager, which applies a
runtime-data namespace from the RuntimeContext. The default IsolationScope.USER
falls back to the sessionId when no userId is set, so files land under
<workspace>/<sessionId>/memory/ and <workspace>/<sessionId>/MEMORY.md. The
example 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).
CompactionMiddleware resolves it from the model context window (qwen-plus =
131072) to min(8000, max(2000, usable * 0.25)) = 8000 tokens. Because
keepTokens > 0, the cutoff logic uses the token-based keep window and ignores
keepMessages. This short demo conversation is only ~1,000 tokens — far below the
8,000-token keep window — so the cutoff resolves to 0 and compaction is skipped
every time.

Fix

  • Resolve memory-file paths through the namespace-aware WorkspaceManager
    (getMemoryDir(ctx) / resolveRuntimeDataPath(ctx, "MEMORY.md")), and move the
    existing Thread.sleep(3000) before the file check.
  • Add .keepTokens(0) to force message-count keep mode, so compaction triggers
    after a few turns as the Javadoc describes.

Verification

Ran the example with qwen-plus before and after.

Memory files — after:
── Memory files on disk ──
2026-07-01.md
── MEMORY.md content ──

  • Paris is the capital city of France.
  • Tokyo, Japan has a population of approximately 14 million people.

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!

@147cai 147cai requested a review from a team July 1, 2026 08:23
…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.
@147cai 147cai force-pushed the fix/memory-compaction-example-paths branch from 6d9692a to ed1aa54 Compare July 1, 2026 08:47
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@147cai 147cai changed the title fix(examples): read memory files from session namespace in MemoryCompactionExample fix(examples): make MemoryCompactionExample show memory files and fire compaction Jul 1, 2026
@codecov

codecov Bot commented Jul 1, 2026

Copy link
Copy Markdown

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

Development

Successfully merging this pull request may close these issues.

2 participants