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(interactions): keep ask and permission cards when an answer fails
vastsa62af05bad0de
diff --git a/apps/desktop/src/stores/slices/interaction-slice.ts b/apps/desktop/src/stores/slices/interaction-slice.tsindex fb976f50e..72bb73fc5 100644--- a/apps/desktop/src/stores/slices/interaction-slice.ts+++ b/apps/desktop/src/stores/slices/interaction-slice.ts@@ -22,6 +22,15 @@ import { resolveSettingsTab } from "../../lib/settings-search";import type { StoreAccess } from "./types";const PLAN_APPROVAL_TIMEOUT = "PLAN_APPROVAL_TIMEOUT";++/** The answering side no longer holds this request (already settled, expired+ * or its runtime is gone), so retrying the same card cannot succeed. */+const INTERACTION_REQUEST_GONE = new Set(["NOT_FOUND", "ASKTOOL_NOT_FOUND"]);++function interactionRequestGone(error: unknown): boolean {+ const code = (error as { code?: unknown } | null)?.code;+ return typeof code === "string" && INTERACTION_REQUEST_GONE.has(code);+}const TOAST_DURATION_MS = 4000;const TOAST_ERROR_DURATION_MS = 8000;const TOAST_STACK_LIMIT = 4;@@ -138,34 +147,41 @@ export function createInteractionSlice({resolvePermission: async (sessionId, requestId, decision) => {const permission = headPermission(get().pendingPermissions, sessionId);if (!permission || permission.requestId !== requestId) return;+ const removeCard = () => set((state) => ({+ pendingPermissions: removePermission(+ state.pendingPermissions,+ sessionId,+ requestId,+ ),+ }));try {await api.resolvePermission({ requestId, decision });- } finally {- set((state) => ({- pendingPermissions: removePermission(- state.pendingPermissions,- sessionId,- requestId,- ),- }));+ } catch (error) {+ // A transient failure keeps the card so the user can retry. A request+ // the Host no longer holds already ended with its tool call; retrying+ // can never succeed, so the stale card is dropped without an error.+ if (!interactionRequestGone(error)) throw error;}+ removeCard();},resolveAsk: async (sessionId, resolution: AskToolResolution) => {const ask = headAsk(get().pendingAsks, sessionId);if (!ask || ask.requestId !== resolution.requestId) return;+ const removeCard = () => set((state) => ({+ pendingAsks: removeAsk(+ state.pendingAsks,+ sessionId,+ resolution.requestId,+ ),+ askDrafts: removeAskDraft(state.askDrafts, resolution.requestId),+ }));try {await api.resolveAskTool(resolution);- } finally {- set((state) => ({- pendingAsks: removeAsk(- state.pendingAsks,- sessionId,- resolution.requestId,- ),- askDrafts: removeAskDraft(state.askDrafts, resolution.requestId),- }));+ } catch (error) {+ if (!interactionRequestGone(error)) throw error;}+ removeCard();},setAskDraft: (requestId: string, draft: AskDraft) => {diff --git a/apps/desktop/test/interactive-response-retry.test.mjs b/apps/desktop/test/interactive-response-retry.test.mjsnew file mode 100644index 000000000..93f2e43fc--- /dev/null+++ b/apps/desktop/test/interactive-response-retry.test.mjs@@ -0,0 +1,78 @@+import assert from "node:assert/strict";+import { register } from "node:module";+import test from "node:test";+register(new URL("./helpers/ts-import-hooks.mjs", import.meta.url));+const { createInteractionSlice } = await import("../src/stores/slices/interaction-slice.ts");+const { api } = await import("../src/lib/api.ts");++// Based on the reproduction in #2235 by yg2224.++function fixture() {+ const request = { requestId: "request", sessionId: "session", toolCallId: "tool" };+ const next = { ...request, requestId: "next" };+ const draft = { index: 0, answers: [{ values: ["chosen"], customSelected: false, customText: "", skipped: false }] };+ let state = {+ pendingAsks: { session: [request, next] },+ pendingPermissions: { session: [request, next] },+ askDrafts: { request: draft },+ };+ const slice = createInteractionSlice({+ get: () => state,+ set: patch => { state = { ...state, ...(typeof patch === "function" ? patch(state) : patch) }; },+ });+ return { request, next, draft, slice, state: () => state };+}++function coded(code, message = code) {+ return Object.assign(new Error(message), { code });+}++for (const kind of ["ask", "permission"]) {+ const method = kind === "ask" ? "resolveAskTool" : "resolvePermission";+ const answer = slice => kind === "ask"+ ? slice.resolveAsk("session", { requestId: "request", sessionId: "session", answers: [["chosen"]] })+ : slice.resolvePermission("session", "request", "allow-once");+ const cards = state => kind === "ask" ? state.pendingAsks : state.pendingPermissions;++ test(`a failed ${kind} answer preserves its card and draft, and a retry settles it`, async t => {+ const f = fixture();+ t.mock.method(api, method, async () => { throw new Error("temporary transport failure"); });+ await assert.rejects(answer(f.slice), /transport failure/);+ assert.equal(cards(f.state()).session[0], f.request);+ assert.equal(f.state().askDrafts.request, f.draft);++ t.mock.method(api, method, async () => ({ ok: true }));+ await answer(f.slice);+ assert.deepEqual(cards(f.state()).session, [f.next]);+ if (kind === "ask") assert.equal(f.state().askDrafts.request, undefined);+ });++ for (const code of kind === "ask" ? ["ASKTOOL_NOT_FOUND", "NOT_FOUND"] : ["NOT_FOUND"]) {+ test(`a ${kind} answer for a request that is gone (${code}) drops the card without an error`, async t => {+ const f = fixture();+ t.mock.method(api, method, async () => { throw coded(code); });+ await answer(f.slice);+ assert.deepEqual(cards(f.state()).session, [f.next]);+ if (kind === "ask") assert.equal(f.state().askDrafts.request, undefined);+ });+ }+}++test("a late successful answer cannot remove a replacement request or its draft", async t => {+ const first = { requestId: "first", sessionId: "session", toolCallId: "tool" };+ const replacement = { ...first, requestId: "replacement" };+ const draft = { index: 0, answers: [] };+ let state = { pendingAsks: { session: [first] }, askDrafts: { first: draft } };+ const slice = createInteractionSlice({+ get: () => state,+ set: patch => { state = { ...state, ...(typeof patch === "function" ? patch(state) : patch) }; },+ });+ let finish;+ t.mock.method(api, "resolveAskTool", () => new Promise(resolve => { finish = resolve; }));+ const pending = slice.resolveAsk("session", { requestId: "first", answers: [null] });+ state = { pendingAsks: { session: [replacement] }, askDrafts: { replacement: draft } };+ finish({ ok: true });+ await pending;+ assert.deepEqual(state.pendingAsks.session, [replacement]);+ assert.equal(state.askDrafts.replacement, draft);+});diff --git a/docs/spec/04-ux/08-component-spec.md b/docs/spec/04-ux/08-component-spec.mdindex a5b3f8aef..20e3c0933 100644--- a/docs/spec/04-ux/08-component-spec.md+++ b/docs/spec/04-ux/08-component-spec.md@@ -2978,6 +2978,12 @@ fields. It is never a JSON dump.- Buttons: primary (Allow once), secondary (Allow session), danger (Deny)- No countdown is shown; the card remains pending until an explicit decision orcancellation.+- A decision that fails to reach the Host keeps the card pending with its+ buttons re-enabled and shows the error, so the same request can be retried.+ If the Host reports the request no longer exists (`NOT_FOUND`), the card is+ removed without an error. AskTool cards follow the same rule, keeping their+ answer draft on a retriable failure; `ASKTOOL_NOT_FOUND` and `NOT_FOUND` mean+ the request is gone.- The first action locks all buttons. Resolution errors use an error toast;successful or failed completion returns focus to the current composer.- The originating session's composer cannot send during pending permission,diff --git a/docs/zh-CN/spec/04-ux/08-component-spec.md b/docs/zh-CN/spec/04-ux/08-component-spec.mdindex 243461b8d..feafd5548 100644--- a/docs/zh-CN/spec/04-ux/08-component-spec.md+++ b/docs/zh-CN/spec/04-ux/08-component-spec.md@@ -1970,6 +1970,7 @@ pi-ai 结果信封携带 `details` 中的结构化有效负载并重复它- 按钮:主要(允许一次)、次要(允许会话)、危险(拒绝)- 不显示倒计时;卡片会一直保持待处理,直到明确决定或取消。+- 决定未能送达 Host 时,卡片保持待处理、按钮恢复可用并显示错误,可重试同一请求。Host 报告请求已不存在(`NOT_FOUND`)时直接移除卡片,不提示错误。AskTool 卡片规则相同,可重试的失败保留回答草稿;`ASKTOOL_NOT_FOUND` 与 `NOT_FOUND` 表示请求已不存在。- 第一个操作锁定所有按钮。解决错误使用错误提示;成功或失败的完成会将焦点返回到当前输入框。- 原始会话的输入框无法在等待许可期间发送,