Skip to content

The agent that grows with you

fix(cron): render watchdog-degraded exits in `hermes gateway status`; degraded is the last writer

teknium18bfc66b2527f
diff --git a/gateway/shutdown_watchdog.py b/gateway/shutdown_watchdog.py
index b892bcfe733..028b78c0c23 100644
--- a/gateway/shutdown_watchdog.py
+++ b/gateway/shutdown_watchdog.py
@@ -146,25 +146,20 @@ def start_loop_liveness_watchdog(
def _mark_exited_quietly(exit_code: int, reason: str) -> None:
- """Publish terminal watchdog evidence to both lifecycle stores, best effort.
-
- ``gateway_state.json`` and the lifecycle sentinel are independent records. The
- liveness watchdog already had authority to terminate a wedged process, but only
- updated the sentinel; status readers therefore observed ``running`` until a
- replacement process booted. Keep these terminal writes together so every
- watchdog exit exposes the same degraded reason before ``os._exit`` bypasses normal
- teardown.
- """
- with contextlib.suppress(Exception):
- from gateway.status import write_runtime_status
- write_runtime_status(
- gateway_state="degraded",
- exit_reason=reason,
- restart_requested=exit_code == GATEWAY_SERVICE_RESTART_EXIT_CODE,
- )
+ """Best-effort terminal stamp on BOTH lifecycle records before ``os._exit`` skips teardown:
+ the lifecycle ledger (so the next boot names the watchdog, not SIGKILL/OOM) and
+ ``gateway_state.json`` (so ``hermes gateway status`` and every other reader of that file stop
+ seeing ``running`` for a process the watchdog killed — #113372). The runtime-status write goes
+ LAST: it is the record housekeeping refreshes, so nothing may overwrite it after we stamp it."""
with contextlib.suppress(Exception):
from gateway.lifecycle_ledger import mark_exited
mark_exited(exit_code, reason=reason)
+ with contextlib.suppress(Exception):
+ from gateway.status import write_runtime_status
+ # Only the supervisor-restart code asserts a restart; other codes leave the recorded
+ # operator intent (a restart-drain that wedged is still a requested restart) untouched.
+ restart = {"restart_requested": True} if exit_code == GATEWAY_SERVICE_RESTART_EXIT_CODE else {}
+ write_runtime_status(gateway_state="degraded", exit_reason=reason, **restart)
def _process_hermes_home() -> Path:
diff --git a/hermes_cli/gateway.py b/hermes_cli/gateway.py
index 5c8807db042..fd0038ff657 100644
--- a/hermes_cli/gateway.py
+++ b/hermes_cli/gateway.py
@@ -5078,6 +5078,16 @@ def _platform_status(platform: dict) -> str:
return "partially configured" if any(present) else "not configured"
+# Operator wording for the out-of-loop watchdog exit reasons stamped by gateway/shutdown_watchdog.py.
+_WATCHDOG_EXIT_REASONS = {
+ "loop_liveness_watchdog": (
+ "event loop stopped dispatching (housekeeping, cron and the kanban dispatcher froze); "
+ f"the liveness watchdog exited with code {GATEWAY_SERVICE_RESTART_EXIT_CODE} for the supervisor to restart it"
+ ),
+ "shutdown_watchdog": "shutdown drain wedged; the shutdown watchdog forced the exit (see logs/gateway-shutdown-watchdog.log)",
+}
+
+
def _runtime_health_lines() -> list[str]:
"""Summarize the latest persisted gateway runtime health state."""
try:
@@ -5112,6 +5122,10 @@ def _runtime_health_lines() -> list[str]:
if gateway_state == "startup_failed" and exit_reason:
lines.append(f"⚠ Last startup issue: {exit_reason}")
+ elif gateway_state == "degraded" and exit_reason:
+ # An out-of-loop watchdog hard-exited the process (#113372): the loop stopped dispatching, so
+ # housekeeping/cron/kanban froze together. Without this arm the file would read 'running'.
+ lines.append(f"⚠ Gateway exited degraded: {_WATCHDOG_EXIT_REASONS.get(exit_reason, exit_reason)}")
elif gateway_state == "draining":
action = "restart" if state.get("restart_requested") else "shutdown"
from gateway.status import parse_active_agents
diff --git a/tests/hermes_cli/test_gateway_runtime_health.py b/tests/hermes_cli/test_gateway_runtime_health.py
index aeab6bfa63a..bd17528f856 100644
--- a/tests/hermes_cli/test_gateway_runtime_health.py
+++ b/tests/hermes_cli/test_gateway_runtime_health.py
@@ -64,6 +64,21 @@ def test_runtime_health_lines_include_fatal_platform_and_startup_reason(monkeypa
assert "⚠ Last startup issue: telegram conflict" in lines
+def test_runtime_health_lines_render_watchdog_degraded_exit(monkeypatch):
+ """A watchdog-stamped ``degraded`` + exit_reason renders as a health line; the startup-time
+ ``degraded`` (retryable platforms queued, no exit_reason) stays silent (#113372)."""
+ record = {"gateway_state": "degraded", "exit_reason": "loop_liveness_watchdog",
+ "pid": 4242, "updated_at": _iso_age(30), "platforms": {}}
+ monkeypatch.setattr("gateway.status.read_runtime_status", lambda: record)
+
+ degraded = [ln for ln in _runtime_health_lines() if ln.startswith("⚠ Gateway exited degraded:")]
+ assert len(degraded) == 1
+ assert "event loop stopped dispatching" in degraded[0]
+
+ record["exit_reason"] = None
+ assert not [ln for ln in _runtime_health_lines() if "degraded" in ln]
+
+
def test_runtime_status_running_pid_validates_live_gateway_record(monkeypatch):
from gateway import status as status_mod
diff --git a/website/docs/user-guide/messaging/index.md b/website/docs/user-guide/messaging/index.md
index d363ec5178f..9a6314e42f6 100644
--- a/website/docs/user-guide/messaging/index.md
+++ b/website/docs/user-guide/messaging/index.md
@@ -184,6 +184,20 @@ stack to `~/.hermes/logs/gateway_faulthandler.log` and the gateway keeps
running — use it to see what a stalled or misbehaving gateway is doing without
restarting it.
+### Built-in event-loop liveness watchdog
+
+On every platform the gateway runs an out-of-loop watchdog thread that probes
+the asyncio loop (`gateway.loop_watchdog_probe_interval_s`, default 30 s). When
+the loop stops dispatching for `gateway.loop_watchdog_max_strikes` consecutive
+probes (default 3), housekeeping, the cron scheduler and the embedded kanban
+dispatcher have all frozen with it, so the watchdog dumps every thread's stack
+to the log, stamps `gateway_state.json` with `gateway_state: degraded` and
+`exit_reason: loop_liveness_watchdog`, and exits with code `75` so the service
+supervisor restarts the process. `hermes gateway status` renders that record as
+`⚠ Gateway exited degraded: event loop stopped dispatching …` until a new
+gateway process overwrites it. Set `gateway.loop_watchdog: false` in
+`config.yaml` to disable the watchdog.
+
### Optional Linux event-loop watchdog
A systemd-managed gateway can opt into process recovery when Python's asyncio