Skip to content

The agent that grows with you

fix(gateway): report a served profile's adapters from the multiplexer record

unsupportedpastels4fc32c8aca96
diff --git a/hermes_cli/web_routers/status.py b/hermes_cli/web_routers/status.py
index e58e429a063..7cf70917d98 100644
--- a/hermes_cli/web_routers/status.py
+++ b/hermes_cli/web_routers/status.py
@@ -20,7 +20,8 @@ from starlette.concurrency import run_in_threadpool
from fastapi import HTTPException, Request
from gateway.status import (
derive_gateway_busy, derive_gateway_drainable, normalize_updated_at, parse_active_agents,
- profile_platforms_from_multiplexer, resolve_gateway_liveness, retained_gateway_state,
+ multiplexer_liveness_for_profile, profile_platforms_from_multiplexer, resolve_gateway_liveness,
+ retained_gateway_state,
runtime_status_heartbeat_age_s, runtime_status_is_stale)
from hermes_cli import __release_date__
from hermes_cli.config import get_config_path, get_env_path
@@ -310,15 +311,31 @@ async def _resolve_gateway_status(profile_dir: Optional[Path], health_url) -> di
runtime = local_runtime
if runtime is None and remote_health_body and remote_health_body.get("gateway_state"):
runtime = remote_health_body
- if liveness.runtime is not None:
+ mux_runtime = liveness.runtime
+ if mux_runtime is None and gateway_running and profile_dir is not None:
+ # The PID rung answers before the multiplexer rung, and a served profile's gateway.lock is
+ # held by the multiplexer itself, so the ladder returns without its record while the
+ # profile's own gateway_state.json may be a retired standalone gateway's (#139080). When the
+ # lock holder IS the multiplexer serving this profile, its record is the live one; a
+ # profile's own (--force) gateway holds the lock under a different PID and keeps its record.
+ try:
+ served = await run_in_threadpool(multiplexer_liveness_for_profile, profile_dir)
+ except Exception: # health: allow BLE001 -- status route must never 500; traceback is logged
+ # A failed lookup keeps the profile's own record. A missing multiplexer record does
+ # not raise: the helper returns (pid, {}), which still replaces it on a PID match.
+ _log.warning("/api/status multiplexer record lookup failed", exc_info=True)
+ served = None
+ if served is not None and served[0] == liveness.pid:
+ mux_runtime = served[1]
+ if mux_runtime is not None:
# Served by the multiplexer: its record is this profile's runtime, with the profile's own
# adapters under ``<profile>:<platform>`` re-keyed to the standalone shape. Unscoped, the
# profile is the process's own home (a pooled ``hermes --profile X serve``).
# Fold on the profile NAME, never ``profile_dir.name``: ``?profile=default`` resolves the
# root itself, whose basename (``.hermes``) matched nothing and read as a named id (#123088).
served_name = profile_name_for_home(profile_dir or get_process_hermes_home())
- runtime = {**liveness.runtime,
- "platforms": profile_platforms_from_multiplexer(liveness.runtime, served_name or "")}
+ runtime = {**mux_runtime,
+ "platforms": profile_platforms_from_multiplexer(mux_runtime, served_name or "")}
gateway_state = None
gateway_platforms: dict = {}
@@ -353,9 +370,9 @@ async def _resolve_gateway_status(profile_dir: Optional[Path], health_url) -> di
if gateway_running and gateway_state is None and remote_health_body is not None:
gateway_state = "running"
- # ``liveness.runtime`` is set only when the shared multiplexer answered for a served profile: its
+ # ``mux_runtime`` is set only when the shared multiplexer answered for a served profile: its
# gateway IS that process, so name every bot a restart would blip ("default, alpha, beta").
- served = (liveness.runtime or {}).get("served_profiles")
+ served = (mux_runtime or {}).get("served_profiles")
return {
"runtime": runtime, "gateway_running": gateway_running, "gateway_pid": liveness.pid,
"gateway_state": gateway_state, "gateway_platforms": gateway_platforms,
diff --git a/tests/hermes_cli/test_web_server_gateway_topology.py b/tests/hermes_cli/test_web_server_gateway_topology.py
index 2e60a1b3e16..28122726ad1 100644
--- a/tests/hermes_cli/test_web_server_gateway_topology.py
+++ b/tests/hermes_cli/test_web_server_gateway_topology.py
@@ -443,6 +443,108 @@ class TestStatusEndpointTopology:
assert data["components"]["platforms"]["configured"] == 2
assert data["components"]["platforms"]["connected"] == 2
+ def _scope_to_served_profile(self, monkeypatch, tmp_path, *, own_runtime, lock_pid, mux_pid, mux_runtime):
+ from contextlib import nullcontext
+
+ from gateway.status import GatewayLiveness
+ import hermes_cli.web_routers.status as status_router
+ from hermes_cli import web_server_profiles
+
+ profile = tmp_path / "profiles" / "coder"
+ profile.mkdir(parents=True)
+ monkeypatch.setattr(web_server_profiles, "_resolve_profile_dir", lambda name: profile)
+ monkeypatch.setattr(web_server_profiles, "_config_profile_scope", lambda profile: nullcontext())
+ monkeypatch.setattr(status_router, "read_runtime_status", lambda path=None: own_runtime)
+ # The PID rung answers first (gateway.lock resolves), so the ladder never attaches a runtime.
+ monkeypatch.setattr(
+ status_router, "resolve_gateway_liveness",
+ lambda **kwargs: GatewayLiveness(running=True, pid=lock_pid, source="pid"),
+ )
+ monkeypatch.setattr(
+ status_router, "multiplexer_liveness_for_profile",
+ lambda home: (mux_pid, mux_runtime) if home == profile else None,
+ )
+ monkeypatch.setattr(_web_server_gateway, "_load_configured_gateway_platforms", lambda: {"telegram"})
+
+ def test_profile_scoped_status_uses_multiplexer_record_when_lock_names_it(self, monkeypatch, tmp_path):
+ # #139080: a profile migrated onto the multiplexer keeps its retired standalone gateway's
+ # gateway_state.json (stopped, telegram disconnected) while the shared gateway holds its
+ # gateway.lock. Liveness came from that lock, so the platform map must come from the same
+ # owner's live record, never the dead one.
+ self._scope_to_served_profile(
+ monkeypatch, tmp_path,
+ own_runtime={
+ "gateway_state": "stopped", "updated_at": "2026-10-02T12:33:48Z",
+ "platforms": {"telegram": {"state": "disconnected"}},
+ },
+ lock_pid=4242, mux_pid=4242,
+ mux_runtime={
+ "gateway_state": "running",
+ "served_profiles": ["default", "coder", "other"],
+ "platforms": {
+ "telegram": {"state": "disconnected"},
+ "coder:telegram": {"state": "connected"},
+ "other:discord": {"state": "connected"},
+ },
+ },
+ )
+
+ resp = self.client.get("/api/status", params={"profile": "coder"})
+
+ assert resp.status_code == 200
+ data = resp.json()
+ assert data["gateway_state"] == "running"
+ # Only coder's adapters: neither the default's flat telegram nor another profile's entry.
+ assert set(data["gateway_platforms"]) == {"telegram"}
+ assert data["gateway_platforms"]["telegram"]["state"] == "connected"
+ assert data["gateway_shared_with"] == ["default", "coder", "other"]
+ assert data["components"]["platforms"]["connected"] == 1
+
+ def test_profile_scoped_status_keeps_own_gateway_record_over_multiplexer(self, monkeypatch, tmp_path):
+ # A --force-started gateway of the profile's own holds the lock under a different PID: its
+ # record is the live one, and the multiplexer's view of the profile must not replace it.
+ self._scope_to_served_profile(
+ monkeypatch, tmp_path,
+ own_runtime={"gateway_state": "running", "platforms": {"telegram": {"state": "connected"}}},
+ lock_pid=5151, mux_pid=4242,
+ mux_runtime={
+ "gateway_state": "running",
+ "served_profiles": ["default", "coder"],
+ "platforms": {"coder:telegram": {"state": "disconnected"}},
+ },
+ )
+
+ resp = self.client.get("/api/status", params={"profile": "coder"})
+
+ assert resp.status_code == 200
+ data = resp.json()
+ assert data["gateway_platforms"]["telegram"]["state"] == "connected"
+ assert data["gateway_shared_with"] is None
+
+ def test_profile_scoped_status_keeps_own_record_when_multiplexer_lookup_raises(self, monkeypatch, tmp_path):
+ # The multiplexer lookup is optional: a failure there must not 500 the status route or
+ # drop the record the ladder already selected.
+ import hermes_cli.web_routers.status as status_router
+
+ self._scope_to_served_profile(
+ monkeypatch, tmp_path,
+ own_runtime={"gateway_state": "running", "platforms": {"telegram": {"state": "connected"}}},
+ lock_pid=4242, mux_pid=4242, mux_runtime={},
+ )
+
+ def _boom(home):
+ raise OSError("unreadable multiplexer record")
+
+ monkeypatch.setattr(status_router, "multiplexer_liveness_for_profile", _boom)
+
+ resp = self.client.get("/api/status", params={"profile": "coder"})
+
+ assert resp.status_code == 200
+ data = resp.json()
+ assert data["gateway_state"] == "running"
+ assert data["gateway_platforms"]["telegram"]["state"] == "connected"
+ assert data["gateway_shared_with"] is None
+
def test_status_rejects_malformed_namespaced_platform_key(self, monkeypatch):
monkeypatch.setattr(_gw_status, "get_running_pid_cached", lambda: 123)
monkeypatch.setattr(