From 48042c387556af4f9343f99432eb69214ec29f48 Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Sun, 12 Apr 2026 13:06:12 +0100 Subject: [PATCH] fix(agents): avoid duplicate subagent ended hook loads --- src/agents/subagent-registry-lifecycle.ts | 8 ++++++-- src/agents/subagent-registry.ts | 3 +++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/agents/subagent-registry-lifecycle.ts b/src/agents/subagent-registry-lifecycle.ts index 2ac419f7664..73a7bb194e8 100644 --- a/src/agents/subagent-registry-lifecycle.ts +++ b/src/agents/subagent-registry-lifecycle.ts @@ -265,14 +265,16 @@ export function createSubagentRegistryLifecycleController(params: { await safeRemoveAttachmentsDir(giveUpParams.entry); } const completionReason = resolveCleanupCompletionReason(giveUpParams.entry); - await emitCompletionEndedHookIfNeeded(giveUpParams.entry, completionReason); logAnnounceGiveUp(giveUpParams.entry, giveUpParams.reason); + // Retry-limit / expiry give-up should not leave cleanup stuck behind the + // best-effort ended hook. Mark the run cleaned first, then fire the hook. completeCleanupBookkeeping({ runId: giveUpParams.runId, entry: giveUpParams.entry, cleanup: giveUpParams.entry.cleanup, completedAt: Date.now(), }); + await emitCompletionEndedHookIfNeeded(giveUpParams.entry, completionReason); }; const beginSubagentCleanup = (runId: string) => { @@ -445,14 +447,16 @@ export function createSubagentRegistryLifecycleController(params: { await safeRemoveAttachmentsDir(entry); } const completionReason = resolveCleanupCompletionReason(entry); - await emitCompletionEndedHookIfNeeded(entry, completionReason); logAnnounceGiveUp(entry, deferredDecision.reason); + // Giving up on announce delivery is terminal for cleanup even if the + // best-effort hook is still resolving. completeCleanupBookkeeping({ runId, entry, cleanup, completedAt: now, }); + await emitCompletionEndedHookIfNeeded(entry, completionReason); return; } diff --git a/src/agents/subagent-registry.ts b/src/agents/subagent-registry.ts index cecd7e5895f..4fe924f9c9a 100644 --- a/src/agents/subagent-registry.ts +++ b/src/agents/subagent-registry.ts @@ -324,6 +324,9 @@ async function emitSubagentEndedHookForRun(params: { sendFarewell?: boolean; accountId?: string; }) { + if (params.entry.endedHookEmittedAt) { + return; + } const cfg = subagentRegistryDeps.loadConfig(); await ensureSubagentRegistryPluginRuntimeLoaded({ config: cfg,