[loop-cycle-38] fix: add retry logic for Ollama 500 errors (#131) (#133)

Co-authored-by: hermes <hermes@timmy.local>
Co-committed-by: hermes <hermes@timmy.local>
This commit is contained in:
2026-03-15 09:38:21 -04:00
committed by rockachopa
parent f4e5148825
commit 76df262563
2 changed files with 152 additions and 9 deletions

View File

@@ -10,6 +10,7 @@ SubAgent is the single seed class for ALL agents. Differentiation
comes entirely from config (agents.yaml), not from Python subclasses.
"""
import asyncio
import logging
from abc import ABC, abstractmethod
from typing import Any
@@ -124,15 +125,34 @@ class BaseAgent(ABC):
Returns:
Agent response
"""
try:
result = self.agent.run(message, stream=False)
response = result.content if hasattr(result, "content") else str(result)
except (httpx.ConnectError, httpx.ReadError, ConnectionError) as exc:
logger.error("Ollama disconnected: %s", exc)
raise
except Exception as exc:
logger.error("Agent run failed: %s", exc)
raise
max_retries = 3
last_exception = None
for attempt in range(1, max_retries + 1):
try:
result = self.agent.run(message, stream=False)
response = result.content if hasattr(result, "content") else str(result)
break # Success, exit the retry loop
except (httpx.ConnectError, httpx.ReadError, ConnectionError) as exc:
logger.error("Ollama disconnected: %s", exc)
raise
except Exception as exc:
last_exception = exc
if attempt < max_retries:
logger.warning(
"Agent run failed on attempt %d/%d: %s. Retrying...",
attempt,
max_retries,
exc,
)
await asyncio.sleep(1)
else:
logger.error(
"Agent run failed after %d attempts: %s",
max_retries,
exc,
)
raise last_exception from exc
# Emit completion event
if self.event_bus: