Skip to content

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

fix(chat): serialize stop with prompt admission

vastsad7b9d7324bf0
diff --git a/apps/desktop/electron/main/ipc/agent-ipc.ts b/apps/desktop/electron/main/ipc/agent-ipc.ts
index 2a1dd3d88..473c47051 100644
--- a/apps/desktop/electron/main/ipc/agent-ipc.ts
+++ b/apps/desktop/electron/main/ipc/agent-ipc.ts
@@ -710,7 +710,12 @@ export function registerAgentIpc({
handle(IPC.invoke.agentAbort, async (req: { sessionId: string; turnId?: string }) => {
if (!sidecar) throw new Error("sidecar unavailable");
- const releaseSessionOperation = req.turnId ? await acquireSessionOperation(req.sessionId) : undefined;
+ // Prompt admission holds this same session operation until the sidecar has
+ // accepted the turn. Waiting here closes the startup window where the
+ // renderer already shows Stop but activeTurns/runtime are not ready yet.
+ // Without the wait, agent.abort can return successfully while finding no
+ // runtime, and the prompt then starts after the user's first click.
+ const releaseSessionOperation = await acquireSessionOperation(req.sessionId);
try {
const abortedTurnId = activeTurns.get(req.sessionId);
if (req.turnId && abortedTurnId !== req.turnId) return { ok: false, aborted: false };
diff --git a/apps/desktop/test/session-message-input.test.mjs b/apps/desktop/test/session-message-input.test.mjs
index a61a7d95a..1743c23d8 100644
--- a/apps/desktop/test/session-message-input.test.mjs
+++ b/apps/desktop/test/session-message-input.test.mjs
@@ -110,3 +110,69 @@ test("prompt IPC persists original session text, skips slash expansion and binds
assert.equal(events.length, 2);
assert.equal(released, true);
});
+
+test("abort waits for prompt admission before dispatching to the sidecar", async () => {
+ const handlers = new Map();
+ const calls = [];
+ let releasePrompt;
+ let promptOperation;
+ const host = {
+ async call(method) {
+ calls.push(method);
+ if (method === "settings.get") return {};
+ if (method === "session.get") return { session: { id: "target", messages: [] } };
+ if (method === "session.beginTurn") return { turnId: "turn-1" };
+ if (method === "session.appendMessage") return {};
+ assert.fail(`unexpected RPC ${method}`);
+ },
+ };
+ const activeTurns = new Map();
+ const sidecarCalls = [];
+ registerAgentIpc({
+ registrar: { handle: (channel, handler) => handlers.set(channel, handler) },
+ getHost: () => host,
+ getSidecar: () => ({
+ setProjectInstructionRoot() {},
+ async call(method) {
+ sidecarCalls.push(method);
+ if (method === "agent.prompt") return { accepted: true, turnId: "turn-1" };
+ if (method === "agent.abort") return { ok: true, aborted: true };
+ assert.fail(`unexpected sidecar RPC ${method}`);
+ },
+ }),
+ getAgentHostBridge: () => null,
+ logger: { app() {} }, vendorOAuth: {}, agentExtensions: {}, cancelSessionTools() {},
+ persistenceOutbox: {}, dataDir: "/unused-for-no-attachments",
+ activeTurns, activeTurnUsages: new Map(), approvedExecutionIdsBySession: new Map(), claimedExecutionSessions: new Map(),
+ resolveAgentRuntimeLaunch: async () => ({
+ providerId: "provider", modelId: "model",
+ sidecarParams: { sessionId: "target", provider: { modelConfig: { input: ["text"] } } },
+ }),
+ acquireSessionOperation: async () => {
+ if (!promptOperation) {
+ promptOperation = new Promise((resolve) => { releasePrompt = resolve; });
+ }
+ await promptOperation;
+ return () => {};
+ },
+ finishTurn: async () => { assert.fail("the prompt should not be finalized by this test"); },
+ lockAbortReason() {}, async finishApprovedExecution() {}, async dispatchApprovedPlan() {},
+ async dispatchExecutionForProposal() {}, emitAgentEvent() {}, setNotificationViewingSessionId() {},
+ optionalWorkspaceRoot: async () => null,
+ composerCommandService: { buildComposerCommands: async () => [] },
+ loadComposerTemplatesCached: async () => [],
+ });
+
+ const prompt = handlers.get(IPC.invoke.agentPrompt)({ sessionId: "target", content: "hello" });
+ await Promise.resolve();
+ const abort = handlers.get(IPC.invoke.agentAbort)({ sessionId: "target" });
+ await Promise.resolve();
+ assert.deepEqual(sidecarCalls, [], "abort must not bypass prompt admission");
+
+ releasePrompt();
+ await prompt;
+ await abort;
+ assert.deepEqual(sidecarCalls, ["agent.prompt", "agent.abort"]);
+ assert.equal(activeTurns.get("target"), "turn-1");
+ assert.deepEqual(calls.slice(0, 2), ["settings.get", "session.get"]);
+});
diff --git a/docs/spec/06-delivery/04-e2e-test-plan.md b/docs/spec/06-delivery/04-e2e-test-plan.md
index 2aac1092c..c470663d9 100644
--- a/docs/spec/06-delivery/04-e2e-test-plan.md
+++ b/docs/spec/06-delivery/04-e2e-test-plan.md
@@ -1472,11 +1472,13 @@ identify the platform validation still needed.
- **Preconditions**: A session can produce both a deliberately delayed first
token and a streaming response.
-- **Steps**: 1) Send ordinary text and stop before assistant text, thinking, or
- a tool row begins. 2) Confirm the user row is undone and the text returns to
- the composer. 3) Send again, wait for partial output, then stop during the
- stream. 4) Observe the transcript and composer.
-- **Expected**: The unanswered send is undone and its draft restored. The
+- **Steps**: 1) Send ordinary text and click Stop immediately, including while
+ the prompt is still being admitted. 2) Confirm the user row is undone and
+ the text returns to the composer after that first Stop. 3) Send again, wait
+ for partial output, then stop during the stream. 4) Observe the transcript
+ and composer.
+- **Expected**: The first Stop is honored even if prompt admission is still in
+ progress; the unanswered send is undone and its draft restored. The
streaming send stops with its partial response preserved and no draft
restoration or duplicate user turn. The session remains usable.
- **Specs linked**: `03-runtime/02-agent-runtime.md`