Repository navigation
fix(browser): restore programmatic focus for CDP text insertion - #7041
Conversation
|
Ready to review this PR? Stage has broken it down into 3 individual chapters for you:
Chapters generated by Stage for commit 0bbc5c1 on Jul 1, 2026 9:46pm UTC. |
📝 WalkthroughWalkthroughThis change adds per-session tracking of pending 🚥 Pre-merge checks | ✅ 3 | ❌ 2❌ Failed checks (2 warnings)
✅ Passed checks (3 passed)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (2)
src/main/browser/cdp-ws-proxy.ts (1)
280-309: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winAdd a "Why" comment for the pending-focus invalidation design.
The rule that
DOM.focusis only replayed for the immediately nextInput.insertText— and is silently invalidated byPage.bringToFront(Line 282),Page.captureScreenshot(Line 295), or any other intervening command (Line 309) — is a non-obvious constraint central to this fix. A future edit to this fallthrough could easily break the replay guarantee without realizing it.As per coding guidelines, "When writing or modifying code driven by a design doc or non-obvious constraint, add a comment explaining why the code behaves the way it does."
📝 Suggested comment
+ // Why: only replay DOM.focus for the immediately next Input.insertText — any + // other intervening command (tab switch, screenshot, eval, navigation) means + // the stored focus target may no longer be relevant, so drop it. const effectiveSessionId = this.getEffectiveSessionId(msg.sessionId)Source: Coding guidelines
src/main/browser/cdp-ws-proxy.test.ts (1)
331-503: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winSolid coverage of the replay/suppression/error paths — consider also covering the bringToFront/captureScreenshot invalidation branches.
These new tests correctly validate replay ordering, session-id propagation, suppression after intervening traffic, and both failure paths (initial focus fails vs. replay fails), matching the implementation in
cdp-ws-proxy.ts. One gap: there's no test exercising thatPage.bringToFrontorPage.captureScreenshotclears a pendingDOM.focus(Lines 282 and 295 incdp-ws-proxy.ts), even though those are explicit new branches in this PR.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: 91345e4c-b111-4d74-836c-dd6e55487996
📒 Files selected for processing (2)
src/main/browser/cdp-ws-proxy.test.tssrc/main/browser/cdp-ws-proxy.ts
# Conflicts: # src/main/browser/cdp-ws-proxy.test.ts # src/main/browser/cdp-ws-proxy.ts
Collapse the three per-handler deletes (Page.bringToFront, Page.captureScreenshot, catch-all) of the pending DOM.focus replay into a single guarded delete keyed on 'not DOM.focus and not Input.insertText'. States the invariant in one place and removes the trap where a future early-returning handler forgets to clear the stale focus. Also clear the map on client teardown and shorten the field comment. Co-authored-by: Orca <[email protected]>
# Conflicts: # src/main/browser/cdp-ws-proxy.ts
…laration emit Co-authored-by: Orca <[email protected]>
…id-flight Co-authored-by: Orca <[email protected]>
Co-authored-by: Orca <[email protected]>
|
Took this over to get it across the line, @rodboev. Summary of what I validated and changed: Validated
Changed on top of your work (review-driven)
Thanks for the fix — the core focus-replay design (per-session, one-shot, synchronously-stored to close the pipelining race) held up well under review. |
|
Merged (squash) — thanks @rodboev! This passed the full review bar: the DOM.focus-replay fix is validated against a real, reproduced-on-main bug (native |
|
Thanks Rod 🙏 The native-focus-blurs-the-DOM-selection insight is chef's kiss — that's a nasty, invisible bug and you nailed the mechanism exactly. Always a pleasure reviewing your stuff. 🐋 |
…lyai#7041) * fix(browser): restore programmatic focus for CDP text insertion * test(browser): cover the DOM focus replay error path * fix(browser): serialize DOM.focus replay against pipelined Input.insertText * refactor(browser): consolidate pending-focus invalidation into one guard Collapse the three per-handler deletes (Page.bringToFront, Page.captureScreenshot, catch-all) of the pending DOM.focus replay into a single guarded delete keyed on 'not DOM.focus and not Input.insertText'. States the invariant in one place and removes the trap where a future early-returning handler forgets to clear the stale focus. Also clear the map on client teardown and shorten the field comment. Co-authored-by: Orca <[email protected]> * test(browser): annotate mock webContents return type for portable declaration emit Co-authored-by: Orca <[email protected]> * fix(browser): drop pending focus replay when the client disconnects mid-flight Co-authored-by: Orca <[email protected]> * fix(browser): re-check active client after DOM.focus replay round-trip Co-authored-by: Orca <[email protected]> --------- Co-authored-by: Neil <[email protected]> Co-authored-by: Orca <[email protected]>
Description
Agent-browser drives the embedded Electron webview over a CDP WebSocket proxy. To type into a field it sends
DOM.focus(to focus the target element) followed byInput.insertText. The proxy calls the nativewebContents.focus()immediately before forwardingInput.insertText(Electron webview guests need native focus for synthetic input). That native focus blurs the elementDOM.focushad just selected, soInput.insertTextlands on nothing and the text is silently dropped.This restores programmatic focus by remembering each session's last
DOM.focusparams and replaying thatDOM.focusin between the nativewebContents.focus()and theInput.insertText— re-selecting the intended element so the text actually goes in.Key properties of the fix (
src/main/browser/cdp-ws-proxy.ts):Input.insertText. Any other CDP command (Page.bringToFront,Page.captureScreenshot, navigate/reload, eval traffic, …) clears it, because those can move DOM focus.set()synchronously before anyawait, so anInput.insertTextpipelined in the same tick still finds the pending focus (covered by a test whereInput.insertTextis dispatched whileDOM.focusis still in flight).forwardInsertTextre-checksisActiveClientafter each await (the initial focus and the replay round-trip), so a client that disconnects mid-flight never injects a strayDOM.focusorInput.insertTextinto the live user page. Cleared on client disconnect/replacement.DOM.focusis never stored/replayed; a replay that fails surfaces the error on the insert instead of typing into the wrong place.Evidence
Bug is real and present on
main—main'sInput.insertTextpath only callswebContents.focus()and forwards, with noDOM.focusreplay. A focused repro onorigin/mainproduces the sequence['DOM.focus', 'Input.insertText'](no replay); with the fix it is['DOM.focus', 'DOM.focus', 'Input.insertText'].Unit coverage — 11 targeted tests in
cdp-ws-proxy-focus-replay.test.ts(root + OOPIF replay, no-replay after eval, failed-focus not replayed, replay-error surfaced, pipelining race,Page.bringToFront/Page.captureScreenshotinvalidation, reconnect isolation, and two disconnect-mid-flight guards), plus the existingcdp-ws-proxysuite.ELI5
The robot types by first "clicking" a text box, then typing. But right before typing, the app has to tap the window to wake it up — and that tap un-clicks the box, so the letters go nowhere. This change re-clicks the box in the split second between the tap and the typing, so the words land where they're supposed to.
Trade-offs
DOM.focusround-trip perInput.insertTextthat follows a focus. This is inherent to the native-focus constraint (native focus must happen, and it blurs), and it only occurs on the focus→insert pattern, not on read-only eval/wait polling.DOM.focusbeforeInput.insertText. No regressions to the printToPDF / screenshot / navigate / reload paths.