diff --git a/src/agents/subagent-registry-completion.test.ts b/src/agents/subagent-registry-completion.test.ts index 54685caf88f..28b0fa98c10 100644 --- a/src/agents/subagent-registry-completion.test.ts +++ b/src/agents/subagent-registry-completion.test.ts @@ -68,6 +68,18 @@ describe("emitSubagentEndedHookOnce", () => { { status: "ok" }, ), ).toBe(true); + expect( + mod.shouldUpdateRunOutcome( + { status: "ok" }, + { status: "ok", startedAt: 1_000, endedAt: 2_000, elapsedMs: 1_000 }, + ), + ).toBe(true); + expect( + mod.shouldUpdateRunOutcome( + { status: "ok", startedAt: 1_000, endedAt: 2_000, elapsedMs: 1_000 }, + { status: "ok" }, + ), + ).toBe(false); }); it("records ended hook marker even when no subagent_ended hooks are registered", async () => { diff --git a/src/agents/subagent-registry-completion.ts b/src/agents/subagent-registry-completion.ts index 3c887e93bab..da3ad3999b9 100644 --- a/src/agents/subagent-registry-completion.ts +++ b/src/agents/subagent-registry-completion.ts @@ -42,6 +42,15 @@ export function runOutcomeHasTiming(outcome: SubagentRunOutcome | undefined): bo ); } +export function shouldUpdateRunOutcome( + current: SubagentRunOutcome | undefined, + next: SubagentRunOutcome | undefined, +): boolean { + return ( + !runOutcomesEqual(current, next) || (!runOutcomeHasTiming(current) && runOutcomeHasTiming(next)) + ); +} + export function resolveLifecycleOutcomeFromRunOutcome( outcome: SubagentRunOutcome | undefined, ): SubagentLifecycleEndedOutcome { diff --git a/src/agents/subagent-registry-lifecycle.ts b/src/agents/subagent-registry-lifecycle.ts index 2caa54831c7..6d89a3265ca 100644 --- a/src/agents/subagent-registry-lifecycle.ts +++ b/src/agents/subagent-registry-lifecycle.ts @@ -23,7 +23,7 @@ import { resolveCleanupCompletionReason, resolveDeferredCleanupDecision, } from "./subagent-registry-cleanup.js"; -import { runOutcomeHasTiming, runOutcomesEqual } from "./subagent-registry-completion.js"; +import { shouldUpdateRunOutcome } from "./subagent-registry-completion.js"; import { ANNOUNCE_COMPLETION_HARD_EXPIRY_MS, ANNOUNCE_EXPIRY_MS, @@ -594,10 +594,7 @@ export function createSubagentRegistryLifecycleController(params: { startedAt: entry.startedAt, endedAt, }); - const shouldPersistOutcome = - !runOutcomesEqual(entry.outcome, outcome) || - (!runOutcomeHasTiming(entry.outcome) && runOutcomeHasTiming(outcome)); - if (shouldPersistOutcome) { + if (shouldUpdateRunOutcome(entry.outcome, outcome)) { entry.outcome = outcome; mutated = true; }