The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(browser): after a disconnect a profile falls back to its own BROWSER_CDP_URL
Teknium45b15aac70d3
diff --git a/tests/tools/test_browser_profile_live_cdp.py b/tests/tools/test_browser_profile_live_cdp.pyindex 82467bdd8c7..8f17363777f 100644--- a/tests/tools/test_browser_profile_live_cdp.py+++ b/tests/tools/test_browser_profile_live_cdp.py@@ -33,13 +33,31 @@ def test_a_live_override_is_this_profiles_alone(profiles, monkeypatch):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 winsbt_cdp.set_live_cdp_override(None)- assert bt_cdp._get_cdp_override_raw() == "http://operator:9222"+ assert bt_cdp._get_cdp_override_raw() == ""+++def test_disconnect_falls_back_to_this_profiles_own_env_never_the_launch_env(profiles, monkeypatch):+ """After a disconnect the BROWSER_CDP_URL tier is the profile's own ``.env``; the launch process's+ ``os.environ`` value belongs to the launch profile and must not reach a served one."""+ from tui_gateway.server import _resolve_browser_cdp_url # bound onto server.py globals+ from tools.browser_camofox import is_camofox_mode++ a, b = profiles+ (b / ".env").write_text("BROWSER_CDP_URL=http://127.0.0.1:7777\n")+ monkeypatch.setenv("BROWSER_CDP_URL", "http://127.0.0.1:6666") # the launch profile's+ with _profile_runtime_scope(b, hydrate_secrets=False):+ bt_cdp.set_live_cdp_override("http://127.0.0.1:4444")+ assert bt_cdp._get_cdp_override_raw() == "http://127.0.0.1:4444"+ bt_cdp.set_live_cdp_override(None)+ assert bt_cdp._get_cdp_override_raw() == _resolve_browser_cdp_url() == "http://127.0.0.1:7777"+ with _profile_runtime_scope(a, hydrate_secrets=False): # no BROWSER_CDP_URL in A's .env+ assert bt_cdp._get_cdp_override_raw() == _resolve_browser_cdp_url() == ""+ monkeypatch.setattr("tools.browser_camofox._config_cdp_url", lambda: "")+ monkeypatch.setattr("tools.tool_backend_helpers.read_selection", lambda _: "camofox")+ assert is_camofox_mode() is True # the launch env's CDP URL does not switch A off Camofox+ assert bt_cdp._get_cdp_override_raw() == "http://127.0.0.1:6666" # single-profile: env as beforedef test_a_new_session_records_its_profile(profiles, monkeypatch):diff --git a/tools/browser_camofox.py b/tools/browser_camofox.pyindex f4be688c53e..390686a308e 100644--- a/tools/browser_camofox.py+++ b/tools/browser_camofox.py@@ -98,8 +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."""- from tools.browser_tool_cdp import live_cdp_override- if live_cdp_override() or os.getenv("BROWSER_CDP_URL", "").strip() or _config_cdp_url():+ from tools.browser_tool_cdp import env_cdp_url, live_cdp_override+ if live_cdp_override() or env_cdp_url() or _config_cdp_url():return Falsetry:from tools.tool_backend_helpers import read_selectiondiff --git a/tools/browser_tool_cdp.py b/tools/browser_tool_cdp.pyindex f453b874f57..c2f35729917 100644--- a/tools/browser_tool_cdp.py+++ b/tools/browser_tool_cdp.py@@ -73,16 +73,28 @@ def set_live_cdp_override(url: Optional[str]) -> None:_live_overrides.pop(key, None)+def env_cdp_url() -> str:+ """``BROWSER_CDP_URL`` from this profile's secret scope. A routed profile (multiplex, or a scope+ bound for another home) reads only its own ``.env``: ``os.environ`` holds the launch profile's+ value. Single-profile reads fall through to ``os.environ`` as before (``get_secret``)."""+ from agent.secret_scope import current_secret_scope, get_secret, serves_routed_profile+ if serves_routed_profile():+ value = (current_secret_scope() or {}).get("BROWSER_CDP_URL")+ else:+ value = get_secret("BROWSER_CDP_URL")+ return (value or "").strip()++def _get_cdp_override_raw() -> str:"""Return the *configured* CDP override without any network I/O.- Precedence: this profile's live ``/browser connect``, then the operator's ``BROWSER_CDP_URL`` env,+ Precedence: this profile's live ``/browser connect``, then its ``BROWSER_CDP_URL`` (:func:`env_cdp_url`),then ``browser.cdp_url``. Is-it-configuredgates (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'sschema build with no error."""- env_override = live_cdp_override() or os.environ.get("BROWSER_CDP_URL", "").strip()+ env_override = live_cdp_override() or env_cdp_url()return env_override or _origin()._browser_cfg("cdp_url", "", lambda v: str(v or "").strip(), "browser.cdp_url from config")diff --git a/tui_gateway/methods_browser.py b/tui_gateway/methods_browser.pyindex 6ab282b91aa..775bf330050 100644--- a/tui_gateway/methods_browser.py+++ b/tui_gateway/methods_browser.py@@ -15,8 +15,8 @@ def _resolve_browser_cdp_url() -> str:``tools.browser_tool_cdp._get_cdp_override`` HTTP-probes discovery URLs). Same precedence (thisprofile's live connect, env, then ``browser.cdp_url``) minus WS resolution; ``browser_navigate``normalizes on the next call."""- from tools.browser_tool_cdp import live_cdp_override- if env_url := live_cdp_override() or os.environ.get("BROWSER_CDP_URL", "").strip():+ from tools.browser_tool_cdp import env_cdp_url, live_cdp_override+ if env_url := live_cdp_override() or env_cdp_url():return env_urlwith contextlib.suppress(Exception):from hermes_cli.config import read_raw_config