From 5a481d3ebf8132943c11c477ebabc282a919556c Mon Sep 17 00:00:00 2001 From: Lilleman auf Larv Date: Fri, 2 Oct 2026 22:49:39 +0200 Subject: [PATCH] Answer the first round: one clock read, the limit's message and time zone on stderr, plainer help and comments --- CHANGELOG.md | 2 +- bin/loop_claude/claude_code/entries.py | 4 ++++ bin/loop_claude/cycle/judge.py | 6 +++--- bin/loop_claude/cycle/retry.py | 15 ++++++++------- bin/loop_claude/cycle/worker.py | 12 +++++++----- bin/loop_claude/failure.py | 3 ++- bin/loop_claude/loop/main.py | 5 +++-- bin/test_loop_claude.py | 12 ++++++------ bin/test_loop_claude_black_box.py | 4 ++-- 9 files changed, 36 insertions(+), 27 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8ebbb850..cc88bf19 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ## Unreleased -- `loop-claude` waits out a usage limit and retries, without counting it as a failure: 2 minutes past the reset, or every 10 minutes when claude names none. It prints when the next try is due. +- `loop-claude` waits out a usage limit and retries, without counting it as a failure: 2 minutes past the reset, or every 10 minutes when claude names none. It prints what claude said about the limit and when the next try is due. - Install the `claude-security` plugin (README → Install): larv-review's full security scan now runs once you confirm its cost; an unattended larv-review reports the scan as not run. `settings.json` silences the plugin's scan tip after `git push`. - systems-architect and product-owner-reviewer can no longer edit what they review; stability-reviewer keeps its shell to run tests, and loses Claude Code's file-editing tools. - Every project's `todo.md` takes one shape, and larv-review's gate checks it: one table of items, each scored by the formula and goal weights the file states and held to its bar, an item under the bar dropped unless its Exempt cell says why. `bin/rank_todo.py` ranks the table, and `rank_todo.py --help` shows a valid file. diff --git a/bin/loop_claude/claude_code/entries.py b/bin/loop_claude/claude_code/entries.py index c5a2a2b2..6bfb73ac 100644 --- a/bin/loop_claude/claude_code/entries.py +++ b/bin/loop_claude/claude_code/entries.py @@ -94,6 +94,10 @@ def hit_limit(event: dict) -> bool: return ends_stream(event) and event.get("api_error_status") == 429 +def limit_said(event: dict) -> str: + return result if isinstance(result := event.get("result"), str) else "claude said nothing more" + + # {"type": "rate_limit_event", "rate_limit_info": {"status": "rejected", "resetsAt": 1790790000, ...}} def rejected_until(event: dict) -> int | None: """When a usage limit that rejected the request resets, in seconds since the epoch.""" diff --git a/bin/loop_claude/cycle/judge.py b/bin/loop_claude/cycle/judge.py index c35d2e10..06486ba1 100644 --- a/bin/loop_claude/cycle/judge.py +++ b/bin/loop_claude/cycle/judge.py @@ -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")) diff --git a/bin/loop_claude/cycle/retry.py b/bin/loop_claude/cycle/retry.py index 73210ce9..bc0f2ea8 100644 --- a/bin/loop_claude/cycle/retry.py +++ b/bin/loop_claude/cycle/retry.py @@ -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 diff --git a/bin/loop_claude/cycle/worker.py b/bin/loop_claude/cycle/worker.py index 5b02ca66..cf830eb0 100644 --- a/bin/loop_claude/cycle/worker.py +++ b/bin/loop_claude/cycle/worker.py @@ -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: diff --git a/bin/loop_claude/failure.py b/bin/loop_claude/failure.py index 8bca78a3..9c61548a 100644 --- a/bin/loop_claude/failure.py +++ b/bin/loop_claude/failure.py @@ -9,5 +9,6 @@ class Failure: @dataclasses.dataclass(frozen=True) class Limited: - """claude hit a usage limit, which resets at resets_at in seconds since the epoch, or at a time it did not say.""" + """claude hit a usage limit and said so; resets_at is when the limit resets, in seconds since the epoch, or None when claude did not say.""" resets_at: int | None + said: str diff --git a/bin/loop_claude/loop/main.py b/bin/loop_claude/loop/main.py index 19d199ae..2f283ab9 100644 --- a/bin/loop_claude/loop/main.py +++ b/bin/loop_claude/loop/main.py @@ -46,8 +46,9 @@ 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 - no failure: it is retried 2 minutes past its reset, or every 10 minutes while claude names no - future reset, as the judge's reply never does; stderr says when the next try is due. + not a failure: the worker or the judge is retried 2 minutes after the limit resets, or every 10 + minutes while claude names no future reset time. The judge's reply never names one. stderr says + when the next try is due. Exit 2, when the human is needed and cannot answer: - stdin or stdout is not a terminal, when a model must be picked or an interrupt answered diff --git a/bin/test_loop_claude.py b/bin/test_loop_claude.py index b0a856fb..9dc1c59d 100644 --- a/bin/test_loop_claude.py +++ b/bin/test_loop_claude.py @@ -260,7 +260,7 @@ class Retried(unittest.TestCase): def test_waits_out_a_usage_limit_without_counting_a_failure(self): fake = FakeEffects() - results = [Failure("one"), Limited(NOW + 3600), Failure("two"), Limited(None), Limited(NOW - 600), Failure("three")] + results = [Failure("one"), Limited(NOW + 3600, "limit"), Failure("two"), Limited(None, "limit"), Limited(NOW - 600, "limit"), Failure("three")] self.assertEqual(retry.retried("worker", lambda: results.pop(0), fake.effects()), Stop(1, "the worker failed three times running: three")) self.assertEqual(fake.calls, [("sleep", 60), ("sleep", 3720), ("sleep", 60), ("sleep", 600), ("sleep", 600)]) @@ -269,16 +269,16 @@ class Limits(unittest.TestCase): def test_the_worker_reports_the_latest_reset_its_stream_names(self): rejected = [{"type": "rate_limit_event", "rate_limit_info": {"status": "rejected", "resetsAt": resets_at}} for resets_at in (NOW + 60, NOW + 600)] allowed = {"type": "rate_limit_event", "rate_limit_info": {"status": "allowed", "resetsAt": NOW + 9000}} - limited = {"type": "result", "api_error_status": 429} - fake = FakeEffects(streams=[Streamed(1, rejected[1], allowed, rejected[0], limited), Streamed(1, limited), Streamed(0, rejected[0], {"type": "result"})]) - self.assertEqual(worker.run_worker("/r", [], fake.effects()), Limited(NOW + 600)) - self.assertEqual(worker.run_worker("/r", [], fake.effects()), Limited(None)) + limited = {"type": "result", "api_error_status": 429, "result": "You've hit your session limit"} + fake = FakeEffects(streams=[Streamed(1, rejected[1], allowed, rejected[0], limited), Streamed(1, {"type": "result", "api_error_status": 429}), Streamed(0, rejected[0], {"type": "result"})]) + self.assertEqual(worker.run_worker("/r", [], fake.effects()), Limited(NOW + 600, "You've hit your session limit")) + self.assertEqual(worker.run_worker("/r", [], fake.effects()), Limited(None, "claude said nothing more")) self.assertIsNone(worker.run_worker("/r", [], fake.effects())) def test_the_judge_reports_a_limit_whatever_its_exit_code(self): reply = json.dumps({"type": "result", "api_error_status": 429, "is_error": True, "result": "You've hit your session limit · resets 7:40pm (Europe/Stockholm)"}) fake = FakeEffects(claude=[ran(reply, code=1, stderr="limited")]) - self.assertEqual(judge.run_judge("/r", [], "", "", fake.effects()), Limited(None)) + self.assertEqual(judge.run_judge("/r", [], "", "", fake.effects()), Limited(None, "You've hit your session limit · resets 7:40pm (Europe/Stockholm)")) def test_reads_only_a_rejecting_events_whole_seconds(self): for info, want in [({"status": "rejected", "resetsAt": NOW}, NOW), ({"status": "allowed", "resetsAt": NOW}, None), ({"status": "rejected", "resetsAt": True}, None), ({"status": "rejected"}, None), ("rejected", None)]: diff --git a/bin/test_loop_claude_black_box.py b/bin/test_loop_claude_black_box.py index 59d16384..2b3f446a 100644 --- a/bin/test_loop_claude_black_box.py +++ b/bin/test_loop_claude_black_box.py @@ -399,8 +399,8 @@ class Cycles(unittest.TestCase): self.assertEqual(run.code, 130) self.assertEqual(run.roles(), ["worker"] * 4 + ["judge"] * 2 + ["worker"]) self.assertEqual([round(seconds, -1) for seconds in run.sleeps], [60, 3720, 60, 600]) - self.assertIn(f"the worker hit a usage limit; retrying at {time.strftime('%Y-%m-%d %H:%M', time.localtime(resets_at + 120))}\n", run.stderr) - self.assertIn("the judge hit a usage limit; retrying at ", run.stderr) + self.assertIn(f"the worker hit a usage limit (You've hit your session limit); retrying at {time.strftime('%Y-%m-%d %H:%M %Z', time.localtime(resets_at + 120))}\n", run.stderr) + self.assertIn("the judge hit a usage limit (You've hit your session limit); retrying at ", run.stderr) def test_retries_the_judge_alone_when_its_verdict_quotes_nothing_in_the_transcript(self): checkout = Checkout(self)