Skip to content

The agent that grows with you

fix(compat): make skills hub hook installation idempotent

ethernetce16d3691b03
diff --git a/tests/tools/test_plugin_compat_warning.py b/tests/tools/test_plugin_compat_warning.py
index 2dc9d9779dd..479e12d7c78 100644
--- a/tests/tools/test_plugin_compat_warning.py
+++ b/tests/tools/test_plugin_compat_warning.py
@@ -44,3 +44,26 @@ def test_importing_the_facade_itself_does_not_warn():
warnings.simplefilter("always")
importlib.import_module("tools.web_tools")
assert not [w for w in rec if issubclass(w.category, HermesPluginCompatWarning)]
+
+
+def test_skills_hub_compat_hook_is_idempotent_across_reload_and_reinstall():
+ import tools.skills_hub as hub
+
+ hub = importlib.reload(hub)
+ assert hub.__getattr__ is not hub._plugin_compat_prev_getattr
+
+ hook = hub.__getattr__
+ previous = hub._plugin_compat_prev_getattr
+ hub._install_plugin_compat_getattr()
+ hub._install_plugin_compat_getattr()
+
+ assert hub.__getattr__ is hook
+ assert hub._plugin_compat_prev_getattr is previous
+ assert hub.HERMES_HOME.is_dir()
+ with pytest.raises(AttributeError):
+ getattr(hub, "not_a_real_hub_attribute")
+
+ name, (target_module, target_name) = next(iter(hub._PLUGIN_COMPAT_LAZY.items()))
+ with warnings.catch_warnings():
+ warnings.simplefilter("ignore")
+ assert getattr(hub, name) is getattr(importlib.import_module(target_module), target_name)
diff --git a/tools/skills_hub.py b/tools/skills_hub.py
index 14573088e6c..4d66a45ccff 100644
--- a/tools/skills_hub.py
+++ b/tools/skills_hub.py
@@ -78,17 +78,6 @@ def __getattr__(name: str):
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
-def __getattr__(name): # PEP 562 — chained onto the module's own __getattr__
- target = _PLUGIN_COMPAT_LAZY.get(name)
- if target is None:
- return _plugin_compat_prev_getattr(name)
- import importlib
- from hermes_cli.plugin_compat import warn_once
- warn_once(__name__, name, *target)
- return getattr(importlib.import_module(target[0]), target[1])
-# ---- END PLUGIN-COMPAT ----
-
-
# ---------------------------------------------------------------------------
# Install-path safety + guarded HTTP
# ---------------------------------------------------------------------------
@@ -486,4 +475,29 @@ _PLUGIN_COMPAT_LAZY = {
'windows_hide_flags': ('hermes_cli._subprocess_compat', 'windows_hide_flags'),
}
-_plugin_compat_prev_getattr = __getattr__
+
+def _plugin_compat_getattr(name):
+ target = _PLUGIN_COMPAT_LAZY.get(name)
+ if target is None:
+ return _plugin_compat_prev_getattr(name)
+ import importlib
+ from hermes_cli.plugin_compat import warn_once
+ warn_once(__name__, name, *target)
+ return getattr(importlib.import_module(target[0]), target[1])
+
+
+setattr(_plugin_compat_getattr, "_plugin_compat_wrapper", True)
+
+
+def _install_plugin_compat_getattr():
+ """Chain the compat resolver once, including across retained-module reloads."""
+ global __getattr__, _plugin_compat_prev_getattr
+ current = __getattr__
+ if getattr(current, "_plugin_compat_wrapper", False):
+ return
+ _plugin_compat_prev_getattr = current
+ __getattr__ = _plugin_compat_getattr
+
+
+_install_plugin_compat_getattr()
+# ---- END PLUGIN-COMPAT ----