Skip to content

The agent that grows with you

fix(update): a historical updater's hand-off arms the host record instead of crashing

Teknium3113844f8e3c
diff --git a/hermes_cli/update_host_obligation.py b/hermes_cli/update_host_obligation.py
index 7cef722767c..781ad17807c 100644
--- a/hermes_cli/update_host_obligation.py
+++ b/hermes_cli/update_host_obligation.py
@@ -170,14 +170,33 @@ def _record_mutex(path: Path):
The sidecar sits in a subdirectory: the state dir is the gateway lock dir, whose ``--replace``
cleanup unlinks every top-level ``*.lock``, and a deleted sidecar splits the lock in two.
"""
- from hermes_cli.update_lock import marker_mutex # stdlib-only, like this module
-
guard = path.parent / f".{path.stem}.mutex"
guard.mkdir(parents=True, exist_ok=True)
- with marker_mutex(guard / "record"):
+ with _marker_mutex()(guard / "record"):
yield
+def _marker_mutex():
+ """``update_lock.marker_mutex``. A historical updater imported ``update_lock`` before its pull and
+ then lazily imports this pulled module, so its in-memory copy predates the mutex: load the pulled
+ ``update_lock`` (stdlib-only) beside it, which locks the same sidecar every current process does."""
+ import importlib.util
+ import sys
+
+ from hermes_cli import update_lock
+
+ if hasattr(update_lock, "marker_mutex"):
+ return update_lock.marker_mutex
+ name = "hermes_cli._update_lock_pulled"
+ pulled = sys.modules.get(name)
+ if pulled is None:
+ spec = importlib.util.spec_from_file_location(name, update_lock.__file__)
+ pulled = importlib.util.module_from_spec(spec)
+ sys.modules[name] = pulled # before exec: dataclasses resolve the module by name
+ spec.loader.exec_module(pulled)
+ return pulled.marker_mutex
+
+
def _owners(record: dict) -> list[str]:
owners = record.get("owners")
return [str(o) for o in owners] if isinstance(owners, list) else []
diff --git a/tests/hermes_cli/test_old_updater_takeover.py b/tests/hermes_cli/test_old_updater_takeover.py
index d817cecb769..836ae657d3b 100644
--- a/tests/hermes_cli/test_old_updater_takeover.py
+++ b/tests/hermes_cli/test_old_updater_takeover.py
@@ -601,3 +601,17 @@ def test_the_takeover_breadcrumb_is_written_temp_fsync_rename(tmp_path, monkeypa
crumb = get_hermes_home() / "fleet_restart_pending"
assert replaced and replaced[-1][0] == crumb and replaced[-1][1] > 0
assert f"expected_sha={'a' * 40}" in crumb.read_text(encoding="utf-8")
+
+
+def test_a_historical_updater_arms_the_host_record_under_the_current_mutex(tmp_path, monkeypatch):
+ """An N-1 updater imported ``update_lock`` before its pull, then lazily imports the pulled
+ ``update_host_obligation``: the arm must still write the record, under the same sidecar lock
+ current processes hold, instead of crashing the update on the missing ``marker_mutex``."""
+ from hermes_cli import update_lock
+ from hermes_cli.update_host_obligation import read_host_obligation, write_host_obligation
+
+ monkeypatch.setenv("HERMES_GATEWAY_LOCK_DIR", str(tmp_path / "gateway-locks"))
+ monkeypatch.delattr(update_lock, "marker_mutex") # N-1's in-memory update_lock had no such name
+ assert write_host_obligation(expected_sha="a" * 40)
+ assert read_host_obligation()["expected_sha"] == "a" * 40
+ assert (tmp_path / "gateway-locks" / ".host-update-restart.mutex" / "record.lock").exists()