Skip to content

fix(memory): correct off-by-one in memory flush cycle dedup#26145

Closed
drvoss wants to merge 1 commit into
openclaw:mainfrom
drvoss:fix/memory-flush-cycle
Closed

fix(memory): correct off-by-one in memory flush cycle dedup#26145
drvoss wants to merge 1 commit into
openclaw:mainfrom
drvoss:fix/memory-flush-cycle

Conversation

@drvoss

@drvoss drvoss commented Feb 25, 2026

Copy link
Copy Markdown
Contributor

Summary

The memory flush cycle had an off-by-one bug: after flushing triggered a compaction, memoryFlushCompactionCount was updated to the new compaction count, making the dedup check skip every other flush cycle.

The fix keeps memoryFlushCompactionCount at the pre-flush value so the next cycle correctly detects a mismatch and runs the flush.

Change Type

  • Bug fix

Scope

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

Linked Issue

Relates to #12590

Security Impact

No security impact. Memory management logic only.

Human Verification

  • Run a long session and observe that memory summaries are created on every eligible cycle, not every other cycle.

AI-Assisted

This PR was prepared with AI assistance.

Greptile Summary

Corrected the memory flush deduplication logic in agent-runner-memory.ts:139-148 by removing the update to memoryFlushCompactionCount after a flush-triggered compaction. Previously, when a flush triggered a compaction, the code updated memoryFlushCompactionCount to the post-increment value, causing shouldRunMemoryFlush to see matching counts on the next cycle and skip the flush—resulting in flushes running only every other eligible cycle.

The fix keeps memoryFlushCompactionCount at the pre-flush value so the next cycle correctly detects a mismatch between compactionCount and memoryFlushCompactionCount, ensuring flushes run on every eligible cycle when tokens remain high.

Changes:

  • Removed assignment of nextCount return value and the conditional update to memoryFlushCompactionCount
  • Added clear comment explaining why memoryFlushCompactionCount should not be updated
  • Added new test case in reply-state.test.ts:322-334 to verify the flush-triggered compaction scenario
  • Added missing setSessionRuntimeModel mock in run.skill-filter.test.ts:116 (unrelated test maintenance)

Confidence Score: 5/5

  • This PR is safe to merge with minimal risk
  • The fix correctly addresses the off-by-one bug by removing the problematic assignment. The logic is sound: keeping memoryFlushCompactionCount at the pre-flush value ensures the deduplication check works correctly on subsequent cycles. The new test case validates the exact scenario that was broken, and the change is minimal and well-documented with an explanatory comment.
  • No files require special attention

Last reviewed commit: c64bdc4

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

2 files reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

Comment on lines 139 to 149
if (memoryCompactionCompleted) {
const nextCount = await incrementCompactionCount({
await incrementCompactionCount({
sessionEntry: activeSessionEntry,
sessionStore: activeSessionStore,
sessionKey: params.sessionKey,
storePath: params.storePath,
});
if (typeof nextCount === "number") {
memoryFlushCompactionCount = nextCount;
}
// Do not update memoryFlushCompactionCount here: keeping it at the
// pre-flush compactionCount ensures shouldRunMemoryFlush sees a mismatch
// on the next turn and runs again if tokens are still high.
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Integration test not updated for new behavior

The fix correctly stops updating memoryFlushCompactionCount to the post-increment value, but the integration test in agent-runner.runreplyagent.test.ts:1618 still asserts the old behavior:

expect(stored[sessionKey].memoryFlushCompactionCount).toBe(2);

With this fix, when compaction completes (bumping compactionCount from 1 → 2), memoryFlushCompactionCount should remain at 1 (the pre-flush value). The assertion at line 1618 should be updated to .toBe(1) to match the intended behavior. As-is, the test "increments compaction count when flush compaction completes" will fail.

Prompt To Fix With AI
This is a comment left during a code review.
Path: src/auto-reply/reply/agent-runner-memory.ts
Line: 139-149

Comment:
**Integration test not updated for new behavior**

The fix correctly stops updating `memoryFlushCompactionCount` to the post-increment value, but the integration test in `agent-runner.runreplyagent.test.ts:1618` still asserts the old behavior:

```
expect(stored[sessionKey].memoryFlushCompactionCount).toBe(2);
```

With this fix, when compaction completes (bumping `compactionCount` from 1 → 2), `memoryFlushCompactionCount` should remain at `1` (the pre-flush value). The assertion at line 1618 should be updated to `.toBe(1)` to match the intended behavior. As-is, the test "increments compaction count when flush compaction completes" will fail.

How can I resolve this? If you propose a fix, please make it concise.

@openclaw-barnacle openclaw-barnacle Bot added the agents Agent runtime and tooling label Feb 25, 2026
@drvoss drvoss force-pushed the fix/memory-flush-cycle branch from 1af1a73 to 97c171c Compare February 25, 2026 05:58
@drvoss drvoss force-pushed the fix/memory-flush-cycle branch 2 times, most recently from 89db58e to 0e5eee9 Compare February 25, 2026 07:19
@openclaw-barnacle openclaw-barnacle Bot added size: XS and removed agents Agent runtime and tooling size: S labels Feb 25, 2026
@drvoss drvoss closed this Feb 25, 2026
@drvoss drvoss force-pushed the fix/memory-flush-cycle branch from 0e5eee9 to fb76e31 Compare February 25, 2026 07:30
@drvoss drvoss reopened this Feb 25, 2026
@drvoss drvoss force-pushed the fix/memory-flush-cycle branch from c64bdc4 to 8e30857 Compare February 25, 2026 07:45
@openclaw-barnacle

Copy link
Copy Markdown

This pull request has been automatically marked as stale due to inactivity.
Please add updates or it will be closed.

@openclaw-barnacle openclaw-barnacle Bot added the stale Marked as stale due to inactivity label Mar 3, 2026
@drvoss

drvoss commented Mar 3, 2026

Copy link
Copy Markdown
Contributor Author

Updating this PR to keep it active. The fix is still valid and ready for review.

@openclaw-barnacle openclaw-barnacle Bot removed the stale Marked as stale due to inactivity label Mar 4, 2026
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@drvoss drvoss force-pushed the fix/memory-flush-cycle branch from 8e30857 to 70bd99f Compare March 7, 2026 06:16
@drvoss

drvoss commented Mar 7, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto current main with a clean single-commit branch. Removes the post-increment memoryFlushCompactionCount = nextCount update to fix the off-by-one in flush cycle dedup.

@drvoss

drvoss commented Mar 17, 2026

Copy link
Copy Markdown
Contributor Author

Closing to reduce active PR count. Will rebase and resubmit if still needed.

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.

1 participant