@@ -150,7 +150,7 @@ describe("environment shell synchronization", () => {
150150 } ) ,
151151 ) ;
152152
153- it . effect ( "resumes from a warm shell cache without reloading the HTTP snapshot" , ( ) =>
153+ it . effect ( "replaces a warm shell cache with an authoritative snapshot on a new session " , ( ) =>
154154 Effect . gen ( function * ( ) {
155155 const cachedSnapshot : OrchestrationShellSnapshot = {
156156 snapshotSequence : 5 ,
@@ -219,22 +219,19 @@ describe("environment shell synchronization", () => {
219219 Effect . provideService ( ShellSnapshotLoader , snapshotLoader ) ,
220220 ) ;
221221
222- // Wait until the subscription is established from the warm cache .
222+ // A new session refreshes the cache before opening the stream .
223223 yield * SubscriptionRef . changes ( capturedAfterSequence ) . pipe (
224224 Stream . filter ( ( value ) => value !== undefined ) ,
225225 Stream . runHead ,
226226 ) ;
227227
228- expect ( yield * SubscriptionRef . get ( capturedAfterSequence ) ) . toBe ( 5 ) ;
228+ expect ( yield * SubscriptionRef . get ( capturedAfterSequence ) ) . toBe ( 9_999 ) ;
229229 expect ( yield * Ref . get ( capturedCompletionMarker ) ) . toBe ( true ) ;
230- expect ( yield * SubscriptionRef . get ( loaderCalls ) ) . toBe ( 0 ) ;
230+ expect ( yield * SubscriptionRef . get ( loaderCalls ) ) . toBe ( 1 ) ;
231231 const synchronizing = yield * SubscriptionRef . get ( shellState ) ;
232232 expect ( synchronizing . status ) . toBe ( "synchronizing" ) ;
233- expect ( Option . getOrThrow ( synchronizing . snapshot ) ) . toEqual ( cachedSnapshot ) ;
233+ expect ( Option . getOrThrow ( synchronizing . snapshot ) ) . toEqual ( resetSnapshot ) ;
234234
235- // When the resume gap overflows SHELL_RESUME_MAX_GAP the server resets
236- // the stream with a fresh snapshot; the client applies it as usual.
237- yield * Queue . offer ( events , { kind : "snapshot" , snapshot : resetSnapshot } ) ;
238235 yield * Queue . offer ( events , { kind : "synchronized" } ) ;
239236 yield * SubscriptionRef . changes ( shellState ) . pipe (
240237 Stream . filter ( ( value ) => value . status === "live" ) ,
@@ -243,7 +240,7 @@ describe("environment shell synchronization", () => {
243240
244241 const live = yield * SubscriptionRef . get ( shellState ) ;
245242 expect ( Option . getOrThrow ( live . snapshot ) ) . toEqual ( resetSnapshot ) ;
246- expect ( yield * SubscriptionRef . get ( loaderCalls ) ) . toBe ( 0 ) ;
243+ expect ( yield * SubscriptionRef . get ( loaderCalls ) ) . toBe ( 1 ) ;
247244 } ) ,
248245 ) ;
249246
@@ -263,10 +260,11 @@ describe("environment shell synchronization", () => {
263260 ) ,
264261 } as unknown as WsRpcProtocolClient ;
265262 const supervisorState = yield * SubscriptionRef . make ( AVAILABLE_CONNECTION_STATE ) ;
263+ const activeSession = yield * SubscriptionRef . make ( Option . some ( session ( client ) ) ) ;
266264 const supervisor = EnvironmentSupervisor . EnvironmentSupervisor . of ( {
267265 target : TARGET ,
268266 state : supervisorState ,
269- session : yield * SubscriptionRef . make ( Option . some ( session ( client ) ) ) ,
267+ session : activeSession ,
270268 prepared : yield * SubscriptionRef . make ( Option . some ( PREPARED ) ) ,
271269 connect : Effect . void ,
272270 disconnect : Effect . void ,
@@ -304,12 +302,12 @@ describe("environment shell synchronization", () => {
304302 ) ,
305303 ) ;
306304
307- // The warm cache resumes at its own cursor without an HTTP load .
305+ // A new session starts from an authoritative HTTP snapshot .
308306 for ( let attempt = 0 ; attempt < 100 ; attempt += 1 ) {
309307 if ( ( yield * Ref . get ( capturedAfterSequences ) ) . length >= 1 ) break ;
310308 yield * Effect . yieldNow ;
311309 }
312- expect ( yield * Ref . get ( capturedAfterSequences ) ) . toEqual ( [ 1 ] ) ;
310+ expect ( yield * Ref . get ( capturedAfterSequences ) ) . toEqual ( [ 10 ] ) ;
313311 yield * Queue . offer ( events , { kind : "synchronized" } ) ;
314312 yield * SubscriptionRef . changes ( shellState ) . pipe (
315313 Stream . filter ( ( value ) => value . status === "live" ) ,
@@ -333,24 +331,31 @@ describe("environment shell synchronization", () => {
333331 if ( ( yield * Ref . get ( capturedAfterSequences ) ) . length >= 2 ) break ;
334332 yield * Effect . yieldNow ;
335333 }
336- expect ( yield * Ref . get ( capturedAfterSequences ) ) . toEqual ( [ 1 , 40 ] ) ;
334+ expect ( yield * Ref . get ( capturedAfterSequences ) ) . toEqual ( [ 10 , 40 ] ) ;
337335 yield * Queue . offer ( events , { kind : "synchronized" } ) ;
338336
339337 yield * Queue . offer ( wakeups , "application-active-probe" ) ;
340338 for ( let attempt = 0 ; attempt < 100 ; attempt += 1 ) {
341339 if ( ( yield * Ref . get ( capturedAfterSequences ) ) . length >= 3 ) break ;
342340 yield * Effect . yieldNow ;
343341 }
344- expect ( yield * Ref . get ( capturedAfterSequences ) ) . toEqual ( [ 1 , 40 , 40 ] ) ;
342+ expect ( yield * Ref . get ( capturedAfterSequences ) ) . toEqual ( [ 10 , 40 , 40 ] ) ;
345343
346344 yield * Queue . offer ( wakeups , "application-active-reconnect" ) ;
347345 for ( let attempt = 0 ; attempt < 10 ; attempt += 1 ) {
348346 yield * Effect . yieldNow ;
349347 }
350348 expect ( ( yield * Ref . get ( capturedAfterSequences ) ) . length ) . toBe ( 3 ) ;
351- // The in-memory cursor made every resubscription warm: the HTTP
352- // snapshot loader is never consulted.
353- expect ( yield * Ref . get ( loaderCalls ) ) . toBe ( 0 ) ;
349+ expect ( yield * Ref . get ( loaderCalls ) ) . toBe ( 1 ) ;
350+
351+ // Replacing the session performs another authoritative refresh.
352+ yield * SubscriptionRef . set ( activeSession , Option . some ( session ( client ) ) ) ;
353+ for ( let attempt = 0 ; attempt < 100 ; attempt += 1 ) {
354+ if ( ( yield * Ref . get ( capturedAfterSequences ) ) . length >= 4 ) break ;
355+ yield * Effect . yieldNow ;
356+ }
357+ expect ( yield * Ref . get ( capturedAfterSequences ) ) . toEqual ( [ 10 , 40 , 40 , 20 ] ) ;
358+ expect ( yield * Ref . get ( loaderCalls ) ) . toBe ( 2 ) ;
354359 } ) ,
355360 ) ;
356361} ) ;
0 commit comments