Skip to content

The agent that grows with you

fix(pm): only fall back on import/IO errors when discovering plugin aliases

Charan Rathore4941a296bbe5
diff --git a/pm/plugin_eviction.py b/pm/plugin_eviction.py
index 34585faed49..b56cd07f9fb 100644
--- a/pm/plugin_eviction.py
+++ b/pm/plugin_eviction.py
@@ -21,6 +21,7 @@ import base64
import hashlib
import io
import json
+import logging
import subprocess
import sys
from pathlib import Path
@@ -30,6 +31,8 @@ from pm.environments import install_state_dir, runtime_facts_path
from pm.filesystem import durable_write_bytes, file_digest, native, read_bytes_or_none
from pm.package import InstallError
+logger = logging.getLogger(__name__)
+
Entry = tuple[Path, str, Path] # (home plugins dir, selection key, plugin dir)
@@ -80,7 +83,10 @@ def _spellings(name: str) -> set[str]:
from hermes_cli.plugins_cmd import _plugin_aliases
return _plugin_aliases(name)
- except Exception: # alias discovery must never fail an update
+ except (ImportError, OSError) as exc:
+ # Discovery only adds manifest-name aliases; the key and its bare leaf are always known.
+ # Anything else is a real bug in the plugin tree and should surface, not be hidden.
+ logger.warning("Could not discover aliases of plugin '%s': %s", name, exc)
return {name, name.split("/")[-1]}
diff --git a/tests/pm/test_plugin_eviction_config.py b/tests/pm/test_plugin_eviction_config.py
index 41d95e4562e..bed274b1718 100644
--- a/tests/pm/test_plugin_eviction_config.py
+++ b/tests/pm/test_plugin_eviction_config.py
@@ -9,10 +9,14 @@ from pathlib import Path
import pytest
import hermes_yaml as yaml
+import hermes_cli.plugins_cmd as _plugins_cmd
import pm.publication as publication
from pm.plugin_eviction import PluginEviction, _discard_aliases
+_REAL_ALIASES = _plugins_cmd._plugin_aliases
+
+
@pytest.fixture(autouse=True)
def _no_alias_discovery(monkeypatch):
"""Alias discovery scans installed plugins; these tests only need key + bare leaf."""
@@ -150,3 +154,41 @@ def test_discard_aliases_keeps_other_entries_in_order():
names = ["a", "x", "b", "x"]
_discard_aliases(names, {"x"})
assert names == ["a", "b"]
+
+
+def test_alias_discovery_failure_from_io_falls_back_and_is_logged(tmp_path, monkeypatch, caplog):
+ import hermes_cli.plugins_cmd as plugins_cmd
+
+ def unreadable(key, entries=None):
+ raise OSError("tree unreadable")
+
+ monkeypatch.setattr(plugins_cmd, "_plugin_aliases", unreadable)
+ with caplog.at_level("WARNING", logger="pm.plugin_eviction"):
+ cfg, _ = _evict(tmp_path, ["web/firecrawl"], "plugins:\n enabled: [web/firecrawl, firecrawl, keep]\n")
+ assert cfg["plugins"]["enabled"] == ["keep"]
+ assert "tree unreadable" in caplog.text
+
+
+def test_unexpected_alias_discovery_error_is_not_swallowed(tmp_path, monkeypatch):
+ import hermes_cli.plugins_cmd as plugins_cmd
+
+ def broken(key, entries=None):
+ raise RuntimeError("half-broken plugin tree")
+
+ monkeypatch.setattr(plugins_cmd, "_plugin_aliases", broken)
+ with pytest.raises(RuntimeError, match="half-broken plugin tree"):
+ _evict(tmp_path, ["plug-a"], "plugins:\n enabled: [plug-a]\n")
+
+
+def test_real_alias_discovery_skips_a_malformed_manifest_and_still_removes_aliases(tmp_path, monkeypatch):
+ monkeypatch.setenv("HERMES_HOME", str(tmp_path))
+ monkeypatch.setattr(_plugins_cmd, "_plugin_aliases", _REAL_ALIASES)
+ good = tmp_path / "plugins" / "web" / "firecrawl"
+ good.mkdir(parents=True)
+ (good / "plugin.yaml").write_text("name: web-firecrawl\n", encoding="utf-8")
+ bad = tmp_path / "plugins" / "broken"
+ bad.mkdir(parents=True)
+ (bad / "plugin.yaml").write_text("name: [unterminated\n", encoding="utf-8")
+ cfg, _ = _evict(tmp_path, ["web/firecrawl"],
+ "plugins:\n enabled: [web/firecrawl, web-firecrawl, firecrawl, keep]\n")
+ assert cfg["plugins"]["enabled"] == ["keep"]