Skip to content

The agent that grows with you

fix(gateway): retain failed replacement evidence

fangliquana4f16e3fefdc
diff --git a/hermes_cli/gateway.py b/hermes_cli/gateway.py
index 7f1a45f5dcd..0ae4a523fb4 100644
--- a/hermes_cli/gateway.py
+++ b/hermes_cli/gateway.py
@@ -1271,18 +1271,27 @@ def _wait_for_systemd_service_restart(
new_pid = None
if not new_pid:
new_pid = _systemd_main_pid_from_props(props)
+
+ runtime_state = _read_gateway_runtime_status()
+ try:
+ runtime_pid = int((runtime_state or {}).get("pid", 0) or 0)
+ except (TypeError, ValueError):
+ runtime_pid = 0
if (
- new_pid
- and previous_pid is not None
- and new_pid != previous_pid
+ previous_pid is not None
and replacement_observed is not None
and not replacement_observed
+ and any(
+ candidate_pid > 0 and candidate_pid != previous_pid
+ for candidate_pid in (new_pid or 0, runtime_pid)
+ )
):
replacement_observed.append(True)
if active_state == "active":
if new_pid and (previous_pid is None or new_pid != previous_pid):
- runtime_state = _gateway_runtime_status_for_pid(new_pid)
+ if runtime_pid != new_pid:
+ runtime_state = _gateway_runtime_status_for_pid(new_pid)
gateway_state = (runtime_state or {}).get("gateway_state")
if gateway_state == "running":
print(f"✓ {scope_label} service restarted (PID {new_pid})")
diff --git a/tests/hermes_cli/test_gateway_service.py b/tests/hermes_cli/test_gateway_service.py
index f1593c4b8a8..99f0099f420 100644
--- a/tests/hermes_cli/test_gateway_service.py
+++ b/tests/hermes_cli/test_gateway_service.py
@@ -891,6 +891,35 @@ class TestGatewaySystemServiceRouting:
assert gateway_cli._systemd_restart_wait_timeout() == 155.0
+ def test_wait_records_a_short_lived_failed_replacement(self, monkeypatch):
+ ticks = iter((0.0, 0.0, 2.0))
+ monkeypatch.setattr(gateway_cli.time, "monotonic", lambda: next(ticks))
+ monkeypatch.setattr(gateway_cli.time, "sleep", lambda _seconds: None)
+ monkeypatch.setattr(
+ gateway_cli,
+ "_read_systemd_unit_properties",
+ lambda system=False, properties=None: {
+ "ActiveState": "failed",
+ "MainPID": "0",
+ },
+ )
+ monkeypatch.setattr("gateway.status.get_running_pid", lambda: None)
+ monkeypatch.setattr(
+ gateway_cli,
+ "_read_gateway_runtime_status",
+ lambda: {"pid": 777, "gateway_state": "startup_failed"},
+ )
+ replacement_observed = []
+
+ result = gateway_cli._wait_for_systemd_service_restart(
+ previous_pid=654,
+ timeout=1.0,
+ replacement_observed=replacement_observed,
+ )
+
+ assert result is False
+ assert replacement_observed == [True]
+