fix(coding-agent): truncate tool results in compaction summarization to prevent overflow, fixes #1796
This commit is contained in:
79
packages/coding-agent/test/compaction-serialization.test.ts
Normal file
79
packages/coding-agent/test/compaction-serialization.test.ts
Normal file
@@ -0,0 +1,79 @@
|
||||
import type { Message } from "@mariozechner/pi-ai";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { serializeConversation } from "../src/core/compaction/utils.js";
|
||||
|
||||
describe("serializeConversation", () => {
|
||||
it("should truncate long tool results", () => {
|
||||
const longContent = "x".repeat(5000);
|
||||
const messages: Message[] = [
|
||||
{
|
||||
role: "toolResult",
|
||||
toolCallId: "tc1",
|
||||
toolName: "read",
|
||||
content: [{ type: "text", text: longContent }],
|
||||
isError: false,
|
||||
timestamp: Date.now(),
|
||||
},
|
||||
];
|
||||
|
||||
const result = serializeConversation(messages);
|
||||
|
||||
expect(result).toContain("[Tool result]:");
|
||||
expect(result).toContain("[... 3000 more characters truncated]");
|
||||
expect(result).not.toContain("x".repeat(3000));
|
||||
// First 2000 chars should be present
|
||||
expect(result).toContain("x".repeat(2000));
|
||||
});
|
||||
|
||||
it("should not truncate short tool results", () => {
|
||||
const shortContent = "x".repeat(1500);
|
||||
const messages: Message[] = [
|
||||
{
|
||||
role: "toolResult",
|
||||
toolCallId: "tc1",
|
||||
toolName: "read",
|
||||
content: [{ type: "text", text: shortContent }],
|
||||
isError: false,
|
||||
timestamp: Date.now(),
|
||||
},
|
||||
];
|
||||
|
||||
const result = serializeConversation(messages);
|
||||
|
||||
expect(result).toBe(`[Tool result]: ${shortContent}`);
|
||||
expect(result).not.toContain("truncated");
|
||||
});
|
||||
|
||||
it("should not truncate assistant or user messages", () => {
|
||||
const longText = "y".repeat(5000);
|
||||
const messages: Message[] = [
|
||||
{
|
||||
role: "user",
|
||||
content: [{ type: "text", text: longText }],
|
||||
timestamp: Date.now(),
|
||||
},
|
||||
{
|
||||
role: "assistant",
|
||||
content: [{ type: "text", text: longText }],
|
||||
api: "anthropic",
|
||||
provider: "anthropic",
|
||||
model: "test",
|
||||
usage: {
|
||||
input: 0,
|
||||
output: 0,
|
||||
cacheRead: 0,
|
||||
cacheWrite: 0,
|
||||
totalTokens: 0,
|
||||
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
|
||||
},
|
||||
stopReason: "stop",
|
||||
timestamp: Date.now(),
|
||||
},
|
||||
];
|
||||
|
||||
const result = serializeConversation(messages);
|
||||
|
||||
expect(result).not.toContain("truncated");
|
||||
expect(result).toContain(longText);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user