Skip to content

The agent that grows with you

fix(update): the interrupted-pull marker is written temp + fsync + rename before git moves

Tekniumef75941b6b44
diff --git a/hermes_cli/_early_recovery.py b/hermes_cli/_early_recovery.py
index f20c82c593e..336e6add832 100644
--- a/hermes_cli/_early_recovery.py
+++ b/hermes_cli/_early_recovery.py
@@ -696,17 +696,26 @@ def write_zip_swap_journal(root: Path, phase: str, entries: list) -> None:
import json
journal = Path(root) / ZIP_SWAP_JOURNAL
- tmp = journal.with_name(journal.name + ".tmp")
- # A fresh inode every time: writing into a pre-existing temp would follow its symlink or hardlink
- # onto another file (.env). fsync before the rename: an empty journal over a mixed tree is no record.
+ write_durable_text(journal, json.dumps({"pid": os.getpid(), "phase": phase, "entries": entries}),
+ tmp=journal.with_name(journal.name + ".tmp"))
+
+
+def write_durable_text(path: Path, text: str, *, tmp: Path | None = None) -> None:
+ """``text`` at ``path`` as one durable record, never a half-written one.
+
+ A fresh inode every time: writing into a pre-existing temp would follow its symlink or hardlink
+ onto another file (.env). fsync before the rename: an empty record over a mixed tree is no record.
+ The default temp name carries the pid, so two writers never unlink each other's temp.
+ """
+ tmp = tmp if tmp is not None else path.with_name(f"{path.name}.{os.getpid()}.tmp")
tmp.unlink(missing_ok=True)
fd = os.open(tmp, os.O_WRONLY | os.O_CREAT | os.O_EXCL | getattr(os, "O_NOFOLLOW", 0), 0o644)
try:
with os.fdopen(fd, "w", encoding="utf-8") as handle:
- handle.write(json.dumps({"pid": os.getpid(), "phase": phase, "entries": entries}))
+ handle.write(text)
handle.flush()
os.fsync(handle.fileno())
- os.replace(tmp, journal)
+ os.replace(tmp, path)
except BaseException:
tmp.unlink(missing_ok=True)
raise
diff --git a/hermes_cli/update_cmd_commit.py b/hermes_cli/update_cmd_commit.py
index ae6a2acf879..447e836e3e4 100644
--- a/hermes_cli/update_cmd_commit.py
+++ b/hermes_cli/update_cmd_commit.py
@@ -33,6 +33,7 @@ from hermes_cli._early_recovery import (
recovery_closure_dir,
recovery_closure_verified,
restore_interrupted_pull,
+ write_durable_text,
)
from hermes_cli.update_custody import run_git
@@ -206,9 +207,10 @@ def arm_tree_move(git_cmd, root: Path, *, pre: str | None, target: str, stash: s
if pre:
with suppress(OSError, subprocess.SubprocessError): # no closure: the in-tree repair still runs
publish_recovery_closure(git_cmd, root, pre)
- marker.write_text(f"pid={os.getpid()}\npre={pre or ''}\ntarget={target}\nstash={stash or ''}\n"
- + (f"rollback={rollback}\nref={ref}\n" if rollback else "")
- + (f"git={git}\n" if (git := _absolute_git(git_cmd)) else ""), encoding="utf-8")
+ # The restore's only record, durable before git writes: temp + fsync + rename, never in place.
+ write_durable_text(marker, f"pid={os.getpid()}\npre={pre or ''}\ntarget={target}\nstash={stash or ''}\n"
+ + (f"rollback={rollback}\nref={ref}\n" if rollback else "")
+ + (f"git={git}\n" if (git := _absolute_git(git_cmd)) else ""))
return marker
diff --git a/tests/hermes_cli/test_update_interrupted_pull.py b/tests/hermes_cli/test_update_interrupted_pull.py
index afd19b5ace2..5121212991b 100644
--- a/tests/hermes_cli/test_update_interrupted_pull.py
+++ b/tests/hermes_cli/test_update_interrupted_pull.py
@@ -806,3 +806,34 @@ def test_a_move_killed_after_git_wrote_a_symlink_change_is_restored(tmp_path, ba
assert _git(root, "rev-parse", "HEAD") == pre and not er.interrupted_pull_marker(root).exists()
assert _git(root, "status", "--porcelain", "--untracked-files=all") == ""
assert _what_is_at(root / "L") == before
+
+
+def test_the_tree_move_marker_is_durable_before_it_appears_under_its_name(checkout, commit_point, monkeypatch):
+ """The marker is the restore's only record: it is written to a temp file, fsynced, then renamed
+ over its name. An in-place write could leave a power cut with an empty or half marker over a
+ torn tree, which no launch can identify (review C3)."""
+ from hermes_cli import update_cmd_commit
+
+ root, a, b = checkout
+ marker = er.interrupted_pull_marker(root)
+ marker.write_text("pid=1\npre=older\n", encoding="utf-8")
+ synced, replaced = [], []
+ real_fsync, real_replace = os.fsync, os.replace
+
+ def fsync(fd):
+ synced.append(fd)
+ return real_fsync(fd)
+
+ def replace(src, dst):
+ assert synced, "renamed into place before its bytes were fsynced"
+ assert marker.read_text(encoding="utf-8") == "pid=1\npre=older\n" # never rewritten in place
+ replaced.append((Path(src), Path(dst)))
+ return real_replace(src, dst)
+
+ monkeypatch.setattr(os, "fsync", fsync)
+ monkeypatch.setattr(os, "replace", replace)
+ update_cmd_commit.arm_tree_move(["git"], root, pre=None, target=b, stash=None)
+
+ assert [dst for _src, dst in replaced] == [marker]
+ assert f"target={b}" in marker.read_text(encoding="utf-8")
+ assert not list(marker.parent.glob(marker.name + "*.tmp"))