Files
2026-11-18 16:21:35 +01:00

45 lines
1.6 KiB
Python

import typing
from loop_effects.effects import Effects, say
from failure import Failure
from claude_code.reply import Limited
from agent_cycle.stop import Stop
T = typing.TypeVar("T")
FAILURES_RUNNING = 3
PAST_RESET = 120
RETRY_DELAY = 60
UNKNOWN_RESET_POLL = 600
def retried(who: str, attempt: typing.Callable[[], T | Failure | Limited], effects: Effects, keep: typing.Callable[[Failure], Stop | None] = lambda failure: None) -> T | Stop:
"""The attempt's result, retried on a failure. keep sees each failure, the third running included; a Stop from keep ends the retrying."""
failures = 0
while True:
result = attempt()
if isinstance(result, Limited):
# Hitting the limit proves claude started and reached the API, so the failures before it stop counting towards three.
failures = 0
now = effects.now()
wait = limit_wait(result, now)
say(f"the {who} hit a usage limit ({result.said}); retrying at {effects.local_time(now + wait)}", effects)
effects.sleep(wait)
continue
if not isinstance(result, Failure):
return result
if stop := keep(result):
return stop
failures += 1
if failures == FAILURES_RUNNING:
return Stop(1, f"the {who} failed three times running: {result.reason}")
say(f"retrying the {who} in a minute: {result.reason}", effects)
effects.sleep(RETRY_DELAY)
def limit_wait(limited: Limited, now: float) -> float:
# The limit rejects every request until it resets, so wait for a known reset and poll rarely for an unknown one.
if limited.resets_at is not None and limited.resets_at + PAST_RESET > now:
return limited.resets_at + PAST_RESET - now
return UNKNOWN_RESET_POLL