Skip to content

The agent that grows with you

fix(gateway): drop argv identity from host rendezvous liveness

kinoko-senpai69316aa66f88
diff --git a/gateway/host_rendezvous.py b/gateway/host_rendezvous.py
index 577f027978a..0c491419ffc 100644
--- a/gateway/host_rendezvous.py
+++ b/gateway/host_rendezvous.py
@@ -262,46 +262,15 @@ def _current_boot_epoch() -> str:
return ""
-def _read_process_cmdline(pid: int) -> str:
- """Command line of ``pid``, or ``""`` when it cannot be read. Never raises."""
- try:
- raw = Path(f"/proc/{pid}/cmdline").read_bytes()
- if raw:
- return raw.replace(b"\x00", b" ").decode("utf-8", "replace")
- except OSError:
- pass
- try:
- import psutil
-
- return " ".join(psutil.Process(int(pid)).cmdline())
- except Exception:
- logger.debug("cmdline unreadable for pid %s", pid, exc_info=True)
- return ""
-
-
-def _cmdline_contradicts_gateway(pid: int) -> bool:
- """True only when the live command line is readable and cannot be a Hermes gateway.
-
- Unreadable cmdline, and this process, are not contradictions: a missing ``/proc`` or a
- test that publishes its own PID must not false-stale a live owner. A recycled PID whose
- start ticks fall inside the drift tolerance (``mpris-proxy`` after reboot) is.
- """
- if pid == os.getpid():
- return False
- cmd = _read_process_cmdline(pid).lower()
- if not cmd:
- return False
- return "hermes" not in cmd and "gateway" not in cmd
-
-
def _record_incarnation_matches(record: HostRecord) -> Optional[bool]:
"""Whether the record still names the same live process incarnation.
New records prefer the canonical start fingerprint: on Linux/WSL it comes from /proc start
ticks and is immune to wall-clock shifts that move psutil.create_time(). Those ticks are
NOT unique across reboots — they reset, and an early-boot PID reused within the drift
- tolerance looks like the dead owner. A published boot epoch, a command line that is not
- a gateway, or (for legacy records) a create_time gap past reboot scale, is a mismatch.
+ tolerance looks like the dead owner. A published boot epoch, or (for legacy records with
+ no epoch) a create_time gap past reboot scale, is a mismatch. Empty epoch is a no-op so
+ macOS and pre-upgrade records are not false-staled. Do not infer identity from argv.
Legacy records without a start fingerprint keep the create-time check.
"""
recorded_epoch = getattr(record, "boot_epoch", "") or ""
@@ -320,8 +289,6 @@ def _record_incarnation_matches(record: HostRecord) -> Optional[bool]:
matched = None if current is None else start_time_fingerprints_match(record.start_time, current)
if matched is not True:
return matched
- if record.role == ROLE_GATEWAY and _cmdline_contradicts_gateway(record.pid):
- return False
if (
record.role == ROLE_GATEWAY
and not recorded_epoch
diff --git a/tests/gateway/test_host_rendezvous.py b/tests/gateway/test_host_rendezvous.py
index aa27d489f3a..1f4a3e46d5d 100644
--- a/tests/gateway/test_host_rendezvous.py
+++ b/tests/gateway/test_host_rendezvous.py
@@ -136,35 +136,43 @@ def test_live_host_record_survives_wall_clock_create_time_drift(host_dir, monkey
@pytest.mark.platforms("posix")
-def test_reused_early_pid_after_reboot_is_not_the_gateway(monkeypatch):
+def test_reused_early_pid_after_reboot_is_not_the_gateway():
"""Boot-relative start ticks reset on reboot and collide within the 2s drift tolerance.
- A legacy record (no bootEpoch) whose live PID is not a Hermes gateway must not keep a
- supervised unit in the transient-refuse loop. PID 1674 / ticks 827 vs 845 is the shape
- that wedged systemd against mpris-proxy.
+ A legacy record (no bootEpoch) with a reboot-scale createTime gap must not keep a
+ supervised unit in the transient-refuse loop. The same tick match with a WSL-scale
+ gap (#117505) stays live.
"""
- record = hr.HostRecord(
- role=hr.ROLE_GATEWAY,
- pid=1674,
- create_time=1_700_000_000.0,
- start_time=827,
- host="",
- port=None,
- protocol_version=hr.HOST_PROTOCOL_VERSION,
- token_fingerprint="",
- profiles=("default",),
- updated_at="2026-01-01T00:00:00+00:00",
- )
- monkeypatch.setattr(hr, "_pid_incarnation_matches", lambda pid, create_time: True)
- monkeypatch.setattr(hr, "_cmdline_contradicts_gateway", lambda pid: True)
- from gateway import status
+ from gateway.status import get_process_start_time
- monkeypatch.setattr(status, "get_process_start_time", lambda pid: 845)
- assert hr.liveness_is_proven(record) is False
- assert hr.record_is_stale(record) is True
+ child = subprocess.Popen(["sleep", "30"])
+ try:
+ start = get_process_start_time(child.pid)
+ live_create = hr.process_create_time(child.pid)
+ assert start is not None and live_create is not None
+ record = hr.HostRecord(
+ role=hr.ROLE_GATEWAY,
+ pid=child.pid,
+ create_time=float(live_create) - 3600.0,
+ start_time=int(start) + 10,
+ host="",
+ port=None,
+ protocol_version=hr.HOST_PROTOCOL_VERSION,
+ token_fingerprint="",
+ profiles=("default",),
+ updated_at="2026-01-01T00:00:00+00:00",
+ )
+ assert hr.liveness_is_proven(record) is False
+ assert hr.record_is_stale(record) is True
+ drifted = dataclasses.replace(record, create_time=float(live_create) - 5.0)
+ assert hr.liveness_is_proven(drifted) is True
+ assert hr.record_is_stale(drifted) is False
+ finally:
+ child.kill()
+ child.wait(timeout=10)
def test_boot_epoch_mismatch_rejects_a_tick_match(host_dir):
record = hr.publish_record(hr.ROLE_GATEWAY, profiles=("default",), home=str(host_dir))
assert record is not None and record.boot_epoch
@@ -174,6 +182,15 @@ def test_boot_epoch_mismatch_rejects_a_tick_match(host_dir):
assert hr.record_is_stale(foreign) is True
+def test_empty_boot_epoch_does_not_stale_a_live_owner(host_dir):
+ record = hr.publish_record(hr.ROLE_GATEWAY, profiles=("default",), home=str(host_dir))
+ assert record is not None
+ empty = dataclasses.replace(record, boot_epoch="")
+ assert hr.liveness_is_proven(empty) is True
+ assert hr.record_is_stale(empty) is False
+
+
@pytest.mark.platforms("posix") # POSIX signal disposition
def test_sigterm_removes_the_record_and_its_live_session_token(host_dir):
"""SIGTERM is the NORMAL stop (systemd stop, docker stop, the update relaunch) and it does not