diff --git a/apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.test.ts index 5cbb4975e5e57..1ff34b8a2c6cb 100644 --- a/apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.test.ts @@ -1004,6 +1004,9 @@ const openSessionWithRunningCommand = Effect.fn("openSessionWithRunningCommand") options?: { readonly profileDirectory?: string; readonly initialToolCalls?: ReadonlyArray; + readonly promptUpdates?: ReadonlyArray; + readonly promptStopReason?: EffectAcpSchema.StopReason; + readonly steerStopReason?: EffectAcpSchema.StopReason; }, ) { const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner; @@ -1036,6 +1039,7 @@ const openSessionWithRunningCommand = Effect.fn("openSessionWithRunningCommand") const usage = { sessionUpdate: "usage_update", used: 9000, size: 1048576 } as const; const offers: Array = []; const scheduledFinalizes = yield* Queue.unbounded(); + const promptSettledDeferred = yield* Deferred.make(); const instanceId = ProviderInstanceId.make("antigravity-wake-test"); const threadId = ThreadId.make("thread-antigravity-wake"); const selfInvocation = yield* resolveSelfInvocation(); @@ -1082,33 +1086,39 @@ const openSessionWithRunningCommand = Effect.fn("openSessionWithRunningCommand") if (prompts.length > 1) { yield* Deferred.succeed(steered, undefined); yield* Deferred.await(releaseSteer); - return { stopReason: "end_turn" as const }; + return { stopReason: options?.steerStopReason ?? ("end_turn" as const) }; } - if (options?.initialToolCalls !== undefined) { - for (const toolUpdate of options.initialToolCalls) { - yield* send(toolUpdate); + if (options?.promptUpdates !== undefined) { + for (const promptUpdate of options.promptUpdates) { + yield* send(promptUpdate); } } else { - yield* send( - detachedChildProfile === undefined - ? { sessionUpdate: "tool_call", ...command, status: "in_progress" } - : { - sessionUpdate: "tool_call", - toolCallId: "call_research", - kind: "other", - title: "Run invoke_subagent", - status: "completed", - rawInput: { TypeName: "research", Role: "Researcher", Prompt: "Read it" }, - }, - ); + if (options?.initialToolCalls !== undefined) { + for (const toolUpdate of options.initialToolCalls) { + yield* send(toolUpdate); + } + } else { + yield* send( + detachedChildProfile === undefined + ? { sessionUpdate: "tool_call", ...command, status: "in_progress" } + : { + sessionUpdate: "tool_call", + toolCallId: "call_research", + kind: "other", + title: "Run invoke_subagent", + status: "completed", + rawInput: { TypeName: "research", Role: "Researcher", Prompt: "Read it" }, + }, + ); + } + yield* send({ + sessionUpdate: "agent_message_chunk", + content: { type: "text", text: "I'll render once the fetch finishes." }, + }); } - yield* send({ - sessionUpdate: "agent_message_chunk", - content: { type: "text", text: "I'll render once the fetch finishes." }, - }); yield* send(usage); if (steering?.holdsFirstPrompt === true) yield* Deferred.await(steered); - return { stopReason: "end_turn" as const }; + return { stopReason: options?.promptStopReason ?? ("end_turn" as const) }; }), })), ); @@ -1135,6 +1145,8 @@ const openSessionWithRunningCommand = Effect.fn("openSessionWithRunningCommand") : { continuationRequests: adapterOptions.continuationRequests }), testHooks: { onDeferredFinalizeScheduled: (debounce) => Queue.offer(scheduledFinalizes, debounce), + afterPromptSettledWithBackgroundWork: () => + Deferred.succeed(promptSettledDeferred, undefined).pipe(Effect.asVoid), }, }); const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({ @@ -1220,6 +1232,7 @@ const openSessionWithRunningCommand = Effect.fn("openSessionWithRunningCommand") usage, offers, scheduledFinalizes, + awaitPromptSettledWithBackgroundWork: Deferred.await(promptSettledDeferred), send, startTurn, until, @@ -1326,7 +1339,21 @@ describe("AntigravityAdapterV2 work after the prompt ends", () => { const { command, usage, offers, send, startTurn, until, throughPromptSettled } = yield* openSessionWithRunningCommand({ WaitMsBeforeAsync: 10000 }); yield* startTurn(1, false); - yield* throughPromptSettled; + const settled = yield* throughPromptSettled; + const preWakeReply = settled.findLast( + (event) => + event.type === "turn_item.updated" && + event.turnItem.type === "assistant_message" && + event.turnItem.text === "I'll render once the fetch finishes.", + ); + assert.equal( + preWakeReply?.type === "turn_item.updated" && + preWakeReply.turnItem.type === "assistant_message" + ? preWakeReply.turnItem.phase + : null, + "final_answer", + "the completed reply before a deferred background wake must stay visible", + ); // Progress keeps a command past the post-settle hold bound, which counts silence only. for (const minute of [10, 20, 30]) { yield* TestClock.adjust("10 minutes"); @@ -1516,7 +1543,20 @@ describe("AntigravityAdapterV2 work after the prompt ends", () => { ], }); yield* startTurn(1, false); - yield* throughPromptSettled; + const settled = yield* throughPromptSettled; + const initialReply = settled.findLast( + (event) => + event.type === "turn_item.updated" && + event.turnItem.type === "assistant_message" && + event.turnItem.text === "I'll render once the fetch finishes.", + ); + assert.equal( + initialReply?.type === "turn_item.updated" && + initialReply.turnItem.type === "assistant_message" + ? initialReply.turnItem.phase + : null, + "final_answer", + ); // Command 1 finishes and wakes the model. yield* writeFile( @@ -1684,11 +1724,27 @@ describe("AntigravityAdapterV2 work after the prompt ends", () => { const run = yield* until((event) => event.type === "turn.terminal"); const terminal = run.at(-1); assert.equal(terminal?.type === "turn.terminal" && terminal.status, "completed"); - assert.include( - JSON.stringify(run), - "All 34 tests passed.", - "reply 2 must stay in the same run", + const wake1Reply = run.findLast( + (event) => + event.type === "turn_item.updated" && + event.turnItem.type === "assistant_message" && + event.turnItem.text === "gates passed; waiting for tests.", ); + const wake2Reply = run.findLast( + (event) => + event.type === "turn_item.updated" && + event.turnItem.type === "assistant_message" && + event.turnItem.text === "All 34 tests passed.", + ); + assert.equal( + wake1Reply?.type === "turn_item.updated" && + wake1Reply.turnItem.type === "assistant_message" + ? wake1Reply.turnItem.phase + : null, + "final_answer", + "reply 1 must stay visible as a distinct final answer when reply 2 follows in the same run", + ); + assert.isDefined(wake2Reply, "reply 2 must be its own assistant message in the same run"); assert.lengthOf(offers, 0, "the open run needs no continuation"); }).pipe(Effect.provide(layerSession)), ); @@ -1745,7 +1801,20 @@ describe("AntigravityAdapterV2 work after the prompt ends", () => { ], }); yield* startTurn(1, false); - yield* throughPromptSettled; + const settled = yield* throughPromptSettled; + const initialReply = settled.findLast( + (event) => + event.type === "turn_item.updated" && + event.turnItem.type === "assistant_message" && + event.turnItem.text === "I'll render once the fetch finishes.", + ); + assert.equal( + initialReply?.type === "turn_item.updated" && + initialReply.turnItem.type === "assistant_message" + ? initialReply.turnItem.phase + : null, + "final_answer", + ); assert.equal(yield* Queue.take(scheduledFinalizes), "2 minutes"); yield* TestClock.adjust("9 seconds"); @@ -1772,6 +1841,236 @@ describe("AntigravityAdapterV2 work after the prompt ends", () => { }).pipe(Effect.provide(layerSession)), ); + it.effect.each([ + { + name: "keeps pre-tool commentary foldable before a deferred background command", + mode: "pre-tool-commentary" as const, + expectedPhase: undefined, + }, + { + name: "keeps named pre-tool commentary foldable before a deferred background command", + mode: "named-pre-tool-commentary" as const, + expectedPhase: undefined, + }, + { + name: "does not restore a named reply cleared by an empty replacement before settlement", + mode: "cleared-named-reply" as const, + expectedPhase: undefined, + }, + { + name: "preserves a pre-wake reply closed by a background command completion before end_turn", + mode: "background-tool-completion" as const, + expectedPhase: "final_answer" as const, + }, + { + name: "preserves a pre-wake reply closed by trailing reasoning before end_turn", + mode: "trailing-reasoning" as const, + expectedPhase: "final_answer" as const, + }, + { + name: "does not promote assistant text when the prompt ends cancelled", + mode: "cancelled-prompt" as const, + expectedPhase: undefined, + }, + { + name: "clears stale final_answer when a promoted named segment reopens before a new tool", + mode: "reopened-named-commentary" as const, + expectedPhase: undefined, + }, + ])("$name", ({ mode, expectedPhase }) => + Effect.gen(function* () { + const cmd = { + toolCallId: "call_pause", + kind: "execute", + title: "bin/session pause", + rawInput: { CommandLine: "bin/session pause", WaitMsBeforeAsync: 10000 }, + } as const; + const otherCmd = { + toolCallId: "call_keep_open", + kind: "execute", + title: "bin/wait", + rawInput: { CommandLine: "bin/wait", WaitMsBeforeAsync: 10000 }, + } as const; + const targetText = + mode === "pre-tool-commentary" || mode === "named-pre-tool-commentary" + ? "Let me pause the stack." + : "Full UI/UX audit reply."; + const promptUpdates: ReadonlyArray = + mode === "pre-tool-commentary" + ? [ + { sessionUpdate: "agent_message_chunk", content: { type: "text", text: targetText } }, + { sessionUpdate: "tool_call", ...cmd, status: "in_progress" }, + ] + : mode === "named-pre-tool-commentary" + ? [ + { + sessionUpdate: "agent_message_chunk", + messageId: "msg-commentary", + content: { type: "text", text: targetText }, + }, + { sessionUpdate: "tool_call", ...cmd, status: "in_progress" }, + // Trailing whitespace on the same named stream must not restore pre-tool commentary. + { + sessionUpdate: "agent_message_chunk", + messageId: "msg-commentary", + content: { type: "text", text: "\n" }, + }, + ] + : mode === "cleared-named-reply" + ? [ + { sessionUpdate: "tool_call", ...cmd, status: "in_progress" }, + { + sessionUpdate: "agent_message_chunk", + messageId: "msg-cleared", + content: { type: "text", text: targetText }, + }, + { + sessionUpdate: "agent_thought_chunk", + content: { type: "text", text: "Retracting draft reply." }, + }, + { + sessionUpdate: "agent_message", + messageId: "msg-cleared", + content: [], + }, + ] + : mode === "background-tool-completion" + ? [ + { sessionUpdate: "tool_call", ...otherCmd, status: "in_progress" }, + { sessionUpdate: "tool_call", ...cmd, status: "in_progress" }, + { + sessionUpdate: "agent_message_chunk", + content: { type: "text", text: targetText }, + }, + { + sessionUpdate: "tool_call_update", + ...cmd, + status: "completed", + rawOutput: { output: "Stack paused.", exit_code: 0 }, + }, + ] + : mode === "trailing-reasoning" + ? [ + { sessionUpdate: "tool_call", ...cmd, status: "in_progress" }, + { + sessionUpdate: "agent_message_chunk", + content: { type: "text", text: targetText }, + }, + { + sessionUpdate: "agent_thought_chunk", + content: { type: "text", text: "Waiting on background pause." }, + }, + ] + : mode === "reopened-named-commentary" + ? [ + { sessionUpdate: "tool_call", ...cmd, status: "in_progress" }, + { + sessionUpdate: "agent_message_chunk", + messageId: "msg-reopened", + content: { type: "text", text: targetText }, + }, + ] + : [ + { sessionUpdate: "tool_call", ...cmd, status: "in_progress" }, + { + sessionUpdate: "agent_message_chunk", + content: { type: "text", text: targetText }, + }, + ]; + const { usage, send, startTurn, until, awaitPromptSettledWithBackgroundWork } = + yield* openSessionWithRunningCommand({}, undefined, undefined, { + promptUpdates, + ...(mode === "cancelled-prompt" ? { promptStopReason: "cancelled" as const } : {}), + }); + yield* startTurn(1, false); + yield* awaitPromptSettledWithBackgroundWork; + const pendingCmd = mode === "background-tool-completion" ? otherCmd : cmd; + yield* send({ + sessionUpdate: "tool_call_update", + ...pendingCmd, + status: "completed", + rawOutput: { output: "Command exited with code 0.", exit_code: 0 }, + }); + if (mode === "reopened-named-commentary") { + // Reopening the previously promoted named segment before a new tool must drop its stale final_answer phase. + yield* send({ + sessionUpdate: "agent_message_chunk", + messageId: "msg-reopened", + content: { type: "text", text: " Checking again." }, + }); + yield* send({ + sessionUpdate: "tool_call", + toolCallId: "call_recheck", + kind: "execute", + title: "bin/check", + status: "completed", + }); + } + yield* send({ + sessionUpdate: "agent_thought_chunk", + content: { type: "text", text: "Background command completed." }, + }); + yield* send({ + sessionUpdate: "agent_message_chunk", + content: { type: "text", text: "Stack s23 is paused." }, + }); + yield* send(usage); + yield* TestClock.adjust("3 seconds"); + const allEvents = yield* until((event) => event.type === "turn.terminal"); + if (mode === "reopened-named-commentary") { + const reopened = allEvents.findLast( + (event) => + event.type === "turn_item.updated" && + event.turnItem.type === "assistant_message" && + event.turnItem.nativeItemRef?.nativeId?.endsWith(":assistant:message:msg-reopened"), + ); + assert.equal( + reopened?.type === "turn_item.updated" && reopened.turnItem.type === "assistant_message" + ? reopened.turnItem.phase + : null, + expectedPhase, + ); + return; + } + if (mode === "cleared-named-reply") { + const cleared = allEvents.findLast( + (event) => + event.type === "turn_item.updated" && + event.turnItem.type === "assistant_message" && + event.turnItem.nativeItemRef?.nativeId?.endsWith(":assistant:message:msg-cleared"), + ); + assert.equal( + cleared?.type === "turn_item.updated" && cleared.turnItem.type === "assistant_message" + ? cleared.turnItem.text + : null, + "", + "settlement must not republish text cleared by an empty replacement", + ); + assert.equal( + cleared?.type === "turn_item.updated" && cleared.turnItem.type === "assistant_message" + ? cleared.turnItem.phase + : null, + undefined, + ); + return; + } + const preWakeReply = allEvents.findLast( + (event) => + event.type === "turn_item.updated" && + event.turnItem.type === "assistant_message" && + event.turnItem.status === "completed" && + event.turnItem.text.trim() === targetText, + ); + assert.equal( + preWakeReply?.type === "turn_item.updated" && + preWakeReply.turnItem.type === "assistant_message" + ? preWakeReply.turnItem.phase + : null, + expectedPhase, + ); + }).pipe(Effect.provide(layerSession)), + ); + // agy names its background commands `/task-`; the agent passes that to // t3_background_wait, which must not fail since the adapter already holds the run open. it.effect("accepts t3_background_wait for a command it already tracks", () => @@ -1941,6 +2240,86 @@ describe("AntigravityAdapterV2 work after the prompt ends", () => { }).pipe(Effect.provide(layerSession)), ); + it.effect( + "does not mark a cancelled steer's partial reply as final_answer on a continuation run", + () => + Effect.gen(function* () { + const opened = yield* openSessionWithRunningCommand( + { IsDaemon: true, WaitMsBeforeAsync: 500 }, + undefined, + { agentVersion: "1.5.11", holdsFirstPrompt: false }, + { steerStopReason: "cancelled" }, + ); + const { + session, + providerThread, + command, + usage, + offers, + send, + startTurn, + until, + throughTerminal, + } = opened; + yield* startTurn(1, false); + yield* throughTerminal; + yield* send({ + sessionUpdate: "tool_call_update", + ...command, + status: "completed", + rawOutput: { output: "Command exited with code 0.", exit_code: 0 }, + }); + assert.lengthOf(offers, 1); + yield* offers[0]!.dispatchIfCurrent!(Effect.void); + yield* startTurn(2, true); + const followUp = { toolCallId: "call_build", kind: "execute", title: "vp run build" }; + yield* send({ sessionUpdate: "tool_call", ...followUp, status: "in_progress" }); + const continued = yield* until( + (event) => + event.type === "turn_item.updated" && + event.turnItem.nativeItemRef?.nativeId === followUp.toolCallId, + ); + const providerTurnId = continued.flatMap((event) => + event.type === "provider_turn.updated" ? [event.providerTurn.id] : [], + )[0]!; + + yield* session.steerTurn({ + threadId: ThreadId.make("thread-antigravity-wake"), + runId: RunId.make("run-antigravity-wake-2"), + providerThread, + providerTurnId, + message: { + messageId: MessageId.make("message-antigravity-steer-cancelled"), + text: "Skip the build", + attachments: [], + createdBy: "user", + creationSource: "web", + }, + }); + yield* send({ + sessionUpdate: "agent_message_chunk", + content: { type: "text", text: "Partial steer reply." }, + }); + yield* send(usage); + yield* send({ sessionUpdate: "tool_call_update", ...followUp, status: "completed" }); + yield* opened.releaseSteer; + const events = yield* until((event) => event.type === "turn.terminal"); + const partial = events.findLast( + (event) => + event.type === "turn_item.updated" && + event.turnItem.type === "assistant_message" && + event.turnItem.text === "Partial steer reply.", + ); + assert.equal( + partial?.type === "turn_item.updated" && partial.turnItem.type === "assistant_message" + ? partial.turnItem.phase + : null, + undefined, + "a steer prompt that ends cancelled must not promote its partial text on usage_update", + ); + }).pipe(Effect.provide(layerSession)), + ); + it.effect("steers a run held open by a detached subagent without stopping it", () => Effect.gen(function* () { const fileSystem = yield* FileSystem.FileSystem; diff --git a/packages/provider-acp/src/server/adapter.ts b/packages/provider-acp/src/server/adapter.ts index 908c99e4eaeb3..64444d96a4a39 100644 --- a/packages/provider-acp/src/server/adapter.ts +++ b/packages/provider-acp/src/server/adapter.ts @@ -1636,8 +1636,11 @@ function selectAutoApprovedPermissionOption( interface ActiveTextSegment { readonly nativeItemId: string; readonly startedAt: DateTime.Utc; + completedAt?: DateTime.Utc | undefined; sourceMessageId: string | null; text: string; + hasNonWhitespace: boolean; + finalAnswer?: boolean | undefined; // The running node and first text have been emitted. emitted: boolean; // Text changed since the last emission; a delayed flush is scheduled. @@ -1668,6 +1671,8 @@ interface ActiveAcpTurn { readonly user: ActiveTextStream; readonly assistant: ActiveTextStream; readonly reasoning: ActiveTextStream; + /** The latest non-empty assistant segment not yet followed by a new tool or input request. */ + closingAssistant: ActiveTextSegment | null; providerRetry?: | { readonly failure: OrchestrationV2ProviderFailure; @@ -3158,32 +3163,22 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( : context.reasoning; const textEmissionPermit = yield* Semaphore.make(1); - /** - * Emits the stream's current segment. A `flushOf` call is a delayed - * flush: it emits only while that segment is still current and has - * unsent text. The permit keeps a flush from landing after the - * segment's completed snapshot. - */ - const emitTextSegment = Effect.fnUntraced(function* ( + const writeTextSegment = Effect.fnUntraced(function* ( context: ActiveAcpTurn, kind: "user" | "assistant" | "reasoning", + segment: ActiveTextSegment, completed: boolean, - flushOf?: ActiveTextSegment, + emitsNode: boolean, ) { - const stream = textStreamFor(context, kind); - const segment = stream.current; - if (segment === null) return; - if (flushOf !== undefined && (segment !== flushOf || !segment.dirty)) return; - // The node carries no text, so it changes only when the segment - // opens and when it completes. - const emitsNode = completed || !segment.emitted; - segment.emitted = true; - segment.dirty = false; // Read the text in the same step that clears `dirty`. Chunks keep // appending while this emission yields; each one marks the segment // dirty again and reaches the next flush or the close. const text = segment.text; const now = yield* DateTime.now; + if (completed) { + segment.completedAt ??= now; + } + const completedAt = completed ? (segment.completedAt ?? now) : null; const ordinal = yield* resolveItemOrdinal(context, segment.nativeItemId); const nodeId = providerNodeId(segment.nativeItemId); const turnItemId = providerTurnItemId(segment.nativeItemId); @@ -3211,7 +3206,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( runtimeRequestId: null, checkpointScopeId: null, startedAt: segment.startedAt, - completedAt: completed ? now : null, + completedAt, }, }); } @@ -3253,7 +3248,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( status: completed ? "completed" : "running", title: null, startedAt: segment.startedAt, - completedAt: completed ? now : null, + completedAt, updatedAt: now, type: "user_message", messageId, @@ -3278,9 +3273,12 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( status: completed ? "completed" : "running", title: null, startedAt: segment.startedAt, - completedAt: completed ? now : null, + completedAt, updatedAt: now, type: "assistant_message", + ...(completed && segment.finalAnswer === true + ? { phase: "final_answer" as const } + : {}), messageId, text, streaming: !completed, @@ -3306,13 +3304,44 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( status: completed ? "completed" : "running", title: null, startedAt: segment.startedAt, - completedAt: completed ? now : null, + completedAt, updatedAt: now, type: "reasoning", text, streaming: !completed, }, }); + }); + + /** + * Emits the stream's current segment. A `flushOf` call is a delayed + * flush: it emits only while that segment is still current and has + * unsent text. The permit keeps a flush from landing after the + * segment's completed snapshot. + */ + const emitTextSegment = Effect.fnUntraced(function* ( + context: ActiveAcpTurn, + kind: "user" | "assistant" | "reasoning", + completed: boolean, + flushOf?: ActiveTextSegment, + ) { + const stream = textStreamFor(context, kind); + const segment = stream.current; + if (segment === null) return; + if (flushOf !== undefined && (segment !== flushOf || !segment.dirty)) return; + // The node carries no text, so it changes only when the segment + // opens and when it completes. + const emitsNode = completed || !segment.emitted; + segment.emitted = true; + segment.dirty = false; + yield* writeTextSegment(context, kind, segment, completed, emitsNode); + }, textEmissionPermit.withPermit); + + const promoteAssistantSegment = Effect.fnUntraced(function* ( + context: ActiveAcpTurn, + segment: ActiveTextSegment, + ) { + yield* writeTextSegment(context, "assistant", segment, true, true); }, textEmissionPermit.withPermit); /** @@ -3356,6 +3385,27 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( yield* closeTextStream(context, "assistant"); }); + // A reply that finishes a prompt or wake before background work continues in the + // same run must carry `final_answer`, so a later wake note does not fold it away. + const closeTextStreamsAsFinalAnswer = Effect.fnUntraced(function* (context: ActiveAcpTurn) { + const closing = context.closingAssistant; + context.closingAssistant = null; + yield* closeTextStream(context, "user"); + yield* closeTextStream(context, "reasoning"); + const current = context.assistant.current; + if (current !== null) { + if (closing !== null && current === closing && closing.hasNonWhitespace) { + current.finalAnswer = true; + } + yield* closeTextStream(context, "assistant"); + if (closing === current) return; + } + if (closing !== null && !closing.finalAnswer && closing.hasNonWhitespace) { + closing.finalAnswer = true; + yield* promoteAssistantSegment(context, closing); + } + }); + const openTextSegment = Effect.fnUntraced(function* ( context: ActiveAcpTurn, kind: "user" | "assistant" | "reasoning", @@ -3375,10 +3425,17 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( startedAt: yield* DateTime.now, sourceMessageId, text: "", + hasNonWhitespace: false, emitted: false, dirty: false, } - : { ...closed, emitted: false, dirty: false }; + : { + ...closed, + completedAt: undefined, + finalAnswer: undefined, + emitted: false, + dirty: false, + }; if (closed === undefined) stream.nextSegment += 1; stream.current = segment; return segment; @@ -3412,6 +3469,16 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( segment.sourceMessageId = sourceMessageId; } segment.text += text; + if ( + (!segment.hasNonWhitespace || + (kind === "assistant" && context.closingAssistant !== segment)) && + text.trim().length > 0 + ) { + segment.hasNonWhitespace = true; + if (kind === "assistant") { + context.closingAssistant = segment; + } + } yield* publishTextSegment(context, kind); }); @@ -3430,6 +3497,17 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( segment = yield* openTextSegment(context, kind, messageId); } segment.text = text; + segment.hasNonWhitespace = text.trim().length > 0; + if (kind === "assistant") { + if (segment.hasNonWhitespace) { + context.closingAssistant = segment; + } else { + segment.finalAnswer = undefined; + if (context.closingAssistant?.nativeItemId === segment.nativeItemId) { + context.closingAssistant = null; + } + } + } yield* publishTextSegment(context, kind); }); @@ -4444,6 +4522,10 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( yield* closeTextStream(context, "reasoning"); yield* closeTextStream(context, "user"); const previous = context.tools.get(incoming.toolCallId); + // A brand-new tool call turns any earlier assistant text into pre-tool commentary. + if (previous === undefined && projectedStatus === undefined) { + context.closingAssistant = null; + } const merged = mergeToolCallState(previous, incoming); const normalized = flavor.normalizeToolCall?.(merged) ?? merged; const backgroundTaskId = flavor.extractBackgroundTaskId?.(normalized); @@ -5787,6 +5869,16 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( } else if (context.nativeThreadId === notification.sessionId) { if (update.sessionUpdate === "usage_update") { context.replyOpen = false; + if ( + !context.finalized && + !context.interrupted && + (context.promptSettled + ? context.promptSettledStatus === "completed" + : context.promptSequence === 0 && + acpIsProviderContinuationMessage(context.input.message)) + ) { + yield* closeTextStreamsAsFinalAnswer(context); + } if (context.promptSettled) yield* idleReportedSubagents(context); yield* rearmDeferredFinalize(context); } else if (acpRootSessionUpdateIngestsOutput(notification)) { @@ -6353,6 +6445,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( generation: number, transportRequestId: string, ) { + context.closingAssistant = null; yield* closeTextStreams(context); const parsed = parsePermissionRequest(params); const approvalOptions = flavor.approvalOptions?.(params); @@ -6478,6 +6571,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( transportRequestId: string, ) { const context = yield* activeContext; + context.closingAssistant = null; yield* closeTextStreams(context); const requestId = yield* idAllocator.allocate.runtimeRequest({ driver, @@ -9314,8 +9408,12 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( ? yield* flavor.hasPendingReply(context.nativeThreadId) : false; if (context.finalized) return; - if (deferredBackgroundWork || pendingReply) { - yield* closeTextStreams(context); + if (!context.interrupted && (deferredBackgroundWork || pendingReply)) { + if (status === "completed") { + yield* closeTextStreamsAsFinalAnswer(context); + } else { + yield* closeTextStreams(context); + } yield* schedulePostSettleHoldExpiry(context); if (pendingReply) { context.replyOpen = true; @@ -9326,7 +9424,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( ); return; } - yield* finalizeTurn(context, status); + yield* finalizeTurn(context, context.interrupted ? "interrupted" : status); }), ).pipe( Effect.andThen(() => options.testHooks?.afterPromptCallback?.() ?? Effect.void), @@ -9543,6 +9641,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( user: { current: null, nextSegment: 0, closedNamedSegments: new Map() }, assistant: { current: null, nextSegment: 0, closedNamedSegments: new Map() }, reasoning: { current: null, nextSegment: 0, closedNamedSegments: new Map() }, + closingAssistant: null, contextUsage: rememberedContextUsage ?? turnInput.providerThread.contextUsage ?? null, nativeMetadata: initialNativeMetadata, tools: new Map(), @@ -9830,6 +9929,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( } context.promptSettled = false; context.promptSettledStatus = null; + context.closingAssistant = null; context.backgroundFinalizeGeneration += 1; context.promptSentAt = yield* Clock.currentTimeMillis; context.lastProgressAt = context.promptSentAt;