The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(serve): honour multiplex_profiles: false, freeze the launch env last, be loud on failure
teknium1d80aaf1c072b
diff --git a/hermes_cli/web_server.py b/hermes_cli/web_server.pyindex 2c568bf31bf..35f847dbc53 100644--- a/hermes_cli/web_server.py+++ b/hermes_cli/web_server.py@@ -1436,18 +1436,6 @@ def start_server(_apply_ssh_session_token(ssh_session_token or "")_apply_ssh_owner_nonce(ssh_owner_nonce)- # One host process serves every profile, and this one can be asked for any of them via- # ``?profile=``. Decide at boot instead of on the first such request: activation is one-way, so- # everything the backend had already done by then (idle-reaper flushes, hosted rooms, cron)- # stayed on single-profile assumptions and was never re-scoped. No-op on a single-profile host.- try:- from tui_gateway.launch_profile_policy import activate_multi_profile_hosting_eagerly-- if activate_multi_profile_hosting_eagerly():- _log.debug("multi-profile hosting activated at startup (more than one profile home)")- except Exception as exc:- _log.debug("eager multi-profile activation skipped: %s", exc)-# Dashboard-mode starts don't route through main.py's `serve` path, which# applies the same RLIMIT_NOFILE floor (policy in resource_limits, #81547).from hermes_cli.resource_limits import apply_nofile_soft_limit@@ -1493,6 +1481,22 @@ def start_server(_report_port_in_use(host, port)raise SystemExit(PORT_IN_USE_EXIT_CODE)+ # LAST boot step, deliberately. One host process serves every profile and this one can be asked+ # for any of them via ``?profile=``, so the decision is made here instead of on the first such+ # request — activation is one-way, and everything the backend had already done by then+ # (idle-reaper flushes, hosted rooms, cron) stayed on single-profile assumptions. It runs after+ # the keepalive / auth gate / uvicorn build because activation FREEZES ``os.environ`` as the+ # launch profile's credentials, and that snapshot is the only source for launch keys with no+ # ``.env`` to rebuild from (systemd ``Environment=``, ``op run``, Compose): anything injected or+ # rotated by a later boot step would otherwise be invisible for the process lifetime. No-op on a+ # single-profile host and on one that pinned ``gateway.multiplex_profiles: false``.+ try:+ from tui_gateway.launch_profile_policy import activate_multi_profile_hosting_eagerly++ activate_multi_profile_hosting_eagerly()+ except Exception:+ _log.warning("eager multi-profile activation failed", exc_info=True)+async def _serve():# startup split from main_loop so the bound (ephemeral) port is readable.if not config.loaded:diff --git a/hermes_constants.py b/hermes_constants.pyindex 89ee7f8d773..9184c261368 100644--- a/hermes_constants.py+++ b/hermes_constants.py@@ -288,6 +288,28 @@ def named_profile_has_identity(profile_home: str | Path) -> bool:return any((home / marker).is_file() or (home / marker).is_symlink() for marker in _PROFILE_IDENTITY_MARKERS)+def named_profile_has_servable_identity(profile_home: str | Path) -> bool:+ """Stricter than :func:`named_profile_has_identity`: is this dir a profile a host should change+ its own posture for?++ An EMPTY ``.env`` is all a crashed ``hermes profile create`` leaves behind, and it is enough for+ ``named_profile_has_identity``. Listing such a shell is harmless; counting it as a second tenant+ is not — it flips the whole host's credential reads fail-closed at the next boot. Every other+ marker, and a non-empty or symlinked ``.env``, still counts.+ """+ home = Path(profile_home)+ for marker in _PROFILE_IDENTITY_MARKERS:+ path = home / marker+ if path.is_symlink():+ return True+ try:+ if path.is_file() and (marker != ".env" or path.stat().st_size > 0):+ return True+ except OSError:+ continue+ return False++def named_profile_is_live(profile_home: str | Path) -> bool:"""A resolvable named profile: an existing dir with identity that has not been deleted.``-p``/``--profile`` resolution and ``profile_exists`` share this so a stale ghost shell candiff --git a/tui_gateway/launch_profile_policy.py b/tui_gateway/launch_profile_policy.pyindex e6a311bc0d3..6b1b3dc6121 100644--- a/tui_gateway/launch_profile_policy.py+++ b/tui_gateway/launch_profile_policy.py@@ -18,11 +18,14 @@ Two facts anchor this module:from __future__ import annotationsimport contextlib+import loggingimport osimport threadingfrom pathlib import Pathfrom typing import Dict, Iterator, Optional+logger = logging.getLogger(__name__)+_lock = threading.Lock()_snapshot: Optional[Dict[str, str]] = None@@ -48,6 +51,45 @@ def activate_multi_profile_hosting() -> None:set_multiplex_active(True)+def _multiplex_disabled_explicitly() -> bool:+ """True when this host has deliberately turned multiplexing OFF.++ ``profiles_to_serve(multiplex=True)`` takes the flag as an argument and never reads config, so+ the eager gate must consult the operator's setting itself or it arms the fail-closed guard on+ hosts that pinned ``gateway.multiplex_profiles: false`` to keep per-profile gateways. Same+ precedence the gateway boot uses: ``GATEWAY_MULTIPLEX_PROFILES`` over config.yaml; unset means+ "not disabled" (the default is on, and the lazy backstop still fires either way).+ """+ from gateway.config import _coerce_bool, _env_multiplex_profiles_override+ env_override = _env_multiplex_profiles_override()+ if env_override is not None:+ return not env_override+ from hermes_cli.config import load_config+ cfg = load_config() or {}+ value = cfg.get("multiplex_profiles")+ gateway_cfg = cfg.get("gateway")+ if value is None and isinstance(gateway_cfg, dict):+ value = gateway_cfg.get("multiplex_profiles")+ return value is not None and not _coerce_bool(value, True)+++def _servable_profile_homes() -> set:+ """Resolved homes this host could be asked to serve: the launch home plus every profile dir+ carrying a real servability marker.++ ``named_profile_has_identity`` accepts an EMPTY ``.env``, which is all a crashed+ ``hermes profile create`` leaves behind — counting it would flip a single-profile host+ fail-closed at its next boot.+ """+ from hermes_constants import named_profile_has_servable_identity+ from hermes_cli.profiles import profiles_to_serve++ homes = {Path(home).resolve() for name, home in profiles_to_serve(multiplex=True)+ if name == "default" or named_profile_has_servable_identity(home)}+ homes.add(Path(os.environ.get("HERMES_HOME") or Path.home() / ".hermes").resolve())+ return homes++def activate_multi_profile_hosting_eagerly() -> bool:"""Activate at HOST startup when this machine has more than one servable profile home.@@ -56,22 +98,37 @@ def activate_multi_profile_hosting_eagerly() -> bool:connects, MCP discovery) ran under single-profile assumptions and is never re-scoped, and thelaunch profile's env had already been mutated by then, so ``capture_launch_env`` froze apolluted snapshot. One ``hermes serve`` / ``hermes gateway run`` per host means the process- knows at boot whether it can be asked for a second home: decide there, once.+ knows at boot whether it can be asked for a second home: decide there, once. Call it as the LAST+ boot step: the frozen snapshot is the only source for launch keys with no ``.env`` to rebuild+ from, so every credential the boot still injects must already be in ``os.environ``.A genuinely single-profile host still never activates (byte-identical behaviour, ``os.environ``- precedence preserved). Returns True when this call activated hosting.+ precedence preserved), and so does one that pinned ``gateway.multiplex_profiles: false``. An+ unreadable profiles directory fails CLOSED: we cannot prove the host is single-profile, and the+ lazy backstop only fires once a request has already been answered. Returns True when this call+ activated hosting."""from agent.secret_scope import is_multiplex_activeif is_multiplex_active():return Falsetry:- from hermes_cli.profiles import profiles_to_serve- homes = {Path(home).resolve() for _name, home in profiles_to_serve(multiplex=True)}- homes.add(Path(os.environ.get("HERMES_HOME") or Path.home() / ".hermes").resolve())+ if _multiplex_disabled_explicitly():+ return Falseexcept Exception:- return False+ logger.warning("Could not read gateway.multiplex_profiles; continuing with the profile probe",+ exc_info=True)+ try:+ homes = _servable_profile_homes()+ except Exception:+ logger.warning(+ "Could not enumerate this host's profile homes; activating multi-profile hosting "+ "fail-closed (unscoped credential reads will raise instead of borrowing the launch "+ "profile's)", exc_info=True)+ activate_multi_profile_hosting()+ return Trueif len(homes) < 2:return False+ logger.info("Multi-profile hosting activated at startup (%d servable profile homes)", len(homes))activate_multi_profile_hosting()return True@@ -100,7 +157,15 @@ def launch_secret_scope(launch_home: "str | Path") -> Dict[str, str]:not, so the body's credential source is decided once at entry: a request that entered whilesingle-profile keeps resolving from this mapping after a concurrent first secondary flips``get_secret`` to fail closed (``_MULTIPLEX_ACTIVE`` is read on every ``get_secret``, the- scope decision was made at entry)."""+ scope decision was made at entry).++ PRECEDENCE, and it is not the process's: ``<launch home>/.env`` (plus hydrated external sources)+ WINS over the env. For a key present in both, an unscoped read returned the ambient+ ``os.environ`` value before activation and returns the ``.env`` value after — so activation is+ not purely "stricter" for the launch tenant, it also changes which of the launch profile's own+ two values it sees. Env-only keys (systemd ``Environment=``, ``op run``, Compose) are unaffected:+ nothing in the files shadows them.+ """from agent.secret_scope import _is_global_env, build_profile_secret_scopescope = {k: v for k, v in _launch_env().items() if not _is_global_env(k)}scope.update(build_profile_secret_scope(Path(launch_home)))