plugins_capability_ctx.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. """Capability-gated ``PluginContext`` surface: ``has_capability`` probing and
  2. ``side_question`` (ask the session's own agent after a turn, ``llm.side_question``)."""
  3. from __future__ import annotations
  4. import logging
  5. from typing import Optional
  6. from hermes_cli.plugin_capabilities import plugin_capability_granted
  7. from hermes_cli.plugins_loader import _plugin_home_scope
  8. logger = logging.getLogger(__name__)
  9. class PluginCapabilityCtxMixin:
  10. """Mixed into :class:`hermes_cli.plugins.PluginContext` (``manifest``, ``_manager``, ``plugin_id``)."""
  11. # -- capability probing (#64228) -----------------------------------------
  12. def has_capability(self, capability: str) -> bool:
  13. """True when *capability* is live for this plugin (probe, then degrade gracefully). Bundled
  14. plugins are trusted for ``tools.override``; otherwise granted_capabilities or the legacy
  15. ``allow_*`` key decides. Unknown ids / unreadable consent -> False (fail closed)."""
  16. if self.manifest.source == "bundled" and capability == "tools.override":
  17. return True
  18. return plugin_capability_granted(self.plugin_id, capability)
  19. def side_question(self, token: str, prompt: str, *, timeout: float = 15.0) -> Optional[str]:
  20. """Ask the session's own agent ``prompt`` about the turn that just ended; text or None.
  21. ``token`` is ``post_llm_call``'s ``side_question_token``: one attempt per turn, shared by
  22. every plugin of the session's profile, valid for 60 s and only until the next turn starts
  23. (cron runs and subagent turns get no token). Same model, the whole
  24. session (memory included) and its warm prompt cache; tools never run and nothing is
  25. written to the transcript. Needs the ``llm.side_question`` capability; without it this
  26. returns None and leaves the attempt to a consented plugin."""
  27. from agent.side_question_lease import SIDE_QUESTION_CAPABILITY, redeem_side_question
  28. if not self._side_question_granted(SIDE_QUESTION_CAPABILITY):
  29. if not getattr(self, "_side_question_denial_logged", False):
  30. self._side_question_denial_logged = True
  31. logger.info("Plugin %s called ctx.side_question without the %s capability; returning None",
  32. self.plugin_id, SIDE_QUESTION_CAPABILITY)
  33. return None
  34. return redeem_side_question(
  35. token, prompt, plugin_id=self.plugin_id, home=self._manager.home_path, timeout=timeout)
  36. def _side_question_granted(self, capability: str) -> bool:
  37. """Consent read from THIS manager's profile home: plugins call from their own threads,
  38. where the active profile may be the launch profile's."""
  39. try:
  40. from hermes_cli.config import load_config
  41. with _plugin_home_scope(self._manager.home_path):
  42. cfg = load_config() or {}
  43. except Exception: # health: allow BLE001 -- fail closed: unreadable consent state never grants
  44. return False
  45. return plugin_capability_granted(self.plugin_id, capability, config=cfg)