run_plugin_rewire.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. """Late plugin loads re-wire the running adapters (#87770).
  2. A plugin that finishes loading after an adapter connected — a tool-triggered force re-discovery, or
  3. ``hermes plugins install/enable`` / the Desktop / ``plugins.manage`` nudging the gateway over the control
  4. socket — used to leave its platform handlers (slash commands, button callbacks, inbound transforms)
  5. silently unwired until a restart. The runner subscribes to ``PluginManager.on_plugin_loaded`` per served
  6. profile and calls every live adapter's idempotent ``rewire_plugin_handlers()`` on the event loop.
  7. Scope is handlers only: a mid-run-loaded plugin's tools and system-prompt sections stay deferred to the
  8. next session (prompt-cache invariant; same as ``/skills install``).
  9. """
  10. from __future__ import annotations
  11. import asyncio
  12. import contextlib
  13. import logging
  14. import threading
  15. from pathlib import Path
  16. from typing import Any, Callable, Dict, Optional
  17. logger = logging.getLogger(__name__)
  18. class GatewayPluginRewireMixin:
  19. """Subscribe once per plugin manager; re-wire that profile's adapters on every loaded event."""
  20. _plugin_rewire_unsubscribe: Optional[dict[str, Callable[[], None]]] = None
  21. def _subscribe_plugin_rewire(self, manager: Any, profile_name: Optional[str] = None,
  22. profile_home: Optional[Path] = None) -> None:
  23. """Idempotent per manager scope: a served-profile rescan re-enters ``_load_secondary_profile_config``
  24. and must not stack a second listener (two listeners = two re-wire passes, still deduped, but noise)."""
  25. subs = self._plugin_rewire_unsubscribe
  26. if subs is None:
  27. subs = self._plugin_rewire_unsubscribe = {}
  28. scope = str(getattr(manager, "scope_key", "") or id(manager))
  29. if scope in subs:
  30. return
  31. try:
  32. loop = asyncio.get_running_loop()
  33. except RuntimeError:
  34. loop = None
  35. def _on_loaded(_names) -> None:
  36. # Fires on the discovering thread (tool worker, control-socket executor, or the loop itself):
  37. # adapters own PTB/Bolt handler tables, so always hop onto the gateway loop.
  38. if loop is None or loop.is_closed():
  39. return
  40. loop.call_soon_threadsafe(self._rewire_plugin_handlers, profile_name, profile_home)
  41. subs[scope] = manager.on_plugin_loaded(_on_loaded)
  42. def _rewire_plugin_handlers(self, profile_name: Optional[str] = None,
  43. profile_home: Optional[Path] = None) -> int:
  44. """Call ``rewire_plugin_handlers()`` on every live adapter of one profile (``None`` = the launch
  45. profile's ``self.adapters``); a secondary's adapters read their own manager, so bind its scope.
  46. Returns the number of adapters re-wired."""
  47. if profile_name is None:
  48. adapters = dict(getattr(self, "adapters", None) or {})
  49. else:
  50. adapters = dict((getattr(self, "_profile_adapters", None) or {}).get(profile_name) or {})
  51. if not adapters:
  52. return 0
  53. from gateway.run import _profile_runtime_scope
  54. scope = (_profile_runtime_scope(profile_home, hydrate_secrets=False)
  55. if profile_home is not None else contextlib.nullcontext())
  56. count = 0
  57. with scope:
  58. for platform, adapter in adapters.items():
  59. try:
  60. adapter.rewire_plugin_handlers()
  61. count += 1
  62. except Exception:
  63. logger.warning("[%s] plugin handler re-wire failed", getattr(platform, "value", platform),
  64. exc_info=True)
  65. logger.info("Re-wired plugin handlers on %d adapter(s)%s", count,
  66. f" for profile '{profile_name}'" if profile_name else "")
  67. return count
  68. def reload_plugins_verb(runner: Any, loop: asyncio.AbstractEventLoop) -> Callable[..., dict]:
  69. """Control-socket ``reload-plugins``: force re-discovery under the requested home's scope so a plugin
  70. installed/enabled by another process (CLI, Desktop, ``plugins.manage``) loads now; the loaded event
  71. then re-wires that profile's adapters. Only the gateway home and served profile homes are accepted.
  72. Answer: ``{"reloaded", "home", "plugins", "activations", "adapters_rewired"}``. Runs on the socket executor thread
  73. (discovery is blocking); the count is read on the loop AFTER the re-wire callback (FIFO), so a
  74. truthful "active now" reaches the caller."""
  75. def _handler(params: Optional[dict] = None) -> dict:
  76. from hermes_constants import get_hermes_home, hermes_home_key
  77. from hermes_cli.plugins import discover_plugins, get_plugin_manager
  78. from hermes_cli.plugins_activation import activation_summaries, dependency_blocked_summaries
  79. from gateway.run import _profile_runtime_scope
  80. params = params or {}
  81. gateway_home = Path(get_hermes_home())
  82. requested = Path(str(params.get("home") or gateway_home)).expanduser()
  83. req_key = hermes_home_key(requested)
  84. profile_name: Optional[str] = None
  85. if req_key != hermes_home_key(gateway_home):
  86. served = (getattr(runner, "_served_profile_homes", None) or {}).items()
  87. profile_name = next((str(n) for n, h in served if hermes_home_key(h) == req_key), None)
  88. if profile_name is None:
  89. return {"reloaded": False, "error": "home is not served by this gateway", "home": str(requested)}
  90. if profile_name == (getattr(runner, "_primary_profile_name", None) or "default"):
  91. profile_name = None
  92. scope = (_profile_runtime_scope(requested, hydrate_secrets=False) if profile_name
  93. else contextlib.nullcontext())
  94. with scope:
  95. discover_plugins(force=True)
  96. manager = get_plugin_manager()
  97. # A plugin that failed on a dependency this gateway booted without rides along, so the
  98. # enabling caller asks for the restart that loads it.
  99. names = sorted(manager._plugins)
  100. activations = activation_summaries(manager) + dependency_blocked_summaries(manager)
  101. _reconcile_plugin_mcp_servers(runner, requested)
  102. try:
  103. rewired = asyncio.run_coroutine_threadsafe(_count_adapters(runner, profile_name), loop).result(timeout=5.0)
  104. except Exception:
  105. rewired = None
  106. return {"reloaded": True, "home": str(requested), "plugins": names, "activations": activations,
  107. "adapters_rewired": rewired}
  108. return _handler
  109. # Long enough for a local stdio server to connect, short enough to stay well inside the caller's
  110. # 30 s socket timeout: a slow server keeps connecting in the background after the answer goes out.
  111. _MCP_CONNECT_WAIT_S = 10.0
  112. def _reconcile_plugin_mcp_servers(runner: Any, home: Path) -> None:
  113. """Connect the MCP servers a just-enabled plugin declares (and drop a disabled one's) now, so the
  114. next session's tool list has them. The forced re-discovery only registers their configs; until
  115. this, the housekeeping reconcile connected them up to a minute later, and every session started in
  116. that window came up without the plugin's tools. Runs under the same profile scope that chore binds,
  117. on its own thread so a slow server delays the answer by at most ``_MCP_CONNECT_WAIT_S``."""
  118. from gateway.run import _profile_runtime_scope
  119. from gateway.run_turn import GatewayTurnMixin
  120. multiplex = bool(getattr(getattr(runner, "config", None), "multiplex_profiles", False))
  121. def _reconcile() -> None:
  122. from tools.mcp_oauth import suppress_interactive_oauth
  123. from tools.mcp_tool_discovery import reconcile_mcp_servers_with_config
  124. scope = _profile_runtime_scope(home) if multiplex else GatewayTurnMixin._standalone_launch_scope()
  125. try:
  126. with scope, suppress_interactive_oauth():
  127. result = reconcile_mcp_servers_with_config()
  128. except Exception:
  129. logger.warning("MCP reconcile after reload-plugins failed for %s", home, exc_info=True)
  130. return
  131. if result["removed"] or result["added"]:
  132. logger.info("MCP servers reconciled after reload-plugins (%s): removed=%s added=%s",
  133. home, result["removed"], result["added"])
  134. thread = threading.Thread(target=_reconcile, name="reload-plugins-mcp", daemon=True)
  135. thread.start()
  136. thread.join(timeout=_MCP_CONNECT_WAIT_S)
  137. async def _count_adapters(runner: Any, profile_name: Optional[str]) -> int:
  138. """Live adapters for the profile; scheduled after the loaded-event callback, so they are re-wired."""
  139. if profile_name is None:
  140. return len(getattr(runner, "adapters", None) or {})
  141. return len((getattr(runner, "_profile_adapters", None) or {}).get(profile_name) or {})