@@ -2955,48 +2955,119 @@ describe("ClaudeAdapterV2 background wake turns", () => {
29552955 ) ,
29562956 ) ;
29572957
2958- it . effect ( "requests a continuation for assistant output stranded after a settle" , ( ) =>
2958+ it . effect (
2959+ "requests a continuation immediately for stranded output with no relevant background work" ,
2960+ ( ) =>
2961+ Effect . scoped (
2962+ Effect . gen ( function * ( ) {
2963+ const harness = yield * makeWakeHarness ;
2964+ const now = yield * DateTime . now ;
2965+ const strandedText = "Output produced after the run had already settled." ;
2966+
2967+ yield * harness . runtime . startTurn (
2968+ makeClaudeTestTurnInput ( {
2969+ threadId : harness . threadId ,
2970+ providerThread : harness . providerThread ,
2971+ now,
2972+ attemptId : RunAttemptId . make ( "attempt-claude-stranded-1" ) ,
2973+ text : "Do the thing." ,
2974+ attachments : [ ] ,
2975+ } ) ,
2976+ ) ;
2977+ yield * Queue . offer ( harness . sdkMessages , turnOneResult ) ;
2978+ yield * awaitUntil ( ( ) => harness . terminalEvents ( ) . length === 1 , "first turn terminal" ) ;
2979+ assert . lengthOf ( harness . continuationRequests , 0 ) ;
2980+
2981+ // No background task and no notification: just the model still talking
2982+ // after an early terminal left it with no turn to talk into. Without a
2983+ // continuation these frames sit in the buffer until the session
2984+ // recycles, which is how the output disappears.
2985+ yield * Queue . offer (
2986+ harness . sdkMessages ,
2987+ makeAssistantTextFrame ( {
2988+ uuid : "00000000-0000-4000-8000-000000000221" ,
2989+ text : strandedText ,
2990+ } ) ,
2991+ ) ;
2992+ yield * awaitUntil (
2993+ ( ) => harness . continuationRequests . length === 1 ,
2994+ "continuation request" ,
2995+ ) ;
2996+ assert . equal ( harness . continuationRequests [ 0 ] ?. threadId , harness . threadId ) ;
2997+
2998+ yield * harness . runtime . startTurn (
2999+ makeClaudeTestTurnInput ( {
3000+ threadId : harness . threadId ,
3001+ providerThread : harness . providerThread ,
3002+ now,
3003+ attemptId : RunAttemptId . make ( "attempt-claude-stranded-2" ) ,
3004+ text : "Continuing." ,
3005+ attachments : [ ] ,
3006+ providerTurnOrdinal : 2 ,
3007+ messageCreatedBy : "agent" ,
3008+ messageCreationSource : "provider" ,
3009+ } ) ,
3010+ ) ;
3011+ yield * awaitUntil (
3012+ ( ) =>
3013+ harness . events . some (
3014+ ( event ) => event . type === "message.updated" && event . message . text === strandedText ,
3015+ ) ,
3016+ "stranded output projected" ,
3017+ ) ;
3018+ assert . lengthOf ( harness . offeredMessages , 1 ) ;
3019+ } ) . pipe ( Effect . provide ( Layer . merge ( idAllocatorLayer , NodeServices . layer ) ) ) ,
3020+ ) ,
3021+ ) ;
3022+
3023+ it . effect ( "lets a later task notification own the buffered output continuation" , ( ) =>
29593024 Effect . scoped (
29603025 Effect . gen ( function * ( ) {
29613026 const harness = yield * makeWakeHarness ;
29623027 const now = yield * DateTime . now ;
2963- const strandedText = "Output produced after the run had already settled ." ;
3028+ const strandedText = "The background build finished before its notification arrived ." ;
29643029
29653030 yield * harness . runtime . startTurn (
29663031 makeClaudeTestTurnInput ( {
29673032 threadId : harness . threadId ,
29683033 providerThread : harness . providerThread ,
29693034 now,
2970- attemptId : RunAttemptId . make ( "attempt-claude-stranded-1" ) ,
2971- text : "Do the thing ." ,
3035+ attemptId : RunAttemptId . make ( "attempt-claude-stranded-notify- 1" ) ,
3036+ text : "Run the build in the background ." ,
29723037 attachments : [ ] ,
29733038 } ) ,
29743039 ) ;
3040+ yield * Queue . offer ( harness . sdkMessages , wakeTaskStarted ) ;
29753041 yield * Queue . offer ( harness . sdkMessages , turnOneResult ) ;
29763042 yield * awaitUntil ( ( ) => harness . terminalEvents ( ) . length === 1 , "first turn terminal" ) ;
2977- assert . lengthOf ( harness . continuationRequests , 0 ) ;
29783043
2979- // No background task and no notification: just the model still talking
2980- // after an early terminal left it with no turn to talk into. Without a
2981- // continuation these frames sit in the buffer until the session
2982- // recycles, which is how the output disappears.
29833044 yield * Queue . offer (
29843045 harness . sdkMessages ,
29853046 makeAssistantTextFrame ( {
2986- uuid : "00000000-0000-4000-8000-000000000221 " ,
3047+ uuid : "00000000-0000-4000-8000-000000000222 " ,
29873048 text : strandedText ,
29883049 } ) ,
29893050 ) ;
3051+ let bufferedYields = 0 ;
3052+ yield * awaitUntil ( ( ) => bufferedYields ++ >= 50 , "stranded output to buffer" ) ;
3053+ assert . lengthOf ( harness . continuationRequests , 0 ) ;
3054+
3055+ yield * TestClock . adjust ( "1 second" ) ;
3056+ assert . lengthOf ( harness . continuationRequests , 0 ) ;
3057+ yield * Queue . offer ( harness . sdkMessages , wakeNotification ) ;
29903058 yield * awaitUntil ( ( ) => harness . continuationRequests . length === 1 , "continuation request" ) ;
2991- assert . equal ( harness . continuationRequests [ 0 ] ?. threadId , harness . threadId ) ;
3059+ assert . equal ( harness . continuationRequests [ 0 ] ?. detail , WAKE_SUMMARY ) ;
3060+
3061+ yield * TestClock . adjust ( "2 seconds" ) ;
3062+ assert . lengthOf ( harness . continuationRequests , 1 ) ;
29923063
29933064 yield * harness . runtime . startTurn (
29943065 makeClaudeTestTurnInput ( {
29953066 threadId : harness . threadId ,
29963067 providerThread : harness . providerThread ,
29973068 now,
2998- attemptId : RunAttemptId . make ( "attempt-claude-stranded-2" ) ,
2999- text : "Continuing ." ,
3069+ attemptId : RunAttemptId . make ( "attempt-claude-stranded-notify- 2" ) ,
3070+ text : "Background task completed ." ,
30003071 attachments : [ ] ,
30013072 providerTurnOrdinal : 2 ,
30023073 messageCreatedBy : "agent" ,
@@ -3010,7 +3081,50 @@ describe("ClaudeAdapterV2 background wake turns", () => {
30103081 ) ,
30113082 "stranded output projected" ,
30123083 ) ;
3013- assert . lengthOf ( harness . offeredMessages , 1 ) ;
3084+ yield * Queue . offer ( harness . sdkMessages , wakeResult ) ;
3085+ yield * awaitUntil ( ( ) => harness . terminalEvents ( ) . length === 2 , "continuation terminal" ) ;
3086+ } ) . pipe ( Effect . provide ( Layer . merge ( idAllocatorLayer , NodeServices . layer ) ) ) ,
3087+ ) ,
3088+ ) ;
3089+
3090+ it . effect ( "offers buffered output after the notification fallback bound" , ( ) =>
3091+ Effect . scoped (
3092+ Effect . gen ( function * ( ) {
3093+ const harness = yield * makeWakeHarness ;
3094+ const now = yield * DateTime . now ;
3095+
3096+ yield * harness . runtime . startTurn (
3097+ makeClaudeTestTurnInput ( {
3098+ threadId : harness . threadId ,
3099+ providerThread : harness . providerThread ,
3100+ now,
3101+ attemptId : RunAttemptId . make ( "attempt-claude-stranded-fallback" ) ,
3102+ text : "Run the build in the background." ,
3103+ attachments : [ ] ,
3104+ } ) ,
3105+ ) ;
3106+ yield * Queue . offer ( harness . sdkMessages , wakeTaskStarted ) ;
3107+ yield * Queue . offer ( harness . sdkMessages , turnOneResult ) ;
3108+ yield * awaitUntil ( ( ) => harness . terminalEvents ( ) . length === 1 , "first turn terminal" ) ;
3109+ yield * Queue . offer (
3110+ harness . sdkMessages ,
3111+ makeAssistantTextFrame ( {
3112+ uuid : "00000000-0000-4000-8000-000000000223" ,
3113+ text : "The notification never arrived, but this output must not remain stranded." ,
3114+ } ) ,
3115+ ) ;
3116+ let bufferedYields = 0 ;
3117+ yield * awaitUntil ( ( ) => bufferedYields ++ >= 50 , "stranded output to buffer" ) ;
3118+ assert . lengthOf ( harness . continuationRequests , 0 ) ;
3119+
3120+ yield * TestClock . adjust ( "1 second" ) ;
3121+ assert . lengthOf ( harness . continuationRequests , 0 ) ;
3122+ yield * TestClock . adjust ( "1 second" ) ;
3123+ yield * awaitUntil ( ( ) => harness . continuationRequests . length === 1 , "fallback request" ) ;
3124+ assert . isNull ( harness . continuationRequests [ 0 ] ?. detail ) ;
3125+
3126+ yield * TestClock . adjust ( "2 seconds" ) ;
3127+ assert . lengthOf ( harness . continuationRequests , 1 ) ;
30143128 } ) . pipe ( Effect . provide ( Layer . merge ( idAllocatorLayer , NodeServices . layer ) ) ) ,
30153129 ) ,
30163130 ) ;
@@ -3316,6 +3430,17 @@ describe("ClaudeAdapterV2 background wake turns", () => {
33163430 assert . isTrue ( yield * harness . hasPendingBackgroundWork ) ;
33173431 assert . lengthOf ( harness . continuationRequests , 0 ) ;
33183432
3433+ yield * Queue . offer (
3434+ harness . sdkMessages ,
3435+ makeAssistantTextFrame ( {
3436+ uuid : "00000000-0000-4000-8000-000000000206" ,
3437+ text : "The background subagent has finished." ,
3438+ } ) ,
3439+ ) ;
3440+ let bufferedYields = 0 ;
3441+ yield * awaitUntil ( ( ) => bufferedYields ++ >= 50 , "subagent output to buffer" ) ;
3442+ assert . lengthOf ( harness . continuationRequests , 0 ) ;
3443+
33193444 yield * Queue . offer ( harness . sdkMessages , subagentNotification ) ;
33203445 yield * awaitUntil ( ( ) => harness . continuationRequests . length === 1 , "continuation request" ) ;
33213446 assert . equal ( harness . continuationRequests [ 0 ] ?. threadId , harness . threadId ) ;
@@ -4289,6 +4414,16 @@ describe("ClaudeAdapterV2 background wake turns", () => {
42894414 "first turn terminal" ,
42904415 ) ;
42914416 assert . isTrue ( yield * hasPendingBackgroundWork ) ;
4417+ yield * Queue . offer (
4418+ firstProcess ,
4419+ makeAssistantTextFrame ( {
4420+ uuid : "00000000-0000-4000-8000-000000000601" ,
4421+ text : "Output buffered before the query process is replaced." ,
4422+ } ) ,
4423+ ) ;
4424+ let bufferedYields = 0 ;
4425+ yield * awaitUntil ( ( ) => bufferedYields ++ >= 50 , "pre-replacement output to buffer" ) ;
4426+ assert . lengthOf ( continuationRequests , 0 ) ;
42924427
42934428 const alternateModel = {
42944429 ...CLAUDE_TEST_MODEL_SELECTION ,
@@ -4327,9 +4462,9 @@ describe("ClaudeAdapterV2 background wake turns", () => {
43274462 ) ,
43284463 "roster cleared on process replace while remaining active" ,
43294464 ) ;
4330- // After replace, the in-memory Waiting probe must be false even if a
4331- // late empty-level event was already present before background work .
4332- assert . isFalse ( yield * hasPendingBackgroundWork ) ;
4465+ // The process-scoped roster is gone, but the already-buffered output
4466+ // still pins the session until its continuation can drain it .
4467+ assert . isTrue ( yield * hasPendingBackgroundWork ) ;
43334468 const emptyActiveRosterEvents = providerThreadRosterEvents ( events ) . filter (
43344469 ( event ) =>
43354470 event . providerThread . status === "active" &&
@@ -4341,6 +4476,9 @@ describe("ClaudeAdapterV2 background wake turns", () => {
43414476 [ ] ,
43424477 ) ;
43434478 assert . equal ( emptyActiveRosterEvents . at ( - 1 ) ?. providerThread . status , "active" ) ;
4479+ yield * TestClock . adjust ( "2 seconds" ) ;
4480+ yield * awaitUntil ( ( ) => continuationRequests . length === 1 , "preserved output fallback" ) ;
4481+ assert . isNull ( continuationRequests [ 0 ] ?. detail ) ;
43444482
43454483 // A late notification from the previous process must not wake after
43464484 // eligibility was reset with the process. Offer on the new process
@@ -4349,7 +4487,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
43494487 yield * Queue . offer ( secondProcess , wakeNotification ) ;
43504488 let settleYields = 0 ;
43514489 yield * awaitUntil ( ( ) => settleYields ++ >= 50 , "stale notification settle" ) ;
4352- assert . lengthOf ( continuationRequests , 0 ) ;
4490+ assert . lengthOf ( continuationRequests , 1 ) ;
43534491
43544492 yield * Queue . offer (
43554493 secondProcess ,
@@ -5020,7 +5158,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
50205158 ) ;
50215159
50225160 it . effect (
5023- "clears buffered wake and continuation state when same-native-thread replacement open fails" ,
5161+ "preserves buffered wake and continuation state when same-native-thread replacement open fails" ,
50245162 ( ) =>
50255163 Effect . scoped (
50265164 Effect . gen ( function * ( ) {
@@ -5144,7 +5282,8 @@ describe("ClaudeAdapterV2 background wake turns", () => {
51445282 )
51455283 . pipe ( Effect . exit ) ;
51465284 assert . isTrue ( Exit . isFailure ( failedStart ) ) ;
5147- assert . isFalse ( yield * hasPendingBackgroundWork ) ;
5285+ assert . isTrue ( yield * hasPendingBackgroundWork ) ;
5286+ assert . lengthOf ( continuationRequests , 1 ) ;
51485287
51495288 yield * runtime . startTurn (
51505289 makeClaudeTestTurnInput ( {
@@ -5183,6 +5322,30 @@ describe("ClaudeAdapterV2 background wake turns", () => {
51835322 ( ) => events . filter ( ( event ) => event . type === "turn.terminal" ) . length === 2 ,
51845323 "retry turn terminal" ,
51855324 ) ;
5325+
5326+ // The already-requested continuation survives the failed process
5327+ // replacement and drains the wake output after the user retry.
5328+ yield * Queue . offer ( retryProcess , wakeResult ) ;
5329+ yield * runtime . startTurn (
5330+ makeClaudeTestTurnInput ( {
5331+ threadId,
5332+ providerThread : { ...providerThread , status : "active" } ,
5333+ now,
5334+ attemptId : RunAttemptId . make ( "attempt-claude-replace-open-fail-wake-d" ) ,
5335+ text : "Background task completed." ,
5336+ attachments : [ ] ,
5337+ providerTurnOrdinal : 3 ,
5338+ modelSelection : alternateModel ,
5339+ messageCreatedBy : "agent" ,
5340+ messageCreationSource : "provider" ,
5341+ } ) ,
5342+ ) ;
5343+ yield * awaitUntil (
5344+ ( ) => events . filter ( ( event ) => event . type === "turn.terminal" ) . length === 3 ,
5345+ "preserved continuation terminal" ,
5346+ ) ;
5347+ assert . lengthOf ( continuationRequests , 1 ) ;
5348+
51865349 yield * Queue . offer (
51875350 retryProcess ,
51885351 claudeSdkFrame ( {
0 commit comments