Skip to content

The agent that grows with you

fix: pin the iteration-limit summary's router-shim rejection through handle_max_iterations

teknium19f7928ecdea7
diff --git a/agent/chat_completion_helpers.py b/agent/chat_completion_helpers.py
index ec2dd261f94..f4b8b2fbaef 100644
--- a/agent/chat_completion_helpers.py
+++ b/agent/chat_completion_helpers.py
@@ -3009,7 +3009,9 @@ class _StreamingCall(StreamingWaitMonitor):
if tool_calls_acc:
self._route_suppressed_text(delta_content)
elif (pending_text_parts or _provider_stream_text_may_be_sse(delta_content)
- or router_timeout_shim_may_follow("".join(content_parts))):
+ # A shim cannot follow text already released to the display, so the
+ # whole-content re-join runs only until the first emitted delta.
+ or (not self.deltas_were_sent["yes"] and router_timeout_shim_may_follow("".join(content_parts)))):
pending_text_parts.append(delta_content)
pending = "".join(pending_text_parts)
if not (_provider_stream_text_may_be_sse(pending) or router_timeout_shim_may_follow(pending)):
diff --git a/tests/agent/test_router_timeout_shim.py b/tests/agent/test_router_timeout_shim.py
index c0501e6f5e5..cfd38b44987 100644
--- a/tests/agent/test_router_timeout_shim.py
+++ b/tests/agent/test_router_timeout_shim.py
@@ -65,3 +65,24 @@ def test_auxiliary_validation_rejects_router_timeout_shim():
generated = SimpleNamespace(choices=[SimpleNamespace(message=SimpleNamespace(content=SHIM, tool_calls=None))],
usage=SimpleNamespace(completion_tokens=9), model="m")
assert _validate_llm_response(generated, "title") is generated
+
+
+@patch("agent.process_bootstrap.OpenAI")
+def test_iteration_limit_summary_retries_past_router_timeout_shim(_mock_openai):
+ """The iteration-limit summary is the fourth consumer: a shim takes the retry slot and the
+ real summary from the second call is returned instead of the sentinel text."""
+ from run_agent import AIAgent
+
+ def _completion(content, completion_tokens):
+ return SimpleNamespace(choices=[SimpleNamespace(message=SimpleNamespace(content=content, tool_calls=None),
+ finish_reason="stop")],
+ usage=SimpleNamespace(completion_tokens=completion_tokens), model="m")
+
+ agent = AIAgent(api_key="test-key", base_url="https://openrouter.ai/api/v1", model="test/model", quiet_mode=True,
+ skip_context_files=True, skip_memory=True, enabled_toolsets=[])
+ agent.api_mode = "chat_completions"
+ agent._cached_system_prompt = "You are helpful."
+ agent.client.chat.completions.create.side_effect = [_completion(SHIM, 0), _completion("Real summary.", 3)]
+
+ assert agent._handle_max_iterations([{"role": "user", "content": "do stuff"}], 60) == "Real summary."
+ assert agent.client.chat.completions.create.call_count == 2
diff --git a/tests/agent/transports/test_chat_completions.py b/tests/agent/transports/test_chat_completions.py
index a21980d6bef..c272aa45107 100644
--- a/tests/agent/transports/test_chat_completions.py
+++ b/tests/agent/transports/test_chat_completions.py
@@ -601,7 +601,6 @@ class TestChatCompletionsValidate:
("content", "tool_calls", "usage"),
[
("Connect timeout, please try again later.", None, SimpleNamespace(completion_tokens=1)),
- ("The router said: Connect timeout, please try again later.", None, None),
("Connect timeout, please try again later.", [SimpleNamespace()], None),
],
)