diff --git a/src/agents/subagent-registry.steer-restart.test.ts b/src/agents/subagent-registry.steer-restart.test.ts index 3bb410278ef..e6e8ad78f5e 100644 --- a/src/agents/subagent-registry.steer-restart.test.ts +++ b/src/agents/subagent-registry.steer-restart.test.ts @@ -564,6 +564,35 @@ describe("subagent registry steer restarts", () => { ); }); + it("treats a child session as inactive when only a stale older row is still unended", async () => { + const childSessionKey = "agent:main:subagent:stale-active-older-row"; + + mod.addSubagentRunForTests({ + runId: "run-stale-older", + childSessionKey, + requesterSessionKey: MAIN_REQUESTER_SESSION_KEY, + requesterDisplayKey: MAIN_REQUESTER_DISPLAY_KEY, + task: "older stale row", + startedAt: 100, + createdAt: 100, + cleanup: "keep", + }); + mod.addSubagentRunForTests({ + runId: "run-current-ended", + childSessionKey, + requesterSessionKey: MAIN_REQUESTER_SESSION_KEY, + requesterDisplayKey: MAIN_REQUESTER_DISPLAY_KEY, + task: "current ended row", + startedAt: 200, + createdAt: 200, + endedAt: 250, + outcome: { status: "ok" }, + cleanup: "keep", + }); + + expect(mod.isSubagentSessionRunActive(childSessionKey)).toBe(false); + }); + it("recovers announce cleanup when completion arrives after a kill marker", async () => { const childSessionKey = "agent:main:subagent:kill-race"; registerRun({ diff --git a/src/agents/subagent-registry.ts b/src/agents/subagent-registry.ts index 407beec9d71..fdde8cc5fea 100644 --- a/src/agents/subagent-registry.ts +++ b/src/agents/subagent-registry.ts @@ -1596,16 +1596,17 @@ export function resolveRequesterForChildSession(childSessionKey: string): { export function isSubagentSessionRunActive(childSessionKey: string): boolean { const runIds = findRunIdsByChildSessionKey(childSessionKey); + let latest: SubagentRunRecord | undefined; for (const runId of runIds) { const entry = subagentRuns.get(runId); if (!entry) { continue; } - if (typeof entry.endedAt !== "number") { - return true; + if (!latest || entry.createdAt > latest.createdAt) { + latest = entry; } } - return false; + return Boolean(latest && typeof latest.endedAt !== "number"); } export function shouldIgnorePostCompletionAnnounceForSession(childSessionKey: string): boolean {