The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(plugins): isolate SystemExit from hook callbacks
Jonyd1221d43b74f
diff --git a/hermes_cli/plugins_dispatch.py b/hermes_cli/plugins_dispatch.pyindex bdbd4a9bba2..65ba6f268b4 100644--- a/hermes_cli/plugins_dispatch.py+++ b/hermes_cli/plugins_dispatch.py@@ -215,12 +215,12 @@ class PluginDispatchMixin:ret = self._invoke_hook_callback(cb, kwargs)if ret is not None:results.append(ret)- except Exception as exc:+ except (Exception, SystemExit) as exc:self._report_hook_failure(hook_name, cb, kwargs, exc)return resultsdef _report_hook_failure(- self, hook_name: str, cb: Callable, kwargs: Dict[str, Any], exc: Exception, *, surface: str = "Hook"+ self, hook_name: str, cb: Callable, kwargs: Dict[str, Any], exc: BaseException, *, surface: str = "Hook") -> None:"""One WARNING per distinct (hook, callback, error); identical repeats at DEBUG.@@ -273,7 +273,7 @@ class PluginDispatchMixin:context = contextvars.copy_context()done = threading.Event()outcome: Dict[str, Any] = {}- failure: Dict[str, Exception] = {}+ failure: Dict[str, BaseException] = {}def _release_token() -> None:with self._hook_timeout_lock:@@ -288,7 +288,7 @@ class PluginDispatchMixin:def _runner() -> None:try:outcome["value"] = context.run(self._invoke_hook_callback, cb, kwargs)- except Exception as exc:+ except BaseException as exc:failure["exc"] = excfinally:_release_token()diff --git a/tests/hermes_cli/test_plugins.py b/tests/hermes_cli/test_plugins.pyindex 52cc99a1afb..d6c0afa46e9 100644--- a/tests/hermes_cli/test_plugins.py+++ b/tests/hermes_cli/test_plugins.py@@ -1120,6 +1120,22 @@ class TestForceReloadSymmetry:mgr._hooks["post_tool_call"] = [boom, lambda **_kw: "survived"]assert mgr.invoke_hook("post_tool_call") == ["survived"]+ def test_system_exit_is_reported_under_timeout_path(self, monkeypatch, caplog):+ """Bounded hooks isolate SystemExit without losing its failure report."""+ monkeypatch.setattr(+ "hermes_cli.plugins._resolve_hook_callback_timeout", lambda: 1.0+ )++ def exits(**_kwargs):+ raise SystemExit("bounded plugin requested process exit")++ mgr = PluginManager()+ mgr._hooks["post_tool_call"] = [exits, lambda **_kw: "survived"]++ with caplog.at_level(logging.WARNING, logger="hermes_cli.plugins"):+ assert mgr.invoke_hook("post_tool_call") == ["survived"]+ assert "bounded plugin requested process exit" in caplog.text+def test_hook_callback_timeout_reads_config(self, tmp_path, monkeypatch):hermes_home = tmp_path / "hermes_test"hermes_home.mkdir(parents=True, exist_ok=True)@@ -1154,6 +1170,42 @@ class TestForceReloadSymmetry:assert mgr.invoke_hook("subagent_stop", parent_session_id="p1") == ["ok"]assert seen["thread"] is caller+ def test_system_exit_from_caller_thread_hook_is_isolated(self, monkeypatch, caplog):+ """A plugin dependency calling sys.exit() must not terminate hook dispatch."""+ monkeypatch.setattr(+ "hermes_cli.plugins._resolve_hook_callback_timeout", lambda: 1.0+ )++ def exits(**_kwargs):+ raise SystemExit("plugin requested process exit")++ mgr = PluginManager()+ mgr._hooks["subagent_stop"] = [exits, lambda **_kw: "survived"]++ with caplog.at_level(logging.WARNING, logger="hermes_cli.plugins"):+ assert mgr.invoke_hook("subagent_stop", parent_session_id="p1") == ["survived"]+ assert "plugin requested process exit" in caplog.text++ def test_keyboard_interrupt_from_caller_thread_hook_propagates(self, monkeypatch):+ """Plugin isolation must not swallow an operator's Ctrl-C."""+ monkeypatch.setattr(+ "hermes_cli.plugins._resolve_hook_callback_timeout", lambda: 1.0+ )+ later_calls = []++ def interrupts(**_kwargs):+ raise KeyboardInterrupt++ mgr = PluginManager()+ mgr._hooks["subagent_stop"] = [+ interrupts,+ lambda **_kw: later_calls.append(True),+ ]++ with pytest.raises(KeyboardInterrupt):+ mgr.invoke_hook("subagent_stop", parent_session_id="p1")+ assert later_calls == []+def test_hung_callback_suppresses_repeat_fires(self, monkeypatch):"""A still-running timed-out callback must not spawn another worker."""import time