Skip to content

The agent that grows with you

fix(agent): normalize zero-cooldown semantics, dedupe test driver

kshitijk4poor44e52e752423
diff --git a/agent/turn_recovery.py b/agent/turn_recovery.py
index ff92e7e72fc..be2a46378f3 100644
--- a/agent/turn_recovery.py
+++ b/agent/turn_recovery.py
@@ -994,7 +994,7 @@ def compute_error_backoff(
_error_body = getattr(api_error, "body", None)
if isinstance(_error_body, dict):
# Some providers nest it as error.retry_after (the same unwrap
- # _extract_rate_limit_context uses), others put it at the top level.
+ # extract_api_error_context uses), others put it at the top level.
_nested = _error_body.get("error")
_payload = _nested if isinstance(_nested, dict) else _error_body
_retry_after = parse_retry_after_seconds(_payload.get("retry_after"))
@@ -1003,10 +1003,15 @@ def compute_error_backoff(
# caused us to retry before the actual reset window and re-trip the limit. 600s covers all
# realistic provider reset windows while still rejecting pathological values. (#26293)
_retry_after = min(_retry_after, 600)
- wait_time = _retry_after if _retry_after else jittered_backoff(retry_count, base_delay=2.0, max_delay=60.0)
+ if _retry_after <= 0:
+ # A zero/expired cooldown (retry-after: 0, or an HTTP-date in the
+ # past, which the parser clamps to 0.0) carries no usable wait —
+ # treat it as absent so we never hot-loop the provider.
+ _retry_after = None
+ wait_time = _retry_after if _retry_after is not None else jittered_backoff(retry_count, base_delay=2.0, max_delay=60.0)
_backoff_policy = None
_adaptive = is_rate_limited or is_zai_coding_overload
- if _adaptive and not _retry_after:
+ if _adaptive and _retry_after is None:
wait_time, _backoff_policy = adaptive_rate_limit_backoff(
retry_count, base_url=str(base_url), model=model, error=api_error, default_wait=wait_time,
)
diff --git a/tests/run_agent/test_run_agent.py b/tests/run_agent/test_run_agent.py
index 867ba239907..80b8882d2a9 100644
--- a/tests/run_agent/test_run_agent.py
+++ b/tests/run_agent/test_run_agent.py
@@ -1966,20 +1966,32 @@ class TestRetryAfterCap:
This covers rate-limit headers (#26293) and retryable 5xx responses.
"""
- def _drive_once(self, agent, retry_after_value):
- """Raise one 429 carrying ``Retry-After`` and capture the wait the loop
- chose. Interrupt during the backoff sleep so the test doesn't actually
- wait, and return the status string that reports the wait time."""
+ @staticmethod
+ def _retryable_error(status_code, headers, body=None):
+ """A provider error carrying optional Retry-After surfaces."""
+ message = (
+ "Error code: 429 - Rate limit exceeded."
+ if status_code == 429
+ else f"Error code: {status_code} - origin response timeout"
+ )
- class _RateLimitError(Exception):
- status_code = 429
- response = SimpleNamespace(headers={"retry-after": str(retry_after_value)})
+ class _ProviderError(Exception):
+ def __init__(self):
+ super().__init__(message)
+ self.status_code = status_code
+ self.response = SimpleNamespace(headers=headers)
+ if body is not None:
+ self.body = body
- def __str__(self):
- return "Error code: 429 - Rate limit exceeded."
+ return _ProviderError()
+
+ def _drive_once(self, agent, error, status_marker):
+ """Raise ``error`` from the API call and capture the backoff status the
+ loop chose. Interrupt during the backoff sleep so the test doesn't
+ actually wait, and return the status string reporting the wait."""
def _fake_api_call(api_kwargs):
- raise _RateLimitError()
+ raise error
agent._interruptible_api_call = _fake_api_call
agent._persist_session = lambda *args, **kwargs: None
@@ -1987,77 +1999,60 @@ class TestRetryAfterCap:
captured = []
original_buffer = agent._buffer_status
+ original_emit = agent._emit_status
def _capture_status(msg, *args, **kwargs):
captured.append(msg)
- # Break out of the incremental backoff sleep immediately rather
- # than blocking for the full Retry-After window.
- if "Waiting" in msg:
+ # Break out of the backoff sleep immediately rather than blocking
+ # for the full Retry-After window.
+ if status_marker in msg:
agent._interrupt_requested = True
return original_buffer(msg, *args, **kwargs)
+ def _capture_emit(msg):
+ captured.append(msg)
+ if status_marker in msg:
+ agent._interrupt_requested = True
+ return original_emit(msg)
+
agent._buffer_status = _capture_status
+ agent._emit_status = _capture_emit
agent.run_conversation("hello")
- return next((m for m in captured if "Waiting" in m), "")
+ return next((m for m in captured if status_marker in m), "")
def test_retry_after_under_cap_is_honored(self, agent):
# 300s > old 120s cap but < new 600s cap → used verbatim.
- status = self._drive_once(agent, 300)
+ error = self._retryable_error(429, {"retry-after": "300"})
+ status = self._drive_once(agent, error, "Waiting")
assert "Waiting 300.0s" in status
@pytest.mark.parametrize(
- ("headers", "body"),
+ ("headers", "body", "expected_wait"),
[
- ({"Retry-After": "120"}, {}),
- ({}, {"status": 524, "retry_after": 120}),
- ({}, {"status": 524, "error": {"retry_after": 120}}),
+ ({"Retry-After": "120"}, {}, "120.0"),
+ ({}, {"status": 524, "retry_after": 120}, "120.0"),
+ ({}, {"status": 524, "error": {"retry_after": 120}}, "120.0"),
+ # Above the 600s ceiling → capped, never used verbatim.
+ ({"Retry-After": "3600"}, {}, "600.0"),
+ # No cooldown on header or body → falls through to jittered
+ # backoff (patched to 0.0 by the conftest fixture), no crash.
+ ({}, {"status": 524}, "0.0"),
],
- ids=("header", "problem-detail-body", "nested-problem-detail-body"),
+ ids=(
+ "header",
+ "problem-detail-body",
+ "nested-problem-detail-body",
+ "over-cap-is-capped",
+ "no-cooldown-falls-back",
+ ),
)
def test_retry_after_on_cloudflare_524_is_honored(
- self, agent, headers, body
+ self, agent, headers, body, expected_wait
):
"""A retryable 5xx must not bypass the provider's cooldown."""
-
- class _CloudflareTimeout(Exception):
- status_code = 524
-
- def __str__(self):
- return "Error code: 524 - origin response timeout"
-
- error = _CloudflareTimeout()
- error.response = SimpleNamespace(headers=headers)
- error.body = body
-
- def _fake_api_call(api_kwargs):
- raise error
-
- agent._interruptible_api_call = _fake_api_call
- agent._persist_session = lambda *args, **kwargs: None
- agent._save_trajectory = lambda *args, **kwargs: None
-
- captured = []
- original_buffer = agent._buffer_status
- original_emit = agent._emit_status
-
- def _capture_status(msg, *args, **kwargs):
- captured.append(msg)
- if "Retrying in" in msg:
- agent._interrupt_requested = True
- return original_buffer(msg, *args, **kwargs)
-
- def _capture_emit(msg):
- captured.append(msg)
- if "Retrying in" in msg:
- agent._interrupt_requested = True
- return original_emit(msg)
-
- agent._buffer_status = _capture_status
- agent._emit_status = _capture_emit
- agent.run_conversation("hello")
-
- assert any("Retrying in 120.0s" in msg for msg in captured)
-
+ error = self._retryable_error(524, headers, body)
+ status = self._drive_once(agent, error, "Retrying in")
+ assert f"Retrying in {expected_wait}s" in status
class TestConcurrentToolExecution: