Skip to content

Commit 611b3f1

Browse files
authored
fix(agents): preserve authoritative child completion results (#127739)
* fix(agents): preserve authoritative child completion results * fix(agents): decouple child completion result types
1 parent f048a43 commit 611b3f1

7 files changed

Lines changed: 256 additions & 106 deletions

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

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -578,13 +578,13 @@ describe("buildChildCompletionFindings", () => {
578578
expect(findings).toContain("ANNOUNCE_SKIP");
579579
});
580580

581-
it("uses frozen child completion text when normalized completion is absent", () => {
581+
it("uses the canonical captured child completion text", () => {
582582
const findings = buildChildCompletionFindings([
583583
{
584584
childSessionKey: "agent:main:subagent:child",
585585
task: "child task",
586586
createdAt: 1,
587-
frozenResultText: "final child output",
587+
completion: { resultText: "final child output" },
588588
execution: { outcome: { status: "ok" } },
589589
},
590590
]);
@@ -625,6 +625,72 @@ describe("buildChildCompletionFindings", () => {
625625
expect(findings).not.toContain("NO_REPLY");
626626
});
627627

628+
it.each([
629+
{
630+
name: "visible",
631+
terminalReply: { disposition: "visible", text: "authoritative final output" } as const,
632+
resultText: "older captured output",
633+
expected: "authoritative final output",
634+
},
635+
{
636+
name: "silent",
637+
terminalReply: { disposition: "silent" } as const,
638+
resultText: "NO_REPLY",
639+
expected: undefined,
640+
},
641+
{
642+
name: "empty",
643+
terminalReply: { disposition: "empty" } as const,
644+
resultText: null,
645+
expected: undefined,
646+
},
647+
])(
648+
"keeps producer-owned $name terminal evidence authoritative over older fallback",
649+
({ terminalReply, resultText, expected }) => {
650+
const findings = buildChildCompletionFindings([
651+
{
652+
childSessionKey: "agent:main:subagent:child",
653+
task: "child task",
654+
createdAt: 1,
655+
completion: {
656+
required: true,
657+
resultText,
658+
fallbackResultText: "older captured fallback",
659+
terminalReply,
660+
},
661+
execution: { outcome: { status: "ok" } },
662+
},
663+
]);
664+
665+
if (expected === undefined) {
666+
expect(findings).toBeUndefined();
667+
} else {
668+
expect(findings).toContain(expected);
669+
expect(findings).not.toContain("older captured output");
670+
}
671+
},
672+
);
673+
674+
it.each(["silent", "empty"] as const)(
675+
"treats %s terminal evidence without retained text as an intentional non-result",
676+
(disposition) => {
677+
const findings = buildChildCompletionFindings([
678+
{
679+
childSessionKey: "agent:main:subagent:child",
680+
task: "child task",
681+
createdAt: 1,
682+
completion: {
683+
resultText: disposition === "silent" ? "NO_REPLY" : null,
684+
terminalReply: { disposition },
685+
},
686+
execution: { outcome: { status: "ok" } },
687+
},
688+
]);
689+
690+
expect(findings).toBeUndefined();
691+
},
692+
);
693+
628694
it.each(["ANNOUNCE_SKIP", "REPLY_SKIP", "HEARTBEAT_OK"])(
629695
"does not override an intentional %s completion with fallback output",
630696
(resultText) => {

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

Lines changed: 22 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@ import { formatDurationCompact } from "../../../infra/format-time/format-duratio
1313
import { buildAgentRunTerminalOutcomeFromWaitResult } from "../../agent-run-terminal-outcome.js";
1414
import { wrapPromptDataBlock } from "../../sanitize-for-prompt.js";
1515
import { extractStoredAssistantText, sanitizeTextContent } from "../../tools/chat-history-text.js";
16-
import {
17-
isAnnounceSkip,
18-
selectDeliverableSessionsReply,
19-
} from "../../tools/sessions-send-tokens.js";
16+
import { isAnnounceSkip } from "../../tools/sessions-send-tokens.js";
17+
import { resolveSubagentCompletionResultText } from "../completion/subagent-completion-result.js";
2018
import { compareSubagentRunGeneration } from "../registry/subagent-run-generation.js";
2119
import { classifySubagentTerminalOutcome } from "../subagent-terminal-outcome.js";
2220
import {
@@ -441,11 +439,7 @@ type ChildCompletionRow = {
441439
label?: string;
442440
createdAt: number;
443441
execution: ChildCompletionExecution;
444-
frozenResultText?: string | null;
445-
completion?: {
446-
resultText?: string | null;
447-
fallbackResultText?: string | null;
448-
};
442+
completion?: Parameters<typeof resolveSubagentCompletionResultText>[0]["completion"];
449443
};
450444

451445
type ChildCompletionSection = {
@@ -454,21 +448,12 @@ type ChildCompletionSection = {
454448
actionable: boolean;
455449
};
456450

457-
function selectChildCompletionResultText(child: ChildCompletionRow): string | undefined {
458-
const primary = child.completion?.resultText;
459-
const fallback = child.completion?.fallbackResultText ?? child.frozenResultText;
460-
if (child.execution.outcome?.status === "ok") {
461-
return selectDeliverableSessionsReply(primary, fallback);
462-
}
463-
return (primary ?? fallback)?.trim() || undefined;
464-
}
465-
466451
function hasCapturedChildCompletionReply(child: ChildCompletionRow): boolean {
467-
return [
468-
child.completion?.resultText,
469-
child.completion?.fallbackResultText,
470-
child.frozenResultText,
471-
].some((value) => Boolean(value?.trim()));
452+
return Boolean(
453+
child.completion?.terminalReply ||
454+
child.completion?.resultText?.trim() ||
455+
child.completion?.fallbackResultText?.trim(),
456+
);
472457
}
473458

474459
export function buildChildCompletionFindings(
@@ -496,7 +481,7 @@ export function buildChildCompletionFindings(
496481

497482
const sections: ChildCompletionSection[] = [];
498483
for (const [index, child] of sorted.entries()) {
499-
const resultText = selectChildCompletionResultText(child);
484+
const resultText = resolveSubagentCompletionResultText(child);
500485
const outcome = describeSubagentOutcome(child.execution.outcome);
501486
if (
502487
child.execution.outcome?.status === "ok" &&
@@ -569,20 +554,12 @@ export function buildChildCompletionFindings(
569554
}
570555

571556
export function dedupeLatestChildCompletionRows(
572-
children: Array<{
573-
runId: string;
574-
childSessionKey: string;
575-
task: string;
576-
label?: string;
577-
generation?: number;
578-
createdAt: number;
579-
execution: ChildCompletionExecution;
580-
frozenResultText?: string | null;
581-
completion?: {
582-
resultText?: string | null;
583-
fallbackResultText?: string | null;
584-
};
585-
}>,
557+
children: Array<
558+
ChildCompletionRow & {
559+
runId: string;
560+
generation?: number;
561+
}
562+
>,
586563
) {
587564
const latestByChildSessionKey = new Map<string, (typeof children)[number]>();
588565
for (const child of children) {
@@ -595,21 +572,13 @@ export function dedupeLatestChildCompletionRows(
595572
}
596573

597574
export function filterCurrentDirectChildCompletionRows(
598-
children: Array<{
599-
runId: string;
600-
childSessionKey: string;
601-
requesterSessionKey: string;
602-
requesterAgentId?: string;
603-
task: string;
604-
label?: string;
605-
createdAt: number;
606-
execution: ChildCompletionExecution;
607-
frozenResultText?: string | null;
608-
completion?: {
609-
resultText?: string | null;
610-
fallbackResultText?: string | null;
611-
};
612-
}>,
575+
children: Array<
576+
ChildCompletionRow & {
577+
runId: string;
578+
requesterSessionKey: string;
579+
requesterAgentId?: string;
580+
}
581+
>,
613582
params: {
614583
requesterSessionKey: string;
615584
requesterAgentId?: string;

0 commit comments

Comments
 (0)