Skip to content

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

fix(desktop): resolve ask cards through the Host input path

mingkun968c5c77cae76a6
diff --git a/apps/desktop/electron/main/agent-host-bridge.ts b/apps/desktop/electron/main/agent-host-bridge.ts
index cd4c7cb56..40f6b98aa 100644
--- a/apps/desktop/electron/main/agent-host-bridge.ts
+++ b/apps/desktop/electron/main/agent-host-bridge.ts
@@ -369,6 +369,29 @@ export function createAgentHostBridge(options: AgentHostBridgeOptions) {
context: { requestId: `live-voice:${input.sessionId}` },
}));
},
+ /**
+ * Resolve a Composer ask card through the Host-owned input path, matched
+ * by the runtime request id the card carries. Returns `null` when this
+ * Host holds no matching open input so the caller falls back to the
+ * direct sidecar resolve; otherwise the pending input is deleted before
+ * the sidecar settles, so a later `pendingInteractiveRequests` read (for
+ * example after switching windows back to the session) no longer
+ * resurrects the already answered card.
+ */
+ async resolveAskByRequestId(resolution: AskToolResolution): Promise<{ ok: boolean } | null> {
+ const sessionId = String(resolution?.sessionId ?? "").trim();
+ const requestId = String(resolution?.requestId ?? "").trim();
+ if (!sessionId || !requestId) return null;
+ const entry = agentHost.pendingInputRequests(sessionId)
+ .find((candidate) => candidate.original.requestId === requestId);
+ if (!entry) return null;
+ await forIpc(() => agentHost.respondInput(DESKTOP_PRINCIPAL, {
+ inputId: entry.input.id,
+ answers: resolution.answers,
+ context: { requestId },
+ }));
+ return { ok: true };
+ },
lookupWorkAdmission(request: {
sessionId: string;
idempotencyKey: string;
diff --git a/apps/desktop/electron/main/ipc/agent-ipc.ts b/apps/desktop/electron/main/ipc/agent-ipc.ts
index 473c47051..1b9d1befc 100644
--- a/apps/desktop/electron/main/ipc/agent-ipc.ts
+++ b/apps/desktop/electron/main/ipc/agent-ipc.ts
@@ -837,6 +837,17 @@ export function registerAgentIpc({
const sessionId = String(resolution?.sessionId ?? "").trim();
const requestId = String(resolution?.requestId ?? "").trim();
if (!sessionId || !requestId) throw new Error("asktool resolution identity required");
+ // Prefer the Host-owned input path when it still holds this ask: it
+ // deletes the pending input before settling the sidecar, so switching
+ // windows back to the session cannot resurrect the answered card via
+ // `pendingInteractiveRequests`. Unknown requests keep the direct
+ // sidecar resolve for compatibility.
+ const settled = await agentHostBridge?.resolveAskByRequestId({
+ ...resolution,
+ sessionId,
+ requestId,
+ });
+ if (settled) return settled;
return sidecar.call("asktool.resolve", {
...resolution,
sessionId,
diff --git a/apps/desktop/test/asktool-resolve-clears-host-input.test.mjs b/apps/desktop/test/asktool-resolve-clears-host-input.test.mjs
new file mode 100644
index 000000000..f43589d01
--- /dev/null
+++ b/apps/desktop/test/asktool-resolve-clears-host-input.test.mjs
@@ -0,0 +1,102 @@
+import assert from "node:assert/strict";
+import test from "node:test";
+import { dirname } from "node:path";
+import { fileURLToPath } from "node:url";
+import { createServer } from "vite";
+import { IPC } from "@pi-desktop/shared";
+
+const here = dirname(fileURLToPath(import.meta.url));
+
+const askRequest = {
+ requestId: "ask_1",
+ sessionId: "s1",
+ toolCallId: "c9",
+ questions: [
+ {
+ question: "Which?",
+ options: [
+ { label: "a", description: "First choice" },
+ { label: "b", description: "Second choice" },
+ ],
+ },
+ ],
+};
+
+function ingestAsk(bridge) {
+ bridge.ingest({ sessionId: "s1", turnId: "rt_1", ts: Date.now(), event: { type: "agent_start" } });
+ bridge.ingest({
+ sessionId: "s1",
+ turnId: "rt_1",
+ ts: Date.now(),
+ event: { type: "asktool_request", request: askRequest },
+ });
+}
+
+test("resolveAskByRequestId settles the Host input so the card does not come back", async (t) => {
+ const server = await createServer({
+ root: dirname(here),
+ configFile: false,
+ server: { middlewareMode: true, hmr: false, ws: false },
+ appType: "custom",
+ optimizeDeps: { noDiscovery: true, include: [] },
+ });
+ t.after(() => server.close());
+ const { createAgentHostBridge } = await server.ssrLoadModule("/electron/main/agent-host-bridge.ts");
+ const calls = [];
+ const bridge = createAgentHostBridge({
+ channels: IPC.invoke,
+ getHost: () => null,
+ log: () => undefined,
+ async invoke(channel, args) {
+ calls.push({ channel, args });
+ return { ok: true };
+ },
+ });
+
+ ingestAsk(bridge);
+ assert.equal((await bridge.pendingInteractiveRequests("s1")).asks.length, 1);
+
+ const settled = await bridge.resolveAskByRequestId({
+ sessionId: "s1",
+ requestId: "ask_1",
+ answers: [["a"]],
+ });
+ assert.deepEqual(settled, { ok: true });
+ assert.equal(calls.length, 1);
+ assert.equal(calls[0].channel, IPC.invoke.askToolResolve);
+ assert.equal(calls[0].args[0].requestId, "ask_1");
+ assert.deepEqual(calls[0].args[0].answers, [["a"]]);
+ assert.deepEqual((await bridge.pendingInteractiveRequests("s1")).asks, []);
+});
+
+test("resolveAskByRequestId returns null for unknown requests", async (t) => {
+ const server = await createServer({
+ root: dirname(here),
+ configFile: false,
+ server: { middlewareMode: true, hmr: false, ws: false },
+ appType: "custom",
+ optimizeDeps: { noDiscovery: true, include: [] },
+ });
+ t.after(() => server.close());
+ const { createAgentHostBridge } = await server.ssrLoadModule("/electron/main/agent-host-bridge.ts");
+ let invoked = 0;
+ const bridge = createAgentHostBridge({
+ channels: IPC.invoke,
+ getHost: () => null,
+ log: () => undefined,
+ async invoke() {
+ invoked += 1;
+ return { ok: true };
+ },
+ });
+
+ assert.equal(
+ await bridge.resolveAskByRequestId({ sessionId: "s1", requestId: "missing", answers: [null] }),
+ null,
+ );
+ assert.equal(
+ await bridge.resolveAskByRequestId({ sessionId: "", requestId: "ask_1", answers: [null] }),
+ null,
+ );
+ assert.equal(invoked, 0);
+});