Skip to content

The agent that grows with you

refactor(cli/plugins): move hook/event-bus/prompt-section dispatch into PluginDispatchMixin (plugins_dispatch.py)

Tekniumf0cfbb33e139
diff --git a/hermes_cli/plugins.py b/hermes_cli/plugins.py
index 8b4c1883a4a..d80dae95d9e 100644
--- a/hermes_cli/plugins.py
+++ b/hermes_cli/plugins.py
@@ -12,8 +12,6 @@ from __future__ import annotations
import asyncio
import builtins
-import contextvars
-import copy
import importlib.metadata
import inspect
import json
@@ -23,7 +21,6 @@ import queue
import re
import sys
import threading
-import time
import types
from contextlib import suppress
from dataclasses import dataclass, field
@@ -34,7 +31,7 @@ from hermes_constants import get_hermes_home, hermes_home_key
from registration_lifecycle import replacement_coordinator
from utils import env_var_enabled
from hermes_cli.config import cfg_get, load_config_readonly
-from hermes_cli.middleware import OBSERVER_SCHEMA_VERSION, VALID_MIDDLEWARE
+from hermes_cli.middleware import VALID_MIDDLEWARE
from hermes_cli.plugin_capabilities import ( # noqa: F401 — re-exported
CAPABILITY_REGISTRY,
VALID_CAPABILITY_IDS,
@@ -82,6 +79,39 @@ from hermes_cli.plugins_loader import ( # noqa: F401 — re-exported
_serialized_replacement,
_plugin_home_scope,
)
+from hermes_cli.plugins_dispatch import ( # noqa: F401 — re-exported
+ PluginDispatchMixin,
+ _HOOK_TIMEOUT_BOUNDED_HOOKS,
+ _HOOK_TIMEOUT_FAIL_CLOSED_HOOKS,
+ _HOOK_CALLER_THREAD_HOOKS,
+ _HOOK_TIMEOUT_SUPPRESSION_SECONDS,
+ _PRE_TOOL_CALL_TIMEOUT_BLOCK_MESSAGE,
+ SYSTEM_PROMPT_SECTION_POSITIONS,
+ DEFAULT_SYSTEM_PROMPT_SECTION_MAX_CHARS,
+ MAX_SYSTEM_PROMPT_SECTION_CHARS,
+ MAX_SYSTEM_PROMPT_SECTIONS,
+ MAX_SYSTEM_PROMPT_SECTIONS_TOTAL_CHARS,
+ _SYSTEM_PROMPT_SECTION_ID_RE,
+ _SYSTEM_PROMPT_SECTION_HEADING_PREFIX,
+ PLUGIN_SECTIONS_START,
+ PLUGIN_SECTIONS_END,
+ is_valid_system_prompt_section_id,
+ format_system_prompt_section,
+ format_system_prompt_sections,
+ HERMES_EVENT_NAMESPACE,
+ _EVENT_EMIT_DEPTH_CAP,
+ _EVENT_PENDING_CAP,
+ _EVENT_WORKER_STOP,
+ PluginSystemPromptSection,
+ RenderedPluginSystemPromptSection,
+ _EventSubscription,
+ _QueuedPluginEvent,
+ _HOOK_CALLBACK_TIMEOUT_SECS,
+ _HOOK_SKIPPED,
+ _MAX_HOOK_CALLBACK_TIMEOUT_SECS,
+ _hook_uses_callback_timeout,
+ _pre_tool_call_timeout_block,
+)
from hermes_cli.plugins_state import ( # noqa: F401 — re-exported
_PLUGIN_SETTING_RESERVED_ROOTS,
_PLUGIN_SETTING_SEGMENT_RE,
@@ -241,79 +271,6 @@ VALID_HOOKS: Set[str] = {
# the shell-hook allow-list, so these are refused loudly instead of having output silently ignored.
SHELL_UNSUPPORTED_HOOKS: Set[str] = {"transform_api_error_classification"}
-# Allowlist of agent-turn hot-path hooks bounded by plugins.hook_callback_timeout (fail-open:
-# abandon without join — joining reintroduced a shutdown hang). Unlisted hooks run synchronously.
-# Intentionally unbounded: on_session_finalize/reset (last-chance flush — abandon can lose state);
-# subagent_start (observer); pre_gateway_dispatch (policy gate — neither fail mode is acceptable);
-# pre/post_approval_* (approval UX has its own timeout); kanban_* (own heartbeat/stale reclaim).
-_HOOK_TIMEOUT_BOUNDED_HOOKS: Set[str] = {
- "post_tool_call",
- "transform_terminal_output",
- "transform_tool_result",
- "transform_llm_output",
- "pre_llm_call",
- "post_llm_call",
- "pre_api_request",
- "post_api_request",
- "api_request_error",
- "pre_verify",
- "on_session_start",
- "on_session_end",
-}
-
-# Policy hooks: timeout / still-running must fail closed (block the tool).
-_HOOK_TIMEOUT_FAIL_CLOSED_HOOKS: Set[str] = {"pre_tool_call"}
-# Documented parent-thread serialization contract — never run on a timeout worker (hooks.md).
-_HOOK_CALLER_THREAD_HOOKS: Set[str] = {"subagent_stop"}
-# After a timeout, suppress the same callback this long so a hung hook cannot pile up threads.
-_HOOK_TIMEOUT_SUPPRESSION_SECONDS = 60.0
-
-_PRE_TOOL_CALL_TIMEOUT_BLOCK_MESSAGE = (
- "pre_tool_call plugin callback timed out or is still running"
-)
-
-
-# System-prompt sections are tightly bounded: they become high-trust prompt bytes charged every turn.
-SYSTEM_PROMPT_SECTION_POSITIONS = frozenset({"after_memory"})
-DEFAULT_SYSTEM_PROMPT_SECTION_MAX_CHARS = 4_000
-MAX_SYSTEM_PROMPT_SECTION_CHARS = 4_000
-MAX_SYSTEM_PROMPT_SECTIONS = 32
-MAX_SYSTEM_PROMPT_SECTIONS_TOTAL_CHARS = 8_000
-_SYSTEM_PROMPT_SECTION_ID_RE = re.compile(r"^[a-z0-9][a-z0-9._-]{0,127}$")
-_SYSTEM_PROMPT_SECTION_HEADING_PREFIX = "## Plugin Context: "
-PLUGIN_SECTIONS_START = "<!-- hermes-plugin-sections:start -->"
-PLUGIN_SECTIONS_END = "<!-- hermes-plugin-sections:end -->"
-
-
-def is_valid_system_prompt_section_id(value: Any) -> bool:
- """Return whether *value* is a stable, heading-safe section identifier."""
- return isinstance(value, str) and bool(_SYSTEM_PROMPT_SECTION_ID_RE.fullmatch(value))
-
-
-def format_system_prompt_section(section_id: str, content: str) -> str:
- """Render an auditable, length-framed block recoverable from the full prompt."""
- return (
- f"{_SYSTEM_PROMPT_SECTION_HEADING_PREFIX}{section_id}\n"
- f"<!-- hermes-plugin-section-chars:{len(content)} -->\n\n"
- f"{content}"
- )
-
-
-def format_system_prompt_sections(sections: list) -> str:
- """Render the canonical container used for persistence recovery."""
- if not sections:
- return ""
- blocks = [format_system_prompt_section(item.id, item.content) for item in sections]
- return f"{PLUGIN_SECTIONS_START}\n" + "\n\n".join(blocks) + f"\n{PLUGIN_SECTIONS_END}"
-# Reserved event namespace prefix — only core may publish ``hermes:<event>``.
-HERMES_EVENT_NAMESPACE = "hermes"
-
-# Event recursion depth cap (subscribers may emit); over-deep emits are dropped with a warning.
-_EVENT_EMIT_DEPTH_CAP = 8
-# Max queued + running events per manager generation; emit never waits — a full budget drops.
-_EVENT_PENDING_CAP = 64
-_EVENT_WORKER_STOP = object()
-
_env_enabled = env_var_enabled # imported by plugins/memory
@@ -322,46 +279,6 @@ _env_enabled = env_var_enabled # imported by plugins/memory
# ---------------------------------------------------------------------------
-@dataclass(frozen=True)
-class PluginSystemPromptSection:
- """A plugin-owned section rendered once for each new session."""
-
- id: str
- content: Union[str, Callable[[Mapping[str, Any]], str]]
- position: str
- max_chars: int
- plugin: str
-
-
-@dataclass(frozen=True)
-class RenderedPluginSystemPromptSection:
- """Validated prompt bytes frozen on the owning AIAgent."""
-
- id: str
- content: str
- position: str
- plugin: str
-
-
-@dataclass(frozen=True)
-class _EventSubscription:
- """Host-owned subscription ledger entry."""
-
- owner: str
- callback: Callable
-
-
-@dataclass(frozen=True)
-class _QueuedPluginEvent:
- """Immutable dispatch envelope consumed by the event worker."""
-
- event: str
- payload: Dict[str, Any]
- subscriptions: tuple[_EventSubscription, ...]
- depth: int
- generation: int
-
-
@dataclass
class LoadedPlugin:
"""Runtime state for a single loaded plugin."""
@@ -1706,13 +1623,6 @@ for _row in _SCOPED_PROVIDER_REGISTRARS:
setattr(PluginContext, _row[0], _make_scoped_provider_registrar(*_row))
del _row
-# Hook callback timeout (non-blocking abandon). Default cap per Python hook callback; overridden by
-# ``plugins.hook_callback_timeout``. Shell hooks enforce their own subprocess timeout.
-_HOOK_CALLBACK_TIMEOUT_SECS = 30.0
-_HOOK_SKIPPED = object() # returned by _run_hook_callback_bounded on skip/timeout
-_MAX_HOOK_CALLBACK_TIMEOUT_SECS = 600.0
-
-
def _resolve_hook_callback_timeout() -> float:
"""Effective hook-callback timeout from ``plugins.hook_callback_timeout`` (default 30s; ``<= 0``
disables the threaded path; clamped to ``_MAX_HOOK_CALLBACK_TIMEOUT_SECS``)."""
@@ -1751,22 +1661,7 @@ def _resolve_hook_callback_timeout() -> float:
return timeout
-def _hook_uses_callback_timeout(hook_name: str, timeout: float) -> bool:
- """Whether *hook_name* should run under the non-blocking timeout path."""
- if timeout <= 0 or hook_name in _HOOK_CALLER_THREAD_HOOKS:
- return False
- return (
- hook_name in _HOOK_TIMEOUT_BOUNDED_HOOKS
- or hook_name in _HOOK_TIMEOUT_FAIL_CLOSED_HOOKS
- )
-
-
-def _pre_tool_call_timeout_block() -> Dict[str, str]:
- """Fail-closed directive when a policy callback times out or is still running."""
- return {"action": "block", "message": _PRE_TOOL_CALL_TIMEOUT_BLOCK_MESSAGE}
-
-
-class PluginManager(PluginLoaderMixin):
+class PluginManager(PluginLoaderMixin, PluginDispatchMixin):
"""Central manager that discovers, loads, and invokes plugins."""
def __init__(self, scope_key: Optional[str] = None) -> None:
@@ -2319,8 +2214,6 @@ class PluginManager(PluginLoaderMixin):
plugin_id: str,
) -> None:
"""Register one plugin-owned approval transport for this profile."""
- import re
-
from hermes_cli.approval_transport import RegisteredApprovalTransport
clean = str(name).strip().lower()
@@ -2416,392 +2309,6 @@ class PluginManager(PluginLoaderMixin):
# -- hook invocation ----------------------------------------------------------
- @staticmethod
- def _invoke_hook_callback(callback: Callable, payload: Dict[str, Any]) -> Any:
- """Invoke a hook while withholding additive fields from narrow legacy callbacks."""
- try:
- parameters = inspect.signature(callback).parameters
- except (TypeError, ValueError):
- return callback(**payload) # no introspectable signature: historical behavior
-
- if any(
- parameter.kind == inspect.Parameter.VAR_KEYWORD
- for parameter in parameters.values()
- ):
- return callback(**payload)
-
- accepted_payload = {
- name: value
- for name, value in payload.items()
- if name in parameters
- and parameters[name].kind
- in {
- inspect.Parameter.POSITIONAL_OR_KEYWORD,
- inspect.Parameter.KEYWORD_ONLY,
- }
- }
- return callback(**accepted_payload)
-
- def invoke_hook(self, hook_name: str, **kwargs: Any) -> List[Any]:
- """Call all callbacks for *hook_name*; return their non-``None`` results.
-
- Payloads evolve additively: ``**kwargs`` callbacks get everything, narrow signatures only
- what they declare. Each callback is isolated. Hooks in ``_HOOK_TIMEOUT_BOUNDED_HOOKS`` and
- ``pre_tool_call`` are bounded by ``plugins.hook_callback_timeout`` (worker abandoned, never
- joined); ``pre_tool_call`` fails closed with a block directive, others skip.
- ``_HOOK_CALLER_THREAD_HOOKS`` always run on the caller thread. ``pre_llm_call`` callbacks may
- return ``{"context": "..."}`` (or a plain string) to inject into the turn.
- """
- # Gateway platform events define event-local envelopes; a bus-wide version here would turn
- # unrelated adapter payloads into one monolithic compatibility contract.
- if hook_name != "gateway_platform_event":
- kwargs.setdefault("telemetry_schema_version", OBSERVER_SCHEMA_VERSION)
- callbacks = self._hooks.get(hook_name, [])
- results: List[Any] = []
- timeout = _resolve_hook_callback_timeout()
- use_timeout = _hook_uses_callback_timeout(hook_name, timeout)
- fail_closed = hook_name in _HOOK_TIMEOUT_FAIL_CLOSED_HOOKS
-
- for cb in callbacks:
- try:
- if use_timeout:
- ret = self._run_hook_callback_bounded(hook_name, cb, kwargs, timeout)
- if ret is _HOOK_SKIPPED:
- if fail_closed:
- results.append(_pre_tool_call_timeout_block())
- continue
- else:
- ret = self._invoke_hook_callback(cb, kwargs)
- if ret is not None:
- results.append(ret)
- except Exception as exc:
- logger.warning(
- "Hook '%s' callback %s raised: %s",
- hook_name,
- getattr(cb, "__name__", repr(cb)),
- exc,
- )
- return results
-
- def _run_hook_callback_bounded(
- self, hook_name: str, cb: Callable, kwargs: Dict[str, Any], timeout: float
- ) -> Any:
- """Run one callback on a daemon worker with a wall-clock cap; ``_HOOK_SKIPPED`` when
- suppressed, still running, or timed out (worker abandoned, never joined). Exceptions
- propagate."""
- callback_name = getattr(cb, "__name__", repr(cb))
- callback_key = (hook_name, id(cb))
- token = object()
- now = time.monotonic()
- with self._hook_timeout_lock:
- suppressed_until = self._hook_timeout_suppressed_until.get(callback_key)
- running = callback_key in self._hook_running_callbacks
- if (suppressed_until is not None and suppressed_until > now) or running:
- logger.warning(
- "Hook '%s' callback %s skipped after previous "
- "timeout or while still running",
- hook_name,
- callback_name,
- )
- return _HOOK_SKIPPED
- if suppressed_until is not None:
- self._hook_timeout_suppressed_until.pop(callback_key, None)
- self._hook_running_callbacks[callback_key] = token
-
- context = contextvars.copy_context()
- done = threading.Event()
- outcome: Dict[str, Any] = {}
- failure: Dict[str, Exception] = {}
-
- def _runner() -> None:
- try:
- outcome["value"] = context.run(self._invoke_hook_callback, cb, kwargs)
- except Exception as exc:
- failure["exc"] = exc
- finally:
- with self._hook_timeout_lock:
- if self._hook_running_callbacks.get(callback_key) is token:
- self._hook_running_callbacks.pop(callback_key, None)
- done.set()
-
- thread = threading.Thread(
- target=_runner,
- name=f"hermes-hook-{callback_name}"[:40],
- daemon=True,
- )
- thread.start()
- if not done.wait(timeout=timeout): # do not join — that would reintroduce the hang
- with self._hook_timeout_lock:
- self._hook_timeout_suppressed_until[callback_key] = (
- time.monotonic() + self._hook_timeout_suppression_seconds
- )
- logger.warning(
- "Hook '%s' callback %s timed out after %gs — skipping",
- hook_name,
- callback_name,
- timeout,
- )
- return _HOOK_SKIPPED
- if "exc" in failure:
- raise failure["exc"]
- return outcome.get("value")
-
- def _subscribe_event(self, owner: str, event: str, callback: Callable) -> None:
- """Add an owner-tagged event subscription in registration order."""
- if not callable(callback):
- raise TypeError("Event subscriber callback must be callable")
- entry = _EventSubscription(owner=owner, callback=callback)
- with self._event_lock:
- self._subscriptions.setdefault(event, []).append(entry)
-
- def _remove_plugin_subscriptions(self, owner: str) -> int:
- """Remove every subscription owned by *owner*; return the count. Queued envelopes re-check
- membership per callback, so this also cancels already-snapshotted deliveries."""
- removed = 0
- with self._event_lock:
- for event in list(self._subscriptions):
- entries = self._subscriptions[event]
- retained = [entry for entry in entries if entry.owner != owner]
- removed += len(entries) - len(retained)
- if retained:
- self._subscriptions[event] = retained
- else:
- del self._subscriptions[event]
- return removed
-
- def _ensure_event_worker_locked(self) -> None:
- worker = self._event_worker
- if worker is not None and worker.is_alive():
- return
- dispatch_queue = self._event_queue
- worker = threading.Thread(
- target=self._event_worker_loop,
- args=(dispatch_queue,),
- name="hermes-plugin-events",
- daemon=True,
- )
- self._event_worker = worker
- worker.start()
-
- def _event_worker_loop(self, dispatch_queue: queue.Queue[Any]) -> None:
- while True:
- item = dispatch_queue.get()
- try:
- if item is _EVENT_WORKER_STOP:
- return
- self._deliver_event(item)
- finally:
- if item is not _EVENT_WORKER_STOP:
- self._mark_event_done(item.generation)
- dispatch_queue.task_done()
-
- def _mark_event_done(self, generation: int) -> None:
- with self._event_idle:
- pending = self._event_pending_by_generation.get(generation, 0)
- if pending > 0:
- self._event_pending_by_generation[generation] = pending - 1
- self._event_idle.notify_all()
-
- def _deliver_event(self, item: _QueuedPluginEvent) -> None:
- """Deliver one queued event on the host-owned worker thread."""
- with self._event_lock:
- if item.generation != self._event_generation:
- return
- previous_depth = getattr(self._emit_depth, "value", 0)
- self._emit_depth.value = item.depth
- try:
- for subscription in item.subscriptions:
- with self._event_lock:
- if item.generation != self._event_generation:
- break
- # Owner unload may have removed this entry after the event was queued.
- if not any(
- current is subscription
- for current in self._subscriptions.get(item.event, [])
- ):
- continue
- callback = subscription.callback
- try:
- # Fresh deep copy per subscriber: no callback can mutate what the next sees.
- owned_payload = copy.deepcopy(item.payload)
- result = callback(**owned_payload)
- resolve_plugin_command_result(result)
- except Exception as exc:
- logger.warning(
- "Event '%s' subscriber %s raised: %s",
- item.event,
- getattr(callback, "__name__", repr(callback)),
- exc,
- )
- finally:
- self._emit_depth.value = previous_depth
-
- def _wait_for_event_dispatch(self, timeout: float = 2.0) -> bool:
- """Wait for the current event generation to become idle (test helper)."""
- with self._event_idle:
- generation = self._event_generation
- return self._event_idle.wait_for(
- lambda: self._event_pending_by_generation.get(generation, 0) == 0,
- timeout=timeout,
- )
-
- def _dispatch_event(self, event: str, payload: Dict[str, Any]) -> int:
- """Queue *event* without blocking; return the subscriber count scheduled. Pending work is
- bounded per generation so a blocking subscriber costs one worker and later emits drop."""
- depth = getattr(self._emit_depth, "value", 0)
- if depth >= _EVENT_EMIT_DEPTH_CAP:
- logger.warning(
- "Event bus recursion cap (%d) exceeded while dispatching '%s' "
- "— dropping this emit to prevent an infinite loop",
- _EVENT_EMIT_DEPTH_CAP, event,
- )
- return 0
-
- with self._event_lock:
- subscriptions = tuple(self._subscriptions.get(event, []))
- if not subscriptions:
- return 0
- generation = self._event_generation
- pending = self._event_pending_by_generation.get(generation, 0)
- if pending >= _EVENT_PENDING_CAP:
- logger.warning(
- "Event bus pending budget (%d) exhausted while dispatching "
- "'%s' — dropping this emit",
- _EVENT_PENDING_CAP,
- event,
- )
- return 0
- item = _QueuedPluginEvent(
- event=event,
- payload=dict(payload),
- subscriptions=subscriptions,
- depth=depth + 1,
- generation=generation,
- )
- try:
- self._event_queue.put_nowait(item)
- except queue.Full:
- logger.warning(
- "Event bus pending budget (%d) exhausted while dispatching "
- "'%s' — dropping this emit",
- _EVENT_PENDING_CAP,
- event,
- )
- return 0
- self._event_pending_by_generation[generation] = pending + 1
- self._ensure_event_worker_locked()
- return len(subscriptions)
-
- def has_hook(self, hook_name: str) -> bool:
- """Return True when at least one callback is registered for a hook."""
- return bool(self._hooks.get(hook_name))
-
- def iter_hook_callbacks(self, hook_name: str) -> tuple[Callable, ...]:
- """Return a stable snapshot of callbacks registered for a hook."""
- return tuple(self._hooks.get(hook_name, ()))
-
- def render_system_prompt_sections(
- self, session_info: Mapping[str, Any]
- ) -> List[RenderedPluginSystemPromptSection]:
- """Render all registered sections deterministically and fail open."""
- frozen_info = types.MappingProxyType(dict(session_info))
- rendered: List[RenderedPluginSystemPromptSection] = []
- total_chars = len(PLUGIN_SECTIONS_START) + len(PLUGIN_SECTIONS_END) + 2
- for section_id in sorted(self._system_prompt_sections):
- section = self._system_prompt_sections[section_id]
- if len(rendered) >= MAX_SYSTEM_PROMPT_SECTIONS:
- logger.warning(
- "Plugin system prompt section %s exceeded the section-count "
- "budget (%d) and was skipped",
- section.id,
- MAX_SYSTEM_PROMPT_SECTIONS,
- )
- continue
- text = self._render_prompt_section_text(section, frozen_info)
- if text is None:
- continue
- rendered_chars = len(format_system_prompt_section(section.id, text))
- if rendered:
- rendered_chars += 2 # canonical ``\n\n`` separator
- if total_chars + rendered_chars > MAX_SYSTEM_PROMPT_SECTIONS_TOTAL_CHARS:
- logger.warning(
- "Plugin system prompt section %s (%s) exceeded the aggregate "
- "session budget (%d chars) and was skipped",
- section.id,
- section.plugin,
- MAX_SYSTEM_PROMPT_SECTIONS_TOTAL_CHARS,
- )
- continue
- rendered.append(
- RenderedPluginSystemPromptSection(
- id=section.id,
- content=text,
- position=section.position,
- plugin=section.plugin,
- )
- )
- total_chars += rendered_chars
- logger.info(
- "Session plugin prompt section: id=%s plugin=%s position=%s chars=%d",
- section.id,
- section.plugin,
- section.position,
- len(text),
- )
- return rendered
-
- @staticmethod
- def _render_prompt_section_text(
- section: PluginSystemPromptSection, frozen_info: Mapping[str, Any]
- ) -> Optional[str]:
- """Evaluate one section; return its stripped text or None (with a warning) when skipped."""
- def _skip(detail: str, *args: Any) -> None:
- logger.warning(
- "Plugin system prompt section %s (%s) " + detail,
- section.id, section.plugin, *args,
- )
-
- try:
- value = section.content(frozen_info) if callable(section.content) else section.content
- except Exception as exc:
- _skip("raised and was skipped: %s", exc)
- return None
- if not isinstance(value, str):
- _skip("returned %s, not str; skipped", type(value).__name__)
- return None
- text = value.strip()
- if not text:
- return None
- if PLUGIN_SECTIONS_START in text or PLUGIN_SECTIONS_END in text:
- _skip("contained a reserved persistence marker and was skipped")
- return None
- if len(text) > section.max_chars:
- _skip("exceeded max_chars (%d > %d) and was skipped", len(text), section.max_chars)
- return None
- return text
-
- def has_middleware(self, kind: str) -> bool:
- """Return True when at least one callback is registered for middleware."""
- return bool(self._middleware.get(kind))
-
- def invoke_middleware(self, kind: str, **kwargs: Any) -> List[Any]:
- """Call middleware callbacks for *kind* (each isolated); return non-``None`` results."""
- callbacks = self._middleware.get(kind, [])
- results: List[Any] = []
- for cb in callbacks:
- try:
- ret = cb(**kwargs)
- if ret is not None:
- results.append(ret)
- except Exception as exc:
- logger.warning(
- "Middleware '%s' callback %s raised: %s",
- kind,
- getattr(cb, "__name__", repr(cb)),
- exc,
- )
- return results
-
# -- adapter accessors / introspection -----------------------------------------
def get_slack_action_handlers(self) -> List[tuple]:
diff --git a/hermes_cli/plugins_dispatch.py b/hermes_cli/plugins_dispatch.py
new file mode 100644
index 00000000000..81bcd13dc95
--- /dev/null
+++ b/hermes_cli/plugins_dispatch.py
@@ -0,0 +1,584 @@
+"""Plugin hook / middleware / event-bus / system-prompt-section dispatch.
+
+Mixed into :class:`hermes_cli.plugins.PluginManager`. ``_resolve_hook_callback_timeout`` stays on the
+origin (tests patch it there) and is looked up lazily.
+"""
+
+from __future__ import annotations
+
+import contextvars
+import copy
+import inspect
+import logging
+import queue
+import re
+import threading
+import time
+import types
+from dataclasses import dataclass
+from typing import Any, Callable, Dict, List, Mapping, Optional, Set, Union
+
+from hermes_cli.middleware import OBSERVER_SCHEMA_VERSION
+
+logger = logging.getLogger("hermes_cli.plugins")
+
+
+# Allowlist of agent-turn hot-path hooks bounded by plugins.hook_callback_timeout (fail-open:
+# abandon without join — joining reintroduced a shutdown hang). Unlisted hooks run synchronously.
+# Intentionally unbounded: on_session_finalize/reset (last-chance flush — abandon can lose state);
+# subagent_start (observer); pre_gateway_dispatch (policy gate — neither fail mode is acceptable);
+# pre/post_approval_* (approval UX has its own timeout); kanban_* (own heartbeat/stale reclaim).
+_HOOK_TIMEOUT_BOUNDED_HOOKS: Set[str] = {
+ "post_tool_call",
+ "transform_terminal_output",
+ "transform_tool_result",
+ "transform_llm_output",
+ "pre_llm_call",
+ "post_llm_call",
+ "pre_api_request",
+ "post_api_request",
+ "api_request_error",
+ "pre_verify",
+ "on_session_start",
+ "on_session_end",
+}
+
+
+# Policy hooks: timeout / still-running must fail closed (block the tool).
+_HOOK_TIMEOUT_FAIL_CLOSED_HOOKS: Set[str] = {"pre_tool_call"}
+
+
+# Documented parent-thread serialization contract — never run on a timeout worker (hooks.md).
+_HOOK_CALLER_THREAD_HOOKS: Set[str] = {"subagent_stop"}
+
+
+# After a timeout, suppress the same callback this long so a hung hook cannot pile up threads.
+_HOOK_TIMEOUT_SUPPRESSION_SECONDS = 60.0
+
+
+_PRE_TOOL_CALL_TIMEOUT_BLOCK_MESSAGE = (
+ "pre_tool_call plugin callback timed out or is still running"
+)
+
+
+# System-prompt sections are tightly bounded: they become high-trust prompt bytes charged every turn.
+SYSTEM_PROMPT_SECTION_POSITIONS = frozenset({"after_memory"})
+
+
+DEFAULT_SYSTEM_PROMPT_SECTION_MAX_CHARS = 4_000
+
+
+MAX_SYSTEM_PROMPT_SECTION_CHARS = 4_000
+
+
+MAX_SYSTEM_PROMPT_SECTIONS = 32
+
+
+MAX_SYSTEM_PROMPT_SECTIONS_TOTAL_CHARS = 8_000
+
+
+_SYSTEM_PROMPT_SECTION_ID_RE = re.compile(r"^[a-z0-9][a-z0-9._-]{0,127}$")
+
+
+_SYSTEM_PROMPT_SECTION_HEADING_PREFIX = "## Plugin Context: "
+
+
+PLUGIN_SECTIONS_START = "<!-- hermes-plugin-sections:start -->"
+
+
+PLUGIN_SECTIONS_END = "<!-- hermes-plugin-sections:end -->"
+
+
+def is_valid_system_prompt_section_id(value: Any) -> bool:
+ """Return whether *value* is a stable, heading-safe section identifier."""
+ return isinstance(value, str) and bool(_SYSTEM_PROMPT_SECTION_ID_RE.fullmatch(value))
+
+
+def format_system_prompt_section(section_id: str, content: str) -> str:
+ """Render an auditable, length-framed block recoverable from the full prompt."""
+ return (
+ f"{_SYSTEM_PROMPT_SECTION_HEADING_PREFIX}{section_id}\n"
+ f"<!-- hermes-plugin-section-chars:{len(content)} -->\n\n"
+ f"{content}"
+ )
+
+
+def format_system_prompt_sections(sections: list) -> str:
+ """Render the canonical container used for persistence recovery."""
+ if not sections:
+ return ""
+ blocks = [format_system_prompt_section(item.id, item.content) for item in sections]
+ return f"{PLUGIN_SECTIONS_START}\n" + "\n\n".join(blocks) + f"\n{PLUGIN_SECTIONS_END}"
+
+
+# Reserved event namespace prefix — only core may publish ``hermes:<event>``.
+HERMES_EVENT_NAMESPACE = "hermes"
+
+
+# Event recursion depth cap (subscribers may emit); over-deep emits are dropped with a warning.
+_EVENT_EMIT_DEPTH_CAP = 8
+
+
+# Max queued + running events per manager generation; emit never waits — a full budget drops.
+_EVENT_PENDING_CAP = 64
+
+
+_EVENT_WORKER_STOP = object()
+
+
+@dataclass(frozen=True)
+class PluginSystemPromptSection:
+ """A plugin-owned section rendered once for each new session."""
+
+ id: str
+ content: Union[str, Callable[[Mapping[str, Any]], str]]
+ position: str
+ max_chars: int
+ plugin: str
+
+
+@dataclass(frozen=True)
+class RenderedPluginSystemPromptSection:
+ """Validated prompt bytes frozen on the owning AIAgent."""
+
+ id: str
+ content: str
+ position: str
+ plugin: str
+
+
+@dataclass(frozen=True)
+class _EventSubscription:
+ """Host-owned subscription ledger entry."""
+
+ owner: str
+ callback: Callable
+
+
+@dataclass(frozen=True)
+class _QueuedPluginEvent:
+ """Immutable dispatch envelope consumed by the event worker."""
+
+ event: str
+ payload: Dict[str, Any]
+ subscriptions: tuple[_EventSubscription, ...]
+ depth: int
+ generation: int
+
+
+# Hook callback timeout (non-blocking abandon). Default cap per Python hook callback; overridden by
+# ``plugins.hook_callback_timeout``. Shell hooks enforce their own subprocess timeout.
+_HOOK_CALLBACK_TIMEOUT_SECS = 30.0
+
+
+_HOOK_SKIPPED = object() # returned by _run_hook_callback_bounded on skip/timeout
+
+
+_MAX_HOOK_CALLBACK_TIMEOUT_SECS = 600.0
+
+
+def _hook_uses_callback_timeout(hook_name: str, timeout: float) -> bool:
+ """Whether *hook_name* should run under the non-blocking timeout path."""
+ if timeout <= 0 or hook_name in _HOOK_CALLER_THREAD_HOOKS:
+ return False
+ return (
+ hook_name in _HOOK_TIMEOUT_BOUNDED_HOOKS
+ or hook_name in _HOOK_TIMEOUT_FAIL_CLOSED_HOOKS
+ )
+
+
+def _pre_tool_call_timeout_block() -> Dict[str, str]:
+ """Fail-closed directive when a policy callback times out or is still running."""
+ return {"action": "block", "message": _PRE_TOOL_CALL_TIMEOUT_BLOCK_MESSAGE}
+
+
+class PluginDispatchMixin:
+ @staticmethod
+ def _invoke_hook_callback(callback: Callable, payload: Dict[str, Any]) -> Any:
+ """Invoke a hook while withholding additive fields from narrow legacy callbacks."""
+ try:
+ parameters = inspect.signature(callback).parameters
+ except (TypeError, ValueError):
+ return callback(**payload) # no introspectable signature: historical behavior
+
+ if any(
+ parameter.kind == inspect.Parameter.VAR_KEYWORD
+ for parameter in parameters.values()
+ ):
+ return callback(**payload)
+
+ accepted_payload = {
+ name: value
+ for name, value in payload.items()
+ if name in parameters
+ and parameters[name].kind
+ in {
+ inspect.Parameter.POSITIONAL_OR_KEYWORD,
+ inspect.Parameter.KEYWORD_ONLY,
+ }
+ }
+ return callback(**accepted_payload)
+
+ def invoke_hook(self, hook_name: str, **kwargs: Any) -> List[Any]:
+ """Call all callbacks for *hook_name*; return their non-``None`` results.
+
+ Payloads evolve additively: ``**kwargs`` callbacks get everything, narrow signatures only
+ what they declare. Each callback is isolated. Hooks in ``_HOOK_TIMEOUT_BOUNDED_HOOKS`` and
+ ``pre_tool_call`` are bounded by ``plugins.hook_callback_timeout`` (worker abandoned, never
+ joined); ``pre_tool_call`` fails closed with a block directive, others skip.
+ ``_HOOK_CALLER_THREAD_HOOKS`` always run on the caller thread. ``pre_llm_call`` callbacks may
+ return ``{"context": "..."}`` (or a plain string) to inject into the turn.
+ """
+ from hermes_cli.plugins import _resolve_hook_callback_timeout
+
+ # Gateway platform events define event-local envelopes; a bus-wide version here would turn
+ # unrelated adapter payloads into one monolithic compatibility contract.
+ if hook_name != "gateway_platform_event":
+ kwargs.setdefault("telemetry_schema_version", OBSERVER_SCHEMA_VERSION)
+ callbacks = self._hooks.get(hook_name, [])
+ results: List[Any] = []
+ timeout = _resolve_hook_callback_timeout()
+ use_timeout = _hook_uses_callback_timeout(hook_name, timeout)
+ fail_closed = hook_name in _HOOK_TIMEOUT_FAIL_CLOSED_HOOKS
+
+ for cb in callbacks:
+ try:
+ if use_timeout:
+ ret = self._run_hook_callback_bounded(hook_name, cb, kwargs, timeout)
+ if ret is _HOOK_SKIPPED:
+ if fail_closed:
+ results.append(_pre_tool_call_timeout_block())
+ continue
+ else:
+ ret = self._invoke_hook_callback(cb, kwargs)
+ if ret is not None:
+ results.append(ret)
+ except Exception as exc:
+ logger.warning(
+ "Hook '%s' callback %s raised: %s",
+ hook_name,
+ getattr(cb, "__name__", repr(cb)),
+ exc,
+ )
+ return results
+
+ def _run_hook_callback_bounded(
+ self, hook_name: str, cb: Callable, kwargs: Dict[str, Any], timeout: float
+ ) -> Any:
+ """Run one callback on a daemon worker with a wall-clock cap; ``_HOOK_SKIPPED`` when
+ suppressed, still running, or timed out (worker abandoned, never joined). Exceptions
+ propagate."""
+ callback_name = getattr(cb, "__name__", repr(cb))
+ callback_key = (hook_name, id(cb))
+ token = object()
+ now = time.monotonic()
+ with self._hook_timeout_lock:
+ suppressed_until = self._hook_timeout_suppressed_until.get(callback_key)
+ running = callback_key in self._hook_running_callbacks
+ if (suppressed_until is not None and suppressed_until > now) or running:
+ logger.warning(
+ "Hook '%s' callback %s skipped after previous "
+ "timeout or while still running",
+ hook_name,
+ callback_name,
+ )
+ return _HOOK_SKIPPED
+ if suppressed_until is not None:
+ self._hook_timeout_suppressed_until.pop(callback_key, None)
+ self._hook_running_callbacks[callback_key] = token
+
+ context = contextvars.copy_context()
+ done = threading.Event()
+ outcome: Dict[str, Any] = {}
+ failure: Dict[str, Exception] = {}
+
+ def _runner() -> None:
+ try:
+ outcome["value"] = context.run(self._invoke_hook_callback, cb, kwargs)
+ except Exception as exc:
+ failure["exc"] = exc
+ finally:
+ with self._hook_timeout_lock:
+ if self._hook_running_callbacks.get(callback_key) is token:
+ self._hook_running_callbacks.pop(callback_key, None)
+ done.set()
+
+ thread = threading.Thread(
+ target=_runner,
+ name=f"hermes-hook-{callback_name}"[:40],
+ daemon=True,
+ )
+ thread.start()
+ if not done.wait(timeout=timeout): # do not join — that would reintroduce the hang
+ with self._hook_timeout_lock:
+ self._hook_timeout_suppressed_until[callback_key] = (
+ time.monotonic() + self._hook_timeout_suppression_seconds
+ )
+ logger.warning(
+ "Hook '%s' callback %s timed out after %gs — skipping",
+ hook_name,
+ callback_name,
+ timeout,
+ )
+ return _HOOK_SKIPPED
+ if "exc" in failure:
+ raise failure["exc"]
+ return outcome.get("value")
+
+ def _subscribe_event(self, owner: str, event: str, callback: Callable) -> None:
+ """Add an owner-tagged event subscription in registration order."""
+ if not callable(callback):
+ raise TypeError("Event subscriber callback must be callable")
+ entry = _EventSubscription(owner=owner, callback=callback)
+ with self._event_lock:
+ self._subscriptions.setdefault(event, []).append(entry)
+
+ def _remove_plugin_subscriptions(self, owner: str) -> int:
+ """Remove every subscription owned by *owner*; return the count. Queued envelopes re-check
+ membership per callback, so this also cancels already-snapshotted deliveries."""
+ removed = 0
+ with self._event_lock:
+ for event in list(self._subscriptions):
+ entries = self._subscriptions[event]
+ retained = [entry for entry in entries if entry.owner != owner]
+ removed += len(entries) - len(retained)
+ if retained:
+ self._subscriptions[event] = retained
+ else:
+ del self._subscriptions[event]
+ return removed
+
+ def _ensure_event_worker_locked(self) -> None:
+ worker = self._event_worker
+ if worker is not None and worker.is_alive():
+ return
+ dispatch_queue = self._event_queue
+ worker = threading.Thread(
+ target=self._event_worker_loop,
+ args=(dispatch_queue,),
+ name="hermes-plugin-events",
+ daemon=True,
+ )
+ self._event_worker = worker
+ worker.start()
+
+ def _event_worker_loop(self, dispatch_queue: queue.Queue[Any]) -> None:
+ while True:
+ item = dispatch_queue.get()
+ try:
+ if item is _EVENT_WORKER_STOP:
+ return
+ self._deliver_event(item)
+ finally:
+ if item is not _EVENT_WORKER_STOP:
+ self._mark_event_done(item.generation)
+ dispatch_queue.task_done()
+
+ def _mark_event_done(self, generation: int) -> None:
+ with self._event_idle:
+ pending = self._event_pending_by_generation.get(generation, 0)
+ if pending > 0:
+ self._event_pending_by_generation[generation] = pending - 1
+ self._event_idle.notify_all()
+
+ def _deliver_event(self, item: _QueuedPluginEvent) -> None:
+ """Deliver one queued event on the host-owned worker thread."""
+ from hermes_cli.plugins import resolve_plugin_command_result
+
+ with self._event_lock:
+ if item.generation != self._event_generation:
+ return
+ previous_depth = getattr(self._emit_depth, "value", 0)
+ self._emit_depth.value = item.depth
+ try:
+ for subscription in item.subscriptions:
+ with self._event_lock:
+ if item.generation != self._event_generation:
+ break
+ # Owner unload may have removed this entry after the event was queued.
+ if not any(
+ current is subscription
+ for current in self._subscriptions.get(item.event, [])
+ ):
+ continue
+ callback = subscription.callback
+ try:
+ # Fresh deep copy per subscriber: no callback can mutate what the next sees.
+ owned_payload = copy.deepcopy(item.payload)
+ result = callback(**owned_payload)
+ resolve_plugin_command_result(result)
+ except Exception as exc:
+ logger.warning(
+ "Event '%s' subscriber %s raised: %s",
+ item.event,
+ getattr(callback, "__name__", repr(callback)),
+ exc,
+ )
+ finally:
+ self._emit_depth.value = previous_depth
+
+ def _wait_for_event_dispatch(self, timeout: float = 2.0) -> bool:
+ """Wait for the current event generation to become idle (test helper)."""
+ with self._event_idle:
+ generation = self._event_generation
+ return self._event_idle.wait_for(
+ lambda: self._event_pending_by_generation.get(generation, 0) == 0,
+ timeout=timeout,
+ )
+
+ def _dispatch_event(self, event: str, payload: Dict[str, Any]) -> int:
+ """Queue *event* without blocking; return the subscriber count scheduled. Pending work is
+ bounded per generation so a blocking subscriber costs one worker and later emits drop."""
+ depth = getattr(self._emit_depth, "value", 0)
+ if depth >= _EVENT_EMIT_DEPTH_CAP:
+ logger.warning(
+ "Event bus recursion cap (%d) exceeded while dispatching '%s' "
+ "— dropping this emit to prevent an infinite loop",
+ _EVENT_EMIT_DEPTH_CAP, event,
+ )
+ return 0
+
+ with self._event_lock:
+ subscriptions = tuple(self._subscriptions.get(event, []))
+ if not subscriptions:
+ return 0
+ generation = self._event_generation
+ pending = self._event_pending_by_generation.get(generation, 0)
+ if pending >= _EVENT_PENDING_CAP:
+ logger.warning(
+ "Event bus pending budget (%d) exhausted while dispatching "
+ "'%s' — dropping this emit",
+ _EVENT_PENDING_CAP,
+ event,
+ )
+ return 0
+ item = _QueuedPluginEvent(
+ event=event,
+ payload=dict(payload),
+ subscriptions=subscriptions,
+ depth=depth + 1,
+ generation=generation,
+ )
+ try:
+ self._event_queue.put_nowait(item)
+ except queue.Full:
+ logger.warning(
+ "Event bus pending budget (%d) exhausted while dispatching "
+ "'%s' — dropping this emit",
+ _EVENT_PENDING_CAP,
+ event,
+ )
+ return 0
+ self._event_pending_by_generation[generation] = pending + 1
+ self._ensure_event_worker_locked()
+ return len(subscriptions)
+
+ def has_hook(self, hook_name: str) -> bool:
+ """Return True when at least one callback is registered for a hook."""
+ return bool(self._hooks.get(hook_name))
+
+ def iter_hook_callbacks(self, hook_name: str) -> tuple[Callable, ...]:
+ """Return a stable snapshot of callbacks registered for a hook."""
+ return tuple(self._hooks.get(hook_name, ()))
+
+ def render_system_prompt_sections(
+ self, session_info: Mapping[str, Any]
+ ) -> List[RenderedPluginSystemPromptSection]:
+ """Render all registered sections deterministically and fail open."""
+ frozen_info = types.MappingProxyType(dict(session_info))
+ rendered: List[RenderedPluginSystemPromptSection] = []
+ total_chars = len(PLUGIN_SECTIONS_START) + len(PLUGIN_SECTIONS_END) + 2
+ for section_id in sorted(self._system_prompt_sections):
+ section = self._system_prompt_sections[section_id]
+ if len(rendered) >= MAX_SYSTEM_PROMPT_SECTIONS:
+ logger.warning(
+ "Plugin system prompt section %s exceeded the section-count "
+ "budget (%d) and was skipped",
+ section.id,
+ MAX_SYSTEM_PROMPT_SECTIONS,
+ )
+ continue
+ text = self._render_prompt_section_text(section, frozen_info)
+ if text is None:
+ continue
+ rendered_chars = len(format_system_prompt_section(section.id, text))
+ if rendered:
+ rendered_chars += 2 # canonical ``\n\n`` separator
+ if total_chars + rendered_chars > MAX_SYSTEM_PROMPT_SECTIONS_TOTAL_CHARS:
+ logger.warning(
+ "Plugin system prompt section %s (%s) exceeded the aggregate "
+ "session budget (%d chars) and was skipped",
+ section.id,
+ section.plugin,
+ MAX_SYSTEM_PROMPT_SECTIONS_TOTAL_CHARS,
+ )
+ continue
+ rendered.append(
+ RenderedPluginSystemPromptSection(
+ id=section.id,
+ content=text,
+ position=section.position,
+ plugin=section.plugin,
+ )
+ )
+ total_chars += rendered_chars
+ logger.info(
+ "Session plugin prompt section: id=%s plugin=%s position=%s chars=%d",
+ section.id,
+ section.plugin,
+ section.position,
+ len(text),
+ )
+ return rendered
+
+ @staticmethod
+ def _render_prompt_section_text(
+ section: PluginSystemPromptSection, frozen_info: Mapping[str, Any]
+ ) -> Optional[str]:
+ """Evaluate one section; return its stripped text or None (with a warning) when skipped."""
+ def _skip(detail: str, *args: Any) -> None:
+ logger.warning(
+ "Plugin system prompt section %s (%s) " + detail,
+ section.id, section.plugin, *args,
+ )
+
+ try:
+ value = section.content(frozen_info) if callable(section.content) else section.content
+ except Exception as exc:
+ _skip("raised and was skipped: %s", exc)
+ return None
+ if not isinstance(value, str):
+ _skip("returned %s, not str; skipped", type(value).__name__)
+ return None
+ text = value.strip()
+ if not text:
+ return None
+ if PLUGIN_SECTIONS_START in text or PLUGIN_SECTIONS_END in text:
+ _skip("contained a reserved persistence marker and was skipped")
+ return None
+ if len(text) > section.max_chars:
+ _skip("exceeded max_chars (%d > %d) and was skipped", len(text), section.max_chars)
+ return None
+ return text
+
+ def has_middleware(self, kind: str) -> bool:
+ """Return True when at least one callback is registered for middleware."""
+ return bool(self._middleware.get(kind))
+
+ def invoke_middleware(self, kind: str, **kwargs: Any) -> List[Any]:
+ """Call middleware callbacks for *kind* (each isolated); return non-``None`` results."""
+ callbacks = self._middleware.get(kind, [])
+ results: List[Any] = []
+ for cb in callbacks:
+ try:
+ ret = cb(**kwargs)
+ if ret is not None:
+ results.append(ret)
+ except Exception as exc:
+ logger.warning(
+ "Middleware '%s' callback %s raised: %s",
+ kind,
+ getattr(cb, "__name__", repr(cb)),
+ exc,
+ )
+ return results
diff --git a/tests/hermes_cli/test_plugin_event_bus.py b/tests/hermes_cli/test_plugin_event_bus.py
index 4a511dea148..f02454bc1a4 100644
--- a/tests/hermes_cli/test_plugin_event_bus.py
+++ b/tests/hermes_cli/test_plugin_event_bus.py
@@ -236,9 +236,9 @@ def test_emit_returns_before_blocking_subscriber_finishes():
def test_pending_budget_drops_new_event_without_blocking(monkeypatch, caplog):
- from hermes_cli import plugins as plugins_mod
+ from hermes_cli import plugins_dispatch
- monkeypatch.setattr(plugins_mod, "_EVENT_PENDING_CAP", 1)
+ monkeypatch.setattr(plugins_dispatch, "_EVENT_PENDING_CAP", 1)
manager = _fresh_manager()
ctx_a = _make_ctx(manager, "plugin_a", key="a")
ctx_b = _make_ctx(manager, "plugin_b", key="b")
@@ -394,7 +394,7 @@ def test_manifest_emits_listens_present():
def test_manifest_parse_reads_emits_listens(tmp_path):
- """_parse_manifest picks up optional emits/listens from plugin.yaml."""
+ """parse_manifest_file picks up optional emits/listens from plugin.yaml."""
import yaml
plugin_dir = tmp_path / "myplug"
@@ -411,8 +411,9 @@ def test_manifest_parse_reads_emits_listens(tmp_path):
encoding="utf-8",
)
- manager = _fresh_manager()
- manifest = manager._parse_manifest(manifest_file, plugin_dir, "user", "")
+ from hermes_cli.plugins import parse_manifest_file
+
+ manifest = parse_manifest_file(manifest_file, plugin_dir, "user", "")
assert manifest is not None
assert manifest.emits == ["ping"]
assert manifest.listens == ["other:evt"]
@@ -428,8 +429,9 @@ def test_manifest_parse_absent_emits_listens(tmp_path):
yaml.safe_dump({"name": "bare"}), encoding="utf-8"
)
- manager = _fresh_manager()
- manifest = manager._parse_manifest(manifest_file, plugin_dir, "user", "")
+ from hermes_cli.plugins import parse_manifest_file
+
+ manifest = parse_manifest_file(manifest_file, plugin_dir, "user", "")
assert manifest is not None
assert manifest.emits == []
assert manifest.listens == []