Answer the architect: a pure worker_outcome, Limited beside the retry, a limit ends a run of failures
This commit is contained in:
@@ -3,10 +3,10 @@ import json
|
||||
import typing
|
||||
|
||||
from loop_claude.claude_code.entries import hit_limit, limit_said, parse_entry
|
||||
from loop_claude.cycle.retry import retried
|
||||
from loop_claude.cycle.retry import Limited, retried
|
||||
from loop_claude.cycle.verdicts import Finished, Interrupt, Unfinished, Verdict
|
||||
from loop_claude.effects import Effects
|
||||
from loop_claude.failure import Failure, Limited
|
||||
from loop_claude.failure import Failure
|
||||
from loop_claude.stop import Stop
|
||||
|
||||
JUDGED_TRANSCRIPT_TAIL = 40000
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
import dataclasses
|
||||
import sys
|
||||
import time
|
||||
import typing
|
||||
|
||||
from loop_claude.effects import Effects
|
||||
from loop_claude.failure import Failure, Limited
|
||||
from loop_claude.failure import Failure
|
||||
from loop_claude.stop import Stop
|
||||
|
||||
T = typing.TypeVar("T")
|
||||
@@ -12,11 +13,20 @@ PAST_RESET = 120
|
||||
UNKNOWN_RESET_POLL = 600
|
||||
|
||||
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
class Limited:
|
||||
"""claude hit a usage limit; said is claude's message about it, and resets_at is when the limit resets, in seconds since the epoch, or None when unknown."""
|
||||
resets_at: int | None
|
||||
said: str
|
||||
|
||||
|
||||
def retried(who: str, attempt: typing.Callable[[], T | Failure | Limited], effects: Effects) -> T | Stop:
|
||||
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 no longer run on.
|
||||
failures = 0
|
||||
now = effects.now()
|
||||
wait = limit_wait(result, now)
|
||||
print(f"the {who} hit a usage limit ({result.said}); retrying at {time.strftime('%Y-%m-%d %H:%M %Z', time.localtime(now + wait))}", file=sys.stderr, flush=True)
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
from loop_claude.claude_code.entries import assistant_parts, ends_stream, hit_limit, limit_said, parse_entry, rejected_until
|
||||
from loop_claude.claude_code.session import Session
|
||||
from loop_claude.claude_code.transcript import part_line
|
||||
from loop_claude.cycle.retry import Limited
|
||||
from loop_claude.effects import Effects
|
||||
from loop_claude.failure import Failure, Limited
|
||||
from loop_claude.failure import Failure
|
||||
|
||||
PRINTED_TOOL_INPUT = 160
|
||||
|
||||
@@ -12,27 +13,28 @@ def run_worker(root: str, args: list[str], effects: Effects) -> Failure | Limite
|
||||
proc = effects.stream_claude(args, root)
|
||||
except OSError as failure:
|
||||
return Failure(f"could not start claude: {failure.strerror}")
|
||||
done = False
|
||||
limit: str | None = None
|
||||
result: dict | None = None
|
||||
resets: list[int] = []
|
||||
for raw in proc.lines():
|
||||
event = parse_entry(raw) or {}
|
||||
done = done or ends_stream(event)
|
||||
if hit_limit(event):
|
||||
limit = limit_said(event)
|
||||
if ends_stream(event):
|
||||
result = 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 limit is not None:
|
||||
return Limited(max(resets, default=None), limit)
|
||||
return worker_outcome(proc.wait(), result, resets)
|
||||
|
||||
|
||||
def worker_outcome(code: int, result: dict | None, resets: list[int]) -> Failure | Limited | None:
|
||||
if result is not None and hit_limit(result):
|
||||
return Limited(max(resets, default=None), limit_said(result))
|
||||
if code < 0:
|
||||
return Failure(f"claude killed by signal {-code}")
|
||||
if code:
|
||||
return Failure(f"claude exited {code}")
|
||||
return None if done else Failure("claude exited 0 without a result event")
|
||||
return None if result is not None else Failure("claude exited 0 without a result event")
|
||||
|
||||
|
||||
def worker_args(task: str, session: Session, model: str, resuming: bool) -> list[str]:
|
||||
|
||||
@@ -5,10 +5,3 @@ import dataclasses
|
||||
class Failure:
|
||||
"""A step failed, and reason says why; its caller decides what follows."""
|
||||
reason: str
|
||||
|
||||
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
class Limited:
|
||||
"""claude hit a usage limit; said is claude's message about it, and resets_at is when the limit resets, in seconds since the epoch, or None when unknown."""
|
||||
resets_at: int | None
|
||||
said: str
|
||||
|
||||
@@ -46,7 +46,7 @@ Exit 1, saying why, when:
|
||||
worker fails when claude cannot start, exits non-zero, is killed, or ends without a result event;
|
||||
a judge when claude cannot start or exits non-zero, or its reply misses or mistypes a field,
|
||||
interrupts without a question, or quotes a line the transcript does not hold. A usage limit is
|
||||
not a failure: the worker or the judge is retried 2 minutes after the limit resets, or every 10
|
||||
not a failure, and ends a run of them: the worker or the judge is retried 2 minutes after the limit resets, or every 10
|
||||
minutes when the reset time is unknown or past. The judge's reply gives its reset time only as
|
||||
text, so the judge is always retried every 10 minutes. stderr says when the next try is due.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user