fix: stream internal model calls with idle timeouts (#5730)
* fix: stream internal model calls with idle timeouts * fix(providers): reject streams without terminal events
This commit is contained in:
1 parent
aeb7b207d7
commit
73410e0217
60 files changed
+815
-795
No files matched your search
+4
-60
@@ -4,7 +4,6 @@ from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import inspect
|
||||
import os
|
||||
import time
|
||||
from collections.abc import Awaitable, Callable, Iterable
|
||||
from copy import deepcopy
|
||||
@@ -109,7 +108,6 @@ class AgentRunSpec:
|
||||
consolidate_provider_compaction: ProviderCompactionConsolidator | None = None
|
||||
injection_callback: InjectionCallback | None = None
|
||||
terminal_injection_callback: InjectionCallback | None = None
|
||||
llm_timeout_s: float | None = None
|
||||
continuation_callback: ContinuationCallback | None = None
|
||||
finalize_on_max_iterations: bool = True
|
||||
provider_state: ProviderConversationState | None = None
|
||||
@@ -873,7 +871,6 @@ class AgentRunner:
|
||||
malformed_retry: bool = False,
|
||||
transcript: list[dict[str, Any]] | None,
|
||||
) -> tuple[LLMResponse, LLMUsage]:
|
||||
timeout_s = self._resolve_llm_timeout_s(spec)
|
||||
tool_definitions = spec.tools.get_definitions()
|
||||
messages, provider_context = await self.context_governor.prepare_request(
|
||||
request_state,
|
||||
@@ -991,44 +988,19 @@ class AgentRunner:
|
||||
on_stream_recover=_stream_recover,
|
||||
)
|
||||
else:
|
||||
coro = spec.runtime.provider.chat_with_retry(
|
||||
coro = spec.runtime.provider.chat_stream_with_retry(
|
||||
**kwargs,
|
||||
provider_context=provider_context,
|
||||
)
|
||||
|
||||
# Streaming requests also have provider-level idle timeouts
|
||||
# (NANOBOT_STREAM_IDLE_TIMEOUT_S), but a stream that keeps producing
|
||||
# very slow deltas can still run forever. Use a more generous wall-clock
|
||||
# timeout for streaming while preserving NANOBOT_LLM_TIMEOUT_S=0 as an
|
||||
# opt-out for all LLM wall-clock timeouts.
|
||||
outer_timeout_s = (
|
||||
max(300.0, timeout_s * 2)
|
||||
if wants_streaming and timeout_s is not None
|
||||
else timeout_s
|
||||
)
|
||||
# Providers bound the wait for each stream event, including reasoning and tool deltas.
|
||||
request_started_at = time.perf_counter()
|
||||
try:
|
||||
response = (
|
||||
await coro if outer_timeout_s is None
|
||||
else await asyncio.wait_for(coro, timeout=outer_timeout_s)
|
||||
)
|
||||
response = await coro
|
||||
except asyncio.CancelledError:
|
||||
_pause_generation()
|
||||
await _close_native_reasoning()
|
||||
raise
|
||||
except asyncio.TimeoutError:
|
||||
if outer_timeout_s is None:
|
||||
response = LLMResponse(
|
||||
content="Error calling LLM: stream stalled",
|
||||
finish_reason="error",
|
||||
error_kind="timeout",
|
||||
)
|
||||
else:
|
||||
response = LLMResponse(
|
||||
content=f"Error calling LLM: timed out after {outer_timeout_s:g}s",
|
||||
finish_reason="error",
|
||||
error_kind="timeout",
|
||||
)
|
||||
_pause_generation()
|
||||
await _close_native_reasoning()
|
||||
if first_output_at is not None:
|
||||
@@ -1261,23 +1233,10 @@ class AgentRunner:
|
||||
messages,
|
||||
tools=None,
|
||||
)
|
||||
coro = spec.runtime.provider.chat_with_retry(
|
||||
response = await spec.runtime.provider.chat_stream_with_retry(
|
||||
**kwargs,
|
||||
provider_context=provider_context,
|
||||
)
|
||||
timeout_s = self._resolve_llm_timeout_s(spec)
|
||||
try:
|
||||
response = (
|
||||
await coro
|
||||
if timeout_s is None
|
||||
else await asyncio.wait_for(coro, timeout=timeout_s)
|
||||
)
|
||||
except asyncio.TimeoutError:
|
||||
response = LLMResponse(
|
||||
content=f"Error calling LLM: timed out after {timeout_s:g}s",
|
||||
finish_reason="error",
|
||||
error_kind="timeout",
|
||||
)
|
||||
await self.context_governor.summarize_provider_compaction(
|
||||
request_state,
|
||||
response,
|
||||
@@ -1286,21 +1245,6 @@ class AgentRunner:
|
||||
request_state.provider_compaction_applied |= response.provider_compaction_applied
|
||||
return response
|
||||
|
||||
@staticmethod
|
||||
def _resolve_llm_timeout_s(spec: AgentRunSpec) -> float | None:
|
||||
"""Resolve the wall-clock limit shared by every model request path."""
|
||||
timeout_s = spec.llm_timeout_s
|
||||
if timeout_s is None:
|
||||
# Default to a finite timeout to avoid per-session lock starvation when an LLM
|
||||
# request hangs indefinitely (e.g. gateway/network stall).
|
||||
# Set NANOBOT_LLM_TIMEOUT_S=0 to disable.
|
||||
raw = os.environ.get("NANOBOT_LLM_TIMEOUT_S", "300").strip()
|
||||
try:
|
||||
timeout_s = float(raw)
|
||||
except (TypeError, ValueError):
|
||||
timeout_s = 300.0
|
||||
return timeout_s if timeout_s > 0 else None
|
||||
|
||||
@staticmethod
|
||||
def _budget_exhausted_finalization_messages(
|
||||
messages: list[dict[str, Any]],
|
||||
|
||||
Reference in new issue
Block a user