Skip to content

The agent that grows with you

fix(cli): verify gateway liveness before emitting recorded standalone warning

webtecnica4ee44378f03b
diff --git a/hermes_cli/gateway_multiplex_mode.py b/hermes_cli/gateway_multiplex_mode.py
index 3744a6150d0..1029374992f 100644
--- a/hermes_cli/gateway_multiplex_mode.py
+++ b/hermes_cli/gateway_multiplex_mode.py
@@ -326,10 +326,20 @@ def recorded_standalone_warning_lines() -> list[str]:
"""Same box, rebuilt from the live gateway's ``gateway_state.json`` for processes that did not
make the decision (``hermes update``'s summary, ``hermes gateway status``)."""
try:
- from gateway.status import read_runtime_status
- reason = (read_runtime_status() or {}).get("multiplex_standalone_reason")
+ from gateway.status import (
+ read_runtime_status,
+ runtime_status_is_stale,
+ runtime_status_pid_is_live,
+ )
+ record = read_runtime_status() or {}
+ if record.get("gateway_state") in (None, "stopped", "startup_failed"):
+ return []
+ if not runtime_status_pid_is_live(record) or runtime_status_is_stale(record):
+ return []
+ reason = record.get("multiplex_standalone_reason")
except Exception:
return []
if not reason:
return []
return standalone_warning_lines(MultiplexDecision(False, "guard", str(reason)))
+
diff --git a/tests/hermes_cli/test_gateway_multiplex_mode.py b/tests/hermes_cli/test_gateway_multiplex_mode.py
index 7cda76c18f3..66a9b7c05e4 100644
--- a/tests/hermes_cli/test_gateway_multiplex_mode.py
+++ b/tests/hermes_cli/test_gateway_multiplex_mode.py
@@ -185,3 +185,56 @@ def test_guard_refusal_is_recorded_in_runtime_status_and_cleared_on_default(tmp_
assert "coder" in gw_status.read_runtime_status(tmp_path / "gateway_state.json")["multiplex_standalone_reason"]
record_multiplex_decision(MultiplexDecision(True, "default", "unset; default applies"))
assert gw_status.read_runtime_status(tmp_path / "gateway_state.json")["multiplex_standalone_reason"] is None
+
+
+def test_recorded_standalone_warning_lines_suppressed_for_dead_or_stale_record(tmp_path, monkeypatch):
+ """Dead or stale gateway_state.json must not emit standalone warnings (#120991)."""
+ import gateway.status as gw_status
+ from datetime import datetime, timezone
+
+ state_file = tmp_path / "gateway_state.json"
+ monkeypatch.setattr(gw_status, "_get_runtime_status_path", lambda: state_file)
+
+ # 1. Stopped gateway
+ state_file.write_text(json.dumps({
+ "gateway_state": "stopped",
+ "pid": os.getpid(),
+ "updated_at": datetime.now(timezone.utc).isoformat(),
+ "multiplex_standalone_reason": "orphan reason",
+ }), encoding="utf-8")
+ assert mode.recorded_standalone_warning_lines() == []
+
+ # 2. Dead PID
+ state_file.write_text(json.dumps({
+ "gateway_state": "running",
+ "pid": 999999999,
+ "updated_at": datetime.now(timezone.utc).isoformat(),
+ "multiplex_standalone_reason": "orphan reason",
+ }), encoding="utf-8")
+ monkeypatch.setattr(gw_status, "runtime_status_pid_is_live", lambda r: False)
+ assert mode.recorded_standalone_warning_lines() == []
+
+ # 3. Stale heartbeat
+ state_file.write_text(json.dumps({
+ "gateway_state": "running",
+ "pid": os.getpid(),
+ "updated_at": "2020-01-01T00:00:00Z",
+ "multiplex_standalone_reason": "orphan reason",
+ }), encoding="utf-8")
+ monkeypatch.setattr(gw_status, "runtime_status_pid_is_live", lambda r: True)
+ assert mode.recorded_standalone_warning_lines() == []
+
+ # 4. Live and fresh record emits warning
+ state_file.write_text(json.dumps({
+ "gateway_state": "running",
+ "pid": os.getpid(),
+ "updated_at": datetime.now(timezone.utc).isoformat(),
+ "multiplex_standalone_reason": "real standalone reason",
+ }), encoding="utf-8")
+ monkeypatch.setattr(gw_status, "runtime_status_pid_is_live", lambda r: True)
+ monkeypatch.setattr(gw_status, "runtime_status_is_stale", lambda r: False)
+ monkeypatch.setattr(mode, "unserved_profiles", lambda: ["other_profile"])
+ lines = mode.recorded_standalone_warning_lines()
+ assert any("STANDALONE" in line for line in lines)
+ assert any("real standalone reason" in line for line in lines)
+