@@ -67,7 +67,15 @@ const BASE_PROJECTION: OrchestrationV2ThreadProjection = {
6767 thread : { ...v2Projection . thread , title : "Cached thread" } ,
6868} ;
6969
70- type TestThreadInput = OrchestrationV2ThreadStreamItem | Error ;
70+ /** Ends the stream with a defect, like a chunk the client cannot decode. */
71+ class TestStreamDefect {
72+ readonly message : string ;
73+ constructor ( message : string ) {
74+ this . message = message ;
75+ }
76+ }
77+
78+ type TestThreadInput = OrchestrationV2ThreadStreamItem | Error | TestStreamDefect ;
7179
7280function testSession (
7381 client : WsRpcProtocolClient ,
@@ -118,6 +126,7 @@ const makeHarness = Effect.fn("TestEnvironmentThreads.makeHarness")(function* (o
118126 const latest = yield * Ref . make < EnvironmentThreadState > ( EMPTY_ENVIRONMENT_THREAD_STATE ) ;
119127 const retryCount = yield * Ref . make ( 0 ) ;
120128 const subscriptionCount = yield * Ref . make ( 0 ) ;
129+ const subscriptions = yield * Queue . unbounded < number | undefined > ( ) ;
121130 const loaderCalls = yield * Ref . make ( 0 ) ;
122131 const lastSubscribeAfterSequence = yield * Ref . make < number | undefined > ( undefined ) ;
123132 const lastRequestCompletionMarker = yield * Ref . make ( false ) ;
@@ -132,7 +141,11 @@ const makeHarness = Effect.fn("TestEnvironmentThreads.makeHarness")(function* (o
132141 const streamFrom = ( queue : Queue . Queue < TestThreadInput > ) =>
133142 Stream . fromQueue ( queue ) . pipe (
134143 Stream . mapEffect ( ( input ) =>
135- input instanceof Error ? Effect . fail ( input ) : Effect . succeed ( input ) ,
144+ input instanceof Error
145+ ? Effect . fail ( input )
146+ : input instanceof TestStreamDefect
147+ ? Effect . die ( new Error ( input . message ) )
148+ : Effect . succeed ( input ) ,
136149 ) ,
137150 ) ;
138151 const client = {
@@ -150,6 +163,7 @@ const makeHarness = Effect.fn("TestEnvironmentThreads.makeHarness")(function* (o
150163 ) ,
151164 Effect . andThen ( Ref . set ( lastAcceptBoundedSnapshot , input . acceptBoundedSnapshot ) ) ,
152165 Effect . andThen ( Ref . set ( lastAcceptCompactTurnItems , input . acceptCompactTurnItems ) ) ,
166+ Effect . andThen ( Queue . offer ( subscriptions , input . afterSequence ) ) ,
153167 Effect . as ( streamFrom ( inputs ) ) ,
154168 ) ,
155169 ) ,
@@ -261,6 +275,7 @@ const makeHarness = Effect.fn("TestEnvironmentThreads.makeHarness")(function* (o
261275 latest,
262276 retryCount,
263277 subscriptionCount,
278+ subscriptions,
264279 loaderCalls,
265280 lastSubscribeAfterSequence,
266281 lastRequestCompletionMarker,
@@ -1251,6 +1266,54 @@ describe("EnvironmentThreads", () => {
12511266 } ) ,
12521267 ) ;
12531268
1269+ it . effect ( "reloads the full snapshot after a resumed replay hits a defect" , ( ) =>
1270+ Effect . gen ( function * ( ) {
1271+ const harness = yield * makeHarness ( { cached : BASE_PROJECTION , completionMarker : true } ) ;
1272+ expect ( yield * Queue . take ( harness . subscriptions ) ) . toBe ( CACHED_SNAPSHOT_SEQUENCE ) ;
1273+
1274+ yield * Queue . offer ( harness . inputs , new TestStreamDefect ( "Replayed event did not decode" ) ) ;
1275+ expect ( yield * Queue . take ( harness . subscriptions ) ) . toBeUndefined ( ) ;
1276+
1277+ yield * Queue . offerAll ( harness . inputs , [
1278+ snapshot (
1279+ { ...BASE_PROJECTION , thread : { ...BASE_PROJECTION . thread , title : "Reloaded thread" } } ,
1280+ CACHED_SNAPSHOT_SEQUENCE + 5 ,
1281+ ) ,
1282+ synchronized ( ) ,
1283+ ] ) ;
1284+ // The failed resume is retried silently: no state on the way to live
1285+ // reports an error.
1286+ let live = yield * Queue . take ( harness . observed ) ;
1287+ while ( live . status !== "live" ) {
1288+ expect ( Option . isNone ( live . error ) ) . toBe ( true ) ;
1289+ live = yield * Queue . take ( harness . observed ) ;
1290+ }
1291+ expect ( Option . getOrThrow ( live . data ) . thread . title ) . toBe ( "Reloaded thread" ) ;
1292+ expect ( Option . isNone ( live . error ) ) . toBe ( true ) ;
1293+
1294+ yield * harness . replaceSession ;
1295+ expect ( yield * Queue . take ( harness . subscriptions ) ) . toBe ( CACHED_SNAPSHOT_SEQUENCE + 5 ) ;
1296+ } ) ,
1297+ ) ;
1298+
1299+ it . effect ( "stops when the snapshot reload after a failed resume also hits a defect" , ( ) =>
1300+ Effect . gen ( function * ( ) {
1301+ const harness = yield * makeHarness ( { cached : BASE_PROJECTION , completionMarker : true } ) ;
1302+ expect ( yield * Queue . take ( harness . subscriptions ) ) . toBe ( CACHED_SNAPSHOT_SEQUENCE ) ;
1303+ yield * Queue . offer ( harness . inputs , new TestStreamDefect ( "Replayed event did not decode" ) ) ;
1304+ expect ( yield * Queue . take ( harness . subscriptions ) ) . toBeUndefined ( ) ;
1305+
1306+ // A failed resume never publishes an error, so this one is the snapshot attempt's.
1307+ yield * Queue . offer ( harness . inputs , new TestStreamDefect ( "Snapshot did not decode" ) ) ;
1308+ const failed = yield * awaitThreadState ( harness . observed , ( value ) =>
1309+ Option . isSome ( value . error ) ,
1310+ ) ;
1311+ expect ( failed . status ) . not . toBe ( "live" ) ;
1312+ expect ( Option . getOrThrow ( failed . error ) ) . toBe ( "Could not synchronize the thread." ) ;
1313+ expect ( yield * Queue . size ( harness . subscriptions ) ) . toBe ( 0 ) ;
1314+ } ) ,
1315+ ) ;
1316+
12541317 it . effect ( "keeps replayed updates synchronizing until the completion marker arrives" , ( ) =>
12551318 Effect . gen ( function * ( ) {
12561319 const harness = yield * makeHarness ( { cached : BASE_PROJECTION , completionMarker : true } ) ;
0 commit comments