The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
feat(gateway): host-wide singleton lock + rendezvous record
teknium19eb90b0a0385
diff --git a/gateway/host_rendezvous.py b/gateway/host_rendezvous.pynew file mode 100644index 00000000000..735280e36f7--- /dev/null+++ b/gateway/host_rendezvous.py@@ -0,0 +1,328 @@+"""Host-wide singleton rendezvous: one lock + one record per ROLE per OS user.++Multiplex-only (Teknium ruling): exactly ONE ``hermes serve`` and ONE ``hermes gateway run``+per host, each multiplexing every profile. The per-``HERMES_HOME`` gateway lock/PID files+(``gateway.status``) cannot express that — N profiles are N homes, so N processes each take+their own flock and none of them ever sees the others. This module adds the missing layer:++* a **host lock** (flock/``msvcrt``) held for the lifetime of the winning process, and+* a **rendezvous record** the winner publishes so a second invocation can find it, prove it+ is the same live process, and ATTACH instead of binding a second port.++Both live in :func:`gateway.status._get_lock_dir` — the only cross-profile lock root already+in the tree (``$HERMES_GATEWAY_LOCK_DIR`` else ``$XDG_STATE_HOME/hermes/gateway-locks``),+which scopes to the **OS user**. That is the correct granularity: separate OS users have+separate ``$HOME``s, separate ``~/.hermes`` profile roots, separate ports-by-convention and+separate credentials, so "one per host" means "one per host per OS user".++**Staleness is proved, never assumed.** A record carries ``(pid, createTime)``; a record whose+PID is dead, or whose PID is alive with a different process creation time (PID reuse), is+STALE and is ignored — an attaching client must never dial a recycled PID's port.++**Relationship to ``spawn-ledger.json``** (``hermes_cli/process_identity.py``): the ledger stays+the append-only machine roster of every long-lived Hermes process (Desktop's attach ladder reads+it) and is still written unchanged. It cannot be the host record: it has no lock, no+single-writer semantics, no removal on clean exit, and no place to publish a protocol version or+an authentication handle. The record here is authoritative for "who owns this host role"; the+ledger remains authoritative for "what is running". Both are written, and this module reuses the+ledger's ``(pid, create_time)`` liveness proof rather than inventing a second one.+"""++from __future__ import annotations++import contextlib+import hashlib+import json+import logging+import os+from dataclasses import dataclass+from datetime import datetime, timezone+from pathlib import Path+from typing import Any, Optional, Sequence++from utils import atomic_json_write++logger = logging.getLogger(__name__)++#: Bumped when the record's shape or the attach handshake changes incompatibly. A reader that+#: does not recognise the version refuses to attach instead of guessing.+HOST_PROTOCOL_VERSION = 1++ROLE_GATEWAY = "gateway"+ROLE_SERVE = "serve"+_ROLES = (ROLE_GATEWAY, ROLE_SERVE)++# Open lock handles, keyed by role: the OS releases the flock when this process dies, which is+# what makes a crashed owner's host lock re-acquirable without a reaper.+_lock_handles: dict[str, Any] = {}+++@dataclass(frozen=True)+class HostRecord:+ """A published host-role owner. ``profiles`` is the SERVED set, not the launch profile."""++ role: str+ pid: int+ create_time: Optional[float]+ host: str+ port: Optional[int]+ protocol_version: int+ token_fingerprint: str+ profiles: tuple[str, ...]+ updated_at: str++ def to_json(self) -> dict[str, Any]:+ return {+ "role": self.role,+ "pid": self.pid,+ "createTime": self.create_time,+ "host": self.host,+ "port": self.port,+ "protocolVersion": self.protocol_version,+ "tokenFingerprint": self.token_fingerprint,+ "profiles": list(self.profiles),+ "updatedAt": self.updated_at,+ }++ @classmethod+ def from_json(cls, payload: Any) -> Optional["HostRecord"]:+ if not isinstance(payload, dict):+ return None+ pid = payload.get("pid")+ role = payload.get("role")+ if not isinstance(pid, int) or pid <= 0 or role not in _ROLES:+ return None+ create = payload.get("createTime")+ port = payload.get("port")+ profiles = payload.get("profiles")+ version = payload.get("protocolVersion")+ return cls(+ role=role,+ pid=pid,+ create_time=float(create) if isinstance(create, (int, float)) else None,+ host=str(payload.get("host") or ""),+ port=int(port) if isinstance(port, int) and 0 < port <= 65535 else None,+ protocol_version=version if isinstance(version, int) else 0,+ token_fingerprint=str(payload.get("tokenFingerprint") or ""),+ profiles=tuple(str(p) for p in profiles if isinstance(p, str)) if isinstance(profiles, list) else (),+ updated_at=str(payload.get("updatedAt") or ""),+ )+++def host_state_dir() -> Path:+ """Per-OS-USER rendezvous dir (shared by every profile of this user)."""+ from gateway.status import _get_lock_dir++ return _get_lock_dir()+++def _validated_role(role: str) -> str:+ if role not in _ROLES:+ raise ValueError(f"unknown host role: {role!r}")+ return role+++def record_path(role: str) -> Path:+ return host_state_dir() / f"host-{_validated_role(role)}.json"+++def lock_path(role: str) -> Path:+ return host_state_dir() / f"host-{_validated_role(role)}.lock"+++def token_path(role: str) -> Path:+ return host_state_dir() / f"host-{_validated_role(role)}.token"+++def token_fingerprint(token: str) -> str:+ """Short, non-reversible handle for a session token (safe to publish in the record)."""+ return hashlib.sha256(token.encode("utf-8", "replace")).hexdigest()[:16] if token else ""+++def process_create_time(pid: Optional[int] = None) -> Optional[float]:+ """Creation time of ``pid`` (default: this process); ``None`` when unknowable."""+ from hermes_cli.process_identity import _process_create_time++ return _process_create_time(pid)+++def _pid_incarnation_matches(pid: int, create_time: Optional[float]) -> Optional[bool]:+ """Reuse the spawn ledger's proof: True/False when provable, ``None`` when it cannot say."""+ from hermes_cli.process_identity import _pid_alive_matches++ return _pid_alive_matches(pid, create_time)+++def record_is_stale(record: Optional[HostRecord]) -> bool:+ """A record nobody may attach to: absent, unknown protocol, dead PID, or PID reuse.++ ``None`` from the liveness probe (no psutil, permission denied) is NOT stale — refusing to+ attach on an unprovable answer is the safe direction for a *singleton*, but declaring the+ owner dead on one would let a second process bind a second port, which is the bug.+ """+ if record is None:+ return True+ if record.protocol_version != HOST_PROTOCOL_VERSION:+ return True+ return _pid_incarnation_matches(record.pid, record.create_time) is False+++def read_record(role: str, *, include_stale: bool = False) -> Optional[HostRecord]:+ """Published record for ``role``; ``None`` when absent, corrupt or (by default) stale."""+ try:+ raw = record_path(role).read_text(encoding="utf-8")+ except (OSError, UnicodeDecodeError):+ return None+ try:+ record = HostRecord.from_json(json.loads(raw))+ except (ValueError, TypeError):+ return None+ if record is None:+ return None+ return record if include_stale or not record_is_stale(record) else None+++def read_token(role: str) -> str:+ """Owner-written session token for ``role`` (``""`` when absent/unreadable).++ The file is 0600, so reading it proves the caller is the same OS user that owns the record —+ which is exactly the authority boundary the host lock is scoped to. This is the handle an+ attaching client uses when the backend is auth-gated and therefore withholds its token from+ an unauthenticated ``GET /``.+ """+ try:+ return token_path(role).read_text(encoding="utf-8").strip()+ except (OSError, UnicodeDecodeError):+ return ""+++def record_token_is_consistent(record: HostRecord) -> bool:+ """Does the on-disk token still hash to the record's fingerprint?++ A record published without a token (the gateway) has an empty fingerprint and is consistent+ by definition. A mismatch means the record and the token file come from different+ incarnations (a torn restart) — discovery must not attach with a token the owner rejects.+ """+ if not record.token_fingerprint:+ return True+ return token_fingerprint(read_token(record.role)) == record.token_fingerprint+++def _write_private_text(path: Path, text: str) -> None:+ """Create/replace ``path`` with 0600 content (owner-only), atomically via tmp + replace."""+ tmp = path.with_name(path.name + ".tmp")+ fd = os.open(str(tmp), os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600)+ try:+ with os.fdopen(fd, "w", encoding="utf-8") as handle:+ handle.write(text)+ handle.flush()+ with contextlib.suppress(OSError):+ os.fsync(handle.fileno())+ except BaseException:+ with contextlib.suppress(OSError):+ os.unlink(str(tmp))+ raise+ os.replace(str(tmp), str(path))+++def acquire_host_lock(role: str) -> bool:+ """Take the host-wide lock for ``role``. Idempotent; False when another process holds it."""+ role = _validated_role(role)+ if _lock_handles.get(role) is not None:+ return True+ from gateway.status import _try_acquire_file_lock++ path = lock_path(role)+ try:+ path.parent.mkdir(parents=True, exist_ok=True)+ handle = open(path, "a+", encoding="utf-8")+ except OSError:+ logger.debug("host %s lock could not be opened at %s", role, path, exc_info=True)+ return False+ if not _try_acquire_file_lock(handle):+ with contextlib.suppress(OSError):+ handle.close()+ return False+ _lock_handles[role] = handle+ return True+++def release_host_lock(role: str) -> None:+ """Release the host lock for ``role`` when this process holds it."""+ handle = _lock_handles.pop(_validated_role(role), None)+ if handle is None:+ return+ from gateway.status import _release_file_lock++ _release_file_lock(handle)+ with contextlib.suppress(OSError):+ handle.close()+++def owns_host_lock(role: str) -> bool:+ """True when THIS process holds the host lock for ``role`` (re-probing our own flock lies)."""+ return _lock_handles.get(_validated_role(role)) is not None+++def publish_record(+ role: str,+ *,+ host: str = "",+ port: Optional[int] = None,+ profiles: Sequence[str] = (),+ token: Optional[str] = None,+) -> Optional[HostRecord]:+ """Publish this process as the host owner of ``role``. ``None`` when the write failed.++ ``token`` (serve) is persisted 0600 next to the record and only its fingerprint is published.+ """+ role = _validated_role(role)+ record = HostRecord(+ role=role,+ pid=os.getpid(),+ create_time=process_create_time(),+ host=str(host or ""),+ port=int(port) if isinstance(port, int) and port > 0 else None,+ protocol_version=HOST_PROTOCOL_VERSION,+ token_fingerprint=token_fingerprint(token or ""),+ profiles=tuple(str(p) for p in profiles),+ updated_at=datetime.now(timezone.utc).isoformat(),+ )+ try:+ record_path(role).parent.mkdir(parents=True, exist_ok=True)+ if token:+ _write_private_text(token_path(role), token)+ atomic_json_write(record_path(role), record.to_json(), mode=0o600)+ except OSError:+ logger.debug("host %s record publish failed", role, exc_info=True)+ return None+ return record+++def clear_record(role: str) -> None:+ """Remove this process's record + token on clean exit (never another owner's)."""+ role = _validated_role(role)+ existing = read_record(role, include_stale=True)+ if existing is not None and existing.pid != os.getpid():+ return+ for path in (record_path(role), token_path(role)):+ with contextlib.suppress(OSError):+ path.unlink(missing_ok=True)+++def served_profiles() -> tuple[str, ...]:+ """Profiles this process multiplexes; ``()`` when the roster cannot be read."""+ try:+ from hermes_cli.profiles import profiles_to_serve++ return tuple(name for name, _ in profiles_to_serve(multiplex=True))+ except Exception:+ logger.debug("served profile roster unavailable", exc_info=True)+ return ()+++def describe(record: HostRecord) -> str:+ """One-line human description used by attach messages and conflict logs."""+ where = f"{record.host or '127.0.0.1'}:{record.port}" if record.port else "no bound port"+ profiles = ", ".join(record.profiles) if record.profiles else "unknown"+ return f"PID {record.pid} ({where}; profiles: {profiles})"diff --git a/gateway/run.py b/gateway/run.pyindex e93dc6455fa..163d9957f7b 100644--- a/gateway/run.py+++ b/gateway/run.py@@ -5170,9 +5170,36 @@ def _start_gateway_claim_pid_file() -> bool:return Falseatexit.register(remove_pid_file)atexit.register(release_gateway_runtime_lock)+ _claim_host_gateway_role()return True+def _claim_host_gateway_role() -> None:+ """Take the HOST-wide gateway lock alongside the per-home one and publish the record.++ Observe-only in this step: the per-home lock above still decides whether this process runs,+ so a host with two gateways (the shape the multiplex-only ruling forbids) starts as it always+ did and says so in the log. Flipping this into a refusal is a separate, reviewable change.+ """+ import atexit+ from gateway import host_rendezvous as hr++ try:+ if hr.acquire_host_lock(hr.ROLE_GATEWAY):+ hr.publish_record(hr.ROLE_GATEWAY, profiles=hr.served_profiles())+ atexit.register(hr.clear_record, hr.ROLE_GATEWAY)+ atexit.register(hr.release_host_lock, hr.ROLE_GATEWAY)+ return+ owner = hr.read_record(hr.ROLE_GATEWAY)+ logger.warning(+ "Another gateway already owns this host (%s). Multiplex-only expects exactly one "+ "gateway per host; starting anyway (observe-only).",+ hr.describe(owner) if owner else "owner unknown",+ )+ except Exception:+ logger.debug("host gateway rendezvous failed", exc_info=True)++async def _start_gateway_start_control_socket(runner):"""Start the gateway control socket (identify/status/pause-for-update); None when unavailable."""import atexitdiff --git a/gateway/status.py b/gateway/status.pyindex 98374bd5770..5e8267020cf 100644--- a/gateway/status.py+++ b/gateway/status.py@@ -306,7 +306,14 @@ def _get_runtime_status_path() -> Path:def _get_lock_dir() -> Path:- """Machine-local dir for token-scoped gateway locks; ``HERMES_GATEWAY_LOCK_DIR`` overrides."""+ """Cross-profile rendezvous dir for machine-local locks; ``HERMES_GATEWAY_LOCK_DIR`` overrides.++ Scope is the **OS user**, not the kernel host: separate users have separate ``$HOME``s,+ separate ``~/.hermes`` profile roots and separate credentials, so "one gateway per host"+ means "one per host per OS user". Holds the token-scoped locks (:func:`acquire_scoped_lock`)+ and the host-role lock + rendezvous record (``gateway/host_rendezvous.py``); the per-home+ ``gateway.pid``/``gateway.lock`` above deliberately stay under each profile's HERMES_HOME.+ """override = os.getenv("HERMES_GATEWAY_LOCK_DIR")if override:return Path(override)diff --git a/hermes_cli/main.py b/hermes_cli/main.pyindex fe96b0348be..4026861eb2e 100644--- a/hermes_cli/main.py+++ b/hermes_cli/main.py@@ -774,6 +774,7 @@ from hermes_cli.main_platform_setup import (cmd_whatsapp_cloud,)from hermes_cli.main_dashboard import (+ _attach_to_host_backend,_finalize_update_output,_find_stale_dashboard_pids,_install_hangup_protection,@@ -2702,6 +2703,7 @@ def cmd_dashboard(args):_ssh_owner_nonce = _dashboard_validate_serve_args(args, _headless_backend, _token_file)_dashboard_sanitize_desktop_env(_headless_backend)+ _attach_to_host_backend(args, _headless_backend)_route_named_profile_dashboard(args, _headless_backend, _ssh_owner_nonce, _token_file)# Apply the final process/profile policy after dashboard routing, but beforediff --git a/hermes_cli/main_dashboard.py b/hermes_cli/main_dashboard.pyindex 148d0b96597..a6067dec79e 100644--- a/hermes_cli/main_dashboard.py+++ b/hermes_cli/main_dashboard.py@@ -751,15 +751,68 @@ def _is_electron_packaged_web_dist(path: str) -> bool:return "app.asar" in path.replace("\\", "/")+def _host_backend_attachment():+ """Live host serve/dashboard record to attach to, or ``None``.++ Record-based discovery replaces the old bare-TCP probe: "something accepts a connection on+ this port" proved nothing about WHO answers (a foreign service, or a recycled PID's new+ owner). The record carries ``(pid, createTime)`` so liveness is proved against the same+ incarnation, and its token fingerprint must still match the 0600 token file the owner wrote.+ """+ try:+ from gateway import host_rendezvous as hr++ record = hr.read_record(hr.ROLE_SERVE)+ if record is None or not record.port:+ return None+ return record if hr.record_token_is_consistent(record) else None+ except Exception:+ return None+++def _attach_to_host_backend(args, headless_backend: bool) -> None:+ """Multiplex-only: a second `hermes serve`/`dashboard` attaches to the host backend.++ Exactly ONE backend runs per host and multiplexes every profile, so a second invocation —+ for ANY profile, the default included — reports the live one and exits 0 instead of binding+ a second port. ``--isolated`` opts out (Desktop's SSH backend proves ownership with it) and+ Desktop pool backends (HERMES_DESKTOP=1) keep their own lifecycle. Returns normally when no+ live record exists, leaving the re-exec fallback below to run.+ """+ if getattr(args, "isolated", False) or os.environ.get("HERMES_DESKTOP") == "1":+ return+ record = _host_backend_attachment()+ if record is None:+ return++ try:+ from hermes_cli.profiles import get_active_profile_name+ profile = get_active_profile_name()+ except Exception:+ profile = "default"+ wanted = getattr(args, "open_profile", "") or profile+ url = f"http://{record.host or '127.0.0.1'}:{record.port}/?profile={wanted}"++ kind = "backend" if headless_backend else "dashboard"+ print(f"Hermes {kind} already running on this host: PID {record.pid}, port {record.port}.")+ print(f" Managing profile '{wanted}': {url}")+ if not headless_backend and not args.no_open:+ with contextlib.suppress(Exception):+ import webbrowser+ webbrowser.open(url)+ sys.exit(0)++def _route_named_profile_dashboard(args, _headless_backend: bool, _ssh_owner_nonce: str, _token_file: str) -> None:"""Route a named-profile launch to the single MACHINE dashboard (per-request ``?profile=`` scopingmakes one server per profile pure fragmentation).- Already listening → open ``?profile=<name>`` and exit; else re-exec pinned to- ``-p default`` (so ``_apply_profile_override`` can't re-route via the sticky- active_profile file). ``--isolated`` opts out; Desktop pool backends- (HERMES_DESKTOP=1) stay per-profile. Returns normally when no routing applies.+ No-record fallback to :func:`_attach_to_host_backend`, which already attached (and exited)+ when the host publishes a live rendezvous record: re-exec pinned to ``-p default`` (so+ ``_apply_profile_override`` can't re-route via the sticky active_profile file). ``--isolated``+ opts out; Desktop pool backends (HERMES_DESKTOP=1) stay per-profile. Returns normally when no+ routing applies."""try:from hermes_cli.profiles import get_active_profile_name@@ -775,16 +828,6 @@ def _route_named_profile_dashboard():return- url = f"http://{args.host or '127.0.0.1'}:{args.port}/?profile={_launch_profile}"- if _dashboard_listening(args.host, args.port):- print(f"Machine dashboard already running on port {args.port}.")- print(f" Managing profile '{_launch_profile}': {url}")- if not args.no_open:- with contextlib.suppress(Exception):- import webbrowser- webbrowser.open(url)- sys.exit(0)-print(f"Routing to the machine dashboard (profile '{_launch_profile}' "f"preselected). Use --isolated for a dedicated per-profile server."diff --git a/hermes_cli/web_server.py b/hermes_cli/web_server.pyindex e89b78ed094..6d575eb4293 100644--- a/hermes_cli/web_server.py+++ b/hermes_cli/web_server.py@@ -1273,6 +1273,37 @@ def _on_server_started(_best_effort("process-identity registration", _register_identity)+ # Host rendezvous (multiplex-only): the host lock + record that let a SECOND `hermes serve`+ # for any profile find this process and attach instead of binding a second port. Published+ # after the bind so the record carries the real port, and beside — not instead of — the+ # spawn-ledger entry above, which Desktop's attach ladder reads.+ def _publish_host_record() -> None:+ import atexit++ from gateway import host_rendezvous as hr++ if not hr.acquire_host_lock(hr.ROLE_SERVE):+ owner = hr.read_record(hr.ROLE_SERVE)+ _log.warning(+ "Another backend already owns this host (%s); this one bound anyway "+ "(observe-only). Multiplex-only expects exactly one backend per host.",+ hr.describe(owner) if owner else "owner unknown",+ )+ return+ hr.publish_record(+ hr.ROLE_SERVE,+ host=host,+ port=actual_port,+ profiles=hr.served_profiles(),+ # The live session token, so an attaching client of the same OS user can+ # authenticate even when the backend is gated and `GET /` withholds it.+ token=_SESSION_TOKEN,+ )+ atexit.register(hr.clear_record, hr.ROLE_SERVE)+ atexit.register(hr.release_host_lock, hr.ROLE_SERVE)++ _best_effort("host rendezvous publish", _publish_host_record)+_write_dashboard_ready_file(actual_port)# Port-discovery sentinel parsed by the Desktop spawn (matches either# token). Written to fd 1: tui_gateway.server redirects sys.stdout to