Skip to content

fix(harness): respect disabled memory hooks during compaction - #2699

Open
RICARDO51167 wants to merge 4 commits into
agentscope-ai:mainfrom
RICARDO51167:codex/issue-2694-disable-compaction-flush
Open

RICARDO51167 wants to merge 4 commits into
agentscope-ai:mainfrom
RICARDO51167:codex/issue-2694-disable-compaction-flush

Conversation

@RICARDO51167

Copy link
Copy Markdown
Contributor

AgentScope-Java Version

2.0.3-SNAPSHOT

Description

Fixes #2694.

disableMemoryHooks() already disabled per-call memory flush and background consolidation, but normal compaction and the emergency context-overflow path could still flush long-term memory.

This PR:

  • derives an immutable effective CompactionConfig that forces flushBeforeCompact=false when memory hooks are disabled;
  • applies the same rule to emergency compaction and retry;
  • keeps compaction summarization and session JSONL offload independent;
  • preserves the behavior of directly constructed CompactionMiddleware instances;
  • adds configuration and Harness integration coverage for normal compaction, emergency compaction, offload, and compatibility cases;
  • updates the v2 English and Chinese memory, compaction, and workspace documentation.

Verification:

  • mvn -pl agentscope-harness -am -Dtest=HarnessAgentCompactionMemoryHooksTest,CompactionConfigTest,CompactionMiddlewareTest -Dsurefire.failIfNoSpecifiedTests=false test
  • mvn spotless:apply spotless:check
  • mvn test
  • mvn verify

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
  • Code is ready for review

@CLAassistant

CLAassistant commented Aug 13, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@codecov

codecov Bot commented Aug 13, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 85.24590% with 9 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...agent/memory/compaction/ConversationCompactor.java 80.85% 6 Missing and 3 partials ⚠️

📢 Thoughts on this report? Let us know!

@AgentScopeJavaBot AgentScopeJavaBot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 AI Review

This PR fixes issue #2694 where disableMemoryHooks() did not fully suppress long-term memory flush during compaction. The fix introduces an immutable withFlushBeforeCompact(boolean) copy method on CompactionConfig and derives an effective config in both the normal compaction middleware path and the emergency context-overflow path (forceCompactAndRetry), forcing flushBeforeCompact=false when memory hooks are disabled.

The approach is clean and minimal: the original CompactionConfig is never mutated (immutable copy via copyBuilder()), the withEffective() method is refactored to share the same helper (DRY), and memoryHooksDisabled is threaded through as a final boolean on the HarnessAgent instance. Javadoc is well-updated on all touched APIs. Test coverage is thorough — four integration scenarios covering both compaction paths plus a dedicated unit test for the withFlushBeforeCompact contract and immutability guarantee.

No critical issues found.

@AgentScopeJavaBot AgentScopeJavaBot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 AI Review

This PR fixes issue #2694 where disableMemoryHooks() did not fully suppress long-term memory flush during compaction. The fix introduces an immutable withFlushBeforeCompact(boolean) copy method on CompactionConfig and derives an effective config in both the normal compaction middleware path and the emergency context-overflow path (forceCompactAndRetry), forcing flushBeforeCompact=false when memory hooks are disabled.

The approach is clean and minimal: the original CompactionConfig is never mutated (immutable copy via copyBuilder()), the withEffective() method is refactored to share the same helper (DRY), and memoryHooksDisabled is threaded through as a final boolean on the HarnessAgent instance. Javadoc is well-updated on all touched APIs. Test coverage is thorough — four integration scenarios covering both compaction paths plus a dedicated unit test for the withFlushBeforeCompact contract and immutability guarantee.

No critical issues found.

@AgentScopeJavaBot AgentScopeJavaBot added bug Something isn't working area/harness agentscope-harness (test/runtime support) labels Aug 15, 2026
@oss-maintainer

Copy link
Copy Markdown
Collaborator

⚠️ Merge conflict detected

This PR currently conflicts with main and cannot be merged. Please rebase (or merge main in) and resolve the conflicts:

git fetch upstream
git checkout codex/issue-2694-disable-compaction-flush
git rebase upstream/main
# resolve conflicts, then:
git push --force-with-lease

This is a one-time reminder. Once the conflicts are resolved, comment @oss-maintainer please re-review and I will pick it up again.


Automated notification by github-manager-bot

@RICARDO51167
RICARDO51167 force-pushed the codex/issue-2694-disable-compaction-flush branch from e8c81bf to c7eaab2 Compare September 18, 2026 02:58

@oss-maintainer oss-maintainer left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary

Solid, well-tested fix. disableMemoryHooks() now really means "no memory flush", including inside compaction, and the new withFlushBeforeCompact / copyBuilder() extraction makes both projections expressible. The four behavioural tests in HarnessAgentCompactionMemoryHooksTest cover both hook states, the normal and the overflow flush path, and the explicit-disable case; EN and ZH docs are updated consistently. CI is green, CLA signed, mergeable.

No blocking issue found. The one gap worth tracking is that the emergency path (forceCompactAndRetry) still rebuilds its config from scratch, so it honours the new flush flag but drops the rest of the caller's compaction settings — the same class of asymmetry this PR removes for flush. Details inline.


Automated review by github-manager-bot

