Skip to content

fix(cmd-j): focus the destination workspace's own terminal after a jump - #10695

Merged
nwparker merged 2 commits into
mainfrom
nwparker/fix-9939-cmdj-focus
Jul 27, 2026
Merged

nwparker merged 2 commits into
mainfrom
nwparker/fix-9939-cmdj-focus

Conversation

@nwparker

@nwparker nwparker commented Jul 26, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #9939

What's broken

Switch workspace with Cmd+J and the keyboard goes nowhere. You have to click the terminal before you can type. @yanghoonkim: "really need it!!!!!!"

ELI5

Orca keeps every workspace you've visited alive in the background and just hides the ones you're not looking at. When you jumped to a new workspace, the code said "focus the first terminal you can find on the page" — and the first one it found was usually the workspace you just left, which is now hidden.

Focusing a hidden element in Chromium doesn't throw an error. It doesn't do anything at all. So the keyboard was left attached to nothing, silently.

The fix: ask the store which tab the destination workspace actually restored, and focus that specific tab, instead of grabbing whatever terminal happens to come first in the document.

Root cause

Terminal.tsx:2136-2145 gives inactive worktrees the hidden class while keeping them mounted. handleSelectWorktree called focusFallbackSurface(), which resolves through palette-focus-restore-target.ts:13 — a bare document.querySelector('.xterm-helper-textarea') with no worktree or tab-type scoping at all.

.focus() on a display:none element is a silent no-op in Chromium: no error, no focus, activeElement stays document.body.

The sibling handlers in the same file already did this correctly — handleSelectBrowserPage dispatches a scoped requestBrowserFocus({ pageId }). Only the plain worktree-row path fell through to the unscoped query.

Fix proof

Measured in real Chromium (Playwright, Chromium 147) — not happy-dom, which does not replicate the display:none no-op and would have shown a false pass:

{ "before": { "picked": "background",  "activeElementIsBody": true,  "focusReachedATerminal": false },
  "after":  { "picked": "destination", "activeElementIsBody": false, "focusReachedATerminal": true  } }

#9939 before/after

Proof of no regressions

  • 3,045 tests / 334 files pass across src/renderer/src/lib/ and src/renderer/src/components/cmd-j/
  • pnpm typecheck clean, oxlint clean
  • Verified the riskiest assumption independently: read worktrees.ts:4509-4745 and confirmed setActiveWorktree commits via a synchronous Zustand set(), so reading the store immediately after activation is not observing stale state. Both reviewers confirmed this separately.
  • Creation flows are byte-identical. The helper rename (new-workspace-terminal-focus → workspace-activation-terminal-focus) adds only a return statement; both existing callers ignore it.

Mutation testing

Two independent reviewers (GPT-5.6-sol + Fable) found the helper well-pinned but the call-site wiring completely unpinned — reverting to the original bug passed all 10,140 component tests. happy-dom can't see this class of bug, so the added tests use the repo's existing source-boundary pattern (feature-interaction-writer-boundaries.test.ts), pinning which API the handler selects rather than who ended up focused.

Mutation Before After
Full revert to bare focusFallbackSurface() SURVIVED KILLED
Ignore helper return, always fall back SURVIVED KILLED
Project-target reverts to unscoped query SURVIVED KILLED
Helper return false → return true KILLED KILLED
Drop activeTabType !== 'terminal' guard KILLED KILLED
Drop activeWorktreeId race guard KILLED KILLED

Review loop: 2 rounds to clean (round 1 GPT + Fable → both NON-BLOCKING with a shared coverage finding; round 2 closed it).

Trade-offs / known limits

Stated plainly rather than claimed away:

  1. ~1 frame focus-steal window. The rAF callback validates store state but not whether another element claimed focus in the meantime. Deliberately clicking a rename input within one frame of the jump could lose focus to the terminal. Both reviewers rated this non-blocking — the user's explicit intent was to jump to that terminal.
  2. Non-terminal destinations still use the old fallback. If the destination restored a browser/editor tab, the helper declines and the caller keeps prior behavior. Not a regression (previously broken for all destinations), but not fixed either.
  3. Cmd+1..9 and nav back/forward have the same bug (useIpcEvents.ts:1416, worktree-activation.ts:729). Out of scope here; worth a follow-up.

