plugins_transaction.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. """Publish plugin code and its dependency selection through one recoverable handoff."""
  2. from __future__ import annotations
  3. from collections.abc import Callable
  4. from pathlib import Path
  5. from typing import Any
  6. import shutil
  7. def recover_plugin_publication(project: Path, row: dict, journal: Path) -> None:
  8. """Recover a shipped caller's row through the stdlib boot-journal owner."""
  9. from hermes_cli.runtime_state import _recover_plugin_publication
  10. _recover_plugin_publication(project, row, journal)
  11. def publish_plugin(staged: Path, target: Path, old_metadata: dict, new_metadata: dict,
  12. *, target_digest: str | None = None, require_consent: bool = False,
  13. assume_consent: bool = False) -> None:
  14. """Publish *staged* into *target*. *assume_consent* is consent the caller
  15. already holds (``--yes-deps``, or a memory-provider migration under
  16. ``security.allow_lazy_installs``): the active-replacement consent veto is
  17. skipped, so an unattended install carries that decision instead of being
  18. refused non-interactively."""
  19. from pm.client import sync_venv
  20. from pm.plugin_inputs import StagedUpdate
  21. from pm.store import tree_digest
  22. if require_consent and not assume_consent:
  23. from hermes_cli import plugins_cmd
  24. from pm.workspace import enabled_plugin_dirs
  25. if target.resolve() in enabled_plugin_dirs(installing=target):
  26. consented, reason = plugins_cmd._install_plugin_python_deps(
  27. plugins_cmd._read_manifest_for_install(staged), staged, plugins_cmd._console())
  28. if not consented:
  29. outcome = ("Reinstall declined: {}. The installed plugin and active environment are unchanged."
  30. if target.exists() else "Install declined: {}. Nothing was installed.")
  31. # The refusal carries its closed class (plugins_cmd_install._ConsentRefusal).
  32. from hermes_cli.observability.shared_metrics_fields import tagged_failure_class
  33. raise plugins_cmd.PluginOperationError(
  34. outcome.format(reason), failure_class=tagged_failure_class(reason) or "other")
  35. sync_venv(explicit=True, plugins=StagedUpdate({
  36. "staged": str(staged.resolve()), "target": str(target.absolute()),
  37. "old_metadata": old_metadata, "new_metadata": new_metadata,
  38. "target_digest": target_digest if target_digest is not None else (tree_digest(target) if target.exists() else None),
  39. }))
  40. def _refresh_declared_dependencies(target: Path, staged: Path, manifest: dict, *, interactive: bool) -> None:
  41. """Dependency changes carried by an update clear the same gates as an install.
  42. New Python requirements install packages into the shared environment: install and
  43. reinstall prompt for that, so an update prompts too, and an unattended one (dashboard,
  44. ``plugins.auto_apply`` from the gateway) is REFUSED rather than consented on the user's
  45. behalf. Publication replaces the whole tree, so a Node sidecar the user accepted earlier is
  46. rebuilt in the staged copy when its package.json/lock moved (custom pulls copy a stale
  47. node_modules; catalog re-pins clone without one).
  48. """
  49. from hermes_cli import plugins_cmd as pc
  50. from hermes_cli.runtime_state import _bytes
  51. from pm.plugin_declarations import read_python_declaration
  52. from pm.workspace import enabled_plugin_dirs, install_node_sidecar
  53. if (target / "node_modules").is_dir() and (staged / "package.json").is_file() and (
  54. not (staged / "node_modules").is_dir()
  55. or any(_bytes(target / name) != _bytes(staged / name) for name in ("package.json", "package-lock.json"))):
  56. reason = install_node_sidecar(staged, explicit=True)
  57. if reason:
  58. raise pc.PluginOperationError(
  59. f"Node dependencies could not be refreshed: {reason}. "
  60. "The installed plugin and active environment are unchanged.")
  61. if target.resolve() not in enabled_plugin_dirs(installing=target):
  62. return # a disabled plugin's deps are admitted (and consented) by `hermes plugins enable`
  63. before, after = read_python_declaration(target), read_python_declaration(staged)
  64. added = tuple(spec for spec in after.install_requirements if spec not in before.install_requirements)
  65. if not added and not (after.installs_packages and not before.installs_packages):
  66. return
  67. if not interactive:
  68. raise pc.PluginOperationError(
  69. f"The update declares new Python dependencies ({', '.join(added) or 'in its pyproject.toml'}); "
  70. f"run `hermes plugins update {target.name}` in a terminal to review them.")
  71. consented, reason = pc._consent_python_deps(manifest.get("name", target.name), added, pc._console())
  72. if not consented:
  73. raise pc.PluginOperationError(
  74. f"Update declined: {reason}. The installed plugin and active environment are unchanged.")
  75. def update_plugin(
  76. target: Path,
  77. *,
  78. catalog_entry=None,
  79. interactive: bool = False,
  80. force: bool = False,
  81. scan_decision_cb: Callable[[Any], bool] | None = None,
  82. carry_user_files: Callable[[Path], list[str]] | None = None,
  83. surface_gate: Callable[[dict, Path], None] = lambda _manifest, _staged: None,
  84. ) -> str:
  85. """Prepare a catalog re-pin or custom Git pull without changing the live tree.
  86. *interactive*: a terminal user is present to consent to newly declared dependencies;
  87. the dashboard and the gateway's auto-apply pass False and get a refusal instead.
  88. Every update is scanned: *force* accepts a caution verdict (the user already saw it), and
  89. *scan_decision_cb(result)* may accept one by asking; a dangerous verdict always blocks.
  90. *carry_user_files(staged)* may merge user-owned state into the staged tree before
  91. manifest validation, example-file generation, dependency preparation and publication; it
  92. returns the carried paths so a scan block can attribute findings to them.
  93. *surface_gate(manifest, staged)* may raise (``RepinConsentRequired`` for a widened plugin) to
  94. refuse the candidate before it is scanned or published."""
  95. import tempfile
  96. from hermes_cli import plugins_cmd as pc
  97. from hermes_cli.plugins_cmd_catalog import RepinConsentRequired, refuse_if_installed_removed
  98. from pm.store import tree_digest
  99. target = target.resolve()
  100. metadata = pc._read_install_metadata()
  101. record = dict(metadata.get(target.name, {}))
  102. if catalog_entry is None and record.get("pinned"):
  103. raise pc.PluginOperationError(f"Plugin '{target.name}' is pinned; reinstall with an explicit --ref to change it.")
  104. source = catalog_entry.install_identifier if catalog_entry else str(record.get("source") or "")
  105. if not source or (catalog_entry is None and not (target / ".git").is_dir()):
  106. raise pc.PluginOperationError(f"Plugin '{target.name}' has no owned Git checkout; reinstall from its source.")
  107. feed_revision = None
  108. if catalog_entry is None:
  109. from hermes_cli.plugins_provenance import Provenance, ProvenanceClass
  110. from hermes_cli.plugins_updates import check_local_provenance, default_fetch, parse_feed_yml
  111. checked = check_local_provenance(Provenance(target.name, ProvenanceClass.GIT, target, record))
  112. if checked.needs_fixing:
  113. raise pc.PluginOperationError(checked.needs_fixing)
  114. if record.get("update_url"):
  115. feed = parse_feed_yml(default_fetch(record["update_url"]))
  116. proposed = (feed.get("artifacts") or {}).get("git", "")
  117. if pc._EXACT_COMMIT_RE.fullmatch(proposed):
  118. feed_revision = proposed.lower()
  119. elif proposed not in (source, source.split("#", 1)[0]):
  120. raise pc.PluginOperationError("Update feed must select a commit or the recorded Git source.")
  121. if feed.get("min_hermes"):
  122. pc._check_manifest_version({"requires_hermes": feed["min_hermes"]}, target.name)
  123. refuse_if_installed_removed(target.name, target)
  124. before = tree_digest(target)
  125. with tempfile.TemporaryDirectory(prefix=".update-", dir=target.parent) as directory:
  126. staged = Path(directory) / "plugin"
  127. try:
  128. if catalog_entry:
  129. git_url, subdir = pc._resolve_git_url(source)
  130. revision = pc._clone_plugin_repo(staged, git_url, catalog_entry.sha)
  131. staged = pc._resolve_subdir_within(staged, subdir) if subdir else staged
  132. output = f"Updated to catalog pin {revision}"
  133. catalog_record = {
  134. "name": catalog_entry.name,
  135. "repo": catalog_entry.repo,
  136. "tier": catalog_entry.tier,
  137. "pin": catalog_entry.sha,
  138. "sha": revision,
  139. }
  140. record.update(
  141. catalog=catalog_record,
  142. pinned=True,
  143. source=pc._canonical_source(git_url, subdir),
  144. )
  145. record.pop("catalog_name", None)
  146. record.pop("catalog_tier", None)
  147. from hermes_cli.plugins_cmd_catalog import write_catalog_sidecar_record
  148. write_catalog_sidecar_record(staged, catalog_record, revision)
  149. else:
  150. # Copy Git metadata and local changes. Autostash only ever touches the copy.
  151. shutil.copytree(target, staged, symlinks=True, ignore=shutil.ignore_patterns("__pycache__"))
  152. if feed_revision:
  153. git = pc._resolve_git_executable()
  154. status = pc._git_or_raise(git, target, "status", "--porcelain", failure_prefix="Could not inspect plugin edits: ")
  155. if status.stdout.strip():
  156. raise pc.PluginOperationError("Pinned feed update has local changes; save them before updating.")
  157. pc._checkout_exact_revision(staged, git, feed_revision)
  158. output = f"Updated to feed commit {feed_revision}"
  159. else:
  160. ok, output = pc._git_pull_plugin_dir(staged)
  161. if not ok:
  162. raise pc.PluginOperationError(output)
  163. revision = pc._git_head_revision(staged, pc._resolve_git_executable())
  164. merged = carry_user_files(staged) if carry_user_files is not None else None
  165. manifest = pc._read_manifest_for_install(staged)
  166. surface_gate(manifest, staged)
  167. installed_name = str(manifest.get("name") or target.name)
  168. if catalog_entry is None and installed_name != target.name:
  169. raise pc.PluginOperationError("The updated plugin changed its installed name; reinstall it explicitly.")
  170. try:
  171. new_target = pc._sanitize_plugin_name(installed_name, target.parent)
  172. except ValueError as exc:
  173. raise pc.PluginOperationError(str(exc)) from exc
  174. if new_target != target and new_target.exists():
  175. raise pc.PluginOperationError(
  176. f"The updated plugin renamed itself to '{installed_name}', but that plugin already exists.")
  177. pc._check_manifest_version(manifest, installed_name)
  178. pc._scan_merged_tree(staged, source, merged, force=force, scan_decision_cb=scan_decision_cb,
  179. reviewed_pin=catalog_entry is not None)
  180. pc._copy_example_files(staged, pc._console())
  181. _refresh_declared_dependencies(target, staged, manifest, interactive=interactive)
  182. if tree_digest(target) != before:
  183. raise pc.PluginOperationError("Plugin files changed while preparing the update; retry.")
  184. record["revision"] = revision
  185. if new_target == target and tree_digest(staged) == before:
  186. return output
  187. publish_plugin(
  188. staged,
  189. new_target,
  190. metadata,
  191. {**metadata, installed_name: record},
  192. target_digest=before if new_target == target else None,
  193. )
  194. return output
  195. except (pc.PluginOperationError, RepinConsentRequired):
  196. raise
  197. except Exception as exc:
  198. raise pc.PluginOperationError(f"Plugin '{target.name}' update was not published: {exc}") from exc