Skip to content

Commit e10635a

Browse files
committed
fix: restore Context Doctor handling before tool retries
Separate leaked-call classification from retry handling so Context Doctor can apply XML suppression, thought splitting, and log settings before the history entry is saved. Keep rejected calls out of JSON repair and tool dispatch; disabling XML suppression retains them only as thoughts. Core still owns bounded retries and history insertion, defaults to {} when the plugin is disabled, and clears replayable Responses output and pending continuation state. Update plugin settings guidance and ownership docs. Validation: 455 focused tests passed locally and in Docker's framework runtime. Restarted the idle live app, verified health and actual extension ordering, and found no startup error markers.
1 parent aa19223 commit e10635a

9 files changed

Lines changed: 195 additions & 46 deletions

File tree

‎extensions/python/message_loop_result/AGENTS.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66

77
## Ownership
88

9-
- `_05_leaked_tool_call.py` classifies completed text once through `helpers.extract_tools`, then authorizes a complete candidate using the Responses turn map or lazily resolved Chat Completions prompt/MCP surface. Missing/empty/failed discovery rejects recovery. It records the decision in `params_temporary["leaked_tool_call"]` for Context Doctor and response logging; neither reparses it. Rejected/truncated calls insert `{}` through `hist_add_ai_response` with no replayable output items, emit a static counted warning, and skip processing. The original result remains available for diagnostics. Responses rejects clear the pending provider continuation after history insertion, retaining recorded response IDs while forcing the next turn to use sanitized local history. Provider-reported truncation without a recoverable call remains owned by `_05_truncated_response.py`. Native calls remain authoritative; quoted examples and multiple calls are never recovered.
9+
- `_05_leaked_tool_call.py` classifies completed text once through `helpers.extract_tools`, then authorizes a complete candidate using the Responses turn map or lazily resolved Chat Completions prompt/MCP surface. Missing/empty/failed discovery rejects recovery. It records the decision in `params_temporary["leaked_tool_call"]` for Context Doctor, retry handling and response logging; none reparses it. Native calls remain authoritative; quoted examples and multiple calls are never recovered.
1010
- Extensions receive mutable `result_data` with `llm_result` and may set `skip_default_processing` after fully handling the turn.
1111
- `_05_truncated_response.py` retries turns the provider cut off at the output token limit (`LLMResult.truncated`) that carry no complete tool request: add an agent-facing `fw.msg_output_limit.md` history warning with a separate `fw.msg_output_limit_response.md` user notice naming the finish reason, instead of the generic misformat warning, and count them through the stop-unusable-response-loop extension. Native calls are complete only when every raw argument value is a JSON object; a synthesized envelope around malformed arguments does not qualify.
12+
- `_15_tool_call_retry.py` handles rejected/truncated leaked calls after Context Doctor can supply non-executable history in `result_data["tool_call_retry_response"]`. Without a plugin override it uses `{}`. It inserts history once through `hist_add_ai_response` with no replayable output items, emits a static counted warning, and skips dispatch. The original result remains available for diagnostics. Responses rejects clear the pending provider continuation after history insertion, retaining recorded response IDs while forcing the next turn to use the formatted local history. Earlier provider-truncation handling remains authoritative.
1213
- `_20_empty_response.py` retries fully empty turns (no response and no reasoning): count them toward the unusable-response limit without adding a warning to model history, and use `fw.msg_empty_response.md` for the UI warning text only.
1314
- `_20_empty_response.py` retries reasoning-only turns: add an agent-facing `fw.msg_reasoning_only.md` history warning with a separate `fw.msg_reasoning_only_response.md` user notice, and count them through the stop-unusable-response-loop extension.
1415
- `_30_repeat_response.py` compares `LLMResult.repeat_response_text()` for native calls (public text plus all calls), otherwise full response text. It retries content that exactly matches `loop_data.last_response`, regardless of separate reasoning or provider IDs, using `fw.msg_repeat.md` for history and `fw.msg_repeat_response.md` for the UI warning text. State advancement is owned by `hist_add_ai_response`; do not call `_remember_llm_result_state` manually.

‎extensions/python/message_loop_result/_05_leaked_tool_call.py‎

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
"""Recover leaked text calls before optional repair or final-answer fallback."""
1+
"""Classify and authorize leaked calls before optional history cleanup."""
22

33
from __future__ import annotations
44

55
import json
6-
from dataclasses import replace
76
from typing import Any
87

