Skip to content

The agent that grows with you

fix(cli): symmetric Windows resume-failure handling + atexit disarm

Tyler Lyon18f4b29daf31
diff --git a/hermes_cli/update_cmd.py b/hermes_cli/update_cmd.py
index 0da8f27006d..be34f5bf207 100644
--- a/hermes_cli/update_cmd.py
+++ b/hermes_cli/update_cmd.py
@@ -1281,7 +1281,18 @@ def _finish_already_up_to_date(
active_lazy_features=active_lazy_features,
active_tool_dependencies=active_tool_dependencies, upstream_checked=_plan.upstream_checked,
_windows_gateway_resume=_windows_gateway_resume)
- _m()._resume_windows_gateways_after_update(_windows_gateway_resume)
+ # Same contract as the pull path's _resume_windows_gateways_and_merge_outcome: a failed
+ # Windows gateway resume (e.g. the relaunch verification racing a Job-Object kill, #48820)
+ # must demote this run to incomplete, never abort it. A bare call here let the identical
+ # RuntimeError the pull path treats as a warning kill "Already up to date" outright (#115563).
+ resume_outcome = _GatewayRestartOutcome(
+ incomplete=False, phase_errors=[], pre_restart_gateway_pids=[],
+ restarted_services=[], failed_or_stale_units=[], relaunched_profiles=[],
+ externally_supervised_profiles=[], killed_pids=set(),
+ )
+ _resume_windows_gateways_and_merge_outcome(resume_outcome, _windows_gateway_resume, gateway_mode)
+ if resume_outcome.incomplete:
+ current_checkout_complete = False
# A prior pull may still owe the fleet a restart; catch up here too, BEFORE the exit
# gate so a partial outcome can't strand the fleet on stale code.
# Catch up even on the "Already up to date" path — that early return is what left the gateway on stale
diff --git a/hermes_cli/update_cmd_windows.py b/hermes_cli/update_cmd_windows.py
index 9abc2fe7f66..f7937b7704c 100644
--- a/hermes_cli/update_cmd_windows.py
+++ b/hermes_cli/update_cmd_windows.py
@@ -1215,6 +1215,14 @@ def _resume_windows_gateways_after_update(token: dict | None) -> None:
from hermes_cli.update_cmd import _m
if not token or not token.get("resume_needed"):
return
+ # The foreground call sites register this same function via atexit as a safety net for
+ # process death before they get a chance to run it themselves (#115563). Once execution
+ # actually reaches here — foreground or the atexit fallback itself — ownership is taken:
+ # unregister immediately so a failure below (or the foreground caller failing after this
+ # returns) cannot replay the same RuntimeError a second time at interpreter teardown.
+ # ``unregister`` is a no-op when this function was never registered.
+ import atexit
+ atexit.unregister(_resume_windows_gateways_after_update)
if not _m()._is_windows():
token["resume_needed"] = False
return
diff --git a/tests/hermes_cli/test_windows_update_restart_reconciliation.py b/tests/hermes_cli/test_windows_update_restart_reconciliation.py
index 33b8e6bc7ea..e6c63c55d53 100644
--- a/tests/hermes_cli/test_windows_update_restart_reconciliation.py
+++ b/tests/hermes_cli/test_windows_update_restart_reconciliation.py
@@ -202,3 +202,73 @@ def test_merge_helper_reads_token_keys_into_restart_outcome(monkeypatch):
assert outcome.restarted_services == ["hermes-gateway", "svc"]
assert outcome.failed_or_stale_units == ["p3"]
assert outcome.incomplete is False
+
+
+# ---------------------------------------------------------------------------
+# #115563: symmetric resume-failure handling + atexit double-fire
+# ---------------------------------------------------------------------------
+
+def test_already_up_to_date_path_demotes_on_windows_resume_failure(monkeypatch):
+ """The pull path treats a Windows resume failure as a warning (outcome.incomplete = True,
+ update continues); the 'Already up to date' path called _resume_windows_gateways_after_update
+ bare, so the identical RuntimeError killed the run instead (#115563). It must now route
+ through the same _resume_windows_gateways_and_merge_outcome contract and demote
+ current_checkout_complete, landing on the same 'partial' + exit(1) path a failed repair
+ already uses — not an unhandled exception."""
+ from hermes_cli import update_cmd
+
+ class _Plan:
+ auto_stash_ref = None
+ parked_branch_switched = False
+ switch_block_reason = ""
+ upstream_checked = True
+
+ monkeypatch.setattr(update_cmd, "_invalidate_update_cache", lambda: None)
+ monkeypatch.setattr(update_cmd, "_repair_current_checkout", lambda **_kw: True)
+ monkeypatch.setattr(update_cmd, "_apply_pending_fleet_restart_catchup", lambda **_kw: None)
+
+ finalized = []
+ monkeypatch.setattr(update_cmd, "_finalize_receipt", lambda status, *_a: finalized.append(status))
+
+ def _fail_resume(_token):
+ raise RuntimeError("Windows gateway relaunch after update was not verified alive")
+
+ monkeypatch.setattr(hm, "_resume_windows_gateways_after_update", _fail_resume)
+
+ with patch("builtins.print"):
+ with pytest.raises(SystemExit) as exc_info:
+ update_cmd._finish_already_up_to_date(
+ git_cmd="git", branch="main", current_branch="main", _plan=_Plan(),
+ assume_yes=True, gateway_mode=False, gw_input_fn=None,
+ pre_update_snapshot_id=None, had_desktop_app_before_update=False,
+ active_lazy_features=None, active_tool_dependencies=None,
+ _windows_gateway_resume={"resume_needed": True},
+ )
+
+ # Demoted to the existing partial-repair contract, not an unhandled RuntimeError.
+ assert exc_info.value.code == 1
+ assert finalized == ["partial"]
+
+
+def test_resume_unregisters_its_own_atexit_fallback_before_running(monkeypatch):
+ """Every foreground call site registers this same function via atexit as a dead-process
+ safety net. Once execution actually reaches here it must disarm that fallback immediately
+ -- otherwise a failure below (or the foreground caller dying right after return) replays
+ the identical error a second time at interpreter teardown (#115563)."""
+ import atexit
+
+ from hermes_cli import update_cmd_windows
+
+ calls = []
+ monkeypatch.setattr(
+ atexit, "unregister",
+ lambda fn: calls.append(fn) or None,
+ )
+ monkeypatch.setattr(hm, "_is_windows", lambda: False)
+
+ token = {"resume_needed": True}
+ update_cmd_windows._resume_windows_gateways_after_update(token)
+
+ assert calls == [update_cmd_windows._resume_windows_gateways_after_update]
+ assert token["resume_needed"] is False
+