The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(update): the foreign-lock record and the takeover breadcrumb are written temp + fsync + rename
Tekniume52209f228d5
diff --git a/hermes_cli/_early_recovery.py b/hermes_cli/_early_recovery.pyindex 7d157b0e3e3..af2bcb808e3 100644--- a/hermes_cli/_early_recovery.py+++ b/hermes_cli/_early_recovery.py@@ -1133,9 +1133,7 @@ def _remember_foreign_lock(marker: Path, lock: Path) -> None:returnlines = [line for line in marker.read_text(encoding="utf-8-sig").splitlines()if not line.startswith("foreign_lock=")]- tmp = marker.with_name(marker.name + ".tmp")- tmp.write_text("\n".join([*lines, f"foreign_lock={identity}"]) + "\n", encoding="utf-8")- os.replace(tmp, marker)+ write_durable_text(marker, "\n".join([*lines, f"foreign_lock={identity}"]) + "\n")def _redo_rollback_head(git, git_dir: Path, root: Path, pre: str, mode: str, *, after_failure: bool) -> str | None:diff --git a/hermes_cli/_update_takeover.py b/hermes_cli/_update_takeover.pyindex 51334f4da83..8c7d4660a49 100644--- a/hermes_cli/_update_takeover.py+++ b/hermes_cli/_update_takeover.py@@ -80,11 +80,12 @@ def _arm_fleet_obligation(root: Path) -> None:return# Same fallback as ``update_cmd_fleet._write_fleet_restart_pending_marker``: the per-home# breadcrumb every reader still honours.+ from hermes_cli._early_recovery import write_durable_textfrom hermes_constants import get_hermes_homelines = [f"started={time.time()}", f"pid={os.getpid()}"] + ([f"expected_sha={sha}"] if sha else [])try:- (get_hermes_home() / "fleet_restart_pending").write_text("\n".join(lines) + "\n", encoding="utf-8")+ write_durable_text(get_hermes_home() / "fleet_restart_pending", "\n".join(lines) + "\n")except OSError as exc:print(f"Warning: could not record the owed gateway restart ({exc}); run `hermes update` again ""to restart the gateway onto the new code.", file=sys.stderr)diff --git a/tests/hermes_cli/test_early_recovery.py b/tests/hermes_cli/test_early_recovery.pyindex cadc7fdfc6c..d04384bcb1b 100644--- a/tests/hermes_cli/test_early_recovery.py+++ b/tests/hermes_cli/test_early_recovery.py@@ -273,3 +273,22 @@ def test_a_failed_repair_counts_its_attempt_even_on_an_undecodable_marker(tmp_paer._count_failed_attempt(marker)assert er._read_marker_attempts(marker) == 1+++def test_the_foreign_lock_record_is_fsynced_under_a_per_writer_temp(tmp_path, monkeypatch):+ """_remember_foreign_lock rewrote the restore's only record through a fixed-name ``.tmp`` with no+ fsync (review C12, invariant 5): a crash could leave an empty marker, and two writers shared one temp."""+ marker, lock = tmp_path / "hermes-update-pull", tmp_path / "update.lock"+ marker.write_text("pid=1\npre=abc\n", encoding="utf-8")+ lock.write_text("x", encoding="utf-8")+ synced, replaced = [], []+ real_fsync, real_replace = os.fsync, os.replace+ monkeypatch.setattr(os, "fsync", lambda fd: synced.append(fd) or real_fsync(fd))+ monkeypatch.setattr(os, "replace", lambda src, dst: replaced.append((Path(src), Path(dst), len(synced)))+ or real_replace(src, dst))++ er._remember_foreign_lock(marker, lock)++ assert [(dst, count > 0) for _src, dst, count in replaced] == [(marker, True)]+ assert replaced[0][0].name != marker.name + ".tmp"+ assert marker.read_text(encoding="utf-8").endswith(f"foreign_lock={er._lock_identity(lock)}\n")diff --git a/tests/hermes_cli/test_old_updater_takeover.py b/tests/hermes_cli/test_old_updater_takeover.pyindex ee7f0c9082d..d817cecb769 100644--- a/tests/hermes_cli/test_old_updater_takeover.py+++ b/tests/hermes_cli/test_old_updater_takeover.py@@ -580,3 +580,24 @@ def test_a_finish_child_that_cannot_start_after_the_commit_is_owed_not_failed(tmassert (get_hermes_home() / ".update_exit_code").read_text(encoding="utf-8").strip() == "0"assert json.loads(result.read_text(encoding="utf-8"))["resume_handled"] is Falseassert "finishing steps did not run" in capsys.readouterr().err+++def test_the_takeover_breadcrumb_is_written_temp_fsync_rename(tmp_path, monkeypatch):+ """With the host record unwritable the takeover falls back to the per-home breadcrumb; it was+ written in place (review C12, invariant 5): a crash mid-write left a truncated debt record."""+ from hermes_cli import _update_takeover, update_host_obligation+ from hermes_constants import get_hermes_home++ monkeypatch.setattr(update_host_obligation, "write_host_obligation", lambda **_kw: False)+ monkeypatch.setattr(_update_takeover, "_head_sha", lambda _root: "a" * 40)+ synced, replaced = [], []+ real_fsync, real_replace = os.fsync, os.replace+ monkeypatch.setattr(os, "fsync", lambda fd: synced.append(fd) or real_fsync(fd))+ monkeypatch.setattr(os, "replace", lambda src, dst: replaced.append((Path(dst), len(synced)))+ or real_replace(src, dst))++ _update_takeover._arm_fleet_obligation(tmp_path)++ 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")