Skip to content

The agent that grows with you

fix(plugins): scan the carried tree once and name preserved files in a block

kshitijk4poor5630c223d02a
diff --git a/hermes_cli/plugins_cmd_catalog.py b/hermes_cli/plugins_cmd_catalog.py
index 7ca07f9a837..4b0bd400527 100644
--- a/hermes_cli/plugins_cmd_catalog.py
+++ b/hermes_cli/plugins_cmd_catalog.py
@@ -350,7 +350,7 @@ def _stash_local_files(target: Path, rels: list[str], stash: Path) -> None:
shutil.copy2(src, dst)
-def _carry_user_files(old: Path, new: Path, local: Optional[list[str]]) -> None:
+def _carry_user_files(old: Path, new: Path, local: Optional[list[str]]) -> list[str]:
"""Carry user-owned files into a staged replacement without reviving old plugin code.
For a git checkout, *local* is the ``??``/``!!`` set and may contain a directory entry
@@ -359,11 +359,13 @@ def _carry_user_files(old: Path, new: Path, local: Optional[list[str]]) -> None:
tree are carried while executable/declarative plugin surfaces remain revision-owned. If a
user-owned path cannot be represented safely in the new tree (a layout clash, or a symlink in a
git checkout's untracked/ignored set), fail before publication rather than silently dropping it.
+ Returns the carried paths (POSIX, relative to the tree) so a later scan block can name them.
"""
from hermes_cli.plugins_cmd import PluginOperationError
keep = {Path(rel) for rel in local or ()}
linked: list[str] = []
+ carried: list[str] = []
def _user_link(rel: Path) -> None:
# Git-owned user state that is a symlink is refused, never followed: a link injected after the
@@ -452,12 +454,14 @@ def _carry_user_files(old: Path, new: Path, local: Optional[list[str]]) -> None:
if dst.is_symlink() or dst.is_file():
dst.unlink()
shutil.copy2(src, dst, follow_symlinks=False)
+ carried.append(rel.as_posix())
if linked:
raise PluginOperationError(
f"Cannot preserve symlinked user file(s) {', '.join(sorted(linked))}: links are not followed "
"into an update. Replace each with a regular file (or remove it) and retry. "
"The installed plugin was left unchanged."
)
+ return carried
class RepinResult(NamedTuple):
diff --git a/hermes_cli/plugins_cmd_install.py b/hermes_cli/plugins_cmd_install.py
index 6a807b5292d..4cf470644a5 100644
--- a/hermes_cli/plugins_cmd_install.py
+++ b/hermes_cli/plugins_cmd_install.py
@@ -269,6 +269,19 @@ def _refuse_unavailable_portable_plugin(plugin_name: str, tree: Path) -> None:
)
+def _preserved_files_note(exc: Exception, merged) -> str:
+ """Scan-block text for a tree that also holds user files preserved from the installed copy."""
+ preserved = {str(rel) for rel in merged}
+ findings = getattr(getattr(exc, "scan_result", None), "findings", None) or []
+ hits = sorted({f.file for f in findings if f.file in preserved})
+ if hits:
+ note = ("These findings come from user files preserved from the installed copy: "
+ f"{', '.join(hits)}. Move or remove them and retry the update.")
+ else:
+ note = "The scanned tree included user files preserved from the installed copy."
+ return f"{exc}\n\n{note}"
+
+
def _install_plugin_core(
identifier: str,
*,
@@ -291,7 +304,9 @@ def _install_plugin_core(
checked-out sha — provenance lives OUTSIDE the plugin tree, so a repo cannot forge it;
its ``pin`` is kept only when the checkout satisfies it (a ``--ref`` install is off-pin).
*allow_removed* records that the user knowingly bypassed the kill list.
- *before_swap(manifest, tree)* runs on the validated clone before anything moves into place
+ *before_swap(manifest, tree)* runs on the manifest-checked clone BEFORE the security scan, so
+ the single scan and portable-package check admit the merged tree (file-count/size limits
+ included). It may return the relative paths it merged in, which a scan block then attributes,
and may raise :class:`PluginOperationError` to abort (re-pin consent)."""
requested_revision = _pc()._normalize_exact_revision(ref) if ref is not None else None
try:
@@ -326,9 +341,18 @@ def _install_plugin_core(
except ValueError as e:
raise _pc().PluginOperationError(str(e)) from e
_check_manifest_version(manifest, plugin_name)
+ # A callback may merge user-owned state into the candidate tree; run it first so one scan
+ # admits the final bytes.
+ merged = before_swap(manifest, tmp_target) if before_swap is not None else None
# Scan BEFORE anything is moved into place; raises PluginScanBlocked when blocked.
- _pc()._scan_plugin_tree(tmp_target, identifier, force=force, scan_decision_cb=scan_decision_cb,
- reviewed_pin=at_reviewed_pin)
+ try:
+ _pc()._scan_plugin_tree(tmp_target, identifier, force=force, scan_decision_cb=scan_decision_cb,
+ reviewed_pin=at_reviewed_pin)
+ except _pc().PluginScanBlocked as exc:
+ if not merged:
+ raise
+ raise _pc().PluginScanBlocked(_preserved_files_note(exc, merged),
+ scan_result=exc.scan_result) from exc
if not python_deps:
from pm.workspace import enabled_plugin_dirs
@@ -337,13 +361,6 @@ def _install_plugin_core(
"--no-deps cannot replace an active plugin. Retry without --no-deps; "
"PM must prepare its dependencies before publication.")
_refuse_unavailable_portable_plugin(plugin_name, tmp_target)
- if before_swap is not None:
- before_swap(manifest, tmp_target)
- # A callback may merge user-owned state into the candidate tree. Admit the final
- # bytes through the same security and portable-package gates as the pristine clone.
- _pc()._scan_plugin_tree(tmp_target, identifier, force=force, scan_decision_cb=scan_decision_cb,
- reviewed_pin=at_reviewed_pin)
- _refuse_unavailable_portable_plugin(plugin_name, tmp_target)
if target.exists() and not force:
raise _pc().PluginOperationError(
diff --git a/hermes_cli/plugins_transaction.py b/hermes_cli/plugins_transaction.py
index 5e939ec597d..4a43dd8a93c 100644
--- a/hermes_cli/plugins_transaction.py
+++ b/hermes_cli/plugins_transaction.py
@@ -81,14 +81,15 @@ def update_plugin(
*,
catalog_entry=None,
interactive: bool = False,
- carry_user_files: Callable[[Path], None] | None = None,
+ carry_user_files: Callable[[Path], list[str]] | None = None,
) -> str:
"""Prepare a catalog re-pin or custom Git pull without changing the live tree.
*interactive*: a terminal user is present to consent to newly declared dependencies;
the dashboard and the gateway's auto-apply pass False and get a refusal instead.
*carry_user_files(staged)* may merge user-owned state into the staged tree before
- manifest validation, example-file generation, dependency preparation and publication."""
+ manifest validation, example-file generation, dependency preparation and publication; it
+ returns the carried paths so a scan block can attribute findings to them."""
import tempfile
from hermes_cli import plugins_cmd as pc
@@ -161,8 +162,7 @@ def update_plugin(
if not ok:
raise pc.PluginOperationError(output)
revision = pc._git_head_revision(staged, pc._resolve_git_executable())
- if carry_user_files is not None:
- carry_user_files(staged)
+ merged = carry_user_files(staged) if carry_user_files is not None else None
manifest = pc._read_manifest_for_install(staged)
installed_name = str(manifest.get("name") or target.name)
if catalog_entry is None and installed_name != target.name:
@@ -175,7 +175,13 @@ def update_plugin(
raise pc.PluginOperationError(
f"The updated plugin renamed itself to '{installed_name}', but that plugin already exists.")
pc._check_manifest_version(manifest, installed_name)
- pc._scan_plugin_tree(staged, source, force=False)
+ try:
+ pc._scan_plugin_tree(staged, source, force=False)
+ except pc.PluginScanBlocked as exc:
+ if not merged:
+ raise
+ from hermes_cli.plugins_cmd_install import _preserved_files_note
+ raise pc.PluginScanBlocked(_preserved_files_note(exc, merged), scan_result=exc.scan_result) from exc
pc._copy_example_files(staged, pc._console())
_refresh_declared_dependencies(target, staged, manifest, interactive=interactive)
if tree_digest(target) != before: