Skip to content

The agent that grows with you

fix(update): a failed stage copy keeps its journal until the partial copy is gone

Tekniumbd77f4826a28
diff --git a/hermes_cli/_early_recovery.py b/hermes_cli/_early_recovery.py
index 5014e2ebaa8..51c174d68ce 100644
--- a/hermes_cli/_early_recovery.py
+++ b/hermes_cli/_early_recovery.py
@@ -569,8 +569,21 @@ def write_zip_swap_journal(root: Path, phase: str, entries: list) -> None:
def _drop_path(path: Path) -> None:
if path.is_dir() and not path.is_symlink():
import shutil
+ import stat
- shutil.rmtree(path)
+ try:
+ shutil.rmtree(path)
+ except OSError:
+ # A staged copy keeps its source's modes: a read-only directory refuses to give up its
+ # entries. These trees are the update's own copies, so make them writable and retry.
+ rwx = stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR
+ os.chmod(path, os.stat(path).st_mode | rwx)
+ for dirpath, dirnames, files in os.walk(path):
+ for name in (*dirnames, *files):
+ child = os.path.join(dirpath, name)
+ if not os.path.islink(child): # about to be deleted: only the owner bits matter
+ os.chmod(child, os.stat(child).st_mode | (rwx if name in dirnames else stat.S_IWUSR))
+ shutil.rmtree(path)
elif path.exists() or path.is_symlink():
path.unlink()
diff --git a/hermes_cli/update_cmd_zip.py b/hermes_cli/update_cmd_zip.py
index ac399fe2912..2618de177fd 100644
--- a/hermes_cli/update_cmd_zip.py
+++ b/hermes_cli/update_cmd_zip.py
@@ -343,12 +343,14 @@ def _graft_nested_artifacts(item: str, live: str, staging: str) -> None:
def _stage_entries(extracted: str, entries: list[str], project_root: str) -> list[tuple[str, str]]:
"""Phase 1 for every entry; on failure nothing is live yet, so drop partial staging copies so a retry
- starts from the same free space."""
+ starts from the same free space. Each entry is recorded BEFORE its copy starts: a copy that fails
+ partway (an unreadable source file) leaves a partial staging tree that must be dropped too."""
staged: list[tuple[str, str]] = []
try:
for item in entries:
dst = os.path.join(project_root, item)
- staged.append((_stage_replacement(os.path.join(extracted, item), dst), dst))
+ staged.append((f"{dst}.hermes-update-staging", dst))
+ _stage_replacement(os.path.join(extracted, item), dst)
# The source ZIP carries only source; the built outputs (#70337/#87331 release/, then
# dist/, apps/desktop/node_modules and web_dist — #90495) exist only in the LIVE tree. Graft
# them into the staged copy BEFORE the swap so the commit preserves them atomically.
@@ -359,6 +361,21 @@ def _stage_entries(extracted: str, entries: list[str], project_root: str) -> lis
return staged
+def _staging_left(root: Path, entries: list[str]) -> list[str]:
+ """The declared staging paths still on disk (a cleanup that could not finish)."""
+ return [item for item in entries if os.path.lexists(os.path.join(root, item + ".hermes-update-staging"))]
+
+
+def _drop_journal_if_clean(root: Path, entries: list[str]) -> None:
+ """The journal is the only record of the staging paths: it goes only once every one of them is gone."""
+ left = _staging_left(root, entries)
+ if left:
+ print(f" ⚠ Could not remove the staged copies of {', '.join(left)}; the next `hermes` launch "
+ "removes them.")
+ return
+ (root / ZIP_SWAP_JOURNAL).unlink(missing_ok=True)
+
+
def _journaled_stage_and_swap(extracted: str, entries: list[str], root: Path, target_sha) -> list:
"""Stage + swap under the ZIP swap journal and its owner lock (``_early_recovery``), so a kill at
any point is finished or rolled back by the next launch and no staging/backup sibling leaks."""
@@ -381,7 +398,7 @@ def _journaled_stage_and_swap(extracted: str, entries: list[str], root: Path, ta
try:
staged = _stage_entries(extracted, entries, str(root))
except BaseException:
- (root / ZIP_SWAP_JOURNAL).unlink(missing_ok=True) # _stage_entries dropped its copies
+ _drop_journal_if_clean(root, entries) # _stage_entries dropped its copies, if it could
raise
try:
# TOCTOU re-check right before the swap: download + extract + staging can take minutes and
@@ -389,7 +406,7 @@ def _journaled_stage_and_swap(extracted: str, entries: list[str], root: Path, ta
recheck_reason = _zip_overlay_block_reason(root, ignore_staging_artifacts=True, shipped=entries)
if recheck_reason is not None:
_discard_staged(staged)
- (root / ZIP_SWAP_JOURNAL).unlink(missing_ok=True)
+ _drop_journal_if_clean(root, entries)
print(f"✗ ZIP fallback aborted before the swap: {recheck_reason}.")
print(" Files appeared in the checkout while the update was downloading; committing the swap would delete them.")
print(_STASH_HINT)
diff --git a/tests/hermes_cli/test_update_zip_two_phase.py b/tests/hermes_cli/test_update_zip_two_phase.py
index 6c781ac3f35..807cac445a9 100644
--- a/tests/hermes_cli/test_update_zip_two_phase.py
+++ b/tests/hermes_cli/test_update_zip_two_phase.py
@@ -17,7 +17,7 @@ from pathlib import Path
import pytest
-from hermes_cli import update_cmd
+from hermes_cli import update_cmd, update_cmd_zip
from hermes_constants import venv_bin_dir, venv_python_path
# ---------------------------------------------------------------------------
@@ -357,3 +357,148 @@ def test_root_files_never_go_missing_mid_swap(tmp_path, monkeypatch):
assert {n: (live / n).read_text(encoding="utf-8-sig") for n in ("hermes_constants.py", "hermes_bootstrap.py")} == {
"hermes_constants.py": "new", "hermes_bootstrap.py": "new"}
assert not [p for p in os.listdir(live) if "hermes-update" in p]
+
+
+# ---------------------------------------------------------------------------
+# ZIP swap owner lock and staging journal (_early_recovery / _journaled_stage_and_swap)
+# ---------------------------------------------------------------------------
+
+_LOCK_ROLE = r'''
+import json, os, sys, time
+from pathlib import Path
+from hermes_cli import _early_recovery as er
+role, work = sys.argv[1], Path(sys.argv[2])
+live, lock_path = work / "live", work / "live" / ".hermes-update-zip-swap.lock"
+def wait_for(name):
+ end = time.time() + 30
+ while not (work / name).exists():
+ if time.time() > end:
+ raise SystemExit(f"{role}: no {name}")
+ time.sleep(0.01)
+def mark(name, owned):
+ (work / name).write_text(json.dumps({"owned": bool(owned), "inode": os.stat(lock_path).st_ino
+ if lock_path.exists() else None}))
+if role == "a": # owner whose release is caught right after its unlock: B already holds the inode
+ real = er._lock_fd
+ def lock_fd(fd, lock):
+ done = real(fd, lock)
+ if not lock:
+ wait_for("b")
+ return done
+ er._lock_fd = lock_fd
+ with er.zip_swap_owner_lock(live) as owned:
+ mark("a", owned)
+ wait_for("b-waiting"); time.sleep(0.3)
+ (work / "a-done").touch()
+elif role == "b": # waiter that wins the lock as A lets go
+ wait_for("a"); (work / "b-waiting").touch()
+ with er.zip_swap_owner_lock(live, wait=20) as owned:
+ mark("b", owned)
+ wait_for("c")
+else: # newcomer after A's release completed
+ wait_for("a-done")
+ with er.zip_swap_owner_lock(live) as owned:
+ mark("c", owned)
+'''
+
+
+def test_zip_swap_lock_is_one_inode_across_a_release(tmp_path):
+ """Three real processes: A releases while B waits; B wins. C must then be refused: a release that
+ unlinks the lock path lets C lock a fresh inode next to B's (two "exclusive" owners)."""
+ import json
+ import subprocess
+ import sys
+
+ (tmp_path / "live").mkdir()
+ repo = os.path.realpath(Path(update_cmd.__file__).parent.parent)
+ procs = [subprocess.Popen([sys.executable, "-c", _LOCK_ROLE, role, str(tmp_path)], cwd=repo,
+ env={**os.environ, "PYTHONPATH": repo}) for role in "abc"]
+ assert [p.wait(timeout=90) for p in procs] == [0, 0, 0]
+ a, b, c = (json.loads((tmp_path / r).read_text()) for r in "abc")
+ assert a["owned"] and b["owned"]
+ assert not c["owned"], f"two processes held the ZIP swap lock at once (inodes {b['inode']} and {c['inode']})"
+ assert a["inode"] == b["inode"] == c["inode"]
+
+
[email protected](os.name == "nt" or (hasattr(os, "geteuid") and os.geteuid() == 0),
+ reason="POSIX permission bits; root ignores them")
+def test_zip_swap_lock_refuses_admission_without_a_lock(tmp_path):
+ """A root where the lock file cannot be created grants nothing: no lock, no swap, and a named reason."""
+ from hermes_cli._early_recovery import zip_swap_owner_lock
+
+ live = tmp_path / "live"
+ live.mkdir()
+ live.chmod(0o555)
+ try:
+ with zip_swap_owner_lock(live) as owned:
+ assert not owned, "admitted to the ZIP swap without holding any lock"
+ assert owned.reason.startswith("cannot open the ZIP swap lock")
+ with pytest.raises(RuntimeError, match="no ZIP swap lock"):
+ update_cmd_zip._journaled_stage_and_swap(str(tmp_path), [], live, None)
+ finally:
+ live.chmod(0o755)
+
+
+def _unreadable_release(tmp_path: Path, *, read_only_dir: bool) -> tuple[Path, Path, list[Path]]:
+ live, extracted = tmp_path / "live", tmp_path / "extracted"
+ live.mkdir()
+ extracted.mkdir()
+ (live / "keep.txt").write_text("live data")
+ (extracted / "first.txt").write_text("new first")
+ (extracted / "second").mkdir()
+ (extracted / "second" / "good.txt").write_text("new copied data")
+ locked = []
+ if read_only_dir: # copytree copies its mode: the partial stage gets a directory nobody can empty
+ ro = extracted / "second" / "aaa_ro"
+ ro.mkdir()
+ (ro / "x.txt").write_text("x")
+ locked.append(ro)
+ blocked = extracted / "second" / "zzz_no_read.txt"
+ blocked.write_text("unreadable")
+ locked.append(blocked)
+ for path in locked:
+ path.chmod(0o555 if path.is_dir() else 0)
+ return live, extracted, locked
+
+
+_POSIX_MODES = pytest.mark.skipif(os.name == "nt" or (hasattr(os, "geteuid") and os.geteuid() == 0),
+ reason="POSIX permission bits; root ignores them")
+
+
+@_POSIX_MODES
+def test_unreadable_source_file_leaves_no_partial_stage(tmp_path):
+ """The copy of ``second`` dies on an unreadable file after copying the rest: that partial staging
+ tree is the failing entry's and must be dropped like the finished ones (nothing live changes)."""
+ from hermes_cli._early_recovery import ZIP_SWAP_JOURNAL, restore_interrupted_zip_swap
+
+ live, extracted, locked = _unreadable_release(tmp_path, read_only_dir=False)
+ try:
+ with pytest.raises(OSError):
+ update_cmd_zip._journaled_stage_and_swap(str(extracted), ["first.txt", "second"], live, None)
+ finally:
+ for path in locked:
+ path.chmod(0o755)
+ assert not list(live.glob("*.hermes-update-staging")), "a partial stage leaked past the failed copy"
+ assert not (live / ZIP_SWAP_JOURNAL).exists()
+ assert restore_interrupted_zip_swap(live) is False
+ assert sorted(p.name for p in live.iterdir()) == [".hermes-update-zip-swap.lock", "keep.txt"]
+
+
+@_POSIX_MODES
+def test_a_stage_cleanup_that_cannot_finish_keeps_the_journal_for_recovery(tmp_path):
+ """When the updater cannot remove its partial stage, the journal is that stage's only record: it
+ stays, and the next launch's recovery removes the stage, then the journal."""
+ from hermes_cli._early_recovery import ZIP_SWAP_JOURNAL, restore_interrupted_zip_swap
+
+ live, extracted, locked = _unreadable_release(tmp_path, read_only_dir=True)
+ try:
+ with pytest.raises(OSError):
+ update_cmd_zip._journaled_stage_and_swap(str(extracted), ["first.txt", "second"], live, None)
+ finally:
+ for path in locked:
+ path.chmod(0o755)
+ assert (live / "second.hermes-update-staging").exists(), "harness: the cleanup was expected to fail"
+ assert (live / ZIP_SWAP_JOURNAL).exists(), "the journal went while its staging path was still on disk"
+ assert restore_interrupted_zip_swap(live) is False # nothing live moved: no relaunch
+ assert not list(live.glob("*.hermes-update-staging")) and not (live / ZIP_SWAP_JOURNAL).exists()
+ assert (live / "keep.txt").read_text() == "live data"