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]:
|
||||
|
||||
Reference in New Issue
Block a user