The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(install): surface aborted gateway restart during hermes update
PRATHAMESH7595018b6bba49
diff --git a/hermes_cli/main.py b/hermes_cli/main.pyindex 23a0559ed1d..71bf80fbc06 100644--- a/hermes_cli/main.py+++ b/hermes_cli/main.py@@ -5242,6 +5242,7 @@ from hermes_cli.update_cmd import ( # noqa: F401_stash_apply_failed_only_on_existing_untracked,_stash_local_changes_if_needed,_stop_process_trees,+ _surviving_gateway_pids_after_failed_restart,_sync_fork_with_upstream,_sync_with_upstream_if_needed,_update_node_dependencies,@@ -5252,6 +5253,7 @@ from hermes_cli.update_cmd import ( # noqa: F401_venv_core_imports_healthy,_venv_launcher_ancestors,_wait_for_windows_update_gateway_exit,+ _warn_gateway_restart_phase_aborted,_warn_incomplete_gateway_fleet_restart,_web_build_toolchain_ready,_web_toolchain_roots,diff --git a/hermes_cli/update_cmd.py b/hermes_cli/update_cmd.pyindex aec1e433798..117a1544dfe 100644--- a/hermes_cli/update_cmd.py+++ b/hermes_cli/update_cmd.py@@ -3548,6 +3548,47 @@ def _warn_incomplete_gateway_fleet_restart(failed_units: list) -> None:print(" systemctl --user restart <unit> # user-scope")print(" sudo systemctl restart <unit> # system-scope")+def _surviving_gateway_pids_after_failed_restart():+ """Best-effort PIDs of gateways still running after the restart phase died.++ Returns ``None`` when the answer cannot be determined — most importantly+ when ``hermes_cli.gateway`` itself no longer imports, which is one of the+ ways the restart phase aborts in the first place (the update replaced the+ checkout under a process that already loaded the old modules). ``None`` and+ a non-empty list are both treated as "assume stale" by the caller; only a+ positive empty result is proof that nothing needs restarting.+ """+ try:+ from hermes_cli.gateway import find_gateway_pids++ return list(find_gateway_pids(all_profiles=True))+ except Exception as exc: # pragma: no cover - defensive+ logger.debug("Could not probe for surviving gateways after update: %s", exc)+ return None++def _warn_gateway_restart_phase_aborted(exc: BaseException, pids) -> None:+ """Print a recovery warning when the whole restart phase raised.++ Issue #78574: the gateway auto-restart phase was wrapped in a blanket+ ``except Exception`` that only logged at debug level, so an early failure+ (e.g. importing ``hermes_cli.gateway`` from the freshly pulled checkout)+ erased every drain/restart line from the update output. The update still+ printed "Update complete!" and exited 0 while the running gateway kept+ serving pre-update modules against replaced source files — the next turn+ died with an ImportError.+ """+ print()+ print(f"⚠ Update incomplete — gateway auto-restart failed: {exc}")+ if pids:+ listed = ", ".join(str(pid) for pid in pids)+ print(f" Gateway process(es) still running pre-update code: {listed}")+ else:+ print(" Any gateway still running is serving pre-update code")+ print(" (mixed sys.modules) against the updated checkout.")+ print(" Restart it manually, then verify:")+ print(" hermes gateway restart")+ print(" hermes gateway status")+def _refresh_windows_gateway_launchers() -> None:"""Regenerate installed Windows gateway launcher scripts after update.@@ -5804,6 +5845,20 @@ def _cmd_update_impl(args, gateway_mode: bool):except Exception as e:logger.debug("Gateway restart during update failed: %s", e)+ # An exception escaping the whole phase means the drain/restart+ # output the user relies on never printed. Don't let that pass for+ # a clean update: surface it and treat the fleet as stale unless we+ # can positively prove no gateway is running (#78574).+ _surviving = _surviving_gateway_pids_after_failed_restart()+ if _surviving is None or _surviving:+ gateway_fleet_restart_incomplete = True+ _warn_gateway_restart_phase_aborted(e, _surviving)+ if gateway_mode:+ _exit_code_path = get_hermes_home() / ".update_exit_code"+ try:+ _exit_code_path.write_text("1", encoding="utf-8")+ except OSError:+ pass_m()._resume_windows_gateways_after_update(_windows_gateway_resume)diff --git a/tests/hermes_cli/test_update_gateway_restart_aborted.py b/tests/hermes_cli/test_update_gateway_restart_aborted.pynew file mode 100644index 00000000000..4d7f6574e5f--- /dev/null+++ b/tests/hermes_cli/test_update_gateway_restart_aborted.py@@ -0,0 +1,73 @@+"""Regression for #78574 — a crashed gateway-restart phase must not stay silent.++``hermes update`` wrapped its entire gateway auto-restart phase in a blanket+``except Exception`` that only logged at debug level. When the phase raised+early (e.g. importing ``hermes_cli.gateway`` from the freshly pulled checkout+inside a process that already loaded the pre-update modules), every drain and+restart line vanished from the update output, the update printed+"Update complete!" and exited 0 — while the still-running default-profile+gateway kept serving pre-update modules and died on the next turn with+``ImportError: cannot import name 'is_trivial_prompt'``.+"""++from __future__ import annotations++import sys+import types++from hermes_cli.main import (+ _surviving_gateway_pids_after_failed_restart,+ _warn_gateway_restart_phase_aborted,+)+++class TestSurvivingGatewayProbe:+ def test_reports_running_gateway_pids(self, monkeypatch):+ fake = types.ModuleType("hermes_cli.gateway")+ fake.find_gateway_pids = lambda **_kwargs: [4321]+ monkeypatch.setitem(sys.modules, "hermes_cli.gateway", fake)++ assert _surviving_gateway_pids_after_failed_restart() == [4321]++ def test_empty_when_no_gateway_is_running(self, monkeypatch):+ fake = types.ModuleType("hermes_cli.gateway")+ fake.find_gateway_pids = lambda **_kwargs: []+ monkeypatch.setitem(sys.modules, "hermes_cli.gateway", fake)++ # An empty list is the only "nothing to restart" proof; it must be+ # distinguishable from the undeterminable case below.+ assert _surviving_gateway_pids_after_failed_restart() == []++ def test_undeterminable_when_gateway_module_is_broken(self, monkeypatch):+ """The probe must not raise — a broken gateway module is the bug's cause."""+ fake = types.ModuleType("hermes_cli.gateway")++ def _boom(**_kwargs):+ raise ImportError("cannot import name 'is_trivial_prompt'")++ fake.find_gateway_pids = _boom+ monkeypatch.setitem(sys.modules, "hermes_cli.gateway", fake)++ assert _surviving_gateway_pids_after_failed_restart() is None+++class TestAbortedRestartWarning:+ def test_warns_with_recovery_command_and_cause(self, capsys):+ _warn_gateway_restart_phase_aborted(+ ImportError("cannot import name 'is_trivial_prompt'"),+ [4321],+ )+ out = capsys.readouterr().out++ assert "Update incomplete" in out+ assert "is_trivial_prompt" in out+ assert "4321" in out+ assert "hermes gateway restart" in out++ def test_warns_even_when_surviving_pids_are_unknown(self, capsys):+ _warn_gateway_restart_phase_aborted(RuntimeError("systemctl exploded"), None)+ out = capsys.readouterr().out++ assert "Update incomplete" in out+ assert "systemctl exploded" in out+ assert "hermes gateway restart" in out