Skip to content

The agent that grows with you

fix(update): the obligation restore never deletes or writes through a same-name sibling

Teknium8b2124e0c1c5
diff --git a/hermes_cli/update_cmd_commit.py b/hermes_cli/update_cmd_commit.py
index 7ff0aae6bfc..57d275c3959 100644
--- a/hermes_cli/update_cmd_commit.py
+++ b/hermes_cli/update_cmd_commit.py
@@ -118,7 +118,7 @@ def disarm_commit_obligations() -> None:
if root is not None:
_owe_for(root, head)
return
- from hermes_cli.update_host_obligation import host_obligation_path, release_host_obligation
+ from hermes_cli.update_host_obligation import host_obligation_path, release_host_obligation, replace_bytes
snapshot, _armed_snapshot = _armed_snapshot, None
armed = dict(_armed_bytes)
@@ -136,15 +136,9 @@ def disarm_commit_obligations() -> None:
if data is None:
path.unlink(missing_ok=True)
else:
- # A fresh file, never through whatever a leftover ``.restore`` name points at.
- tmp = path.with_name(f".{path.name}.{os.getpid()}.restore")
- tmp.unlink(missing_ok=True)
- fd = os.open(tmp, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600)
- with os.fdopen(fd, "wb") as handle:
- handle.write(data)
- handle.flush()
- os.fsync(handle.fileno())
- os.replace(tmp, path)
+ # An unpredictable exclusive temp, never a fixed name: nothing at any guessable
+ # sibling is written through or deleted first (reviews N05, O1).
+ replace_bytes(path, data)
except OSError:
pass # an owed tail/restart left armed is a retry, never a lost obligation
diff --git a/hermes_cli/update_host_obligation.py b/hermes_cli/update_host_obligation.py
index a31996fecec..11eb0de34fb 100644
--- a/hermes_cli/update_host_obligation.py
+++ b/hermes_cli/update_host_obligation.py
@@ -218,10 +218,10 @@ def release_host_obligation(owner: str) -> None:
if found is None:
path.unlink(missing_ok=True)
else:
- _replace_bytes(path, found)
+ replace_bytes(path, found)
-def _replace_bytes(path: Path, data: bytes) -> 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")
diff --git a/tests/hermes_cli/test_update_commit_obligations.py b/tests/hermes_cli/test_update_commit_obligations.py
index 6f44eea1b7a..e7bc3ccb521 100644
--- a/tests/hermes_cli/test_update_commit_obligations.py
+++ b/tests/hermes_cli/test_update_commit_obligations.py
@@ -369,3 +369,37 @@ def test_a_second_update_in_one_process_never_hands_back_the_first_updates_debt(
commit.disarm_commit_obligations()
assert {path: path.read_bytes() if path.exists() else None for path in owed} == owed
+
+
[email protected]("posix") # unprivileged symlinks
[email protected]("kind", ["regular", "symlink", "hardlink", "none"])
+def test_disarm_restores_through_an_unpredictable_temp_and_leaves_every_sibling(root, tmp_path, kind):
+ """The restore used a fixed ``.<name>.<pid>.restore`` temp and unlinked whatever stood there
+ first: a user's ordinary file of that name was deleted (review O1). Nothing at the old name, or
+ any alias target, is touched; the record still comes back."""
+ from hermes_cli.venv_sync import completion_pending_path
+
+ tail = completion_pending_path(root)
+ tail.parent.mkdir(parents=True, exist_ok=True)
+ tail.write_bytes(b"OLD-TAIL")
+ sentinel = tmp_path / "sentinel"
+ sentinel.write_bytes(b"PRECIOUS")
+ commit.arm_commit_obligations(root, "a" * 40)
+ planted = tail.with_name(f".{tail.name}.{os.getpid()}.restore")
+ if kind == "regular":
+ planted.write_bytes(b"USER-FILE")
+ elif kind == "symlink":
+ planted.symlink_to(sentinel)
+ elif kind == "hardlink":
+ os.link(sentinel, planted)
+
+ commit.disarm_commit_obligations()
+
+ assert tail.read_bytes() == b"OLD-TAIL" and not tail.is_symlink()
+ assert sentinel.read_bytes() == b"PRECIOUS"
+ if kind == "regular":
+ assert planted.read_bytes() == b"USER-FILE"
+ elif kind != "none":
+ assert os.path.lexists(planted) and planted.read_bytes() == b"PRECIOUS"
+ assert sorted(p.name for p in tail.parent.iterdir() if p.name.endswith(".restore")) == (
+ [planted.name] if kind != "none" else [])