Skip to content

fix(browser): read the engine's messages in the turn they arrive, not the next frame - #640

Merged
Hona merged 2 commits into
devfrom
browser-tools/rpc-quiet-wake
Nov 21, 2026
Merged

Hona merged 2 commits into
devfrom
browser-tools/rpc-quiet-wake

Conversation

@Hona

@Hona Hona commented Nov 21, 2026

Copy link
Copy Markdown
Member

An agent's browser command makes several DevTools round trips one after another. For example, click scrolls, resolves and measures the element, then moves, presses and releases the mouse. In the timing table (22 Nov, notes\for-owner-engine.md section (j)), every operation cost ours 20,000-30,000 µs more than official (the cheapest reads) and the input family about +250,000 µs (6-8 round trips).

  • Where the time went:
    • Engine: measured on its own over its socket with no app (C:\tmp\opencode\browser-tools\engine-rtt.ts), it answers a renderer DevTools call in 0.3 ms. An Input.dispatchMouseEvent takes 16.6 ms with its feat(browser-engine): add serialized per-tab InputGuard with scroll-offset hit test and focused-frame walk #444 guard, which official's dispatchInput pays too.
    • App: the engine's reader thread woke the window with the window's waker. For a shown window, Rune turns that wake into a redraw request (WakeWork::Paint), and the app polls the engine's inbox only in that next frame. So every round trip waited for a frame.
  • Fix:
    • BrowserHost::set_message_waker gives the engine's messages their own waker. Pages' frames still wake the window as before.
    • The app sets it after its first frame, in both the first window and new ones, to launch::browser_waker. That posts a quiet update_if, which reads the inbox (poll_browser) and steps the agent's commands (rpc::poll) in the turn the message arrives.
    • A frame follows only when the pane changed, as inbox_waker already does for the server's stream.
  • Unchanged: what each command sends, in what order, and every check.
    • The engine's input guard is untouched: it still hit-tests, verifies and re-verifies each input event before dispatch, and still refuses cross-origin-frame targets.
    • This only changes when the app reads the answer.
  • Engine: no change; no ENGINE_REVISION move.

Re-measured on this PR's CI build in a timing ticket (three rounds each side): in the PR comments.

… the next frame

Each DevTools round trip of an agent's browser command waited for the window's next frame: the engine's reader woke the window, Rune turned that into a redraw request, and the inbox was polled in that frame. The engine's messages now get their own waker, a quiet update that polls the inbox and steps the RPC commands at once; a frame follows only when the pane changed. Pages' frames still wake the window.
@github-actions

github-actions Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

PR test build retired — merged into dev. Install or auto-update OpenCode (Rune) from https://anoma.ly/release/opencode/desktop-rune/dev/macos-arm64-dmg.

The worker that fetches an agent's browser command woke the window too, so each command waited a frame (about 13 ms on blade, measured) before it started. It now wakes the browser's quiet waker.
@Hona
Hona merged commit 6e5ddc8 into dev Nov 21, 2026
7 checks passed
@Hona
Hona deleted the browser-tools/rpc-quiet-wake branch November 21, 2026 21:37
Hona added a commit that referenced this pull request Nov 22, 2026
…with extensions too

READER-12 #635-B2: an engine running extensions drops Page.* events but not Runtime.executionContextsCleared, which bumps the tab's generation when it lands; skipping the frame-tracker wait with extensions let back return before it, so the next command failed document_changed (seen on dev's #640 build with uBlock Origin Lite). back/forward now wait until the tab's generation moves past the one they started at, with or without extensions; without extensions the main frame's tracked URL moving on settles it too (a same-document entry clears no contexts). LOAD_SETTLE_GRACE still ends any wait.
Hona added a commit that referenced this pull request Nov 22, 2026
…indow's next frame again

