fix: update totalTokens after compaction using last-call usage (#15018)

Merged via /review-pr -> /prepare-pr -> /merge-pr.

Prepared head SHA: 9214291bf7
Co-authored-by: shtse8 <8020099+shtse8@users.noreply.github.com>
Co-authored-by: gumadeiras <5599352+gumadeiras@users.noreply.github.com>
Reviewed-by: @gumadeiras
This commit is contained in:
Kyle Tse
2026-02-12 23:02:30 +00:00
committed by GitHub
parent 033d5b5c15
commit a10f228a5b
10 changed files with 602 additions and 19 deletions

View File

@@ -131,6 +131,68 @@ describe("createFollowupRunner compaction", () => {
expect(onBlockReply.mock.calls[0][0].text).toContain("Auto-compaction complete");
expect(sessionStore.main.compactionCount).toBe(1);
});
it("updates totalTokens after auto-compaction using lastCallUsage", async () => {
const storePath = path.join(
await fs.mkdtemp(path.join(tmpdir(), "openclaw-followup-compaction-")),
"sessions.json",
);
const sessionKey = "main";
const sessionEntry: SessionEntry = {
sessionId: "session",
updatedAt: Date.now(),
totalTokens: 180_000,
compactionCount: 0,
};
const sessionStore: Record<string, SessionEntry> = { [sessionKey]: sessionEntry };
await saveSessionStore(storePath, sessionStore);
const onBlockReply = vi.fn(async () => {});
runEmbeddedPiAgentMock.mockImplementationOnce(
async (params: {
onAgentEvent?: (evt: { stream: string; data: Record<string, unknown> }) => void;
}) => {
params.onAgentEvent?.({
stream: "compaction",
data: { phase: "end", willRetry: false },
});
return {
payloads: [{ text: "done" }],
meta: {
agentMeta: {
// Accumulated usage across pre+post compaction calls.
usage: { input: 190_000, output: 8_000, total: 198_000 },
// Last call usage reflects post-compaction context.
lastCallUsage: { input: 11_000, output: 2_000, total: 13_000 },
model: "claude-opus-4-5",
provider: "anthropic",
},
},
};
},
);
const runner = createFollowupRunner({
opts: { onBlockReply },
typing: createMockTypingController(),
typingMode: "instant",
sessionEntry,
sessionStore,
sessionKey,
storePath,
defaultModel: "anthropic/claude-opus-4-5",
agentCfgContextTokens: 200_000,
});
await runner(baseQueuedRun());
const store = loadSessionStore(storePath, { skipCache: true });
expect(store[sessionKey]?.compactionCount).toBe(1);
expect(store[sessionKey]?.totalTokens).toBe(11_000);
// We only keep the total estimate after compaction.
expect(store[sessionKey]?.inputTokens).toBeUndefined();
expect(store[sessionKey]?.outputTokens).toBeUndefined();
});
});
describe("createFollowupRunner messaging tool dedupe", () => {
@@ -212,7 +274,8 @@ describe("createFollowupRunner messaging tool dedupe", () => {
messagingToolSentTargets: [{ tool: "slack", provider: "slack", to: "channel:C1" }],
meta: {
agentMeta: {
usage: { input: 10, output: 5 },
usage: { input: 1_000, output: 50 },
lastCallUsage: { input: 400, output: 20 },
model: "claude-opus-4-5",
provider: "anthropic",
},
@@ -234,7 +297,11 @@ describe("createFollowupRunner messaging tool dedupe", () => {
expect(onBlockReply).not.toHaveBeenCalled();
const store = loadSessionStore(storePath, { skipCache: true });
expect(store[sessionKey]?.totalTokens ?? 0).toBeGreaterThan(0);
// totalTokens should reflect the last call usage snapshot, not the accumulated input.
expect(store[sessionKey]?.totalTokens).toBe(400);
expect(store[sessionKey]?.model).toBe("claude-opus-4-5");
// Accumulated usage is still stored for usage/cost tracking.
expect(store[sessionKey]?.inputTokens).toBe(1_000);
expect(store[sessionKey]?.outputTokens).toBe(50);
});
});