Skip to content

The agent that grows with you

fix(browser): close regressions the ported tests caught

Teknium5df6ccf7ccab
diff --git a/tools/browser_launcher.py b/tools/browser_launcher.py
index 6b088aae6fd..698c688bdd5 100644
--- a/tools/browser_launcher.py
+++ b/tools/browser_launcher.py
@@ -129,6 +129,11 @@ def _resolve_executable() -> Optional[str]:
return None
+def _explicit_executable_configured(chromium_executable) -> bool:
+ """An operator-pinned browser binary (even a missing one) is their choice: never download over it."""
+ return chromium_executable(allow_override=True) != chromium_executable(allow_override=False)
+
+
def _maybe_autoinstall_chromium() -> bool:
"""One-shot ``pm.ensure("chromium")`` (a failed ~170 MB download must not retry per call). Docker
supplies the binary, Termux has no PM Chromium, and an explicit executable is the user's choice."""
@@ -136,8 +141,9 @@ def _maybe_autoinstall_chromium() -> bool:
if _autoinstall_attempted:
return False
_autoinstall_attempted = True
+ from hermes_cli.browser_runtime import chromium_executable
from hermes_constants import is_termux
- if running_in_docker() or is_termux():
+ if running_in_docker() or is_termux() or _explicit_executable_configured(chromium_executable):
return False
from pm import InstallError, ensure, lazy_installs_allowed
if not lazy_installs_allowed():
@@ -259,14 +265,29 @@ def _wait_for_devtools(proc: subprocess.Popen, user_data_dir: str, deadline: flo
def _singleton_pid(user_data_dir: str) -> Optional[int]:
+ """Pid of the Chromium holding ``user_data_dir``: the ``SingletonLock`` symlink (POSIX), else (Windows
+ has no symlink, only a locked ``lockfile``) the browser process whose argv names the dir."""
try:
target = os.readlink(os.path.join(user_data_dir, "SingletonLock"))
except OSError:
- return None
+ return _pid_naming_dir(user_data_dir) if os.name == "nt" else None
pid_text = target.rpartition("-")[2]
return int(pid_text) if pid_text.isdigit() else None
+def _pid_naming_dir(user_data_dir: str) -> Optional[int]:
+ """The browser (not a ``--type=`` helper) process whose argv carries ``--user-data-dir=<dir>``."""
+ import psutil
+ for proc in psutil.process_iter(["pid"]):
+ try:
+ argv = proc.cmdline()
+ except (psutil.Error, OSError):
+ continue
+ if not any(tok.startswith("--type=") for tok in argv) and _cmdline_names_dir(proc.pid, user_data_dir):
+ return proc.pid
+ return None
+
+
def _devtools_endpoint_live(port: int, path: str) -> bool:
"""The port answers ``/json/version`` with the browser id the DevToolsActivePort file names (the file
outlives a crashed Chromium and its port can be recycled by another CDP server)."""
@@ -548,8 +569,8 @@ def _is_recorded_browser(record: dict) -> bool:
if not isinstance(pid, int) or not isinstance(udd, str) or not _pid_alive(pid):
return False
expected = record.get("start_time")
- if expected is not None and _start_time(pid) != expected:
- return False
+ if expected is None or _start_time(pid) != expected:
+ return False # no recorded fingerprint, or a recycled pid: never signal on argv alone
if not _cmdline_names_dir(pid, udd):
logger.warning("browser: refusing to reap pid %s: its command line does not name %s", pid, udd)
return False
diff --git a/tools/browser_session.py b/tools/browser_session.py
index ce1974cacf4..cd9b353c66a 100644
--- a/tools/browser_session.py
+++ b/tools/browser_session.py
@@ -195,7 +195,14 @@ def _launch_target(cache_key: str) -> tuple[str, dict[str, Any], bool]:
def _create_chromium_session(cache_key: str) -> dict[str, Any]:
- if browser_in_sandbox():
+ from tools.bot_desktop import placement, runtime
+ try:
+ # Policy first: a refused placement raises, a terminal placement starts its sandbox screen; neither
+ # ever falls back to a host browser.
+ where = runtime.tool_placement()
+ except RuntimeError as exc:
+ raise RuntimeError(f"The browser cannot run here: {exc}") from exc
+ if where == placement.TERMINAL:
from tools.browser_session_sandbox import create_sandbox_session
return create_sandbox_session(cache_key)
from tools.browser_launcher import launch_chromium
diff --git a/tools/browser_session_lifecycle.py b/tools/browser_session_lifecycle.py
index 82d5b3a434f..a70df208bfd 100644
--- a/tools/browser_session_lifecycle.py
+++ b/tools/browser_session_lifecycle.py
@@ -272,7 +272,8 @@ def _force_reap(cache_key: str) -> None:
stop_cdp_supervisor(cache_key)
bs = _bs()
with bs._cleanup_lock:
- session_info = bs._active_sessions.pop(cache_key, None)
+ session_info = bs._active_sessions.get(cache_key)
+ _forget_session_tracking(cache_key, session=True) # untracked even if the release below fails
if session_info:
release_session_backend(cache_key, session_info)