Skip to content

The agent that grows with you

fix(telemetry): smoke stops only the probe home's verified live gateway

Teknium7191be2fc52b
diff --git a/scripts/smoke_nemo_relay_shared_metrics.py b/scripts/smoke_nemo_relay_shared_metrics.py
index 8b5ce96154e..c4b07a4e188 100644
--- a/scripts/smoke_nemo_relay_shared_metrics.py
+++ b/scripts/smoke_nemo_relay_shared_metrics.py
@@ -258,31 +258,25 @@ def _arguments() -> argparse.Namespace:
def _stop_auto_started_gateway(home: Path, timeout: float = 60.0) -> None:
"""Stop the gateway the one-shot chat auto-started for ``home`` and wait for it to exit.
- Left alone it lingers for ``gateway.unmanaged_idle_exit_seconds`` after every smoke run. Stopping
- it before the package export also settles its asynchronous rows (delivery, first-reply latency),
- so SQLite and the export see the same counters.
+ Left alone it lingers for ``gateway.unmanaged_idle_exit_seconds`` after every smoke run. Only the
+ live process the gateway's own identity check proves is ``home``'s gateway (start time, gateway
+ command line, home) is signalled: a stale ``gateway.pid`` whose PID was recycled is left alone.
"""
import psutil
+ from gateway.status import live_gateway_pid_for_home
- try:
- record = json.loads((home / "gateway.pid").read_text(encoding="utf-8-sig"))
- except (OSError, ValueError):
- return
- if (
- not isinstance(record, dict)
- or record.get("kind") != "hermes-gateway"
- or Path(str(record.get("hermes_home"))).resolve() != home.resolve()
- ):
+ pid = live_gateway_pid_for_home(home)
+ if pid is None:
return
try:
- gateway = psutil.Process(int(record["pid"]))
+ gateway = psutil.Process(pid) # binds the create time: terminate() refuses a reused PID
gateway.terminate()
gateway.wait(timeout=timeout)
except psutil.NoSuchProcess:
return
except psutil.TimeoutExpired as exc:
raise AssertionError(
- f"Auto-started gateway {record['pid']} did not exit within {timeout:.0f}s"
+ f"Auto-started gateway {pid} did not exit within {timeout:.0f}s"
) from exc
@@ -739,6 +733,9 @@ def _validate_packages(
def main() -> int:
args = _arguments()
hermes_repo = args.hermes_repo.resolve()
+ # The gateway identity check must come from the tree under test, not whatever tree the venv's
+ # editable install points at.
+ sys.path.insert(0, str(hermes_repo))
relay_python = args.relay_python.resolve() if args.relay_python else None
hermes = _resolve_hermes_executable(hermes_repo)
if relay_python is not None and not any(
diff --git a/tests/scripts/test_smoke_nemo_relay_shared_metrics.py b/tests/scripts/test_smoke_nemo_relay_shared_metrics.py
index 7cf6834d7a9..9bfcf2c7865 100644
--- a/tests/scripts/test_smoke_nemo_relay_shared_metrics.py
+++ b/tests/scripts/test_smoke_nemo_relay_shared_metrics.py
@@ -1,9 +1,12 @@
"""Tests for the shared-metrics smoke artifact."""
+import json
+import subprocess
from pathlib import Path
import pytest
+from gateway import status
from hermes_cli.observability import shared_metrics_contract as contract
from hermes_cli.observability.shared_metrics_update import update_receipt_fields
from scripts import smoke_nemo_relay_shared_metrics as smoke
@@ -53,6 +56,31 @@ def test_smoke_expects_only_contract_metrics_with_their_current_fields():
assert set(smoke.EXPECTED_STARTUP_SURFACES) <= contract.STARTUP_SURFACES
[email protected]("posix") # flock-held gateway lock
[email protected]("start_time_shift", [0, 10_000], ids=["live-non-gateway", "recycled-pid"])
+def test_gateway_stop_spares_a_process_that_is_not_the_homes_live_gateway(tmp_path, start_time_shift):
+ """A held lock and a gateway record naming this home do not make the live PID a gateway."""
+ import fcntl
+
+ decoy = subprocess.Popen(["sleep", "300"])
+ try:
+ record = json.dumps({
+ "pid": decoy.pid, "kind": "hermes-gateway", "argv": ["hermes", "gateway", "run"],
+ "start_time": status._get_process_start_time(decoy.pid) + start_time_shift,
+ "hermes_home": str(tmp_path),
+ })
+ (tmp_path / "gateway.pid").write_text(record)
+ with open(tmp_path / "gateway.lock", "w") as lock:
+ lock.write(record)
+ lock.flush()
+ fcntl.flock(lock.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
+ smoke._stop_auto_started_gateway(tmp_path, timeout=5)
+ assert decoy.poll() is None
+ finally:
+ decoy.kill()
+ decoy.wait()
+
+
def test_smoke_update_expectations_match_the_receipt_projection():
run, stage_rows = update_receipt_fields(smoke.UPDATE_RECEIPT)