Skip to content

The agent that grows with you

fix(update): prove divergence before reset fallback

JoaoMarcos44f4b49df25f85
diff --git a/hermes_cli/update_cmd.py b/hermes_cli/update_cmd.py
index 5c60cc906dd..974f424259c 100644
--- a/hermes_cli/update_cmd.py
+++ b/hermes_cli/update_cmd.py
@@ -745,8 +745,12 @@ def _complete_source_update(request: dict | None) -> None:
def _reconcile_diverged_checkout(git_cmd, branch: str, pre_pull_sha, *, target_ref=None) -> None:
- """Fast-forward failed: merge on a custom branch (local commits survive) or reset --hard on the
- same branch after parking the old HEAD behind a rescue ref. ``sys.exit(1)`` on failure."""
+ """Reconcile a proven non-fast-forwardable checkout.
+
+ The caller must first establish that HEAD is not an ancestor of the update target; ordinary
+ fast-forward failures (locks, object transport errors) must never reach this destructive path.
+ Merge on a custom branch (local commits survive) or reset --hard on the same branch after
+ parking the old HEAD behind a rescue ref. ``sys.exit(1)`` on failure."""
# A custom branch (local commits atop origin/<branch>) also can't ff, and reset --hard
# would discard that work: merge instead, stop on conflict.
merge_ref = target_ref if target_ref is not None else f"origin/{branch}"
@@ -764,12 +768,10 @@ def _reconcile_diverged_checkout(git_cmd, branch: str, pre_pull_sha, *, target_r
print(" Then re-run the update. Local work is untouched.")
sys.exit(1)
return
- # Same branch: the reset below is right either way, but the two causes of divergence here
- # are indistinguishable from the checkout alone. An upstream force-push/rebase loses
- # nothing; local commits on this branch lose everything, and the reflog is the only way
- # back — an expiring log the user has to know to reach for, in a directory Hermes updates
- # unattended. So park pre_pull_sha behind a rescue ref for BOTH, orphan divergence (no
- # common ancestor: corrupted HEAD, re-init) included.
+ # Same branch and proven non-ancestor: an upstream force-push/rebase may lose nothing,
+ # while local commits on this branch lose everything. The reflog is only an expiring
+ # recovery path, so park pre_pull_sha behind a rescue ref for BOTH, orphan divergence
+ # (no common ancestor: corrupted HEAD, re-init) included.
merge_base_result = _git_run(git_cmd, ["merge-base", "HEAD", merge_ref])
has_common_ancestor = bool(
merge_base_result.returncode == 0 and merge_base_result.stdout.strip())
@@ -900,8 +902,28 @@ def _pull_updates(
# 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)
- elif _git_run(git_cmd, ["merge", "--ff-only", merge_ref]).returncode != 0:
- _reconcile_diverged_checkout(git_cmd, branch, pre_pull_sha, target_ref=merge_ref)
+ else:
+ merge_result = _git_run(git_cmd, ["merge", "--ff-only", merge_ref])
+ if merge_result.returncode != 0:
+ ancestry = _git_run(
+ git_cmd, ["merge-base", "--is-ancestor", "HEAD", merge_ref])
+ if ancestry.returncode == 1:
+ _reconcile_diverged_checkout(
+ git_cmd, branch, pre_pull_sha, target_ref=merge_ref)
+ else:
+ print("✗ Fast-forward failed; refusing to reset because history divergence was not proven.")
+ detail = (merge_result.stderr or merge_result.stdout or "").strip()
+ if detail:
+ print(f" {detail}")
+ if ancestry.returncode == 0:
+ print(f" HEAD is still an ancestor of {merge_ref}.")
+ else:
+ print(f" Could not verify whether HEAD is an ancestor of {merge_ref}.")
+ ancestry_detail = (ancestry.stderr or ancestry.stdout or "").strip()
+ if ancestry_detail:
+ print(f" {ancestry_detail}")
+ print(" Resolve the Git error and re-run `hermes update`; no reset was attempted.")
+ sys.exit(1)
except KeyboardInterrupt:
raise # Ctrl-C reached git too (same process group): the tree may be torn, keep the marker
except BaseException:
diff --git a/tests/hermes_cli/test_update_diverged_rescue_ref.py b/tests/hermes_cli/test_update_diverged_rescue_ref.py
index 27ffb406257..41b2b4010f5 100644
--- a/tests/hermes_cli/test_update_diverged_rescue_ref.py
+++ b/tests/hermes_cli/test_update_diverged_rescue_ref.py
@@ -81,3 +81,71 @@ def test_hermes_update_keeps_local_commit_behind_a_rescue_ref(
out = capsys.readouterr().out
_assert_reset_kept_local_commit(checkout, local_sha, out)
assert "1 commit(s) not on origin/main" in out
+
+
+def fast_forward_checkout(tmp_path):
+ """A checkout whose HEAD is strictly behind origin/main."""
+ upstream = tmp_path / "linear-upstream"
+ upstream.mkdir()
+ _git(upstream, "init", "-q", "-b", "main")
+ first = _commit(upstream, "shared.txt", "shared\n")
+ _commit(upstream, "upstream-only.txt", "upstream\n")
+
+ checkout = tmp_path / "linear-checkout"
+ _git(tmp_path, "clone", "-q", str(upstream), str(checkout))
+ _git(checkout, "reset", "-q", "--hard", first)
+ return checkout, first
+
+
+def test_live_index_lock_is_reported_without_false_divergence(
+ fast_forward_checkout, monkeypatch, capsys):
+ checkout, before = fast_forward_checkout
+ monkeypatch.setattr(update_cmd._m(), "PROJECT_ROOT", checkout)
+ (checkout / ".git" / "index.lock").touch()
+
+ with pytest.raises(SystemExit) as exc:
+ update_cmd._pull_updates(
+ GIT, "main", None, prompt_for_restore=False, gw_input_fn=None,
+ discard_local_changes=False, keep_stash=False)
+
+ assert exc.value.code == 1
+ assert _git(checkout, "rev-parse", "HEAD").stdout.strip() == before
+ assert _rescue_refs(checkout) == {}
+ out = capsys.readouterr().out
+ assert "index.lock" in out
+ assert "HEAD is still an ancestor of origin/main" in out
+ assert "Local history has diverged" not in out
+ assert "Fast-forward not possible (history diverged)" not in out
+ assert "git reset --hard" not in out
+
+
+def test_operational_ff_failure_preserves_git_error_without_reset(
+ fast_forward_checkout, monkeypatch, capsys):
+ checkout, before = fast_forward_checkout
+ monkeypatch.setattr(update_cmd._m(), "PROJECT_ROOT", checkout)
+ real_git_run = update_cmd._git_run
+
+ def fail_merge(git_cmd, args, *rest, **kwargs):
+ if args[:2] == ["merge", "--ff-only"]:
+ return subprocess.CompletedProcess(
+ git_cmd + args, 128, stdout="",
+ stderr="fatal: unable to read tree: RPC failed; transfer closed")
+ return real_git_run(git_cmd, args, *rest, **kwargs)
+
+ monkeypatch.setattr(update_cmd, "_git_run", fail_merge)
+
+ with pytest.raises(SystemExit) as exc:
+ update_cmd._pull_updates(
+ GIT, "main", None, prompt_for_restore=False, gw_input_fn=None,
+ discard_local_changes=False, keep_stash=False)
+
+ assert exc.value.code == 1
+ assert _git(checkout, "rev-parse", "HEAD").stdout.strip() == before
+ assert _rescue_refs(checkout) == {}
+ out = capsys.readouterr().out
+ assert "RPC failed; transfer closed" in out
+ assert "HEAD is still an ancestor of origin/main" in out
+ assert "Local history has diverged" not in out
+ assert "git reset --hard" not in out
+