GeezerVison is showing github.com — fix(acp): keep one answer when a running tool reports progress (#13386) · IanWorley/t3code@e144732
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/server/scripts/acp-mock-agent.ts +43Lines changed: 43 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -19,6 +19,8 @@ const emitToolCalls = process.env.T3_ACP_EMIT_TOOL_CALLS === "1"; | 19 | 19 | const emitInterleavedAssistantToolCalls = | 20 | 20 | process.env.T3_ACP_EMIT_INTERLEAVED_ASSISTANT_TOOL_CALLS === "1"; | 21 | 21 | 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"; | 22 | 24 | const emitAskQuestion = process.env.T3_ACP_EMIT_ASK_QUESTION === "1"; | 23 | 25 | const emitXAiAskUserQuestion = process.env.T3_ACP_EMIT_XAI_ASK_USER_QUESTION === "1"; | 24 | 26 | const emitXAiExitPlanMode = process.env.T3_ACP_EMIT_XAI_EXIT_PLAN_MODE === "1"; | @@ -952,6 +954,47 @@ const program = Effect.gen(function* () { | 952 | 954 | return yield* Effect.never; | 953 | 955 | } | 954 | 956 |
| | 957 | + if (emitBackgroundToolDuringAnswer) { | | 958 | + // A command backgrounded earlier reports progress and then finishes | | 959 | + // while the next answer is still streaming. | | 960 | + const toolCallId = "background-1"; | | 961 | + const say = (text: string) => | | 962 | + agent.client.sessionUpdate({ | | 963 | + sessionId: requestedSessionId, | | 964 | + update: { sessionUpdate: "agent_message_chunk", content: { type: "text", text } }, | | 965 | + }); | | 966 | + const progress = (status: "in_progress" | "completed", stdout: string) => | | 967 | + agent.client.sessionUpdate({ | | 968 | + sessionId: requestedSessionId, | | 969 | + update: { | | 970 | + sessionUpdate: "tool_call_update", | | 971 | + toolCallId, | | 972 | + status, | | 973 | + rawOutput: { stdout }, | | 974 | + }, | | 975 | + }); | | 976 | + yield* agent.client.sessionUpdate({ | | 977 | + sessionId: requestedSessionId, | | 978 | + update: { | | 979 | + sessionUpdate: "tool_call", | | 980 | + toolCallId, | | 981 | + title: "Terminal", | | 982 | + kind: "execute", | | 983 | + status: "in_progress", | | 984 | + rawInput: { command: "sleep 3 && echo done" }, | | 985 | + }, | | 986 | + }); | | 987 | + yield* say("| a | b |\n|---|---|\n| 1 "); | | 988 | + yield* progress("in_progress", "."); | | 989 | + yield* say("| x |\n"); | | 990 | + yield* progress("completed", "done"); | | 991 | + yield* say("| 2 | y |\n"); | | 992 | + // Agents can repeat a terminal update after the call finished. | | 993 | + yield* progress("completed", "done"); | | 994 | + yield* say("| 3 | z |"); | | 995 | + return { stopReason: "end_turn" }; | | 996 | + } | | 997 | + | 955 | 998 | if (emitInterleavedAssistantToolCalls) { | 956 | 999 | const toolCallId = "tool-call-1"; | 957 | 1000 |
| |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/provider/acp/AcpJsonRpcConnection.test.ts +48Lines changed: 48 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -882,6 +882,54 @@ describe("AcpSessionRuntime", () => { | 882 | 882 | ), | 883 | 883 | ); | 884 | 884 |
| | 885 | + it.effect("keeps one answer when an earlier tool reports progress mid-stream", () => | | 886 | + Effect.gen(function* () { | | 887 | + const runtime = yield* AcpSessionRuntime.AcpSessionRuntime; | | 888 | + yield* runtime.start(); | | 889 | + yield* runtime.prompt({ prompt: [{ type: "text", text: "hi" }] }); | | 890 | + | | 891 | + const notes = Array.from(yield* Stream.runCollect(Stream.take(runtime.getEvents(), 9))); | | 892 | + // The coalesced progress tick emits nothing, and neither the completion | | 893 | + // nor a repeated one splits the markdown table across items. | | 894 | + expect(notes.map((note) => note._tag)).toEqual([ | | 895 | + "ToolCallUpdated", | | 896 | + "AssistantItemStarted", | | 897 | + "ContentDelta", | | 898 | + "ContentDelta", | | 899 | + "ToolCallUpdated", | | 900 | + "ContentDelta", | | 901 | + "ToolCallUpdated", | | 902 | + "ContentDelta", | | 903 | + "AssistantItemCompleted", | | 904 | + ]); | | 905 | + const itemIds = new Set( | | 906 | + notes.flatMap((note) => | | 907 | + note._tag === "ContentDelta" || | | 908 | + note._tag === "AssistantItemStarted" || | | 909 | + note._tag === "AssistantItemCompleted" | | 910 | + ? [note.itemId] | | 911 | + : [], | | 912 | + ), | | 913 | + ); | | 914 | + expect(itemIds.size).toBe(1); | | 915 | + }).pipe( | | 916 | + Effect.provide( | | 917 | + AcpSessionRuntime.layer({ | | 918 | + spawn: { | | 919 | + command: mockAgentCommand, | | 920 | + args: mockAgentArgs, | | 921 | + env: { T3_ACP_EMIT_BACKGROUND_TOOL_DURING_ANSWER: "1" }, | | 922 | + }, | | 923 | + cwd: process.cwd(), | | 924 | + clientInfo: { name: "t3-test", version: "0.0.0" }, | | 925 | + authMethodId: "test", | | 926 | + }), | | 927 | + ), | | 928 | + Effect.scoped, | | 929 | + Effect.provide(NodeServices.layer), | | 930 | + ), | | 931 | + ); | | 932 | + | 885 | 933 | it.effect("emits status-only tool updates through completion", () => | 886 | 934 | Effect.gen(function* () { | 887 | 935 | const runtime = yield* AcpSessionRuntime.AcpSessionRuntime; | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/provider/acp/AcpSessionRuntime.ts +21-6Lines changed: 21 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -40,6 +40,8 @@ import { | 40 | 40 | type AcpToolCallState, | 41 | 41 | } from "./AcpRuntimeModel.ts"; | 42 | 42 |
| | 43 | +const MAX_SHOWN_TOOL_CALL_IDS = 256; | | 44 | + | 43 | 45 | interface AcpToolCallTrackedState { | 44 | 46 | readonly state: AcpToolCallState; | 45 | 47 | readonly lastEmittedDetailLength: number | undefined; | @@ -343,6 +345,9 @@ export const make = ( | 343 | 345 | const eventQueue = yield* Queue.unbounded<AcpSessionRuntimeEvent>(); | 344 | 346 | const modeStateRef = yield* Ref.make<AcpSessionModeState | undefined>(undefined); | 345 | 347 | const toolCallsRef = yield* Ref.make(new Map<string, AcpToolCallTrackedState>()); | | 348 | + // Recently shown tool calls. A late update to a finished call is not a new | | 349 | + // boundary in the answer, although its progress state is gone. | | 350 | + const shownToolCallIds = new Set<string>(); | 346 | 351 | const assistantItemRuntimeId = yield* crypto.randomUUIDv4.pipe( | 347 | 352 | Effect.mapError( | 348 | 353 | (cause) => | @@ -540,6 +545,7 @@ export const make = ( | 540 | 545 | modeStateRef, | 541 | 546 | configOptionsRef, | 542 | 547 | toolCallsRef, | | 548 | + shownToolCallIds, | 543 | 549 | assistantSegmentRef, | 544 | 550 | assistantItemRuntimeId, | 545 | 551 | params: notification, | @@ -1182,6 +1188,7 @@ const handleSessionUpdate = ({ | 1182 | 1188 | availableCommandsReady, | 1183 | 1189 | configOptionsRef, | 1184 | 1190 | toolCallsRef, | | 1191 | + shownToolCallIds, | 1185 | 1192 | assistantSegmentRef, | 1186 | 1193 | assistantItemRuntimeId, | 1187 | 1194 | params, | @@ -1194,6 +1201,7 @@ const handleSessionUpdate = ({ | 1194 | 1201 | >; | 1195 | 1202 | readonly configOptionsRef: Ref.Ref<ReadonlyArray<EffectAcpSchema.SessionConfigOption>>; | 1196 | 1203 | readonly toolCallsRef: Ref.Ref<Map<string, AcpToolCallTrackedState>>; | | 1204 | + readonly shownToolCallIds: Set<string>; | 1197 | 1205 | readonly assistantSegmentRef: Ref.Ref<AcpAssistantSegmentState>; | 1198 | 1206 | readonly assistantItemRuntimeId: string; | 1199 | 1207 | readonly params: EffectAcpSchema.SessionNotification; | @@ -1215,11 +1223,7 @@ const handleSessionUpdate = ({ | 1215 | 1223 | } | 1216 | 1224 | for (const event of parsed.events) { | 1217 | 1225 | if (event._tag === "ToolCallUpdated") { | 1218 | | - yield* closeActiveAssistantSegment({ | 1219 | | - queue, | 1220 | | - assistantSegmentRef, | 1221 | | - }); | 1222 | | - const { merged, decision } = yield* Ref.modify(toolCallsRef, (current) => { | | 1226 | + const { merged, decision, active } = yield* Ref.modify(toolCallsRef, (current) => { | 1223 | 1227 | const tracked = current.get(event.toolCall.toolCallId); | 1224 | 1228 | const previous = tracked?.state; | 1225 | 1229 | const nextToolCall = mergeToolCallState(previous, event.toolCall); | @@ -1241,11 +1245,22 @@ const handleSessionUpdate = ({ | 1241 | 1245 | skippedSinceEmit: decision.skippedSinceEmit, | 1242 | 1246 | }); | 1243 | 1247 | } | 1244 | | - return [{ merged: nextToolCall, decision }, next] as const; | | 1248 | + return [{ merged: nextToolCall, decision, active: tracked !== undefined }, next] as const; | 1245 | 1249 | }); | 1246 | 1250 | if (!decision.emit) { | 1247 | 1251 | continue; | 1248 | 1252 | } | | 1253 | + // A new tool call is a boundary in the prose. Progress on a call that | | 1254 | + // is already shown, such as a background command finishing, is not. | | 1255 | + if (!shownToolCallIds.has(merged.toolCallId)) { | | 1256 | + shownToolCallIds.add(merged.toolCallId); | | 1257 | + // Only recent calls get late updates; keep a long session bounded. | | 1258 | + if (shownToolCallIds.size > MAX_SHOWN_TOOL_CALL_IDS) { | | 1259 | + shownToolCallIds.delete(shownToolCallIds.values().next().value!); | | 1260 | + } | | 1261 | + // A call still running is already on screen, even if it aged out. | | 1262 | + if (!active) yield* closeActiveAssistantSegment({ queue, assistantSegmentRef }); | | 1263 | + } | 1249 | 1264 | yield* Queue.offer(queue, { | 1250 | 1265 | _tag: "ToolCallUpdated", | 1251 | 1266 | toolCall: merged, | |
|