Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

fix(keybindings): scope the dictation gates to the surfaces the gesture claims

enesbef6d42f1bec
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index 5b68db0e0..95fc01b52 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -100,11 +100,14 @@ three times.
screen. A previous design moved that into the emulator and failed structurally; `CLAUDE.md` explains
why in detail.
-**A new keyboard chord has to survive the shells, not just the renderer.** Electron's default
-application menu is live (we never replace it) and owns ⌘0, ⌘M, ⌘W, ⌘Q, ⌘R and friends — a menu
-accelerator is handled before the page, so your `keydown` branch simply never runs. Steal it back in
-`main/index.ts`'s `before-input-event` and forward it, like the three already there. Browsers own a
-different set. And any chord that reaches the canvas needs the two refusals every canvas shortcut
+**A new keyboard chord has to survive the shells, not just the renderer.** The application menu is
+ours (`buildAppMenu` in `main/index.ts`), but its command-style accelerators — ⌘Q, ⌘M, ⌘W, ⌘0, ⌘⇧B,
+⌘, — are still handled above the page, so your `keydown` branch simply never runs: steal the chord
+back in `main/keydown-intercept.ts`'s `before-input-event` allowlist and forward it, like the three
+already there. Two legs stand the menu down instead of stealing — the terminal-first policy and an
+armed shortcut recorder (`menuStandsDown` → `menuItemIdsToSuspend`, since a disabled item suppresses
+its accelerator) — and Reload (⌘R / ⌘⇧R) is the named exception that always stays with the app,
+because it is the crash-recovery lever. Browsers own a different set. And any chord that reaches the canvas needs the two refusals every canvas shortcut
here has: not while the kanban board covers it, not while the user is typing.
**Comments explain WHY, and name the failure they prevent.** The codebase is deliberately dense with
diff --git a/src/main/keydown-intercept.ts b/src/main/keydown-intercept.ts
index 66ae803bc..1233d873b 100644
--- a/src/main/keydown-intercept.ts
+++ b/src/main/keydown-intercept.ts
@@ -277,9 +277,13 @@ export const MENU_ITEM_ID_SETTINGS = 'app-settings'
* earlier argument here was that a recorder greying out Minimize was a worse trade than an
* unrecordable ⌘M; that trade REVERSED once the menu leg existed for the policy anyway. The cost is
* now a momentary grey-out while a modal recorder is armed — self-healing the instant it disarms —
- * and the benefit is four chords that could not be bound at all: ⌘M everywhere, off-mac Ctrl+W, and
- * ⌘⇧B / ⌘, which did not merely fail to record but FIRED — pressing ⌘⇧B into the recorder opened
- * the kanban board behind the Settings dialog, and ⌘, re-opened Settings.
+ * and the benefit is that four chords now REACH the recorder instead of acting on the app: ⌘M
+ * everywhere, off-mac Ctrl+W, and ⌘⇧B / ⌘, which did not merely fail to record but FIRED —
+ * pressing ⌘⇧B into the recorder opened the kanban board behind the Settings dialog, and ⌘,
+ * re-opened Settings. **Reaching the recorder is all this buys, and it is the unconditional part.**
+ * Whether the chord can then be BOUND is a separate question, answered by the pre-save gates in
+ * `ShortcutsSection.commitCandidate`: while its owning command still holds it, it is refused there
+ * like any other taken chord — the user has to Disable or remap that command first.
*
* **KNOWN GAP for the recording half, pre-existing and accepted.** This list was written for the
* POLICY stand-down, so it covers only the command-style items that policy needed. The always-on
diff --git a/src/renderer/components/settings/sections/ShortcutsSection.test.tsx b/src/renderer/components/settings/sections/ShortcutsSection.test.tsx
index b787a7bf9..1ca500f71 100644
--- a/src/renderer/components/settings/sections/ShortcutsSection.test.tsx
+++ b/src/renderer/components/settings/sections/ShortcutsSection.test.tsx
@@ -295,7 +295,12 @@ describe('commitCandidate', () => {
// Dictation is its own conflict bucket (Task 1), so NEITHER of the three gates above can see an
// overlap with it — the detector is silent by design and the load path deliberately permits one.
// These two gates are what makes `conflictBucket`'s "the Settings UI REFUSES to create one" true.
- it("refuses binding another command onto Dictate's keyed chord, naming Dictate", () => {
+ //
+ // They are SCOPED, and the four tests below are the discriminating matrix: the keyed gesture is
+ // offered only in plain app focus (`globalKeybindings.ts` — `!ctx.typing && !ctx.terminal &&
+ // !ctx.kanbanOpen`), so an 'app'/'canvas'-scope command really does lose the chord most of the
+ // time, while a 'terminal'/'scm'-scope one NEVER competes with it and must stay bindable.
+ it("refuses a canvas-scope command on Dictate's keyed chord, naming Dictate", () => {
setKb({ 'speech.dictation': ['Cmd+Alt+D'] })
const r = commitCandidate('canvas.fitAll', 'Cmd+Alt+D', 'replace')
expect(r.ok).toBe(false)
@@ -303,21 +308,52 @@ describe('commitCandidate', () => {
expect(kb()['canvas.fitAll']).toBeUndefined()
})
- it('refuses a keyed Dictate chord that another command already holds', () => {
+ it("refuses an app-scope command on Dictate's keyed chord, naming Dictate", () => {
+ setKb({ 'speech.dictation': ['Cmd+Alt+D'] })
+ const r = commitCandidate('panel.explorer', 'Cmd+Alt+D', 'replace')
+ expect(r.ok).toBe(false)
+ if (!r.ok) expect(r.error).toContain('Dictate')
+ expect(kb()['panel.explorer']).toBeUndefined()
+ })
+
+ // The other half of the pair, and the one that reds if gate 1 loses its scope check: Find in
+ // terminal fires only in terminal focus, where the gesture is not offered at all — so this
+ // binding was legal before the branch, works at dispatch, and must stay accepted.
+ it("allows a terminal-scope command on Dictate's keyed chord", () => {
+ setKb({ 'speech.dictation': ['Cmd+Alt+D'] })
+ expect(commitCandidate('terminal.find', 'Cmd+Alt+D', 'replace')).toEqual({ ok: true })
+ expect(kb()['terminal.find']).toEqual(['Cmd+Alt+D'])
+ })
+
+ it('refuses a keyed Dictate chord that a global-bucket command already holds', () => {
const r = commitCandidate('speech.dictation', 'Cmd+K', 'replace')
expect(r.ok).toBe(false)
if (!r.ok) expect(r.error).toContain('Command palette')
expect(kb()['speech.dictation']).toBeUndefined()
})
- // Both gates are keyed-only for free: a hold chord's identity ends in `:(hold)`, which no keyed
- // identity can equal — so neither direction needs an explicit hold guard.
- it('a HOLD dictation chord never trips the overlap gates', () => {
+ // Gate 2's mirror of the same rule, and it reds if the loop stops skipping the two focused
+ // scopes: Find in terminal holds Cmd+F and Commit holds Cmd+Enter, neither of which the keyed
+ // gesture could ever take from them.
+ it('allows a keyed Dictate chord that only a focused-surface command holds', () => {
+ expect(commitCandidate('speech.dictation', 'Cmd+F', 'replace')).toEqual({ ok: true })
+ expect(kb()['speech.dictation']).toEqual(['Cmd+F'])
+ expect(commitCandidate('speech.dictation', 'Cmd+Enter', 'replace')).toEqual({ ok: true })
+ expect(kb()['speech.dictation']).toEqual(['Cmd+Enter'])
+ })
+
+ // DOCUMENTATION OF A PROPERTY, not a guard test — stated honestly because both stay GREEN if the
+ // dictation gates are deleted outright. Nothing can make them red by deletion: a hold chord's
+ // identity ends in `:(hold)` and no keyed identity can equal it, so correct code has no path to
+ // a refusal here. What the second one does discriminate is a mutation of `bindingIdentity`
+ // itself — drop the key segment and the default `Cmd+Alt` hold chord starts swallowing every
+ // Cmd+Alt+<key> candidate.
+ it('documents that a HOLD dictation chord cannot trip the overlap gates', () => {
const r = commitCandidate('speech.dictation', 'Cmd+Ctrl', 'replace')
expect(r.ok).toBe(true)
})
- it('the default hold chord does not block other commands (keyed-only gate)', () => {
+ it('documents that the default hold chord blocks no keyed candidate', () => {
const r = commitCandidate('canvas.fitAll', 'Cmd+Alt+F9', 'replace')
expect(r.ok).toBe(true)
})
diff --git a/src/renderer/components/settings/sections/ShortcutsSection.tsx b/src/renderer/components/settings/sections/ShortcutsSection.tsx
index e12d36cec..8685af567 100644
--- a/src/renderer/components/settings/sections/ShortcutsSection.tsx
+++ b/src/renderer/components/settings/sections/ShortcutsSection.tsx
@@ -13,7 +13,9 @@
* 3. `findMainInterceptShadowing` — a CROSS-bucket hit the conflict check cannot see.
* 4. REVERSE shadowing — the same collision seen from the non-intercepted side.
* 5. The two DICTATION overlap gates — the one overlap `conflictBucket` deliberately does not
- * report, refused here in both directions (see the block itself for why).
+ * report, refused here in both directions, but only for `app`/`canvas`-scope commands: the
+ * keyed gesture has a focus gate, so a terminal- or scm-scope command never competes with it
+ * (see the block itself for why).
* (2) returns early, so a candidate that trips both detectors (a main-intercepted command taking
* a chord another GLOBAL command already holds — `node.close` ← `Cmd+K`) produces exactly ONE
* message. The shadow message is reserved for what only it can see: `node.close` ← `Cmd+F`, where
@@ -131,6 +133,9 @@ export function commitCandidate(
const existing = effectiveBindings(id)
const nextList = mode === 'add' ? [...existing.filter((b) => b !== combo), combo] : [combo]
const chord = formatShortcut(combo, isMac)
+ // The candidate's platform identity, computed ONCE: the reverse-shadow block and both dictation
+ // gates all ask the same question of it, and three copies of one call is three chances to drift.
+ const identity = bindingIdentity(combo, isMac)
const conflicts = findKeybindingConflicts({ ...current, [id]: nextList }, isMac).filter((c) =>
c.commandIds.includes(id)
@@ -159,9 +164,8 @@ export function commitCandidate(
// every check and produced a permanently dead shortcut with no warning — main swallows the chord
// in `before-input-event` and the terminal surface is never offered it.
if (!MAIN_INTERCEPTED_COMMAND_IDS.includes(id)) {
- const target = bindingIdentity(combo, isMac)
for (const cid of MAIN_INTERCEPTED_COMMAND_IDS) {
- const hit = effectiveBindings(cid).some((b) => bindingIdentity(b, isMac) === target)
+ const hit = effectiveBindings(cid).some((b) => bindingIdentity(b, isMac) === identity)
if (!hit) continue
const title = COMMANDS_BY_ID.get(cid)?.title ?? cid
const own = COMMANDS_BY_ID.get(id)?.title ?? id
@@ -183,13 +187,20 @@ export function commitCandidate(
// Both gates are keyed-only WITHOUT an explicit hold guard: `bindingIdentity` renders a
// modifier-only chord as `…:(hold)`, which no keyed identity can ever equal — so the default
// `Cmd+Alt` hold chord blocks nothing, and a hold candidate trips nothing.
+ //
+ // Both are also SCOPED to `app`/`canvas`, because the gesture has a FOCUS GATE: the keyed
+ // dictation listener runs only in plain app focus (`dispatchGlobalKeydown` in
+ // `renderer/lib/globalKeybindings.ts` — `!ctx.typing && !ctx.terminal && !ctx.kanbanOpen`). A
+ // `terminal`- or `scm`-scope command dispatches ONLY where that gate is already shut, so it
+ // never competes with dictation for its chord; refusing the overlap would forbid a binding that
+ // was legal before this branch and still works at dispatch (⌘⌥D on Find in terminal, say).
const dictationId: CommandId = 'speech.dictation'
const dictationTitle = COMMANDS_BY_ID.get(dictationId)?.title ?? dictationId
- const identity = bindingIdentity(combo, isMac)
- if (id !== dictationId) {
+ const candidateScope = COMMANDS_BY_ID.get(id)?.scope
+ if (id !== dictationId && (candidateScope === 'app' || candidateScope === 'canvas')) {
// "rarely", not "never": dictation's keyed listener only claims the chord where it listens.
- // In terminal or typing focus the gesture is not offered and this command CAN still fire —
- // the message must not overclaim a chord that is merely usually lost.
+ // An app-scope command flagged `allowInTerminal` still fires in terminal focus under
+ // app-first — the message must not overclaim a chord that is merely usually lost.
const taken = effectiveBindings(dictationId).some((b) => bindingIdentity(b, isMac) === identity)
if (taken) {
const own = COMMANDS_BY_ID.get(id)?.title ?? id
@@ -198,9 +209,12 @@ export function commitCandidate(
error: `${chord} is used by ${dictationTitle}, which claims it first; it would rarely reach ${own}.`
}
}
- } else {
+ } else if (id === dictationId) {
for (const def of COMMAND_DEFINITIONS) {
if (def.id === dictationId) continue
+ // The same focus gate, read from the other side: a focused-surface command cannot be hidden
+ // by a gesture that is never offered on that surface.
+ if (def.scope === 'terminal' || def.scope === 'scm') continue
const hit = effectiveBindings(def.id).some((b) => bindingIdentity(b, isMac) === identity)
if (!hit) continue
return {
@@ -286,7 +300,10 @@ export function ShortcutsSection({ isActive }: { isActive: boolean }): React.JSX
// items in `menuItemIdsToSuspend` (Minimize, Toggle Kanban Board, Settings, off-mac Close),
// so those chords reach the recorder — but RELOAD is deliberately never suspended, because it
// is the crash-recovery lever. See `src/main/keydown-intercept.ts`.
- description="Remap any command. Overrides are stored in settings.json under `keybindings`; Disable turns a command's shortcut off, Reset restores its default. Reload (⌘R / ⌘⇧R) cannot be recorded — it always stays with the app."
+ // The residual clause is the KNOWN GAP `menuItemIdsToSuspend` documents: one list drives
+ // both stand-downs, so the always-on app roles are deliberately left unsuspended and still
+ // act while recording. The ruling stands; the user is simply told.
+ description="Remap any command. Overrides are stored in settings.json under `keybindings`; Disable turns a command's shortcut off, Reset restores its default. Reload (⌘R / ⌘⇧R) cannot be recorded — it always stays with the app; the system-level chords the menu keeps — ⌘Q (Quit), ⌘H (Hide) and the developer roles — also act while recording, so don't try to bind them."
isActive={isActive}
searchEntries={entries}
>
diff --git a/src/shared/keybindings.test.ts b/src/shared/keybindings.test.ts
index dd5185938..2a537cd70 100644
--- a/src/shared/keybindings.test.ts
+++ b/src/shared/keybindings.test.ts
@@ -6,6 +6,7 @@ import {
normalizeBindingForCommand,
getEffectiveBindings,
bindingIdentity,
+ conflictBucket,
findKeybindingConflicts,
sanitizeKeybindingOverrides,
resolveCommandForKeyEvent,
@@ -196,6 +197,25 @@ describe('bindingIdentity', () => {
})
})
+describe('conflictBucket', () => {
+ // The whole mapping, asserted over the REGISTRY rather than a hand-picked example per bucket:
+ // 'app' and 'canvas' share the global keyspace, 'terminal' and 'scm' are their own, and
+ // `speech.dictation` is the one command whose bucket comes from its id instead of its scope.
+ // Kills two mutants a per-bucket sample can miss: a scope-mapping typo (folding 'scm' — or a
+ // scope added later — into 'global', or dropping 'canvas' out of it), and the removal of the
+ // dictation branch, which would send Dictate back into 'global' and re-report the collision
+ // the bucket exists to stop.
+ it('maps every command to its scope bucket, dictation excepted', () => {
+ for (const def of COMMAND_DEFINITIONS) {
+ if (def.id === 'speech.dictation') continue
+ expect(conflictBucket(def)).toBe(
+ def.scope === 'app' || def.scope === 'canvas' ? 'global' : def.scope
+ )
+ }
+ expect(conflictBucket(COMMANDS_BY_ID.get('speech.dictation')!)).toBe('dictation')
+ })
+})
+
describe('findKeybindingConflicts', () => {
it('reports nothing for pure defaults', () => {
expect(findKeybindingConflicts({}, true)).toEqual([])
diff --git a/src/shared/keybindings.ts b/src/shared/keybindings.ts
index 3522a896a..e71112217 100644
--- a/src/shared/keybindings.ts
+++ b/src/shared/keybindings.ts
@@ -328,9 +328,9 @@ export function findMainInterceptShadowing(
* Loops until conflict-free so one bad edit cannot leave ambiguous dispatch. This is the
* SETTINGS-LOAD path: it applies what survives and warns about what it dropped. It is not the
* pre-save gate — its warnings are unstructured strings, which cannot tell a UI which field to
- * block on. The future Settings UI blocks pre-save by calling `normalizeBindingForCommand` and
- * `findKeybindingConflicts` directly, per candidate binding: same detector, different surfacing
- * (design.md D3). */
+ * block on. The Settings UI blocks pre-save in `ShortcutsSection.commitCandidate`, which runs
+ * `normalizeBindingForCommand` (inside its recorder) and `findKeybindingConflicts` directly, per
+ * candidate binding: same detector, different surfacing (design.md D3). */
export function sanitizeKeybindingOverrides(
raw: unknown,
isMac: boolean