mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-11 11:21:41 +00:00
fix(agents): harden compaction and reset safety
Co-authored-by: jaden-clovervnd <91520439+jaden-clovervnd@users.noreply.github.com> Co-authored-by: Sid <201593046+Sid-Qin@users.noreply.github.com> Co-authored-by: Marcus Widing <245375637+widingmarcus-cyber@users.noreply.github.com>
This commit is contained in:
@@ -122,4 +122,72 @@ describe("compaction hook wiring", () => {
|
||||
|
||||
expect(hookMocks.runner.runAfterCompaction).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("clears stale assistant usage after final compaction", () => {
|
||||
const messages = [
|
||||
{ role: "user", content: "hello" },
|
||||
{
|
||||
role: "assistant",
|
||||
content: "response one",
|
||||
usage: { totalTokens: 180_000, input: 100, output: 50 },
|
||||
},
|
||||
{
|
||||
role: "assistant",
|
||||
content: "response two",
|
||||
usage: { totalTokens: 181_000, input: 120, output: 60 },
|
||||
},
|
||||
];
|
||||
|
||||
const ctx = {
|
||||
params: { runId: "r4", session: { messages } },
|
||||
state: { compactionInFlight: true },
|
||||
log: { debug: vi.fn(), warn: vi.fn() },
|
||||
maybeResolveCompactionWait: vi.fn(),
|
||||
getCompactionCount: () => 1,
|
||||
incrementCompactionCount: vi.fn(),
|
||||
};
|
||||
|
||||
handleAutoCompactionEnd(
|
||||
ctx as never,
|
||||
{
|
||||
type: "auto_compaction_end",
|
||||
willRetry: false,
|
||||
} as never,
|
||||
);
|
||||
|
||||
const assistantOne = messages[1] as { usage?: unknown };
|
||||
const assistantTwo = messages[2] as { usage?: unknown };
|
||||
expect(assistantOne.usage).toBeUndefined();
|
||||
expect(assistantTwo.usage).toBeUndefined();
|
||||
});
|
||||
|
||||
it("does not clear assistant usage while compaction is retrying", () => {
|
||||
const messages = [
|
||||
{
|
||||
role: "assistant",
|
||||
content: "response",
|
||||
usage: { totalTokens: 184_297, input: 130_000, output: 2_000 },
|
||||
},
|
||||
];
|
||||
|
||||
const ctx = {
|
||||
params: { runId: "r5", session: { messages } },
|
||||
state: { compactionInFlight: true },
|
||||
log: { debug: vi.fn(), warn: vi.fn() },
|
||||
noteCompactionRetry: vi.fn(),
|
||||
resetForCompactionRetry: vi.fn(),
|
||||
getCompactionCount: () => 0,
|
||||
};
|
||||
|
||||
handleAutoCompactionEnd(
|
||||
ctx as never,
|
||||
{
|
||||
type: "auto_compaction_end",
|
||||
willRetry: true,
|
||||
} as never,
|
||||
);
|
||||
|
||||
const assistant = messages[0] as { usage?: unknown };
|
||||
expect(assistant.usage).toEqual({ totalTokens: 184_297, input: 130_000, output: 2_000 });
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user