Skip to content

fix(memory): preserve pre-increment compaction count in memoryFlushCompactionCount (#12590)#69138

Closed
MoerAI wants to merge 1 commit into
openclaw:mainfrom
MoerAI:fix/memory-flush-dedup
Closed

fix(memory): preserve pre-increment compaction count in memoryFlushCompactionCount (#12590)#69138
MoerAI wants to merge 1 commit into
openclaw:mainfrom
MoerAI:fix/memory-flush-dedup

Conversation

@MoerAI

@MoerAI MoerAI commented Apr 20, 2026

Copy link
Copy Markdown
Contributor

Summary

memoryFlush only fires on every other auto-compaction cycle instead of every cycle. The dedup logic falsely skips the flush because memoryFlushCompactionCount is set to the post-increment compaction count.

Root Cause

In runMemoryFlushIfNeeded (src/auto-reply/reply/agent-runner-memory.ts), when compaction completes during a flush, incrementCompactionCount bumps compactionCount from N to N+1, and then memoryFlushCompactionCount is overwritten to N+1 (the new count). On the next cycle, hasAlreadyFlushedForCurrentCompaction compares memoryFlushCompactionCount === compactionCountN+1 === N+1 → true → flush is incorrectly skipped.

Changes

  • src/auto-reply/reply/agent-runner-memory.ts: Stop overwriting memoryFlushCompactionCount with the post-increment count. Keep it at the pre-increment value so the dedup check correctly detects a new compaction cycle.
  • src/auto-reply/reply/agent-runner-memory.test.ts: Update test expectation to match the corrected pre-increment behavior.

Test

pnpm test src/auto-reply/reply/agent-runner-memory.test.ts
pnpm test src/auto-reply/reply/agent-runner-memory.dedup.test.ts
pnpm test src/auto-reply/reply/reply-state.test.ts

All 50 tests pass (3 + 15 + 32).

Closes #12590

…mpactionCount (openclaw#12590)

runMemoryFlushIfNeeded was overwriting memoryFlushCompactionCount with the
post-increment compaction count when compaction completed during a flush.
This caused hasAlreadyFlushedForCurrentCompaction to compare equal on the
next cycle (N+1 === N+1) and falsely skip the flush, making memoryFlush
fire on every other auto-compaction cycle instead of every cycle.

Keep memoryFlushCompactionCount at the pre-increment value so the dedup
check correctly detects a new compaction cycle.
@greptile-apps

greptile-apps Bot commented Apr 20, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes a bug where memoryFlush would fire on every other auto-compaction cycle instead of every cycle. The root cause was that memoryFlushCompactionCount was being overwritten with the post-increment compaction count, making the hasAlreadyFlushedForCurrentCompaction dedup check falsely return true on the next invocation and skip the flush. The fix is correct: keeping memoryFlushCompactionCount at the pre-increment value restores the intended dedup semantics, and the primary token-threshold gate still prevents spurious repeated flushes within the same cycle.

Confidence Score: 5/5

Safe to merge — targeted, well-documented bug fix with passing tests.

The change is a two-line deletion and one-line no-op (void nextCount) in a clearly understood code path, backed by an explanatory comment and a corrected test expectation. The logic is sound: using the pre-increment compaction count preserves dedup within a cycle while allowing the flush to fire on each new compaction cycle. No P0/P1 findings.

No files require special attention.

Reviews (1): Last reviewed commit: "fix(memory): preserve pre-increment comp..." | Re-trigger Greptile

@prtags

prtags Bot commented Apr 23, 2026

Copy link
Copy Markdown

Related work from PRtags group charming-crane-ftmo

Title: Open PR duplicate: memoryFlush compaction counter skip

Number Title
#51421 fix(memory): memoryFlush fires every compaction cycle instead of every other
#69138* fix(memory): preserve pre-increment compaction count in memoryFlushCompactionCount (#12590)

* This PR

@clawsweeper

clawsweeper Bot commented Apr 26, 2026

Copy link
Copy Markdown
Contributor

Closing this as duplicate or superseded after Codex automated review.

PR #69138 is a focused and plausible fix, but it duplicates the same remaining memoryFlush compaction-counter work already tracked by older open PR #51421 and bug #12590. Current main still has the implicated post-increment assignment, so this is not implemented; the best cleanup is to close this duplicate and keep review concentrated on #51421.

Best possible solution:

Close #69138 as a duplicate and keep the remaining review and landing discussion on older open PR #51421, with #12590 as the user-facing bug report. When landing the canonical fix, preserve the pre-increment counter behavior and include regression coverage that proves memoryFlush can run on consecutive compaction cycles.

What I checked:

So I’m closing this here and keeping the remaining discussion on the canonical linked item.

Codex Review notes: model gpt-5.5, reasoning high; reviewed against d54d2d6b9b8a.

@clawsweeper clawsweeper Bot closed this Apr 26, 2026
@MoerAI MoerAI deleted the fix/memory-flush-dedup branch April 27, 2026 11:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: memoryFlush does not fire reliably

1 participant