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): clear the recording bit on reload, and stop siblings clearing it

enes879c69bb4a53
diff --git a/src/main/index.ts b/src/main/index.ts
index ee3e87938..8694d9251 100644
--- a/src/main/index.ts
+++ b/src/main/index.ts
@@ -90,6 +90,7 @@ import {
import { setMainWindow, getMainWindow, sendToMain, closeAction, createCrashReloadPolicy } from './main-window'
import {
installKeydownIntercepts,
+ navigationClearsRecording,
resolveInterceptBindings,
type KeydownInterceptBindings
} from './keydown-intercept'
@@ -297,9 +298,12 @@ settingsStore.onChange((s) => {
// reaches the page, so the recorder's own preventDefault cannot save it.
// A module-level `let` read through a closure, exactly like `interceptBindings` above: the window
// is created later and can be recreated (macOS dock reopen), so nothing may capture a value.
-// FAIL-SAFE DIRECTION: every uncertainty resolves to `false` (intercepts on). A renderer that
-// crashes or a window that closes while armed clears it — see `createWindow` — because the
-// alternative is ⌘W/⌘M/⌘0 dead app-wide with no component left alive to release the bit.
+// FAIL-SAFE DIRECTION: every uncertainty resolves to `false` (intercepts on). The alternative is
+// ⌘W/⌘M/⌘0 dead app-wide with no component left alive to release the bit, so EVERY way the page
+// that armed it can stop existing owes a clear — `createWindow` wires three (window `closed`,
+// `render-process-gone`, and a main-frame navigation, i.e. ⌘R). That is three known paths, not a
+// proof of exhaustiveness: a fourth would show up as "⌘W stopped working after I did X", so add
+// its reset beside the others rather than assuming this list is closed.
let shortcutRecording = false
const clearShortcutRecording = (): void => {
shortcutRecording = false
@@ -708,6 +712,16 @@ function createWindow(): BrowserWindow {
// are the user's effective `node.toggleMarkdown` / `node.close` bindings (⌘0 is not remappable).
installKeydownIntercepts(win, currentInterceptBindings, interceptIsMac, () => shortcutRecording)
+ // The THIRD way the page that armed a recorder can go away: a reload. The View menu above
+ // restores `{role:'reload'}` / `{role:'forceReload'}`, and ⌘R/⌘⇧R are accelerators — handled
+ // above the page, so the recorder cannot preventDefault its way out of one, and a same-process
+ // reload fires neither `closed` nor `render-process-gone`. `navigationClearsRecording` is the
+ // (tested) filter: a same-document navigation is the SAME page with the recorder still armed,
+ // and a subframe is not this page at all.
+ win.webContents.on('did-start-navigation', (details) => {
+ if (navigationClearsRecording(details)) clearShortcutRecording()
+ })
+
// Open external links in the system browser — only safe schemes (no file://, no custom
// protocol handlers). Reachable from remotely-fetched announcement URLs and rendered
// markdown links, so the allowlist mirrors the shellOpenExternal IPC handler.
diff --git a/src/main/keydown-intercept.test.ts b/src/main/keydown-intercept.test.ts
index 24501bfa1..b39b5a0a5 100644
--- a/src/main/keydown-intercept.test.ts
+++ b/src/main/keydown-intercept.test.ts
@@ -4,6 +4,7 @@ import { MAIN_INTERCEPTED_COMMAND_IDS } from '../shared/keybindings'
import {
installKeydownIntercepts,
keydownIntercept,
+ navigationClearsRecording,
resolveInterceptBindings,
type KeydownInterceptBindings,
type KeydownInterceptInput,
@@ -359,6 +360,39 @@ describe('an armed shortcut recorder suspends every interception', () => {
expect(seam.prevented).toEqual(['KeyW'])
})
+ // THE HOLE REVIEW FOUND. The app's own View menu restores `{role:'reload'}` /
+ // `{role:'forceReload'}`, and ⌘R/⌘⇧R are ACCELERATORS — they fire above the page, so the
+ // recorder's preventDefault cannot stop them. A same-process reload fires no React unmount, no
+ // window `closed` and no `render-process-gone`, so every release path this feature had missed
+ // it and the bit stayed true forever: ⌘W/⌘M/⌘0 dead app-wide with nothing left to clear them.
+ // This mirrors index.ts's wiring exactly — the listener runs the same predicate.
+ it('a reload while armed restores interception; an in-page navigation does not', () => {
+ let recording = true
+ const seam = install(() => DEFAULTS, true, () => recording)
+ const onNavigation = (d: { isMainFrame: boolean; isSameDocument: boolean }): void => {
+ if (navigationClearsRecording(d)) recording = false
+ }
+ seam.fire({ meta: true, key: 'w', code: 'KeyW' })
+ expect(seam.sent).toEqual([])
+ // pushState / a fragment jump is not a page change: the recorder is still mounted and armed,
+ // so disarming here would suppress the recorder instead of the intercept.
+ onNavigation({ isMainFrame: true, isSameDocument: true })
+ seam.fire({ meta: true, key: 'w', code: 'KeyW' })
+ expect(seam.sent).toEqual([])
+ onNavigation({ isMainFrame: true, isSameDocument: false }) // ⌘R
+ seam.fire({ meta: true, key: 'w', code: 'KeyW' })
+ expect(seam.sent).toEqual([IPC.appCloseNode])
+ })
+
+ it('only a real main-frame document navigation clears the bit', () => {
+ expect(navigationClearsRecording({ isMainFrame: true, isSameDocument: false })).toBe(true)
+ expect(navigationClearsRecording({ isMainFrame: true, isSameDocument: true })).toBe(false)
+ // A subframe navigating is not this page going away — an iframe/ad reloading itself must not
+ // silently re-arm the app's shortcuts under a recorder that is still listening.
+ expect(navigationClearsRecording({ isMainFrame: false, isSameDocument: false })).toBe(false)
+ expect(navigationClearsRecording({ isMainFrame: false, isSameDocument: true })).toBe(false)
+ })
+
it('is read per event, not captured at install time', () => {
// The bit lives in a module-level `let` in index.ts that the renderer flips over IPC long
// after the window was created; a captured boolean would make the whole feature inert.
diff --git a/src/main/keydown-intercept.ts b/src/main/keydown-intercept.ts
index 5c49d444f..498cf4a27 100644
--- a/src/main/keydown-intercept.ts
+++ b/src/main/keydown-intercept.ts
@@ -7,15 +7,22 @@ import { matchesShortcut } from '../shared/shortcut'
* pair whose renderer half is `src/renderer/lib/zoomShortcut.ts` (same shape: a key-state-only
* input, an action-or-null out, and the refusals as the point of the module).
*
- * **Why the main process gets a say at all.** We never call `Menu.setApplicationMenu`, so Electron
- * installs its DEFAULT application menu, and that menu already claims all three of these chords:
+ * **Why the main process gets a say at all.** A menu accelerator is handled *before* the page sees
+ * the key, so a renderer-side listener for one would simply never run on the desktop.
+ * `before-input-event`'s `preventDefault` suppresses the menu item AND the page event, which is why
+ * each claimed chord has to forward its intent to the renderer over IPC itself rather than letting
+ * the key through. The three chords, and who claims them TODAY:
*
- * ⌘M → Window ▸ Minimize ⌘W → Window ▸ Close ⌘0 → View ▸ Actual Size (`resetZoom`)
+ * ⌘M → Window ▸ Minimize — a live accelerator on **every** platform (`{role:'minimize'}`).
+ * ⌘W → Window ▸ Close — a live accelerator on **Windows/Linux only**; the mac template has no
+ * `{role:'close'}`, so on macOS this module is now ⌘W's only handler.
+ * ⌘0 → not in any menu at all any more; this module is its only handler, which is exactly why
+ * the View menu's Fit View item deliberately carries NO accelerator.
*
- * A menu accelerator is handled *before* the page sees the key, so a renderer-side listener for any
- * of them would simply never run on the desktop. `before-input-event`'s `preventDefault` suppresses
- * the menu item AND the page event, which is why each claimed chord has to forward its intent to
- * the renderer over IPC itself rather than letting the key through.
+ * (This paragraph used to say "we never call `Menu.setApplicationMenu`, so Electron installs its
+ * DEFAULT menu". That has been false since `buildAppMenu` landed in `index.ts`. The menu is OURS
+ * now, so the list above is a fact about `buildAppMenu` — check it there, not against Electron's
+ * defaults, and update this comment when you edit that template.)
*
* **Why it is pure and lives here rather than in the callback.** Deleting a branch from an inline
* callback breaks nothing and throws nothing — the shortcut just quietly starts minimizing the
@@ -28,10 +35,14 @@ import { matchesShortcut } from '../shared/shortcut'
* **This module stays the closed list of main-intercepted chords.** ⌘M and ⌘W are now resolved
* from the keybinding registry (`node.toggleMarkdown` / `node.close`), so the USER decides which
* chord they are — but nothing else is intercepted here, because everything else reaches the
- * renderer's dispatcher on its own. That closed list is ALSO written down in
+ * renderer's dispatcher on its own. The **remappable** half of that list is ALSO written down in
* `shared/keybindings.ts` as `MAIN_INTERCEPTED_COMMAND_IDS` (the Settings UI's app-wide shadow
* warning reads it, and cannot derive it from here — main is not importable from the renderer);
- * a third intercept added here owes that list an entry, which `keydown-intercept.test.ts` pins.
+ * a third REGISTRY-BACKED intercept added here owes that list an entry, which
+ * `keydown-intercept.test.ts` pins. Note what that pin does NOT cover: a hardcoded ⌘0-shaped
+ * intercept has no command id, so it cannot appear in the list and the invariant test cannot see
+ * it — it would swallow its chord app-wide with the Settings recorder reporting no conflict. If
+ * you add one, the Settings UI needs a separate way to know about it.
*
* Desktop-only by construction (it exists to fight a native menu), so it stays in `src/main` next
* to `main-window.ts` rather than moving to `src/core` — the Server Edition's browser shell has no
@@ -157,6 +168,33 @@ export function keydownIntercept(
return null
}
+/**
+ * PURE. Does this `did-start-navigation` mean the page that armed a shortcut recorder is going
+ * away, so the recording bit must be cleared?
+ *
+ * **Why the bit needs a navigation leg at all.** The recording bit is GLOBAL and lives in the main
+ * process, so every way the renderer can stop existing owes it a release. Window `closed` and
+ * `render-process-gone` cover two of them. The third is a **reload** — and the app's own View menu
+ * restores `{role:'reload'}` / `{role:'forceReload'}`, whose ⌘R/⌘⇧R are ACCELERATORS: they are
+ * handled above the page, so the recorder's `preventDefault` cannot stop a user from pressing one
+ * while armed. That reload fires no React unmount, no `closed` and no `render-process-gone`; the
+ * new page mounts no recorder, and the bit would stay true forever — ⌘W/⌘M/⌘0 dead app-wide with
+ * nothing left alive to clear them.
+ *
+ * The two filters are both refusals, and both matter:
+ * - `isSameDocument` (Electron's newer name for the old `isInPlace`) is a `pushState`, a
+ * `replaceState` or a fragment jump — the SAME page, with the recorder still mounted and still
+ * armed. Clearing there would re-arm the intercepts under a live recorder, i.e. re-open the very
+ * bug this feature closes.
+ * - A subframe navigating is not this page going away either.
+ */
+export function navigationClearsRecording(details: {
+ isMainFrame: boolean
+ isSameDocument: boolean
+}): boolean {
+ return details.isMainFrame && !details.isSameDocument
+}
+
/** The renderer channel a claimed action is forwarded on. */
export function keydownInterceptChannel(action: KeydownInterceptAction): string {
if (action === 'toggle-markdown') return IPC.appToggleMarkdown
@@ -186,11 +224,22 @@ export interface KeydownInterceptTarget {
* sanitize per keystroke would be real work on the input path).
*
* `isRecording` is the same shape and for the same reason — the renderer flips it over IPC while
- * this window is alive. **It is checked BEFORE `preventDefault`, not before the send**: a claimed
- * chord never reaches the page at all, so while the Settings shortcut recorder is armed the only
- * way it can SEE ⌘W is for this listener to leave the key completely alone. Swallowing but not
- * forwarding would still hand the recorder nothing — and forwarding is the live bug, since ⌘W
+ * this window is alive. **It is checked BEFORE `preventDefault`, not before the send**: a chord
+ * THIS MODULE claims never reaches the page at all, so while the Settings shortcut recorder is
+ * armed, leaving the key completely alone is the only way the recorder can see it. Swallowing but
+ * not forwarding would still hand the recorder nothing — and forwarding is the live bug, since ⌘W
* pressed into the recorder deletes the canvas's selected nodes.
+ *
+ * **What this stand-down does NOT buy, and cannot.** It only stops US from taking the key; it has
+ * no say over the application MENU, whose accelerators are handled above the page either way. So
+ * every menu accelerator is unrecordable while this stands down — including **⌘M**, which
+ * `{role:'minimize'}` owns on every platform, and **Ctrl+W** on Windows/Linux, where the Window
+ * submenu has a `{role:'close'}`. Pressing one of those into an armed recorder minimizes/closes the
+ * window instead of recording. Concretely, the stand-down fully delivers **⌘0** everywhere and
+ * **⌘W on macOS** (neither is in the menu), and cannot deliver ⌘M anywhere. Fixing that means
+ * suspending the MENU while recording (`Menu.setApplicationMenu(null)` around the armed window, or
+ * per-item `enabled:false`) — a change to `buildAppMenu`, not to this module. Known limitation;
+ * see the PR body.
*/
export function installKeydownIntercepts(
win: KeydownInterceptTarget,
diff --git a/src/preload/index.ts b/src/preload/index.ts
index 290ebd9c0..9d1d9f1e5 100644
--- a/src/preload/index.ts
+++ b/src/preload/index.ts
@@ -594,8 +594,10 @@ const api: NodeTerminalApi = {
// Per-node subscriptions (each terminal/editor listens) — multiplexed so they don't pile up
// ipcRenderer listeners and trip the MaxListeners warning.
shortcuts: {
- // Fire-and-forget: nothing waits on the answer, and a dropped disarm is covered by main's own
- // window/renderer-death resets (index.ts `clearShortcutRecording`).
+ // Fire-and-forget: nothing waits on the answer. Main clears the bit itself on the three ways
+ // this page can stop existing — window closed, renderer died, main-frame navigation (⌘R) —
+ // but those are a backstop for a page that VANISHED, not a general safety net: an ordinary
+ // disarm that never sends leaves the shortcuts suppressed until one of them happens.
setRecording: (active: boolean) => ipcRenderer.send(IPC.uiShortcutRecording, active)
},
onMarkdownToggle: subscribe(IPC.appToggleMarkdown),
diff --git a/src/renderer/components/settings/ShortcutRecorderButton.tsx b/src/renderer/components/settings/ShortcutRecorderButton.tsx
index a0c8bc6b2..97e43dd19 100644
--- a/src/renderer/components/settings/ShortcutRecorderButton.tsx
+++ b/src/renderer/components/settings/ShortcutRecorderButton.tsx
@@ -16,9 +16,15 @@
* fire `blur` on a focused element that is REMOVED from the DOM, so closing Settings while the
* recorder is armed can skip `onBlur` entirely — leaving the bit set with no component left to
* clear it, suppressing ⌘W/⌘M/⌘0 app-wide until the next arm/disarm cycle. `release` (below) is
- * the teardown both `stop()` and the unmount cleanup run; it touches refs only, so it is stable
- * and safe to run when the recorder was never armed (`setRecording(false)` on a shell that was
- * never told `true` is a plain no-op).
+ * the teardown both `stop()` and the unmount cleanup run; it touches refs only, so it is stable.
+ * It is safe to run from an instance that was never armed because it CHECKS (`armedRef`) — not
+ * because the send would be harmless. One global bit, many instances: an unconditional release
+ * would let a sibling unmounting for unrelated reasons clear the armed recorder's bit.
+ *
+ * **A menu accelerator is still out of reach.** The main-process stand-down only stops nodeterm's
+ * own `before-input-event` intercepts; the application menu's accelerators are handled above the
+ * page regardless. So ⌘M (Window ▸ Minimize, every platform) and Ctrl+W (Windows/Linux) cannot be
+ * recorded here — see the limitation note in `src/main/keydown-intercept.ts`.
*/
import { useCallback, useEffect, useRef, useState } from 'react'
import { COMMANDS_BY_ID, normalizeBindingForCommand, type CommandId } from '@shared/keybindings'
@@ -39,13 +45,26 @@ export function ShortcutRecorderButton({
const [capturing, setCapturing] = useState(false)
const [hint, setHint] = useState('')
const stateRef = useRef<RecordingState>({ mods: null })
+ // Did THIS instance arm the (global) main-process bit? See `release`.
+ const armedRef = useRef(false)
const def = COMMANDS_BY_ID.get(commandId)!
const opts = { isMac, allowHold: def.allowHoldChord === true }
// Refs only ⇒ stable with no deps, so the mount-time cleanup below cannot capture stale state,
- // and idempotent ⇒ running it on an unmount that was never armed is a no-op.
+ // and idempotent ⇒ running it twice, or on an unmount that was never armed, does nothing.
+ //
+ // **`armedRef` is what makes it safe to run from every instance.** The main-process bit is ONE
+ // global boolean while this component is rendered once per command, so a sibling unmounting for
+ // reasons that have nothing to do with recording must not speak for the armed one. That is not
+ // hypothetical in this page: settings rows are wrapped in `SearchableRow`, which returns `null`
+ // for every row the filter box does not match — so typing there unmounts a batch of recorders at
+ // once. An unconditional `setRecording(false)` in that cleanup would clear the ARMED recorder's
+ // bit from under it, re-arming ⌘W/⌘M mid-capture: the original bug, reached by a different door.
+ // Only the instance that sent `true` may send `false`.
const release = useCallback((): void => {
stateRef.current = { mods: null }
+ if (!armedRef.current) return
+ armedRef.current = false
window.nodeTerminal.shortcuts.setRecording(false)
}, [])
// The unmount leg — the one that matters most (Settings closed mid-recording fires no blur).
@@ -60,6 +79,7 @@ export function ShortcutRecorderButton({
stateRef.current = { mods: null }
setCapturing(true)
setHint('')
+ armedRef.current = true
window.nodeTerminal.shortcuts.setRecording(true)
}
const commit = (combo: string): void => {