98
from helpers.extension import Extension
@@ -64,28 +63,3 @@ def execute(self, result_data: dict[str, Any] | None = None, **kwargs: Any) -> N
6463
if getattr(llm_result, "truncated", False):
6564
return
6665
params["leaked_tool_call"] = status
67-
if status == "quoted":
68-
return
69-
70-
# Do not teach the next turn to repeat rejected syntax or replay it via
71-
# provider output items. Keep the original result for diagnostics.
72-
result_data["skip_default_processing"] = True
73-
log_item = params.get("log_item_generating")
74-
self.agent.hist_add_ai_response(
75-
"{}", id=log_item.id if log_item else "",
76-
llm_result=replace(llm_result, response="{}", output_items=[]),
77-
)
78-
if llm_result.mode == "responses" and llm_result.response_id:
79-
self.agent._clear_responses_pending_state()
80-
prompt = (
81-
"fw.msg_truncated_tool_call.md"
82-
if status == "truncated"
83-
else "fw.msg_leaked_tool_call.md"
84-
)
85-
warning = self.agent.read_prompt(prompt)
86-
warning_message = self.agent.hist_add_warning(message=warning)
87-
self.agent.context.log.log(
88-
type="warning",
89-
content=f"{self.agent.agent_name}: {warning}",
90-
id=warning_message.id,
91-
)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
"""Retry rejected calls after optional plugins format their history entry."""
2+
3+
from dataclasses import replace
4+
from typing import Any
5+
6+
from helpers.extension import Extension
7+
8+
9+
class ToolCallRetry(Extension):
10+
def execute(self, result_data: dict[str, Any] | None = None, **kwargs: Any) -> None:
11+
if not self.agent or not isinstance(result_data, dict):
12+
return
13+
if result_data.get("skip_default_processing"):
14+
return
15+
params = self.agent.loop_data.params_temporary
16+
status = params.get("leaked_tool_call")
17+
if status not in {"rejected", "truncated"}:
18+
return
19+
20+
llm_result = result_data["llm_result"]
21+
history_response = result_data.get("tool_call_retry_response", "{}")
22+
result_data["skip_default_processing"] = True
23+
log_item = params.get("log_item_generating")
24+
self.agent.hist_add_ai_response(
25+
history_response, id=log_item.id if log_item else "",
26+
llm_result=replace(llm_result, response=history_response, output_items=[]),
27+
)
28+
# A provider continuation would replay the original, unformatted output.
29+
if llm_result.mode == "responses" and llm_result.response_id:
30+
self.agent._clear_responses_pending_state()
31+
prompt = (
32+
"fw.msg_truncated_tool_call.md"
33+
if status == "truncated"
34+
else "fw.msg_leaked_tool_call.md"
35+
)
36+
warning = self.agent.read_prompt(prompt)
37+
warning_message = self.agent.hist_add_warning(message=warning)
38+
self.agent.context.log.log(
39+
type="warning",
40+
content=f"{self.agent.agent_name}: {warning}",
41+
id=warning_message.id,
42+
)

‎plugins/_context_doctor/AGENTS.md‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@
1515

1616
## Local Contracts
1717

