Skip to content

The agent that grows with you

fix(update): an autostash the update could not restore ends the update partial, never 'Update complete!'

teknium152d814cfd04c
diff --git a/hermes_cli/update_cmd.py b/hermes_cli/update_cmd.py
index 6dd2f37737a..a11ce49ac10 100644
--- a/hermes_cli/update_cmd.py
+++ b/hermes_cli/update_cmd.py
@@ -75,7 +75,7 @@ from hermes_cli.update_cmd_stash import ( # noqa: F401
_git_untracked_paths, _park_stashed_changes, _print_stash_cleanup_guidance,
_reject_unsafe_stash_restore, _resolve_stash_selector, _restore_stashed_changes,
_restored_python_paths, _stash_apply_failed_only_on_existing_untracked,
- _stash_local_changes_if_needed, _surface_pending_autostash,
+ _stash_local_changes_if_needed, _unrestored_autostash_notice,
_warn_orphaned_update_autostashes)
from hermes_cli.update_cmd_config import ( # noqa: F401
_LAST_SIBLING_SNAPSHOTS, _check_and_apply_config_migration, _migrate_sibling_profile_configs,
@@ -720,12 +720,14 @@ def _source_completion_request(opts, plan, snapshot_id, windows_resume, desktop,
def _complete_source_update(request: dict | None) -> None:
- # Success boundary: completion must never be reported while this run's local
- # patches sit unrestored and unnamed in the stash (#122557). No-op once the
- # stash was restored, parked or discarded.
- _surface_pending_autostash()
+ # Never "Update complete!" while this run's local patches sit unrestored in the stash (#122557).
+ unrestored = _unrestored_autostash_notice()
if request is None:
+ if unrestored:
+ print(unrestored)
stop_for_relaunch(incomplete=True)
+ if unrestored:
+ request["completion_message"] = unrestored
from copy import deepcopy
current = _completion_receipt._current.get()
if current is not None:
@@ -1307,13 +1309,9 @@ def _current_branch_name(git_cmd, *, check: bool = False) -> str:
def _handle_update_called_process_error(
e, args, gateway_mode: bool, had_desktop_app_before_update: bool,
*, target_sha: str | None = None, target_repository: str | None = None, completion_request=None) -> None:
- """Git/installer failure: ZIP-fallback when safe, else report and ``sys.exit(1)``.
-
- A stash created this run that no settle step touched is named first (#122557):
- neither verdict below may leave the patches invisible in ``git stash list``, and
- the ZIP fallback in particular goes on to report success.
- """
- _surface_pending_autostash()
+ """Git/installer failure: ZIP-fallback when safe, else report and ``sys.exit(1)``."""
+ if unrestored := _unrestored_autostash_notice():
+ print(unrestored) # a stash taken this run that no settle step reported (#122557)
stage = _format_update_failure_stage(e)
if _should_zip_fallback_on_update_error(e):
print(f"⚠ {stage}: {e}")
diff --git a/hermes_cli/update_cmd_stash.py b/hermes_cli/update_cmd_stash.py
index c1225dbe570..ae55fbfa1b8 100644
--- a/hermes_cli/update_cmd_stash.py
+++ b/hermes_cli/update_cmd_stash.py
@@ -26,12 +26,10 @@ _AUTOSTASH_WARN_AGE_DAYS = 7
_STASH_LEFT_IN_PLACE = " The stash was left in place. You can remove it manually after checking the result."
-#: This run's autostash while still unsettled: (stash ref, stashed path count).
-#: Set the moment the update stashes local patches; cleared as soon as they are
-#: restored, parked or discarded (_record_stash_disposition), when the failed-pull
-#: branch has already reported them, or when _surface_pending_autostash names them.
-#: Any update outcome reported while this is still set would claim the patches were
-#: handled when they were not (#122557).
+#: This run's autostash until it is settled: (stash ref, stashed path count). Set when the
+#: update stashes local patches; cleared when they are restored, discarded or parked because
+#: the user asked for it, or once a failure verdict already named them. While it is set, no
+#: outcome may claim the update completed (#122557).
_pending_autostash: Optional[tuple[str, int]] = None
@@ -84,6 +82,7 @@ def _print_first_line(text: str) -> None:
def _stash_local_changes_if_needed(git_cmd: list[str], cwd: Path) -> Optional[str]:
global _pending_autostash
from hermes_cli.update_cmd_git import _git_run
+ _pending_autostash = None
status = _git_run(git_cmd, ["status", "--porcelain", "-z"], cwd, check=True)
if not status.stdout.strip():
return None
@@ -155,27 +154,19 @@ def _clear_pending_autostash() -> None:
_pending_autostash = None
-def _surface_pending_autostash() -> bool:
- """Loudly name this run's unsettled autostash; True when something was printed (#122557).
+def _unrestored_autostash_notice() -> Optional[str]:
+ """What to tell the user while this run's autostash is unsettled (#122557), else ``None``.
- Called by every update outcome boundary (failure verdicts and the completion request)
- so local patches parked mid-run can never be invisible in ``git stash list`` while the
- command claims success or reports a bare failure. Deliberately separate from
- ``_warn_orphaned_update_autostashes``, which surfaces leftovers from EARLIER runs by age.
+ Used as the completion line (it is not a success line, so the update ends partial and
+ exits non-zero) and printed by failure verdicts, which otherwise never name the stash.
"""
- global _pending_autostash
- pending, _pending_autostash = _pending_autostash, None
- if pending is None:
- return False
- stash_ref, file_count = pending
- print()
- print(f"! hermes update stashed {file_count} local modification(s) and could not restore them.")
- print(" The install may be functional, but your local patches are NOT applied.")
- print(f" Stash ref: {stash_ref}")
- print(f" Review with: git stash show --stat {stash_ref}")
- print(f" Re-apply with: git stash show -p {stash_ref} | git apply --3way")
- print(f" Discard with: git stash drop {stash_ref}")
- return True
+ if _pending_autostash is None:
+ return None
+ stash_ref, file_count = _pending_autostash
+ return (f"⚠ hermes update stashed {file_count} local modification(s) and did NOT restore them.\n"
+ f" Stash ref: {stash_ref}\n"
+ f" Review with: git stash show --stat {stash_ref}\n"
+ f" Re-apply with: git stash show -p {stash_ref} | git apply --3way")
def _warn_orphaned_update_autostashes(git_cmd: list[str], cwd: Path) -> int:
@@ -227,12 +218,14 @@ def _warn_orphaned_update_autostashes(git_cmd: list[str], cwd: Path) -> int:
return 0
-def _record_stash_disposition(outcome: str, stash_ref: str, detail: str = "") -> None:
+def _record_stash_disposition(outcome: str, stash_ref: str, detail: str = "", *, chosen: bool = False) -> None:
"""Note the autostash disposition in the update receipt so a parked stash is visible
to automation reading receipts instead of stdout (#115363: an update that ended with
- local changes parked in the stash reported a bare success with no trace of them)."""
+ local changes parked in the stash reported a bare success with no trace of them).
+ A park the user did not ask for (``chosen``) keeps the stash unsettled (#122557)."""
global _pending_autostash
- _pending_autostash = None # this run's stash was reported: restored, parked or discarded
+ if outcome != "parked" or chosen:
+ _pending_autostash = None
from hermes_cli.update_receipt import record_step
record_step(
"local_changes_stash",
@@ -300,7 +293,7 @@ def _park_stashed_changes(stash_ref: str) -> None:
print("ℹ️ Local changes were stashed before updating and were NOT re-applied (--keep-stash).")
print(f" Stash ref: {stash_ref}")
print(f" Restore manually with: git stash apply {stash_ref}")
- _record_stash_disposition("parked", stash_ref, "--keep-stash")
+ _record_stash_disposition("parked", stash_ref, "--keep-stash", chosen=True)
def _git_untracked_paths(git_cmd: list[str], cwd: Path) -> set[str] | None:
@@ -357,7 +350,7 @@ def _reject_unsafe_stash_restore(
print(f" Your local changes remain preserved in stash: {stash_ref}")
print(f" Inspect them with: git stash show --stat {stash_ref}")
print(f" Restore manually after fixing them: git stash apply {stash_ref}")
- _clear_pending_autostash() # reported loudly right above; not "silent" anymore
+ _clear_pending_autostash() # named right above, and the update fails
raise SystemExit(1)
@@ -441,7 +434,7 @@ def _restore_stashed_changes(
) -> bool:
from hermes_cli.update_cmd import _critical_module_import_failures, _git_untracked_paths, _restored_python_paths, _validate_python_files_syntax
if prompt_user and not _confirm_restore(stash_ref, input_fn):
- _record_stash_disposition("parked", stash_ref, "restore declined")
+ _record_stash_disposition("parked", stash_ref, "restore declined", chosen=True)
return False
preexisting_untracked = _git_untracked_paths(git_cmd, cwd)
if preexisting_untracked is None:
diff --git a/tests/hermes_cli/test_update_autostash.py b/tests/hermes_cli/test_update_autostash.py
index 4da91d14bc3..11a5bfac562 100644
--- a/tests/hermes_cli/test_update_autostash.py
+++ b/tests/hermes_cli/test_update_autostash.py
@@ -643,3 +643,33 @@ def test_untracked_file_the_update_does_not_track_is_never_reported_replaced(tmp
assert (tmp_path / "mod.py").read_text(encoding="utf-8") == "X = 2\n"
assert "The update added" not in capsys.readouterr().out
+
+
+def test_unrequested_park_is_never_reported_as_update_complete(monkeypatch, tmp_path):
+ """A restore that conflicts leaves the patch parked: the completion line must say so (#122557);
+ a park the user asked for (--keep-stash) stays a normal completion."""
+ import hermes_cli.update_cmd_stash as stash_mod
+
+ monkeypatch.setattr(stash_mod, "_pending_autostash", None)
+
+ def git(*args):
+ return subprocess.run(["git", *args], cwd=tmp_path, capture_output=True, text=True, check=True)
+
+ git("init", "-q", "-b", "main")
+ git("config", "user.email", "[email protected]")
+ git("config", "user.name", "t")
+ source = tmp_path / "notes.txt"
+ source.write_text("VALUE = 1\n", encoding="utf-8")
+ git("add", "-A")
+ git("commit", "-qm", "init")
+ source.write_text("VALUE = 2\n", encoding="utf-8")
+ stash_ref = hermes_main._stash_local_changes_if_needed(["git"], tmp_path)
+ source.write_text("VALUE = 3\n", encoding="utf-8")
+ git("commit", "-qam", "pulled change")
+
+ assert hermes_main._restore_stashed_changes(["git"], tmp_path, stash_ref, prompt_user=False) is False
+ notice = stash_mod._unrestored_autostash_notice()
+ assert notice and not notice.startswith("\u2713") and stash_ref in notice
+
+ stash_mod._park_stashed_changes(stash_ref)
+ assert stash_mod._unrestored_autostash_notice() is None