diff --git a/packages/plugin/src/server/acp-internal/connection.ts b/packages/plugin/src/server/acp-internal/connection.ts index c93bf071da..c191fc3645 100644 --- a/packages/plugin/src/server/acp-internal/connection.ts +++ b/packages/plugin/src/server/acp-internal/connection.ts @@ -740,10 +740,12 @@ class AcpRuntime { await this.drainNotifications(); this.fallbackChunkIds.clear(); const canceled = this.activePrompt?.cancelling === true; + const preserveBackground = !canceled && this.outOfTurnActivity !== null; this.terminalizeTransientItems(canceled ? "canceled" : "failed", { - preserveBackgroundTools: !canceled, + turnId: preserveBackground ? turnId : undefined, }); - if (canceled) this.clearBackgroundWork(); + if (preserveBackground) this.syncBackgroundWorkFromInProgressTools(); + else if (canceled) this.clearBackgroundWork(); this.finishTurnPermissions(turnId); if (this.activePrompt?.turnId === turnId) this.activePrompt = null; this.emit({ @@ -1278,9 +1280,13 @@ class AcpRuntime { if (previous?.settlement === "canceled") return; if (update.sessionUpdate === "tool_call") this.fallbackChunkIds.clear(); const previousStatus = previous?.snapshot.status; + const backgroundProgress = + this.backgroundToolCalls.has(update.toolCallId) && + (update.status == null || update.status === previousStatus); // An already finished tool can receive output without waking the agent. const outOfTurn = this.isOutOfTurnLive() && + !backgroundProgress && previousStatus !== "completed" && previousStatus !== "failed" && (previous?.settlement === undefined || update.status != null); @@ -1410,13 +1416,13 @@ class AcpRuntime { private terminalizeTransientItems( state: AcpToolSettlement, - options?: { preserveInProgressTools?: boolean; preserveBackgroundTools?: boolean }, + options?: { preserveInProgressTools?: boolean; turnId?: string }, ): void { for (const [id, record] of this.toolCalls) { const { snapshot, turnId, settlement } = record; if (settlement === "canceled" || (settlement && state !== "canceled")) continue; if (snapshot.status !== "pending" && snapshot.status !== "in_progress") continue; - if (options?.preserveBackgroundTools && this.backgroundToolCalls.has(id)) continue; + if (options?.turnId && options.turnId !== turnId) continue; if (options?.preserveInProgressTools && snapshot.status === "in_progress") continue; this.toolCalls.set(id, { ...record, settlement: state }); this.emitToolCall(terminalToolItem(toolTimelineItem(snapshot), state), turnId); diff --git a/packages/plugin/src/server/acp.test.ts b/packages/plugin/src/server/acp.test.ts index cf5c43ad47..c7c626e826 100644 --- a/packages/plugin/src/server/acp.test.ts +++ b/packages/plugin/src/server/acp.test.ts @@ -61,8 +61,9 @@ function openInput( async function waitForEvent( events: ProviderEvent[], predicate: (event: ProviderEvent) => boolean, + timeout = 3_000, ): Promise { - await expect.poll(() => events.find(predicate), { timeout: 3_000 }).toBeTruthy(); + await expect.poll(() => events.find(predicate), { timeout }).toBeTruthy(); return events.find(predicate)!; } @@ -3226,6 +3227,13 @@ describe("ACP out-of-turn background activity", () => { title: "sleep 10", status: "in_progress", }, + { + sessionUpdate: "tool_call", + toolCallId: "cmd-2", + kind: "execute", + title: "Other command", + status: "in_progress", + }, { sessionUpdate: "agent_message_chunk", content: { type: "text", text: "Waiting." } }, ], }); @@ -3300,6 +3308,14 @@ describe("ACP out-of-turn background activity", () => { content: { type: "text", text: "Part 2." }, }, }); + agent.notify("session/update", { + sessionId: "connector-session", + update: { + sessionUpdate: "tool_call_update", + toolCallId: "cmd-2", + rawOutput: "Background progress", + }, + }); agent.notify("session/update", { sessionId: "connector-session", update: { sessionUpdate: "usage_update", used: 1200, size: 100000 }, @@ -3310,6 +3326,7 @@ describe("ACP out-of-turn background activity", () => { event.type === "session.turn" && event.turnId === autoTurnId && event.state === "completed", + 1_000, ); const startedAutonomousTurns = events.filter( (event) => @@ -3336,6 +3353,20 @@ describe("ACP out-of-turn background activity", () => { const params = request.params as { prompt?: Array<{ text?: string }> }; if (request.method !== "session/prompt" || params.prompt?.[0]?.text !== "Second") return false; + instance.notify("session/update", { + sessionId: "connector-session", + update: { sessionUpdate: "tool_call_update", toolCallId: "cmd-3", status: "in_progress" }, + }); + instance.notify("session/update", { + sessionId: "connector-session", + update: { + sessionUpdate: "tool_call", + toolCallId: "cmd-4", + kind: "execute", + title: "Follow-up command", + status: "in_progress", + }, + }); instance.reject(request, "Follow-up rejected"); return true; }, @@ -3358,6 +3389,13 @@ describe("ACP out-of-turn background activity", () => { title: "cmd-2", status: "in_progress", }, + { + sessionUpdate: "tool_call", + toolCallId: "cmd-3", + kind: "execute", + title: "Earlier command", + status: "pending", + }, { sessionUpdate: "agent_message_chunk", content: { type: "text", text: "Waiting on both." }, @@ -3451,8 +3489,21 @@ describe("ACP out-of-turn background activity", () => { kind: "shell", description: "cmd-2", }, + { id: "cmd-3", kind: "shell", description: "Earlier command" }, ], }); + expect( + events.findLast((event) => event.type === "timeline.item" && event.item.id === "cmd-3"), + ).toMatchObject({ + turnId: "acp:msg-1", + item: { status: "running" }, + }); + expect( + events.findLast((event) => event.type === "timeline.item" && event.item.id === "cmd-4"), + ).toMatchObject({ + turnId: "acp:msg-2", + item: { status: "failed" }, + }); expect(await queryCanStop(connection, events, "stop-with-cmd2")).toBe(false); } finally { await connection.close();