From ca9952cbb3087564fb19b5e26a0bb3564a1a3693 Mon Sep 17 00:00:00 2001 From: Teknium <127238744+teknium1@users.noreply.github.com> Date: Mon, 31 Aug 2026 07:44:45 -0700 Subject: [PATCH] fix(plugins): doctor temp home can no longer be stranded by a failed staging copy Follow-up to the salvaged manifest guard (#90859): the doctor's temporary HERMES_HOME now enters the ExitStack before the staging copytree, so ENOSPC / KeyboardInterrupt / any exception during staging deterministically removes the hermes-plugin-doctor-* directory instead of relying on the TemporaryDirectory GC finalizer (which cannot run while the traceback pins the frame). Regression test proven via sabotage run against the old code. --- contributors/emails/developer.kj@gmail.com | 1 + hermes_cli/plugin_dev.py | 63 ++++++++++++---------- tests/hermes_cli/test_plugin_dev.py | 48 ++++++++++++++++- 3 files changed, 83 insertions(+), 29 deletions(-) create mode 100644 contributors/emails/developer.kj@gmail.com diff --git a/contributors/emails/developer.kj@gmail.com b/contributors/emails/developer.kj@gmail.com new file mode 100644 index 0000000000..f60bb14092 --- /dev/null +++ b/contributors/emails/developer.kj@gmail.com @@ -0,0 +1 @@ +DevKJ diff --git a/hermes_cli/plugin_dev.py b/hermes_cli/plugin_dev.py index 647f7c2039..2687fd7489 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"), - ) - - stack.enter_context( - patch.dict( - os.environ, - { - "HERMES_HOME": str(home), - "HERMES_BUNDLED_PLUGINS": str(bundled), - "HERMES_ENABLE_PROJECT_PLUGINS": "0", - }, - clear=False, + 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-") ) - ) - 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)) + 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.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 5dbdac65dc..253ed27274 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}"