Skip to content

Commit 4a6f99c

Browse files
authored
refactor(agents): route terminal-reason groups through the canonical classifier (#124122)
Root AGENTS.md: terminal state normalizes via agent-run-terminal-outcome.ts; projections must not rederive timeout/cancel precedence. Three consumers re-enumerated the reason groups by hand, so any future reason value would silently misclassify: - subagent-announce-output.ts applySubagentWaitOutcome: byte-equivalent four-way partition replaced with a switch on classifyAgentRunTerminalOutcome (status "timeout" iff reason is a timeout classification, per buildAgentRunTerminalOutcome). - subagent-registry-run-wait.ts waitAborted: aborted|cancelled|superseded is exactly classification === "cancellation" (waitBlocked stays finer-grained intentionally). - embedded-agent-runner terminal-outcome.ts: isEmbeddedRunTerminalTimeout / Abort become one-liners over the classifier. Rider: dropped the provably unreachable chatLink && !finished guard in finalizeLifecycleEvent — peek and shift run in one synchronous frame, so the shifted head is exactly the peeked entry (comment records the invariant). Net -1 production LOC; 2005 tests across agents/subagents/embedded/gateway suites green.
1 parent 5419a70 commit 4a6f99c

4 files changed

Lines changed: 32 additions & 33 deletions

File tree

‎src/agents/embedded-agent-runner/run/terminal-outcome.ts‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
22
buildAgentRunTerminalOutcomeFromAttempt,
3+
classifyAgentRunTerminalOutcome,
34
type AgentRunTerminalOutcome,
45
} from "../../agent-run-terminal-outcome.js";
56
import type { EmbeddedRunAttemptResult } from "./types.js";
@@ -29,15 +30,11 @@ export function resolveEmbeddedRunAttemptTerminalOutcome(params: {
2930
}
3031

3132
export function isEmbeddedRunTerminalTimeout(outcome: AgentRunTerminalOutcome): boolean {
32-
return outcome.reason === "hard_timeout" || outcome.reason === "timed_out";
33+
return classifyAgentRunTerminalOutcome(outcome) === "timeout";
3334
}
3435

3536
export function isEmbeddedRunTerminalAbort(outcome: AgentRunTerminalOutcome): boolean {
36-
return (
37-
outcome.reason === "aborted" ||
38-
outcome.reason === "cancelled" ||
39-
outcome.reason === "superseded"
40-
);
37+
return classifyAgentRunTerminalOutcome(outcome) === "cancellation";
4138
}
4239

4340
export function isEmbeddedRunTerminalInterrupted(outcome: AgentRunTerminalOutcome): boolean {

‎src/agents/subagents/announce/subagent-announce-output.ts‎

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ import type { SessionTranscriptRuntimeTarget } from "../../../config/sessions/se
1010
import { resolveFreshSessionTotalTokens } from "../../../config/sessions/types.js";
1111
import { isFastTestRuntimeEnv } from "../../../infra/env.js";
1212
import { formatDurationCompact } from "../../../infra/format-time/format-duration.js";
13-
import { buildAgentRunTerminalOutcomeFromWaitResult } from "../../agent-run-terminal-outcome.js";
13+
import {
14+
buildAgentRunTerminalOutcomeFromWaitResult,
15+
classifyAgentRunTerminalOutcome,
16+
} from "../../agent-run-terminal-outcome.js";
1417
import { wrapPromptDataBlock } from "../../sanitize-for-prompt.js";
1518
import { extractStoredAssistantText, sanitizeTextContent } from "../../tools/chat-history-text.js";
1619
import {
@@ -340,23 +343,23 @@ export function applySubagentWaitOutcome(params: {
340343
const terminalOutcome = buildAgentRunTerminalOutcomeFromWaitResult(params.wait);
341344
let outcome = next.outcome;
342345
// Capture/announcement callers can pass raw wait snapshots that bypass the
343-
// primary normalizers, so preserve the shared timeout/cancel precedence here.
344-
if (terminalOutcome?.status === "timeout") {
345-
outcome = { status: "timeout" };
346-
} else if (
347-
terminalOutcome?.reason === "aborted" ||
348-
terminalOutcome?.reason === "cancelled" ||
349-
terminalOutcome?.reason === "superseded"
350-
) {
351-
outcome = { status: "error", error: "subagent run terminated" };
352-
} else if (
353-
terminalOutcome?.reason === "blocked" ||
354-
terminalOutcome?.reason === "abandoned" ||
355-
terminalOutcome?.reason === "failed"
356-
) {
357-
outcome = { status: "error", error: terminalOutcome.error ?? waitError };
358-
} else if (terminalOutcome?.reason === "completed") {
359-
outcome = { status: "ok" };
346+
// primary normalizers, so apply the canonical classification here instead
347+
// of re-enumerating reason groups.
348+
if (terminalOutcome) {
349+
switch (classifyAgentRunTerminalOutcome(terminalOutcome)) {
350+
case "timeout":
351+
outcome = { status: "timeout" };
352+
break;
353+
case "cancellation":
354+
outcome = { status: "error", error: "subagent run terminated" };
355+
break;
356+
case "failure":
357+
outcome = { status: "error", error: terminalOutcome.error ?? waitError };
358+
break;
359+
case "success":
360+
outcome = { status: "ok" };
361+
break;
362+
}
360363
}
361364
next.outcome = outcome ? withSubagentOutcomeTiming(outcome, next) : undefined;
362365
return next;

‎src/agents/subagents/registry/subagent-registry-run-wait.ts‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ import { callGateway } from "../../../gateway/call.js";
66
import { isFastTestRuntimeEnv } from "../../../infra/env.js";
77
import { createSubsystemLogger } from "../../../logging/subsystem.js";
88
import type { DetachedTaskFindResult } from "../../../tasks/detached-task-runtime-contract.js";
9-
import { buildAgentRunTerminalOutcomeFromWaitResult } from "../../agent-run-terminal-outcome.js";
9+
import {
10+
buildAgentRunTerminalOutcomeFromWaitResult,
11+
classifyAgentRunTerminalOutcome,
12+
} from "../../agent-run-terminal-outcome.js";
1013
import { isRecoverableAgentWaitError, waitForAgentRun } from "../../run-wait.js";
1114
import {
1215
type SubagentRunOutcome,
@@ -289,9 +292,8 @@ export class SubagentWaitManager {
289292
const waitTerminalOutcome = buildAgentRunTerminalOutcomeFromWaitResult(wait);
290293
const waitBlocked = waitTerminalOutcome?.reason === "blocked";
291294
const waitAborted =
292-
waitTerminalOutcome?.reason === "aborted" ||
293-
waitTerminalOutcome?.reason === "cancelled" ||
294-
waitTerminalOutcome?.reason === "superseded";
295+
waitTerminalOutcome !== undefined &&
296+
classifyAgentRunTerminalOutcome(waitTerminalOutcome) === "cancellation";
295297
const waitStatus = waitTerminalOutcome?.status ?? wait.status;
296298
if (wait.yielded === true && waitStatus !== "timeout" && !waitBlocked) {
297299
this.options.clearPendingLifecycleError(runId);

‎src/gateway/server-chat.ts‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -811,12 +811,9 @@ export function createAgentEventHandler({
811811
))
812812
) {
813813
if (!isAborted) {
814+
// peek() (chatLink) and this shift() run in one synchronous frame, so
815+
// the shifted head is exactly the peeked entry.
814816
const finished = chatLink ? chatRunState.registry.shift(evt.runId) : undefined;
815-
if (chatLink && !finished) {
816-
clearRunContextForEvent(evt);
817-
return;
818-
}
819-
820817
const terminalSessionKey = finished?.sessionKey ?? sessionKey;
821818
const terminalRunId = finished?.clientRunId ?? eventRunId;
822819
const terminalAgentId = finished?.agentId ?? sessionAgentId;

0 commit comments

Comments
 (0)