browser-harness icon indicating copy to clipboard operation
browser-harness copied to clipboard

helpers: CDP attach lost after goto_url in the same run — capture_screenshot fails with 'Not attached to an active page'

Open Antonio-Faure opened this issue 2 months ago • 3 comments

Version: browser-harness 0.1.13 (pip), Python 3.12, Linux container, local Chrome driven over CDP. Usage pattern: one browser-harness process per step: Python code on stdin, results on stdout, Chrome already running and shared via CDP.

Repro

Two statements in the same run — navigate, then capture:

echo 'goto_url("https://example.com"); print(capture_screenshot("/tmp/p.png"))' | browser-harness
RuntimeError: {'code': -32000, 'message': 'Not attached to an active page'}

Same failure with a file:// target. Splitting the same two statements into two separate runs works:

echo 'goto_url("https://example.com")' | browser-harness          # ok
echo 'print(capture_screenshot("/tmp/p.png"))' | browser-harness  # ok, PNG written

What it looks like

Navigation replaces the page target, and the CDP session the daemon attached to at run start is not re-established for the remainder of the run — every subsequent CDP call in that same run fails with -32000 Not attached to an active page. A fresh run attaches anew, which is why splitting into two runs works. (Hypothesis, from reading daemon.py: attach_first_page establishes the session at startup, and a mid-run navigation can invalidate it without a re-attach path.)

Why this matters

"Navigate, then inspect/screenshot what loaded" is the most natural pattern for an LLM-driven agent executing code snippets against a page. Today it silently forces a two-run dance, and the error message doesn't point at navigation as the cause.

Ask

Re-attach to the current page after a navigation within the same run (e.g. detect the lost target and call the same session-replacement path), or expose an explicit reattach() helper that callers can use after goto_url.

Antonio-Faure avatar Oct 02 '26 18:10 Antonio-Faure

Hi, I'd like to work on this issue.

I'll first reproduce the CDP session loss on current main using the provided goto_url() + capture_screenshot() case, then trace the session lifecycle around navigation and propose the smallest fix with regression coverage.

I'll post my findings here before opening the PR.

Pipirasas avatar Oct 03 '26 15:10 Pipirasas

I reproduced this on current main on Windows 10 / Python 3.12.15 with local Chrome over CDP.

A few findings suggest this is not actually a lost target/session:

  • The target ID remains identical before and after goto_url().
  • page_info() / Runtime.evaluate works immediately after the navigation in the same run.
  • capture_screenshot() immediately after goto_url() can fail with -32000 Not attached to an active page (and in some runs can remain blocked until its timeout).
  • A clean run with goto_url(...), followed by wait_for_load(), followed by capture_screenshot(...) succeeds.
  • I also verified clean runs with a short post-navigation delay; 500 ms and 1 s both succeeded in my environment.

This also seems consistent with the existing Browser Harness usage contract: SKILL.md says to call wait_for_load() after navigation, and the repository's domain-skill examples generally use goto_url() followed by wait_for_load().

So the original hypothesis that navigation replaces the target and requires a CDP re-attach does not appear to hold in my reproduction. Re-attaching on the Not attached to an active page error may also be risky because it could select a different open tab unless recovery is explicitly bound to the tracked target.

Would you prefer this issue to be treated as:

  1. documented usage (goto_url() is intentionally non-blocking and callers should wait_for_load()),
  2. a documentation/error-message improvement, or
  3. a robustness improvement where screenshot handling waits/retries safely during the short navigation transition?

I have a regression test setup locally, but I have not changed production code yet because I wanted to confirm the intended API semantics first.

Pipirasas avatar Oct 03 '26 16:10 Pipirasas

Thanks for the careful reproduction — the identical target ID and the working Runtime.evaluate right after navigation are convincing: this is the navigation-transition window, not a lost session.

My preference: 3 + 2, with 1 as the documented contract.

  • goto_url() staying non-blocking is fine, but the failure is misleading: -32000 "Not attached to an active page" points at CDP re-attach (exactly the wrong hypothesis I started from) while the actual remedy is wait_for_load() / a short settle. A bounded wait-and-retry inside capture_screenshot — bound to the tracked target, never re-selecting a tab — would remove the footgun for single-process pipelines.
  • Keep the retry short and bounded (e.g., wait for the tracked target's ready state, ~1–2 s ceiling) so genuine failures still surface quickly, and make the error message mention wait_for_load() when the transition is the cause.
  • Agreed on avoiding auto re-attach, for the tab-selection reason you gave.

A regression test mirroring the single-run repro (goto_url(...); capture_screenshot(...)) would be ideal. No objection to you opening the PR along those lines — thanks for picking it up.

Antonio-Faure avatar Oct 03 '26 17:10 Antonio-Faure