@@ -8,17 +8,22 @@ import type {
88} from "@t3tools/contracts" ;
99import { ProjectId , ProviderInstanceId , ThreadId } from "@t3tools/contracts" ;
1010import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient" ;
11+ import type * as Cause from "effect/Cause" ;
1112import * as DateTime from "effect/DateTime" ;
13+ import * as Deferred from "effect/Deferred" ;
1214import * as Duration from "effect/Duration" ;
1315import * as Effect from "effect/Effect" ;
1416import * as Option from "effect/Option" ;
17+ import * as PubSub from "effect/PubSub" ;
18+ import * as Queue from "effect/Queue" ;
1519import * as SqlClient from "effect/sql/SqlClient" ;
1620import * as Stream from "effect/Stream" ;
1721import * as TestClock from "effect/testing/TestClock" ;
1822
1923import {
2024 archivedShellStreamItemFromThreadShell ,
2125 buildActiveShellSnapshot ,
26+ bufferShellLiveStream ,
2227 coalesceShellApplicationEvents ,
2328 coalesceStoredThreadEvents ,
2429 composeShellStreamWithEnrichment ,
@@ -54,6 +59,64 @@ const emptyShellSnapshot = {
5459 archivedThreads : [ ] ,
5560} as OrchestrationV2ShellSnapshot ;
5661
62+ it . effect ( "coalesces a slow shell subscriber by identity and location in sequence order" , ( ) =>
63+ Effect . scoped (
64+ Effect . gen ( function * ( ) {
65+ const input = yield * Queue . unbounded <
66+ Extract < OrchestrationV2ShellStreamItem , { readonly sequence : number } > ,
67+ Cause . Done
68+ > ( ) ;
69+ const drained = yield * Deferred . make < void > ( ) ;
70+ const shell = shellFixture ( { id : ThreadId . make ( "same" ) , title : "Before" } ) ;
71+ const project = {
72+ id : ProjectId . make ( "same" ) ,
73+ title : "Before" ,
74+ workspaceRoot : "/workspace/same" ,
75+ repositoryIdentity : null ,
76+ defaultModelSelection : null ,
77+ scripts : [ ] ,
78+ createdAt : "2026-09-01T00:00:00Z" ,
79+ updatedAt : "2026-09-01T00:00:00Z" ,
80+ } ;
81+ const initial = {
82+ kind : "thread.updated" as const ,
83+ sequence : 1 ,
84+ location : "active" as const ,
85+ thread : shell ,
86+ } ;
87+ const updates = [
88+ { kind : "project.updated" , sequence : 2 , project } ,
89+ { ...initial , sequence : 3 } ,
90+ { ...initial , sequence : 4 , location : "archive" } ,
91+ { ...initial , sequence : 5 , thread : { ...shell , id : ThreadId . make ( "other" ) } } ,
92+ {
93+ kind : "project.updated" ,
94+ sequence : 6 ,
95+ project : { ...project , id : ProjectId . make ( "other" ) } ,
96+ } ,
97+ { kind : "project.removed" , sequence : 7 , projectId : project . id } ,
98+ { kind : "thread.removed" , sequence : 8 , location : "active" , threadId : shell . id } ,
99+ { kind : "project.updated" , sequence : 9 , project : { ...project , title : "Recreated" } } ,
100+ { ...initial , sequence : 10 , thread : { ...shell , title : "Recreated" } } ,
101+ { kind : "thread.removed" , sequence : 11 , location : "archive" , threadId : shell . id } ,
102+ ] satisfies Array < Extract < OrchestrationV2ShellStreamItem , { readonly sequence : number } > > ;
103+ const pull = yield * Stream . toPull (
104+ bufferShellLiveStream (
105+ Stream . fromQueue ( input ) . pipe ( Stream . ensuring ( Deferred . succeed ( drained , undefined ) ) ) ,
106+ { maxItems : 6 , maxSerializedBytes : 1_500 } ,
107+ ) ,
108+ ) ;
109+ yield * Queue . offer ( input , initial ) ;
110+ expect ( yield * pull ) . toEqual ( [ initial ] ) ;
111+ yield * Queue . offerAll ( input , updates ) ;
112+ yield * Queue . end ( input ) ;
113+ // Finish producing the burst while the first batch is still awaiting its ACK.
114+ yield * Deferred . await ( drained ) ;
115+ expect ( yield * pull ) . toEqual ( [ updates [ 3 ] , updates [ 4 ] , updates [ 7 ] , updates [ 8 ] , updates [ 9 ] ] ) ;
116+ } ) ,
117+ ) ,
118+ ) ;
119+
57120describe ( "buildActiveShellSnapshot" , ( ) => {
58121 it ( "never duplicates archived rows into the regular shell" , ( ) => {
59122 const active = shellFixture ( { archivedAt : null } ) ;
@@ -356,6 +419,104 @@ describe("shellStreamItemsFromResumeSnapshot", () => {
356419} ) ;
357420
358421describe ( "composeShellStreamWithEnrichment" , ( ) => {
422+ it . effect ( "preserves pending repository resolutions while an enrichment ACK is withheld" , ( ) =>
423+ Effect . scoped (
424+ Effect . gen ( function * ( ) {
425+ // Match ProjectEnrichmentService's sliding notifications and the WS batching path.
426+ const changes = yield * Effect . acquireRelease (
427+ PubSub . sliding < { readonly workspaceRoot : string } > ( 256 ) ,
428+ PubSub . shutdown ,
429+ ) ;
430+ const subscription = yield * PubSub . subscribe ( changes ) ;
431+ const pull = yield * Stream . toPull (
432+ composeShellStreamWithEnrichment ( {
433+ initial : Stream . empty ,
434+ tail : Stream . empty ,
435+ enrichment : Stream . fromSubscription ( subscription ) . pipe (
436+ Stream . groupedWithin ( 64 , Duration . millis ( 25 ) ) ,
437+ Stream . map ( ( changes ) =>
438+ shellStreamItemFromEnrichmentRefresh ( {
439+ snapshot : emptyShellSnapshot ,
440+ changes,
441+ } ) ,
442+ ) ,
443+ ) ,
444+ } ) ,
445+ ) ;
446+
447+ const firstRoots = Array . from ( { length : 64 } , ( _ , index ) => `/workspace/first-${ index } ` ) ;
448+ yield * PubSub . publishAll (
449+ changes ,
450+ firstRoots . map ( ( workspaceRoot ) => ( { workspaceRoot } ) ) ,
451+ ) ;
452+ expect ( yield * pull ) . toMatchObject ( [ { resolvedRepositoryIdentityRoots : firstRoots } ] ) ;
453+
454+ // The next completion can be batched before the client acknowledges the first.
455+ yield * PubSub . publish ( changes , { workspaceRoot : "/workspace/pending" } ) ;
456+ yield * Effect . yieldNow ;
457+ yield * TestClock . adjust ( Duration . millis ( 25 ) ) ;
458+ const laterRoots = Array . from ( { length : 256 } , ( _ , index ) => `/workspace/later-${ index } ` ) ;
459+ yield * PubSub . publishAll (
460+ changes ,
461+ laterRoots . map ( ( workspaceRoot ) => ( { workspaceRoot } ) ) ,
462+ ) ;
463+
464+ const resumed = yield * Stream . fromPull ( Effect . succeed ( pull ) ) . pipe (
465+ Stream . takeUntil (
466+ ( item ) =>
467+ item . resolvedRepositoryIdentityRoots ?. includes ( "/workspace/later-255" ) ?? false ,
468+ ) ,
469+ Stream . runCollect ,
470+ ) ;
471+ expect ( resumed . flatMap ( ( item ) => item . resolvedRepositoryIdentityRoots ?? [ ] ) ) . toEqual ( [
472+ "/workspace/pending" ,
473+ ...laterRoots ,
474+ ] ) ;
475+ } ) ,
476+ ) ,
477+ ) ;
478+
479+ it . effect ( "keeps a merged shell batch charged while the client withholds its ACK" , ( ) =>
480+ Effect . scoped (
481+ Effect . gen ( function * ( ) {
482+ const update = yield * Deferred . make < void > ( ) ;
483+ const closed = yield * Deferred . make < void > ( ) ;
484+ const initial = {
485+ kind : "thread.removed" as const ,
486+ sequence : 1 ,
487+ location : "active" as const ,
488+ threadId : ThreadId . make ( "thread-a" ) ,
489+ } ;
490+ const pull = yield * Stream . toPull (
491+ composeShellStreamWithEnrichment ( {
492+ initial : Stream . empty ,
493+ tail : bufferShellLiveStream (
494+ Stream . concat (
495+ Stream . succeed ( initial ) ,
496+ Stream . fromEffect ( Deferred . await ( update ) ) . pipe (
497+ Stream . map ( ( ) => ( { ...initial , sequence : 2 } ) ) ,
498+ ) ,
499+ ) . pipe ( Stream . ensuring ( Deferred . succeed ( closed , undefined ) ) ) ,
500+ { maxItems : 1 } ,
501+ ) ,
502+ enrichment : Stream . empty ,
503+ } ) ,
504+ ) ;
505+ expect ( yield * pull ) . toEqual ( [ initial ] ) ;
506+ // Let the merge producer run while the client still withholds its ACK.
507+ yield * Effect . yieldNow ;
508+ yield * Deferred . succeed ( update , undefined ) ;
509+ yield * Deferred . await ( closed ) ;
510+ const result = yield * Stream . fromPull ( Effect . succeed ( pull ) ) . pipe (
511+ Stream . runDrain ,
512+ Effect . result ,
513+ ) ;
514+ expect ( result . _tag ) . toBe ( "Failure" ) ;
515+ if ( result . _tag === "Failure" ) expect ( result . failure . _tag ) . toBe ( "LiveStreamBufferError" ) ;
516+ } ) ,
517+ ) ,
518+ ) ;
519+
359520 it . effect (
360521 "emits every initial item before enrichment even when enrichment is already ready" ,
361522 ( ) =>
0 commit comments