Repository navigation
perf(browser): dispatch coordinate pointer input in process instead of one subprocess per event - #15309
perf(browser): dispatch coordinate pointer input in process instead of one subprocess per event#15309atreidesend wants to merge 2 commits into
Conversation
…f one subprocess per event
mouseMove, mouseDown, mouseUp and mouseWheel each spawned the agent-browser
helper per event, and the remote pane serializes its events, so one click cost
four spawns and a scroll notch two. The helper was only translating these into
Input.dispatchMouseEvent calls of its own -- no snapshot or element-ref state
involved -- so we can dispatch over the electron debugger directly, the way
mouseClick already does. Measured on the same host and build, main vs this
change (loopback p50, n=25): mouseMove 160ms -> 0.5ms, scroll notch 320ms ->
0.7ms, click 637ms -> 1.5ms.
Dispatch doesn't await the renderer's ack by default, b/c the ack is
frame-bound: ~16ms on a watched tab, ~1s per event on a tab with no screencast
subscriber, and at 1Hz two clicks can never land inside the 500ms double-click
interval -- so awaiting costs correctness as well as latency. A late failure is
reported on the caller's next pointer event instead of being swallowed, the
debugger lease is held until the in-flight command settles, and
ORCA_BROWSER_INPUT_AWAIT_ACK=1 restores the waiting behavior.
Pointer state is tracked per WebContents: position carries from mouseMove into
down/up/wheel, the buttons mask accumulates across held buttons as the helper
did, and repeated presses at the same point inside 500ms now report clickCount
2/3 (the helper hardcoded 1, which made dblclick through the remote pane
impossible to express). Wheel dispatches at the tracked pointer position
instead of the (0,0) the helper used, so inner scrollables under the cursor
actually scroll. Back/forward buttons pass through as real X1/X2 presses, and
non-finite coordinates are rejected up front -- an un-awaited dispatch would
otherwise report success and park chromium's invalid-params error on a later
event.
Result shapes match the helper byte for byte: {moved:true}, {pressed:true},
{released:true}, {scrolled:true,deltaX,deltaY}. Element-ref commands still use
the helper.
Co-Authored-By: Claude Fable 5 <[email protected]>
📝 WalkthroughWalkthroughPointer commands now dispatch through Electron CDP instead of Merge Risk: 🟡 Moderate · up to The PR substantially speeds pointer input, but deferred event dispatch can let an older failed send overwrite newer pointer state, causing later clicks, drags, or scrolling to target incorrectly. Merge should wait for this state-management issue to be fixed or explicitly accepted by the owner. 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 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: 2
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID: 02513e7c-965a-4d39-982f-e418538041af
📒 Files selected for processing (4)
src/main/browser/agent-browser-bridge-command-transport.test.tssrc/main/browser/agent-browser-bridge-pointer-input.test.tssrc/main/browser/agent-browser-bridge.tssrc/main/browser/cdp-pointer-input.ts
Included review availability: Your plan includes up to 10 reviews per rolling hour; 9 remain after this review.
…ailed dispatches Two state-tracking gaps in the in-process pointer path: Releasing one of several held buttons set the active button to 'none', so a later unqualified mouseUp defaulted to left and could never release the still-held button. The active button is now derived from the remaining mask. And b/c callers mutate pointer state before dispatching, a dispatch that failed (in either ack mode) left the state claiming buttons that were never pressed. Each dispatch now carries a pre-dispatch snapshot and the failure paths restore it, so a retry after the surfaced error starts from reality. Co-Authored-By: Claude Fable 5 <[email protected]>
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
src/main/browser/cdp-pointer-input.ts (1)
155-188: 🎯 Functional Correctness | 🟠 Major | 🏗️ Heavy liftSerialize unacknowledged pointer dispatches per tab.
processQueuestarts the next command whendispatchCdpPointerEventreturns, before the CDP promise settles. If an older send then rejects, itsrevertsnapshot overwrites newer pointer state. Await each send before advancing the queue, or apply rollback only for the matching dispatch sequence. Add a deferred-send regression test.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID: 6981ca22-5a21-47c8-a259-8196a1260678
📒 Files selected for processing (3)
src/main/browser/agent-browser-bridge-pointer-input.test.tssrc/main/browser/agent-browser-bridge.tssrc/main/browser/cdp-pointer-input.ts
🚧 Files skipped from review as they are similar to previous changes (2)
- src/main/browser/agent-browser-bridge.ts
- src/main/browser/agent-browser-bridge-pointer-input.test.ts
Included review availability: Your plan includes up to 10 reviews per rolling hour; 7 remain after this review.
|
Superseded by #20593. This PR was opened against a Two claims in this PR's description did not survive re-verification and are corrected there:
The 6 reviews here are worth reading against #20593 — the review threads don't carry over. |
ELI5
Moving the mouse in the remote browser pane launches a whole new process on the host for every wiggle, click and scroll tick -- about 160ms each, and a click is four of them. This makes the runtime deliver mouse events the same fast way it already delivers taps from the mobile app: directly, in process. A click drops from ~640ms to ~1.5ms, and double-click works through the pane for the first time.
What Changed
mouseMove/mouseDown/mouseUp/mouseWheelno longer go throughexecAgentBrowser(anexecFileof the agent-browser helper per event) -- they dispatchInput.dispatchMouseEventover the electron debugger, the waymouseClickhas done since it was added for mobile. A newcdp-pointer-input.tsmodule tracks per-tab pointer state (position, held-buttons mask, click cadence) in a WeakMap keyed by the WebContents, so state dies with the tab.Element-ref commands (
click --element,snapshot, ...) are untouched, they genuinely need the helper's snapshot state.Two deliberate design points worth flagging up front:
browser_erroron the caller's next pointer event instead of being swallowed, and the debugger lease is held until the in-flight command settles.ORCA_BROWSER_INPUT_AWAIT_ACK=1restores the waiting behavior if you'd rather have the strict semantics.clickCount: 1. Double-click through the remote pane -- select a word, open a file from a list -- was previously impossible to express at all.Why
Measured on the same host, same dev build, main vs this branch (loopback, p50, n=25):
browser.mouseMovebrowser.mouseWheelbrowser.evalmeasures 0.4-0.5ms on the same socket, so the transport was never the cost. Both ends serialize input (enqueueRemoteInputon the client, the per-session command queue on the server), which means the per-event cost multiplies into every gesture -- this is most of "the remote pane feels laggy". The helper was only translating these events intoInput.dispatchMouseEventcalls of its own, so no behavior actually depended on the subprocess.Callouts
Some deltas from the helper path, all deliberate, listed so nothing is surprising in review:
{moved:true},{pressed:true},{released:true},{scrolled:true,deltaX,deltaY}. No wire or schema change, so mixed-version pairs just get a faster server (checked againstdocs/reference/remote-wire-compatibility.md-- nothing there applies).browser.eval, no sleep -- but that ordering is measured, not contractual.mouse move(the CLI never passes x/y), so inner scrollables under the cursor never scrolled and whatever sat at the top-left corner did. Both panes (desktop and mobile) sendmouseMovebefore every wheel, so interactive scrolling gets strictly better; headless automation that relied on the (0,0) targeting would notice.dblclickwithdetail: 2, as a physical mouse would. The old path physically couldn't produce sub-500ms pairs (4x160ms per click), so nothing existing can have depended on fast pairs stayingdetail: 1-- but newly-fast CLI scripting that double-invokes a control in <500ms will now look like a double-click to the page, b/c it is one.button: 'back'/'forward'pass through as real X1/X2 presses with the right buttons-mask bits, as the helper dispatched them. (The element-click path's existing left-coercion is untouched.)mouseUpwith no button releases the held button; the helper always releasedleft, which left a right-button press stuck forever. Both panes always pass the button, so only CLI/RPC callers see this.browser_error) -- an un-awaited dispatch would otherwise report success and park chromium's invalid-params error on a later event.wc.focus()on mouseDown mirrors whatmouseClickalready does. The helper never focused anything, so relative to the old down/up path this widens the focus surface the same way every mobile tap already does -- flagging for coordination with fix(browser): restore host focus after synthetic mouse presses #10875/fix(browser): stop agent browser input from stealing the user's focus #11732 (agent-input focus stealing), which this PR neither fixes nor worsens beyond the established precedent.mouseMovebefore any down/up/wheel in the same serialized queue, which re-establishes position.Linked Issue
Fixes #15308
Visual Proof
Before/after recordings attached here -- the same click / double-click / scroll sequence driven through the exact RPCs the pane sends, at human cadence, recorded from the screencast frames the pane receives. Before is current main, after is this branch: same 1680px scrolled, but before crawls and never fires dblclick; after is instant, fires
dblclick, and selects the word.before-pointer.mp4
after-pointer.mp4
Testing
pnpm exec vitest run --config config/vitest.config.ts src/main/browser/-- the whole directory passes, including the newagent-browser-bridge-pointer-input.test.ts(13 tests: CDP payloads for move/down/up/wheel, position tracking, per-tab state isolation, clickCount escalation and reset, back/forward passthrough, non-finite rejection, deferred-failure surfacing, ack-await mode, lease release ordering, queue cleanup).pnpm run typecheck,pnpm exec oxlint,pnpm run check:code-quality:changed,pnpm run check:max-lines-ratchet-- all clean.dblclickand selects a word), right-click (contextmenu), drag-selection across a text span, plus the keyboard checks. The same suite on a main build of the same base fails both double-click checks. Latency numbers above were taken on the same rig, main vs branch.mouseClickon every mobile tap, but I haven't run them myself.AI Disclosure
Built with Claude Code (Fable 5). It started as a hand-validated patch prototype running against a remote runtime I use daily; all the numbers here come from scripted probes against real instances (including the main-branch baseline, same host and build), and claims about the helper's current behavior were re-checked by running the pinned agent-browser 0.27 binary directly. I reviewed the diff before submitting.
Notes
Security: no new inputs parsed beyond a finiteness check, RPC surface and schemas unchanged. Cross-platform: the electron debugger dispatch path is the same one
mouseClickuses on all three platforms. Remote/SSH: no wire change, server-side speedup only. Performance: hot path loses a subprocess spawn per event, nothing new beyond a small per-tab state object.Review
Adversarially reviewed with independent agent passes before submission: correctness (races, debugger-lease lifetimes, event ordering, per-tab state isolation), cross-platform (the dispatch path is the one
mouseClickalready uses on macOS/Windows/Linux; no platform-conditional code added), remote/SSH and mixed versions (no wire or schema change; result shapes byte-compared against the shipped helper binary), agent/integration compatibility (element-ref commands untouched; helper fallback preserved), performance (removes a subprocess spawn per event from the hot path), and basic security (no new parsed inputs, no new surface). Findings that survived review are disclosed in Callouts above.Agent skill upstream boundary
docs/reference/agent-skill-sharing-upstream-boundary.mdand copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.Checklist
N/Awith reasonpnpm lint,pnpm typecheck,pnpm test, andpnpm buildpass (or CI will cover; local preferred)