plugins_state.py 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. """Read every profile's enabled plugins in config order for the shared union.
  2. Plugin admission owns writes; discovery never edits a profile's selection.
  3. """
  4. from __future__ import annotations
  5. from pathlib import Path
  6. from typing import Any, Optional
  7. import logging
  8. LOG = logging.getLogger(__name__)
  9. # plugins.<RECORD> maps each plugin an update disabled to {enabled, reason}: what to undo once it fits.
  10. RECORD = "disabled_by_update"
  11. def _profiles_root() -> Path:
  12. # Plugin discovery and dependency publication must use the same home root.
  13. from pm.environments import dependency_home_root
  14. return dependency_home_root() / "profiles"
  15. def _selection_readers(config_path: Path):
  16. """``(version(path), read(path))`` for a home's selection. PM's dependency runtime reads the
  17. LOCAL file it publishes to and compare-and-swaps (``pm.publication``): it has neither the
  18. remote config backend's client nor the plane credential, so a remote deployment never fails
  19. PM closed there. Every other process reads through the selected config backend; a file
  20. deployment never asks whether this is PM's runtime (the selector is all it needs)."""
  21. import os
  22. from hermes_cli import config_backend
  23. if os.environ.get(config_backend.BACKEND_ENV, "").strip().lower() in ("", "file"):
  24. return config_backend.config_version, config_backend.read_config_doc
  25. from pm.package import InstallError
  26. from pm.runtime import is_runtime
  27. try:
  28. in_runtime = is_runtime()
  29. except InstallError: # an application process whose packaged PM runtime is damaged: not PM's runtime
  30. in_runtime = False
  31. if not in_runtime:
  32. return config_backend.config_version, config_backend.read_config_doc
  33. backend, home = config_backend.local_file_layer(config_path)
  34. return (lambda _path: backend.version(home)), (lambda _path: backend.read_user_doc(home))
  35. def read_home_selection(home: Path) -> Optional[dict[str, Any]]:
  36. """The plugin/memory selection a home's config.yaml declares (None: no config yet).
  37. The public reader for anything that must agree with what PM installs for that home.
  38. An unreadable selection raises rather than shrinking the next dependency generation;
  39. empty YAML is an explicit empty configuration, as in the CLI loader.
  40. """
  41. config_path = home / "config.yaml"
  42. config_version, read_config_doc = _selection_readers(config_path)
  43. # Stat before the YAML stack: a home without config needs none (PM's own runtime tree has none).
  44. # Only a missing file means "no config"; a symlink loop or permission error still refuses.
  45. try:
  46. config_version(config_path)
  47. except FileNotFoundError:
  48. return None
  49. except OSError as exc:
  50. raise ValueError(f"could not read plugin selection: {config_path}") from exc
  51. # Missing YAML support is a broken runtime, not an empty plugin selection.
  52. from ruamel.yaml.error import YAMLError
  53. try:
  54. config = read_config_doc(config_path)
  55. except FileNotFoundError:
  56. return None
  57. except (OSError, UnicodeError) as exc:
  58. raise ValueError(f"could not read plugin selection: {config_path}") from exc
  59. except YAMLError as exc:
  60. raise ValueError(f"could not parse plugin selection: {config_path}") from exc
  61. if config is None:
  62. return {}
  63. if not isinstance(config, dict):
  64. raise ValueError(f"configuration must be a mapping: {config_path}")
  65. for section in ("plugins", "memory"):
  66. if config.get(section) is not None and not isinstance(config[section], dict):
  67. raise ValueError(f"{section} must be a mapping: {config_path}")
  68. plugins = config.get("plugins") or {}
  69. for key in ("enabled", "disabled"):
  70. names = plugins.get(key)
  71. if names is not None and (not isinstance(names, list)
  72. or any(not isinstance(name, str) for name in names)):
  73. raise ValueError(f"plugins.{key} must be a list of names: {config_path}")
  74. provider = (config.get("memory") or {}).get("provider")
  75. if provider is not None and not isinstance(provider, str):
  76. raise ValueError(f"memory.provider must be a name: {config_path}")
  77. return config
  78. def _enabled_from_config(config: dict[str, Any]) -> list[str]:
  79. """plugins.enabled from an already-parsed config, ORDER-PRESERVING."""
  80. plugins_cfg = config.get("plugins")
  81. if not isinstance(plugins_cfg, dict):
  82. return []
  83. enabled = plugins_cfg.get("enabled")
  84. if not isinstance(enabled, list):
  85. return []
  86. disabled = plugins_cfg.get("disabled", [])
  87. disabled = set(disabled) if isinstance(disabled, list) else set()
  88. out: list[str] = []
  89. for name in enabled:
  90. if (isinstance(name, str) and name and name not in out
  91. and name not in disabled and name.rsplit("/", 1)[-1] not in disabled):
  92. out.append(name)
  93. return out
  94. def _is_directory(path: Path) -> bool:
  95. import stat
  96. try:
  97. return stat.S_ISDIR(path.stat().st_mode)
  98. except FileNotFoundError:
  99. return False
  100. except OSError as exc:
  101. raise ValueError(f"could not inspect plugin directory: {path}") from exc
  102. def dependency_homes() -> list[Path]:
  103. """Every home whose selection feeds the shared venv: the default home plus each LIVE profile.
  104. Enumerates the complete union or refuses; a partial scan cannot remove members.
  105. Live means what ``hermes profile`` lists (hermes_constants): a valid id carrying an identity
  106. marker and no tombstone. Staging dirs (``.work.staging-*``), deleted profiles and stray
  107. marker-less dirs must not put plugins into the shared environment.
  108. """
  109. from hermes_constants import PROFILE_ID_RE, named_profile_is_live
  110. from pm.environments import dependency_home_root
  111. homes = [dependency_home_root()]
  112. root = _profiles_root()
  113. try:
  114. profiles = sorted(root.iterdir(), key=str)
  115. except FileNotFoundError:
  116. return homes
  117. except OSError as exc:
  118. raise ValueError(f"could not enumerate profiles: {root}") from exc
  119. homes.extend(profile for profile in profiles
  120. if _is_directory(profile) and profile.name != "default"
  121. and PROFILE_ID_RE.match(profile.name) and named_profile_is_live(profile))
  122. return homes
  123. def enabled_plugins_ordered(*, proposed_home=None, enabled=None, disabled=None,
  124. installing: Path | None = None,
  125. skip_invalid_secondary: bool = False) -> dict[Path, list[str]]:
  126. """plugins_dir → ordered enabled list, per home. Keyed by the
  127. PLUGINS DIR (where the member dirs live), not the home itself.
  128. The ACTIVE MEMORY PROVIDER joins its home's list: providers install
  129. via ``memory.provider`` (mnemosyne's documented path), not via
  130. plugins.enabled — without this, a provider's dep plugin never joins
  131. the union. Admission refuses a conflicting candidate without changing
  132. the active environment or disabling an existing provider."""
  133. out: dict[Path, list[str]] = {}
  134. homes = dependency_homes()
  135. for home in homes:
  136. # ONE parse per home feeds both queries (enabled + provider).
  137. try:
  138. config = read_home_selection(home)
  139. except ValueError as exc:
  140. if not skip_invalid_secondary or home == homes[0]:
  141. raise
  142. LOG.warning("Skipping broken secondary profile %s during dependency verification: %s", home, exc)
  143. continue
  144. config = config or {}
  145. if proposed_home is not None and home.resolve() == Path(proposed_home).resolve():
  146. config = {**config, "plugins": {"enabled": list(enabled or ()), "disabled": list(disabled or ())}}
  147. names = _enabled_from_config(config)
  148. provider = _provider_from_config(home, config, installing=installing)
  149. if provider and provider not in names:
  150. names.append(provider)
  151. if names:
  152. out[home / "plugins"] = names
  153. return out
  154. def _provider_from_config(home: Path, config: dict[str, Any], *, installing: Path | None = None) -> Optional[str]:
  155. """The ``memory.provider`` key of an already-parsed config, when its
  156. plugin dir exists (no dir = not a member) and plugins.disabled does not park it."""
  157. provider = (config.get("memory") or {}).get("provider")
  158. if not provider or not provider.strip():
  159. return None
  160. name = provider.strip()
  161. plugin_dir = home / "plugins" / name
  162. if installing is not None and plugin_dir.resolve() == installing.resolve():
  163. return name
  164. if not _is_directory(plugin_dir) or _parked(plugin_dir, config):
  165. return None
  166. return name
  167. def _parked(plugin_dir: Path, config: dict[str, Any]) -> bool:
  168. """Whether plugins.disabled names this provider plugin. The provider loader honours the same
  169. spellings (plugins.memory._explicitly_disabled), so a parked provider is never loaded and
  170. installing its dependencies could only fail the generation for nothing."""
  171. disabled = (config.get("plugins") or {}).get("disabled") or []
  172. return bool(plugin_spellings(plugin_dir) & set(disabled))
  173. def plugin_spellings(plugin_dir: Path, key: str | None = None) -> set[str]:
  174. """Every name a plugins list may use for this plugin: the selection key and its bare leaf, the
  175. directory name and the manifest ``name:``. Read from the plugin itself, so it holds in any home."""
  176. from pm.plugin_declarations import native_manifest_file, read_native_manifest
  177. names = {plugin_dir.name} | ({key, key.rsplit("/", 1)[-1]} if key else set())
  178. try:
  179. manifest_file = native_manifest_file(plugin_dir)
  180. if manifest_file is not None:
  181. names.add(str(read_native_manifest(manifest_file).get("name") or ""))
  182. except ValueError:
  183. pass # no readable manifest: the other spellings still hold
  184. names.discard("")
  185. return names