@@ -6,8 +6,11 @@ import {
66 AuthSessionId ,
77 AuthSourceControlWriteScope ,
88 EnvironmentId ,
9+ EventId ,
10+ MessageId ,
911 ProjectId ,
1012 ThreadId ,
13+ TurnItemId ,
1114 ProviderDriverKind ,
1215 ProviderInstanceId ,
1316 SourceControlRepositoryError ,
@@ -37,6 +40,7 @@ import { Headers } from "effect/http";
3740import { RpcGroup , RpcMessage , RpcServer } from "effect/rpc" ;
3841import * as SqlClient from "effect/sql/SqlClient" ;
3942import * as Statement from "effect/sql/Statement" ;
43+ import { vi } from "vite-plus/test" ;
4044
4145import * as AssetSigner from "./assets/AssetSigner.ts" ;
4246import * as PreviewAutomationBroker from "./mcp/PreviewAutomationBroker.ts" ;
@@ -556,6 +560,104 @@ it.effect("shell groups read distinct threads once and keep each event's own seq
556560 } ) . pipe ( Effect . scoped , Effect . provide ( ReplayTestLayer ) ) ,
557561) ;
558562
563+ it . effect ( "shares each live event item across subscribers and dedupes bodies per subscriber" , ( ) =>
564+ Effect . gen ( function * ( ) {
565+ const threadId = ThreadId . make ( "thread:shared-live" ) ;
566+ const messageId = MessageId . make ( "message:shared-live" ) ;
567+ const at = DateTime . makeUnsafe ( "2026-10-31T00:00:00.000Z" ) ;
568+ const owner = { threadId, runId : null , nodeId : null , createdAt : at , updatedAt : at } ;
569+ // DateTime memoizes its formatted parts, so only the plain records are frozen.
570+ const freeze = < A > ( value : A ) : A => {
571+ if ( typeof value === "object" && value !== null && ! DateTime . isDateTime ( value ) ) {
572+ for ( const child of Object . values ( value ) ) freeze ( child ) ;
573+ Object . freeze ( value ) ;
574+ }
575+ return value ;
576+ } ;
577+ const live : ReadonlyArray < OrchestrationV2StoredEvent > = freeze ( [
578+ {
579+ sequence : 1 ,
580+ commandId : null ,
581+ event : {
582+ id : EventId . make ( "event:shared-live:message" ) ,
583+ type : "message.updated" ,
584+ threadId,
585+ occurredAt : at ,
586+ payload : {
587+ ...owner ,
588+ id : messageId ,
589+ role : "assistant" ,
590+ text : "Done" ,
591+ attachments : [ ] ,
592+ streaming : false ,
593+ createdBy : "agent" ,
594+ creationSource : "provider" ,
595+ } ,
596+ } ,
597+ } ,
598+ {
599+ sequence : 2 ,
600+ commandId : null ,
601+ event : {
602+ id : EventId . make ( "event:shared-live:reply" ) ,
603+ type : "turn-item.updated" ,
604+ threadId,
605+ occurredAt : at ,
606+ payload : {
607+ ...owner ,
608+ id : TurnItemId . make ( "item:shared-live:reply" ) ,
609+ type : "assistant_message" ,
610+ providerThreadId : null ,
611+ providerTurnId : null ,
612+ nativeItemRef : null ,
613+ parentItemId : null ,
614+ ordinal : 1 ,
615+ title : null ,
616+ status : "completed" ,
617+ messageId,
618+ text : "Done" ,
619+ streaming : false ,
620+ startedAt : at ,
621+ completedAt : at ,
622+ } ,
623+ } ,
624+ } ,
625+ ] as ReadonlyArray < OrchestrationV2StoredEvent > ) ;
626+ const stringify = yield * Effect . acquireRelease (
627+ Effect . sync ( ( ) => vi . spyOn ( JSON , "stringify" ) ) ,
628+ ( spy ) => Effect . sync ( ( ) => spy . mockRestore ( ) ) ,
629+ ) ;
630+ // The EventSink hands every subscriber the same projected objects.
631+ const subscribe = ( acceptDedupedBodies : boolean ) =>
632+ subscribeOrchestrationV2Thread ( { threadId, afterSequence : 0 , acceptDedupedBodies } ) . pipe (
633+ Effect . flatMap ( Stream . runCollect ) ,
634+ Effect . map ( ( items ) => Array . from ( items ) ) ,
635+ Effect . provideService ( ThreadManagement . ThreadManagementService , {
636+ ensureLegacyTranscript : ( ) => Effect . void ,
637+ streamStoredEventsFrom : ( ) => Stream . fromIterable ( live ) ,
638+ } as unknown as ThreadManagement . ThreadManagementService [ "Service" ] ) ,
639+ ) ;
640+ const [ deduped , first , second ] = yield * Effect . forEach ( [ true , false , false ] , subscribe ) ;
641+
642+ assert . lengthOf ( first ! , live . length ) ;
643+ first ! . forEach ( ( item , index ) => {
644+ assert . strictEqual ( item , second ! [ index ] ) ;
645+ assert . strictEqual ( item . kind === "event" && item . event , live [ index ] ! . event ) ;
646+ } ) ;
647+ assert . strictEqual ( deduped ! [ 0 ] , first ! [ 0 ] ) ;
648+ const reply = deduped ! [ 1 ] ;
649+ assert . deepInclude ( reply ?. kind === "event" ? reply . event . payload : undefined , {
650+ text : "" ,
651+ textOmitted : true ,
652+ } ) ;
653+ // The live buffer sizes each shared item once, not once per subscriber.
654+ assert . lengthOf (
655+ stringify . mock . calls . filter ( ( [ value ] ) => value ?. kind === "event" ) ,
656+ live . length ,
657+ ) ;
658+ } ) . pipe ( Effect . scoped , Effect . provide ( ReplayTestLayer ) ) ,
659+ ) ;
660+
559661it . effect ( "shell snapshots omit idle children and older settled threads only on request" , ( ) =>
560662 Effect . gen ( function * ( ) {
561663 const shell = (
0 commit comments