Skip to content

The agent that grows with you

test: host singleton invariants — one host lock, attach, stale record ignored

teknium1837b905168dc
diff --git a/tests/gateway/test_host_rendezvous.py b/tests/gateway/test_host_rendezvous.py
new file mode 100644
index 00000000000..a0265f5d0b7
--- /dev/null
+++ b/tests/gateway/test_host_rendezvous.py
@@ -0,0 +1,89 @@
+"""Host-wide singleton invariants (multiplex-only): one lock per host, staleness is proved."""
+
+import json
+import os
+import subprocess
+import sys
+import time
+from pathlib import Path
+
+import pytest
+
+from gateway import host_rendezvous as hr
+
+_CHILD = """
+import json, os, sys, time
+sys.path.insert(0, {tree!r})
+from gateway.status import acquire_gateway_runtime_lock
+from gateway import host_rendezvous as hr
+print(json.dumps({{
+ "per_home": acquire_gateway_runtime_lock(),
+ "host": hr.acquire_host_lock(hr.ROLE_GATEWAY),
+}}), flush=True)
+time.sleep(60)
+"""
+
+
+def host_dir(tmp_path, monkeypatch):
+ monkeypatch.setenv("HERMES_GATEWAY_LOCK_DIR", str(tmp_path / "locks"))
+ yield tmp_path
+ hr.release_host_lock(hr.ROLE_GATEWAY)
+ hr.release_host_lock(hr.ROLE_SERVE)
+
+
+def test_two_homes_take_their_own_per_home_lock_but_only_one_host_lock(host_dir, monkeypatch):
+ """The per-home lock is per HERMES_HOME (N profiles = N locks); the host lock is not.
+
+ This is the whole point of the host layer: before it, a second profile's gateway took its
+ own ``gateway.lock`` and nothing on the machine noticed.
+ """
+ tree = str(Path(__file__).resolve().parents[2])
+ home_a, home_b = host_dir / "home_a", host_dir / "home_b"
+ for home in (home_a, home_b):
+ home.mkdir()
+
+ env = {**os.environ, "HERMES_HOME": str(home_a), "HERMES_GATEWAY_LOCK_DIR": str(host_dir / "locks")}
+ child = subprocess.Popen(
+ [sys.executable, "-c", _CHILD.format(tree=tree)], env=env, cwd=tree,
+ stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
+ assert child.stdout is not None
+ try:
+ deadline = time.time() + 60
+ line = ""
+ while not line.strip() and time.time() < deadline:
+ line = child.stdout.readline()
+ first = json.loads(line)
+ assert first == {"per_home": True, "host": True}
+
+ monkeypatch.setenv("HERMES_HOME", str(home_b))
+ from gateway import status
+
+ assert status.acquire_gateway_runtime_lock() is True, "second home must get its OWN lock"
+ assert hr.acquire_host_lock(hr.ROLE_GATEWAY) is False, "host lock is not per-home"
+ finally:
+ child.kill()
+ child.wait(timeout=10)
+ from gateway import status
+
+ status.release_gateway_runtime_lock()
+
+
+ "pid,create_time",
+ [(2**22 - 1, 1.0), (os.getpid(), 1.0)],
+ ids=["dead-pid", "creation-time-mismatch"],
+)
+def test_stale_record_is_never_attachable(host_dir, pid, create_time):
+ """A dead PID and a live PID from another incarnation are both stale — attaching to either
+ dials whatever now owns that port."""
+ record = hr.HostRecord(
+ role=hr.ROLE_SERVE, pid=pid, create_time=create_time, host="127.0.0.1", port=9119,
+ protocol_version=hr.HOST_PROTOCOL_VERSION, token_fingerprint="", profiles=("default",),
+ updated_at="2026-01-01T00:00:00+00:00")
+ hr.record_path(hr.ROLE_SERVE).parent.mkdir(parents=True, exist_ok=True)
+ hr.record_path(hr.ROLE_SERVE).write_text(json.dumps(record.to_json()), encoding="utf-8")
+
+ assert hr.record_is_stale(record) is True
+ assert hr.read_record(hr.ROLE_SERVE) is None
+ assert hr.read_record(hr.ROLE_SERVE, include_stale=True) is not None
diff --git a/tests/hermes_cli/test_serve_host_attach.py b/tests/hermes_cli/test_serve_host_attach.py
new file mode 100644
index 00000000000..b7ad64d09f5
--- /dev/null
+++ b/tests/hermes_cli/test_serve_host_attach.py
@@ -0,0 +1,60 @@
+"""A second `hermes serve` attaches to the host backend instead of binding a second port."""
+
+import json
+import os
+from types import SimpleNamespace
+
+import pytest
+
+from gateway import host_rendezvous as hr
+from hermes_cli.main_dashboard import _attach_to_host_backend
+
+
+def _args(**over):
+ base = dict(host="127.0.0.1", port=9200, no_open=True, isolated=False, open_profile="")
+ return SimpleNamespace(**{**base, **over})
+
+
+def host_dir(tmp_path, monkeypatch):
+ monkeypatch.setenv("HERMES_GATEWAY_LOCK_DIR", str(tmp_path / "locks"))
+ monkeypatch.delenv("HERMES_DESKTOP", raising=False)
+ return tmp_path
+
+
+def _publish(create_time, pid=None):
+ record = hr.HostRecord(
+ role=hr.ROLE_SERVE, pid=pid if pid is not None else os.getpid(), create_time=create_time,
+ host="127.0.0.1", port=9119, protocol_version=hr.HOST_PROTOCOL_VERSION,
+ token_fingerprint="", profiles=("default", "alpha"),
+ updated_at="2026-01-01T00:00:00+00:00")
+ path = hr.record_path(hr.ROLE_SERVE)
+ path.parent.mkdir(parents=True, exist_ok=True)
+ path.write_text(json.dumps(record.to_json()), encoding="utf-8")
+
+
+def test_second_serve_attaches_to_the_live_host_backend(host_dir, capsys):
+ """A live record (real pid + its real creation time) ends the second launch at exit 0 —
+ it never reaches the bind."""
+ _publish(hr.process_create_time())
+
+ with pytest.raises(SystemExit) as exc:
+ _attach_to_host_backend(_args(), headless_backend=True)
+
+ assert exc.value.code == 0
+ assert "port 9119" in capsys.readouterr().out
+
+
+def test_stale_record_is_ignored_and_the_launch_proceeds(host_dir):
+ """A record whose creation time does not match the live PID is a recycled PID, not a
+ backend: the launch must fall through and bind, never attach."""
+ _publish(1.0)
+
+ assert _attach_to_host_backend(_args(), headless_backend=True) is None
+
+
+def test_isolated_never_attaches(host_dir):
+ """`--isolated` is load-bearing for Desktop's SSH backend ownership proof."""
+ _publish(hr.process_create_time())
+
+ assert _attach_to_host_backend(_args(isolated=True), headless_backend=True) is None