DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.
Python ai-agentsai-tutorclawbotcli-tooldeepresearchinteractive-learninglearningmulti-agent-systemsrag
fix(chat): bound cold-start stream waits and avoid nested retries (#1421)
Bingxi Zhao (Frank)081c9e6d9833
diff --git a/deeptutor/agents/loop/agent_loop.py b/deeptutor/agents/loop/agent_loop.pyindex 9752f105f..1bd08b2a6 100644--- a/deeptutor/agents/loop/agent_loop.py+++ b/deeptutor/agents/loop/agent_loop.py@@ -107,6 +107,19 @@ MAX_REASONING_ONLY_RECOVERIES = 2# user-visible output. Once output is visible, replay is unsafe because it can# duplicate prose or tool calls._PROVIDER_RETRY_DELAYS = (0.5, 1.5)+_STREAM_IDLE_TIMEOUT_SECONDS = 90.0+++async def _bounded_stream_chunks(stream: Any):+ """#1421: a connected provider that never produces a chunk must settle."""+ iterator = stream.__aiter__()+ while True:+ try:+ yield await asyncio.wait_for(iterator.__anext__(), _STREAM_IDLE_TIMEOUT_SECONDS)+ except StopAsyncIteration:+ return+ except TimeoutError:+ raise LLMProviderTransportError("The model provider stream timed out.") from Nonedef _reasoning_budget_exhausted(result: "LLMCallResult", max_tokens: int) -> bool:@@ -1247,7 +1260,12 @@ class AgentLoop:try:sent_content = [message.get("content") for message in kwargs["messages"]]sent_had_images = has_image_parts(kwargs["messages"])- response_stream = await self._create_response_stream(kwargs, trace_meta, stage)+ # The loop owns retries. Bound both connection setup and idle+ # reads so cold-start failures cannot park a durable turn.+ response_stream = await asyncio.wait_for(+ self._create_response_stream(kwargs, trace_meta, stage),+ _STREAM_IDLE_TIMEOUT_SECONDS,+ )# Retain an actual provider fallback for later rounds. The# request-only image deduplication must never overwrite history.if sent_had_images and not has_image_parts(kwargs["messages"]):@@ -1262,7 +1280,7 @@ class AgentLoop:):if "content" in accepted and accepted["content"] is not sent:original["content"] = accepted["content"]- async for chunk in response_stream:+ async for chunk in _bounded_stream_chunks(response_stream):usage = getattr(chunk, "usage", None)if usage is not None:usage_seen = usagediff --git a/deeptutor/runtime/agentic/client.py b/deeptutor/runtime/agentic/client.pyindex e8bc29972..dd0c1ae7f 100644--- a/deeptutor/runtime/agentic/client.py+++ b/deeptutor/runtime/agentic/client.py@@ -124,7 +124,7 @@ def _build_openai_client(config: LLMClientConfig,*,disable_ssl_verify: bool,- sdk_max_retries: int | None = None,+ sdk_max_retries: int | None = 0,) -> Any:# A stale SSL_CERT_FILE (common with cloned conda envs) makes httpx's# create_ssl_context raise FileNotFoundError mid-__init__, aborting client@@ -531,6 +531,7 @@ class _ProviderOpenAIStream:async def close(self) -> None:if self._task and not self._task.done():self._task.cancel()+ await asyncio.gather(self._task, return_exceptions=True)def _raise_for_error_response(self, response: Any) -> None:"""Turn an error-shaped response back into the exception it describes.diff --git a/tests/agents/chat/test_agent_loop.py b/tests/agents/chat/test_agent_loop.pyindex 97b154e12..d55096d88 100644--- a/tests/agents/chat/test_agent_loop.py+++ b/tests/agents/chat/test_agent_loop.py@@ -2386,6 +2386,55 @@ async def test_midloop_transport_failure_retries_current_round(assert progress == ["The model provider connection was interrupted; retrying."][email protected]("phase", ["connect", "stream", "partial_stream"])+async def test_cold_start_stalled_provider_settles_with_bounded_retries(monkeypatch, phase):+ """#1421: a provider that never raises its own timeout cannot hold discovery forever."""+ attempts = []+ stopped = []++ async def stalled_stream():+ try:+ if phase == "partial_stream":+ yield _llm_chunk(content="Partial answer.")+ await asyncio.Event().wait()+ yield _llm_chunk(content="unreachable")+ finally:+ stopped.append(True)++ async def create(**_kwargs):+ attempts.append(True)+ if phase == "connect":+ await asyncio.Event().wait()+ return stalled_stream()++ client = SimpleNamespace(chat=SimpleNamespace(completions=SimpleNamespace(create=create)))+ pipeline = AgenticChatPipeline(language="en")+ pipeline.registry = _Registry()+ monkeypatch.setattr(agent_loop_mod, "_PROVIDER_RETRY_DELAYS", (0, 0))+ monkeypatch.setattr(agent_loop_mod, "_STREAM_IDLE_TIMEOUT_SECONDS", 0.01)+ monkeypatch.setattr(pipeline, "_compose_enabled_tools", lambda _: ["web_search"])+ monkeypatch.setattr(pipeline, "_build_openai_client", lambda: client)+ bus = StreamBus()+ events, consumer = await _collect_bus_events(bus)+ try:+ with pytest.raises(LLMProviderTransportError) as failure:+ await asyncio.wait_for(+ pipeline.run(UnifiedContext(session_id="cold", user_message="Hello"), bus), 1+ )+ assert failure.value.retryable+ assert len(attempts) == (1 if phase == "partial_stream" else 3)+ assert failure.value.partial_response is (phase == "partial_stream")+ if phase != "connect":+ assert len(stopped) == len(attempts)+ assert (+ sum(event.metadata.get("error_code") == "provider_transport" for event in events) >= 1+ )+ finally:+ await bus.close()+ await consumer++@pytest.mark.asyncioasync def test_first_round_transport_failure_retries_then_becomes_structured_error(monkeypatch: pytest.MonkeyPatch,diff --git a/tests/core/test_agentic_client_provider_kwargs.py b/tests/core/test_agentic_client_provider_kwargs.pyindex 8fba38ac5..ebdd88cb7 100644--- a/tests/core/test_agentic_client_provider_kwargs.py+++ b/tests/core/test_agentic_client_provider_kwargs.py@@ -226,6 +226,42 @@ async def test_provider_stream_raises_an_error_response_instead_of_streaming_it(assert excinfo.value.partial_response is False+async def test_closing_native_stream_awaits_provider_cancellation() -> None:+ import asyncio++ started = asyncio.Event()+ stopped = asyncio.Event()++ class Provider:+ async def chat_stream(self, **_kwargs):+ started.set()+ try:+ await asyncio.Event().wait()+ finally:+ stopped.set()++ stream = _stream_for(Provider())+ stream.__aiter__()+ await started.wait()+ await stream.close()+ assert stopped.is_set()+++def test_agentic_sdk_does_not_multiply_the_loop_retry_budget():+ # The real SDK constructor pins the absence of its hidden retry ladder.+ client = agentic_client._build_openai_client(+ LLMClientConfig(+ binding="custom", model="test", api_key="k", base_url="https://provider.example/v1"+ ),+ disable_ssl_verify=False,+ )+ assert client.max_retries == 0+ import asyncio++ asyncio.run(client.close())++@pytest.mark.asyncioasync def test_provider_stream_marks_an_error_after_output_as_partial() -> None:"""An error that interrupts a visible answer must not be replayed."""