Skip to content

Commit 6db0506

Browse files
authored
fix: lower successful agent stop completion logs (#101703)
1 parent 380bc24 commit 6db0506

2 files changed

Lines changed: 93 additions & 2 deletions

File tree

src/agents/agent-command.ingress-diagnostics.test.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,63 @@ afterEach(() => {
6363
vi.clearAllMocks();
6464
});
6565

66+
describe("resolveAgentRunLifecycleEndLogLevel", () => {
67+
it("logs successful stop and tool-use metadata at info", () => {
68+
expect(
69+
testing.resolveAgentRunLifecycleEndLogLevel({
70+
aborted: false,
71+
stopReason: "stop",
72+
}),
73+
).toBe("info");
74+
expect(
75+
testing.resolveAgentRunLifecycleEndLogLevel({
76+
aborted: false,
77+
stopReason: "toolUse",
78+
}),
79+
).toBe("info");
80+
});
81+
82+
it("does not log ordinary end-turn completions", () => {
83+
expect(
84+
testing.resolveAgentRunLifecycleEndLogLevel({
85+
aborted: false,
86+
stopReason: "end_turn",
87+
}),
88+
).toBeUndefined();
89+
expect(testing.resolveAgentRunLifecycleEndLogLevel({ aborted: false })).toBeUndefined();
90+
});
91+
92+
it("keeps timeout metadata out of error severity", () => {
93+
expect(
94+
testing.resolveAgentRunLifecycleEndLogLevel({
95+
aborted: true,
96+
stopReason: "timeout",
97+
}),
98+
).toBe("warn");
99+
expect(
100+
testing.resolveAgentRunLifecycleEndLogLevel({
101+
stopReason: "stop",
102+
timeoutPhase: "provider",
103+
providerStarted: true,
104+
}),
105+
).toBe("warn");
106+
});
107+
108+
it("logs cancelled and failed endings at error", () => {
109+
expect(
110+
testing.resolveAgentRunLifecycleEndLogLevel({
111+
aborted: true,
112+
stopReason: "stop",
113+
}),
114+
).toBe("error");
115+
expect(
116+
testing.resolveAgentRunLifecycleEndLogLevel({
117+
stopReason: "error",
118+
}),
119+
).toBe("error");
120+
});
121+
});
122+
66123
function makeResult(overrides?: Record<string, unknown>) {
67124
return {
68125
payloads: [{ text: "hello", mediaUrl: "" }],

src/agents/agent-command.ts

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ import {
7373
resolveMessageChannel,
7474
} from "../utils/message-channel.js";
7575
import { estimateUsageCost, resolveModelCostConfig } from "../utils/usage-format.js";
76+
import { buildAgentRunTerminalOutcome } from "./agent-run-terminal-outcome.js";
7677
import { resolveAgentRuntimeConfig } from "./agent-runtime-config.js";
7778
import {
7879
clearAutoFallbackPrimaryProbeSelection,
@@ -250,6 +251,37 @@ function parseAgentCommandModelRef(
250251
type AttemptExecutionRuntime = typeof import("./command/attempt-execution.runtime.js");
251252
type 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+
253285
function 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

Comments
 (0)