Skip to content

fix(browser): only pixel-capturing commands wait for the page to be drawn - #22528

Merged
Jinwoo-H merged 6 commits into
mainfrom
browser-commands-drop-visibility-lease
Sep 23, 2026
Merged

Jinwoo-H merged 6 commits into
mainfrom
browser-commands-drop-visibility-lease

Conversation

@Jinwoo-H

@Jinwoo-H Jinwoo-H commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor
Files Added Deleted Net
Test 3 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​83 $\color{#cf222e}{\Huge{\mathbf{−}}}$​134 $\color{#cf222e}{\Huge{\mathbf{−}}}$​51
Prod 7 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​65 $\color{#cf222e}{\Huge{\mathbf{−}}}$​156 $\color{#cf222e}{\Huge{\mathbf{−}}}$​91

ELI5

When a phone or an agent drives a page in Orca's built-in browser, Orca used to ask the desktop window to redraw that page before every single command, and wait for it. If the desktop window was minimized or in the background, that wait always ran to its 2-second limit. Now Orca only waits for a redraw when a command actually needs pixels (a PDF, or a raw passthrough command that may take a screenshot or recording). Clicks, typing, scrolling, mouse moves, page scripts and snapshots run immediately.

What Changed

Before: every targeted browser command (click, mouse move, wheel, type, snapshot, eval, ...) first took an "automation visibility" lease. The desktop renderer marked the page as drawable and waited for two animation frames of the desktop window, capped at 2 s. With the desktop window minimized or throttled, each command paid the full cap. From a phone this shows up as taps and scrolls that each stall for ~1-2 s.

After: ordinary commands run straight against the page. Only commands that can capture pixels still take the lease:

  • exec (raw passthrough, which can run screenshot / record)
  • pdf

Screenshot and full-page screenshot are unchanged; they already manage their own lease.

Mechanism: the queue option ensureVisible (default on, opted out with ensureVisible: false) is replaced by needsPaint (default off, opted in by exec and pdf). The ensureVisible: false call sites are deleted. Queued commands now look up the page's webContents and set up its session when they start running, not when they are queued. For exec/pdf that happens after the lease, so the leased path no longer needs its own refresh step. If a page gets a new guest webContents, browser:registerGuest reports it to the bridge in the same tick (onProcessSwap). That destroys the page's session and rejects the commands queued for it with browser_tab_closed, as it did before this PR. The next command runs against the new guest. The old session-restart path (restartSessionForTarget) had no remaining caller and is deleted.

Why

Measured on a real Electron 43 window: input events, page JavaScript, layout, lazy loading and the accessibility tree all work on a page hidden with display:none, even with the window minimized. Only pixel capture (screenshots, screencast frames) needs the page drawn. PDF keeps the lease until it is checked on a hidden page. The lease never mounted anything either: acquireAutomationVisibility is a no-op for an unregistered page (browser-manager-visibility.ts), and the runtime wakes an explicitly targeted page before the command runs (runtime-browser-commands-active-screencasts-by-page-id.ts). Leasing also moved pages into a drawn-but-transparent state where scrolling is slower.

Alternatives considered: shortening the 2 s cap, or skipping the wait when the window is hidden. Both keep a per-command round trip to the desktop renderer for work that never needed it; flipping the default removes the round trip entirely and makes the rare case the explicit one.

Supersedes draft PR #22390.

Linked Issue

Relates to Linear STA-8024 (phone taps stall ~2 s). Making pixel capture itself fast on a throttled window is follow-up work.

Visual Proof

N/A — no visible UI change; the change is command latency. Timings below.

Testing

Live, background dev build (ORCA_BACKGROUND_LAUNCH=1), browser tab on a local page, desktop switched to no active worktree so the page is hidden:

before (dev build) after
orca mouse move x5 ~1000 ms each 123-141 ms each
orca mouse wheel x5 ~1000 ms each 125-138 ms each
same, desktop renderer frozen (Page.setWebLifecycleState frozen) ~2150 ms each 123-137 ms each

CLI round-trip floor (orca tab list) on the same build was 124-142 ms. The page received every event while hidden (mousemove count and scrollY advanced as expected, document.hidden === true). orca pdf still takes the lease and returns a valid PDF; PDF output on a hidden page without the lease was not checked.

Automated:

  • New: an ordinary command (snapshot, click, mouse click) does not take the lease — fails against the old code.
  • exec and pdf do take the lease, including the page re-registration path — fail if needsPaint is removed from them.
  • Keypress page-death tests adjusted for one fewer page lookup (the extra lookup came from the lease's re-resolve).
  • Re-registration tests now report the swap the way production does (onProcessSwap): a click queued behind a pdf whose lease swaps the page's guest is rejected with browser_tab_closed, the pdf itself runs on the new guest, and intercept routes survive the swap.
  • pnpm tc:node, pnpm run check:code-quality:changed pass. src/main/browser + src/main/runtime suites pass except browser-manager-viewport-ownership.test.ts, which fails identically on main.

Tested on macOS only. Not done: Windows, Linux, a page reached over SSH, and PDF on a hidden page without the lease. The change is in main-process command routing only, with no platform- or SSH-specific code paths.

  • I manually tested these changes locally
  • Automated tests added/updated, or explained why not below

AI Disclosure

Review

Agent skill upstream boundary

  • Not applicable, or this change follows docs/reference/agent-skill-sharing-upstream-boundary.md and copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.

Notes

Remote/mobile: commands from paired phones take the same main-process path and benefit the same way; no wire change.

Checklist

  • This PR is small and focused
  • I explained what changed and why (ELI5, the user-facing before/after, the mechanism, and why over the alternatives)
  • Before/after screenshots or videos attached for UI changes, or N/A with reason
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote, and path/shortcut impact considered (or N/A)
  • pnpm lint, pnpm typecheck, pnpm test, and pnpm build pass (or CI will cover; local preferred)

…rawn

Every targeted browser command used to take an automation-visibility lease,
which waits for two desktop-window animation frames (capped at 2 s). When the
desktop window is minimized or throttled, that wait always hits the cap, so
each phone tap or agent command stalled for up to 2 s (STA-8024).

Input, page JavaScript, layout and accessibility snapshots all work on a
hidden page; only pixel capture needs it drawn. The lease is now opt-in via
`needsPaint` on the two commands that can capture pixels through this path
(`exec` passthrough and `pdf`); `ensureVisible` is removed. Screenshots keep
managing their own lease.

(Commits 24821ae and 07b4a09 carry this change under WIP messages.)
…mands used

Only exec and pdf take the automation-visibility lease now, and both create an
agent-browser session, so the lease's re-registration path always recreates
the session. Remove the `recreate` option from restartSessionForTarget and the
options passthrough that fed it. Also fix the keypress test comment that still
counted the lease's extra page lookup, and note that screenshots lease inside
the screenshot lock.
@coderabbitai

coderabbitai Bot commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

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

📝 Walkthrough

Walkthrough

The browser bridge now uses needsPaint to control automation visibility for queued commands. Screenshot, full-page screenshot, PDF, and generic exec use paint-aware routing. Ordinary commands do not acquire visibility. The queue resolves the command target during execution. The bridge removes the target-session restart method. Tests cover visibility leases, page re-registration, and updated keypress lookup counts.

Merge Risk: 🔵 Low · up to 11bd9

Only screenshot, PDF and generic exec commands now wait for the page to be drawn, which speeds up ordinary input on hidden pages. Queued commands look up the current page when they start. Standard page re-registration still tears down the old session and rejects stale commands. The remaining gap is test-only: the intercept-route test cannot tell the replacement proxy from the old one. This does not block merging.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main change: only pixel-capturing browser commands wait for the page to be drawn.
Description check ✅ Passed The description is complete and matches the template. It explains the user impact, mechanism, rationale, linked issue, testing, platform coverage, and checklist status. It also documents the known fai…
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 10 files.
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.
✨ Finishing Touches
📝 Generate docstrings
  • Commit to this branch
  • Create a new PR

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: Advanced

Run ID: 78e9adcc-8f80-46e2-b597-4f9dbe9ba4d8

📥 Commits

Reviewing files that changed from the base of the PR and between 1116c54 and 724d69d.

📒 Files selected for processing (10)
  • src/main/browser/agent-browser-bridge-automation-visibility.test.ts
  • src/main/browser/agent-browser-bridge-capture-commands.ts
  • src/main/browser/agent-browser-bridge-interaction-commands.ts
  • src/main/browser/agent-browser-bridge-keypress-input.test.ts
  • src/main/browser/agent-browser-bridge-lifecycle.ts
  • src/main/browser/agent-browser-bridge-queue.ts
  • src/main/browser/agent-browser-bridge-session-lifecycle.test.ts
  • src/main/browser/agent-browser-bridge-state-commands.ts
  • src/main/browser/agent-browser-bridge-types.ts
  • src/main/browser/browser-manager-visibility.ts

Included review availability: Your plan provides up to 10 included reviews per hour; 7 remain after this review.

@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 critical issues — two rough edges worth a look.

Reviewed changes

Read the complete 10-file diff at head 724d69d7 for the switch from a default-on visibility lease to an opt-in needsPaint flag in main-process browser command routing.

  • Lease becomes opt-in — EnqueueTargetedCommandOptions.ensureVisible (default on) is replaced by needsPaint (default off); executeWithVisibleTarget short-circuits unless a command opts in.
  • Only pixel capture opts in — exec (agent-browser-bridge-state-commands.ts:269) and pdf (agent-browser-bridge-interaction-commands.ts:257); screenshot/fullPageScreenshot keep their internal lease and drop the now-redundant ensureVisible: false.
  • Session recreation simplified — restartSessionForTarget drops { recreate } and always calls ensureSession, which is valid because after the change only exec/pdf still reach the refresh path, and both use the default ensureSession.
  • Tests retargeted — new negative test for ordinary commands, an exec-lease test, a pdf re-registration test; the snapshot-lease and stale-session-after-re-registration tests removed; keypress page-lookup counts 3→2.

ℹ️ The "works while hidden" claim rests on manual measurement, not coverage

The lease's prior rationale, named in the comment this PR deletes, was lazy-loading sites (browser-manager-visibility.ts:232 before the change). The replacement comment asserts input, JS, layout, lazy loading, and the accessibility tree all work on a display:none page. The new does not lease test only proves routing changed — it never exercises a hidden page. The PR's manual table measures mousemove and scrollY, not lazy-loaded content or accessibility-tree completeness, which are the terms the old rationale named and the ones most plausibly sensitive to display:none.

Technical details
# Add coverage (or recorded evidence) for ordinary commands on a hidden page

## Affected sites
- `src/main/browser/agent-browser-bridge-automation-visibility.test.ts:82-96` — the new test asserts `acquireAutomationVisibility` is not called; it would also pass if `snapshot` returned nothing on a real hidden page.
- `src/main/browser/browser-manager-visibility.ts:232` — the lease-purpose comment this PR rewrites.

## Required outcome
- Evidence that the claim holds for the specific terms the old rationale named, or a scope note making the residual risk explicit. A focused test or captured transcript for a lazy-loaded page and for the accessibility tree on a parked pane is the strongest form.
- `wait --selector`, `scrollintoview`, and `snapshot` are the commands whose correctness depends on the claim most directly.

## Open questions for the human
- On a page hidden with `display:none` at the *embedder* (the `<webview>` element), does Chromium's guest still deliver `IntersectionObserver` callbacks and include nodes outside the visual viewport in `Accessibility.getFullAXTree`? The guest's own document is not `display:none`, so the answer is not obvious from the code.

ℹ️ Ordinary commands no longer re-resolve a re-registered guest

Before this PR every command took the lease and then ran refreshTargetAfterAutomationVisibility, which re-resolved the target and restarted the session when the page's webContentsId had changed. Now only exec/pdf do. The standard re-registration paths look covered — browser:registerGuest fires onProcessSwap, which destroys the session and rejects queued commands, and the runtime awaits waitForTabRegistration before routing — but one writer bypasses that door: the client-hosted page guest binding calls browserManager.registerGuest/unregisterGuest directly.

Technical details
# Confirm or cover the client-hosted guest-binding re-registration path

## Affected sites
- `src/main/browser/agent-browser-bridge-queue.ts:99` — ordinary commands now skip `refreshTargetAfterAutomationVisibility`.
- `src/main/browser/browser-client-page-guest-binding.ts:34,55` — `bind`/`release` call `browserManager.registerGuest`/`unregisterGuest` directly, so neither `onProcessSwap` nor `onTabClosed` fires.
- `src/main/browser/browser-client-page-automation-runtime.ts:57-77` — the bridge is notified on retire only when the automation runtime holds a matching registration.

## Required outcome
- Either confirm that local ordinary agent-browser commands never target a purely user-driven client-hosted page (no change needed), or add a regression test that an ordinary command routes to the replacement registration after such a page re-registers.

## Open questions for the human
- Can `browser-client-page-guest-binding.bind` replace a page's guest while an ordinary (non-`needsPaint`) command is already resolved or queued on that page id?

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

…ueued

Enqueue resolved the page's guest webContents and created its session up
front. Before this PR every command re-resolved after its lease, which hid
that; now a command queued behind exec/pdf, whose lease can re-register the
page with a new guest webContents, would run against the old one.

Enqueue now only picks the page (and so the queue). The webContents lookup
and session setup move into the queued job and run when the command starts,
after the lease for exec/pdf.

The lease path's refresh collapses into that step. Resolving after the lease
already sees a re-registration that happens during the lease, so the only
thing left from the refresh is the case where the page's existing session is
bound to an older guest: that session is restarted on the new one (keeping
intercept routes) and the active-tab pointers move with it. That check now
covers every session-backed command, not only leased ones.

Admission is still checked at enqueue and again when the job starts.
Every renderer re-registration of a page with a different guest goes through
browser:registerGuest, which calls onProcessSwap in the same tick. That
destroys the page's session and rejects its queued commands, so by the time a
queued command runs, no session can still be bound to an older guest. The
reconcile in the queue and restartSessionForTarget, its only caller, are
removed along with the two tests that called it directly.

A queued command now resolves the page's current guest, ensures its session,
and runs. The re-registration tests now report the swap through
onProcessSwap, as production does: commands queued behind the swapping lease
are rejected with browser_tab_closed, the leased pdf runs on the new guest,
and intercept routes survive the swap.

@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

Reviewed the delta since the prior pullfrog review at 724d69d7 — commit 9de773e resolves each command's page when it runs, so commands queued behind a pixel-capturing command no longer run against a pre-re-registration webContents.

  • Target resolved at run time — enqueueTargetedCommand captures only the browserPageId; executeQueuedCommand (renamed from executeWithVisibleTarget) resolves the full target inside its queue slot, after any needsPaint lease.
  • Session reconciliation moved into the run path — refreshTargetAfterAutomationVisibility becomes resolveSessionTarget, comparing the session's bound webContentsId to the freshly resolved target, moving the active-tab pointers, and restarting the session on the new guest; ensureSession: false commands get the fresh target without session work.
  • Regression tests added — a click and a snapshot queued behind a pdf whose lease re-registers the page dispatch to the new webContents, and the pdf lease test now asserts the old guest never gets a proxy ([200]). Both assertions fail on the previous head.

Verified locally: pnpm tc:node passes; the affected test files pass (42 tests); the src/main/browser suite fails only in browser-manager-viewport-ownership.test.ts, which the PR body notes fails identically on main.

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

@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

Reviewed the delta since the prior pullfrog review at 9de773e — commit 11bd99b removes the session-restart reconcile and routes guest swaps through onProcessSwap.

  • Session-restart reconcile deleted — resolveSessionTarget, refreshTargetAfterAutomationVisibility, and restartSessionForTarget are gone; executeQueuedCommand resolves the current target at execution time and calls ensureSession directly, relying on onProcessSwap to retire a page's session when its guest changes.
  • Guest swaps modelled the production way — the leased-swap test reports the swap through onProcessSwap in the same tick as the mapping change; the click queued behind the lease is rejected with browser_tab_closed, the pdf runs on the new guest, and the intercept-route test keeps its patterns across the swap.
  • Test surface trimmed — the restartSessionForTarget unit tests are deleted with the method.

Verified locally: pnpm tc:node passes; the affected test files pass (40 tests); the src/main/browser suite fails only in browser-manager-viewport-ownership.test.ts, which the PR body notes fails identically on main.

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

Jinwoo-H added a commit that referenced this pull request Sep 23, 2026
#22528 now resolves a queued command's page and session when the job
starts and lets onProcessSwap reject commands queued behind a guest swap.
Kept that shape and dropped its optional paint lease, since captures hold
paint in cdp-screenshot.ts: a job is admission check, resolve target,
ensureSession, execute. Its lease tests go with the deleted lease test
file; the swap-while-queued case moves to the session lifecycle tests,
driven by a running command instead of a lease.
@Jinwoo-H
Jinwoo-H merged commit 7f203ad into main Sep 23, 2026
1 check passed

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

🧹 Nitpick comments (1)
src/main/browser/agent-browser-bridge-automation-visibility.test.ts (1)

245-245: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Distinguish the replacement proxy in the intercept-route test.

The mock reports port 9222 for both proxies. The later assertions check two route calls to that port, so they cannot detect a restored route sent to the old proxy instead of webContents 200. Give the proxies distinct ports and assert that the second route uses the replacement proxy’s port. (raw.githubusercontent.com)

Based on learnings: a test must distinguish mock instances when both can produce the same observable output.

Source: Learnings


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Advanced

Run ID: fdbd03db-2a32-40ef-b216-02ce62bfe35e

📥 Commits

Reviewing files that changed from the base of the PR and between 724d69d and 11bd99b.

📒 Files selected for processing (4)
  • src/main/browser/agent-browser-bridge-automation-visibility.test.ts
  • src/main/browser/agent-browser-bridge-lifecycle.ts
  • src/main/browser/agent-browser-bridge-queue.ts
  • src/main/browser/agent-browser-bridge-session-lifecycle.test.ts
💤 Files with no reviewable changes (2)
  • src/main/browser/agent-browser-bridge-session-lifecycle.test.ts
  • src/main/browser/agent-browser-bridge-lifecycle.ts

Included review availability: Your plan provides up to 10 included reviews per hour; 5 remain after this review.

Jinwoo-H added a commit that referenced this pull request Sep 23, 2026
…desktop window

Screenshots were the last browser commands that still borrowed the desktop
window: they took the per-page automation-visibility lease, which waits for
two desktop-window animation frames (capped at 2 s) and never arrives when the
window is minimized or throttled. Only pixel capture actually needs a page
drawn — input, scripts, layout, the accessibility tree and PDF all work on a
hidden page.

Capture now takes a main-owned paint hold: a synchronous, per-page ref-count
that tells the renderer one way (no reply awaited) to keep the page drawn and
keeps the desktop renderer unthrottled while held. Both Orca's full-page
capture and the agent-browser helper's screenshots take it in
cdp-screenshot.ts and retry on a bounded schedule until the page answers with
a frame; a CDP error fails fast.

Deleted: the queue's needsPaint lease, the executeJavaScript acquire path and
its two racing 2 s timeouts and late-token cleanup, the renderer's rAF wait and
window bridge, the capture commands' own leases, the fixed 300/500 ms settle
waits, and the global one-screenshot-at-a-time lock.

Rebased onto main after #22528 landed; content identical to the reviewed
branch head 7b390ed.
Jinwoo-H added a commit that referenced this pull request Sep 24, 2026
…desktop window (#22534)

* fix(browser): let pixel capture hold its own page drawn, without the desktop window

Screenshots were the last browser commands that still borrowed the desktop
window: they took the per-page automation-visibility lease, which waits for
two desktop-window animation frames (capped at 2 s) and never arrives when the
window is minimized or throttled. Only pixel capture actually needs a page
drawn — input, scripts, layout, the accessibility tree and PDF all work on a
hidden page.

Capture now takes a main-owned paint hold: a synchronous, per-page ref-count
that tells the renderer one way (no reply awaited) to keep the page drawn and
keeps the desktop renderer unthrottled while held. Both Orca's full-page
capture and the agent-browser helper's screenshots take it in
cdp-screenshot.ts and retry on a bounded schedule until the page answers with
a frame; a CDP error fails fast.

Deleted: the queue's needsPaint lease, the executeJavaScript acquire path and
its two racing 2 s timeouts and late-token cleanup, the renderer's rAF wait and
window bridge, the capture commands' own leases, the fixed 300/500 ms settle
waits, and the global one-screenshot-at-a-time lock.

Rebased onto main after #22528 landed; content identical to the reviewed
branch head 7b390ed.

* fix(browser): probe for a frame instead of repeating the full capture

Retrying a capture resent the caller's full request, so on an already
drawn tall page (a full-page capture takes ~0.5 s) the 250 ms retry
started a second full beyond-viewport capture while the first was still
running. Measured on Electron 43: any later request makes a held page
produce a frame, and that frame answers every pending capture with a
full, correct image. So the capture is sent once and 1x1 probes follow
until it answers; their results are ignored.

Also report a detached debugger as detached rather than destroyed, and
give the layout-metrics timeout its own "did not respond" message, since
that request doesn't need a drawn page.
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.

1 participant