From f2c813cb31a690502ef55ac1ca44f9f54658880d Mon Sep 17 00:00:00 2001 From: Ayaan Zaidi Date: Fri, 8 May 2026 18:27:34 +0530 Subject: [PATCH] refactor(memory): simplify memory flush counter --- CHANGELOG.md | 2 +- src/auto-reply/reply/agent-runner-memory.ts | 5 ++--- src/auto-reply/reply/reply-state.test.ts | 24 ++++++--------------- 3 files changed, 10 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ca15ebf17b1..ab4d0a2cfa9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -231,7 +231,7 @@ Docs: https://docs.openclaw.ai - Docs/Docker: document a local Compose override for Docker Desktop DNS failures in the shared-network `openclaw-cli` sidecar, keeping the default compose setup hardened while unblocking `openclaw plugins install` when users opt in. Fixes #79018. Thanks @Jason-Vaughan. - Installer: when npm installs `openclaw` outside the parent shell PATH, print follow-up commands with the resolved binary path instead of telling users to run `openclaw` from a shell that will report `command not found`. Fixes #72382. Thanks @jbob762. - Plugins/runtime: share MIME and JSON Schema helpers across bundled plugins while preserving canonical media MIME inference, browser URL wildcard semantics, migration home-path resolution, QA request-limit responses, and extensionless text file previews. -- Agents/memory flush: persist the pre-increment compaction counter after flush-triggered compaction so consecutive eligible compaction cycles run memoryFlush instead of alternating. Fixes #12590; carries forward #51421 and refs #12760, #26145, and #46513. Thanks @Kaspre, @lailoo, @drvoss, @Br1an67, and @dial481. +- Agents/memory flush: persist the pre-increment compaction counter after flush-triggered compaction so consecutive eligible compaction cycles run memoryFlush instead of alternating. Fixes #12590. Refs #12760, #26145, and #46513. Thanks @Kaspre, @lailoo, @drvoss, @Br1an67, and @dial481. - Compute plugin callback authorization dynamically [AI]. (#78866) Thanks @pgondhi987. - fix(active-memory): require admin scope for global toggles [AI]. (#78863) Thanks @pgondhi987. - Honor owner enforcement for native commands [AI]. (#78864) Thanks @pgondhi987. diff --git a/src/auto-reply/reply/agent-runner-memory.ts b/src/auto-reply/reply/agent-runner-memory.ts index 8c54ae38c8e..4e821883566 100644 --- a/src/auto-reply/reply/agent-runner-memory.ts +++ b/src/auto-reply/reply/agent-runner-memory.ts @@ -968,7 +968,7 @@ export async function runMemoryFlushIfNeeded(params: { return result; }, }); - const memoryFlushCompactionCount = + const flushedCompactionCount = activeSessionEntry?.compactionCount ?? (params.sessionKey ? activeSessionStore?.[params.sessionKey]?.compactionCount : 0) ?? 0; @@ -1001,7 +1001,6 @@ export async function runMemoryFlushIfNeeded(params: { }); } } - // Persist the pre-increment count so the next compaction cycle remains eligible to flush. } if (params.storePath && params.sessionKey) { try { @@ -1010,7 +1009,7 @@ export async function runMemoryFlushIfNeeded(params: { sessionKey: params.sessionKey, update: async () => ({ memoryFlushAt: memoryDeps.now(), - memoryFlushCompactionCount, + memoryFlushCompactionCount: flushedCompactionCount, }), }); if (updatedEntry) { diff --git a/src/auto-reply/reply/reply-state.test.ts b/src/auto-reply/reply/reply-state.test.ts index eccae974a2c..4d242382927 100644 --- a/src/auto-reply/reply/reply-state.test.ts +++ b/src/auto-reply/reply/reply-state.test.ts @@ -300,23 +300,13 @@ describe("shouldRunMemoryFlush", () => { softThresholdTokens: 2_000, }; - expect( - shouldRunMemoryFlush({ entry: { totalTokens: 95_000, compactionCount: 1 }, ...params }), - ).toBe(true); - - expect( - shouldRunMemoryFlush({ - entry: { totalTokens: 95_000, compactionCount: 2, memoryFlushCompactionCount: 1 }, - ...params, - }), - ).toBe(true); - - expect( - shouldRunMemoryFlush({ - entry: { totalTokens: 95_000, compactionCount: 3, memoryFlushCompactionCount: 2 }, - ...params, - }), - ).toBe(true); + for (const entry of [ + { totalTokens: 95_000, compactionCount: 1 }, + { totalTokens: 95_000, compactionCount: 2, memoryFlushCompactionCount: 1 }, + { totalTokens: 95_000, compactionCount: 3, memoryFlushCompactionCount: 2 }, + ]) { + expect(shouldRunMemoryFlush({ entry, ...params })).toBe(true); + } }); it("ignores stale cached totals", () => {