Your lightweight open-source personal agent.
Python agent-frameworkai-agentai-agentschatbotchatopsdiscord-botllm-agentsllmslocal-llmmcpmodel-context-protocolmulti-agentopenai-compatibleopenclawpersonal-ai-assistantpythonself-hostedtelegram-bot-ai-assistantwebuiworkflow-automation
fix(agent): preserve LoopHook error semantics when extra hooks are present
Xubin Ren758c4e74c9d3
diff --git a/nanobot/agent/loop.py b/nanobot/agent/loop.pyindex 0e58fa557..c45257657 100644--- a/nanobot/agent/loop.py+++ b/nanobot/agent/loop.py@@ -102,6 +102,47 @@ class LoopHook(AgentHook):return self._loop._strip_think(content)+class _LoopHookChain(AgentHook):+ """Run the core loop hook first, then best-effort extra hooks.++ This preserves the historical failure behavior of ``LoopHook`` while still+ letting user-supplied hooks opt into ``CompositeHook`` isolation.+ """++ __slots__ = ("_primary", "_extras")++ def __init__(self, primary: AgentHook, extra_hooks: list[AgentHook]) -> None:+ self._primary = primary+ self._extras = CompositeHook(extra_hooks)++ def wants_streaming(self) -> bool:+ return self._primary.wants_streaming() or self._extras.wants_streaming()++ async def before_iteration(self, context: AgentHookContext) -> None:+ await self._primary.before_iteration(context)+ await self._extras.before_iteration(context)++ async def on_stream(self, context: AgentHookContext, delta: str) -> None:+ await self._primary.on_stream(context, delta)+ await self._extras.on_stream(context, delta)++ async def on_stream_end(self, context: AgentHookContext, *, resuming: bool) -> None:+ await self._primary.on_stream_end(context, resuming=resuming)+ await self._extras.on_stream_end(context, resuming=resuming)++ async def before_execute_tools(self, context: AgentHookContext) -> None:+ await self._primary.before_execute_tools(context)+ await self._extras.before_execute_tools(context)++ async def after_iteration(self, context: AgentHookContext) -> None:+ await self._primary.after_iteration(context)+ await self._extras.after_iteration(context)++ def finalize_content(self, context: AgentHookContext, content: str | None) -> str | None:+ content = self._primary.finalize_content(context, content)+ return self._extras.finalize_content(context, content)++class AgentLoop:"""The agent loop is the core processing engine.@@ -294,7 +335,7 @@ class AgentLoop:message_id=message_id,)hook: AgentHook = (- CompositeHook([loop_hook, *self._extra_hooks])+ _LoopHookChain(loop_hook, self._extra_hooks)if self._extra_hookselse loop_hook)diff --git a/tests/agent/test_hook_composite.py b/tests/agent/test_hook_composite.pyindex 8a43a4249..203c892fb 100644--- a/tests/agent/test_hook_composite.py+++ b/tests/agent/test_hook_composite.py@@ -308,6 +308,27 @@ async def test_agent_loop_extra_hook_error_isolation(tmp_path):assert content == "still works"+async def test_agent_loop_extra_hooks_do_not_swallow_loop_hook_errors(tmp_path):+ """Extra hooks must not change the core LoopHook failure behavior."""+ from nanobot.providers.base import LLMResponse, ToolCallRequest++ loop = _make_loop(tmp_path, hooks=[AgentHook()])+ loop.provider.chat_with_retry = AsyncMock(return_value=LLMResponse(+ content="working",+ tool_calls=[ToolCallRequest(id="c1", name="list_dir", arguments={"path": "."})],+ usage={},+ ))+ loop.tools.get_definitions = MagicMock(return_value=[])+ loop.tools.execute = AsyncMock(return_value="ok")++ async def bad_progress(*args, **kwargs):+ raise RuntimeError("progress failed")++ with pytest.raises(RuntimeError, match="progress failed"):+ await loop._run_agent_loop([], on_progress=bad_progress)++@pytest.mark.asyncioasync def test_agent_loop_no_hooks_backward_compat(tmp_path):"""Without hooks param, behavior is identical to before."""