From a9ed0c641481b7533c25d8dd14d15b17c63d43e3 Mon Sep 17 00:00:00 2001 From: Lilleman auf Larv Date: Sun, 27 Sep 2026 16:47:44 +0200 Subject: [PATCH 1/3] Build and validate the reload state in one unit --- bin/loop-claude | 93 ++++++++++++++++++++++------------------- bin/test_loop_claude.py | 8 ++-- 2 files changed, 54 insertions(+), 47 deletions(-) diff --git a/bin/loop-claude b/bin/loop-claude index f7171a0f..5bf42cb5 100755 --- a/bin/loop-claude +++ b/bin/loop-claude @@ -42,6 +42,7 @@ Claude Code internals this rests on, observed on 2.1.282, the model catalog on 2 - ~/.claude/cache/model-catalog/*.json holds catalog.config.models, each with an id and a name; the newest file has the highest fetchedAt. """ +import dataclasses import glob import json import os @@ -90,29 +91,18 @@ JUDGE_SCHEMA = { def main(): - argv = sys.argv[1:] - if argv[:1] == [RELOAD_FLAG]: - 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"] - root = state["root"] or checkout_root() - cycle_number, failures = state["cycle_number"], state["failures"] + state = loop_state(sys.argv[1:]) while True: - reload_if_edited(root, task, cycle_number, failures) - cycle_number += 1 - premises = read_premises(root) - name = session_name(root) - print(f"\n=== cycle {cycle_number} ===", flush=True) - failure = run_cycle(root, task, premises, name, uuid.uuid5(SESSION_NAMESPACE, name), project_model(root)) + reload_if_edited(state) + state = dataclasses.replace(state, cycle_number=state.cycle_number + 1) + premises = read_premises(state.root) + name = session_name(state.root) + print(f"\n=== cycle {state.cycle_number} ===", flush=True) + failure = run_cycle(state.root, state.task, premises, name, uuid.uuid5(SESSION_NAMESPACE, name), project_model(state.root)) + state = dataclasses.replace(state, failures=state.failures + 1 if failure else 0) if not failure: - failures = 0 continue - failures += 1 - if failures == 3: + if state.failures == 3: sys.exit(f"the worker failed three times running: {failure}") print(f"retrying the worker in a minute: {failure}", file=sys.stderr) time.sleep(60) @@ -137,29 +127,6 @@ def run_cycle(root, task, premises, name, session_id, chosen): return "" -def reload_if_edited(root, task, cycle_number, failures): - try: - source = open(__file__, encoding="utf-8").read() - except OSError as failure: - print(f"keeping the running script: {failure}", file=sys.stderr) - return - if source == RELOAD_BASELINE: - return - trial = subprocess.run([sys.executable, __file__, "--help"], capture_output=True, text=True) - if trial.returncode != 0: - print(f"keeping the running script: {trial.stderr.strip() or f'exit {trial.returncode}'}", file=sys.stderr) - return - 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 cannot read the loop's state from {handed}; rerun loop-claude ") - return state - - def checkout_root(): found = subprocess.run(["git", "rev-parse", "--show-toplevel"], capture_output=True, text=True) if found.returncode != 0: @@ -250,6 +217,46 @@ def project(root): return root.removeprefix(os.path.expanduser("~/code/")).lstrip("/") +# The reload + + +@dataclasses.dataclass(frozen=True) +class LoopState: + cycle_number: int + failures: int + root: str + task: str + + +def loop_state(argv): + if argv[:1] != [RELOAD_FLAG]: + task = " ".join(argv) + if task in ("", "-h", "--help"): + print(__doc__.strip()) + sys.exit(0 if task else 1) + return LoopState(0, 0, checkout_root(), task) + handed = parse(argv[1]) if len(argv) == 2 else None + shape = {field.name: field.type for field in dataclasses.fields(LoopState)} + if not handed or handed.keys() != shape.keys() or any(type(handed[name]) is not kind for name, kind in shape.items()) or not (handed["root"] and handed["task"]): + sys.exit(f"the reload cannot read the loop's state from {argv[1:]}; rerun loop-claude ") + return LoopState(**handed) + + +def reload_if_edited(state): + try: + source = open(__file__, encoding="utf-8").read() + except OSError as failure: + print(f"keeping the running script: {failure}", file=sys.stderr) + return + if source == RELOAD_BASELINE: + return + trial = subprocess.run([sys.executable, __file__, "--help"], capture_output=True, text=True) + 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, json.dumps(dataclasses.asdict(state))]) + + # ~/.claude/loop-claude-models.json diff --git a/bin/test_loop_claude.py b/bin/test_loop_claude.py index 44876d00..5e87bef7 100644 --- a/bin/test_loop_claude.py +++ b/bin/test_loop_claude.py @@ -436,19 +436,19 @@ class Reload(unittest.TestCase): 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")) 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=0) + loop.reload_if_edited(loop.LoopState(1, 0, "/r", "do it")) execv.assert_not_called() open(script, "w").write(loop.RELOAD_BASELINE.replace("RELOAD_FLAG = ", "RELOAD_FLAG = \"x\" or ")) 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) + loop.reload_if_edited(loop.LoopState(1, 2, "/r", "do it")) 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) 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": ""})]): + 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": ""})], [json.dumps({"cycle_number": "3", "failures": 0, "root": "/r", "task": "do it"})], [json.dumps({"cycle_number": 3, "failures": 0, "root": "", "task": "do it"})], [json.dumps({"cycle_number": 3, "failures": 0, "root": "/r", "task": "do it", "turns": 1})]): 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 cannot read the loop's state from {handed}; rerun loop-claude ") @@ -464,7 +464,7 @@ class Main(unittest.TestCase): loop.main() mocks[0].assert_not_called() mocks[1].assert_called_once_with("/r") - mocks[2].assert_called_once_with("/r", "do it", 3, 2) + mocks[2].assert_called_once_with(loop.LoopState(3, 2, "/r", "do it")) self.assertEqual(mocks[3].call_args.args[0], "/r") mocks[4].assert_called_once_with("/r") self.assertEqual(mocks[3].call_args.args[-1], mocks[4].return_value) -- 2.52.0 From 3f614e0e92cd51a953ec70be588f751a1705dac0 Mon Sep 17 00:00:00 2001 From: Lilleman auf Larv Date: Sun, 27 Sep 2026 16:49:19 +0200 Subject: [PATCH 2/3] Name loop_state among the leaves that exit, and tick off the reload state --- todo.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/todo.md b/todo.md index 2c0115db..15883b47 100644 --- a/todo.md +++ b/todo.md @@ -6,8 +6,7 @@ Ships when a four-seat scoring run puts every dimension and the overall at 7.0 o Locality, 6.25: -- Build and validate the reload state in one unit, today spelled in `main`, `reload_if_edited` and `reloaded_state`. -- Return every failure to `main` and exit only there, today `run_cycle`, `reloaded_state`, `checkout_root`, `read_premises`, `judge` and the models file exit from leaves. +- Return every failure to `main` and exit only there, today `run_cycle`, `loop_state`, `checkout_root`, `read_premises`, `judge` and the models file exit from leaves. - Put the three-strikes rule in one place, today `main` for the worker and `judge` for the judge. - Say in `run_cycle` that the interrupt appends to the transcript under the loop and that `archive` skips a stamp already written, today inferred from `archive`. - Name each clause of `typed` for the entry kind it drops, so one edit cannot silently change what both `human_turns` and `render` see. -- 2.52.0 From b03480e0a29377c4c0fcc9d5f2db7c2624deef93 Mon Sep 17 00:00:00 2001 From: Lilleman auf Larv Date: Sun, 27 Sep 2026 16:50:20 +0200 Subject: [PATCH 3/3] Say what an older file does with the handover, and name ask_model among the leaves that exit --- docs/decisions.md | 2 +- todo.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/decisions.md b/docs/decisions.md index 4ba36ae2..035c4014 100644 --- a/docs/decisions.md +++ b/docs/decisions.md @@ -46,7 +46,7 @@ ## loop-claude: a reload into an older file is unsupported -2026-09-26, lilleman. Serves goal 4. A running loop whose `bin/loop-claude` on disk is rolled back to a version with an older handover dies with a traceback in that older file, which this version cannot change; the human who rolled it back reruns `loop-claude `. Valid while nobody but the developer runs it: other team members running it while it is still developed reopens this. +2026-09-26, lilleman. Serves goal 4. A running loop whose `bin/loop-claude` on disk is rolled back to a version with an older handover fails in that older file, or, older than the handover itself, runs `--reloaded ` as its task; this version cannot change either; the human who rolled it back reruns `loop-claude `. Valid while nobody but the developer runs it: other team members running it while it is still developed reopens this. ## loop-claude: a CR-only README.md is reported at line 1 diff --git a/todo.md b/todo.md index 15883b47..a654c891 100644 --- a/todo.md +++ b/todo.md @@ -6,7 +6,7 @@ Ships when a four-seat scoring run puts every dimension and the overall at 7.0 o Locality, 6.25: -- Return every failure to `main` and exit only there, today `run_cycle`, `loop_state`, `checkout_root`, `read_premises`, `judge` and the models file exit from leaves. +- Return every failure to `main` and exit only there, today `run_cycle`, `loop_state`, `checkout_root`, `read_premises`, `judge`, `ask_model` and the models file exit from leaves. - Put the three-strikes rule in one place, today `main` for the worker and `judge` for the judge. - Say in `run_cycle` that the interrupt appends to the transcript under the loop and that `archive` skips a stamp already written, today inferred from `archive`. - Name each clause of `typed` for the entry kind it drops, so one edit cannot silently change what both `human_turns` and `render` see. -- 2.52.0