@@ -3813,7 +3813,12 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
38133813 const wakeBuffers = yield * Ref . make (
38143814 new Map <
38153815 string ,
3816- { readonly messages : ReadonlyArray < SDKMessage > ; readonly detail : string | null }
3816+ {
3817+ readonly messages : ReadonlyArray < SDKMessage > ;
3818+ readonly detail : string | null ;
3819+ // A user turn already ran the buffered wake to its result.
3820+ readonly settled : boolean ;
3821+ }
38173822 > ( ) ,
38183823 ) ;
38193824 // Claude's wake result carries no task id, and its wake can run after a prompt the user
@@ -3870,8 +3875,21 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
38703875 } ) ;
38713876 const clearWakeReports = ( nativeThreadId : string ) =>
38723877 takeWakeReports ( nativeThreadId ) . pipe ( Effect . asVoid ) ;
3873- // An empty roster level can land before the task's notification, and
3874- // the wake still needs to name the task.
3878+ // A user turn ran the wake to its result. Its work is reported there, but
3879+ // whatever Claude emitted before that turn began (its `init`, sometimes
3880+ // output) stays buffered. A continuation still queued replays it and
3881+ // would wait for a result that already arrived, so mark it settled.
3882+ const settleWakeInUserTurn = ( nativeThreadId : string ) =>
3883+ Effect . gen ( function * ( ) {
3884+ yield * clearWakeReports ( nativeThreadId ) ;
3885+ yield * Ref . update ( wakeBuffers , ( current ) => {
3886+ const entry = current . get ( nativeThreadId ) ;
3887+ if ( entry === undefined || entry . settled ) return current ;
3888+ return new Map ( current ) . set ( nativeThreadId , { ...entry , settled : true } ) ;
3889+ } ) ;
3890+ } ) ;
3891+ // Last roster entry per opaque task. An empty roster level can land before
3892+ // the task's notification, and the wake still needs to name the task.
38753893 const lastKnownOpaqueTasks = yield * Ref . make (
38763894 new Map < string , OrchestrationV2PendingBackgroundTask > ( ) ,
38773895 ) ;
@@ -6843,6 +6861,8 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
68436861 updated . set ( wakeInput . nativeThreadId , {
68446862 messages : [ ...( existing ?. messages ?? [ ] ) , message ] ,
68456863 detail : notificationSummary ?? existing ?. detail ?? null ,
6864+ // A new wake's `init` reopens the buffer for its own result.
6865+ settled : ! isWakeTurnStart && ( existing ?. settled ?? false ) ,
68466866 } ) ;
68476867 return updated ;
68486868 } ) ;
@@ -8170,7 +8190,8 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
81708190 isClaudeTaskNotificationOriginResult ( message ) &&
81718191 ! isClaudeProviderContinuationTurn ( context . input )
81728192 ) {
8173- yield * clearWakeReports ( liveQuery . nativeThreadId ) ;
8193+ // This user turn ran the wake itself, so no offer will name its work.
8194+ yield * settleWakeInUserTurn ( liveQuery . nativeThreadId ) ;
81748195 yield * Effect . logInfo ( "orchestration-v2.claude-task-notification-result-accepted" , {
81758196 providerTurnId : context . providerTurnId ,
81768197 num_turns : message . num_turns ,
@@ -8444,7 +8465,8 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
84448465 // stays out: settling here would end the turn before its prompt
84458466 // runs. The prompt's own turn ends it with an echoing result.
84468467 if ( isClaudeTaskNotificationOriginResult ( message ) ) {
8447- yield * clearWakeReports ( liveQuery . nativeThreadId ) ;
8468+ // This turn showed the wake, so no offer will name its work.
8469+ yield * settleWakeInUserTurn ( liveQuery . nativeThreadId ) ;
84488470 }
84498471 yield * Effect . logInfo ( "orchestration-v2.claude-result-for-other-turn" , {
84508472 providerTurnId : context . providerTurnId ,
@@ -9208,7 +9230,10 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
92089230 } ) ;
92099231 }
92109232 yield * attachTurn ;
9211- const drained = ( yield * Ref . get ( wakeBuffers ) ) . get ( nativeThreadId ) ?. messages ?? [ ] ;
9233+ // Exit and interrupt cleanup can run during transcript reads.
9234+ // Keep pending outcomes visible until replay applies them.
9235+ const buffer = ( yield * Ref . get ( wakeBuffers ) ) . get ( nativeThreadId ) ;
9236+ const drained = buffer ?. messages ?? [ ] ;
92129237 yield * Ref . update ( requestedContinuations , ( current ) => {
92139238 const updated = new Set ( current ) ;
92149239 updated . delete ( nativeThreadId ) ;
@@ -9273,13 +9298,19 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
92739298 yield * handleSdkMessage ( { query : querySession . query , message : lastResult } ) ;
92749299 return ;
92759300 }
9301+ // A drained `init` means Claude began the wake turn, so its output
9302+ // may still be on the way: stay open for it, unless a user turn
9303+ // already received its result.
92769304 const hasNativeWakeFrame = drained . some (
92779305 ( entry ) =>
92789306 entry . type === "user" ||
92799307 entry . type === "assistant" ||
92809308 isClaudeTurnStartMessage ( entry ) ,
92819309 ) ;
9282- if ( hasOpaqueTaskNotification && ! hasNativeWakeFrame ) {
9310+ if (
9311+ buffer ?. settled === true ||
9312+ ( hasOpaqueTaskNotification && ! hasNativeWakeFrame )
9313+ ) {
92839314 const completedAt = yield * DateTime . now ;
92849315 yield * finalizeActiveTurn ( { context, status : "completed" , completedAt } ) ;
92859316 }
0 commit comments