Skip to content

The agent that grows with you

fix(desktop): only active terminal tab owns add-selection shortcut

686f6c619a9680218c58
diff --git a/apps/desktop/src/app/right-sidebar/terminal/selection.test.ts b/apps/desktop/src/app/right-sidebar/terminal/selection.test.ts
new file mode 100644
index 00000000000..ec0e6160b88
--- /dev/null
+++ b/apps/desktop/src/app/right-sidebar/terminal/selection.test.ts
@@ -0,0 +1,37 @@
+import { describe, expect, it } from 'vitest'
+
+import { isMacPlatform, shouldOwnAddSelectionShortcut } from './selection'
+
+import { isComposerChord } from '@/lib/keybinds/chords'
+
+const key = (init: Partial<KeyboardEvent> & { key: string }) =>
+ ({
+ altKey: false,
+ ctrlKey: false,
+ metaKey: false,
+ shiftKey: false,
+ type: 'keydown',
+ ...init,
+ }) as KeyboardEvent
+
+/** Chord that the composer-focus ladder accepts on this host. */
+const addSelectionKey = () =>
+ isMacPlatform() ? key({ key: 'l', metaKey: true }) : key({ key: 'l', ctrlKey: true })
+
+describe('shouldOwnAddSelectionShortcut', () => {
+ it('only the active tab claims the add-selection shortcut when text is selected (#76116)', () => {
+ const chord = addSelectionKey()
+
+ expect(shouldOwnAddSelectionShortcut(chord, { active: true, hasSelection: true })).toBe(true)
+
+ // Inactive mounted tabs must not also fire — that was the N-pill bug.
+ expect(shouldOwnAddSelectionShortcut(chord, { active: false, hasSelection: true })).toBe(false)
+ })
+
+ it('never claims the shortcut with no selection so clear-screen still reaches the shell', () => {
+ const chord = addSelectionKey()
+
+ expect(shouldOwnAddSelectionShortcut(chord, { active: true, hasSelection: false })).toBe(false)
+ expect(isComposerChord(chord)).toBe(true)
+ })
+})
diff --git a/apps/desktop/src/app/right-sidebar/terminal/selection.ts b/apps/desktop/src/app/right-sidebar/terminal/selection.ts
index af72dc6f134..1fca1e8c172 100644
--- a/apps/desktop/src/app/right-sidebar/terminal/selection.ts
+++ b/apps/desktop/src/app/right-sidebar/terminal/selection.ts
@@ -1,6 +1,7 @@
import type { ITheme, Terminal } from '@xterm/xterm'
import type { CSSProperties } from 'react'
+import { isComposerChord } from '@/lib/keybinds/chords'
import { isMacPlatform } from '@/lib/platform'
import type { DesktopTerminalPalette } from '@/themes/types'
@@ -100,6 +101,16 @@ export function resolveSurfaceColor(fallback: string): string {
export { isMacPlatform }
+/** Whether this terminal session should own a global ⌘/Ctrl+L keydown.
+ * `active` gates the fanout: every open tab stays mounted, so an inactive
+ * tab must never claim the press (#76116). */
+export function shouldOwnAddSelectionShortcut(
+ event: KeyboardEvent,
+ opts: { active: boolean; hasSelection: boolean }
+) {
+ return opts.active && opts.hasSelection && isComposerChord(event)
+}
+
export function terminalSelectionLabel(term: Terminal, shellName: string, text: string) {
const pos = term.getSelectionPosition()
diff --git a/apps/desktop/src/app/right-sidebar/terminal/use-terminal-session.ts b/apps/desktop/src/app/right-sidebar/terminal/use-terminal-session.ts
index 70bb6b32b13..f61b29927e0 100644
--- a/apps/desktop/src/app/right-sidebar/terminal/use-terminal-session.ts
+++ b/apps/desktop/src/app/right-sidebar/terminal/use-terminal-session.ts
@@ -20,6 +20,7 @@ import { terminalLinkHandler, terminalWebLinksAddon } from './links'
import {
isMacPlatform,
resolveSurfaceColor,
+ shouldOwnAddSelectionShortcut,
terminalSelectionAnchor,
terminalSelectionLabel,
terminalTheme
@@ -473,12 +474,26 @@ export function useTerminalSession({
triggerHaptic('selection')
}, [])
- // Always listen — gating on the React selection state misses selections the
- // TUI redraw races. Only swallow ⌘/Ctrl+L when there's text to send, else it
- // must reach the shell as clear-screen.
+ // Only the active tab owns the global ⌘/Ctrl+L listener. Every open tab
+ // stays mounted, so registering the capture handler on every session
+ // fired N identical add-selection calls for a single keypress (#76116).
+ // Still do not gate on React selection state — TUI redraw races can
+ // clear that while xterm / window still have live text.
+ // Only swallow ⌘/Ctrl+L when there's text to send; otherwise it must
+ // reach the shell as clear-screen.
useEffect(() => {
+ if (!active) {
+ return
+ }
+
const onKeyDown = (event: KeyboardEvent) => {
- if (!isComposerChord(event) || !readSelection().trim()) {
+ if (!isComposerChord(event)) {
+ return
+ }
+
+ const hasSelection = Boolean(readSelection().trim())
+
+ if (!shouldOwnAddSelectionShortcut(event, { active: true, hasSelection })) {
return
}
@@ -490,7 +505,7 @@ export function useTerminalSession({
window.addEventListener('keydown', onKeyDown, { capture: true })
return () => window.removeEventListener('keydown', onKeyDown, { capture: true })
- }, [addSelectionToChat, readSelection])
+ }, [active, addSelectionToChat, readSelection])
// eslint-disable-next-line no-restricted-syntax -- legitimate non-atom ref write (see eslint rule comment)
useEffect(() => {