Skip to content

Agent Zero AI framework

Tighten context doctor usable-response detection

linkliti093b9106ef09
diff --git a/plugins/_context_doctor/AGENTS.md b/plugins/_context_doctor/AGENTS.md
index 9dd0de858..3f4f59cee 100644
--- a/plugins/_context_doctor/AGENTS.md
+++ b/plugins/_context_doctor/AGENTS.md
@@ -18,7 +18,7 @@
- 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.
-- `looks_like_tool_call` returns True when the transformed output is a dict containing at least one A0 key (thoughts/headline/tool_name/tool_args). Empty dicts or dicts without any A0 key fall through to the raw-text fallback branch.
+- `looks_like_tool_call` returns True only when the transformed output is a dict containing at least one A0 key (thoughts/headline/tool_name/tool_args) with a usable non-empty value: non-empty list of non-empty strings for `thoughts`, non-empty string for `headline`/`tool_name`, non-empty dict for `tool_args`. Empty dicts, missing keys, or keys with empty/blank values fall through to the raw-text fallback branch.
- A raw-text fallback conversion must emit a `fw.msg_thoughts_fallback.md` history warning and a separate `fw.msg_thoughts_fallback_response.md` user notice, refresh the generating log and response item with the transformed thoughts JSON, and set `skip_default_processing` so the retry advances the unusable-response counter.
- The fallback passes the repaired `llm_result` to `hist_add_ai_response`, which owns Responses-API state advancement (`_remember_llm_result_state`); do not call the state method directly. The fallback turn's `response_id` is recorded so the next turn's `previous_response_id` chain stays intact.
- The fallback warning is counted by the core stop-unusable-response loop alongside `fw.msg_misformat.md`, `fw.msg_repeat.md`, `fw.msg_empty_response.md`, and `fw.msg_reasoning_only.md`.
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 5fa677521..2b788438f 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
@@ -24,7 +24,7 @@ class ContextDoctor(Extension):
# Extract LLM response
llm_result = result_data.get("llm_result")
response = getattr(llm_result, "response", None)
- if not isinstance(response, str) or not response:
+ if not isinstance(response, str) or not response.strip():
return
# Repair response before default processing
diff --git a/plugins/_context_doctor/helpers/context_doctor.py b/plugins/_context_doctor/helpers/context_doctor.py
index 52e24df75..fd0f28b91 100644
--- a/plugins/_context_doctor/helpers/context_doctor.py
+++ b/plugins/_context_doctor/helpers/context_doctor.py
@@ -136,7 +136,7 @@ def transform_response(
def looks_like_tool_call(response: str, transformed: str) -> bool:
- """Return True if transformed output is a valid A0 response JSON."""
+ """Return True if transformed output is a JSON with usable A0 content."""
if not response:
return False
try:
@@ -145,7 +145,25 @@ def looks_like_tool_call(response: str, transformed: str) -> bool:
return False
if not isinstance(parsed, dict) or not parsed:
return False
- return any(k in parsed for k in ("thoughts", "headline", "tool_name", "tool_args"))
+
+ for key in ("thoughts", "headline", "tool_name", "tool_args"):
+ if key not in parsed:
+ continue
+ value = parsed[key]
+ if key == "thoughts":
+ if (
+ isinstance(value, list)
+ and value
+ and all(isinstance(item, str) and item.strip() for item in value)
+ ):
+ return True
+ elif key in ("headline", "tool_name"):
+ if isinstance(value, str) and value.strip():
+ return True
+ elif key == "tool_args":
+ if isinstance(value, dict) and value:
+ return True
+ return False
def update_log_item(
diff --git a/plugins/_context_doctor/tests/test_context_doctor.py b/plugins/_context_doctor/tests/test_context_doctor.py
index 584563bf5..93ea78e0a 100644
--- a/plugins/_context_doctor/tests/test_context_doctor.py
+++ b/plugins/_context_doctor/tests/test_context_doctor.py
@@ -138,6 +138,30 @@ def test_looks_like_tool_call_rejects_non_a0_dict():
assert not looks_like_tool_call('{"foo":"bar"}', '{"foo":"bar"}')
+def test_looks_like_tool_call_rejects_empty_thoughts_list():
+ assert not looks_like_tool_call('{"thoughts":[]}', '{"thoughts":[]}')
+
+
+def test_looks_like_tool_call_rejects_blank_thoughts_entries():
+ assert not looks_like_tool_call('{"thoughts":[""," "]}', '{"thoughts":[""," "]}')
+
+
+def test_looks_like_tool_call_rejects_empty_headline():
+ assert not looks_like_tool_call('{"headline":""}', '{"headline":""}')
+
+
+def test_looks_like_tool_call_rejects_empty_tool_name():
+ assert not looks_like_tool_call('{"tool_name":""}', '{"tool_name":""}')
+
+
+def test_looks_like_tool_call_rejects_empty_tool_args():
+ assert not looks_like_tool_call('{"tool_args":{}}', '{"tool_args":{}}')
+
+
+def test_looks_like_tool_call_rejects_wrong_thoughts_type():
+ assert not looks_like_tool_call('{"thoughts":"x"}', '{"thoughts":"x"}')
+
+
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",