Skip to content

The agent that grows with you

fix(gateway): stay up degraded when the last platform fails non-retryably mid-run

unsupportedpastels6fcd2e296729
diff --git a/gateway/run_adapters.py b/gateway/run_adapters.py
index 818b30904ec..012a3aa7bee 100644
--- a/gateway/run_adapters.py
+++ b/gateway/run_adapters.py
@@ -412,6 +412,8 @@ class GatewayAdapterLifecycleMixin:
)
self._ensure_reconnect_watcher_running()
return
+ if self._park_last_fatal_platform(adapter):
+ return
self._exit_reason = adapter.fatal_error_message or "All messaging adapters disconnected"
if adapter.fatal_error_retryable:
self._exit_with_failure = True
diff --git a/gateway/run_startup.py b/gateway/run_startup.py
index 69d1babbd7d..5b832acdb20 100644
--- a/gateway/run_startup.py
+++ b/gateway/run_startup.py
@@ -98,6 +98,22 @@ class GatewayStartupMixin:
def _serving_state(self) -> str:
return "degraded" if self._startup_parked_platforms else "running"
+ def _park_last_fatal_platform(self, adapter) -> bool:
+ """Mid-run twin of the all-fatal startup case: the last platform failed non-retryably, so park it
+ and stay up degraded for local clients and cron. Return False when the gateway must stop instead
+ (retryable, or another gateway owns the token/profile)."""
+ if adapter.fatal_error_retryable or is_global_startup_conflict(adapter.fatal_error_code):
+ return False
+ self._startup_parked_platforms = True
+ self._update_runtime_status(self._serving_state())
+ logger.error(
+ "%s is parked after a non-retryable error (fix it, then `hermes gateway restart`). The gateway "
+ "is DEGRADED — it serves local clients and cron with no messaging platform. %s: %s",
+ adapter.platform.value, adapter.fatal_error_code or "unknown",
+ adapter.fatal_error_message or "unknown error",
+ )
+ return True
+
async def _run_startup_resume_event(
self, adapter: BasePlatformAdapter, event: MessageEvent, session_key: str,
) -> None:
diff --git a/tests/gateway/test_startup_failed_platform_reports_degraded.py b/tests/gateway/test_startup_failed_platform_reports_degraded.py
index b706a708ecc..b3726587ca2 100644
--- a/tests/gateway/test_startup_failed_platform_reports_degraded.py
+++ b/tests/gateway/test_startup_failed_platform_reports_degraded.py
@@ -9,6 +9,7 @@ the same way.
"""
import logging
import time
+from unittest.mock import AsyncMock
import pytest
@@ -78,6 +79,27 @@ def _runner(monkeypatch, tmp_path, platforms, create_adapter) -> GatewayRunner:
return runner
+class _LostMidRunAdapter(_HealthyAdapter):
+ """Connects, then loses the bot mid-run non-retryably (token revoked, conflict retries spent)."""
+
+ async def fail(self, code: str) -> None:
+ self._set_fatal_error(code, "Telegram rejected the bot token.", retryable=False)
+ await self._notify_fatal_error()
+
+
+async def _start_then_lose_the_only_platform(monkeypatch, tmp_path, code):
+ adapter = _LostMidRunAdapter()
+ runner = _runner(
+ monkeypatch, tmp_path, {Platform.TELEGRAM: PlatformConfig(enabled=True, token="***")},
+ lambda platform, platform_config: adapter,
+ )
+ assert await runner.start() is True
+ real_stop, stop = runner.stop, AsyncMock()
+ runner.stop = stop
+ await adapter.fail(code)
+ return runner, stop, real_stop
+
+
def _runner_with_one_parked_platform(monkeypatch, tmp_path) -> GatewayRunner:
return _runner(
monkeypatch, tmp_path,
@@ -142,6 +164,45 @@ async def test_sole_parked_platform_keeps_the_gateway_up_degraded(monkeypatch, t
await runner.stop()
+async def test_last_platform_lost_non_retryably_mid_run_keeps_the_gateway_up_degraded(
+ monkeypatch, tmp_path, caplog
+):
+ """The mid-run twin of the all-fatal startup case: a revoked token on the only platform parks it
+ and the gateway stays up ``degraded`` for local clients and cron instead of stopping."""
+ with caplog.at_level(logging.ERROR):
+ _, stop, real_stop = await _start_then_lose_the_only_platform(
+ monkeypatch, tmp_path, "telegram_auth_error"
+ )
+ try:
+ stop.assert_not_awaited()
+ assert any(
+ record.levelno >= logging.ERROR and "telegram" in record.getMessage()
+ and "hermes gateway restart" in record.getMessage()
+ for record in caplog.records
+ )
+ assert await flush_runtime_status_async()
+ state = read_runtime_status()
+ assert state["gateway_state"] == "degraded"
+ assert state["platforms"]["telegram"]["state"] == "fatal"
+ assert state["platforms"]["telegram"]["error_code"] == "telegram_auth_error"
+ finally:
+ await real_stop()
+
+
+async def test_mid_run_ownership_conflict_on_the_last_platform_still_stops(monkeypatch, tmp_path):
+ """Protection: another gateway owning the token mid-run keeps today's clean stop."""
+ runner, stop, real_stop = await _start_then_lose_the_only_platform(
+ monkeypatch, tmp_path, "telegram_token_lock"
+ )
+ try:
+ stop.assert_awaited()
+ assert runner._exit_with_failure is False
+ finally:
+ await real_stop()
+
+
@pytest.mark.asyncio
async def test_every_platform_connected_still_reports_a_normal_run(monkeypatch, tmp_path, caplog):
"""Protection: a clean startup keeps the normal ``running`` state and logs no ERROR."""