"""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 work of one PR, 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 an exit below or Ctrl-C. Words: ~/.claude/CLAUDE.md → "Vocabulary". Exit 1: no task, git cannot start or finds no checkout, no premises, a models file it cannot read or write, claude leaves no transcript or one it cannot read or archive, a finished chunk's session it cannot delete, claude cannot start for an interrupt, python cannot start for a reload, a reload whose loop run it cannot read, or the worker or the judge fails three times running, retried a minute apart within the cycle. 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 pathlib import sys import typing from loop_claude.claude_code.session import session_for from loop_claude.cycle.steps import Cycle, run_cycle from loop_claude.effects import Effects from loop_claude.loop.reload import RELOAD_FLAG, LoopRun, decode_handed, exec_if_edited, fingerprint from loop_claude.outcome import Stop from loop_claude.paths import Paths from loop_claude.project.checkout import checkout_root, project_name from loop_claude.project.models import project_models from loop_claude.project.premises import read_premises def main(argv: list[str], env: typing.Mapping[str, str], paths: Paths, launcher: pathlib.Path, cwd: pathlib.Path, effects: Effects) -> int: try: stop = run_loop(argv, env, paths, launcher, cwd, effects) 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: list[str], env: typing.Mapping[str, str], paths: Paths, launcher: pathlib.Path, cwd: pathlib.Path, effects: Effects) -> Stop: loop_run = loop_run_from(argv, cwd, effects) if isinstance(loop_run, Stop): return loop_run baseline = fingerprint(launcher) project = project_name(loop_run.root, paths.code) session = session_for(project, paths) while True: if stop := exec_if_edited(launcher, baseline, loop_run, effects): return stop loop_run = dataclasses.replace(loop_run, cycle_number=loop_run.cycle_number + 1) premises = read_premises(loop_run.root) if isinstance(premises, Stop): return premises models = project_models(project, paths, env, effects) if isinstance(models, Stop): return models print(f"\n=== cycle {loop_run.cycle_number} ===", flush=True) if stop := run_cycle(Cycle(effects=effects, models=models, paths=paths, premises=premises, project=project, root=loop_run.root, session=session, task=loop_run.task)): return stop def loop_run_from(argv: list[str], cwd: pathlib.Path, effects: Effects) -> LoopRun | Stop: 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, effects) return root if isinstance(root, Stop) else LoopRun(0, root, task) loop_run = decode_handed(argv[1], LoopRun) if len(argv) == 2 else None if not (loop_run and loop_run.root and loop_run.task): return Stop(1, f"the reload cannot read the loop run from {argv[1:]}; rerun loop-claude ") return loop_run