The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(process-identity): trust boot ticks only within the boot that recorded them
Teknium6c3f8a2542b2
diff --git a/hermes_cli/process_identity.py b/hermes_cli/process_identity.pyindex 6a25f62d2a5..c9ba5958631 100644--- a/hermes_cli/process_identity.py+++ b/hermes_cli/process_identity.py@@ -9,6 +9,7 @@ root — no launcher→worker chains left holding ``.pyd`` locks.from __future__ import annotations+import functoolsimport hashlibimport jsonimport logging@@ -126,9 +127,37 @@ def _process_create_time(pid: Optional[int] = None) -> Optional[float]:return None[email protected]_cache(maxsize=1)+def _live_boot_id() -> Optional[str]:+ """The kernel's id for the running boot (Linux); ``None`` elsewhere or when unreadable."""+ try:+ return Path("/proc/sys/kernel/random/boot_id").read_text(encoding="utf-8").strip() or None+ except OSError:+ return None+++# No live process has a negative boot_start, so an entry from an earlier boot never matches.+_EARLIER_BOOT = -1.0+++def _entry_boot(entry: dict, key: str) -> Optional[float]:+ """The entry's boot-relative birth ``key`` (``boot_start``/``spawner_boot``) as exact proof.++ Boot ticks restart at every boot while the ledger survives reboots (and early-boot services+ get the same pid at the same tick), so they identify a process only within the boot that+ recorded them: an entry from another boot is :data:`_EARLIER_BOOT`, matching nothing alive.+ ``None`` (epoch match) when either boot id is unknown.+ """+ recorded, live = entry.get("boot_id"), _live_boot_id()+ if not isinstance(recorded, str) or live is None:+ return None+ return entry.get(key) if recorded == live else _EARLIER_BOOT++def _verified_boot_start(pid: Optional[int], create_time: Optional[float]) -> Optional[float]:- """:func:`boot_start` of ``pid`` while it still matches ``create_time``; ``None`` when unproven."""- if not isinstance(pid, int) or pid <= 0 or create_time is None:+ """:func:`boot_start` of ``pid`` while it still matches ``create_time``; ``None`` when unproven+ or when the boot is unidentifiable (ticks alone repeat across reboots)."""+ if not isinstance(pid, int) or pid <= 0 or create_time is None or _live_boot_id() is None:return Nonetry:import psutil@@ -140,11 +169,17 @@ def _verified_boot_start(pid: Optional[int], create_time: Optional[float]) -> Opdef _step_can_explain_mismatch(pid: int, boot: Optional[float]) -> bool:- """A live ``pid`` failed an epoch-only match on Linux: a clock step explains that as well as a- reused pid, so the mismatch proves nothing. ``False`` when ``boot`` was recorded (exact)."""+ """A running ``pid`` failed an epoch-only match on Linux: a clock step explains that as well as+ a reused pid, so the mismatch proves nothing. ``False`` when ``boot`` gave an exact verdict and+ when ``pid`` is gone or a zombie (exited, only awaiting its parent's reap)."""import psutil # the caller's epoch match already needed it- return boot is None and bool(getattr(psutil, "LINUX", False)) and psutil.pid_exists(pid)+ if boot is not None or not getattr(psutil, "LINUX", False):+ return False+ try:+ return psutil.Process(pid).status() != psutil.STATUS_ZOMBIE+ except psutil.Error:+ return False# Layer 1 — spawn tags@@ -209,9 +244,11 @@ class LedgerEntry:# machine). Attach-first readers must never adopt it; argv is truncated, so this is canonical.isolated: bool = False# Linux boot-relative births (:func:`boot_start`) of the process and its spawner: exact- # identity across clock steps. ``None`` elsewhere and in older entries (epoch match only).+ # identity across clock steps, within the boot named by ``boot_id`` (:func:`_entry_boot`).+ # ``None`` elsewhere and in older entries (epoch match only).boot_start: Optional[float] = Nonespawner_boot: Optional[float] = None+ boot_id: Optional[str] = Nonedef _ledger_path() -> Path:@@ -401,7 +438,7 @@ def _new_entry() -> LedgerEntry:return LedgerEntry(pid, create_time, purpose, install_id(project_root), spawner_pid, spawner_create, time.time(), argv="",- boot_start=_verified_boot_start(pid, create_time),+ boot_start=_verified_boot_start(pid, create_time), boot_id=_live_boot_id(),)@@ -485,7 +522,7 @@ def ledger_entries(if (all_installs or e.get("install") == want_install)and isinstance(e.get("pid"), int)and (_pid_alive_matches(e["pid"], e.get("create_time"), strict=True) is True- if verified_only else _pid_alive_matches(e["pid"], e.get("create_time"), boot=e.get("boot_start")) is not False)+ if verified_only else _pid_alive_matches(e["pid"], e.get("create_time"), boot=_entry_boot(e, "boot_start")) is not False)]@@ -495,7 +532,7 @@ def entry_still_running(entry: dict, *, keep_unprovable: bool = False) -> bool:pid disagrees on Linux also counts, because a clock step explains that as well as pid reuse.Readers that protect a process (ledger pruning, the scratch prune's owner check) pass it;killers never do."""- pid, boot = entry.get("pid"), entry.get("boot_start")+ pid, boot = entry.get("pid"), _entry_boot(entry, "boot_start")if not isinstance(pid, int) or pid <= 0:return Falseif _pid_alive_matches(pid, entry.get("create_time"), boot=boot) is not False:@@ -508,11 +545,12 @@ def spawner_is_dead(entry: dict) -> Optional[bool]:spawner_pid = entry.get("spawner_pid")if not isinstance(spawner_pid, int) or spawner_pid <= 0:return None- boot = entry.get("spawner_boot")+ # A spawner recorded in an earlier boot is dead (:func:`_entry_boot`), as the epoch match says.+ boot = _entry_boot(entry, "spawner_boot")alive = _pid_alive_matches(spawner_pid, entry.get("spawner_create"), boot=boot)# An entry that survives a clock step (``boot_start``) with an epoch-only spawner: a step# would read its live spawner as dead and let a reaper kill the child.- if alive is False and entry.get("boot_start") is not None and _step_can_explain_mismatch(spawner_pid, boot):+ if alive is False and _entry_boot(entry, "boot_start") is not None and _step_can_explain_mismatch(spawner_pid, boot):return Nonereturn None if alive is None else not alive@@ -598,7 +636,7 @@ def reap_orphaned_backend_owner(try:if kill_fn is not None:kill_fn(pid)- elif not _terminate_then_kill(pid, entry.get("create_time"), entry.get("boot_start")):+ elif not _terminate_then_kill(pid, entry.get("create_time"), _entry_boot(entry, "boot_start")):return Noneexcept Exception:logger.debug("backend owner reap failed for %s", entry, exc_info=True)@@ -633,7 +671,7 @@ def reap_orphaned_mcp_helpers(*, project_root: Optional[Path] = None, kill_fn=Noimport psutilproc = psutil.Process(pid)- if not _same_incarnation(proc, entry.get("create_time"), entry.get("boot_start")):+ if not _same_incarnation(proc, entry.get("create_time"), _entry_boot(entry, "boot_start")):continue # PID reused since registration# Windows: descendants (npx.cmd → node.exe) have no pgid to group-kill and# reparent with ParentId=null when the direct child exits first (#61059), sodiff --git a/tests/test_scratch_registered_clock_step.py b/tests/test_scratch_registered_clock_step.pyindex b38374a5c51..fdf01302bde 100644--- a/tests/test_scratch_registered_clock_step.py+++ b/tests/test_scratch_registered_clock_step.py@@ -1,5 +1,7 @@-"""The scratch prune's owner check survives a wall-clock step (NTP, VM resume)."""+"""The spawn ledger's boot-relative identity: a registered gateway's scratch survives a wall-clock+step (NTP, VM resume), and boot ticks never vouch for a process across a reboot."""+import jsonimport osimport subprocessimport sys@@ -9,6 +11,10 @@ from pathlib import Pathimport psutilimport pytest+from hermes_cli.data_cleanup_holders import _refuse_backend_writers+from hermes_cli.process_identity import (+ _ledger_path, boot_start, install_id, ledger_entries, reap_orphaned_mcp_helpers, register_self,+)from hermes_constants import get_scratch_dir, prune_scratch_dirfrom tests.waiting import wait_until@@ -50,3 +56,44 @@ def test_prune_keeps_a_registered_gateways_shell_after_a_clock_step(tmp_path, mochild.kill()gateway.kill()gateway.wait(timeout=10)++[email protected]("linux") # boot ids and boot-relative ticks are recorded only here+def test_ledger_never_vouches_for_an_earlier_boot_or_an_exited_backend(tmp_path, monkeypatch):+ """The ledger survives reboots but boot ticks restart: an MCP-helper entry written in an+ earlier boot whose pid and start tick equal an unrelated process running now (early-boot+ services are deterministic) names a stranger, and its spawner is gone, so the orphan reaper+ must leave the stranger alone. An older epoch-only serve entry whose backend has exited (a+ zombie its parent has not reaped yet) no longer owns HERMES_HOME. Neither row survives the+ next ledger write."""+ monkeypatch.setenv("HERMES_HOME", str(tmp_path))+ stranger = subprocess.Popen(["sleep", "60"], stdin=subprocess.DEVNULL)+ backend = subprocess.Popen(["true"], stdin=subprocess.DEVNULL)+ spawner = subprocess.Popen(["true"], stdin=subprocess.DEVNULL)+ spawner.wait(timeout=10)+ try:+ wait_until(lambda: psutil.Process(backend.pid).status() == psutil.STATUS_ZOMBIE,+ timeout=10, what="the backend exited")+ live = psutil.Process(stranger.pid)+ earlier_boot = time.time() - 3 * 86400+ _ledger_path().write_text(json.dumps([+ {"pid": stranger.pid, "create_time": live.create_time() - 3 * 86400, "purpose": "mcp-helper",+ "install": install_id(), "spawner_pid": spawner.pid, "spawner_create": earlier_boot,+ "registered_at": earlier_boot, "argv": "", "boot_start": boot_start(live), "spawner_boot": 1.0,+ "boot_id": "00000000-0000-4000-8000-000000000000"},+ {"pid": backend.pid, "create_time": psutil.Process(backend.pid).create_time(), "purpose": "serve",+ "install": install_id(), "spawner_pid": None, "spawner_create": None,+ "registered_at": time.time(), "argv": ""},+ ]), encoding="utf-8")++ assert stranger.pid not in [e["pid"] for e in ledger_entries()]+ assert reap_orphaned_mcp_helpers() == []+ assert stranger.poll() is None, "the orphan reaper killed a process from a later boot"+ _refuse_backend_writers(tmp_path) # raises while a backend still owns the home+ assert register_self("gateway")+ rows = json.loads(_ledger_path().read_text(encoding="utf-8"))+ assert [row["pid"] for row in rows] == [os.getpid()]+ finally:+ stranger.kill()+ stranger.wait(timeout=10)+ backend.wait(timeout=10)