Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

fix(agent-runtime): record compaction file ops for aliased path arguments

xpeng5278-web91a06646d791
diff --git a/docs/spec/03-runtime/02-agent-runtime.md b/docs/spec/03-runtime/02-agent-runtime.md
index f5304b1fe..1ca248a76 100644
--- a/docs/spec/03-runtime/02-agent-runtime.md
+++ b/docs/spec/03-runtime/02-agent-runtime.md
@@ -670,7 +670,11 @@ checkpoint's `readFiles` / `modifiedFiles` and the `<read-files>` section of a
summary were always empty (D618). Codex-shaped compaction folds pi's retained
tail into that same summary, and the runtime records file operations from the
folded tail as well, so a file touched only inside the recent-token window
-still appears in the checkpoint lists and summary tags.
+still appears in the checkpoint lists and summary tags. The stored tool call keeps
+the argument spelling the model sent, because `normalizeToolParams` folds
+alias names only when the call executes (D273). The collector therefore reads
+the path from `path` or from the same aliases (`file_path`, `filepath`,
+`filePath`, `filename`, `fileName`, `file`), with the same precedence.
A delegate (§5f) runs the same derivation against its own resolved model and
compacts at its own turn boundaries, without a durable checkpoint chain of
diff --git a/packages/agent-runtime/src/pi-file-ops.test.ts b/packages/agent-runtime/src/pi-file-ops.test.ts
index 43adef657..6dd235c5b 100644
--- a/packages/agent-runtime/src/pi-file-ops.test.ts
+++ b/packages/agent-runtime/src/pi-file-ops.test.ts
@@ -5,10 +5,14 @@ import { describe, expect, it } from "vitest";
import { withPiFileOpToolNames } from "./pi-file-ops.js";
-function toolCallMessage(name: string, path: string, id = "call-1"): AgentMessage {
+function toolCallWithArguments(
+ name: string,
+ args: Record<string, unknown>,
+ id = "call-1",
+): AgentMessage {
return {
role: "assistant",
- content: [{ type: "toolCall", id, name, arguments: { path } }],
+ content: [{ type: "toolCall", id, name, arguments: args }],
api: "openai-completions",
provider: "local",
model: "local-model",
@@ -25,6 +29,10 @@ function toolCallMessage(name: string, path: string, id = "call-1"): AgentMessag
} as AgentMessage;
}
+function toolCallMessage(name: string, path: string, id = "call-1"): AgentMessage {
+ return toolCallWithArguments(name, { path }, id);
+}
+
function userMessage(text: string, timestamp: number): AgentMessage {
return { role: "user", content: [{ type: "text", text }], timestamp } as AgentMessage;
}
@@ -145,4 +153,97 @@ describe("pi's file-op collection through the adapter", () => {
expect(toolCallNames(preparation!.messagesToSummarize)).toEqual(["read", "edit"]);
});
+
+ it("records Read, Write, and Edit paths sent under argument aliases", () => {
+ // Models keep the spelling they sent. Strong models send `file_path`;
+ // weaker ones send `filepath` / `file` (D273 / issue #454). The stored
+ // tool call is not folded, so the checkpoint collector has to read them.
+ const entries = [
+ messageEntry("u1", userMessage("touch the files", 1), 1),
+ messageEntry("a1", toolCallWithArguments("Read", { file_path: "src/a.txt" }), 2),
+ messageEntry("a2", toolCallWithArguments("Edit", { filepath: "src/b.ts" }, "call-2"), 3),
+ messageEntry("a3", toolCallWithArguments("Write", { file: "src/c.ts" }, "call-3"), 4),
+ messageEntry("u2", userMessage("latest question", 5), 5),
+ ];
+
+ const preparation = preparationOf(entries, true);
+
+ expect(preparation).toBeDefined();
+ expect([...preparation!.fileOps.read]).toEqual(["src/a.txt"]);
+ expect([...preparation!.fileOps.edited]).toEqual(["src/b.ts"]);
+ expect([...preparation!.fileOps.written]).toEqual(["src/c.ts"]);
+ });
+
+ it("uses the key the call ran with: path first, then aliases in normalizeToolParams order", () => {
+ const entries = [
+ messageEntry("u1", userMessage("touch the files", 1), 1),
+ messageEntry(
+ "a1",
+ toolCallWithArguments("Read", {
+ file: "from-file",
+ fileName: "from-fileName",
+ filename: "from-filename",
+ filepath: "from-filepath",
+ filePath: "from-filePath",
+ file_path: "from-file_path",
+ path: "src/canonical.txt",
+ }),
+ 2,
+ ),
+ messageEntry(
+ "a2",
+ toolCallWithArguments("Write", {
+ path: "",
+ file_path: "src/not-used.ts",
+ file: "src/ignored.ts",
+ }, "call-2"),
+ 3,
+ ),
+ messageEntry(
+ "a3",
+ toolCallWithArguments("Edit", {
+ filepath: "src/lower.ts",
+ filePath: "src/camel.ts",
+ }, "call-3"),
+ 4,
+ ),
+ messageEntry(
+ "a4",
+ toolCallWithArguments("Read", {
+ filename: "src/name.ts",
+ fileName: "src/nameCamel.ts",
+ file: "src/bare.ts",
+ }, "call-4"),
+ 5,
+ ),
+ messageEntry(
+ "a5",
+ toolCallWithArguments("Read", {
+ fileName: "src/only-fileName.ts",
+ file: "src/bare.ts",
+ }, "call-5"),
+ 6,
+ ),
+ messageEntry("a6", toolCallWithArguments("Read", { file: "src/only-file.ts" }, "call-6"), 7),
+ messageEntry(
+ "a7",
+ toolCallWithArguments("Write", { path: "", file_path: "", file: 12 }, "call-7"),
+ 8,
+ ),
+ messageEntry("u2", userMessage("latest question", 9), 9),
+ ];
+
+ const preparation = preparationOf(entries, true);
+
+ expect(preparation).toBeDefined();
+ expect([...preparation!.fileOps.read]).toEqual([
+ "src/canonical.txt",
+ "src/name.ts",
+ "src/only-fileName.ts",
+ "src/only-file.ts",
+ ]);
+ // `path: ""` wins over `file_path` at execution too, so nothing was written.
+ expect([...preparation!.fileOps.written]).toEqual([]);
+ expect([...preparation!.fileOps.edited]).toEqual(["src/lower.ts"]);
+ });
});
diff --git a/packages/agent-runtime/src/pi-runtime-compaction-plan.ts b/packages/agent-runtime/src/pi-runtime-compaction-plan.ts
index aab1450d5..296a84dac 100644
--- a/packages/agent-runtime/src/pi-runtime-compaction-plan.ts
+++ b/packages/agent-runtime/src/pi-runtime-compaction-plan.ts
@@ -113,6 +113,38 @@ function createFileOperations(): FileOperations {
return { read: new Set(), written: new Set(), edited: new Set() };
}
+/**
+ * Spellings of `path` kept on a stored `Read` / `Write` / `Edit` call.
+ *
+ * `path` plus `PATH_ARG_ALIASES` from runtime.ts, in that map's order (D273 /
+ * issue #454). This module cannot import the map: runtime.ts already imports
+ * the planner. `normalizeToolParams` folds an alias only when the call
+ * executes, and the runtime defines no `prepareArguments`, so the assistant
+ * message still carries the spelling the model sent.
+ */
+const FILE_OPERATION_PATH_KEYS = [
+ "path",
+ "file_path",
+ "filepath",
+ "filePath",
+ "filename",
+ "fileName",
+ "file",
+] as const;
+
+/**
+ * The path the call actually ran with: the first key that is present wins,
+ * the same precedence `normalizeToolParams` applies, and it only counts when
+ * it is a non-empty string.
+ */
+function fileOperationPath(args: unknown): string | undefined {
+ if (!args || typeof args !== "object") return undefined;
+ const record = args as Record<string, unknown>;
+ const key = FILE_OPERATION_PATH_KEYS.find((name) => record[name] !== undefined);
+ const value = key === undefined ? undefined : record[key];
+ return typeof value === "string" && value.length > 0 ? value : undefined;
+}
+
/** Record one assistant message's Read/Write/Edit paths. Mutates `fileOps`. */
export function collectMessageFileOperations(
message: AgentMessage,
@@ -121,8 +153,7 @@ export function collectMessageFileOperations(
if (message.role !== "assistant" || !Array.isArray(message.content)) return;
for (const block of message.content) {
if (!block || block.type !== "toolCall") continue;
- const args = block.arguments;
- const path = args && typeof args.path === "string" ? args.path : undefined;
+ const path = fileOperationPath(block.arguments);
if (!path) continue;
switch (block.name) {
case "read": fileOps.read.add(path); break;
diff --git a/packages/agent-runtime/src/runtime.test.ts b/packages/agent-runtime/src/runtime.test.ts
index 1c9f56213..b5303a758 100644
--- a/packages/agent-runtime/src/runtime.test.ts
+++ b/packages/agent-runtime/src/runtime.test.ts
@@ -8088,6 +8088,126 @@ describe("DesktopAgentRuntime per-turn context protection", () => {
await runtime.dispose();
});
+ it("records aliased path arguments on the checkpoint file lists", async () => {
+ const runtime = createRuntime();
+ const readAssistant = {
+ ...assistant,
+ content: [
+ {
+ type: "toolCall" as const,
+ id: "read-a",
+ name: "Read",
+ arguments: { file_path: "src/a.txt" },
+ },
+ ],
+ stopReason: "toolUse" as const,
+ };
+ const editAssistant = {
+ ...assistant,
+ timestamp: 3,
+ content: [
+ {
+ type: "toolCall" as const,
+ id: "edit-b",
+ name: "Edit",
+ arguments: { filepath: "src/b.ts" },
+ },
+ ],
+ stopReason: "toolUse" as const,
+ };
+ const writeAssistant = {
+ ...assistant,
+ timestamp: 5,
+ content: [
+ {
+ type: "toolCall" as const,
+ id: "write-c",
+ name: "Write",
+ arguments: { file: "src/c.ts" },
+ },
+ ],
+ stopReason: "toolUse" as const,
+ };
+ (runtime as any).fullEntries = [
+ {
+ type: "message",
+ id: "user-1",
+ seq: 0,
+ parentId: null,
+ timestamp: Date.parse("2026-07-28T00:00:00Z"),
+ message: { role: "user", content: "update the files", timestamp: 1 },
+ },
+ {
+ type: "message",
+ id: "read-assistant",
+ seq: 1,
+ parentId: "user-1",
+ timestamp: Date.parse("2026-07-28T00:00:01Z"),
+ message: readAssistant,
+ },
+ {
+ type: "message",
+ id: "edit-assistant",
+ seq: 2,
+ parentId: "read-assistant",
+ timestamp: Date.parse("2026-07-28T00:00:03Z"),
+ message: editAssistant,
+ },
+ {
+ type: "message",
+ id: "write-assistant",
+ seq: 3,
+ parentId: "edit-assistant",
+ timestamp: Date.parse("2026-07-28T00:00:05Z"),
+ message: writeAssistant,
+ },
+ {
+ type: "message",
+ id: "user-next",
+ seq: 4,
+ parentId: "write-assistant",
+ timestamp: Date.parse("2026-07-28T00:00:06Z"),
+ message: { role: "user", content: "next", timestamp: 6 },
+ },
+ ];
+ const entries = (runtime as any).entriesWithCompaction();
+ const budget = (runtime as any).contextBudget(
+ entries.map((entry: any) => entry.message),
+ );
+ // Larger than the history, so the calls sit in pi's retained tail and the
+ // shaped preparation is what records them.
+ const preparation = (runtime as any).prepareCompactionInput(
+ entries,
+ { ...budget, keepRecentTokens: 1_000_000 },
+ 20_000,
+ "completed_turn",
+ );
+
+ expect(preparation.ok).toBe(true);
+ expect([...preparation.value.fileOps.read]).toEqual(["src/a.txt"]);
+ expect([...preparation.value.fileOps.edited]).toEqual(["src/b.ts"]);
+ expect([...preparation.value.fileOps.written]).toEqual(["src/c.ts"]);
+
+ (runtime as any).models = {
+ completeSimple: async () =>
+ assistantMessage({ content: [{ type: "text", text: "Older work." }] }),
+ };
+ const summary = await (runtime as any).generateCompaction(
+ preparation.value,
+ new AbortController().signal,
+ );
+ expect(summary.ok).toBe(true);
+ expect(summary.value.details.readFiles).toEqual(["src/a.txt"]);
+ expect(summary.value.details.modifiedFiles).toEqual(["src/b.ts", "src/c.ts"]);
+ expect(summary.value.summary).toContain(
+ "<read-files>\nsrc/a.txt\n</read-files>",
+ );
+ expect(summary.value.summary).toContain(
+ "<modified-files>\nsrc/b.ts\nsrc/c.ts\n</modified-files>",
+ );
+ await runtime.dispose();
+ });
+
it("retains only the active user message and truncates it at the retention cap", async () => {
const runtime = createRuntime();
// Historical user messages are summarized. The active prompt is retained