Skip to content

The agent that grows with you

fix(browser): a live CDP override and its reap belong to one profile

Tekniume52d48c433a2
diff --git a/tests/tools/test_browser_profile_live_cdp.py b/tests/tools/test_browser_profile_live_cdp.py
new file mode 100644
index 00000000000..7f20ae5766e
--- /dev/null
+++ b/tests/tools/test_browser_profile_live_cdp.py
@@ -0,0 +1,73 @@
+"""CLI-9 (#134889): a live ``/browser connect`` belongs to ONE profile on a multiplexed gateway.
+
+The override map is keyed by ``hermes_home_key()``; browser sessions and CDP supervisors remember
+the profile that created them, so reaping one profile's browsers never closes another's.
+"""
+import types
+
+import pytest
+
+from gateway.run import _profile_runtime_scope
+from hermes_constants import hermes_home_key
+from tools import browser_tool, browser_tool_cdp as bt_cdp, browser_tool_lifecycle as lifecycle
+from tools import browser_tool_session as bt_session
+from tools.browser_supervisor import SUPERVISOR_REGISTRY
+
+
+def profiles(tmp_path, monkeypatch):
+ a, b = tmp_path / "a", tmp_path / "b"
+ a.mkdir()
+ b.mkdir()
+ (b / "config.yaml").write_text("browser:\n cdp_url: ws://b.invalid/devtools/browser/PROFILE-B\n")
+ monkeypatch.delenv("BROWSER_CDP_URL", raising=False)
+ monkeypatch.setattr(bt_cdp, "_live_overrides", {})
+ return a, b
+
+
+def test_a_live_override_is_this_profiles_alone(profiles, monkeypatch):
+ a, b = profiles
+ with _profile_runtime_scope(a, hydrate_secrets=False):
+ bt_cdp.set_live_cdp_override("http://127.0.0.1:9333")
+ assert bt_cdp._get_cdp_override_raw() == "http://127.0.0.1:9333"
+ with _profile_runtime_scope(b, hydrate_secrets=False):
+ assert bt_cdp.live_cdp_override() == ""
+ assert bt_cdp._get_cdp_override_raw().endswith("/PROFILE-B")
+ monkeypatch.setenv("BROWSER_CDP_URL", "http://operator:9222") # an operator's env still applies
+ with _profile_runtime_scope(b, hydrate_secrets=False):
+ assert bt_cdp._get_cdp_override_raw() == "http://operator:9222"
+ with _profile_runtime_scope(a, hydrate_secrets=False):
+ assert bt_cdp._get_cdp_override_raw() == "http://127.0.0.1:9333" # the live connect wins
+ bt_cdp.set_live_cdp_override(None)
+ assert bt_cdp._get_cdp_override_raw() == "http://operator:9222"
+
+
+def test_a_new_session_records_its_profile(profiles, monkeypatch):
+ a, _b = profiles
+ monkeypatch.setattr(browser_tool, "_active_sessions", {})
+ monkeypatch.setattr(lifecycle, "_start_browser_cleanup_thread", lambda: None)
+ monkeypatch.setattr(bt_session, "_create_session_for_key", lambda key, local: {"session_name": "s"})
+ monkeypatch.setattr(bt_cdp, "_ensure_cdp_supervisor", lambda key: None)
+ with _profile_runtime_scope(a, hydrate_secrets=False):
+ info = bt_session._get_session_info("task-a")
+ assert info["home_key"] == hermes_home_key(a)
+
+
+def test_reaping_one_profile_leaves_the_other_profiles_browser_and_supervisor(profiles, monkeypatch):
+ a, b = profiles
+ key_a, key_b = hermes_home_key(a), hermes_home_key(b)
+ sessions = {"ta": {"home_key": key_a}, "ta::local": {"home_key": key_a}, "tb": {"home_key": key_b}}
+ monkeypatch.setattr(browser_tool, "_active_sessions", sessions)
+ closed = []
+
+ def close(task_id):
+ closed.append(task_id)
+ sessions.pop(task_id, None)
+ monkeypatch.setattr(lifecycle, "_cleanup_single_browser_session", close)
+ stopped = []
+ sup = lambda name: types.SimpleNamespace(stop=lambda: stopped.append(name))
+ monkeypatch.setattr(SUPERVISOR_REGISTRY, "_by_task", {"ta": sup("ta"), "vault-a": sup("vault-a"), "tb": sup("tb")})
+ monkeypatch.setattr(SUPERVISOR_REGISTRY, "_home_by_task", {"ta": key_a, "vault-a": key_a, "tb": key_b})
+ lifecycle.cleanup_profile_browsers(key_a)
+ assert sorted(closed) == ["ta", "ta::local"] and list(sessions) == ["tb"]
+ assert sorted(stopped) == ["ta", "vault-a"] and list(SUPERVISOR_REGISTRY._by_task) == ["tb"]
diff --git a/tests/tools/test_browser_use_cli.py b/tests/tools/test_browser_use_cli.py
index 511aea1fb86..7a62d078fae 100644
--- a/tests/tools/test_browser_use_cli.py
+++ b/tests/tools/test_browser_use_cli.py
@@ -1298,7 +1298,7 @@ class TestExecHybridPrivateUrlRouting:
monkeypatch.setattr(bt_cloud, "_get_cloud_provider", lambda: object())
monkeypatch.setattr(bt_cloud, "_auto_local_for_private_urls", lambda: True)
monkeypatch.setattr(bu_cli, "_exec_route_is_sidecar", {}, raising=False)
- monkeypatch.setattr(bu_cli, "_driven_daemons", set())
+ monkeypatch.setattr(bu_cli, "_driven_daemons", {})
self.cloud_keys = []
monkeypatch.setattr(bt_session, "_get_session_info",
lambda key: self.cloud_keys.append(key) or {"cdp_url": "wss://cloud.example/x"})
diff --git a/tools/browser_camofox.py b/tools/browser_camofox.py
index 0aa3ac198ad..f4be688c53e 100644
--- a/tools/browser_camofox.py
+++ b/tools/browser_camofox.py
@@ -98,7 +98,8 @@ def is_camofox_mode() -> bool:
written, a set ``CAMOFOX_URL`` still activates Camofox). A CDP override (``BROWSER_CDP_URL``
env or ``browser.cdp_url``, same precedence as ``browser_tool_cdp._get_cdp_override()``) wins.
"""
- if os.getenv("BROWSER_CDP_URL", "").strip() or _config_cdp_url():
+ from tools.browser_tool_cdp import live_cdp_override
+ if live_cdp_override() or os.getenv("BROWSER_CDP_URL", "").strip() or _config_cdp_url():
return False
try:
from tools.tool_backend_helpers import read_selection
diff --git a/tools/browser_supervisor.py b/tools/browser_supervisor.py
index 243eac97174..3ea1770f85c 100644
--- a/tools/browser_supervisor.py
+++ b/tools/browser_supervisor.py
@@ -504,6 +504,7 @@ class _SupervisorRegistry:
def __init__(self) -> None:
self._lock = threading.Lock()
self._by_task: dict[str, CDPSupervisor] = {}
+ self._home_by_task: dict[str, str] = {} # profile home that started it (``stop_home``)
def get(self, task_id: str) -> Optional[CDPSupervisor]:
with self._lock:
@@ -511,6 +512,7 @@ class _SupervisorRegistry:
def _pop(self, task_id: str) -> Optional[CDPSupervisor]:
with self._lock:
+ self._home_by_task.pop(task_id, None)
return self._by_task.pop(task_id, None)
def capture(self, task_id: str, *, timeout: float = 10.0) -> CapturedCDP:
@@ -524,6 +526,8 @@ class _SupervisorRegistry:
dialog_timeout_s: float = DEFAULT_DIALOG_TIMEOUT_S, start_timeout: float = 15.0) -> CDPSupervisor:
"""Idempotently ensure a supervisor runs for ``(task_id, cdp_url)``; one bound to a
different ``cdp_url`` or unhealthy (dead thread / stopped loop) is stopped and replaced."""
+ from hermes_constants import hermes_home_key
+ home = hermes_home_key()
with self._lock:
existing = self._by_task.get(task_id)
if existing is not None:
@@ -545,6 +549,7 @@ class _SupervisorRegistry:
supervisor.stop()
return already
self._by_task[task_id] = supervisor
+ self._home_by_task[task_id] = home
return supervisor
def stop(self, task_id: str) -> None:
@@ -552,11 +557,19 @@ class _SupervisorRegistry:
if supervisor is not None:
supervisor.stop()
+ def stop_home(self, home_key: str) -> None:
+ """Stop the supervisors one profile home started (``/browser connect`` on a multiplexed gateway)."""
+ with self._lock:
+ tasks = [task for task, home in self._home_by_task.items() if home == home_key]
+ for task_id in tasks:
+ self.stop(task_id)
+
def stop_all(self) -> None:
"""Stop every running supervisor. For shutdown / test teardown."""
with self._lock:
items = list(self._by_task.values())
self._by_task.clear()
+ self._home_by_task.clear()
for supervisor in items:
supervisor.stop()
diff --git a/tools/browser_tool_cdp.py b/tools/browser_tool_cdp.py
index 45b36c4c967..f453b874f57 100644
--- a/tools/browser_tool_cdp.py
+++ b/tools/browser_tool_cdp.py
@@ -4,7 +4,8 @@ Split out of ``tools/browser_tool.py``. Facade-owned state is read through ``_bt
import contextlib
import os
-from typing import Tuple
+import threading
+from typing import Optional, Tuple
from agent.proxy_bypass import loopback_request_kwargs
from tools.browser_tool_origin import origin_module as _origin
@@ -48,15 +49,40 @@ def _resolve_cdp_override(cdp_url: str) -> str:
return raw
+# Live ``/browser connect`` endpoints, one per profile home (``hermes_home_key()``): a multiplexed
+# gateway serves every profile from this process, so a process-wide value would reach them all.
+_live_overrides: dict[str, str] = {}
+_live_overrides_lock = threading.Lock()
+
+
+def live_cdp_override() -> str:
+ """The current profile's live ``/browser connect`` endpoint, or ""."""
+ from hermes_constants import hermes_home_key
+ with _live_overrides_lock:
+ return _live_overrides.get(hermes_home_key(), "")
+
+
+def set_live_cdp_override(url: Optional[str]) -> None:
+ """Set (or with a falsy ``url`` clear) the current profile's live endpoint."""
+ from hermes_constants import hermes_home_key
+ key = hermes_home_key()
+ with _live_overrides_lock:
+ if url:
+ _live_overrides[key] = url
+ else:
+ _live_overrides.pop(key, None)
+
+
def _get_cdp_override_raw() -> str:
"""Return the *configured* CDP override without any network I/O.
- Precedence: ``BROWSER_CDP_URL`` env (live ``/browser connect``), then ``browser.cdp_url``. Is-it-configured
+ Precedence: this profile's live ``/browser connect``, then the operator's ``BROWSER_CDP_URL`` env,
+ then ``browser.cdp_url``. Is-it-configured
gates (check_fns, ``_is_local_mode`` / ``_is_local_backend``, ``hermes doctor``) MUST use this, not
:func:`_get_cdp_override`: its 10s HTTP discovery against a stale ``cdp_url`` would stall every startup's
schema build with no error.
"""
- env_override = os.environ.get("BROWSER_CDP_URL", "").strip()
+ env_override = live_cdp_override() or os.environ.get("BROWSER_CDP_URL", "").strip()
return env_override or _origin()._browser_cfg("cdp_url", "", lambda v: str(v or "").strip(), "browser.cdp_url from config")
diff --git a/tools/browser_tool_lifecycle.py b/tools/browser_tool_lifecycle.py
index 8ac65a5aad7..11fb9569ca1 100644
--- a/tools/browser_tool_lifecycle.py
+++ b/tools/browser_tool_lifecycle.py
@@ -742,6 +742,29 @@ def _cleanup_single_browser_session(task_id: str) -> None:
_bt.logger.debug("Removed task %s from active sessions", task_id)
+def cleanup_profile_browsers(home_key: str) -> None:
+ """Close one profile home's browser sessions, CDP supervisors and harness daemons, and drop its
+ cached config slots (``/browser connect`` on a multiplexed gateway). Other profiles' browsers stay."""
+ with _bt._cleanup_lock:
+ task_ids = [task for task, info in _bt._active_sessions.items() if info.get("home_key") == home_key]
+ for task_id in task_ids:
+ _cleanup_single_browser_session(task_id)
+ _drop_last_active_binding(task_id)
+
+ def _stop_supervisors():
+ from tools.browser_supervisor import SUPERVISOR_REGISTRY # type: ignore[import-not-found]
+ SUPERVISOR_REGISTRY.stop_home(home_key)
+ _best_effort("CDP supervisor stop", _stop_supervisors)
+
+ def _stop_harness():
+ from tools.browser_use_cli import stop_harness_daemons
+ stop_harness_daemons(home_key)
+ _best_effort("Browser Use harness daemon stop", _stop_harness)
+ for cache in ("_cached_command_timeout", "_cached_snapshot_threshold"):
+ with contextlib.suppress(AttributeError, TypeError):
+ getattr(_bt, cache).pop(home_key, None)
+
+
def cleanup_all_browsers() -> None:
"""Clean up all active browser sessions (shutdown) and reset cached lookups."""
with _bt._cleanup_lock:
diff --git a/tools/browser_tool_session.py b/tools/browser_tool_session.py
index e88dbc7ca9f..9ee13dfe0b5 100644
--- a/tools/browser_tool_session.py
+++ b/tools/browser_tool_session.py
@@ -15,6 +15,7 @@ from pathlib import Path
from typing import Any, Callable, Dict, List, Optional
from hermes_cli._subprocess_compat import windows_hide_flags
+from hermes_constants import hermes_home_key
from tools.browser_tool_origin import origin as _bt
from tools import browser_tool_cdp as _cdp
from tools import browser_tool_cloud as _cloud
@@ -420,6 +421,7 @@ def _get_session_info(task_id: Optional[str] = None) -> dict[str, Any]:
session_info = dict(session_info)
session_info.setdefault("session_key", task_id)
session_info.setdefault("owner_task_id", _bt._bare_task_id_for_session_key(task_id))
+ session_info.setdefault("home_key", hermes_home_key()) # the profile reaped on /browser connect
_bt._active_sessions[task_id] = session_info
_bt._suspect_browser_sessions.pop(task_id, None) # brand-new session is healthy by definition
diff --git a/tools/browser_use_cli.py b/tools/browser_use_cli.py
index 938f1ac8ba2..07973c985f7 100644
--- a/tools/browser_use_cli.py
+++ b/tools/browser_use_cli.py
@@ -19,7 +19,7 @@ import time
from pathlib import Path
from typing import Any, Callable, Dict, List, NamedTuple, Optional
-from hermes_constants import get_hermes_home
+from hermes_constants import get_hermes_home, hermes_home_key
from utils import is_truthy_value
logger = logging.getLogger(__name__)
@@ -693,7 +693,7 @@ def _run_cli_killing_process_group(cmd, code, env, timeout):
# BU_NAMEs whose harness daemon this process has driven. The daemon reads BU_CDP_* once, at start, and
# outlives every call, so a backend swap (/browser connect|disconnect) that only changes the resolved
# endpoint leaves later browser_exec calls in the old browser until these are stopped.
-_driven_daemons: set = set()
+_driven_daemons: dict[str, str] = {} # BU_NAME -> profile home that drove it
_driven_daemons_lock = threading.Lock()
# Route cache key -> whether its last browser_exec ran on the hybrid local sidecar.
_exec_route_is_sidecar: dict[str, bool] = {}
@@ -702,12 +702,14 @@ _real_profile_fallback: dict[str, str] = {}
_real_profile_note_delivered: set = set()
-def stop_harness_daemons() -> None:
- """Stop every harness daemon this process drove, through the harness's own identity-checked
- ``--reload``; the next browser_exec respawns one on the endpoint it resolves then."""
+def stop_harness_daemons(home_key: Optional[str] = None) -> None:
+ """Stop every harness daemon this process drove (only ``home_key``'s when given), through the
+ harness's own identity-checked ``--reload``; the next browser_exec respawns one on the endpoint
+ it resolves then."""
with _driven_daemons_lock:
- names = sorted(_driven_daemons)
- _driven_daemons.clear()
+ names = sorted(name for name, home in _driven_daemons.items() if home_key in (None, home))
+ for name in names:
+ _driven_daemons.pop(name, None)
cmd = _find_cli() if names else None
if not cmd:
return
@@ -796,7 +798,7 @@ def browser_exec(code: str, session: str = "", timeout_s: int = _DEFAULT_TIMEOUT
def dispatch() -> dict[str, Any]:
_attach_vault_supervisor(env, task_id)
with _driven_daemons_lock:
- _driven_daemons.add(env.get("BU_NAME", "default"))
+ _driven_daemons[env.get("BU_NAME", "default")] = hermes_home_key()
try:
return {"proc": _run_cli_killing_process_group(cmd, code, env, timeout)}
except subprocess.TimeoutExpired: