From 3ef230e1f72aa55b98f4ad7064a4cf3afccbc691 Mon Sep 17 00:00:00 2001 From: Jinwoo Hong <73622457+Jinwoo-H@users.noreply.github.com> Date: Thu, 4 Jun 2026 17:05:07 -0400 Subject: [PATCH] Fix persistent terminal attention for agent completions (#4658) * WIP: Changes before auto-review fixes Co-authored-by: Orca * fix: route agent terminal attention through notifications Co-authored-by: Orca --------- Co-authored-by: Orca --- .../terminal-pane/pty-connection-types.ts | 1 + .../terminal-pane/pty-connection.test.ts | 96 ++++++++++-- .../terminal-pane/pty-connection.ts | 137 ++++++++++++------ .../use-notification-dispatch.test.ts | 14 ++ .../use-notification-dispatch.ts | 5 + .../use-terminal-pane-lifecycle.ts | 1 + ...gent-hook-completion-notifications.test.ts | 25 ++++ .../agent-hook-completion-notifications.ts | 27 ++-- 8 files changed, 239 insertions(+), 67 deletions(-) diff --git a/src/renderer/src/components/terminal-pane/pty-connection-types.ts b/src/renderer/src/components/terminal-pane/pty-connection-types.ts index 162fe34c4..d532ae9f0 100644 --- a/src/renderer/src/components/terminal-pane/pty-connection-types.ts +++ b/src/renderer/src/components/terminal-pane/pty-connection-types.ts @@ -49,6 +49,7 @@ export type PtyConnectionDeps = { terminalTitle?: string paneKey?: string agentStatusSnapshot?: ParsedAgentStatusPayload + suppressOsNotification?: boolean }) => void setCacheTimerStartedAt: (key: string, ts: number | null) => void syncPanePtyLayoutBinding: (paneId: number, ptyId: string | null) => void diff --git a/src/renderer/src/components/terminal-pane/pty-connection.test.ts b/src/renderer/src/components/terminal-pane/pty-connection.test.ts index ccde2b3cf..3a4572654 100644 --- a/src/renderer/src/components/terminal-pane/pty-connection.test.ts +++ b/src/renderer/src/components/terminal-pane/pty-connection.test.ts @@ -4037,8 +4037,53 @@ describe('connectPanePty', () => { expect.objectContaining({ source: 'terminal-bell' }) ) vi.advanceTimersByTime(AGENT_TASK_COMPLETE_NOTIFICATION_MAX_WAIT_MS) - expect(deps.dispatchNotification).not.toHaveBeenCalledWith( - expect.objectContaining({ source: 'agent-task-complete' }) + expect(deps.dispatchNotification).toHaveBeenCalledWith( + expect.objectContaining({ + source: 'agent-task-complete', + suppressOsNotification: true + }) + ) + }) + + it('raises terminal attention for agent completion when OS completion notifications are disabled', async () => { + const { connectPanePty } = await import('./pty-connection') + const transport = createMockTransport('pty-codex') + transportFactoryQueue.push(transport) + + vi.useFakeTimers() + mockStoreState.settings = { + ...mockStoreState.settings, + experimentalTerminalAttention: true, + notifications: { + enabled: true, + agentTaskComplete: false, + terminalBell: true, + suppressWhenFocused: false, + customSoundPath: null + } + } + const pane = createPane(1) + const manager = createManager(1) + const deps = createDeps() + + connectPanePty(pane as never, manager as never, deps as never) + + const idleHandler = createdTransportOptions[0]?.onAgentBecameIdle as + | ((title: string) => void) + | undefined + if (!idleHandler) { + throw new Error('Expected idle handler to be registered') + } + + idleHandler('* Codex done') + vi.advanceTimersByTime(AGENT_TASK_COMPLETE_NOTIFICATION_MAX_WAIT_MS) + + expect(deps.dispatchNotification).toHaveBeenCalledWith( + expect.objectContaining({ + source: 'agent-task-complete', + paneKey: makePaneKey('tab-1', LEAF_1), + suppressOsNotification: true + }) ) }) @@ -4809,11 +4854,36 @@ describe('connectPanePty', () => { }) }) - // Why: show-until-interact — a real keystroke through xterm onData is the - // canonical "user is here" signal that dismisses the bell. Guarded by the - // replay and codex-stale checks (see separate tests) so synthetic xterm - // auto-replies and blocked stale input never count as interaction. - it('clears tab and worktree unread on real keystroke via onData', async () => { + // Why: show-until-interact — a DOM keydown is the keyboard "user is here" + // signal that dismisses attention. Raw xterm onData is intentionally lower + // level because it can include terminal-generated replies/control bytes. + it('clears tab and worktree unread on real keydown', async () => { + const { connectPanePty } = await import('./pty-connection') + const transport = createMockTransport() + transportFactoryQueue.push(transport) + + const pane = createPane(1) + pane.terminal.element = createPaneContainer() + const manager = createManager(1) + const deps = createDeps() + + connectPanePty(pane as never, manager as never, deps as never) + + const keydown = new Event('keydown') + Object.defineProperty(keydown, 'key', { value: 'a' }) + Object.defineProperty(keydown, 'repeat', { value: false }) + Object.defineProperty(keydown, 'ctrlKey', { value: false }) + Object.defineProperty(keydown, 'metaKey', { value: false }) + Object.defineProperty(keydown, 'shiftKey', { value: false }) + ;(pane.terminal.element as EventTarget).dispatchEvent(keydown) + + expect(deps.clearTerminalTabUnread).toHaveBeenCalledWith('tab-1') + expect(deps.clearTerminalPaneUnread).toHaveBeenCalledWith(makePaneKey('tab-1', LEAF_1)) + expect(deps.clearWorktreeUnread).toHaveBeenCalledWith('wt-1') + expect(transport.sendInput).not.toHaveBeenCalled() + }) + + it('does not clear pane attention from raw onData after a bell', async () => { const { connectPanePty } = await import('./pty-connection') const transport = createMockTransport() transportFactoryQueue.push(transport) @@ -4829,14 +4899,16 @@ describe('connectPanePty', () => { connectPanePty(pane as never, manager as never, deps as never) - if (!onDataHandler) { - throw new Error('expected onData handler to be registered') + const bellHandler = createdTransportOptions[0]?.onBell as (() => void) | undefined + if (!bellHandler || !onDataHandler) { + throw new Error('expected bell and onData handlers to be registered') } + + bellHandler() ;(onDataHandler as (data: string) => void)('a') - expect(deps.clearTerminalTabUnread).toHaveBeenCalledWith('tab-1') - expect(deps.clearTerminalPaneUnread).toHaveBeenCalledWith(makePaneKey('tab-1', LEAF_1)) - expect(deps.clearWorktreeUnread).toHaveBeenCalledWith('wt-1') + expect(deps.markTerminalPaneUnread).toHaveBeenCalledWith(makePaneKey('tab-1', LEAF_1)) + expect(deps.clearTerminalPaneUnread).not.toHaveBeenCalled() expect(transport.sendInput).toHaveBeenCalledWith('a') }) diff --git a/src/renderer/src/components/terminal-pane/pty-connection.ts b/src/renderer/src/components/terminal-pane/pty-connection.ts index f3522892b..9b8a62ed9 100644 --- a/src/renderer/src/components/terminal-pane/pty-connection.ts +++ b/src/renderer/src/components/terminal-pane/pty-connection.ts @@ -199,35 +199,66 @@ function isAgentTaskCompleteNotificationEnabledFromState( return notifications?.enabled !== false && notifications?.agentTaskComplete !== false } -const agentTaskCompleteNotificationEnabledListeners = new Set<() => void>() -let agentTaskCompleteNotificationSettingsUnsubscribe: (() => void) | null = null -let agentTaskCompleteNotificationEnabledSnapshot: boolean | null = null +function isTerminalAttentionEnabledFromState( + state: ReturnType +): boolean { + return state.settings?.experimentalTerminalAttention === true +} -function subscribeAgentTaskCompleteNotificationEnabled(listener: () => void): () => void { - if (agentTaskCompleteNotificationSettingsUnsubscribe === null) { - agentTaskCompleteNotificationEnabledSnapshot = isAgentTaskCompleteNotificationEnabled() - agentTaskCompleteNotificationSettingsUnsubscribe = useAppStore.subscribe((state) => { - const enabled = isAgentTaskCompleteNotificationEnabledFromState(state) - if (enabled === agentTaskCompleteNotificationEnabledSnapshot) { +function isAgentTaskCompleteTrackingEnabled(): boolean { + const state = useAppStore.getState() + return ( + isAgentTaskCompleteNotificationEnabledFromState(state) || + isTerminalAttentionEnabledFromState(state) + ) +} + +function isAgentTaskCompleteTrackingEnabledFromState( + state: ReturnType +): boolean { + return ( + isAgentTaskCompleteNotificationEnabledFromState(state) || + isTerminalAttentionEnabledFromState(state) + ) +} + +const agentTaskCompleteTrackingEnabledListeners = new Set<() => void>() +let agentTaskCompleteTrackingSettingsUnsubscribe: (() => void) | null = null +let agentTaskCompleteTrackingSettingsSnapshot: string | null = null + +function getAgentTaskCompleteTrackingSettingsSnapshot( + state: ReturnType +): string { + return `${isAgentTaskCompleteTrackingEnabledFromState(state)}:${isAgentTaskCompleteNotificationEnabledFromState(state)}` +} + +function subscribeAgentTaskCompleteTrackingEnabled(listener: () => void): () => void { + if (agentTaskCompleteTrackingSettingsUnsubscribe === null) { + agentTaskCompleteTrackingSettingsSnapshot = getAgentTaskCompleteTrackingSettingsSnapshot( + useAppStore.getState() + ) + agentTaskCompleteTrackingSettingsUnsubscribe = useAppStore.subscribe((state) => { + const snapshot = getAgentTaskCompleteTrackingSettingsSnapshot(state) + if (snapshot === agentTaskCompleteTrackingSettingsSnapshot) { return } - agentTaskCompleteNotificationEnabledSnapshot = enabled - for (const subscriber of Array.from(agentTaskCompleteNotificationEnabledListeners)) { + agentTaskCompleteTrackingSettingsSnapshot = snapshot + for (const subscriber of Array.from(agentTaskCompleteTrackingEnabledListeners)) { subscriber() } }) } - agentTaskCompleteNotificationEnabledListeners.add(listener) + agentTaskCompleteTrackingEnabledListeners.add(listener) return () => { - agentTaskCompleteNotificationEnabledListeners.delete(listener) + agentTaskCompleteTrackingEnabledListeners.delete(listener) if ( - agentTaskCompleteNotificationEnabledListeners.size === 0 && - agentTaskCompleteNotificationSettingsUnsubscribe !== null + agentTaskCompleteTrackingEnabledListeners.size === 0 && + agentTaskCompleteTrackingSettingsUnsubscribe !== null ) { - agentTaskCompleteNotificationSettingsUnsubscribe() - agentTaskCompleteNotificationSettingsUnsubscribe = null - agentTaskCompleteNotificationEnabledSnapshot = null + agentTaskCompleteTrackingSettingsUnsubscribe() + agentTaskCompleteTrackingSettingsUnsubscribe = null + agentTaskCompleteTrackingSettingsSnapshot = null } } } @@ -403,7 +434,8 @@ export function connectPanePty( let agentTaskCompleteStatusUnsubscribe: (() => void) | null = null let agentTaskCompleteSettingsUnsubscribe: (() => void) | null = null let agentTaskCompleteNotificationGeneration = 0 - let wasAgentTaskCompleteNotificationEnabled = isAgentTaskCompleteNotificationEnabled() + let wasAgentTaskCompleteTrackingEnabled = isAgentTaskCompleteTrackingEnabled() + let wasAgentTaskCompleteOsNotificationEnabled = isAgentTaskCompleteNotificationEnabled() let terminalBellNotificationTimer: ReturnType | null = null let pendingTerminalBellNotification = false let reattachIdleAgentCursorResetTimer: ReturnType | null = null @@ -687,6 +719,9 @@ export function connectPanePty( }) commandLifecycle.attachXtermConsumer(pane.terminal) const onTerminalKeyDown = (event: KeyboardEvent): void => { + deps.clearTerminalTabUnread(deps.tabId) + deps.clearTerminalPaneUnread(cacheKey) + deps.clearWorktreeUnread(deps.worktreeId) if (isPlainEscapeKeyEvent(event)) { setPendingTerminalInputIntent('plain-escape') return @@ -731,7 +766,7 @@ export function connectPanePty( ...(meta?.agentStatus ? { agentStatusSnapshot: meta.agentStatus } : {}) }), shouldPollProcessCadence: () => - isAgentTaskCompleteNotificationEnabled() && deps.isVisibleRef.current, + isAgentTaskCompleteTrackingEnabled() && deps.isVisibleRef.current, isLive: () => { if (disposed) { return false @@ -788,7 +823,7 @@ export function connectPanePty( const onTitleChange = (title: string, rawTitle: string): void => { manager.setPaneGpuRendering(pane.id, !isGeminiTerminalTitle(rawTitle)) deps.setRuntimePaneTitle(deps.tabId, pane.id, title) - if (syncAgentTaskCompleteNotificationEnabled()) { + if (syncAgentTaskCompleteTrackingEnabled()) { agentCompletionCoordinator.observeTitle(rawTitle) } // Why: only the focused pane should drive the tab title — otherwise two @@ -997,24 +1032,32 @@ export function connectPanePty( } } - const syncAgentTaskCompleteNotificationEnabled = (): boolean => { - const enabled = isAgentTaskCompleteNotificationEnabled() - if (!enabled && wasAgentTaskCompleteNotificationEnabled) { - // Why: disabling notifications is an event-time boundary. Drop pending - // timers and coordinator state so completions observed while off cannot - // replay if the user turns the setting back on. + const syncAgentTaskCompleteTrackingEnabled = (): boolean => { + const enabled = isAgentTaskCompleteTrackingEnabled() + const osNotificationsEnabled = isAgentTaskCompleteNotificationEnabled() + if ( + !osNotificationsEnabled && + wasAgentTaskCompleteOsNotificationEnabled && + pendingTerminalBellNotification + ) { + scheduleTerminalBellNotification() + } + if (!enabled && wasAgentTaskCompleteTrackingEnabled) { + // Why: disabling every completion consumer is an event-time boundary. + // Drop pending timers and coordinator state so old work cannot replay. agentTaskCompleteNotificationGeneration += 1 clearPendingAgentTaskCompleteNotification() agentCompletionCoordinator.resetCompletionState({ requireFreshWorking: true }) if (pendingTerminalBellNotification) { scheduleTerminalBellNotification() } - } else if (enabled && !wasAgentTaskCompleteNotificationEnabled) { - // Why: a pane may have observed work while agent-complete was disabled. - // Re-enabling should not let the next idle event notify for that old task. + } else if (enabled && !wasAgentTaskCompleteTrackingEnabled) { + // Why: a pane may have observed work while all completion consumers were + // disabled. Re-enabling should not let the next idle event report old work. agentCompletionCoordinator.resetCompletionState({ requireFreshWorking: true }) } - wasAgentTaskCompleteNotificationEnabled = enabled + wasAgentTaskCompleteTrackingEnabled = enabled + wasAgentTaskCompleteOsNotificationEnabled = osNotificationsEnabled return enabled } @@ -1025,7 +1068,7 @@ export function connectPanePty( agentStatusSnapshot?: ParsedAgentStatusPayload } = {} ): void => { - if (!syncAgentTaskCompleteNotificationEnabled()) { + if (!syncAgentTaskCompleteTrackingEnabled()) { return } clearPendingAgentTaskCompleteNotification() @@ -1036,19 +1079,24 @@ export function connectPanePty( clearPendingAgentTaskCompleteNotification() if ( generationAtSchedule !== agentTaskCompleteNotificationGeneration || - !syncAgentTaskCompleteNotificationEnabled() + !syncAgentTaskCompleteTrackingEnabled() ) { return } - pendingTerminalBellNotification = false - clearTerminalBellNotificationTimer() if (disposed) { return } + // Why: terminal attention is a visual pane affordance, not an OS + // notification. Route through dispatch so stale pane completions are + // rejected before unread attention is marked. + const shouldDispatchOsNotification = isAgentTaskCompleteNotificationEnabled() + pendingTerminalBellNotification = false + clearTerminalBellNotificationTimer() deps.dispatchNotification({ source: 'agent-task-complete', terminalTitle: title, paneKey: cacheKey, + ...(shouldDispatchOsNotification ? {} : { suppressOsNotification: true }), ...(options.agentStatusSnapshot ? { agentStatusSnapshot: options.agentStatusSnapshot } : {}) }) } @@ -1076,8 +1124,8 @@ export function connectPanePty( AGENT_TASK_COMPLETE_NOTIFICATION_MAX_WAIT_MS ) } - agentTaskCompleteSettingsUnsubscribe = subscribeAgentTaskCompleteNotificationEnabled(() => { - if (syncAgentTaskCompleteNotificationEnabled()) { + agentTaskCompleteSettingsUnsubscribe = subscribeAgentTaskCompleteTrackingEnabled(() => { + if (syncAgentTaskCompleteTrackingEnabled()) { agentCompletionCoordinator.startProcessTracking() } }) @@ -1109,7 +1157,7 @@ export function connectPanePty( if (isClaudeAgent(title) && (settings === null || settings.promptCacheTimerEnabled)) { deps.setCacheTimerStartedAt(cacheKey, Date.now()) } - if (syncAgentTaskCompleteNotificationEnabled()) { + if (syncAgentTaskCompleteTrackingEnabled()) { agentCompletionCoordinator.observeClassifiedTitleCompletion(title) } // Why: some agent TUIs leave xterm in DECSCUSR steady-cursor mode when @@ -1117,7 +1165,7 @@ export function connectPanePty( queueAgentIdleCursorReset() } const onAgentBecameWorking = (): void => { - if (syncAgentTaskCompleteNotificationEnabled()) { + if (syncAgentTaskCompleteTrackingEnabled()) { agentCompletionCoordinator.observeTitleWorking() } // Why: a new API call refreshes the prompt-cache TTL, so clear any running @@ -1212,7 +1260,7 @@ export function connectPanePty( const currentState = useAppStore.getState() const title = currentState.runtimePaneTitlesByTabId?.[deps.tabId]?.[pane.id] currentState.setAgentStatus(cacheKey, payload, title) - if (syncAgentTaskCompleteNotificationEnabled()) { + if (syncAgentTaskCompleteTrackingEnabled()) { agentCompletionCoordinator.observeHookStatus(payload) } if (payload.state === 'working' && pendingTerminalBellNotification) { @@ -1261,12 +1309,9 @@ export function connectPanePty( clearPendingTerminalInputIntent() return } - // Why: a real keystroke into the terminal is the unambiguous "user is - // here" signal that dismisses attention. Guarded by the replay and - // codex-stale checks above so synthetic xterm auto-replies never count. - deps.clearTerminalTabUnread(deps.tabId) - deps.clearTerminalPaneUnread(cacheKey) - deps.clearWorktreeUnread(deps.worktreeId) + // Why: attention dismissal is tied to DOM keyboard/pointer interaction. + // xterm onData can also carry terminal-generated replies or control bytes, + // so clearing here would hide a fresh pane highlight before the user sees it. if (shouldSuppressForegroundCursor) { // Why: native Windows ConPTY can leave the old visual cursor painted // until the shell echoes the next frame; other PTY hosts should keep diff --git a/src/renderer/src/components/terminal-pane/use-notification-dispatch.test.ts b/src/renderer/src/components/terminal-pane/use-notification-dispatch.test.ts index ae04d8f9c..3e607cf62 100644 --- a/src/renderer/src/components/terminal-pane/use-notification-dispatch.test.ts +++ b/src/renderer/src/components/terminal-pane/use-notification-dispatch.test.ts @@ -218,6 +218,20 @@ describe('dispatchTerminalNotification', () => { expect(mockState.markTerminalPaneUnread).not.toHaveBeenCalled() }) + it('can mark terminal attention without dispatching an OS notification', () => { + dispatchTerminalNotification('wt-primary', { + source: 'agent-task-complete', + terminalTitle: 'codex', + paneKey, + suppressOsNotification: true + }) + + expect(mockState.markWorktreeUnread).toHaveBeenCalledWith('wt-primary') + expect(mockState.markTerminalTabUnread).toHaveBeenCalledWith('tab-1') + expect(mockState.markTerminalPaneUnread).toHaveBeenCalledWith(paneKey) + expect(window.api.notifications.dispatch).not.toHaveBeenCalled() + }) + it('marks the visible focused worktree unread when terminal attention is disabled', () => { mockState.settings.experimentalTerminalAttention = false mockState.activeWorktreeId = 'wt-primary' diff --git a/src/renderer/src/components/terminal-pane/use-notification-dispatch.ts b/src/renderer/src/components/terminal-pane/use-notification-dispatch.ts index cd7baffd9..ef820bc53 100644 --- a/src/renderer/src/components/terminal-pane/use-notification-dispatch.ts +++ b/src/renderer/src/components/terminal-pane/use-notification-dispatch.ts @@ -18,6 +18,7 @@ export type TerminalNotificationEvent = { terminalTitle?: string paneKey?: string agentStatusSnapshot?: ParsedAgentStatusPayload + suppressOsNotification?: boolean } function hasLivePtyForWorktree(state: StoreSnapshot, candidateWorktreeId: string): boolean { @@ -282,6 +283,10 @@ export function dispatchTerminalNotification( } } + if (event.suppressOsNotification) { + return + } + // Why: prefer worktree.repoId over string-parsing the worktreeId. The // `${repoId}::${path}` format is an implementation detail of id // construction; coupling the notification dispatcher to it would silently diff --git a/src/renderer/src/components/terminal-pane/use-terminal-pane-lifecycle.ts b/src/renderer/src/components/terminal-pane/use-terminal-pane-lifecycle.ts index 5dc899186..0ce04e4ac 100644 --- a/src/renderer/src/components/terminal-pane/use-terminal-pane-lifecycle.ts +++ b/src/renderer/src/components/terminal-pane/use-terminal-pane-lifecycle.ts @@ -161,6 +161,7 @@ type UseTerminalPaneLifecycleDeps = { terminalTitle?: string paneKey?: string agentStatusSnapshot?: ParsedAgentStatusPayload + suppressOsNotification?: boolean }) => void setCacheTimerStartedAt: (key: string, ts: number | null) => void syncPanePtyLayoutBinding: (paneId: number, ptyId: string | null) => void diff --git a/src/renderer/src/hooks/agent-hook-completion-notifications.test.ts b/src/renderer/src/hooks/agent-hook-completion-notifications.test.ts index 0548d86d1..5f5dfbf09 100644 --- a/src/renderer/src/hooks/agent-hook-completion-notifications.test.ts +++ b/src/renderer/src/hooks/agent-hook-completion-notifications.test.ts @@ -6,6 +6,7 @@ const dispatchTerminalNotification = vi.fn() type MockStoreState = { settings: { + experimentalTerminalAttention?: boolean notifications: { enabled: boolean agentTaskComplete: boolean @@ -56,6 +57,7 @@ describe('agent hook completion notifications', () => { dispatchTerminalNotification.mockClear() mockStoreState = { settings: { + experimentalTerminalAttention: false, notifications: { enabled: true, agentTaskComplete: true @@ -121,6 +123,29 @@ describe('agent hook completion notifications', () => { ) }) + it('tracks hook completion for terminal attention when OS completion notifications are disabled', async () => { + mockStoreState.settings.experimentalTerminalAttention = true + mockStoreState.settings.notifications.agentTaskComplete = false + const { observeAgentHookCompletionForNotification } = + await import('./agent-hook-completion-notifications') + + observeAgentHookCompletionForNotification({ + paneKey, + worktreeId: 'wt-1', + payload: hookStatus('done') + }) + vi.advanceTimersByTime(HOOK_DONE_QUIET_MS) + + expect(dispatchTerminalNotification).toHaveBeenCalledWith( + 'wt-1', + expect.objectContaining({ + source: 'agent-task-complete', + paneKey, + suppressOsNotification: true + }) + ) + }) + it('uses tab-level PTY liveness when an inactive pane leaf binding is temporarily missing', async () => { mockStoreState.terminalLayoutsByTabId = { 'tab-1': { diff --git a/src/renderer/src/hooks/agent-hook-completion-notifications.ts b/src/renderer/src/hooks/agent-hook-completion-notifications.ts index 421387bae..876258159 100644 --- a/src/renderer/src/hooks/agent-hook-completion-notifications.ts +++ b/src/renderer/src/hooks/agent-hook-completion-notifications.ts @@ -16,8 +16,8 @@ type StoreSnapshot = ReturnType const coordinatorsByPaneKey = new Map() const paneKeysRequiringFreshWorking = new Set() -let wasAgentTaskCompleteNotificationEnabled = isAgentTaskCompleteNotificationEnabled() -let requireFreshWorkingForNewCoordinators = !wasAgentTaskCompleteNotificationEnabled +let wasAgentTaskCompleteTrackingEnabled = isAgentTaskCompleteTrackingEnabled() +let requireFreshWorkingForNewTrackingCoordinators = !wasAgentTaskCompleteTrackingEnabled function disposeCoordinatorForPaneKey(paneKey: string): void { coordinatorsByPaneKey.get(paneKey)?.coordinator.dispose() @@ -45,17 +45,25 @@ function isAgentTaskCompleteNotificationEnabled(): boolean { return notifications?.enabled !== false && notifications?.agentTaskComplete !== false } +function isTerminalAttentionEnabled(): boolean { + return useAppStore.getState().settings?.experimentalTerminalAttention === true +} + +function isAgentTaskCompleteTrackingEnabled(): boolean { + return isAgentTaskCompleteNotificationEnabled() || isTerminalAttentionEnabled() +} + export function syncAgentHookCompletionNotificationSettings(): boolean { pruneClosedPaneCoordinators() - const enabled = isAgentTaskCompleteNotificationEnabled() - if (!enabled || (!wasAgentTaskCompleteNotificationEnabled && enabled)) { - requireFreshWorkingForNewCoordinators = true + const enabled = isAgentTaskCompleteTrackingEnabled() + if (!enabled || (!wasAgentTaskCompleteTrackingEnabled && enabled)) { + requireFreshWorkingForNewTrackingCoordinators = true for (const [paneKey, entry] of coordinatorsByPaneKey) { paneKeysRequiringFreshWorking.add(paneKey) entry.coordinator.resetCompletionState({ requireFreshWorking: true }) } } - wasAgentTaskCompleteNotificationEnabled = enabled + wasAgentTaskCompleteTrackingEnabled = enabled return enabled } @@ -145,6 +153,7 @@ function createCoordinator(paneKey: string, worktreeId: string): AgentCompletion source: 'agent-task-complete', terminalTitle: title, paneKey, + suppressOsNotification: !isAgentTaskCompleteNotificationEnabled(), ...(meta?.agentStatus ? { agentStatusSnapshot: meta.agentStatus } : {}) }) }, @@ -182,7 +191,7 @@ export function observeAgentHookCompletionForNotification({ coordinator: createCoordinator(paneKey, worktreeId) } coordinatorsByPaneKey.set(paneKey, entry) - if (requireFreshWorkingForNewCoordinators) { + if (requireFreshWorkingForNewTrackingCoordinators) { paneKeysRequiringFreshWorking.add(paneKey) } } @@ -202,8 +211,8 @@ export function resetAgentHookCompletionNotificationCoordinators(): void { } coordinatorsByPaneKey.clear() paneKeysRequiringFreshWorking.clear() - wasAgentTaskCompleteNotificationEnabled = isAgentTaskCompleteNotificationEnabled() - requireFreshWorkingForNewCoordinators = !wasAgentTaskCompleteNotificationEnabled + wasAgentTaskCompleteTrackingEnabled = isAgentTaskCompleteTrackingEnabled() + requireFreshWorkingForNewTrackingCoordinators = !wasAgentTaskCompleteTrackingEnabled } export function _getAgentHookCompletionNotificationCoordinatorCountForTest(): number {