Skip to content

The agent that grows with you

fix(browser): each served profile's harness screenshots and logs stay in its own tmp dir

Tekniuma4362ae61095
diff --git a/tests/tools/test_browser_profile_scope_edges.py b/tests/tools/test_browser_profile_scope_edges.py
index 8dcb9a6bc64..737fee293e3 100644
--- a/tests/tools/test_browser_profile_scope_edges.py
+++ b/tests/tools/test_browser_profile_scope_edges.py
@@ -133,3 +133,54 @@ def test_the_short_socket_dir_is_claimed_only_when_private_and_ours(tmp_path):
link = tmp_path / "hbh-link"
link.symlink_to(fresh)
assert not bu_cli._claim_private_dir(str(link))
+
+
+# --- per-profile harness tmp dir (screenshots, debug overlays, daemon logs) ---------------------------
+
+
+def test_each_served_profile_gets_its_own_harness_tmp_dir(profiles, tmp_path):
+ """browser_harness/_ipc.py ``_TMP`` (paths.tmp_dir: BH_TMP_DIR, else <home>/tmp) holds ``shot.png``,
+ debug-click overlays and ``bu-<NAME>.log``; one shared dir let profile B read A's screenshots."""
+ from tools import browser_use_cli as bu_cli
+ a, b = profiles
+ seen = {}
+ for home in (a, b):
+ env = {"BH_HOME": str(tmp_path / "bh")}
+ with _profile_runtime_scope(home, hydrate_secrets=False):
+ bu_cli._namespace_harness_daemons(env)
+ assert env.get("BH_TMP_DIR_SHARED") == "1" # several BU_NAMEs share it: keep bu-<name>.log stems
+ seen[home] = env.get("BH_TMP_DIR")
+ assert seen[a] and seen[b] and seen[a] != seen[b]
+ assert all(str(p).startswith(str(tmp_path / "bh" / "tmp")) for p in seen.values())
+ unscoped = {"BH_HOME": str(tmp_path / "bh")}
+ bu_cli._namespace_harness_daemons(unscoped)
+ assert unscoped == {"BH_HOME": str(tmp_path / "bh")} # single-profile: unchanged
+
+
+def test_a_served_profile_under_an_inherited_tmp_dir_nests_inside_it(profiles, tmp_path):
+ from tools import browser_use_cli as bu_cli
+ a, _b = profiles
+ env = {"BH_TMP_DIR": str(tmp_path / "shared-tmp")}
+ with _profile_runtime_scope(a, hydrate_secrets=False):
+ bu_cli._namespace_harness_daemons(env)
+ assert env["BH_TMP_DIR"].startswith(str(tmp_path / "shared-tmp") + "/p-")
+ # The runtime dir still derives from the inherited BH_TMP_DIR (harness fallback), not the new one.
+ assert env["BH_RUNTIME_DIR"].startswith(str(tmp_path / "shared-tmp") + "/p-")
+
+
+def test_an_unscoped_shutdown_reload_reaches_the_daemon_with_its_own_tmp_dir(profiles, monkeypatch):
+ """The reaper/shutdown runs outside any profile scope; its ``--reload`` must still carry the
+ daemon's own tmp dir, or the harness writes that profile's log into the shared one."""
+ from tools import browser_use_cli as bu_cli
+ a, _b = profiles
+ with _profile_runtime_scope(a, hydrate_secrets=False):
+ env = {"HOME": "/home/u"}
+ bu_cli._namespace_harness_daemons(env)
+ key = (*(env.get(k, "") for k in bu_cli._HARNESS_DIR_KEYS), "s1")
+ monkeypatch.setattr(bu_cli, "_driven_daemons", {key: hermes_home_key(a)})
+ monkeypatch.setattr(bu_cli, "_find_cli", lambda: ["browser-harness"])
+ seen = []
+ monkeypatch.setattr(bu_cli, "_run_cli_killing_process_group", lambda cmd, code, e, t: seen.append(e))
+ bu_cli.stop_harness_daemons()
+ assert [(e["BH_TMP_DIR"], e["BH_TMP_DIR_SHARED"], e["BU_NAME"]) for e in seen] == [
+ (env["BH_TMP_DIR"], "1", "s1")]
diff --git a/tools/browser_use_cli.py b/tools/browser_use_cli.py
index 3a3ee5aeece..5d8f15fcb76 100644
--- a/tools/browser_use_cli.py
+++ b/tools/browser_use_cli.py
@@ -183,23 +183,27 @@ def _base_subprocess_env() -> dict:
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."""
+ """A served profile's harness daemons live in their own runtime and tmp dirs. 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; one shared tmp dir mixed their
+ screenshots and logs. 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")
- runtime = str(Path(base) / f"p-{hashlib.sha256(tag.encode()).hexdigest()[:12]}")
+ root = env.get("BH_HOME") or env.get("BROWSER_HARNESS_HOME")
+ if not root: # browser-harness paths.home_dir(), resolved against the child's env
+ 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")
+ root = str(Path(root).expanduser())
+ leaf = f"p-{hashlib.sha256(tag.encode()).hexdigest()[:12]}"
+ base = env.get("BH_RUNTIME_DIR") or env.get("BH_TMP_DIR") or str(Path(root) / "runtime")
+ runtime = str(Path(base) / leaf)
env["BH_RUNTIME_DIR"] = _socket_fit_runtime_dir(runtime, base, tag)
env["BH_RUNTIME_DIR_SHARED"] = "1" # several BU_NAMEs share the dir: keep bu-<name> stems
+ # Screenshots, debug overlays and daemon logs (paths.tmp_dir): the profile's own, never shared.
+ env["BH_TMP_DIR"] = str(Path(env.get("BH_TMP_DIR") or str(Path(root) / "tmp")) / leaf)
+ env["BH_TMP_DIR_SHARED"] = "1"
# browser_harness/_ipc.py: a shared runtime dir names a daemon socket ``<dir>/bu-<BU_NAME>.sock`` and
@@ -756,10 +760,10 @@ def _run_cli_killing_process_group(cmd, code, env, timeout):
# 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,
+# identity (its _HARNESS_DIR_KEYS values plus BU_NAME): a served profile's runtime and tmp dirs are 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
+_HARNESS_DIR_KEYS = ("BH_RUNTIME_DIR", "BH_RUNTIME_DIR_SHARED", "BH_TMP_DIR", "BH_TMP_DIR_SHARED")
+_driven_daemons: dict[tuple[str, ...], str] = {} # (*_HARNESS_DIR_KEYS values, 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] = {}