Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins
TypeScript ai-agentcoding-agentdesktop-appelectronglobali18nlocal-firstmcppipi-agentpi-desktoppluginsreactrusttypescript
fix(agent-runtime): feed pi's file-op collector the names it reads
vastsa0f7ebc33f868
diff --git a/docs/spec/03-runtime/02-agent-runtime.md b/docs/spec/03-runtime/02-agent-runtime.mdindex 1a5090e81..6ad3ff082 100644--- a/docs/spec/03-runtime/02-agent-runtime.md+++ b/docs/spec/03-runtime/02-agent-runtime.md@@ -575,6 +575,15 @@ be left with the guard off and no way to restore it. Manual `/compact` remainsavailable while the session is idle. Checkpoint generation is abortable andcounts as running state until durable persistence completes.+The file list a checkpoint carries is read out of the summarized range by pi's+own collector, which recognizes the lowercase spellings `read` / `write` /+`edit` — the names pi's tools carry. PI-Desktop registers `Read` / `Write` /+`Edit`, so the runtime converts exactly those three on the way into pi's+preparation (`withPiFileOpToolNames`): nothing stored changes, and every other+tool name is left spelled the way we register it. Without that conversion a+checkpoint's `readFiles` / `modifiedFiles` and the `<read-files>` section of a+summary were always empty (D618).+A delegate (§5f) runs the same derivation against its own resolved model andcompacts at its own turn boundaries, without a durable checkpoint chain ofits own (ADR 0299).diff --git a/docs/spec/08-meta/decisions-log.md b/docs/spec/08-meta/decisions-log.mdindex 380b89801..7587bfb4d 100644--- a/docs/spec/08-meta/decisions-log.md+++ b/docs/spec/08-meta/decisions-log.md@@ -6715,3 +6715,20 @@ that was sitting at the bottom — including after the turn had finished.checkpoint reports the summed usage of the requests that produced it. Only anempty range, or one past that request bound, still falls back on budgetgrounds. See ADR 0302, `03-runtime/02-agent-runtime.md`, ADR 0049, ADR 0282.++## 2026-09-22 — pi's file-op collector is fed the names it reads (D618, issue #827)++- A checkpoint's `readFiles` / `modifiedFiles`, and the `<read-files>` section a+ summary appends, come from pi's own `extractFileOpsFromMessage`, which switches+ on the lowercase names `read` / `write` / `edit` — the names pi's tools carry.+ PI-Desktop registers `Read` / `Write` / `Edit`, so the collector matched+ nothing and every checkpoint reported an empty file list; issue #827 turned+ this up while reading a compaction report.+- The conversion stays on our side of the boundary. `withPiFileOpToolNames`+ copies the entries handed to pi's `prepareCompaction` with exactly those three+ names respelled; no stored byte changes — a transcript, a checkpoint and a+ rebuilt context keep our names — and the input array comes back untouched when+ no such call is present, so the common path allocates nothing.+- Only those three are converted. pi's collector reads nothing else, so `Grep`,+ `Glob`, `Bash`, plugin and MCP names keep the spelling we register, and the+ summarized text changes only where pi consumes the name.diff --git a/docs/zh-CN/spec/03-runtime/02-agent-runtime.md b/docs/zh-CN/spec/03-runtime/02-agent-runtime.mdindex c65d10e2f..02b884b0e 100644--- a/docs/zh-CN/spec/03-runtime/02-agent-runtime.md+++ b/docs/zh-CN/spec/03-runtime/02-agent-runtime.md@@ -438,6 +438,13 @@ Headroom 是 16,384 个代币储备底线的最大值,模型最大输出会话空闲时可用。检查点生成是可中止的并且计为运行状态,直到持久持久性完成。+检查点携带的文件清单由 pi 自己的收集器从被摘要的区间里读出,它只认小写拼写+`read` / `write` / `edit`——也就是 pi 自己工具的名字。PI-Desktop 注册的是+`Read` / `Write` / `Edit`,因此运行时**只在交给 pi 的准备阶段**转换这三个名字+(`withPiFileOpToolNames`):存储内容不变,其它工具名一律保持我们注册的拼写。+缺少这一步时,检查点的 `readFiles` / `modifiedFiles` 与摘要里的 `<read-files>` 段+恒为空(D618)。+委托(第 5f 节)对照它自己解析出的模型走同一条推导,并在它自己的回合边界上压缩,但没有属于它自己的持久检查点链(ADR 0299)。diff --git a/docs/zh-CN/spec/08-meta/decisions-log.md b/docs/zh-CN/spec/08-meta/decisions-log.mdindex ae5077a49..7e7990913 100644--- a/docs/zh-CN/spec/08-meta/decisions-log.md+++ b/docs/zh-CN/spec/08-meta/decisions-log.md@@ -4765,3 +4765,15 @@ that amendment are retired by ADR 0268; the upstream work-panel lifecycle stays.- 单次缩减后仍过大的摘要提示不再被跳过:范围会被切成每片都能放下的连续分片,最多 16 次请求,每次携带上一片的摘要,检查点报告这些请求的用量总和。只有空范围或超过该请求上限的范围才因预算原因回退。见 ADR 0302、`03-runtime/02-agent-runtime.md`、ADR 0049、ADR 0282。++## 2026-09-22 —— 交给 pi 的文件操作收集器使用它认识的拼写(D618,issue #827)++- 检查点的 `readFiles` / `modifiedFiles` 以及摘要追加的 `<read-files>` 段来自 pi 自己的+ `extractFileOpsFromMessage`,它只匹配小写名字 `read` / `write` / `edit`——即 pi 自己+ 工具的名字。PI-Desktop 注册的是 `Read` / `Write` / `Edit`,因此该收集器什么都匹配不到,+ 每个检查点都报出空文件清单;issue #827 在排查压缩报告时发现了这一点。+- 转换只发生在边界这一侧:`withPiFileOpToolNames` 把交给 pi `prepareCompaction` 的条目+ 复制一份,并改写这三个名字的拼写。存储字节不变——transcript、检查点与重建后的上下文都+ 保留我们的拼写——而且当历史里没有这类调用时原样返回入参数组,常见路径不产生任何分配。+- 只转换这三个名字。pi 的收集器不读其它名字,因此 `Grep`、`Glob`、`Bash`、插件与 MCP 名字+ 保持我们注册的拼写,被摘要的文本只在 pi 真正消费该名字的地方发生变化。diff --git a/packages/agent-runtime/src/pi-file-ops.test.ts b/packages/agent-runtime/src/pi-file-ops.test.tsnew file mode 100644index 000000000..161c834ee--- /dev/null+++ b/packages/agent-runtime/src/pi-file-ops.test.ts@@ -0,0 +1,135 @@+import { prepareCompaction, type AgentMessage, type Entry } from "@earendil-works/pi-agent-core";+import { describe, expect, it } from "vitest";++import { withPiFileOpToolNames } from "./pi-file-ops.js";++function toolCallMessage(name: string, path: string, id = "call-1"): AgentMessage {+ return {+ role: "assistant",+ content: [{ type: "toolCall", id, name, arguments: { path } }],+ api: "openai-completions",+ provider: "local",+ model: "local-model",+ usage: {+ input: 1,+ output: 1,+ cacheRead: 0,+ cacheWrite: 0,+ totalTokens: 2,+ cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },+ },+ stopReason: "toolUse",+ timestamp: 2,+ } as AgentMessage;+}++function userMessage(text: string, timestamp: number): AgentMessage {+ return { role: "user", content: [{ type: "text", text }], timestamp } as AgentMessage;+}++function messageEntry(id: string, message: AgentMessage, second: number): Entry {+ return {+ type: "message",+ id,+ parentId: null,+ timestamp: `2026-09-22T00:00:0${second}.000Z`,+ message,+ } as Entry;+}++function nameOf(entry: Entry): unknown {+ const message = entry.message as { content?: Array<{ name?: unknown }> } | undefined;+ return message?.content?.[0]?.name;+}++/** Small enough that the history ends up in `messagesToSummarize`. */+const settings = { enabled: true, reserveTokens: 16_384, keepRecentTokens: 1 };++describe("withPiFileOpToolNames", () => {+ it("spells the tool calls pi's file-op collector reads the way pi spells them", () => {+ const entries = [+ messageEntry("a1", toolCallMessage("Read", "src/a.txt"), 1),+ messageEntry("a2", toolCallMessage("Write", "src/b.txt", "call-2"), 2),+ messageEntry("a3", toolCallMessage("Edit", "src/c.txt", "call-3"), 3),+ ];++ const mapped = withPiFileOpToolNames(entries);++ expect(mapped.map(nameOf)).toEqual(["read", "write", "edit"]);+ });++ it("leaves every other tool name and every other message untouched", () => {+ const entries = [+ messageEntry("a1", toolCallMessage("Grep", "src/a.txt"), 2),+ messageEntry("a2", toolCallMessage("Bash", "src/b.txt", "call-2"), 3),+ messageEntry("a3", toolCallMessage("plugin_x_tool", "src/c.txt", "call-3"), 4),+ ];++ // Nothing needed converting, so the array comes back as it was handed in.+ expect(withPiFileOpToolNames(entries)).toBe(entries);+ expect(entries.map(nameOf)).toEqual(["Grep", "Bash", "plugin_x_tool"]);+ });++ it("copies instead of mutating what it is handed", () => {+ const entries = [messageEntry("a1", toolCallMessage("Read", "src/a.txt"), 1)];++ const mapped = withPiFileOpToolNames(entries);++ expect(mapped).not.toBe(entries);+ expect(mapped[0]).not.toBe(entries[0]);+ // The stored history keeps our spelling.+ expect(nameOf(entries[0])).toBe("Read");+ });++ it("survives entries without a message and messages without content blocks", () => {+ const entries = [+ { type: "compaction", id: "c1", timestamp: "2026-09-22T00:00:01.000Z" } as Entry,+ messageEntry("a1", { role: "assistant", content: [] } as unknown as AgentMessage, 2),+ messageEntry("a2", { role: "toolResult", toolCallId: "call-1" } as unknown as AgentMessage, 3),+ ];++ expect(withPiFileOpToolNames(entries)).toBe(entries);+ });+});++describe("pi's file-op collection through the adapter", () => {+ const entries = [+ messageEntry("u1", userMessage("read src/a.txt and then edit src/a.ts", 1), 1),+ messageEntry("a1", toolCallMessage("Read", "src/a.txt"), 2),+ messageEntry("a2", toolCallMessage("Edit", "src/a.ts", "call-2"), 3),+ messageEntry("u2", userMessage("latest question", 4), 4),+ ];++ it("collects the files a checkpoint used to report as empty", () => {+ const prepared = prepareCompaction(withPiFileOpToolNames(entries), settings);+ const preparation = prepared.value;++ expect(preparation).toBeDefined();+ expect([...preparation!.fileOps.read]).toEqual(["src/a.txt"]);+ expect([...preparation!.fileOps.edited]).toEqual(["src/a.ts"]);+ });++ it("collects nothing from the same history without the adapter", () => {+ // The regression this adapter exists for: pi's collector switches on+ // `read` / `write` / `edit`, so our capitalized names matched nothing.+ const preparation = prepareCompaction(entries, settings).value;++ expect(preparation).toBeDefined();+ expect(preparation!.fileOps.read.size).toBe(0);+ expect(preparation!.fileOps.edited.size).toBe(0);+ expect(preparation!.fileOps.written.size).toBe(0);+ });++ it("still names the summarized tool calls, so the request text changes only in spelling", () => {+ const preparation = prepareCompaction(withPiFileOpToolNames(entries), settings).value;+ const names = preparation!.messagesToSummarize+ .filter((message) => message.role === "assistant")+ .flatMap((message) =>+ (message.content as Array<{ type: string; name?: string }>)+ .filter((block) => block.type === "toolCall")+ .map((block) => block.name),+ );++ expect(names).toEqual(["read", "edit"]);+ });+});diff --git a/packages/agent-runtime/src/pi-file-ops.ts b/packages/agent-runtime/src/pi-file-ops.tsnew file mode 100644index 000000000..482dadef6--- /dev/null+++ b/packages/agent-runtime/src/pi-file-ops.ts@@ -0,0 +1,61 @@+import type { Entry } from "@earendil-works/pi-agent-core";++import { isRecord } from "./agent-messages.js";++/**+ * The tool names pi's own compaction inspects, mapped from ours to pi's+ * spelling.+ *+ * `extractFileOpsFromMessage` in `@earendil-works/pi-coding-agent` switches on+ * the lowercase names `read` / `write` / `edit`, because those are the names+ * pi's own tools carry. PI-Desktop registers `Read` / `Write` / `Edit`, so that+ * collector matched nothing: a checkpoint's `readFiles` / `modifiedFiles` and+ * the `<read-files>` section of a summary were always empty (issue #827 turned+ * this up while reading a compaction report).+ *+ * Only these three are converted, and only on the way into pi's preparation.+ * There is nothing to convert for `grep`, `glob` or `bash`: pi's collector does+ * not read them, so leaving them spelled the way we register them keeps the+ * summarized conversation text unchanged for every other tool.+ */+export const PI_FILE_OP_TOOL_NAMES: Readonly<Record<string, string>> = {+ Read: "read",+ Write: "write",+ Edit: "edit",+};++/**+ * Copy `entries`, spelling the tool calls pi's file-op collector reads the way+ * pi spells them.+ *+ * The stored history keeps our names: this returns copies and never mutates the+ * entries it is handed, so a transcript, a checkpoint and a rebuilt context are+ * unaffected. The input array itself is returned when nothing needed+ * converting, so a history without those calls costs nothing.+ */+export function withPiFileOpToolNames(entries: Entry[]): Entry[] {+ let changed = false;+ const mapped = entries.map((entry) => {+ const message = (entry as { message?: unknown }).message;+ if (+ !isRecord(message) ||+ message.role !== "assistant" ||+ !Array.isArray(message.content)+ ) {+ return entry;+ }+ let messageChanged = false;+ const content = message.content.map((block: unknown) => {+ if (!isRecord(block) || block.type !== "toolCall") return block;+ const name = typeof block.name === "string" ? block.name : undefined;+ const piName = name ? PI_FILE_OP_TOOL_NAMES[name] : undefined;+ if (!piName) return block;+ messageChanged = true;+ return { ...block, name: piName };+ });+ if (!messageChanged) return entry;+ changed = true;+ return { ...entry, message: { ...message, content } } as Entry;+ });+ return changed ? mapped : entries;+}diff --git a/packages/agent-runtime/src/runtime.ts b/packages/agent-runtime/src/runtime.tsindex 7e307dc30..53faef906 100644--- a/packages/agent-runtime/src/runtime.ts+++ b/packages/agent-runtime/src/runtime.ts@@ -2,6 +2,7 @@ import { restoreHostedSearchReplay } from "./hosted-search-replay.js";import { readLocalRequestErrorDetails } from "./local-request-errors.js";import { imageGenerationDescription, imageGenerationParameters } from "./image-generation/tool.js";import { scheduledToolParameters, scheduledToolDescriptions } from "./scheduled-tools.js";+import { withPiFileOpToolNames } from "./pi-file-ops.js";import { randomUUID } from "node:crypto";import {settledDelegationMessage,@@ -5923,7 +5924,7 @@ Delegation rules:retainedUserTokens = this.retainedUserMessageBudget(budget),retentionMode: CompactionRetentionMode = "completed_turn",) {- const prepared = prepareCompaction(entries, {+ const prepared = prepareCompaction(withPiFileOpToolNames(entries), {enabled: this.compactionEnabled,reserveTokens: budget.requestHeadroom,keepRecentTokens: budget.keepRecentTokens,