Skip to content

A self-improving RLM agent for coding workflows and long-running autonomous tasks.

Rust

[RSI] Keep tool-result tails in compaction summaries (#2217)

Seth Karten6be5d4f297f1
diff --git a/packages/coding-agent/.changes/eng-6083-compaction-tail-truncation.md b/packages/coding-agent/.changes/eng-6083-compaction-tail-truncation.md
new file mode 100644
index 0000000000..861da42168
--- /dev/null
+++ b/packages/coding-agent/.changes/eng-6083-compaction-tail-truncation.md
@@ -0,0 +1 @@
+- Fixed compaction summaries dropping the tail of tool results: truncated tool output now keeps the last 500 characters so errors and log tails survive compaction.
diff --git a/packages/coding-agent/src/core/compaction/utils.ts b/packages/coding-agent/src/core/compaction/utils.ts
index 72a764566d..b9481170e2 100644
--- a/packages/coding-agent/src/core/compaction/utils.ts
+++ b/packages/coding-agent/src/core/compaction/utils.ts
@@ -71,15 +71,27 @@ export function formatFileOperations(readFiles: string[], modifiedFiles: string[
}
/** Maximum characters for a tool result in serialized summaries. */
const TOOL_RESULT_MAX_CHARS = 2000;
+/**
+ * Characters kept from the end of a truncated tool result. Tool output is
+ * tail-heavy: exit errors, stack traces, and log tails appear at the end.
+ */
+const TOOL_RESULT_TAIL_CHARS = 500;
/**
* Truncate text to a maximum character length for summarization.
- * Keeps the beginning and appends a truncation marker.
+ * Keeps the beginning and the end within the same total budget, marking
+ * the elided middle.
*/
function truncateForSummary(text: string, maxChars: number): string {
if (text.length <= maxChars) return text;
- const truncatedChars = text.length - maxChars;
- return `${text.slice(0, maxChars)}\n\n[... ${truncatedChars} more characters truncated]`;
+ // The marker's digit counts are largest when the elided and kept sizes hit
+ // the text and budget maxima, so reserve space for that worst case to keep
+ // the result within maxChars.
+ const markerMaxLength =
+ `[... ${text.length} characters truncated; first ${maxChars} and last ${TOOL_RESULT_TAIL_CHARS} kept ...]`.length;
+ const headChars = maxChars - TOOL_RESULT_TAIL_CHARS - markerMaxLength - 4;
+ const elided = text.length - headChars - TOOL_RESULT_TAIL_CHARS;
+ return `${text.slice(0, headChars)}\n\n[... ${elided} characters truncated; first ${headChars} and last ${TOOL_RESULT_TAIL_CHARS} kept ...]\n\n${text.slice(text.length - TOOL_RESULT_TAIL_CHARS)}`;
}
/**
diff --git a/packages/coding-agent/test/compaction-serialization.test.ts b/packages/coding-agent/test/compaction-serialization.test.ts
index 75ceea1b16..ea2cfa3236 100644
--- a/packages/coding-agent/test/compaction-serialization.test.ts
+++ b/packages/coding-agent/test/compaction-serialization.test.ts
@@ -3,8 +3,11 @@ 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);
+ it("should truncate long tool results keeping head and tail", () => {
+ const head = "A".repeat(1431);
+ const middle = "B".repeat(3069);
+ const tail = "T".repeat(500);
+ const longContent = head + middle + tail;
const messages: Message[] = [
{
role: "toolResult",
@@ -19,9 +22,15 @@ describe("serializeConversation", () => {
const result = serializeConversation(messages);
expect(result).toContain("[Tool result]:");
- expect(result).toContain("[... 3000 more characters truncated]");
- expect(result).not.toContain("x".repeat(3000));
- expect(result).toContain("x".repeat(2000));
+ // The head (setup/output context) survives...
+ expect(result).toContain(head);
+ // ...the tail survives (errors and log tails live at the end)...
+ expect(result).toContain(tail);
+ // ...and the marker records exactly how much was elided.
+ expect(result).toContain("[... 3069 characters truncated; first 1431 and last 500 kept ...]");
+ expect(result).not.toContain("B".repeat(10));
+ // ...and the truncated result stays within the summary budget.
+ expect(result.length).toBeLessThanOrEqual("[Tool result]: ".length + 2000);
});
it("should not truncate short tool results", () => {