From e5e95f30ea8a1a082fc643d7a7d78a906a5e1712 Mon Sep 17 00:00:00 2001 From: Tak Hoffman <781889+Takhoffman@users.noreply.github.com> Date: Fri, 10 Apr 2026 21:01:31 -0500 Subject: [PATCH] fix: prefer target entry for compact counters --- src/auto-reply/reply/commands-compact.test.ts | 42 ++++++++++++++++++- src/auto-reply/reply/commands-compact.ts | 2 +- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/src/auto-reply/reply/commands-compact.test.ts b/src/auto-reply/reply/commands-compact.test.ts index 86203fd9f5b..1309cc42aea 100644 --- a/src/auto-reply/reply/commands-compact.test.ts +++ b/src/auto-reply/reply/commands-compact.test.ts @@ -31,7 +31,7 @@ vi.mock("./commands-compact.runtime.js", () => ({ waitForEmbeddedPiRunEnd: vi.fn().mockResolvedValue(undefined), })); -const { compactEmbeddedPiSession, resolveSessionFilePathOptions } = +const { compactEmbeddedPiSession, incrementCompactionCount, resolveSessionFilePathOptions } = await import("./commands-compact.runtime.js"); function buildCompactParams( @@ -279,4 +279,44 @@ describe("handleCompactCommand", () => { }), ); }); + + it("prefers the target session entry when incrementing compaction count", async () => { + vi.mocked(compactEmbeddedPiSession).mockResolvedValueOnce({ + ok: true, + compacted: true, + result: { + tokensAfter: 321, + }, + }); + + await handleCompactCommand( + { + ...buildCompactParams("/compact", { + commands: { text: true }, + channels: { whatsapp: { allowFrom: ["*"] } }, + } as OpenClawConfig), + sessionKey: "agent:target:whatsapp:direct:12345", + sessionEntry: { + sessionId: "wrapper-session", + updatedAt: Date.now(), + }, + sessionStore: { + "agent:target:whatsapp:direct:12345": { + sessionId: "target-session", + updatedAt: Date.now(), + }, + }, + } as HandleCommandsParams, + true, + ); + + expect(vi.mocked(incrementCompactionCount)).toHaveBeenCalledWith( + expect.objectContaining({ + sessionEntry: expect.objectContaining({ + sessionId: "target-session", + }), + tokensAfter: 321, + }), + ); + }); }); diff --git a/src/auto-reply/reply/commands-compact.ts b/src/auto-reply/reply/commands-compact.ts index e21c0f5f7cb..b4cf4f1b3e1 100644 --- a/src/auto-reply/reply/commands-compact.ts +++ b/src/auto-reply/reply/commands-compact.ts @@ -161,7 +161,7 @@ export const handleCompactCommand: CommandHandler = async (params) => { if (result.ok && result.compacted) { await runtime.incrementCompactionCount({ cfg: params.cfg, - sessionEntry: params.sessionEntry, + sessionEntry: targetSessionEntry, sessionStore: params.sessionStore, sessionKey: params.sessionKey, storePath: params.storePath,