Skip to content

The agent that grows with you

fix(gateway): drop the redundant whole-block lease; one invariant test per atom; document the watchdog env vars

teknium1b5310236223a
diff --git a/gateway/run.py b/gateway/run.py
index 59a9afab97a..2ff17be402e 100644
--- a/gateway/run.py
+++ b/gateway/run.py
@@ -3602,12 +3602,6 @@ class GatewayRunner(
if self._session_db is not None:
try:
from hermes_cli.config import load_config as _load_full_config
- from hermes_startup_watchdog import report_startup_progress as _report_startup_progress
- # Startup-watchdog lease (#111092): construction-time maintenance is I/O-bound
- # with near-zero CPU, which the watchdog misreads as a parked deadlock. The
- # maintenance functions renew per long step; this covers the block as a whole.
- # No-op when the watchdog is not armed; never raises.
- _report_startup_progress(900.0, phase="gateway_startup_state_maintenance")
_sess_cfg = (_load_full_config().get("sessions") or {})
if _sess_cfg.get("auto_archive", False):
self._session_db._db.maybe_auto_archive(
diff --git a/tests/hermes_state/test_startup_maintenance_lease.py b/tests/hermes_state/test_startup_maintenance_lease.py
index 419cc999b86..935aad19609 100644
--- a/tests/hermes_state/test_startup_maintenance_lease.py
+++ b/tests/hermes_state/test_startup_maintenance_lease.py
@@ -1,73 +1,42 @@
-"""Startup-watchdog progress leases across state.db auto-maintenance.
+"""Construction-time state.db maintenance must keep the startup watchdog's progress lease alive (#111092).
-Regression for #111092: gateway restart live-lock on large installs. The construction-time
-maintenance block (auto-archive, auto-prune, orphan sweep, VACUUM) is I/O-bound with
-near-zero CPU, which the startup watchdog misreads as a parked deadlock and kills with
-exit 75. Each long synchronous step must hold a ``report_startup_progress`` lease; leases
-are clamped to 900 s per call, so the lease is renewed per step rather than once at
-entry.
-
-Invariant (not a change-detector): every long maintenance step reports a progress lease.
+Archive, prune, orphan sweep and VACUUM are I/O-bound with near-zero CPU, so the watchdog's
+CPU fallback cannot tell them from a parked deadlock; only a phase lease can. Leases are
+clamped to ``_MAX_LEASE_S`` per call, so each long step renews rather than one lease at entry.
"""
from __future__ import annotations
-from pathlib import Path
+import time
+import hermes_startup_watchdog as sw
from hermes_state import SessionDB
-import hermes_state_maintenance
-import hermes_state_sessions
-
-
-def _make_db(tmp_path: Path) -> SessionDB:
- return SessionDB(db_path=tmp_path / "state.db")
-
-def test_prune_sweep_and_vacuum_each_hold_a_lease(tmp_path, monkeypatch):
- leases: list[str] = []
- monkeypatch.setattr(
- hermes_state_maintenance,
- "report_startup_progress",
- lambda expected_s, phase="": leases.append(phase),
- )
- db = _make_db(tmp_path)
+def test_maintenance_steps_renew_the_armed_watchdog_lease(tmp_path, monkeypatch):
+ monkeypatch.setenv("HERMES_HOME", str(tmp_path))
+ sw._reset_for_tests()
+ handle = sw.arm_startup_watchdog(timeout_s=300.0)
+ db = SessionDB(db_path=tmp_path / "state.db")
try:
- # Simulate a large-DB sweep where every step does long I/O-bound work.
- monkeypatch.setattr(db, "prune_sessions", lambda **kw: 120)
+ # Maintenance is throttled by state_meta; a fresh DB has never run it.
+ monkeypatch.setattr(db, "prune_sessions", lambda **kw: 120) # VACUUM only runs after a real prune
monkeypatch.setattr(db, "sweep_orphaned_sessions", lambda **kw: [])
monkeypatch.setattr(db, "_freelist_ratio", lambda: 1.0)
- monkeypatch.setattr(db, "vacuum", lambda: 0)
+ monkeypatch.setattr(db, "vacuum", lambda: None)
+ monkeypatch.setattr(db, "archive_stale_sessions", lambda *a, **kw: 0)
+ before = handle._lease_count
- result = db.maybe_auto_prune_and_vacuum(
- retention_days=90,
- min_interval_hours=0,
- vacuum=True,
- min_vacuum_interval_days=0,
+ db.maybe_auto_archive(idle_days=3, min_interval_hours=0)
+ db.maybe_auto_prune_and_vacuum(
+ retention_days=90, min_interval_hours=0, vacuum=True, min_vacuum_interval_days=0,
)
- assert result["pruned"] == 120
- assert result["vacuumed"] is True
- # A lease must be held across EACH long step: without them the watchdog
- # fires exit 75 mid-sweep on a large state.db (#111092).
- assert "state_db_auto_prune" in leases
- assert "state_db_auto_sweep" in leases
- assert "state_db_auto_vacuum" in leases
- finally:
- db.close()
-
-def test_auto_archive_holds_a_lease(tmp_path, monkeypatch):
- leases: list[str] = []
- monkeypatch.setattr(
- hermes_state_sessions,
- "report_startup_progress",
- lambda expected_s, phase="": leases.append(phase),
- )
- db = _make_db(tmp_path)
- try:
- monkeypatch.setattr(db, "archive_stale_sessions", lambda *a, **kw: 7)
- result = db.maybe_auto_archive(idle_days=3, min_interval_hours=0)
- assert result["archived"] == 7
- assert "state_db_auto_archive" in leases
+ # One renewal per long step (archive, prune, sweep, vacuum), the last one
+ # taken right before VACUUM so a multi-minute rewrite never outlives the clamp.
+ assert handle._lease_count - before == 4
+ assert handle._lease_phase == "state_db_auto_vacuum"
+ assert handle._lease_until > time.monotonic()
finally:
db.close()
+ sw._reset_for_tests()
diff --git a/website/docs/reference/environment-variables.md b/website/docs/reference/environment-variables.md
index 159c8ed0a6b..af7ba2089c5 100644
--- a/website/docs/reference/environment-variables.md
+++ b/website/docs/reference/environment-variables.md
@@ -829,6 +829,8 @@ Advanced per-platform knobs for throttling the outbound message batcher. Most us
| `HERMES_AGENT_TIMEOUT` | Gateway inactivity timeout for a running agent in seconds (default: `1800`, 30 minutes). Resets on every tool call and streamed token. Set to `0` to disable. |
| `HERMES_GATEWAY_MAX_STARTS` | Respawn-storm circuit breaker: maximum gateway (re)starts allowed within the window before an exponential backoff is slept to break the storm (default: `5`, `0` disables). Also configurable via `gateway.respawn_storm.max_starts` in `config.yaml`. |
| `HERMES_GATEWAY_START_WINDOW_S` | Respawn-storm breaker window in seconds (default: `120`). Also configurable via `gateway.respawn_storm.window_seconds` in `config.yaml`. |
+| `HERMES_STARTUP_WATCHDOG` | Startup-liveness watchdog for `hermes gateway run`: if the process does not reach a live event loop within the timeout, holds no progress lease and shows no CPU progress, it dumps every thread's stack to `logs/gateway-startup-watchdog.log` and exits with code `75` so the service supervisor (systemd, s6, Windows task) restarts it. Set to `0` to opt out. Env-only because `config.yaml` parsing is itself inside the watched window; `gateway.startup_watchdog: false` in `config.yaml` is bridged into this variable when unset. |
+| `HERMES_STARTUP_WATCHDOG_TIMEOUT_S` | Startup watchdog timeout in seconds (default: `300`). Slow-but-alive phases (state.db schema migrations, repair, construction-time archive/prune/VACUUM) hold their own progress leases, so raise this only when a large install's startup is legitimately longer than five minutes *outside* those phases (many multiplexed profiles, thousands of skills on a slow disk). Bridged from `gateway.startup_watchdog_timeout_seconds` in `config.yaml` when unset. |
| `HERMES_AGENT_TIMEOUT_WARNING` | Gateway: send a warning message after this many seconds of inactivity (default: 75% of `HERMES_AGENT_TIMEOUT`). |
| `HERMES_AGENT_NOTIFY_INTERVAL` | Gateway: interval in seconds between progress notifications on long-running agent turns. |
| `HERMES_CHECKPOINT_TIMEOUT` | Timeout for filesystem checkpoint creation in seconds (default: `30`). |