@@ -207,18 +207,26 @@ export async function executeAgent(
207207) : Promise < AgentExecutionOutput > {
208208 const { workingDir, projectRoot, engine : engineOverride , model : modelOverride , logger, stderrLogger, onTelemetry, abortSignal, timeout, parentId, disableMonitoring, ui, uniqueAgentId, displayPrompt, resumeMonitoringId, resumePrompt, resumeSessionId : resumeSessionIdOption , selectedConditions } = options ;
209209
210+ debug ( `[AgentRunner] executeAgent called: agentId=%s promptLength=%d` , agentId , prompt . length ) ;
211+ debug ( `[AgentRunner] Options: workingDir=%s engineOverride=%s modelOverride=%s parentId=%s` ,
212+ workingDir , engineOverride ?? '(none)' , modelOverride ?? '(none)' , parentId ?? '(none)' ) ;
213+ debug ( `[AgentRunner] Resume options: resumeMonitoringId=%s resumeSessionId=%s resumePrompt=%s` ,
214+ resumeMonitoringId ?? '(none)' , resumeSessionIdOption ?? '(none)' , resumePrompt ? resumePrompt . slice ( 0 , 50 ) + '...' : '(none)' ) ;
215+
210216 // If resuming, use direct sessionId or look up from monitor
211217 let resumeSessionId : string | undefined = resumeSessionIdOption ;
212218 if ( ! resumeSessionId && resumeMonitoringId !== undefined ) {
213219 const monitor = AgentMonitorService . getInstance ( ) ;
214220 const resumeAgent = monitor . getAgent ( resumeMonitoringId ) ;
221+ debug ( `[AgentRunner] Looking up sessionId from monitoringId=%d, found agent=%s` ,
222+ resumeMonitoringId , resumeAgent ? 'yes' : 'no' ) ;
215223 if ( resumeAgent ?. sessionId ) {
216224 resumeSessionId = resumeAgent . sessionId ;
217- debug ( `[RESUME ] Using sessionId ${ resumeSessionId } from monitoringId ${ resumeMonitoringId } ` ) ;
225+ debug ( `[AgentRunner ] Using sessionId %s from monitoringId %d` , resumeSessionId , resumeMonitoringId ) ;
218226 }
219227 }
220228 if ( resumeSessionId ) {
221- debug ( `[RESUME] Resuming with sessionId: ${ resumeSessionId } ` ) ;
229+ debug ( `[AgentRunner] Will resume with sessionId: %s` , resumeSessionId ) ;
222230 }
223231
224232 // Load agent config to determine engine and model
@@ -284,18 +292,21 @@ export async function executeAgent(
284292 if ( resumeMonitoringId !== undefined ) {
285293 // RESUME: Use existing monitoring entry (skip registration, use existing log file)
286294 monitoringAgentId = resumeMonitoringId ;
287- debug ( `[RESUME] Using existing monitoringId ${ monitoringAgentId } , skipping registration` ) ;
295+ debug ( `[AgentRunner] RESUME: Using existing monitoringId=%d , skipping registration` , monitoringAgentId ) ;
288296
289297 // Mark as running again (was paused)
290298 await monitor . markRunning ( monitoringAgentId ) ;
299+ debug ( `[AgentRunner] RESUME: Marked agent as running` ) ;
291300
292301 // Register monitoring ID with UI so it can load existing logs
293302 if ( ui && uniqueAgentId ) {
303+ debug ( `[AgentRunner] RESUME: Registering monitoringId=%d with UI (uniqueAgentId=%s)` , monitoringAgentId , uniqueAgentId ) ;
294304 ui . registerMonitoringId ( uniqueAgentId , monitoringAgentId ) ;
295305 }
296306 } else {
297307 // NEW EXECUTION: Register new monitoring entry
298308 const promptForDisplay = displayPrompt || prompt ;
309+ debug ( `[AgentRunner] NEW: Registering new monitoring entry for agentId=%s` , agentId ) ;
299310 monitoringAgentId = await monitor . register ( {
300311 name : agentId ,
301312 prompt : promptForDisplay , // This gets truncated in monitor for memory efficiency
@@ -304,13 +315,15 @@ export async function executeAgent(
304315 engineProvider : engineType ,
305316 modelName : model ,
306317 } ) ;
318+ debug ( `[AgentRunner] NEW: Registered with monitoringId=%d` , monitoringAgentId ) ;
307319
308320 // Store FULL prompt for debug mode logging (not the display prompt)
309321 // In debug mode, we want to see the complete composite prompt with template + input files
310322 loggerService . storeFullPrompt ( monitoringAgentId , prompt ) ;
311323
312324 // Register monitoring ID with UI immediately so it can load logs
313325 if ( ui && uniqueAgentId && monitoringAgentId !== undefined ) {
326+ debug ( `[AgentRunner] NEW: Registering monitoringId=%d with UI (uniqueAgentId=%s)` , monitoringAgentId , uniqueAgentId ) ;
314327 ui . registerMonitoringId ( uniqueAgentId , monitoringAgentId ) ;
315328 }
316329 }
@@ -324,6 +337,8 @@ export async function executeAgent(
324337 // Get engine and execute
325338 // NOTE: Prompt is already complete - no template loading or building here
326339 const engine = getEngine ( engineType ) ;
340+ debug ( `[AgentRunner] Starting engine execution: engine=%s model=%s resumeSessionId=%s` ,
341+ engineType , model , resumeSessionId ?? '(new session)' ) ;
327342
328343 let totalStdout = '' ;
329344
@@ -416,8 +431,11 @@ export async function executeAgent(
416431 timestamp : new Date ( ) . toISOString ( ) ,
417432 } ) ;
418433
434+ debug ( `[AgentRunner] Engine execution completed, outputLength=%d` , totalStdout . length ) ;
435+
419436 // Mark agent as completed
420437 if ( monitor && monitoringAgentId !== undefined ) {
438+ debug ( `[AgentRunner] Marking agent %d as completed` , monitoringAgentId ) ;
421439 await monitor . complete ( monitoringAgentId ) ;
422440 // Note: Don't close stream here - workflow may write more messages
423441 // Streams will be closed by cleanup handlers or monitoring service shutdown
@@ -427,7 +445,7 @@ export async function executeAgent(
427445 // Always load on fresh execution; on resume, workflow.ts decides whether to use them
428446 // based on chain resume state (chainResumeInfo)
429447 let chainedPrompts : ChainedPrompt [ ] | undefined ;
430- debug ( `[ChainedPrompts] agentConfig.chainedPromptsPath: ${ agentConfig . chainedPromptsPath } ` ) ;
448+ debug ( `[AgentRunner] ChainedPrompts path: %s` , agentConfig . chainedPromptsPath ?? '(none)' ) ;
431449 if ( agentConfig . chainedPromptsPath ) {
432450 chainedPrompts = await loadChainedPrompts (
433451 agentConfig . chainedPromptsPath ,
@@ -439,21 +457,30 @@ export async function executeAgent(
439457 debug ( `[ChainedPrompts] No chainedPromptsPath for agent '${ agentId } '` ) ;
440458 }
441459
460+ debug ( `[AgentRunner] Returning result: monitoringAgentId=%d chainedPrompts=%d` ,
461+ monitoringAgentId ?? - 1 , chainedPrompts ?. length ?? 0 ) ;
462+
442463 return {
443464 output : stdout ,
444465 agentId : monitoringAgentId ,
445466 chainedPrompts,
446467 } ;
447- } catch ( error ) {
468+ } catch ( err ) {
469+ debug ( `[AgentRunner] Error during execution: %s` , ( err as Error ) . message ) ;
470+
448471 // Mark agent as failed (unless already paused - that means intentional abort)
449472 if ( monitor && monitoringAgentId !== undefined ) {
450473 const agent = monitor . getAgent ( monitoringAgentId ) ;
474+ debug ( `[AgentRunner] Agent status: %s` , agent ?. status ?? '(not found)' ) ;
451475 if ( agent ?. status !== 'paused' ) {
452- await monitor . fail ( monitoringAgentId , error as Error ) ;
476+ debug ( `[AgentRunner] Marking agent %d as failed` , monitoringAgentId ) ;
477+ await monitor . fail ( monitoringAgentId , err as Error ) ;
478+ } else {
479+ debug ( `[AgentRunner] Agent is paused, not marking as failed` ) ;
453480 }
454481 // Note: Don't close stream here - workflow may write more messages
455482 // Streams will be closed by cleanup handlers or monitoring service shutdown
456483 }
457- throw error ;
484+ throw err ;
458485 }
459486}
0 commit comments