@@ -123,6 +123,190 @@ const makePersistenceHarness = Effect.fn("test.makeShellPersistenceHarness")(fun
123123} ) ;
124124
125125describe ( "environment shell synchronization" , ( ) => {
126+ it . effect . each ( [
127+ { bufferSize : Infinity , expectedSequences : [ 51 ] , existingRows : false } ,
128+ { bufferSize : Infinity , expectedSequences : [ 51 ] , existingRows : true } ,
129+ // RpcClient defaults to a 16-event buffer, which splits larger server chunks.
130+ { bufferSize : 16 , expectedSequences : [ 17 , 33 , 49 , 51 ] , existingRows : false } ,
131+ { bufferSize : 16 , expectedSequences : [ 17 , 33 , 49 , 51 ] , existingRows : true } ,
132+ ] ) ( "batches $bufferSize events (existing: $existingRows)" , ( input ) =>
133+ Effect . gen ( function * ( ) {
134+ const { bufferSize, expectedSequences, existingRows } = input ;
135+ const events = yield * Queue . bounded < OrchestrationV2ShellStreamItem > ( bufferSize ) ;
136+ let rowVisits = 0 ;
137+ const rows = Array . from ( { length : 50 } , ( _ , index ) => ( {
138+ ...v2ThreadShell ,
139+ get id ( ) {
140+ rowVisits += 1 ;
141+ return ThreadId . make ( `thread-${ index } ` ) ;
142+ } ,
143+ } ) ) ;
144+ const batchSnapshot = { ...LIVE_SHELL_SNAPSHOT , threads : existingRows ? rows : [ ] } ;
145+ const client = {
146+ [ ORCHESTRATION_V2_WS_METHODS . subscribeShell ] : ( ) => Stream . fromQueue ( events ) ,
147+ } as unknown as WsRpcProtocolClient ;
148+ const supervisorState = yield * SubscriptionRef . make ( AVAILABLE_CONNECTION_STATE ) ;
149+ const activeSession = yield * SubscriptionRef . make < Option . Option < RpcSession . RpcSession > > (
150+ Option . some ( session ( client ) ) ,
151+ ) ;
152+ const supervisor = EnvironmentSupervisor . EnvironmentSupervisor . of ( {
153+ target : TARGET ,
154+ state : supervisorState ,
155+ session : activeSession ,
156+ prepared : yield * SubscriptionRef . make ( Option . some ( PREPARED ) ) ,
157+ connect : Effect . void ,
158+ disconnect : Effect . void ,
159+ retryNow : Effect . void ,
160+ } satisfies EnvironmentSupervisor . EnvironmentSupervisor [ "Service" ] ) ;
161+ const cache = Persistence . EnvironmentCacheStore . of ( {
162+ loadShell : ( ) => Effect . succeedNone ,
163+ saveShell : ( ) => Effect . void ,
164+ loadThread : ( ) => Effect . succeedNone ,
165+ saveThread : ( ) => Effect . void ,
166+ removeThread : ( ) => Effect . void ,
167+ loadServerConfig : ( ) => Effect . succeedNone ,
168+ saveServerConfig : ( ) => Effect . void ,
169+ loadVcsRefs : ( ) => Effect . succeedNone ,
170+ saveVcsRefs : ( ) => Effect . void ,
171+ removeVcsRefs : ( ) => Effect . void ,
172+ clearVcsRefs : ( ) => Effect . void ,
173+ clear : ( ) => Effect . void ,
174+ } ) ;
175+ const shellState = yield * makeEnvironmentShellState ( ) . pipe (
176+ Effect . provideService ( EnvironmentSupervisor . EnvironmentSupervisor , supervisor ) ,
177+ Effect . provideService ( Persistence . EnvironmentCacheStore , cache ) ,
178+ Effect . provideService (
179+ ShellSnapshotLoader . ShellSnapshotLoader ,
180+ ShellSnapshotLoader . ShellSnapshotLoader . of ( {
181+ load : ( ) => Effect . succeedNone ,
182+ } ) ,
183+ ) ,
184+ ) ;
185+ yield * SubscriptionRef . set ( supervisorState , {
186+ desired : true ,
187+ network : "online" ,
188+ phase : "connected" ,
189+ stage : null ,
190+ attempt : 1 ,
191+ generation : 1 ,
192+ lastFailure : null ,
193+ retryAt : null ,
194+ } ) ;
195+ yield * Queue . offer ( events , { kind : "snapshot" , snapshot : batchSnapshot } ) ;
196+ yield * Queue . offer ( events , { kind : "synchronized" } ) ;
197+ yield * SubscriptionRef . changes ( shellState ) . pipe (
198+ Stream . filter ( ( state ) => state . status === "live" ) ,
199+ Stream . runHead ,
200+ ) ;
201+
202+ // Observe before publishing so no batch can arrive before the subscription.
203+ const observed = yield * SubscriptionRef . changes ( shellState ) . pipe (
204+ Stream . drop ( 1 ) ,
205+ Stream . takeUntil (
206+ ( state ) => Option . isSome ( state . snapshot ) && state . snapshot . value . snapshotSequence === 51 ,
207+ ) ,
208+ Stream . runCollect ,
209+ Effect . forkScoped ( { startImmediately : true } ) ,
210+ ) ;
211+ rowVisits = 0 ;
212+ yield * Queue . offerAll (
213+ events ,
214+ Array . from ( { length : 50 } , ( _ , index ) => ( {
215+ kind : "thread.updated" as const ,
216+ sequence : 2 + index ,
217+ location : "active" as const ,
218+ thread : {
219+ ...v2ThreadShell ,
220+ get id ( ) {
221+ rowVisits += 1 ;
222+ return ThreadId . make ( `thread-${ index } ` ) ;
223+ } ,
224+ title : "Updated" ,
225+ } ,
226+ } ) ) ,
227+ ) ;
228+ const states = yield * Fiber . join ( observed ) ;
229+ if ( existingRows ) {
230+ // Index each received batch once; distinct updates must not rescan every row.
231+ expect ( rowVisits ) . toBeLessThanOrEqual ( 50 * ( expectedSequences . length + 10 ) ) ;
232+ }
233+ const snapshots = states . map ( ( state ) => Option . getOrThrow ( state . snapshot ) ) ;
234+ expect ( snapshots . map ( ( snapshot ) => snapshot . snapshotSequence ) ) . toEqual ( expectedSequences ) ;
235+ expect ( snapshots . at ( - 1 ) ! . threads . map ( ( thread ) => thread . id ) ) . toEqual (
236+ Array . from ( { length : 50 } , ( _ , index ) => `thread-${ index } ` ) ,
237+ ) ;
238+
239+ const removedObserved = yield * SubscriptionRef . changes ( shellState ) . pipe (
240+ Stream . filter (
241+ ( state ) => Option . isSome ( state . snapshot ) && state . snapshot . value . snapshotSequence === 52 ,
242+ ) ,
243+ Stream . runHead ,
244+ Effect . forkScoped ( { startImmediately : true } ) ,
245+ ) ;
246+ yield * Queue . offerAll ( events , [
247+ { kind : "future.event" , sequence : 99 } as never ,
248+ {
249+ kind : "thread.removed" ,
250+ sequence : 52 ,
251+ location : "active" ,
252+ threadId : ThreadId . make ( "thread-0" ) ,
253+ } ,
254+ {
255+ kind : "thread.removed" ,
256+ sequence : 51 ,
257+ location : "active" ,
258+ threadId : ThreadId . make ( "thread-1" ) ,
259+ } ,
260+ ] ) ;
261+ const removedState = Option . getOrThrow ( yield * Fiber . join ( removedObserved ) ) ;
262+ expect ( removedState . generation ) . toBe ( states . at ( - 1 ) ! . generation ) ;
263+ expect ( [ ...removedState . removedAt ] ) . toEqual ( [ [ ThreadId . make ( "thread-0" ) , 52 ] ] ) ;
264+
265+ // An authoritative snapshot resets even a higher live cursor in the same batch.
266+ const resetRow = { ...v2ThreadShell , id : ThreadId . make ( "reset-row" ) } ;
267+ const resetObserved = yield * SubscriptionRef . changes ( shellState ) . pipe (
268+ Stream . filter (
269+ ( state ) => Option . isSome ( state . snapshot ) && state . snapshot . value . snapshotSequence === 4 ,
270+ ) ,
271+ Stream . runHead ,
272+ Effect . forkScoped ( { startImmediately : true } ) ,
273+ ) ;
274+ yield * Queue . offerAll ( events , [
275+ {
276+ kind : "thread.removed" ,
277+ sequence : 52 ,
278+ location : "active" ,
279+ threadId : ThreadId . make ( "thread-0" ) ,
280+ } ,
281+ {
282+ kind : "snapshot" ,
283+ snapshot : { ...LIVE_SHELL_SNAPSHOT , snapshotSequence : 2 , threads : [ resetRow ] } ,
284+ } ,
285+ { kind : "thread.removed" , sequence : 2 , location : "active" , threadId : resetRow . id } ,
286+ {
287+ kind : "thread.updated" ,
288+ sequence : 3 ,
289+ location : "active" ,
290+ thread : { ...resetRow , title : "After reset" } ,
291+ } ,
292+ { kind : "synchronized" } ,
293+ {
294+ kind : "thread.updated" ,
295+ sequence : 4 ,
296+ location : "active" ,
297+ thread : { ...resetRow , title : "After completion" } ,
298+ } ,
299+ ] ) ;
300+ const resetState = Option . getOrThrow ( yield * Fiber . join ( resetObserved ) ) ;
301+ expect ( resetState . status ) . toBe ( "live" ) ;
302+ expect ( resetState . generation ) . toBeGreaterThan ( removedState . generation ) ;
303+ expect ( resetState . removedAt . size ) . toBe ( 0 ) ;
304+ expect ( Option . getOrThrow ( resetState . snapshot ) . threads ) . toEqual ( [
305+ { ...resetRow , title : "After completion" } ,
306+ ] ) ;
307+ } ) . pipe ( Effect . scoped ) ,
308+ ) ;
309+
126310 it . effect ( "skips unchanged shell writes, persists resets and flushes changed rows" , ( ) =>
127311 Effect . gen ( function * ( ) {
128312 const saved = yield * SubscriptionRef . make < ReadonlyArray < OrchestrationV2ShellSnapshot > > ( [ ] ) ;
0 commit comments