@@ -1739,6 +1739,13 @@ function isClaudeBackgroundTasksChangedMessage(message: SDKMessage): boolean {
17391739 ) ;
17401740}
17411741
1742+ // Claude opens every turn it runs with a root `init` frame. Outside a T3 turn
1743+ // that turn is a wake, and `init` comes 20-110 ms after the notification that
1744+ // caused it but seconds before its first output (model thinking time).
1745+ function isClaudeTurnStartMessage ( message : SDKMessage ) : boolean {
1746+ return message . type === "system" && message . subtype === "init" ;
1747+ }
1748+
17421749function claudePendingBackgroundTasksFromRoster (
17431750 roster : ReadonlyMap < string , OrchestrationV2PendingBackgroundTask > ,
17441751) : ReadonlyArray < OrchestrationV2PendingBackgroundTask > {
@@ -5095,12 +5102,14 @@ export function makeClaudeAdapterV2(
50955102 // replay them to the turn that was still starting when they
50965103 // arrived; the offer gate below keeps them from requesting a
50975104 // continuation on their own.
5105+ const isWakeTurnStart = isClaudeTurnStartMessage ( message ) ;
50985106 const isWakeEvidence =
50995107 isPendingTaskNotification ||
51005108 isPendingSubagentNotification ||
51015109 isNestedSubagentNotification ||
51025110 isKnownSubagentTaskStarted ||
51035111 isNewSubagentTaskStarted ||
5112+ isWakeTurnStart ||
51045113 message . type === "assistant" ||
51055114 message . type === "user" ||
51065115 message . type === "result" ||
@@ -5157,12 +5166,14 @@ export function makeClaudeAdapterV2(
51575166 }
51585167 // A terminal task notification can clear the Waiting roster without
51595168 // Claude dequeuing it into a native model turn. Buffer it for replay,
5160- // but do not open an opaque-task continuation until native user,
5161- // assistant, or result output proves that Claude actually began the
5162- // wake turn. Subagent notifications retain their existing immediate
5163- // offer because their projected lifecycle owns the continuation.
5164- // Only root output proves it: a background subagent keeps streaming
5165- // its own frames while the root is idle.
5169+ // but do not open an opaque-task continuation until the turn's `init`,
5170+ // or native user, assistant, or result output, proves that Claude
5171+ // actually began the wake turn. `init` comes first, so the thread
5172+ // shows working while Claude thinks instead of looking finished.
5173+ // Subagent notifications retain their existing immediate offer
5174+ // because their projected lifecycle owns the continuation. Only
5175+ // root frames prove it: a background subagent keeps streaming its
5176+ // own frames while the root is idle.
51665177 const buffered = ( yield * Ref . get ( wakeBuffers ) ) . get ( wakeInput . nativeThreadId ) ;
51675178 const hasBufferedNotification =
51685179 buffered ?. messages . some (
@@ -5175,6 +5186,7 @@ export function makeClaudeAdapterV2(
51755186 if (
51765187 ! isPendingSubagentNotification &&
51775188 ! isNativeOpaqueWakeFrame &&
5189+ ! isWakeTurnStart &&
51785190 message . type !== "result"
51795191 ) {
51805192 return ;
@@ -7135,8 +7147,13 @@ export function makeClaudeAdapterV2(
71357147 yield * handleSdkMessage ( { query : querySession . query , message : lastResult } ) ;
71367148 return ;
71377149 }
7150+ // A drained `init` means Claude began the wake turn, so its output
7151+ // may still be on the way: stay open for it.
71387152 const hasNativeWakeFrame = drained . some (
7139- ( entry ) => entry . type === "user" || entry . type === "assistant" ,
7153+ ( entry ) =>
7154+ entry . type === "user" ||
7155+ entry . type === "assistant" ||
7156+ isClaudeTurnStartMessage ( entry ) ,
71407157 ) ;
71417158 if ( hasOpaqueTaskNotification && ! hasNativeWakeFrame ) {
71427159 const completedAt = yield * DateTime . now ;
0 commit comments