Raven-1/tests/_browser_cache.py
Dizhan Xue b963862e50
fix(*): keep the browser touch note, explain launch failures, isolate the test profile (#848)
## Summary

Follow-ups to #839 in the browser driver, the browser tools and the
real-browser tests.

**The touch note survives the end of an owner's binding (regression from
#839).** #839 had the driver tell the tools' `_acted` store whenever it
dropped an owner's tab binding, and the store then deleted that owner's
last-act stamp. The readback promises `note: the user interacted with
the browser since your last action` until the owner acts again, and the
end of a binding is not the end of the owner: `Browser.release()` has no
production caller, so the listener fired only on a reap after
`OWNER_IDLE_S`, on the owner's tab closing and on the browser closing.
When the reader closed an agent's tab from the panel, or the binding
idled out, the agent's next read lost the note. The listener is removed
and the driver's owner removal is back to its pre-#839 form;
`_ACTED_MAX` still bounds the store.

**A host missing a system library is told so.** The driver now answers
`Chromium is installed but cannot start here: the system library
libatk-1.0.so.0 is missing. Installing system libraries needs root, so
it is the user's step: on Debian or Ubuntu, <python> -m playwright
install-deps chromium`. Before, on x64 the loader's refusal came back as
Playwright's whole launch log, after a throwaway relaunch that could not
help; where Playwright's own dependency check runs (its linux-arm64
Chromium builds), its report suggests `playwright install-deps`, which
the driver read as "Chromium is not installed". The message says whose
step it is because tool results reach the model too.

**Only failures a fresh profile cannot fix skip the throwaway
relaunch.** The relaunch was gated on the substring "profile", and every
Playwright launch failure quotes the command line,
`--user-data-dir=<home>/.raven/browser-profile` included, so every
failure relaunched and logged "profile is busy". Every failure is still
retried once except a missing system library, Playwright's host check
and a missing executable, and the warning says the launch failed rather
than that the profile is busy. Matching Chromium's lock words instead
would be narrower and wrong under another locale: with
`LANG=zh_CN.UTF-8` a second headful launch on a held profile prints its
line in Chinese, Playwright's English rewrite of it does not fire, and
the error says only "Target page, context or browser has been closed".

**The profile path is read at launch.** `PROFILE_DIR` was computed at
import, before `tests/conftest.py` gives each test its own HOME, so the
real-browser tests used the developer's own `~/.raven/browser-profile`,
with parallel workers opening it at once; the cookie
`test_browser_real_web.py` sets turned up in a developer's own cookie
jar. `_profile_dir()` now resolves the path on each launch.

**The real-browser suites skip on a host that cannot load Chromium.**
`tests/_browser_cache.py` gains `chromium_launch_failure()`: one bare
headless launch per process, with Playwright alone. When it fails for a
missing system library, both
`tests/integration/test_browser_*real_web.py` modules skip with the
library named: the twelve `libatk-1.0.so.0` launch failures reported on
#839 become twelve skips. Any other launch failure, such as an older
build left in the cache or a timeout under load, is not a reason to
skip, and the suites fail on it as before.

`docs/browser-and-desktop.md` said a second process would get a
throwaway profile. The headless shell takes no profile lock, so a second
headless Chromium opens the same profile; the sentence now says so.

Not in this PR:

