11import type { AgentEventPayload } from "../../../infra/agent-events.js" ;
22import { runWithGatewayIndependentRootWorkAdmission } from "../../../process/gateway-work-admission.js" ;
3- import {
4- formatAbandonedLivenessError ,
5- formatBlockedLivenessError ,
6- isAbandonedLivenessState ,
7- isBlockedLivenessState ,
8- } from "../../../shared/agent-liveness.js" ;
3+ import { buildAgentRunTerminalOutcomeFromLifecycleEvent } from "../../agent-run-terminal-outcome.js" ;
94import { normalizeAgentRunTerminalReplySnapshot } from "../../agent-run-terminal-reply.js" ;
10- import { isAbortedAgentStopReason } from "../../run-termination .js" ;
5+ import { classifySubagentTerminalOutcome } from "../subagent-terminal-outcome .js" ;
116import {
127 SUBAGENT_ENDED_REASON_COMPLETE ,
138 SUBAGENT_ENDED_REASON_ERROR ,
@@ -82,11 +77,6 @@ export function createSubagentRegistryListener(config: {
8277 }
8378 const endedAt = typeof evt . data ?. endedAt === "number" ? evt . data . endedAt : Date . now ( ) ;
8479 const startedAt = typeof evt . data ?. startedAt === "number" ? evt . data . startedAt : undefined ;
85- const error = typeof evt . data ?. error === "string" ? evt . data . error : undefined ;
86- const livenessState =
87- typeof evt . data ?. livenessState === "string" ? evt . data . livenessState : undefined ;
88- const stopReason =
89- typeof evt . data ?. stopReason === "string" ? evt . data . stopReason : undefined ;
9080 const terminalReply = normalizeAgentRunTerminalReplySnapshot ( evt . data ?. terminalReply ) ;
9181 // sessions_yield ends the turn by aborting the run signal, so a yielded
9282 // terminal can also look aborted. An explicit yield is authoritative — pause,
@@ -106,72 +96,69 @@ export function createSubagentRegistryListener(config: {
10696 }
10797 return ;
10898 }
109- if ( isAbortedAgentStopReason ( stopReason ) ) {
110- pendingLifecycle . clear ( evt . runId ) ;
111- await completeSubagentRunWithRecovery (
112- {
113- runId : evt . runId ,
114- endedAt,
115- outcome : {
116- status : "error" ,
117- error : "subagent run terminated" ,
118- } ,
119- reason : SUBAGENT_ENDED_REASON_KILLED ,
120- sendFarewell : true ,
121- accountId : entry . requesterOrigin ?. accountId ,
122- triggerCleanup : true ,
123- startedAt,
124- terminalReply,
125- } ,
126- "lifecycle-killed-event" ,
127- ) ;
128- return ;
129- }
130- if ( phase === "error" ) {
131- pendingLifecycle . scheduleError ( {
99+ const terminalOutcome = buildAgentRunTerminalOutcomeFromLifecycleEvent ( {
100+ phase,
101+ data : evt . data ,
102+ startedAt,
103+ endedAt,
104+ } ) ;
105+ const classification = classifySubagentTerminalOutcome ( terminalOutcome ) ;
106+ if (
107+ classification === "cancellation" &&
108+ evt . data ?. aborted === true &&
109+ evt . data . stopReason === undefined &&
110+ evt . data . status === undefined &&
111+ evt . data . timeoutPhase === undefined
112+ ) {
113+ pendingLifecycle . scheduleCancellation ( {
132114 runId : evt . runId ,
133115 endedAt,
134116 startedAt,
135117 terminalReply,
136- error,
137118 } ) ;
138119 return ;
139120 }
140- const blocked = isBlockedLivenessState ( livenessState ) ;
141- const abandoned = isAbandonedLivenessState ( livenessState ) ;
142- if ( blocked || abandoned ) {
143- pendingLifecycle . clear ( evt . runId ) ;
144- const blockedParams = {
121+ if ( classification === "timeout" ) {
122+ pendingLifecycle . scheduleTimeout ( {
145123 runId : evt . runId ,
146124 endedAt,
147- outcome : {
148- status : "error" as const ,
149- error : blocked
150- ? formatBlockedLivenessError ( error )
151- : formatAbandonedLivenessError ( error ) ,
152- } ,
153- reason : SUBAGENT_ENDED_REASON_ERROR ,
154- sendFarewell : true ,
155- accountId : entry . requesterOrigin ?. accountId ,
156- triggerCleanup : true ,
157125 startedAt,
158126 terminalReply,
159- } ;
160- await completeSubagentRunWithRecovery (
161- blockedParams ,
162- blocked ? "lifecycle-blocked-event" : "lifecycle-abandoned-event" ,
163- ) ;
127+ } ) ;
164128 return ;
165129 }
166- if ( evt . data ?. aborted ) {
167- pendingLifecycle . scheduleTimeout ( {
130+ if ( phase === "error" && classification === "failure" ) {
131+ pendingLifecycle . scheduleError ( {
168132 runId : evt . runId ,
169133 endedAt,
170134 startedAt,
171135 terminalReply,
136+ error : terminalOutcome . error ,
172137 } ) ;
173138 return ;
174139 }
140+ if ( classification !== "success" ) {
141+ const cancelled = classification === "cancellation" ;
142+ pendingLifecycle . clear ( evt . runId ) ;
143+ await completeSubagentRunWithRecovery (
144+ {
145+ runId : evt . runId ,
146+ endedAt,
147+ outcome : {
148+ status : "error" as const ,
149+ error : cancelled ? "subagent run terminated" : terminalOutcome . error ,
150+ } ,
151+ reason : cancelled ? SUBAGENT_ENDED_REASON_KILLED : SUBAGENT_ENDED_REASON_ERROR ,
152+ sendFarewell : true ,
153+ accountId : entry . requesterOrigin ?. accountId ,
154+ triggerCleanup : true ,
155+ startedAt,
156+ terminalReply,
157+ } ,
158+ cancelled ? "lifecycle-killed-event" : `lifecycle-${ terminalOutcome . reason } -event` ,
159+ ) ;
160+ return ;
161+ }
175162 pendingLifecycle . clear ( evt . runId ) ;
176163 const completionParams = {
177164 runId : evt . runId ,
0 commit comments