Skip to content

The agent that grows with you

fix(plugins): doctor refuses to copy a plugin into its own temp dir

Tekniume1e956f1df14
diff --git a/hermes_cli/plugin_dev.py b/hermes_cli/plugin_dev.py
index b5b2d5212e4..ab0f3adb606 100644
--- a/hermes_cli/plugin_dev.py
+++ b/hermes_cli/plugin_dev.py
@@ -28,6 +28,21 @@ def _deny_network(*_args: Any, **_kwargs: Any) -> None:
raise RuntimeError("network access is disabled while Plugin Doctor runs")
+def _stage_copy(source: Path, staged: Path) -> None:
+ """Copy *source* to *staged*, refusing a destination inside the source.
+
+ TMPDIR can point inside the plugin being checked; copying into a descendant of the source
+ walks its own output until the path is too long or the disk is full. Both sides are resolved
+ so a symlinked TMPDIR or plugin path cannot hide the overlap.
+ """
+ src = source.resolve()
+ if staged.resolve().is_relative_to(src):
+ raise _DoctorLoadError(
+ f"Refusing to stage {src}: Doctor's temporary directory {staged.parent} is inside it, "
+ "so the copy would recurse into itself. Point TMPDIR outside the plugin.")
+ shutil.copytree(source, staged, ignore=shutil.ignore_patterns(*_STAGING_SKIPPED_DIRS, "*.pyc"))
+
+
@contextmanager
def _doctor_runtime(plugin_path: Path):
"""Load one plugin through the real runtime and restore global state afterwards.
@@ -45,10 +60,9 @@ def _doctor_runtime(plugin_path: Path):
bundled.mkdir(parents=True)
plugins_root.mkdir(parents=True)
staging_root = plugins_root / plugin_path.name
- ignore = shutil.ignore_patterns(*_STAGING_SKIPPED_DIRS, "*.pyc")
if _has_manifest(plugin_path):
staged = staging_root
- shutil.copytree(plugin_path, staged, ignore=ignore)
+ _stage_copy(plugin_path, staged)
else:
# A category: stage only its plugin under the category name (discovery keys it
# ``category/plugin``), never the loose files or skipped dirs beside it.
@@ -58,7 +72,7 @@ def _doctor_runtime(plugin_path: Path):
raise _DoctorLoadError(
f"{plugin_path} holds {len(members)} plugins ({names}); point Doctor at one of them")
staged = staging_root / members[0].name
- shutil.copytree(members[0], staged, ignore=ignore)
+ _stage_copy(members[0], staged)
stack.enter_context(patch.dict(os.environ, {
"HERMES_HOME": str(home),
"HERMES_BUNDLED_PLUGINS": str(bundled),
diff --git a/tests/hermes_cli/test_plugin_dev.py b/tests/hermes_cli/test_plugin_dev.py
index f27bdd6612c..d56859a2875 100644
--- a/tests/hermes_cli/test_plugin_dev.py
+++ b/tests/hermes_cli/test_plugin_dev.py
@@ -2,6 +2,8 @@ from __future__ import annotations
from pathlib import Path
+import pytest
+
def test_doctor_uses_registration_to_reject_bad_hook_and_callback_signature(
tmp_path: Path,
@@ -333,6 +335,37 @@ def test_doctor_removes_temp_home_when_staging_copy_fails(
assert leftovers == [], f"stranded doctor temp dirs: {leftovers}"
[email protected]("layout", ["direct", "category"])
+def test_doctor_refuses_to_stage_a_plugin_containing_its_temp_dir(
+ tmp_path: Path, monkeypatch, layout: str
+) -> None:
+ """A valid plugin that holds Doctor's temp dir (reached through a symlinked TMPDIR) must
+ not be copied into itself: the copy would walk its own output until ENAMETOOLONG/ENOSPC."""
+ import shutil
+ import tempfile
+
+ from hermes_cli import plugin_dev
+
+ target = tmp_path / "target"
+ plugin = target / "inner" if layout == "category" else target
+ plugin.mkdir(parents=True)
+ (plugin / "plugin.yaml").write_text("name: inner\n", encoding="utf-8")
+ (plugin / "__init__.py").write_text("def register(ctx):\n pass\n", encoding="utf-8")
+ (plugin / "scratch").mkdir()
+ alias = tmp_path / "tmp-alias"
+ alias.symlink_to(plugin / "scratch")
+ monkeypatch.setattr(tempfile, "tempdir", str(alias))
+ copied: list[Path] = []
+ monkeypatch.setattr(plugin_dev.shutil, "copytree", lambda src, *a, **k: copied.append(src))
+
+ report = plugin_dev.doctor_plugin(str(target))
+
+ assert copied == []
+ assert any("would recurse into itself" in issue.message for issue in report.findings)
+ assert list((plugin / "scratch").iterdir()) == []
+
+
def test_doctor_loads_model_provider_plugins_through_provider_discovery(tmp_path: Path) -> None:
"""`kind: model-provider` registers a ProviderProfile at import and has no register(ctx);
doctor must judge it by that contract and leave the live registry untouched."""