agent-zero/extensions/python/message_loop_result/_05_truncated_response.py
Mohammad Hijjawi 2f8a06515e Surface output-limit truncation instead of misformat retries
Record the Chat Completions finish_reason on LLMResult and add a
message_loop_result extension that, when a turn stopped at the output
token limit (finish_reason "length"/"max_tokens") without a complete
tool request, retries with a warning naming truncation as the cause
rather than the generic misformat message. The warning counts toward
the existing unusable-response loop limit.

Context Doctor now leaves results already handled by an earlier
extension untouched, so truncated envelopes are not repaired and
dispatched with cut-off arguments.

Fixes #1836
2026-10-13 19:38:05 +02:00

41 lines
1.5 KiB
Python

"""Retry model turns cut off by the output token limit with a specific warning."""
from __future__ import annotations
from typing import Any
from helpers import extract_tools
from helpers.extension import Extension
from helpers.print_style import PrintStyle
class TruncatedResponse(Extension):
def execute(self, result_data: dict[str, Any] | None = None, **kwargs: Any) -> None:
if not self.agent or not isinstance(result_data, dict):
return
if result_data.get("skip_default_processing"):
return
llm_result = result_data.get("llm_result")
if not getattr(llm_result, "truncated", False):
return
if getattr(llm_result, "function_calls", None):
return
response = getattr(llm_result, "response", "")
if isinstance(response, str) and extract_tools.extract_tool_request(response):
return
# Name truncation as the cause instead of the generic misformat warning.
warning = self.agent.read_prompt("fw.msg_output_limit.md")
warning_message = self.agent.hist_add_warning(message=warning)
notice = self.agent.read_prompt(
"fw.msg_output_limit_response.md",
finish_reason=getattr(llm_result, "finish_reason", ""),
)
PrintStyle(font_color="orange", padding=True).print(notice)
self.agent.context.log.log(
type="warning",
content=f"{self.agent.agent_name}: {notice}",
id=warning_message.id,
)
result_data["skip_default_processing"] = True