Files
hermes-agent/hermes_cli/plugin_rpc.py
BearHuddleston 6ed7199c07 fix(plugins): run a marked profile's plugin code under the name it was opened by
#156725 refuses to run a profile that a plugin runs elsewhere, and asks the
marker by the home's name. b912bfb00e7 passed that name (owner_home) to
host_launcher() for the plugin host's argv only. The plugin manager still
bound home_path, the resolved directory, around discovery, loading,
unload and a plugin's calls, and PluginHost._home was home_path too. For
profiles/<name> linking to a directory outside the Hermes root, code under
those bindings asks the marker about a directory with no marker folder
above it, and is told the profile runs here:

- A model-provider plugin's extraction (plugin_host_profiles._extract)
  started the launcher the profile's own config.yaml names. It runs while
  a bundled plugin loads (its imports list the providers) and when the
  host starts (_hosted_ambient_credentials). `hermes -p ops tools list`,
  `profile list` and `memory status` each started it once with a cold
  extraction cache.
- The host child's environment was built from that directory's .env,
  which reads as empty for a marked profile: a PYTHONPATH there ran a
  module from it in the host child.

Bind owner_home wherever the manager or its host runs code. home_path
stays the registries' key; for a home that is not a link both name the
same directory.
2026-11-27 19:28:21 -06:00

44 lines
2.3 KiB
Python

"""Plugin JSON-RPC methods: a plugin's frontend calling its own Python half over the gateway socket.
Bound into ``PluginContext`` (``from hermes_cli.plugin_rpc import ...``), so they are ordinary
``ctx.*`` methods and ``register_rpc`` gets the abandoned-load guard of every registrar. The session
authority serves the methods (``gateway/session_plugin_rpc.py``); unload drops them with the ledger.
"""
from __future__ import annotations
from typing import Any, Callable, Optional
from hermes_cli.plugin_events import PLUGIN_EVENT_PREFIX, emit_plugin_session_event, plugin_event_name
from hermes_cli.plugins_ledger import PluginRegistration
__all__ = ["emit_to_session", "plugin_rpc_handler", "register_rpc"]
def register_rpc(self, name: str, handler: Callable[..., Any]) -> PluginRegistration:
"""Serve ``handler(params, *, session_id, **kwargs)`` as ``plugin.<plugin_id>.<name>``: the namespace
is FORCED (``name`` is the bare dotted name). The return value, JSON-encodable, is the result."""
if not callable(handler):
raise TypeError(f"Plugin '{self.plugin_id}' register_rpc() handler must be callable")
method = plugin_event_name(self.plugin_id, name, kind="RPC method")
return self._register_entry("rpc_method", method, self._manager._plugin_rpc_methods, handler,
"Plugin %s registered RPC method: %s", method)
def emit_to_session(self, session_id: str, event: str, payload: Optional[dict[str, Any]] = None) -> bool:
"""Push ``plugin.<plugin_id>.<event>`` to the viewers of ``session_id`` only.
Looked up in the plugin's own profile (its manager's home), never the ambient scope: a thread the
plugin started carries none, and on a multiplexed gateway that would read as the launch profile."""
from hermes_cli.plugins_loader import _plugin_home_scope
with _plugin_home_scope(self._manager.owner_home):
return emit_plugin_session_event(self.plugin_id, session_id, event, payload)
def plugin_rpc_handler(method: Any) -> Optional[Callable[..., Any]]:
"""The loaded plugin handler serving wire ``method`` for the active profile, else None."""
if not isinstance(method, str) or not method.startswith(PLUGIN_EVENT_PREFIX):
return None
from hermes_cli.plugins import _ensure_plugins_discovered
return _ensure_plugins_discovered()._plugin_rpc_methods.get(method)