The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(browser): resolve managed Browser Use provider through its session path
NgThachThanh004dcc079ec9
diff --git a/tests/tools/test_browser_use_cli.py b/tests/tools/test_browser_use_cli.pyindex fa1bb6d6b03..059490994e8 100644--- a/tests/tools/test_browser_use_cli.py+++ b/tests/tools/test_browser_use_cli.py@@ -583,6 +583,74 @@ class TestBackendCdpResolution:assert bu_cli._resolve_backend_cdp(env, "t1") is Noneassert env["BU_CDP_WS"] == "wss://gateway.example/cdp/managed"+ def test_never_configured_autodetect_managed_provider_resolves_through_provider(self, monkeypatch):+ """A never-configured install (no ``browser.cloud_provider``, no BROWSER_USE_API_KEY) can+ still get a ``browser-use`` provider back from autodetect — via the managed Nous gateway+ token, which needs no API key. That provider DOES have a session/CDP path, so it must+ resolve through the provider instead of the direct-API shortcut: the shortcut leaves+ BU_CDP_* unset and the harness falls back to hunting the user's profile dirs+ (chrome-not-running on a headless host with no ~/.config/chromium)."""+ class _BUProvider:+ name = "browser-use"++ def _get_config_or_none(self, refresh_token=True):+ return {"api_key": "tok", "base_url": "https://gateway", "managed_mode": True}++ monkeypatch.setattr("tools.browser_tool_cdp._get_cdp_override", lambda: "")+ monkeypatch.setattr(bt_cloud, "_get_cloud_provider", lambda: _BUProvider())+ monkeypatch.setattr(+ bt_session, "_get_session_info",+ lambda task_id: {"cdp_url": "wss://gateway.example/cdp/autodetect"},+ )+ # Never-configured: browser section present but no selection at all.+ monkeypatch.setattr(bu_cli, "_read_browser_cfg", lambda: {"backend": "browser-use"})+ env = {}+ assert bu_cli._resolve_backend_cdp(env, "t1") is None+ assert env["BU_CDP_WS"] == "wss://gateway.example/cdp/autodetect"++ def test_direct_api_provider_still_takes_native_shortcut(self, monkeypatch):+ """The managed-vs-direct split must not regress the direct-API path: with a real+ BROWSER_USE_API_KEY and no gateway, the CLI still talks to BU cloud natively instead of+ double-sessioning through the provider."""+ class _BUProvider:+ name = "browser-use"++ def _get_config_or_none(self, refresh_token=True):+ return {"api_key": "bu-key", "base_url": "https://api.browser-use.com", "managed_mode": False}++ monkeypatch.setattr("tools.browser_tool_cdp._get_cdp_override", lambda: "")+ monkeypatch.setattr(bt_cloud, "_get_cloud_provider", lambda: _BUProvider())+ monkeypatch.setattr(+ bt_session, "_get_session_info",+ lambda key: (_ for _ in ()).throw(AssertionError("must skip provider")),+ )+ monkeypatch.setattr(bu_cli, "_read_browser_cfg", lambda: {"cloud_provider": "browser-use"})+ env = {}+ assert bu_cli._resolve_backend_cdp(env, "t1") is None+ assert "BU_CDP_WS" not in env and "BU_CDP_URL" not in env++ def test_never_configured_managed_provider_failure_errors_never_silently_unset(self, monkeypatch):+ """A managed provider that cannot produce a session must surface that failure, NOT fall+ through with BU_CDP_* unset — the harness's own discovery then fails with a misleading+ chrome-not-running instead of naming the real problem."""+ class _BUProvider:+ name = "browser-use"++ def _get_config_or_none(self, refresh_token=True):+ return {"api_key": "tok", "base_url": "https://gateway", "managed_mode": True}++ monkeypatch.setattr("tools.browser_tool_cdp._get_cdp_override", lambda: "")+ monkeypatch.setattr(bt_cloud, "_get_cloud_provider", lambda: _BUProvider())+ monkeypatch.setattr(+ bt_session, "_get_session_info",+ lambda task_id: (_ for _ in ()).throw(RuntimeError("no managed session")),+ )+ monkeypatch.setattr(bu_cli, "_read_browser_cfg", lambda: {"backend": "browser-use"})+ env = {}+ err = bu_cli._resolve_backend_cdp(env, "t1")+ assert err and "no managed session" in err+ assert "BU_CDP_WS" not in env and "BU_CDP_URL" not in env+def test_legacy_use_gateway_flag_still_resolves_gateway_provider(self, monkeypatch):"""Regression guard for the pre-picker shape of the same selection."""class _BUProvider:diff --git a/tools/browser_use_cli.py b/tools/browser_use_cli.pyindex dc59328b3b4..4872cb82fad 100644--- a/tools/browser_use_cli.py+++ b/tools/browser_use_cli.py@@ -216,6 +216,25 @@ def _use_gateway(browser_cfg: dict) -> bool:return str(browser_cfg.get("cloud_provider") or "").strip().lower() == NOUS_MANAGED_PROVIDER+def _is_managed_browser_use(provider: Any) -> bool:+ """True when a resolved ``browser-use`` provider is backed by the managed Nous Tool Gateway+ rather than a direct ``BROWSER_USE_API_KEY`` config.++ Autodetect hands back a managed provider on installs that never configured a cloud browser+ (the gateway token needs no API key), and that provider reports the same ``name`` as the+ direct-API one. The direct-API shortcut below then skipped session resolution for a provider+ that CAN provide one, leaving BU_CDP_* unset so the harness fell back to hunting the user's+ profile dirs — ``chrome-not-running`` on a host with no ~/.config/chromium."""+ resolver = getattr(provider, "_get_config_or_none", None)+ if not callable(resolver):+ return False+ try:+ config = resolver(refresh_token=False)+ except Exception:+ return False+ return bool(isinstance(config, dict) and config.get("managed_mode"))++def get_browser_backend() -> str:"""Configured browser backend key ("" = unset → default). YAML 1.1 parses anunquoted ``off`` as False — that must mean BACKEND_DISABLED, not "unset"."""@@ -497,7 +516,8 @@ def _resolve_backend_cdp(env: dict, task_id: Optional[str], session_name: str =# from the picker, or the pre-picker use_gateway: true) DO resolve through the provider: the gateway# provisions the browser server-side and returns its CDP URL.provider_key = str(getattr(provider, "name", "") or "").strip().lower()- if provider_key == _BACKEND_KEY and not _use_gateway(_read_browser_cfg()):+ if (provider_key == _BACKEND_KEY and not _use_gateway(_read_browser_cfg())+ and not _is_managed_browser_use(provider)):env[_PRIVATE_BROWSER_SENTINEL] = "1" # named BU cloud browsers are exclusive to their daemonreturn None