@@ -402,16 +402,19 @@ export function projectAgentRunAttemptTerminal(terminal: AgentRunAttemptTerminal
402402 } ;
403403}
404404
405+ const AGENT_RUN_TERMINAL_CLASSIFICATION = {
406+ completed : "success" ,
407+ hard_timeout : "timeout" ,
408+ timed_out : "timeout" ,
409+ cancelled : "cancellation" ,
410+ aborted : "cancellation" ,
411+ blocked : "failure" ,
412+ abandoned : "failure" ,
413+ failed : "failure" ,
414+ } as const ;
415+
405416/** Normalized terminal reason for an agent run. */
406- type AgentRunTerminalReason =
407- | "completed"
408- | "hard_timeout"
409- | "timed_out"
410- | "cancelled"
411- | "aborted"
412- | "blocked"
413- | "abandoned"
414- | "failed" ;
417+ type AgentRunTerminalReason = keyof typeof AGENT_RUN_TERMINAL_CLASSIFICATION ;
415418
416419/** Normalized terminal outcome for an agent run. */
417420export type AgentRunTerminalOutcome = {
@@ -426,29 +429,9 @@ export type AgentRunTerminalOutcome = {
426429 endedAt ?: number ;
427430} ;
428431
429- /** Carries a canonical terminal outcome when an embedded attempt exits by throwing. */
430- export class AgentRunTerminalOutcomeError extends Error {
431- readonly terminalOutcome : AgentRunTerminalOutcome ;
432-
433- constructor ( error : unknown , terminalOutcome : AgentRunTerminalOutcome ) {
434- super ( error instanceof Error ? error . message : String ( error ) , { cause : error } ) ;
435- this . name = "AgentRunTerminalOutcomeError" ;
436- this . terminalOutcome = terminalOutcome ;
437- }
438- }
439-
440- /** Finds a canonical terminal outcome through ordinary error wrapper boundaries. */
441- export function findAgentRunTerminalOutcome ( error : unknown ) : AgentRunTerminalOutcome | undefined {
442- let candidate = error ;
443- const seen = new Set < object > ( ) ;
444- while ( candidate && typeof candidate === "object" && ! seen . has ( candidate ) ) {
445- seen . add ( candidate ) ;
446- if ( candidate instanceof AgentRunTerminalOutcomeError ) {
447- return candidate . terminalOutcome ;
448- }
449- candidate = ( candidate as { cause ?: unknown } ) . cause ;
450- }
451- return undefined ;
432+ /** Collapses terminal reasons into the four projections shared by run consumers. */
433+ export function classifyAgentRunTerminalOutcome ( outcome : Pick < AgentRunTerminalOutcome , "reason" > ) {
434+ return AGENT_RUN_TERMINAL_CLASSIFICATION [ outcome . reason ] ;
452435}
453436
454437/** Raw terminal input collected from run wait/liveness/timeout paths. */
@@ -468,6 +451,11 @@ type AgentRunTerminalWaitInput = Omit<AgentRunTerminalInput, "status"> & {
468451 status ?: unknown ;
469452} ;
470453
454+ type AgentRunLifecycleTerminalData = Omit < AgentRunTerminalWaitInput , "status" > & {
455+ aborted ?: unknown ;
456+ status ?: unknown ;
457+ } ;
458+
471459/** Shared grace window for terminal observations that may still be followed by a retry. */
472460export const AGENT_RUN_TERMINAL_RETRY_GRACE_MS = 15_000 ;
473461
@@ -574,6 +562,64 @@ export function buildAgentRunTerminalOutcome(
574562 } ;
575563}
576564
565+ /** Builds the canonical outcome directly from a terminal lifecycle event. */
566+ export function buildAgentRunTerminalOutcomeFromLifecycleEvent ( input : {
567+ phase : "end" | "error" ;
568+ data ?: AgentRunLifecycleTerminalData ;
569+ abortSignal ?: AbortSignal ;
570+ startedAt ?: unknown ;
571+ endedAt ?: unknown ;
572+ } ) : AgentRunTerminalOutcome {
573+ const data = input . data ;
574+ const abortFields =
575+ typeof data ?. aborted === "boolean"
576+ ? { }
577+ : resolveAgentRunAbortLifecycleFields ( input . abortSignal ) ;
578+ const stopReason = asNonEmptyString ( data ?. stopReason ) ?? abortFields . stopReason ;
579+ const timeoutPhase = normalizeAgentRunTimeoutPhase ( data ?. timeoutPhase ) ;
580+ const lifecycleStatus = asNonEmptyString ( data ?. status ) ?. toLowerCase ( ) ;
581+ // Bare `aborted` is cancellation; timeout needs a structured status, stop
582+ // reason, or phase so legacy lifecycle gaps cannot turn user stops into timeouts.
583+ const timedOut =
584+ stopReason === "timeout" ||
585+ timeoutPhase !== undefined ||
586+ lifecycleStatus === "timeout" ||
587+ lifecycleStatus === "timed_out" ;
588+ const aborted =
589+ data ?. aborted === true || abortFields . aborted === true || lifecycleStatus === "aborted" ;
590+ const cancellationStatus =
591+ lifecycleStatus === "cancelled" ||
592+ lifecycleStatus === "canceled" ||
593+ lifecycleStatus === "aborted" ;
594+ const cancelled = cancellationStatus || aborted ;
595+ const failed =
596+ input . phase === "error" ||
597+ lifecycleStatus === "error" ||
598+ lifecycleStatus === "failed" ||
599+ stopReason === "error" ;
600+ const normalizedStopReason =
601+ ! timedOut &&
602+ cancelled &&
603+ ! isAbortedAgentStopReason ( stopReason ) &&
604+ ! isCancellationStopReason ( stopReason ) &&
605+ ( stopReason === undefined || cancellationStatus )
606+ ? aborted
607+ ? "aborted"
608+ : "stop"
609+ : stopReason ;
610+ const outcome = buildAgentRunTerminalOutcome ( {
611+ status : timedOut ? "timeout" : cancelled || failed ? "error" : "ok" ,
612+ error : data ?. error ,
613+ stopReason : normalizedStopReason ,
614+ livenessState : data ?. livenessState ,
615+ timeoutPhase,
616+ providerStarted : data ?. providerStarted ,
617+ startedAt : input . startedAt ?? data ?. startedAt ,
618+ endedAt : input . endedAt ?? data ?. endedAt ,
619+ } ) ;
620+ return stopReason && outcome . stopReason !== stopReason ? { ...outcome , stopReason } : outcome ;
621+ }
622+
577623function hasRestartAbortReason ( value : unknown ) : boolean {
578624 let candidate = value ;
579625 for ( let depth = 0 ; depth < 3 ; depth += 1 ) {
0 commit comments