Files
hermes-agent/hermes_cli/gateway_status_notes.py
Maciej Lisiewski 95f184b304 fix(gateway): keep systemctl status redaction in place, no facade shim
Undo the salvaged move of _run_systemctl/_service_scope_label into a new
module re-exported from hermes_cli.gateway (AGENTS.md: no re-export shims
for internal moves). The capture+redact printer now lives in the existing
gateway_status_notes sibling and gateway.py only swaps the one call, so
the file shrinks by nothing and grows by nothing.

The test now drives systemd_status() end to end against a stand-in
systemctl process and reads the real fds (capfd), so on the base it
fails with the credential on the terminal.

Fixes #143938
2026-11-14 12:05:38 +01:00

58 lines
2.8 KiB
Python

"""Notes `hermes gateway status` prints beside the running/not-running verdict."""
from __future__ import annotations
import sys
from hermes_cli.config import get_hermes_home
def _incompatible_owner_verdict() -> str | None:
"""``incompatible: <reason>`` when the gateway answering for this home is one this Hermes's clients
(TUI, Desktop, chat) refuse, e.g. an older Hermes's daemon left running across an upgrade."""
from hermes_cli.gateway_runtime import discover_gateway_endpoint
try:
observed = discover_gateway_endpoint(get_hermes_home(), timeout=1.5)
except (OSError, ValueError): # discovery reports peers as verdicts; only a bad home lands here
return None
if observed.state != "incompatible":
return None
return f"incompatible: {observed.reason_code or 'unknown'}"
def report_incompatible_owner(pids) -> bool:
"""Print ✗ (with the fix) when the running gateway cannot serve this Hermes; True if printed.
``pids`` names the scanned processes for the manual-run line; empty for a service-managed one."""
stale = _incompatible_owner_verdict()
if not stale:
return False
running = f"Gateway is running (PID: {', '.join(map(str, pids))})" if pids else "The running gateway"
print(f"✗ {running} but cannot serve this Hermes ({stale})")
print(" It was started by another Hermes version; TUI, Desktop and chat cannot attach to it.")
print(" Replace it with this version:")
print(" hermes gateway restart")
return True
def print_standalone_by_config() -> None:
"""Default-profile status: name the profiles that opted out of the host multiplexer by config,
so the served set the host gateway reports is not mistaken for the installed roster."""
from hermes_cli.profiles import get_active_profile_name, profiles_to_serve
if (get_active_profile_name() or "default") != "default":
return
roster = {name for name, _home in profiles_to_serve(True, include_standalone=True)}
served = {name for name, _home in profiles_to_serve(True)}
names = sorted(roster - served - {"default"})
if names:
print(f"standalone by config (temporary compatibility shim): {', '.join(names)}")
def print_scrubbed_output(result) -> None:
"""Print a captured status command's stdout/stderr through the support-text redactor.
``systemctl status`` renders the unit's CGroup tree with every child's full argv, so an MCP
server launched with ``--api-key <value>`` would otherwise leak into shared status output."""
from hermes_cli.debug_redaction import redact_debug_support_text
if result.stdout:
print(redact_debug_support_text(result.stdout), end="")
if result.stderr:
print(redact_debug_support_text(result.stderr), end="", file=sys.stderr)