fix: prefer target entry for compact counters

This commit is contained in:
Tak Hoffman
2026-04-10 21:01:31 -05:00
parent fb5611b0c4
commit e5e95f30ea
2 changed files with 42 additions and 2 deletions

View File

@@ -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,
}),
);
});
});

View File

@@ -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,