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.

feat(keybindings): per-command binding validation with canonical output

enes8b9a2535951a
diff --git a/src/shared/keybindings.test.ts b/src/shared/keybindings.test.ts
index 064119a4e..1acafb1a3 100644
--- a/src/shared/keybindings.test.ts
+++ b/src/shared/keybindings.test.ts
@@ -1,5 +1,10 @@
import { describe, it, expect } from 'vitest'
-import { COMMAND_DEFINITIONS, COMMANDS_BY_ID, isCommandId } from './keybindings'
+import {
+ COMMAND_DEFINITIONS,
+ COMMANDS_BY_ID,
+ isCommandId,
+ normalizeBindingForCommand
+} from './keybindings'
import { parseShortcut } from './shortcut'
describe('registry invariants', () => {
@@ -37,3 +42,54 @@ describe('registry invariants', () => {
expect(isCommandId('node.selfDestruct')).toBe(false)
})
})
+
+const def = (id: string) => {
+ const d = COMMANDS_BY_ID.get(id as never)
+ if (!d) throw new Error(`missing ${id}`)
+ return d
+}
+
+describe('normalizeBindingForCommand', () => {
+ it('canonicalizes token order and casing', () => {
+ const r = normalizeBindingForCommand(def('node.newTerminal'), 'shift+t+cmd', true)
+ expect(r).toEqual({ ok: true, value: 'Cmd+Shift+T' })
+ })
+ it('rejects a chord with no modifier for a normal command', () => {
+ const r = normalizeBindingForCommand(def('node.newTerminal'), 'T', true)
+ expect(r.ok).toBe(false)
+ })
+ it('rejects shift-only chords (stealing typed text)', () => {
+ expect(normalizeBindingForCommand(def('node.newTerminal'), 'Shift+T', true).ok).toBe(false)
+ })
+ it('allows safe bare keys only with allowBareKey', () => {
+ expect(normalizeBindingForCommand(def('canvas.deleteSelection'), 'Delete', true)).toEqual({
+ ok: true, value: 'Delete'
+ })
+ expect(normalizeBindingForCommand(def('canvas.deleteSelection'), 'X', true).ok).toBe(false)
+ expect(normalizeBindingForCommand(def('node.newTerminal'), 'Delete', true).ok).toBe(false)
+ })
+ it('allows hold chords only with allowHoldChord', () => {
+ expect(normalizeBindingForCommand(def('speech.dictation'), 'Cmd+Alt', true)).toEqual({
+ ok: true, value: 'Cmd+Alt'
+ })
+ expect(normalizeBindingForCommand(def('node.newTerminal'), 'Cmd+Alt', true).ok).toBe(false)
+ })
+ it('rejects Cmd combined with literal Ctrl on non-mac', () => {
+ expect(normalizeBindingForCommand(def('node.newTerminal'), 'Cmd+Ctrl+T', false).ok).toBe(false)
+ expect(normalizeBindingForCommand(def('node.newTerminal'), 'Cmd+Ctrl+T', true).ok).toBe(true)
+ })
+ it('rejects garbage', () => {
+ expect(normalizeBindingForCommand(def('node.newTerminal'), '', true).ok).toBe(false)
+ expect(normalizeBindingForCommand(def('node.newTerminal'), '+++', true).ok).toBe(false)
+ })
+ it('every default in the registry survives its own validation', () => {
+ for (const d of COMMAND_DEFINITIONS) {
+ for (const isMac of [true, false]) {
+ for (const s of d.defaultBindings[isMac ? 'darwin' : 'other']) {
+ const r = normalizeBindingForCommand(d, s, isMac)
+ expect(r, `${d.id}: ${s} (isMac=${isMac})`).toEqual({ ok: true, value: s })
+ }
+ }
+ }
+ })
+})
diff --git a/src/shared/keybindings.ts b/src/shared/keybindings.ts
index 513f7ccac..d559c4334 100644
--- a/src/shared/keybindings.ts
+++ b/src/shared/keybindings.ts
@@ -129,3 +129,45 @@ export const COMMANDS_BY_ID: ReadonlyMap<CommandId, CommandDefinition> = new Map
export function isCommandId(v: string): v is CommandId {
return COMMANDS_BY_ID.has(v as CommandId)
}
+
+/** Modifier-less keys that are safe to bind bare — they don't type a character. */
+export const SAFE_BARE_KEYS: ReadonlySet<string> = new Set([
+ 'DELETE', 'BACKSPACE', 'ESCAPE', 'ENTER', 'TAB',
+ 'ARROWUP', 'ARROWDOWN', 'ARROWLEFT', 'ARROWRIGHT', 'PAGEUP', 'PAGEDOWN',
+ 'F1', 'F2', 'F3', 'F4', 'F5', 'F6', 'F7', 'F8', 'F9', 'F10', 'F11', 'F12'
+])
+
+export type NormalizedBinding = { ok: true; value: string } | { ok: false; error: string }
+
+/** Parse + validate one binding string against a command's flags; returns the canonical
+ * serialized form on success. The single validation path for defaults (pinned by a registry
+ * test), Settings-recorder captures, and hand-edited settings.json overrides. */
+export function normalizeBindingForCommand(
+ def: CommandDefinition,
+ raw: string,
+ isMac: boolean
+): NormalizedBinding {
+ const p = parseShortcut(raw)
+ const hasAnyToken = p.cmd || p.ctrl || p.alt || p.shift || p.key !== null
+ if (!hasAnyToken) return { ok: false, error: 'Use a shortcut like Cmd+K or Cmd+Shift+P.' }
+ if (!isMac && p.cmd && p.ctrl) {
+ return { ok: false, error: 'Cmd already means Ctrl on this platform; use one of them.' }
+ }
+ if (p.key === null) {
+ if (!def.allowHoldChord) {
+ return { ok: false, error: 'This command needs a key, not a modifier-only chord.' }
+ }
+ if (!p.cmd && !p.ctrl && !p.alt) {
+ return { ok: false, error: 'A hold chord needs at least one non-shift modifier.' }
+ }
+ return { ok: true, value: serializeShortcut(p) }
+ }
+ const hasStrongModifier = p.cmd || p.ctrl || p.alt
+ if (!hasStrongModifier) {
+ if (p.shift) return { ok: false, error: 'Shift-only shortcuts would steal typed text.' }
+ if (!def.allowBareKey || !SAFE_BARE_KEYS.has(p.key)) {
+ return { ok: false, error: 'Include a modifier key.' }
+ }
+ }
+ return { ok: true, value: serializeShortcut(p) }
+}