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): release in-flight tools from the stop itself
liubai984367febf6f
diff --git a/docs/adr/tool-abort-abandons-host-call.md b/docs/adr/tool-abort-abandons-host-call.mdindex 5f4d571b5..c2c8e5ca7 100644--- a/docs/adr/tool-abort-abandons-host-call.md+++ b/docs/adr/tool-abort-abandons-host-call.md@@ -40,10 +40,23 @@ In both cases the wait is a `Promise.race` against the execute call. Losing therace abandons the wait, not the host call: the orphaned `tools.execute` settleson its own, its result is discarded, and the runtime never surfaces it.+The runtime also drives the tools from `abort()` itself, not only from the+signal each tool was handed. `abort()` walks the in-flight tool calls and runs+the same handler the signal would. Relying on the signal alone was not enough:+a stop can reach the runtime without reaching the tool, and then `abort()` stays+blocked on the turn settlement, which keeps the `agent.abort` RPC pending. The+renderer awaits that RPC before it clears "processing", so the session stayed+pinned even after the turn was over in the ledger. One field report showed+exactly that: three turns recorded `aborted` while the tool's process was still+alive minutes later.+## Consequences- Stop always ends the turn, so a stuck tool can no longer block new turns,compaction, or session switching.+- A stop releases every in-flight tool, including one whose own abort signal+ never fired, so the stop RPC always returns and the renderer always leaves+ "processing".- A non-cancellable tool keeps consuming host resources until it finishes. Thatis honest residual work, not a stuck UI, and it is why the durable fix stayswith host-core: teach `tools.execute` an abort token so `Grep` and friends candiff --git a/docs/spec/03-runtime/03-tools-and-permissions.md b/docs/spec/03-runtime/03-tools-and-permissions.mdindex a24e5f4d9..5aac0a098 100644--- a/docs/spec/03-runtime/03-tools-and-permissions.md+++ b/docs/spec/03-runtime/03-tools-and-permissions.md@@ -125,7 +125,10 @@ return the output it collected before the kill. Every other tool has nohost-side cancellation: the runtime stops waiting the moment the signal fires,returns `TOOL_ABORTED`, and leaves the host call to settle on its own with itsresult discarded. Abandoned calls keep consuming host resources until they-finish, and a retry must carry a fresh tool call id.+finish, and a retry must carry a fresh tool call id. The stop releases the+in-flight tools directly as well, so it still ends the turn when a tool's own+abort signal never fires; the stop RPC must return even in that case, because+the renderer waits for it before it leaves the running state.`JevClassify` is an external provider operation rather than a workspaceexecution tool. It validates its structured request, observes cancellation,diff --git a/packages/agent-runtime/src/runtime.test.ts b/packages/agent-runtime/src/runtime.test.tsindex 2165814a0..b59808034 100644--- a/packages/agent-runtime/src/runtime.test.ts+++ b/packages/agent-runtime/src/runtime.test.ts@@ -1917,6 +1917,31 @@ describe("DesktopAgentRuntime configuration matching", () => {vi.useRealTimers();}});++ it("releases an in-flight tool when the stop lands without the tool signal firing", async () => {+ const host = {+ call: vi.fn((method: string) =>+ method === "tools.execute"+ ? new Promise<never>(() => undefined)+ : Promise.resolve({ ok: true, content: "abort requested" }),+ ),+ onNotification: vi.fn(() => () => undefined),+ };+ const runtime = createRuntime({ host });+ const grep = (runtime as any).toolCatalog.get("Grep");+ // A signal that never fires stands in for the field case: the stop reached+ // the runtime but not the tool, so the executor kept awaiting a host call+ // that would not return and the abort RPC never came back.+ const silent = new AbortController();+ const pending = grep.execute("grep-silent", { query: "needle" }, silent.signal);+ await new Promise<void>((resolve) => setImmediate(resolve));++ await runtime.abort();++ await expect(pending).rejects.toMatchObject({ errorCode: "TOOL_ABORTED" });+ expect(silent.signal.aborted).toBe(false);+ await runtime.dispose();+ });});describe("DesktopAgentRuntime live activity", () => {diff --git a/packages/agent-runtime/src/runtime.ts b/packages/agent-runtime/src/runtime.tsindex 49a0bce10..cfe952871 100644--- a/packages/agent-runtime/src/runtime.ts+++ b/packages/agent-runtime/src/runtime.ts@@ -2012,6 +2012,12 @@ export class DesktopAgentRuntime {private contextReminderClaimed = false;private contextFallbackReminderClaimed = false;private activeToolProgressCleanups = new Set<(flush: boolean) => void>();+ /**+ * Abort handlers for the tool calls currently awaiting `tools.execute`.+ * `abort()` drives these directly: a tool must be released even when the+ * signal it was handed never fires, or the stop RPC stays pending forever.+ */+ private activeToolAborts = new Set<() => void>();private hostCloseUnsubscribe?: () => void;private turnSubagentUsage?: MessageUsage;private compactionRequestShape?: CompactionRequestShape;@@ -3529,6 +3535,7 @@ Do not invent objections or turn speculative risks into blockers. Stop when theunsubscribeOutput?.();signal?.removeEventListener("abort", abort);this.activeToolProgressCleanups.delete(cleanup);+ this.activeToolAborts.delete(abort);};this.activeToolProgressCleanups.add(cleanup);if (signal?.aborted) {@@ -3540,6 +3547,7 @@ Do not invent objections or turn speculative risks into blockers. Stop when theif (signal) {signal.addEventListener("abort", abort, { once: true });}+ this.activeToolAborts.add(abort);let result: {ok: boolean;@@ -9152,6 +9160,14 @@ Do not invent objections or turn speculative risks into blockers. Stop when thethis.providerRetryAbort?.abort();if (this.compactionInProgress) this.compactionAborted = true;this.compactionAbort?.abort();+ // `agent.abort()` normally fires the signal each in-flight tool was handed,+ // and the tool executor reacts to that. When the signal does not reach a+ // tool, the executor keeps awaiting `tools.execute`, the turn never+ // settles, and this method never returns. The renderer awaits this RPC+ // before it clears the "processing" state, so one unyielding tool pinned+ // the whole session until it finished on its own (#1767). Drive the tools+ // from here as well, so a stop always releases them.+ for (const abortTool of [...this.activeToolAborts]) abortTool();await settlement;}