The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(browser): an attacher leases a shared browser under its launcher's record, whatever namespace wrote it
Tekniumd812d97ecc7f
diff --git a/tests/tools/test_browser_launcher_pidns.py b/tests/tools/test_browser_launcher_pidns.pyindex 9926d447c15..2a3163f6fd9 100644--- a/tests/tools/test_browser_launcher_pidns.py+++ b/tests/tools/test_browser_launcher_pidns.py@@ -104,6 +104,48 @@ def test_a_holder_in_another_namespace_keeps_the_shared_browser_up(tmp_path):kill_fake(mine.pid, script)+_FOREIGN_ATTACHER = textwrap.dedent("""\+ import sys+ from tools import browser_launcher_holders as holders+ holders.ns_identity = lambda: sys.argv[3] # this process writes as if from another pid namespace+""") + _HOLDER + 'launcher.stop_browser("profile:" + udd)\n'++[email protected]_system_guard_bypass # the fake browser is ours to stop if a leg failed+def test_an_attacher_from_another_namespace_is_seen_by_the_owner(tmp_path):+ """A host CLI attaching to a container gateway's shared Chromium (one lock dir, one network) leases the+ browser under the OWNER's record identity, its own namespace tag only on the holder part: the owner's stop+ finds that lease and keeps the browser up. Once the attacher lets go, nothing of it is left behind."""+ import hashlib+ script = write_fake_chromium(tmp_path / "chromium")+ udd, key = tmp_path / "pinned-profile", f"profile:{tmp_path / 'pinned-profile'}"+ mine, err = launcher.launch_chromium(key, executable=str(script), user_data_dir=str(udd), ephemeral=False,+ headed=False, env=dict(os.environ), timeout=30)+ assert err is None and mine.owned+ holder = subprocess.Popen([sys.executable, "-c", _FOREIGN_ATTACHER, str(script), str(udd), _FOREIGN],+ cwd=_REPO, env=dict(os.environ), stdin=subprocess.PIPE, stdout=subprocess.PIPE,+ text=True)+ try:+ assert holder.stdout.readline().split() == [str(mine.pid), "False"]+ record = launcher._record_path(mine)+ foreign_tag = f"-n{hashlib.sha256(_FOREIGN.encode()).hexdigest()[:12]}"+ assert [p.name for p in record.parent.glob("*.lease")] == [f"{record.stem}@{holder.pid}{foreign_tag}.lease"]++ launcher.stop_browser(key)+ assert launcher._pid_alive(mine.pid), "the owner stopped a browser another namespace's holder still drives"+ assert launcher.reap_orphaned_browsers() == 0 and launcher._pid_alive(mine.pid)++ holder.stdin.write("\n")+ holder.stdin.flush()+ assert holder.wait(timeout=15) == 0+ assert not list(record.parent.glob("*.lease")), "the attacher left its lease behind"+ assert launcher.reap_orphaned_browsers() == 1+ assert wait_until(lambda: not launcher._pid_alive(mine.pid), timeout=15, what="the browser to stop")+ finally:+ holder.kill()+ kill_fake(mine.pid, script)++@pytest.mark.live_system_guard_bypass # the fake browser is ours to stop if a leg faileddef test_a_dead_holder_in_this_namespace_is_still_dropped(tmp_path):"""Same namespace, behaviour unchanged: the lease of a holder that crashed is dropped, so the launcher'sdiff --git a/tools/browser_launcher.py b/tools/browser_launcher.pyindex 8b916eeea67..f7e52b319b6 100644--- a/tools/browser_launcher.py+++ b/tools/browser_launcher.py@@ -76,6 +76,9 @@ class LaunchedBrowser:owned: boolephemeral: boolhome_key: str+ # File name of the launch record its leases are keyed by, when that is not this process's own name for+ # it: an attached browser keeps its launcher's record, whichever PID namespace wrote it.+ record_name: Optional[str] = None_browsers: dict[str, LaunchedBrowser] = {}@@ -382,14 +385,28 @@ def _attach_running(key: str, user_data_dir: str, home_key: str) -> Optional[Laulogger.info("browser: attaching to the Chromium already running on %s (pid %s)", user_data_dir, pid)browser = _make_browser(key, port, path, pid=pid, start_time=_start_time(pid), user_data_dir=user_data_dir,owned=False, ephemeral=False, home_key=home_key)- record = _record_path(browser)- if record is not None:+ records = _state_dir(browser)+ if records is not None:from tools import browser_launcher_holders as holders+ browser.record_name = _launch_record_name(records, browser)_adopt_record(browser)- holders.write_lease(record, browser) # its launcher's exit must not stop it under us+ holders.write_lease(_record_path(browser), browser) # its launcher's exit must not stop it under usreturn browser+def _launch_record_name(records: Path, browser: LaunchedBrowser) -> Optional[str]:+ """The launch record already naming ``browser`` in ``records``, written in ANY PID namespace: an attacher+ leases the browser under its launcher's record, or a launcher in another namespace (a container gateway a+ host CLI attached to) would miss the lease and stop the browser under it. This namespace's record wins+ over another's on the same pid number; None when there is no record (a browser a human started)."""+ from tools.browser_launcher_holders import ns_tag+ stem = f"{_profile_digest(browser.user_data_dir)[:16]}-{browser.pid}"+ pattern = re.compile(rf"^{re.escape(stem)}(-n[0-9a-f]{{12}})?\.json$")+ names = sorted(p.name for p in records.glob(f"{stem}*.json") if pattern.match(p.name))+ own = f"{stem}{ns_tag()}.json"+ return own if own in names else next(iter(names), None)++def _start_shared(key: str, executable: str, udd: str, home_key: str,**launch) -> tuple[Optional[LaunchedBrowser], Optional[str]]:"""Attach to, or launch, shared profile ``udd``'s one Chromium as ONE step under the profile's cross-process@@ -708,6 +725,8 @@ def _record_path(browser: LaunchedBrowser) -> Optional[Path]:records = _state_dir(browser)if records is None:return None+ if browser.record_name:+ return records / browser.record_namefrom tools.browser_launcher_holders import ns_tagreturn records / f"{_profile_digest(browser.user_data_dir)[:16]}-{browser.pid}{ns_tag()}.json"diff --git a/tools/browser_launcher_holders.py b/tools/browser_launcher_holders.pyindex b0db2885ffe..358084c1cd5 100644--- a/tools/browser_launcher_holders.py+++ b/tools/browser_launcher_holders.py@@ -10,7 +10,9 @@ holder that crashed is dropped once its pid and start time no longer match, neveA pid means something only inside the PID namespace and boot that wrote it (a ``HERMES_HOME`` shared by acontainer gateway and a host CLI): every record, lease and throwaway-dir name carries the writer's-:func:`ns_identity`, and one from another namespace or boot, or one lacking it, is never judged dead.+:func:`ns_identity`, and one from another namespace or boot, or one lacking it, is never judged dead. A lease is+keyed by the record of the browser it holds, whichever namespace wrote that record; only its holder part carries+the lease writer's tag, so a launcher finds every holder whatever namespace it attached from.Every decision runs under the records dir's lock: a lease written while another process decides cannot bemissed. Siblings late-import the facade (its ``_pid_alive`` / ``_start_time`` are the patched seams).