"""Run a task as headless claude cycles; a judge interrupts only when a decision is the human's. usage: loop-claude Run it anywhere inside a project checkout whose README.md has ## Goals and ## Audience, the premises; the task runs at the checkout root. Each cycle a worker runs the task; a judge then reads the transcript against them and rules whether the chunk, the smallest PR-able change, is finished. A question only the human can answer, or a release ready to cut, is an interrupt: `claude --resume` on the terminal, every worktree first marked trusted in ~/.claude.json. An unfinished chunk resumes its session next cycle; the next chunk starts a fresh session. Cycles continue until that interrupt or Ctrl-C. Words: ~/.claude/CLAUDE.md → "Vocabulary". Exit 1: no task, not inside a checkout, no premises, a models file it cannot read or write, claude leaves no transcript or one it cannot read or archive, claude cannot start for an interrupt, a reload with unreadable state, or the worker or the judge fails three times running. A 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 verdict misses or mistypes a field, interrupts without a question, or quotes nothing in the transcript. Exit 2: the human is needed, to pick the model or answer an interrupt, and stdin or stdout is not a terminal, the input ends, or the interrupt ended with nothing typed. Rerunning resumes any open session. Each project has one session, "A loop-claude", being the checkout relative to ~/code, or to / outside it, deleted when the chunk finishes. At every interrupt and when the chunk finishes its transcript is copied to $LOOP_CLAUDE_TRANSCRIPTS//.jsonl (default ~/.claude/loop-claude-transcripts), the verdict beside it in .verdict.md. Every claude it starts runs the project's model, saved per in ~/.claude/loop-claude-models.json and read each cycle; a project without an entry, as on its first run, is asked for one. LOOP_CLAUDE_JUDGE_MODEL overrides it for the judge, LOOP_CLAUDE_WORKER_MODEL for the worker and the interrupt; with both set, nothing is asked. A change to bin/loop-claude or bin/loop_claude/*.py takes effect at the next cycle boundary, once loop-claude --help succeeds. """ import dataclasses import sys from loop_claude.checkout import checkout_root from loop_claude.cycle import run_cycle from loop_claude.models import project_models from loop_claude.outcome import Continue, Retry, Stop from loop_claude.premises import read_premises from loop_claude.reload import RELOAD_FLAG, decode_state, fingerprint, reload_if_edited from loop_claude.strike import strike @dataclasses.dataclass(frozen=True) class LoopState: cycle_number: int failures: int root: str task: str def main(argv, env, paths, launcher, cwd): try: stop = run_loop(argv, env, paths, launcher, cwd) except KeyboardInterrupt: stop = Stop(130, "") if stop.reason: print(stop.reason, file=sys.stderr if stop.code else sys.stdout) return stop.code def run_loop(argv, env, paths, launcher, cwd): state = loop_state(argv, cwd) if isinstance(state, Stop): return state baseline = fingerprint(launcher) while True: reload_if_edited(launcher, baseline, state) state = dataclasses.replace(state, cycle_number=state.cycle_number + 1) premises = read_premises(state.root) if isinstance(premises, Stop): return premises models = project_models(state.root, paths, env) if isinstance(models, Stop): return models print(f"\n=== cycle {state.cycle_number} ===", flush=True) match run_cycle(state.root, state.task, premises, models, paths): case Stop() as stop: return stop case Retry(failure): state = dataclasses.replace(state, failures=state.failures + 1) if stop := strike("worker", state.failures, failure): return stop case Continue(): state = dataclasses.replace(state, failures=0) def loop_state(argv, cwd): if argv[:1] != [RELOAD_FLAG]: task = " ".join(argv) if task in ("", "-h", "--help"): return Stop(0 if task else 1, __doc__.strip()) root = checkout_root(cwd) return root if isinstance(root, Stop) else LoopState(0, 0, root, task) state = decode_state(argv[1], LoopState) if len(argv) == 2 else None if not (state and state.root and state.task): return Stop(1, f"the reload cannot read the loop's state from {argv[1:]}; rerun loop-claude ") return state