From 7168c2d904a1af12674b6c2000fcb1acdd015833 Mon Sep 17 00:00:00 2001 From: Jeremy Gollehon Date: Wed, 14 Jan 2026 23:51:19 -0800 Subject: [PATCH] fix(background-agent): prevent stale entries in pending notifications Update BackgroundManager to track batched notifications only for running tasks. Implement cleanup for completed or cancelled tasks to avoid stale entries in pending notifications. Enhance logging to include task status for better debugging. --- src/features/background-agent/manager.ts | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/features/background-agent/manager.ts b/src/features/background-agent/manager.ts index e1d6b8ff..f166b476 100644 --- a/src/features/background-agent/manager.ts +++ b/src/features/background-agent/manager.ts @@ -275,12 +275,18 @@ export class BackgroundManager { subagentSessions.add(existingTask.sessionID) this.startPolling() - // Track for batched notifications (external tasks need tracking too) - const pending = this.pendingByParent.get(input.parentSessionID) ?? new Set() - pending.add(existingTask.id) - this.pendingByParent.set(input.parentSessionID, pending) + // Track for batched notifications only if task is still running + // Don't add stale entries for completed tasks + if (existingTask.status === "running") { + const pending = this.pendingByParent.get(input.parentSessionID) ?? new Set() + pending.add(existingTask.id) + this.pendingByParent.set(input.parentSessionID, pending) + } else { + // Don't re-add completed/cancelled tasks; clean any stale entry + this.cleanupPendingByParent(existingTask) + } - log("[background-agent] External task already registered:", { taskId: existingTask.id, sessionID: existingTask.sessionID }) + log("[background-agent] External task already registered:", { taskId: existingTask.id, sessionID: existingTask.sessionID, status: existingTask.status }) return existingTask }