plugin_host_profiles.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. """Model-provider plugins under ``plugins.isolation: host``.
  2. Model-provider discovery runs while ``hermes_cli.config`` / ``hermes_cli.auth`` are still importing,
  3. so it must not start the plugin host (building the host's environment imports those same modules).
  4. Profile DATA therefore comes from a one-shot, credential-free extraction
  5. (``plugin_host_child --extract-profiles``), cached under the profile home by the plugin's file
  6. fingerprint. Overridden methods and callable fields run later in the profile's plugin host, addressed
  7. by (plugin dir, profile name, attribute) so they survive host restarts.
  8. """
  9. from __future__ import annotations
  10. import asyncio
  11. import hashlib
  12. import inspect
  13. import json
  14. import logging
  15. import os
  16. import subprocess
  17. import sys
  18. from pathlib import Path
  19. from typing import Any, Callable, Dict, List
  20. from hermes_cli.plugin_host_wire import PluginHostUnavailable, decode, signature_from
  21. logger = logging.getLogger(__name__)
  22. _EXTRACT_TIMEOUT_SECS = 60.0
  23. _CACHE_VERSION = 1
  24. def load_hosted_profiles(plugin_dir: Path, module_name: str) -> tuple[list[Any], dict[str, str]]:
  25. """ProviderProfile proxies for a model-provider plugin whose code runs in the plugin host, plus
  26. ``{profile name: reason}`` for its profiles that cannot cross the host boundary (the caller
  27. loads those in-process)."""
  28. from providers.base import ProviderProfile
  29. payload = _cached_extraction(Path(plugin_dir), module_name)
  30. if payload.get("error"):
  31. raise PluginHostUnavailable(str(payload["error"]))
  32. entries = payload.get("profiles") or []
  33. in_process = {str(e["name"]): str(e["in_process"]) for e in entries if e.get("in_process")}
  34. hosted = [_profile_proxy(ProviderProfile, str(plugin_dir), module_name, e)
  35. for e in entries if not e.get("in_process")]
  36. return hosted, in_process
  37. def is_hosted_profile(profile: Any) -> bool:
  38. """True for a profile proxy whose code runs in the plugin host."""
  39. return type(profile).__module__ == __name__
  40. def _fingerprint(plugin_dir: Path) -> str:
  41. digest = hashlib.sha256(f"{_CACHE_VERSION}|{sys.executable}".encode())
  42. try:
  43. from hermes_cli import __version__
  44. digest.update(str(__version__).encode())
  45. except Exception:
  46. pass
  47. for path in sorted(plugin_dir.rglob("*")):
  48. if not path.is_file() or "__pycache__" in path.parts or ".git" in path.parts:
  49. continue
  50. stat = path.stat()
  51. digest.update(f"{path.relative_to(plugin_dir)}|{stat.st_size}|{stat.st_mtime_ns}".encode())
  52. return digest.hexdigest()
  53. def _cached_extraction(plugin_dir: Path, module_name: str) -> dict[str, Any]:
  54. from hermes_constants import get_hermes_home
  55. # Keyed by the full path too: a user and a project plugin may share a directory name.
  56. path_key = hashlib.sha256(str(plugin_dir.resolve()).encode("utf-8")).hexdigest()[:12]
  57. cache = get_hermes_home() / "cache" / "plugin_host" / "model-providers" / f"{plugin_dir.name}-{path_key}.json"
  58. fingerprint = _fingerprint(plugin_dir)
  59. try:
  60. cached = json.loads(cache.read_text(encoding="utf-8-sig"))
  61. if cached.get("fingerprint") == fingerprint and cached.get("module_name") == module_name:
  62. return cached["payload"]
  63. except (OSError, ValueError, KeyError):
  64. pass
  65. payload = _extract(plugin_dir, module_name)
  66. if not payload.get("error"):
  67. cache.parent.mkdir(parents=True, exist_ok=True)
  68. tmp = cache.with_suffix(f".{os.getpid()}.tmp")
  69. tmp.write_text(json.dumps({"fingerprint": fingerprint, "module_name": module_name,
  70. "payload": payload}), encoding="utf-8")
  71. os.replace(tmp, cache)
  72. return payload
  73. def _extract(plugin_dir: Path, module_name: str) -> dict[str, Any]:
  74. """Import the plugin in a throwaway host process with no credentials and read its profiles."""
  75. from hermes_cli.plugin_isolation import HOST_PROCESS_ENV, host_launcher
  76. from hermes_constants import get_hermes_home
  77. repo_root = str(Path(__file__).resolve().parents[1])
  78. # Absolute: the child starts in the home, so a relative HERMES_HOME or plugin dir would resolve twice.
  79. home = str(get_hermes_home().resolve())
  80. env = {key: os.environ[key] for key in ("PATH", "LANG", "LC_ALL", "TZ", "SYSTEMROOT") if key in os.environ}
  81. # Windows resolves Path.home() from USERPROFILE, never HOME: without it the child cannot start.
  82. env.update(HOME=str(Path.home()), USERPROFILE=str(Path.home()), HERMES_HOME=home,
  83. PYTHONPATH=repo_root,
  84. **{HOST_PROCESS_ENV: "1"})
  85. # -P and a trusted cwd: ``-m`` otherwise puts the launch directory ahead of PYTHONPATH, so a
  86. # ``hermes_cli/`` package in the directory Hermes was started from would run instead of ours.
  87. argv = [*host_launcher(), sys.executable, "-P", "-m", "hermes_cli.plugin_host_child", "--extract-profiles",
  88. str(plugin_dir.resolve()), module_name]
  89. try:
  90. done = subprocess.run(argv, stdin=subprocess.DEVNULL, capture_output=True, env=env,
  91. cwd=home, timeout=_EXTRACT_TIMEOUT_SECS, check=False)
  92. except (OSError, subprocess.TimeoutExpired) as exc:
  93. return {"error": f"profile extraction failed: {exc}"}
  94. try:
  95. return json.loads(done.stdout.decode("utf-8"))
  96. except ValueError:
  97. tail = done.stderr.decode("utf-8", "replace").strip().splitlines()[-3:]
  98. return {"error": f"profile extraction exited {done.returncode}: {' | '.join(tail)}"}
  99. def _host_call(plugin_dir: str, module_name: str, profile: str, attr: str) -> Callable[..., Any]:
  100. def call(*args: Any, **kwargs: Any) -> Any:
  101. from hermes_cli.plugin_isolation import user_plugin_host
  102. host = user_plugin_host()
  103. if host is None:
  104. raise PluginHostUnavailable("plugins.isolation is no longer 'host'; restart Hermes")
  105. return host.profile_call(plugin_dir, module_name, profile, attr, args, kwargs)
  106. return call
  107. def _profile_proxy(base: type, plugin_dir: str, module_name: str, entry: dict[str, Any]) -> Any:
  108. name = str(entry["name"])
  109. namespace: dict[str, Any] = {"__module__": __name__,
  110. "__repr__": lambda self_: f"<plugin-host profile {name!r}>"}
  111. fields = {key: decode(value) for key, value in (entry.get("fields") or {}).items()}
  112. for attr, meta in (entry.get("calls") or {}).items():
  113. call = _host_call(plugin_dir, module_name, name, attr)
  114. if meta.get("async"):
  115. sync_call = call
  116. async def call(*args: Any, _sync: Callable[..., Any] = sync_call, **kwargs: Any) -> Any:
  117. return await asyncio.to_thread(_sync, *args, **kwargs)
  118. signature = signature_from(meta.get("sig"))
  119. if signature is not None:
  120. call.__signature__ = signature # type: ignore[attr-defined]
  121. call.__name__ = call.__qualname__ = attr
  122. if meta.get("field"):
  123. fields[attr] = call
  124. else:
  125. namespace[attr] = _as_method(call)
  126. cls = type(f"Hosted{entry.get('type') or base.__name__}", (base,), namespace)
  127. profile = object.__new__(cls)
  128. for key, value in fields.items():
  129. object.__setattr__(profile, key, value)
  130. return profile
  131. def _as_method(call: Callable[..., Any]) -> Callable[..., Any]:
  132. if asyncio.iscoroutinefunction(call):
  133. async def method(self_: Any, *args: Any, **kwargs: Any) -> Any:
  134. return await call(*args, **kwargs)
  135. else:
  136. def method(self_: Any, *args: Any, **kwargs: Any) -> Any:
  137. return call(*args, **kwargs)
  138. method.__name__ = method.__qualname__ = call.__name__
  139. signature = getattr(call, "__signature__", None)
  140. if signature is not None: # callers introspect accepted kwargs; bound access drops ``self`` again
  141. self_param = inspect.Parameter("self", inspect.Parameter.POSITIONAL_OR_KEYWORD)
  142. method.__signature__ = signature.replace( # type: ignore[attr-defined]
  143. parameters=[self_param, *signature.parameters.values()])
  144. return method