@@ -49,6 +49,7 @@ let isOperationInProgress = false
4949let currentSessionID : string | null = null
5050let currentModel : string | null = null
5151let currentAgent : string | undefined
52+ let autoWakeupSessionID : string | null = null
5253
5354// Live block for tool/task display
5455const block = createLiveBlock ( )
@@ -59,6 +60,9 @@ setBlockFreeze(() => block.freeze())
5960// Track task tool ID to child session ID mapping
6061const taskToChildSession = new Map < string , string > ( )
6162
63+ // Track part IDs to tool IDs for linking metadata updates
64+ const partIdToToolId = new Map < string , string > ( )
65+
6266// Store Bus subscription cleanup function
6367let busUnsubscribe : ( ( ) => void ) | undefined
6468
@@ -76,6 +80,7 @@ async function main() {
7680 } )
7781
7882 function cleanup ( ) {
83+ if ( autoWakeupSessionID ) Session . disableAutoWakeup ( autoWakeupSessionID )
7984 if ( busUnsubscribe ) busUnsubscribe ( )
8085 block . freeze ( )
8186 write ( cursor . show )
@@ -104,6 +109,20 @@ async function main() {
104109 const part = event . properties . part
105110 if ( part . type !== "tool" ) return
106111
112+ // Check for task metadata updates (sessionId becomes available after session creation)
113+ if ( part . tool === "task" && part . callID ) {
114+ const metadata =
115+ part . state . status === "running" || part . state . status === "completed" ? part . state . metadata : undefined
116+ const sessionId = metadata ?. sessionId as string | undefined
117+ if ( sessionId ) {
118+ const toolId = partIdToToolId . get ( part . callID )
119+ if ( toolId && ! taskToChildSession . has ( toolId ) ) {
120+ Panel . addChild ( sessionId )
121+ taskToChildSession . set ( toolId , sessionId )
122+ }
123+ }
124+ }
125+
107126 // Find which task this child session belongs to
108127 let taskId : string | null = null
109128 for ( const [ id , childSessionID ] of taskToChildSession . entries ( ) ) {
@@ -168,6 +187,8 @@ async function main() {
168187 if ( key . name === "escape" && isOperationInProgress && currentSessionID ) {
169188 block . freeze ( )
170189 SessionPrompt . cancel ( currentSessionID )
190+ taskToChildSession . clear ( )
191+ partIdToToolId . clear ( )
171192 isOperationInProgress = false
172193 currentSessionID = null
173194 write ( "\n" )
@@ -396,6 +417,7 @@ interface StreamOptions {
396417async function streamResponse ( source : AsyncIterable < ChatChunk > , options : StreamOptions ) {
397418 block . reset ( )
398419 taskToChildSession . clear ( )
420+ partIdToToolId . clear ( )
399421 const spinner = new Spinner ( "Thinking" )
400422 spinner . start ( )
401423
@@ -424,6 +446,11 @@ async function streamResponse(source: AsyncIterable<ChatChunk>, options: StreamO
424446 for await ( const chunk of source ) {
425447 if ( chunk . type === "start" && chunk . sessionID && ! currentSessionID ) {
426448 currentSessionID = chunk . sessionID
449+ if ( autoWakeupSessionID && autoWakeupSessionID !== currentSessionID ) {
450+ Session . disableAutoWakeup ( autoWakeupSessionID )
451+ }
452+ Session . enableAutoWakeup ( currentSessionID )
453+ autoWakeupSessionID = currentSessionID
427454 }
428455
429456 if ( first && chunk . type !== "done" && chunk . type !== "start" ) {
@@ -475,7 +502,10 @@ async function streamResponse(source: AsyncIterable<ChatChunk>, options: StreamO
475502
476503 const label = dedupCount > 1 ? `${ summary } (×${ dedupCount } )` : summary
477504 block . toolStart ( lastToolId , tool , label )
478- if ( chunk . callID ) idMap . set ( chunk . callID , lastToolId )
505+ if ( chunk . callID ) {
506+ idMap . set ( chunk . callID , lastToolId )
507+ partIdToToolId . set ( chunk . callID , lastToolId )
508+ }
479509
480510 if ( tool === "task" && chunk . input && typeof chunk . input === "object" ) {
481511 const input = chunk . input as TaskInput
@@ -484,11 +514,6 @@ async function streamResponse(source: AsyncIterable<ChatChunk>, options: StreamO
484514 if ( agent && description ) {
485515 block . taskStart ( lastToolId , agent , description )
486516 }
487- const childSessionID = typeof chunk . metadata ?. sessionId === "string" ? chunk . metadata . sessionId : undefined
488- if ( childSessionID ) {
489- Panel . addChild ( childSessionID )
490- taskToChildSession . set ( lastToolId , childSessionID )
491- }
492517 }
493518 lastChunkType = "tool"
494519 }
0 commit comments