@@ -128,6 +128,15 @@ export const ACP_PROTOCOL = "acp.ndjson-jsonrpc" as const;
128128 */
129129const ACP_DEFERRED_FINALIZE_DEBOUNCE : Duration . Input = "3000 millis" ;
130130
131+ /**
132+ * Coalescing window for streamed text. A text segment's first chunk and its
133+ * close emit at once; a later chunk schedules one full-text snapshot this long
134+ * after it, and the chunks inside the window share it instead of each emitting
135+ * the whole reply, which grew with the square of the reply length. The flush
136+ * is scheduled, not a hard deadline, so it can land later under host load.
137+ */
138+ const ACP_TEXT_FLUSH_INTERVAL : Duration . Input = "50 millis" ;
139+
131140export interface AcpAdapterV2RuntimeInput {
132141 readonly cwd : string ;
133142 readonly runtimePolicy : ProviderAdapter . ProviderAdapterV2RuntimePolicy ;
@@ -957,6 +966,10 @@ interface ActiveTextSegment {
957966 readonly startedAt : DateTime . Utc ;
958967 sourceMessageId : string | null ;
959968 text : string ;
969+ // The running node and first text have been emitted.
970+ emitted : boolean ;
971+ // Text changed since the last emission; a delayed flush is scheduled.
972+ dirty : boolean ;
960973}
961974
962975interface ActiveTextStream {
@@ -2168,14 +2181,32 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
21682181 ? context . assistant
21692182 : context . reasoning ;
21702183
2184+ const textEmissionPermit = yield * Semaphore . make ( 1 ) ;
2185+ /**
2186+ * Emits the stream's current segment. A `flushOf` call is a delayed
2187+ * flush: it emits only while that segment is still current and has
2188+ * unsent text. The permit keeps a flush from landing after the
2189+ * segment's completed snapshot.
2190+ */
21712191 const emitTextSegment = Effect . fnUntraced ( function * (
21722192 context : ActiveAcpTurn ,
21732193 kind : "user" | "assistant" | "reasoning" ,
21742194 completed : boolean ,
2195+ flushOf ?: ActiveTextSegment ,
21752196 ) {
21762197 const stream = textStreamFor ( context , kind ) ;
21772198 const segment = stream . current ;
21782199 if ( segment === null ) return ;
2200+ if ( flushOf !== undefined && ( segment !== flushOf || ! segment . dirty ) ) return ;
2201+ // The node carries no text, so it changes only when the segment
2202+ // opens and when it completes.
2203+ const emitsNode = completed || ! segment . emitted ;
2204+ segment . emitted = true ;
2205+ segment . dirty = false ;
2206+ // Read the text in the same step that clears `dirty`. Chunks keep
2207+ // appending while this emission yields; each one marks the segment
2208+ // dirty again and reaches the next flush or the close.
2209+ const text = segment . text ;
21792210 const now = yield * DateTime . now ;
21802211 const ordinal = yield * resolveItemOrdinal ( context , segment . nativeItemId ) ;
21812212 const nodeId = providerNodeId ( segment . nativeItemId ) ;
@@ -2185,7 +2216,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
21852216 nativeId : segment . nativeItemId ,
21862217 strength : "weak" as const ,
21872218 } ;
2188- if ( kind !== "user" ) {
2219+ if ( kind !== "user" && emitsNode ) {
21892220 yield * emitProviderEvent ( {
21902221 type : "node.updated" ,
21912222 driver,
@@ -2219,7 +2250,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
22192250 runId : context . input . runId ,
22202251 nodeId : messageNodeId ,
22212252 role : kind ,
2222- text : segment . text ,
2253+ text,
22232254 attachments : [ ] ,
22242255 streaming : ! completed ,
22252256 createdAt : segment . startedAt ,
@@ -2251,7 +2282,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
22512282 type : "user_message" ,
22522283 messageId,
22532284 inputIntent : "turn_start" ,
2254- text : segment . text ,
2285+ text,
22552286 attachments : [ ] ,
22562287 } ,
22572288 }
@@ -2275,7 +2306,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
22752306 updatedAt : now ,
22762307 type : "assistant_message" ,
22772308 messageId,
2278- text : segment . text ,
2309+ text,
22792310 streaming : ! completed ,
22802311 } ,
22812312 } ,
@@ -2302,10 +2333,31 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
23022333 completedAt : completed ? now : null ,
23032334 updatedAt : now ,
23042335 type : "reasoning" ,
2305- text : segment . text ,
2336+ text,
23062337 streaming : ! completed ,
23072338 } ,
23082339 } ) ;
2340+ } , textEmissionPermit . withPermit ) ;
2341+
2342+ /**
2343+ * Publishes a text change. A segment's first text emits at once so
2344+ * live output starts immediately; later changes coalesce into one
2345+ * snapshot per ACP_TEXT_FLUSH_INTERVAL. Closing the segment emits its
2346+ * final text and makes a pending flush a no-op.
2347+ */
2348+ const publishTextSegment = Effect . fnUntraced ( function * (
2349+ context : ActiveAcpTurn ,
2350+ kind : "user" | "assistant" | "reasoning" ,
2351+ ) {
2352+ const segment = textStreamFor ( context , kind ) . current ;
2353+ if ( segment === null ) return ;
2354+ if ( ! segment . emitted ) return yield * emitTextSegment ( context , kind , false ) ;
2355+ if ( segment . dirty ) return ;
2356+ segment . dirty = true ;
2357+ yield * emitTextSegment ( context , kind , false , segment ) . pipe (
2358+ Effect . delay ( ACP_TEXT_FLUSH_INTERVAL ) ,
2359+ Effect . forkIn ( sessionScope ) ,
2360+ ) ;
23092361 } ) ;
23102362
23112363 const closeTextStream = Effect . fnUntraced ( function * (
@@ -2354,6 +2406,8 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
23542406 startedAt : now ,
23552407 sourceMessageId,
23562408 text : "" ,
2409+ emitted : false ,
2410+ dirty : false ,
23572411 } ;
23582412 stream . nextSegment += 1 ;
23592413 } else if ( stream . current . sourceMessageId === null && sourceMessageId !== null ) {
@@ -2362,7 +2416,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
23622416 stream . current . sourceMessageId = sourceMessageId ;
23632417 }
23642418 stream . current . text += text ;
2365- yield * emitTextSegment ( context , kind , false ) ;
2419+ yield * publishTextSegment ( context , kind ) ;
23662420 } ) ;
23672421
23682422 const replaceText = Effect . fnUntraced ( function * (
@@ -2383,12 +2437,14 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
23832437 startedAt : now ,
23842438 sourceMessageId : messageId ,
23852439 text,
2440+ emitted : false ,
2441+ dirty : false ,
23862442 } ;
23872443 stream . nextSegment += 1 ;
23882444 } else {
23892445 stream . current . text = text ;
23902446 }
2391- yield * emitTextSegment ( context , kind , false ) ;
2447+ yield * publishTextSegment ( context , kind ) ;
23922448 } ) ;
23932449
23942450 const emitSubagentAssistant = Effect . fnUntraced ( function * (
0 commit comments