Files
hermes-agent/gateway/status_served.py
unsupportedpastels e6ec18fba6 fix(gateway): keep the host's own record whole on unscoped /api/status
The served-profile lookup in /api/status ran whenever the PID rung
answered, so the default profile's own backend on the multiplex host
projected its live record through the default-profile fold and dropped
every <profile>:<platform> entry: another profile's fatal adapter
(e.g. reviewer:discord duplicate_credential) vanished and the platforms
component read ok.

Move the record choice into one read-only helper,
gateway.status_served.served_profile_runtime, used by /api/status and the
messaging card so they cannot pick different records:

- the profile's own record wins whenever it proves a live gateway of
  that home (the host's own record on the host, or an own --force one);
- a missing/unreadable multiplexer record ((pid, {})) is no record;
- a caller-supplied local PID must be the multiplexer's.

/api/status now only consults it for a PID proven on this host
(local_liveness_pid): a health-probe PID belongs to the remote container.
gateway_shared_with comes from the selected live record, so the host's
own unscoped readout names the profiles a restart would blip (now
asserted). The helper lives in a sibling module because gateway/status.py
may not grow.

Refs #134889 (MSG-6)
Refs #139162
Refs #139080
Addresses review by @kshitijk4poor on #139272.
2026-10-15 16:33:11 -04:00

48 lines
2.3 KiB
Python

"""Which runtime record describes a profile the host multiplexer may serve.
One rule for ``/api/status`` and the messaging card so the two surfaces never pick different records
for the same profile. Read-only: it selects and projects existing ``gateway_state.json`` records and
writes nothing. ``gateway.status`` is resolved at call time so its test seams
(``monkeypatch.setattr(gateway.status, ...)``) still intercept.
"""
from __future__ import annotations
from pathlib import Path
from typing import Any, Optional
def local_liveness_pid(liveness: Any) -> Optional[int]:
"""``liveness.pid`` when it was proven on THIS host; None for a health-probe answer, whose PID
belongs to the remote container and may collide with an unrelated local one."""
return None if liveness.source == "health" else liveness.pid
def served_profile_runtime(
home: Path, own_runtime: Optional[dict[str, Any]], *, gateway_pid: Optional[int] = None
) -> Optional[dict[str, Any]]:
"""The multiplexer's record projected onto ``home``'s profile (bare platform keys, the shape a
standalone gateway writes) when it is that profile's live runtime record, else None.
- ``own_runtime`` wins whenever it proves a live gateway of ``home`` -- an own ``--force`` one, or
the host's own record on the host itself, which must stay whole so other profiles'
``<profile>:<platform>`` failures remain visible there;
- a missing/unreadable multiplexer record (``(pid, {})``) is no record;
- ``gateway_pid`` (a PID the caller proved on this host) must BE the multiplexer, so a gateway of
the profile's own holding its lock keeps its record.
"""
from gateway import status
if own_runtime is not None and status.get_runtime_status_running_pid(own_runtime, expected_home=home) is not None:
return None
served = status.multiplexer_liveness_for_profile(home)
if served is None or not served[1]:
return None
mux_pid, record = served
if gateway_pid is not None and mux_pid != gateway_pid:
return None
# Fold on the profile NAME, never ``home.name``: the default root's basename is ``.hermes`` (or
# any custom HERMES_HOME), so its flat keys never matched (#123088).
name = status.profile_name_for_home(home) or "default"
return {**record, "platforms": status.profile_platforms_from_multiplexer(record, name)}