Skip to content

The agent that grows with you

fix(stream): report 1-indexed drop attempt in _retry_after_drop (#90215)

codexbt7c10f13f5c58
diff --git a/agent/chat_completion_helpers.py b/agent/chat_completion_helpers.py
index e912eb0bbb3..07c15bf45a8 100644
--- a/agent/chat_completion_helpers.py
+++ b/agent/chat_completion_helpers.py
@@ -3508,7 +3508,7 @@ class _StreamingCall(StreamingWaitMonitor):
clients are never closed from inside a request (FD-recycle hazard); the
OpenAI primary is replaced lazily."""
self.agent._emit_stream_drop(
- error=e, attempt=attempt + 2, max_attempts=max_retries + 1, mid_tool_call=mid_tool_call, diag=self.clients.diag)
+ error=e, attempt=attempt + 1, max_attempts=max_retries + 1, mid_tool_call=mid_tool_call, diag=self.clients.diag)
if self.agent._is_provider_stream_parse_error(e):
from agent.anthropic_adapter import buffer_anthropic_tool_input
buffer_anthropic_tool_input(self.api_kwargs, getattr(self.agent, "_anthropic_base_url", None))
diff --git a/tests/agent/test_stream_drop_logging.py b/tests/agent/test_stream_drop_logging.py
index 1b0d361a713..11170d59738 100644
--- a/tests/agent/test_stream_drop_logging.py
+++ b/tests/agent/test_stream_drop_logging.py
@@ -109,3 +109,22 @@ def test_quiet_mode_does_not_clobber_runagent_logger_level():
for name in ("run_agent", "tools", "trajectory_compressor", "cron", "hermes_cli"):
logger = logging.getLogger(name)
assert logger.getEffectiveLevel() <= logging.WARNING
+
+
+def test_retry_after_drop_reports_the_attempt_that_dropped():
+ """First drop (0-indexed attempt 0) must be announced as attempt 1/N, not 2/N (#90215)."""
+ from types import SimpleNamespace
+ from unittest.mock import MagicMock
+
+ from agent.chat_completion_helpers import _StreamingCall
+
+ agent = MagicMock()
+ agent._is_provider_stream_parse_error.return_value = False
+ fake = SimpleNamespace(
+ agent=agent,
+ clients=SimpleNamespace(diag=None, close_once=lambda reason: None),
+ _cancel_current_stream_attempt=lambda reason: None,
+ )
+ _StreamingCall._retry_after_drop(fake, ConnectionError("drop"), 0, 2, mid_tool_call=False, reason="t")
+ assert agent._emit_stream_drop.call_args.kwargs["attempt"] == 1
+ assert agent._emit_stream_drop.call_args.kwargs["max_attempts"] == 3