fix(browser): a shared daemon is owned only by the Hermes whose command started it

Two same-user Hermes processes can reach one daemon. Both read bu-default.pid on the default
Browser Use name, and every Hermes shares the real-profile attach session. The identity checks (uid,
module, session leader, socket-dir binding) prove what the daemon is, not who started it, so both
Hermes registered its group. Killing either one then killed the browser the other was still using.
The real-profile attach's before/after pid-file diff had the same hole: two concurrent attaches
both see the file change.

Each agent-browser and browser-harness command now carries this process's spawn tag (spawn_env:
pid plus create time). The daemon it forks inherits the tag, and _own_local_daemon /
_own_harness_daemon register a daemon only when its tag names this process
(process_identity.spawned_by_this_process; an unreadable environment fails closed). The pid-file
diff is gone. Probed on macOS against agent-browser 0.26.0: the forked daemon carries the tag.

New tests start a verified daemon tagged with another live process. Each fails with its check
removed (the daemon gets registered) and passes with it.

Reported by Greptile on #137057 (two threads).
This commit is contained in:
kshitijk4poor
2026-10-18 14:29:31 +05:30
committed by kshitij
parent 57644ec000
commit e8ccfffc0b
3 changed files with 55 additions and 3 deletions

View File

@@ -212,6 +212,25 @@ def parse_spawn_tag(raw: object) -> Optional[SpawnTag]:
return SpawnTag(install, purpose, pid, create) if pid > 0 else None
def spawned_by_this_process(pid: int) -> bool:
"""True when ``pid`` carries a spawn tag naming THIS process (pid and create time).
A shared daemon (one session or pid file every Hermes of this user can reach) is owned only by
the Hermes whose command actually started it: the CLI it ran was given ``spawn_env`` and the
daemon inherited it. A pid file proves what a process is, not who started it. Fails closed:
an unreadable environment (another user, a race) is not ours."""
try:
import psutil
tag = parse_spawn_tag(psutil.Process(int(pid)).environ().get(SPAWN_ENV_VAR))
except Exception:
return False
if tag is None or tag.spawner_pid != os.getpid():
return False
mine = _process_create_time()
return tag.spawner_create is None or mine is None or abs(tag.spawner_create - mine) < 2.0
# Layer 2 — spawn ledger