The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor(browser): port the harness-daemon ownership onto main's browser rewrite
kshitijk4poor309d551c8405
diff --git a/tests/tools/test_browser_use_cli_owned_daemon.py b/tests/tools/test_browser_use_cli_owned_daemon.pyindex cbd6146a3a0..80f2497e74b 100644--- a/tests/tools/test_browser_use_cli_owned_daemon.py+++ b/tests/tools/test_browser_use_cli_owned_daemon.py@@ -138,105 +138,6 @@ def _shared_runtime(tmp_path) -> tuple[Path, dict]:return runtime, {"BH_RUNTIME_DIR": str(runtime), "BH_RUNTIME_DIR_SHARED": "1"}-def test_the_exit_path_stops_only_the_daemons_this_process_owns(monkeypatch, tmp_path, registered):- runtime, dirs = _shared_runtime(tmp_path)- ours = browser_use_cli.daemon_identity({**dirs, "BU_NAME": "ours"})- theirs = browser_use_cli.daemon_identity({**dirs, "BU_NAME": "theirs"}) # another live Hermes started it- browser_use_cli._owned_daemon_groups.update({ours: 4242})- monkeypatch.setattr(browser_use_cli, "_driven_daemons", {ours: "home", theirs: "home"})- monkeypatch.setattr(browser_use_cli, "_find_cli", lambda: ["browser-harness"])- monkeypatch.setattr(browser_use_cli, "_base_subprocess_env", dict)- _publish(runtime, "bu-ours", 4242)- reloaded = []- monkeypatch.setattr(browser_use_cli, "_run_cli_killing_process_group",- lambda cmd, code, env, timeout: reloaded.append(env["BU_NAME"]))-- from tools import browser_tool_lifecycle- from tools.browser_tool_origin import origin- monkeypatch.setattr(origin, "_cleanup_done", False)- monkeypatch.setattr(origin, "_active_sessions", {})- for step in ("_stop_all_lightpanda", "_reap_orphaned_browser_sessions"):- monkeypatch.setattr(browser_tool_lifecycle, step, lambda: None)- monkeypatch.setattr(browser_tool_lifecycle._real_profile, "_terminate_real_profile_chrome", lambda: None)-- browser_tool_lifecycle._emergency_cleanup_all_sessions() # the atexit handler- assert reloaded == ["ours"]- assert browser_use_cli._driven_daemons == {theirs: "home"}---def test_exit_never_reloads_a_name_another_hermes_republished_after_ours_exited(monkeypatch, tmp_path, registered):- """--reload goes by name: once our daemon exited and another live Hermes started its replacement- under the same name, exiting here must leave that replacement running and just release ours."""- runtime, dirs = _shared_runtime(tmp_path)- ours = browser_use_cli.daemon_identity({**dirs, "BU_NAME": "work"})- browser_use_cli._owned_daemon_groups.update({ours: 4242})- monkeypatch.setattr(browser_use_cli, "_driven_daemons", {ours: "home"})- monkeypatch.setattr(browser_use_cli, "_find_cli", lambda: ["browser-harness"])- monkeypatch.setattr(browser_use_cli, "_base_subprocess_env", dict)- _publish(runtime, "bu-work", 4242)- reloaded = []- monkeypatch.setattr(browser_use_cli, "_run_cli_killing_process_group",- lambda cmd, code, env, timeout: reloaded.append(env["BU_NAME"]))- with monkeypatch.context() as m: # control: while the name still publishes our pid, it is reloaded- m.setattr(browser_use_cli, "_owned_daemon_groups", {ours: 4242})- m.setattr(browser_use_cli, "_driven_daemons", {})- browser_use_cli.stop_owned_harness_daemons()- assert reloaded == ["work"]- registered.clear()-- _publish(runtime, "bu-work", 5151) # ours exited; the other Hermes's daemon now holds the name- reloaded.clear()-- browser_use_cli.stop_owned_harness_daemons()-- assert reloaded == []- assert registered == [("unregister", [4242])]- assert browser_use_cli._owned_daemon_groups == {}---# The daemon reparents to init once its owner dies, so cleanup leaves this test's subtree.[email protected]_system_guard_bypass-def test_a_sigkilled_owner_takes_its_harness_daemon_with_it(tmp_path):- """End to end through the real supervisor: the owner registers its detached daemon, then dies- by SIGKILL (no Python cleanup runs), and the daemon must go with it."""- pkg = tmp_path / "pkgroot" / "browser_harness"- pkg.mkdir(parents=True)- (pkg / "__init__.py").write_text("", encoding="utf-8")- (pkg / "daemon.py").write_text("import time\ntime.sleep(300)\n", encoding="utf-8")- runtime = tmp_path / "runtime"- runtime.mkdir()- owner_src = (- "import json, os, subprocess, sys, time\n"- "from tools import browser_use_cli\n"- f"d = subprocess.Popen([sys.executable, '-m', 'browser_harness.daemon'], cwd={str(tmp_path / 'pkgroot')!r},"- " stdin=subprocess.DEVNULL, start_new_session=True, env=browser_use_cli._base_subprocess_env())\n"- f"open({str(runtime / 'bu.pid')!r}, 'w', encoding='utf-8').write(json.dumps({{'pid': d.pid}}))\n" # windows-footgun: ok — a write- f"browser_use_cli._own_harness_daemon({{'BH_RUNTIME_DIR': {str(runtime)!r}}})\n"- "print(d.pid, flush=True)\n"- "time.sleep(300)\n"- )- env = dict(os.environ, HERMES_HOME=str(tmp_path / "home"), PYTHONPATH=str(Path(__file__).resolve().parents[2]))- owner = subprocess.Popen([sys.executable, "-c", owner_src], env=env, stdin=subprocess.DEVNULL,- stdout=subprocess.PIPE, text=True)- daemon_pid = None- try:- daemon_pid = int(owner.stdout.readline())- owner.kill()- owner.wait(timeout=10)- wait_until(lambda: not _group_alive(daemon_pid), timeout=15,- what="the harness daemon to die with the kill -9'd Hermes process that drove it")- finally:- if daemon_pid is not None:- try:- os.killpg(daemon_pid, 9) # windows-footgun: ok — posix-marked test- except (ProcessLookupError, PermissionError):- pass- if owner.poll() is None:- owner.kill()- owner.wait(timeout=10)- process_identity._supervised_pgids.clear()--def test_a_daemon_another_live_hermes_started_on_the_same_name_is_never_registered(tmp_path, registered):"""Two Hermes processes on the default BU_NAME read the same bu-default.pid. The daemon belongsto the one whose CLI started it: the other must not hand it to its supervisor, or that Hermes@@ -268,8 +169,6 @@ def test_a_long_first_browser_exec_owns_its_daemon_before_the_call_returns(monke"""The first call starts the daemon and may run for minutes (navigation, user code). Its daemonmust reach the supervisor during the call, or a Hermes killed mid-call leaves it running."""import threading- from tools import browser_tool_cloud as cloud- from tools import browser_tool_session as sessionowned_during_call = threading.Event()running = threading.Event()@@ -284,14 +183,8 @@ def test_a_long_first_browser_exec_owns_its_daemon_before_the_call_returns(monkemonkeypatch.setattr(browser_use_cli, "_find_cli", lambda: ["browser-use"])monkeypatch.setattr(browser_use_cli, "_base_subprocess_env", dict)- monkeypatch.setattr(browser_use_cli, "_real_profile_consented", lambda: False)- monkeypatch.setattr(browser_use_cli, "_resolve_lightpanda_cdp", lambda *a: None)- monkeypatch.setattr("tools.browser_tool_cdp._get_cdp_override", lambda: "")- monkeypatch.setattr("tools.browser_tool._get_open_command_timeout", lambda **_kw: 5)- monkeypatch.setattr(cloud, "_get_cloud_provider", lambda: None)- monkeypatch.setattr(session, "_run_browser_command", lambda *_a, **_kw: {- "success": True, "data": {"cdpUrl": "http://127.0.0.1:9222"}})- monkeypatch.setattr(browser_use_cli, "_attach_vault_supervisor", lambda *a: None)+ monkeypatch.setattr(browser_use_cli, "_route_exec_current",+ lambda env, *_a: ({**env, "BU_CDP_URL": "http://127.0.0.1:9222"}, None, False, 0))monkeypatch.setattr(browser_use_cli, "_run_cli_killing_process_group", run_cli)monkeypatch.setattr(browser_use_cli, "_driven_daemons", {})@@ -310,12 +203,3 @@ def test_a_managed_worker_never_owns_its_harness_daemon(fake_daemon, registered,browser_use_cli._own_harness_daemon({"BH_RUNTIME_DIR": str(runtime), "BU_NAME": "work"})assert registered == []---def test_windows_exit_stops_no_harness_daemon(monkeypatch):- """Windows records no ownership and its exit never stopped a browser_exec-only process's daemons:- reloading every driven one would stop a shared daemon another live Hermes is using."""- monkeypatch.setattr(browser_use_cli.os, "name", "nt")- monkeypatch.setattr(browser_use_cli, "_driven_daemons", {("", "", "", "", "default"): "home"})- monkeypatch.setattr(browser_use_cli, "_reload_daemons", lambda daemons: pytest.fail(f"reloaded {daemons}"))- browser_use_cli.stop_owned_harness_daemons()diff --git a/tools/browser_launcher.py b/tools/browser_launcher.pyindex 8926ed6a698..9262d0ae94e 100644--- a/tools/browser_launcher.py+++ b/tools/browser_launcher.py@@ -254,6 +254,7 @@ def _spawn_kwargs() -> dict[str, Any]:if os.name == "nt":from hermes_cli._subprocess_compat import windows_hide_flagsreturn {"creationflags": windows_hide_flags() | getattr(subprocess, "CREATE_NEW_PROCESS_GROUP", 0)}+ # detached-spawn: ok — a shared-profile Chromium outlives its launcher for its other holders (browser_launcher_holders); a crashed owner's throwaway one is reaped from its launch record by the next Hermesreturn {"start_new_session": True}diff --git a/tools/browser_lightpanda.py b/tools/browser_lightpanda.pyindex 8bc420c1aa5..877dcb80118 100644--- a/tools/browser_lightpanda.py+++ b/tools/browser_lightpanda.py@@ -219,6 +219,7 @@ def launch_lightpanda(session_name: str, *, block_private_networks: bool = Falsetry:with open(log_path, "wb") as log_file:proc = subprocess.Popen(argv, stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL,+ # detached-spawn: ok — a crashed owner's server is reaped from its state record by the next Hermes (reap_orphaned_lightpanda); owning it is a follow-up with the launcher's Chromiumstderr=log_file, env=_browser_env(), start_new_session=True)except (OSError, subprocess.SubprocessError) as e:return None, f"Failed to launch lightpanda serve ({binary}): {e}"diff --git a/tools/browser_use_cli.py b/tools/browser_use_cli.pyindex a5a5af267c9..d3cb91f7c2e 100644--- a/tools/browser_use_cli.py+++ b/tools/browser_use_cli.py@@ -637,29 +637,6 @@ def stop_harness_daemons(home_key: Optional[str] = None) -> None:_reload_daemons(daemons)-def stop_owned_harness_daemons() -> None:- """Stop only the harness daemons this process owns (``_owned_daemon_groups``: started by its own- commands). The exit handler uses this: a daemon another live Hermes started on the same name was- driven here too, but it is that Hermes's to stop. Windows records no ownership (``_own_harness_daemon``- is POSIX-only) and its exit never stopped a browser_exec-only process's daemons, so nothing to do."""- if os.name != "posix":- return- with _owned_daemon_groups_lock:- owned = dict(_owned_daemon_groups)- with _driven_daemons_lock:- for key in owned:- _driven_daemons.pop(key, None)- # --reload stops a daemon by name, and one of ours that exited may since have been replaced under- # that name by another live Hermes: only a name whose published pid is still ours is reloaded.- env = _base_subprocess_env() if owned else {}- current = [key for key, pid in owned.items() if _published_daemon_pid(daemon_env(env, key)) == pid]- for key in owned.keys() - set(current):- with _owned_daemon_groups_lock:- _owned_daemon_groups.pop(key, None)- release_process_group_once_empty(owned[key], timeout=0) # a survivor of ours stays covered- _reload_daemons(sorted(current))--def _published_daemon_pid(env: dict) -> Optional[int]:"""The pid the harness published for the daemon ``env`` drives (``harness_pid_file``: JSON``{"pid", "started"}`` or a bare pid), or None when there is none."""