Skip to content

The agent that grows with you

fix(browser): opt-in metrics name the local engine that served a browser_exec call

Teknium6931f5a6b34d
diff --git a/tests/hermes_cli/test_shared_metrics_loop.py b/tests/hermes_cli/test_shared_metrics_loop.py
index aea49b28311..d7cd291e128 100644
--- a/tests/hermes_cli/test_shared_metrics_loop.py
+++ b/tests/hermes_cli/test_shared_metrics_loop.py
@@ -321,11 +321,11 @@ def browser_exec_route(monkeypatch):
import tools.browser_supervisor as supervisor
import tools.browser_use_cli as bu
- route = SimpleNamespace(fallback=False)
+ route = SimpleNamespace(fallback=False, features={"local": True})
def session_info(key):
if bs.is_local_sidecar_key(key) or route.fallback:
- return {"cdp_url": f"ws://127.0.0.1:47000/devtools/browser/{key}", "features": {"local": True},
+ return {"cdp_url": f"ws://127.0.0.1:47000/devtools/browser/{key}", "features": route.features,
**({"fallback_from_cloud": True, "fallback_reason": "429"} if route.fallback else {})}
return {"cdp_url": "wss://cloud.example/x"}
@@ -367,5 +367,28 @@ def test_browser_exec_counts_against_the_browser_that_actually_served_it(home, m
assert rows == {"browserbase": 1, "local": 2, "cdp": 1}
+def test_a_local_call_names_the_engine_that_served_it_not_the_one_configured_now(home, browser_exec_route):
+ """The label comes from the session the call ran on, never from ``browser.engine`` read afterwards: a Chromium
+ session that outlived a switch to Lightpanda counts as Chromium, and a Lightpanda session as Lightpanda."""
+ import tools.browser_session_cloud as bs_cloud
+ from tools.browser_use_cli import _handle_browser_exec
+
+ def call_with_engine_configured(engine, task_id):
+ (home / "config.yaml").write_text(f"browser:\n engine: {engine}\n")
+ bs_cloud.reset_caches()
+ assert bs_cloud.get_browser_engine() == engine
+ code = 'new_tab("https://example.com/")'
+ assert json.loads(_handle_browser_exec({"code": code}, task_id=task_id))["success"] is True
+
+ browser_exec_route.fallback = True
+ call_with_engine_configured("lightpanda", "chromium-session")
+ call_with_engine_configured("lightpanda", "chromium-session")
+ browser_exec_route.features = {"local": True, "lightpanda": True}
+ call_with_engine_configured("auto", "lightpanda-session")
+
+ rows = {d["backend"]: v for d, v in _rows(home, "hermes.execution_backend.count") if d["outcome"] == "success"}
+ assert rows == {"local": 2, "lightpanda": 1}
+
+
def _raise():
raise RuntimeError("boom")
diff --git a/tools/browser_session_cloud.py b/tools/browser_session_cloud.py
index 99f8f611ba2..cd0f384f0c4 100644
--- a/tools/browser_session_cloud.py
+++ b/tools/browser_session_cloud.py
@@ -267,15 +267,18 @@ def is_local_backend() -> bool:
return terminal_env("TERMINAL_ENV", "local").strip().lower() in ("local", "")
-def browser_backend_name(ran_local: bool = False) -> str:
+def browser_backend_name(local_engine: Optional[str] = None) -> str:
"""Shared-metrics label for the active profile's backend in session-creation precedence
- (CDP override > cloud provider > local engine). ``ran_local``: the call ran on a LOCAL browser (the
- hybrid ``::local`` sidecar, a cloud failure's local fallback, the real profile), so a configured
- provider did not serve it. Config reads only, no network I/O."""
+ (CDP override > cloud provider > local engine). ``local_engine``: the call ran on a LOCAL browser (the
+ hybrid ``::local`` sidecar, a cloud failure's local fallback, the real profile) of that engine (``local``
+ or ``lightpanda``, as its session was created), so neither a configured provider nor ``browser.engine``
+ names it. Config reads only, no network I/O."""
from tools.browser_session_cdp import get_cdp_override_raw
if get_cdp_override_raw():
return "cdp"
- provider = None if ran_local else get_cloud_provider()
+ if local_engine:
+ return local_engine
+ provider = get_cloud_provider()
if provider is not None:
return str(getattr(provider, "name", "") or "other")
return "lightpanda" if get_browser_engine() == "lightpanda" else "local"
diff --git a/tools/browser_use_cli.py b/tools/browser_use_cli.py
index 6388eee1b97..766230c07ef 100644
--- a/tools/browser_use_cli.py
+++ b/tools/browser_use_cli.py
@@ -356,8 +356,8 @@ def _mark_local_browser(env: dict, session: dict, key: str) -> None:
"""Flags for a LOCAL session browser: it runs on the Bot Desktop display (lease fence), and unless a
persistent profile / the real-profile browser is shared by every key, it is exclusive to this key (no
own-tab preamble: nothing to share a tab with)."""
- env[_BOT_DESKTOP_BROWSER_SENTINEL] = "1"
features = session.get("features") or {}
+ env[_BOT_DESKTOP_BROWSER_SENTINEL] = "lightpanda" if features.get("lightpanda") else "local"
if not session.get("shared_profile") and not features.get("real_profile"):
env[_PRIVATE_BROWSER_SENTINEL] = "1"
_export_real_profile_note(env, key)
@@ -485,7 +485,7 @@ def _resolve_real_profile_cdp(env: dict, force_local: bool) -> Optional[str]:
cdp, err = real_profile_cdp()
if cdp and not err:
_set_cdp_env(env, cdp)
- env[_BOT_DESKTOP_BROWSER_SENTINEL] = "1"
+ env[_BOT_DESKTOP_BROWSER_SENTINEL] = "local" # the user's real Chromium, whatever browser.engine says
return err or None
@@ -710,8 +710,9 @@ def browser_exec(code: str, session: str = "", timeout_s: int = _DEFAULT_TIMEOUT
if local_sidecar:
env["BU_NAME"] = _sidecar_daemon_name(session)
_exec_route_is_sidecar[route_key] = local_sidecar
- bot_desktop_browser = bool(env.pop(_BOT_DESKTOP_BROWSER_SENTINEL, None))
- _exec_ran_local.set(bot_desktop_browser) # only local browsers carry the sentinel
+ local_engine = env.pop(_BOT_DESKTOP_BROWSER_SENTINEL, None)
+ bot_desktop_browser = bool(local_engine)
+ _exec_local_engine.set(local_engine) # only local browsers carry the sentinel
# SHARED browser (/browser connect CDP override): pin each named session to its own tab (see
# _OWN_TAB_PREAMBLE). Private per-name browsers skip this — nothing to collide with.
@@ -890,24 +891,26 @@ BROWSER_EXEC_SCHEMA = {
from tools.registry import registry
-# Whether this context's browser_exec call ran on a LOCAL browser: a configured cloud provider did not
-# serve a hybrid ::local sidecar, cloud-fallback or real-profile call, so its metrics row must not name it.
-_exec_ran_local: contextvars.ContextVar[bool] = contextvars.ContextVar("browser_exec_ran_local", default=False)
+# The LOCAL browser this context's browser_exec call ran on ("local" Chromium or "lightpanda", from its session),
+# else None: a configured cloud provider did not serve a hybrid ::local sidecar, cloud-fallback or real-profile
+# call, and browser.engine read afterwards may name another engine than the session's, so neither labels it.
+_exec_local_engine: contextvars.ContextVar[Optional[str]] = contextvars.ContextVar("browser_exec_local_engine",
+ default=None)
def _handle_browser_exec(args: dict, **kw):
"""Registry handler; each call counts against the backend that served it (shared metrics, opt-in)."""
from hermes_cli.observability.shared_metrics_loop import record_browser_call
from tools.browser_session_cloud import browser_backend_name
- token = _exec_ran_local.set(False)
+ token = _exec_local_engine.set(None)
try:
return record_browser_call(lambda: browser_exec(
code=args.get("code", ""), session=args.get("session", "") or "",
timeout_s=args.get("timeout_s", _DEFAULT_TIMEOUT_S), task_id=kw.get("task_id"),
local=bool(args.get("local", False)),
- ), lambda: browser_backend_name(ran_local=_exec_ran_local.get()))
+ ), lambda: browser_backend_name(local_engine=_exec_local_engine.get()))
finally:
- _exec_ran_local.reset(token)
+ _exec_local_engine.reset(token)
registry.register(