Skip to content

The agent that grows with you

fix(desktop): release naturally closed Linux notifications

BearHuddlestonab4455d9656f
diff --git a/apps/desktop/electron/notification-ipc.ts b/apps/desktop/electron/notification-ipc.ts
index 8619e8ff8ba..a06dc77b61f 100644
--- a/apps/desktop/electron/notification-ipc.ts
+++ b/apps/desktop/electron/notification-ipc.ts
@@ -15,8 +15,8 @@ export function registerNativeNotifications({ getMainWindow, focusWindow }: Noti
const dedupeIntervalMs = 1000
const isDuplicateNotification = createEventDeduper(dedupeIntervalMs)
const deliveries = new Map<string, Promise<boolean>>()
- const notifications = createNotificationRegistry()
const linux = process.platform === 'linux' ? createLinuxNotifications() : undefined
+ const notifications = createNotificationRegistry({ releaseOnClose: Boolean(linux) })
ipcMain.handle('hermes:notify', async (event, payload: HermesNotification) => {
// The source renderer owns runtime bindings and plugin callbacks.
diff --git a/apps/desktop/electron/notification-linux.test.ts b/apps/desktop/electron/notification-linux.test.ts
index 162f4b65e29..ba8b0b4384d 100644
--- a/apps/desktop/electron/notification-linux.test.ts
+++ b/apps/desktop/electron/notification-linux.test.ts
@@ -239,6 +239,31 @@ it.skipIf(process.platform !== 'linux')(
}
)
+it.skipIf(process.platform !== 'linux')(
+ 'releases naturally closed notifications while retaining other click targets',
+ async () => {
+ const h = setup(true)
+ expect(await h.notify({ tag: 'closed', focusSessionId: 'closed-session' })).toBe(true)
+ const closedId = h.lastId()
+ expect(await h.notify({ tag: 'active', focusSessionId: 'active-session' })).toBe(true)
+ const activeId = h.lastId()
+ await vi.advanceTimersByTimeAsync(1100)
+ const timersBeforeClose = vi.getTimerCount()
+
+ h.signal('NotificationClosed', [closedId, 2], ':1.666')
+ expect(vi.getTimerCount()).toBe(timersBeforeClose)
+ h.signal('NotificationClosed', [closedId, 2])
+ expect(vi.getTimerCount()).toBe(timersBeforeClose - 1)
+ h.signal('ActionInvoked', [closedId, 'default'])
+ expect(h.source.webContents.send).not.toHaveBeenCalled()
+
+ h.signal('ActionInvoked', [activeId, 'default'])
+ expect(h.source.webContents.send).toHaveBeenCalledWith('hermes:focus-session', 'active-session')
+ expect(vi.getTimerCount()).toBe(0)
+ h.connection.emit('close')
+ }
+)
+
it.skipIf(process.platform !== 'linux')(
'preserves activation, dedupe and source ownership while fencing daemon ID reuse',
async () => {
diff --git a/apps/desktop/electron/notification-registry.test.ts b/apps/desktop/electron/notification-registry.test.ts
index def84922289..9478701d2a1 100644
--- a/apps/desktop/electron/notification-registry.test.ts
+++ b/apps/desktop/electron/notification-registry.test.ts
@@ -25,6 +25,30 @@ it('retains a dismissed banner until its later click or action is consumed', ()
expect(vi.getTimerCount()).toBe(0)
})
+it('releases terminal closes immediately when opted in without expiring unrelated notifications early', () => {
+ vi.useFakeTimers()
+ const registry = createNotificationRegistry({ ttlMs: 1000, releaseOnClose: true })
+ const closed = Object.assign(new EventEmitter(), { close: vi.fn() })
+ const active = Object.assign(new EventEmitter(), { close: vi.fn() })
+ registry.retain(closed)
+ registry.retain(active)
+ const timersBeforeClose = vi.getTimerCount()
+
+ closed.emit('close')
+ expect(registry.has(closed)).toBe(false)
+ expect(registry.has(active)).toBe(true)
+ expect(vi.getTimerCount()).toBe(timersBeforeClose - 1)
+ expect(closed.eventNames()).toEqual([])
+
+ closed.emit('close')
+ vi.advanceTimersByTime(1000)
+ expect(closed.close).not.toHaveBeenCalled()
+ expect(active.close).toHaveBeenCalledOnce()
+ expect(registry.has(active)).toBe(false)
+ expect(active.eventNames()).toEqual([])
+ 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 })
diff --git a/apps/desktop/electron/notification-registry.ts b/apps/desktop/electron/notification-registry.ts
index c30703a73cb..3905cbf3e41 100644
--- a/apps/desktop/electron/notification-registry.ts
+++ b/apps/desktop/electron/notification-registry.ts
@@ -5,12 +5,13 @@ interface RetainedNotification extends EventEmitter {
}
// Windows emits close on banner timeout even while Action Center keeps the
-// notification clickable. Only consumption or failed delivery releases it.
+// notification clickable. Linux's terminal NotificationClosed can opt into release.
const RELEASE_EVENTS = ['click', 'action', 'failed']
const NOTIFICATION_RETENTION_TTL_MS = 10 * 60 * 1000
-export function createNotificationRegistry({ ttlMs = NOTIFICATION_RETENTION_TTL_MS } = {}) {
+export function createNotificationRegistry({ ttlMs = NOTIFICATION_RETENTION_TTL_MS, releaseOnClose = false } = {}) {
const live = new Set<RetainedNotification>()
+ const releaseEvents = releaseOnClose ? [...RELEASE_EVENTS, 'close'] : RELEASE_EVENTS
function retain(notification: RetainedNotification): void {
live.add(notification)
@@ -19,12 +20,12 @@ export function createNotificationRegistry({ ttlMs = NOTIFICATION_RETENTION_TTL_
clearTimeout(timer)
live.delete(notification)
- for (const event of RELEASE_EVENTS) {
+ for (const event of releaseEvents) {
notification.removeListener(event, release)
}
}
- for (const event of RELEASE_EVENTS) {
+ for (const event of releaseEvents) {
notification.on(event, release)
}