Skip to content

The agent that grows with you

fix(plugins): doctor temp home can no longer be stranded by a failed staging copy

Tekniumca9952cbb308
diff --git a/contributors/emails/[email protected] b/contributors/emails/[email protected]
new file mode 100644
index 00000000000..f60bb14092b
--- /dev/null
+++ b/contributors/emails/[email protected]
@@ -0,0 +1 @@
+DevKJ
diff --git a/hermes_cli/plugin_dev.py b/hermes_cli/plugin_dev.py
index 647f7c20390..2687fd74893 100644
--- a/hermes_cli/plugin_dev.py
+++ b/hermes_cli/plugin_dev.py
@@ -40,34 +40,44 @@ def _doctor_runtime(plugin_path: Path):
test framework. Registration code executes under a temporary HERMES_HOME
with outbound socket connects blocked.
"""
- temporary_home = tempfile.TemporaryDirectory(prefix="hermes-plugin-doctor-")
stack = ExitStack()
- home = Path(temporary_home.name)
- bundled = home / "bundled-plugins"
- plugins_root = home / "plugins"
- bundled.mkdir(parents=True)
- plugins_root.mkdir(parents=True)
- copied = plugins_root / plugin_path.name
- shutil.copytree(
- plugin_path,
- copied,
- ignore=shutil.ignore_patterns(".git", "__pycache__", ".pytest_cache", "*.pyc"),
- )
+ try:
+ # The temp dir enters the stack FIRST so any failure below — including
+ # ENOSPC or KeyboardInterrupt inside copytree — deterministically
+ # removes it instead of stranding a hermes-plugin-doctor-* directory
+ # until interpreter GC (or never, on a hard exit).
+ temporary_home = stack.enter_context(
+ tempfile.TemporaryDirectory(prefix="hermes-plugin-doctor-")
+ )
+ home = Path(temporary_home)
+ bundled = home / "bundled-plugins"
+ plugins_root = home / "plugins"
+ bundled.mkdir(parents=True)
+ plugins_root.mkdir(parents=True)
+ copied = plugins_root / plugin_path.name
+ shutil.copytree(
+ plugin_path,
+ copied,
+ ignore=shutil.ignore_patterns(".git", "__pycache__", ".pytest_cache", "*.pyc"),
+ )
- stack.enter_context(
- patch.dict(
- os.environ,
- {
- "HERMES_HOME": str(home),
- "HERMES_BUNDLED_PLUGINS": str(bundled),
- "HERMES_ENABLE_PROJECT_PLUGINS": "0",
- },
- clear=False,
+ stack.enter_context(
+ patch.dict(
+ os.environ,
+ {
+ "HERMES_HOME": str(home),
+ "HERMES_BUNDLED_PLUGINS": str(bundled),
+ "HERMES_ENABLE_PROJECT_PLUGINS": "0",
+ },
+ clear=False,
+ )
)
- )
- stack.enter_context(patch.object(socket, "create_connection", _deny_network))
- stack.enter_context(patch.object(socket.socket, "connect", _deny_network))
- stack.enter_context(patch.object(socket.socket, "connect_ex", _deny_network))
+ stack.enter_context(patch.object(socket, "create_connection", _deny_network))
+ stack.enter_context(patch.object(socket.socket, "connect", _deny_network))
+ stack.enter_context(patch.object(socket.socket, "connect_ex", _deny_network))
+ except BaseException:
+ stack.close()
+ raise
from hermes_cli.plugins import PluginManager
from tools.registry import registry
@@ -130,7 +140,6 @@ def _doctor_runtime(plugin_path: Path):
):
sys.modules.pop(name, None)
stack.close()
- temporary_home.cleanup()
@dataclass(frozen=True)
diff --git a/tests/hermes_cli/test_plugin_dev.py b/tests/hermes_cli/test_plugin_dev.py
index 5dbdac65dce..253ed27274d 100644
--- a/tests/hermes_cli/test_plugin_dev.py
+++ b/tests/hermes_cli/test_plugin_dev.py
@@ -1,6 +1,8 @@
from __future__ import annotations
import argparse
+
+import pytest
from pathlib import Path
from hermes_cli.subcommands.plugins import build_plugins_parser
@@ -138,8 +140,6 @@ def test_doctor_blocks_live_network(tmp_path: Path) -> None:
def test_resolve_rejects_directory_without_manifest(tmp_path: Path) -> None:
"""A non-plugin directory must not resolve — Doctor copies what it resolves."""
- import pytest
-
from hermes_cli.plugin_dev import resolve_plugin_path
not_a_plugin = tmp_path / "home"
@@ -215,3 +215,47 @@ def test_resolve_prefers_installed_id_over_unrelated_local_dir(
monkeypatch.setattr(plugin_dev, "get_hermes_home", lambda: hermes_home)
assert plugin_dev.resolve_plugin_path("sample") == installed.resolve()
+
+
+def test_doctor_removes_temp_home_when_staging_copy_fails(
+ tmp_path: Path, monkeypatch
+) -> None:
+ """A copy failure (e.g. ENOSPC) must not strand a hermes-plugin-doctor-* dir."""
+ import errno
+ import shutil
+ import tempfile
+
+
+
+ from hermes_cli import plugin_dev
+
+ plugin = tmp_path / "sample"
+ plugin.mkdir()
+ (plugin / "plugin.yaml").write_text("name: sample\n", encoding="utf-8")
+ (plugin / "__init__.py").write_text(
+ "def register(ctx):\n pass\n", encoding="utf-8"
+ )
+
+ scratch = tmp_path / "scratch-tmp"
+ scratch.mkdir()
+ monkeypatch.setattr(tempfile, "tempdir", str(scratch))
+
+ def _enospc(*_args, **_kwargs):
+ raise OSError(errno.ENOSPC, "No space left on device")
+
+ monkeypatch.setattr(shutil, "copytree", _enospc)
+
+ caught: OSError | None = None
+ try:
+ with plugin_dev._doctor_runtime(plugin):
+ pass # pragma: no cover - staging fails before yield
+ except OSError as exc:
+ caught = exc
+
+ assert caught is not None and caught.errno == errno.ENOSPC
+ # Check for leftovers WHILE the exception (and its traceback frames) is
+ # still referenced: the old code relied on TemporaryDirectory's GC
+ # finalizer, which cannot run while the traceback pins the frame — the
+ # exact window where a stranded hermes-plugin-doctor-* dir was observed.
+ leftovers = list(scratch.glob("hermes-plugin-doctor-*"))
+ assert leftovers == [], f"stranded doctor temp dirs: {leftovers}"