plugins_activation.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. """Mid-run plugin activation: what a just-loaded plugin does NOW vs next session (#87770).
  2. One seam for every surface: ``PluginManager.on_plugin_loaded`` fires with one
  3. :func:`plugin_activation_summary` per loaded plugin. Gateway handlers (slash commands, transform hooks,
  4. platform callbacks) are live as soon as the plugin loads; portable MCP servers and skills go live in
  5. the open chats of the plugin's profile (:func:`load_and_go_live`; MCP tools are deferred behind
  6. tool_search, so the cached prompt prefix is unchanged); Python tools and system-prompt sections stay
  7. deferred to the next session (prompt-cache invariant, same as ``/skills install``).
  8. :func:`activate_plugin_now` is what the install / enable / update surfaces call after a successful
  9. config or tree change.
  10. """
  11. from __future__ import annotations
  12. import logging
  13. import threading
  14. from pathlib import Path
  15. from typing import Any, Dict, List, Optional
  16. logger = logging.getLogger(__name__)
  17. _GO_LIVE_LOCK = threading.Lock()
  18. # Hooks the gateway consults per inbound/outbound message: live as soon as the registry holds them.
  19. _GATEWAY_TRANSFORM_HOOKS = frozenset({
  20. "transform_llm_output", "transform_tool_result", "transform_terminal_output", "pre_gateway_dispatch",
  21. "gateway_platform_event", "pre_command",
  22. })
  23. def plugin_activation_summary(manager: Any, plugin_key: str) -> dict[str, Any]:
  24. """``{name, key, activated_now: {kind: [names]}, deferred: {kind: [names]}}`` for one loaded plugin,
  25. read from what it actually registered (ownership ledger + handler registries), not from what its
  26. manifest promises. Keys appear only when non-empty.
  27. ``activated_now``: ``gateway_commands`` (slash names), ``gateway_transforms`` / ``hooks`` (hook names),
  28. ``callbacks`` (platforms with a ``register_platform_handler`` factory / Slack action ids), ``locales``
  29. (``<lang>.<surface>`` language-pack layers).
  30. ``deferred``: ``tools`` (tool names; next session), ``prompt`` (section ids; next session),
  31. ``mcp_servers`` (the plugin's mcp.json server names exactly as registered; until ``mcp.reload``)."""
  32. loaded = manager._plugins.get(plugin_key)
  33. manifest = getattr(loaded, "manifest", None)
  34. name = getattr(manifest, "name", None) or plugin_key
  35. regs = [r for r in manager._ownership_ledger.get(plugin_key, []) if getattr(r, "active", True)]
  36. kinds: dict[str, list[str]] = {}
  37. for reg in regs:
  38. kinds.setdefault(reg.kind, []).append(str(reg.key))
  39. now: dict[str, list[str]] = {}
  40. if kinds.get("command"):
  41. now["gateway_commands"] = sorted(kinds["command"])
  42. hooks = set(kinds.get("hook", ()))
  43. if hooks & _GATEWAY_TRANSFORM_HOOKS:
  44. now["gateway_transforms"] = sorted(hooks & _GATEWAY_TRANSFORM_HOOKS)
  45. if hooks - _GATEWAY_TRANSFORM_HOOKS:
  46. now["hooks"] = sorted(hooks - _GATEWAY_TRANSFORM_HOOKS)
  47. callbacks = sorted(platform for platform, factories in manager._platform_handler_factories.items()
  48. if any(plugin == name for _f, plugin in factories))
  49. callbacks += [f"slack:{a}" for a in kinds.get("slack_action_handler", ())]
  50. if callbacks:
  51. now["callbacks"] = callbacks
  52. if kinds.get("locale"): # language-pack layers: the i18n caches reset on registration, so live at once
  53. now["locales"] = sorted(kinds["locale"])
  54. deferred: dict[str, list[str]] = {}
  55. tools = sorted(set(kinds.get("tool", ())) | set(getattr(loaded, "tools_registered", None) or ())
  56. | set(getattr(manifest, "provides_tools", None) or ()))
  57. if tools:
  58. deferred["tools"] = tools
  59. if kinds.get("system_prompt_section"):
  60. deferred["prompt"] = sorted(kinds["system_prompt_section"])
  61. servers = sorted(set(kinds.get("portable_mcp", ())) | {
  62. s for s, owner in manager._portable_mcp_server_plugins.items() if owner == plugin_key})
  63. if servers:
  64. deferred["mcp_servers"] = servers
  65. summary = {"name": name, "key": plugin_key, "activated_now": now, "deferred": deferred}
  66. # Read in the process that loaded the plugin (for a gateway answer, the gateway): a dependency
  67. # installed after it booted is not importable there until it restarts.
  68. from hermes_cli.plugins_loader import missing_python_dependencies
  69. if missing := missing_python_dependencies(manifest):
  70. summary["missing_dependencies"] = missing
  71. return summary
  72. def activation_summaries(manager: Any) -> list[dict[str, Any]]:
  73. """One summary per loaded (non-deferred-platform, non-errored) plugin — the ``on_plugin_loaded`` payload."""
  74. out = []
  75. for key, loaded in list(manager._plugins.items()):
  76. if getattr(loaded, "deferred", False) or getattr(loaded, "error", None):
  77. continue
  78. out.append(plugin_activation_summary(manager, key))
  79. return out
  80. def dependency_blocked_summaries(manager: Any) -> list[dict[str, Any]]:
  81. """Plugins that failed to load in this process while a declared Python dependency is not
  82. importable here, as ``{name, key, activated_now: {}, deferred: {}, missing_dependencies}``. A
  83. plugin importing a just-installed dependency at load time fails in a process booted before the
  84. install; without this entry the caller would read "loaded nothing, needs nothing" instead of
  85. "restart". Never part of the ``on_plugin_loaded`` payload: nothing of these plugins loaded."""
  86. from hermes_cli.plugins_loader import missing_python_dependencies
  87. out = []
  88. for key, loaded in list(manager._plugins.items()):
  89. manifest = getattr(loaded, "manifest", None)
  90. if getattr(loaded, "error", None) and (missing := missing_python_dependencies(manifest)):
  91. out.append({"name": getattr(manifest, "name", None) or key, "key": key, "activated_now": {},
  92. "deferred": {}, "missing_dependencies": missing})
  93. return out
  94. def find_activation(summaries: Optional[list[dict[str, Any]]], name: str) -> Optional[dict[str, Any]]:
  95. """The summary for ``name`` (manifest name, canonical key, or bare leaf of the key)."""
  96. for entry in summaries or ():
  97. key = str(entry.get("key") or "")
  98. if name in (entry.get("name"), key, key.rsplit("/", 1)[-1]):
  99. return entry
  100. return None
  101. def activate_plugin_now(name: str, *, in_process: bool = True) -> dict[str, Any]:
  102. """After an install/enable/update: load the plugin in THIS process (so ``on_plugin_loaded``
  103. subscribers here — the TUI/Desktop server — see it), connect its MCP servers and hand them plus
  104. its skills to the open chats of this profile (:func:`load_and_go_live`), and nudge the running
  105. gateway to load it too over its control socket so live adapters re-wire their handlers. A caller
  106. in another process (``hermes plugins install``) passes ``in_process=False``; the running Desktop /
  107. dashboard backend is then asked to do the in-process half (:func:`notify_serve_backend`). Never raises.
  108. Returns ``{"gateway_reloaded": bool, "activation": summary | None, "restart_required": bool}``.
  109. ``activation.live_now`` lists what is usable in open chats now; ``activation.deferred`` what waits
  110. for the next session. ``restart_required`` is True when no gateway answered (old gateway, not
  111. running), or when this process or the gateway cannot import the plugin's Python dependencies
  112. (``activation.missing_dependencies``; ``activation.missing_in`` names which: ``gateway`` and/or
  113. ``backend``, the Desktop / dashboard process): a running process never switches to the dependency
  114. generation an enable just installed, so only restarting that process loads them."""
  115. from hermes_constants import get_hermes_home
  116. activation: Optional[dict[str, Any]] = load_and_go_live(name) if in_process else None
  117. if not in_process:
  118. activation = (notify_serve_backend(name, Path(get_hermes_home())) or {}).get("activation")
  119. answer = reload_running_gateway()
  120. reloaded = bool(answer and answer.get("reloaded"))
  121. gateway_activation = find_activation((answer or {}).get("activations"), name) if reloaded else None
  122. gaps = {"gateway": (gateway_activation or {}).get("missing_dependencies") or [],
  123. "backend": (activation or {}).get("missing_dependencies") or []}
  124. if reloaded and activation is None:
  125. activation = gateway_activation
  126. missing = sorted({*gaps["gateway"], *gaps["backend"]})
  127. if missing:
  128. activation = {**(activation or {"name": name, "key": name}), "missing_dependencies": missing,
  129. "missing_in": [process for process, deps in gaps.items() if deps]}
  130. return {"gateway_reloaded": reloaded, "activation": activation,
  131. "restart_required": not reloaded or bool(missing)}
  132. def reload_running_gateway() -> Optional[dict[str, Any]]:
  133. """Ask the gateway serving this profile (its own socket, else the multiplexer's for a served
  134. secondary) to force plugin re-discovery, so an enable loads and a disable unloads in the running
  135. gateway now. The ``reload-plugins`` answer, or None when no gateway answered."""
  136. try:
  137. from gateway.control_socket import reload_gateway_plugins
  138. from hermes_constants import get_default_hermes_root, get_hermes_home
  139. home = Path(get_hermes_home())
  140. answer = reload_gateway_plugins(home)
  141. if answer is None:
  142. root = Path(get_default_hermes_root())
  143. if root.resolve() != home.resolve(): # a served secondary: the multiplexer's socket
  144. answer = reload_gateway_plugins(root, profile_home=home)
  145. return answer
  146. except Exception:
  147. logger.debug("gateway reload-plugins nudge failed", exc_info=True)
  148. return None
  149. def gateway_reload_hint(answer: Optional[dict[str, Any]], name: str) -> str:
  150. """One honest sentence after disabling plugin ``name``, a change only the gateway's re-discovery
  151. applies (an open session stops running its commands at once). Callbacks the plugin already
  152. wired into platform adapters outlive a reload (the plugin guide), so a restart still ends those."""
  153. if answer and answer.get("reloaded"):
  154. return (f"The running gateway reloaded its plugins and unloaded {name}; platform hooks it already "
  155. "wired up stay until a gateway restart (hermes gateway restart).")
  156. if answer is not None: # a gateway answered and refused: say so, with its reason
  157. return (f"The running gateway refused to reload its plugins: {answer.get('error') or 'no reason given'}.\n"
  158. "Only the saved config changed; restart the gateway serving this profile to apply it:\n"
  159. " hermes gateway restart")
  160. return ("No running gateway answered. If one is running, restart it for this to take effect:\n"
  161. " hermes gateway restart")
  162. def load_and_go_live(name: str) -> Optional[dict[str, Any]]:
  163. """Force-rediscover plugins in THIS process under the caller's profile scope, connect ``name``'s
  164. MCP servers and hand them (and its skills) to this profile's open chats with a turn note. Returns
  165. the activation summary with ``live_now: {mcp_servers, skills}``; ``deferred`` then keeps only what
  166. waits for the next session (Python ``tools``, ``prompt``). None when the plugin did not load, except
  167. a load that failed on a dependency this process cannot import: that returns the
  168. :func:`dependency_blocked_summaries` entry, so the caller asks for a restart."""
  169. # A forced rediscovery unloads every plugin before it loads them again, which drops their server
  170. # configs, skills and liveness declarations for the length of the pass. Two installs finishing
  171. # together (one card, two rows) each go live; the second one's pass must not run while the first
  172. # reads or connects, or the first plugin comes up with no tools. One go-live at a time.
  173. with _GO_LIVE_LOCK:
  174. return _go_live(name)
  175. def _go_live(name: str) -> Optional[dict[str, Any]]:
  176. from hermes_cli.plugins_activation_live import connect_plugin_mcp, live_notice, plugin_skills
  177. try:
  178. from hermes_cli.plugins import _join_background_discovery, get_plugin_manager
  179. _join_background_discovery()
  180. manager = get_plugin_manager()
  181. # Other forced passes (a reload-plugins verb, the dashboard) do not take the go-live lock, so the
  182. # reads share the discovery lock with the pass that produced them.
  183. with manager._discovery_lock:
  184. manager.discover_and_load(force=True)
  185. activation = find_activation(activation_summaries(manager), name)
  186. blocked = None if activation else find_activation(dependency_blocked_summaries(manager), name)
  187. portable = manager.get_portable_mcp_servers()
  188. skills = plugin_skills(activation["key"]) if activation else []
  189. except Exception:
  190. logger.debug("in-process plugin reload after change to %r failed", name, exc_info=True)
  191. return None
  192. if activation is None:
  193. return blocked
  194. servers = connect_plugin_mcp(activation, portable)
  195. activation["live_now"] = {"mcp_servers": servers, "skills": skills}
  196. activation["deferred"] = {k: v for k, v in (activation.get("deferred") or {}).items() if k != "mcp_servers"}
  197. import sys
  198. server = sys.modules.get("tui_gateway.server") # loaded == this process hosts chats
  199. note = live_notice(activation)
  200. if server is not None and (servers or note):
  201. from hermes_constants import get_hermes_home
  202. try:
  203. server.refresh_plugin_sessions(Path(get_hermes_home()), activation["key"], note)
  204. except Exception:
  205. logger.warning("open chats were not refreshed after activating %r", name, exc_info=True)
  206. return activation
  207. def _serve_backend_record():
  208. """The host-owner record, else the Desktop child's (a Desktop-only box has no host owner)."""
  209. from gateway import host_rendezvous as hr
  210. for role in (hr.ROLE_SERVE, hr.ROLE_DESKTOP_SERVE):
  211. record = hr.read_record(role)
  212. if record is not None and record.port and hr.record_token_is_consistent(record):
  213. return record
  214. return None
  215. def notify_serve_backend(name: str, home: Path) -> Optional[dict[str, Any]]:
  216. """Ask the running dashboard / Desktop backend (``hermes serve``, found through its host record) to
  217. run :func:`load_and_go_live` for ``name`` in ``home``. None when no backend answers. Never raises."""
  218. try:
  219. import json
  220. import urllib.request
  221. from gateway import host_rendezvous as hr
  222. from hermes_cli.url_utils import format_url_host
  223. record = _serve_backend_record()
  224. if record is None:
  225. return None
  226. token = hr.read_token(record.role)
  227. request = urllib.request.Request(
  228. f"http://{format_url_host(hr.dial_host(record))}:{record.port}"
  229. "/api/dashboard/agent-plugins/activate",
  230. data=json.dumps({"name": name, "home": str(home)}).encode("utf-8"), method="POST",
  231. headers={"Content-Type": "application/json", "X-Hermes-Session-Token": token})
  232. with urllib.request.urlopen(request, timeout=60) as response:
  233. return json.loads(response.read().decode("utf-8"))
  234. except Exception:
  235. logger.debug("serve backend activation for %r failed", name, exc_info=True)
  236. return None
  237. def _gateway_present() -> bool:
  238. """A gateway runs for this profile (its own, or the multiplexer serving it from the root), or a
  239. gateway service is installed for it."""
  240. from gateway.status import get_running_pid
  241. from hermes_cli.gateway import _installed_service_kind
  242. from hermes_constants import get_default_hermes_root, get_hermes_home
  243. homes = {Path(get_hermes_home()).resolve(), Path(get_default_hermes_root()).resolve()}
  244. return (any(get_running_pid(home / "gateway.pid", cleanup_stale=False) for home in homes)
  245. or _installed_service_kind() is not None)
  246. _RESTART_LINES = {"gateway": " hermes gateway restart",
  247. "backend": " quit and reopen the Hermes desktop app (or restart hermes dashboard)"}
  248. def dependency_restart_hint(missing: list[str], processes: list[str]) -> str:
  249. """What to do when running processes cannot import a plugin's dependencies: one restart line per
  250. process that reported the gap (``gateway`` / ``backend``), never one that can already import them."""
  251. from hermes_cli.plugins_loader import installed_in_selected_environment
  252. deps = ", ".join(missing)
  253. restart = "\n".join(_RESTART_LINES[p] for p in processes)
  254. if installed_in_selected_environment(missing):
  255. return (f"Its Python dependencies ({deps}) were installed after the running gateway or dashboard "
  256. "started, and a running process never switches dependency environments. Restart them to "
  257. f"load the plugin:\n{restart}")
  258. return f"Its Python dependencies ({deps}) are not installed. Run hermes pm repair, then restart:\n{restart}"
  259. def report_activation(console, name: str) -> None:
  260. """After a CLI install/enable: load *name* where it can go live now (:func:`activate_plugin_now`)
  261. and print :func:`activation_hint`, or nothing when there is nothing to say."""
  262. if hint := activation_hint(activate_plugin_now(name, in_process=False)):
  263. console.print(f"[dim]{hint}[/dim]")
  264. def activation_hint(result: dict[str, Any]) -> str:
  265. """One honest sentence for CLI surfaces from an :func:`activate_plugin_now` result; empty when
  266. there is nothing to say."""
  267. act = result.get("activation") or {}
  268. live = act.get("live_now") or {}
  269. connected = [s for s in live.get("mcp_servers") or () if s.get("connected")]
  270. lines = []
  271. if connected or live.get("skills"):
  272. tools = sum(len(s.get("tools") or ()) for s in connected)
  273. lines.append(f"Live in open chats now: {tools} MCP tools, {len(live.get('skills') or ())} skills.")
  274. lines.extend(f"MCP server {s['name']} not connected: {s.get('error') or 'unknown error'}"
  275. for s in live.get("mcp_servers") or () if not s.get("connected"))
  276. if missing := act.get("missing_dependencies"):
  277. return "\n".join([*lines, dependency_restart_hint(missing, act.get("missing_in") or [])])
  278. if not result.get("gateway_reloaded"):
  279. # Live in open chats, or no gateway runs or is installed for this profile (first-time setup):
  280. # a messaging gateway that starts later loads it at boot, so a restart applies to nothing.
  281. if lines or not _gateway_present():
  282. return "\n".join(lines)
  283. return "\n".join([*lines, "Restart the gateway for the plugin to take effect:\n hermes gateway restart"])
  284. now, deferred = act.get("activated_now") or {}, act.get("deferred") or {}
  285. parts = []
  286. if now:
  287. parts.append("active in the running gateway now: " + ", ".join(sorted(now)))
  288. if deferred:
  289. labels: dict[str, str] = {"tools": "tools (next session)", "prompt": "system prompt (next session)",
  290. "mcp_servers": "MCP servers (next session)"}
  291. parts.append("deferred: " + ", ".join(labels.get(k, k) for k in sorted(deferred)))
  292. if not parts:
  293. return "\n".join([*lines, "Gateway reloaded plugins; nothing of this plugin needs a session or restart."])
  294. return "\n".join([*lines, "Gateway reloaded plugins — " + "; ".join(parts) + "."])