Skip to content

fix(browser): focus the page before keypress so keys stop landing in the terminal - #23895

Closed
ajpeng wants to merge 2 commits into
stablyai:mainfrom
ajpeng:fix/browser-keypress-focus-guest
Closed

ajpeng wants to merge 2 commits into
stablyai:mainfrom
ajpeng:fix/browser-keypress-focus-guest

Conversation

@ajpeng

@ajpeng ajpeng commented Sep 29, 2026

Copy link
Copy Markdown

orca keypress --page typed into whichever Orca pane had focus, usually the terminal that ran it, because the page itself was never focused. It now focuses the page before sending the key, the same way orca click does.

Tested: two new keypress unit tests, bridge suite passes.

AI disclosure: written with Claude (Opus 5.5).

@ajpeng
ajpeng marked this pull request as ready for review September 29, 2026 15:17
@coderabbitai

coderabbitai Bot commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Advanced

Run ID: a5992ccb-eb1f-4196-a8fc-03a27899e260

📥 Commits

Reviewing files that changed from the base of the PR and between 02547a8 and ca9995c.

📒 Files selected for processing (24)
  • src/main/browser/agent-browser-bridge-interaction-commands.ts
  • src/main/browser/agent-browser-bridge-keypress-input.test.ts
  • src/main/browser/browser-guest-keyboard-focus.test.ts
  • src/main/browser/browser-guest-keyboard-focus.ts
  • src/main/ipc/browser-preview-tool-authorization.test.ts
  • src/main/ipc/browser.ts
  • src/preload/api/browser-api.ts
  • src/preload/api/browser-bridge-guest-registration-and-downloads.ts
  • src/renderer/src/components/browser-pane/host-guest/browser-guest-keyboard-focus.test.ts
  • src/renderer/src/components/browser-pane/host-guest/browser-guest-keyboard-focus.ts
  • src/renderer/src/hooks/ipc-events-agent-status-window-test-fixtures.ts
  • src/renderer/src/hooks/ipc-events-close-routing-test-harness.ts
  • src/renderer/src/hooks/ipc-events-terminal-create-window-test-fixtures.ts
  • src/renderer/src/hooks/ipc-events/browser-state-capture-paint-hold.test.ts
  • src/renderer/src/hooks/ipc-events/browser-state-ipc-bridge.ts
  • src/renderer/src/hooks/useIpcEvents-browser-tab-create.test.ts
  • src/renderer/src/hooks/useIpcEvents-cli-worktree-activation.test.ts
  • src/renderer/src/hooks/useIpcEvents-close-routing-browser-pages.test.ts
  • src/renderer/src/hooks/useIpcEvents-lifecycle.test.ts
  • src/renderer/src/hooks/useIpcEvents-ssh-disconnect-cleanup.test.ts
  • src/renderer/src/hooks/useIpcEvents-updater-status.test.ts
  • src/renderer/src/web/preload-api/web-browser-api.ts
  • src/shared/browser-guest-keyboard-focus.ts
  • tests/e2e/browser-keypress-focus.spec.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.


📝 Walkthrough

Walkthrough

The change adds a main-process request-response flow for browser guest keyboard focus. The renderer focuses the webview that matches the guest ID and returns whether it became active. Keypress handling requests focus before CDP dispatch or the agent-browser fallback. A not-on-screen result prevents dispatch. An unanswered result allows dispatch. Unit and end-to-end tests cover the request lifecycle and keypress behavior.

Priority: ⬇️ Low

Merge Risk: ⚪ Minimal · up to ca999

The change focuses browser guests before sending keys and blocks dispatch when focus is explicitly rejected. No concrete merge-blocking defect is established; the headful tests' execution and guest field-focus preservation remain unverified.

Security Architecture Review

Security architecture risk: 🔵 Low · up to ca999

