mirror of
https://github.com/agent0ai/agent-zero.git
synced 2026-12-04 17:38:45 +00:00
Preserve free text and reasoning-only output in the current generation and retry without intermediate warning rows. Require an explicit response tool call to finish in both API modes, including after provider built-in actions. Keep history metadata, Responses state, tool validation and the consecutive-failure stop intact. Allow core retries when Context Doctor and its prompts are unavailable. Validation: 411 backend tests and 2 Chromium live/replay grouping tests passed; live runtime health check passed.
4.1 KiB
4.1 KiB
Context Doctor Plugin DOX
Purpose
- Repair malformed Agent Zero tool-call JSON and preserve raw output as compact thoughts JSON when repair cannot produce a tool call.
Ownership
helpers/context_doctor.pytransforms output and refreshes log fields.hooks.pyinstalls the exact root-pinned repair dependency in the framework runtime.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 the internal no-action reminder (fw.msg_thoughts_fallback.md); the older UI notice is retained for compatibility.webui/config.htmlexposes debug and repair-strategy settings.
Local Contracts
- Core may convert complete alternate-format calls first. Context Doctor owns repair of the resulting text, including missing delimiters, unfinished arguments, fenced calls and selection among multiple JSON candidates. A failed conversion must not bypass repair or force a retry.
- Leave canonical
LLMResult.function_calls, provider built-in actions 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.
- Leave results already handled by an earlier
message_loop_resultextension (skip_default_processing) untouched, such as output-limit truncation retries. - Non-tool output in either transport becomes
{"thoughts":[raw]}and XML-like output becomes{}when suppression is enabled. Free text never becomes a final response; only an explicit response-tool call can finish the loop. Recognizable tool intent still enters repair. - 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.
_select_valuereturns only repaired tool calls or model-authored partial responses;transform_responsewraps otherwise-raw output as thoughts or suppresses XML according to configuration.extract_tools.extract_tool_requestdecides whether repaired output contains an action; thoughts/headline-only objects enter fallback.- Output without a valid tool request, including model-authored thoughts/headlines, must append its repaired assistant history and a
fw.msg_thoughts_fallback.mdinternal reminder, refresh the generating log, and setskip_default_processing. Retry silently without a warning log or final-response bubble; the configured terminal stop remains visible. - The fallback passes the repaired
llm_resulttohist_add_ai_response, which owns Responses-API state advancement (_remember_llm_result_state); do not call the state method directly. The fallback turn'sresponse_idis recorded so the next turn'sprevious_response_idchain 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, andfw.msg_reasoning_only.md. - Log kvps retain streamed
reasoningonly;update_log_itemdoes not restorethoughtsfrom current kvps.update_logcontrols only View Details content: off preserves the original model text (including XML/DSML before core conversion), on shows repaired JSON. Keep the first text in turn-localresult_data.raw_response; never replace provider metadata or model history with this diagnostic copy. - A repaired
responsetool call refreshes the response log item when streaming did not create it. - Runtime setup reads the
json_repairpin from rootrequirements.txt; do not duplicate its version in plugin code.
Work Guidance
- Repair completed model output, including syntactically incomplete JSON; do not apply repair to streaming prefixes.
- Use framework-installed
json_repair; apply plugin-local parser patch before repair. Do not vendor dependencies.
Verification
- Run
pytest plugins/_context_doctor/tests tests/test_leaked_tool_call_integration.py.
Child DOX Index
No child DOX files.