@@ -73,6 +73,7 @@ import {
7373 resolveMessageChannel ,
7474} from "../utils/message-channel.js" ;
7575import { estimateUsageCost , resolveModelCostConfig } from "../utils/usage-format.js" ;
76+ import { buildAgentRunTerminalOutcome } from "./agent-run-terminal-outcome.js" ;
7677import { resolveAgentRuntimeConfig } from "./agent-runtime-config.js" ;
7778import {
7879 clearAutoFallbackPrimaryProbeSelection ,
@@ -250,6 +251,37 @@ function parseAgentCommandModelRef(
250251type AttemptExecutionRuntime = typeof import ( "./command/attempt-execution.runtime.js" ) ;
251252type AgentAttemptResult = Awaited < ReturnType < AttemptExecutionRuntime [ "runAgentAttempt" ] > > ;
252253
254+ function resolveAgentRunLifecycleEndLogLevel ( meta : {
255+ aborted ?: unknown ;
256+ error ?: unknown ;
257+ stopReason ?: unknown ;
258+ livenessState ?: unknown ;
259+ timeoutPhase ?: unknown ;
260+ providerStarted ?: unknown ;
261+ } ) : "info" | "warn" | "error" | undefined {
262+ const status =
263+ meta . stopReason === "timeout" || meta . timeoutPhase
264+ ? "timeout"
265+ : meta . aborted === true || meta . error || meta . stopReason === "error"
266+ ? "error"
267+ : "ok" ;
268+ const outcome = buildAgentRunTerminalOutcome ( {
269+ status,
270+ error : meta . error ,
271+ stopReason : meta . stopReason ,
272+ livenessState : meta . livenessState ,
273+ timeoutPhase : meta . timeoutPhase ,
274+ providerStarted : meta . providerStarted ,
275+ } ) ;
276+ if ( ! outcome . stopReason || outcome . stopReason === "end_turn" ) {
277+ return undefined ;
278+ }
279+ if ( outcome . reason === "completed" ) {
280+ return "info" ;
281+ }
282+ return outcome . status === "timeout" ? "warn" : "error" ;
283+ }
284+
253285function applyAgentRunAbortMetadata < T extends { meta : object } > (
254286 result : T ,
255287 signal : AbortSignal | undefined ,
@@ -1874,8 +1906,9 @@ async function agentCommandInternal(
18741906 }
18751907 attemptLifecycleState . lifecycleEnded = true ;
18761908 const stopReason = runResult . meta . stopReason ;
1877- if ( stopReason && stopReason !== "end_turn" ) {
1878- console . error ( `[agent] run ${ runId } ended with stopReason=${ stopReason } ` ) ;
1909+ const logLevel = resolveAgentRunLifecycleEndLogLevel ( runResult . meta ) ;
1910+ if ( logLevel ) {
1911+ log [ logLevel ] ( `[agent] run ${ runId } ended with stopReason=${ stopReason } ` ) ;
18791912 }
18801913 emitAgentEvent ( {
18811914 runId,
@@ -2736,6 +2769,7 @@ export const testing = {
27362769 resolveAgentRuntimeConfig,
27372770 prepareAgentCommandExecution,
27382771 resolveExplicitAgentCommandSessionKey,
2772+ resolveAgentRunLifecycleEndLogLevel,
27392773 ingressDiagnosticChannel,
27402774 emitIngressModelUsageDiagnostic,
27412775} ;
0 commit comments