Cross-platform

No platform-specific code paths. DOM selectors and Zustand state only — no metaKey handling, no filesystem assumptions. SSH and native remote tabs follow the same renderer mount path; web-runtime workspaces with no mirrored tab decline focus rather than claiming it.

Perf

No new subscriptions, no polling, no added renders. One requestAnimationFrame replacing a path that already used two. Strictly one frame earlier.

0 regressions / risk.

🤖 Generated with Claude Code

Made with Orca 🐋

nwparker and others added 2 commits July 25, 2026 23:04
Selecting a workspace in Cmd+J called the palette's fallback with no preferred
target, so it took the first .xterm-helper-textarea in the whole document. Orca
keeps every visited worktree mounted and hides inactive ones with display:none,
so that first match is routinely the worktree the user just left. Focusing a
display:none element is a silent no-op in Chromium, leaving the keyboard on
document.body.

Resolve the destination's own terminal tab from the store after activation and
focus it scoped by data-terminal-tab-id. The helper reports whether it claimed
focus so a non-terminal destination keeps the previous behavior.

Also covers the two Cmd+J issue-match jump paths, which had no focus call at
all, and restores the exact prior element for project/repo reveal, which does
not change worktrees.

Renames new-workspace-terminal-focus to workspace-activation-terminal-focus
since the palette activates existing workspaces, not just new ones.

Co-authored-by: Orca <[email protected]>
Two independent reviewers found the same hole: reverting the palette wiring
back to an unconditional focusFallbackSurface() — the exact original bug —
passed the entire components suite (10,140 tests).

happy-dom does not reproduce Chromium's display:none focus no-op, so the
regression is invisible to behavioral tests. These use the repo's existing
source-boundary pattern instead, which pins which API the handler selects
rather than who ended up focused.

Kills three mutations that previously survived:
- full revert of the worktree-select routing
- ignoring the helper's return and always falling back
- project-target restoration reverting to the unscoped query
@coderabbitai

coderabbitai Bot commented Jul 26, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Terminal focus queueing now targets the activated workspace and returns whether focus was scheduled. Worktree jump palette paths use scoped focus with guarded fallback restoration, including existing issue and pull-request matches. Project selection restores the previously focused element. Composer submission and worktree creation flows now use the renamed activation-focus helper, with corresponding tests updated and expanded.

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description is mostly freeform and omits required template sections like Screenshots, Testing, AI Review Report, Security Audit, and Notes. Add the missing template sections and fill in testing, review, security, and notes details, or explain any intentionally omitted items.
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: Cmd+J now focuses the destination workspace's terminal after a jump.
Linked Issues check ✅ Passed The change matches #9939 by scoping focus to the destination workspace's terminal after Cmd+J and keeping non-terminal fallback behavior.
Out of Scope Changes check ✅ Passed The diff stays within the focus-routing bugfix and related test updates; no unrelated code paths appear to be added.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: ff69f00b-ba56-47d2-aae6-1dbb4faef7db

📥 Commits

Reviewing files that changed from the base of the PR and between c67aadb and 8827023.

📒 Files selected for processing (7)
  • src/renderer/src/components/WorktreeJumpPalette.tsx
  • src/renderer/src/components/cmd-j/palette-activation-focus-routing.test.ts
  • src/renderer/src/hooks/useComposerState.ts
  • src/renderer/src/lib/workspace-activation-terminal-focus.test.ts
  • src/renderer/src/lib/workspace-activation-terminal-focus.ts
  • src/renderer/src/lib/worktree-creation-flow.test.ts
  • src/renderer/src/lib/worktree-creation-flow.ts

@nwparker
nwparker merged commit 2bb3276 into main Jul 27, 2026
2 checks passed
@nwparker
nwparker deleted the nwparker/fix-9939-cmdj-focus branch July 27, 2026 22:31
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: keyboard focus does not return to active terminal after Switch worktree (Cmd+J palette)

1 participant