Skip to content

The agent that grows with you

fix(update): preserve pre-sync update baseline

Alan Harman-Boxcf4207eb11fd
diff --git a/contributors/emails/[email protected] b/contributors/emails/[email protected]
new file mode 100644
index 00000000000..fd7447fdc2f
--- /dev/null
+++ b/contributors/emails/[email protected]
@@ -0,0 +1,2 @@
+AlanBurningsuit
+# PR #95043
diff --git a/hermes_cli/update_cmd.py b/hermes_cli/update_cmd.py
index 0d1800ed961..60979931665 100644
--- a/hermes_cli/update_cmd.py
+++ b/hermes_cli/update_cmd.py
@@ -916,7 +916,7 @@ def _rollback_if_pulled_syntax_error(git_cmd, pre_pull_sha) -> None:
def _pull_updates(
git_cmd, branch, auto_stash_ref, *, prompt_for_restore, gw_input_fn, discard_local_changes,
- keep_stash, target_ref=None):
+ keep_stash, target_ref=None, pre_sync_sha=None):
"""Fast-forward onto ``origin/<branch>`` and settle the autostash. Divergence by shape:
custom branch -> merge, same branch -> reset, orphan history -> rescue ref first; a
post-pull syntax error in a critical file rolls back. Exits on failure; returns pre-pull SHA."""
@@ -938,7 +938,7 @@ def _pull_updates(
_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)
- _rollback_if_pulled_syntax_error(git_cmd, pre_pull_sha)
+ _rollback_if_pulled_syntax_error(git_cmd, pre_sync_sha or pre_pull_sha)
update_succeeded = True
finally:
if auto_stash_ref is not None:
@@ -970,6 +970,7 @@ class _CheckoutPlan:
prompt_for_restore: bool
switch_block_reason: "str | None"
upstream_checked: bool
+ pre_sync_sha: str | None = None
def _apply_parked_branch_guard(
@@ -1087,6 +1088,7 @@ def _prepare_checkout_for_update(
# "Already up to date!" and verified nothing). Non-fork checkouts have no upstream question: origin IS
# the official repo, so "Already up to date!" is fully verified there.
upstream_checked = True
+ moved_from_sha = None
if commit_count == 0 and is_fork and branch == "main" and not release_tag:
pre_sync_sha = _capture_head_sha(git_cmd, _m().PROJECT_ROOT)
upstream_checked = _m()._sync_with_upstream_if_needed(
@@ -1097,11 +1099,13 @@ def _prepare_checkout_for_update(
git_cmd, _m().PROJECT_ROOT, pre_sync_sha, post_sync_sha)
# HEAD moving is proof of an update even if the count can't be read.
commit_count = max(1, synced_count)
+ moved_from_sha = pre_sync_sha
return _CheckoutPlan(
auto_stash_ref=auto_stash_ref, commit_count=commit_count, in_place_update=in_place_update,
parked_branch_switched=parked_branch_switched, prompt_for_restore=prompt_for_restore,
- switch_block_reason=switch_block_reason, upstream_checked=upstream_checked)
+ switch_block_reason=switch_block_reason, upstream_checked=upstream_checked,
+ pre_sync_sha=moved_from_sha)
@dataclass
@@ -1328,7 +1332,7 @@ def _apply_pulled_update(
_windows_gateway_resume, completion_request: dict) -> None:
"""Post-pull phase: verify HEAD, sync Python/Node/web/Desktop, maintenance, fleet restart."""
post_pull_sha = _verify_head_after_pull(
- git_cmd, branch, pre_pull_sha, in_place_update=_plan.in_place_update,
+ git_cmd, branch, _plan.pre_sync_sha or pre_pull_sha, in_place_update=_plan.in_place_update,
_windows_gateway_resume=_windows_gateway_resume)
if is_fork and branch == "main":
@@ -1483,7 +1487,7 @@ def _cmd_update_impl(args, gateway_mode: bool):
pre_pull_sha = _pull_updates(
git_cmd, branch, _plan.auto_stash_ref, prompt_for_restore=_plan.prompt_for_restore,
gw_input_fn=gw_input_fn, discard_local_changes=opts.discard_local_changes,
- keep_stash=opts.keep_stash, target_ref=target_ref)
+ keep_stash=opts.keep_stash, target_ref=target_ref, pre_sync_sha=_plan.pre_sync_sha)
_apply_pulled_update(
git_cmd, branch, pre_pull_sha, _plan, opts, is_fork=is_fork and not release_tag,
_windows_gateway_resume=_windows_gateway_resume, completion_request=completion_request)
diff --git a/tests/hermes_cli/test_update_completion_routing.py b/tests/hermes_cli/test_update_completion_routing.py
index bf52e0cf158..22f26005139 100644
--- a/tests/hermes_cli/test_update_completion_routing.py
+++ b/tests/hermes_cli/test_update_completion_routing.py
@@ -69,8 +69,9 @@ def test_every_route_hands_off_once(route, tmp_path, monkeypatch):
monkeypatch.setattr(update_cmd_zip, "_abort_zip_update_if_dirty_tree", lambda: None)
swap = Mock()
monkeypatch.setattr(update_cmd_zip, "_download_and_swap_zip", swap)
- plan = SimpleNamespace(in_place_update=False, auto_stash_ref=None, parked_branch_switched=False,
- upstream_checked=True)
+ plan = update_cmd._CheckoutPlan(
+ in_place_update=False, auto_stash_ref=None, parked_branch_switched=False,
+ upstream_checked=True, commit_count=1, prompt_for_restore=False, switch_block_reason=None)
opts = SimpleNamespace(assume_yes=True, gw_input_fn=None, pre_update_version="old")
if route == "pulled":
update_cmd._apply_pulled_update(
diff --git a/tests/hermes_cli/test_update_target_identity.py b/tests/hermes_cli/test_update_target_identity.py
index 1047f0bad8d..575b7b047eb 100644
--- a/tests/hermes_cli/test_update_target_identity.py
+++ b/tests/hermes_cli/test_update_target_identity.py
@@ -88,7 +88,8 @@ def update_tree(tmp_path, monkeypatch):
@pytest.mark.parametrize('case', ['main', 'explicit', 'missing', 'no-move', 'wrong-branch',
- 'fork-no-upstream', 'fork-upstream', 'check-main',
+ 'fork-no-upstream', 'fork-upstream', 'fork-upstream-push-ok',
+ 'fork-upstream-wrong-branch', 'fork-upstream-reverted', 'check-main',
'check-explicit', 'check-missing', 'check-upstream'])
def test_branch_update_uses_real_refs_and_completion_request(update_tree, monkeypatch, case, capsys):
t = update_tree
@@ -105,11 +106,13 @@ def test_branch_update_uses_real_refs_and_completion_request(update_tree, monkey
if case.startswith('fork-') or case == 'check-upstream':
# Origin is current; only upstream has the next commit.
git(t.origin, 'reset', '--hard', t.base)
- if case in {'fork-upstream', 'check-upstream'}:
+ if case.startswith('fork-upstream') or case == 'check-upstream':
upstream = t.origin.parent / 'upstream'
git(t.origin.parent, 'clone', '-q', str(t.origin), str(upstream))
git(upstream, 'reset', '--hard', t.newer)
git(t.clone, 'remote', 'add', 'upstream', str(upstream))
+ if case == 'fork-upstream-push-ok':
+ git(t.origin, 'config', 'receive.denyCurrentBranch', 'updateInstead')
run = subprocess.run
def fault(command, *args, **kwargs):
@@ -117,21 +120,16 @@ def test_branch_update_uses_real_refs_and_completion_request(update_tree, monkey
if case == 'no-move':
return subprocess.CompletedProcess(command, 0, stdout='', stderr='')
result = run(command, *args, **kwargs)
- if case == 'wrong-branch':
+ if case in {'wrong-branch', 'fork-upstream-wrong-branch'}:
run(['git', 'checkout', '-qb', 'wrong'], cwd=t.clone, check=True, capture_output=True)
+ if case == 'fork-upstream-reverted':
+ run(['git', 'reset', '--hard', t.base], cwd=t.clone, check=True, capture_output=True)
return result
return run(command, *args, **kwargs)
monkeypatch.setattr(subprocess, 'run', fault)
- if case == 'fork-upstream':
- # The checkout policy must consult upstream even when origin is current.
- plan = update_cmd._prepare_checkout_for_update(
- ['git'], 'main', 'main', is_fork=True, assume_yes=True, gateway_mode=False,
- gw_input_fn=None, switch_branch=False, _windows_gateway_resume=None)
- assert plan.commit_count > 0 and plan.upstream_checked
- assert git(t.clone, 'rev-parse', 'HEAD') == t.newer
- return
- fails = case in {'missing', 'check-missing', 'no-move', 'wrong-branch'}
+ fails = case in {'missing', 'check-missing', 'no-move', 'wrong-branch',
+ 'fork-upstream-wrong-branch', 'fork-upstream-reverted'}
if fails:
with pytest.raises(SystemExit) as error:
cli_main.cmd_update(t.args)
@@ -153,6 +151,12 @@ def test_branch_update_uses_real_refs_and_completion_request(update_tree, monkey
expected = t.base if case == 'fork-no-upstream' else t.wanted if case == 'explicit' else t.newer
assert git(t.clone, 'rev-parse', 'HEAD') == expected
assert request['source'] == str(t.clone)
+ if case.startswith('fork-upstream'):
+ assert request['expected_sha'] == expected
+ output = capsys.readouterr().out
+ assert 'Code did not move' not in output
+ assert 'Already up to date' not in output
+ assert git(t.origin, 'rev-parse', 'HEAD') == (t.newer if case.endswith('push-ok') else t.base)
if case == 'fork-no-upstream':
assert 'official repo not checked' in request['completion_message']
assert git(t.clone, 'remote') == 'origin'
@@ -351,3 +355,32 @@ def test_stable_zip_consumes_the_same_commit_through_the_real_swap(update_tree,
server.shutdown()
thread.join(timeout=5)
server.server_close()
+
+
[email protected]('sync_first', [False, True])
+def test_update_syntax_failure_restores_pre_update_head(update_tree, monkeypatch, capsys, sync_first):
+ t = update_tree
+ git(t.clone, 'checkout', '-q', 'main')
+ t.args.channel = 'main'
+ monkeypatch.setattr(cli_main, '_sync_with_upstream_if_needed', _sync_with_upstream_if_needed)
+ if sync_first:
+ upstream = t.origin.parent / 'upstream'
+ git(t.origin.parent, 'clone', '-q', str(t.origin), str(upstream))
+ git(t.clone, 'remote', 'add', 'upstream', str(upstream))
+ git(t.origin, 'reset', '--hard', t.base)
+ remote = upstream
+ else:
+ remote = t.origin
+ bad = remote / 'hermes_cli' / 'config.py'
+ bad.parent.mkdir()
+ bad.write_text('def broken(:\n', encoding='utf-8')
+ git(remote, 'add', 'hermes_cli/config.py')
+ git(remote, '-c', 'user.name=Fixture', '-c', '[email protected]',
+ '-c', 'commit.gpgsign=false', 'commit', '-qm', 'invalid syntax')
+ with pytest.raises(SystemExit) as error:
+ cli_main.cmd_update(t.args)
+ assert error.value.code == 1
+ assert not t.requests
+ assert 'Pulled code has a syntax error' in capsys.readouterr().out
+ assert git(t.clone, 'rev-parse', 'HEAD') == t.base
+ assert not (t.clone / 'hermes_cli' / 'config.py').exists()