Skip to content

Commit b14a686

Browse files
committed
refactor(acp): make reply projector single-dispatch
1 parent c5d00cb commit b14a686

2 files changed

Lines changed: 12 additions & 57 deletions

File tree

‎src/auto-reply/reply/acp-projector.test.ts‎

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,6 @@ function emitTool(projector: Projector, event: Omit<EventOf<"tool_call">, "type"
112112
return projector.onEvent({ type: "tool_call", ...event });
113113
}
114114

115-
function finishTurn(
116-
projector: Projector,
117-
event: EventOf<"done"> | EventOf<"error"> = { type: "done" },
118-
) {
119-
return projector.onEvent(event);
120-
}
121-
122115
async function runHiddenBoundaryCase(params: {
123116
streamOverrides?: Record<string, unknown>;
124117
toolCallId: string;
@@ -230,7 +223,7 @@ describe("createAcpReplyProjector", () => {
230223
await emitText(projector, "done");
231224
allowToolSummaries = false;
232225

233-
await finishTurn(projector);
226+
await projector.flush(true);
234227

235228
expect(deliveries).toEqual([{ kind: "final", text: "done" }]);
236229
});
@@ -258,25 +251,11 @@ describe("createAcpReplyProjector", () => {
258251
await emitText(projector, "I don't");
259252
allowToolSummaries = false;
260253

261-
await finishTurn(projector);
254+
await projector.flush(true);
262255

263256
expect(deliveries).toEqual([{ kind: "final", text: "fallback.\n\nI don't" }]);
264257
});
265258

266-
it("does not suppress identical short text across terminal turn boundaries", async () => {
267-
const { deliveries, projector } = createStreamHarness("live");
268-
269-
await emitText(projector, "A");
270-
await finishTurn(projector, { type: "done", stopReason: "end_turn" });
271-
await emitText(projector, "A");
272-
await finishTurn(projector, { type: "done", stopReason: "end_turn" });
273-
274-
expect(blockDeliveries(deliveries)).toEqual([
275-
{ kind: "block", text: "A" },
276-
{ kind: "block", text: "A" },
277-
]);
278-
});
279-
280259
it("flushes staggered live text deltas after idle gaps", async () => {
281260
vi.useFakeTimers();
282261
try {
@@ -343,7 +322,7 @@ describe("createAcpReplyProjector", () => {
343322
await emitText(projector, " now?");
344323
expect(deliveries).toStrictEqual([]);
345324

346-
await finishTurn(projector);
325+
await projector.flush(true);
347326
expect(deliveries).toHaveLength(3);
348327
expect(deliveries[0]).toEqual({
349328
kind: "tool",
@@ -366,7 +345,7 @@ describe("createAcpReplyProjector", () => {
366345
});
367346
expect(deliveries).toStrictEqual([]);
368347

369-
await finishTurn(projector, { type: "error", message: "turn failed" });
348+
await projector.flush(true);
370349
expect(deliveries).toHaveLength(2);
371350
expect(deliveries[0]).toEqual({
372351
kind: "tool",

‎src/auto-reply/reply/acp-projector.ts‎

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -187,15 +187,13 @@ export function createAcpReplyProjector(params: {
187187
accountId: params.accountId,
188188
deliveryMode: settings.deliveryMode,
189189
});
190-
const createTurnBlockReplyPipeline = () =>
191-
createBlockReplyPipeline({
192-
onBlockReply: async (payload) => {
193-
await params.deliver("block", payload);
194-
},
195-
timeoutMs: ACP_BLOCK_REPLY_TIMEOUT_MS,
196-
coalescing: settings.deliveryMode === "live" ? undefined : streaming.coalescing,
197-
});
198-
let blockReplyPipeline = createTurnBlockReplyPipeline();
190+
const blockReplyPipeline = createBlockReplyPipeline({
191+
onBlockReply: async (payload) => {
192+
await params.deliver("block", payload);
193+
},
194+
timeoutMs: ACP_BLOCK_REPLY_TIMEOUT_MS,
195+
coalescing: settings.deliveryMode === "live" ? undefined : streaming.coalescing,
196+
});
199197
const chunker = new EmbeddedBlockChunker(streaming.chunking);
200198
const liveIdleFlushMs = Math.max(streaming.coalescing.idleMs, ACP_LIVE_IDLE_FLUSH_FLOOR_MS);
201199

@@ -267,23 +265,6 @@ export function createAcpReplyProjector(params: {
267265
}, liveIdleFlushMs);
268266
};
269267

270-
const resetTurnState = () => {
271-
clearLiveIdleTimer();
272-
blockReplyPipeline.stop();
273-
blockReplyPipeline = createTurnBlockReplyPipeline();
274-
emittedOutputChars = 0;
275-
truncationNoticeEmitted = false;
276-
lastStatusHash = undefined;
277-
lastToolHash = undefined;
278-
lastUsageTuple = undefined;
279-
lastVisibleOutputTail = undefined;
280-
pendingHiddenBoundary = false;
281-
liveBufferText = "";
282-
finalOnlyOutputText = "";
283-
pendingToolDeliveries.length = 0;
284-
toolLifecycleById.clear();
285-
};
286-
287268
const flushBufferedToolDeliveries = async (force: boolean) => {
288269
if (!(settings.deliveryMode === "final_only" && force)) {
289270
return;
@@ -434,6 +415,7 @@ export function createAcpReplyProjector(params: {
434415
);
435416
};
436417

418+
// One projector serves one dispatch; terminal settlement belongs to tryDispatchAcpReply.
437419
const onEvent = async (event: AcpRuntimeEvent): Promise<void> => {
438420
params.onProgress?.();
439421
if (event.type === "text_delta") {
@@ -514,12 +496,6 @@ export function createAcpReplyProjector(params: {
514496
return;
515497
}
516498
await emitToolSummary(event);
517-
return;
518-
}
519-
520-
if (event.type === "done" || event.type === "error") {
521-
await flush(true);
522-
resetTurnState();
523499
}
524500
};
525501

0 commit comments

Comments
 (0)