Skip to content

The agent that grows with you

fix(browser): each profile drives and reaps its own Browser Use daemon

Tekniumb71c73ea0f51
diff --git a/tests/tools/test_browser_profile_live_cdp.py b/tests/tools/test_browser_profile_live_cdp.py
index 7f20ae5766e..82467bdd8c7 100644
--- a/tests/tools/test_browser_profile_live_cdp.py
+++ b/tests/tools/test_browser_profile_live_cdp.py
@@ -71,3 +71,50 @@ def test_reaping_one_profile_leaves_the_other_profiles_browser_and_supervisor(pr
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"]
+
+
+def test_each_profile_drives_and_reaps_its_own_harness_daemon(profiles, monkeypatch, tmp_path):
+ """The harness names a daemon by BU_NAME alone (unnamed exec: ``default``) and reuses a live one, so
+ with one shared runtime dir B's exec drove A's connected browser and A's reap stopped B's daemon."""
+ import json
+ import stat
+ from tools import browser_use_cli as bu_cli
+
+ a, b = profiles
+ shared = tmp_path / "harness-runtime"
+ cli = tmp_path / "browser-harness" # one daemon per (runtime dir, BU_NAME) latches its first endpoint
+ cli.write_text(f'''#!/bin/sh
+dir="${{BH_RUNTIME_DIR:-{shared}}}"; mkdir -p "$dir"; latch="$dir/bu-${{BU_NAME:-default}}"
+if [ "${{1:-}}" = "--reload" ]; then rm -f "$latch"; exit 0; fi
+cat > /dev/null
+[ -f "$latch" ] || echo "$BU_CDP_URL" > "$latch"
+cat "$latch"
+''')
+ cli.chmod(cli.stat().st_mode | stat.S_IXUSR)
+ monkeypatch.setenv("BH_RUNTIME_DIR", str(shared))
+ monkeypatch.setattr(bu_cli, "_find_cli", lambda: [str(cli)])
+ monkeypatch.setattr(bu_cli, "_driven_daemons", {})
+ monkeypatch.setattr(bu_cli, "_attach_vault_supervisor", lambda env, task_id: None)
+ monkeypatch.setattr(bt_cdp, "_resolve_cdp_override", lambda url: url)
+
+ def drive(home, url=None):
+ with _profile_runtime_scope(home, hydrate_secrets=False):
+ if url:
+ bt_cdp.set_live_cdp_override(url)
+ return json.loads(bu_cli.browser_exec("print(1)", task_id="t"))["output"].strip()
+
+ assert drive(a, "http://127.0.0.1:9301") == "http://127.0.0.1:9301"
+ assert drive(b, "http://127.0.0.1:9302") == "http://127.0.0.1:9302" # B's own daemon, not A's
+ assert drive(a) == "http://127.0.0.1:9301"
+ reloads = []
+ real_run = bu_cli._run_cli_killing_process_group
+ monkeypatch.setattr(bu_cli, "_run_cli_killing_process_group",
+ lambda cmd, code, env, timeout: reloads.append(env.get("BH_RUNTIME_DIR"))
+ or real_run(cmd, code, env, timeout))
+ with _profile_runtime_scope(a, hydrate_secrets=False):
+ lifecycle.cleanup_profile_browsers(hermes_home_key(a))
+ bt_cdp.set_live_cdp_override("http://127.0.0.1:9311")
+ assert len(reloads) == 1 # A's reap stops exactly A's daemon
+ assert [home for home in bu_cli._driven_daemons.values()] == [hermes_home_key(b)]
+ assert drive(b) == "http://127.0.0.1:9302" # B's daemon survived A's reap
+ assert drive(a) == "http://127.0.0.1:9311" # A respawned on its new endpoint
diff --git a/tools/browser_use_cli.py b/tools/browser_use_cli.py
index 07973c985f7..9d36bc0316e 100644
--- a/tools/browser_use_cli.py
+++ b/tools/browser_use_cli.py
@@ -5,6 +5,7 @@ instead of default browser tools
"""
import contextlib
+import hashlib
import importlib
import importlib.util
import json
@@ -177,9 +178,29 @@ def _base_subprocess_env() -> dict:
env.pop("PYTHONPATH", None)
env["PATH"] = _floor_subprocess_path(env.get("PATH", ""))
env.setdefault("ANONYMIZED_TELEMETRY", "false")
+ _namespace_harness_daemons(env)
return env
+def _namespace_harness_daemons(env: dict) -> None:
+ """A served profile's harness daemons live in their own runtime dir. The harness names a daemon by
+ ``BU_NAME`` alone (unnamed calls are ``default``) and reuses any live one, so with one shared dir a
+ profile's exec drove another profile's browser. Outside a served-profile scope nothing changes."""
+ tag = _served_profile_tag()
+ if not tag:
+ return
+ base = env.get("BH_RUNTIME_DIR") or env.get("BH_TMP_DIR")
+ if not base: # browser-harness paths.home_dir() / "runtime", resolved against the child's env
+ root = env.get("BH_HOME") or env.get("BROWSER_HARNESS_HOME")
+ if not root:
+ xdg = env.get("XDG_CONFIG_HOME")
+ root = str(Path(xdg) / "browser-harness") if xdg else str(
+ Path(env.get("HOME") or Path.home()) / ".config" / "browser-harness")
+ base = str(Path(root).expanduser() / "runtime")
+ env["BH_RUNTIME_DIR"] = str(Path(base) / f"p-{hashlib.sha256(tag.encode()).hexdigest()[:12]}")
+ env["BH_RUNTIME_DIR_SHARED"] = "1" # several BU_NAMEs share the dir: keep bu-<name> stems
+
+
def _floor_subprocess_path(path: str) -> str:
"""Keep system commands reachable from profile workers with a minimal PATH.
@@ -690,10 +711,13 @@ def _run_cli_killing_process_group(cmd, code, env, timeout):
return subprocess.CompletedProcess(cmd, proc.returncode, stdout, stderr)
-# 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: dict[str, str] = {} # BU_NAME -> profile home that drove it
+# Harness daemons 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. Keyed by the daemon's
+# identity (its _HARNESS_DIR_KEYS values plus BU_NAME): a served profile's runtime dir is its own,
+# so no other profile can take over the entry.
+_HARNESS_DIR_KEYS = ("BH_RUNTIME_DIR", "BH_RUNTIME_DIR_SHARED")
+_driven_daemons: dict[tuple[str, str, str], str] = {} # (runtime dir, shared flag, BU_NAME) -> home
_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] = {}
@@ -707,16 +731,18 @@ def stop_harness_daemons(home_key: Optional[str] = None) -> None:
harness's own identity-checked ``--reload``; the next browser_exec respawns one on the endpoint
it resolves then."""
with _driven_daemons_lock:
- 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
+ daemons = sorted(key for key, home in _driven_daemons.items() if home_key in (None, home))
+ for key in daemons:
+ _driven_daemons.pop(key, None)
+ cmd = _find_cli() if daemons else None
if not cmd:
return
env = _base_subprocess_env()
- for name in names:
+ for *dirs, name in daemons:
+ target = {k: v for k, v in env.items() if k not in _HARNESS_DIR_KEYS}
+ target.update((k, v) for k, v in zip(_HARNESS_DIR_KEYS, dirs) if v)
with contextlib.suppress(OSError, subprocess.SubprocessError):
- _run_cli_killing_process_group([*cmd, "--reload"], "", {**env, "BU_NAME": name}, 15)
+ _run_cli_killing_process_group([*cmd, "--reload"], "", {**target, "BU_NAME": name}, 15)
def _route_exec(env: dict, session: str, task_id: Optional[str], local: bool, local_sidecar: bool,
@@ -798,7 +824,8 @@ 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[env.get("BU_NAME", "default")] = hermes_home_key()
+ daemon = (*(env.get(k, "") for k in _HARNESS_DIR_KEYS), env.get("BU_NAME", "default"))
+ _driven_daemons[daemon] = hermes_home_key()
try:
return {"proc": _run_cli_killing_process_group(cmd, code, env, timeout)}
except subprocess.TimeoutExpired: