55 */
66import { getRuntimeConfig } from "../config/config.js" ;
77import type { OpenClawConfig } from "../config/types.openclaw.js" ;
8+ import {
9+ recordDurableSubagentRegistered ,
10+ recordDurableSubagentTerminal ,
11+ } from "../durable/subagent.js" ;
812import { callGateway } from "../gateway/call.js" ;
913import { createSubsystemLogger } from "../logging/subsystem.js" ;
1014import { getGlobalHookRunner } from "../plugins/hook-runner-global.js" ;
@@ -313,6 +317,11 @@ export function createSubagentRunManager(params: {
313317 timeoutCompletion . startedAt = startedAt ;
314318 }
315319 completionForRetry = timeoutCompletion ;
320+ recordDurableSubagentTerminal ( {
321+ runId,
322+ childSessionKey : entry . childSessionKey ,
323+ status : "timeout" ,
324+ } ) ;
316325 await params . completeSubagentRun ( completionForRetry ) ;
317326 } ;
318327 if ( waitStatus === "timeout" ) {
@@ -441,6 +450,12 @@ export function createSubagentRunManager(params: {
441450 triggerCleanup : true ,
442451 startedAt : observedStartedAt ,
443452 } ;
453+ recordDurableSubagentTerminal ( {
454+ runId,
455+ childSessionKey : entry . childSessionKey ,
456+ status : outcome . status ,
457+ error : outcome . status === "error" ? outcome . error : undefined ,
458+ } ) ;
444459 await params . completeSubagentRun ( completionForRetry ) ;
445460 } catch ( error ) {
446461 const current = params . runs . get ( runId ) ;
@@ -681,8 +696,9 @@ export function createSubagentRunManager(params: {
681696 params . runs . delete ( runId ) ;
682697 throw error ;
683698 }
699+ let backgroundTask : ReturnType < typeof createRunningTaskRun > | null | undefined ;
684700 try {
685- const task = createRunningTaskRun ( {
701+ backgroundTask = createRunningTaskRun ( {
686702 runtime : "subagent" ,
687703 sourceId : runId ,
688704 ownerKey : requesterSessionKey ,
@@ -699,7 +715,7 @@ export function createSubagentRunManager(params: {
699715 startedAt : now ,
700716 lastEventAt : now ,
701717 } ) ;
702- if ( ! task ) {
718+ if ( ! backgroundTask ) {
703719 log . warn ( "Failed to persist background task for subagent run" , {
704720 runId : registerParams . runId ,
705721 } ) ;
@@ -710,6 +726,18 @@ export function createSubagentRunManager(params: {
710726 error,
711727 } ) ;
712728 }
729+ recordDurableSubagentRegistered ( {
730+ runId,
731+ childSessionKey,
732+ requesterSessionKey,
733+ taskId : backgroundTask ?. taskId ,
734+ taskFlowId : backgroundTask ?. parentFlowId ,
735+ task : registerParams . task ,
736+ taskName : registerParams . taskName ,
737+ label : registerParams . label ,
738+ agentId : registerParams . agentId ,
739+ requesterAgentId : registerParams . requesterAgentId ,
740+ } ) ;
713741 params . ensureListener ( ) ;
714742 params . persist ( ) ;
715743 // Always start sweeper — session-mode runs (no archiveAtMs) also need TTL cleanup.
@@ -787,6 +815,12 @@ export function createSubagentRunManager(params: {
787815 entry . cleanupHandled = true ;
788816 entry . cleanupCompletedAt = now ;
789817 entry . suppressAnnounceReason = "killed" ;
818+ recordDurableSubagentTerminal ( {
819+ runId,
820+ childSessionKey : entry . childSessionKey ,
821+ status : "killed" ,
822+ error : reason ,
823+ } ) ;
790824 if ( ! entriesByChildSessionKey . has ( entry . childSessionKey ) ) {
791825 entriesByChildSessionKey . set ( entry . childSessionKey , entry ) ;
792826 }
0 commit comments