refactor: pass retry exhaustion callbacks explicitly

This commit is contained in:
chengyongru
2026-08-21 16:46:38 +08:00
committed by chengyongru
parent f93d4c3ae4
commit 9a6dc371b3
4 changed files with 68 additions and 78 deletions
+33
View File
@@ -132,6 +132,39 @@ async def test_chat_with_retry_emits_terminal_progress_when_standard_retries_exh
assert progress[-1] == "Model request failed after 4 attempts, giving up."
@pytest.mark.asyncio
async def test_chat_with_retry_routes_terminal_progress_to_explicit_callback(monkeypatch) -> 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"),
])
retry_progress: list[str] = []
terminal_progress: list[str] = []
async def _fake_sleep(delay: int) -> None:
return None
async def _retry_progress(msg: str) -> None:
retry_progress.append(msg)
async def _terminal_progress(msg: str) -> None:
terminal_progress.append(msg)
monkeypatch.setattr("nanobot.providers.base.asyncio.sleep", _fake_sleep)
response = await provider.chat_with_retry(
messages=[{"role": "user", "content": "hello"}],
on_retry_wait=_retry_progress,
on_retry_exhausted=_terminal_progress,
)
assert response.content == "503 final server error"
assert not any("giving up" in message for message in retry_progress)
assert terminal_progress == ["Model request failed after 4 attempts, giving up."]
@pytest.mark.asyncio
async def test_chat_with_retry_preserves_cancelled_error() -> None:
provider = ScriptedProvider([asyncio.CancelledError()])