Carry the reload state as one JSON argument and exit naming a reload without a task #56

Merged
lilleman merged 3 commits from reload-json-state into main 2026-09-26 13:44:57 +02:00
4 changed files with 34 additions and 17 deletions
+1
View File
@@ -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 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.
+22 -11
View File
@@ -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, 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.
@@ -373,19 +374,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 cannot read the loop's state from {handed}; rerun loop-claude <task>")
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)
if task in ("", "-h", "--help"):
print(__doc__.strip())
sys.exit(0 if task else 1)
root = root or checkout_root()
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"]
while True:
reload_if_edited(root, task, cycle_number, failures)
cycle_number += 1
+10 -4
View File
@@ -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,22 @@ 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, 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": ""})]):
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 <task>")
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 +380,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:
+1 -2
View File
@@ -2,9 +2,8 @@
## 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.
- 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.