Agent Zero AI framework
Count empty responses toward unusable-response limit
linklitiade148979046
diff --git a/extensions/python/message_loop_result/AGENTS.md b/extensions/python/message_loop_result/AGENTS.mdindex 534972d61..c1395c5fb 100644--- a/extensions/python/message_loop_result/AGENTS.md+++ b/extensions/python/message_loop_result/AGENTS.md@@ -7,13 +7,14 @@## Ownership- Extensions receive mutable `result_data` with `llm_result` and may set `skip_default_processing` after fully handling the turn.-- `_20_empty_response.py` retries turns with neither response nor reasoning, using `fw.msg_empty_response.md` for agent-prefixed UI warning text only.-- `_30_repeat_response.py` retries nonempty response content that exactly matches `loop_data.last_response`, regardless of reasoning, using `fw.msg_repeat.md` for history and `fw.msg_repeat_response.md` for the agent-prefixed UI warning text.+- `_20_empty_response.py` retries turns with neither response nor reasoning, counts them toward the unusable-response limit without adding a warning to model history, and uses `fw.msg_empty_response.md` for agent-prefixed UI warning text only.+- `_30_repeat_response.py` retries response content that exactly matches `loop_data.last_response`, regardless of reasoning, using `fw.msg_repeat.md` for history and `fw.msg_repeat_response.md` for the agent-prefixed UI warning text.## Local Contracts- Files run in deterministic filename order.- A handler that sets `skip_default_processing` owns needed history and UI side effects for that turn.+- Handlers that should not add side effects after an earlier extension has handled the result must return when `skip_default_processing` is set.- Do not use this point to mutate streamed partial content.## Work Guidancediff --git a/extensions/python/message_loop_result/_20_empty_response.py b/extensions/python/message_loop_result/_20_empty_response.pyindex 3e3842911..aa79bf8c8 100644--- a/extensions/python/message_loop_result/_20_empty_response.py+++ b/extensions/python/message_loop_result/_20_empty_response.py@@ -4,14 +4,21 @@ from __future__ import annotationsfrom typing import Any+from helpers.errors import HandledExceptionfrom helpers.extension import Extensionfrom helpers.print_style import PrintStyle+from helpers.settings import get_settings+++STATE_KEY = "_unusable_response_failures"class EmptyResponse(Extension):def execute(self, result_data: dict[str, Any] | None = None, **kwargs: Any) -> None:if not self.agent or not isinstance(result_data, dict):return+ if result_data.get("skip_default_processing"):+ returnllm_result = result_data.get("llm_result")response = getattr(llm_result, "response", "")@@ -22,14 +29,23 @@ class EmptyResponse(Extension):returnwarning = self.agent.read_prompt("fw.msg_empty_response.md")- log_item = self.agent.loop_data.params_temporary.get("log_item_generating")- assistant_message = self.agent.hist_add_ai_response(- response,- id=log_item.id if log_item else "",- llm_result=llm_result,- )- self.agent._remember_llm_result_state(llm_result, assistant_message)PrintStyle(font_color="orange", padding=True).print(warning)+ state = self.agent.loop_data.params_persistent+ previous = state.get(STATE_KEY, {})+ previous_iteration = previous.get("iteration") if isinstance(previous, dict) else None+ count = (+ previous.get("count", 0) + 1+ if previous_iteration == self.agent.loop_data.iteration - 1+ else 1+ )+ state[STATE_KEY] = {"iteration": self.agent.loop_data.iteration, "count": count}+ limit = get_settings()["max_consecutive_unusable_responses"]+ if count >= limit:+ stop_message = self.agent.read_prompt(+ "fw.msg_unusable_response_limit.md", limit=limit+ )+ self.agent.context.log.log(type="warning", content=stop_message)+ raise HandledException(stop_message)self.agent.context.log.log(type="warning",content=f"{self.agent.agent_name}: {warning}",diff --git a/extensions/python/message_loop_result/_30_repeat_response.py b/extensions/python/message_loop_result/_30_repeat_response.pyindex 543bb0505..45457ff82 100644--- a/extensions/python/message_loop_result/_30_repeat_response.py+++ b/extensions/python/message_loop_result/_30_repeat_response.py@@ -12,12 +12,12 @@ class RepeatResponse(Extension):def execute(self, result_data: dict[str, Any] | None = None, **kwargs: Any) -> None:if not self.agent or not isinstance(result_data, dict):return+ if result_data.get("skip_default_processing"):+ returnllm_result = result_data.get("llm_result")response = getattr(llm_result, "response", "")- if not isinstance(response, str):- return- if not response.strip() or response != self.agent.loop_data.last_response:+ if not isinstance(response, str) or response != self.agent.loop_data.last_response:returnwarning = self.agent.read_prompt("fw.msg_repeat.md")diff --git a/prompts/fw.msg_repeat_response.md b/prompts/fw.msg_repeat_response.mdindex 4f7544ed0..22d427c90 100644--- a/prompts/fw.msg_repeat_response.md+++ b/prompts/fw.msg_repeat_response.md@@ -1 +1 @@-Repeated response detected. Retrying.+Repeated response detected. Retrying.\ No newline at end of filediff --git a/tests/test_message_loop_result.py b/tests/test_message_loop_result.pyindex eb7faec4e..46cdd1c55 100644--- a/tests/test_message_loop_result.py+++ b/tests/test_message_loop_result.py@@ -1,7 +1,11 @@from types import SimpleNamespace+from extensions.python.message_loop_result import _20_empty_response as empty_responsefrom extensions.python.message_loop_result._20_empty_response import EmptyResponsefrom extensions.python.message_loop_result._30_repeat_response import RepeatResponse+from extensions.python._functions.agent.Agent.hist_add_warning.end import (+ _90_stop_unusable_response_loop as response_loop,+)class FakeAgent:@@ -9,6 +13,8 @@ class FakeAgent:self.loop_data = SimpleNamespace(last_response=last_response,params_temporary={},+ params_persistent={},+ iteration=0,)self.logs = []self.context = SimpleNamespace(@@ -20,8 +26,11 @@ class FakeAgent:self.warnings = []self.history = []- def read_prompt(self, name):+ def read_prompt(self, name, **kwargs):+ if name == "fw.msg_unusable_response_limit.md":+ return f"stopped at {kwargs['limit']}"return {+ "fw.msg_misformat.md": "misformatted","fw.msg_empty_response.md": "empty","fw.msg_repeat.md": "repeat","fw.msg_repeat_response.md": "Repeated response detected. Retrying.",@@ -52,11 +61,47 @@ def test_empty_result_skips_default_processing():agent = FakeAgent("")assert _run(agent)["skip_default_processing"] is True- assert agent.history == [""]+ assert agent.history == []assert agent.warnings == []assert agent.logs == [{"type": "warning", "content": "A0: empty"}]+def test_empty_result_counts_toward_unusable_response_limit(monkeypatch):+ monkeypatch.setattr(+ empty_response,+ "get_settings",+ lambda: {"max_consecutive_unusable_responses": 2},+ )+ agent = FakeAgent("")++ assert _run(agent)["skip_default_processing"] is True++ agent.loop_data.iteration = 1+ try:+ _run(agent)+ except response_loop.HandledException as error:+ assert str(error) == "stopped at 2"+ else:+ raise AssertionError("empty response should stop at the configured limit")++ assert agent.loop_data.params_persistent[response_loop.STATE_KEY]["count"] == 2+++def test_later_handlers_skip_a_result_already_handled_by_an_extension():+ response = '{"tool_name":"response"}'+ agent = FakeAgent(response, last_response=response)+ result_data = {+ "llm_result": SimpleNamespace(response=response, reasoning=""),+ "skip_default_processing": True,+ }++ EmptyResponse(agent).execute(result_data)+ RepeatResponse(agent).execute(result_data)++ assert agent.history == []+ assert agent.warnings == []++def test_repeat_skips_default_processing():agent = FakeAgent('{"tool_name":"response"}', last_response='{"tool_name":"response"}')@@ -80,7 +125,7 @@ def test_repeat_ignores_reasoning():def test_result_with_reasoning_uses_default_processing():- agent = FakeAgent("", reasoning="thinking")+ agent = FakeAgent("", reasoning="thinking", last_response="previous")assert "skip_default_processing" not in _run(agent)assert agent.history == []