Skip to content

fix(harness): disableMemoryHooks skips compaction-time memory flush - #3572

Open
dargoner wants to merge 4 commits into
agentscope-ai:mainfrom
dargoner:fix/memory-hooks-compaction-flush
Open

dargoner wants to merge 4 commits into
agentscope-ai:mainfrom
dargoner:fix/memory-hooks-compaction-flush

Conversation

@dargoner

Copy link
Copy Markdown
Contributor

AgentScope-Java Version

2.1.0-SNAPSHOT (based on main @ 5f7f8e6)

Description

Fixes #2694.

Background

disableMemoryHooks() already disables per-call memory flush and background consolidation, but normal compaction could still flush long-term memory from the summarized prefix — spending an extra model call and writing memory for an agent that explicitly opted out of memory hooks.

Note: #2699 addressed the same issue but has been stalled with merge conflicts; this is a minimal, rebased version scoped to the compaction path.

Changes

  • CompactionMiddleware: new memoryHooksEnabled flag (4-arg constructor; the existing 3-arg constructor keeps the current behavior of flushing). When disabled, the effective config for the normal path drops the pre-compaction flush, and the emergency (forced) middleware receives the same flag.
  • CompactionConfig: withoutMemoryFlush() returns a copy with flushBeforeCompact=false (small shared copyBuilder() refactor with withEffective).
  • HarnessAgent: passes !disableMemoryHooks when wiring the compaction middleware.

Summarization and session JSONL offload are unaffected; directly constructed CompactionMiddleware instances keep flushing.

Tests

  • CompactionMiddlewareTest 11 → 13: disabledMemoryHooksSkipCompactionTimeFlush, enabledMemoryHooksStillFlushDuringCompaction; suite 13/13.
  • HarnessAgentTest 55/55 (integration wiring unchanged).
  • mvn spotless:check passes.

Checklist

  • 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.) — N/A for this change (internal gating fix)
  • Code is ready for review

- CompactionMiddleware 内部自建 MemoryFlushManager 并在压缩前无条件 flush,
  绕过了 HarnessAgent.disableMemoryHooks():记忆已关闭仍写长期记忆,并额外
  发起一次抽取 LLM 调用。
- CompactionMiddleware 新增 memoryHooksEnabled 开关(3 参构造保持 true 兼容),
  关闭时对生效配置调用 withoutMemoryFlush(),压缩前置 flush 直接跳过;
  overflowRecovery 内部重建的强制实例同样透传该开关。
- CompactionConfig 抽出 copyBuilder 并新增 withoutMemoryFlush()。
- HarnessAgent 构造 compactionHook 时传入 !disableMemoryHooks。
- 回归:CompactionMiddlewareTest 覆盖关闭时跳过 flush、开启时仍 flush;修复前
  关闭用例断言失败(flush 仍发生)。

只调整压缩期记忆 flush 的开关,不改 trigger/keep/summary 算法。
@codecov

codecov Bot commented Oct 14, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@dargoner
dargoner force-pushed the fix/memory-hooks-compaction-flush branch from 14c7407 to d4e1083 Compare October 18, 2026 13:13

This branch has not been deployed

No deployments
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.

[Bug]:CompactionMiddleware 自建 MemoryFlushManager,绕过 disableMemoryHooks()——「记忆已关闭」时仍会写记忆文件并额外调用 LLM

1 participant