@@ -194,7 +194,7 @@ export class AgentRunner {
194194 defaultCreateErrorResult : AgentRunnerHooks [ 'createErrorResult' ] ,
195195 llmToolArgs ?: ConfigurableAgentArgs , // Specific args if triggered by LLM tool call
196196 parentSession ?: AgentSession // For natural nesting
197- ) : Promise < ConfigurableAgentResult > {
197+ ) : Promise < ConfigurableAgentResult & { agentSession : AgentSession } > {
198198 const targetAgentName = handoffConfig . targetAgentName ;
199199 const targetAgentTool = ToolRegistry . getRegisteredTool ( targetAgentName ) ;
200200
@@ -287,32 +287,42 @@ export class AgentRunner {
287287 targetRunnerConfig , // Pass the constructed config
288288 targetRunnerHooks , // Pass the constructed hooks
289289 targetAgentTool , // Target agent is now the executing agent
290- undefined // No tracing context for handoff (would need to be passed through)
290+ parentSession // Pass parent session for natural nesting
291291 ) ;
292292
293- logger . info ( 'Handoff target agent ${targetAgentTool.name} finished. Result success: ${handoffResult.success}' ) ;
293+ // Extract the result and session
294+ const { agentSession : childSession , ...actualResult } = handoffResult ;
295+
296+ // Add child session to parent's nested sessions (natural nesting)
297+ if ( parentSession ) {
298+ parentSession . nestedSessions . push ( childSession ) ;
299+ }
300+
301+ logger . info ( `Handoff target agent ${ targetAgentTool . name } finished. Result success: ${ actualResult . success } ` ) ;
294302
295303 // Check if the target agent is configured to *include* intermediate steps
296304 if ( targetAgentTool instanceof ConfigurableAgentTool && targetAgentTool . config . includeIntermediateStepsOnReturn === true ) {
297305 // Combine message history if the target agent requests it
298306 logger . info ( `Including intermediateSteps from ${ targetAgentTool . name } based on its config.` ) ;
299307 const combinedIntermediateSteps = [
300308 ...currentMessages , // History *before* the recursive call
301- ...( handoffResult . intermediateSteps || [ ] ) // History *from* the recursive call (should exist if flag is true)
309+ ...( actualResult . intermediateSteps || [ ] ) // History *from* the recursive call (should exist if flag is true)
302310 ] ;
303311 // Return the result from the target agent, but with combined history
304312 return {
305- ...handoffResult ,
313+ ...actualResult ,
306314 intermediateSteps : combinedIntermediateSteps ,
307- terminationReason : handoffResult . terminationReason || 'handed_off'
315+ terminationReason : actualResult . terminationReason || 'handed_off' ,
316+ agentSession : childSession
308317 } ;
309318 }
310319 // Otherwise (default), omit the target's intermediate steps
311320 logger . info ( `Omitting intermediateSteps from ${ targetAgentTool . name } based on its config (default or flag set to false).` ) ;
312321 // Return result from target, ensuring intermediateSteps are omitted
313322 const finalResult = {
314- ...handoffResult ,
315- terminationReason : handoffResult . terminationReason || 'handed_off'
323+ ...actualResult ,
324+ terminationReason : actualResult . terminationReason || 'handed_off' ,
325+ agentSession : childSession
316326 } ;
317327 // Explicitly delete intermediateSteps if they somehow exist on actualResult (shouldn't due to target config)
318328 delete finalResult . intermediateSteps ;
@@ -326,10 +336,9 @@ export class AgentRunner {
326336 config : AgentRunnerConfig ,
327337 hooks : AgentRunnerHooks ,
328338 executingAgent : ConfigurableAgentTool | null ,
329- tracingContext ?: any
330- ) : Promise < ConfigurableAgentResult > {
339+ parentSession ?: AgentSession // For natural nesting
340+ ) : Promise < ConfigurableAgentResult & { agentSession : AgentSession } > {
331341 const agentName = executingAgent ?. name || 'Unknown' ;
332-
333342 logger . info ( 'Starting execution loop for agent: ${agentName}' ) ;
334343 const { apiKey, modelName, systemPrompt, tools, maxIterations, temperature } = config ;
335344 const { prepareInitialMessages, createSuccessResult, createErrorResult } = hooks ;
@@ -343,7 +352,7 @@ export class AgentRunner {
343352 agentDisplayName : executingAgent ?. config ?. ui ?. displayName || agentName ,
344353 agentDescription : executingAgent ?. config ?. description ,
345354 sessionId : crypto . randomUUID ( ) ,
346- parentSessionId : undefined , // No parent session for top-level agent
355+ parentSessionId : parentSession ?. sessionId ,
347356 status : 'running' ,
348357 startTime : new Date ( ) ,
349358 messages : [ ] ,
@@ -502,11 +511,6 @@ export class AgentRunner {
502511 logger . info ( `${ agentName } Created AgentRunner LLM generation trace: ${ generationId } ` ) ;
503512 }
504513
505- logger . info ( '${agentName} Calling LLM with ${messages.length} messages' ) ;
506-
507- // Try to get tracing context from getCurrentTracingContext if not passed explicitly
508- const effectiveTracingContext = tracingContext || getCurrentTracingContext ( ) ;
509-
510514 const llm = LLMClient . getInstance ( ) ;
511515 const provider = AIChatPanel . getProviderForModel ( modelName ) ;
512516 const llmMessages = AgentRunner . convertToLLMMessages ( messages ) ;
@@ -555,25 +559,6 @@ export class AgentRunner {
555559 duration : Date . now ( ) - generationStartTime . getTime ( )
556560 } ) ;
557561 }
558-
559- // Update generation observation with output
560- if ( generationId && effectiveTracingContext ?. traceId ) {
561- const tracingProvider = createTracingProvider ( ) ;
562- try {
563- await tracingProvider . createObservation ( {
564- id : generationId ,
565- name : `LLM Generation (AgentRunner): ${ agentName } ` ,
566- type : 'generation' ,
567- endTime : new Date ( ) ,
568- output : {
569- response : llmResponse . text || 'No text response' ,
570- reasoning : llmResponse . reasoning ?. summary
571- }
572- } , effectiveTracingContext . traceId ) ;
573- } catch ( tracingError ) {
574- logger . warn ( 'Failed to update generation observation:' , tracingError ) ;
575- }
576- }
577562 } catch ( error : any ) {
578563 logger . error ( `${ agentName } LLM call failed:` , error ) ;
579564 const errorMsg = `LLM call failed: ${ error . message || String ( error ) } ` ;
@@ -1057,7 +1042,7 @@ export class AgentRunner {
10571042 const { agentSession : childSession , ...actualResult } = handoffResult ;
10581043
10591044 // Add child session to current session's nested sessions (natural nesting)
1060- if ( this . currentSession && childSession ) {
1045+ if ( this . currentSession ) {
10611046 this . currentSession . nestedSessions . push ( childSession ) ;
10621047 }
10631048
0 commit comments