The gate after the first commit (engine-message wake only reverted; published engine 8): 0/10 failures everywhere without extensions, but with uBlock Origin Lite back -> snapshot still failed [browser.document_changed] 1/10 (dev-464: 7/10). With the fetched-command wake the next command starts in the turn it is fetched, before a late DevTools event of the history commit is read. So it is not innocent: this reverts the rest of #640 (6e5ddc8), browser_waker, set_message_waker and the worker's quiet wake included. Every engine message and fetched command is read at the window's next frame, as in dev-463.
Hona added a commit that referenced this pull request Nov 22, 2026
…commit again (#675)

* fix(browser): the engine's messages wake the window again, so back/forward settle with their commit

Reverts the engine-message wake of #640. Read the turn they arrived, the engine's messages let back/forward return before their history commit's DevTools events landed, so the agent's next command failed [browser.document_changed] (dev-464). Read at the window's next frame they land together again, about 16 ms later per reply. The fetched-command wake of #640 stays.

* fix(browser): revert #640 entirely: a fetched command waits for the window's next frame again

The gate after the first commit (engine-message wake only reverted; published engine 8): 0/10 failures everywhere without extensions, but with uBlock Origin Lite back -> snapshot still failed [browser.document_changed] 1/10 (dev-464: 7/10). With the fetched-command wake the next command starts in the turn it is fetched, before a late DevTools event of the history commit is read. So it is not innocent: this reverts the rest of #640 (6e5ddc8), browser_waker, set_message_waker and the worker's quiet wake included. Every engine message and fetched command is read at the window's next frame, as in dev-463.
Hona added a commit that referenced this pull request Nov 22, 2026
… are applied (#696)

* fix(browser): back/forward return once their commit's DevTools events are applied

dev-464 showed that an agent's next command after back/forward could fail [browser.document_changed]: the command returned before the history commit's DevTools events were applied, each of which bumps the tab's generation, so the next command was authorized against the old one (#640's quick wake made it frequent; #675 reverted it, and back/forward now wait the 1.2 s grace again). A trace of every navigation event (4 configurations x 20 runs) showed that each new document brings exactly its address, the main frame's Runtime.executionContextsCleared and, unless the engine runs extensions, its Page.frameNavigated, and no other bump; every failure had one of these after the return. back/forward now record the frame tracker's counts of those two events and return when the address has moved and both (with extensions, the first) have been applied and the tab is not loading. LOAD_SETTLE_GRACE stays the bound and the fallback (same-document or same-address entries). back/forward with no entry that way return at once, as official does.

* fix(browser): back/forward also wait for the new document's main context; no spin past the grace

READER-12 should-fixes on #696: (a) clearing removes the main frame's execution context and an evaluate before the new one exists fails document_changed; with extensions the context came up to 8 ms after the clearing in 30 of 67 navigating runs (none after the navigation without, 0 of 68): back/forward now also wait for it, in both. (b) ExecPhase::next_wake: once the grace has passed, a wait still loading wakes at its deadline (the load's own messages wake the poll) instead of at a past instant every poll.

* fix(browser): back/forward don't trust the tab's history ends, and wait for a main-frame navigation the engine is known to send

READER-12b's #696 follow-ups. F1/F3: the engine refreshes can_go_back/forward only when loading changes, so a click-made same-document entry could look absent and the no-entry shortcut would skip it; the shortcut is gone: with no entry the back does nothing and the wait ends at the grace, as before #696 (measured: no-entry 1,291 ms median on dev-469 and on #706). F2: whether to wait for the main frame's Page.frameNavigated no longer rests only on the app's view of extensions: a tab whose main-frame navigations have reached the app before waits for this one too.

* refactor(browser): when a load wait is over is a pure function, with its cases as a table

ORCHESTRATOR / READER-12b should-fix on #696: the condition that decides when a back/forward has landed moves out of step_wait_load into exec/settle.rs, tested as a table: cross-document commit + load end; same-document (pushState / hash) ends at the grace; no entry ends at the grace; an iframe navigation does not end it; a load seen that is not the history move does not end it (#706's guarantee); tab closed resolves; the deadline resolves at 15 s. The comment on a tab with no frame tracker yet now says what happens.

* fix(browser): the landing condition's tests leave the repo; settle.rs depends on std only

rune-desktop has no tests (AGENTS.md; guardrails Rule 8 failed #696 on 80da0ff for the #[test] / #[cfg(test)] lines). The table moves outside the repo (operation-detox harness\browser\settle-table.rs, which includes settle.rs by path). settle.rs now holds HistoryFrom and takes the wait's elapsed time and deadline as Durations, so it builds with std alone.
Hona added a commit that referenced this pull request Nov 22, 2026
…n (re-land #640) (#732)

#640 gave the engine's messages their own quiet waker, so each DevTools round trip of an agent's
browser command no longer waits for the window's next frame. #675 reverted it because back/forward
then ended before their history commit was applied. That early end was in the settle logic, fixed
since by #706 and #696 (back/forward now wait for the commit's own DevTools events), so the faster
wake comes back unchanged.

This reverts commit b76cc0a.
Hona added a commit that referenced this pull request Nov 23, 2026
…sk for one (#819)

#732 (re-land of #640) set the browser engine's message waker in open.rs on every target, but launch::browser_waker exists only off wasm32, so the scheduled web build failed on dev (open.rs:203/206). Both lines are now native-only, as browser_waker itself is. Checked: scripts/web.ps1 -Check (cargo check -p opencode-web --lib --target wasm32-unknown-unknown) exit 0, web clock ok.
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