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): terminal-first policy in the dispatch context

enesc4ebecd3d992
diff --git a/src/renderer/lib/globalKeybindings.ts b/src/renderer/lib/globalKeybindings.ts
index 36a0044e9..45807749d 100644
--- a/src/renderer/lib/globalKeybindings.ts
+++ b/src/renderer/lib/globalKeybindings.ts
@@ -65,7 +65,8 @@ export interface GlobalKeydownDeps {
export function dispatchGlobalKeydown(e: GlobalKeyEvent, deps: GlobalKeydownDeps): boolean {
// A child handler (find bar, dialogs, terminal) that already claimed the key wins outright.
if (e.defaultPrevented) return false
- const ctx = keyDispatchContextFor(deps.activeElement(), deps.kanbanOpen())
+ // Task 2 threads the real policy (a deps thunk); app-first until then, which is today's behavior.
+ const ctx = keyDispatchContextFor(deps.activeElement(), deps.kanbanOpen(), false)
// Keyed dictation predates the registry and may deliberately collide with a default; it
// keeps first claim, but only in plain app focus (its old guard blocked inputs AND xterm).
if (!ctx.typing && !ctx.terminal && !ctx.kanbanOpen && deps.gestures.keyedDictation(e)) return true
diff --git a/src/renderer/lib/keyContext.test.ts b/src/renderer/lib/keyContext.test.ts
index a780071a6..acbc18790 100644
--- a/src/renderer/lib/keyContext.test.ts
+++ b/src/renderer/lib/keyContext.test.ts
@@ -41,15 +41,29 @@ describe('isTypingTarget', () => {
describe('keyDispatchContextFor', () => {
it('typing and terminal are disjoint by construction', () => {
- expect(keyDispatchContextFor(el('TEXTAREA', { classes: [XTERM_INPUT_CLASS] }), false)).toEqual({
+ expect(
+ keyDispatchContextFor(el('TEXTAREA', { classes: [XTERM_INPUT_CLASS] }), false, false)
+ ).toEqual({
typing: false,
terminal: true,
- kanbanOpen: false
+ kanbanOpen: false,
+ terminalFirst: false
})
- expect(keyDispatchContextFor(el('INPUT'), true)).toEqual({
+ expect(keyDispatchContextFor(el('INPUT'), true, false)).toEqual({
typing: true,
terminal: false,
- kanbanOpen: true
+ kanbanOpen: true,
+ terminalFirst: false
+ })
+ })
+ it('carries the terminal-first policy through as given', () => {
+ expect(
+ keyDispatchContextFor(el('TEXTAREA', { classes: [XTERM_INPUT_CLASS] }), false, true)
+ ).toEqual({
+ typing: false,
+ terminal: true,
+ kanbanOpen: false,
+ terminalFirst: true
})
})
})
diff --git a/src/renderer/lib/keyContext.ts b/src/renderer/lib/keyContext.ts
index 1cce5771e..2cfadb6d5 100644
--- a/src/renderer/lib/keyContext.ts
+++ b/src/renderer/lib/keyContext.ts
@@ -39,9 +39,12 @@ export function isTypingTarget(el: ContextElement | null): boolean {
return el.isContentEditable === true
}
+/** `terminalFirst` is REQUIRED, not defaulted: it is a user policy, and a default here would let
+ * a call site silently answer "app-first" for a user who chose otherwise. Every caller decides. */
export function keyDispatchContextFor(
el: ContextElement | null,
- kanbanOpen: boolean
+ kanbanOpen: boolean,
+ terminalFirst: boolean
): KeyDispatchContext {
- return { typing: isTypingTarget(el), terminal: isTerminalTarget(el), kanbanOpen }
+ return { typing: isTypingTarget(el), terminal: isTerminalTarget(el), kanbanOpen, terminalFirst }
}
diff --git a/src/shared/keybindings.test.ts b/src/shared/keybindings.test.ts
index 680e838d9..72bb5605f 100644
--- a/src/shared/keybindings.test.ts
+++ b/src/shared/keybindings.test.ts
@@ -9,6 +9,7 @@ import {
findKeybindingConflicts,
sanitizeKeybindingOverrides,
resolveCommandForKeyEvent,
+ normalizeTerminalShortcutPolicy,
MAIN_INTERCEPTED_COMMAND_IDS,
findMainInterceptShadowing
} from './keybindings'
@@ -318,8 +319,12 @@ describe('sanitizeKeybindingOverrides', () => {
const ev = (over: Partial<ShortcutKeyEvent>): ShortcutKeyEvent => ({
metaKey: false, ctrlKey: false, shiftKey: false, altKey: false, key: '', ...over
})
-const ctx = (over: Partial<{ typing: boolean; terminal: boolean; kanbanOpen: boolean }> = {}) => ({
- typing: false, terminal: false, kanbanOpen: false, ...over
+const ctx = (
+ over: Partial<{
+ typing: boolean; terminal: boolean; kanbanOpen: boolean; terminalFirst: boolean
+ }> = {}
+) => ({
+ typing: false, terminal: false, kanbanOpen: false, terminalFirst: false, ...over
})
describe('resolveCommandForKeyEvent', () => {
@@ -441,3 +446,37 @@ describe('findMainInterceptShadowing', () => {
expect(MAIN_INTERCEPTED_COMMAND_IDS).toEqual(['node.close', 'node.toggleMarkdown'])
})
})
+
+describe('normalizeTerminalShortcutPolicy', () => {
+ it('defaults everything but the literal terminal-first to app-first', () => {
+ expect(normalizeTerminalShortcutPolicy('terminal-first')).toBe('terminal-first')
+ expect(normalizeTerminalShortcutPolicy('app-first')).toBe('app-first')
+ expect(normalizeTerminalShortcutPolicy(undefined)).toBe('app-first')
+ expect(normalizeTerminalShortcutPolicy('shell-first')).toBe('app-first')
+ })
+})
+
+describe('resolver under terminal-first', () => {
+ const term = (terminalFirst: boolean) => ({
+ typing: false, terminal: true, kanbanOpen: false, terminalFirst
+ })
+ it('app-first keeps today: allowInTerminal app commands fire over a terminal', () => {
+ expect(resolveCommandForKeyEvent(ev({ metaKey: true, key: 'k' }), term(false), {}, true))
+ .toBe('app.commandPalette')
+ })
+ it('terminal-first blocks allowInTerminal app commands', () => {
+ expect(resolveCommandForKeyEvent(ev({ metaKey: true, key: 'k' }), term(true), {}, true))
+ .toBeNull()
+ expect(resolveCommandForKeyEvent(ev({ metaKey: true, shiftKey: true, key: 'b' }), term(true), {}, true))
+ .toBeNull()
+ })
+ it('terminal-first keeps terminal-scope commands alive', () => {
+ expect(resolveCommandForKeyEvent(ev({ metaKey: true, key: 'f' }), term(true), {}, true))
+ .toBe('terminal.find')
+ })
+ it('terminalFirst is inert outside terminal focus', () => {
+ const app = { typing: false, terminal: false, kanbanOpen: false, terminalFirst: true }
+ expect(resolveCommandForKeyEvent(ev({ metaKey: true, key: 'k' }), app, {}, true))
+ .toBe('app.commandPalette')
+ })
+})
diff --git a/src/shared/keybindings.ts b/src/shared/keybindings.ts
index 1048d5ba4..d7da363bd 100644
--- a/src/shared/keybindings.ts
+++ b/src/shared/keybindings.ts
@@ -363,6 +363,18 @@ export function sanitizeKeybindingOverrides(
return { overrides, warnings }
}
+/** Who wins while an xterm has focus. 'app-first' (default): allowInTerminal app commands still
+ * fire (today's behavior). 'terminal-first': ONLY scope:'terminal' commands fire; everything
+ * else — allowInTerminal app commands, the main-intercepted ⌘W/⌘M, the registry-less gestures —
+ * reaches the PTY. NOTE: the naive reading "terminal-first = the existing allowInTerminal gate"
+ * is vacuous here — that gate IS app-first; this flag tightens past it. */
+export type TerminalShortcutPolicy = 'app-first' | 'terminal-first'
+
+/** Unknown/legacy values read as app-first (the safe, current-behavior direction). */
+export function normalizeTerminalShortcutPolicy(v: unknown): TerminalShortcutPolicy {
+ return v === 'terminal-first' ? 'terminal-first' : 'app-first'
+}
+
/** `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. */
@@ -373,6 +385,9 @@ export interface KeyDispatchContext {
terminal: boolean
/** The kanban board is open for the active project. */
kanbanOpen: boolean
+ /** terminal-first policy is active: while `terminal` is true, only scope:'terminal' commands
+ * may resolve (allowInTerminal is an app-first concept). Inert when `terminal` is false. */
+ terminalFirst: boolean
}
/** Pure dispatch core: the first registry command (source order) whose effective bindings
@@ -398,7 +413,9 @@ export function resolveCommandForKeyEvent(
// `{"speech.dictation": ["Cmd+0"]}` would silently kill zoom-to-100%.
if (def.id === 'speech.dictation') continue
if (ctx.typing && !def.allowWhileTyping) continue
- if (ctx.terminal && !(def.scope === 'terminal' || def.allowInTerminal)) continue
+ if (ctx.terminal) {
+ if (def.scope !== 'terminal' && (ctx.terminalFirst || !def.allowInTerminal)) continue
+ }
if (!ctx.terminal && def.scope === 'terminal') continue
if (ctx.kanbanOpen && def.scope === 'canvas') continue
for (const binding of getEffectiveBindings(def.id, overrides, isMac)) {
diff --git a/src/shared/types.ts b/src/shared/types.ts
index b0cb1d7e4..7490c588e 100644
--- a/src/shared/types.ts
+++ b/src/shared/types.ts
@@ -2,7 +2,7 @@
import { DEFAULT_WORKTREE_PATH_TEMPLATE } from './worktree'
import type { CloneProgress } from './clone-url'
-import type { KeybindingOverrides } from './keybindings'
+import type { KeybindingOverrides, TerminalShortcutPolicy } from './keybindings'
import type { NormalizedAgentEvent } from './agents/normalize'
import type { AgentId, AgentPermissionMode, BuiltinAgentId, PromptInjectionMode } from './agents/config'
import type { AgentMessageDeliverRequest, AgentMessageReply } from './agents/agent-messaging'
@@ -1206,9 +1206,17 @@ export interface Settings {
* entries are dropped with a console warning at read time (sanitizeKeybindingOverrides).
* Optional and deliberately not in DEFAULT_SETTINGS: absent simply means "no overrides". */
keybindings?: KeybindingOverrides
+ /** Who wins while a terminal has keyboard focus: 'app-first' (default) lets allowInTerminal
+ * app commands fire over an xterm; 'terminal-first' reserves every chord but the terminal's
+ * own (find, copy) for the shell/TUI — including ⌘W/⌘M and the zoom/project-jump gestures. */
+ terminalShortcutPolicy: TerminalShortcutPolicy
+ /** Command ids whose "this chord was captured from your terminal" notice has been shown
+ * (app-first only, once per command). Optional and absent from DEFAULT_SETTINGS: absent
+ * means none seen. Lives in settings, not localStorage, so Server Edition shares it. */
+ seenShortcutCaptureNotices?: string[]
/** Per-node hook identity enforcement (src/core/agents/node-identity-policy.ts).
*
- * One of the two optional keys in this interface, and deliberately so: it is a TRI-state, and the two
+ * One of the three optional keys in this interface, and deliberately so: it is a TRI-state, and the two
* non-default states are opposite escape hatches. Absent (the default — it is not in
* DEFAULT_SETTINGS) follows `NODE_IDENTITY_STRICT_AFTER`, so the rollout has one schedule for
* everybody. `true` opts in to strict enforcement before that date. `false` keeps the warning
@@ -1264,6 +1272,9 @@ export const DEFAULT_SETTINGS: Settings = {
commitAgent: 'claude',
commitAgentCommand: '',
commitExtraPrompt: '',
+ // 'app-first' reproduces today's dispatch bit-for-bit: allowInTerminal app commands keep
+ // firing over a focused xterm. Opting into 'terminal-first' is the user's call, never ours.
+ terminalShortcutPolicy: 'app-first',
seenShortcuts: false,
seenOnboarding: false,
notifyOnClaudeDone: true,