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): override sanitization shared by load path and settings UI

enes315f71e43242
diff --git a/src/shared/keybindings.test.ts b/src/shared/keybindings.test.ts
index 954efe3d1..e8f054ec9 100644
--- a/src/shared/keybindings.test.ts
+++ b/src/shared/keybindings.test.ts
@@ -6,7 +6,8 @@ import {
normalizeBindingForCommand,
getEffectiveBindings,
bindingIdentity,
- findKeybindingConflicts
+ findKeybindingConflicts,
+ sanitizeKeybindingOverrides
} from './keybindings'
import { parseShortcut } from './shortcut'
@@ -167,3 +168,61 @@ describe('findKeybindingConflicts', () => {
).toEqual([])
})
})
+
+describe('sanitizeKeybindingOverrides', () => {
+ it('passes through a clean override map', () => {
+ const r = sanitizeKeybindingOverrides({ 'node.newTerminal': ['Cmd+Shift+T'] }, true)
+ expect(r).toEqual({ overrides: { 'node.newTerminal': ['Cmd+Shift+T'] }, warnings: [] })
+ })
+ it('canonicalizes spellings', () => {
+ const r = sanitizeKeybindingOverrides({ 'node.newTerminal': ['shift+cmd+t'] }, true)
+ expect(r.overrides).toEqual({ 'node.newTerminal': ['Cmd+Shift+T'] })
+ })
+ it('drops unknown ids with a warning, keeps the rest', () => {
+ const r = sanitizeKeybindingOverrides(
+ { 'node.selfDestruct': ['Cmd+X'], 'canvas.undo': [] }, true
+ )
+ expect(r.overrides).toEqual({ 'canvas.undo': [] })
+ expect(r.warnings.some((w) => w.includes('node.selfDestruct'))).toBe(true)
+ })
+ it('drops an invalid binding string but keeps valid siblings', () => {
+ const r = sanitizeKeybindingOverrides({ 'node.newTerminal': ['Cmd+Shift+T', 'T'] }, true)
+ expect(r.overrides).toEqual({ 'node.newTerminal': ['Cmd+Shift+T'] })
+ expect(r.warnings.length).toBe(1)
+ })
+ it('a non-empty list that is entirely invalid falls back to defaults, not disabled', () => {
+ const r = sanitizeKeybindingOverrides({ 'node.newTerminal': ['T', '+++'] }, true)
+ expect(r.overrides).toEqual({})
+ expect(r.warnings.length).toBeGreaterThan(0)
+ })
+ it('an explicit empty array still means disabled', () => {
+ expect(sanitizeKeybindingOverrides({ 'canvas.undo': [] }, true).overrides).toEqual({
+ 'canvas.undo': []
+ })
+ })
+ it('strips BOTH sides of a conflicting override pair, naming the titles', () => {
+ const r = sanitizeKeybindingOverrides(
+ { 'canvas.fitAll': ['Cmd+P'], 'canvas.groupSelection': ['Cmd+P'] }, true
+ )
+ expect(r.overrides).toEqual({})
+ expect(r.warnings.some((w) =>
+ w.includes('Fit all nodes in view') && w.includes('Group selection')
+ )).toBe(true)
+ })
+ it('an override conflicting with a DEFAULT strips only the override', () => {
+ const r = sanitizeKeybindingOverrides({ 'canvas.fitAll': ['Cmd+K'] }, true)
+ expect(r.overrides).toEqual({})
+ expect(r.warnings.some((w) => w.includes('Command palette'))).toBe(true)
+ })
+ 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({})
+ })
+ it('is a fixpoint: sanitizing its own output adds nothing', () => {
+ const once = sanitizeKeybindingOverrides(
+ { 'canvas.fitAll': ['Cmd+P'], 'node.newTerminal': ['Cmd+Shift+T'] }, true
+ )
+ const twice = sanitizeKeybindingOverrides(once.overrides, true)
+ expect(twice).toEqual({ overrides: once.overrides, warnings: [] })
+ })
+})
diff --git a/src/shared/keybindings.ts b/src/shared/keybindings.ts
index dc2ade2d5..3ca7fa197 100644
--- a/src/shared/keybindings.ts
+++ b/src/shared/keybindings.ts
@@ -253,3 +253,63 @@ export function findKeybindingConflicts(
}
return conflicts
}
+
+/** 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). */
+export function sanitizeKeybindingOverrides(
+ raw: unknown,
+ isMac: boolean
+): { overrides: KeybindingOverrides; warnings: string[] } {
+ const warnings: string[] = []
+ const overrides: Partial<Record<CommandId, string[]>> = {}
+ if (typeof raw !== 'object' || raw === null || Array.isArray(raw)) {
+ if (raw !== undefined && raw !== null) warnings.push('keybindings must be an object; ignored.')
+ return { overrides, warnings }
+ }
+ for (const [id, value] of Object.entries(raw)) {
+ if (!isCommandId(id)) {
+ warnings.push(`Unknown command "${id}" ignored.`)
+ continue
+ }
+ if (!Array.isArray(value) || value.some((v) => typeof v !== 'string')) {
+ warnings.push(`Bindings for "${id}" must be an array of strings; ignored.`)
+ continue
+ }
+ const def = COMMANDS_BY_ID.get(id)!
+ const normalized: string[] = []
+ for (const s of value as string[]) {
+ const r = normalizeBindingForCommand(def, s, isMac)
+ if (r.ok) {
+ if (!normalized.includes(r.value)) normalized.push(r.value)
+ } else {
+ warnings.push(`Binding "${s}" for "${id}" ignored: ${r.error}`)
+ }
+ }
+ // A list that HAD entries and lost every one of them falls back to defaults rather than
+ // being stored as `[]`: `[]` means "disabled", so a typo in a hand-edited settings.json
+ // would silently turn a working shortcut off. An explicit `[]` in the input still disables.
+ if (value.length > 0 && normalized.length === 0) {
+ warnings.push(`All bindings for "${id}" were invalid; falling back to defaults.`)
+ continue
+ }
+ overrides[id] = normalized
+ }
+ // Strip overridden participants of any remaining conflict until none are left. Bounded:
+ // each pass removes at least one override or exits.
+ for (;;) {
+ const conflicts = findKeybindingConflicts(overrides, isMac)
+ if (conflicts.length === 0) break
+ for (const conflict of conflicts) {
+ const titles = conflict.commandIds
+ .map((cid) => COMMANDS_BY_ID.get(cid)?.title ?? cid)
+ .join(', ')
+ warnings.push(`Conflicting shortcut ${conflict.binding} between: ${titles}. Overrides dropped.`)
+ for (const cid of conflict.commandIds) {
+ if (overrides[cid] !== undefined) delete overrides[cid]
+ }
+ }
+ }
+ return { overrides, warnings }
+}