@@ -127,6 +127,7 @@ const makeHarness = Effect.fn("TestEnvironmentThreads.makeHarness")(function* (o
127127 const retryCount = yield * Ref . make ( 0 ) ;
128128 const subscriptionCount = yield * Ref . make ( 0 ) ;
129129 const subscriptions = yield * Queue . unbounded < number | undefined > ( ) ;
130+ const streamsEnded = yield * Queue . unbounded < void > ( ) ;
130131 const loaderCalls = yield * Ref . make ( 0 ) ;
131132 const lastSubscribeAfterSequence = yield * Ref . make < number | undefined > ( undefined ) ;
132133 const lastRequestCompletionMarker = yield * Ref . make ( false ) ;
@@ -164,7 +165,7 @@ const makeHarness = Effect.fn("TestEnvironmentThreads.makeHarness")(function* (o
164165 Effect . andThen ( Ref . set ( lastAcceptBoundedSnapshot , input . acceptBoundedSnapshot ) ) ,
165166 Effect . andThen ( Ref . set ( lastAcceptCompactTurnItems , input . acceptCompactTurnItems ) ) ,
166167 Effect . andThen ( Queue . offer ( subscriptions , input . afterSequence ) ) ,
167- Effect . as ( streamFrom ( inputs ) ) ,
168+ Effect . as ( streamFrom ( inputs ) . pipe ( Stream . ensuring ( Queue . offer ( streamsEnded , undefined ) ) ) ) ,
168169 ) ,
169170 ) ,
170171 } as unknown as WsRpcProtocolClient ;
@@ -276,6 +277,7 @@ const makeHarness = Effect.fn("TestEnvironmentThreads.makeHarness")(function* (o
276277 retryCount,
277278 subscriptionCount,
278279 subscriptions,
280+ streamsEnded,
279281 loaderCalls,
280282 lastSubscribeAfterSequence,
281283 lastRequestCompletionMarker,
@@ -1296,6 +1298,64 @@ describe("EnvironmentThreads", () => {
12961298 } ) ,
12971299 ) ;
12981300
1301+ it . effect ( "reloads the snapshot when a session replacement races a failed resume" , ( ) =>
1302+ Effect . gen ( function * ( ) {
1303+ const harness = yield * makeHarness ( { cached : BASE_PROJECTION , completionMarker : true } ) ;
1304+ expect ( yield * Queue . take ( harness . subscriptions ) ) . toBe ( CACHED_SNAPSHOT_SEQUENCE ) ;
1305+
1306+ // Hold state application so the replay defect waits behind an update.
1307+ const applying = yield * Deferred . make < void > ( ) ;
1308+ const releaseApplying = yield * Deferred . make < void > ( ) ;
1309+ yield * SubscriptionRef . updateEffect ( harness . threadState , ( value ) =>
1310+ Deferred . succeed ( applying , undefined ) . pipe (
1311+ Effect . andThen ( Deferred . await ( releaseApplying ) ) ,
1312+ Effect . as ( value ) ,
1313+ ) ,
1314+ ) . pipe ( Effect . forkScoped ) ;
1315+ yield * Deferred . await ( applying ) ;
1316+ yield * Queue . offerAll ( harness . inputs , [
1317+ titleUpdated ( "Replayed update" , CACHED_SNAPSHOT_SEQUENCE + 1 ) ,
1318+ new TestStreamDefect ( "Replayed event did not decode" ) ,
1319+ ] ) ;
1320+ yield * Queue . take ( harness . streamsEnded ) ;
1321+
1322+ // A replacement session starts building its input before the buffered
1323+ // defect reaches the retry.
1324+ const replacementStarted = yield * Deferred . make < void > ( ) ;
1325+ const releaseReplacement = yield * Deferred . make < void > ( ) ;
1326+ const session = Option . getOrThrow ( yield * SubscriptionRef . get ( harness . supervisorSession ) ) ;
1327+ yield * SubscriptionRef . set (
1328+ harness . supervisorSession ,
1329+ Option . some ( {
1330+ ...session ,
1331+ initialConfig : Deferred . succeed ( replacementStarted , undefined ) . pipe (
1332+ Effect . andThen ( Deferred . await ( releaseReplacement ) ) ,
1333+ Effect . andThen ( session . initialConfig ) ,
1334+ ) ,
1335+ } ) ,
1336+ ) ;
1337+ yield * Deferred . await ( replacementStarted ) ;
1338+ yield * Deferred . succeed ( releaseApplying , undefined ) ;
1339+ yield * Deferred . succeed ( releaseReplacement , undefined ) ;
1340+
1341+ expect ( yield * Queue . take ( harness . subscriptions ) ) . toBeUndefined ( ) ;
1342+ yield * Queue . offerAll ( harness . inputs , [
1343+ snapshot (
1344+ { ...BASE_PROJECTION , thread : { ...BASE_PROJECTION . thread , title : "Reloaded thread" } } ,
1345+ CACHED_SNAPSHOT_SEQUENCE + 5 ,
1346+ ) ,
1347+ synchronized ( ) ,
1348+ ] ) ;
1349+ let live = yield * Queue . take ( harness . observed ) ;
1350+ while ( live . status !== "live" ) {
1351+ expect ( Option . isNone ( live . error ) ) . toBe ( true ) ;
1352+ live = yield * Queue . take ( harness . observed ) ;
1353+ }
1354+ expect ( Option . getOrThrow ( live . data ) . thread . title ) . toBe ( "Reloaded thread" ) ;
1355+ expect ( Option . isNone ( live . error ) ) . toBe ( true ) ;
1356+ } ) ,
1357+ ) ;
1358+
12991359 it . effect ( "stops when the snapshot reload after a failed resume also hits a defect" , ( ) =>
13001360 Effect . gen ( function * ( ) {
13011361 const harness = yield * makeHarness ( { cached : BASE_PROJECTION , completionMarker : true } ) ;
0 commit comments