Skip to content

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

fix(agent-runtime): leave unexecuted calls from failed turns out of compaction file ops

xpeng5278-web8dfd8db57aa7
diff --git a/docs/spec/03-runtime/02-agent-runtime.md b/docs/spec/03-runtime/02-agent-runtime.md
index bc9c3731c..743bf410d 100644
--- a/docs/spec/03-runtime/02-agent-runtime.md
+++ b/docs/spec/03-runtime/02-agent-runtime.md
@@ -719,6 +719,9 @@ 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.
+Calls on an assistant that stopped with `error`, `aborted`, or `deferred` are
+not counted: pi returns before executing them, and the rebuilt context drops
+the same turns, so they never read or changed a file.
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 6dd235c5b..f3e3d9c04 100644
--- a/packages/agent-runtime/src/pi-file-ops.test.ts
+++ b/packages/agent-runtime/src/pi-file-ops.test.ts
@@ -1,5 +1,8 @@
import type { AgentMessage } from "@earendil-works/pi-agent-core";
-import { prepareCompaction } from "./pi-runtime-compaction-plan.js";
+import {
+ collectMessageFileOperations,
+ prepareCompaction,
+} from "./pi-runtime-compaction-plan.js";
import type { Entry } from "./pi-runtime-types.js";
import { describe, expect, it } from "vitest";
@@ -9,6 +12,7 @@ function toolCallWithArguments(
name: string,
args: Record<string, unknown>,
id = "call-1",
+ stopReason: "toolUse" | "error" | "aborted" | "deferred" = "toolUse",
): AgentMessage {
return {
role: "assistant",
@@ -24,7 +28,7 @@ function toolCallWithArguments(
totalTokens: 2,
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
},
- stopReason: "toolUse",
+ stopReason,
timestamp: 2,
} as AgentMessage;
}
@@ -246,4 +250,57 @@ describe("pi's file-op collection through the adapter", () => {
expect([...preparation!.fileOps.written]).toEqual([]);
expect([...preparation!.fileOps.edited]).toEqual(["src/lower.ts"]);
});
+
+ it("leaves an aborted turn's write out of file ops and still records a toolUse edit", () => {
+ // A stopped turn keeps its toolCall block, but pi never runs it. The
+ // completed edit in the same session did run and must stay listed.
+ const entries = [
+ messageEntry("u1", userMessage("edit src/real.ts", 1), 1),
+ messageEntry("a1", toolCallWithArguments("Edit", { path: "src/real.ts" }, "call-1"), 2),
+ messageEntry("u2", userMessage("write src/never.ts", 3), 3),
+ messageEntry(
+ "a2",
+ toolCallWithArguments("Write", { path: "src/never.ts", content: "x" }, "call-2", "aborted"),
+ 4,
+ ),
+ messageEntry("u3", userMessage("latest question", 5), 5),
+ ];
+
+ const preparation = preparationOf(entries, true);
+
+ expect(preparation).toBeDefined();
+ expect([...preparation!.fileOps.written]).toEqual([]);
+ expect([...preparation!.fileOps.edited]).toEqual(["src/real.ts"]);
+ });
+});
+
+describe("collectMessageFileOperations", () => {
+ it("skips read, write, and edit calls on aborted, error, and deferred turns", () => {
+ const ops = {
+ read: new Set<string>(),
+ written: new Set<string>(),
+ edited: new Set<string>(),
+ };
+
+ collectMessageFileOperations(
+ toolCallWithArguments("write", { path: "src/never.ts", content: "x" }, "c1", "aborted"),
+ ops,
+ );
+ collectMessageFileOperations(
+ toolCallWithArguments("write", { path: "src/errored.ts", content: "x" }, "c2", "error"),
+ ops,
+ );
+ collectMessageFileOperations(
+ toolCallWithArguments("read", { path: "src/deferred.ts" }, "c3", "deferred"),
+ ops,
+ );
+ collectMessageFileOperations(
+ toolCallWithArguments("edit", { path: "src/done.ts" }, "c4", "toolUse"),
+ ops,
+ );
+
+ expect([...ops.written]).toEqual([]);
+ expect([...ops.read]).toEqual([]);
+ expect([...ops.edited]).toEqual(["src/done.ts"]);
+ });
});
diff --git a/packages/agent-runtime/src/pi-runtime-compaction-plan.ts b/packages/agent-runtime/src/pi-runtime-compaction-plan.ts
index 296a84dac..832fdd90a 100644
--- a/packages/agent-runtime/src/pi-runtime-compaction-plan.ts
+++ b/packages/agent-runtime/src/pi-runtime-compaction-plan.ts
@@ -145,12 +145,33 @@ function fileOperationPath(args: unknown): string | undefined {
return typeof value === "string" && value.length > 0 ? value : undefined;
}
+/**
+ * Assistant stop reasons whose tool calls never ran.
+ *
+ * pi-agent-core returns before `executeToolCalls` when a turn stops with
+ * `error` or `aborted`, so those tool results stay empty. `deferred` is the
+ * third reason compaction-tail drops from the rebuilt context
+ * (`DROPPED_STOP_REASONS`); session-context and subagent-context treat the
+ * same three rows as transcript-only. The toolCall blocks stay on the
+ * message anyway, often cut off mid-argument. `messageForSummary` does not
+ * look at stop reason, so `messagesToSummarize` and a split turn's prefix
+ * still contain them. Counting the paths would list files that were never
+ * read or written in the checkpoint's `readFiles` / `modifiedFiles` and in
+ * the summary prompt, and every later compaction copies that list forward.
+ */
+const UNEXECUTED_ASSISTANT_STOP_REASONS: ReadonlySet<string> = new Set([
+ "error",
+ "aborted",
+ "deferred",
+]);
+
/** Record one assistant message's Read/Write/Edit paths. Mutates `fileOps`. */
export function collectMessageFileOperations(
message: AgentMessage,
fileOps: FileOperations,
): void {
if (message.role !== "assistant" || !Array.isArray(message.content)) return;
+ if (UNEXECUTED_ASSISTANT_STOP_REASONS.has(message.stopReason)) return;
for (const block of message.content) {
if (!block || block.type !== "toolCall") continue;
const path = fileOperationPath(block.arguments);