Skip to content

The agent that grows with you

fix(desktop): retain notification handlers across banner dismissal

Brian Sweattf557829bffdd
diff --git a/apps/desktop/electron/notification-registry.test.ts b/apps/desktop/electron/notification-registry.test.ts
new file mode 100644
index 00000000000..8f23f865e51
--- /dev/null
+++ b/apps/desktop/electron/notification-registry.test.ts
@@ -0,0 +1,41 @@
+import { EventEmitter } from 'node:events'
+
+import { afterEach, expect, it, vi } from 'vitest'
+
+import { createNotificationRegistry } from './notification-registry'
+
+afterEach(() => vi.useRealTimers())
+
+it('retains a dismissed banner until its later click or action is consumed', () => {
+ vi.useFakeTimers()
+ const registry = createNotificationRegistry()
+ for (const event of ['click', 'action']) {
+ const notification = Object.assign(new EventEmitter(), { close: vi.fn() })
+ const handler = vi.fn()
+ notification.on(event, handler)
+ registry.retain(notification)
+ notification.emit('close')
+ expect(registry.has(notification)).toBe(true)
+ notification.emit(event)
+ expect(handler).toHaveBeenCalledOnce()
+ expect(registry.has(notification)).toBe(false)
+ }
+ expect(vi.getTimerCount()).toBe(0)
+})
+
+it('dismisses an expired notification before releasing it and releases failed delivery immediately', () => {
+ vi.useFakeTimers()
+ const registry = createNotificationRegistry({ ttlMs: 1000 })
+ const notification = Object.assign(new EventEmitter(), { close: vi.fn(() => expect(registry.has(notification)).toBe(true)) })
+ registry.retain(notification)
+ vi.advanceTimersByTime(1000)
+ expect(notification.close).toHaveBeenCalledOnce()
+ expect(registry.has(notification)).toBe(false)
+
+ const failed = Object.assign(new EventEmitter(), { close: vi.fn() })
+ registry.retain(failed)
+ failed.emit('failed')
+ expect(registry.has(failed)).toBe(false)
+ expect(failed.close).not.toHaveBeenCalled()
+ expect(vi.getTimerCount()).toBe(0)
+})
diff --git a/apps/desktop/electron/notification-registry.ts b/apps/desktop/electron/notification-registry.ts
new file mode 100644
index 00000000000..95a550a8ce8
--- /dev/null
+++ b/apps/desktop/electron/notification-registry.ts
@@ -0,0 +1,39 @@
+import type { EventEmitter } from 'node:events'
+
+interface RetainedNotification extends EventEmitter {
+ close: () => void
+}
+
+// Windows emits close on banner timeout even while Action Center keeps the
+// notification clickable. Only consumption or failed delivery releases it.
+const RELEASE_EVENTS = ['click', 'action', 'failed']
+const NOTIFICATION_RETENTION_TTL_MS = 10 * 60 * 1000
+
+export function createNotificationRegistry({ ttlMs = NOTIFICATION_RETENTION_TTL_MS } = {}) {
+ const live = new Set<RetainedNotification>()
+
+ function retain(notification: RetainedNotification): void {
+ live.add(notification)
+ const release = () => {
+ clearTimeout(timer)
+ live.delete(notification)
+
+ for (const event of RELEASE_EVENTS) {
+ notification.removeListener(event, release)
+ }
+ }
+
+ for (const event of RELEASE_EVENTS) {
+ notification.on(event, release)
+ }
+
+ // Bound retention without leaving an OS notification whose handler is gone.
+ const timer = setTimeout(() => {
+ notification.close()
+ release()
+ }, ttlMs)
+ timer.unref()
+ }
+
+ return { retain, has: (notification: RetainedNotification) => live.has(notification) }
+}