Skip to content

Commit 79060fc

Browse files
committed
fix(acp): stream a command's expanded prompt
Command prompts return end_turn immediately, before the turn's event stream is joined. Keep the prompt pending until the executor returns, wait for the command's admitted execution to start when it beats the executor, and continue across execution cycles. This is the focused command-streaming part of anomalyco#53913; the unrelated cancellation changes are not included. Original work by @danyel117 in anomalyco#53913. Fixes anomalyco#53914 Refs anomalyco#54371
1 parent 98e4b53 commit 79060fc

3 files changed

Lines changed: 154 additions & 4 deletions

File tree

‎packages/cli/src/acp/translate.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export type TurnContext = {
2626
readonly sessionID: Session.ID
2727
readonly cwd: string
2828
readonly start: TurnStart
29+
readonly command?: boolean
2930
readonly childUpdates: boolean
3031
readonly compaction: boolean
3132
}
@@ -46,6 +47,7 @@ type RetryStatus = {
4647

4748
export type TurnState = {
4849
readonly started: boolean
50+
readonly commandInbox?: string
4951
readonly tools: ReadonlyMap<string, Tool>
5052
readonly retries: ReadonlyMap<string, RetryStatus>
5153
readonly compactions: ACPCompaction.Tracked
@@ -123,8 +125,12 @@ export function fold(state: TurnState, event: OpenCodeEvent, ctx: TurnContext):
123125
return { state: { ...state, asks }, outputs: [{ _tag: "AskSettled", id: settledID }] }
124126
}
125127
if (!sessionID || (sessionID !== ctx.sessionID && !child)) return { state, outputs: [] }
126-
if (event.type === "session.inbox.delivered" && event.data.inboxID === ctx.start.id)
127-
return { state: { ...state, started: true }, outputs: [] }
128+
if (event.type === "session.inbox.delivered") {
129+
if (event.data.inboxID === ctx.start.id) return { state: { ...state, started: true }, outputs: [] }
130+
// A command admits its expanded prompt under a new inbox id. The first one is the command's.
131+
if (ctx.command && state.commandInbox === undefined)
132+
return { state: { ...state, started: true, commandInbox: event.data.inboxID }, outputs: [] }
133+
}
128134
if (!state.started) return { state, outputs: [] }
129135
return child ? childEvent(state, event, ctx, child) : rootEvent(state, event, ctx)
130136
}

‎packages/cli/src/acp/turn.ts‎

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ export const CancelDrainTimeout = Context.Reference<Duration.Input>("@opencode/c
4848
defaultValue: () => "5 seconds",
4949
})
5050

51+
/** A command may return before its admitted prompt reaches the event stream. */
52+
const CommandStartGrace = Duration.millis(500)
53+
5154
type Subscription = {
5255
readonly scope: Scope.Closeable
5356
readonly events: Queue.Dequeue<OpenCodeEvent, unknown>
@@ -60,6 +63,9 @@ type Turn = {
6063
readonly state: Ref.Ref<ACPTranslate.TurnState>
6164
readonly subscription: Subscription
6265
readonly cancelled: Deferred.Deferred<void>
66+
readonly commandFinished: Deferred.Deferred<void>
67+
readonly commandStarted: Deferred.Deferred<void>
68+
readonly commandTerminal: Ref.Ref<ACPTranslate.Terminal | undefined>
6369
readonly background: boolean
6470
}
6571

@@ -93,7 +99,16 @@ export const make = Effect.fnUntraced(function* (input: {
9399
Effect.forever,
94100
Effect.forkIn(subscriptionScope),
95101
)
96-
return { ctx, state, subscription, cancelled: yield* Deferred.make<void>(), background: false } satisfies Turn
102+
return {
103+
ctx,
104+
state,
105+
subscription,
106+
cancelled: yield* Deferred.make<void>(),
107+
commandFinished: yield* Deferred.make<void>(),
108+
commandStarted: yield* Deferred.make<void>(),
109+
commandTerminal: yield* Ref.make<ACPTranslate.Terminal | undefined>(undefined),
110+
background: false,
111+
} satisfies Turn
97112
})
98113

99114
const take = (subscription: Subscription) =>
@@ -210,13 +225,27 @@ export const make = Effect.fnUntraced(function* (input: {
210225
return [next, next.state]
211226
})
212227
yield* Effect.forEach(folded.outputs, (output) => interpret(turn, output), { discard: true })
228+
if (turn.ctx.command && event.type === "session.execution.started") {
229+
yield* Deferred.succeed(turn.commandStarted, undefined)
230+
}
231+
if (turn.ctx.command && folded.terminal) {
232+
yield* Ref.set(turn.commandTerminal, folded.terminal)
233+
}
213234
return folded
214235
})
215236