The change checks focus before sending keys and restricts who can answer that check. No new security vulnerability was established, but caller permissions and behavior during simultaneous focus changes remain only partly verified.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — The relevant keyboard-routing exposure is the host window's focus tree, potentially including terminal panes. The supplied intent identifies wrong-pane input as pre-existing behavior; the unanswered path does not eliminate it. Upstream admission evidence is insufficient to establish the maximum independently attackable caller scope.

Trust Boundaries and Controls

  • observed — The inspected production window-registration path supplies window.webContents.id. Core registration sets that trusted identity before browser handlers are registered and refreshes it before the idempotent-registration guard. Pending-response sender correlation provides an additional ownership check.

Resilience and Maintainability Implications

  • observed — Commands are serialized per page, not across the host's shared focus state. The renderer acknowledgement is a point-in-time active-element check, and dispatch follows asynchronously without another focus check. This limits the handshake's routing guarantee, but runtime misdelivery or worsened exposure compared with base was not established.

Hardening Proposals

  • proposed — If confirmed keyboard destination is intended to become a security guarantee, consider rejecting unanswered focus requests and coordinating focus-sensitive dispatch across the host renderer rather than only per page. These would strengthen the existing behavior, not remedy a verified new vulnerability.
🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description explains the user-facing problem, the fix, and limited testing details. It omits most required template sections, including the linked issue, visual proof or an explicit N/A reason, re… Complete the required template sections. Add the linked issue, provide visual proof or state N/A with a reason, document review and security/performance/cross-platform/SSH considerations, complete the Agent skill upstream boundary section, …
Docstring Coverage ⚠️ Warning Docstring coverage is 4.55% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 22 functions across 24 files. 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 and concisely describes the main change: focusing the browser page before sending keypresses so keys do not go to the terminal.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Full details: Description check

Explanation

The description explains the user-facing problem, the fix, and limited testing details. It omits most required template sections, including the linked issue, visual proof or an explicit N/A reason, review notes, upstream-boundary declaration, cross-platform and compatibility considerations, checklist status, and complete testing details.

Resolution

Complete the required template sections. Add the linked issue, provide visual proof or state N/A with a reason, document review and security/performance/cross-platform/SSH considerations, complete the Agent skill upstream boundary section, and report the required test and checklist status.

  • Fix all pre-merge checks with AI

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.

@greptile-apps

greptile-apps Bot commented Sep 29, 2026 •

Copy link
Copy Markdown

RetriggerConfidence Score: 3/5

[Medium risk] Adds keyboard focus coordination between browser guest and host.

The PR does not appear safe to merge while keypress can proceed without confirmed focus and the new field-focus end-to-end test cannot reach its keypress assertions.

Findings

  1. P1 Unanswered focus still dispatches keys ▶
  2. P1 Field test uses invalid ref ▶

Summary

The PR replaces native guest focus before browser keypresses with a main-to-renderer request that focuses the hosting webview, and adds unit and end-to-end coverage.

  • An unanswered focus request still permits key dispatch without confirmed focus.
  • The new end-to-end field test constructs an invalid snapshot ref.
Diagram
sequenceDiagram
  participant B as Browser keypress
  participant M as Main focus request
  participant R as Host renderer
  participant W as Webview
  B->>M: Request guest keyboard focus
  M->>R: Send request with guest ID
  R->>W: Focus matching webview
  R-->>M: Focus result
  M-->>B: focused / not-on-screen / unanswered
  B->>W: Dispatch key unless not-on-screen
Loading

Reviews (2) · Last reviewed commit: "fix(browser): focus the page's webview f..."

@pullfrog pullfrog 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.

Important

The fix targets the right symptom, but native webContents.focus() is known in this codebase to blur a DOM element focused by a prior command — that is exactly why CdpDomFocusReplay exists — and neither new focus call replays it. Please verify fill/focus → keypress Enter on a real page before merging.

Reviewed changes

  • Focus before the direct key dispatch — keypress now calls wc.focus() after acquiring the debugger lease and before the two Input.dispatchKeyEvent calls.
  • Focus before the helper fallback — the unmapped-key branch focuses the guest webContents before handing press to agent-browser.
  • Two unit tests — assert focus runs once and before the dispatch/helper call for a mapped (Escape) and an unmapped (MediaPlayPause) key.

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using DeepSeek Flash (free via Pullfrog for OSS) | 𝕏

