Agent Zero AI framework
Fix raw-text fallback classification
linkliti73ba1bb0cc90
diff --git a/plugins/_context_doctor/AGENTS.md b/plugins/_context_doctor/AGENTS.mdindex 3f4f59cee..4f02ed42c 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 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.+- `_select_value` returns only repaired tool calls or model-authored partial responses; `transform_response` wraps otherwise-raw output as thoughts afterward. `looks_like_tool_call` rejects that synthesized wrapper, so it reaches the fallback branch. Repaired output is usable only with a non-empty thoughts list, headline/tool name string, or tool-args dict.- 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/helpers/context_doctor.py b/plugins/_context_doctor/helpers/context_doctor.pyindex fd0f28b91..6b1d61272 100644--- a/plugins/_context_doctor/helpers/context_doctor.py+++ b/plugins/_context_doctor/helpers/context_doctor.py@@ -110,26 +110,26 @@ def _split_thoughts(value: dict[str, Any]) -> None:value["thoughts"] = split-def _select_value(response: str, *, suppress_xml: bool) -> dict[str, Any] | None:- """Pick the repaired tool call, partial result, or raw-text fallback."""+def _select_value(response: str) -> dict[str, Any] | None:+ """Pick the repaired tool call or partial result."""if response:tool_call, partial_response = _repair(response)if tool_call is not None:return tool_callif partial_response is not None:return partial_response- if suppress_xml and "<" in response and ">" in response:- return None- return {"thoughts": [response]}+ return Nonedef transform_response(response: str, *, suppress_xml: bool, split_thoughts: bool = True) -> str:"""Repair model output, falling back to compact thoughts JSON for raw text."""- value = _select_value(response, suppress_xml=suppress_xml)+ value = _select_value(response)if value is None:- return "{}"+ if suppress_xml and "<" in response and ">" in response:+ return "{}"+ value = {"thoughts": [response]}if split_thoughts:_split_thoughts(value)return _compact_json(value)@@ -137,7 +137,7 @@ def transform_response(def looks_like_tool_call(response: str, transformed: str) -> bool:"""Return True if transformed output is a JSON with usable A0 content."""- if not response:+ if not response or _select_value(response) is None:return Falsetry:parsed = json.loads(transformed)diff --git a/plugins/_context_doctor/tests/test_context_doctor.py b/plugins/_context_doctor/tests/test_context_doctor.pyindex 93ea78e0a..db3babeb2 100644--- a/plugins/_context_doctor/tests/test_context_doctor.py+++ b/plugins/_context_doctor/tests/test_context_doctor.py@@ -102,20 +102,20 @@ def test_updates_log_kvps_and_heading_while_preserving_raw_content():assert log_item.data["heading"] == "A0: Done"-def test_converted_to_thoughts_detects_plain_text_wrap():- assert looks_like_tool_call("plain text", '{"thoughts":["plain text"]}')+def test_converted_to_thoughts_rejects_plain_text_wrap():+ assert not looks_like_tool_call("plain text", '{"thoughts":["plain text"]}')-def test_converted_to_thoughts_detects_split_multi_paragraph_wrap():+def test_converted_to_thoughts_rejects_split_multi_paragraph_wrap():response = "first\n\nsecond"transformed = '{"thoughts":["first","second"]}'- assert looks_like_tool_call(response, transformed)+ assert not looks_like_tool_call(response, transformed)def test_transform_and_fallback_agree_on_multi_paragraph_raw_text():response = "first\n\nsecond"transformed = transform_response(response, suppress_xml=False)- assert looks_like_tool_call(response, transformed)+ assert not looks_like_tool_call(response, transformed)def test_converted_to_thoughts_detects_native_thoughts_json():@@ -231,7 +231,7 @@ def test_extension_does_not_use_legacy_response_message_key(monkeypatch):assert "log_item_response" not in agent.loop_data.params_temporary-def test_extension_handles_fallback_with_warning_and_skip(monkeypatch):+def test_extension_handles_raw_text_fallback_with_warning_and_skip(monkeypatch):monkeypatch.setattr("plugins._context_doctor.extensions.python.message_loop_result._10_context_doctor.get_plugin_config",lambda *args, **kwargs: {"suppress_xml": True, "update_log": False},@@ -254,16 +254,16 @@ def test_extension_handles_fallback_with_warning_and_skip(monkeypatch):hist_add_ai_response=lambda message, **kwargs: ai_responses.append(message) or SimpleNamespace(id="ai"),hist_add_warning=lambda message: warnings.append(message) or SimpleNamespace(id="warning"),)- llm_result = SimpleNamespace(response="<xml>not a tool call</xml>")+ llm_result = SimpleNamespace(response="Hello there…")result_data = {"llm_result": llm_result}ContextDoctor(agent).execute(result_data)assert result_data["skip_default_processing"] is True- assert llm_result.response == "{}"- assert ai_responses == ["{}"]+ assert llm_result.response == '{"thoughts":["Hello there…"]}'+ assert ai_responses == ['{"thoughts":["Hello there…"]}']assert warnings == ["fallback warning"]assert logs[0]["content"] == "A0: fallback notice"assert logs[0]["id"] == "warning"assert log_item.data["kvps"]["reasoning"] == "thinking"- assert log_item.data["content"] == "<xml>not a tool call</xml>"+ assert log_item.data["content"] == "Hello there…"diff --git a/tests/test_tool_args_preference.py b/tests/test_tool_args_preference.pyindex 98b36ced5..e22c260f7 100644--- a/tests/test_tool_args_preference.py+++ b/tests/test_tool_args_preference.py@@ -27,7 +27,8 @@ def test_tool_args_preference_toggle_is_wired_in_webui() -> None:def test_message_stream_gates_tool_args_display_behind_preference() -> None:messages = read("webui/js/messages.js")- assert "if (preferencesStore.showToolArgs) {" in messages+ assert "if (preferencesStore.showToolArgs && !isResponse) {" in messagesassert "kvps?.tool_args ?? kvps?.args" in messages- assert 'kvps.tool_name !== "response"' in messages+ assert 'const isResponse = kvps?.tool_name === "response";' in messages+ assert "!reservedKeys.has(key)" in messagesassert 'icon://build[Tool]' in messages