From d583ba40b02c6facfb30503efbc0eb52d5e73f6b Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Sun, 8 Nov 2026 23:08:24 -0800 Subject: [PATCH] fix(native-chat): only a chat Stop closes OpenCode subagents, an early subagent Stop stays available, and the close request has a deadline Co-authored-by: SeaBee <85791528+seabee33@users.noreply.github.com> --- src/main/acp/protocol/acp-session-runtime.ts | 3 +- ...acp-structured-opencode-recordings.test.ts | 39 +++++++++++++++++++ .../session/acp-structured-session-adapter.ts | 7 +++- src/main/acp/timeline/acp-child-sessions.ts | 11 ++++++ .../acp/turns/acp-structured-child-stop.ts | 8 ++-- 5 files changed, 62 insertions(+), 6 deletions(-) diff --git a/src/main/acp/protocol/acp-session-runtime.ts b/src/main/acp/protocol/acp-session-runtime.ts index 9e532c570c1..bcec31f447d 100644 --- a/src/main/acp/protocol/acp-session-runtime.ts +++ b/src/main/acp/protocol/acp-session-runtime.ts @@ -146,7 +146,8 @@ export class AcpSessionRuntime { deleteSession = (sessionId: string) => this.call('session/delete', { sessionId }, z.unknown()) /** Any session the agent reports on this connection, including a subagent's child session. */ - closeSession = (sessionId: string) => this.call('session/close', { sessionId }, z.unknown()) + closeSession = (sessionId: string, timeoutMs = 10_000): Promise => + this.peer.request('session/close', { sessionId }, { timeoutMs }) start( options: AcpSessionStartOptions, diff --git a/src/main/acp/session/acp-structured-opencode-recordings.test.ts b/src/main/acp/session/acp-structured-opencode-recordings.test.ts index ab19356d2ca..807c14a09a3 100644 --- a/src/main/acp/session/acp-structured-opencode-recordings.test.ts +++ b/src/main/acp/session/acp-structured-opencode-recordings.test.ts @@ -215,7 +215,46 @@ describe('OpenCode 2 recordings through the adapter', () => { fence: 1 }) await waitFor(async () => expect(await turns(rig)).toMatchObject([{ state: 'completed' }])) + // A permission card's Cancel stops only its own turn. + await rig.adapter.cancelTurn({ sessionId: SESSION, fence: 1, prompt: { itemId: 'card' } }) + expect(closed).toEqual([]) await rig.adapter.cancelTurn({ sessionId: SESSION, fence: 1 }) await waitFor(() => expect(closed).toEqual([{ sessionId: 'child-1' }])) }) + + it("a subagent's own Stop before it starts running stays available, and works once it runs", async () => { + const child = { + rootSessionId: PROVIDER_SESSION, + childSessionId: 'child-1', + parentSessionId: PROVIDER_SESSION, + depth: 1 + } + let notify: (status: string) => void = () => {} + const rig = await openAcpAdapterRig({ + spec: acpLaunchSpecFor('opencode2')!, + initialize: { agentCapabilities: { loadSession: true, sessionCapabilities: { close: {} } } }, + script: (agent) => { + notify = (status) => + agent.notify('opencode/session/child_update', { ...child, type: 'status', status }) + agent.on('session/prompt', (frame) => { + notify('created') + agent.reply(frame, { stopReason: 'end_turn' }) + }) + agent.on('session/close', (frame) => agent.reply(frame, {})) + } + }) + await rig.acquire() + await rig.adapter.dispatch({ + sessionId: SESSION, + clientMessageId: 'send-1', + body: ask, + fence: 1 + }) + await waitFor(async () => expect(await turns(rig)).toMatchObject([{ state: 'completed' }])) + const stop = () => + rig.adapter.stopBackgroundTasks!({ sessionId: SESSION, fence: 1, taskIds: ['child-1'] }) + await expect(stop()).resolves.toEqual({ cancelled: false, stillRunning: true }) + notify('running') + await waitFor(() => expect(stop()).resolves.toEqual({ cancelled: true })) + }) }) diff --git a/src/main/acp/session/acp-structured-session-adapter.ts b/src/main/acp/session/acp-structured-session-adapter.ts index 2ac3b449eb8..acb9333867c 100644 --- a/src/main/acp/session/acp-structured-session-adapter.ts +++ b/src/main/acp/session/acp-structured-session-adapter.ts @@ -155,8 +155,11 @@ export class AcpStructuredSessionAdapter implements SessionAdapter { if (input.turnId !== undefined && liveTurnId !== null && input.turnId !== liveTurnId) { return { cancelled: false, refusal: { turnNotRunning: true } } } - // A user's Stop ends subagents too, even when the root turn ended just before it arrived. - closeAcpChildSessions(session) + // A chat Stop ends subagents too, even when the root turn ended just before it arrived; + // a permission card's Cancel stops only its own turn. + if (!input.prompt) { + closeAcpChildSessions(session) + } if (!stop.acpStopHasWork(session)) { return { cancelled: false, refusal: { turnNotRunning: true } } } diff --git a/src/main/acp/timeline/acp-child-sessions.ts b/src/main/acp/timeline/acp-child-sessions.ts index 0bb796c7564..a895e616bfa 100644 --- a/src/main/acp/timeline/acp-child-sessions.ts +++ b/src/main/acp/timeline/acp-child-sessions.ts @@ -30,6 +30,8 @@ export class AcpChildSessions { private readonly openText = new Map() /** Children that have not reported an ending, which a user's Stop also ends. */ private readonly running = new Set() + /** Live children whose execution has started, so closing their session interrupts it. */ + private readonly executing = new Set() constructor( private readonly rootSessionId: string, @@ -42,6 +44,10 @@ export class AcpChildSessions { return [...this.running] } + started(id: string): boolean { + return this.executing.has(id) + } + /** A child session's events, or undefined when the notification is the root's own. */ notification( method: string, @@ -109,8 +115,13 @@ export class AcpChildSessions { if (child.state !== undefined || !known) { this.running.add(child.agentId) } + // A child seen mid-run (its start came before this process) is executing too. + if ((child.started || child.update) && this.running.has(child.agentId)) { + this.executing.add(child.agentId) + } } else { this.running.delete(child.agentId) + this.executing.delete(child.agentId) events.push( ...this.closeText(`${child.agentId}:assistant`), ...this.closeText(`${child.agentId}:reasoning`) diff --git a/src/main/acp/turns/acp-structured-child-stop.ts b/src/main/acp/turns/acp-structured-child-stop.ts index b3a15e0d5b1..01084551618 100644 --- a/src/main/acp/turns/acp-structured-child-stop.ts +++ b/src/main/acp/turns/acp-structured-child-stop.ts @@ -5,7 +5,7 @@ import { import type { AcpStructuredSession } from '../session/acp-structured-session-contract' import type { AcpStructuredConnection } from '../process/acp-structured-connection' import type { AcpChildStops, AcpDialect } from '../acp-dialects/acp-dialect' -import { AcpAgentError } from '../transport/acp-errors' +import { AcpAgentError, AcpRequestTimeoutError } from '../transport/acp-errors' import type { InitializeResponse } from '../generated/acp-protocol.generated' import type { AgentChildWorkStopAcknowledgement, @@ -89,15 +89,17 @@ async function closeAcpChildSession( session: AcpStructuredSession, id: string ): Promise { + // Closing a child that has not started running interrupts nothing, so Stop stays available. + const started = session.lane.translator.children.started(id) try { await session.connection.closeSession(id) - return { cancelled: true } } catch (error) { - if (error instanceof AcpAgentError) { + if (error instanceof AcpAgentError || error instanceof AcpRequestTimeoutError) { return { cancelled: false, stillRunning: true } } throw error } + return started ? { cancelled: true } : { cancelled: false, stillRunning: true } } /** A user's Stop also ends subagents that outlive the cancelled turn. */