Skip to content

armgit / eve

Public Imported from GitHub
0

The Open Framework for Building Agents

fix(eve): stop compaction from re-capping already-capped tool results (#4771)

roboeve[bot]b3825949d5d1
diff --git a/.changeset/stable-compaction-caps.md b/.changeset/stable-compaction-caps.md
new file mode 100644
index 000000000..8a8754598
--- /dev/null
+++ b/.changeset/stable-compaction-caps.md
@@ -0,0 +1,5 @@
+---
+"eve": patch
+---
+
+Compaction no longer re-caps a tool result it already capped. A capped result now fits the size limit it was measured against, so later compactions leave it as it is instead of nesting another truncation notice and keeping less of the original content. A summary written after a cap also sees the whole content the capped result kept.
diff --git a/packages/eve/src/harness/compaction/engine.test.ts b/packages/eve/src/harness/compaction/engine.test.ts
index 73e210d83..501f1bd8d 100644
--- a/packages/eve/src/harness/compaction/engine.test.ts
+++ b/packages/eve/src/harness/compaction/engine.test.ts
@@ -399,6 +399,27 @@ function toolExchange(input: {
];
}
+function toolResultExchange(
+ output: { type: "json"; value: { content: string } } | { type: "text"; value: string },
+): [ModelMessage, ModelMessage] {
+ return [
+ {
+ content: [{ input: {}, toolCallId: "call-0", toolName: "grep", type: "tool-call" }],
+ role: "assistant",
+ },
+ {
+ content: [{ output, toolCallId: "call-0", toolName: "grep", type: "tool-result" }],
+ role: "tool",
+ },
+ ];
+}
+
+function toolResultValue(message: ModelMessage | undefined): string {
+ const part = Array.isArray(message?.content) ? message.content[0] : undefined;
+ const output = part?.type === "tool-result" ? part.output : undefined;
+ return output?.type === "text" ? output.value : "";
+}
+
function checkpointHead(text: string): ModelMessage[] {
return [compactionMarker(CHECKPOINT_MARKER), assistant(text)];
}
@@ -572,7 +593,7 @@ describe("compactMessages: tool-result cap heuristic", () => {
expect(shouldCompact(result, { recentWindowSize: 1, threshold: ROOMY })).toBe(false);
});
- it.fails("leaves an already-capped tool result unchanged on a later compaction", async () => {
+ it("leaves an already-capped tool result unchanged on a later compaction", async () => {
const annotations = (message: ModelMessage | undefined) =>
JSON.stringify(message).split("Truncated by eve").length - 1;
const messages = [
@@ -599,9 +620,10 @@ describe("compactMessages: tool-result cap heuristic", () => {
});
it("does not split a UTF-16 surrogate pair when capping tool results", async () => {
- // JSON.stringify of this output places πŸ“₯'s high surrogate at index 1999
- // of the 2000-unit TRANSCRIPT_PAYLOAD_LIMIT cut.
- const content = `${"x".repeat(1964)}πŸ“₯${"y".repeat(500)}`;
+ // JSON.stringify of this output places πŸ“₯'s high surrogate at index 1886,
+ // the last unit of the 1887-unit cut the annotation leaves of the
+ // 2000-unit TRANSCRIPT_PAYLOAD_LIMIT.
+ const content = `${"x".repeat(1851)}πŸ“₯${"y".repeat(500)}`;
const call: ModelMessage = {
content: [{ input: {}, toolCallId: "call-0", toolName: "grep", type: "tool-call" }],
role: "assistant",
@@ -632,9 +654,59 @@ describe("compactMessages: tool-result cap heuristic", () => {
expect(value).toBe(value.toWellFormed());
expect(/[\uD800-\uDBFF](?![\uDC00-\uDFFF])/.test(value)).toBe(false);
expect(value).not.toContain("πŸ“₯");
- expect(value).toContain("x".repeat(1964));
+ expect(value).toContain("x".repeat(1851));
});
+ it.each([
+ ["plain JSON", { type: "json" as const, value: { content: "x".repeat(4_000) } }],
+ ["escape-heavy JSON", { type: "json" as const, value: { content: 'a"b\\c\n\t'.repeat(800) } }],
+ ["astral characters", { type: "json" as const, value: { content: "πŸ“₯".repeat(2_000) } }],
+ ["text", { type: "text" as const, value: `line "one"\n${"y".repeat(4_000)}` }],
+ ])("returns a capped %s result unchanged on every later compaction", async (_name, output) => {
+ let history: ModelMessage[] = [
+ user("investigate"),
+ ...toolResultExchange(output),
+ user("next"),
+ ];
+ const { result: first } = await compact(history, { recentWindowSize: 1 });
+ const capped = first[2];
+ const cappedValue = toolResultValue(capped);
+ expect(cappedValue.startsWith("[Truncated by eve")).toBe(true);
+ expect(cappedValue.length).toBeLessThanOrEqual(2_000);
+
+ history = first;
+ for (let round = 0; round < 3; round += 1) {
+ const { result, summarizer } = await compact(history, { recentWindowSize: 1 });
+ expect(summarizer).not.toHaveBeenCalled();
+ expect(result[2]).toBe(capped);
+ history = result;
+ }
+ });
+
+ it.each([
+ ["plain JSON", { content: `${"x".repeat(1_700)}KEPT-END-7781${"z".repeat(4_000)}` }],
+ ["escape-heavy JSON", { content: `${'q"\\'.repeat(300)}KEPT-END-7781${"z".repeat(4_000)}` }],
+ ])(
+ "keeps the whole capped %s content when a later compaction summarizes",
+ async (_name, value) => {
+ const history: ModelMessage[] = [
+ user("investigate"),
+ ...toolResultExchange({ type: "json", value }),
+ user("next"),
+ ];
+ const { result: first } = await compact(history, { recentWindowSize: 1 });
+ const cappedValue = toolResultValue(first[2]);
+ expect(cappedValue).toContain("KEPT-END-7781");
+
+ const summarize = summarizeWith("forced checkpoint");
+ await compactMessages(first, { recentWindowSize: 1, threshold: ROOMY }, summarize, true);
+
+ const transcript = String(summarize.mock.calls[0]?.[0]?.messages?.[0]?.content);
+ expect(transcript).toContain(cappedValue.replace(/\s+/g, " ").trim());
+ expect(transcript.split("Truncated by eve").length - 1).toBe(1);
+ },
+ );
+
it("stubs content-output file parts instead of truncating into their payloads", async () => {
const base64 = "iVBORw0KGgo".repeat(500);
const messages: ModelMessage[] = [
diff --git a/packages/eve/src/harness/compaction/engine.ts b/packages/eve/src/harness/compaction/engine.ts
index 01824958e..13acb6f10 100644
--- a/packages/eve/src/harness/compaction/engine.ts
+++ b/packages/eve/src/harness/compaction/engine.ts
@@ -5,6 +5,7 @@ import {
COMPACTION_PROMPT_ENVELOPE,
COMPACTION_RESUMPTION_MESSAGE,
createCompactionPrompt,
+ serializeToolResultOutput,
sliceUtf16Safe,
stubContentOutputFileParts,
TRANSCRIPT_PAYLOAD_LIMIT,
@@ -258,6 +259,7 @@ export async function compactMessages(
const CAPPED_RESULT_ANNOTATION =
"[Truncated by eve: tool result reduced during context compaction. Re-run the tool if you need the full output.]";
+const CAPPED_RESULT_PREFIX = `${CAPPED_RESULT_ANNOTATION}\n\n`;
/**
* Caps oversized tool-result outputs in place, keeping message structure and
@@ -266,6 +268,10 @@ const CAPPED_RESULT_ANNOTATION =
* kept β€” capping never destroys material the summarizer would need, unlike
* dropping results outright. The annotation leads the value so it survives
* prefix-capped renderings.
+ *
+ * Capping is idempotent: the capped value, annotation included, fits the
+ * same limit it was measured against, and a text output is measured by its
+ * value, so a later compaction leaves an already-capped part as it is.
*/
function capToolResults(messages: readonly ModelMessage[]): ModelMessage[] {
return messages.map((message) => {
@@ -282,7 +288,7 @@ function capToolResults(messages: readonly ModelMessage[]): ModelMessage[] {
// size check: serializing raw payloads into a prefix cap would keep
// bytes the model cannot read while cutting the text it can.
const output = stubContentOutputFileParts(part.output) as typeof part.output;
- const serialized = JSON.stringify(output) ?? "";
+ const serialized = serializeToolResultOutput(output);
if (serialized.length <= TRANSCRIPT_PAYLOAD_LIMIT) {
if (output === part.output) {
return part;
@@ -296,7 +302,7 @@ function capToolResults(messages: readonly ModelMessage[]): ModelMessage[] {
...part,
output: {
type: "text" as const,
- value: `${CAPPED_RESULT_ANNOTATION}\n\n${sliceUtf16Safe(serialized, TRANSCRIPT_PAYLOAD_LIMIT)}`,
+ value: `${CAPPED_RESULT_PREFIX}${sliceUtf16Safe(serialized, TRANSCRIPT_PAYLOAD_LIMIT - CAPPED_RESULT_PREFIX.length)}`,
},
};
});
diff --git a/packages/eve/src/harness/compaction/prompt.ts b/packages/eve/src/harness/compaction/prompt.ts
index 38873b174..95659ec09 100644
--- a/packages/eve/src/harness/compaction/prompt.ts
+++ b/packages/eve/src/harness/compaction/prompt.ts
@@ -214,7 +214,7 @@ function renderTranscriptToolResult(
function renderToolResultOutput(value: unknown, limit: number): string {
const parts = contentToolOutputParts(value);
if (parts === undefined) {
- return renderPayload(value, limit);
+ return capText(serializeToolResultOutput(value), limit);
}
const rendered = parts
.map((part) => renderContentToolOutputPart(part, limit))
@@ -223,6 +223,22 @@ function renderToolResultOutput(value: unknown, limit: number): string {
return capText(rendered, limit);
}
+/**
+ * The text a tool-result output is measured and clipped by, in history
+ * capping and in the summarizer transcript alike: a text output is its
+ * value, any other output its JSON. Sharing it keeps a capped history
+ * result whole when the summarizer renders it.
+ */
+export function serializeToolResultOutput(output: unknown): string {
+ if (output !== null && typeof output === "object") {
+ const candidate = output as { readonly type?: unknown; readonly value?: unknown };
+ if (candidate.type === "text" && typeof candidate.value === "string") {
+ return candidate.value;
+ }
+ }
+ return JSON.stringify(output) ?? "";
+}
+
function contentToolOutputParts(value: unknown): readonly unknown[] | undefined {
if (value === null || typeof value !== "object") return undefined;
const candidate = value as { readonly type?: unknown; readonly value?: unknown };