webContents.focus() on a guest does not move focus in the embedder's tree, and
CDP key events go to whichever widget holds that focus, so keys still landed in
Orca's own UI. Main now asks the renderer to focus the <webview> hosting the
guest and waits for its answer; a page that cannot take focus fails the
keypress instead of typing into Orca.
// Why: a key sent to an unfocused guest lands in whatever the Orca window has focused,
// usually a terminal, so the agent that ran keypress types into its own PTY.
private async focusGuestForKeys(wc: Electron.WebContents, browserPageId: string): Promise<void> {
if ((await requestBrowserGuestKeyboardFocus(wc)) === 'not-on-screen') {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Unanswered focus still dispatches keys When the host renderer is busy for longer than the one-second timeout, the focus request returns unanswered. This check allows keypress to continue even though the webview has not been confirmed focused, so the key can still land in the focused terminal.

Comment on lines +123 to +130
const ref =
refs !== null && typeof refs === 'object'
? Object.entries(refs).find(([, entry]) => readProperty(entry, 'role') === 'textbox')?.[0]
: undefined
if (!ref) {
throw new Error(`No textbox ref in snapshot: ${JSON.stringify(snapshot)}`)
}
return `@${ref}`

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Field test uses invalid ref Snapshot refs is an array of objects with ref values such as @e1. Object.entries(refs) supplies the array index instead, so this helper returns @0 for the first textbox. The test then passes a nonexistent element to browser.fill and fails before it tests keypress focus.

@pullfrog pullfrog 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.

✅ No new issues found.

Reviewed changes

The delta since the prior pullfrog review (02547a8a) replaced the direct native focus call with a renderer round-trip, so I re-reviewed the focus protocol, the bridge wiring, and the new regression tests.

  • Replaced native webContents.focus() with a renderer round-trip — main now asks guest.hostWebContents to focus the <webview> hosting the guest and waits for its answer (browser:guest-keyboard-focus-requested / browser:respondGuestKeyboardFocus), which moves focus in the embedder's tree rather than only on the guest.
  • Added explicit handling for a page that cannot take focus — focusGuestForKeys throws browser_error with a tab switch --page … --focus hint when the embedder answers not-on-screen; an unanswered request still dispatches.
  • Added the shared protocol and preload surface — request/response types, BrowserApi methods, a trusted-renderer ipcMain handler, and the new channel added to the preview-tool authorization classification.
  • Added unit and headful e2e coverage — focus-module and renderer-helper unit tests, bridge ordering/refusal tests, fixture and lifecycle updates, and tests/e2e/browser-keypress-focus.spec.ts, which asserts fill/focus → keypress keeps the field active and leaks no keys to the host.

This resolves the prior review's concern: the field a prior fill/focus targeted is no longer blurred, and the new e2e spec pins both that and the terminal-leak fix.

Pullfrog  | View workflow run | Using DeepSeek Flash (free via Pullfrog for OSS) | 𝕏

@nwparker

Copy link
Copy Markdown
Contributor

Closing this one as superseded. Thank you for it. The fix for agent browser input and the user's keyboard focus (#20595) landed in #28452, #28453 and #28454, and this PR's investigation helped shape it.

What landed:

  • Agent keys and text go straight to the targeted page as key/character events, never by way of window focus. On a <webview> page, CDP key and text input lands wherever the window's focus is. CDP focus emulation doesn't change that, so emulation alone wasn't enough.
  • Agent clicks move focus into the page no matter how the click is sent. The pane now gives focus back to the user's element when the click ends, and focus emulation keeps the page from seeing a blur.
  • Input that only CDP can deliver borrows focus for that one command and gives it back. If focus can't be protected, the input is refused instead of sent.

The design and measurements are in docs/reference/agent-browser-focus-isolation.md.

@nwparker nwparker closed this 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.

2 participants