diff --git a/src/features/background-agent/manager.test.ts b/src/features/background-agent/manager.test.ts
index 2e78f63f3..273ae7bd3 100644
--- a/src/features/background-agent/manager.test.ts
+++ b/src/features/background-agent/manager.test.ts
@@ -2987,6 +2987,28 @@ describe("BackgroundManager.handleEvent - session.deleted cascade", () => {
manager.shutdown()
resetToastManager()
})
+
+ test("should clean pending notifications for deleted sessions", () => {
+ //#given
+ const manager = createBackgroundManager()
+ const sessionID = "session-pending-notifications"
+
+ manager.queuePendingNotification(sessionID, "queued")
+ expect(getPendingNotifications(manager).get(sessionID)).toEqual([
+ "queued",
+ ])
+
+ //#when
+ manager.handleEvent({
+ type: "session.deleted",
+ properties: { info: { id: sessionID } },
+ })
+
+ //#then
+ expect(getPendingNotifications(manager).has(sessionID)).toBe(false)
+
+ manager.shutdown()
+ })
})
describe("BackgroundManager.handleEvent - session.error", () => {
diff --git a/src/features/background-agent/manager.ts b/src/features/background-agent/manager.ts
index 1bc9e2b4b..247e9f948 100644
--- a/src/features/background-agent/manager.ts
+++ b/src/features/background-agent/manager.ts
@@ -830,6 +830,8 @@ export class BackgroundManager {
tasksToCancel.set(descendant.id, descendant)
}
+ this.pendingNotifications.delete(sessionID)
+
if (tasksToCancel.size === 0) return
for (const task of tasksToCancel.values()) {
@@ -866,6 +868,13 @@ export class BackgroundManager {
subagentSessions.delete(task.sessionID)
}
}
+
+ for (const task of tasksToCancel.values()) {
+ if (task.parentSessionID) {
+ this.pendingNotifications.delete(task.parentSessionID)
+ }
+ }
+
SessionCategoryRegistry.remove(sessionID)
}