mirror of
https://github.com/stablyai/orca.git
synced 2026-11-16 10:58:10 +00:00
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>
This commit is contained in:
@@ -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<unknown> =>
|
||||
this.peer.request('session/close', { sessionId }, { timeoutMs })
|
||||
|
||||
start(
|
||||
options: AcpSessionStartOptions,
|
||||
|
||||
@@ -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 }))
|
||||
})
|
||||
})
|
||||
|
||||
@@ -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 } }
|
||||
}
|
||||
|
||||
@@ -30,6 +30,8 @@ export class AcpChildSessions {
|
||||
private readonly openText = new Map<string, ProviderTimelineTextItem>()
|
||||
/** Children that have not reported an ending, which a user's Stop also ends. */
|
||||
private readonly running = new Set<string>()
|
||||
/** Live children whose execution has started, so closing their session interrupts it. */
|
||||
private readonly executing = new Set<string>()
|
||||
|
||||
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`)
|
||||
|
||||
@@ -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<ChildStopResult> {
|
||||
// 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. */
|
||||
|
||||
Reference in New Issue
Block a user