Skip to content

The agent that grows with you

fix(webapp): keep Alt chords Hermes runs off the PTY, Option remaps in macOS Firefox, and the switcher hold

dokterdok664394605eb4
diff --git a/apps/desktop/src/app/hooks/use-keybinds-browser.test.tsx b/apps/desktop/src/app/hooks/use-keybinds-browser.test.tsx
index ee255d9ee7a..b8347e964bf 100644
--- a/apps/desktop/src/app/hooks/use-keybinds-browser.test.tsx
+++ b/apps/desktop/src/app/hooks/use-keybinds-browser.test.tsx
@@ -19,6 +19,7 @@ beforeEach(() => {
afterEach(() => {
cleanup()
+ vi.useRealTimers()
vi.restoreAllMocks()
window.document.body.replaceChildren()
delete window.document.documentElement.dataset.hermesDesktopHost
@@ -122,6 +123,20 @@ it('dispatches the macOS ⌃⌘ browser defaults, including ⌃⌘W over an inte
expect(closeTab).toHaveBeenCalledOnce()
})
+it('keeps an Option remap on macOS, where Firefox reports Option as AltGraph', async () => {
+ vi.spyOn(navigator, 'platform', 'get').mockReturnValue('MacIntel')
+ const { press } = await mountKeybinds()
+ const { setBinding } = await import('@/store/keybinds')
+ const { $commandPaletteOpen } = await import('@/store/command-palette')
+ act(() => setBinding('nav.commandPalette', ['alt+.']))
+
+ const option = press({ key: '≥', code: 'Period', altKey: true, ctrlKey: false, modifierAltGraph: true })
+
+ expect(option.getModifierState('AltGraph')).toBe(true)
+ expect(option.defaultPrevented).toBe(true)
+ expect($commandPaletteOpen.get()).toBe(true)
+})
+
it('steps the session switcher with Alt+] and commits when Alt is released', async () => {
vi.spyOn(navigator, 'platform', 'get').mockReturnValue('Linux x86_64')
const { $selectedStoredSessionId, $sessions } = await import('@/store/session')
@@ -145,3 +160,61 @@ it('steps the session switcher with Alt+] and commits when Alt is released', asy
expect($switcherOpen.get()).toBe(false)
expect(switcherActive()).toBe(false)
})
+
+it('keeps the switcher step held while another key is released', async () => {
+ vi.spyOn(navigator, 'platform', 'get').mockReturnValue('Linux x86_64')
+ const { $selectedStoredSessionId, $sessions } = await import('@/store/session')
+ const { $switcherOpen, SWITCHER_REVEAL_MS } = await import('@/store/session-switcher')
+ $sessions.set(['a', 'b', 'c'].map(id => ({ id }) as never))
+ $selectedStoredSessionId.set('a')
+ const { press } = await mountKeybinds()
+ vi.useFakeTimers()
+
+ const release = (key: string, code: string, init: KeyboardEventInit = {}) =>
+ act(() => {
+ window.dispatchEvent(new KeyboardEvent('keyup', { bubbles: true, code, key, ...init }))
+ })
+
+ press({ key: ']', code: 'BracketRight', altKey: true, ctrlKey: false })
+ release('a', 'KeyA', { altKey: true })
+ act(() => vi.advanceTimersByTime(SWITCHER_REVEAL_MS))
+ expect($switcherOpen.get()).toBe(true)
+
+ release(']', 'BracketRight', { altKey: true })
+ release('Alt', 'AltLeft')
+ expect($switcherOpen.get()).toBe(false)
+
+ // A quick tap of the step key itself, Alt still down: no HUD.
+ press({ key: ']', code: 'BracketRight', altKey: true, ctrlKey: false })
+ release(']', 'BracketRight', { altKey: true })
+ act(() => vi.advanceTimersByTime(SWITCHER_REVEAL_MS))
+ expect($switcherOpen.get()).toBe(false)
+})
+
+it('marks only chords an action ran, so the terminal keeps a declined one', async () => {
+ vi.spyOn(navigator, 'platform', 'get').mockReturnValue('Linux x86_64')
+ const { deps, press } = await mountKeybinds(true)
+ const { setBinding } = await import('@/store/keybinds')
+ const { keybindHandled } = await import('@/lib/keybinds/handled-keys')
+
+ const ran = press({ key: 't', code: 'KeyT', altKey: true, ctrlKey: false })
+ expect(deps.openNewSessionTab).toHaveBeenCalledOnce()
+ expect(keybindHandled(ran)).toBe(true)
+
+ // view.tabSlot.9 declines with no tab strip: cancelled, but no action ran.
+ act(() => setBinding('view.tabSlot.9', ['alt+9']))
+
+ const declined = new KeyboardEvent('keydown', {
+ altKey: true,
+ bubbles: true,
+ cancelable: true,
+ code: 'Digit9',
+ key: '9'
+ })
+
+ act(() => {
+ window.document.body.dispatchEvent(declined)
+ })
+ expect(declined.defaultPrevented).toBe(true)
+ expect(keybindHandled(declined)).toBe(false)
+})
diff --git a/apps/desktop/src/app/hooks/use-keybinds.ts b/apps/desktop/src/app/hooks/use-keybinds.ts
index d5749e78776..36dd8040dbd 100644
--- a/apps/desktop/src/app/hooks/use-keybinds.ts
+++ b/apps/desktop/src/app/hooks/use-keybinds.ts
@@ -27,6 +27,7 @@ import {
import { handleApprovalKey, releaseApprovalKey } from '@/lib/keybinds/approval-keys'
import { actionAllowedInInput, comboFromEvent, IS_MAC, isEditableTarget, isFocusWithin } from '@/lib/keybinds/combo'
import { composerFocusKeysAllowed, isComposerFocusSoftCombo, typeToFocusChar } from '@/lib/keybinds/composer-focus-keys'
+import { markKeybindHandled } from '@/lib/keybinds/handled-keys'
import { registerBuiltinActionRunner } from '@/lib/keybinds/plugin-actions'
import { stepReasoningEffort, writeSessionReasoningEffort } from '@/lib/reasoning-step'
import { openWorktreeDialog } from '@/store/coding-status'
@@ -78,6 +79,7 @@ import {
$switcherOpen,
closeSwitcher,
commitOnCtrlUp,
+ noteSwitcherKeyDown,
onSwitcherTabDown,
onSwitcherTabUp,
openOrAdvanceSwitcher,
@@ -481,6 +483,8 @@ export function useKeybinds(deps: KeybindRuntimeDeps): void {
useEffect(() => {
const onKeyDown = (event: KeyboardEvent) => {
+ noteSwitcherKeyDown(event.code)
+
// An active IME composition owns the keyboard. Windows Chinese IMEs
// (Microsoft Pinyin, Sogou) use Ctrl+, as their punctuation-mode toggle,
// so without this guard that keystroke ALSO matched `nav.settings` and
@@ -616,6 +620,7 @@ export function useKeybinds(deps: KeybindRuntimeDeps): void {
continue
}
+ markKeybindHandled(event)
recordAction(actionId, 'shortcut')
return
@@ -634,7 +639,7 @@ export function useKeybinds(deps: KeybindRuntimeDeps): void {
// releasing its step key ends the hold, and releasing the last
// non-Shift modifier commits.
if (!SWITCHER_MODIFIER_KEYS.has(event.key)) {
- onSwitcherTabUp()
+ onSwitcherTabUp(event.code)
} else if (event.key !== 'Shift' && !event.ctrlKey && !event.altKey && !event.metaKey) {
commitSwitcherRef.current()
}
diff --git a/apps/desktop/src/app/right-sidebar/terminal/terminal-input-bindings.test.ts b/apps/desktop/src/app/right-sidebar/terminal/terminal-input-bindings.test.ts
index b203a96328d..e207d708004 100644
--- a/apps/desktop/src/app/right-sidebar/terminal/terminal-input-bindings.test.ts
+++ b/apps/desktop/src/app/right-sidebar/terminal/terminal-input-bindings.test.ts
@@ -1,6 +1,8 @@
import type { Terminal } from '@xterm/xterm'
import { expect, it, vi } from 'vitest'
+import { markKeybindHandled } from '@/lib/keybinds/handled-keys'
+
import { terminalMenuHandleFor } from './terminal-context-menu'
import { bindTerminalClipboard } from './terminal-input-bindings'
@@ -34,3 +36,30 @@ it('routes word erase only to a live shell and releases the context-menu handle
dispose()
expect(terminalMenuHandleFor(host)).toBeNull()
})
+
+it('keeps a plain Alt chord Hermes ran off the PTY and leaves shared Ctrl keys and other cancels to the shell', () => {
+ const attachCustomKeyEventHandler = vi.fn()
+ const term = { attachCustomKeyEventHandler, getSelection: () => '' } as unknown as Terminal
+ const dispose = bindTerminalClipboard(term, document.createElement('div'), vi.fn(), vi.fn(), () => true)
+ const passToPty = attachCustomKeyEventHandler.mock.calls[0]![0] as (event: KeyboardEvent) => boolean
+
+ const ran = (init: KeyboardEventInit) => {
+ const event = new KeyboardEvent('keydown', { cancelable: true, ...init })
+ event.preventDefault()
+ markKeybindHandled(event)
+
+ return event
+ }
+
+ expect(passToPty(ran({ altKey: true, key: 't' }))).toBe(false)
+ // Ctrl+D is EOF in the shell even though it is also a Hermes chord.
+ expect(passToPty(ran({ ctrlKey: true, key: 'd' }))).toBe(true)
+ expect(passToPty(new KeyboardEvent('keydown', { altKey: true, key: 'b' }))).toBe(true)
+
+ // Cancelled by someone else (a tooltip's Escape, a declined passthrough
+ // chord): still the shell's.
+ const cancelled = new KeyboardEvent('keydown', { altKey: true, cancelable: true, key: '9' })
+ cancelled.preventDefault()
+ expect(passToPty(cancelled)).toBe(true)
+ dispose()
+})
diff --git a/apps/desktop/src/app/right-sidebar/terminal/terminal-input-bindings.ts b/apps/desktop/src/app/right-sidebar/terminal/terminal-input-bindings.ts
index 45e86efb0ec..c2540131639 100644
--- a/apps/desktop/src/app/right-sidebar/terminal/terminal-input-bindings.ts
+++ b/apps/desktop/src/app/right-sidebar/terminal/terminal-input-bindings.ts
@@ -2,6 +2,7 @@ import type { Terminal } from '@xterm/xterm'
import { writeClipboardText } from '@/lib/clipboard'
import { triggerHaptic } from '@/lib/haptics'
+import { keybindHandled } from '@/lib/keybinds/handled-keys'
import { terminalClipboardIntent } from './clipboard'
import { isMacPlatform } from './selection'
@@ -84,6 +85,14 @@ export function bindTerminalClipboard(
// to the PTY; every path that doesn't copy or paste returns true, so plain
// Ctrl+C with no selection still interrupts the running process.
term.attachCustomKeyEventHandler(event => {
+ // The window keybind dispatcher runs in capture, before xterm. A plain Alt
+ // chord it ran as a Hermes action (a browser tab's Alt+T, Alt+[) must not
+ // also reach the shell as its Meta key (ESC t, ESC [). Ctrl chords stay
+ // shared, as natively: Ctrl+D is still EOF and Ctrl+B a tmux prefix.
+ if (keybindHandled(event) && event.altKey && !event.ctrlKey && !event.metaKey) {
+ return false
+ }
+
const intent = terminalClipboardIntent(event, {
hasSelection: Boolean(term.getSelection()),
isMac: isMacPlatform()
diff --git a/apps/desktop/src/lib/keybinds/combo.ts b/apps/desktop/src/lib/keybinds/combo.ts
index 8f37437d0a8..f4b67b6f59e 100644
--- a/apps/desktop/src/lib/keybinds/combo.ts
+++ b/apps/desktop/src/lib/keybinds/combo.ts
@@ -141,8 +141,9 @@ export function comboFromEvent(event: KeyboardEvent): string | null {
// AltGr typing a character. Windows reports it as Ctrl+Alt; Linux can
// report Alt with AltGraph but no Ctrl, which must not read as a plain
- // Alt chord (the browser-tab defaults are Alt+letter off macOS).
- if (event.altKey && !event.ctrlKey && !event.metaKey && event.getModifierState?.('AltGraph')) {
+ // Alt chord (the browser-tab defaults are Alt+letter off macOS). Firefox on
+ // macOS reports every Option press as AltGraph, so Option chords stay chords.
+ if (!IS_MAC && event.altKey && !event.ctrlKey && !event.metaKey && event.getModifierState?.('AltGraph')) {
return null
}
diff --git a/apps/desktop/src/lib/keybinds/handled-keys.ts b/apps/desktop/src/lib/keybinds/handled-keys.ts
new file mode 100644
index 00000000000..408ac6ab686
--- /dev/null
+++ b/apps/desktop/src/lib/keybinds/handled-keys.ts
@@ -0,0 +1,14 @@
+// Keydowns the window keybind dispatcher ran as a Hermes action. It cancels
+// every chord it matches in capture, including one whose passthrough handler
+// then declines, so `defaultPrevented` alone doesn't say an action ran. xterm
+// ignores `defaultPrevented`; the terminal asks here before sending a key to
+// the PTY.
+const handled = new WeakSet<Event>()
+
+export function markKeybindHandled(event: Event): void {
+ handled.add(event)
+}
+
+export function keybindHandled(event: Event): boolean {
+ return handled.has(event)
+}
diff --git a/apps/desktop/src/store/session-switcher.ts b/apps/desktop/src/store/session-switcher.ts
index a3eda6a5504..0604cc5ce91 100644
--- a/apps/desktop/src/store/session-switcher.ts
+++ b/apps/desktop/src/store/session-switcher.ts
@@ -20,6 +20,12 @@ let pendingBrowse = false
let revealTimer: ReturnType<typeof setTimeout> | null = null
let tabHeld = false
let closedAt = 0
+// The physical key (`code`) of the keydown being dispatched, and of the one that
+// stepped the switcher: only the step key's keyup ends the hold, so another key
+// lifting mid-hold can't hide the HUD. A missing code on either side releases on
+// any key, as before.
+let keyDown = ''
+let stepKey = ''
function clearRevealTimer(): void {
if (revealTimer) {
@@ -44,11 +50,20 @@ function scheduleReveal(): void {
}, SWITCHER_REVEAL_MS)
}
+export function noteSwitcherKeyDown(code: string): void {
+ keyDown = code
+}
+
export function onSwitcherTabDown(): void {
tabHeld = true
+ stepKey = keyDown
}
-export function onSwitcherTabUp(): void {
+export function onSwitcherTabUp(code = ''): void {
+ if (code && stepKey && code !== stepKey) {
+ return
+ }
+
tabHeld = false
if (!$switcherOpen.get()) {