Skip to content

The agent that grows with you

fix(update): a syntax rollback resets HEAD and the index before any file

Tekniumf562145c700a
diff --git a/hermes_cli/_early_recovery.py b/hermes_cli/_early_recovery.py
index eabe1da9dc2..3cbecb4c9e2 100644
--- a/hermes_cli/_early_recovery.py
+++ b/hermes_cli/_early_recovery.py
@@ -648,10 +648,12 @@ def _restore_holding_claim(root: Path, marker: Path, *, after_failure: bool = Fa
**({"text": True, "encoding": "utf-8", "errors": "replace"} if text else {}))
rollback = fields.get("rollback", "").strip()
- if rollback in ("soft", "detach") and pre and target and git("rev-parse", "HEAD").stdout.strip() == target:
- # A syntax rollback killed before it moved HEAD back: redo that step (it writes no file), so
- # the restore below lands on ``pre`` (the code the update started from), not the broken tree.
- git(*(["reset", "--soft", pre] if rollback == "soft" else ["update-ref", "--no-deref", "HEAD", pre]))
+ if rollback in ("branch", "detach") and pre and target and git("rev-parse", "HEAD").stdout.strip() == target:
+ # 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.
+ if rollback == "detach":
+ git("update-ref", "--no-deref", "HEAD", pre)
+ git("reset", "-q", pre)
if not pre or not target or git("rev-parse", "HEAD").stdout.strip() != pre:
marker.unlink() # git finished (HEAD moved) or the marker is unusable
return False
diff --git a/hermes_cli/update_cmd.py b/hermes_cli/update_cmd.py
index cba3a7f4dab..3f538b53788 100644
--- a/hermes_cli/update_cmd.py
+++ b/hermes_cli/update_cmd.py
@@ -927,25 +927,25 @@ def _rollback_if_pulled_syntax_error(git_cmd, pre_pull_sha, *, rollback_branch=N
root = _m().PROJECT_ROOT
parked = rollback_branch not in (None, "HEAD")
print(f"→ Rolling back to {rollback_branch if parked else pre_pull_sha[:10]}...")
- # HEAD goes back FIRST, without writing a file, then the files follow. A kill anywhere in
+ # HEAD and the index go back FIRST, without writing a file, then the files follow. A kill in
# between leaves HEAD on pre_pull_sha with the broken files reading as the move's own
# writes, so the next launch's restore lands on pre_pull_sha, never on the broken commit;
# ``rollback=`` lets it redo the HEAD step when the kill came before it.
- mode = "soft" if rollback_branch is None else "detach"
- head_step = (["reset", "--soft", pre_pull_sha] if mode == "soft"
- else ["update-ref", "--no-deref", "HEAD", pre_pull_sha])
+ mode = "branch" if rollback_branch is None else "detach"
with _best_effort('Could not write the interrupted-pull marker: %s'):
_commit.arm_tree_move(git_cmd, root, pre=pre_pull_sha, target=_capture_head_sha(git_cmd, root),
stash=None, rollback=mode)
- rollback_args = head_step
- rollback_result = _git_run(git_cmd, head_step)
+ if mode == "detach": # never move the update branch onto a parked/detached commit
+ rollback_args = ["update-ref", "--no-deref", "HEAD", pre_pull_sha]
+ rollback_result = _git_run(git_cmd, rollback_args)
+ if mode == "branch" or rollback_result.returncode == 0:
+ rollback_args = ["reset", "-q", pre_pull_sha] # HEAD (and the index) only, no file
+ rollback_result = _git_run(git_cmd, rollback_args)
if rollback_result.returncode == 0:
rollback_args = ["reset", "--hard", pre_pull_sha]
rollback_result = _git_run(git_cmd, rollback_args)
- if rollback_result.returncode != 0 and _commit.settle_failed_tree_move(root):
- rollback_result = subprocess.CompletedProcess(rollback_args, 0, "", "")
- else:
- interrupted_pull_marker(root).unlink(missing_ok=True) # nothing moved: still the pulled tree
+ if rollback_result.returncode != 0 and _commit.settle_failed_tree_move(root):
+ rollback_result = subprocess.CompletedProcess(rollback_args, 0, "", "") # restored in-process
if rollback_result.returncode == 0 and parked:
# Same commit, so only HEAD's name changes. The parked branch can be unavailable (e.g.
# checked out in another worktree): its commit then stays checked out detached.
diff --git a/hermes_cli/update_cmd_commit.py b/hermes_cli/update_cmd_commit.py
index af63ad846b4..2534691586f 100644
--- a/hermes_cli/update_cmd_commit.py
+++ b/hermes_cli/update_cmd_commit.py
@@ -108,8 +108,9 @@ def arm_tree_move(git_cmd, root: Path, *, pre: str | None, target: str, stash: s
rollback: str | None = None) -> Path:
"""Write the interrupted-pull marker for one git tree move (pre -> target).
- ``rollback`` (``soft``/``detach``): a syntax rollback that moves HEAD back to ``pre`` before any
- file; a kill before that step finds HEAD still on ``target`` and the restore redoes it first.
+ ``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.
"""
marker = interrupted_pull_marker(root)
marker.write_text(f"pid={os.getpid()}\npre={pre or ''}\ntarget={target}\nstash={stash or ''}\n"
diff --git a/tests/e2e/core/upgrade/git/test_hostile_commit.py b/tests/e2e/core/upgrade/git/test_hostile_commit.py
index 9a91b72a9e5..fe35846a537 100644
--- a/tests/e2e/core/upgrade/git/test_hostile_commit.py
+++ b/tests/e2e/core/upgrade/git/test_hostile_commit.py
@@ -166,7 +166,7 @@ def test_kill_during_branch_switch_is_restored(world):
parked = _head(sb)
_release(world, {"e2e_cp0_c.py": "C = 1\n"})
# CP0: `git checkout main` dies after rewriting one file of main's tree.
- _hostile_git(world, 'if [ "${@: -2:1}" = checkout ] && [ "${@: -1}" = main ]; then '
+ _hostile_git(world, 'if [ "${@: -1}" = main ] && case " $* " in *" checkout "*) true;; *) false;; esac; then '
'"$REAL" show main:e2e_cp0_a.py > e2e_cp0_a.py; kill -KILL $PPID; exit 137; fi')
killed = _update(sb)
_hostile_git(world, "")
@@ -279,8 +279,11 @@ def test_failed_upstream_sync_after_origin_pull_keeps_the_tail_owed(world):
I.publish_commit(upstream, root, "upstream: touches the locked dir", {"zzz_e2e_fork/mod.py": "V = 2\n"})
I.git("remote", "add", "upstream", str(upstream), cwd=sb.checkout)
flag = sb.checkout / ".git" / "e2e-upstream-ff-failed"
- # Kill the updater at its next branch check once the upstream merge has failed (and been settled).
- _hostile_git(world, f'case " $* " in *" merge --ff-only upstream/main "*) "$REAL" "$@"; rc=$?; '
+ # origin reads as a fork (the sandbox shim otherwise reports the official URL). Kill the updater at
+ # its next branch check once the upstream merge has failed (and been settled).
+ _hostile_git(world, 'case " $* " in *" remote get-url origin "*) echo https://github.com/e2e-fork/hermes-agent.git; '
+ 'exit 0;; esac\n'
+ f'case " $* " in *" merge --ff-only upstream/main "*) "$REAL" "$@"; rc=$?; '
f'[ $rc != 0 ] && touch "{flag}"; exit $rc;; esac\n'
f'if [ -e "{flag}" ]; then case " $* " in *" rev-parse --abbrev-ref HEAD "*) '
'kill -KILL $PPID; exit 137;; esac; fi')