Skip to content

The agent that grows with you

fix: stream retry no longer replays the old model on the switched-to provider

teknium166c9440826c2
diff --git a/agent/chat_completion_helpers.py b/agent/chat_completion_helpers.py
index 11701d317ae..284e1612a42 100644
--- a/agent/chat_completion_helpers.py
+++ b/agent/chat_completion_helpers.py
@@ -2542,9 +2542,32 @@ class _StreamingCall(StreamingWaitMonitor):
self.managed_stream_holder = {"stream": None}
# Per-attempt: single-writer token, request-local client, raw HTTP response (chat wire).
self._writer_token = self._attempt_request_client = self._attempt_stream_response = None
+ # The route ``api_kwargs`` was assembled for; a retry must not replay it on another one.
+ self._request_route = self._live_route()
# ── shared small helpers ────────────────────────────────────────────
+ def _live_route(self) -> tuple:
+ agent = self.agent
+ return tuple(str(getattr(agent, attr, "") or "") for attr in ("model", "provider", "base_url", "api_mode"))
+
+ def _route_switched_under_request(self) -> bool:
+ """True once ``/model`` (``switch_model``) re-pointed the agent while this request was in
+ flight. The captured payload names the OLD model and is shaped for the OLD provider, but
+ every (re)open builds its client from the LIVE agent, so a retry would send a foreign model
+ slug to the new base_url (404 + a rate-limit hold, #112121). The turn loop rebuilds the
+ request for the current route on its own next attempt, so hand the error back to it.
+ """
+ live = self._live_route()
+ if live == self._request_route:
+ return False
+ logger.warning(
+ "Stream retry skipped: model/provider switched mid-request (%s via %s -> %s via %s); "
+ "handing back to the turn loop to rebuild the request for the current route.",
+ self._request_route[0], self._request_route[1] or self._request_route[2], live[0], live[1] or live[2],
+ )
+ return True
+
@staticmethod
def _quiet(fn, *args) -> None:
"""Best-effort callback: never let a display hook break the stream."""
@@ -3246,6 +3269,9 @@ class _StreamingCall(StreamingWaitMonitor):
self._close_managed_stream()
if not self._handle_stream_error(e, _stream_attempt, _max_stream_retries):
return
+ if self._route_switched_under_request():
+ self.result["error"] = e
+ return
except InterruptedError as e:
# Fast pre-retry interrupt surfaces through the normal result channel.
self.result["error"] = e
diff --git a/tests/agent/test_stream_retry_route_switch.py b/tests/agent/test_stream_retry_route_switch.py
new file mode 100644
index 00000000000..9c22d331859
--- /dev/null
+++ b/tests/agent/test_stream_retry_route_switch.py
@@ -0,0 +1,85 @@
+"""A stream retry never replays a request built for a route the agent has since left.
+
+Regression for #112121: ``_StreamingCall`` captures ``api_kwargs`` once, but every stream
+(re)open builds its client from the LIVE agent. When ``/model`` (``switch_model``) re-pointed
+the agent while attempt 1 was stalled, attempt 2 sent the OLD model slug to the NEW provider's
+base_url (404, then a rate-limit hold). The streamer now hands the transient error back to the
+turn loop, which rebuilds the request for the current route on its own next attempt.
+"""
+from types import SimpleNamespace
+from unittest.mock import MagicMock, patch
+
+import httpx
+import pytest
+
+
+def _make_agent():
+ from run_agent import AIAgent
+
+ agent = AIAgent(
+ api_key="test-key", base_url="https://openrouter.ai/api/v1", model="deepseek/deepseek-v4-flash",
+ provider="openrouter", quiet_mode=True, skip_context_files=True, skip_memory=True,
+ enabled_toolsets=[], max_iterations=1,
+ )
+ agent.api_mode = "chat_completions"
+ return agent
+
+
+def _ok_stream(model):
+ chunks = [
+ SimpleNamespace(choices=[SimpleNamespace(index=0, delta=SimpleNamespace(
+ content="ok", tool_calls=None, reasoning_content=None, reasoning=None), finish_reason=None)],
+ model=model, usage=None),
+ SimpleNamespace(choices=[SimpleNamespace(index=0, delta=SimpleNamespace(
+ content=None, tool_calls=None, reasoning_content=None, reasoning=None), finish_reason="stop")],
+ model=model, usage=None),
+ ]
+ stream = MagicMock()
+ stream.__iter__ = MagicMock(return_value=iter(chunks))
+ stream.response = MagicMock(headers={})
+ return stream
+
+
+def _run(agent, on_create):
+ """Drive the streaming call with a request-local client whose ``create`` is ``on_create``."""
+ client = MagicMock()
+ client.chat.completions.create.side_effect = on_create
+ with patch("run_agent.AIAgent._create_request_openai_client", return_value=client), \
+ patch("run_agent.AIAgent._close_request_openai_client"):
+ return agent._interruptible_streaming_api_call(
+ {"model": "deepseek/deepseek-v4-flash", "messages": [{"role": "user", "content": "hi"}]})
+
+
+def test_stream_retry_does_not_replay_stale_route_after_switch_model():
+ """After a mid-request switch_model the streamer sends nothing more and surfaces the
+ transient error (the turn loop rebuilds for the new route); on base it re-sent the
+ deepseek slug to the moonshot base_url."""
+ agent = _make_agent()
+ sent = []
+
+ def switch_then_drop(**kwargs):
+ sent.append((kwargs["model"], agent.base_url))
+ with patch("agent.model_metadata.get_model_context_length", return_value=128000):
+ agent.switch_model("kimi-k2.6", "kimi-coding", api_key="k",
+ base_url="https://api.moonshot.ai/v1", api_mode="chat_completions")
+ raise httpx.ReadError("stale stream killed")
+
+ with pytest.raises(httpx.ReadError):
+ _run(agent, switch_then_drop)
+ assert sent == [("deepseek/deepseek-v4-flash", "https://openrouter.ai/api/v1")]
+
+
+def test_stream_retry_still_reconnects_in_place_when_route_unchanged():
+ """Control: without a switch the transient drop is retried inside the streamer."""
+ agent = _make_agent()
+ sent = []
+
+ def drop_once_then_succeed(**kwargs):
+ sent.append(kwargs["model"])
+ if len(sent) == 1:
+ raise httpx.ReadError("stale stream killed")
+ return _ok_stream(kwargs["model"])
+
+ response = _run(agent, drop_once_then_succeed)
+ assert response is not None
+ assert sent == ["deepseek/deepseek-v4-flash", "deepseek/deepseek-v4-flash"]