Skip to content

The agent that grows with you

fix(gateway): a killed update's dead marker no longer blocks every launch

Tekniumed8973bbb749
diff --git a/hermes_cli/gateway_runtime.py b/hermes_cli/gateway_runtime.py
index c3c209ebc15..690ccf946b3 100644
--- a/hermes_cli/gateway_runtime.py
+++ b/hermes_cli/gateway_runtime.py
@@ -179,6 +179,36 @@ def discover_gateway_endpoint(profile_home: str | Path, *, timeout: float = 2.0)
return GatewayDiscovery("inaccessible", reason_code="invalid_control_peer")
+def _update_fenced(homes) -> bool:
+ """True while an update owns the install, judged the way main's launchers and the Desktop
+ judge ``.hermes-update-in-progress``: a live (or still being written) marker, or a dead or
+ malformed one while the checkout lock is held (a killed update's tree still runs). A dead
+ marker over a free lock is a killed update: it never blocks a launch.
+
+ Read-only: a client never clears the updater's fence; the launch-time reclaim
+ (``update_lock.read_live_update``) and the next update remove a dead marker. A marker that
+ exists but cannot be read is unjudgeable and fences.
+ """
+ import time
+ from hermes_cli import update_lock
+
+ for fence_home in homes:
+ path = fence_home / update_lock.MARKER_NAME
+ try:
+ raw = path.read_bytes()
+ if not raw:
+ # An exclusive first publish in flight (contract A3), not yet a dead claim.
+ if time.time() - path.stat().st_mtime < update_lock.EMPTY_MARKER_GRACE_SECONDS:
+ return True
+ except FileNotFoundError:
+ continue
+ except OSError:
+ return True
+ if update_lock.judge_marker(raw)[0] in {"live", "ours"} or update_lock.checkout_lock_held():
+ return True
+ return False
+
+
def ensure_gateway_runtime(profile_home: str | Path, *, timeout: float = DEFAULT_ENSURE_TIMEOUT) -> GatewayDiscovery:
"""Ensure once, never install/replace; pending remains pending at deadline.
@@ -190,7 +220,6 @@ def ensure_gateway_runtime(profile_home: str | Path, *, timeout: float = DEFAULT
RuntimeStartError, discover_existing_gateway_service, remaining,
start_existing_gateway_service,
)
- from hermes_cli.update_lock import MARKER_NAME
from hermes_constants import get_default_hermes_root, get_process_hermes_home
if not math.isfinite(timeout) or timeout <= 0:
@@ -201,13 +230,8 @@ def ensure_gateway_runtime(profile_home: str | Path, *, timeout: float = DEFAULT
delay = 0.025
try:
while True:
- # Never clear the updater's fence, even if malformed. Repair is not
- # a client operation. The install-root marker also covers profiles.
- for fence_home in {home, get_process_hermes_home(), get_default_hermes_root()}:
- try:
- (fence_home / MARKER_NAME).lstat()
- except FileNotFoundError:
- continue
+ # The install-root marker also covers profiles.
+ if _update_fenced({home, get_process_hermes_home(), get_default_hermes_root()}):
return GatewayDiscovery("draining", reason_code="update_paused")
observed = discover_gateway_endpoint(home, timeout=remaining(deadline))
if observed.reason_code == "control_timeout":
diff --git a/tests/hermes_cli/test_gateway_runtime_ensure.py b/tests/hermes_cli/test_gateway_runtime_ensure.py
index f65fcb28836..dff06557244 100644
--- a/tests/hermes_cli/test_gateway_runtime_ensure.py
+++ b/tests/hermes_cli/test_gateway_runtime_ensure.py
@@ -115,7 +115,11 @@ def test_public_ensure_json_deadline_and_invalid_invocation(tmp_path):
assert callable(getattr(runtime, "ensure_gateway_runtime", None))
home = tmp_path / "profile"
home.mkdir(mode=0o700)
- (home / ".hermes-update-in-progress").write_text("private-token-not-for-stdout", encoding="utf-8")
+ # A LIVE claim (this test process, at its own incarnation) fences; a dead one would not.
+ from hermes_cli.update_lock import process_create_time
+ (home / ".hermes-update-in-progress").write_text(
+ f"{os.getpid()}\n{int(time.time())}\nct:{process_create_time():.3f}\nrun:private-token-not-for-stdout\n",
+ encoding="utf-8")
env = {**os.environ, "HERMES_HOME": str(home)}
result = subprocess.run([sys.executable, "-m", "hermes_cli.main", "gateway", "ensure", "--json"],
env=env, stdin=subprocess.DEVNULL, capture_output=True, text=True, timeout=15)
@@ -134,6 +138,29 @@ def test_public_ensure_json_deadline_and_invalid_invocation(tmp_path):
assert "private-value" not in result.stdout
+def test_update_fence_follows_update_lock_judgement(tmp_path, monkeypatch):
+ """A killed update's dead or malformed marker never blocks a launch (main's launch contract);
+ the same marker fences while the checkout lock is held, and a live claim always fences."""
+ from hermes_cli import gateway_runtime as runtime, update_lock
+ from hermes_cli.update_lock import process_create_time
+
+ marker = tmp_path / update_lock.MARKER_NAME
+ dead = subprocess.Popen([sys.executable, "-c", "pass"])
+ dead_ct = process_create_time(dead.pid)
+ dead.wait(timeout=30)
+ held = []
+ monkeypatch.setattr(update_lock, "checkout_lock_held", lambda *a, **k: bool(held))
+ for body in (f"{dead.pid}\n{int(time.time())}\nct:{dead_ct or 1.0:.3f}\n", "not a marker"):
+ marker.write_text(body, encoding="utf-8")
+ assert not runtime._update_fenced({tmp_path}), body
+ held.append(True)
+ assert runtime._update_fenced({tmp_path}), body
+ held.clear()
+ marker.write_text(f"{os.getpid()}\n{int(time.time())}\nct:{process_create_time():.3f}\n", encoding="utf-8")
+ assert runtime._update_fenced({tmp_path})
+ assert marker.is_file() # read-only: a client never clears the updater's fence
+
+
@pytest.mark.platforms("linux")
def test_unmanaged_child_uses_explicit_home_and_survives_launcher_exit(tmp_path, monkeypatch):
from hermes_cli import gateway_runtime_start as start