Skip to content

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

Rust

[RSI, bug] compaction: keep tool identity in serialized summarizer input (#2424)

Seth Karten5fb3d9acbafa
diff --git a/packages/coding-agent/.changes/compaction-tool-result-labels.md b/packages/coding-agent/.changes/compaction-tool-result-labels.md
new file mode 100644
index 0000000000..30e8484e28
--- /dev/null
+++ b/packages/coding-agent/.changes/compaction-tool-result-labels.md
@@ -0,0 +1,3 @@
+- Fixed: compaction, branch, and refine summaries now label each serialized tool result with its tool name, so parallel tool calls can be paired with their results.
+- Fixed: failed tool results are marked as errors in the label, keeping failure attribution visible in summaries.
+- Fixed: serialized tool calls now carry a sequential `#N` prefix and results repeat the matching index, so repeated calls of the same tool pair unambiguously.
diff --git a/packages/coding-agent/docs/compaction.md b/packages/coding-agent/docs/compaction.md
index 11f2837cfe..69b3080d5f 100644
--- a/packages/coding-agent/docs/compaction.md
+++ b/packages/coding-agent/docs/compaction.md
@@ -259,12 +259,15 @@ Before summarization, messages are serialized to text via [`serializeConversatio
[User]: What they said
[Assistant thinking]: Internal reasoning
[Assistant]: Response text
-[Assistant tool calls]: ipython(code="open('foo.ts').read()"); edit(path="bar.ts", ...)
-[Tool result]: Output from tool
+[Assistant tool calls]: #1 ipython(code="open('foo.ts').read()"); #2 bash(command="npm test")
+[Tool result (ipython) #1]: Output from tool
+[Tool result (bash, error) #2]: Output from a failed tool call
```
This prevents the model from treating it as a conversation to continue.
+Each tool call carries a sequential `#N` prefix and its result repeats the matching index, so repeated calls of the same tool in one turn pair unambiguously. Branch summaries drop tool-result entries before serialization (their context remains attached to the assistant tool call), so they show the `#N` call prefixes without any result lines. When the serialized input lacks the matching call — possible for extension callers passing partial message lists, since `serializeConversation` is exported — the result falls back to the name-only label.
+
Tool results are truncated to 2000 characters during serialization. Content beyond that limit is replaced with a marker indicating how many characters were truncated. This keeps summarization requests within reasonable token budgets, since tool results, especially from `ipython` and optional `bash`, are typically the largest contributors to context size.
## Custom Summarization via Extensions
@@ -323,8 +326,8 @@ pi.on("session_before_compact", async (event, ctx) => {
// [User]: message text
// [Assistant thinking]: thinking content
// [Assistant]: response text
- // [Assistant tool calls]: ipython(code="open('...').read()"); bash(command="...")
- // [Tool result]: output text
+ // [Assistant tool calls]: #1 ipython(code="open('...').read()"); #2 bash(command="...")
+ // [Tool result (ipython) #1]: output text
// Now send to your model for summarization
const summary = await myModel.summarize(conversationText);
diff --git a/packages/coding-agent/src/core/compaction/utils.ts b/packages/coding-agent/src/core/compaction/utils.ts
index 6373043672..aa811c8506 100644
--- a/packages/coding-agent/src/core/compaction/utils.ts
+++ b/packages/coding-agent/src/core/compaction/utils.ts
@@ -140,9 +140,19 @@ function truncateForSummary(text: string, maxChars: number): string {
*
* Tool results are truncated to keep the summarization request within
* reasonable token budgets. Full content is not needed for summarization.
+ *
+ * Tool calls are serialized with a sequential `#N` prefix and results repeat
+ * the matching index, so repeated calls of the same tool pair unambiguously.
*/
export function serializeConversation(messages: Message[]): string {
const parts: string[] = [];
+ // Tool calls are serialized with a 1-based sequential index and results
+ // repeat the index of their call (matched by toolCallId), so repeated
+ // calls of the same tool pair unambiguously in the summarizer input.
+ // The short index stands in for the raw provider toolCallId, which can
+ // exceed 450 characters on some providers.
+ const toolCallIndices = new Map<string, number>();
+ let toolCallIndex = 0;
for (const msg of messages) {
if (msg.role === "user") {
@@ -169,7 +179,9 @@ export function serializeConversation(messages: Message[]): string {
const argsStr = Object.entries(args)
.map(([k, v]) => `${k}=${JSON.stringify(v)}`)
.join(", ");
- toolCalls.push(`${block.name}(${argsStr})`);
+ toolCallIndex += 1;
+ toolCallIndices.set(block.id, toolCallIndex);
+ toolCalls.push(`#${toolCallIndex} ${block.name}(${argsStr})`);
}
}
@@ -188,7 +200,19 @@ export function serializeConversation(messages: Message[]): string {
.map((c) => c.text)
.join("");
if (content) {
- parts.push(`[Tool result]: ${truncateForSummary(content, TOOL_RESULT_MAX_CHARS)}`);
+ // Label the tool name, error status, and the index of the
+ // paired call so the summarizer can match each result to
+ // its `#N`-prefixed entry in the [Assistant tool calls]
+ // lines even when the same tool is called repeatedly in
+ // one turn. Results whose call is not part of the input
+ // (extension callers may pass partial message lists)
+ // fall back to the name-only label.
+ const callIndex = toolCallIndices.get(msg.toolCallId);
+ const indexSuffix = callIndex === undefined ? "" : ` #${callIndex}`;
+ const label = msg.isError
+ ? `[Tool result (${msg.toolName}, error)${indexSuffix}]`
+ : `[Tool result (${msg.toolName})${indexSuffix}]`;
+ parts.push(`${label}: ${truncateForSummary(content, TOOL_RESULT_MAX_CHARS)}`);
}
}
}
diff --git a/packages/coding-agent/test/compaction.test.ts b/packages/coding-agent/test/compaction.test.ts
index cee582f15f..c966e135bf 100644
--- a/packages/coding-agent/test/compaction.test.ts
+++ b/packages/coding-agent/test/compaction.test.ts
@@ -1,5 +1,5 @@
import type { AgentMessage } from "@earendil-works/pi-agent-core";
-import type { AssistantMessage, Message, Usage } from "@earendil-works/pi-ai";
+import type { AssistantMessage, Message, ToolCall, Usage } from "@earendil-works/pi-ai";
import { readFileSync } from "fs";
import { join } from "path";
import { beforeEach, describe, expect, it } from "vitest";
@@ -48,10 +48,10 @@ function createUserMessage(text: string): AgentMessage {
return { role: "user", content: text, timestamp: Date.now() };
}
-function createAssistantMessage(text: string, usage?: Usage): AssistantMessage {
+function createAssistantMessage(content: AssistantMessage["content"] | string, usage?: Usage): AssistantMessage {
return {
role: "assistant",
- content: [{ type: "text", text }],
+ content: typeof content === "string" ? [{ type: "text", text: content }] : content,
usage: usage || createMockUsage(100, 50),
stopReason: "stop",
timestamp: Date.now(),
@@ -340,59 +340,67 @@ describe("Large session fixture", () => {
// Conversation serialization (merged from compaction-serialization.test.ts)
// ============================================================================
-function toolResult(text: string): Message {
+function toolResult(text: string, toolName = "ipython", isError = false, toolCallId = "tc1"): Message {
return {
role: "toolResult",
- toolCallId: "tc1",
- toolName: "ipython",
+ toolCallId,
+ toolName,
content: [{ type: "text", text }],
- isError: false,
+ isError,
timestamp: Date.now(),
};
}
+function toolCall(id: string, name: string, args: Record<string, unknown>): ToolCall {
+ return { type: "toolCall", id, name, arguments: args };
+}
+
describe("serializeConversation", () => {
it("truncates long tool results keeping head and tail within the summary budget", () => {
const head = "A".repeat(1431);
const tail = "T".repeat(500);
const result = serializeConversation([toolResult(head + "B".repeat(3069) + tail)]);
- expect(result).toContain("[Tool result]:");
+ expect(result).toContain("[Tool result (ipython)]:");
expect(result).toContain(head);
expect(result).toContain(tail);
expect(result).toContain("[... 3069 characters truncated; first 1431 and last 500 kept ...]");
expect(result).not.toContain("B".repeat(10));
- expect(result.length).toBeLessThanOrEqual("[Tool result]: ".length + 2000);
+ expect(result.length).toBeLessThanOrEqual("[Tool result (ipython)]: ".length + 2000);
});
- it("leaves short tool results untouched", () => {
+ it.each([
+ ["labels short success results with the tool name", "bash", false, "[Tool result (bash)]"],
+ ["labels short error results as failed", "edit", true, "[Tool result (edit, error)]"],
+ ])("%s", (_label, toolName, isError, label) => {
const shortContent = "x".repeat(1500);
+ expect(serializeConversation([toolResult(shortContent, toolName, isError)])).toBe(`${label}: ${shortContent}`);
+ });
- expect(serializeConversation([toolResult(shortContent)])).toBe(`[Tool result]: ${shortContent}`);
+ it.each([
+ [
+ "pairs repeated same-name tool calls with their results by index",
+ [toolCall("c1", "ipython", { code: "a" }), toolCall("c2", "ipython", { code: "b" })],
+ [toolResult("first output", "ipython", false, "c1"), toolResult("second output", "ipython", true, "c2")],
+ '[Assistant tool calls]: #1 ipython(code="a"); #2 ipython(code="b")\n\n' +
+ "[Tool result (ipython) #1]: first output\n\n" +
+ "[Tool result (ipython, error) #2]: second output",
+ ],
+ [
+ "falls back to the name-only label when the result's call was not serialized",
+ [toolCall("c1", "bash", { command: "ls" })],
+ [toolResult("orphan output", "ipython", false, "tc-orphan")],
+ '[Assistant tool calls]: #1 bash(command="ls")\n\n[Tool result (ipython)]: orphan output',
+ ],
+ ])("%s", (_label, calls, results, expected) => {
+ expect(serializeConversation([createAssistantMessage(calls), ...results])).toBe(expected);
});
it("does not truncate user or assistant messages", () => {
const longText = "y".repeat(5000);
- const usage: Usage = {
- input: 0,
- output: 0,
- cacheRead: 0,
- cacheWrite: 0,
- totalTokens: 0,
- cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
- };
const result = serializeConversation([
{ role: "user", content: [{ type: "text", text: longText }], timestamp: Date.now() },
- {
- role: "assistant",
- content: [{ type: "text", text: longText }],
- api: "anthropic",
- provider: "anthropic",
- model: "test",
- usage,
- stopReason: "stop",
- timestamp: Date.now(),
- },
+ createAssistantMessage(longText),
]);
expect(result).not.toContain("truncated");