Give the reload and the strike their own modules

This commit is contained in:
2026-09-28 11:11:23 +02:00
parent 5de7e0fc9c
commit 4171a1ecbe
9 changed files with 88 additions and 69 deletions
+17 -16
View File
@@ -15,10 +15,11 @@ import uuid
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent))
from loop_claude import checkout, cycle, main, premises
from loop_claude import checkout, cycle, main, premises, reload
from loop_claude.outcome import Continue, Retry, Stop
from loop_claude.paths import paths_from
CWD = pathlib.Path("/c")
LAUNCHER = pathlib.Path(__file__).resolve().with_name("loop-claude")
PATHS = paths_from(pathlib.Path("/h"), {})
SESSION_ID = str(getattr(uuid, "uuid7", uuid.uuid4)())
@@ -111,11 +112,11 @@ class CheckoutRoot(unittest.TestCase):
def test_is_the_checkout_from_any_directory_inside_it_and_exits_outside_one(self):
with tempfile.TemporaryDirectory() as tmp, contextlib.chdir(tmp):
root = os.getcwd()
self.assertEqual(checkout.checkout_root().code, 1)
self.assertEqual(checkout.checkout_root(os.getcwd()).code, 1)
subprocess.run(["git", "init", "-q"], check=True)
os.makedirs("a/b")
with contextlib.chdir("a/b"):
self.assertEqual(checkout.checkout_root(), root)
self.assertEqual(checkout.checkout_root(os.getcwd()), root)
subprocess.run(["git", "init", "-q", "other"], check=True)
with contextlib.chdir("other"):
self.assertEqual(checkout.checkouts(root), [root])
@@ -439,14 +440,14 @@ class Reload(unittest.TestCase):
shutil.copytree(LAUNCHER.parent, copy, ignore=shutil.ignore_patterns("__pycache__", "test_*"))
launcher, edited = copy / "loop-claude", copy / "loop_claude" / "cycle.py"
state = main.LoopState(1, 2, "/r", "do it")
with unittest.mock.patch.object(main, "__file__", str(copy / "loop_claude" / "main.py")), unittest.mock.patch("os.execv") as execv, contextlib.redirect_stderr(io.StringIO()) as err:
baseline = main.fingerprint(launcher)
with unittest.mock.patch("os.execv") as execv, contextlib.redirect_stderr(io.StringIO()) as err:
baseline = reload.fingerprint(launcher)
edited.write_text(edited.read_text() + "\ndef\n")
main.reload_if_edited(launcher, baseline, state)
reload.reload_if_edited(launcher, baseline, state)
execv.assert_not_called()
self.assertIn("keeping the running script", err.getvalue())
edited.write_text(edited.read_text().replace("\ndef\n", "\n# edited\n"))
main.reload_if_edited(launcher, baseline, state)
reload.reload_if_edited(launcher, baseline, state)
execv.assert_called_once_with(sys.executable, [sys.executable, str(launcher), main.RELOAD_FLAG, json.dumps({"cycle_number": 1, "failures": 2, "root": "/r", "task": "do it"})])
@@ -454,17 +455,17 @@ class Main(unittest.TestCase):
def test_returns_the_exit_code_of_the_stop_the_loop_returned(self):
for argv, code, stream in ((["--help"], 0, "stdout"), ([], 1, "stderr"), ([main.RELOAD_FLAG, "3"], 1, "stderr")):
with self.subTest(argv=argv), contextlib.redirect_stdout(io.StringIO()) as out, contextlib.redirect_stderr(io.StringIO()) as err:
self.assertEqual(main.main(argv, {}, PATHS, LAUNCHER), code)
self.assertEqual({"stdout": out, "stderr": err}[stream].getvalue(), main.loop_state(argv).reason + "\n")
self.assertEqual(main.main(argv, {}, PATHS, LAUNCHER, CWD), code)
self.assertEqual({"stdout": out, "stderr": err}[stream].getvalue(), main.loop_state(argv, CWD).reason + "\n")
with unittest.mock.patch.object(main, "run_loop", side_effect=KeyboardInterrupt):
self.assertEqual(main.main(["t"], {}, PATHS, LAUNCHER), 130)
self.assertEqual(main.main(["t"], {}, PATHS, LAUNCHER, CWD), 130)
class RunLoop(unittest.TestCase):
def test_stops_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": ""})], [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):
self.assertEqual(main.loop_state([main.RELOAD_FLAG, *handed]), Stop(1, f"the reload cannot read the loop's state from {handed}; rerun loop-claude <task>"))
self.assertEqual(main.loop_state([main.RELOAD_FLAG, *handed], CWD), Stop(1, 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(main, name) for name in ("checkout_root", "read_premises", "reload_if_edited", "run_cycle", "project_model")]
@@ -473,7 +474,7 @@ class RunLoop(unittest.TestCase):
mocks = [stack.enter_context(patch) for patch in patches]
stack.enter_context(unittest.mock.patch.object(sys, "stdout", io.StringIO()))
mocks[3].return_value = stopped
self.assertEqual(main.run_loop([main.RELOAD_FLAG, json.dumps({"cycle_number": 3, "failures": 2, "root": "/r", "task": "do it"})], {}, PATHS, LAUNCHER), stopped)
self.assertEqual(main.run_loop([main.RELOAD_FLAG, json.dumps({"cycle_number": 3, "failures": 2, "root": "/r", "task": "do it"})], {}, PATHS, LAUNCHER, CWD), stopped)
mocks[0].assert_not_called()
mocks[1].assert_called_once_with("/r")
self.assertEqual(mocks[2].call_args.args[2], main.LoopState(3, 2, "/r", "do it"))
@@ -481,19 +482,19 @@ class RunLoop(unittest.TestCase):
mocks[4].assert_called_once_with("/r", PATHS, {})
self.assertEqual(mocks[3].call_args.args[3], mocks[4].return_value)
mocks[0].return_value = "/s"
self.assertEqual(main.run_loop(["do", "it"], {}, PATHS, LAUNCHER), stopped)
mocks[0].assert_called_once_with()
self.assertEqual(main.run_loop(["do", "it"], {}, PATHS, LAUNCHER, CWD), stopped)
mocks[0].assert_called_once_with(CWD)
mocks[1].assert_called_with("/s")
self.assertIn("=== cycle 4 ===", sys.stdout.getvalue())
for leaf in (0, 1, 4):
with self.subTest(leaf=leaf):
mocks[leaf].return_value = Stop(2, f"leaf {leaf}")
self.assertEqual(main.run_loop(["do it"], {}, PATHS, LAUNCHER), mocks[leaf].return_value)
self.assertEqual(main.run_loop(["do it"], {}, PATHS, LAUNCHER, CWD), mocks[leaf].return_value)
mocks[leaf].return_value = "/s"
mocks[3].side_effect = [Retry("claude exited 3")] * 3
stack.enter_context(unittest.mock.patch.object(sys, "stderr", io.StringIO()))
sleep = stack.enter_context(unittest.mock.patch("time.sleep"))
self.assertEqual(main.run_loop([main.RELOAD_FLAG, json.dumps({"cycle_number": 3, "failures": 0, "root": "/r", "task": "do it"})], {}, PATHS, LAUNCHER), Stop(1, "the worker failed three times running: claude exited 3"))
self.assertEqual(main.run_loop([main.RELOAD_FLAG, json.dumps({"cycle_number": 3, "failures": 0, "root": "/r", "task": "do it"})], {}, PATHS, LAUNCHER, CWD), Stop(1, "the worker failed three times running: claude exited 3"))
self.assertIn("retrying the worker in a minute: claude exited 3", sys.stderr.getvalue())
self.assertEqual(sleep.call_count, 2)