From ba03c5ab275179d642ff945a4aabd892386d3c9b Mon Sep 17 00:00:00 2001 From: xxxigm Date: Mon, 6 Jul 2026 16:26:27 +0700 Subject: [PATCH] test(retry): cover Z.AI overload retry ceiling reachability Assert the invariant that the Z.AI overload retry ceiling exceeds the short-retry threshold (the original bug had them equal, so the long tier was dead code), and walk the attempt range the retry loop actually traverses to prove the full 30/60/90/120s long-backoff schedule now runs. --- tests/test_retry_utils.py | 45 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/tests/test_retry_utils.py b/tests/test_retry_utils.py index ff08d3a4062..460a9b8c0f3 100644 --- a/tests/test_retry_utils.py +++ b/tests/test_retry_utils.py @@ -210,3 +210,48 @@ def test_non_zai_backoff_returns_default_wait(): ) assert wait == 12.0 assert policy is None + + +def test_zai_overload_retry_ceiling_exceeds_short_attempts(): + """Invariant: the ceiling must sit above the short-retry threshold, or the + long-backoff tier is unreachable and the whole schedule is dead code + (the original bug: default api_max_retries == short_attempts == 3).""" + from agent.retry_utils import ( + zai_coding_overload_retry_ceiling, + _ZAI_CODING_OVERLOAD_LONG_BACKOFF, + ) + + short_attempts = 3 + ceiling = zai_coding_overload_retry_ceiling(short_attempts) + assert ceiling > short_attempts + # Room for every long-backoff entry to run before giving up: the loop's + # give-up check (retry_count >= ceiling) runs before the attempt's backoff, + # so the ceiling sits one past the final long entry. + assert ceiling == short_attempts + len(_ZAI_CODING_OVERLOAD_LONG_BACKOFF) + 1 + + +def test_zai_overload_ceiling_makes_long_tier_reachable(monkeypatch): + """End-to-end over the attempt range the retry loop actually walks: with the + extended ceiling, at least one attempt reaches the long-backoff tier and the + full 30/60/90/120s schedule is exercised.""" + monkeypatch.setattr(retry_utils, "jittered_backoff", lambda *a, **kw: kw["base_delay"]) + from agent.retry_utils import zai_coding_overload_retry_ceiling + + err = _zai_overload_error() + ceiling = zai_coding_overload_retry_ceiling() + + long_waits = [] + # The loop computes backoff for attempts 1..ceiling-1 (it gives up at ceiling). + for attempt in range(1, ceiling): + _wait, policy = adaptive_rate_limit_backoff( + attempt, + base_url="https://api.z.ai/api/coding/paas/v4", + model="glm-5.2", + error=err, + default_wait=1.0, + ) + if policy == "zai_coding_overload_long": + long_waits.append(_wait) + + assert long_waits, "long-backoff tier never reached within the retry ceiling" + assert long_waits == [30.0, 60.0, 90.0, 120.0]