Answer the first round: one clock read, the limit's message and time zone on stderr, plainer help and comments

This commit is contained in:
2026-10-02 22:49:39 +02:00
parent e7ff869398
commit 5a481d3ebf
9 changed files with 36 additions and 27 deletions
+3 -3
View File
@@ -2,7 +2,7 @@ import dataclasses
import json
import typing
from loop_claude.claude_code.entries import hit_limit, parse_entry
from loop_claude.claude_code.entries import hit_limit, limit_said, 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
@@ -69,9 +69,9 @@ def run_judge(root: str, args: list[str], prompt: str, rendered_transcript: str,
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.
# The json output names the reset only in prose, such as "resets 7:40pm (Europe/Stockholm)", so the judge's limit carries no reset time.
if hit_limit(reply):
return Limited(None)
return Limited(None, limit_said(reply))
if proc.returncode != 0:
return Failure(proc.stderr.strip() or f"the judge exited {proc.returncode}")
verdict = verdict_from(reply.get("structured_output"))
+8 -7
View File
@@ -17,9 +17,10 @@ def retried(who: str, attempt: typing.Callable[[], T | Failure | Limited], effec
while True:
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())
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)
effects.sleep(wait)
continue
if not isinstance(result, Failure):
return result
@@ -30,8 +31,8 @@ def retried(who: str, attempt: typing.Callable[[], T | Failure | Limited], effec
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.
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
return now + UNKNOWN_RESET_POLL
return limited.resets_at + PAST_RESET - now
return UNKNOWN_RESET_POLL
+7 -5
View File
@@ -1,4 +1,4 @@
from loop_claude.claude_code.entries import assistant_parts, ends_stream, hit_limit, parse_entry, rejected_until
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.effects import Effects
@@ -12,20 +12,22 @@ 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 = limited = False
done = False
limit: str | None = None
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 hit_limit(event):
limit = limit_said(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 limit is not None:
return Limited(max(resets, default=None), limit)
if code < 0:
return Failure(f"claude killed by signal {-code}")
if code: