fix: graceful return on max retries instead of crashing thread

run_conversation raised the raw exception after exhausting retries,
which crashed the background thread in cli.py (unhandled exception
in Thread). Now returns a proper error result dict with failed=True
and persists the session, matching the pattern used by other error
paths (invalid responses, empty content, etc.).

Also wraps cli.py's run_agent thread function in try/except as a
safety net against any future unhandled exceptions from
run_conversation.

Made-with: Cursor
This commit is contained in:
Teknium
2026-03-25 19:00:33 -07:00
parent 156b50358b
commit 08d3be0412
3 changed files with 35 additions and 12 deletions

View File

@@ -1425,8 +1425,8 @@ class TestRetryExhaustion:
assert "error" in result
assert "Invalid API response" in result["error"]
def test_api_error_raises_after_retries(self, agent):
"""Exhausted retries on API errors must raise, not fall through."""
def test_api_error_returns_gracefully_after_retries(self, agent):
"""Exhausted retries on API errors must return error result, not crash."""
self._setup_agent(agent)
agent.client.chat.completions.create.side_effect = RuntimeError("rate limited")
with (
@@ -1435,8 +1435,11 @@ class TestRetryExhaustion:
patch.object(agent, "_cleanup_task_resources"),
patch("run_agent.time", self._make_fast_time_mock()),
):
with pytest.raises(RuntimeError, match="rate limited"):
agent.run_conversation("hello")
result = agent.run_conversation("hello")
assert result.get("completed") is False
assert result.get("failed") is True
assert "error" in result
assert "rate limited" in result["error"]
# ---------------------------------------------------------------------------