@@ -236,7 +236,11 @@ describe("subagent registry lifecycle error grace", () => {
236236 const run = mod
237237 . listSubagentRunsForRequester ( MAIN_REQUESTER_SESSION_KEY )
238238 . find ( ( candidate ) => candidate . runId === runId ) ;
239- if ( run ?. cleanupHandled === false ) {
239+ if (
240+ run ?. cleanupHandled === false &&
241+ run . delivery ?. status === "pending" &&
242+ run . delivery . payload
243+ ) {
240244 return ;
241245 }
242246 await vi . advanceTimersByTimeAsync ( 1 ) ;
@@ -282,12 +286,13 @@ describe("subagent registry lifecycle error grace", () => {
282286 throw new Error ( `expected ${ expectedCount } agent call(s), got ${ getAgentCalls ( ) . length } ` ) ;
283287 } ;
284288
285- const waitForFrozenResultText = async ( runId : string , expectedText : string ) => {
289+ const waitForFrozenResult = async ( runId : string , matches : ( resultText : string ) => boolean ) => {
286290 for ( let attempt = 0 ; attempt < 80 ; attempt += 1 ) {
287291 const run = mod
288292 . listSubagentRunsForRequester ( MAIN_REQUESTER_SESSION_KEY )
289293 . find ( ( candidate ) => candidate . runId === runId ) ;
290- if ( run ?. completion ?. resultText === expectedText ) {
294+ const resultText = run ?. completion ?. resultText ;
295+ if ( run && typeof resultText === "string" && matches ( resultText ) ) {
291296 return run ;
292297 }
293298 await vi . advanceTimersByTimeAsync ( 1 ) ;
@@ -296,11 +301,15 @@ describe("subagent registry lifecycle error grace", () => {
296301 throw new Error ( `run ${ runId } frozen result did not refresh` ) ;
297302 } ;
298303
304+ const waitForFrozenResultText = async ( runId : string , expectedText : string ) =>
305+ waitForFrozenResult ( runId , ( resultText ) => resultText === expectedText ) ;
306+
299307 function registerCompletionRun (
300308 runId : string ,
301309 childSuffix : string ,
302310 task : string ,
303311 requesterTurnRunId ?: string ,
312+ expectsCompletionMessage = true ,
304313 ) {
305314 mod . registerSubagentRun ( {
306315 runId,
@@ -311,7 +320,7 @@ describe("subagent registry lifecycle error grace", () => {
311320 requesterDisplayKey : MAIN_REQUESTER_DISPLAY_KEY ,
312321 task,
313322 cleanup : "keep" ,
314- expectsCompletionMessage : true ,
323+ expectsCompletionMessage,
315324 } ) ;
316325 }
317326
@@ -451,10 +460,18 @@ describe("subagent registry lifecycle error grace", () => {
451460 } ) ,
452461 ) ;
453462
454- emitLifecycleEvent ( "run-yield-alpha" , { phase : "end" , endedAt : Date . now ( ) } ) ;
463+ emitLifecycleEvent ( "run-yield-alpha" , {
464+ phase : "end" ,
465+ endedAt : Date . now ( ) ,
466+ terminalReply : { disposition : "visible" , text : "alpha complete" } ,
467+ } ) ;
455468 await waitForAgentCallCount ( 1 ) ;
456469
457- emitLifecycleEvent ( "run-yield-beta" , { phase : "end" , endedAt : Date . now ( ) + 1 } ) ;
470+ emitLifecycleEvent ( "run-yield-beta" , {
471+ phase : "end" ,
472+ endedAt : Date . now ( ) + 1 ,
473+ terminalReply : { disposition : "visible" , text : "beta complete" } ,
474+ } ) ;
458475 await waitForAgentCallCount ( 2 ) ;
459476 const betaBeforeYield = mod
460477 . listSubagentRunsForRequester ( MAIN_REQUESTER_SESSION_KEY )
@@ -583,7 +600,11 @@ describe("subagent registry lifecycle error grace", () => {
583600 expect ( getRequesterWakeCalls ( ) ) . toHaveLength ( 0 ) ;
584601 expect ( liveChild . execution . status ) . toBe ( "running" ) ;
585602
586- emitLifecycleEvent ( "run-frozen-live-child" , { phase : "end" , endedAt : Date . now ( ) + 1 } ) ;
603+ emitLifecycleEvent ( "run-frozen-live-child" , {
604+ phase : "end" ,
605+ endedAt : Date . now ( ) + 1 ,
606+ terminalReply : { disposition : "visible" , text : "live child complete" } ,
607+ } ) ;
587608 await waitForAgentCallCount ( 1 ) ;
588609 await waitForDeliveredCleanup ( "run-frozen-live-child" ) ;
589610
@@ -616,7 +637,11 @@ describe("subagent registry lifecycle error grace", () => {
616637 await vi . advanceTimersByTimeAsync ( 20_000 ) ;
617638 expect ( getAgentCalls ( ) ) . toHaveLength ( 0 ) ;
618639
619- emitLifecycleEvent ( "run-transient-error" , { phase : "end" , endedAt : 1_250 } ) ;
640+ emitLifecycleEvent ( "run-transient-error" , {
641+ phase : "end" ,
642+ endedAt : 1_250 ,
643+ terminalReply : { disposition : "visible" , text : "Final answer transient" } ,
644+ } ) ;
620645 await flushAsync ( ) ;
621646
622647 await waitForAgentCallCount ( 1 ) ;
@@ -650,7 +675,11 @@ describe("subagent registry lifecycle error grace", () => {
650675 agentCallPlan = [ "throw" , "ok" ] ;
651676
652677 const endedAt = Date . now ( ) ;
653- emitLifecycleEvent ( "run-freeze" , { phase : "end" , endedAt } ) ;
678+ emitLifecycleEvent ( "run-freeze" , {
679+ phase : "end" ,
680+ endedAt,
681+ terminalReply : { disposition : "visible" , text : "Final answer X" } ,
682+ } ) ;
654683 await flushAsync ( ) ;
655684
656685 await waitForAgentCallCount ( 1 ) ;
@@ -661,7 +690,11 @@ describe("subagent registry lifecycle error grace", () => {
661690 await waitForCleanupHandledFalse ( "run-freeze" ) ;
662691
663692 setAssistantOutput ( "agent:main:subagent:freeze" , "Late reply Y" ) ;
664- emitLifecycleEvent ( "run-freeze" , { phase : "end" , endedAt : endedAt + 100 } ) ;
693+ emitLifecycleEvent ( "run-freeze" , {
694+ phase : "end" ,
695+ endedAt : endedAt + 100 ,
696+ terminalReply : { disposition : "visible" , text : "Final answer X" } ,
697+ } ) ;
665698 await flushAsync ( ) ;
666699
667700 await waitForAgentCallCount ( 2 ) ;
@@ -680,7 +713,14 @@ describe("subagent registry lifecycle error grace", () => {
680713 agentCallPlan = [ "throw" , "ok" ] ;
681714
682715 const endedAt = Date . now ( ) ;
683- emitLifecycleEvent ( "run-refresh" , { phase : "end" , endedAt } ) ;
716+ emitLifecycleEvent ( "run-refresh" , {
717+ phase : "end" ,
718+ endedAt,
719+ terminalReply : {
720+ disposition : "visible" ,
721+ text : "Both spawned. Waiting for completion events..." ,
722+ } ,
723+ } ) ;
684724 await flushAsync ( ) ;
685725
686726 await waitForAgentCallCount ( 1 ) ;
@@ -713,7 +753,14 @@ describe("subagent registry lifecycle error grace", () => {
713753 ) ;
714754 expect ( ( runAfterRefresh ?. completion ?. capturedAt ?? 0 ) >= firstCapturedAt ) . toBe ( true ) ;
715755
716- emitLifecycleEvent ( "run-refresh" , { phase : "end" , endedAt : endedAt + 300 } ) ;
756+ emitLifecycleEvent ( "run-refresh" , {
757+ phase : "end" ,
758+ endedAt : endedAt + 300 ,
759+ terminalReply : {
760+ disposition : "visible" ,
761+ text : "All 3 subagents complete. Here's the final summary." ,
762+ } ,
763+ } ) ;
717764 await flushAsync ( ) ;
718765
719766 await waitForAgentCallCount ( 2 ) ;
@@ -729,7 +776,14 @@ describe("subagent registry lifecycle error grace", () => {
729776 agentCallPlan = [ "throw" , "ok" ] ;
730777
731778 const endedAt = Date . now ( ) ;
732- emitLifecycleEvent ( "run-refresh-silent" , { phase : "end" , endedAt } ) ;
779+ emitLifecycleEvent ( "run-refresh-silent" , {
780+ phase : "end" ,
781+ endedAt,
782+ terminalReply : {
783+ disposition : "visible" ,
784+ text : "All work complete, final summary" ,
785+ } ,
786+ } ) ;
733787 await flushAsync ( ) ;
734788 await waitForCleanupHandledFalse ( "run-refresh-silent" ) ;
735789 await waitForFrozenResultText ( "run-refresh-silent" , "All work complete, final summary" ) ;
@@ -747,7 +801,14 @@ describe("subagent registry lifecycle error grace", () => {
747801 . find ( ( candidate ) => candidate . runId === "run-refresh-silent" ) ;
748802 expect ( runAfterSilent ?. completion ?. resultText ) . toBe ( "All work complete, final summary" ) ;
749803
750- emitLifecycleEvent ( "run-refresh-silent" , { phase : "end" , endedAt : endedAt + 300 } ) ;
804+ emitLifecycleEvent ( "run-refresh-silent" , {
805+ phase : "end" ,
806+ endedAt : endedAt + 300 ,
807+ terminalReply : {
808+ disposition : "visible" ,
809+ text : "All work complete, final summary" ,
810+ } ,
811+ } ) ;
751812 await flushAsync ( ) ;
752813
753814 await waitForAgentCallCount ( 2 ) ;
@@ -758,29 +819,24 @@ describe("subagent registry lifecycle error grace", () => {
758819 } ) ;
759820
760821 it ( "regression, captures frozen completion output with 100KB cap and retains it for keep-mode cleanup" , async ( ) => {
761- registerCompletionRun ( "run-capped" , "capped" , "capped result test" ) ;
822+ registerCompletionRun ( "run-capped" , "capped" , "capped result test" , undefined , false ) ;
762823 setAssistantOutput ( "agent:main:subagent:capped" , "x" . repeat ( 120 * 1024 ) ) ;
763824
764825 emitLifecycleEvent ( "run-capped" , { phase : "end" , endedAt : Date . now ( ) } ) ;
765826 await flushAsync ( ) ;
766827
767- await waitForAgentCallCount ( 1 ) ;
768- const cappedResults = getAgentResultsForChildSession ( "agent:main:subagent:capped" ) ;
769- expect ( cappedResults ) . toHaveLength ( 1 ) ;
770- expect ( cappedResults [ 0 ] ) . toContain ( "[truncated: frozen completion output exceeded 100KB" ) ;
771- expect ( Buffer . byteLength ( cappedResults [ 0 ] ?? "" , "utf8" ) ) . toBeLessThanOrEqual ( 100 * 1024 ) ;
772-
773- const run = mod
774- . listSubagentRunsForRequester ( MAIN_REQUESTER_SESSION_KEY )
775- . find ( ( candidate ) => candidate . runId === "run-capped" ) ;
776- if ( ! run ) {
777- throw new Error ( "expected capped run to exist" ) ;
778- }
828+ const run = await waitForFrozenResult ( "run-capped" , ( resultText ) =>
829+ resultText . includes ( "[truncated: frozen completion output exceeded 100KB" ) ,
830+ ) ;
831+ expect ( getAgentCalls ( ) ) . toHaveLength ( 0 ) ;
779832 expect ( run . runId ) . toBe ( "run-capped" ) ;
780833 expect ( typeof run . completion ?. resultText ) . toBe ( "string" ) ;
781834 expect ( run . completion ?. resultText ) . toContain (
782835 "[truncated: frozen completion output exceeded 100KB" ,
783836 ) ;
837+ expect ( Buffer . byteLength ( run . completion ?. resultText ?? "" , "utf8" ) ) . toBeLessThanOrEqual (
838+ 100 * 1024 ,
839+ ) ;
784840 expect ( run . completion ?. capturedAt ) . toBeTypeOf ( "number" ) ;
785841 } ) ;
786842
@@ -823,7 +879,11 @@ describe("subagent registry lifecycle error grace", () => {
823879 expect ( getAgentCalls ( ) ) . toHaveLength ( 0 ) ;
824880
825881 // Before the grace window, the run successfully ends (non-aborted)
826- emitLifecycleEvent ( "run-timeout-cancel" , { phase : "end" , endedAt : 4_500 } ) ;
882+ emitLifecycleEvent ( "run-timeout-cancel" , {
883+ phase : "end" ,
884+ endedAt : 4_500 ,
885+ terminalReply : { disposition : "visible" , text : "Final answer after recovery" } ,
886+ } ) ;
827887 await flushAsync ( ) ;
828888
829889 await waitForAgentCallCount ( 1 ) ;
@@ -857,8 +917,16 @@ describe("subagent registry lifecycle error grace", () => {
857917 agentCallPlan = [ "throw" , "throw" , "ok" , "ok" ] ;
858918
859919 const parallelEndedAt = Date . now ( ) ;
860- emitLifecycleEvent ( "run-parallel-a" , { phase : "end" , endedAt : parallelEndedAt } ) ;
861- emitLifecycleEvent ( "run-parallel-b" , { phase : "end" , endedAt : parallelEndedAt + 1 } ) ;
920+ emitLifecycleEvent ( "run-parallel-a" , {
921+ phase : "end" ,
922+ endedAt : parallelEndedAt ,
923+ terminalReply : { disposition : "visible" , text : "Final answer A" } ,
924+ } ) ;
925+ emitLifecycleEvent ( "run-parallel-b" , {
926+ phase : "end" ,
927+ endedAt : parallelEndedAt + 1 ,
928+ terminalReply : { disposition : "visible" , text : "Final answer B" } ,
929+ } ) ;
862930 await flushAsync ( ) ;
863931
864932 await waitForAgentCallCount ( 2 ) ;
@@ -868,8 +936,16 @@ describe("subagent registry lifecycle error grace", () => {
868936 setAssistantOutput ( "agent:main:subagent:parallel-a" , "Late overwrite" ) ;
869937 setAssistantOutput ( "agent:main:subagent:parallel-b" , "Late overwrite" ) ;
870938
871- emitLifecycleEvent ( "run-parallel-a" , { phase : "end" , endedAt : parallelEndedAt + 100 } ) ;
872- emitLifecycleEvent ( "run-parallel-b" , { phase : "end" , endedAt : parallelEndedAt + 101 } ) ;
939+ emitLifecycleEvent ( "run-parallel-a" , {
940+ phase : "end" ,
941+ endedAt : parallelEndedAt + 100 ,
942+ terminalReply : { disposition : "visible" , text : "Final answer A" } ,
943+ } ) ;
944+ emitLifecycleEvent ( "run-parallel-b" , {
945+ phase : "end" ,
946+ endedAt : parallelEndedAt + 101 ,
947+ terminalReply : { disposition : "visible" , text : "Final answer B" } ,
948+ } ) ;
873949 await flushAsync ( ) ;
874950
875951 await waitForAgentCallCount ( 4 ) ;
0 commit comments