Skip to content

fix(mcp): drain a synthesized navigation before the tool replies - #745

Merged
SGavrl merged 1 commit into
h4ckf0r0day:mainfrom
ntdatt812:fix/618-mcp-drains-pending-navigation
Aug 31, 2026
Merged

SGavrl merged 1 commit into
h4ckf0r0day:mainfrom
ntdatt812:fix/618-mcp-drains-pending-navigation

Conversation

@ntdatt812

@ntdatt812 ntdatt812 commented Aug 29, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #618.

What changed

settle_synthetic_navigation() on BrowserState: consume the pending navigation, and if one landed, clear the ref table (it names elements in a document that is gone) and give the new document one event-loop slice, so a tool that reads the URL or the text next sees the landed page rather than an empty one. It is a no-op when nothing is pending.

It is called from browser_click, browser_fill, browser_type, browser_press_key and browser_evaluate, not from browser_click alone. The invariant @yinnho describes is the useful thing here: every tool that can synthesize a click, a keypress, or run a script drains pending navigation before it replies. browser_evaluate running location.href = ... or form.submit(), and an Enter through browser_press_key, land in the same pending state, so fixing only the reported tool would leave the same bug one call to the left. Those five tools become async; two existing tests that called them directly gained .await.

@yinnho's framing in the issue is right and holds here exactly: pumping is not draining. A submit click runs the page's own form glue and leaves the navigation as pending state. It becomes a request only when a driving layer converts it. The CDP path converts it, in two places: crates/obscura-cdp/src/domains/input.rs:227 after a dispatched mouse event, and crates/obscura-cdp/src/domains/runtime.rs:32 after Runtime.evaluate. MCP had no equivalent, which is the whole of your Path A versus Path B.

Why the existing pump does not already cover it

BrowserState::advance_active_page_tasks already pumps and drains, and it is armed after every dispatch. It still never gets a turn here, for two independent reasons:

tokio::select! {
    biased;
    read = reader.read_line(&mut line) => Some(read?),
    pump_result = state.advance_active_page_tasks() => { ... }
}

The select is biased with read_line first, so a client that already has its next tool call queued, which an agent always does, wins that race every time. And the response is written before the loop comes back around at all, so even an unbiased select would reply first.

That is also where the misleading part of the report comes from. location.href reports the destination because the pending navigation has already updated the page's URL state. The agent is told the submit happened while no request has left the process, which is worse than an outright failure.

Validation

New test click_on_a_submit_button_issues_the_request_before_replying, built on your repro page: a recording server serving the form, browser_fill on #q, browser_click on #go. The assertion is at the wire, not on location.href, because the URL was the thing that lied.

Red with the drain removed from tool_click, green with it:

FAIL obscura-mcp tests::click_on_a_submit_button_issues_the_request_before_replying
PASS obscura-mcp tests::click_on_a_submit_button_issues_the_request_before_replying

Full four-configuration sequence from CONTRIBUTING, in a rust:latest container so the result matches CI rather than my Windows host:

Step Result
cargo build --release -p obscura-cli --bins --features render ok
cargo nextest run --release --features render --no-fail-fast 1507 passed, 4 skipped
cargo build --release -p obscura-cli --bins --no-default-features ok
cargo nextest run --release -p obscura --no-fail-fast 23 passed
cargo nextest run --release --workspace --exclude obscura --exclude obscura-render --no-default-features --no-fail-fast 758 passed, 3 skipped
cargo check --release -p obscura-render --no-default-features ok

Rendering

Not applicable. No layout, paint, screenshot, screencast or PDF code is touched. browser_screenshot and browser_pdf are not among the five tools changed.

Performance

The added work is one process_pending_navigation() per call to those five tools, which returns immediately when nothing is pending, so the cost on the common path is a check. When a navigation is pending the tool now waits for one event-loop slice before replying, which is work the client was going to pay for on its next call anyway, moved to where it is correct. The other MCP tools are untouched.

