Skip to content

The agent that grows with you

fix(desktop): route Ctrl+R to the terminal instead of reloading the app

ethernetdd962a804f2f
diff --git a/apps/desktop/src/app/context-menu/app-context-menu.test.tsx b/apps/desktop/src/app/context-menu/app-context-menu.test.tsx
index 9e5ec3cf116..8f43004057e 100644
--- a/apps/desktop/src/app/context-menu/app-context-menu.test.tsx
+++ b/apps/desktop/src/app/context-menu/app-context-menu.test.tsx
@@ -413,6 +413,7 @@ describe('AppContextMenu', () => {
const unregister = registerTerminalContextMenu(host.querySelector('[data-terminal]')!, {
getSelection: () => 'picked text',
paste,
+ reload: vi.fn(),
selectAll: vi.fn()
})
@@ -432,6 +433,7 @@ describe('AppContextMenu', () => {
const unregister = registerTerminalContextMenu(host.querySelector('[data-terminal]')!, {
getSelection: () => '',
paste: null,
+ reload: vi.fn(),
selectAll: vi.fn()
})
diff --git a/apps/desktop/src/app/contrib/hooks/use-desktop-integrations.ts b/apps/desktop/src/app/contrib/hooks/use-desktop-integrations.ts
index dce969716f3..acb7b7eb60f 100644
--- a/apps/desktop/src/app/contrib/hooks/use-desktop-integrations.ts
+++ b/apps/desktop/src/app/contrib/hooks/use-desktop-integrations.ts
@@ -5,6 +5,7 @@ import { resumeAccountConnect } from '@/app/capabilities/connectors/data/deep-li
import { closeActiveTab } from '@/app/chat/close-tab'
import { commandFocusedPreview } from '@/app/chat/right-rail/preview-nav'
import { openSession } from '@/app/open-session'
+import { commandFocusedTerminal } from '@/app/right-sidebar/terminal/terminal-context-menu'
import { openConnectionDoneLink } from '@/components/assistant-ui/connector-tool'
import { $diskPluginsScanPending } from '@/contrib/runtime-loader'
import { getSession } from '@/hermes'
@@ -490,6 +491,10 @@ export function useDesktopIntegrations({
// app-level meaning to fall back to; an unfocused swipe is a no-op.
useEffect(() => {
const unsubscribe = window.hermesDesktop?.onPreviewNav?.(command => {
+ if (commandFocusedTerminal(command)) {
+ return
+ }
+
if (!commandFocusedPreview(command) && command === 'reload') {
window.location.reload()
}
diff --git a/apps/desktop/src/app/right-sidebar/terminal/terminal-context-menu.test.ts b/apps/desktop/src/app/right-sidebar/terminal/terminal-context-menu.test.ts
new file mode 100644
index 00000000000..5cca64d3851
--- /dev/null
+++ b/apps/desktop/src/app/right-sidebar/terminal/terminal-context-menu.test.ts
@@ -0,0 +1,108 @@
+import { afterEach, describe, expect, it, vi } from 'vitest'
+
+import { commandFocusedTerminal, registerTerminalContextMenu, terminalMenuHandleFor } from './terminal-context-menu'
+
+const handle = (overrides: Partial<Parameters<typeof registerTerminalContextMenu>[1]> = {}) => ({
+ getSelection: () => '',
+ paste: null,
+ reload: vi.fn(),
+ selectAll: vi.fn(),
+ ...overrides
+})
+
+afterEach(() => {
+ document.body.innerHTML = ''
+})
+
+/** instance.tsx shape: outer div carries data-terminal, hostRef points at the
+ * inner xterm host div, and a click lands on the canvas inside it. */
+function mountProductionShape(): { canvas: HTMLElement; host: HTMLElement; scope: HTMLElement } {
+ const scope = document.createElement('div')
+ scope.setAttribute('data-terminal', '')
+ const host = document.createElement('div')
+ const canvas = document.createElement('canvas')
+ host.appendChild(canvas)
+ scope.appendChild(host)
+ document.body.appendChild(scope)
+
+ return { canvas, host, scope }
+}
+
+describe('terminalMenuHandleFor (production DOM shape)', () => {
+ it('finds the handle registered on the xterm host, not the scope div', () => {
+ const { canvas, host } = mountProductionShape()
+
+ const registered = handle()
+ const unregister = registerTerminalContextMenu(host, registered)
+
+ expect(terminalMenuHandleFor(canvas)).toBe(registered)
+ unregister()
+ })
+
+ it('returns null outside any terminal scope', () => {
+ const outside = document.createElement('div')
+ document.body.appendChild(outside)
+
+ expect(terminalMenuHandleFor(outside)).toBeNull()
+ })
+})
+
+describe('commandFocusedTerminal', () => {
+ it('runs reload on the terminal holding DOM focus', () => {
+ const { host, scope } = mountProductionShape()
+ const textarea = document.createElement('textarea')
+ host.appendChild(textarea)
+ textarea.focus()
+
+ const reload = vi.fn()
+ const unregister = registerTerminalContextMenu(host, handle({ reload }))
+
+ expect(commandFocusedTerminal('reload')).toBe(true)
+ expect(reload).toHaveBeenCalledTimes(1)
+ unregister()
+ scope.remove()
+ })
+
+ it('answers false when focus is outside every terminal', () => {
+ const outside = document.createElement('div')
+ document.body.appendChild(outside)
+ outside.focus?.()
+
+ expect(commandFocusedTerminal('reload')).toBe(false)
+ })
+
+ it('answers false for back and forward — no terminal verb', () => {
+ const { host, scope } = mountProductionShape()
+ const textarea = document.createElement('textarea')
+ host.appendChild(textarea)
+ textarea.focus()
+
+ const reload = vi.fn()
+ const unregister = registerTerminalContextMenu(host, handle({ reload }))
+
+ expect(commandFocusedTerminal('back')).toBe(false)
+ expect(commandFocusedTerminal('forward')).toBe(false)
+ expect(reload).not.toHaveBeenCalled()
+ unregister()
+ scope.remove()
+ })
+
+ it('unregisters idempotently — a later handle for the same scope replaces, an old remove does not', () => {
+ const { host, scope } = mountProductionShape()
+ const textarea = document.createElement('textarea')
+ host.appendChild(textarea)
+ textarea.focus()
+
+ const first = handle()
+ const second = handle()
+ const removeFirst = registerTerminalContextMenu(host, first)
+ registerTerminalContextMenu(host, second)
+
+ removeFirst()
+
+ expect(commandFocusedTerminal('reload')).toBe(true)
+ expect(first.reload).not.toHaveBeenCalled()
+ expect(second.reload).toHaveBeenCalledTimes(1)
+ scope.remove()
+ })
+})
diff --git a/apps/desktop/src/app/right-sidebar/terminal/terminal-context-menu.ts b/apps/desktop/src/app/right-sidebar/terminal/terminal-context-menu.ts
index fca40ec9896..a3d22c93e41 100644
--- a/apps/desktop/src/app/right-sidebar/terminal/terminal-context-menu.ts
+++ b/apps/desktop/src/app/right-sidebar/terminal/terminal-context-menu.ts
@@ -1,29 +1,42 @@
/**
- * Context-menu handles for the GUI terminals.
+ * Verb registry for the GUI terminals.
*
* xterm paints to a canvas: a right-click inside it crosses no link, image,
* editable, or DOM selection, so the app context menu would see a bare
- * surface and offer the window verbs. Each terminal registers a handle for
- * its host instead, and the coordinator resolves a click back to it through
- * the `[data-terminal]` focus-scope marker both terminal flavours carry.
+ * surface and offer the window verbs. Focus-routed chords hit the same blind
+ * spot from the other side: main claims Ctrl/Cmd+R on every window
+ * (before-input-event + preventDefault) so Chromium's default host reload
+ * cannot fire, and that claim also stops the keystroke before xterm sees it.
+ * Both paths resolve through a handle registered for the terminal's
+ * `[data-terminal]` scope.
*/
export interface TerminalMenuHandle {
getSelection: () => string
/** Null on the read-only agent mirror — it has no PTY to paste into. */
paste: ((text: string) => void) | null
+ /** Re-deliver the Ctrl/Cmd+R chord main claimed (#96482). User terminals
+ * write the ^R byte into their PTY (readline reverse-i-search). Read-only
+ * agent mirrors have no PTY, so they swallow it — focus is still in a
+ * terminal, and the app-level reload fallback must not fire there. */
+ reload: () => void
selectAll: () => void
}
const handles = new WeakMap<Element, TerminalMenuHandle>()
-/** Register the handle for a terminal host. Returns an idempotent remove. */
+/** Register the handle for a terminal host. The registry keys by the
+ * `[data-terminal]` scope above the host: both resolvers start from a click
+ * target or the focused element and walk up to that scope, while the xterm
+ * host is a nested div inside it (instance.tsx). Returns an idempotent
+ * remove. */
export function registerTerminalContextMenu(host: Element, handle: TerminalMenuHandle): () => void {
- handles.set(host, handle)
+ const scope = host.closest('[data-terminal]') ?? host
+ handles.set(scope, handle)
return () => {
- if (handles.get(host) === handle) {
- handles.delete(host)
+ if (handles.get(scope) === handle) {
+ handles.delete(scope)
}
}
}
@@ -34,3 +47,21 @@ export function terminalMenuHandleFor(element: Element | null): TerminalMenuHand
return host ? (handles.get(host) ?? null) : null
}
+
+/** Run a focus-routed chord on the terminal holding DOM focus — the terminal
+ * rung of the preview-nav routing (see commandFocusedPreview in
+ * chat/right-rail/preview-nav.ts). Only `reload` is a terminal verb; `back`
+ * and `forward` mean nothing there. False = focus is elsewhere in the app,
+ * so the caller falls back to the app-level meaning. */
+export function commandFocusedTerminal(command: 'back' | 'forward' | 'reload'): boolean {
+ if (command !== 'reload') {
+ return false
+ }
+
+ const host = document.activeElement?.closest('[data-terminal]')
+ const handle = host ? handles.get(host) : undefined
+
+ handle?.reload()
+
+ return Boolean(handle)
+}
diff --git a/apps/desktop/src/app/right-sidebar/terminal/use-agent-terminal.ts b/apps/desktop/src/app/right-sidebar/terminal/use-agent-terminal.ts
index d1b7f34380f..7ea4f711363 100644
--- a/apps/desktop/src/app/right-sidebar/terminal/use-agent-terminal.ts
+++ b/apps/desktop/src/app/right-sidebar/terminal/use-agent-terminal.ts
@@ -86,10 +86,13 @@ export function useAgentTerminal({ active, id, procId }: { active: boolean; id:
// Right-clicks resolve through the app context menu; the handle carries
// the xterm selection the DOM resolver cannot see. paste stays null —
- // there is nothing to paste into.
+ // there is nothing to paste into. reload swallows the chord for the same
+ // reason: no PTY, and focus being here must still stop the app-level
+ // Ctrl/Cmd+R reload fallback.
const contextMenuDisposable = registerTerminalContextMenu(host, {
getSelection: () => term.getSelection(),
paste: null,
+ reload: () => {},
selectAll: () => term.selectAll()
})
diff --git a/apps/desktop/src/app/right-sidebar/terminal/use-terminal-session.ts b/apps/desktop/src/app/right-sidebar/terminal/use-terminal-session.ts
index efa82cd232d..76723f694d3 100644
--- a/apps/desktop/src/app/right-sidebar/terminal/use-terminal-session.ts
+++ b/apps/desktop/src/app/right-sidebar/terminal/use-terminal-session.ts
@@ -796,7 +796,9 @@ export function useTerminalSession({
// The app context menu resolves right-clicks on this host through the
// registered handle: xterm's selection is not a DOM selection, so the
- // DOM resolver would see nothing here.
+ // DOM resolver would see nothing here. The same handle answers the
+ // focus-routed Ctrl/Cmd+R: main claimed the keystroke, so re-deliver the
+ // ^R byte to the PTY ourselves (#96482).
cleanup.push(
registerTerminalContextMenu(host, {
getSelection: () => term.getSelection(),
@@ -805,6 +807,14 @@ export function useTerminalSession({
term.focus()
term.paste(text)
},
+ reload: () => {
+ hasSessionActivityRef.current = true
+ const sessionId = sessionIdRef.current
+
+ if (sessionId) {
+ void terminalApi.write(sessionId, '\x12')
+ }
+ },
selectAll: () => term.selectAll()
})
)