The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(update): every tree move names the armed commit and verifies HEAD before its marker goes (review O2, O3)
Teknium803f83c7f1eb
diff --git a/hermes_cli/update_cmd.py b/hermes_cli/update_cmd.pyindex a04580b3b23..61ffef103f3 100644--- a/hermes_cli/update_cmd.py+++ b/hermes_cli/update_cmd.py@@ -991,6 +991,26 @@ def _rollback_if_pulled_syntax_error(git_cmd, pre_pull_sha, *, rollback_branch=Nsys.exit(1)+def _landed_off_target(git_cmd, target_sha: str, *, merged: bool = True) -> str | None:+ """The HEAD a git move landed on when it is neither ``target_sha`` nor (``merged``: a custom+ branch's merge) a commit containing it, else None: the identity the marker and debt were armed for."""+ landed = (_git_run(git_cmd, ["rev-parse", "-q", "--verify", "HEAD"]).stdout or "").strip()+ if landed == target_sha or (merged and landed and _git_run(+ git_cmd, ["merge-base", "--is-ancestor", target_sha, landed]).returncode == 0):+ return None+ return landed or "an unreadable HEAD"+++def _refuse_unselected_head(git_cmd, target_sha: str, unmoved: str | None) -> None:+ """``sys.exit(1)`` (the caller settles it as a failed move, its marker still on disk) when git+ landed on a commit other than the armed one (review O2/O3). A HEAD still on ``unmoved`` is+ ``_verify_head_after_pull``'s "code did not move" refusal."""+ if (landed := _landed_off_target(git_cmd, target_sha)) not in (None, unmoved):+ print(f"✗ The checkout landed on {landed[:10]}, not the selected commit {target_sha[:10]} "+ "(a ref moved during the update).")+ sys.exit(1)++def _update_movement_baseline(git_cmd, pre_pull_sha, pre_sync_sha, rollback_branch, target_sha):"""Distinguish a branch awaiting repair from an already-applied early sync."""if rollback_branch is not None and pre_pull_sha and target_sha:@@ -1043,21 +1063,22 @@ def _pull_updates(print(f"✗ {refused}.")sys.exit(1)try:- # merge --ff-only the already-fetched ref instead of `git pull`, which would do a- # SECOND network fetch; identical in effect given the fresh tracking ref.+ # merge --ff-only the already-fetched commit instead of `git pull`, which would do a+ # SECOND network fetch. Every move names ``target_sha``, never the ref: a ref that moves+ # after the arm cannot land a commit the marker and the debt do not name (review O3).if merge_ref != f"origin/{branch}":# Keep detached local commits reachable, too. Named branches are# untouched by checkout --detach; an autostash protects dirty files._park_detached_head(git_cmd, _m().PROJECT_ROOT, branch)- _git_run(git_cmd, ["checkout", "--detach", merge_ref], check=True)+ _git_run(git_cmd, ["checkout", "--detach", target_sha], check=True)else:- merge_result = _git_run(git_cmd, ["merge", "--ff-only", merge_ref])+ merge_result = _git_run(git_cmd, ["merge", "--ff-only", target_sha])if merge_result.returncode != 0:ancestry = _git_run(- git_cmd, ["merge-base", "--is-ancestor", "HEAD", merge_ref])+ git_cmd, ["merge-base", "--is-ancestor", "HEAD", target_sha])if ancestry.returncode == 1:_reconcile_diverged_checkout(- git_cmd, branch, pre_pull_sha, target_ref=merge_ref)+ git_cmd, branch, pre_pull_sha, target_ref=target_sha)else:print("✗ Fast-forward failed; refusing to reset because history divergence was not proven.")detail = (merge_result.stderr or merge_result.stdout or "").strip()@@ -1072,6 +1093,7 @@ def _pull_updates(print(f" {ancestry_detail}")print(" Resolve the Git error and re-run `hermes update`; no reset was attempted.")sys.exit(1)+ _refuse_unselected_head(git_cmd, target_sha, pre_pull_sha) # before the marker goesexcept KeyboardInterrupt:raise # Ctrl-C reached git too (same process group): the tree may be torn, keep the markerexcept BaseException:@@ -1125,6 +1147,8 @@ def _pull_updates(#: ``args`` of the result ``_switch_branch_at_commit_point`` returns when the commit point refused._ARM_REFUSED = ["arm_commit_point"]+#: ... and when the branch ref moved between its resolution and the checkout (review O2).+_REF_MOVED = ["ref_moved"]def _switch_branch_at_commit_point(git_cmd, branch, target_ref, *, pre, stash):@@ -1137,8 +1161,11 @@ def _switch_branch_at_commit_point(git_cmd, branch, target_ref, *, pre, stash):# --no-guess: with no local *branch* git would otherwise create it from origin/<branch> and# rewrite the tree while this loop believed the checkout could not move.- attempts = [(["checkout", "--no-guess", branch], resolve(f"refs/heads/{branch}")),- (["checkout", "-B", branch, f"origin/{branch}"], resolve(f"origin/{branch}"))]+ # The -B form names the resolved commit, never the ref (review O2); the local-branch form must+ # name the branch to attach HEAD to it, so its landing is verified against the armed commit.+ local, remote = resolve(f"refs/heads/{branch}"), resolve(f"origin/{branch}")+ attempts = [(["checkout", "--no-guess", branch], local),+ (["checkout", "-B", branch, remote or f"origin/{branch}"], remote)]result = Nonefor args, target in attempts:if not target:@@ -1161,6 +1188,16 @@ def _switch_branch_at_commit_point(git_cmd, branch, target_ref, *, pre, stash):else:_commit.owe_restore_of(root, pre)raise+ if result.returncode == 0 and (landed := _landed_off_target(git_cmd, target, merged=False)) is not None:+ # The branch moved between its resolution and the checkout: the tree is whole at a commit+ # the debt does not name. Settled like a failed move: the debt follows the landed HEAD.+ print(f" ✗ The branch moved to {landed[:10]} after {target[:10]} was selected.")+ if _commit.settle_failed_tree_move(root):+ _commit.disarm_commit_obligations() # off the run's start: re-owed for the landed HEAD+ else:+ _commit.owe_restore_of(root, pre)+ return subprocess.CompletedProcess(_REF_MOVED, 1, result.stdout, (+ f"'{branch}' moved to {landed[:10]} during the switch; re-run `hermes update`"))if result.returncode == 0:interrupted_pull_marker(root).unlink(missing_ok=True) # git exited 0: whole at *target*return result@@ -1203,7 +1240,7 @@ def _exit_after_failed_branch_switch(git_cmd, branch, track_result, auto_stash_rif auto_stash_ref is not None:_m()._restore_stashed_changes(git_cmd, _m().PROJECT_ROOT, auto_stash_ref, prompt_user=False, input_fn=gw_input_fn)- if track_result.args == _ARM_REFUSED:+ if track_result.args in (_ARM_REFUSED, _REF_MOVED):print(f"✗ {detail}.")else:print(f"✗ Branch '{branch}' does not exist locally or on origin.")diff --git a/tests/hermes_cli/test_update_commit_obligations.py b/tests/hermes_cli/test_update_commit_obligations.pyindex 39cba70ffc3..c0434bc38be 100644--- a/tests/hermes_cli/test_update_commit_obligations.py+++ b/tests/hermes_cli/test_update_commit_obligations.py@@ -194,3 +194,80 @@ def test_one_installs_disarm_never_deletes_another_installs_debt_for_the_same_sh_enter_run(run_b)commit.disarm_commit_obligations()assert not host_obligation_path().exists() # the last owner puts back what the FIRST one found+++def _abc_clone(tmp_path, monkeypatch):+ """Upstream commits A, B, C; a clone of it with PROJECT_ROOT pointed at it. Returns (clone, a, b, c)."""+ import hermes_cli.main as hermes_main++ up, clone = tmp_path / "up", tmp_path / "clone"+ up.mkdir()+ _git(up, "init", "-q", "-b", "main")+ shas = []+ for name in "ABC":+ (up / "f").write_text(name, encoding="utf-8")+ _git(up, "add", "-A")+ _git(up, "commit", "-qm", name)+ shas.append(_git(up, "rev-parse", "HEAD"))+ _git(tmp_path, "clone", "-q", str(up), str(clone))+ monkeypatch.setattr(hermes_main, "PROJECT_ROOT", clone)+ monkeypatch.setattr(commit, "_owns_live_checkout", lambda _root: False)+ monkeypatch.chdir(clone)+ return (clone, *shas)+++def _move_ref_after_arm(monkeypatch, clone, ref, sha):+ """The race: ``ref`` moves to ``sha`` right after the commit point armed (marker + debt)."""+ real = commit.arm_commit_point++ def arm_then_move(*args, **kwargs):+ refused = real(*args, **kwargs)+ _git(clone, "update-ref", ref, sha)+ return refused++ monkeypatch.setattr(commit, "arm_commit_point", arm_then_move)++[email protected](shutil.which("git") is None, reason="needs git")+def test_the_pull_merges_the_armed_commit_not_a_tracking_ref_that_moved(tmp_path, monkeypatch):+ """CP1 armed B, then origin/main moved to C before ``merge --ff-only origin/main``: HEAD landed C+ while the debt named B and the marker was gone (review O3). The merge names the armed OID."""+ from hermes_cli import update_cmd+ from hermes_cli._early_recovery import interrupted_pull_marker+ from hermes_cli.update_host_obligation import read_host_obligation++ clone, a, b, c = _abc_clone(tmp_path, monkeypatch)+ _git(clone, "reset", "-q", "--hard", a)+ _git(clone, "update-ref", "refs/remotes/origin/main", b)+ commit.record_run_start(["git"], clone)+ _move_ref_after_arm(monkeypatch, clone, "refs/remotes/origin/main", c)+ update_cmd._pull_updates(["git"], "main", None, prompt_for_restore=False, gw_input_fn=None,+ discard_local_changes=False, keep_stash=False)+ assert _git(clone, "rev-parse", "HEAD") == b+ assert (read_host_obligation() or {}).get("expected_sha") == b+ assert not interrupted_pull_marker(clone).exists()++[email protected](shutil.which("git") is None, reason="needs git")+def test_a_branch_that_moves_during_the_switch_leaves_head_and_debt_agreeing(tmp_path, monkeypatch):+ """CP0 resolved main to A and armed A, then main moved to B before ``checkout main``: the switch+ landed B, reported success and dropped the marker while the debt named A (review O2). It must+ refuse, with the debt following the HEAD it landed on."""+ from hermes_cli import update_cmd+ from hermes_cli._early_recovery import interrupted_pull_marker+ from hermes_cli.update_host_obligation import read_host_obligation++ clone, a, b, _c = _abc_clone(tmp_path, monkeypatch)+ _git(clone, "reset", "-q", "--hard", a)+ _git(clone, "checkout", "-q", "-b", "feat")+ (clone / "g").write_text("X", encoding="utf-8")+ _git(clone, "add", "-A")+ _git(clone, "commit", "-qm", "X")+ x = _git(clone, "rev-parse", "HEAD")+ commit.record_run_start(["git"], clone)+ _move_ref_after_arm(monkeypatch, clone, "refs/heads/main", b)+ switched = update_cmd._switch_branch_at_commit_point(["git"], "main", "origin/main", pre=x, stash=None)+ assert switched.returncode == 1 and "moved" in switched.stderr+ assert _git(clone, "rev-parse", "HEAD") == b+ assert (read_host_obligation() or {}).get("expected_sha") == b+ assert not interrupted_pull_marker(clone).exists()