d1dbb0ac9e
A `gateway.multiplex_profiles` gateway enumerated `profiles/` once at boot, so a profile created afterwards (CLI, dashboard, Desktop, TUI) was never served until `hermes gateway restart`; Desktop and the dashboard gave no reminder, so a new profile's bot simply never connected. The served set is now reconciled at runtime (`gateway/run_profile_reconcile.py`): - `hermes_cli/profiles.py` create/delete ping the multiplexer over its control socket (new `rescan-profiles` verb); a supervised watcher rescans every 30s as the safety net. - A new profile gets its adapters under its own runtime scope from its config/.env (`_start_one_profile_adapters`, same duplicate-credential guard as boot, now seeded with the LIVE secondaries' claims), `served_profiles` in gateway_state.json is updated, MCP discovery + log routing run for it. Other profiles' adapters are never touched. - A served profile whose config.yaml/.env changed is re-scanned so a token added after create builds the adapter; already-live/queued platforms are skipped (no second poller). - A deleted profile (tombstone) has its reconnects cancelled, adapters torn down, pairing/busy bookkeeping and cached agents dropped, and this process's SQLite / memory-store handles released so the deleter's rmtree succeeds. - The in-process cron ticker takes a live enumerator so new profiles' jobs fire. - PUT /api/messaging/platforms/<id>?profile=X returns `hot_served` when a live multiplexer rebuilt X's adapters; Desktop/dashboard skip the restart banner then. - `hermes profile create` confirms hot-serve; the restart reminder stays for a gateway that did not pick the profile up (older build / signal failed).
95 lines
4.8 KiB
Python
95 lines
4.8 KiB
Python
"""Which profiles does the LIVE default multiplexer serve? One answer for every CLI/dashboard surface.
|
|
|
|
``gateway/run_adapters.py::_record_served_profiles`` writes ``served_profiles`` into the default
|
|
home's ``gateway_state.json`` at startup. That record is the truth about the running process; the
|
|
default ``config.yaml`` plus ``GATEWAY_MULTIPLEX_PROFILES`` as seen by the *CLI* process is only a
|
|
guess (``hermes -p coder ...`` loads coder's ``.env``, so an env-only opt-in on the default profile
|
|
is invisible to it, and an allowlist edited after start flips the guess before the restart).
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
from pathlib import Path
|
|
from typing import Optional
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
def live_default_gateway_pid() -> Optional[int]:
|
|
"""PID of the default profile's gateway when its pid record names a live process, else None."""
|
|
from hermes_constants import get_default_hermes_root
|
|
from gateway.status import _pid_exists, _pid_from_record, _read_pid_record
|
|
rec = _read_pid_record(get_default_hermes_root() / "gateway.pid")
|
|
pid = _pid_from_record(rec) if rec else None
|
|
return pid if pid and _pid_exists(pid) else None
|
|
|
|
|
|
def recorded_served_profiles(default_root: Optional[Path] = None) -> Optional[list[str]]:
|
|
"""``served_profiles`` the live default gateway recorded, or None when the key is absent (a record
|
|
from before the multiplexer recorded it, or a stopped/absent gateway). Callers fall back to config
|
|
derivation only on None: an empty list is an authoritative "serves nobody else"."""
|
|
from hermes_constants import get_default_hermes_root
|
|
from gateway.status import read_runtime_status
|
|
if live_default_gateway_pid() is None:
|
|
return None
|
|
runtime = read_runtime_status((default_root or get_default_hermes_root()) / "gateway_state.json")
|
|
served = (runtime or {}).get("served_profiles")
|
|
return [str(p) for p in served] if isinstance(served, list) else None
|
|
|
|
|
|
def multiplexer_served_secondaries() -> list[str]:
|
|
"""Named profiles the live default multiplexer serves (excludes ``default``); empty when none."""
|
|
return [p for p in (recorded_served_profiles() or []) if p and p != "default"]
|
|
|
|
|
|
def served_profile_ingress_urls(profile: Optional[str] = None) -> dict[str, dict[str, str]]:
|
|
"""``{profile: {platform: url}}`` for every secondary inbound-port platform the live multiplexer
|
|
serves on its shared listener (``<profile>:<platform>`` entries carrying ``ingress_url``). This is
|
|
what the user pastes into the vendor console (Twilio, LINE, Teams, ...). ``profile`` narrows the map."""
|
|
from hermes_constants import get_default_hermes_root
|
|
from gateway.status import read_runtime_status
|
|
if live_default_gateway_pid() is None:
|
|
return {}
|
|
runtime = read_runtime_status(get_default_hermes_root() / "gateway_state.json") or {}
|
|
platforms = runtime.get("platforms")
|
|
if not isinstance(platforms, dict):
|
|
return {}
|
|
urls: dict[str, dict[str, str]] = {}
|
|
for key, entry in platforms.items():
|
|
if not (isinstance(key, str) and ":" in key and isinstance(entry, dict)):
|
|
continue
|
|
url = entry.get("ingress_url")
|
|
if not url or entry.get("state") in ("fatal", "disconnected", "stopped"):
|
|
continue
|
|
name, platform = key.split(":", 1)
|
|
if profile and name != profile:
|
|
continue
|
|
urls.setdefault(name, {})[platform] = str(url)
|
|
return urls
|
|
|
|
|
|
def format_ingress_url_lines(urls: dict[str, str], indent: str = " ") -> list[str]:
|
|
"""One ``<indent><platform>: <url>`` line per platform, sorted."""
|
|
return [f"{indent}{platform}: {url}" for platform, url in sorted(urls.items())]
|
|
|
|
|
|
def notify_multiplexer_profiles_changed(profile_name: str, *, timeout: float = 8.0) -> Optional[list[str]]:
|
|
"""Tell the live default multiplexer that ``profiles/`` changed (``profile_name`` was created or
|
|
deleted) so it hot-serves / unroutes it now instead of at its next periodic rescan. Returns the
|
|
served-profile list the gateway answered with, or None when no multiplexer answered (no live default
|
|
gateway, single-profile gateway, or a gateway predating the verb). Never raises."""
|
|
try:
|
|
from hermes_constants import get_default_hermes_root
|
|
from gateway.control_socket import rescan_gateway_profiles
|
|
if live_default_gateway_pid() is None:
|
|
return None
|
|
answer = rescan_gateway_profiles(get_default_hermes_root(), timeout=timeout)
|
|
except Exception:
|
|
logger.debug("multiplexer rescan notification failed for %r", profile_name, exc_info=True)
|
|
return None
|
|
if not isinstance(answer, dict) or answer.get("multiplex") is False:
|
|
return None
|
|
served = answer.get("served_profiles")
|
|
return [str(p) for p in served] if isinstance(served, list) else None
|