mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-07-30 19:09:28 +00:00
Second, deeper pass over tools/gateway/hermes_cli plus first pass over the trees wave 1 missed (acp, acp_adapter, skills, computer_use, docker, dashboard, conformance, monitoring, secret_sources, hermes_state, providers). Same rubric as wave 1 (AGENTS.md test policy); security, alternation/caching invariants, issue-number regressions, and E2E kept. Real test-quality fixes found and rooted out along the way: - tests/tools/test_command_guards.py made real auxiliary-LLM HTTPS calls (DEFAULT_CONFIG smart-approval leaked in) — pinned approval mode=manual via autouse fixture: 17.4s → 0.4s. - test_model_switch_custom_providers.py / test_user_providers_model_switch.py silently probed live provider catalogs (~2s/test) — stubbed cached_provider_model_ids/provider_model_ids/fetch_api_models. - test_telegram_noise_filter.py: 15-platform copy-paste matrix over shared gateway.run logic → 3 representative platforms (55s → 3.9s). - test_gateway_shutdown.py: stop()'s 5s interrupt-deadline loop spun on MagicMock agents — interrupt.side_effect now clears _running_agents (22s → 1.0s). - test_gateway_inactivity_timeout.py poll-harness timings shrunk 3-5x (24s → 1.1s); test_mcp_stability.py backoff/SIGTERM-grace sleeps patched (15.4s → 2.5s); test_async_delegation.py negative-drain wait 5s → 0.5s. - test_telegram_init_deadline.py: loop-block margin restored to 1.0s with rationale comment — the watchdog-dump assertion needs the loop blocked well past deadline+grace under parallel load (flaked once in the 40-worker verification run at a 0.2s margin). Verification: full hermetic suite via scripts/run_tests.sh — 2,438 files, 21,718 tests passed, 0 failed, 293.9s wall. Suite totals vs original baseline: 46,820 → 19,757 test functions (−57.8%), wall 583.5s → 293.9s (−50%), subprocess CPU 13,564s → 11,623s.
170 lines
5.7 KiB
Python
170 lines
5.7 KiB
Python
"""Tests for gateway.lifecycle_ledger — unclean-shutdown detection (NS-608).
|
|
|
|
The ledger is a tiny sentinel state machine:
|
|
``record_startup`` claims ``state/gateway.lifecycle.json`` as
|
|
``phase=running``; every exit path calls ``mark_exited``; the next boot's
|
|
``record_startup``/``detect_unclean_exit`` reports a still-``running``
|
|
sentinel from a dead process as an unclean death (SIGKILL / OOM / VM loss)
|
|
and enriches the report with the last heartbeat's memory sample.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import os
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from gateway.lifecycle_ledger import (
|
|
detect_unclean_exit,
|
|
get_lifecycle_sentinel_path,
|
|
mark_exited,
|
|
read_prior_exit_label,
|
|
record_startup,
|
|
sample_memory,
|
|
)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Helpers
|
|
# ---------------------------------------------------------------------------
|
|
|
|
_DEAD_PID = 2 ** 22 + 12345 # beyond default pid_max on Linux; never alive
|
|
|
|
|
|
def _write_sentinel(home: Path, payload: dict) -> Path:
|
|
path = get_lifecycle_sentinel_path(home)
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
path.write_text(json.dumps(payload), encoding="utf-8")
|
|
return path
|
|
|
|
|
|
def _read_sentinel(home: Path) -> dict:
|
|
return json.loads(get_lifecycle_sentinel_path(home).read_text(encoding="utf-8"))
|
|
|
|
|
|
def _write_heartbeat(home: Path, payload: dict) -> Path:
|
|
path = home / "state" / "gateway.heartbeat"
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
path.write_text(json.dumps(payload), encoding="utf-8")
|
|
return path
|
|
|
|
|
|
def _exit_diag_records(home: Path) -> list[dict]:
|
|
path = home / "logs" / "gateway-exit-diag.log"
|
|
if not path.exists():
|
|
return []
|
|
return [
|
|
json.loads(line)
|
|
for line in path.read_text(encoding="utf-8").splitlines()
|
|
if line.strip()
|
|
]
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# sample_memory
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
@pytest.mark.skipif(sys.platform != "linux", reason="/proc is Linux-only")
|
|
def test_sample_memory_has_expected_keys_on_linux() -> None:
|
|
sample = sample_memory()
|
|
assert sample.get("rss_kib", 0) > 0
|
|
assert sample.get("mem_total_kib", 0) > 0
|
|
assert "mem_available_kib" in sample
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# First boot / clean lifecycle
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_first_boot_reports_nothing_and_claims_sentinel(tmp_path: Path) -> None:
|
|
assert record_startup(home=tmp_path) is None
|
|
sentinel = _read_sentinel(tmp_path)
|
|
assert sentinel["phase"] == "running"
|
|
assert sentinel["pid"] == os.getpid()
|
|
assert "start_time" in sentinel
|
|
|
|
|
|
def test_clean_exit_then_boot_reports_nothing(tmp_path: Path) -> None:
|
|
record_startup(home=tmp_path)
|
|
mark_exited(0, reason="graceful_shutdown", home=tmp_path)
|
|
|
|
sentinel = _read_sentinel(tmp_path)
|
|
assert sentinel["phase"] == "exited"
|
|
assert sentinel["exit_code"] == 0
|
|
assert sentinel["exit_reason"] == "graceful_shutdown"
|
|
|
|
assert record_startup(home=tmp_path) is None
|
|
assert _exit_diag_records(tmp_path) == []
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Unclean-death detection
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_running_sentinel_from_dead_pid_is_unclean(tmp_path: Path) -> None:
|
|
_write_sentinel(tmp_path, {
|
|
"phase": "running",
|
|
"pid": _DEAD_PID,
|
|
"start_time": 1000.0,
|
|
"started_at": "2026-07-11T04:30:00+00:00",
|
|
})
|
|
|
|
evidence = detect_unclean_exit(home=tmp_path)
|
|
assert evidence is not None
|
|
assert evidence["prior_pid"] == _DEAD_PID
|
|
assert evidence["prior_started_at"] == "2026-07-11T04:30:00+00:00"
|
|
|
|
|
|
def test_record_startup_persists_unclean_report_and_reclaims(tmp_path: Path) -> None:
|
|
_write_sentinel(tmp_path, {
|
|
"phase": "running",
|
|
"pid": _DEAD_PID,
|
|
"start_time": 1000.0,
|
|
"started_at": "2026-07-11T04:30:00+00:00",
|
|
})
|
|
|
|
evidence = record_startup(home=tmp_path)
|
|
assert evidence is not None
|
|
|
|
records = _exit_diag_records(tmp_path)
|
|
assert len(records) == 1
|
|
assert records[0]["tag"] == "gateway.previous_unclean_exit"
|
|
assert records[0]["prior_pid"] == _DEAD_PID
|
|
assert records[0]["pid"] == os.getpid()
|
|
|
|
# Sentinel reclaimed for the new life.
|
|
sentinel = _read_sentinel(tmp_path)
|
|
assert sentinel["phase"] == "running"
|
|
assert sentinel["pid"] == os.getpid()
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Takeover ownership guard on mark_exited
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_mark_exited_leaves_pid_none_sentinel_alone(tmp_path: Path) -> None:
|
|
"""A sentinel with pid=None has unknown ownership — mark_exited must not
|
|
clobber it with a clean-exit claim it cannot prove is its own."""
|
|
_write_sentinel(tmp_path, {"phase": "running", "pid": None, "start_time": 2000.0})
|
|
mark_exited(0, reason="graceful_shutdown", home=tmp_path)
|
|
sentinel = _read_sentinel(tmp_path)
|
|
assert sentinel["phase"] == "running"
|
|
assert sentinel["pid"] is None
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# read_prior_exit_label (container-boot annotation)
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_prior_exit_label_survives_corrupt_sentinel(tmp_path: Path) -> None:
|
|
path = get_lifecycle_sentinel_path(tmp_path)
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
path.write_text("garbage", encoding="utf-8")
|
|
assert read_prior_exit_label(tmp_path) == "unknown"
|