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.

test(keybindings): pin the whole table + close the review's doc gaps

enes5e04c6b657f2
diff --git a/src/shared/keybindings.test.ts b/src/shared/keybindings.test.ts
index 0a8735c41..ccb9afdf9 100644
--- a/src/shared/keybindings.test.ts
+++ b/src/shared/keybindings.test.ts
@@ -43,6 +43,66 @@ describe('registry invariants', () => {
expect(COMMANDS_BY_ID.get('canvas.fitAll')?.defaultBindings.darwin).toEqual([])
})
+ it('pins the WHOLE table — every row PR 2 will dispatch on, in source order', () => {
+ // Source order is contractual (first match wins in the resolver), so the array order is
+ // asserted too. A dropped flag — allowInTerminal above all — reds this test.
+ const table = COMMAND_DEFINITIONS.map((d) => ({
+ id: d.id,
+ title: d.title,
+ group: d.group,
+ scope: d.scope,
+ darwin: d.defaultBindings.darwin,
+ other: d.defaultBindings.other,
+ allowWhileTyping: d.allowWhileTyping,
+ allowInTerminal: d.allowInTerminal,
+ allowBareKey: d.allowBareKey,
+ allowHoldChord: d.allowHoldChord
+ }))
+ expect(table).toEqual([
+ { id: 'app.commandPalette', title: 'Command palette', group: 'General', scope: 'app',
+ darwin: ['Cmd+K'], other: ['Cmd+K'], allowInTerminal: true },
+ { id: 'app.settings', title: 'Open settings', group: 'General', scope: 'app',
+ darwin: ['Cmd+Comma'], other: ['Cmd+Comma'], allowInTerminal: true },
+ { id: 'app.shortcutsPanel', title: 'Keyboard shortcuts panel', group: 'General', scope: 'app',
+ darwin: ['Cmd+Slash'], other: ['Cmd+Slash'], allowInTerminal: true },
+ { id: 'view.kanbanToggle', title: 'Toggle kanban board', group: 'General', scope: 'app',
+ darwin: ['Cmd+Shift+B'], other: ['Cmd+Shift+B'], allowInTerminal: true },
+ { id: 'panel.explorer', title: 'Toggle explorer panel', group: 'General', scope: 'app',
+ darwin: ['Cmd+Shift+E'], other: ['Cmd+Shift+E'], allowInTerminal: true },
+ { id: 'panel.sourceControl', title: 'Toggle source control panel', group: 'General',
+ scope: 'app', darwin: ['Cmd+Shift+G'], other: ['Cmd+Shift+G'], allowInTerminal: true },
+ { id: 'panel.sessions', title: 'Pin sessions sidebar', group: 'General', scope: 'app',
+ darwin: ['Cmd+Shift+L'], other: ['Cmd+Shift+L'], allowInTerminal: true },
+ { id: 'canvas.undo', title: 'Undo', group: 'Canvas', scope: 'canvas',
+ darwin: ['Cmd+Z'], other: ['Cmd+Z'] },
+ { id: 'canvas.redo', title: 'Redo', group: 'Canvas', scope: 'canvas',
+ darwin: ['Cmd+Shift+Z'], other: ['Cmd+Shift+Z', 'Cmd+Y'] },
+ { id: 'canvas.deleteSelection', title: 'Delete selection', group: 'Canvas', scope: 'canvas',
+ darwin: ['Delete', 'Backspace'], other: ['Delete', 'Backspace'], allowBareKey: true },
+ { id: 'canvas.fitAll', title: 'Fit all nodes in view', group: 'Canvas', scope: 'canvas',
+ darwin: [], other: [] },
+ { id: 'canvas.groupSelection', title: 'Group selection', group: 'Canvas', scope: 'canvas',
+ darwin: [], other: [] },
+ { id: 'node.newTerminal', title: 'New terminal node', group: 'Nodes', scope: 'canvas',
+ darwin: ['Cmd+T'], other: ['Cmd+T'] },
+ { id: 'node.newAgent', title: 'New agent node', group: 'Nodes', scope: 'canvas',
+ darwin: ['Cmd+Shift+C'], other: ['Cmd+Shift+C'] },
+ { id: 'node.close', title: 'Close node / window', group: 'Nodes', scope: 'app',
+ darwin: ['Cmd+W'], other: ['Cmd+W'], allowInTerminal: true, allowWhileTyping: true },
+ { id: 'node.toggleMarkdown', title: 'Toggle markdown view', group: 'Nodes', scope: 'app',
+ darwin: ['Cmd+M'], other: ['Cmd+M'], allowInTerminal: true, allowWhileTyping: true },
+ { id: 'terminal.find', title: 'Find in terminal', group: 'Terminal', scope: 'terminal',
+ darwin: ['Cmd+F'], other: ['Cmd+F'] },
+ { id: 'terminal.copySelection', title: 'Copy terminal selection', group: 'Terminal',
+ scope: 'terminal', darwin: ['Cmd+C'], other: ['Cmd+Shift+C', 'Ctrl+Insert'] },
+ { id: 'scm.commit', title: 'Commit', group: 'Source Control', scope: 'scm',
+ darwin: ['Cmd+Enter'], other: ['Cmd+Enter'], allowWhileTyping: true },
+ { id: 'speech.dictation', title: 'Dictate', group: 'Speech', scope: 'app',
+ darwin: ['Cmd+Alt'], other: ['Cmd+Alt'],
+ allowHoldChord: true, allowInTerminal: true, allowWhileTyping: true }
+ ])
+ })
+
it('isCommandId accepts known ids and rejects unknowns', () => {
expect(isCommandId('node.newTerminal')).toBe(true)
expect(isCommandId('node.selfDestruct')).toBe(false)
@@ -164,6 +224,12 @@ describe('findKeybindingConflicts', () => {
// terminal.find is Cmd+F in the terminal bucket; an app-bucket Cmd+F is legal.
expect(findKeybindingConflicts({ 'canvas.fitAll': ['Cmd+F'] }, true)).toEqual([])
})
+ it('the scm bucket is its own keyspace, not part of global', () => {
+ // scm.commit holds Cmd+Enter; a canvas (global-bucket) override on the same chord is legal,
+ // because Commit dispatches from the focused composer. Reds if conflictBucket folds 'scm'
+ // into 'global'.
+ expect(findKeybindingConflicts({ 'canvas.fitAll': ['Cmd+Enter'] }, true)).toEqual([])
+ })
it('two disabled commands never conflict', () => {
expect(
findKeybindingConflicts({ 'canvas.fitAll': [], 'canvas.groupSelection': [] }, true)
@@ -216,6 +282,22 @@ describe('sanitizeKeybindingOverrides', () => {
expect(r.overrides).toEqual({})
expect(r.warnings.some((w) => w.includes('Command palette'))).toBe(true)
})
+ it('keeps stripping when a REVERTED default uncovers a second conflict', () => {
+ // Pass 1: Cmd+P collides between the two overrides, so both are dropped — which reverts
+ // app.commandPalette to its Cmd+K default, and THAT now collides with canvas.fitAll's
+ // override. Pass 2 strips it. A single-shot strip would leave canvas.fitAll on Cmd+K,
+ // ambiguous against the palette.
+ const r = sanitizeKeybindingOverrides(
+ {
+ 'app.commandPalette': ['Cmd+P'],
+ 'canvas.groupSelection': ['Cmd+P'],
+ 'canvas.fitAll': ['Cmd+K']
+ },
+ true
+ )
+ expect(r.overrides).toEqual({})
+ expect(r.warnings.length).toBeGreaterThanOrEqual(2)
+ })
it('non-object / non-array shapes degrade to empty with a warning', () => {
expect(sanitizeKeybindingOverrides('nope', true).overrides).toEqual({})
expect(sanitizeKeybindingOverrides({ 'canvas.undo': 'Cmd+Z' }, true).overrides).toEqual({})
@@ -294,6 +376,25 @@ describe('resolveCommandForKeyEvent', () => {
ev({ metaKey: true, key: 'f' }), ctx({ terminal: true }), o, true
)).toBe('terminal.find')
})
+ it('resolves the non-mac-only alternate default (Ctrl+Y → redo)', () => {
+ expect(resolveCommandForKeyEvent(ev({ ctrlKey: true, key: 'y' }), ctx(), {}, false))
+ .toBe('canvas.redo')
+ })
+ it('non-mac Ctrl+Shift+C is copy in a terminal and New agent outside one', () => {
+ // By design, the two buckets double-book this chord: node.newAgent (global) and
+ // terminal.copySelection (terminal) both default to it off-mac, and the focus gates — not
+ // the conflict detector — are what keep them apart at dispatch time.
+ const e = ev({ ctrlKey: true, shiftKey: true, key: 'C' })
+ expect(resolveCommandForKeyEvent(e, ctx({ terminal: true }), {}, false))
+ .toBe('terminal.copySelection')
+ expect(resolveCommandForKeyEvent(e, ctx(), {}, false)).toBe('node.newAgent')
+ })
+ it('a bare key resolves, and typing still blocks it', () => {
+ expect(resolveCommandForKeyEvent(ev({ key: 'Delete' }), ctx(), {}, true))
+ .toBe('canvas.deleteSelection')
+ expect(resolveCommandForKeyEvent(ev({ key: 'Delete' }), ctx({ typing: true }), {}, true))
+ .toBeNull()
+ })
it('first matching definition in registry source order wins a tie', () => {
// Deliberately unsanitized colliding override — the resolver must order the tie deterministically.
const o = { 'canvas.redo': ['Cmd+Z'] }
diff --git a/src/shared/keybindings.ts b/src/shared/keybindings.ts
index efcfec75e..4606501de 100644
--- a/src/shared/keybindings.ts
+++ b/src/shared/keybindings.ts
@@ -13,7 +13,7 @@ import {
matchesShortcut,
isHoldChord
} from './shortcut'
-import type { ParsedShortcut, ShortcutKeyEvent } from './shortcut'
+import type { ShortcutKeyEvent } from './shortcut'
export type CommandScope = 'app' | 'canvas' | 'terminal' | 'scm'
export type CommandGroup = 'General' | 'Canvas' | 'Nodes' | 'Terminal' | 'Source Control' | 'Speech'
@@ -261,9 +261,12 @@ export function findKeybindingConflicts(
}
/** Validate a raw `settings.keybindings` value (hand-editable JSON) into a safe override map.
- * Loops until conflict-free so one bad edit cannot leave ambiguous dispatch. The load path
- * applies the result and warns; the Settings UI runs the same function pre-save and BLOCKS
- * instead — one detector, two surfacings (design.md D3). */
+ * 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). */
export function sanitizeKeybindingOverrides(
raw: unknown,
isMac: boolean
@@ -304,6 +307,10 @@ export function sanitizeKeybindingOverrides(
}
// Strip overridden participants of any remaining conflict until none are left. Bounded:
// each pass removes at least one override or exits.
+ // NEVER pass `includeDefaults: true` here. Termination rests on the touchesOverride reporting
+ // gate: every reported conflict has at least one OVERRIDE to delete, so a pass always shrinks
+ // the map. A default-vs-default conflict has no deletable participant, so it would be reported
+ // forever and the loop would spin.
for (;;) {
const conflicts = findKeybindingConflicts(overrides, isMac)
if (conflicts.length === 0) break
@@ -320,6 +327,9 @@ export function sanitizeKeybindingOverrides(
return { overrides, warnings }
}
+/** `typing` and `terminal` are expected to be DISJOINT — xterm's hidden textarea is a terminal,
+ * not a typing surface, so a caller classifying focus must not report both. If one does anyway,
+ * `typing` wins (it is checked first) and every terminal-scope command becomes unreachable. */
export interface KeyDispatchContext {
/** A real input/textarea/contentEditable has focus (xterm's hidden textarea excluded). */
typing: boolean
@@ -348,6 +358,10 @@ export function resolveCommandForKeyEvent(
if (!ctx.terminal && def.scope === 'terminal') continue
if (ctx.kanbanOpen && def.scope === 'canvas') continue
for (const binding of getEffectiveBindings(def.id, overrides, isMac)) {
+ // Hold chords belong to the dedicated hold listener, and this skip STATES that contract
+ // rather than relying on it being unreachable: `matchesShortcut` also refuses a key-null
+ // chord today (`parsed.key !== null`), so removing this line changes nothing — until the
+ // day it does.
if (isHoldChord(binding)) continue
if (matchesShortcut(e, binding, isMac)) return def.id
}