The Open Framework for Building Agents
fix(eve): make compaction the sole context manager (#175)
Casey Gowrie3f3a86b55294
diff --git a/.changeset/cool-cups-clean.md b/.changeset/cool-cups-clean.mdnew file mode 100644index 000000000..5784ea60c--- /dev/null+++ b/.changeset/cool-cups-clean.md@@ -0,0 +1,5 @@+---+"eve": patch+---++Improve conversation compaction for longer, more reliable sessions.diff --git a/packages/eve/src/harness/compaction.test.ts b/packages/eve/src/harness/compaction.test.tsindex ad48732d1..59392d9ff 100644--- a/packages/eve/src/harness/compaction.test.ts+++ b/packages/eve/src/harness/compaction.test.ts@@ -340,10 +340,73 @@ describe("compactMessages", () => {content: "Summary of the large SQL result",role: "assistant",},+ { content: "Continue.", role: "user" },]);expect(vi.mocked(generateText)).toHaveBeenCalledTimes(1);});+ it("drops tool results and strips assistant tool calls from the kept tail", async () => {+ const { generateText } = await import("ai");++ vi.mocked(generateText).mockResolvedValue({+ text: "summary",+ } as Awaited<ReturnType<typeof generateText>>);++ const messages: ModelMessage[] = [+ { content: "old context to summarize", role: "user" },+ { content: "older reply", role: "assistant" },+ // Recent window (last 4):+ { content: "do the thing", role: "user" },+ {+ content: [+ { text: "Running the tool.", type: "text" },+ { input: { x: 1 }, toolCallId: "call-1", toolName: "run", type: "tool-call" },+ ],+ role: "assistant",+ },+ {+ content: [+ {+ output: { type: "json", value: { ok: true } },+ toolCallId: "call-1",+ toolName: "run",+ type: "tool-result",+ },+ ],+ role: "tool",+ },+ { content: "All done.", role: "assistant" },+ ];++ const model = {} as Parameters<typeof compactMessages>[1];+ const result = await compactMessages(messages, model, {+ recentWindowSize: 4,+ threshold: 100_000,+ });++ // No tool-result message and no tool_use/tool_result part survives anywhere,+ // so the rebuilt history has no dangling tool calls.+ for (const message of result) {+ expect(message.role).not.toBe("tool");+ if (Array.isArray(message.content)) {+ for (const part of message.content) {+ expect(part.type).not.toBe("tool-call");+ expect(part.type).not.toBe("tool-result");+ }+ }+ }++ // User messages are kept; the assistant tool-call turn is reduced to its text.+ expect(result).toEqual([+ { content: "Summary of our conversation so far:", role: "user" },+ { content: "summary", role: "assistant" },+ { content: "do the thing", role: "user" },+ { content: "Running the tool.", role: "assistant" },+ { content: "All done.", role: "assistant" },+ { content: "Continue.", role: "user" },+ ]);+ });+it("forwards headers to the generateText call", async () => {const { generateText } = await import("ai");diff --git a/packages/eve/src/harness/compaction.ts b/packages/eve/src/harness/compaction.tsindex 5a908e18a..076794757 100644--- a/packages/eve/src/harness/compaction.ts+++ b/packages/eve/src/harness/compaction.ts@@ -31,9 +31,8 @@ type ModelMessageContentPart = Exclude<ModelMessage["content"], string>[number];* deciding whether compaction is needed; the real token count comes back* from the model each step via {@link CompactionConfig.lastKnownInputTokens}.*- * Accepts any JSON-serializable value so the reactive pruning system can- * apply the same heuristic to individual tool-result parts — keeping- * every layer of context management on one consistent token ruler.+ * Accepts any JSON-serializable value so callers can apply the same heuristic+ * to whole message arrays or individual content parts on one consistent ruler.*/export function estimateTokens(value: unknown): number {return JSON.stringify(value).length / 4;@@ -118,7 +117,7 @@ export async function compactMessages(while (true) {const { older, recent } = splitMessagesForCompaction(messages, keep);if (older.length === 0) {- return recent;+ return keepNonToolResultMessages(recent);}const prunedOlder: CompactionTranscriptMessage[] = older.map((message) => ({@@ -136,19 +135,26 @@ export async function compactMessages(temperature: 0,});- // The recent window may trail with an assistant message (e.g.- // deferred-input continuations where no user message was appended).- // Providers that don't support assistant prefill reject the request.- // Append a synthetic user message so the model resumes from a user turn.+ // Keep recent context as plain conversation: tool results are dropped (the+ // summary above already captures the older ones) and assistant tool calls+ // are stripped, so no tool_use survives without its result. The summarized+ // older region is the durable record of tool activity.+ const keptTail = keepNonToolResultMessages(recent);++ // The kept tail may be empty or trail with an assistant message; the summary+ // assistant message also precedes it. Providers that don't support assistant+ // prefill reject a request that ends on assistant content, so append a+ // synthetic user message to resume from a user turn.+ const lastKeptRole = keptTail.at(-1)?.role;const trailingAssistantGuard: ModelMessage[] =- recent.length > 0 && recent.at(-1)?.role === "assistant"+ lastKeptRole === undefined || lastKeptRole === "assistant"? [{ role: "user", content: "Continue." }]: [];const compacted: ModelMessage[] = [{ content: "Summary of our conversation so far:", role: "user" },{ content: result.text, role: "assistant" },- ...recent,+ ...keptTail,...trailingAssistantGuard,];@@ -160,6 +166,54 @@ export async function compactMessages(}}+/**+ * Returns the kept tail for a compacted history: recent messages with tool+ * activity removed. Tool-result messages are dropped, and assistant messages are+ * reduced to their text content (tool-call and reasoning parts stripped) so the+ * rebuilt history never carries a tool_use without its matching result.+ * Assistant messages with no remaining text are dropped; user messages are kept+ * verbatim.+ */+function keepNonToolResultMessages(messages: readonly ModelMessage[]): ModelMessage[] {+ const kept: ModelMessage[] = [];++ for (const message of messages) {+ if (message.role === "tool") {+ continue;+ }++ if (message.role === "assistant") {+ const text = assistantMessageText(message);+ if (text.length > 0) {+ kept.push({ content: text, role: "assistant" });+ }+ continue;+ }++ kept.push(message);+ }++ return kept;+}++/**+ * Concatenated text content of an assistant message, ignoring tool-call,+ * reasoning, and other non-text parts.+ */+function assistantMessageText(message: ModelMessage): string {+ if (typeof message.content === "string") {+ return message.content.trim();+ }++ return message.content+ .filter(+ (part): part is Extract<ModelMessageContentPart, { type: "text" }> => part.type === "text",+ )+ .map((part) => part.text)+ .join("")+ .trim();+}+function selectRecentWindowSize(messages: readonly ModelMessage[],config: CompactionConfig,diff --git a/packages/eve/src/harness/tool-loop-compaction-accounting.test.ts b/packages/eve/src/harness/tool-loop-compaction-accounting.test.tsindex 2277a1f4d..0ff388cfb 100644--- a/packages/eve/src/harness/tool-loop-compaction-accounting.test.ts+++ b/packages/eve/src/harness/tool-loop-compaction-accounting.test.ts@@ -1,4 +1,3 @@-import type { ModelMessage } from "ai";import { generateText, jsonSchema, type LanguageModel, ToolLoopAgent } from "ai";import { afterEach, describe, expect, it, vi } from "vitest";@@ -283,88 +282,71 @@ describe("tool-loop structured compaction accounting", () => {});});- it("clears exact token accounting when pruning rewrites prompt history", async () => {- // Build a history with a large old tool result that will trigger pruning.- // The pruning thresholds are: protect 40K tokens, min savings 20K tokens.- // We need ~60K+ tokens of tool results total so at least 20K is reclaimable.- //- // estimateTokens uses JSON.stringify then divides by 4.- // A { value: "x".repeat(200_000) } part serializes to ~200K chars ≈ 50K tokens.- const largeOldToolResult = {- content: [- {- output: { value: "x".repeat(200_000) },- toolCallId: "old-call",- toolName: "read_file",- type: "tool-result" as const,- },- ],- role: "tool" as const,- } as ModelMessage;- const recentToolResult = {- content: [- {- output: { value: "y".repeat(200_000) },- toolCallId: "recent-call",- toolName: "read_file",- type: "tool-result" as const,- },- ],- role: "tool" as const,- } as ModelMessage;+ it("keeps tool results verbatim across steps so history is append-only", async () => {+ // A large tool result that would have been a prime pruning target. With no+ // reactive pruning, it must survive verbatim across the continuation step —+ // nothing rewrites earlier messages mid-turn, keeping the prompt prefix+ // stable for the provider cache.+ const largeOutput = { value: "x".repeat(200_000) };setupMockAgentSequence([{- finishReason: "stop",+ finishReason: "tool-calls",response: {- messages: [{ content: "All done.", role: "assistant" }],+ messages: [+ {+ content: [{ input: {}, toolCallId: "call-1", toolName: "add", type: "tool-call" }],+ role: "assistant",+ },+ {+ content: [+ { output: largeOutput, toolCallId: "call-1", toolName: "add", type: "tool-result" },+ ],+ role: "tool",+ },+ ],},- text: "All done.",+ text: "",+ toolCalls: [{ input: {}, toolCallId: "call-1", toolName: "add", type: "tool-call" }],+ toolResults: [+ { output: largeOutput, toolCallId: "call-1", toolName: "add", type: "tool-result" },+ ],+ usage: { inputTokens: 100 },+ },+ {+ finishReason: "stop",+ response: { messages: [{ content: "Done.", role: "assistant" }] },+ text: "Done.",toolCalls: [],toolResults: [],- usage: { inputTokens: 50_000 },},]);const runStep = createToolLoopHarness(createTestConfig());- const result = await runStep(- createTestSession({- compaction: {- lastKnownInputTokens: 50_000,- lastKnownPromptMessageCount: 4,- recentWindowSize: 10,- threshold: 100_000,- },- history: [- { content: "Read some files", role: "user" },- largeOldToolResult,- { content: "Got the old file", role: "assistant" },- recentToolResult,- ],- }),- { message: "Summarize" },+ // Threshold far above the history size so compaction never fires; the only+ // thing that could shrink the large result is pruning, which is gone.+ const first = await runStep(+ createTestSession({ compaction: { recentWindowSize: 10, threshold: 100_000_000 } }),+ { message: "Read a big file" },);+ expect(first.next).toBe(runStep);- // Pruning should have replaced the old tool result and cleared the- // exact token snapshot so the next step falls back to estimation.- expect(result.session.compaction.lastKnownInputTokens).toBeUndefined();- expect(result.session.compaction.lastKnownPromptMessageCount).toBeUndefined();+ const second = await expectStepFn(first.next)(first.session);+ expect(second.next).toBeNull();- // The old tool result should be pruned.- const oldTool = result.session.history.find(+ const toolResult = second.session.history.find((m) =>m.role === "tool" &&Array.isArray(m.content) &&- (m.content[0] as { toolCallId?: string }).toolCallId === "old-call",+ (m.content[0] as { toolCallId?: string }).toolCallId === "call-1",);- expect(oldTool).toBeDefined();+ expect(toolResult).toBeDefined();expect(- (Array.isArray(oldTool?.content) ? (oldTool.content[0] as { output?: unknown }) : undefined)- ?.output,- ).toEqual({- type: "text",- value: "[Tool result pruned to save context. Call the tool again if needed.]",- });+ (Array.isArray(toolResult?.content)+ ? (toolResult.content[0] as { output?: unknown })+ : undefined+ )?.output,+ ).toEqual(largeOutput);});});diff --git a/packages/eve/src/harness/tool-loop.ts b/packages/eve/src/harness/tool-loop.tsindex 3731e22e4..3af22b676 100644--- a/packages/eve/src/harness/tool-loop.ts+++ b/packages/eve/src/harness/tool-loop.ts@@ -141,7 +141,6 @@ import {type HarnessStepResult,isInvalidToolCall,} from "#harness/step-hooks.js";-import { pruneToolResults } from "#harness/tool-result-pruning.js";import { buildToolSetFromDefinitions, buildToolSetWithProviderTools } from "#harness/tools.js";import {CODE_MODE_TOOL_NAME,@@ -1557,23 +1556,12 @@ async function handleStepResult(input: {// --- Continue or terminate ------------------------------------------------- const prunedHistory = pruneToolResults(promptMessages);- const historyWasPruned = prunedHistory !== promptMessages;-- // When pruning rewrites messages that the model already counted, the- // exact input-token snapshot recorded by createNextCompactionConfig- // becomes stale. Clear it so the next step falls back to the- // estimation heuristic instead of overestimating and compacting early.- let compaction = baseSession.compaction;- if (historyWasPruned && compaction.lastKnownInputTokens !== undefined) {- compaction = {- recentWindowSize: compaction.recentWindowSize,- threshold: compaction.threshold,- };- }-- const updatedHistory: ModelMessage[] = [...prunedHistory, ...responseMessages];- let nextSession: HarnessSession = { ...baseSession, compaction, history: updatedHistory };+ // History grows by append only; nothing rewrites earlier messages mid-turn,+ // so the prompt prefix stays stable and the provider's prompt cache keeps+ // hitting across steps. Compaction is the sole mechanism that ever rewrites+ // history, and it runs before the model call (see `maybeCompact`).+ const updatedHistory: ModelMessage[] = [...promptMessages, ...responseMessages];+ let nextSession: HarnessSession = { ...baseSession, history: updatedHistory };// A `final_output` call is terminal even when the model emits it alongside// executing tools: continuing the loop would leave the no-execute call as a@@ -1601,7 +1589,7 @@ async function handleStepResult(input: {return finishTaskTurn({emissionState,emit,- prunedHistory,+ history: promptMessages,result,schema: nextSession.outputSchema,session: nextSession,@@ -1612,7 +1600,7 @@ async function handleStepResult(input: {return finishConversationTurn({emissionState,emit,- prunedHistory,+ history: promptMessages,result,schema: nextSession.outputSchema,session: nextSession,@@ -1640,12 +1628,12 @@ function extractFinalOutput(result: HarnessStepResult): JsonValue | undefined {*/function persistStructuredAssistantTurn(session: HarnessSession,- prunedHistory: readonly ModelMessage[],+ history: readonly ModelMessage[],structured: JsonValue,): HarnessSession {return {...session,- history: [...prunedHistory, { content: JSON.stringify(structured), role: "assistant" }],+ history: [...history, { content: JSON.stringify(structured), role: "assistant" }],outputSchema: undefined,};}@@ -1676,13 +1664,13 @@ async function emitStructuredResult(async function finishTaskTurn(input: {readonly emissionState: ReturnType<typeof getHarnessEmissionState>;readonly emit?: ToolLoopHarnessConfig["handleEvent"];- readonly prunedHistory: readonly ModelMessage[];+ readonly history: readonly ModelMessage[];readonly result: HarnessStepResult;readonly schema: JsonObject | undefined;readonly session: HarnessSession;readonly stepOutput: string | null;}): Promise<StepResult> {- const { emit, prunedHistory, result, schema, stepOutput } = input;+ const { emit, history, result, schema, stepOutput } = input;let { emissionState, session } = input;if (schema === undefined) {@@ -1707,7 +1695,7 @@ async function finishTaskTurn(input: {};}- session = persistStructuredAssistantTurn(session, prunedHistory, structured);+ session = persistStructuredAssistantTurn(session, history, structured);if (emit) {emissionState = await emitStructuredResult(emit, emissionState, structured, "task");session = setHarnessEmissionState(session, emissionState);@@ -1723,12 +1711,12 @@ async function finishTaskTurn(input: {async function finishConversationTurn(input: {readonly emissionState: ReturnType<typeof getHarnessEmissionState>;readonly emit?: ToolLoopHarnessConfig["handleEvent"];- readonly prunedHistory: readonly ModelMessage[];+ readonly history: readonly ModelMessage[];readonly result: HarnessStepResult;readonly schema: JsonObject | undefined;readonly session: HarnessSession;}): Promise<StepResult> {- const { emit, prunedHistory, result, schema } = input;+ const { emit, history, result, schema } = input;let { emissionState, session } = input;if (schema === undefined) {@@ -1752,7 +1740,7 @@ async function finishConversationTurn(input: {return { next: null, session };}- session = persistStructuredAssistantTurn(session, prunedHistory, structured);+ session = persistStructuredAssistantTurn(session, history, structured);if (emit) {emissionState = await emitStructuredResult(emit, emissionState, structured, "conversation");session = setHarnessEmissionState(session, emissionState);diff --git a/packages/eve/src/harness/tool-result-pruning.test.ts b/packages/eve/src/harness/tool-result-pruning.test.tsdeleted file mode 100644index 2cf7fcd0b..000000000--- a/packages/eve/src/harness/tool-result-pruning.test.ts+++ /dev/null@@ -1,311 +0,0 @@-import { type ModelMessage, modelMessageSchema } from "ai";-import { describe, expect, it } from "vitest";--import { pruneToolResults } from "#harness/tool-result-pruning.js";--const PRUNED_PLACEHOLDER_OUTPUT = {- type: "text",- value: "[Tool result pruned to save context. Call the tool again if needed.]",-} as const;--/**- * Creates a tool-result message with a text payload of `charCount` characters.- * At 4 chars per token + structured overhead, this gives predictable token- * estimates for testing threshold behavior.- */-function makeToolResultMessage(- toolName: string,- charCount: number,- callId = "call-1",-): ModelMessage {- return {- content: [- {- output: { type: "text", value: "x".repeat(charCount) },- toolCallId: callId,- toolName,- type: "tool-result" as const,- },- ],- role: "tool" as const,- } as ModelMessage;-}--function makeAssistantMessage(text: string): ModelMessage {- return { content: text, role: "assistant" };-}--function makeUserMessage(text: string): ModelMessage {- return { content: text, role: "user" };-}--describe("pruneToolResults", () => {- it("returns messages unchanged when there are no tool results", () => {- const messages: ModelMessage[] = [makeUserMessage("Hello"), makeAssistantMessage("Hi there")];-- const result = pruneToolResults(messages);- expect(result).toEqual(messages);- });-- it("returns messages unchanged when tool results are small", () => {- const messages: ModelMessage[] = [- makeUserMessage("Read file A"),- makeAssistantMessage("I'll read that."),- makeToolResultMessage("read_file", 100),- ];-- const result = pruneToolResults(messages);- expect(result).toEqual(messages);- });-- it("returns messages unchanged when total tool result tokens are below the minimum savings threshold", () => {- // ~30K chars = ~15K tokens (below the 20K min savings threshold)- const messages: ModelMessage[] = [- makeUserMessage("Read files"),- makeToolResultMessage("read_file", 30_000, "call-old"),- makeAssistantMessage("Got it."),- makeToolResultMessage("read_file", 30_000, "call-new"),- ];-- const result = pruneToolResults(messages);- // Even though the first result is outside the protection window,- // total reclaimable tokens are below the minimum savings threshold.- expect(result).toEqual(messages);- });-- it("prunes old tool results beyond the protection window", () => {- // Build a history with many large tool results.- // Each result is ~200K chars = ~100K structured tokens.- // Protection window is 40K tokens, min savings is 20K.- const messages: ModelMessage[] = [- makeUserMessage("Read many files"),- // Old results (should be pruned)- makeToolResultMessage("read_file", 200_000, "call-1"),- makeAssistantMessage("Got file 1"),- makeToolResultMessage("read_file", 200_000, "call-2"),- makeAssistantMessage("Got file 2"),- // Recent result (should be protected)- makeToolResultMessage("read_file", 200_000, "call-3"),- ];-- const result = pruneToolResults(messages);-- // The most recent tool result should be preserved- const lastToolMsg = result.find(- (m) =>- m.role === "tool" &&- Array.isArray(m.content) &&- (m.content[0] as { toolCallId?: string }).toolCallId === "call-3",- );- expect(lastToolMsg).toBeDefined();- const lastPart = Array.isArray(lastToolMsg?.content)- ? (lastToolMsg.content[0] as { output?: unknown })- : undefined;- expect(lastPart?.output).toMatchObject({ value: expect.any(String) });- expect((lastPart!.output as { value: string }).value.length).toBe(200_000);-- // Older tool results should be pruned- const oldToolMsg = result.find(- (m) =>- m.role === "tool" &&- Array.isArray(m.content) &&- (m.content[0] as { toolCallId?: string }).toolCallId === "call-1",- );- expect(oldToolMsg).toBeDefined();- const oldPart = Array.isArray(oldToolMsg?.content)- ? (oldToolMsg.content[0] as { output?: unknown })- : undefined;- expect(oldPart?.output).toEqual(PRUNED_PLACEHOLDER_OUTPUT);- });-- it("does not overcount tokens when a tool message has multiple parallel results", () => {- const multiPartToolMessage: ModelMessage = {- content: [- {- output: { type: "text", value: "a".repeat(50_000) },- toolCallId: "call-a",- toolName: "read_file",- type: "tool-result" as const,- },- {- output: { type: "text", value: "b".repeat(50_000) },- toolCallId: "call-b",- toolName: "read_file",- type: "tool-result" as const,- },- {- output: { type: "text", value: "c".repeat(50_000) },- toolCallId: "call-c",- toolName: "read_file",- type: "tool-result" as const,- },- ],- role: "tool" as const,- } as ModelMessage;-- const messages: ModelMessage[] = [- makeUserMessage("Read files"),- makeToolResultMessage("read_file", 400_000, "call-old"),- makeAssistantMessage("Got old file"),- multiPartToolMessage,- makeAssistantMessage("Got three files"),- makeToolResultMessage("read_file", 40_000, "call-recent"),- ];-- const result = pruneToolResults(messages);-- const multiPartMsg = result.find(- (m) => m.role === "tool" && Array.isArray(m.content) && m.content.length === 3,- );- expect(multiPartMsg).toBeDefined();- const prunedPartsCount = (- Array.isArray(multiPartMsg?.content) ? multiPartMsg.content : []- ).filter(- (part) =>- JSON.stringify((part as { output?: unknown }).output) ===- JSON.stringify(PRUNED_PLACEHOLDER_OUTPUT),- ).length;-- // Either all multi-part entries are treated the same (all protected or- // all pruned). The bug would cause only the newest part to be protected.- expect(prunedPartsCount).not.toBe(2);-- const parts = Array.isArray(multiPartMsg?.content) ? multiPartMsg.content : [];- for (const part of parts) {- const typedPart = part as { output?: unknown };- expect(typedPart.output).toMatchObject({ value: expect.any(String) });- expect(typedPart.output).not.toEqual(PRUNED_PLACEHOLDER_OUTPUT);- }-- // The old result should be pruned.- const oldMsg = result.find(- (m) =>- m.role === "tool" &&- Array.isArray(m.content) &&- (m.content[0] as { toolCallId?: string }).toolCallId === "call-old",- );- expect(oldMsg).toBeDefined();- const oldPart = Array.isArray(oldMsg?.content)- ? (oldMsg.content[0] as { output?: unknown })- : undefined;- expect(oldPart?.output).toEqual(PRUNED_PLACEHOLDER_OUTPUT);- });-- it("accounts for mixed-size parallel results individually", () => {- const mixedMultiPartMessage: ModelMessage = {- content: [- {- output: { type: "text", value: "huge".repeat(50_000) },- toolCallId: "call-big",- toolName: "read_file",- type: "tool-result" as const,- },- {- output: { type: "text", value: "tiny" },- toolCallId: "call-small-1",- toolName: "grep",- type: "tool-result" as const,- },- {- output: { type: "text", value: "tiny" },- toolCallId: "call-small-2",- toolName: "grep",- type: "tool-result" as const,- },- ],- role: "tool" as const,- } as ModelMessage;-- const messages: ModelMessage[] = [- makeUserMessage("Explore codebase"),- makeToolResultMessage("read_file", 200_000, "call-old"),- makeAssistantMessage("Found something"),- mixedMultiPartMessage,- makeAssistantMessage("Processed"),- makeToolResultMessage("read_file", 10_000, "call-recent"),- ];-- const result = pruneToolResults(messages);-- const multiPartMsg = result.find(- (m) => m.role === "tool" && Array.isArray(m.content) && m.content.length === 3,- );- expect(multiPartMsg).toBeDefined();-- const parts = Array.isArray(multiPartMsg?.content) ? multiPartMsg.content : [];- for (const part of parts) {- const typed = part as { output?: unknown };- expect(typed.output).not.toEqual(PRUNED_PLACEHOLDER_OUTPUT);- }-- // The old result should be pruned- const oldMsg = result.find(- (m) =>- m.role === "tool" &&- Array.isArray(m.content) &&- (m.content[0] as { toolCallId?: string }).toolCallId === "call-old",- );- const oldPart = Array.isArray(oldMsg?.content)- ? (oldMsg.content[0] as { output?: unknown })- : undefined;- expect(oldPart?.output).toEqual(PRUNED_PLACEHOLDER_OUTPUT);- });-- it("uses the default placeholder for all pruned tool results", () => {- const messages: ModelMessage[] = [- makeUserMessage("Read many files"),- makeToolResultMessage("read_file", 200_000, "call-1"),- makeAssistantMessage("Got file 1"),- makeToolResultMessage("read_file", 200_000, "call-2"),- makeAssistantMessage("Got file 2"),- makeToolResultMessage("read_file", 200_000, "call-3"),- ];-- const result = pruneToolResults(messages);-- const oldMsg = result.find(- (m) =>- m.role === "tool" &&- Array.isArray(m.content) &&- (m.content[0] as { toolCallId?: string }).toolCallId === "call-1",- );- const oldPart = Array.isArray(oldMsg?.content)- ? (oldMsg.content[0] as { output?: unknown })- : undefined;- expect(oldPart?.output).toEqual(PRUNED_PLACEHOLDER_OUTPUT);- });-- it("does not modify non-tool messages", () => {- const messages: ModelMessage[] = [- makeUserMessage("Read many files"),- makeToolResultMessage("read_file", 200_000, "call-1"),- makeAssistantMessage("Got file 1"),- makeToolResultMessage("read_file", 200_000, "call-2"),- makeAssistantMessage("Got file 2"),- makeToolResultMessage("read_file", 200_000, "call-3"),- ];-- const result = pruneToolResults(messages);- const userMessages = result.filter((m) => m.role === "user");- const assistantMessages = result.filter((m) => m.role === "assistant");-- expect(userMessages).toHaveLength(1);- expect(assistantMessages).toHaveLength(2);- expect(userMessages[0]).toEqual(makeUserMessage("Read many files"));- });-- it("keeps pruned history compatible with the AI SDK ModelMessage schema", () => {- const messages: ModelMessage[] = [- makeUserMessage("Read many files"),- makeToolResultMessage("read_file", 200_000, "call-1"),- makeAssistantMessage("Got file 1"),- makeToolResultMessage("read_file", 200_000, "call-2"),- makeAssistantMessage("Got file 2"),- makeToolResultMessage("read_file", 200_000, "call-3"),- ];-- const result = pruneToolResults(messages);-- expect(modelMessageSchema.array().safeParse(result).success).toBe(true);- });-});diff --git a/packages/eve/src/harness/tool-result-pruning.ts b/packages/eve/src/harness/tool-result-pruning.tsdeleted file mode 100644index bee8a568b..000000000--- a/packages/eve/src/harness/tool-result-pruning.ts+++ /dev/null@@ -1,146 +0,0 @@-import type { ModelMessage } from "ai";--import { estimateTokens } from "#harness/compaction.js";--/**- * Placeholder text injected in place of pruned tool-result content.- */-const PRUNED_PLACEHOLDER = "[Tool result pruned to save context. Call the tool again if needed.]";--/**- * Token budget reserved for recent tool results that must not be pruned.- *- * Tool results whose cumulative token estimate (walking backwards from- * the end of the history) fits within this window are protected from- * pruning. Everything older is eligible for replacement.- */-const PRUNE_PROTECT_TOKENS = 40_000;--/**- * Minimum token savings before pruning is applied.- *- * If the total tokens reclaimed by pruning would be below this threshold,- * the messages are returned unmodified. This avoids churn from replacing- * tiny tool results.- */-const PRUNE_MIN_SAVINGS = 20_000;--/**- * Content part type for a tool result within a `ModelMessage`.- */-interface ToolResultContentPart {- readonly type: "tool-result";- readonly toolName: string;- readonly output?: unknown;- readonly [key: string]: unknown;-}--type ToolResponsePart = Extract<ModelMessage, { role: "tool" }>["content"][number];-type ToolResultOutput = Extract<ToolResponsePart, { type: "tool-result" }>["output"];--/**- * Reactively prunes old tool-result content from conversation history.- *- * Recent results inside {@link PRUNE_PROTECT_TOKENS} are preserved.- * Older results are replaced with {@link PRUNED_PLACEHOLDER}.- */-export function pruneToolResults(messages: readonly ModelMessage[]): ModelMessage[] {- const locations = collectToolResultLocations(messages);- if (locations.length === 0) {- return messages as ModelMessage[];- }-- let protectedTokens = 0;- let reclaimableTokens = 0;- const toPrune: Set<string> = new Set();-- for (const loc of locations) {- if (protectedTokens < PRUNE_PROTECT_TOKENS) {- protectedTokens += loc.tokens;- } else {- toPrune.add(loc.key);- reclaimableTokens += loc.tokens;- }- }-- if (reclaimableTokens < PRUNE_MIN_SAVINGS) {- return messages as ModelMessage[];- }-- return messages.map((message, messageIndex) => {- if (message.role !== "tool" || !Array.isArray(message.content)) {- return message;- }-- let mutated = false;- const content = message.content.map((part, partIndex) => {- const key = `${messageIndex}:${partIndex}`;- if (!toPrune.has(key)) {- return part;- }-- mutated = true;- return {- ...part,- output: createPrunedToolResultOutput(PRUNED_PLACEHOLDER),- };- });-- if (!mutated) {- return message;- }-- return { ...message, content: content as typeof message.content } as ModelMessage;- });-}--/**- * Location descriptor for one tool-result content part within the- * message array.- */-interface ToolResultLocation {- /** Unique key: `"messageIndex:partIndex"` */- readonly key: string;- /** Estimated token cost of this tool result. */- readonly tokens: number;-}--/**- * Collects all tool-result locations from messages, ordered newest-first.- *- * Each tool-result part is estimated individually via {@link estimateTokens}- * so mixed-size parallel results (e.g. one large read_file and two tiny- * results in one message) are budgeted by individual cost instead of an- * averaged per-message total.- */-function collectToolResultLocations(messages: readonly ModelMessage[]): ToolResultLocation[] {- const locations: ToolResultLocation[] = [];-- for (let i = messages.length - 1; i >= 0; i--) {- const message = messages[i];- if (message === undefined || message.role !== "tool" || !Array.isArray(message.content)) {- continue;- }-- for (let j = message.content.length - 1; j >= 0; j--) {- const part = message.content[j] as ToolResultContentPart | undefined;- if (part?.type !== "tool-result") {- continue;- }-- locations.push({- key: `${i}:${j}`,- tokens: estimateTokens(part),- });- }- }-- return locations;-}--function createPrunedToolResultOutput(text: string): ToolResultOutput {- return {- type: "text",- value: text,- };-}