Skip to content

Commit b1d9113

Browse files
committed
fix(agents): scope cancellation precedence to subagents
1 parent 99d1c51 commit b1d9113

6 files changed

Lines changed: 50 additions & 57 deletions

‎src/agents/agent-run-terminal-outcome.test.ts‎

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,12 @@ describe("agent run terminal outcome", () => {
2929
});
3030

3131
it("normalizes lifecycle signals with timeout, cancellation, failure precedence", () => {
32-
const bareAbort = { phase: "end", data: { aborted: true } } as const;
33-
expect(buildAgentRunTerminalOutcomeFromLifecycleEvent(bareAbort)).toMatchObject({
34-
reason: "aborted",
35-
status: "error",
36-
stopReason: "aborted",
37-
});
38-
const explicitAbort = {
39-
phase: "end",
40-
data: { aborted: true, stopReason: "aborted", livenessState: "blocked" },
41-
} as const;
42-
expect(buildAgentRunTerminalOutcomeFromLifecycleEvent(explicitAbort)).toMatchObject({
43-
reason: "aborted",
44-
status: "error",
45-
stopReason: "aborted",
46-
});
32+
expect(
33+
buildAgentRunTerminalOutcomeFromLifecycleEvent({
34+
phase: "end",
35+
data: { aborted: true },
36+
}),
37+
).toMatchObject({ reason: "aborted", status: "error", stopReason: "aborted" });
4738
expect(
4839
buildAgentRunTerminalOutcomeFromLifecycleEvent({
4940
phase: "end",
@@ -288,23 +279,20 @@ describe("agent run terminal outcome", () => {
288279
});
289280
});
290281

291-
it.each(["blocked", "abandoned"])(
292-
"keeps explicit cancellation ahead of %s liveness",
293-
(livenessState) => {
294-
expect(
295-
buildAgentRunTerminalOutcome({
296-
status: "error",
297-
stopReason: "stop",
298-
livenessState,
299-
}),
300-
).toEqual({
301-
reason: "cancelled",
282+
it("keeps explicit cancellation ahead of abandoned liveness", () => {
283+
expect(
284+
buildAgentRunTerminalOutcome({
302285
status: "error",
303286
stopReason: "stop",
304-
livenessState,
305-
});
306-
},
307-
);
287+
livenessState: "abandoned",
288+
}),
289+
).toEqual({
290+
reason: "cancelled",
291+
status: "error",
292+
stopReason: "stop",
293+
livenessState: "abandoned",
294+
});
295+
});
308296

309297
it("keeps a hard timeout over later aborts or failures for the same run", () => {
310298
const timeout = buildAgentRunTerminalOutcome({

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -513,25 +513,25 @@ export function buildAgentRunTerminalOutcome(
513513
const abandoned = isAbandonedLivenessState(livenessState);
514514
const error = hardTimeout
515515
? rawError
516-
: aborted && !rawError
517-
? AGENT_RUN_ABORTED_ERROR
518-
: superseded || aborted || cancelled
519-
? rawError
520-
: blocked
521-
? formatBlockedLivenessError(rawError)
516+
: blocked
517+
? formatBlockedLivenessError(rawError)
518+
: aborted && !rawError
519+
? AGENT_RUN_ABORTED_ERROR
520+
: superseded || aborted || cancelled
521+
? rawError
522522
: abandoned
523523
? formatAbandonedLivenessError(rawError)
524524
: rawError;
525525
const reason: AgentRunTerminalReason = hardTimeout
526526
? "hard_timeout"
527527
: superseded
528528
? "superseded"
529-
: aborted
530-
? "aborted"
531-
: cancelled
532-
? "cancelled"
533-
: blocked
534-
? "blocked"
529+
: blocked
530+
? "blocked"
531+
: aborted
532+
? "aborted"
533+
: cancelled
534+
? "cancelled"
535535
: abandoned
536536
? "abandoned"
537537
: input.status === "timeout"

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,15 @@ 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 {
14-
buildAgentRunTerminalOutcomeFromWaitResult,
15-
classifyAgentRunTerminalOutcome,
16-
} from "../../agent-run-terminal-outcome.js";
13+
import { buildAgentRunTerminalOutcomeFromWaitResult } from "../../agent-run-terminal-outcome.js";
1714
import { wrapPromptDataBlock } from "../../sanitize-for-prompt.js";
1815
import { extractStoredAssistantText, sanitizeTextContent } from "../../tools/chat-history-text.js";
1916
import {
2017
isAnnounceSkip,
2118
selectDeliverableSessionsReply,
2219
} from "../../tools/sessions-send-tokens.js";
2320
import { compareSubagentRunGeneration } from "../registry/subagent-run-generation.js";
21+
import { classifySubagentTerminalOutcome } from "../subagent-terminal-outcome.js";
2422
import {
2523
captureSubagentCompletionReplyUsing,
2624
readLatestSubagentOutputWithRetryUsing,
@@ -346,7 +344,7 @@ export function applySubagentWaitOutcome(params: {
346344
// primary normalizers, so apply the canonical classification here instead
347345
// of re-enumerating reason groups.
348346
if (terminalOutcome) {
349-
switch (classifyAgentRunTerminalOutcome(terminalOutcome)) {
347+
switch (classifySubagentTerminalOutcome(terminalOutcome)) {
350348
case "timeout":
351349
outcome = { status: "timeout" };
352350
break;

‎src/agents/subagents/registry/subagent-registry-listener.ts‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import type { AgentEventPayload } from "../../../infra/agent-events.js";
22
import { runWithGatewayIndependentRootWorkAdmission } from "../../../process/gateway-work-admission.js";
3-
import {
4-
buildAgentRunTerminalOutcomeFromLifecycleEvent,
5-
classifyAgentRunTerminalOutcome,
6-
} from "../../agent-run-terminal-outcome.js";
3+
import { buildAgentRunTerminalOutcomeFromLifecycleEvent } from "../../agent-run-terminal-outcome.js";
74
import { normalizeAgentRunTerminalReplySnapshot } from "../../agent-run-terminal-reply.js";
5+
import { classifySubagentTerminalOutcome } from "../subagent-terminal-outcome.js";
86
import {
97
SUBAGENT_ENDED_REASON_COMPLETE,
108
SUBAGENT_ENDED_REASON_ERROR,
@@ -104,7 +102,7 @@ export function createSubagentRegistryListener(config: {
104102
startedAt,
105103
endedAt,
106104
});
107-
const classification = classifyAgentRunTerminalOutcome(terminalOutcome);
105+
const classification = classifySubagentTerminalOutcome(terminalOutcome);
108106
if (
109107
classification === "cancellation" &&
110108
evt.data?.aborted === true &&

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,13 @@ 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 {
10-
buildAgentRunTerminalOutcomeFromWaitResult,
11-
classifyAgentRunTerminalOutcome,
12-
} from "../../agent-run-terminal-outcome.js";
9+
import { buildAgentRunTerminalOutcomeFromWaitResult } from "../../agent-run-terminal-outcome.js";
1310
import { isRecoverableAgentWaitError, waitForAgentRun } from "../../run-wait.js";
1411
import {
1512
type SubagentRunOutcome,
1613
withSubagentOutcomeTiming,
1714
} from "../announce/subagent-announce-output.js";
15+
import { classifySubagentTerminalOutcome } from "../subagent-terminal-outcome.js";
1816
import { clearDeliveryState, ensureCompletionState } from "./subagent-delivery-state.js";
1917
import {
2018
SUBAGENT_ENDED_REASON_COMPLETE,
@@ -293,7 +291,7 @@ export class SubagentWaitManager {
293291
const waitBlocked = waitTerminalOutcome?.reason === "blocked";
294292
const waitAborted =
295293
waitTerminalOutcome !== undefined &&
296-
classifyAgentRunTerminalOutcome(waitTerminalOutcome) === "cancellation";
294+
classifySubagentTerminalOutcome(waitTerminalOutcome) === "cancellation";
297295
const waitStatus = waitTerminalOutcome?.status ?? wait.status;
298296
if (wait.yielded === true && waitStatus !== "timeout" && !waitBlocked) {
299297
this.options.clearPendingLifecycleError(runId);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import {
2+
classifyAgentRunTerminalOutcome,
3+
type AgentRunTerminalOutcome,
4+
} from "../agent-run-terminal-outcome.js";
5+
import { isAbortedAgentStopReason } from "../run-termination.js";
6+
7+
/** Subagents apply explicit cancellation ownership before incomplete liveness projections. */
8+
export const classifySubagentTerminalOutcome = (outcome: AgentRunTerminalOutcome) =>
9+
isAbortedAgentStopReason(outcome.stopReason)
10+
? "cancellation"
11+
: classifyAgentRunTerminalOutcome(outcome);

0 commit comments

Comments
 (0)