Agent Zero AI framework
Add split-thoughts repair strategy to context doctor
linklitifb84e46c69b3
diff --git a/plugins/_context_doctor/AGENTS.md b/plugins/_context_doctor/AGENTS.mdindex d3f5393eb..e0cf7b724 100644--- a/plugins/_context_doctor/AGENTS.md+++ b/plugins/_context_doctor/AGENTS.md@@ -11,12 +11,13 @@- `extensions/python/startup_migration/` prepares that dependency after startup and self-update.- `extensions/python/message_loop_result/` normalizes completed model output before default processing.- `prompts/` owns fallback warning texts.-- `webui/config.html` exposes XML suppression and log-detail settings.+- `webui/config.html` exposes debug and repair-strategy settings.## Local Contracts- 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).- A raw-text fallback conversion owns the turn: `fw.msg_thoughts_fallback.md` history warning, separate `fw.msg_thoughts_fallback_response.md` user notice, one unusable-response failure counted by the stop-unusable-response-loop extension, and `skip_default_processing` set.- Log kvps retain streamed `reasoning` and add transformed output; `update_log` controls only View Details content.- A repaired `response` tool call refreshes the response log item when streaming did not create it.diff --git a/plugins/_context_doctor/README.md b/plugins/_context_doctor/README.mdindex fc024d3ca..651b9fad1 100644--- a/plugins/_context_doctor/README.md+++ b/plugins/_context_doctor/README.md@@ -7,4 +7,5 @@ Repairs model tool call JSON before sending it to history and tool processing.- Uses `json_repair` after native parsing has completed.- Accepts only complete Agent Zero tool calls (`tool_name` and object `tool_args`).- Stores repaired tool calls as compact JSON; log kvps retain streamed reasoning and add transformed fields.+- Expands blank-line-separated thoughts into separate entries after repair when the split strategy is enabled.- Optionally replaces XML-like output with `{}` in cases where model uses native XML tool calls.diff --git a/plugins/_context_doctor/default_config.yaml b/plugins/_context_doctor/default_config.yamlindex aa5543214..960c6da9f 100644--- a/plugins/_context_doctor/default_config.yaml+++ b/plugins/_context_doctor/default_config.yaml@@ -1,2 +1,3 @@update_log: falsesuppress_xml: true+split_thoughts: truediff --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.pyindex d323993d6..3d38dd719 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@@ -30,7 +30,9 @@ class ContextDoctor(Extension):# Repair response before default processingconfig = get_plugin_config("_context_doctor", agent=self.agent) or {}transformed = transform_response(- response, suppress_xml=config.get("suppress_xml", True)+ response,+ suppress_xml=config.get("suppress_xml", True),+ split_thoughts=config.get("split_thoughts", True),)llm_result.response = transformeddiff --git a/plugins/_context_doctor/helpers/context_doctor.py b/plugins/_context_doctor/helpers/context_doctor.pyindex 443459f72..bff9557e2 100644--- a/plugins/_context_doctor/helpers/context_doctor.py+++ b/plugins/_context_doctor/helpers/context_doctor.py@@ -96,18 +96,43 @@ def _compact_json(value: dict[str, Any]) -> str:return json.dumps(value, ensure_ascii=False, separators=(",", ":"))-def transform_response(response: str, *, suppress_xml: bool) -> str:- """Repair model output, falling back to compact thoughts JSON for raw text."""+def _split_thoughts(value: dict[str, Any]) -> None:+ """Expand blank-line-separated thought strings into separate entries in place."""+ thoughts = value.get("thoughts")+ if not isinstance(thoughts, list):+ return+ split: list[str] = []+ for item in thoughts:+ if isinstance(item, str) and "\n\n" in item:+ split.extend(part for part in item.split("\n\n") if part)+ else:+ split.append(item)+ 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."""if response:tool_call, partial_response = _repair(response)if tool_call is not None:- return _compact_json(tool_call)+ return tool_callif partial_response is not None:- return _compact_json(partial_response)-+ return partial_responseif suppress_xml and "<" in response and ">" in response:+ return None+ return {"thoughts": [response]}+++def 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)+ if value is None:return "{}"- return _compact_json({"thoughts": [response]})+ if split_thoughts:+ _split_thoughts(value)+ return _compact_json(value)def is_thoughts_fallback(response: str, transformed: str) -> bool:diff --git a/plugins/_context_doctor/tests/conftest.py b/plugins/_context_doctor/tests/conftest.pyindex 4ed02237f..cbb7e9ba5 100644--- a/plugins/_context_doctor/tests/conftest.py+++ b/plugins/_context_doctor/tests/conftest.py@@ -7,6 +7,7 @@ import pytest_DEFAULT_CONFIG = {"update_log": False,"suppress_xml": True,+ "split_thoughts": True,}diff --git a/plugins/_context_doctor/tests/test_context_doctor.py b/plugins/_context_doctor/tests/test_context_doctor.pyindex 4da1220db..ce78ac2be 100644--- a/plugins/_context_doctor/tests/test_context_doctor.py+++ b/plugins/_context_doctor/tests/test_context_doctor.py@@ -60,6 +60,26 @@ def test_suppresses_xml_when_enabled():)+def test_splits_blank_line_thoughts_after_repair():+ response = '{"thoughts":["first\\n\\nsecond"]}'++ assert transform_response(response, suppress_xml=True) == (+ '{"thoughts":["first","second"]}'+ )+++def test_splits_blank_lines_in_raw_text_fallback():+ assert transform_response("first\n\nsecond", suppress_xml=False) == (+ '{"thoughts":["first","second"]}'+ )+++def test_keeps_blank_line_thoughts_when_split_disabled():+ assert transform_response(+ "first\n\nsecond", suppress_xml=False, split_thoughts=False+ ) == '{"thoughts":["first\\n\\nsecond"]}'++def test_updates_log_kvps_and_heading_while_preserving_raw_content():log_item = SimpleNamespace(kvps={"reasoning": "because"},diff --git a/plugins/_context_doctor/webui/config.html b/plugins/_context_doctor/webui/config.htmlindex 14328e3f6..51cc37cba 100644--- a/plugins/_context_doctor/webui/config.html+++ b/plugins/_context_doctor/webui/config.html@@ -23,6 +23,9 @@</label></div></div>+ </div>+ <div class="section">+ <div class="section-title">Strategies</div><div class="field"><div class="field-label"><div class="field-title">Suppress XML output</div>@@ -37,6 +40,20 @@</label></div></div>+ <div class="field">+ <div class="field-label">+ <div class="field-title">Split thoughts on blank lines</div>+ <div class="field-description">+ Replace blank lines inside thoughts with separate thought entries after JSON repair.+ </div>+ </div>+ <div class="field-control">+ <label class="toggle">+ <input type="checkbox" x-model="config.split_thoughts" />+ <span class="toggler"></span>+ </label>+ </div>+ </div></div></div></template>