- `url_for` still predicts the landing page before the permission
prompt, so whatever moves while a person decides moves the call (the
binding-generation idea in the #600 discussion).
- Two headless Ravens share one profile with no lock; documented above,
not changed.
-
`tests/integration/test_browser_tools_real_web.py::test_two_owners_two_tabs_and_neither_takes_the_other`
hung in 2 of 73 runs of both real-browser files on main's tree -- both
times with one profile shared by every worker, never in the 24 runs with
a profile per test -- and in none of 78 runs on this branch or 240 runs
of that test alone. The cause is unknown, and counts this small cannot
credit any change here with a fix. One of the first 45 branch runs, made
before the profile change, also failed one test outright; it was not
seen again and is not explained.

## Type

- [x] Fix
- [ ] Feature
- [ ] Docs
- [ ] CI / tooling
- [ ] Refactor
- [ ] Other

## Verification

Python 3.12, the repo environment with all extras, run from this tree.

- `python -m pytest tests/test_browser_driver.py
tests/test_browser_tools.py tests/test_browser_cache.py -q`: 129 passed.
With `raven/browser/driver.py` and `raven/agent/tools/browser.py` taken
from origin/main, 15 of them fail: the touch-note test for a reader's
close and for an idle binding, the launch-time profile test, the four
host-library cases, the empty-message retry, and the seven probe tests,
which call a helper main lacks.
- Each guarded construct was mutated on its own (14 mutations, among
them: no stamp after a close, a stamp pruned on a tab close, each of the
three no-retry clauses dropped, the host-library check moved after the
not-installed one, the profile path fixed at import, the probe skipping
on any failure, the probe launching on the caller's loop); each turns at
least one test red.
- Full suite, `python -m pytest -q`: 27482 passed, 116 skipped, 2
failed. Both failures are local to this box and touch nothing this
branch changes:
`tests/test_install_script.py::test_resolve_node_dir_answers_each_case_it_exists_for`
(an npm elsewhere on PATH leaks past the fixture) and
`tests/test_subagent_node_runtime.py::test_what_cannot_be_read_names_nothing`
(the suite runs as root, which reads the file the test makes
unreadable).
- `python -m pytest tests/integration/test_browser_real_web.py
tests/integration/test_browser_tools_real_web.py -q` with a real
Chromium: 12 passed in each of 3 runs. A guard around `_profile_dir()`
logged 39 resolutions, none under the real home, and no file under
`~/.raven/browser-profile` changed during the runs.
- Report shapes: the host-library fixtures follow real launches against
a binary built to need an absent library, with Playwright's dependency
check both skipped (the loader's refusal) and forced (both of its Linux
reports). Two headless launches on one profile: the second starts. Two
headful launches on one profile under `xvfb-run`: with
`LANG=en_US.UTF-8` the error says the profile "is already in use"; with
`LANG=zh_CN.UTF-8` it carries none of Chromium's English lock words.
- `ruff check`, `ruff format --check`, `lint-imports` (10 kept), `ty
check raven`, `scripts/check_source_language.py`,
`scripts/check_large_files.py`: all pass.

- [x] Relevant tests pass locally
- [x] Relevant lint / type checks pass locally
- [x] User-facing docs or screenshots are updated when needed

## Risk

User-visible: the touch note is reported again after the reader closes
an agent's tab or the binding idles out; a launch that fails for a
missing system library reads as one line naming it, with no throwaway
relaunch; every other launch failure still gets one throwaway relaunch,
under a different warning.

- [x] Security impact considered: the new message names a root command,
and it is worded as the user's step because tool results reach the
model.
- [x] Backward compatibility considered: `Browser.on_owner_released`
(added in #839) and the module constant `PROFILE_DIR` are removed; `git
grep` finds no reader of either outside the two files changed here.
- [x] Rollback path is clear for risky changes: revert the squash
commit; nothing persisted changes shape.

## Related Issues

Follows up #839; refs #600.

---------

Co-authored-by: Claude (claude-opus-5-5[1m]) <noreply@anthropic.com>
2026-10-03 22:09:53 +08:00

168 lines
6.2 KiB
Python

"""Where Playwright keeps the Chromium a real-page test needs.
This suite redirects ``HOME`` at a per-test tmp dir (``tests/conftest.py``,
so no test reads the config of whoever is running it), and Playwright looks
its browsers up from the home directory -- so a real-page test has to aim the
look-up back at the login's own cache before it launches anything.
The rule mirrored here is ``registryDirectory`` in playwright-core's registry,
read from the driver bundled with playwright 1.62:
* ``PLAYWRIGHT_BROWSERS_PATH=0`` -- a hermetic install, inside the package;
* any other value of it -- that directory, a relative one taken from
``INIT_CWD`` or the working directory;
* otherwise ``ms-playwright`` under a per-OS cache root: ``XDG_CACHE_HOME``
or ``~/.cache`` on Linux, ``~/Library/Caches`` on macOS, ``LOCALAPPDATA`` or
``~/AppData/Local`` on Windows. Playwright refuses every other platform.
Only the home directory needs correcting: the two environment variables are
not sandboxed, so where one is set Playwright already finds the cache.
A browser in the cache is not yet one this host can run -- a missing system
library stops it at launch -- so ``chromium_launch_failure`` starts one and
names the library it could not load.
"""
from __future__ import annotations
import asyncio
import concurrent.futures
import functools
import os
import sys
try:
import pwd
except ImportError: # pragma: no cover - Windows has no pwd module
pwd = None # type: ignore[assignment]
def login_home() -> str:
"""The login directory, ignoring a ``HOME`` a fixture has sandboxed.
``pwd`` rather than ``Path.home()``, which follows ``HOME`` -- the value
every caller here is trying to see past. Without a passwd entry there is
nothing to see past it with, and ``HOME`` is the answer.
"""
if pwd is not None:
try:
return pwd.getpwuid(os.getuid()).pw_dir
except KeyError:
pass
return os.path.expanduser("~")
def playwright_cache(home: str) -> str | None:
"""The default browser cache for ``home``, or None where Playwright has none."""
if sys.platform == "linux":
root = os.environ.get("XDG_CACHE_HOME") or os.path.join(home, ".cache")
elif sys.platform == "darwin":
root = os.path.join(home, "Library", "Caches")
elif sys.platform == "win32":
root = os.environ.get("LOCALAPPDATA") or os.path.join(home, "AppData", "Local")
else:
return None
return os.path.join(root, "ms-playwright")
def browsers_dir() -> str | None:
"""The browser cache this process resolves to. Safe to call at import."""
explicit = os.environ.get("PLAYWRIGHT_BROWSERS_PATH")
if explicit == "0":
try:
import playwright
except ImportError:
return None
return os.path.join(os.path.dirname(playwright.__file__), "driver", "package", ".local-browsers")
if explicit:
return os.path.abspath(os.path.join(os.environ.get("INIT_CWD") or os.getcwd(), explicit))
return playwright_cache(login_home())
def chromium_installed(where: str | None = None) -> bool:
"""Whether a Chromium build sits in the browser cache.
A directory entry rather than ``Browser.probe()``, which only answers
whether the ``playwright`` package imports: skipping on the package alone
turned a missing binary into failures. It does not check the revision
-- a cache holding only an older build still fails the launch, loudly, and
the driver's error then names the path it looked for.
"""
target = where or browsers_dir()
if target is None:
return False
try:
return any(name.startswith("chromium") for name in os.listdir(target))
except OSError:
return False
def _bare_launch() -> None:
"""Start and stop a headless Chromium with Playwright alone; raises on failure.
On a worker thread with a loop of its own, because a caller may already be
inside a running event loop, where ``asyncio.run`` refuses to start.
"""
from playwright.async_api import async_playwright
async def launch() -> None:
async with async_playwright() as pw:
browser = await pw.chromium.launch(headless=True)
await browser.close()
with concurrent.futures.ThreadPoolExecutor(max_workers=1) as pool:
pool.submit(lambda: asyncio.run(launch())).result()
@functools.cache
def chromium_launch_failure() -> str | None:
"""The system library this host lacks for a headless Chromium, or None.
A directory in the cache says a browser was downloaded, not that this host
can run it: one missing a system library it links against fails every test
at launch, and no change to the code under test can fix that. Any other
failure answers None, so the suites run and fail on it loudly -- an older
build left in the cache (see ``chromium_installed``) or a launch that
failed under load is not something this host lacks. Started with Playwright
alone, not through raven's driver, so what it finds is the host's by
construction. Asked once per process, since it costs a browser start, and
aimed at the cache the suites' own fixture will point at, because this runs
before that fixture does.
"""
from raven.browser.driver import _host_library_gap
target = browsers_dir()
if target is None:
return None
saved = os.environ.get("PLAYWRIGHT_BROWSERS_PATH")
os.environ["PLAYWRIGHT_BROWSERS_PATH"] = target
try:
_bare_launch()
except Exception as exc: # noqa: BLE001 - a host's lack is named, anything else is no reason to skip
return _host_library_gap(str(exc))
finally:
if saved is None:
os.environ.pop("PLAYWRIGHT_BROWSERS_PATH", None)
else:
os.environ["PLAYWRIGHT_BROWSERS_PATH"] = saved
return None
def point_at_login_cache(monkeypatch) -> None:
"""Fixture body: aim Playwright at the login's cache, unless already aimed."""
if os.environ.get("PLAYWRIGHT_BROWSERS_PATH"):
return
cache = playwright_cache(login_home())
if cache is not None:
monkeypatch.setenv("PLAYWRIGHT_BROWSERS_PATH", cache)
__all__ = [
"browsers_dir",
"chromium_installed",
"chromium_launch_failure",
"login_home",
"playwright_cache",
"point_at_login_cache",
]