Files
nanobot/nanobot/agent/runner.py
T

1383 lines
55 KiB
Python
Raw Normal View History

"""Shared execution loop for tool-using agents."""
from __future__ import annotations
import asyncio
import time
from collections.abc import Awaitable, Callable, Iterable
2026-06-04 10:16:29 +08:00
from copy import deepcopy
from dataclasses import dataclass, field, replace
from pathlib import Path
from typing import Any, cast
from loguru import logger
from nanobot.agent.context import TranscriptInput
from nanobot.agent.context_governance import (
ContextCompactionState,
ContextGovernanceConfig,
ContextGovernor,
HistoryConsolidator,
ModelRequestState,
ProviderCompactionConsolidator,
TranscriptBuilder,
)
2026-06-03 18:45:13 +08:00
from nanobot.agent.hook import AgentHook, AgentHookContext, AgentRunHookContext
from nanobot.agent.tools.context import tool_log_content_allowed
from nanobot.agent.tools.execution import execute_tool_calls
from nanobot.agent.tools.registry import ToolRegistry
from nanobot.events import NO_EVENTS, EventSink
from nanobot.llm_usage.context import (
LLMUsageSource,
bind_llm_usage_source,
reset_llm_usage_source,
source_from_session_key,
)
from nanobot.providers.base import (
LLMProvider,
LLMResponse,
LLMUsage,
ProviderCallContext,
ProviderConversationState,
)
from nanobot.providers.conversation_state import ProviderConversationStateController
from nanobot.session.history_visibility import is_hidden_history_message
from nanobot.session.summary import SessionSummaryCheckpoint
from nanobot.utils.helpers import (
build_assistant_message,
estimate_message_tokens,
estimate_prompt_tokens_chain,
extract_reasoning,
strip_reasoning_tags,
)
from nanobot.utils.llm_runtime import LLMRuntime
2026-04-25 12:34:29 +00:00
from nanobot.utils.prompt_templates import render_template
from nanobot.utils.runtime import (
EMPTY_FINAL_RESPONSE_MESSAGE,
build_budget_exhausted_finalization_message,
build_finalization_retry_message,
build_length_recovery_message,
is_blank_text,
)
ContinuationCallback = Callable[[], str | None]
CheckpointCallback = Callable[[dict[str, Any]], Awaitable[None]]
InjectionCallback = Callable[[], Awaitable[Iterable[Any] | None]]
_DEFAULT_ERROR_MESSAGE = "Sorry, I encountered an error calling the AI model."
_ARREARAGE_ERROR_MESSAGE = (
"The AI provider rejected the request because the API key is out of quota or the "
"account is in arrears. Please top up / check the billing status of your API key and try again."
)
_PERSISTED_MODEL_ERROR_PLACEHOLDER = "[Assistant reply unavailable due to model error.]"
_MAX_EMPTY_RETRIES = 2
_MAX_LENGTH_RECOVERIES = 3
def _restore_outer_whitespace(content: str, original: str | None) -> str:
"""Restore boundary whitespace stripped while cleaning one recovered segment."""
if not original:
return content
leading_size = len(original) - len(original.lstrip())
trailing_size = len(original) - len(original.rstrip())
leading = original[:leading_size]
trailing = original[-trailing_size:] if trailing_size else ""
return f"{leading}{content}{trailing}"
@dataclass(slots=True)
class AgentRunSpec:
"""Configuration for a single agent execution."""
initial_messages: list[dict[str, Any]] | None
tools: ToolRegistry
runtime: LLMRuntime
max_iterations: int
max_tool_result_chars: int
transcript_input: TranscriptInput | None = None
transcript_builder: TranscriptBuilder | None = None
hook: AgentHook | None = None
error_message: str | None = _DEFAULT_ERROR_MESSAGE
max_iterations_message: str | None = None
concurrent_tools: bool = False
workspace: Path | None = None
session_key: str | None = None
provider_retry_mode: str = "standard"
checkpoint_callback: CheckpointCallback | None = None
consolidate_history: HistoryConsolidator | None = None
consolidate_provider_compaction: ProviderCompactionConsolidator | None = None
injection_callback: InjectionCallback | None = None
terminal_injection_callback: InjectionCallback | None = None
continuation_callback: ContinuationCallback | None = None
finalize_on_max_iterations: bool = True
provider_state: ProviderConversationState | None = None
llm_usage_source: LLMUsageSource | None = None
events: EventSink = NO_EVENTS
@dataclass(slots=True)
class AgentRunResult:
"""Outcome of a shared agent execution."""
final_content: str | None
messages: list[dict[str, Any]]
tools_used: list[str] = field(default_factory=list)
usage: LLMUsage | None = None
# One entry per runner-visible model round. Recovery dispatches needed to
# produce that round's response are folded into the same usage value.
round_usages: list[LLMUsage] = field(default_factory=list)
stop_reason: str = "completed"
error: str | None = None
failure_error_kind: str | None = None
tool_events: list[dict[str, str]] = field(default_factory=list)
had_injections: bool = False
# Terminal tail to emit when the preceding final-content prefix was already streamed.
pending_stream_content: str | None = None
provider_state: ProviderConversationState | None = field(default=None, repr=False)
summary_checkpoint: SessionSummaryCheckpoint | None = field(default=None, repr=False)
provider_compaction_applied: bool = field(default=False, repr=False)
class AgentRunner:
"""Run a tool-capable LLM loop without product-layer concerns."""
def __init__(self) -> None:
self.context_governor = ContextGovernor()
@staticmethod
def _append_injected_messages(
messages: list[dict[str, Any]],
injections: list[dict[str, Any]],
) -> None:
"""Append injected messages without rewriting the raw transcript."""
messages.extend(injections)
async def _try_drain_injections(
self,
spec: AgentRunSpec,
messages: list[dict[str, Any]],
assistant_message: dict[str, Any] | None,
injection_cycles: int,
*,
conversation_state: ProviderConversationStateController | None = None,
phase: str = "after error",
iteration: int | None = None,
allow_continuation: bool = False,
wait_at_terminal: bool = False,
drain_callback: bool = True,
) -> tuple[bool, int]:
"""Append one pending-input snapshot and return whether execution continues."""
injections = await self._drain_injections(spec) if drain_callback else []
real_injection = bool(injections)
if not injections and allow_continuation and assistant_message is not None:
continuation = self._build_continuation_message(spec)
if continuation is not None:
injections = [continuation]
if (
not injections
and wait_at_terminal
and drain_callback
):
injections = await self._drain_injections(spec, terminal=True)
real_injection = bool(injections)
if not injections:
return False, injection_cycles
if real_injection:
injection_cycles += 1
if assistant_message is not None:
messages.append(assistant_message)
if iteration is not None:
checkpoint: dict[str, Any] = {
"phase": "final_response",
"iteration": iteration,
"model": spec.runtime.model,
"assistant_message": assistant_message,
"completed_tool_results": [],
"pending_tool_calls": [],
}
if conversation_state is not None:
checkpoint["provider_state"] = conversation_state.checkpoint(
messages
)
await self._emit_checkpoint(
spec,
checkpoint,
)
self._append_injected_messages(messages, injections)
if real_injection:
preview = "[content hidden]"
if tool_log_content_allowed():
preview = "\n\n".join(
message["content"] for message in injections
if isinstance(message.get("content"), str)
and not is_hidden_history_message(message)
)
preview = preview[:80] + "..." if len(preview) > 80 else preview
logger.info(
"Injected {} follow-up message(s) {} (snapshot {}): {}",
len(injections), phase, injection_cycles, preview,
)
else:
logger.info("Injected caller-requested continuation {}", phase)
return True, injection_cycles
@staticmethod
def _build_continuation_message(spec: AgentRunSpec) -> dict[str, str] | None:
callback = spec.continuation_callback
if callback is None:
return None
try:
content = callback()
except Exception:
logger.opt(exception=tool_log_content_allowed()).error("continuation_callback failed")
return None
if content is None or not content.strip():
return None
return {"role": "user", "content": content}
async def _drain_injections(
self,
spec: AgentRunSpec,
*,
terminal: bool = False,
) -> list[dict[str, Any]]:
"""Drain one pending-input snapshot via the injection callback."""
callback = (
spec.terminal_injection_callback
if terminal
else spec.injection_callback
)
if callback is None:
return []
try:
items = await callback()
except Exception:
logger.opt(exception=tool_log_content_allowed()).error("injection_callback failed")
return []
if not items:
return []
injected_messages: list[dict[str, Any]] = []
for item in items:
if item is None:
continue
if isinstance(item, dict):
message_item = cast(dict[str, Any], item)
if message_item.get("role") == "user" and "content" in message_item:
if self._has_injection_content(message_item.get("content")):
injected_messages.append(message_item)
continue
content = getattr(item, "content") if hasattr(item, "content") else str(item)
if self._has_injection_content(content):
injected_messages.append({"role": "user", "content": content})
return injected_messages
@staticmethod
def _has_injection_content(content: Any) -> bool:
if content is None:
return False
if isinstance(content, str):
return bool(content.strip())
if isinstance(content, list):
return bool(cast(list[Any], content))
return True
async def run(self, spec: AgentRunSpec) -> AgentRunResult:
hook = spec.hook or AgentHook()
messages, compaction = self._initial_transcript_and_compaction(spec)
2026-06-04 10:16:29 +08:00
context = AgentRunHookContext(messages=deepcopy(messages))
llm_usage_source_token = bind_llm_usage_source(
spec.llm_usage_source or source_from_session_key(spec.session_key)
)
2026-06-03 18:45:13 +08:00
try:
await hook.before_run(context)
result = await self._run_core(spec, hook, messages, compaction)
2026-06-03 21:11:20 +08:00
except asyncio.CancelledError as exc:
2026-06-04 10:16:29 +08:00
context.messages = deepcopy(messages)
2026-06-03 21:11:20 +08:00
context.stop_reason = "cancelled"
context.error = None
context.exception = exc
raise
except Exception as exc:
2026-06-04 10:16:29 +08:00
context.messages = deepcopy(messages)
2026-06-03 18:45:13 +08:00
context.stop_reason = "error"
context.error = f"Error: {type(exc).__name__}: {exc}"
context.exception = exc
await hook.on_error(context)
raise
else:
2026-06-04 10:16:29 +08:00
context.messages = deepcopy(result.messages)
2026-06-03 18:45:13 +08:00
context.final_content = result.final_content
context.tools_used = list(result.tools_used)
context.usage = result.usage
2026-06-03 18:45:13 +08:00
context.stop_reason = result.stop_reason
context.error = result.error
2026-06-04 10:16:29 +08:00
context.tool_events = deepcopy(result.tool_events)
2026-06-03 18:45:13 +08:00
context.had_injections = result.had_injections
context.exception = None
if context.error is not None:
await hook.on_error(context)
await hook.after_run(context)
return result
finally:
try:
context.messages = deepcopy(messages)
if context.exception is None:
2026-06-04 10:16:29 +08:00
await hook.on_finally(context)
else:
try:
await hook.on_finally(context)
except Exception:
logger.opt(exception=tool_log_content_allowed()).error(
"AgentHook.on_finally error after {}",
context.stop_reason or "run exception",
)
finally:
reset_llm_usage_source(llm_usage_source_token)
2026-06-03 18:45:13 +08:00
@staticmethod
def _initial_transcript_and_compaction(
spec: AgentRunSpec,
) -> tuple[list[dict[str, Any]], ContextCompactionState]:
"""Build the initial transcript and its compaction state."""
consolidate_history = spec.consolidate_history
if consolidate_history is None:
raise ValueError("consolidate_history is required")
transcript_input = spec.transcript_input
if transcript_input is not None:
if spec.initial_messages is not None:
raise ValueError("provide either transcript_input or initial_messages, not both")
transcript_builder = spec.transcript_builder
if transcript_builder is None:
raise ValueError("transcript_builder is required with transcript_input")
return ContextCompactionState.from_transcript(
transcript_input,
transcript_builder,
consolidate_history,
spec.consolidate_provider_compaction,
)
if spec.initial_messages is None:
raise ValueError("initial_messages is required without transcript_input")
messages = list(spec.initial_messages)
return messages, ContextCompactionState.from_messages(
messages,
consolidate_history,
spec.consolidate_provider_compaction,
)
2026-06-03 18:45:13 +08:00
async def _run_core(
self,
spec: AgentRunSpec,
hook: AgentHook,
messages: list[dict[str, Any]],
compaction: ContextCompactionState,
2026-06-03 18:45:13 +08:00
) -> AgentRunResult:
final_content: str | None = None
tools_used: list[str] = []
usage: LLMUsage | None = None
round_usages: list[LLMUsage] = []
error: str | None = None
failure_error_kind: str | None = None
stop_reason = "completed"
tool_events: list[dict[str, str]] = []
external_lookup_counts: dict[str, int] = {}
# Per-turn throttle for repeated attempts against the same outside target.
workspace_violation_counts: dict[str, int] = {}
empty_content_retries = 0
# Segments from one uninterrupted length-recovery chain. Tool work or
# injected user input starts a new logical answer and clears the chain.
length_recovery_parts: list[str] = []
pending_length_segment: tuple[AgentHookContext, str] | None = None
had_injections = False
injection_cycles = 0
pending_stream_content: str | None = None
conversation_state = ProviderConversationStateController(
provider=spec.runtime.provider,
model=spec.runtime.model,
messages=messages,
state=spec.provider_state,
session_id=spec.session_key,
)
governance_config = ContextGovernanceConfig(
provider=spec.runtime.provider,
model=spec.runtime.model,
tools=spec.tools,
workspace=spec.workspace,
session_key=spec.session_key,
max_tool_result_chars=spec.max_tool_result_chars,
context_window_tokens=spec.runtime.context_window_tokens,
max_tokens=spec.runtime.generation.max_tokens,
)
request_state = ModelRequestState(
config=governance_config,
conversation=conversation_state,
compaction=compaction,
events=spec.events,
)
async def end_length_segment(*, interrupted: bool) -> None:
nonlocal pending_length_segment
if pending_length_segment is None:
return
segment_context, segment_content = pending_length_segment
pending_length_segment = None
if interrupted:
length_recovery_parts.clear()
else:
messages.append(build_length_recovery_message(segment_content))
if hook.wants_streaming():
segment_context.stream_continues_current_message = not interrupted
await hook.on_stream_end(segment_context, resuming=True)
for iteration in range(spec.max_iterations):
# The session inbox cuts a finite snapshot before every model call.
# This includes follow-ups that arrived before the first request and
# messages received while the previous request or tools were running.
drained_before_request, injection_cycles = await self._try_drain_injections(
spec,
messages,
None,
injection_cycles,
phase="before model call",
)
if drained_before_request:
had_injections = True
await end_length_segment(interrupted=drained_before_request)
context = AgentHookContext(
iteration=iteration,
messages=messages,
session_key=spec.session_key,
)
await hook.before_iteration(context)
request_message_count = len(messages)
request_messages = request_state.compaction.request_messages(messages)
response, raw_usage = await self._request_model(
spec,
request_messages,
hook,
context,
request_state=request_state,
transcript=messages,
)
assert request_state.messages is not None
messages_for_model = request_state.messages
conversation_state.observe_response(response, messages)
request_state.compaction.accept_request(
messages_for_model,
raw_boundary=request_message_count,
)
context.response = response
context.tool_calls = list(response.tool_calls)
original_content = response.content
reasoning_text, cleaned_content = extract_reasoning(
response.reasoning_content,
response.thinking_blocks,
response.content,
)
response.content = cleaned_content
round_usages.append(raw_usage)
context.usage = raw_usage
usage = self._merge_usage(usage, raw_usage)
if reasoning_text and not context.streamed_reasoning:
await hook.emit_reasoning(reasoning_text)
await hook.emit_reasoning_end()
context.streamed_reasoning = True
if response.should_execute_tools:
2026-05-12 18:36:03 +08:00
context.tool_calls = list(response.tool_calls)
if hook.wants_streaming():
await hook.on_stream_end(context, resuming=True)
assistant_message = build_assistant_message(
response.content or "",
2026-05-12 18:36:03 +08:00
tool_calls=[tc.to_openai_tool_call() for tc in response.tool_calls],
reasoning_content=response.reasoning_content,
thinking_blocks=response.thinking_blocks,
)
assistant_message = conversation_state.project_response_message(
assistant_message,
response,
)
messages.append(assistant_message)
await self._emit_checkpoint(
spec,
{
"phase": "awaiting_tools",
"iteration": iteration,
"model": spec.runtime.model,
"assistant_message": assistant_message,
"completed_tool_results": [],
2026-05-12 18:36:03 +08:00
"pending_tool_calls": [tc.to_openai_tool_call() for tc in response.tool_calls],
},
)
await hook.before_execute_tools(context)
results, new_events = await execute_tool_calls(
spec.tools,
2026-05-12 18:36:03 +08:00
response.tool_calls,
concurrent=spec.concurrent_tools,
external_lookup_counts=external_lookup_counts,
workspace_violation_counts=workspace_violation_counts,
hook=hook,
context=context,
model_messages=messages_for_model,
compacted_tool_results=request_state.compacted_tool_results,
)
tool_events.extend(new_events)
tools_used.extend(
tool_call.name
for tool_call, event in zip(response.tool_calls, new_events)
if event.get("status") == "ok"
)
context.tool_results = list(results)
context.tool_events = list(new_events)
completed_tool_results: list[dict[str, Any]] = []
2026-05-12 18:36:03 +08:00
for tool_call, result in zip(response.tool_calls, results):
tool_message = {
"role": "tool",
"tool_call_id": tool_call.id,
"name": tool_call.name,
"content": self.context_governor.normalize_tool_result(
governance_config,
tool_call.id,
tool_call.name,
result,
),
}
messages.append(tool_message)
completed_tool_results.append(tool_message)
checkpoint_model_messages = (
self.context_governor.prepare_messages_for_model(
governance_config,
messages,
)
if response.provider_state is not None
else None
)
await self._emit_checkpoint(
spec,
{
"phase": "tools_completed",
"iteration": iteration,
"model": spec.runtime.model,
"assistant_message": assistant_message,
"completed_tool_results": completed_tool_results,
"pending_tool_calls": [],
"provider_state": conversation_state.checkpoint(
messages,
model_messages=checkpoint_model_messages,
),
},
)
empty_content_retries = 0
length_recovery_parts.clear()
await hook.after_iteration(context)
continue
if response.has_tool_calls:
logger.warning(
"Ignoring tool calls under finish_reason='{}' for {}",
response.finish_reason,
spec.session_key or "default",
)
clean = hook.finalize_content(context, response.content)
if (
response.finish_reason
not in {"error", "length", "refusal", "content_filter"}
and is_blank_text(clean)
):
empty_content_retries += 1
if empty_content_retries < _MAX_EMPTY_RETRIES:
logger.warning(
"Empty response on turn {} for {} ({}/{}); retrying",
iteration,
spec.session_key or "default",
empty_content_retries,
_MAX_EMPTY_RETRIES,
)
if hook.wants_streaming():
await hook.on_stream_end(context, resuming=False)
await hook.after_iteration(context)
continue
logger.warning(
"Empty response on turn {} for {} after {} retries; attempting finalization",
iteration,
spec.session_key or "default",
empty_content_retries,
)
if hook.wants_streaming():
await hook.on_stream_end(context, resuming=False)
response = await self._request_finalization_retry(
spec,
messages_for_model,
request_state=request_state,
transcript=messages,
)
retry_usage = self._record_request_usage(spec, request_state, response)
round_usages.append(retry_usage)
usage = self._merge_usage(usage, retry_usage)
raw_usage = self._merge_usage(raw_usage, retry_usage)
context.response = response
context.usage = raw_usage
context.tool_calls = list(response.tool_calls)
original_content = response.content
clean = hook.finalize_content(context, response.content)
if response.finish_reason == "length":
if len(length_recovery_parts) < _MAX_LENGTH_RECOVERIES:
length_recovery_parts.append(
_restore_outer_whitespace(clean or "", original_content)
)
logger.info(
"Output truncated on turn {} for {} ({}/{}); continuing",
iteration,
spec.session_key or "default",
len(length_recovery_parts),
_MAX_LENGTH_RECOVERIES,
)
messages.append(conversation_state.project_response_message(
build_assistant_message(
clean,
reasoning_content=response.reasoning_content,
thinking_blocks=response.thinking_blocks,
),
response,
))
# The next input snapshot decides whether to continue this
# answer or close its stream before answering a new question.
pending_length_segment = (context, clean or "")
await hook.after_iteration(context)
continue
# Some streaming providers recover with a complete response but no
# content deltas. When an earlier length segment is already visible,
# emit this terminal segment into the same stream; otherwise the
# regular full response would duplicate the visible prefix.
if (
length_recovery_parts
and hook.wants_streaming()
and not context.streamed_content
and response.finish_reason != "error"
and not is_blank_text(clean)
):
await hook.on_stream(
context,
_restore_outer_whitespace(clean or "", original_content),
)
context.streamed_content = True
assistant_message: dict[str, Any] | None = None
if response.finish_reason != "error" and not is_blank_text(clean):
assistant_message = build_assistant_message(
clean,
reasoning_content=response.reasoning_content,
thinking_blocks=response.thinking_blocks,
)
assistant_message = conversation_state.project_response_message(
assistant_message,
response,
)
# Check for mid-turn injections BEFORE signaling stream end.
# If injections are found we keep the stream alive (resuming=True)
# so streaming channels don't prematurely finalize the card.
can_make_followup_request = (
iteration + 1 < spec.max_iterations
or spec.finalize_on_max_iterations
)
should_continue, injection_cycles = await self._try_drain_injections(
spec, messages, assistant_message, injection_cycles,
conversation_state=conversation_state,
phase="after final response",
iteration=iteration,
allow_continuation=(
response.finish_reason not in {"refusal", "content_filter"}
),
wait_at_terminal=(
assistant_message is not None
and response.finish_reason
not in {"error", "length", "refusal", "content_filter"}
),
drain_callback=can_make_followup_request,
)
if should_continue:
had_injections = True
if hook.wants_streaming():
await hook.on_stream_end(context, resuming=should_continue)
if should_continue:
length_recovery_parts.clear()
await hook.after_iteration(context)
continue
if response.finish_reason == "error":
if LLMProvider.is_arrearage_response(response):
final_content = _ARREARAGE_ERROR_MESSAGE
else:
final_content = clean or spec.error_message or _DEFAULT_ERROR_MESSAGE
stop_reason = "error"
error = final_content
self._append_model_error_placeholder(messages)
context.final_content = final_content
context.error = error
context.stop_reason = stop_reason
await hook.after_iteration(context)
should_continue, injection_cycles = await self._try_drain_injections(
spec, messages, None, injection_cycles,
phase="after LLM error",
drain_callback=can_make_followup_request,
)
if should_continue:
had_injections = True
length_recovery_parts.clear()
continue
failure_error_kind = LLMProvider.public_error_kind(response)
break
if is_blank_text(clean):
final_content = EMPTY_FINAL_RESPONSE_MESSAGE
stop_reason = "empty_final_response"
error = final_content
self._append_final_message(messages, final_content)
context.final_content = final_content
context.error = error
context.stop_reason = stop_reason
await hook.after_iteration(context)
should_continue, injection_cycles = await self._try_drain_injections(
spec, messages, None, injection_cycles,
phase="after empty response",
drain_callback=can_make_followup_request,
)
if should_continue:
had_injections = True
length_recovery_parts.clear()
continue
break
messages.append(
assistant_message
or conversation_state.project_response_message(
build_assistant_message(
clean,
reasoning_content=response.reasoning_content,
thinking_blocks=response.thinking_blocks,
),
response,
)
)
await self._emit_checkpoint(
spec,
{
"phase": "final_response",
"iteration": iteration,
"model": spec.runtime.model,
"assistant_message": messages[-1],
"completed_tool_results": [],
"pending_tool_calls": [],
"provider_state": conversation_state.checkpoint(messages),
},
)
if length_recovery_parts:
final_content = (
"".join(length_recovery_parts)
+ _restore_outer_whitespace(clean or "", original_content)
).strip()
else:
final_content = clean
context.final_content = final_content
context.stop_reason = stop_reason
await hook.after_iteration(context)
break
else:
stop_reason = "max_iterations"
terminal_content = None
if spec.finalize_on_max_iterations:
# The no-tools finalization is a real model boundary, so include
# exactly the inputs waiting before that request. Without this
# request, leave them in the session inbox for its worker.
drained_after_max_iterations, injection_cycles = (
await self._try_drain_injections(
spec,
messages,
None,
injection_cycles,
phase="before max-iterations finalization",
)
)
if drained_after_max_iterations:
had_injections = True
await end_length_segment(interrupted=drained_after_max_iterations)
terminal_content, usage = await self._try_finalize_after_max_iterations(
spec,
hook,
messages,
usage,
request_state=request_state,
round_usages=round_usages,
)
else:
await end_length_segment(interrupted=False)
if terminal_content is None:
terminal_content = self._max_iterations_fallback(spec)
if length_recovery_parts:
terminal_tail = f"\n\n{terminal_content.lstrip()}"
final_content = (
"".join(length_recovery_parts).rstrip() + terminal_tail
).strip()
pending_stream_content = terminal_tail
else:
final_content = terminal_content
self._append_final_message(messages, terminal_content)
return AgentRunResult(
final_content=final_content,
messages=messages,
tools_used=tools_used,
usage=usage,
round_usages=round_usages,
stop_reason=stop_reason,
error=error,
failure_error_kind=failure_error_kind,
tool_events=tool_events,
had_injections=had_injections,
pending_stream_content=pending_stream_content,
provider_state=conversation_state.finish(messages),
summary_checkpoint=request_state.compaction.summary_checkpoint,
provider_compaction_applied=request_state.provider_compaction_applied,
)
def _build_request_kwargs(
self,
spec: AgentRunSpec,
messages: list[dict[str, Any]],
*,
tools: list[dict[str, Any]] | None,
) -> dict[str, Any]:
kwargs: dict[str, Any] = {
"messages": messages,
"tools": tools,
"model": spec.runtime.model,
"retry_mode": spec.provider_retry_mode,
}
generation = spec.runtime.generation
kwargs["temperature"] = generation.temperature
kwargs["max_tokens"] = generation.max_tokens
kwargs["reasoning_effort"] = generation.reasoning_effort
return kwargs
async def _request_model(
self,
spec: AgentRunSpec,
messages: list[dict[str, Any]],
hook: AgentHook,
context: AgentHookContext,
*,
request_state: ModelRequestState,
malformed_retry: bool = False,
transcript: list[dict[str, Any]] | None,
) -> tuple[LLMResponse, LLMUsage]:
tool_definitions = spec.tools.get_definitions()
messages, provider_context = await self.context_governor.prepare_request(
request_state,
messages,
tool_definitions=tool_definitions,
transcript=transcript,
)
kwargs = self._build_request_kwargs(
spec,
messages,
tools=tool_definitions,
)
wants_streaming = hook.wants_streaming()
provider_context = replace(
provider_context or ProviderCallContext(),
response_preset=spec.runtime.model_preset or "",
)
active_hosted_tools: dict[str, dict[str, Any]] = {}
native_reasoning_open = False
native_reasoning_close_task: asyncio.Task[None] | None = None
request_started_at = 0.0
first_output_at: float | None = None
generation_started_at: float | None = None
generation_elapsed_s = 0.0
def _generation_delta(delta: str) -> None:
nonlocal first_output_at, generation_started_at
if not delta:
return
now = time.perf_counter()
if first_output_at is None:
first_output_at = now
if generation_started_at is None:
generation_started_at = now
def _pause_generation() -> None:
nonlocal generation_elapsed_s, generation_started_at
if generation_started_at is None:
return
generation_elapsed_s += max(0.0, time.perf_counter() - generation_started_at)
generation_started_at = None
async def _close_native_reasoning() -> None:
nonlocal native_reasoning_open, native_reasoning_close_task
if native_reasoning_close_task is None:
if not native_reasoning_open:
return
native_reasoning_open = False
native_reasoning_close_task = asyncio.create_task(
hook.emit_reasoning_end()
)
close_task = native_reasoning_close_task
cancellation: asyncio.CancelledError | None = None
while not close_task.done():
try:
await asyncio.shield(close_task)
except asyncio.CancelledError as exc:
cancellation = cancellation or exc
try:
close_task.result()
finally:
if native_reasoning_close_task is close_task:
native_reasoning_close_task = None
if cancellation is not None:
raise cancellation
async def _provider_tool_event(event: dict[str, Any]) -> None:
if event.get("kind") != "hosted_tool":
return
await _close_native_reasoning()
await hook.on_provider_tool_event(context, event)
call_id = event.get("call_id")
if not call_id:
return
call_id = str(call_id)
if event.get("phase") == "start":
active_hosted_tools[call_id] = dict(event)
elif event.get("phase") in {"end", "error"}:
active_hosted_tools.pop(call_id, None)
if wants_streaming:
2026-06-24 15:20:31 +08:00
thinking_buf = ""
async def _stream(delta: str) -> None:
_generation_delta(delta)
if delta:
context.streamed_content = True
await _close_native_reasoning()
await hook.on_stream(context, delta)
async def _thinking(delta: str) -> None:
nonlocal native_reasoning_open, thinking_buf
if not delta:
return
_generation_delta(delta)
2026-06-24 15:20:31 +08:00
prev_clean = strip_reasoning_tags(thinking_buf)
thinking_buf += delta
new_clean = strip_reasoning_tags(thinking_buf)
incremental = new_clean[len(prev_clean):]
if incremental:
context.streamed_reasoning = True
native_reasoning_open = True
2026-06-24 15:20:31 +08:00
await hook.emit_reasoning(incremental)
async def _stream_recover() -> None:
_pause_generation()
await _close_native_reasoning()
await hook.on_stream_end(context, resuming=True)
coro = spec.runtime.provider.chat_stream_with_retry(
**kwargs,
provider_context=provider_context,
on_content_delta=_stream,
on_thinking_delta=_thinking,
on_tool_call_delta=_provider_tool_event,
on_stream_recover=_stream_recover,
)
else:
coro = spec.runtime.provider.chat_stream_with_retry(
**kwargs,
provider_context=provider_context,
)
# Providers bound the wait for each stream event, including reasoning and tool deltas.
request_started_at = time.perf_counter()
try:
response = await coro
except asyncio.CancelledError:
_pause_generation()
await _close_native_reasoning()
raise
_pause_generation()
await _close_native_reasoning()
if first_output_at is not None:
response.ttft_ms = max(0, round((first_output_at - request_started_at) * 1000))
if generation_elapsed_s > 0:
response.generation_ms = max(1, round(generation_elapsed_s * 1000))
await self.context_governor.summarize_provider_compaction(
request_state,
response,
current_request_boundary=(len(transcript) if transcript is not None else None),
)
request_state.provider_compaction_applied |= response.provider_compaction_applied
round_usage = self._record_request_usage(spec, request_state, response)
# chat_stream_with_retry may recover internally, so only fail unfinished
# hosted calls after the provider returns its final error response.
if response.finish_reason == "error":
for event in list(active_hosted_tools.values()):
await _provider_tool_event({
**event,
"phase": "error",
"result": None,
"error": response.content
or "Model request failed before the provider-hosted tool completed.",
})
dropped, all_dropped, original_finish_reason = (
self._drop_malformed_tool_calls(response)
)
if (
all_dropped
and original_finish_reason in ("tool_calls", "function_call")
and not malformed_retry
):
logger.warning(
"Retrying LLM request after all {} malformed tool call(s) were dropped",
dropped,
)
retry_messages = self._malformed_tool_call_retry_messages(
messages, response.content,
)
retry_response, retry_usage = await self._request_model(
spec, retry_messages, hook, context,
request_state=request_state,
malformed_retry=True,
transcript=None,
)
return retry_response, round_usage + retry_usage
if (
all_dropped
and original_finish_reason in ("tool_calls", "function_call")
and malformed_retry
):
logger.warning(
"Malformed tool calls persisted after retry; falling back to no-tools request",
)
fallback_messages = self._malformed_tool_call_retry_messages(
messages, response.content,
)
fallback_response = await self._request_no_tools(
spec,
fallback_messages,
request_state=request_state,
)
fallback_usage = self._record_request_usage(
spec,
request_state,
fallback_response,
)
return fallback_response, round_usage + fallback_usage
return response, round_usage
@staticmethod
def _drop_malformed_tool_calls(
response: LLMResponse,
) -> tuple[int, bool, str | None]:
"""Strip tool calls whose name is missing/non-string from the response.
Returns (dropped_count, all_dropped, original_finish_reason).
A degenerate call (name=None or "") cannot be executed, and if it were
persisted into the assistant message it would be replayed on every
subsequent turn, causing upstream validation errors
(``tool_use.name: Input should be a valid string``) that permanently
wedge the session. Dropping it here keeps it out of execution, the
assistant message, and the saved history in one place.
"""
calls = getattr(response, "tool_calls", None)
if not calls:
return (0, False, getattr(response, "finish_reason", None))
valid = [tc for tc in calls if tc.has_valid_name()]
if len(valid) == len(calls):
return (0, False, getattr(response, "finish_reason", None))
dropped = len(calls) - len(valid)
original_finish_reason = getattr(response, "finish_reason", None)
logger.warning(
"Dropped {} malformed tool call(s) with missing/non-string name "
"from LLM response (finish_reason={!r})",
dropped,
original_finish_reason,
)
response.tool_calls = valid
# The opaque candidate still contains every raw function_call item.
# Advancing it after dropping even one call would replay an unmatched
# call without a corresponding tool output on the next request.
response.provider_state = None
if not valid:
response.finish_reason = "stop"
return (dropped, not valid, original_finish_reason)
@staticmethod
def _malformed_tool_call_retry_messages(
messages: list[dict[str, Any]],
assistant_text: str | None,
) -> list[dict[str, Any]]:
retry_messages = list(messages)
note = (
"The previous model response attempted to call tools, but every tool call "
"was malformed: the tool_use blocks had missing or non-string tool names. "
"Do not answer with a promise to use tools. Either call the required tools again "
"using valid tool names from the provided tool list and JSON object inputs, or give "
"a final answer only if no tool is required."
)
if assistant_text:
note += (
f"\n\nPrevious assistant text before the malformed calls:\n"
f"{assistant_text}"
)
retry_messages.append({"role": "user", "content": note})
return retry_messages
async def _request_finalization_retry(
self,
spec: AgentRunSpec,
messages: list[dict[str, Any]],
*,
request_state: ModelRequestState,
transcript: list[dict[str, Any]],
) -> LLMResponse:
retry_messages = self._finalization_retry_messages(messages)
response = await self._request_no_tools(
spec,
retry_messages,
request_state=request_state,
transcript=transcript,
)
request_state.conversation.observe_response(
response,
transcript,
adopt_candidate_state=False,
)
return response
@staticmethod
def _finalization_retry_messages(messages: list[dict[str, Any]]) -> list[dict[str, Any]]:
retry_messages = list(messages)
retry_messages.append(build_finalization_retry_message())
return retry_messages
async def _try_finalize_after_max_iterations(
self,
spec: AgentRunSpec,
hook: AgentHook,
messages: list[dict[str, Any]],
usage: LLMUsage | None,
*,
request_state: ModelRequestState,
round_usages: list[LLMUsage],
) -> tuple[str | None, LLMUsage | None]:
compaction = request_state.compaction
request_messages = compaction.request_messages(messages)
retry_messages = self._budget_exhausted_finalization_messages(request_messages)
try:
response = await self._request_no_tools(
spec,
retry_messages,
request_state=request_state,
transcript=messages,
)
except Exception:
logger.opt(exception=tool_log_content_allowed()).error(
"Budget-exhausted finalization failed for {}; using fallback",
spec.session_key or "default",
)
return None, usage
raw_usage = self._record_request_usage(spec, request_state, response)
round_usages.append(raw_usage)
usage = self._merge_usage(usage, raw_usage)
if response.finish_reason == "error" or response.has_tool_calls:
logger.warning(
"Budget-exhausted finalization returned finish_reason='{}' "
"with {} tool call(s) for {}; using fallback",
response.finish_reason,
len(response.tool_calls),
spec.session_key or "default",
)
return None, usage
context = AgentHookContext(
iteration=spec.max_iterations,
messages=messages,
response=response,
usage=raw_usage,
session_key=spec.session_key,
)
clean = hook.finalize_content(context, response.content)
if is_blank_text(clean):
return None, usage
return clean, usage
async def _request_no_tools(
self,
spec: AgentRunSpec,
messages: list[dict[str, Any]],
*,
request_state: ModelRequestState,
transcript: list[dict[str, Any]] | None = None,
) -> LLMResponse:
messages, provider_context = await self.context_governor.prepare_request(
request_state,
messages,
tool_definitions=None,
transcript=transcript,
)
kwargs = self._build_request_kwargs(
spec,
messages,
tools=None,
)
response = await spec.runtime.provider.chat_stream_with_retry(
**kwargs,
provider_context=replace(
provider_context or ProviderCallContext(),
response_preset=spec.runtime.model_preset or "",
),
)
await self.context_governor.summarize_provider_compaction(
request_state,
response,
current_request_boundary=(len(transcript) if transcript is not None else None),
)
request_state.provider_compaction_applied |= response.provider_compaction_applied
return response
@staticmethod
def _budget_exhausted_finalization_messages(
messages: list[dict[str, Any]],
) -> list[dict[str, Any]]:
retry_messages = list(messages)
retry_messages.append(build_budget_exhausted_finalization_message())
return retry_messages
@staticmethod
def _max_iterations_fallback(spec: AgentRunSpec) -> str:
if spec.max_iterations_message:
return spec.max_iterations_message.format(
max_iterations=spec.max_iterations,
)
return render_template(
"agent/max_iterations_message.md",
strip=True,
max_iterations=spec.max_iterations,
)
def _usage_or_estimate(
self,
spec: AgentRunSpec,
messages: list[dict[str, Any]],
response: LLMResponse,
*,
tool_definitions: list[dict[str, Any]] | None,
) -> LLMUsage:
usage = response.usage
if response.finish_reason == "error":
if usage is None or usage.total_tokens == 0:
usage = LLMUsage.empty_request()
elif usage is None or usage.total_tokens == 0:
usage = self._estimate_response_usage(
spec,
messages,
response,
tool_definitions=tool_definitions,
)
return usage.with_timing(
generation_ms=response.generation_ms,
ttft_ms=response.ttft_ms,
)
def _record_request_usage(
self,
spec: AgentRunSpec,
state: ModelRequestState,
response: LLMResponse,
) -> LLMUsage:
assert state.messages is not None
state.usage = self._usage_or_estimate(
spec,
state.messages,
response,
tool_definitions=state.tool_definitions,
)
return state.usage
def _estimate_response_usage(
self,
spec: AgentRunSpec,
messages: list[dict[str, Any]],
response: LLMResponse,
*,
tool_definitions: list[dict[str, Any]] | None,
) -> LLMUsage:
prompt_tokens, _ = estimate_prompt_tokens_chain(
spec.runtime.provider,
spec.runtime.model,
messages,
tool_definitions,
)
assistant_message = build_assistant_message(
response.content or "",
tool_calls=[tc.to_openai_tool_call() for tc in response.tool_calls],
reasoning_content=response.reasoning_content,
thinking_blocks=response.thinking_blocks,
)
completion_tokens = estimate_message_tokens(assistant_message)
return LLMUsage.estimated(
input_tokens=max(0, prompt_tokens),
output_tokens=max(0, completion_tokens),
)
@staticmethod
def _merge_usage(
left: LLMUsage | None,
right: LLMUsage | None,
) -> LLMUsage | None:
if left is None:
return right
if right is None:
return left
return left + right
2026-08-22 19:41:37 +08:00
async def _emit_checkpoint(
self,
spec: AgentRunSpec,
payload: dict[str, Any],
) -> None:
callback = spec.checkpoint_callback
if callback is not None:
await callback(payload)
@staticmethod
def _append_final_message(messages: list[dict[str, Any]], content: str | None) -> None:
if not content:
return
if (
messages
and messages[-1].get("role") == "assistant"
and not messages[-1].get("tool_calls")
):
if messages[-1].get("content") == content:
return
messages[-1] = build_assistant_message(content)
return
messages.append(build_assistant_message(content))
@staticmethod
def _append_model_error_placeholder(messages: list[dict[str, Any]]) -> None:
if messages and messages[-1].get("role") == "assistant" and not messages[-1].get("tool_calls"):
return
messages.append(build_assistant_message(_PERSISTED_MODEL_ERROR_PLACEHOLDER))