@@ -278,6 +278,124 @@ function itemIds(projection: OrchestrationV2ThreadProjection): string[] {
278278 return projection . turnItems . map ( ( item ) => String ( item . id ) ) ;
279279}
280280
281+ it . effect . each ( [ "thread.visit-recorded" , "thread.pull-request-link-synced" ] as const ) (
282+ "resumes persisted %s with a current legacy snapshot or opted-in replay" ,
283+ ( type ) =>
284+ Effect . gen ( function * ( ) {
285+ const events = yield * OrchestrationEventStore . OrchestrationEventStore ;
286+ const projections = yield * ProjectionStore . ProjectionStoreV2 ;
287+ const persist = Effect . fnUntraced ( function * ( event : OrchestrationV2DomainEvent ) {
288+ const [ stored ] = yield * events . appendAgentEvents ( { events : [ event ] } ) ;
289+ yield * projections . apply ( event ) ;
290+ return stored ! ;
291+ } ) ;
292+ const at = yield * DateTime . now ;
293+ const refreshedAt = DateTime . add ( at , { seconds : 1 } ) ;
294+ const renamedAt = DateTime . add ( at , { seconds : 2 } ) ;
295+ yield * persist ( threadCreated ( PARENT , at ) ) ;
296+ const thread = yield * projections . getThread ( PARENT ) ;
297+ const link = {
298+ host : "github.com" ,
299+ repository : "acme/app" ,
300+ number : 1 ,
301+ url : "https://github.com/acme/app/pull/1" ,
302+ source : "manual" as const ,
303+ linkedAt : DateTime . formatIso ( at ) ,
304+ snapshot : null ,
305+ stack : null ,
306+ } ;
307+ const before = yield * persist ( {
308+ id : EventId . make ( "resume:link" ) ,
309+ type : "thread.pull-request-synced" ,
310+ threadId : PARENT ,
311+ occurredAt : at ,
312+ payload : { ...thread , pullRequests : [ link ] } ,
313+ } ) ;
314+ const snapshot = {
315+ state : "merged" as const ,
316+ title : "Merged" ,
317+ headBranch : "feature" ,
318+ baseBranch : "main" ,
319+ isDraft : false ,
320+ updatedAt : DateTime . formatIso ( refreshedAt ) ,
321+ syncedAt : DateTime . formatIso ( refreshedAt ) ,
322+ } ;
323+ const compact = yield * persist ( {
324+ id : EventId . make ( "resume:compact" ) ,
325+ threadId : PARENT ,
326+ occurredAt : refreshedAt ,
327+ ...( type === "thread.visit-recorded"
328+ ? { type, payload : { lastVisitedAt : refreshedAt } }
329+ : {
330+ type,
331+ payload : {
332+ host : link . host ,
333+ repository : link . repository ,
334+ number : link . number ,
335+ snapshot,
336+ stack : null ,
337+ } ,
338+ } ) ,
339+ } ) ;
340+ const refreshed = yield * projections . getThread ( PARENT ) ;
341+ const expected = {
342+ ...thread ,
343+ title : "Renamed after refresh" ,
344+ updatedAt : renamedAt ,
345+ lastVisitedAt : type === "thread.visit-recorded" ? refreshedAt : null ,
346+ pullRequests : [ type === "thread.pull-request-link-synced" ? { ...link , snapshot } : link ] ,
347+ } ;
348+ const renamed = yield * persist ( {
349+ id : EventId . make ( "resume:rename" ) ,
350+ type : "thread.metadata-updated" ,
351+ threadId : PARENT ,
352+ occurredAt : renamedAt ,
353+ payload : { ...refreshed , title : expected . title , updatedAt : renamedAt } ,
354+ } ) ;
355+
356+ for ( const acceptThreadFieldEvents of [ undefined , false , true ] ) {
357+ const items = yield * subscribeOrchestrationV2Thread ( {
358+ threadId : PARENT ,
359+ afterSequence : before . sequence ,
360+ requestCompletionMarker : true ,
361+ ...( acceptThreadFieldEvents === undefined ? { } : { acceptThreadFieldEvents } ) ,
362+ } ) . pipe (
363+ Effect . flatMap ( ( stream ) =>
364+ stream . pipe (
365+ Stream . takeUntil ( ( item ) => item . kind === "synchronized" ) ,
366+ Stream . runCollect ,
367+ ) ,
368+ ) ,
369+ Effect . map ( ( items ) =>
370+ items . map ( ( item ) =>
371+ decodeStreamItem ( JSON . parse ( JSON . stringify ( encodeStreamItem ( item ) ) ) ) ,
372+ ) ,
373+ ) ,
374+ ) ;
375+ if ( acceptThreadFieldEvents === true ) {
376+ assert . deepEqual ( items , [
377+ { kind : "event" , sequence : compact . sequence , event : compact . event } ,
378+ { kind : "event" , sequence : renamed . sequence , event : renamed . event } ,
379+ { kind : "synchronized" } ,
380+ ] ) ;
381+ } else {
382+ assert . deepEqual (
383+ items . map ( ( item ) => item . kind ) ,
384+ [ "snapshot" , "synchronized" ] ,
385+ ) ;
386+ const first = items [ 0 ] ;
387+ if ( first ?. kind !== "snapshot" ) return assert . fail ( "missing reconnect snapshot" ) ;
388+ assert . strictEqual ( first . snapshotSequence , renamed . sequence ) ;
389+ assert . deepEqual ( first . projection . thread , expected ) ;
390+ }
391+ }
392+ } ) . pipe (
393+ Effect . provide (
394+ Layer . mergeAll ( management , OrchestrationEventStore . layer ) . pipe ( Layer . provideMerge ( store ) ) ,
395+ ) ,
396+ ) ,
397+ ) ;
398+
281399it . layer ( TestLayer ) ( "compact bounded snapshot transport" , ( it ) => {
282400 it . effect ( "delivers compact visits and PR refreshes to current and legacy subscribers" , ( ) =>
283401 Effect . gen ( function * ( ) {
0 commit comments