216237
const consume = Effect.fnUntraced(function* (turn: Turn) {
217238
while (true) {
218239
const folded = yield* advance(turn)
219240
if (folded.terminal) {
241+
// A registered executor can own several execution cycles. Its return,
242+
// rather than the first terminal event, ends command-owned streaming.
243+
if (
244+
turn.ctx.command &&
245+
!(yield* Deferred.isDone(turn.commandFinished)) &&
246+
!(yield* Deferred.isDone(turn.cancelled))
247+
)
248+
continue
220249
yield* asksSettled(turn.subscription)
221250
return folded.terminal
222251
}
@@ -299,7 +328,20 @@ export const make = Effect.fnUntraced(function* (input: {
299328
const events = yield* consume(turn).pipe(Effect.forkScoped)
300329
return yield* Effect.gen(function* () {
301330
yield* submit(attached, prompt)
302-
if (prompt.command) return "succeeded" as const
331+
if (prompt.command) {
332+
yield* Deferred.succeed(turn.commandFinished, undefined)
333+
const terminal = yield* Ref.get(turn.commandTerminal)
334+
if (terminal !== undefined && (yield* Deferred.isDone(turn.commandStarted))) {
335+
yield* asksSettled(turn.subscription)
336+
return terminal
337+
}
338+
const started = yield* Deferred.await(turn.commandStarted).pipe(
339+
Effect.timeoutOption(CommandStartGrace),
340+
)
341+
if (Option.isSome(started)) return yield* Fiber.join(events)
342+
yield* asksSettled(turn.subscription)
343+
return (yield* Ref.get(turn.commandTerminal)) ?? "succeeded"
344+
}
303345
return yield* Fiber.join(events)
304346
}).pipe(Effect.onInterrupt(() => windDown(turn, events)))
305347
}).pipe(Effect.scoped)
@@ -372,6 +414,7 @@ export const make = Effect.fnUntraced(function* (input: {
372414
sessionID: attached.id,
373415
cwd: attached.cwd,
374416
start: prompt.start,
417+
command: prompt.command !== undefined,
375418
childUpdates: capabilities.childSessionUpdates,
376419
compaction: capabilities.compaction,
377420
},

‎packages/cli/test/acp/prompt.test.ts‎

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,107 @@ const held = {
5555
onInterrupt: ({ sessionID }) => [interrupted(sessionID)],
5656
} satisfies WireOptions
5757

58+
test("streams every execution submitted by a still-running command", async () => {
59+
const gate = Promise.withResolvers<Response>()
60+
await using acp = await startSession({
61+
fetch: (request) => (request.method === "POST" && request.path.endsWith("/command") ? gate.promise : undefined),
62+
})
63+
let responded = false
64+
const prompt = acp.prompt(acp.sessionId, "/review now").then((result) => {
65+
responded = true
66+
return result
67+
})
68+
try {
69+
await acp.until(() => acp.server.requests.some((request) => request.path.endsWith("/command")))
70+
acp.server.send(
71+
delivered(acp.sessionId, "msg_command_owned"),
72+
durableEvent("session.execution.started", { sessionID: acp.sessionId }),
73+
textDelta(acp.sessionId, "msg_phase1", "phase one"),
74+
)
75+
await acp.waitForUpdate(
76+
(item) =>
77+
item.update.sessionUpdate === "agent_message_chunk" &&
78+
item.update.content.type === "text" &&
79+
item.update.content.text === "phase one",
80+
)
81+
acp.server.send(
82+
succeeded(acp.sessionId),
83+
delivered(acp.sessionId, "msg_command_next"),
84+
durableEvent("session.execution.started", { sessionID: acp.sessionId }),
85+
textDelta(acp.sessionId, "msg_phase2", "phase two"),
86+
succeeded(acp.sessionId),
87+
)
88+
await acp.waitForUpdate(
89+
(item) =>
90+
item.update.sessionUpdate === "agent_message_chunk" &&
91+
item.update.content.type === "text" &&
92+
item.update.content.text === "phase two",
93+
)
94+
expect(responded).toBe(false)
95+
gate.resolve(new Response(null, { status: 204 }))
96+
expect((await prompt).stopReason).toBe("end_turn")
97+
} finally {
98+
gate.resolve(new Response(null, { status: 204 }))
99+
}
100+
})
101+
102+
test("a returned command waits for the current execution's final text and terminal event", async () => {
103+
const gate = Promise.withResolvers<Response>()
104+
await using acp = await startSession({
105+
fetch: (request) => (request.method === "POST" && request.path.endsWith("/command") ? gate.promise : undefined),
106+
})
107+
let responded = false
108+
const prompt = acp.prompt(acp.sessionId, "/review now").then((result) => {
109+
responded = true
110+
return result
111+
})
112+
await acp.until(() => acp.server.requests.some((request) => request.path.endsWith("/command")))
113+
acp.server.send(
114+
delivered(acp.sessionId, "msg_owned"),
115+
durableEvent("session.execution.started", { sessionID: acp.sessionId }),
116+
textDelta(acp.sessionId, "msg_text", "before return"),
117+
)
118+
await acp.waitForUpdate((item) => item.update.sessionUpdate === "agent_message_chunk")
119+
gate.resolve(new Response(null, { status: 204 }))
120+
await new Promise((resolve) => setTimeout(resolve, 20))
121+
expect(responded).toBe(false)
122+
acp.server.send(textDelta(acp.sessionId, "msg_text", "final text"), succeeded(acp.sessionId))
123+
expect((await prompt).stopReason).toBe("end_turn")
124+
expect(
125+
acp.updates.some(
126+
(item) =>
127+
item.update.sessionUpdate === "agent_message_chunk" &&
128+
item.update.content.type === "text" &&
129+
item.update.content.text === "final text",
130+
),
131+
).toBe(true)
132+
})
133+
134+
test("a command that returns before its expanded prompt starts still streams", async () => {
135+
const gate = Promise.withResolvers<Response>()
136+
await using acp = await startSession({
137+
fetch: (request) => (request.method === "POST" && request.path.endsWith("/command") ? gate.promise : undefined),
138+
})
139+
const prompt = acp.prompt(acp.sessionId, "/review now")
140+
await acp.until(() => acp.server.requests.some((request) => request.path.endsWith("/command")))
141+
gate.resolve(new Response(null, { status: 204 }))
142+
await new Promise((resolve) => setTimeout(resolve, 20))
143+
acp.server.send(
144+
delivered(acp.sessionId, "msg_command_owned"),
145+
durableEvent("session.execution.started", { sessionID: acp.sessionId }),
146+
textDelta(acp.sessionId, "msg_expanded", "expanded output"),
147+
succeeded(acp.sessionId),
148+
)
149+
await acp.waitForUpdate(
150+
(item) =>
151+
item.update.sessionUpdate === "agent_message_chunk" &&
152+
item.update.content.type === "text" &&
153+
item.update.content.text === "expanded output",
154+
)
155+
expect((await prompt).stopReason).toBe("end_turn")
156+
})
157+
158+
58159
describe("acp prompt turns over the wire", () => {
59160
test("streams an admitted turn and resolves with usage after its terminal event", async () => {
60161
const releaseAdmission = Promise.withResolvers<void>()

0 commit comments

Comments
 (0)