Files
hermes-agent/hermes_cli/gateway_cli_commands.py
unsupportedpastels 4b6a8edd6a fix(plugins): fire pre_command in the gateway for every local command
After the cutover no local surface fired pre_command where the command runs.
slash.exec and command.dispatch (classic CLI forwards, Ink TUI, Desktop) never
fired it, and the classic CLI fired it from its own second copy of the plugin.

- The gateway fires pre_command for every recognized command a local session
  runs, under the session's profile: before the handler for commands it runs
  itself (slash.exec, command.dispatch; an accepted TUI read fires once inside
  the read path), and for commands the classic CLI runs in its own process
  through the new command.observe (plain data: the command as typed and its
  arguments). command.observe replies first, so the CLI never waits on a
  plugin. The hook arguments keep main's values: surface and platform "cli"
  on every local surface, session_key the session id.
- It fires only for commands that run. A busy rejection or a named refusal
  fires nothing; during a turn it fires when the busy dispatcher accepts the
  command, so a refused /goal, /loop or empty /queue or /steer fires nothing
  either. Every command that does run mid-turn fires, as main's CLI did,
  except messaging's escape hatches (/stop and the approval controls).
- Each delivery waits out its callback on its own thread, never the shared
  executor, with at most one delivery in flight per session: one session's
  deliveries fire in order and a slow callback never holds up another
  session's. The hook dispatcher's existing bounds apply
  (plugins.hook_callback_timeout and its abandoned-worker cap), and with
  plugins.isolation: host the hook runs in the plugin host. pre_command takes
  that bounded path everywhere, so off the gateway its callbacks also run on a
  worker thread.
- On a gateway session the classic CLI loads no plugin code. It reads skills,
  plugin commands and plugin toolsets from the gateway (commands.catalog,
  scoped to the session's own folder, and plugins.list) for /help, skill
  dispatch and completion, /tools and the status and banner fallbacks. -s and
  skills.auto_load are rendered in the gateway, and /reload-skills and
  /skills install rescan there and drop the CLI's cached catalog. These reads
  are cached per gateway life and never served once the connection has hung
  up. The "Unknown toolsets" warning comes from the gateway (a new
  unknown_toolsets field when the session opens). An older gateway without
  command.observe gets one named notice, and nothing fires locally.
- The canonical gateway connection serves plugins.list for its profile:
  loaded plugins with tool/hook/command counts, load error, toolsets and
  portable MCP server names (optional fields added to the contract; TS and
  OpenRPC regenerated). On a gateway session the CLI's /plugins reads it
  instead of this process's plugin manager. Off the gateway the in-process
  paths are unchanged.

Key-less inject_message (B2-11), which the classic CLI's own plugin copy
used to serve, is handled in the next commit.

Refs #134889 (PLG-7)
Refs #139523
2026-10-16 19:39:48 -04:00

68 lines
2.6 KiB
Python

"""Classic CLI commands whose state lives in the gateway session, answered there.
Rewired, the session's tools, checkpoints, environment and handoff watcher run in the gateway, so
main's handlers (which read this process's own agent and environment) answer nothing or the wrong
thing:
- ``/diff session``: the checkpoint baseline is the gateway tools'. ``rollback.session_diff`` reads it
under the session's frozen checkpoint policy (``--checkpoints`` included) and returns it as data
for main's terminal renderer. Working-tree modes stay main's local git read of the same checkout.
- ``/reload``: ``reload.env`` re-reads ``.env`` where turns and tools run (this process re-reads too).
- ``/handoff``: no gateway verb carries a handoff yet; refused as Ink and Desktop refuse it, instead
of the store's ``request_handoff`` refusal dying silently in the process loop.
"""
from __future__ import annotations
from agent.i18n import t
from hermes_cli.gateway_client import GatewayClientError
def _diff(cli, connection, cmd):
args = cmd.partition(" ")[2].strip().lower().split()
if "session" not in args:
return False
from hermes_cli.cli_commands_session_tools import _lines, _pr, _t, print_session_diff
result = connection.call("rollback.session_diff", session_id=cli.session_id) or {}
if not result.get("enabled"):
_pr(*_lines(_t("diff.not_enabled")))
else:
print_session_diff(cli, result, stat_only=bool({"--stat", "stat"} & set(args)))
return True
def _reload(cli, connection, cmd):
from hermes_cli.config import reload_env
reload_env()
updated = (connection.call("reload.env", session_id=cli.session_id) or {}).get("updated", 0)
print(f" {t('cli.reload.env_reloaded', count=updated)}")
return True
def _handoff(cli, connection, cmd):
from cli import _cprint
from hermes_cli.gateway_chat_commands import refusal
_cprint(f" {refusal('handoff')}")
return True
_ROUTES = {"diff": _diff, "reload": _reload, "handoff": _handoff}
# Named refusals: nothing runs, so the gateway is not told (no ``pre_command``).
REFUSED = frozenset({"handoff"})
def run(cli, canonical, cmd):
"""True when the gateway answered the command (the local handler must not run)."""
route = _ROUTES.get(canonical)
if route is None:
return False
from hermes_cli.gateway_side import connection_for
connection = connection_for(cli)
if connection is None:
return False
try:
return route(cli, connection, (cmd or "").strip())
except GatewayClientError as exc:
from cli import _cprint
_cprint(f" /{canonical}: {exc}")
return True