mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 10:11:24 +00:00
Compaction: sanitize token split accounting (#24058)
* Compaction: sanitize token split accounting * Tests/Compaction: type sanitize token estimate callback
This commit is contained in:
52
src/agents/compaction.token-sanitize.test.ts
Normal file
52
src/agents/compaction.token-sanitize.test.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
import type { AgentMessage } from "@mariozechner/pi-agent-core";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
|
||||
const piCodingAgentMocks = vi.hoisted(() => ({
|
||||
estimateTokens: vi.fn((_message: unknown) => 1),
|
||||
generateSummary: vi.fn(async () => "summary"),
|
||||
}));
|
||||
|
||||
vi.mock("@mariozechner/pi-coding-agent", async () => {
|
||||
const actual = await vi.importActual<typeof import("@mariozechner/pi-coding-agent")>(
|
||||
"@mariozechner/pi-coding-agent",
|
||||
);
|
||||
return {
|
||||
...actual,
|
||||
estimateTokens: piCodingAgentMocks.estimateTokens,
|
||||
generateSummary: piCodingAgentMocks.generateSummary,
|
||||
};
|
||||
});
|
||||
|
||||
import { chunkMessagesByMaxTokens, splitMessagesByTokenShare } from "./compaction.js";
|
||||
|
||||
describe("compaction token accounting sanitization", () => {
|
||||
it("does not pass toolResult.details into per-message token estimates", () => {
|
||||
const messages: AgentMessage[] = [
|
||||
{
|
||||
role: "toolResult",
|
||||
toolCallId: "call_1",
|
||||
toolName: "browser",
|
||||
isError: false,
|
||||
content: [{ type: "text", text: "ok" }],
|
||||
details: { raw: "x".repeat(50_000) },
|
||||
timestamp: 1,
|
||||
// oxlint-disable-next-line typescript/no-explicit-any
|
||||
} as any,
|
||||
{
|
||||
role: "user",
|
||||
content: "next",
|
||||
timestamp: 2,
|
||||
},
|
||||
];
|
||||
|
||||
splitMessagesByTokenShare(messages, 2);
|
||||
chunkMessagesByMaxTokens(messages, 16);
|
||||
|
||||
const calledWithDetails = piCodingAgentMocks.estimateTokens.mock.calls.some((call) => {
|
||||
const message = call[0] as { details?: unknown } | undefined;
|
||||
return Boolean(message?.details);
|
||||
});
|
||||
|
||||
expect(calledWithDetails).toBe(false);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user