The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(review): fail closed on unopenable or just-created runtime leases
teknium16da0d5a887cb
diff --git a/hermes_cli/runtime_state.py b/hermes_cli/runtime_state.pyindex 1cc75833dce..6fd4aa84bb3 100644--- a/hermes_cli/runtime_state.py+++ b/hermes_cli/runtime_state.py@@ -34,6 +34,10 @@ LOG = logging.getLogger(__name__)# same rule boot_bootstrap._RecordLock states for home maintenance.INSTALL_LOCK_TIMEOUT_SECONDS = 10.0+# A lease file younger than this is never a prune candidate: its creator may still sit between+# ``O_CREAT|O_EXCL`` and ``flock`` (two syscalls; the window is microseconds, the margin generous).+LEASE_GRACE_SECONDS = 60.0+@contextmanagerdef runtime_lock(project: Path, *, timeout: float | None = INSTALL_LOCK_TIMEOUT_SECONDS):"""Hold the per-install dependency lock; yields True when held, False when the wait expired.@@ -209,6 +213,12 @@ def _prune_unlocked_leases(leases: Path) -> bool:Kernel locks disappear even when ``execv``, ``os._exit`` or a crash bypasses``atexit``. Cleaning those unlocked files whenever a reader arrives bounds leaks inthe selected generation too, which generation GC intentionally never visits.++ Fail closed: a lease this user cannot open (root-owned 0o600 from a ``sudo hermes`` on the+ same checkout) is held, never a boot failure; a lease younger than ``LEASE_GRACE_SECONDS``+ is held too, because ``lease_directory`` creates and locks in two syscalls and callers+ outside ``runtime_lock`` (pm workers, launch) would otherwise prune a peer's lease in+ between — leaving that peer locking an unlinked inode for its lifetime."""held = Falsefor lease in leases.glob("*"):@@ -216,8 +226,11 @@ def _prune_unlocked_leases(leases: Path) -> bool:fd = os.open(lease, os.O_RDWR)except FileNotFoundError:continue+ except OSError:+ held = True+ continuetry:- if not _lock(fd, wait=False):+ if time.time() - os.fstat(fd).st_mtime < LEASE_GRACE_SECONDS or not _lock(fd, wait=False):held = Trueelse:with suppress(OSError):diff --git a/tests/pm/test_runtime_gc.py b/tests/pm/test_runtime_gc.pyindex 02c94663695..1e9beb594e6 100644--- a/tests/pm/test_runtime_gc.py+++ b/tests/pm/test_runtime_gc.py@@ -5,6 +5,8 @@ from pathlib import Pathimport subprocessimport sys+import pytest+from pm.runtime import collect_runtime_generations@@ -69,6 +71,7 @@ os._exit(0)leases = generation / ".leases"stale = list(leases.iterdir())assert len(stale) == 1+ os.utime(stale[0], (0, 0)) # past LEASE_GRACE_SECONDS: nobody can still be between create and flockrelease = lease_directory(generation)try:@@ -79,3 +82,28 @@ os._exit(0)release()assert list(leases.iterdir()) == []++[email protected](getattr(os, "geteuid", lambda: 1)() == 0, reason="root opens mode-0 files")+def test_prune_keeps_unopenable_and_just_created_sibling_leases(tmp_path):+ """A lease this user cannot open (root-owned, `sudo hermes` on the same checkout) or one a+ peer created but has not flocked yet is HELD: boot never raises, GC never collects."""+ from hermes_cli.runtime_state import lease_directory, leases_held++ generation = _generation(tmp_path / "pm-runtime", "selected")+ leases = generation / ".leases"+ leases.mkdir()+ foreign = leases / "foreign"+ foreign.touch()+ os.utime(foreign, (0, 0))+ foreign.chmod(0)+ fresh = leases / "fresh"+ fresh.touch()+ try:+ lease_directory(generation)()+ assert foreign.exists() and fresh.exists()+ assert leases_held(generation)+ os.utime(fresh, (0, 0))+ assert leases_held(generation) and not fresh.exists()+ finally:+ foreign.chmod(0o600)