plugins_admission.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. """Submit proposed plugin selections to the independent PM publisher."""
  2. from __future__ import annotations
  3. from pathlib import Path
  4. from typing import Iterable, Optional
  5. class AdmissionRefused(RuntimeError):
  6. """The candidate set was refused; config and environment untouched."""
  7. class DependencyConflict(AdmissionRefused):
  8. """PM's resolver proved the candidate set cannot co-install with the pinned dependency set.
  9. The raw ``uv lock`` output names hashed workspace members, not the plugin the user asked for, so
  10. the message leads with the plugin and keeps the resolver's cause for the details.
  11. """
  12. def __init__(self, cause: str, *, plugin: Optional[str] = None):
  13. self.cause = cause
  14. self.plugin = plugin
  15. who = f"Plugin '{plugin}'" if plugin else "The plugin selection"
  16. super().__init__(
  17. f"{who} conflicts with the dependencies pinned by Hermes core or an enabled plugin, "
  18. f"so it was not admitted. Resolver: {cause}")
  19. def candidate_member_dirs(
  20. candidate_enabled: Iterable[str],
  21. candidate_disabled: Iterable[str] = (),
  22. *,
  23. active_plugins_dir: Optional[Path] = None,
  24. extra_dirs: Iterable[Path] = (),
  25. ) -> list[Path]:
  26. """Shipped callers' member-list adapter; new discovery belongs to PM.
  27. Without an active plugins dir, preserve every home's recorded selection.
  28. """
  29. from pm.publication import candidate_members
  30. active = Path(active_plugins_dir) if active_plugins_dir else None
  31. return candidate_members(
  32. extra_dirs,
  33. proposed_home=active.parent if active else None,
  34. enabled=candidate_enabled,
  35. disabled=candidate_disabled,
  36. )
  37. def admit_plugin_set_change(
  38. candidate_enabled: set,
  39. candidate_disabled: set,
  40. *,
  41. active_plugins_dir: Optional[Path] = None,
  42. extra_dirs: Iterable[Path] = (),
  43. expected_config: str | None = None,
  44. plugin: Optional[str] = None,
  45. settled: Iterable[str] = (),
  46. ) -> None:
  47. """PM discovers and validates the proposed union under its install lock.
  48. No config or dependency selection is written by this application process. *plugin* names the
  49. plugin being admitted so a resolver conflict is reported against it (:class:`DependencyConflict`).
  50. *settled* names the plugins the user just decided on, so an update never undoes that decision.
  51. """
  52. from hermes_constants import get_hermes_home
  53. from pm.client import sync_venv
  54. from pm.plugin_inputs import Selection
  55. home = Path(active_plugins_dir).parent if active_plugins_dir is not None else get_hermes_home()
  56. try:
  57. sync_venv(explicit=True, plugins=Selection({
  58. "home": str(home.resolve()), "enabled": sorted(candidate_enabled),
  59. "disabled": sorted(candidate_disabled), "extra_dirs": [str(Path(d).resolve()) for d in extra_dirs],
  60. "settled": sorted(settled),
  61. **({"expected_config": expected_config} if expected_config is not None else {}),
  62. }))
  63. except Exception as exc:
  64. from pm.workspace import ResolutionConflict
  65. if isinstance(exc, ResolutionConflict):
  66. raise DependencyConflict(exc.cause, plugin=plugin) from exc
  67. raise AdmissionRefused(str(exc)) from exc