@@ -315,6 +315,8 @@ const CLAUDE_MCP_REGISTRATION_TIMEOUT = "90 seconds";
315315// Claude can consume a subagent result before the launching turn ends, leaving
316316// its notification with no native wake turn to answer the continuation.
317317const CLAUDE_WAKE_TURN_START_DEADLINE = "2 minutes" ;
318+ // An opened wake turn that streams nothing and holds no tool call this long is idle.
319+ const CLAUDE_WAKE_TURN_IDLE_DEADLINE = "10 minutes" ;
318320// Claude Code flushes transcript writes every 100 ms, so a workflow member's
319321// reply can land after the progress frame that settles it. Its transcript
320322// stays the source of its answer this long after it settles.
@@ -3929,6 +3931,7 @@ interface ActiveClaudeTurnContext {
39293931 promptEcho : "pending" | "confirmed" ;
39303932 promptStarted : boolean ;
39313933 nativeWakeStarted : boolean ;
3934+ wakeFrames : number ;
39323935 gatedFramesBeforeEcho : number ;
39333936 readonly heldRootFrames : Array < SDKMessage > ;
39343937}
@@ -4708,6 +4711,8 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
47084711 > ( ) ;
47094712 // Buffered idle outcomes must not settle a resumed child again.
47104713 const appliedSubagentOutcomes = new WeakSet < SDKMessage > ( ) ;
4714+ // A prompt offered into an opened wake turn is answered with it; that turn's result settles the prompt.
4715+ const wakeStartsAnsweredByPrompt = new WeakSet < SDKMessage > ( ) ;
47114716 // Subagent frames can precede the task_started that registers their owner.
47124717 const pendingSubagentFramesByToolUseId = yield * Ref . make (
47134718 new Map < string , ReadonlyArray < SDKMessage > > ( ) ,
@@ -9351,12 +9356,9 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
93519356 const message = input . message ;
93529357 const context = yield * Ref . get ( activeTurn ) ;
93539358 const liveQuery = yield * Ref . get ( queryContext ) ;
9354- if (
9355- context ?. promptUuid === null &&
9356- liveQuery ?. query === input . query &&
9357- isClaudeRootTurnFrame ( message )
9358- ) {
9359- context . nativeWakeStarted = true ;
9359+ if ( context ?. promptUuid === null && liveQuery ?. query === input . query ) {
9360+ context . wakeFrames += 1 ;
9361+ if ( isClaudeRootTurnFrame ( message ) ) context . nativeWakeStarted = true ;
93609362 }
93619363 if (
93629364 context === null ||
@@ -10213,6 +10215,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
1021310215 promptEcho : isClaudeProviderContinuationTurn ( turnInput ) ? "confirmed" : "pending" ,
1021410216 promptStarted : false ,
1021510217 nativeWakeStarted : false ,
10218+ wakeFrames : 0 ,
1021610219 gatedFramesBeforeEcho : 0 ,
1021710220 heldRootFrames : [ ] ,
1021810221 } ;
@@ -10277,6 +10280,16 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
1027710280 if ( userMessage !== null ) {
1027810281 yield * attachTurn ;
1027910282 yield * startUserTurnForWakeReports ( nativeThreadId ) ;
10283+ const buffered = ( yield * Ref . get ( wakeBuffers ) ) . get ( nativeThreadId ) ?. messages ?? [ ] ;
10284+ const openWakeStart = buffered . findLast ( isClaudeTurnStartMessage ) ;
10285+ if (
10286+ openWakeStart !== undefined &&
10287+ ! buffered
10288+ . slice ( buffered . indexOf ( openWakeStart ) )
10289+ . some ( ( entry ) => entry . type === "result" )
10290+ ) {
10291+ wakeStartsAnsweredByPrompt . add ( openWakeStart ) ;
10292+ }
1028010293 yield * Effect . uninterruptibleMask ( ( restore ) =>
1028110294 restore (
1028210295 querySession . query . offer ( userMessage , ( ) => {
@@ -10379,37 +10392,67 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
1037910392 yield * handleSdkMessage ( { query : querySession . query , message : lastResult } ) ;
1038010393 return ;
1038110394 }
10395+ const latestWakeStart = drained . findLast ( isClaudeTurnStartMessage ) ;
1038210396 if ( hasOpaqueTaskNotification && ! context . nativeWakeStarted ) {
1038310397 const completedAt = yield * DateTime . now ;
1038410398 yield * finalizeActiveTurn ( { context, status : "completed" , completedAt } ) ;
10385- } else if ( ! context . nativeWakeStarted ) {
10386- yield * Effect . sleep ( CLAUDE_WAKE_TURN_START_DEADLINE ) . pipe (
10387- Effect . andThen (
10388- sdkMessageLock . withPermits ( 1 ) (
10389- Effect . gen ( function * ( ) {
10390- if (
10391- ( yield * Ref . get ( activeTurn ) ) !== context ||
10392- ( yield * Ref . get ( queryContext ) ) !== querySession ||
10393- querySession . stopping ||
10394- context . nativeWakeStarted ||
10395- ( yield * Ref . get ( interruptedTurns ) ) . has ( context . providerTurnId )
10396- )
10397- return ;
10398- yield * Effect . logWarning (
10399- "orchestration-v2.claude-wake-turn-never-opened" ,
10400- {
10401- providerTurnId : context . providerTurnId ,
10402- nativeThreadId,
10403- } ,
10404- ) ;
10405- const completedAt = yield * DateTime . now ;
10406- yield * finalizeActiveTurn ( { context, status : "completed" , completedAt } ) ;
10407- } ) ,
10408- ) ,
10409- ) ,
10410- Effect . forkIn ( sessionScope ) ,
10411- ) ;
10399+ return ;
1041210400 }
10401+ if (
10402+ latestWakeStart !== undefined &&
10403+ wakeStartsAnsweredByPrompt . has ( latestWakeStart )
10404+ ) {
10405+ yield * Effect . logInfo ( "orchestration-v2.claude-wake-turn-answered-by-prompt" , {
10406+ providerTurnId : context . providerTurnId ,
10407+ nativeThreadId,
10408+ } ) ;
10409+ const completedAt = yield * DateTime . now ;
10410+ yield * finalizeActiveTurn ( { context, status : "completed" , completedAt } ) ;
10411+ return ;
10412+ }
10413+ yield * Effect . gen ( function * ( ) {
10414+ let framesSeen = context . wakeFrames ;
10415+ while ( true ) {
10416+ yield * Effect . sleep (
10417+ context . nativeWakeStarted
10418+ ? CLAUDE_WAKE_TURN_IDLE_DEADLINE
10419+ : CLAUDE_WAKE_TURN_START_DEADLINE ,
10420+ ) ;
10421+ const settled = yield * sdkMessageLock . withPermits ( 1 ) (
10422+ Effect . gen ( function * ( ) {
10423+ if (
10424+ ( yield * Ref . get ( activeTurn ) ) !== context ||
10425+ ( yield * Ref . get ( queryContext ) ) !== querySession ||
10426+ querySession . stopping ||
10427+ ( yield * Ref . get ( interruptedTurns ) ) . has ( context . providerTurnId )
10428+ )
10429+ return true ;
10430+ if (
10431+ context . nativeWakeStarted &&
10432+ ( context . wakeFrames !== framesSeen ||
10433+ context . toolCalls . size > 0 ||
10434+ ( yield * Ref . get ( pendingRuntimeRequests ) ) . size > 0 )
10435+ ) {
10436+ framesSeen = context . wakeFrames ;
10437+ return false ;
10438+ }
10439+ yield * Effect . logWarning (
10440+ context . nativeWakeStarted
10441+ ? "orchestration-v2.claude-wake-turn-went-idle"
10442+ : "orchestration-v2.claude-wake-turn-never-opened" ,
10443+ {
10444+ providerTurnId : context . providerTurnId ,
10445+ nativeThreadId,
10446+ } ,
10447+ ) ;
10448+ const completedAt = yield * DateTime . now ;
10449+ yield * finalizeActiveTurn ( { context, status : "completed" , completedAt } ) ;
10450+ return true ;
10451+ } ) ,
10452+ ) ;
10453+ if ( settled ) return ;
10454+ }
10455+ } ) . pipe ( Effect . forkIn ( sessionScope ) ) ;
1041310456 } ) ,
1041410457 ) ;
1041510458 } ,
0 commit comments