Wait out a usage limit and retry, without counting it as a failed run
This commit is contained in:
@@ -2,11 +2,11 @@ import dataclasses
|
||||
import json
|
||||
import typing
|
||||
|
||||
from loop_claude.claude_code.entries import structured_output
|
||||
from loop_claude.claude_code.entries import hit_limit, parse_entry
|
||||
from loop_claude.cycle.retry import retried
|
||||
from loop_claude.cycle.verdicts import Finished, Interrupt, Unfinished, Verdict
|
||||
from loop_claude.effects import Effects
|
||||
from loop_claude.failure import Failure
|
||||
from loop_claude.failure import Failure, Limited
|
||||
from loop_claude.stop import Stop
|
||||
|
||||
JUDGED_TRANSCRIPT_TAIL = 40000
|
||||
@@ -63,14 +63,18 @@ def judge(root: str, task: str, premises: str, rendered_transcript: str, model:
|
||||
return retried("judge", lambda: run_judge(root, args, prompt, rendered_transcript, effects), effects)
|
||||
|
||||
|
||||
def run_judge(root: str, args: list[str], prompt: str, rendered_transcript: str, effects: Effects) -> Verdict | Failure:
|
||||
def run_judge(root: str, args: list[str], prompt: str, rendered_transcript: str, effects: Effects) -> Verdict | Failure | Limited:
|
||||
try:
|
||||
proc = effects.claude(args, root, prompt)
|
||||
except OSError as failure:
|
||||
return Failure(f"could not start claude: {failure.strerror}")
|
||||
reply = parse_entry(proc.stdout) or {}
|
||||
# The json output names the reset only in prose, such as "resets 7:40pm (Europe/Stockholm)", so the retry polls.
|
||||
if hit_limit(reply):
|
||||
return Limited(None)
|
||||
if proc.returncode != 0:
|
||||
return Failure(proc.stderr.strip() or f"the judge exited {proc.returncode}")
|
||||
verdict = verdict_from(structured_output(proc.stdout))
|
||||
verdict = verdict_from(reply.get("structured_output"))
|
||||
if isinstance(verdict, Failure):
|
||||
return Failure(f"{verdict.reason}; it replied: {proc.stdout.strip() or 'nothing'}")
|
||||
if not grounded(verdict.evidence, rendered_transcript):
|
||||
|
||||
@@ -1,21 +1,37 @@
|
||||
import sys
|
||||
import time
|
||||
import typing
|
||||
|
||||
from loop_claude.effects import Effects
|
||||
from loop_claude.failure import Failure
|
||||
from loop_claude.failure import Failure, Limited
|
||||
from loop_claude.stop import Stop
|
||||
|
||||
T = typing.TypeVar("T")
|
||||
|
||||
PAST_RESET = 120
|
||||
UNKNOWN_RESET_POLL = 600
|
||||
|
||||
def retried(who: str, attempt: typing.Callable[[], T | Failure], effects: Effects) -> T | Stop:
|
||||
|
||||
def retried(who: str, attempt: typing.Callable[[], T | Failure | Limited], effects: Effects) -> T | Stop:
|
||||
failures = 0
|
||||
while True:
|
||||
failures += 1
|
||||
result = attempt()
|
||||
if isinstance(result, Limited):
|
||||
due = retry_due(result, effects.now())
|
||||
print(f"the {who} hit a usage limit; retrying at {time.strftime('%Y-%m-%d %H:%M', time.localtime(due))}", file=sys.stderr, flush=True)
|
||||
effects.sleep(due - effects.now())
|
||||
continue
|
||||
if not isinstance(result, Failure):
|
||||
return result
|
||||
failures += 1
|
||||
if failures == 3:
|
||||
return Stop(1, f"the {who} failed three times running: {result.reason}")
|
||||
print(f"retrying the {who} in a minute: {result.reason}", file=sys.stderr)
|
||||
effects.sleep(60)
|
||||
|
||||
|
||||
def retry_due(limited: Limited, now: float) -> float:
|
||||
# A retry before the reset fails (https://platform.claude.com/docs/en/api/rate-limits), so polling sooner only spends requests.
|
||||
if limited.resets_at is not None and limited.resets_at + PAST_RESET > now:
|
||||
return limited.resets_at + PAST_RESET
|
||||
return now + UNKNOWN_RESET_POLL
|
||||
|
||||
@@ -1,25 +1,31 @@
|
||||
from loop_claude.claude_code.entries import assistant_parts, ends_stream, parse_entry
|
||||
from loop_claude.claude_code.entries import assistant_parts, ends_stream, hit_limit, parse_entry, rejected_until
|
||||
from loop_claude.claude_code.session import Session
|
||||
from loop_claude.claude_code.transcript import part_line
|
||||
from loop_claude.effects import Effects
|
||||
from loop_claude.failure import Failure
|
||||
from loop_claude.failure import Failure, Limited
|
||||
|
||||
PRINTED_TOOL_INPUT = 160
|
||||
|
||||
|
||||
def run_worker(root: str, args: list[str], effects: Effects) -> Failure | None:
|
||||
def run_worker(root: str, args: list[str], effects: Effects) -> Failure | Limited | None:
|
||||
try:
|
||||
proc = effects.stream_claude(args, root)
|
||||
except OSError as failure:
|
||||
return Failure(f"could not start claude: {failure.strerror}")
|
||||
done = False
|
||||
done = limited = False
|
||||
resets: list[int] = []
|
||||
for raw in proc.lines():
|
||||
event = parse_entry(raw) or {}
|
||||
done = done or ends_stream(event)
|
||||
limited = limited or hit_limit(event)
|
||||
if (resets_at := rejected_until(event)) is not None:
|
||||
resets.append(resets_at)
|
||||
for part in assistant_parts(event):
|
||||
if line := part_line(part, PRINTED_TOOL_INPUT):
|
||||
print(line, flush=True)
|
||||
code = proc.wait()
|
||||
if limited:
|
||||
return Limited(max(resets, default=None))
|
||||
if code < 0:
|
||||
return Failure(f"claude killed by signal {-code}")
|
||||
if code:
|
||||
|
||||
Reference in New Issue
Block a user