Skip to content

fix(browser): emulate CDP page focus for rich-editor insertText - #10376

Closed
innocarpe wants to merge 3 commits into
stablyai:mainfrom
innocarpe:fix/browser-cdp-focus-emulation
Closed

innocarpe wants to merge 3 commits into
stablyai:mainfrom
innocarpe:fix/browser-cdp-focus-emulation

Conversation

@innocarpe

@innocarpe innocarpe commented Jul 24, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #10375: embedded automation guests can report document.hasFocus() === false, causing CDP text input to no-op. CDP focus emulation is enabled on both bridge paths while preserving existing DOM-focus replay.

Takeover / attribution

Original implementation and diagnosis by @innocarpe. I took over the PR after reproducing the shipped regression, centralized its 5 s best-effort timeout so direct CdpBridge attach cannot hang, and added bridge-path plus timeout regression tests.

ELI5

The page thinks it is not selected, so some editors ignore typing. We tell Chromium to treat the automation page as focused before sending text.

Fix proof

Live Orca 1.4.159-rc.1: active https://httpbin.org/forms/post returned document.hasFocus() = false; after clicking Customer name, orca type --input AAA returned success but the input value remained empty. Commit eaf21cbb1f sends Emulation.setFocusEmulationEnabled({ enabled: true }) on both CDP attach paths.

No-regression proof / trade-offs

  • 0 regressions / risk: UNKNOWN. Focus emulation intentionally changes focus semantics for automation guests; live macOS regression is reproduced, but Linux/Windows Electron validation remains pending.
  • One CDP command per debugger attach, bounded to 5,000 ms; stalled commands no longer hold either attach path.
  • SSH and folder workspaces are unaffected: no path, provider, or Git behavior changes.

Review loops

2 loops; final verdict: implementation clean after direct-bridge coverage and shared timeout. Remaining proof is a newly built app on non-macOS.

Tests

  • pnpm exec vitest run --config config/vitest.config.ts src/main/browser/cdp-focus-emulation.test.ts src/main/browser/cdp-bridge-integration.test.ts src/main/browser/cdp-ws-proxy.test.ts src/main/browser/cdp-ws-proxy-focus-replay.test.ts
  • pnpm run typecheck:node
  • git diff --check

Linked issue

Fixes #10375.

Screenshots / evidence

No uploadable URL: Orca CLI captured the active browser viewport as in-memory PNG data, without a file path usable by gh image or gh attach. Command/evaluation evidence is recorded above and in the category report.

Electron 43+ can leave embedded guest views with document.hasFocus() false
even when Orca is frontmost, so Draft.js-style editors drop Input.insertText
and execCommand. Enable Emulation.setFocusEmulationEnabled on debugger attach
in CdpWsProxy and CdpBridge.
@coderabbitai

coderabbitai Bot commented Jul 24, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Adds a shared helper for sending Emulation.setFocusEmulationEnabled with { enabled: true }. Both CdpBridge and CdpWsProxy invoke it during debugger attachment and ignore failures. Unit and integration tests are updated to verify the command and its ordering before focus replay, input, navigation, reload, evaluation, and session lifecycle commands.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description has useful context but misses required template sections like AI Review Report, Security Audit, and Notes. Add the missing template sections, including an AI review summary, security audit, and Notes, and keep the testing checklist in the required format.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed The changes enable focus emulation on debugger attach in the touched browser paths, matching #10375's requirement for document.hasFocus() to stay true.
Out of Scope Changes check ✅ Passed The diff stays focused on CDP focus emulation and related tests, with no unrelated behavior changes apparent.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Title check ✅ Passed Title is concise and matches the main change: enabling CDP focus emulation for rich-editor text input.

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: 700438d3-7e9d-419b-be1f-f9c0861a4457

📥 Commits

Reviewing files that changed from the base of the PR and between 1bd36ce and 26b4cad.

📒 Files selected for processing (6)
  • src/main/browser/cdp-bridge.ts
  • src/main/browser/cdp-focus-emulation.test.ts
  • src/main/browser/cdp-focus-emulation.ts
  • src/main/browser/cdp-ws-proxy-focus-replay.test.ts
  • src/main/browser/cdp-ws-proxy.test.ts
  • src/main/browser/cdp-ws-proxy.ts

Comment thread src/main/browser/cdp-ws-proxy.ts Resolved
CodeRabbit: stalled debugger.sendCommand could hang agent-browser attach.
@nwparker nwparker added the bug Something isn't working label Jul 27, 2026
@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

bug Something isn't working

Projects

None yet

3 participants