Skip to content

Agent Zero AI framework

Preserve native calls in assistant history and repeat checks

Alessandro5602d5c4be7d
diff --git a/AGENTS.md b/AGENTS.md
index 75c431aac..a822b69f2 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -16,6 +16,7 @@
- `agent.py` owns `Agent`, `AgentContext`, and loop data.
- `Agent.hist_add_ai_response` owns Responses-API state advancement: it calls `_remember_llm_result_state` internally. Model turns pass an `LLMResult`; omitted results and legacy positional string IDs use the non-LLM sentinel. Callers must not invoke `_remember_llm_result_state` manually.
+- Native function calls use canonical call content for history and repeat comparison, even when accompanied by commentary. The canonical content passes through the normal history template/masking hook; provider output metadata remains intact.
- `initialize.py` owns framework initialization.
- `models.py` owns model-provider configuration and LiteLLM integration.
- `run_ui.py` is the WebUI entry point.
diff --git a/agent.py b/agent.py
index 716beabc7..0cd058693 100644
--- a/agent.py
+++ b/agent.py
@@ -780,6 +780,7 @@ class Agent:
id, llm_result = llm_result, None
if llm_result is None:
llm_result = LLMResult.non_llm()
+ message = llm_result.function_calls_text() or message
self.loop_data.last_response = message
content = self.parse_prompt("fw.ai_response.md", message=message)
msg = self.hist_add_message(
diff --git a/extensions/python/message_loop_result/AGENTS.md b/extensions/python/message_loop_result/AGENTS.md
index f88bbef8e..e1d2783a7 100644
--- a/extensions/python/message_loop_result/AGENTS.md
+++ b/extensions/python/message_loop_result/AGENTS.md
@@ -9,7 +9,7 @@
- Extensions receive mutable `result_data` with `llm_result` and may set `skip_default_processing` after fully handling the turn.
- `_20_empty_response.py` retries fully empty turns (no response and no reasoning): count them toward the unusable-response limit without adding a warning to model history, and use `fw.msg_empty_response.md` for agent-prefixed UI warning text only.
- `_20_empty_response.py` retries reasoning-only turns: add an agent-facing `fw.msg_reasoning_only.md` history warning with a separate `fw.msg_reasoning_only_response.md` user notice, and count them through the stop-unusable-response-loop extension.
-- `_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. State advancement is owned by `hist_add_ai_response`; do not call `_remember_llm_result_state` manually.
+- `_30_repeat_response.py` compares canonical function-call content when native calls exist, otherwise response text; it retries 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. State advancement is owned by `hist_add_ai_response`; do not call `_remember_llm_result_state` manually.
## Local Contracts
diff --git a/extensions/python/message_loop_result/_30_repeat_response.py b/extensions/python/message_loop_result/_30_repeat_response.py
index 4b4e09ba7..a9ca0780e 100644
--- a/extensions/python/message_loop_result/_30_repeat_response.py
+++ b/extensions/python/message_loop_result/_30_repeat_response.py
@@ -17,6 +17,8 @@ class RepeatResponse(Extension):
llm_result = result_data.get("llm_result")
response = getattr(llm_result, "response", "")
+ if getattr(llm_result, "function_calls", None):
+ response = llm_result.function_calls_text()
if not isinstance(response, str) or response != self.agent.loop_data.last_response:
return
diff --git a/tests/test_message_loop_result.py b/tests/test_message_loop_result.py
index ac1c418e8..320694d31 100644
--- a/tests/test_message_loop_result.py
+++ b/tests/test_message_loop_result.py
@@ -140,3 +140,29 @@ def test_reasoning_only_retries_with_agent_warning():
"id": "warning",
}
]
+
+
+def test_native_repeat_detection_compares_calls_instead_of_commentary():
+ from helpers.llm_result import LLMResult
+
+ def result(query, commentary):
+ return LLMResult.from_response({
+ "output_text": commentary,
+ "output": [{
+ "type": "function_call", "name": "lookup", "call_id": "call",
+ "arguments": {"q": query},
+ }],
+ })
+
+ previous = result("first", "Working.")
+ agent = FakeAgent("", last_response=previous.function_calls_text())
+ changed_call = {"llm_result": result("second", "Working.")}
+ RepeatResponse(agent).execute(changed_call)
+ assert not changed_call.get("skip_default_processing")
+ assert agent.warnings == []
+
+ repeated_call = {"llm_result": result("first", "Different commentary.")}
+ RepeatResponse(agent).execute(repeated_call)
+ assert repeated_call["skip_default_processing"] is True
+ assert agent.history == [previous.function_calls_text()]
+ assert agent.warnings == ["repeat"]
diff --git a/tests/test_responses_architecture.py b/tests/test_responses_architecture.py
index e42bd328a..f90a8ecf9 100644
--- a/tests/test_responses_architecture.py
+++ b/tests/test_responses_architecture.py
@@ -76,7 +76,7 @@ def test_llm_result_missing_mode_keeps_legacy_default(data):
@pytest.mark.parametrize("call_form", [
- "message", "legacy_id", "keyword_id", "result", "result_id", "keywords",
+ "message", "legacy_id", "keyword_id", "result", "result_id", "keywords", "native",
])
def test_history_response_call_compatibility_preserves_state(monkeypatch, call_form):
monkeypatch.setattr(extension, "call_extensions_sync", lambda *args, **kwargs: None)
@@ -94,6 +94,11 @@ def test_history_response_call_compatibility_preserves_state(monkeypatch, call_f
agent._remember_llm_result_state = remember
result = LLMResult(response_id="resp_1", provider_model_key="test/model")
+ if call_form == "native":
+ result = LLMResult.from_response({
+ "id": "resp_1", "output_text": "hello",
+ "output": [{"type": "function_call", "name": "lookup", "call_id": "call_1", "arguments": '{"q":"a0"}'}],
+ }, provider_model_key="test/model")
args, kwargs = {
"message": ((), {}),
"legacy_id": (("message_id",), {}),
@@ -101,13 +106,17 @@ def test_history_response_call_compatibility_preserves_state(monkeypatch, call_f
"result": ((result,), {}),
"result_id": ((result, "message_id"), {}),
"keywords": ((), {"llm_result": result, "id": "message_id"}),
+ "native": ((), {"llm_result": result, "id": "message_id"}),
}[call_form]
message = agent.hist_add_ai_response("hello", *args, **kwargs)
- expected = result if call_form in {"result", "result_id", "keywords"} else LLMResult.non_llm()
+ expected = result if call_form in {"result", "result_id", "keywords", "native"} else LLMResult.non_llm()
assert remembered == [expected]
- assert result_from_metadata(message.metadata) == expected
+ assert result_from_metadata(message.metadata).metadata() == expected.metadata()
assert agent.history.all_messages() == [message]
- assert agent.loop_data.last_response == "hello"
+ assert message.content == (expected.function_calls_text() or "hello")
+ assert agent.loop_data.last_response == message.content
+ if call_form == "native":
+ assert result.response == "hello"
if call_form not in {"message", "result"}:
assert message.id == "message_id"
state = agent.get_data(Agent.DATA_NAME_RESPONSES_STATE)