@oss-maintainer oss-maintainer left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary

Re-review after the new commit 21793739 ("preserve compaction config on overflow") — the previous review covered c7eaab2b and still stands. Moving dynamic-default resolution out of CompactionMiddleware into ConversationCompactor.resolveEffectiveConfig fixes a real gap: the overflow-recovery path in HarnessAgent.forceCompactAndRetry used to build a throwaway CompactionConfig, so a user-tuned triggerTokens / keepTokens / truncate / prune / offload config was silently dropped exactly when the context was already overflowing. Inheriting effectiveCompactionConfig.withTriggerMessages(1) is the right shape, the shallow-copy javadoc on the new withTriggerMessages is consistent with withEffective, and the added coverage in CompactionConfigTest / ConversationCompactorTest / HarnessAgentCompactionMemoryHooksTest matches the change. CI is green (7/7).

Findings

  • [Warning] HarnessAgent.java:1076 — effectiveCompactionConfig is nullable; the overflow path now depends on an implicit compactionHook != null invariant.
  • [Warning] HarnessAgent.java:1085 — MemoryFlushManager switches from the agent model to the compaction model; confirm that is intended.
  • [Info] ConversationCompactor.java:219 — public entry point now resolves dynamic defaults for every caller; worth a changelog note.
  • [Info] ConversationCompactor.java:272 — effectiveKeep = 0 fallback collapses to message-based keep on the recovery path (pre-existing, not a regression).

Suggestions

Both warnings are small and non-blocking: a local null guard (or Objects.requireNonNull with a message) next to withTriggerMessages(1), and one sentence in the PR description about the flush-model change. Neither changes the correctness of the fix itself, so no re-review is needed unless you disagree with the model-choice point.


Automated review by github-manager-bot

: "default";

CompactionConfig forceConfig = CompactionConfig.builder().triggerMessages(1).build();
CompactionConfig forceConfig = effectiveCompactionConfig.withTriggerMessages(1);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

effectiveCompactionConfig is still a nullable field — build() only assigns it inside if (!disableCompaction && compactionConfig != null) (line 2611), and the previous code built a throwaway config here so this path could never NPE. Today the invariant holds only implicitly: recoverFromOverflow() gates on compactionHook != null, and both are assigned in the same block. Since this is the emergency overflow-recovery path, a future builder branch that wires compactionHook without the field (or a second constructor path) would surface as an NPE exactly when the agent is already in trouble. Could you make the coupling explicit, e.g. a local fallback (CompactionConfig base = effectiveCompactionConfig != null ? effectiveCompactionConfig : CompactionConfig.defaults();) or Objects.requireNonNull(effectiveCompactionConfig, "compactionHook requires a compaction config"), plus a comment stating compactionHook != null implies the field is set?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in ea9e78a: forceCompactAndRetry now uses Objects.requireNonNull with a clear message, and the adjacent comment documents the compactionHook/config invariant.

@oss-maintainer oss-maintainer left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary

Approving on ea9e78a5. That commit answers every point from my review on 21793739, and answers them in the way I would have picked:

  • forceCompactAndRetry no longer reads a nullable field implicitly — Objects.requireNonNull(effectiveCompactionConfig, ...) makes the "compactionHook and the effective config are created together" invariant explicit at the boundary, with the comment saying so.
  • The flush-model question is settled rather than papered over: memory extraction stays on getModel(), only summarisation follows CompactionConfig.model, and that split is now stated in both docs/v2/en and docs/v2/zh plus pinned by flushCalls counters in HarnessAgentCompactionMemoryHooksTest. Reverting the flush to the agent model also keeps this PR's blast radius to "emergency compaction reuses the user's compaction settings", which is the change the title advertises.
  • The effectiveKeep = 0 fallback is now logged, so a model that reports no context window is at least visible on the recovery path instead of silently degrading to a message-count cutoff.
  • Release notes got an Unreleased entry for the ConversationCompactor resolution change — the part that affects callers who use it directly rather than through middleware.

Underlying fix is unchanged from the previous two passes and still correct: resolveEffectiveConfig in the compactor means the overflow-recovery path can no longer diverge from normal compaction, which was the actual bug in #2694.

CI is green on this head (ubuntu + windows build, validate, module-sync, codecov/patch, license/cla). Two non-blocking suggestions are inline — one about asserting the positive half of the flush-model split, one about a second release-notes bullet.


Automated review by github-manager-bot

Comment thread docs/v2/en/docs/others/release-notes.md Resolved

@oss-maintainer oss-maintainer left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-reviewed the new commit (82e112b4) since my previous approval. The delta is test-only plus release-notes lines: emergencyCompactionUsesAgentModelForFlushAndConfiguredModelForSummary now pins which model sees which prompt (flushPromptSeen asserting the memory-extraction prompt reached the reasoning model, and assertNull on the configured compaction model) instead of only counting calls, and RoutingModel records the rendered flush prompt to support it. Renaming the test to match what it now asserts is the right call — the old name promised coverage of the offload setting that the assertions do not check.

The strengthened assertions match the behaviour I verified in the previous pass (flush goes to the agent model, summary goes to the configured model), so the intent of the fix stays pinned. LGTM.


Automated review by github-manager-bot

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

area/harness agentscope-harness (test/runtime support) bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

4 participants