Skip to content

Agent Zero AI framework

Split unusable response loop control

linklitidbbdd26cc54a
diff --git a/extensions/python/message_loop_result/_20_loop_control.py b/extensions/python/message_loop_result/_20_empty_response.py
similarity index 65%
rename from extensions/python/message_loop_result/_20_loop_control.py
rename to extensions/python/message_loop_result/_20_empty_response.py
index ff2c45bf2..53fdc803d 100644
--- a/extensions/python/message_loop_result/_20_loop_control.py
+++ b/extensions/python/message_loop_result/_20_empty_response.py
@@ -1,4 +1,4 @@
-"""Handle completed model turns that should retry instead of dispatching tools."""
+"""Retry completed model turns with neither response nor reasoning."""
from __future__ import annotations
@@ -8,7 +8,7 @@ from helpers.extension import Extension
from helpers.print_style import PrintStyle
-class LoopControl(Extension):
+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
@@ -18,16 +18,10 @@ class LoopControl(Extension):
reasoning = getattr(llm_result, "reasoning", "")
if not isinstance(response, str) or not isinstance(reasoning, str):
return
-
- if not response.strip():
- if reasoning.strip():
- return
- warning = self.agent.read_prompt("fw.msg_empty_response.md")
- elif response != self.agent.loop_data.last_response:
+ if response.strip() or reasoning.strip():
return
- else:
- warning = self.agent.read_prompt("fw.msg_repeat.md")
+ warning = 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,
@@ -37,10 +31,9 @@ class LoopControl(Extension):
self.agent._remember_llm_result_state(llm_result, assistant_message)
warning_message = self.agent.hist_add_warning(message=warning)
PrintStyle(font_color="orange", padding=True).print(warning)
- log_content = warning
- if response:
- log_content = f"{self.agent.agent_name}: Repeated response detected. Retrying."
self.agent.context.log.log(
- type="warning", content=log_content, id=warning_message.id
+ type="warning",
+ content=f"{self.agent.agent_name}: {warning}",
+ id=warning_message.id,
)
result_data["skip_default_processing"] = True
diff --git a/extensions/python/message_loop_result/_30_repeat_response.py b/extensions/python/message_loop_result/_30_repeat_response.py
new file mode 100644
index 000000000..543bb0505
--- /dev/null
+++ b/extensions/python/message_loop_result/_30_repeat_response.py
@@ -0,0 +1,38 @@
+"""Retry completed model turns that repeat the prior response."""
+
+from __future__ import annotations
+
+from typing import Any
+
+from helpers.extension import Extension
+from helpers.print_style import PrintStyle
+
+
+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
+
+ llm_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:
+ return
+
+ warning = self.agent.read_prompt("fw.msg_repeat.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)
+ warning_message = self.agent.hist_add_warning(message=warning)
+ PrintStyle(font_color="orange", padding=True).print(warning)
+ self.agent.context.log.log(
+ type="warning",
+ content=f"{self.agent.agent_name}: {self.agent.read_prompt('fw.msg_repeat_response.md')}",
+ id=warning_message.id,
+ )
+ result_data["skip_default_processing"] = True
diff --git a/prompts/fw.msg_repeat_response.md b/prompts/fw.msg_repeat_response.md
new file mode 100644
index 000000000..4f7544ed0
--- /dev/null
+++ b/prompts/fw.msg_repeat_response.md
@@ -0,0 +1 @@
+Repeated response detected. Retrying.
diff --git a/tests/test_message_loop_result.py b/tests/test_message_loop_result.py
index 9ce75487b..2b32f692f 100644
--- a/tests/test_message_loop_result.py
+++ b/tests/test_message_loop_result.py
@@ -1,6 +1,7 @@
from types import SimpleNamespace
-from extensions.python.message_loop_result._20_loop_control import LoopControl
+from extensions.python.message_loop_result._20_empty_response import EmptyResponse
+from extensions.python.message_loop_result._30_repeat_response import RepeatResponse
class FakeAgent:
@@ -23,6 +24,7 @@ class FakeAgent:
return {
"fw.msg_empty_response.md": "empty",
"fw.msg_repeat.md": "repeat",
+ "fw.msg_repeat_response.md": "Repeated response detected. Retrying.",
}[name]
def hist_add_ai_response(self, response, **kwargs):
@@ -41,7 +43,8 @@ def _run(agent):
result_data = {
"llm_result": SimpleNamespace(response=agent.response, reasoning=agent.reasoning)
}
- LoopControl(agent).execute(result_data)
+ EmptyResponse(agent).execute(result_data)
+ RepeatResponse(agent).execute(result_data)
return result_data
@@ -51,6 +54,13 @@ def test_empty_result_skips_default_processing():
assert _run(agent)["skip_default_processing"] is True
assert agent.history == [""]
assert agent.warnings == ["empty"]
+ assert agent.logs == [
+ {
+ "type": "warning",
+ "content": "A0: empty",
+ "id": "warning",
+ }
+ ]
def test_repeat_skips_default_processing():
@@ -67,6 +77,14 @@ def test_repeat_skips_default_processing():
]
+def test_repeat_ignores_reasoning():
+ response = '{"tool_name":"response"}'
+ agent = FakeAgent(response, reasoning="thinking", last_response=response)
+
+ assert _run(agent)["skip_default_processing"] is True
+ assert agent.warnings == ["repeat"]
+
+
def test_result_with_reasoning_uses_default_processing():
agent = FakeAgent("", reasoning="thinking")