The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): boot waits while a lock holder has not published its record
Teknium515212f4153e
diff --git a/gateway/run_bootstrap.py b/gateway/run_bootstrap.pyindex 25aa6997c88..475f0348bc2 100644--- a/gateway/run_bootstrap.py+++ b/gateway/run_bootstrap.py@@ -552,7 +552,10 @@ _BROWSE_RECONCILE_RESERVE_WAIT_S = 5.0def _reserve_waiting_out_browse_reconcile(ownership, homes, *, wait_s: float = _BROWSE_RECONCILE_RESERVE_WAIT_S):- from gateway.runtime_ownership import OwnershipConflict, maintenance_holder+ """Reserve *homes*, waiting (bounded) while a contender is a browse reconcile or has not yet+ published its lock record (flock taken, record unwritten). Only a positively identified holder+ that is not browse maintenance refuses at once; the flock exclusion itself is never weakened."""+ from gateway.runtime_ownership import OwnershipConflict, lock_record_unpublished, maintenance_holderfrom hermes_state_reconcile import BROWSE_RECONCILE_PURPOSEdeadline = time.monotonic() + wait_slogged = False@@ -560,7 +563,8 @@ def _reserve_waiting_out_browse_reconcile(ownership, homes, *, wait_s: float = _try:return ownership.reserve(homes)except OwnershipConflict:- holders = [h for h in homes if maintenance_holder(h, BROWSE_RECONCILE_PURPOSE)]+ holders = [h for h in homes+ if maintenance_holder(h, BROWSE_RECONCILE_PURPOSE) or lock_record_unpublished(h)]if not holders or time.monotonic() >= deadline:raiseif not logged:diff --git a/gateway/runtime_ownership.py b/gateway/runtime_ownership.pyindex f4701dcaff4..df37e189f6a 100644--- a/gateway/runtime_ownership.py+++ b/gateway/runtime_ownership.py@@ -227,6 +227,16 @@ def maintenance_holder(home: Path, purpose: str) -> dict | None:return record+def lock_record_unpublished(home: Path) -> bool:+ """True when *home*'s ``gateway.lock`` record names no live holder yet: empty, torn, or a dead+ previous holder's. :meth:`ProfileOwnership.reserve` takes the flock BEFORE it writes the record,+ so a contender that loses the flock in that window cannot tell maintenance from a gateway; it+ must retry (bounded) instead of reporting a runtime owner. Advisory only, never grants the lock."""+ from gateway.status import _live_pid_from_record, _read_gateway_lock_record+ record = _read_gateway_lock_record(canonical_home(home) / 'gateway.lock')+ return not record or _live_pid_from_record(record) is None++def live_runtime_owner(home: Path) -> int | None:"""PID of a live runtime owner of *home* (a gateway, or a reservation that is not marked asmaintenance), else None. A purpose-marked maintenance holder is never an owner."""diff --git a/tests/hermes_cli/test_browse_schema_drift.py b/tests/hermes_cli/test_browse_schema_drift.pyindex 950fbfba59f..d0d25f01602 100644--- a/tests/hermes_cli/test_browse_schema_drift.py+++ b/tests/hermes_cli/test_browse_schema_drift.py@@ -446,3 +446,63 @@ def test_gateway_boot_reserve_waits_out_a_browse_reconcile(tmp_path):contender.close()gateway.kill()gateway.wait()+++# ── Independent review round 2 ──+++_RAW_FLOCK_HOLDER = r'''+import fcntl, os, sys, time+from pathlib import Path+home = Path(sys.argv[1]); home.mkdir(parents=True, exist_ok=True)+fh = open(home / "gateway.lock", "a+")+fcntl.flock(fh.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)+fh.seek(0); fh.truncate(); fh.flush() # flock held, record not yet published (reserve()'s window)+print("held", flush=True)+time.sleep(float(sys.argv[2]))+'''++[email protected]("linux", "macos")+def test_gateway_boot_waits_while_the_holder_has_not_published_its_record(tmp_path):+ """R2-1: reserve() takes the flock before it writes the purpose record. A boot that loses the+ flock in that window must retry (bounded), not abort as though a gateway owned the profile."""+ import os+ import subprocess+ import sys+ import time++ from gateway.run_bootstrap import _reserve_waiting_out_browse_reconcile+ from gateway.runtime_ownership import OwnershipConflict, ProfileOwnership++ home = _profile_home(tmp_path)+ holder = subprocess.Popen([sys.executable, "-c", _RAW_FLOCK_HOLDER, str(home), "1.0"],+ env=os.environ, stdout=subprocess.PIPE, text=True)+ assert holder.stdout.readline().strip() == "held"+ booting = ProfileOwnership()+ try:+ assert (home / "gateway.lock").read_text() == ""+ started = time.monotonic()+ _reserve_waiting_out_browse_reconcile(booting, [home], wait_s=10)+ assert booting.owns(home)+ assert 0.5 < time.monotonic() - started < 9+ finally:+ booting.close()+ holder.kill()+ holder.wait()++ # Still bounded: an unpublished holder that never lets go is refused once the wait expires.+ holder = subprocess.Popen([sys.executable, "-c", _RAW_FLOCK_HOLDER, str(home), "30"],+ env=os.environ, stdout=subprocess.PIPE, text=True)+ assert holder.stdout.readline().strip() == "held"+ contender = ProfileOwnership()+ try:+ started = time.monotonic()+ with pytest.raises(OwnershipConflict):+ _reserve_waiting_out_browse_reconcile(contender, [home], wait_s=0.6)+ assert 0.5 < time.monotonic() - started < 3+ finally:+ contender.close()+ holder.kill()+ holder.wait()+