GeezerVison is showing github.com — Keep one ACP answer when a running tool reports progress · felippeoka/f5@243ac08
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 |
|---|
@@ -15,6 +15,8 @@ const exitLogPath = process.env.T3_ACP_EXIT_LOG_PATH; | 15 | 15 | const emitToolCalls = process.env.T3_ACP_EMIT_TOOL_CALLS === "1"; | 16 | 16 | const emitInterleavedAssistantToolCalls = | 17 | 17 | process.env.T3_ACP_EMIT_INTERLEAVED_ASSISTANT_TOOL_CALLS === "1"; | | 18 | +const emitBackgroundToolDuringAnswer = | | 19 | + process.env.T3_ACP_EMIT_BACKGROUND_TOOL_DURING_ANSWER === "1"; | 18 | 20 | const emitGenericToolPlaceholders = process.env.T3_ACP_EMIT_GENERIC_TOOL_PLACEHOLDERS === "1"; | 19 | 21 | const emitAskQuestion = process.env.T3_ACP_EMIT_ASK_QUESTION === "1"; | 20 | 22 | const failSetConfigOption = process.env.T3_ACP_FAIL_SET_CONFIG_OPTION === "1"; | @@ -440,6 +442,47 @@ const program = Effect.gen(function* () { | 440 | 442 | process.exit(1); | 441 | 443 | } | 442 | 444 |
| | 445 | + if (emitBackgroundToolDuringAnswer) { | | 446 | + // A command backgrounded earlier reports progress and then finishes | | 447 | + // while the next answer is still streaming. | | 448 | + const toolCallId = "background-1"; | | 449 | + const say = (text: string) => | | 450 | + agent.client.sessionUpdate({ | | 451 | + sessionId: requestedSessionId, | | 452 | + update: { sessionUpdate: "agent_message_chunk", content: { type: "text", text } }, | | 453 | + }); | | 454 | + const progress = (status: "in_progress" | "completed", stdout: string) => | | 455 | + agent.client.sessionUpdate({ | | 456 | + sessionId: requestedSessionId, | | 457 | + update: { | | 458 | + sessionUpdate: "tool_call_update", | | 459 | + toolCallId, | | 460 | + status, | | 461 | + rawOutput: { stdout }, | | 462 | + }, | | 463 | + }); | | 464 | + yield* agent.client.sessionUpdate({ | | 465 | + sessionId: requestedSessionId, | | 466 | + update: { | | 467 | + sessionUpdate: "tool_call", | | 468 | + toolCallId, | | 469 | + title: "Terminal", | | 470 | + kind: "execute", | | 471 | + status: "in_progress", | | 472 | + rawInput: { command: "sleep 3 && echo done" }, | | 473 | + }, | | 474 | + }); | | 475 | + yield* say("| a | b |\n|---|---|\n| 1 "); | | 476 | + yield* progress("in_progress", "."); | | 477 | + yield* say("| x |\n"); | | 478 | + yield* progress("completed", "done"); | | 479 | + yield* say("| 2 | y |\n"); | | 480 | + // Agents can repeat a terminal update after the call finished. | | 481 | + yield* progress("completed", "done"); | | 482 | + yield* say("| 3 | z |"); | | 483 | + return { stopReason: "end_turn" }; | | 484 | + } | | 485 | + | 443 | 486 | if (emitInterleavedAssistantToolCalls) { | 444 | 487 | const toolCallId = "tool-call-1"; | 445 | 488 |
| |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/provider/acp/AcpJsonRpcConnection.test.ts +44Lines changed: 44 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -441,6 +441,50 @@ describe("AcpSessionRuntime", () => { | 441 | 441 | ), | 442 | 442 | ); | 443 | 443 |
| | 444 | + it.effect("keeps one answer when an earlier tool reports progress mid-stream", () => | | 445 | + Effect.gen(function* () { | | 446 | + const runtime = yield* AcpSessionRuntime; | | 447 | + yield* runtime.start(); | | 448 | + yield* runtime.prompt({ prompt: [{ type: "text", text: "hi" }] }); | | 449 | + | | 450 | + const notes = Array.from(yield* Stream.runCollect(Stream.take(runtime.getEvents(), 9))); | | 451 | + // Neither the progress tick, the completion, nor a repeated completion | | 452 | + // splits the markdown table across assistant items. | | 453 | + expect(notes.map((note) => note._tag)).toEqual([ | | 454 | + "ToolCallUpdated", | | 455 | + "AssistantItemStarted", | | 456 | + "ContentDelta", | | 457 | + "ToolCallUpdated", | | 458 | + "ContentDelta", | | 459 | + "ToolCallUpdated", | | 460 | + "ContentDelta", | | 461 | + "ToolCallUpdated", | | 462 | + "ContentDelta", | | 463 | + ]); | | 464 | + const itemIds = new Set( | | 465 | + notes.flatMap((note) => | | 466 | + note._tag === "ContentDelta" || note._tag === "AssistantItemStarted" ? [note.itemId] : [], | | 467 | + ), | | 468 | + ); | | 469 | + expect(itemIds.size).toBe(1); | | 470 | + }).pipe( | | 471 | + Effect.provide( | | 472 | + AcpSessionRuntime.layer({ | | 473 | + spawn: { | | 474 | + command: bunExe, | | 475 | + args: [mockAgentPath], | | 476 | + env: { T3_ACP_EMIT_BACKGROUND_TOOL_DURING_ANSWER: "1" }, | | 477 | + }, | | 478 | + cwd: process.cwd(), | | 479 | + clientInfo: { name: "t3-test", version: "0.0.0" }, | | 480 | + authMethodId: "test", | | 481 | + }), | | 482 | + ), | | 483 | + Effect.scoped, | | 484 | + Effect.provide(NodeServices.layer), | | 485 | + ), | | 486 | + ); | | 487 | + | 444 | 488 | it.effect("segments assistant text around ACP tool calls", () => | 445 | 489 | Effect.gen(function* () { | 446 | 490 | const runtime = yield* AcpSessionRuntime; | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/provider/acp/AcpSessionRuntime.ts +22-4Lines changed: 22 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -303,6 +303,9 @@ const makeAcpSessionRuntime = ( | 303 | 303 | const modeStateRef = yield* Ref.make<AcpSessionModeState | undefined>(undefined); | 304 | 304 | const toolCallsRef = yield* Ref.make(new Map<string, AcpToolCallState>()); | 305 | 305 | const toolEmissions = new Map<string, { length: number | undefined; skipped: number }>(); | | 306 | + // Recently shown tool calls. A late update to a finished call is not a new | | 307 | + // boundary in the answer, although its progress state is gone. | | 308 | + const shownToolCallIds = new Set<string>(); | 306 | 309 | const assistantSegmentRef = yield* Ref.make<AcpAssistantSegmentState>({ nextSegmentIndex: 0 }); | 307 | 310 | const configOptionsRef = yield* Ref.make(sessionConfigOptionsFromSetup(undefined)); | 308 | 311 | const startStateRef = yield* Ref.make<AcpStartState>({ _tag: "NotStarted" }); | @@ -457,6 +460,7 @@ const makeAcpSessionRuntime = ( | 457 | 460 | modeStateRef, | 458 | 461 | toolCallsRef, | 459 | 462 | toolEmissions, | | 463 | + shownToolCallIds, | 460 | 464 | assistantSegmentRef, | 461 | 465 | params: normalized, | 462 | 466 | ...(hardeningEnabled | @@ -888,6 +892,7 @@ const makeAcpSessionRuntime = ( | 888 | 892 | modeStateRef, | 889 | 893 | toolCallsRef, | 890 | 894 | toolEmissions, | | 895 | + shownToolCallIds, | 891 | 896 | assistantSegmentRef, | 892 | 897 | params: commands, | 893 | 898 | }); | @@ -1092,11 +1097,14 @@ function configOptionCurrentValueMatches( | 1092 | 1097 | return currentValue.trim() === String(value).trim(); | 1093 | 1098 | } | 1094 | 1099 |
| | 1100 | +const MAX_SHOWN_TOOL_CALL_IDS = 256; | | 1101 | + | 1095 | 1102 | const handleSessionUpdate = ({ | 1096 | 1103 | queue, | 1097 | 1104 | modeStateRef, | 1098 | 1105 | toolCallsRef, | 1099 | 1106 | toolEmissions, | | 1107 | + shownToolCallIds, | 1100 | 1108 | assistantSegmentRef, | 1101 | 1109 | params, | 1102 | 1110 | onActivity, | @@ -1105,6 +1113,7 @@ const handleSessionUpdate = ({ | 1105 | 1113 | readonly modeStateRef: Ref.Ref<AcpSessionModeState | undefined>; | 1106 | 1114 | readonly toolCallsRef: Ref.Ref<Map<string, AcpToolCallState>>; | 1107 | 1115 | readonly toolEmissions: Map<string, { length: number | undefined; skipped: number }>; | | 1116 | + readonly shownToolCallIds: Set<string>; | 1108 | 1117 | readonly assistantSegmentRef: Ref.Ref<AcpAssistantSegmentState>; | 1109 | 1118 | readonly params: EffectAcpSchema.SessionNotification; | 1110 | 1119 | readonly onActivity?: Effect.Effect<void>; | @@ -1119,10 +1128,19 @@ const handleSessionUpdate = ({ | 1119 | 1128 | } | 1120 | 1129 | for (const event of parsed.events) { | 1121 | 1130 | if (event._tag === "ToolCallUpdated") { | 1122 | | - yield* closeActiveAssistantSegment({ | 1123 | | - queue, | 1124 | | - assistantSegmentRef, | 1125 | | - }); | | 1131 | + // A new tool call is a boundary in the prose. Progress on a call that | | 1132 | + // is already shown, such as a background command finishing, is not. | | 1133 | + const toolCallId = event.toolCall.toolCallId; | | 1134 | + if (!shownToolCallIds.has(toolCallId)) { | | 1135 | + shownToolCallIds.add(toolCallId); | | 1136 | + // Only recent calls get late updates; keep a long session bounded. | | 1137 | + if (shownToolCallIds.size > MAX_SHOWN_TOOL_CALL_IDS) { | | 1138 | + shownToolCallIds.delete(shownToolCallIds.values().next().value!); | | 1139 | + } | | 1140 | + // A call still running is already on screen, even if it aged out. | | 1141 | + const running = (yield* Ref.get(toolCallsRef)).has(toolCallId); | | 1142 | + if (!running) yield* closeActiveAssistantSegment({ queue, assistantSegmentRef }); | | 1143 | + } | 1126 | 1144 | const { previous, merged } = yield* Ref.modify(toolCallsRef, (current) => { | 1127 | 1145 | const previous = current.get(event.toolCall.toolCallId); | 1128 | 1146 | const nextToolCall = mergeToolCallState(previous, event.toolCall); | |
|