Skip to content

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

Merged
Hona merged 6 commits into
devfrom
browser-tools/history-commit
Nov 22, 2026
Merged

Hona merged 6 commits into
devfrom
browser-tools/history-commit

Conversation

@Hona

@Hona Hona commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

back/forward now return once the history commit's own DevTools events have been applied, instead of after the fixed 1.2 s wait that #675 restored. The aim is correct first and fast second: dev-464 returned too early, and the agent's next command failed.

The measured order

Before designing this, a trace build (#676, scratch) logged every navigation event in order: the tab updates, the main frame's Runtime.* and Page.* events, and every generation bump with its cause. It ran in four configurations (no extension; uBlock Origin Lite; a page with a cross-origin frame; both), with 20 runs each of back, forward and reload, and #640's quick wake on:

Configuration back: next command failed forward reload bumps per new document last bump, ms (median / max)
no extension 9/20 7/14 0/20 3 6.7 / 16
uBlock Origin Lite 14/20 7/13 0/20 2 5.3 / 248
cross-origin frame 12/20 6/14 0/20 3 11.4 / 124
both 4/20 7/14 0/20 2 19.1 / 535
  • What each new document brings: every one moved the tab's generation exactly 3 times without extensions:

    • its address (sync_tabs);
    • the main frame's Runtime.executionContextsCleared;
    • the main frame's Page.frameNavigated.

    With extensions it moved twice, because the engine keeps Page.* events to itself (cdp_ledger).

  • Why the next command failed: every failure had one of these bumps after back/forward returned.

    • The result goes out after the state taken at return.
    • A later bump goes out in a later state, so the next command was authorized against the old generation: want 25 now 26 in the trace.
  • Why reload never failed: its wait ends on the load, 34–92 ms in, after its last bump (15–21 ms).

The change

  • rpc/frames.rs: the frame tracker counts the main frame's Runtime.executionContextsCleared (top-level session) and Page.frameNavigated (no parent, top-level session).
  • exec/nav.rs: back/forward record the address and both counts when they start. They return when the address has moved and the count of cleared contexts has gone up. Without extensions, the count of main-frame navigations must also have gone up. The tab must not be loading.
    • LOAD_SETTLE_GRACE (1.2 s) stays the upper bound. It is also the fallback for an entry that sends none of these, such as a same-document or same-address entry.
    • With no entry that way, the back does nothing and the wait ends at the grace, as before. The tab's can_go_back/can_go_forward are not consulted: the engine refreshes them only when loading changes, so a click-made same-document entry could look absent (READER-12b F1).
    • The main-frame navigation is awaited whenever the tab's main-frame navigations have reached the app before, whatever the app believes about extensions (READER-12b F2).
    • The new document's main execution context must exist again too (READER-12 should-fix). Past the grace, a wait still loading wakes at its deadline, not every poll.
  • browser-pane extensions_active(): whether the running engine was started with --extension. Only then are Page.* events dropped.
  • exec/mod.rs next_wake: a back/forward wait wakes at the grace until its commit is applied.
  • No engine change: ENGINE_REVISION stays 9, and no wire message changes.
  • Not in this PR: fix(browser): read the engine's messages in the turn they arrive, not the next frame #640's quick wake stays off. Bringing it back is a separate change, measured with this condition in place.

Proof (in the comments, before merge)

  • Sequence gate on the published engine 156.0.26-9: back→forward, back→snapshot, back→evaluate, forward→snapshot, in all four configurations, 20 runs per cell, 0 first-try failures required. The published dev-469 is the before column.
  • Driven cases:
    • a same-document entry made by a real click;
    • a page the back/forward cache can't keep (an unload handler, Cache-Control: no-store);
    • slow pages (800 ms, 3 s);
    • back with no entry;
    • back twice without waiting;
    • a page that redirects itself on load (the next command must get an honest document_changed).
  • Timing: back/forward medians and worst cases per configuration, before and after, as windows.
  • Smoke: tools\smoke.ps1.

… 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.
…ext; 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.
@github-actions

github-actions Bot commented Nov 22, 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.

Hona added a commit that referenced this pull request Nov 22, 2026
…ts load, not a 10 ms poll

READER-12b F1 on #706: with history set, next_wake stayed at the (past) grace, so a back/forward still loading after 1.2 s was polled every 10 ms until its load ended. Past the grace it now wakes at its deadline; the load's own messages wake the poll, as #696 does.
Hona added a commit that referenced this pull request Nov 22, 2026
…tory change has landed (#706)

* fix(browser): back/forward never end their wait early, before the history change has landed

A back/forward wait ended at the load's end when a poll happened to see the page loading, which can be before the history change's DevTools events land; each bumps the tab's generation, so the agent's next command failed [browser.document_changed]. Measured on the published dev-469: 1 in 30. The wait now always runs to LOAD_SETTLE_GRACE (1.2 s) for back/forward, as 9 in 10 already did; next_wake wakes at the grace for them even after a load. Old since #287 (20 Nov). #696 is the real fix.

* fix(browser): a back/forward still loading past the grace waits for its load, not a 10 ms poll

READER-12b F1 on #706: with history set, next_wake stayed at the (past) grace, so a back/forward still loading after 1.2 s was polled every 10 ms until its load ended. Past the grace it now wakes at its deadline; the load's own messages wake the poll, as #696 does.
…commit

# Conflicts:
#	apps/desktop/src/surfaces/browser/rpc/exec/mod.rs
#	apps/desktop/src/surfaces/browser/rpc/exec/nav.rs
@Hona

Hona commented Nov 22, 2026

Copy link
Copy Markdown
Member Author

Merged dev 5054ee4 (dev-471) in at 98c1426. Conflicts with #706 (same lines in exec/nav.rs and exec/mod.rs) resolved to this PR's version: #706's 'history: bool' gives way to 'history_from', which never ends a back/forward at a load it happened to see either. One addition so that holds with no frame tracker yet: start_history now always makes a HistoryFrom (counts from zero), so such a wait runs to the grace instead of taking the old saw_loading path. Next: READER-12b's follow-ups (F1 no-entry and click-made pushState; F2 extensions_active vs the engine; F3 no-entry waits for !loading), then the full gate.

Hona added 3 commits November 22, 2026 23:00
…it 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.
…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.
… 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
Hona merged commit 4ebe971 into dev Nov 22, 2026
7 checks passed
@Hona
Hona deleted the browser-tools/history-commit branch November 22, 2026 14:19
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.
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