fix(subagent): route nested announce to parent even when parent run ended

When a depth-2 subagent (Birdie) completes and its parent (Newton) is a
depth-1 subagent, the announce should go to Newton, not bypass to the
grandparent (Jaris).

Previously, isSubagentSessionRunActive(Newton) returned false because
Newton's agent turn completed after spawning Birdie. This triggered the
fallback to grandparent even though Newton's SESSION was still alive and
waiting for child results.

Now we only fallback to grandparent if the parent SESSION is actually
deleted (no sessionId in session store). If the parent session exists,
we inject into it even if the current run has ended — this starts a new
agent turn to process the child result.

Fixes #18037

Test Plan:
- Added regression test: routes to parent when run ended but session alive
- Added regression test: falls back to grandparent only when session deleted
This commit is contained in:
Operative-001
2026-02-16 13:21:19 +01:00
committed by Peter Steinberger
parent 235794d9f6
commit 6931ca7035
2 changed files with 126 additions and 12 deletions

View File

@@ -508,23 +508,39 @@ export async function runSubagentAnnounceFlow(params: {
let requesterIsSubagent = requesterDepth >= 1;
// If the requester subagent has already finished, bubble the announce to its
// requester (typically main) so descendant completion is not silently lost.
// BUT: only fallback if the parent SESSION is deleted, not just if the current
// run ended. A parent waiting for child results has no active run but should
// still receive the announce — injecting will start a new agent turn.
if (requesterIsSubagent) {
const { isSubagentSessionRunActive, resolveRequesterForChildSession } =
await import("./subagent-registry.js");
if (!isSubagentSessionRunActive(targetRequesterSessionKey)) {
const fallback = resolveRequesterForChildSession(targetRequesterSessionKey);
if (!fallback?.requesterSessionKey) {
// Without a requester fallback we cannot safely deliver this nested
// completion. Keep cleanup retryable so a later registry restore can
// recover and re-announce instead of silently dropping the result.
shouldDeleteChildSession = false;
return false;
// Parent run has ended. Check if parent SESSION still exists.
// If it does, the parent may be waiting for child results — inject there.
const parentSessionEntry = loadSessionEntryByKey(targetRequesterSessionKey);
const parentSessionAlive =
parentSessionEntry &&
typeof parentSessionEntry.sessionId === "string" &&
parentSessionEntry.sessionId.trim();
if (!parentSessionAlive) {
// Parent session is truly gone — fallback to grandparent
const fallback = resolveRequesterForChildSession(targetRequesterSessionKey);
if (!fallback?.requesterSessionKey) {
// Without a requester fallback we cannot safely deliver this nested
// completion. Keep cleanup retryable so a later registry restore can
// recover and re-announce instead of silently dropping the result.
shouldDeleteChildSession = false;
return false;
}
targetRequesterSessionKey = fallback.requesterSessionKey;
targetRequesterOrigin =
normalizeDeliveryContext(fallback.requesterOrigin) ?? targetRequesterOrigin;
requesterDepth = getSubagentDepthFromSessionStore(targetRequesterSessionKey);
requesterIsSubagent = requesterDepth >= 1;
}
targetRequesterSessionKey = fallback.requesterSessionKey;
targetRequesterOrigin =
normalizeDeliveryContext(fallback.requesterOrigin) ?? targetRequesterOrigin;
requesterDepth = getSubagentDepthFromSessionStore(targetRequesterSessionKey);
requesterIsSubagent = requesterDepth >= 1;
// If parent session is alive (just has no active run), continue with parent
// as target. Injecting the announce will start a new agent turn for processing.
}
}