diff --git a/bin/loop-claude b/bin/loop-claude index 50748e4f..6d06abc1 100755 --- a/bin/loop-claude +++ b/bin/loop-claude @@ -45,8 +45,7 @@ import uuid ARCHIVE = os.path.expanduser(os.environ.get("LOOP_CLAUDE_TRANSCRIPTS", "~/.claude/loop-claude-transcripts")) RELOAD_BASELINE = open(__file__, encoding="utf-8").read() -RELOAD_CYCLE = "LOOP_CLAUDE_CYCLE" -RELOAD_FAILURES = "LOOP_CLAUDE_FAILURES" +RELOAD_FLAG = "--reloaded" SESSION_NAMESPACE = uuid.UUID("64fb586b-d926-4bb3-a44c-c903e86d3eec") JUDGE_RULES = ( @@ -346,7 +345,7 @@ def run_cycle(task, premises, name, session_id): return True -def reload_if_edited(cycle_number, failures): +def reload_if_edited(task, cycle_number, failures): try: source = open(__file__, encoding="utf-8").read() except OSError as failure: @@ -358,21 +357,21 @@ def reload_if_edited(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.environ[RELOAD_CYCLE] = str(cycle_number) - os.environ[RELOAD_FAILURES] = str(failures) - os.execv(sys.executable, [sys.executable, __file__, *sys.argv[1:]]) + os.execv(sys.executable, [sys.executable, __file__, RELOAD_FLAG, str(cycle_number), str(failures), task]) def main(): - task = " ".join(sys.argv[1:]) + argv = sys.argv[1:] + cycle_number, failures = 0, 0 + if argv[:1] == [RELOAD_FLAG]: + cycle_number, failures, argv = int(argv[1]), int(argv[2]), argv[3:] + task = " ".join(argv) if task in ("", "-h", "--help"): print(__doc__.strip()) sys.exit(0 if task else 1) os.chdir(checkout_root()) - cycle_number = int(os.environ.pop(RELOAD_CYCLE, 0)) - failures = int(os.environ.pop(RELOAD_FAILURES, 0)) while True: - reload_if_edited(cycle_number, failures) + reload_if_edited(task, cycle_number, failures) cycle_number += 1 premises = read_premises() name = session_name() diff --git a/bin/test_loop_claude.py b/bin/test_loop_claude.py index ee984536..05f1a469 100644 --- a/bin/test_loop_claude.py +++ b/bin/test_loop_claude.py @@ -8,6 +8,7 @@ import os import pathlib import stat import subprocess +import sys import tempfile import unittest import unittest.mock @@ -309,18 +310,34 @@ class RunCycle(unittest.TestCase): class Reload(unittest.TestCase): - def test_runs_the_edited_file_before_trusting_it(self): + def test_runs_the_edited_file_before_trusting_it_and_hands_it_the_counters_on_argv(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")) with unittest.mock.patch.object(loop, "__file__", script), unittest.mock.patch("os.execv") as execv: - loop.reload_if_edited(cycle_number=1, failures=0) + loop.reload_if_edited("do it", cycle_number=1, failures=0) execv.assert_not_called() - open(script, "w").write(loop.RELOAD_BASELINE.replace("RELOAD_CYCLE = ", "RELOAD_CYCLE = \"x\" or ")) + 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(cycle_number=1, failures=2) - execv.assert_called_once() - self.assertEqual((os.environ.pop(loop.RELOAD_CYCLE), os.environ.pop(loop.RELOAD_FAILURES)), ("1", "2")) + loop.reload_if_edited("do it", cycle_number=1, failures=2) + execv.assert_called_once_with(sys.executable, [sys.executable, script, loop.RELOAD_FLAG, "1", "2", "do it"]) + self.assertEqual(dict(os.environ), environment) + + +class Main(unittest.TestCase): + def test_resumes_the_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", "do it"])) + stack.enter_context(unittest.mock.patch.object(sys, "stdout", io.StringIO())) + mocks[0].return_value = os.getcwd() + mocks[3].side_effect = SystemExit(0) + with self.assertRaises(SystemExit): + loop.main() + mocks[2].assert_called_once_with("do it", 3, 2) + self.assertIn("=== cycle 4 ===", sys.stdout.getvalue()) if __name__ == "__main__": diff --git a/todo.md b/todo.md index 5b7bb780..4fad0218 100644 --- a/todo.md +++ b/todo.md @@ -4,7 +4,6 @@ Ordered by how many seats of the 2026-09-25 scoring panel (`docs/decisions.md`) named the place; each is one chunk. -- Hand the counters `reload_if_edited` carries across `os.execv` outside the `LOOP_CLAUDE_` prefix `--help` documents, today `LOOP_CLAUDE_CYCLE` and `LOOP_CLAUDE_FAILURES` a user can set by mistake. - Define a human turn at `typed` in one line and name what it tests, today the definition sits in the docstring 150 lines above. - Place `larv-review`'s Re-entry section after the passes it cites, today five forward references before ยง0. - State the fits-in-your-head thresholds once in `agents/systems-architect.md`, cited by heading, today echoed at seven places and cited as "step 4" by number at nine; fold the Quality Bar into the sections it restates.