18-
- Core leaked-call recovery runs first. Return on `skip_default_processing`; read its `params_temporary["leaked_tool_call"]` decision instead of classifying again. Recovered calls pass through as canonical JSON; quoted examples use the existing raw-text fallback without JSON repair. Rejected/truncated and multiple envelopes never reach this plugin's repair helpers.
19-
- The core rejection path stores `{}` instead of rejected syntax and removes replayable output/continuation state before retrying. This applies with or without this plugin; `suppress_xml` continues to govern this plugin's non-tool XML fallback.
18+
- Core leaked-call classification runs first. Return on `skip_default_processing`; read its `params_temporary["leaked_tool_call"]` decision instead of classifying again. Recovered calls pass through as canonical JSON; quoted examples use the existing raw-text fallback without JSON repair. Rejected/truncated and multiple envelopes never enter JSON repair or become executable calls.
19+
- For rejected/truncated leaked calls, set `result_data["tool_call_retry_response"]` and refresh the generating log before core retry handling. Use `{}` by default; when `suppress_xml` is off, retain rejected XML-like text as thoughts through `format_fallback`, respecting `split_thoughts`. Rejected non-XML JSON stays `{}`. Core owns history insertion, removal of replayable output/continuation state, the counted warning and skipping dispatch, with `{}` as its fallback when this plugin is disabled.
2020
- 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.
2121
- Repaired and fallback JSON is always minified.
2222
- Leave results already handled by an earlier `message_loop_result` extension (`skip_default_processing`) untouched, such as output-limit truncation retries.
2323
- Usable Responses output text stays untouched for the core response-tool dispatcher. Recognizable tool intent still enters repair; Chat Completions non-tool output becomes `{"thoughts":[raw]}` and XML-like output becomes `{}` when suppression is enabled.
2424
- 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.
25-
- `_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.
25+
- `_select_value` returns only repaired tool calls or model-authored partial responses; `transform_response` uses the repair-free `format_fallback` for otherwise-raw output. `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.
2626
- 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.
2727
- 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.
2828
- 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`.
@@ -37,7 +37,7 @@
3737

3838
## Verification
3939

40-
- Run `pytest plugins/_context_doctor/tests`.
40+
- Run `pytest plugins/_context_doctor/tests tests/test_leaked_tool_call_integration.py`.
4141

4242
## Child DOX Index
4343

‎plugins/_context_doctor/README.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Repairs model tool call JSON before sending it to history and tool processing.
88
- Accepts only complete Agent Zero tool calls (`tool_name` and object `tool_args`).
99
- Stores repaired tool calls as compact JSON; log kvps retain streamed reasoning and add transformed fields.
1010
- Expands blank-line-separated thoughts into separate entries after repair when the split strategy is enabled.
11-
- Core recovery validates standalone leaked XML/DSML/native calls against offered tools before this plugin; rejected or truncated calls use bounded retries with `{}` in model history so malformed syntax is not repeated back to the model. Responses retries also drop the rejected turn's provider continuation and replayable output items.
11+
- Core recovery validates standalone leaked XML/DSML/native calls against offered tools before this plugin. For rejected or truncated calls, Context Doctor formats the history and log before core performs the bounded retry. Responses retries also drop the rejected turn's provider continuation and replayable output items.
1212
- Uses the core recovery decision without parsing it again; quoted tool examples remain non-executable.
13-
- Optionally replaces non-tool XML-like output with `{}`.
13+
- Suppresses unusable XML-like output in model history by default. Turning suppression off keeps rejected XML as non-executable thoughts, respecting thought splitting and log settings. Rejected JSON remains suppressed.
14+
- Core still retries safely with `{}` in history when Context Doctor is disabled.

‎plugins/_context_doctor/extensions/python/message_loop_result/_10_context_doctor.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from helpers.plugins import get_plugin_config
1111
from helpers.print_style import PrintStyle
1212
from plugins._context_doctor.helpers.context_doctor import (
13+
format_fallback,
1314
looks_like_tool_call,
1415
transform_response,
1516
update_log_item,
@@ -37,6 +38,21 @@ def execute(self, result_data: dict[str, Any] | None = None, **kwargs: Any) -> N
3738
config = get_plugin_config("_context_doctor", agent=self.agent) or {}
3839
params = getattr(getattr(self.agent, "loop_data", None), "params_temporary", {})
3940
recovery = params.get("leaked_tool_call")
41+
if recovery in {"rejected", "truncated"}:
42+
transformed = "{}"
43+
if not config.get("suppress_xml", True) and "<" in response:
44+
transformed = format_fallback(
45+
response, suppress_xml=False,
46+
split_thoughts=config.get("split_thoughts", True),
47+
)
48+
result_data["tool_call_retry_response"] = transformed
49+
log_item = params.get("log_item_generating")
50+
if log_item is not None:
51+
update_log_item(
52+
self.agent, log_item, transformed,
53+
update_log=config.get("update_log", False), raw_response=response,
54+
)
55+
return # Core owns the retry; rejected calls never enter JSON repair.
4056
if recovery == "quoted":
4157
transformed = json.dumps(
4258
{"thoughts": [response]}, ensure_ascii=False, separators=(",", ":")

‎plugins/_context_doctor/helpers/context_doctor.py‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,21 @@ def transform_response(
127127
"""Repair model output, falling back to compact thoughts JSON for raw text."""
128128
value = _select_value(response)
129129
if value is None:
130-
if suppress_xml and "<" in response and ">" in response:
131-
return "{}"
132-
value = {"thoughts": [response]}
130+
return format_fallback(
131+
response, suppress_xml=suppress_xml, split_thoughts=split_thoughts
132+
)
133+
if split_thoughts:
134+
_split_thoughts(value)
135+
return _compact_json(value)
136+
137+
138+
def format_fallback(
139+
response: str, *, suppress_xml: bool, split_thoughts: bool = True
140+
) -> str:
141+
"""Format non-executable text without salvaging a tool call from it."""
142+
if suppress_xml and "<" in response and ">" in response:
143+
return "{}"
144+
value = {"thoughts": [response]}
133145
if split_thoughts:
134146
_split_thoughts(value)
135147
return _compact_json(value)

‎plugins/_context_doctor/webui/config.html‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
<div class="field-label">
3131
<div class="field-title">Suppress XML output</div>
3232
<div class="field-description">
33-
Replace malformed XML-like output without JSON object in cases where model uses native XML tool calls.
33+
Remove unusable XML-like output from model history. When off, keep it as non-executable thoughts.
3434
</div>
3535
</div>
3636
<div class="field-control">

0 commit comments

Comments
 (0)