The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(update): a rollback settles on the paths it wrote and never advises reset --hard (review F73)
Teknium89103b807823
diff --git a/hermes_cli/_early_recovery.py b/hermes_cli/_early_recovery.pyindex c7270bca60e..1a96a7424df 100644--- a/hermes_cli/_early_recovery.py+++ b/hermes_cli/_early_recovery.py@@ -1016,8 +1016,7 @@ def _restore_holding_claim(root: Path, marker: Path, *, after_failure: bool = Faif written is None: # after a gc or re-clone: nothing left to compare againstif rollback and not _rollback_verified(git, git_dir, pre):print(f"⚠ An interrupted `hermes update` rollback to {pre[:10]} is not verified and commit "- f"{target[:10]} is gone. Inspect `git -C {root} status`; `git -C {root} reset --hard "- f"{pre[:10]}` finishes it.", file=sys.stderr)+ f"{target[:10]} is gone; the marker was kept. Inspect `git -C {root} status`.", file=sys.stderr)return Falsemarker.unlink()print(f"⚠ Ignoring a stale interrupted-update marker: commit {target[:10]} is gone.", file=sys.stderr)@@ -1042,12 +1041,12 @@ def _restore_holding_claim(root: Path, marker: Path, *, after_failure: bool = Fafor rel in sorted(new_dirs, key=lambda d: d.count("/"), reverse=True):with contextlib.suppress(OSError):(root / rel).rmdir() # only when empty: an untracked file inside keeps it- if rollback and not _rollback_verified(git, git_dir, pre):- # A rollback owns the whole tree (the update parked local edits first): only HEAD on ``pre``- # with no tracked change left is a finished rollback. Anything less keeps its only record.- print(f" ✗ The rollback to {pre[:10]} is not verified yet (tracked files still differ); the next "- f"launch retries. Inspect `git -C {root} status`; `git -C {root} reset --hard {pre[:10]}` "- "finishes it by hand.", file=sys.stderr)+ if rollback and not _rollback_verified(git, git_dir, pre, owned=set(restore)):+ # Only HEAD on ``pre`` with every path the update wrote back at ``pre`` is a finished rollback;+ # anything less keeps its only record. Other tracked edits (made after the kill, or autocrlf /+ # filemode noise) are not the rollback's to judge, and no reset is advised: it would wipe them.+ print(f" ✗ The rollback to {pre[:10]} is not verified yet (files the update wrote still differ); "+ f"the next launch retries. Inspect `git -C {root} status`.", file=sys.stderr)return bool(restore or added)marker.unlink()if not restore and not added:@@ -1080,13 +1079,15 @@ def _redo_rollback_head(git, git_dir: Path, root: Path, pre: str, mode: str, *,return None-def _rollback_verified(git, git_dir: Path, pre: str) -> bool:- """The whole-tree rollback landed: HEAD is ``pre``, no index lock, no tracked change."""+def _rollback_verified(git, git_dir: Path, pre: str, owned: set[str] | None = None) -> bool:+ """The rollback landed: HEAD is ``pre``, no index lock, and no tracked change among ``owned`` (the+ paths the update wrote), or anywhere when they are unknown (``target`` gone)."""head = git("rev-parse", "HEAD")if head.returncode != 0 or head.stdout.strip() != pre or (git_dir / "index.lock").exists():return False- status = git("status", "--porcelain", "-z", "--untracked-files=no")- return status.returncode == 0 and not status.stdout.strip("\0")+ status = git("status", "--porcelain", "-z", "--untracked-files=no", "--no-renames")+ changed = {entry[3:] for entry in status.stdout.split("\0") if entry}+ return status.returncode == 0 and not (changed if owned is None else changed & owned)def _put_back_paths(git, root: Path, restore: list[str], added: list[str]) -> subprocess.CompletedProcess | None:diff --git a/tests/hermes_cli/test_update_interrupted_pull.py b/tests/hermes_cli/test_update_interrupted_pull.pyindex 878bf20d08a..f2bd540c1e6 100644--- a/tests/hermes_cli/test_update_interrupted_pull.py+++ b/tests/hermes_cli/test_update_interrupted_pull.py@@ -410,6 +410,19 @@ def test_a_rollback_marker_outlives_a_lock_that_may_still_be_live(tmp_path):assert _git(root, "status", "--porcelain", "--untracked-files=no") == ""+def test_a_rollback_settles_beside_an_unrelated_tracked_edit_and_keeps_it(tmp_path, capsys):+ """HEAD and the index are back on ``pre`` but the broken files are still on disk, and the user has+ since edited a file the update never touched: the rollback finishes on its own paths, the edit stays."""+ root, pre, target = _broken_release(tmp_path, 4)+ marker = _rollback_marker(root, pre, target)+ _git(root, "reset", "-q", pre) # the rollback's reset landed; its file restore was killed+ (root / "bulk" / "f1.txt").write_text("my edit\n", encoding="utf-8", newline="")+ assert er.restore_interrupted_pull(root) is True+ assert "reset --hard" not in capsys.readouterr().err+ assert not marker.exists() and (root / "module.py").read_text(encoding="utf-8") == "good = True\n"+ assert _git(root, "status", "--porcelain", "--untracked-files=no") == "M bulk/f1.txt"++def test_a_launch_that_cannot_get_the_repair_claim_never_continues_from_the_torn_tree(tmp_path, monkeypatch):"""Another launch holds the restore claim past the wait while the marker says the tree is torn:this launch must stop (fail closed) instead of reporting 'nothing to repair' and importing it."""