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(main): disable the menu accelerators the stand-down would otherwise hand them

enes1d5d772dc95e
diff --git a/src/main/index.ts b/src/main/index.ts
index d1f31f853..c027c51e4 100644
--- a/src/main/index.ts
+++ b/src/main/index.ts
@@ -89,7 +89,10 @@ import {
} from '../core/agents/pending-approvals'
import { setMainWindow, getMainWindow, sendToMain, closeAction, createCrashReloadPolicy } from './main-window'
import {
+ MENU_ITEM_ID_CLOSE,
+ MENU_ITEM_ID_MINIMIZE,
installKeydownIntercepts,
+ menuItemIdsToSuspend,
navigationClearsRecording,
policyStandsDown,
resolveInterceptBindings,
@@ -307,6 +310,11 @@ const currentInterceptPolicy = (): TerminalShortcutPolicy =>
settingsStore.onChange((s) => {
interceptBindings = resolveInterceptBindings(s.keybindings, interceptIsMac)
interceptPolicy = normalizeTerminalShortcutPolicy(s.terminalShortcutPolicy)
+ // A policy flip changes the stood-down answer with no focus event behind it, so the menu leg
+ // owes a sync here too. (`buildAppMenu` re-runs on this same hook and syncs at its end, so this
+ // is belt-and-braces for the ordering between the two `onChange` subscribers — cheap, and not
+ // something to leave to subscriber registration order.)
+ syncMenuForStandDown()
})
// TWO bits the RENDERER owns and main only mirrors. Both are module-level `let`s read through a
// closure, exactly like `interceptBindings` above: the window is created later and can be recreated
@@ -331,11 +339,24 @@ settingsStore.onChange((s) => {
// The two bits are cleared TOGETHER, by one function called at all three sites, because they die
// for the same reason (the page is gone) and a reset that remembered only one of them would be
// invisible until a user hit the policy path.
+//
+// ONE RULE FOR A FOURTH RESET, and it is specific to `terminalFocused`: only clear it where the
+// renderer's DOCUMENT is ending. The mirror is change-deduped and never re-asserts — it holds its
+// own `last` and reports only on a change — so clearing main's bit under a page that is still
+// alive and still focused on its terminal strands the two out of sync (`last === true` there,
+// `false` here) with no event that will ever reconcile them, and the policy is dead until the user
+// happens to click away and back. The three below are safe precisely because each one means that
+// page, and its mirror, are gone. `shortcutRecording` has no such constraint (the recorder
+// re-arms), so do not reason about the two bits interchangeably.
let shortcutRecording = false
let terminalFocused = false
const clearRendererKeyState = (): void => {
shortcutRecording = false
terminalFocused = false
+ // The menu leg follows the same state, so it must follow it here too — otherwise a crash or a
+ // reload while stood down would leave Window ▸ Minimize disabled with nothing left to re-enable
+ // it, i.e. ⌘M dead app-wide. Safe before any window exists: it no-ops without a menu.
+ syncMenuForStandDown()
}
const sshStore = new SshStore()
const ptyManager = new PtyManager()
@@ -584,7 +605,15 @@ function buildAppMenu(win: BrowserWindow): void {
{ label: 'View', submenu: viewSubmenu },
{
label: 'Window',
- submenu: [{ role: 'minimize' }, { role: 'zoom' }, { type: 'separator' }, { role: 'front' }]
+ // `id` on minimize: `syncMenuForStandDown` disables it while the intercepts are stood
+ // down, so ⌘M falls through to the terminal instead of minimizing the window. mac has no
+ // `{role:'close'}` here at all — which is why the intercept is ⌘W's only handler on mac.
+ submenu: [
+ { role: 'minimize', id: MENU_ITEM_ID_MINIMIZE },
+ { role: 'zoom' },
+ { type: 'separator' },
+ { role: 'front' }
+ ]
}
]
: [
@@ -604,9 +633,69 @@ function buildAppMenu(win: BrowserWindow): void {
},
{ label: 'View', submenu: viewSubmenu },
{ label: 'Settings', submenu: [settingsItem] },
- { label: 'Window', submenu: [{ role: 'minimize' }, { role: 'close' }] }
+ // Both ids matter off-mac: `{role:'close'}` owns Ctrl+W here, which is readline's kill-word
+ // in a terminal — a stand-down that left it enabled would CLOSE the window on a keystroke
+ // a terminal-first user meant for their shell.
+ {
+ label: 'Window',
+ submenu: [
+ { role: 'minimize', id: MENU_ITEM_ID_MINIMIZE },
+ { role: 'close', id: MENU_ITEM_ID_CLOSE }
+ ]
+ }
]
Menu.setApplicationMenu(Menu.buildFromTemplate(template))
+ // A REBUILD resets every item to `enabled: true`, and this function runs on every settings
+ // change — so without this line, a terminal-first user with a terminal focused would have ⌘M
+ // silently start minimizing again the moment they toggled any unrelated setting. The sync is
+ // idempotent and cheap; it belongs at the end of every path that installs a menu, not only at
+ // the paths where the stand-down state changes.
+ syncMenuForStandDown()
+}
+
+/**
+ * Enable/disable the menu items whose ACCELERATORS would otherwise beat the intercept stand-down.
+ *
+ * `installKeydownIntercepts` standing down means only that main stops calling `preventDefault` —
+ * the key then reaches the page, and if the page ignores it, the MENU, which is handled above the
+ * page either way. So under `terminal-first` with a terminal focused, ⌘M would still hit
+ * `{role:'minimize'}` and (Windows/Linux) Ctrl+W would still hit `{role:'close'}` — the latter
+ * closing the window on what a terminal user means as readline's kill-word, i.e. strictly worse
+ * than not having the policy at all. Disabling the item suppresses its accelerator, so the chord
+ * falls through: page → the renderer's dispatcher (terminal context under terminal-first, where
+ * nothing matches) → xterm → the PTY. That is what makes "everything reaches the shell" true for
+ * ⌘M and Ctrl+W too, not just for ⌘0 and mac's ⌘W.
+ *
+ * Called from every place the stood-down answer can change — the `ui:terminal-focus` receiver, the
+ * policy recompute in `settingsStore.onChange`, `clearRendererKeyState`, and the end of
+ * `buildAppMenu` (a rebuild resets `enabled`). NEVER per keystroke: this is a menu mutation, and
+ * `before-input-event` is the one path in the app where work is measured in keystrokes.
+ *
+ * FAIL-SAFE: a missing menu, or an item id that no longer resolves, does nothing at all. That
+ * leaves the pre-feature behaviour (menu enabled, intercepts deciding), which is the direction
+ * where the app still works — the cost is that a silent id drift is invisible, which is why both
+ * sides of the lookup use the same exported constants rather than string literals.
+ *
+ * KNOWN COST, deliberate: while a terminal is focused under terminal-first, Window ▸ Minimize is
+ * greyed out to the MOUSE as well. It re-enables the moment focus leaves the terminal (and the
+ * traffic-light button is unaffected), so this is a visible but self-healing trade. The alternative
+ * — rebuilding the whole menu without those accelerators on every focus change — costs a menu
+ * rebuild per click between the canvas and a terminal, and closes any menu the user has open.
+ *
+ * UNTESTED, and here is why: this is Electron menu mutation reached through the module-level
+ * `Menu` singleton inside `src/main/index.ts`, which no test imports (the same gap the intercept
+ * WIRING has — see `keydown-intercept.ts`'s header on why the decision lives in a pure module).
+ * The two testable parts were extracted: `policyStandsDown` (the state) and `menuItemIdsToSuspend`
+ * (the list, including the mac/non-mac asymmetry), both pinned in `keydown-intercept.test.ts`.
+ */
+function syncMenuForStandDown(): void {
+ const menu = Menu.getApplicationMenu()
+ if (!menu) return
+ const enabled = !policyStandsDown(currentInterceptPolicy(), terminalFocused)
+ for (const id of menuItemIdsToSuspend(interceptIsMac)) {
+ const item = menu.getMenuItemById(id)
+ if (item) item.enabled = enabled
+ }
}
function createWindow(): BrowserWindow {
@@ -1004,6 +1093,10 @@ app.whenReady().then(async () => {
ipcMain.on(IPC.uiTerminalFocus, (event, focused: boolean) => {
if (getMainWindow()?.webContents.id !== event.sender.id) return
terminalFocused = focused === true
+ // The mirror is change-deduped, so this fires only on a real focus transition — the right
+ // cadence for a menu mutation, and the reason the sync is here rather than on the keystroke
+ // path. It must be INSIDE the guard: a rejected sender must not move the menu either.
+ syncMenuForStandDown()
})
// Bring the window forward after a file is dropped into a terminal. On macOS a drag-drop from
diff --git a/src/main/keydown-intercept.test.ts b/src/main/keydown-intercept.test.ts
index 475b5f0f5..64de4c3e7 100644
--- a/src/main/keydown-intercept.test.ts
+++ b/src/main/keydown-intercept.test.ts
@@ -2,8 +2,11 @@ import { describe, expect, it } from 'vitest'
import { IPC } from '../shared/ipc'
import { MAIN_INTERCEPTED_COMMAND_IDS } from '../shared/keybindings'
import {
+ MENU_ITEM_ID_CLOSE,
+ MENU_ITEM_ID_MINIMIZE,
installKeydownIntercepts,
keydownIntercept,
+ menuItemIdsToSuspend,
navigationClearsRecording,
policyStandsDown,
resolveInterceptBindings,
@@ -479,6 +482,34 @@ describe('terminal-first stands every interception down while a terminal is focu
})
})
+/**
+ * Standing the INTERCEPTS down is only half of "the terminal gets the chord". The other half is the
+ * application MENU, which is handled above the page either way — so a stand-down that only stopped
+ * `preventDefault` would hand ⌘M to `{role:'minimize'}` and, on Windows/Linux, Ctrl+W to
+ * `{role:'close'}`. For a terminal-first user that is strictly WORSE than not having the policy:
+ * a Linux user's readline kill-word would close their window. `index.ts` therefore disables those
+ * menu items for exactly as long as the intercepts are stood down, and this list is which ones.
+ */
+describe('menuItemIdsToSuspend', () => {
+ it('is minimize everywhere and, off-mac, close as well', () => {
+ // The mac template has no `{role:'close'}` at all (Window ▸ Minimize / Zoom / Front), which is
+ // exactly why `keydownIntercept` is ⌘W's only handler there. Listing a close id on mac would
+ // be a no-op today and a silent lie the day someone adds the role.
+ expect(menuItemIdsToSuspend(true)).toEqual([MENU_ITEM_ID_MINIMIZE])
+ expect(menuItemIdsToSuspend(false)).toEqual([MENU_ITEM_ID_MINIMIZE, MENU_ITEM_ID_CLOSE])
+ })
+
+ // The ids are the ONLY link between `buildAppMenu`'s template and the sync that disables the
+ // items — `getMenuItemById` answers `null` for a typo, and the fail-safe there is to do nothing,
+ // which looks exactly like the feature working. Both sides import these constants, so the typo
+ // class cannot happen; this pins that they are distinct and non-empty.
+ it('the ids are distinct and non-empty', () => {
+ expect(MENU_ITEM_ID_MINIMIZE).toBeTruthy()
+ expect(MENU_ITEM_ID_CLOSE).toBeTruthy()
+ expect(MENU_ITEM_ID_MINIMIZE).not.toBe(MENU_ITEM_ID_CLOSE)
+ })
+})
+
/**
* The composition index.ts hands to the 5th parameter, as a pure function so it can be pressed.
* The BYTE-IDENTICAL claim of this whole change lives here: on the shipped default (`app-first`)
diff --git a/src/main/keydown-intercept.ts b/src/main/keydown-intercept.ts
index c257d4f2a..379ab2336 100644
--- a/src/main/keydown-intercept.ts
+++ b/src/main/keydown-intercept.ts
@@ -224,6 +224,43 @@ export function policyStandsDown(
return policy === 'terminal-first' && terminalFocused
}
+/**
+ * Menu item ids `buildAppMenu` stamps on the two roles whose ACCELERATORS survive an intercept
+ * stand-down. Exported constants used by both sides — the template that sets them and the sync that
+ * looks them up — because `getMenuItemById` answers `null` for a typo and the fail-safe there is to
+ * do nothing, which is indistinguishable from the feature working.
+ */
+export const MENU_ITEM_ID_MINIMIZE = 'window-minimize'
+export const MENU_ITEM_ID_CLOSE = 'window-close'
+
+/**
+ * PURE. Which menu items must be disabled while the intercepts are stood down.
+ *
+ * **Why the stand-down needs a menu leg at all.** `preventDefault` is the only lever this module
+ * has, and NOT calling it hands the key to the page *and*, if the page ignores it, to the menu —
+ * whose accelerators are handled above the page either way. So a stand-down that stopped at the
+ * intercept would deliver ⌘0 and (on macOS) ⌘W to the terminal, and hand ⌘M to
+ * `{role:'minimize'}` and, on Windows/Linux, Ctrl+W to `{role:'close'}`. For a terminal-first user
+ * that is strictly WORSE than not having the policy: Ctrl+W is readline's kill-word, and a Linux
+ * user pressing it would close their window. Disabling the item suppresses its accelerator, so the
+ * chord falls through to the page → the renderer's dispatcher (terminal context, terminal-first:
+ * nothing matches) → xterm → the PTY. That is what completes "under terminal-first everything
+ * reaches the shell", ⌘M included.
+ *
+ * **This is NOT the recording stand-down's story** — keep the two apart. Recording is a transient,
+ * user-initiated arming of a dialog, and its ⌘M limitation (documented on `installKeydownIntercepts`)
+ * stands: nothing here re-enables recording those chords, because a recorder that silently disabled
+ * Minimize for the duration of a keypress is a different and worse trade.
+ *
+ * mac carries only minimize: the mac template has no `{role:'close'}` at all (Window ▸ Minimize /
+ * Zoom / Front), which is exactly why `keydownIntercept` is ⌘W's only handler there.
+ */
+export function menuItemIdsToSuspend(isMac: boolean): string[] {
+ return isMac
+ ? [MENU_ITEM_ID_MINIMIZE]
+ : [MENU_ITEM_ID_MINIMIZE, MENU_ITEM_ID_CLOSE]
+}
+
/** The renderer channel a claimed action is forwarded on. */
export function keydownInterceptChannel(action: KeydownInterceptAction): string {
if (action === 'toggle-markdown') return IPC.appToggleMarkdown
@@ -279,6 +316,18 @@ export interface KeydownInterceptTarget {
* assert. Both are checked BEFORE `preventDefault` for the same reason: a claimed chord never
* reaches the page at all, and the page is exactly who terminal-first stands down FOR.
*
+ * **The policy stand-down DOES get the menu leg the recording one does not** — the two cases are
+ * different and the paragraph above stays true for recording. `index.ts`'s `syncMenuForStandDown`
+ * disables `{role:'minimize'}` (and, off-mac, `{role:'close'}`) for exactly as long as
+ * `isStoodDown` is true, via the ids in `menuItemIdsToSuspend` below. It has to: not calling
+ * `preventDefault` hands the key to the page and, failing that, to the menu, so without the menu
+ * leg a terminal-first user's ⌘M would minimize the window and their Ctrl+W — readline's kill-word
+ * — would CLOSE it on Windows/Linux, which is strictly worse than not having the policy. With the
+ * items disabled the chord completes the trip it was promised: page → the renderer's dispatcher
+ * (terminal context, terminal-first, nothing matches) → xterm → the PTY. Recording keeps the
+ * limitation because a recorder that greyed out Minimize for the duration of a keypress is a worse
+ * trade than an unrecordable ⌘M.
+ *
* **The stand-down leg cannot be more reliable than the mirror behind it**, and the mirror is a
* renderer reporting over fire-and-forget IPC. Every uncertainty therefore resolves to NOT stood
* down (intercepts on = the pre-feature app): `index.ts` starts `terminalFocused` false and every
diff --git a/src/renderer/lib/terminalFocusMirror.test.ts b/src/renderer/lib/terminalFocusMirror.test.ts
index 0ea064d13..dcb13a49e 100644
--- a/src/renderer/lib/terminalFocusMirror.test.ts
+++ b/src/renderer/lib/terminalFocusMirror.test.ts
@@ -166,11 +166,20 @@ describe('installTerminalFocusMirror', () => {
expect(report).not.toHaveBeenCalled()
})
- it('stops completely on teardown — no listeners, no timer', async () => {
+ it('releases the bit on teardown, then stops completely — no listeners, no timer', async () => {
const term = terminalTextarea()
+ term.focus()
stop = installTerminalFocusMirror({ report })
+ await settle()
+ report.mockClear()
+
+ // Teardown reports `false` on its way out rather than leaving main holding a `true` nobody
+ // owns any more. Main's three page-death resets cover the page VANISHING; this covers the
+ // ordinary case where the page lives and only this mirror stops. A remount re-reports from
+ // scratch (`last` starts null), so nothing is lost by releasing here.
stop()
stop = null
+ expect(report.mock.calls).toEqual([[false]])
report.mockClear()
term.focus()
diff --git a/src/renderer/lib/terminalFocusMirror.ts b/src/renderer/lib/terminalFocusMirror.ts
index 93f8d16ab..d8c156305 100644
--- a/src/renderer/lib/terminalFocusMirror.ts
+++ b/src/renderer/lib/terminalFocusMirror.ts
@@ -123,6 +123,11 @@ export function installTerminalFocusMirror(opts: TerminalFocusMirrorOptions): ()
push(focusedNow())
return () => {
+ // Release the bit on the way out rather than leaving main holding a `true` that nobody owns
+ // any more. Main's resets cover the page VANISHING; this covers the ordinary case where the
+ // page lives and only this mirror stops. Before `stopped`, or `push` would refuse it. A
+ // remount re-reports from scratch (`last` starts null), so nothing is lost.
+ push(false)
stopped = true
window.removeEventListener('focusin', onFocusIn)
window.removeEventListener('focusout', onFocusOut)