fix(retry): prevent replay amplification and isolate backoff (NAN-313)

This commit is contained in:
chengyongru authored and chengyongru committed 2026-10-29 11:13:44 +08:00
1 parent b6d9009bb4
commit 4e77d9c3f2
19 files changed
+1178 -587

No files matched your search

+108 -28
View File
@@ -1,5 +1,6 @@
import asyncio
import copy
import random
import pytest
@@ -14,6 +15,11 @@ from nanobot.providers.base import (
)
def assert_local_backoff(delays: list[float], ceilings: tuple[float, ...]) -> None:
assert len(delays) == len(ceilings)
assert all(ceiling / 2 <= delay <= ceiling for delay, ceiling in zip(delays, ceilings))
class ScriptedProvider(LLMProvider):
def __init__(self, responses):
super().__init__(provider_name="scripted")
@@ -64,9 +70,9 @@ async def test_chat_with_retry_retries_transient_error_then_succeeds(monkeypatch
LLMResponse(content="429 rate limit", finish_reason="error"),
LLMResponse(content="ok"),
])
delays: list[int] = []
delays: list[float] = []
async def _fake_sleep(delay: int) -> None:
async def _fake_sleep(delay: float) -> None:
delays.append(delay)
monkeypatch.setattr("nanobot.providers.base.asyncio.sleep", _fake_sleep)
@@ -76,7 +82,7 @@ async def test_chat_with_retry_retries_transient_error_then_succeeds(monkeypatch
assert response.finish_reason == "stop"
assert response.content == "ok"
assert provider.calls == 2
assert delays == [1]
assert_local_backoff(delays, (1,))
@pytest.mark.asyncio
@@ -156,9 +162,9 @@ async def test_chat_with_retry_does_not_retry_non_transient_error(monkeypatch) -
provider = ScriptedProvider([
LLMResponse(content="401 unauthorized", finish_reason="error"),
])
delays: list[int] = []
delays: list[float] = []
async def _fake_sleep(delay: int) -> None:
async def _fake_sleep(delay: float) -> None:
delays.append(delay)
monkeypatch.setattr("nanobot.providers.base.asyncio.sleep", _fake_sleep)
@@ -171,25 +177,37 @@ async def test_chat_with_retry_does_not_retry_non_transient_error(monkeypatch) -
@pytest.mark.asyncio
async def test_chat_with_retry_returns_final_error_after_retries(monkeypatch) -> None:
@pytest.mark.parametrize("fraction", [0.0, 1.0])
async def test_chat_with_retry_returns_final_error_after_retries(monkeypatch, fraction) -> None:
provider = ScriptedProvider([
LLMResponse(content="429 rate limit a", finish_reason="error"),
LLMResponse(content="429 rate limit b", finish_reason="error"),
LLMResponse(content="429 rate limit c", finish_reason="error"),
LLMResponse(content="503 final server error", finish_reason="error"),
])
delays: list[int] = []
delays: list[float] = []
statuses: list[RetryStatusEvent] = []
monkeypatch.setattr(random, "uniform", lambda low, high: low + (high - low) * fraction)
monkeypatch.setattr("nanobot.providers.base.time.time", lambda: 1000.0)
async def _fake_sleep(delay: int) -> None:
async def _fake_sleep(delay: float) -> None:
delays.append(delay)
monkeypatch.setattr("nanobot.providers.base.asyncio.sleep", _fake_sleep)
async def status(event: RetryStatusEvent) -> None:
statuses.append(event)
response = await provider.chat_with_retry(messages=[{"role": "user", "content": "hello"}])
monkeypatch.setattr("nanobot.providers.base.asyncio.sleep", _fake_sleep)
response = await provider.chat_with_retry(
messages=[{"role": "user", "content": "hello"}], on_retry_status=status,
)
assert response.content == "503 final server error"
assert provider.calls == 4
assert delays == [1, 2, 4]
assert delays == [base * (0.5 + fraction / 2) for base in (1, 2, 4)]
assert [event.next_retry_at for event in statuses if event.state == "waiting"] == [
1000.0 + delay for delay in delays
]
assert statuses[-1].state == "exhausted"
@pytest.mark.asyncio
@@ -207,7 +225,7 @@ async def test_chat_with_retry_emits_terminal_progress_when_standard_retries_exh
progress: list[str] = []
statuses: list[RetryStatusEvent] = []
async def _fake_sleep(delay: int) -> None:
async def _fake_sleep(delay: float) -> None:
return None
async def _progress(msg: str) -> None:
@@ -245,7 +263,7 @@ async def test_chat_with_retry_routes_terminal_progress_to_explicit_callback(mon
retry_progress: list[str] = []
terminal_progress: list[str] = []
async def _fake_sleep(delay: int) -> None:
async def _fake_sleep(delay: float) -> None:
return None
async def _retry_progress(msg: str) -> None:
@@ -284,9 +302,9 @@ async def test_chat_stream_with_retry_does_not_retry_after_emitting_content(monk
LLMResponse(content="ok"),
])
deltas: list[str] = []
delays: list[int] = []
delays: list[float] = []
async def _fake_sleep(delay: int) -> None:
async def _fake_sleep(delay: float) -> None:
delays.append(delay)
async def _on_delta(delta: str) -> None:
@@ -318,9 +336,9 @@ async def test_chat_stream_with_retry_retries_timeout_after_emitting_content(mon
LLMResponse(content="full retry response"),
])
deltas: list[str] = []
delays: list[int] = []
delays: list[float] = []
async def _fake_sleep(delay: int) -> None:
async def _fake_sleep(delay: float) -> None:
delays.append(delay)
async def _on_delta(delta: str) -> None:
@@ -337,7 +355,7 @@ async def test_chat_stream_with_retry_retries_timeout_after_emitting_content(mon
assert response.finish_reason == "stop"
assert provider.calls == 2
assert deltas == ["partial"]
assert delays == [1]
assert_local_backoff(delays, (1,))
assert provider.last_kwargs.get("on_content_delta") is None
@@ -356,9 +374,9 @@ async def test_chat_stream_with_retry_retries_timeout_in_new_stream_segment(
provider = ScriptedProvider([first, second])
deltas: list[str] = []
recoveries: list[str] = []
delays: list[int] = []
delays: list[float] = []
async def _fake_sleep(delay: int) -> None:
async def _fake_sleep(delay: float) -> None:
delays.append(delay)
async def _on_delta(delta: str) -> None:
@@ -380,7 +398,7 @@ async def test_chat_stream_with_retry_retries_timeout_in_new_stream_segment(
assert provider.calls == 2
assert deltas == ["partial", "full retry response"]
assert recoveries == ["recover"]
assert delays == [1]
assert_local_backoff(delays, (1,))
assert provider.last_kwargs.get("on_content_delta") is not None
@@ -633,7 +651,8 @@ async def test_image_fallback_without_meta_uses_default_placeholder() -> None:
@pytest.mark.asyncio
async def test_chat_with_retry_uses_retry_after_and_emits_wait_progress(monkeypatch) -> None:
@pytest.mark.parametrize("retry_mode", ["standard", "persistent"])
async def test_chat_with_retry_uses_retry_after_and_emits_wait_progress(monkeypatch, retry_mode) -> None:
provider = ScriptedProvider([
LLMResponse(content="429 rate limit, retry after 7s", finish_reason="error"),
LLMResponse(content="ok"),
@@ -641,17 +660,21 @@ async def test_chat_with_retry_uses_retry_after_and_emits_wait_progress(monkeypa
delays: list[float] = []
progress: list[str] = []
def unexpected_draw(*args):
pytest.fail("A provider-directed Retry-After must not be jittered")
async def _fake_sleep(delay: float) -> None:
delays.append(delay)
async def _progress(msg: str) -> None:
progress.append(msg)
monkeypatch.setattr(random, "uniform", unexpected_draw)
monkeypatch.setattr("nanobot.providers.base.asyncio.sleep", _fake_sleep)
response = await provider.chat_with_retry(
messages=[{"role": "user", "content": "hello"}],
on_retry_wait=_progress,
on_retry_wait=_progress, retry_mode=retry_mode,
)
assert response.content == "ok"
@@ -744,7 +767,7 @@ async def test_chat_with_retry_retries_structured_status_code_without_keyword(mo
assert response.content == "ok"
assert provider.calls == 2
assert delays == [1]
assert_local_backoff(delays, (1,))
@pytest.mark.asyncio
@@ -821,7 +844,7 @@ async def test_chat_with_retry_retries_structured_timeout_kind(monkeypatch) -> N
assert response.content == "ok"
assert provider.calls == 2
assert delays == [1]
assert_local_backoff(delays, (1,))
@pytest.mark.asyncio
@@ -891,7 +914,7 @@ async def test_persistent_retry_aborts_after_ten_identical_transient_errors(monk
assert response.finish_reason == "error"
assert response.content == "429 rate limit"
assert provider.calls == 10
assert delays == [1, 2, 4, 4, 4, 4, 4, 4, 4]
assert_local_backoff(delays, (1, 2, 4, 4, 4, 4, 4, 4, 4))
@pytest.mark.asyncio
@@ -963,7 +986,7 @@ async def test_chat_with_retry_retries_zhipu_1302_rate_limit(monkeypatch) -> Non
assert response.content == "ok"
assert provider.calls == 2
assert delays == [1]
assert_local_backoff(delays, (1,))
@pytest.mark.asyncio
@@ -989,7 +1012,7 @@ async def test_chat_with_retry_retries_zhipu_1302_with_429_status(monkeypatch) -
assert response.content == "ok"
assert provider.calls == 2
assert delays == [1]
assert_local_backoff(delays, (1,))
@pytest.mark.asyncio
@@ -1006,3 +1029,60 @@ async def test_chat_stream_with_retry_normalizes_explicit_none_max_tokens() -> N
assert response.content == "ok"
assert provider.last_kwargs["max_tokens"] == 4096
assert provider.last_kwargs["temperature"] == 0.7
async def test_concurrent_requests_keep_retry_status_and_attempts_local(monkeypatch) -> None:
ready = asyncio.Event()
calls = {"a": 0, "b": 0}
statuses = {"a": [], "b": []}
class ConcurrentProvider(ScriptedProvider):
async def chat(self, messages, **kwargs):
key = messages[-1]["content"]
calls[key] += 1
if calls[key] == 1:
if all(calls.values()):
ready.set()
await ready.wait()
return LLMResponse(content="connection failed", finish_reason="error")
return LLMResponse(content=key)
provider = ConcurrentProvider([])
monkeypatch.setattr(provider, "_CHAT_RETRY_DELAYS", (0.1,))
async def request(key):
async def status(event):
statuses[key].append((event.state, event.attempt, event.max_attempts))
return await provider.chat_with_retry(
messages=[{"role": "user", "content": key}], on_retry_status=status,
)
results = await asyncio.wait_for(asyncio.gather(request("a"), request("b")), 2)
assert [result.content for result in results] == ["a", "b"]
assert calls == {"a": 2, "b": 2}
assert statuses == {key: [("waiting", 1, 2), ("recovered", 2, 2)] for key in calls}
async def test_cancellation_interrupts_retry_wait_without_another_call() -> None:
provider = ScriptedProvider([LLMResponse(
content="rate limited", finish_reason="error", error_status_code=429, error_retry_after_s=120,
)])
waiting = asyncio.Event()
statuses = []
async def status(event):
statuses.append((event.state, event.attempt))
waiting.set()
task = asyncio.create_task(provider.chat_with_retry(messages=[], on_retry_status=status))
try:
await asyncio.wait_for(waiting.wait(), 2)
task.cancel()
with pytest.raises(asyncio.CancelledError):
await task
finally:
task.cancel()
await asyncio.gather(task, return_exceptions=True)
assert provider.calls == 1
assert statuses == [("waiting", 1)]