mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-05-02 02:01:47 +00:00
feat(gateway,cli): confirm /reload-mcp to warn about prompt cache invalidation
Reloading MCP servers rebuilds the tool set for the active session, which invalidates the provider prompt cache (tool schemas are baked into the system prompt). The next message re-sends full input tokens — can be expensive on long-context or high-reasoning models. To surface that cost, /reload-mcp now routes through a new slash-confirm primitive with three options: Approve Once / Always Approve / Cancel. 'Always Approve' persists approvals.mcp_reload_confirm: false so future reloads run silently. Coverage: * Classic CLI (cli.py) — interactive numbered prompt. * TUI (tui_gateway + Ink ops.ts) — text warning on first call; `now` / `always` args skip the gate; `always` also persists the opt-out. * Messenger gateway — button UI on Telegram (inline keyboard), Discord (discord.ui.View), Slack (Block Kit actions); text fallback on every other platform via /approve /always /cancel replies intercepted in gateway/run.py _handle_message. * Config key: approvals.mcp_reload_confirm (default true). * Auto-reload paths (CLI file watcher, TUI config-sync mtime poll) pass confirm=true so they do NOT prompt. Implementation: * tools/slash_confirm.py — module-level pending-state store used by all adapters and by the CLI prompt. Thread-safe register/resolve/clear. * gateway/platforms/base.py — send_slash_confirm hook (default 'Not supported' → text fallback). * gateway/run.py — _request_slash_confirm helper + text intercept in _handle_message (yields to in-progress tool-exec approvals so dangerous-command /approve still unblocks the tool thread first). Tests: * tests/tools/test_slash_confirm.py — primitive lifecycle + async resolution + double-click atomicity (16 tests). * tests/hermes_cli/test_mcp_reload_confirm_gate.py — default-config shape + deep-merge preserves user opt-out (5 tests). Targeted runs (hermetic): 89 passed (slash-confirm, config gate, existing agent cache, existing telegram approval buttons).
This commit is contained in:
parent
7fae87bc00
commit
4d7fc0f37c
14 changed files with 1287 additions and 9 deletions
|
|
@ -3744,6 +3744,40 @@ def _(rid, params: dict) -> dict:
|
|||
def _(rid, params: dict) -> dict:
|
||||
session = _sessions.get(params.get("session_id", ""))
|
||||
try:
|
||||
# Gate: /reload-mcp invalidates the prompt cache for this session.
|
||||
# Respect the ``approvals.mcp_reload_confirm`` config toggle — if
|
||||
# set (default true) AND the caller did not pass ``confirm=true``
|
||||
# in params, surface a warning to the transcript instead of just
|
||||
# reloading silently. Users pass confirm=true either by
|
||||
# re-invoking after reading the warning, or by setting the
|
||||
# config key to false permanently.
|
||||
user_confirm = bool(params.get("confirm", False))
|
||||
if not user_confirm:
|
||||
try:
|
||||
from hermes_cli.config import load_config as _load_config
|
||||
_cfg = _load_config()
|
||||
_approvals = _cfg.get("approvals") if isinstance(_cfg, dict) else None
|
||||
_confirm_required = True
|
||||
if isinstance(_approvals, dict):
|
||||
_confirm_required = bool(_approvals.get("mcp_reload_confirm", True))
|
||||
except Exception:
|
||||
_confirm_required = True
|
||||
if _confirm_required:
|
||||
# Return a structured response the Ink client can surface
|
||||
# as a warning/confirmation without actually reloading yet.
|
||||
# Ink's ops.ts reads ``status`` and prints ``message`` to
|
||||
# the transcript; a follow-up invocation with confirm=true
|
||||
# (or an `always` choice that flips the config) proceeds.
|
||||
return _ok(rid, {
|
||||
"status": "confirm_required",
|
||||
"message": (
|
||||
"⚠️ /reload-mcp invalidates the prompt cache (next "
|
||||
"message re-sends full input tokens). Reply `/reload-mcp "
|
||||
"now` to proceed, or `/reload-mcp always` to proceed and "
|
||||
"silence this prompt permanently."
|
||||
),
|
||||
})
|
||||
|
||||
from tools.mcp_tool import shutdown_mcp_servers, discover_mcp_tools
|
||||
|
||||
shutdown_mcp_servers()
|
||||
|
|
@ -3753,6 +3787,15 @@ def _(rid, params: dict) -> dict:
|
|||
if hasattr(agent, "refresh_tools"):
|
||||
agent.refresh_tools()
|
||||
_emit("session.info", params.get("session_id", ""), _session_info(agent))
|
||||
|
||||
# Honor `always=true` by persisting the opt-out to config.
|
||||
if bool(params.get("always", False)):
|
||||
try:
|
||||
from cli import save_config_value as _save_cfg
|
||||
_save_cfg("approvals.mcp_reload_confirm", False)
|
||||
except Exception as _exc:
|
||||
logger.warning("Failed to persist mcp_reload_confirm=false: %s", _exc)
|
||||
|
||||
return _ok(rid, {"status": "reloaded"})
|
||||
except Exception as e:
|
||||
return _err(rid, 5015, str(e))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue