[loop-cycle-50] fix: agent retry uses exponential backoff instead of fixed 1s delay (#174) (#181)
All checks were successful
Tests / lint (push) Successful in 6s
Tests / test (push) Successful in 1m20s

This commit was merged in pull request #181.
This commit is contained in:
2026-03-15 12:08:30 -04:00
parent bcd6d7e321
commit 5b57bf3dd0
2 changed files with 19 additions and 6 deletions

View File

@@ -1,6 +1,6 @@
"""Tests for agent retry logic on transient errors."""
from unittest.mock import MagicMock
from unittest.mock import MagicMock, patch
import httpx
import pytest
@@ -42,11 +42,16 @@ async def test_run_retries_on_transient_error(sub_agent):
sub_agent.agent.run = mock_run
# Act
result = await sub_agent.run("test message")
with patch("timmy.agents.base.asyncio.sleep") as mock_sleep:
result = await sub_agent.run("test message")
# Assert
assert result == "Success after retries"
assert call_count == 3 # 2 failures + 1 success
# Verify exponential backoff: attempt 1 = 1s, attempt 2 = 2s
assert mock_sleep.call_count == 2
mock_sleep.assert_any_call(1)
mock_sleep.assert_any_call(2)
@pytest.mark.asyncio
@@ -56,11 +61,16 @@ async def test_run_exhausts_retries(sub_agent):
sub_agent.agent.run.side_effect = Exception("Ollama 500 error: XML parse error")
# Act & Assert
with pytest.raises(Exception, match="Ollama 500 error: XML parse error"):
await sub_agent.run("test message")
with patch("timmy.agents.base.asyncio.sleep") as mock_sleep:
with pytest.raises(Exception, match="Ollama 500 error: XML parse error"):
await sub_agent.run("test message")
# Should have been called 3 times (max retries)
assert sub_agent.agent.run.call_count == 3
# Verify exponential backoff: attempt 1 = 1s, attempt 2 = 2s
assert mock_sleep.call_count == 2
mock_sleep.assert_any_call(1)
mock_sleep.assert_any_call(2)
@pytest.mark.asyncio
@@ -114,10 +124,13 @@ async def test_run_logs_retry_attempts(sub_agent, caplog):
sub_agent.agent.run = mock_run
# Act
result = await sub_agent.run("test message")
with patch("timmy.agents.base.asyncio.sleep") as mock_sleep:
result = await sub_agent.run("test message")
# Assert
assert result == "Success"
assert call_count == 2 # 1 failure + 1 success
assert "Agent run failed on attempt 1/3" in caplog.text
assert "Retrying..." in caplog.text
# Verify sleep was called with 1s for first attempt
mock_sleep.assert_called_once_with(1)