fix(web): terminal doesn't get keyboard focus when opening a session/agent view
Bug
When opening a terminal view — clicking an agent from the Kanban board, selecting an orchestrator session, or otherwise navigating into a session's terminal — the xterm.js terminal never receives keyboard focus. The user has to manually click inside the terminal pane before typing does anything.
Analyzed against: 860fa5a2 (origin/main)
Confidence: High — traced exact code path, root cause confirmed by absence of any .focus() call.
Root Cause
frontend/src/renderer/components/XtermTerminal.tsxowns the realTerminalinstance (term) from@xterm/xterm, which has a built-interm.focus()method — but nothing in this file ever calls it.- The handle object it exposes outward,
AttachableTerminal(defined infrontend/src/renderer/hooks/useTerminalSession.ts), only exposescols,rows,write,writeln,clear,onUserInput,onResize— nofocusmethod, so callers have no way to trigger it even if they wanted to. frontend/src/renderer/components/TerminalPane.tsx'sAttachedTerminalkeys the terminal byterminalHandleId(key={terminalKey}), so React fully remountsXtermTerminal(destroy + recreate the xterm.js instance) on every session switch — giving a natural, reliable "just opened" moment via the mount effect, which currently does nothing focus-related.
Screen Recording
Fix
- Add
focus: () => term.focus()to theAttachableTerminalhandle object built inXtermTerminal.tsx— exposes the capability (mechanism), without deciding when to use it. - Add
focus: () => voidto theAttachableTerminaltype inuseTerminalSession.ts. - Call
terminal.focus()inTerminalPane.tsx's existing mount effect (inAttachedTerminal), gated onhandleIdbeing present —TerminalPaneis the layer that knows whether a session/handle is actually attached (showEmptyState = !handleId), so it decides when to focus (policy), rather thanXtermTerminalfocusing unconditionally on every mount, which would also steal focus onto the empty-state pane (no session selected).
Impact
- Low severity, high friction: every session/agent switch currently requires an extra click before the user can type, on every navigation.
Additional repro path for the same root cause: switching between tabs, not just opening a session view.
On macOS (darwin 23.6.0), AO desktop, repo at 3934bc86a: with two tabs open (an agent session tab and a shell terminal tab), switching from one to the other leaves the new tab's terminal without keyboard focus. Typing does nothing until you click somewhere inside the terminal pane. Every tab switch costs an extra click.
This matches your traced root cause exactly — nothing calls term.focus() in XtermTerminal.tsx, and AttachableTerminal (frontend/src/renderer/hooks/useTerminalSession.ts) exposes no focus method, so a tab-switch handler has no way to request focus even if one were added.
Worth noting for whoever picks this up: the tab strips live at frontend/src/renderer/components/CenterPane.tsx:217-245 and ShellTerminalsView.tsx:87-108. Adding focus to the AttachableTerminal handle would serve both this and the tab-switch case with the same change.
Related: I've filed #3639 for tab cycling via Ctrl+Tab. These compound — without a tab shortcut you must mouse to the tab, and after switching you must click again into the terminal, so keyboard-only tab navigation is impossible today even once a shortcut exists.
Confirmed current-rewrite report
This remains reproducible in the current desktop rewrite: select or switch between CLI/TUI worker sessions, then type. The destination terminal does not receive keyboard input until the user clicks inside it. Chat sessions do not show the problem because the Chat composer explicitly restores editor focus on session and visibility changes.
Analyzed against current origin/main commit d47999aa05a835bb5e4074c6f87a3d24b0f93d26. Confidence is high. The current lifecycle is:
SessionViewsends non-chat sessions throughCenterPane, while Chat sessions mountChatWorkspace/ChatComposer.CenterPaneonly requested terminal focus for shell or special agent-switch states. Ordinary TUI/CLI worker selection did not setfocusRequested.XtermTerminalalready called xterm's focus method whenfocusRequestedwas true and the terminal was visible, including retained-terminal visibility transitions.
The implemented fix requests focus for enabled non-chat worker sessions and adds guards so open dialogs/menus and other deliberate controls retain focus. The selected session navigation button is the intentional handoff into the destination TUI. The reporter manually confirmed that the implemented behavior works.
Affected files in the current rewrite:
frontend/src/renderer/components/CenterPane.tsxfrontend/src/renderer/components/XtermTerminal.tsxfrontend/src/renderer/components/CenterPane.test.tsxfrontend/src/renderer/components/TerminalPane.test.tsxfrontend/src/renderer/components/XtermTerminal.test.tsx
Verification: focused terminal suites pass 163 tests; session/Chat regression suites pass 226 tests; frontend typecheck and renderer build pass. Native Electron launch/readiness was also confirmed against the isolated daemon and API. The older open PRs #2435, #2485, and #3762 target an earlier attach-handle design and remain unmerged, so this report covers the current implementation path.
Current rewrite recovery update
The bug is confirmed on current origin/main 5a51a3f90c4b649c21f92d1fcd58204e5e8e1a89. Expected: selecting or switching to an enabled non-Chat CLI/TUI worker lets the user type immediately. Actual before this fix: the destination xterm did not receive keyboard input until the terminal was clicked. Chat is unaffected because ChatComposer explicitly restores editor focus on session changes.
Root cause: CenterPane requested terminal focus only for shell and special agent-switch states, so ordinary non-Chat worker switches never set focusRequested. XtermTerminal already handled the focus request and retained-terminal visibility transition, but lacked guards around the request. Confidence: high.
Recovery: the exact verified five-file patch was recovered read-only from worker agent-orchestrator-225 file contents and matched with no diff; it was not reconstructed. The preserved corrupted worktree was not mutated. The patch requests focus for enabled non-Chat workers, blocks dialogs/menus and deliberate controls, and permits the selected session navigation button to hand focus to the destination terminal.
Verification on this clean branch: focus suites 163 passed; current Chat/session regression set (SessionView, ChatComposer, ChatWorkspace) 225 passed; frontend typecheck passed; renderer Vite build passed; git diff --check passed. Electron native launch/readiness and isolated TUI/Chat API availability were previously confirmed, and the reporter manually confirmed the keyboard behavior.
Related older work: PRs #2435 and #2485 are closed/open historical implementations, and PR #3762 remains open against the earlier attach-handle design; this update targets the current rewrite path and keeps the existing issue labels unchanged.
Fixed by #4847
