Skip to content

The agent that grows with you

fix: the host restart record has one durable writer that also syncs its directory

Teknium25c688a0c7a7
diff --git a/hermes_cli/update_host_obligation.py b/hermes_cli/update_host_obligation.py
index 442f47e758b..8661af0bcf7 100644
--- a/hermes_cli/update_host_obligation.py
+++ b/hermes_cli/update_host_obligation.py
@@ -67,19 +67,8 @@ def _owed_path() -> Path:
def _write_record(path: Path, record: dict) -> None:
- """``utils.atomic_json_write(path, record, mode=0o600)`` in stdlib only (see above): the temp
- file is 0600 from ``mkstemp``, fsynced, then renamed over the record."""
- fd, tmp = tempfile.mkstemp(dir=path.parent, prefix=f".{path.stem}_", suffix=".tmp")
- try:
- with os.fdopen(fd, "w", encoding="utf-8") as handle:
- json.dump(record, handle, indent=2, ensure_ascii=False)
- handle.flush()
- os.fsync(handle.fileno())
- os.replace(tmp, path)
- except BaseException:
- with contextlib.suppress(OSError):
- os.unlink(tmp)
- raise
+ """``utils.atomic_json_write(path, record, mode=0o600)`` in stdlib only (see above)."""
+ replace_bytes(path, json.dumps(record, indent=2, ensure_ascii=False).encode("utf-8"))
def read_host_obligation() -> Optional[dict]:
@@ -289,9 +278,9 @@ def _release_locked(path: Path, owner: str) -> None:
def replace_bytes(path: Path, data: bytes) -> None:
- """Put ``data`` back at ``path``: a fresh ``mkstemp`` file (never through a planted alias),
- fsynced, then renamed over the record."""
- fd, tmp = tempfile.mkstemp(dir=path.parent, prefix=f".{path.stem}_", suffix=".restore")
+ """``data`` at ``path`` as one durable record: a fresh 0600 ``mkstemp`` file (never written
+ through a planted alias), fsynced, renamed over ``path``, then the rename itself made durable."""
+ fd, tmp = tempfile.mkstemp(dir=path.parent, prefix=f".{path.stem}_", suffix=".tmp")
try:
with os.fdopen(fd, "wb") as handle:
handle.write(data)
@@ -302,6 +291,12 @@ def replace_bytes(path: Path, data: bytes) -> None:
with contextlib.suppress(OSError):
os.unlink(tmp)
raise
+ if os.name != "nt": # Windows opens no directory handle; NTFS journals the rename itself
+ directory = os.open(path.parent, os.O_RDONLY)
+ try:
+ os.fsync(directory)
+ finally:
+ os.close(directory)
def clear_host_obligation() -> None:
diff --git a/tests/hermes_cli/test_update_host_obligation.py b/tests/hermes_cli/test_update_host_obligation.py
index d749546e548..cd57c7dfc61 100644
--- a/tests/hermes_cli/test_update_host_obligation.py
+++ b/tests/hermes_cli/test_update_host_obligation.py
@@ -281,6 +281,24 @@ def test_a_completion_never_erases_the_unkeyed_debt_an_older_install_armed(tmp_p
assert not host_obligation.host_obligation_present()
[email protected]("posix") # Windows opens no directory handle; NTFS journals the rename
+def test_every_host_record_write_makes_its_rename_durable(tmp_path, monkeypatch):
+ """Arm and restore share one writer, and both fsync the state dir after the rename: a power loss
+ after the arm returned must not roll the directory back to "nothing owed"."""
+ import stat
+
+ monkeypatch.setenv("HERMES_GATEWAY_LOCK_DIR", str(tmp_path / "gateway-locks"))
+ events, real_fsync, real_replace = [], os.fsync, os.replace
+ monkeypatch.setattr(os, "fsync", lambda fd: events.append(
+ "dir" if stat.S_ISDIR(os.fstat(fd).st_mode) else "file") or real_fsync(fd))
+ monkeypatch.setattr(os, "replace", lambda src, dst: events.append("rename") or real_replace(src, dst))
+
+ assert host_obligation.write_host_obligation(expected_sha=SHA)
+ host_obligation.replace_bytes(host_obligation.host_obligation_path(), b"{}")
+
+ assert events == ["file", "rename", "dir"] * 2
+
+
def test_unreadable_host_record_is_never_discharged_by_the_legacy_marker(two_profiles, no_live_fleet, monkeypatch, tmp_path):
"""A record whose terms are UNKNOWN cannot be settled by another record's terms.