Files
hermes-agent/tools/thread_context.py
liuhao1024 ee02668f51 fix(mcp): carry the turn thread's prompt callbacks across the elicitation consent hop
A server-initiated elicitation mid tools/call routes its consent through
asyncio.to_thread. The handler already replays the owning call's contextvars
snapshot (_pending_call_context) so gateway-platform detection survives the
task hop, but the per-thread prompt callbacks (approval, sudo, vault unlock)
live in threading.local and do not follow: on the executor thread
_get_approval_callback() returns None, so a CLI/TUI/ACP session fails the
consent closed as "the user did not approve" even though a callback was
installed for the whole turn — the GHSA-qg5c-hvr5-hjgr class on the
elicitation path.

Capture the callbacks on the agent turn thread next to the contextvars
snapshot (capture_thread_callbacks), carry them in the same pending-call
slot, and install/clear them around the consent invocation (executor threads
are reused, so an install is never leaked past the call).
2026-11-14 11:26:33 -08:00

112 lines
5.0 KiB
Python

"""Propagate agent-turn context into worker threads that dispatch Hermes tools.
A bare ``threading.Thread`` / ``ThreadPoolExecutor`` worker starts with an empty
``contextvars.Context`` and no thread-local approval/sudo callbacks, so tool dispatch inside it
silently loses the approval ContextVars (gateway sessions then auto-approve dangerous commands)
and the CLI callbacks (``prompt_dangerous_approval`` cannot reach the user, GHSA-qg5c-hvr5-hjgr).
Call :func:`propagate_context_to_thread` **on the parent thread** (it snapshots at call time) and
use the result as the worker target; callbacks are installed for the worker's lifetime and
always cleared on exit.
"""
from __future__ import annotations
import contextvars
import logging
from typing import Callable
logger = logging.getLogger(__name__)
def _callback_api():
"""(getter, setter) pairs for every thread-local prompt callback a tool may need mid-dispatch
(lazy: terminal_tool imports tools.approval at load, so a top-level import risks a cycle).
Add a new per-thread prompt here — a callback missing from this table is silently absent on
every parallel/timeout worker, so the tool believes nobody can answer."""
from agent.vault_backends import unlock as vault_unlock
from tools import terminal_tool as tt
return ((tt._get_approval_callback, tt.set_approval_callback),
(tt._get_sudo_password_callback, tt.set_sudo_password_callback),
(vault_unlock.get_unlock_prompt_callback, vault_unlock.set_unlock_prompt_callback),
(vault_unlock.get_save_login_prompt_callback, vault_unlock.set_save_login_prompt_callback),
(vault_unlock.get_code_prompt_callback, vault_unlock.set_code_prompt_callback),
# The unlock owner: a manager unlocked on a worker must still be released with its session.
(vault_unlock.get_current_session_id, vault_unlock.set_current_session_id),
# A background fork's dispatch-side tool whitelist: without it a parallel batch escapes it.
(_get_tool_whitelist, _set_tool_whitelist))
def _get_tool_whitelist():
"""The thread's ``pre_tool_call`` tool whitelist as ``(allowed, deny_fmt)``, or None."""
from hermes_cli.plugins import _thread_tool_whitelist
allowed = getattr(_thread_tool_whitelist, "allowed", None)
return None if allowed is None else (allowed, getattr(_thread_tool_whitelist, "fmt", None))
def _set_tool_whitelist(state) -> None:
from hermes_cli.plugins import clear_thread_tool_whitelist, set_thread_tool_whitelist
if state is None:
clear_thread_tool_whitelist()
elif state[1] is None:
set_thread_tool_whitelist(state[0])
else:
set_thread_tool_whitelist(*state)
def capture_thread_callbacks() -> tuple:
"""``(setter, callback)`` pairs for the *current thread's* prompt callbacks, to replay on
another thread (empty when none are installed or the API is unavailable). The elicitation
consent hop uses this: ``asyncio.to_thread`` carries neither contextvars nor ``threading.local``,
so the tool wrapper snapshots both at the call site."""
try:
return tuple((setter, cb) for getter, setter in _callback_api()
for cb in (getter(),) if cb is not None)
except Exception:
logger.debug("Could not capture prompt callbacks for cross-thread replay",
exc_info=True)
return ()
def propagate_context_to_thread(target: Callable) -> Callable:
"""Wrap *target* to run with the *current* thread's ContextVars and per-thread prompt callbacks
(approval, sudo, password-manager unlock).
Fail-closed: if callback installation raises they stay ``None`` — dangerous commands are then
denied by ``prompt_dangerous_approval`` and the gateway approval queue blocks.
"""
ctx = contextvars.copy_context()
# (setter, parent callback) pairs; None when the callback API could not be captured.
installs = None
try:
installs = tuple((setter, getter()) for getter, setter in _callback_api())
except Exception:
logger.debug("Could not capture parent approval/sudo callbacks", exc_info=True)
def _runner(*args, **kwargs):
def _inner():
if installs is None:
return target(*args, **kwargs)
try:
for setter, cb in installs:
if cb is not None:
setter(cb)
except Exception:
logger.debug("Failed to install propagated approval/sudo callbacks; "
"dangerous-command approval will fail closed", exc_info=True)
try:
return target(*args, **kwargs)
finally:
try:
for setter, _cb in installs:
setter(None)
except Exception:
logger.debug("Failed to clear propagated approval/sudo callbacks",
exc_info=True)
return ctx.run(_inner)
return _runner