The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(update): a failed CP0 branch switch says whether it was refused, torn or a missing branch
Tekniumbdb513c9f0cf
diff --git a/hermes_cli/update_cmd.py b/hermes_cli/update_cmd.pyindex 92498d69da0..644f68f1d2e 100644--- a/hermes_cli/update_cmd.py+++ b/hermes_cli/update_cmd.py@@ -1120,6 +1120,10 @@ def _pull_updates(return movement_baseline+#: ``args`` of the result ``_switch_branch_at_commit_point`` returns when the commit point refused.+_ARM_REFUSED = ["arm_commit_point"]++def _switch_branch_at_commit_point(git_cmd, branch, target_ref, *, pre, stash):"""CP0: switch the parked checkout to *branch* under the interrupted-pull marker and the armedobligations, so a kill mid-switch is restored by the next launch like a killed pull."""@@ -1143,7 +1147,7 @@ def _switch_branch_at_commit_point(git_cmd, branch, target_ref, *, pre, stash):# (CP1 retargets the obligation before its own move).refused = _commit.arm_commit_point(git_cmd, root, target, pre=pre, target=target, stash=stash)if refused: # nothing moved: the caller restores the autostash and exits- return subprocess.CompletedProcess(args, 1, "", refused)+ return subprocess.CompletedProcess(_ARM_REFUSED, 1, "", refused)try:result = _git_run(git_cmd, args)except KeyboardInterrupt:@@ -1176,6 +1180,35 @@ def _refuse_before_commit_point(git_cmd, target_ref, _windows_gateway_resume) ->sys.exit(1)+def _exit_after_failed_branch_switch(git_cmd, branch, track_result, auto_stash_ref, gw_input_fn) -> NoReturn:+ """CP0 did not land: say which of refused / torn / missing branch it was, then ``sys.exit(1)``.++ A torn switch keeps its marker for the next launch's restore, so the autostash stays parked:+ re-applying it onto a half-written tree would mix the user's edits into git's partial writes.+ """+ detail = (track_result.stderr or "").strip()+ if interrupted_pull_marker(_m().PROJECT_ROOT).is_file():+ print(f"✗ Switching to '{branch}' stopped part-way; the next `hermes` launch puts the checkout back.")+ if detail:+ print(f" {detail.splitlines()[0]}")+ if auto_stash_ref is not None:+ print(f" ℹ️ Local changes preserved in stash (ref: {auto_stash_ref})")+ print(" Restore manually with: git stash apply")+ _clear_pending_autostash()+ sys.exit(1)+ # Nothing moved: restore the stash before bailing so the user isn't stranded.+ if 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:+ print(f"✗ {detail}.")+ else:+ print(f"✗ Branch '{branch}' does not exist locally or on origin.")+ if detail:+ print(f" {detail.splitlines()[0]}")+ sys.exit(1)++@dataclassclass _CheckoutPlan:"""What the pre-pull checkout phase decided (see ``_prepare_checkout_for_update``)."""@@ -1270,14 +1303,7 @@ def _prepare_checkout_for_update(track_result = _switch_branch_at_commit_point(git_cmd, branch, target_ref, pre=moved_from_sha, stash=auto_stash_ref)if track_result.returncode != 0:- # Restore the stash before bailing so the user isn't stranded.- if 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)- print(f"✗ Branch '{branch}' does not exist locally or on origin.")- if track_result.stderr.strip():- print(f" {track_result.stderr.strip().splitlines()[0]}")- sys.exit(1)+ _exit_after_failed_branch_switch(git_cmd, branch, track_result, auto_stash_ref, gw_input_fn)prompt_for_restore = (auto_stash_ref is not Nonediff --git a/tests/hermes_cli/test_update_existing_branch_baseline.py b/tests/hermes_cli/test_update_existing_branch_baseline.pyindex ed0d4c97dd9..c6e7cd31f21 100644--- a/tests/hermes_cli/test_update_existing_branch_baseline.py+++ b/tests/hermes_cli/test_update_existing_branch_baseline.py@@ -276,3 +276,47 @@ def test_fork_sync_round_trip_is_not_misclassified_as_noop(tmp_path, monkeypatchassert git(root, "rev-parse", "HEAD") == upstream_tipassert git(root, "rev-parse", "main") == upstream_tipassert git(root, "rev-parse", "--abbrev-ref", "HEAD") == "main"+++def test_a_refused_branch_switch_prints_the_refusal_not_a_missing_branch(checkout, capsys):+ """CP0's commit point refused (no marker can be written): the user used to read "Branch 'main'+ does not exist locally or on origin." above the real reason (review C8)."""+ from hermes_cli import update_cmd_commit+ from hermes_cli._early_recovery import interrupted_pull_marker++ root, old, _tip = checkout+ update_cmd_commit.reset_for_tests()+ interrupted_pull_marker(root).mkdir()+ try:+ with pytest.raises(SystemExit):+ prepare(root)+ finally:+ update_cmd_commit.reset_for_tests()++ out = capsys.readouterr().out+ assert "could not arm the update" in out and "does not exist" not in out+ assert git(root, "rev-parse", "HEAD") == old+++def test_a_torn_branch_switch_leaves_the_autostash_parked(checkout, monkeypatch, capsys):+ """A switch that stopped part-way keeps its marker for the next launch's restore; re-applying the+ autostash onto that half-written tree mixed the user's edits into git's partial writes (C8)."""+ from hermes_cli._early_recovery import interrupted_pull_marker++ root, _old, _tip = checkout+ (root / "cli.py").write_text("value = 'local edit'\n", encoding="utf8")+ restored = []+ monkeypatch.setattr(update_cmd._m(), "_restore_stashed_changes", lambda *a, **k: restored.append(a))++ def torn_switch(git_cmd, branch, target_ref, *, pre, stash):+ interrupted_pull_marker(root).write_text(f"pid=1\npre={pre}\ntarget={pre}\nstash={stash}\n",+ encoding="utf-8")+ return subprocess.CompletedProcess(["checkout", branch], 1, "", "error: unable to write file cli.py")++ monkeypatch.setattr(update_cmd, "_switch_branch_at_commit_point", torn_switch)+ with pytest.raises(SystemExit):+ prepare(root)++ out = capsys.readouterr().out+ assert restored == [], "the autostash was re-applied onto a torn tree"+ assert "Local changes preserved in stash" in out and "does not exist" not in out