Relationship to the other issues on this

#640's framing, that the transport never pumps the page task queue, is not what breaks this: the pump exists and is armed. It loses a race and, more fundamentally, runs after the reply. So this fix is orthogonal to any pump change and does not close #640.

I have not touched location.href reporting a destination for a navigation that has not been converted. After this change the two agree for these tools, but the underlying "URL state moves before the request" behaviour is still reachable from anywhere that does not drain, and it deserves its own issue rather than a rider here.

Checklist

  • The change is focused and does not remove existing behavior without justification.
  • Tests cover the failure or feature.
  • Existing tests pass, including render and no-render configurations when affected.
  • I checked for CPU, latency, and memory regressions.
  • Public API or user-facing behavior changes are documented.

A submit click runs the page's form glue and leaves the navigation as
pending state. It becomes a request only when a driving layer converts it.
The CDP path converts it, in Input.dispatchMouseEvent and after
Runtime.evaluate, which is why the same click POSTs over CDP and does
nothing over MCP.

The transport's own pump cannot cover this. It is armed after every
dispatch, but it sits in a biased select behind read_line, so a client that
sends its next tool call immediately, which an agent does, wins that race
every time. The reply is also written before any pump turn could run, so
browser_click answered "Clicked" while nothing had left the process, and
location.href already reported the destination, telling the agent the
submit had happened.

Add settle_synthetic_navigation and call it from every tool that can
synthesize a click, a keypress, or run a script, so the invariant holds for
the surface rather than for one tool. It is a no-op when nothing is
pending.

Fixes h4ckf0r0day#618.
@ntdatt812

Copy link
Copy Markdown
Contributor Author

Obstacle course, for the CONTRIBUTING pre-PR item I could not tick from a unit test. Companion repo at the ref your CI pins (6ebac82), same host, same invocation, base and candidate back to back:

revision correctness median latency
main (c138019) 32/33 3016.7ms
this branch 32/33 3016.0ms

No delta. The single failure is the same stage on both, and it fails on untouched main:

observer-intersection   modern-web   FAIL   expected 'io:50', got ''

So it is a property of this host rather than of the change, and I have not chased it further since it is outside the diff. It is deterministic rather than flaky here: re-running just that stage with --runs 3 --warmup 1 reproduces it exactly. Mentioning it only in case a 32/33 on a plain rust:latest Debian container is news to you; on your ubuntu-22.04 runner it may well be 33/33.

Everything else in this PR is unchanged.

@SGavrl
SGavrl merged commit 8ba43df into h4ckf0r0day:main Aug 31, 2026
3 checks passed
markjacksoncerberus added a commit to markjacksoncerberus/obscura that referenced this pull request Sep 11, 2026
…ng): the reflected verdict — A PAGE THAT TOOK FIVE SECONDS WAS NOT SLOWED DOWN, IT WAS EXECUTED (a 5s watchdog terminated the V8 isolate and never cancelled the termination, so every later script, handler and CDP evaluate died with it), and then the browser learned to keep the promise `img.alt` makes — Quests h4ckf0r0day#731–h4ckf0r0day#752 (reflection suite 10,065 scoreable → 56,591/56,660; 0 regressions over 392 ritual rows)

The region was chosen by measuring, and the measurement was the finding. A
26-file sample of the platform's biggest files came back 40,500/42,147 — the
campaign is in good health — with three rows reading `testharness did not load`
whose denominators were 10,202, 8,922 and 8,271. 27,395 subtests were not
failing. They were invisible.

h4ckf0r0day#731 ⭐⭐⭐ V8 termination is STICKY. `terminate_execution()` does not unwind a
script, it puts the isolate into a state where every later entry into JS bails
out until `cancel_terminate_execution()` clears it — and that call was never
made, so the `<reset>` script meant to recover was itself terminated on arrival.
The watchdog did not kill a script; it killed the page, and from a driver's side
that is a browser that silently stopped answering.

