Skip to content

The agent that grows with you

fix(cli): guard the post-update fleet check against PID reuse

chelsealong5d7ed70eef85
diff --git a/hermes_cli/update_receipt.py b/hermes_cli/update_receipt.py
index 8989bdb3a23..9a0f85f602b 100644
--- a/hermes_cli/update_receipt.py
+++ b/hermes_cli/update_receipt.py
@@ -320,7 +320,7 @@ def collect_fleet_versions(
expected_sha = None
try:
- from gateway.status import _pid_exists, read_runtime_status
+ from gateway.status import read_runtime_status, runtime_status_pid_is_live
from hermes_cli.profiles import (
_get_default_hermes_home,
_get_profiles_root,
@@ -382,13 +382,15 @@ def collect_fleet_versions(
pid = int(pid)
except (TypeError, ValueError):
continue
- if not _pid_exists(pid):
- # Dead PID: a DOWN row only when this exact pid was alive at
- # update start AND the record still claims a running state —
- # "the restart phase stopped it and nothing came back."
- # Everything else (clean stop, startup failure, stale record
- # from a long-dead gateway) keeps the historical no-row
- # behavior so the feature's rollout can't false-positive.
+ if not runtime_status_pid_is_live(record):
+ # Dead PID (or a live PID recycled by an unrelated process
+ # during the update's own churn — #93258): a DOWN row only
+ # when this exact pid was alive at update start AND the
+ # record still claims a running state — "the restart phase
+ # stopped it and nothing came back." Everything else (clean
+ # stop, startup failure, stale record from a long-dead
+ # gateway) keeps the historical no-row behavior so the
+ # feature's rollout can't false-positive.
gw_state = record.get("gateway_state")
if (
pid in _pre_restart
diff --git a/tests/hermes_cli/test_fleet_matrix_down_state.py b/tests/hermes_cli/test_fleet_matrix_down_state.py
index b9f8c37c5b9..8c403e46bf6 100644
--- a/tests/hermes_cli/test_fleet_matrix_down_state.py
+++ b/tests/hermes_cli/test_fleet_matrix_down_state.py
@@ -71,6 +71,30 @@ def test_cleanly_stopped_gateway_is_not_down(monkeypatch, tmp_path):
assert ur.collect_fleet_versions(pre_restart_pids=[_DEAD_PID]) == []
+def test_recycled_pid_is_not_reported_stale(monkeypatch, tmp_path):
+ """A dead gateway's PID reused by an unrelated process (#93258) must not
+ be reported STALE just because *some* process now answers to that PID.
+ """
+ from gateway.status import _get_process_start_time
+
+ reused_pid = os.getpid()
+ wrong_start_time = (_get_process_start_time(reused_pid) or 0) + 12345
+ _setup(
+ monkeypatch,
+ tmp_path,
+ {
+ "pid": reused_pid,
+ "start_time": wrong_start_time,
+ "gateway_state": "running",
+ "code_sha": "OLDSHA",
+ "kind": "hermes-gateway",
+ },
+ )
+ fleet = ur.collect_fleet_versions(pre_restart_pids=[reused_pid])
+ assert len(fleet) == 1
+ assert fleet[0]["state"] == "down"
+
+
def test_live_gateway_rows_unchanged(monkeypatch, tmp_path):
"""The live-pid path is untouched by the down-state addition."""
_setup(