@@ -421,6 +421,127 @@ it.effect("coalesces streaming replay before synchronization and the live tail",
421421 } ) . pipe ( Effect . scoped , Effect . provide ( ReplayTestLayer ) ) ,
422422) ;
423423
424+ it . effect ( "shell groups read distinct threads once and keep each event's own sequence" , ( ) =>
425+ Effect . gen ( function * ( ) {
426+ const sql = yield * SqlClient . SqlClient ;
427+ const now = "2026-10-27T00:00:00.000Z" ;
428+ const insert = ( aggregateKind : "thread" | "project" , streamId : string , version : number ) =>
429+ sql < { sequence : number } > `
430+ INSERT INTO orchestration_events (
431+ event_id, aggregate_kind, stream_id, stream_version, event_type,
432+ occurred_at, actor_kind, payload_json, metadata_json, application_event_version
433+ ) VALUES (
434+ ${ `shell-group:${ streamId } :${ version } ` } , ${ aggregateKind } , ${ streamId } , ${ version } ,
435+ ${ aggregateKind === "thread" ? "provider-session.detached" : "project.meta-updated" } ,
436+ ${ now } , 'server', ${ JSON . stringify (
437+ aggregateKind === "thread"
438+ ? { providerSessionId : "session:shell-group" , detachedAt : now }
439+ : { projectId : streamId , updatedAt : now } ,
440+ ) } , '{}', 2
441+ ) RETURNING sequence
442+ ` . pipe ( Effect . map ( ( [ row ] ) => row ! . sequence ) ) ;
443+ const shell = ( id : string , archivedAt : string | null = null ) =>
444+ ( { id : ThreadId . make ( id ) , archivedAt } ) as unknown as OrchestrationV2ThreadShell ;
445+ const project = {
446+ id : ProjectId . make ( "project:group" ) ,
447+ } as unknown as OrchestrationProjectShell ;
448+
449+ const after = yield * insert ( "project" , "project:before" , 0 ) ;
450+ yield * insert ( "thread" , "thread:active" , 0 ) ;
451+ const archived = yield * insert ( "thread" , "thread:archived" , 0 ) ;
452+ const projectUpdated = yield * insert ( "project" , project . id , 0 ) ;
453+ const active = yield * insert ( "thread" , "thread:active" , 1 ) ;
454+ const missing = yield * insert ( "thread" , "thread:missing" , 0 ) ;
455+
456+ const shellReads : Array < ReadonlyArray < string > | undefined > = [ ] ;
457+ let batchFails = false ;
458+ const collect = ( afterSequence : number ) =>
459+ subscribeOrchestrationV2Shell ( { afterSequence, requestCompletionMarker : true } ) . pipe (
460+ Effect . flatMap ( ( stream ) =>
461+ stream . pipe (
462+ Stream . takeUntil ( ( item ) => item . kind === "synchronized" ) ,
463+ Stream . runCollect ,
464+ ) ,
465+ ) ,
466+ Effect . provideService ( ThreadManagement . ThreadManagementService , {
467+ listActiveSubagentThreadIds : ( ) => Effect . succeed ( [ ] ) ,
468+ getShellSnapshot : ( options ?: { readonly threadIds ?: ReadonlyArray < string > } ) =>
469+ Effect . suspend ( ( ) => {
470+ shellReads . push ( options ?. threadIds ) ;
471+ return batchFails
472+ ? Effect . fail ( "batch failed" )
473+ : Effect . succeed ( {
474+ schemaVersion : 1 ,
475+ snapshotSequence : Number . MAX_SAFE_INTEGER ,
476+ threads : [ shell ( "thread:active" ) ] ,
477+ archivedThreads : [ shell ( "thread:archived" , now ) ] ,
478+ } ) ;
479+ } ) ,
480+ getThreadShell : ( threadId : string ) =>
481+ Effect . sync ( ( ) => {
482+ shellReads . push ( [ `scalar:${ threadId } ` ] ) ;
483+ return shell ( threadId ) ;
484+ } ) ,
485+ } as unknown as ThreadManagement . ThreadManagementService [ "Service" ] ) ,
486+ Effect . provideService ( ProjectStore . ProjectStoreV2 , {
487+ listShells : ( ) => Effect . succeed ( [ ] ) ,
488+ } as unknown as ProjectStore . ProjectStoreV2 [ "Service" ] ) ,
489+ Effect . provideService ( ProjectService . ProjectService , {
490+ getShell : ( ) => Effect . succeedSome ( project ) ,
491+ } as unknown as ProjectService . ProjectService [ "Service" ] ) ,
492+ Effect . provideServiceEffect (
493+ ProjectEnrichment . ProjectEnrichmentService ,
494+ PubSub . unbounded ( ) . pipe (
495+ Effect . map (
496+ ( changes ) =>
497+ ( {
498+ subscribeChanges : PubSub . subscribe ( changes ) ,
499+ } ) as unknown as ProjectEnrichment . ProjectEnrichmentService [ "Service" ] ,
500+ ) ,
501+ ) ,
502+ ) ,
503+ ) ;
504+
505+ const grouped = yield * collect ( after ) ;
506+ assert . deepEqual ( shellReads , [ [ "thread:archived" , "thread:active" , "thread:missing" ] ] ) ;
507+ assert . deepEqual (
508+ grouped . flatMap ( ( item ) : ReadonlyArray < ReadonlyArray < string | number > > =>
509+ item . kind === "thread.updated"
510+ ? [ [ item . kind , item . sequence , item . thread . id ] ]
511+ : item . kind === "thread.removed"
512+ ? [ [ item . kind , item . sequence , item . threadId ] ]
513+ : item . kind === "project.updated"
514+ ? [ [ item . kind , item . sequence , item . project . id ] ]
515+ : [ ] ,
516+ ) ,
517+ [
518+ [ "thread.removed" , archived , "thread:archived" ] ,
519+ [ "project.updated" , projectUpdated , project . id ] ,
520+ [ "thread.updated" , active , "thread:active" ] ,
521+ [ "thread.removed" , missing , "thread:missing" ] ,
522+ ] ,
523+ ) ;
524+
525+ shellReads . length = 0 ;
526+ const singleton = yield * collect ( active ) ;
527+ assert . deepEqual ( shellReads , [ [ "scalar:thread:missing" ] ] ) ;
528+ assert . deepEqual (
529+ singleton . flatMap ( ( item ) => ( item . kind === "thread.updated" ? [ item . sequence ] : [ ] ) ) ,
530+ [ missing ] ,
531+ ) ;
532+
533+ shellReads . length = 0 ;
534+ batchFails = true ;
535+ yield * collect ( after ) ;
536+ assert . deepEqual ( shellReads , [
537+ [ "thread:archived" , "thread:active" , "thread:missing" ] ,
538+ [ "scalar:thread:archived" ] ,
539+ [ "scalar:thread:active" ] ,
540+ [ "scalar:thread:missing" ] ,
541+ ] ) ;
542+ } ) . pipe ( Effect . scoped , Effect . provide ( ReplayTestLayer ) ) ,
543+ ) ;
544+
424545it . effect ( "shell snapshots omit idle children and older settled threads only on request" , ( ) =>
425546 Effect . gen ( function * ( ) {
426547 const shell = (
0 commit comments