diff --git a/bin/loop_claude/cycle.py b/bin/loop_claude/cycle.py index ec03af38..14055c31 100644 --- a/bin/loop_claude/cycle.py +++ b/bin/loop_claude/cycle.py @@ -5,7 +5,7 @@ from loop_claude.checkout import project_name from loop_claude.file_writes import make_private, replace_file from loop_claude.interrupt import interrupt from loop_claude.judge import judge -from loop_claude.outcome import Continue, Failure, Retry, Stop +from loop_claude.outcome import Continue, Failure, Stop from loop_claude.session import delete_session, session_for, transcript_file from loop_claude.transcript import read, render from loop_claude.worker import run_worker, worker_args @@ -15,7 +15,7 @@ def run_cycle(root, task, premises, models, paths): session = session_for(root, paths) resuming = bool(transcript_file(session)) if failure := run_worker(root, worker_args(task, session, models.worker, resuming)): - return Retry(failure.reason) + return failure path = transcript_file(session) if not path: return Stop(1, f"claude left no transcript for session {session.id}") @@ -57,6 +57,6 @@ def archive(root, path, note, paths): if not copy.exists(): for dest, data in ((copy.with_suffix(".verdict.md"), f"{note}\n".encode()), (copy, raw)): if failure := replace_file(dest, data): - return Failure(f"could not archive the transcript: {dest}: {failure.strerror}") + return Failure(f"could not archive the transcript: {dest}: {failure.reason}") print(f"transcript: {copy}", file=sys.stderr) return None diff --git a/bin/loop_claude/file_writes.py b/bin/loop_claude/file_writes.py index 0654c309..9db6bea0 100644 --- a/bin/loop_claude/file_writes.py +++ b/bin/loop_claude/file_writes.py @@ -1,6 +1,8 @@ import json import os +from loop_claude.outcome import Failure + def make_private(folder): mask = os.umask(0o077) @@ -25,5 +27,5 @@ def replace_file(path, data): except OSError as failure: if os.path.exists(staged): os.remove(staged) - return failure + return Failure(failure.strerror) return None diff --git a/bin/loop_claude/judge.py b/bin/loop_claude/judge.py index 38d402a1..943586ad 100644 --- a/bin/loop_claude/judge.py +++ b/bin/loop_claude/judge.py @@ -53,14 +53,14 @@ def judge(root, task, premises, transcript, model): for failures in itertools.count(1): verdict = run_judge(root, args, prompt) if isinstance(verdict, Failure): - failure = verdict.reason + reason = verdict.reason elif reason := incomplete(verdict): - failure = reason + pass elif not grounded(verdict, transcript): - failure = f"the judge quoted {verdict['evidence']!r}, nothing in the transcript" + (f", for the question: {verdict['question']}" if verdict["interrupt"] else "") + reason = f"the judge quoted {verdict['evidence']!r}, nothing in the transcript" + (f", for the question: {verdict['question']}" if verdict["interrupt"] else "") else: return verdict - if stop := stop_or_sleep_a_minute("judge", failures, failure): + if stop := stop_or_sleep_a_minute("judge", failures, reason): return stop diff --git a/bin/loop_claude/main.py b/bin/loop_claude/main.py index daae4f87..37c2c262 100644 --- a/bin/loop_claude/main.py +++ b/bin/loop_claude/main.py @@ -39,7 +39,7 @@ 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.outcome import Continue, Failure, Stop from loop_claude.premises import read_premises from loop_claude.reload import RELOAD_FLAG, decode_state, exec_if_edited, fingerprint from loop_claude.retry import stop_or_sleep_a_minute @@ -81,9 +81,9 @@ def run_loop(argv, env, paths, launcher, cwd): match run_cycle(state.root, state.task, premises, models, paths): case Stop() as stop: return stop - case Retry(failure): + case Failure(reason): state = dataclasses.replace(state, failures=state.failures + 1) - if stop := stop_or_sleep_a_minute("worker", state.failures, failure): + if stop := stop_or_sleep_a_minute("worker", state.failures, reason): return stop case Continue(): state = dataclasses.replace(state, failures=0) diff --git a/bin/loop_claude/models.py b/bin/loop_claude/models.py index bb184cfe..fa02b44a 100644 --- a/bin/loop_claude/models.py +++ b/bin/loop_claude/models.py @@ -40,7 +40,7 @@ def project_model(name, paths): if isinstance(models, Stop): return models if failure := replace_json(saved, {**models, name: chosen}, indent="\t", sort_keys=True): - return Stop(1, f"could not save the model in {saved}: {failure.strerror}") + return Stop(1, f"could not save the model in {saved}: {failure.reason}") print(f"{name} runs on {chosen}", file=sys.stderr) return chosen diff --git a/bin/loop_claude/outcome.py b/bin/loop_claude/outcome.py index 25395bf1..a4044b75 100644 --- a/bin/loop_claude/outcome.py +++ b/bin/loop_claude/outcome.py @@ -11,11 +11,6 @@ class Failure: reason: str -@dataclasses.dataclass(frozen=True) -class Retry: - failure: str - - @dataclasses.dataclass(frozen=True) class Stop: code: int diff --git a/bin/loop_claude/retry.py b/bin/loop_claude/retry.py index ab5c1a8e..f377899d 100644 --- a/bin/loop_claude/retry.py +++ b/bin/loop_claude/retry.py @@ -4,8 +4,8 @@ import time from loop_claude.outcome import Stop -def stop_or_sleep_a_minute(who, failures, failure): +def stop_or_sleep_a_minute(who, failures, reason): if failures == 3: - return Stop(1, f"the {who} failed three times running: {failure}") - print(f"retrying the {who} in a minute: {failure}", file=sys.stderr) + return Stop(1, f"the {who} failed three times running: {reason}") + print(f"retrying the {who} in a minute: {reason}", file=sys.stderr) time.sleep(60) diff --git a/bin/loop_claude/transcript.py b/bin/loop_claude/transcript.py index 36727ec6..2ddf768e 100644 --- a/bin/loop_claude/transcript.py +++ b/bin/loop_claude/transcript.py @@ -19,13 +19,13 @@ def human_turns(entries): def render(entries): lines = [] for entry in entries: - lines += [line for part in assistant_parts(entry) if (line := said(part))] + lines += [line for part in assistant_parts(entry) if (line := part_line(part))] if (text := human_text(entry)) is not None: lines.append("[human] " + text) return "\n\n".join(lines) -def said(part, limit=400): +def part_line(part, limit=400): if (text := part_text(part)) and text.strip(): return text if call := tool_call(part): diff --git a/bin/loop_claude/trust.py b/bin/loop_claude/trust.py index 14d93e46..68d9b4e7 100644 --- a/bin/loop_claude/trust.py +++ b/bin/loop_claude/trust.py @@ -21,6 +21,6 @@ def trust(config, roots): if not untrusted: return if failure := replace_json(config, data, indent=2): - print(f"could not write {config}: {failure.strerror}", file=sys.stderr) + print(f"could not write {config}: {failure.reason}", file=sys.stderr) return print("trusted for the interrupt: " + ", ".join(untrusted), file=sys.stderr) diff --git a/bin/loop_claude/worker.py b/bin/loop_claude/worker.py index 2688e1b8..ec73096f 100644 --- a/bin/loop_claude/worker.py +++ b/bin/loop_claude/worker.py @@ -2,7 +2,7 @@ import subprocess from loop_claude.claude_code import assistant_parts, ends_stream, parse from loop_claude.outcome import Failure -from loop_claude.transcript import said +from loop_claude.transcript import part_line def run_worker(root, args): @@ -16,7 +16,7 @@ def run_worker(root, args): event = parse(raw) or {} done = done or ends_stream(event) for part in assistant_parts(event): - if line := said(part, 160): + if line := part_line(part, 160): print(line, flush=True) code = proc.wait() if code < 0: diff --git a/bin/test_loop_claude.py b/bin/test_loop_claude.py index ba0d67a6..09e6a87b 100644 --- a/bin/test_loop_claude.py +++ b/bin/test_loop_claude.py @@ -8,7 +8,7 @@ import unittest sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent)) from loop_claude import claude_code, file_writes, judge, main, premises, transcript -from loop_claude.outcome import Stop +from loop_claude.outcome import Failure, Stop CWD = pathlib.Path("/c") @@ -58,12 +58,12 @@ class HumanTurns(unittest.TestCase): ]), 3) -class Said(unittest.TestCase): +class PartLine(unittest.TestCase): def test_renders_text_and_tool_calls_and_nothing_else(self): - self.assertEqual(transcript.said({"type": "text", "text": "hi"}), "hi") - self.assertEqual(transcript.said({"type": "text", "text": " \n"}), "") - self.assertEqual(transcript.said({"type": "tool_use", "name": "Bash", "input": {"command": "x" * 500}}, 20), '[tool Bash] {"command": "xxxxxxx') - self.assertEqual(transcript.said("not a part"), "") + self.assertEqual(transcript.part_line({"type": "text", "text": "hi"}), "hi") + self.assertEqual(transcript.part_line({"type": "text", "text": " \n"}), "") + self.assertEqual(transcript.part_line({"type": "tool_use", "name": "Bash", "input": {"command": "x" * 500}}, 20), '[tool Bash] {"command": "xxxxxxx') + self.assertEqual(transcript.part_line("not a part"), "") class Parse(unittest.TestCase): @@ -103,7 +103,7 @@ class ReplaceFile(unittest.TestCase): def test_removes_what_it_staged_when_the_replace_fails(self): with tempfile.TemporaryDirectory() as root: os.mkdir(os.path.join(root, "in-the-way")) - self.assertIsInstance(file_writes.replace_file(os.path.join(root, "in-the-way"), b"data"), IsADirectoryError) + self.assertEqual(file_writes.replace_file(os.path.join(root, "in-the-way"), b"data"), Failure("Is a directory")) self.assertEqual(os.listdir(root), ["in-the-way"]) diff --git a/todo.md b/todo.md index f87f73b6..e11ccd80 100644 --- a/todo.md +++ b/todo.md @@ -8,7 +8,6 @@ The rebuild lifts every dimension: each item below is one chunk on a worktree ba The places the panel on `rebuild` at 37edc9a named (`docs/decisions.md`), capping dimension first: -- **Give failure one spelling: `Failure.reason` and `Retry.failure`, `replace_file`'s raw `OSError`, and `said` named for what it tests.** Keep `LoopState`'s fields: they are main's `--reloaded` state (`docs/decisions.md`). - **Group `bin/loop_claude/`'s 18 flat modules, and move `principles/review-findings.md` beside larv-review.** Two seats; `claude_code.py` is the first group. - **Show the judge a reply the human sends while claude works.** Claude Code 2.1.284 writes it as an `"attachment"` entry, which `render` drops, so the judge can rule on a question the human already answered during an interrupt. - **Score `rebuild` with the four-seat panel at 7.0 or above on every dimension and the overall, none below the last run, and open its PR into main for the human.** ⚠️ Leave that PR ready and unmerged: only the human merges it, after approving the whole rebuild. A dimension under the floor gets its places filed above this item.