From a64461f2c12ba1094782ce42f19aa7da782b6f77 Mon Sep 17 00:00:00 2001 From: Lilleman auf Larv Date: Sat, 26 Sep 2026 13:35:50 +0200 Subject: [PATCH 1/3] Carry the reload state as one JSON argument and exit naming a reload without a task --- CHANGELOG.md | 1 + bin/loop-claude | 24 +++++++++++++++++------- bin/test_loop_claude.py | 17 +++++++++++++---- todo.md | 1 - 4 files changed, 31 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8852a1b8..1d897379 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ - `loop-claude` archives each session's transcript under `~/.claude/loop-claude-transcripts/`, one folder per project; `LOOP_CLAUDE_TRANSCRIPTS` overrides the root. - `loop-claude` runs at the checkout root wherever inside the checkout it is started. - `loop-claude` says why when it cannot read `~/.claude.json` before an interrupt, and the interrupt then lands in Claude Code's trust dialog. +- `loop-claude` takes an edit to itself at the next cycle boundary once the edited file answers `--help`, and exits 1 naming the reload when the edited file cannot read the running loop's state. - `loop-claude --help` names the Claude Code version its transcript, session and trust handling were observed on; check it first when an upgrade breaks a cycle. - `loop-claude` runs the model each project's settings pick; `LOOP_CLAUDE_WORKER_MODEL` and `LOOP_CLAUDE_JUDGE_MODEL` override it. - `settings.json`, the agents and the status line run on any machine unedited; the status line shows reset times in the machine's own time zone. diff --git a/bin/loop-claude b/bin/loop-claude index e088fbf9..abdcd06f 100755 --- a/bin/loop-claude +++ b/bin/loop-claude @@ -10,8 +10,8 @@ interrupt: `claude --resume` on the terminal, every worktree first marked truste An unfinished chunk resumes the same session next cycle; a finished one 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, claude leaves no transcript, or the worker or -the judge fails three times running. A worker fails when claude exits non-zero, is killed, or ends +Exit 1: no task, not inside a checkout, no premises, claude leaves no transcript, a reload handing +over no task, or the worker or the judge fails three times running. A worker fails when claude exits non-zero, is killed, or ends without a result event; a judge when its verdict misses a field or quotes nothing in the transcript. Exit 2: the human is needed and stdin or stdout is not a terminal, or the interrupt ended without them typing anything. Rerunning resumes any open session. @@ -373,19 +373,29 @@ def reload_if_edited(root, task, cycle_number, failures): if trial.returncode != 0: print(f"keeping the running script: {trial.stderr.strip() or f'exit {trial.returncode}'}", file=sys.stderr) return - os.execv(sys.executable, [sys.executable, __file__, RELOAD_FLAG, str(cycle_number), str(failures), root, task]) + state = {"cycle_number": cycle_number, "failures": failures, "root": root, "task": task} + os.execv(sys.executable, [sys.executable, __file__, RELOAD_FLAG, json.dumps(state)]) + + +def reloaded_state(handed): + state = parse(handed[0]) if len(handed) == 1 else None + if not state or {"cycle_number", "failures", "root"} - state.keys() or not isinstance(state.get("task"), str) or not state["task"]: + sys.exit(f"the reload handed {handed}, not the loop's state with a task; rerun loop-claude ") + return state def main(): argv = sys.argv[1:] - cycle_number, failures, root = 0, 0, "" if argv[:1] == [RELOAD_FLAG]: - cycle_number, failures, root, argv = int(argv[1]), int(argv[2]), argv[3], argv[4:] - task = " ".join(argv) + state = reloaded_state(argv[1:]) + else: + state = {"cycle_number": 0, "failures": 0, "root": "", "task": " ".join(argv)} + task = state["task"] if task in ("", "-h", "--help"): print(__doc__.strip()) sys.exit(0 if task else 1) - root = root or checkout_root() + root = state["root"] or checkout_root() + cycle_number, failures = state["cycle_number"], state["failures"] while True: reload_if_edited(root, task, cycle_number, failures) cycle_number += 1 diff --git a/bin/test_loop_claude.py b/bin/test_loop_claude.py index 2224c7f7..4473e6f0 100644 --- a/bin/test_loop_claude.py +++ b/bin/test_loop_claude.py @@ -337,7 +337,7 @@ class RunCycle(unittest.TestCase): class Reload(unittest.TestCase): - def test_runs_the_edited_file_before_trusting_it_and_hands_it_the_root_and_counters_on_argv(self): + def test_runs_the_edited_file_before_trusting_it_and_hands_it_the_state_as_one_json_argument(self): with tempfile.TemporaryDirectory() as root: script = os.path.join(root, "loop-claude") open(script, "w").write(loop.RELOAD_BASELINE.replace("import glob\n", "import glob\nimport no_such_module\n")) @@ -348,16 +348,25 @@ class Reload(unittest.TestCase): environment = dict(os.environ) with unittest.mock.patch.object(loop, "__file__", script), unittest.mock.patch("os.execv") as execv: loop.reload_if_edited("/r", "do it", cycle_number=1, failures=2) - execv.assert_called_once_with(sys.executable, [sys.executable, script, loop.RELOAD_FLAG, "1", "2", "/r", "do it"]) + execv.assert_called_once_with(sys.executable, [sys.executable, script, loop.RELOAD_FLAG, STATE]) self.assertEqual(dict(os.environ), environment) +STATE = json.dumps({"cycle_number": 1, "failures": 2, "root": "/r", "task": "do it"}) + + class Main(unittest.TestCase): + def test_exits_naming_the_reload_when_it_hands_over_anything_but_the_state_with_a_task(self): + for handed in (["3", "2", "/r", "do it"], ["3"], ["[1]"], [json.dumps({"cycle_number": 3, "failures": 0, "root": "/r"})], [json.dumps({"cycle_number": 3, "failures": 0, "root": "/r", "task": ""})]): + with self.subTest(handed=handed), unittest.mock.patch.object(sys, "argv", ["loop-claude", loop.RELOAD_FLAG, *handed]), self.assertRaises(SystemExit) as stopped: + loop.main() + self.assertEqual(stopped.exception.code, f"the reload handed {handed}, not the loop's state with a task; rerun loop-claude ") + def test_resumes_the_root_and_counters_a_reload_handed_over(self): patches = [unittest.mock.patch.object(loop, name) for name in ("checkout_root", "read_premises", "reload_if_edited", "run_cycle")] with contextlib.ExitStack() as stack: mocks = [stack.enter_context(patch) for patch in patches] - stack.enter_context(unittest.mock.patch.object(sys, "argv", ["loop-claude", loop.RELOAD_FLAG, "3", "2", "/r", "do it"])) + stack.enter_context(unittest.mock.patch.object(sys, "argv", ["loop-claude", loop.RELOAD_FLAG, json.dumps({"cycle_number": 3, "failures": 2, "root": "/r", "task": "do it"})])) stack.enter_context(unittest.mock.patch.object(sys, "stdout", io.StringIO())) mocks[3].side_effect = SystemExit(0) with self.assertRaises(SystemExit): @@ -374,7 +383,7 @@ class Main(unittest.TestCase): mocks[1].assert_called_with("/s") self.assertIn("=== cycle 4 ===", sys.stdout.getvalue()) mocks[3].side_effect = ["claude exited 3"] * 3 - stack.enter_context(unittest.mock.patch.object(sys, "argv", ["loop-claude", loop.RELOAD_FLAG, "3", "0", "/r", "do it"])) + stack.enter_context(unittest.mock.patch.object(sys, "argv", ["loop-claude", loop.RELOAD_FLAG, json.dumps({"cycle_number": 3, "failures": 0, "root": "/r", "task": "do it"})])) stack.enter_context(unittest.mock.patch.object(sys, "stderr", io.StringIO())) sleep = stack.enter_context(unittest.mock.patch("time.sleep")) with self.assertRaises(SystemExit) as stopped: diff --git a/todo.md b/todo.md index 09376d94..8c17f21e 100644 --- a/todo.md +++ b/todo.md @@ -4,7 +4,6 @@ Bar the reload item, ordered by how many seats of the 2026-09-25 scoring panel (`docs/decisions.md`) named the place; each is one chunk. -- Carry the reload state of `bin/loop-claude` as one JSON argument, and exit naming the reload when it arrives without a task, today a new argument shifts the task out and an older running loop reloads into `usage` and exit 1. - Name `end_session` for what it does, today it deletes the session's transcript files. - Define looping reviewer and round cap under `CLAUDE.md` → "Vocabulary", today inline at `agents/review-protocol.md`. - Give "scoring run" a heading in `skills/comprehension-panel/SKILL.md`, today inside a paragraph `larv-review` cites. -- 2.52.0 From 805eb868dbcdd302f708cd46853a728a7b0fd1cb Mon Sep 17 00:00:00 2001 From: Lilleman auf Larv Date: Sat, 26 Sep 2026 13:39:09 +0200 Subject: [PATCH 2/3] Name the reload exit for what it checks, gate --help only on a typed task, drop the outlived todo caveat --- bin/loop-claude | 13 +++++++------ bin/test_loop_claude.py | 5 +---- todo.md | 2 +- 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/bin/loop-claude b/bin/loop-claude index abdcd06f..bbc72cf6 100755 --- a/bin/loop-claude +++ b/bin/loop-claude @@ -10,9 +10,10 @@ interrupt: `claude --resume` on the terminal, every worktree first marked truste An unfinished chunk resumes the same session next cycle; a finished one 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, claude leaves no transcript, a reload handing -over no task, or the worker or the judge fails three times running. A worker fails when claude exits non-zero, is killed, or ends -without a result event; a judge when its verdict misses a field or quotes nothing in the transcript. +Exit 1: no task, not inside a checkout, no premises, claude leaves no transcript, a reload whose +state it cannot read, or the worker or the judge fails three times running. A worker fails when +claude exits non-zero, is killed, or ends without a result event; a judge when its verdict misses a +field or quotes nothing in the transcript. Exit 2: the human is needed and stdin or stdout is not a terminal, or the interrupt ended without them typing anything. Rerunning resumes any open session. @@ -390,10 +391,10 @@ def main(): state = reloaded_state(argv[1:]) else: state = {"cycle_number": 0, "failures": 0, "root": "", "task": " ".join(argv)} + if state["task"] in ("", "-h", "--help"): + print(__doc__.strip()) + sys.exit(0 if state["task"] else 1) task = state["task"] - if task in ("", "-h", "--help"): - print(__doc__.strip()) - sys.exit(0 if task else 1) root = state["root"] or checkout_root() cycle_number, failures = state["cycle_number"], state["failures"] while True: diff --git a/bin/test_loop_claude.py b/bin/test_loop_claude.py index 4473e6f0..84d78aa1 100644 --- a/bin/test_loop_claude.py +++ b/bin/test_loop_claude.py @@ -348,13 +348,10 @@ class Reload(unittest.TestCase): environment = dict(os.environ) with unittest.mock.patch.object(loop, "__file__", script), unittest.mock.patch("os.execv") as execv: loop.reload_if_edited("/r", "do it", cycle_number=1, failures=2) - execv.assert_called_once_with(sys.executable, [sys.executable, script, loop.RELOAD_FLAG, STATE]) + execv.assert_called_once_with(sys.executable, [sys.executable, script, loop.RELOAD_FLAG, json.dumps({"cycle_number": 1, "failures": 2, "root": "/r", "task": "do it"})]) self.assertEqual(dict(os.environ), environment) -STATE = json.dumps({"cycle_number": 1, "failures": 2, "root": "/r", "task": "do it"}) - - class Main(unittest.TestCase): def test_exits_naming_the_reload_when_it_hands_over_anything_but_the_state_with_a_task(self): for handed in (["3", "2", "/r", "do it"], ["3"], ["[1]"], [json.dumps({"cycle_number": 3, "failures": 0, "root": "/r"})], [json.dumps({"cycle_number": 3, "failures": 0, "root": "/r", "task": ""})]): diff --git a/todo.md b/todo.md index 8c17f21e..e38eb146 100644 --- a/todo.md +++ b/todo.md @@ -2,7 +2,7 @@ ## 0.1.0 -Bar the reload item, ordered by how many seats of the 2026-09-25 scoring panel (`docs/decisions.md`) named the place; each is one chunk. +Ordered by how many seats of the 2026-09-25 scoring panel (`docs/decisions.md`) named the place; each is one chunk. - Name `end_session` for what it does, today it deletes the session's transcript files. - Define looping reviewer and round cap under `CLAUDE.md` → "Vocabulary", today inline at `agents/review-protocol.md`. -- 2.52.0 From 99dd74c55bca517b48de3e75ad411796c2b14751 Mon Sep 17 00:00:00 2001 From: Lilleman auf Larv Date: Sat, 26 Sep 2026 13:42:32 +0200 Subject: [PATCH 3/3] Say the reload cannot read the loop's state in one wording across the message, --help and the changelog --- CHANGELOG.md | 2 +- bin/loop-claude | 10 +++++----- bin/test_loop_claude.py | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1d897379..79fde370 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,7 @@ - `loop-claude` archives each session's transcript under `~/.claude/loop-claude-transcripts/`, one folder per project; `LOOP_CLAUDE_TRANSCRIPTS` overrides the root. - `loop-claude` runs at the checkout root wherever inside the checkout it is started. - `loop-claude` says why when it cannot read `~/.claude.json` before an interrupt, and the interrupt then lands in Claude Code's trust dialog. -- `loop-claude` takes an edit to itself at the next cycle boundary once the edited file answers `--help`, and exits 1 naming the reload when the edited file cannot read the running loop's state. +- `loop-claude` takes an edit to itself at the next cycle boundary once the edited file answers `--help`, and exits 1 when the edited file cannot read the running loop's state. - `loop-claude --help` names the Claude Code version its transcript, session and trust handling were observed on; check it first when an upgrade breaks a cycle. - `loop-claude` runs the model each project's settings pick; `LOOP_CLAUDE_WORKER_MODEL` and `LOOP_CLAUDE_JUDGE_MODEL` override it. - `settings.json`, the agents and the status line run on any machine unedited; the status line shows reset times in the machine's own time zone. diff --git a/bin/loop-claude b/bin/loop-claude index bbc72cf6..36b307b3 100755 --- a/bin/loop-claude +++ b/bin/loop-claude @@ -10,10 +10,10 @@ interrupt: `claude --resume` on the terminal, every worktree first marked truste An unfinished chunk resumes the same session next cycle; a finished one 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, claude leaves no transcript, a reload whose -state it cannot read, or the worker or the judge fails three times running. A worker fails when -claude exits non-zero, is killed, or ends without a result event; a judge when its verdict misses a -field or quotes nothing in the transcript. +Exit 1: no task, not inside a checkout, no premises, claude leaves no transcript, a reload with +unreadable state, or the worker or the judge fails three times running. A worker fails when claude +exits non-zero, is killed, or ends without a result event; a judge when its verdict misses a field +or quotes nothing in the transcript. Exit 2: the human is needed and stdin or stdout is not a terminal, or the interrupt ended without them typing anything. Rerunning resumes any open session. @@ -381,7 +381,7 @@ def reload_if_edited(root, task, cycle_number, failures): def reloaded_state(handed): state = parse(handed[0]) if len(handed) == 1 else None if not state or {"cycle_number", "failures", "root"} - state.keys() or not isinstance(state.get("task"), str) or not state["task"]: - sys.exit(f"the reload handed {handed}, not the loop's state with a task; rerun loop-claude ") + sys.exit(f"the reload cannot read the loop's state from {handed}; rerun loop-claude ") return state diff --git a/bin/test_loop_claude.py b/bin/test_loop_claude.py index 84d78aa1..a1eb06ec 100644 --- a/bin/test_loop_claude.py +++ b/bin/test_loop_claude.py @@ -357,7 +357,7 @@ class Main(unittest.TestCase): for handed in (["3", "2", "/r", "do it"], ["3"], ["[1]"], [json.dumps({"cycle_number": 3, "failures": 0, "root": "/r"})], [json.dumps({"cycle_number": 3, "failures": 0, "root": "/r", "task": ""})]): with self.subTest(handed=handed), unittest.mock.patch.object(sys, "argv", ["loop-claude", loop.RELOAD_FLAG, *handed]), self.assertRaises(SystemExit) as stopped: loop.main() - self.assertEqual(stopped.exception.code, f"the reload handed {handed}, not the loop's state with a task; rerun loop-claude ") + self.assertEqual(stopped.exception.code, f"the reload cannot read the loop's state from {handed}; rerun loop-claude ") def test_resumes_the_root_and_counters_a_reload_handed_over(self): patches = [unittest.mock.patch.object(loop, name) for name in ("checkout_root", "read_premises", "reload_if_edited", "run_cycle")] -- 2.52.0