Skip to content

Commit 758698d

Browse files
juliusmarmingeadeebahmad01claude
committed
fix(acp): keep one answer when a running tool reports progress
AcpSessionRuntime closed the active assistant segment on every parsed tool update, before deciding whether to emit it. A background command that ticked or finished while the agent was still writing split the answer into two bubbles, cutting markdown tables and links in half. A coalesced tick split it with nothing shown in between (#13133). Cursor, Grok and Antigravity all share this runtime. Close the segment only when a new tool call is emitted, which is still where a tool card lands between two answers. Co-Authored-By: adeebahmad01 <[email protected]> Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent e67abcf commit 758698d

3 files changed

Lines changed: 93 additions & 6 deletions

File tree

‎apps/server/scripts/acp-mock-agent.ts‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ const emitToolCalls = process.env.T3_ACP_EMIT_TOOL_CALLS === "1";
1919
const emitInterleavedAssistantToolCalls =
2020
process.env.T3_ACP_EMIT_INTERLEAVED_ASSISTANT_TOOL_CALLS === "1";
2121
const emitGenericToolPlaceholders = process.env.T3_ACP_EMIT_GENERIC_TOOL_PLACEHOLDERS === "1";
22+
const emitBackgroundToolDuringAnswer =
23+
process.env.T3_ACP_EMIT_BACKGROUND_TOOL_DURING_ANSWER === "1";
2224
const emitAskQuestion = process.env.T3_ACP_EMIT_ASK_QUESTION === "1";
2325
const emitXAiAskUserQuestion = process.env.T3_ACP_EMIT_XAI_ASK_USER_QUESTION === "1";
2426
const emitXAiExitPlanMode = process.env.T3_ACP_EMIT_XAI_EXIT_PLAN_MODE === "1";
@@ -941,6 +943,44 @@ const program = Effect.gen(function* () {
941943
return yield* Effect.never;
942944
}
943945

946+
if (emitBackgroundToolDuringAnswer) {
947+
// A command backgrounded earlier reports progress and then finishes
948+
// while the next answer is still streaming.
949+
const toolCallId = "background-1";
950+
const say = (text: string) =>
951+
agent.client.sessionUpdate({
952+
sessionId: requestedSessionId,
953+
update: { sessionUpdate: "agent_message_chunk", content: { type: "text", text } },
954+
});
955+
const progress = (status: "in_progress" | "completed", stdout: string) =>
956+
agent.client.sessionUpdate({
957+
sessionId: requestedSessionId,
958+
update: {
959+
sessionUpdate: "tool_call_update",
960+
toolCallId,
961+
status,
962+
rawOutput: { stdout },
963+
},
964+
});
965+
yield* agent.client.sessionUpdate({
966+
sessionId: requestedSessionId,
967+
update: {
968+
sessionUpdate: "tool_call",
969+
toolCallId,
970+
title: "Terminal",
971+
kind: "execute",
972+
status: "in_progress",
973+
rawInput: { command: "sleep 3 && echo done" },
974+
},
975+
});
976+
yield* say("| a | b |\n|---|---|\n| 1 ");
977+
yield* progress("in_progress", ".");
978+
yield* say("| x |\n");
979+
yield* progress("completed", "done");
980+
yield* say("| 2 | y |");
981+
return { stopReason: "end_turn" };
982+
}
983+
944984
if (emitInterleavedAssistantToolCalls) {
945985
const toolCallId = "tool-call-1";
946986

‎apps/server/src/provider/acp/AcpJsonRpcConnection.test.ts‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,52 @@ describe("AcpSessionRuntime", () => {
824824
),
825825
);
826826

827+
it.effect("keeps one answer when an earlier tool reports progress mid-stream", () =>
828+
Effect.gen(function* () {
829+
const runtime = yield* AcpSessionRuntime.AcpSessionRuntime;
830+
yield* runtime.start();
831+
yield* runtime.prompt({ prompt: [{ type: "text", text: "hi" }] });
832+
833+
const notes = Array.from(yield* Stream.runCollect(Stream.take(runtime.getEvents(), 7)));
834+
// The coalesced progress tick emits nothing and the completion lands
835+
// between deltas; neither splits the markdown table across items.
836+
expect(notes.map((note) => note._tag)).toEqual([
837+
"ToolCallUpdated",
838+
"AssistantItemStarted",
839+
"ContentDelta",
840+
"ContentDelta",
841+
"ToolCallUpdated",
842+
"ContentDelta",
843+
"AssistantItemCompleted",
844+
]);
845+
const itemIds = new Set(
846+
notes.flatMap((note) =>
847+
note._tag === "ContentDelta" ||
848+
note._tag === "AssistantItemStarted" ||
849+
note._tag === "AssistantItemCompleted"
850+
? [note.itemId]
851+
: [],
852+
),
853+
);
854+
expect(itemIds.size).toBe(1);
855+
}).pipe(
856+
Effect.provide(
857+
AcpSessionRuntime.layer({
858+
spawn: {
859+
command: mockAgentCommand,
860+
args: mockAgentArgs,
861+
env: { T3_ACP_EMIT_BACKGROUND_TOOL_DURING_ANSWER: "1" },
862+
},
863+
cwd: process.cwd(),
864+
clientInfo: { name: "t3-test", version: "0.0.0" },
865+
authMethodId: "test",
866+
}),
867+
),
868+
Effect.scoped,
869+
Effect.provide(NodeServices.layer),
870+
),
871+
);
872+
827873
it.effect("emits status-only tool updates through completion", () =>
828874
Effect.gen(function* () {
829875
const runtime = yield* AcpSessionRuntime.AcpSessionRuntime;

‎apps/server/src/provider/acp/AcpSessionRuntime.ts‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1202,11 +1202,7 @@ const handleSessionUpdate = ({
12021202
}
12031203
for (const event of parsed.events) {
12041204
if (event._tag === "ToolCallUpdated") {
1205-
yield* closeActiveAssistantSegment({
1206-
queue,
1207-
assistantSegmentRef,
1208-
});
1209-
const { merged, decision } = yield* Ref.modify(toolCallsRef, (current) => {
1205+
const { merged, decision, isNew } = yield* Ref.modify(toolCallsRef, (current) => {
12101206
const tracked = current.get(event.toolCall.toolCallId);
12111207
const previous = tracked?.state;
12121208
const nextToolCall = mergeToolCallState(previous, event.toolCall);
@@ -1228,11 +1224,16 @@ const handleSessionUpdate = ({
12281224
skippedSinceEmit: decision.skippedSinceEmit,
12291225
});
12301226
}
1231-
return [{ merged: nextToolCall, decision }, next] as const;
1227+
return [{ merged: nextToolCall, decision, isNew: tracked === undefined }, next] as const;
12321228
});
12331229
if (!decision.emit) {
12341230
continue;
12351231
}
1232+
// A new tool call is a boundary in the prose. Progress on a call that
1233+
// is already shown, such as a background command finishing, is not.
1234+
if (isNew) {
1235+
yield* closeActiveAssistantSegment({ queue, assistantSegmentRef });
1236+
}
12361237
yield* Queue.offer(queue, {
12371238
_tag: "ToolCallUpdated",
12381239
toolCall: merged,

0 commit comments

Comments
 (0)