Skip to content

Agent Zero AI framework

Preserve native tool calls through Context Doctor

Alessandrob41f0e53f67d
diff --git a/plugins/_context_doctor/AGENTS.md b/plugins/_context_doctor/AGENTS.md
index 4f02ed42c..f36a0dd56 100644
--- a/plugins/_context_doctor/AGENTS.md
+++ b/plugins/_context_doctor/AGENTS.md
@@ -15,6 +15,7 @@
## Local Contracts
+- Leave canonical `LLMResult.function_calls` and their accompanying text untouched in either transport. The agent dispatches those calls through the normal tool-policy gate; text repair must not suppress them or reinterpret a textual follow-up as another call.
- Repaired and fallback JSON is always minified.
- Nonempty non-tool output becomes `{"thoughts":[raw]}`; XML-like output becomes `{}` only when suppression is enabled.
- Blank-line-separated thoughts expand into separate entries after repair when the split strategy is enabled (default on). This rewrites model-authored thoughts for every repaired turn by design, not only the raw-text fallback branch.
diff --git a/plugins/_context_doctor/extensions/python/message_loop_result/_10_context_doctor.py b/plugins/_context_doctor/extensions/python/message_loop_result/_10_context_doctor.py
index 2b788438f..6c1f989da 100644
--- a/plugins/_context_doctor/extensions/python/message_loop_result/_10_context_doctor.py
+++ b/plugins/_context_doctor/extensions/python/message_loop_result/_10_context_doctor.py
@@ -23,6 +23,9 @@ class ContextDoctor(Extension):
# Extract LLM response
llm_result = result_data.get("llm_result")
+ # Canonical calls already belong to the agent's policy-gated dispatcher.
+ if getattr(llm_result, "function_calls", None):
+ return
response = getattr(llm_result, "response", None)
if not isinstance(response, str) or not response.strip():
return
diff --git a/plugins/_context_doctor/tests/test_context_doctor.py b/plugins/_context_doctor/tests/test_context_doctor.py
index db3babeb2..8626ecf0b 100644
--- a/plugins/_context_doctor/tests/test_context_doctor.py
+++ b/plugins/_context_doctor/tests/test_context_doctor.py
@@ -1,5 +1,10 @@
from types import SimpleNamespace
+from unittest.mock import AsyncMock
+import pytest
+
+from agent import Agent
+from helpers.llm_result import LLMResult
from plugins._context_doctor.extensions.python.message_loop_result._10_context_doctor import (
ContextDoctor,
)
@@ -162,6 +167,53 @@ def test_looks_like_tool_call_rejects_wrong_thoughts_type():
assert not looks_like_tool_call('{"thoughts":"x"}', '{"thoughts":"x"}')
[email protected]("mode", ["responses", "chat_completions"])
+ "Running the command now.",
+ '<tool>response</tool>',
+ '{"tool_name":"response","tool_args":{"text":"second call"}}',
+])
+async def test_extension_leaves_native_calls_and_accompanying_text_intact(monkeypatch, mode, text):
+ monkeypatch.setattr(
+ "plugins._context_doctor.extensions.python.message_loop_result._10_context_doctor.get_plugin_config",
+ lambda *args, **kwargs: {},
+ )
+ llm_result = LLMResult.from_response({
+ "id": "resp_native",
+ "usage": {"input_tokens": 2048, "input_tokens_details": {"cached_tokens": 1024}},
+ "output": [
+ {"type": "message", "role": "assistant", "content": [
+ {"type": "output_text", "text": text},
+ ]},
+ {"type": "function_call", "name": "code_execution_tool",
+ "call_id": "call_native", "arguments": '{"runtime":"terminal","code":"echo hi"}'},
+ ],
+ }, mode=mode)
+ before = llm_result.to_dict()
+ result_data = {"llm_result": llm_result}
+
+ agent = SimpleNamespace(
+ agent_name="A0",
+ context=SimpleNamespace(log=SimpleNamespace(log=lambda **kwargs: None)),
+ loop_data=SimpleNamespace(params_temporary={}),
+ hist_add_ai_response=lambda *args, **kwargs: None,
+ hist_add_warning=lambda **kwargs: SimpleNamespace(id="warning"),
+ read_prompt=lambda name: name,
+ get_data=lambda key: {},
+ _log_response_builtin_items=AsyncMock(),
+ _execute_tool_request=AsyncMock(return_value=None),
+ )
+ ContextDoctor(agent).execute(result_data)
+
+ assert llm_result.to_dict() == before
+ assert not result_data.get("skip_default_processing")
+ assert [call.name for call in llm_result.function_calls] == ["code_execution_tool"]
+ await Agent.process_llm_result_tools(agent, llm_result)
+ agent._execute_tool_request.assert_awaited_once()
+ assert agent._execute_tool_request.call_args.kwargs["tool_name"] == "code_execution_tool"
+
+
def test_extension_replaces_result_refreshes_log_and_response_item(monkeypatch):
monkeypatch.setattr(
"plugins._context_doctor.extensions.python.message_loop_result._10_context_doctor.get_plugin_config",