The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): an early startup exit is cleanup, not a clean shutdown
Tekniumd21392c3459d
diff --git a/gateway/run.py b/gateway/run.pyindex 0324f321626..2ae94196dc9 100644--- a/gateway/run.py+++ b/gateway/run.py@@ -3732,6 +3732,8 @@ class GatewayRunner(return t("gateway.busy.action_restarting") if self._restart_requested else t("gateway.busy.action_shutting_down")def _update_runtime_status(self, gateway_state: Optional[str] = None, exit_reason: Optional[str] = None) -> None:+ if getattr(self, "_startup_cleanup_only", False):+ return # cleanup of a start that never ran keeps its startup verdict (startup_failed)# ``active_work`` names each unit only while draining — that is when an observer (``hermes# update``) needs to know WHAT holds the gateway open; a per-turn write would be wasted I/O.active_work = self._describe_active_work() if gateway_state == "draining" else Nonediff --git a/gateway/run_bootstrap.py b/gateway/run_bootstrap.pyindex b3f0df1eaaa..ee5a51b8fea 100644--- a/gateway/run_bootstrap.py+++ b/gateway/run_bootstrap.py@@ -823,8 +823,11 @@ async def start_gateway(config: Optional[GatewayConfig] = None, replace: bool =if runner is not None:from gateway.run_runtime import drain_gateway_runtimeawait drain_gateway_runtime(runner)- # Startup may have opened adapters before an exception. The same- # stop path owns their writers and DB handles on every exit.+ # Startup may have opened adapters before an exception or an early exit verdict. The+ # same stop path owns their writers and DB handles on every exit, but when no stop ran+ # yet this is cleanup, not a shutdown: keep startup_failed and the crash markers.+ if getattr(runner, "_stop_task", None) is None and not getattr(runner, "_running", False):+ runner._startup_cleanup_only = Trueawait runner.stop()finally:if _planned_stop_watcher_stop is not None:diff --git a/gateway/run_shutdown.py b/gateway/run_shutdown.pyindex c74ecc258a4..98f8d6dbce3 100644--- a/gateway/run_shutdown.py+++ b/gateway/run_shutdown.py@@ -2127,16 +2127,16 @@ class GatewayShutdownMixin(GatewaySessionEndMixin):releases PID/lock ownership after the writer drain."""from gateway.run import _hermes_home, _planned_restart_notification_path, _shutdown_gateway_health_exportfrom utils import atomic_json_write+ if getattr(self, "_startup_cleanup_only", False): # start never ran: keep startup_failed + crash markers+ return _shutdown_gateway_health_export(self)# Clean-shutdown marker skips crash-turn recovery next boot; a timed-out drain left# half-finished sessions, so no marker — the next startup recovers their turn markers.if not ctx.timed_out:with suppress(Exception):(_hermes_home / ".clean_shutdown").touch()else:- logger.info(- "Skipping .clean_shutdown marker — drain timed out with "- "interrupted agents; next startup will recover their interrupted turns."- )+ logger.info("Skipping .clean_shutdown marker — drain timed out with interrupted agents; "+ "next startup will recover their interrupted turns.")# Stuck-loop counter: sessions active across 3 consecutive restarts are auto-suspended next boot.if ctx.active_agents:self._increment_restart_failure_counts(set(ctx.active_agents.keys()))diff --git a/tests/gateway/test_bootstrap_early_exit_teardown.py b/tests/gateway/test_bootstrap_early_exit_teardown.pyindex 96afd896dbb..cd425344572 100644--- a/tests/gateway/test_bootstrap_early_exit_teardown.py+++ b/tests/gateway/test_bootstrap_early_exit_teardown.py@@ -5,6 +5,30 @@ import threadingimport pytest+async def test_refused_startup_keeps_startup_failed_and_crash_recovery(monkeypatch):+ from gateway import run as gateway_run+ from gateway.config import GatewayConfig, Platform, PlatformConfig+ from gateway.run_bootstrap import start_gateway+ from gateway.status import read_runtime_status+ from hermes_constants import get_hermes_home++ home = get_hermes_home()+ home.chmod(0o700)+ monkeypatch.setattr(gateway_run, '_hermes_home', home)+ for name in ('GATEWAY_ALLOW_ALL_USERS', 'WHATSAPP_ALLOW_ALL_USERS', 'WHATSAPP_DM_POLICY'):+ monkeypatch.delenv(name, raising=False)+ # The open-DM refusal: start() writes startup_failed and requests a clean exit before recovery.+ config = GatewayConfig(platforms={Platform.WHATSAPP: PlatformConfig(+ enabled=True, extra={'dm_policy': 'open'})})++ assert await asyncio.wait_for(start_gateway(config), 30) is True++ assert not (home / '.clean_shutdown').exists(), 'refused start wrote the clean-shutdown marker'+ assert read_runtime_status()['gateway_state'] == 'startup_failed'+ assert not (home / 'gateway.pid').exists()++@pytest.mark.asyncioasync def test_ready_publication_failure_stops_cron_and_housekeeping(monkeypatch):from gateway import run_bootstrap, run_runtime