Skip to content

The agent that grows with you

fix(update): verify fleet state fallback gateway identity

KoNit-K0eaa51674f98
diff --git a/hermes_cli/update_receipt.py b/hermes_cli/update_receipt.py
index 85135e712a0..495f371165d 100644
--- a/hermes_cli/update_receipt.py
+++ b/hermes_cli/update_receipt.py
@@ -297,7 +297,11 @@ def collect_fleet_versions(*, pre_restart_pids: Optional[list[int]] = None) -> l
results: list[dict[str, Any]] = []
expected_sha = _code_identity(refresh=True).get("sha")
try:
- from gateway.status import read_runtime_status, runtime_status_pid_is_live
+ from gateway.status import (
+ live_gateway_pid_for_home,
+ read_runtime_status,
+ runtime_status_pid_is_live,
+ )
for profile, home in _profile_homes():
sock = _socket_identity(home)
@@ -313,11 +317,20 @@ def collect_fleet_versions(*, pre_restart_pids: Optional[list[int]] = None) -> l
pid = int(record.get("pid"))
except (TypeError, ValueError):
continue
- if runtime_status_pid_is_live(record):
+ # A state file is only a fallback claim. Its SHA is evidence about
+ # its own PID only when the profile's canonical identity resolver
+ # verifies that same live gateway.
+ if live_gateway_pid_for_home(home) == pid:
results.append(
_fleet_row(profile, pid, record.get("code_sha"), record.get("code_version"), expected_sha)
)
continue
+ # A live non-gateway (or a gateway for another profile) can write a
+ # plausible state file. Keep the fail-open visibility row, but never
+ # let that file's SHA classify the process as current or stale.
+ if runtime_status_pid_is_live(record):
+ results.append(_fleet_row(profile, pid, None, record.get("code_version"), None))
+ continue
# Dead PID (or a live PID recycled by an unrelated process during the update's own
# churn): 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."
diff --git a/tests/hermes_cli/test_update_receipt.py b/tests/hermes_cli/test_update_receipt.py
index e7d1b2e778f..cc9f6e73203 100644
--- a/tests/hermes_cli/test_update_receipt.py
+++ b/tests/hermes_cli/test_update_receipt.py
@@ -270,8 +270,14 @@ class TestFleetClassification:
"""Run collect_fleet_versions against one fake default profile."""
home = tmp_path / "fleet_home"
home.mkdir()
+ gateway_record = {
+ "gateway_state": "running",
+ "kind": "hermes-gateway",
+ "argv": ["hermes", "gateway", "run"],
+ **record,
+ }
(home / "gateway_state.json").write_text(
- json.dumps(record), encoding="utf-8"
+ json.dumps(gateway_record), encoding="utf-8"
)
monkeypatch.setattr(
"hermes_cli.build_info.get_code_identity",
@@ -285,9 +291,41 @@ class TestFleetClassification:
"hermes_cli.profiles._get_profiles_root",
lambda: tmp_path / "nonexistent_profiles_root",
)
- monkeypatch.setattr("gateway.status._pid_exists", lambda pid: True)
+ monkeypatch.setattr(ur, "_socket_identity", lambda home: None)
+ monkeypatch.setattr(
+ "gateway.status.live_gateway_pid_for_home",
+ lambda candidate_home: gateway_record["pid"],
+ )
return ur.collect_fleet_versions()
+ def test_live_non_gateway_state_writer_is_unknown(self, monkeypatch, tmp_path):
+ """A state file written by this non-gateway pytest process proves no gateway is current."""
+ from gateway.status import write_runtime_status
+
+ home = tmp_path / "fleet_home"
+ home.mkdir()
+ monkeypatch.setenv("HERMES_HOME", str(home))
+ monkeypatch.setattr(
+ "hermes_cli.build_info.get_code_identity",
+ lambda refresh=False: {"sha": "a" * 40, "short_sha": "a" * 8,
+ "version": "1.0", "source": "git"},
+ )
+ monkeypatch.setattr(
+ "hermes_cli.profiles._get_default_hermes_home", lambda: home
+ )
+ monkeypatch.setattr(
+ "hermes_cli.profiles._get_profiles_root",
+ lambda: tmp_path / "nonexistent_profiles_root",
+ )
+
+ write_runtime_status(gateway_state="running")
+
+ fleet = ur.collect_fleet_versions()
+
+ assert len(fleet) == 1
+ assert fleet[0]["pid"] == os.getpid()
+ assert fleet[0]["state"] == "unknown"
+
def test_current_gateway(self, monkeypatch, tmp_path):
sha = "a" * 40
fleet = self._fleet_with(
@@ -330,6 +368,36 @@ class TestFleetClassification:
monkeypatch.setattr("gateway.status._pid_exists", lambda pid: False)
assert ur.collect_fleet_versions() == []
+ def test_live_runtime_record_without_verified_gateway_is_unknown(
+ self, monkeypatch, tmp_path
+ ):
+ """A live status record alone cannot make a profile current."""
+ home = tmp_path / "fleet_home"
+ home.mkdir()
+ monkeypatch.setattr(
+ ur,
+ "_code_identity",
+ lambda refresh=False: {"sha": "a" * 40},
+ )
+ record = {
+ "pid": 4242,
+ "gateway_state": "running",
+ "kind": "hermes-gateway",
+ "argv": ["hermes", "gateway", "run"],
+ "code_sha": "a" * 40,
+ "code_version": "1.0",
+ }
+ monkeypatch.setattr(ur, "_profile_homes", lambda: [("default", home)])
+ monkeypatch.setattr(ur, "_socket_identity", lambda home: None)
+ monkeypatch.setattr("gateway.status.read_runtime_status", lambda path: record)
+ monkeypatch.setattr("gateway.status.runtime_status_pid_is_live", lambda record: True)
+ monkeypatch.setattr("gateway.status.live_gateway_pid_for_home", lambda home: None)
+
+ fleet = ur.collect_fleet_versions()
+
+ assert len(fleet) == 1
+ assert fleet[0]["state"] == "unknown"
+
def test_matrix_returns_true_only_on_stale(self, capsys):
assert ur.print_fleet_version_matrix([]) is False
ok = ur.print_fleet_version_matrix(