mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-05-02 02:01:47 +00:00
fix: correct off-by-one in retry exhaustion checks
The retry exhaustion checks used > instead of >= to compare retry_count against max_retries. Since the while loop condition is retry_count < max_retries, the check retry_count > max_retries can never be true inside the loop. When retries are exhausted, the loop exits and falls through to response.choices[0] on an invalid response, crashing with IndexError instead of returning a proper error.
This commit is contained in:
parent
6366177118
commit
c33f8d381b
2 changed files with 48 additions and 2 deletions
|
|
@ -758,3 +758,49 @@ class TestRunConversation:
|
|||
)
|
||||
result = agent.run_conversation("search something")
|
||||
mock_compress.assert_called_once()
|
||||
|
||||
|
||||
class TestRetryExhaustion:
|
||||
"""Regression: retry_count > max_retries was dead code (off-by-one).
|
||||
|
||||
When retries were exhausted the condition never triggered, causing
|
||||
the loop to exit and fall through to response.choices[0] on an
|
||||
invalid response, raising IndexError.
|
||||
"""
|
||||
|
||||
def _setup_agent(self, agent):
|
||||
agent._cached_system_prompt = "You are helpful."
|
||||
agent._use_prompt_caching = False
|
||||
agent.tool_delay = 0
|
||||
agent.compression_enabled = False
|
||||
agent.save_trajectories = False
|
||||
|
||||
def test_invalid_response_returns_error_not_crash(self, agent):
|
||||
"""Exhausted retries on invalid (empty choices) response must not IndexError."""
|
||||
self._setup_agent(agent)
|
||||
# Return response with empty choices every time
|
||||
bad_resp = SimpleNamespace(
|
||||
choices=[],
|
||||
model="test/model",
|
||||
usage=None,
|
||||
)
|
||||
agent.client.chat.completions.create.return_value = bad_resp
|
||||
with (
|
||||
patch.object(agent, "_persist_session"),
|
||||
patch.object(agent, "_save_trajectory"),
|
||||
patch.object(agent, "_cleanup_task_resources"),
|
||||
):
|
||||
result = agent.run_conversation("hello")
|
||||
assert result.get("failed") is True or result.get("completed") is False
|
||||
|
||||
def test_api_error_raises_after_retries(self, agent):
|
||||
"""Exhausted retries on API errors must raise, not fall through."""
|
||||
self._setup_agent(agent)
|
||||
agent.client.chat.completions.create.side_effect = RuntimeError("rate limited")
|
||||
with (
|
||||
patch.object(agent, "_persist_session"),
|
||||
patch.object(agent, "_save_trajectory"),
|
||||
patch.object(agent, "_cleanup_task_resources"),
|
||||
):
|
||||
with pytest.raises(RuntimeError, match="rate limited"):
|
||||
agent.run_conversation("hello")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue