Skip to content

The agent that grows with you

fix(update): a resumed rollback only rewinds the ref the rollback left HEAD on (review F18)

Tekniumc17dab978ac3
diff --git a/hermes_cli/_early_recovery.py b/hermes_cli/_early_recovery.py
index 81bfcb6fd08..ad81c702a32 100644
--- a/hermes_cli/_early_recovery.py
+++ b/hermes_cli/_early_recovery.py
@@ -987,6 +987,14 @@ def _restore_holding_claim(root: Path, marker: Path, *, after_failure: bool = Fa
if head is None:
return False
if rollback and pre and target and head == target:
+ # Only the ref the rollback left HEAD on may be rewound: a branch the user checked out at
+ # ``target`` since is theirs (and the update's branch would stay on the broken commit).
+ named = git("symbolic-ref", "-q", "HEAD")
+ if "ref" not in fields or (named.stdout.strip() if named.returncode == 0 else "") != fields["ref"].strip():
+ print(f"⚠ An interrupted `hermes update` rollback to {pre[:10]} was not resumed: HEAD now names "
+ f"another branch. Run `git reset --hard {pre[:10]}` on the updated branch to finish it.",
+ file=sys.stderr)
+ return False
# A syntax rollback killed before it moved HEAD back: redo that step (HEAD and index, no file),
# so the restore below lands on ``pre`` (the code the update started from), not the broken tree.
# Every step is checked, and the killed step's ``index.lock`` goes first, only once its git is
diff --git a/hermes_cli/update_cmd_commit.py b/hermes_cli/update_cmd_commit.py
index fccb0b7541b..431abbed7d3 100644
--- a/hermes_cli/update_cmd_commit.py
+++ b/hermes_cli/update_cmd_commit.py
@@ -150,14 +150,19 @@ def arm_tree_move(git_cmd, root: Path, *, pre: str | None, target: str, stash: s
``rollback`` (``branch``/``detach``): a syntax rollback that moves HEAD and the index back to
``pre`` before any file; a kill before that step finds HEAD still on ``target`` and the restore
- redoes it first.
+ redoes it first, only while HEAD still names the ``ref`` recorded here (empty: detached).
"""
marker = interrupted_pull_marker(root)
+ ref = ""
+ if rollback:
+ named = run_git(git_cmd, ["symbolic-ref", "-q", "HEAD"], cwd=str(root), capture_output=True, text=True,
+ encoding="utf-8", errors="replace", stdin=subprocess.DEVNULL, timeout=60)
+ ref = named.stdout.strip() if named.returncode == 0 else ""
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}\n" if rollback else "")
+ + (f"rollback={rollback}\nref={ref}\n" if rollback else "")
+ (f"git={git}\n" if (git := _absolute_git(git_cmd)) else ""), encoding="utf-8")
return marker
diff --git a/tests/hermes_cli/test_update_interrupted_pull.py b/tests/hermes_cli/test_update_interrupted_pull.py
index 2c109f62422..1a524a58f15 100644
--- a/tests/hermes_cli/test_update_interrupted_pull.py
+++ b/tests/hermes_cli/test_update_interrupted_pull.py
@@ -334,7 +334,8 @@ def _broken_release(tmp_path: Path, nfiles: int) -> tuple[Path, str, str]:
def _rollback_marker(root: Path, pre: str, target: str) -> Path:
marker = er.interrupted_pull_marker(root)
- marker.write_text(f"pid=0\npre={pre}\ntarget={target}\nstash=\nrollback=branch\n", encoding="utf-8", newline="")
+ marker.write_text(f"pid=0\npre={pre}\ntarget={target}\nstash=\nrollback=branch\nref=refs/heads/main\n",
+ encoding="utf-8", newline="")
return marker
@@ -560,7 +561,7 @@ def test_a_recorded_git_that_is_no_longer_git_falls_back_and_still_repairs(tmp_p
if stub.startswith("#!"):
stub_path.chmod(0o755)
marker = er.interrupted_pull_marker(root)
- marker.write_text(f"pid=0\npre={pre}\ntarget={target}\nstash=\nrollback=branch\ngit={stub_path}\n",
+ marker.write_text(f"pid=0\npre={pre}\ntarget={target}\nstash=\nrollback=branch\nref=refs/heads/main\ngit={stub_path}\n",
encoding="utf-8", newline="")
assert er.restore_interrupted_pull(root) is True
assert _git(root, "rev-parse", "HEAD") == pre and not marker.exists()
@@ -720,3 +721,16 @@ def test_an_unmarked_rollback_that_fails_never_reports_the_install_unchanged(che
out = capsys.readouterr().out
assert "Rollback complete" not in out and "Recover manually" in out
+
+
+def test_a_rollback_never_rewinds_a_branch_checked_out_at_its_target_since(tmp_path, commit_point):
+ """The user ran `git checkout -b feature` on the broken release before relaunching: the redo must
+ not reset *feature*, and main must not be left behind with the marker gone (F18)."""
+ root, pre, target = _broken_release(tmp_path, 3)
+ commit_point.arm_tree_move(["git"], root, pre=pre, target=target, stash=None, rollback="branch")
+ _git(root, "checkout", "-q", "-b", "feature")
+
+ er.restore_interrupted_pull(root)
+
+ assert _git(root, "rev-parse", "feature") == target
+ assert er.interrupted_pull_marker(root).exists()