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): guard the retained-tail file-op collection by executed calls
liuzixiang1fa106889fa0
diff --git a/packages/agent-runtime/src/pi-runtime-compaction-plan.ts b/packages/agent-runtime/src/pi-runtime-compaction-plan.tsindex 4f61fa014..86e982953 100644--- a/packages/agent-runtime/src/pi-runtime-compaction-plan.ts+++ b/packages/agent-runtime/src/pi-runtime-compaction-plan.ts@@ -200,7 +200,12 @@ function executedToolCallIds(messages: AgentMessage[]): Set<string> {* full range keeps those successes counted while still dropping calls with no* row or only an error row.*/-function executedToolCallIdsInEntries(entries: Entry[]): Set<string> {+/**+ * The full-range lookup for callers that collect a slice pi already guarded+ * elsewhere — a retained tail folded back into a shaped preparation, say —+ * so the same executed-call contract applies outside prepareCompaction.+ */+export function executedToolCallIdsInEntries(entries: Entry[]): Set<string> {const messages: AgentMessage[] = [];for (const entry of entries) {if (entry.type === "message") messages.push(entry.message);diff --git a/packages/agent-runtime/src/runtime.test.ts b/packages/agent-runtime/src/runtime.test.tsindex 90e1328ff..5e09ebfa1 100644--- a/packages/agent-runtime/src/runtime.test.ts+++ b/packages/agent-runtime/src/runtime.test.ts@@ -8443,11 +8443,37 @@ describe("DesktopAgentRuntime per-turn context protection", () => {timestamp: Date.parse("2026-07-28T00:00:05Z"),message: writeAssistant,},+ // Every executed call carries a non-error result row, the way+ // agent-loop stamps them; the shaped preparation's guard reads these.{type: "message",- id: "user-next",+ id: "read-result",seq: 4,+ parentId: "read-assistant",+ timestamp: Date.parse("2026-07-28T00:00:02Z"),+ message: { role: "toolResult", toolCallId: "read-a", toolName: "read", isError: false, content: [], details: {}, timestamp: 2 },+ },+ {+ type: "message",+ id: "edit-result",+ seq: 5,+ parentId: "edit-assistant",+ timestamp: Date.parse("2026-07-28T00:00:04Z"),+ message: { role: "toolResult", toolCallId: "edit-b", toolName: "edit", isError: false, content: [], details: {}, timestamp: 4 },+ },+ {+ type: "message",+ id: "write-result",+ seq: 6,parentId: "write-assistant",+ timestamp: Date.parse("2026-07-28T00:00:05.5Z"),+ message: { role: "toolResult", toolCallId: "write-c", toolName: "write", isError: false, content: [], details: {}, timestamp: 5 },+ },+ {+ type: "message",+ id: "user-next",+ seq: 7,+ parentId: "write-result",timestamp: Date.parse("2026-07-28T00:00:06Z"),message: { role: "user", content: "next", timestamp: 6 },},@@ -12407,6 +12433,84 @@ describe("DesktopAgentRuntime compaction request headers", () => {});});+describe("DesktopAgentRuntime retained-tail file operations (#2287 follow-up to #2276)", () => {+ /** The shape `prepareCompaction` returns, with a tail pi did not collect. */+ function preparationWithTail() {+ const tailCall = (id: string, path: string) => ({+ role: "assistant",+ content: [{ type: "toolCall", id, name: "read", 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,+ });+ return {+ messagesToSummarize: [+ { role: "user", content: [{ type: "text", text: "older task context" }], timestamp: 1 },+ ],+ turnPrefixMessages: [],+ retainedTail: [+ tailCall("call-1", "src/ran.ts"),+ tailCall("call-2", "src/no-row.ts"),+ tailCall("call-3", "src/failed.ts"),+ ],+ isSplitTurn: false,+ tokensBefore: 240_000,+ fileOps: {+ read: new Set<string>(),+ edited: new Set<string>(),+ written: new Set<string>(),+ },+ settings: { enabled: true, reserveTokens: 16_384, keepRecentTokens: 20_000 },+ };+ }++ it("keeps a retained tail's unexecuted calls out of the shaped file operations", async () => {+ const runtime = createRuntime({ provider });+ // The transcript pi's planner never saw: one call ran, one has no result+ // row (a Stop broke the batch), one keeps an error row. The lookup must+ // span the full transcript, since a tail call's result sits anywhere in it.+ (runtime as any).fullEntries = [+ {+ type: "message",+ id: "e1",+ parentId: null,+ seq: 0,+ timestamp: 3,+ message: { role: "toolResult", toolCallId: "call-1", isError: false, content: [], timestamp: 3 },+ },+ {+ type: "message",+ id: "e2",+ parentId: "e1",+ seq: 1,+ timestamp: 4,+ message: { role: "toolResult", toolCallId: "call-3", isError: true, content: [], timestamp: 4 },+ },+ ];++ const shaped = (runtime as any).codexShapedPreparation(+ preparationWithTail(),+ 0,+ "completed_turn",+ );++ expect([...shaped.fileOps.read]).toEqual(["src/ran.ts"]);+ expect([...shaped.fileOps.written]).toEqual([]);+ expect([...shaped.fileOps.edited]).toEqual([]);+ await runtime.dispose();+ });+});+describe("DesktopAgentRuntime compaction summary retry and sizing (#543, ADR 0282)", () => {/** The shape `prepareCompaction` returns for a single-turn history. */function preparation(messagesToSummarize: unknown[] = [diff --git a/packages/agent-runtime/src/runtime.ts b/packages/agent-runtime/src/runtime.tsindex 67e782e78..dfbff0552 100644--- a/packages/agent-runtime/src/runtime.ts+++ b/packages/agent-runtime/src/runtime.ts@@ -166,6 +166,7 @@ import { withExplicitRequired } from "./tool-schema.js";import { buildSessionContext } from "./session-context.js";import {collectMessageFileOperations,+ executedToolCallIdsInEntries,prepareCompaction,} from "./pi-runtime-compaction-plan.js";import { compact } from "./pi-runtime-compaction-summary.js";@@ -7307,14 +7308,20 @@ Do not invent objections or turn speculative risks into blockers. Stop when the// pi records file operations for `messagesToSummarize` and, on a split// turn, `turnPrefixMessages`. The tail is summarized too — including the// suffix a split left uncollected — so copy the sets and record it there.- // The sets pi returned stay unchanged.+ // The sets pi returned stay unchanged. The tail's own collection obeys+ // the executed-call contract those guarded slices follow: a mid-batch+ // Stop leaves trailing calls with no result row and failed calls keep+ // an error row — either way the call never touched its file. The+ // lookup spans the whole transcript because a tail call's result row+ // can sit anywhere in it.const fileOps: CompactionPreparation["fileOps"] = {read: new Set(preparation.fileOps.read),written: new Set(preparation.fileOps.written),edited: new Set(preparation.fileOps.edited),};+ const executedCallIds = executedToolCallIdsInEntries(this.fullEntries);for (const message of preparation.retainedTail) {- collectMessageFileOperations(message, fileOps);+ collectMessageFileOperations(message, fileOps, executedCallIds);}const latestUser = messagesToSummarize.filter((message): message is UserMessage => message.role === "user")