h4ckf0r0day#732 The budget had both halves backwards: no timeout at all under 10 kB (a
fifty-byte `while(1){}` hung the engine forever) and five seconds above it (the
heaviest real pages need 10-25s, measured). Now every script is guarded at 60s,
`OBSCURA_SCRIPT_TIMEOUT_MS` overrides, `0` disables. That one fix was +28,036
subtests before a line of reflection was written.

h4ckf0r0day#733-h4ckf0r0day#744 Then the suite could be read: 183 reflected members across 33
interfaces were simply absent — `a.ping`, `q.cite`, `td.colSpan`, `th.scope`,
`img.srcset`, `track.kind`, the whole `<frame>`/`<marquee>` family. Rebuilt keyed
by INTERFACE, because `Element.prototype` cannot express that `a.type` is a
DOMString while `input.type` is a 22-keyword enum, or that `value` is a DOMString
on `<data>` and a double on `<meter>`. The clamped types clamp in the GETTER
only; `progress.max = 0` is ignored; `<meter>`'s six numbers are a system; and
`nonce` writes an internal slot and touches the attribute only when connected,
because the attack it defends against is a page that can be made to READ it.

h4ckf0r0day#745-h4ckf0r0day#752, five more root causes from re-reading the same sample:
- `'zIndex' in el.style` was FALSE. A trap answers `get` but not `in`, so
  `'gridTemplateAreas' in body.style` reported we support nothing.
- The browser gave TWO answers to "do you support this?" — `CSS.supports()`'s
  validator chain and the property registry were different lists. Underneath:
  a CSS-wide keyword is valid in EVERY declaration, and a dozen branches forgot.
- `font-family: simple default` is a font called "simple default" — the CSS-wide
  exclusion belongs to the `<family-name>`, not to each `<custom-ident>` in it.
- An unknown media query is not a broken one. MQ4 has three truth values;
  collapsing `<general-enclosed>` to `not all` made `@media not (future-thing)`
  come out TRUE.
- `<img width=200>` was 200 of nothing — the dimension attributes never reached
  the cascade, so on the old web this browser exists for, every image, cell and
  frame laid out at `auto`.
- The URL parser said no and four APIs did not listen (`xhr.open`, `sendBeacon`,
  `location.href`, and `window.location =`, which was replacing the Location
  object with a string).

⚠️ The ritual caught THREE regressions in my own work, which is why it exists:
the computed-style proxy's `if (prop in target)` delegation began answering from
the INLINE declaration once every property had an IDL attribute; a condvar wait
with NO predicate check lost the `notify_one()` of a script that finished before
its watchdog was scheduled, so `join()` froze the ENGINE THREAD for the whole
budget (a hung browser, not a hung script, with nothing in the log); and a
generated table must not assume a reflector is ONLY a reflector — `iframe.src`
navigates the frame. Keeping the existing setters costs 69 subtests, and a
browser that scores 56,660 and cannot load an iframe is not a better browser.

Results: reflection suite 10,065 scoreable → 56,591/56,660 · dimension-attributes
952 → 1720/1720 · CSS-supports-CSSStyleDeclaration 869 → 1495/1495 · url/failure
604 → 1174/1175 · test_font_family_parsing 1768 → 2217/2232 · idlharness?include=HTML
2299 → 2773 · three dead files worth +5,245 more.

Zero regressions: 392 ritual rows, 55,689 → 55,707, two rows moved — one an
improvement (scroll-behavior-main-frame-root 1/40 → 40/40) and one the documented
flaky img file, re-proven solo at 205/228/228 on the pre-arc binary and
207/228/205 on the final one.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01EiXpEBV3Kfpy9zWXHqAhRU
whyparv pushed a commit to whyparv/obscura that referenced this pull request Nov 17, 2026
…pending-navigation

fix(mcp): drain a synthesized navigation before the tool replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants