The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(browser): one throwaway Chromium profile per process, not per profile+key
Teknium641d5f9652f6
diff --git a/tests/tools/test_browser_launcher.py b/tests/tools/test_browser_launcher.pyindex 90d6175857b..b86fd164cdf 100644--- a/tests/tools/test_browser_launcher.py+++ b/tests/tools/test_browser_launcher.py@@ -118,6 +118,26 @@ def test_one_browser_per_key_and_separate_browsers_for_separate_keys(fake_chromiassert b.user_data_dir != a1.user_data_dir[email protected]("posix")[email protected]_system_guard_bypass # the fake browser's reparented helper is ours to stop+def test_two_processes_on_one_profile_never_share_an_ephemeral_profile(fake_chromium):+ """CLI + gateway on one profile launch the same key (``browser-exec-default``, a named session): each+ process gets its own throwaway profile, and a launch never wipes the other process's live one."""+ from hermes_constants import hermes_home_key+ code = ("import sys; from tools.browser_launcher import _default_user_data_dir; "+ "print(_default_user_data_dir(sys.argv[1], sys.argv[2]))")+ other = Path(subprocess.run([sys.executable, "-c", code, "browser-exec-default", hermes_home_key()],+ cwd=Path(__file__).resolve().parents[2], env=dict(os.environ),+ capture_output=True, text=True, check=True).stdout.strip())+ (other / "Default").mkdir(parents=True)+ (other / "Default" / "Cookies").write_text("the other process's session")++ mine, err = launcher.launch_chromium("browser-exec-default", env=dict(os.environ))+ assert err is None+ assert Path(mine.user_data_dir) != other+ assert (other / "Default" / "Cookies").read_text() == "the other process's session"++@pytest.mark.platforms("posix")@pytest.mark.live_system_guard_bypass # the fake browser's reparented helper is ours to stopdef test_stop_signals_the_browser_before_its_helpers_and_removes_the_ephemeral_profile(fake_chromium):diff --git a/tests/tools/test_browser_orphan_reaper.py b/tests/tools/test_browser_orphan_reaper.pyindex 2c08ece5cc7..a1479f586a8 100644--- a/tests/tools/test_browser_orphan_reaper.py+++ b/tests/tools/test_browser_orphan_reaper.py@@ -212,6 +212,18 @@ class TestLeakedBrowserOfThisProcess:assert records.terminated == []+def test_throwaway_profile_of_a_crashed_owner_is_removed_even_when_its_browser_died_too(records, monkeypatch):+ """Throwaway profiles are per process, so no later launch wipes a crashed process's dir: the reap does."""+ udd = Path(records.ephemeral("crashed-abc-99999"))+ (udd / "Default").mkdir(parents=True)+ rec = _record(records, pid=4242, udd=str(udd))+ monkeypatch.setattr(launcher, "_pid_alive", _alive()) # owner and browser both gone++ assert launcher.reap_orphaned_browsers() == 0+ assert records.terminated == []+ assert not udd.exists() and not rec.exists()++def test_browser_attached_after_its_owner_crashed_is_never_reaped_while_in_use(records, tmp_path, monkeypatch):"""The next process ATTACHES to a crashed owner's persistent-profile Chromium (owned=False). The recordstill names the dead owner; this process takes it over, so no reaper kills the browser it handed out."""diff --git a/tools/browser_launcher.py b/tools/browser_launcher.pyindex 0552d17e644..1c827c91f08 100644--- a/tools/browser_launcher.py+++ b/tools/browser_launcher.py@@ -173,8 +173,10 @@ def chromium_ready() -> tuple[bool, str]:# ── Launch ───────────────────────────────────────────────────────────────────────────────────────────def _default_user_data_dir(key: str, home_key: str) -> str:+ """Per process too: two Hermes processes on one profile use the same keys (``browser-exec-default``,+ named sessions), and each launch wipes its throwaway dir first."""digest = hashlib.sha256(f"{home_key}\0{key}".encode()).hexdigest()[:16]- return str(_ephemeral_root(home_key) / digest)+ return str(_ephemeral_root(home_key) / f"{digest}-{os.getpid()}")def _launch_timeout(timeout: Optional[float]) -> float:@@ -676,8 +678,9 @@ def _reap_record(path: Path, home: str) -> bool:if reaped:_terminate(record["pid"], record.get("start_time"), None)logger.info("browser: reaped orphaned Chromium pid %s (%s)", record["pid"], record["user_data_dir"])- if record.get("ephemeral"):- shutil.rmtree(record["user_data_dir"], ignore_errors=True)+ udd = record.get("user_data_dir")+ if record.get("ephemeral") and isinstance(udd, str) and _inside_ephemeral_root(udd, home):+ shutil.rmtree(udd, ignore_errors=True) # its owner is gone: no launch reuses a per-process dirpath.unlink(missing_ok=True)return reaped