agent-orchestrator icon indicating copy to clipboard operation
agent-orchestrator copied to clipboard

fix(web): terminal doesn't get keyboard focus when opening a session/agent view

Open mukesher4 opened this issue 4 months ago • 4 comments

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.tsx owns the real Terminal instance (term) from @xterm/xterm, which has a built-in term.focus() method — but nothing in this file ever calls it.
  • The handle object it exposes outward, AttachableTerminal (defined in frontend/src/renderer/hooks/useTerminalSession.ts), only exposes cols, rows, write, writeln, clear, onUserInput, onResize — no focus method, so callers have no way to trigger it even if they wanted to.
  • frontend/src/renderer/components/TerminalPane.tsx's AttachedTerminal keys the terminal by terminalHandleId (key={terminalKey}), so React fully remounts XtermTerminal (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

Screen Recording

Fix

  1. Add focus: () => term.focus() to the AttachableTerminal handle object built in XtermTerminal.tsx — exposes the capability (mechanism), without deciding when to use it.
  2. Add focus: () => void to the AttachableTerminal type in useTerminalSession.ts.
  3. Call terminal.focus() in TerminalPane.tsx's existing mount effect (in AttachedTerminal), gated on handleId being present — TerminalPane is the layer that knows whether a session/handle is actually attached (showEmptyState = !handleId), so it decides when to focus (policy), rather than XtermTerminal focusing 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.

mukesher4 avatar Jul 05 '26 14:07 mukesher4

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.

homeshrawat avatar Aug 05 '26 23:08 homeshrawat

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:

  • SessionView sends non-chat sessions through CenterPane, while Chat sessions mount ChatWorkspace/ChatComposer.
  • CenterPane only requested terminal focus for shell or special agent-switch states. Ordinary TUI/CLI worker selection did not set focusRequested.
  • XtermTerminal already called xterm's focus method when focusRequested was 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.tsx
  • frontend/src/renderer/components/XtermTerminal.tsx
  • frontend/src/renderer/components/CenterPane.test.tsx
  • frontend/src/renderer/components/TerminalPane.test.tsx
  • frontend/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.

Rishet11 avatar Aug 28 '26 20:08 Rishet11

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.

Rishet11 avatar Aug 28 '26 21:08 Rishet11

Fixed by #4847

Rishet11 avatar Sep 04 '26 20:09 Rishet11