Skip to content

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

Rust

[RSI] Track kernel-performed file edits in compaction summaries (#2226)

Seth Kartenf31c440c7884
diff --git a/packages/coding-agent/.changes/eng-6090-compaction-kernel-file-ops.md b/packages/coding-agent/.changes/eng-6090-compaction-kernel-file-ops.md
new file mode 100644
index 0000000000..64dffdd934
--- /dev/null
+++ b/packages/coding-agent/.changes/eng-6090-compaction-kernel-file-ops.md
@@ -0,0 +1 @@
+- Fixed compaction summaries never recording kernel-performed file edits: ipython tool results now contribute their structured edit diffs to the tracked file operations, so `<modified-files>` reflects the default toolset's edits.
diff --git a/packages/coding-agent/src/core/compaction/utils.ts b/packages/coding-agent/src/core/compaction/utils.ts
index b9481170e2..6373043672 100644
--- a/packages/coding-agent/src/core/compaction/utils.ts
+++ b/packages/coding-agent/src/core/compaction/utils.ts
@@ -19,9 +19,14 @@ export function createFileOps(): FileOperations {
}
/**
- * Extract file operations from tool calls in an assistant message.
+ * Extract file operations from tool calls in an assistant message and from
+ * structured tool results.
*/
export function extractFileOpsFromMessage(message: AgentMessage, fileOps: FileOperations): void {
+ if (message.role === "toolResult") {
+ extractFileOpsFromToolResult(message, fileOps);
+ return;
+ }
if (message.role !== "assistant") return;
if (!("content" in message) || !Array.isArray(message.content)) return;
@@ -44,14 +49,48 @@ export function extractFileOpsFromMessage(message: AgentMessage, fileOps: FileOp
}
}
+/**
+ * Extract file operations from a tool result message.
+ *
+ * The default toolset routes file edits through the ipython kernel: the
+ * kernel's edit skill reports structured diff displays (path, oldStr, newStr)
+ * that ride on the tool result's details, and no assistant-side tool call
+ * ever carries the path. Without this branch, compaction summaries never
+ * learn about kernel-performed edits and <modified-files> stays empty in the
+ * default configuration.
+ */
+function extractFileOpsFromToolResult(message: AgentMessage, fileOps: FileOperations): void {
+ if (message.role !== "toolResult" || message.toolName !== "ipython") return;
+ const details =
+ typeof message.details === "object" && message.details !== null && !Array.isArray(message.details)
+ ? (message.details as Record<string, unknown>)
+ : {};
+ const diffs = Array.isArray(details.diffs) ? details.diffs : [];
+ for (const diff of diffs) {
+ if (typeof diff !== "object" || diff === null || Array.isArray(diff)) continue;
+ const path = (diff as Record<string, unknown>).path;
+ if (typeof path === "string" && path) fileOps.edited.add(path);
+ }
+}
+
+/**
+ * Maximum files kept per summary block, so a single oversized kernel
+ * result cannot produce a file list larger than the model context limit.
+ */
+const FILE_LIST_MAX_ENTRIES = 200;
+
/**
* Compute final file lists from file operations.
* Returns readFiles (files only read, not modified) and modifiedFiles.
+ * Both lists are capped at FILE_LIST_MAX_ENTRIES (sorted, then truncated).
*/
export function computeFileLists(fileOps: FileOperations): { readFiles: string[]; modifiedFiles: string[] } {
const modified = new Set([...fileOps.edited, ...fileOps.written]);
- const readOnly = [...fileOps.read].filter((f) => !modified.has(f)).sort();
- const modifiedFiles = [...modified].sort();
+ const readOnly = [...fileOps.read]
+ .filter((f) => !modified.has(f))
+ .sort()
+ .slice(0, FILE_LIST_MAX_ENTRIES);
+ const modifiedFiles = [...modified].sort().slice(0, FILE_LIST_MAX_ENTRIES);
return { readFiles: readOnly, modifiedFiles };
}
diff --git a/packages/coding-agent/test/compaction-file-ops.test.ts b/packages/coding-agent/test/compaction-file-ops.test.ts
new file mode 100644
index 0000000000..eb7b8df3e6
--- /dev/null
+++ b/packages/coding-agent/test/compaction-file-ops.test.ts
@@ -0,0 +1,101 @@
+import type { AssistantMessage, ToolResultMessage, Usage } from "@earendil-works/pi-ai";
+import { describe, expect, it } from "vitest";
+import {
+ computeFileLists,
+ createFileOps,
+ extractFileOpsFromMessage,
+ formatFileOperations,
+} from "../src/core/compaction/utils.js";
+
+const usage: Usage = {
+ input: 0,
+ output: 0,
+ cacheRead: 0,
+ cacheWrite: 0,
+ totalTokens: 0,
+ cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
+};
+
+function assistant(content: AssistantMessage["content"]): AssistantMessage {
+ return {
+ role: "assistant",
+ content,
+ api: "test",
+ provider: "test",
+ model: "test",
+ usage,
+ stopReason: "toolUse",
+ timestamp: 0,
+ };
+}
+
+function toolResult(details: unknown, toolName = "ipython"): ToolResultMessage {
+ return { role: "toolResult", toolCallId: "call-1", toolName, content: [], details, isError: false, timestamp: 0 };
+}
+
+function toolCall(name: string, args: Record<string, unknown>) {
+ return { type: "toolCall", id: "call-1", name, arguments: args } as const;
+}
+
+describe("compaction file-op extraction", () => {
+ it("records native edit tool calls", () => {
+ const fileOps = createFileOps();
+ extractFileOpsFromMessage(assistant([toolCall("edit", { path: "src/native-edit.ts" })]) as never, fileOps);
+ expect([...fileOps.edited]).toEqual(["src/native-edit.ts"]);
+ });
+
+ it("records kernel-reported edits from ipython tool results", () => {
+ const fileOps = createFileOps();
+ extractFileOpsFromMessage(
+ toolResult({
+ status: "ok",
+ diffs: [
+ { path: "pkg/a.ts", oldStr: "x", newStr: "y" },
+ { path: "pkg/b.ts", oldStr: "x", newStr: "y" },
+ ],
+ }) as never,
+ fileOps,
+ );
+ expect([...fileOps.edited]).toEqual(["pkg/a.ts", "pkg/b.ts"]);
+ });
+
+ it("ignores non-ipython tool results and malformed diff payloads", () => {
+ const fileOps = createFileOps();
+ extractFileOpsFromMessage(toolResult({ diffs: [{ path: "pkg/c.ts" }] }, "bash") as never, fileOps);
+ extractFileOpsFromMessage(toolResult({ diffs: "not-an-array" }) as never, fileOps);
+ extractFileOpsFromMessage(toolResult({ diffs: [{ noPath: true }, { path: 42 }, null] }) as never, fileOps);
+ extractFileOpsFromMessage(toolResult(undefined) as never, fileOps);
+ expect(fileOps.edited.size).toBe(0);
+ });
+
+ it("caps kernel file lists to keep summary blocks bounded", () => {
+ const fileOps = createFileOps();
+ const diffs = Array.from({ length: 250 }, (_, i) => ({
+ path: `pkg/file-${String(i).padStart(3, "0")}.ts`,
+ oldStr: "a",
+ newStr: "b",
+ }));
+ extractFileOpsFromMessage(toolResult({ diffs }) as never, fileOps);
+ const { readFiles, modifiedFiles } = computeFileLists(fileOps);
+ expect(modifiedFiles).toHaveLength(200);
+ expect(modifiedFiles[0]).toBe("pkg/file-000.ts");
+ expect(modifiedFiles[199]).toBe("pkg/file-199.ts");
+ expect(modifiedFiles).not.toContain("pkg/file-249.ts");
+ const summary = formatFileOperations(readFiles, modifiedFiles);
+ expect(summary).not.toContain("pkg/file-249.ts");
+ });
+
+ it("feeds kernel edits into modified-files for summaries", () => {
+ const fileOps = createFileOps();
+ extractFileOpsFromMessage(
+ toolResult({ diffs: [{ path: "src/kernel-edit.ts", oldStr: "a", newStr: "b" }] }) as never,
+ fileOps,
+ );
+ const { readFiles, modifiedFiles } = computeFileLists(fileOps);
+ expect(readFiles).toEqual([]);
+ expect(modifiedFiles).toEqual(["src/kernel-edit.ts"]);
+ expect(formatFileOperations(readFiles, modifiedFiles)).toContain(
+ "<modified-files>\nsrc/kernel-edit.ts\n</modified-files>",
+ );
+ });
+});