|
| 1 | +import type { AgentMessage } from "@earendil-works/pi-agent-core"; |
| 2 | +import { describe, expect, it } from "vitest"; |
| 3 | +import { CLEARED_TOOL_RESULT, microcompact } from "./microcompact.js"; |
| 4 | + |
| 5 | +function toolResult(toolName: string, text: string, opts: { isError?: boolean } = {}): AgentMessage { |
| 6 | + return { |
| 7 | + role: "toolResult", |
| 8 | + toolCallId: `tc-${Math.random().toString(36).slice(2)}`, |
| 9 | + toolName, |
| 10 | + content: [{ type: "text", text }], |
| 11 | + isError: opts.isError ?? false, |
| 12 | + timestamp: 0, |
| 13 | + } as AgentMessage; |
| 14 | +} |
| 15 | + |
| 16 | +function user(text: string): AgentMessage { |
| 17 | + return { role: "user", content: text, timestamp: 0 } as AgentMessage; |
| 18 | +} |
| 19 | + |
| 20 | +function clearedText(m: AgentMessage): string | undefined { |
| 21 | + const content = (m as { content: Array<{ type: string; text?: string }> }).content; |
| 22 | + return content?.[0]?.text; |
| 23 | +} |
| 24 | + |
| 25 | +describe("microcompact", () => { |
| 26 | + it("clears stale compactable tool results, keeping the newest N", () => { |
| 27 | + const big = "x".repeat(5000); |
| 28 | + const messages: AgentMessage[] = []; |
| 29 | + for (let i = 0; i < 10; i++) { |
| 30 | + messages.push(user(`turn ${i}`)); |
| 31 | + messages.push(toolResult("read_file", `${big} #${i}`)); |
| 32 | + } |
| 33 | + const out = microcompact(messages, 3); |
| 34 | + // 10 read_file results, keep 3 → clear 7. |
| 35 | + expect(out.clearedCount).toBe(7); |
| 36 | + expect(out.tokensSaved).toBeGreaterThan(0); |
| 37 | + |
| 38 | + // The last 3 tool results retain their content; earlier ones are cleared. |
| 39 | + const toolResults = out.messages.filter((m) => m.role === "toolResult"); |
| 40 | + const clearedFlags = toolResults.map((m) => clearedText(m) === CLEARED_TOOL_RESULT); |
| 41 | + expect(clearedFlags.slice(0, 7)).toEqual([true, true, true, true, true, true, true]); |
| 42 | + expect(clearedFlags.slice(7)).toEqual([false, false, false]); |
| 43 | + }); |
| 44 | + |
| 45 | + it("preserves message structure (same count, same order, tool results stay tool results)", () => { |
| 46 | + const messages: AgentMessage[] = [ |
| 47 | + user("a"), |
| 48 | + toolResult("read_file", "x".repeat(2000)), |
| 49 | + toolResult("grep", "y".repeat(2000)), |
| 50 | + toolResult("read_file", "z".repeat(2000)), |
| 51 | + ]; |
| 52 | + const out = microcompact(messages, 1); |
| 53 | + expect(out.messages).toHaveLength(messages.length); |
| 54 | + expect(out.messages.map((m) => m.role)).toEqual(["user", "toolResult", "toolResult", "toolResult"]); |
| 55 | + }); |
| 56 | + |
| 57 | + it("never clears errored tool results", () => { |
| 58 | + const messages: AgentMessage[] = [ |
| 59 | + toolResult("shell", "boom", { isError: true }), |
| 60 | + toolResult("read_file", "x".repeat(2000)), |
| 61 | + toolResult("read_file", "y".repeat(2000)), |
| 62 | + ]; |
| 63 | + const out = microcompact(messages, 0); |
| 64 | + // keepRecent 0, but the error is exempt → only the two reads clear. |
| 65 | + expect(out.clearedCount).toBe(2); |
| 66 | + const err = out.messages[0]; |
| 67 | + expect(clearedText(err)).toBe("boom"); |
| 68 | + }); |
| 69 | + |
| 70 | + it("ignores non-compactable tools (ask_user, tasks, config, git)", () => { |
| 71 | + const messages: AgentMessage[] = [ |
| 72 | + toolResult("git_status", "branch info"), |
| 73 | + toolResult("create_task", "task body"), |
| 74 | + toolResult("config", "{}"), |
| 75 | + ]; |
| 76 | + const out = microcompact(messages, 0); |
| 77 | + expect(out.clearedCount).toBe(0); |
| 78 | + expect(out.tokensSaved).toBe(0); |
| 79 | + }); |
| 80 | + |
| 81 | + it("is idempotent — a second pass clears nothing new", () => { |
| 82 | + const messages: AgentMessage[] = [ |
| 83 | + toolResult("read_file", "x".repeat(3000)), |
| 84 | + toolResult("read_file", "y".repeat(3000)), |
| 85 | + toolResult("read_file", "z".repeat(3000)), |
| 86 | + ]; |
| 87 | + const first = microcompact(messages, 1); |
| 88 | + expect(first.clearedCount).toBe(2); |
| 89 | + const second = microcompact(first.messages, 1); |
| 90 | + expect(second.clearedCount).toBe(0); |
| 91 | + }); |
| 92 | + |
| 93 | + it("returns the same array unchanged when nothing to clear", () => { |
| 94 | + const messages: AgentMessage[] = [user("hi"), toolResult("read_file", "small")]; |
| 95 | + const out = microcompact(messages, 6); |
| 96 | + expect(out.clearedCount).toBe(0); |
| 97 | + expect(out.messages).toBe(messages); |
| 98 | + }); |
| 99 | +}); |
0 commit comments