From ed1aa542e2ba4f6a3953242f0c6fe78b17e70ff4 Mon Sep 17 00:00:00 2001 From: 147cai <3244735816@qq.com> Date: Wed, 1 Jul 2026 16:17:41 +0800 Subject: [PATCH] fix(examples): make MemoryCompactionExample show memory files and fire 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 //. 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. --- .../memory/MemoryCompactionExample.java | 24 +++++++++++++++---- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/agentscope-examples/documentation/src/main/java/io/agentscope/examples/documentation2/harness/memory/MemoryCompactionExample.java b/agentscope-examples/documentation/src/main/java/io/agentscope/examples/documentation2/harness/memory/MemoryCompactionExample.java index c52c4fd51..b3bb7c556 100644 --- a/agentscope-examples/documentation/src/main/java/io/agentscope/examples/documentation2/harness/memory/MemoryCompactionExample.java +++ b/agentscope-examples/documentation/src/main/java/io/agentscope/examples/documentation2/harness/memory/MemoryCompactionExample.java @@ -67,6 +67,14 @@ public static void main(String[] args) throws Exception { CompactionConfig.builder() .triggerMessages(6) .keepMessages(2) + // keepTokens defaults to dynamic mode (-1), which the + // middleware resolves from the model context window to a + // large token budget (e.g. ~8k) that always exceeds this + // short demo conversation — so the message-count keep + // window (keepMessages) is never used and compaction never + // fires. Set keepTokens(0) to force message-count keep mode + // so compaction actually triggers after a few turns. + .keepTokens(0) .build()) .memory( MemoryConfig.builder() @@ -118,11 +126,20 @@ public static void main(String[] args) throws Exception { System.out.println(); } + // Wait briefly for the asynchronous memory flush / consolidation to finish + // writing before we read the generated files back from disk. + Thread.sleep(3000); + // ── Check generated memory files ──────────────────────────────────── System.out.println("── Memory files on disk ──\n"); - Path memoryDir = workspace.resolve("memory"); + // Memory files are written under the runtime-data namespace derived from the + // RuntimeContext (default IsolationScope.USER falls back to sessionId when no + // userId is set), e.g. //memory/. Resolve the paths through + // the agent's WorkspaceManager so we read from the same location they were written + // to, rather than assuming they live directly under the workspace root. + Path memoryDir = agent.getWorkspaceManager().getMemoryDir(ctx); if (Files.isDirectory(memoryDir)) { Files.list(memoryDir).sorted().forEach(p -> System.out.println(" " + p.getFileName())); } else { @@ -141,7 +158,7 @@ public static void main(String[] args) throws Exception { } } - Path memoryMd = workspace.resolve("MEMORY.md"); + Path memoryMd = agent.getWorkspaceManager().resolveRuntimeDataPath(ctx, "MEMORY.md"); if (Files.exists(memoryMd)) { String content = Files.readString(memoryMd); System.out.println("\n── MEMORY.md content ──"); @@ -152,9 +169,6 @@ public static void main(String[] args) throws Exception { } } - // Wait briefly for async flush to complete before printing final state - Thread.sleep(3000); - System.out.println("\nWorkspace: " + workspace); System.out.println("\n" + "=".repeat(60)); System.out.println("Done.");