@@ -392,6 +392,8 @@ describe("AgentAwarenessRelay", () => {
392392 const terminal = shell ( { status } ) ;
393393 yield * Ref . set ( currentShell , terminal ) ;
394394 yield * relay . publishThread ( THREAD_ID ) ;
395+ yield * TestClock . adjust ( "5 seconds" ) ;
396+ yield * relay . drain ;
395397 assert . equal ( publications [ 1 ] ?. state ?. phase , status ) ;
396398
397399 const now = yield * DateTime . now ;
@@ -419,6 +421,113 @@ describe("AgentAwarenessRelay", () => {
419421 } ) . pipe ( Effect . scoped ) ,
420422 ) ;
421423
424+ it . effect ( "holds a completion through a continuation gap, then publishes it once" , ( ) =>
425+ Effect . gen ( function * ( ) {
426+ const { relay, currentShell, publications } = yield * makeTestRelay ( ) ;
427+ yield * relay . publishThread ( THREAD_ID ) ;
428+ yield * Ref . set ( currentShell , shell ( { status : "completed" } ) ) ;
429+ yield * relay . publishThread ( THREAD_ID ) ;
430+ yield * Ref . set ( currentShell , shell ( ) ) ;
431+ yield * relay . publishThread ( THREAD_ID ) ;
432+ yield * TestClock . adjust ( "5 seconds" ) ;
433+ yield * relay . drain ;
434+ assert . deepEqual (
435+ publications . map ( ( publication ) => publication . state ?. phase ) ,
436+ [ "running" ] ,
437+ ) ;
438+
439+ yield * Ref . set ( currentShell , shell ( { status : "completed" } ) ) ;
440+ yield * relay . publishThread ( THREAD_ID ) ;
441+ assert . equal ( publications . length , 1 ) ;
442+ yield * TestClock . adjust ( "5 seconds" ) ;
443+ yield * relay . drain ;
444+ assert . deepEqual (
445+ publications . map ( ( publication ) => publication . state ?. phase ) ,
446+ [ "running" , "completed" ] ,
447+ ) ;
448+ } ) ,
449+ ) ;
450+
451+ it . effect ( "still publishes a completion when the thread settles during the hold" , ( ) =>
452+ Effect . gen ( function * ( ) {
453+ const { relay, currentShell, publications } = yield * makeTestRelay ( ) ;
454+ yield * relay . publishThread ( THREAD_ID ) ;
455+ const completed = shell ( { status : "completed" } ) ;
456+ yield * Ref . set ( currentShell , completed ) ;
457+ yield * relay . publishThread ( THREAD_ID ) ;
458+ yield * Ref . set ( currentShell , {
459+ ...completed ,
460+ settledOverride : "settled" ,
461+ settledAt : yield * DateTime . now ,
462+ lastVisitedAt : null ,
463+ } ) ;
464+ yield * relay . publishThread ( THREAD_ID ) ;
465+ yield * TestClock . adjust ( "5 seconds" ) ;
466+ yield * relay . drain ;
467+ assert . deepEqual (
468+ publications . map ( ( publication ) => publication . state ?. phase ?? null ) ,
469+ [ "running" , "completed" ] ,
470+ ) ;
471+
472+ yield * TestClock . adjust ( "5 seconds" ) ;
473+ yield * relay . drain ;
474+ assert . deepEqual (
475+ publications . map ( ( publication ) => publication . state ?. phase ?? null ) ,
476+ [ "running" , "completed" , null ] ,
477+ ) ;
478+ } ) ,
479+ ) ;
480+
481+ it . effect ( "drops a held completion when a queued continuation hides the thread" , ( ) =>
482+ Effect . gen ( function * ( ) {
483+ const { relay, currentShell, publications } = yield * makeTestRelay ( ) ;
484+ yield * relay . publishThread ( THREAD_ID ) ;
485+ yield * Ref . set ( currentShell , shell ( { status : "completed" } ) ) ;
486+ yield * relay . publishThread ( THREAD_ID ) ;
487+ yield * Ref . set ( currentShell , shell ( { status : "queued" } ) ) ;
488+ yield * relay . publishThread ( THREAD_ID ) ;
489+ yield * TestClock . adjust ( "5 seconds" ) ;
490+ yield * relay . drain ;
491+ assert . notInclude (
492+ publications . map ( ( publication ) => publication . state ?. phase ?? null ) ,
493+ "completed" ,
494+ ) ;
495+ } ) ,
496+ ) ;
497+
498+ it . effect ( "retries a failed completion even after the thread settles" , ( ) =>
499+ Effect . gen ( function * ( ) {
500+ const { relay, currentShell, publications } = yield * makeTestRelay ( {
501+ respond : ( attempt ) =>
502+ attempt === 2
503+ ? new Response ( "unavailable" , { status : 503 } )
504+ : Response . json ( { ok : true , deliveries : [ ] } ) ,
505+ } ) ;
506+ yield * relay . publishThread ( THREAD_ID ) ;
507+ const completed = shell ( { status : "completed" } ) ;
508+ yield * Ref . set ( currentShell , completed ) ;
509+ yield * relay . publishThread ( THREAD_ID ) ;
510+ yield * TestClock . adjust ( "5 seconds" ) ;
511+ yield * relay . drain ;
512+ yield * Ref . set ( currentShell , {
513+ ...completed ,
514+ settledOverride : "settled" ,
515+ settledAt : yield * DateTime . now ,
516+ lastVisitedAt : null ,
517+ } ) ;
518+ yield * TestClock . adjust ( "1 second" ) ;
519+ yield * relay . drain ;
520+ assert . deepEqual (
521+ publications . map ( ( publication ) => publication . state ?. phase ?? null ) ,
522+ [ "running" , "completed" , "completed" ] ,
523+ ) ;
524+
525+ yield * TestClock . adjust ( "5 seconds" ) ;
526+ yield * relay . drain ;
527+ assert . equal ( publications . at ( - 1 ) ?. state ?? null , null ) ;
528+ } ) ,
529+ ) ;
530+
422531 it . effect ( "keeps settled live work until its awaited monitor finishes" , ( ) =>
423532 Effect . gen ( function * ( ) {
424533 const { relay, currentShell, publications } = yield * makeTestRelay ( ) ;
@@ -595,6 +704,8 @@ describe("AgentAwarenessRelay", () => {
595704 yield * deliver (
596705 itemEvent ( "event:held-item:end" , item ( "item:tool" , { ...background , status : "completed" } ) ) ,
597706 ) ;
707+ yield * TestClock . adjust ( "5 seconds" ) ;
708+ yield * relay . drain ;
598709 assert . deepEqual (
599710 publications . map ( ( publication ) => publication . state ?. phase ) ,
600711 [ "running" , "completed" ] ,
0 commit comments