Carry the root across a reload; the tests tell it from the cwd

This commit is contained in:
2026-09-25 18:15:38 +02:00
parent 24ebeb0f94
commit 0d2bea9af9
2 changed files with 17 additions and 17 deletions
+6 -6
View File
@@ -352,7 +352,7 @@ def run_cycle(root, task, premises, name, session_id):
return ""
def reload_if_edited(task, cycle_number, failures):
def reload_if_edited(root, task, cycle_number, failures):
try:
source = open(__file__, encoding="utf-8").read()
except OSError as failure:
@@ -364,21 +364,21 @@ def reload_if_edited(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), task])
os.execv(sys.executable, [sys.executable, __file__, RELOAD_FLAG, str(cycle_number), str(failures), root, task])
def main():
argv = sys.argv[1:]
cycle_number, failures = 0, 0
cycle_number, failures, root = 0, 0, ""
if argv[:1] == [RELOAD_FLAG]:
cycle_number, failures, argv = int(argv[1]), int(argv[2]), argv[3:]
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 = checkout_root()
root = root or checkout_root()
while True:
reload_if_edited(task, cycle_number, failures)
reload_if_edited(root, task, cycle_number, failures)
cycle_number += 1
premises = read_premises(root)
name = session_name(root)
+11 -11
View File
@@ -331,38 +331,38 @@ class RunCycle(unittest.TestCase):
class Reload(unittest.TestCase):
def test_runs_the_edited_file_before_trusting_it_and_hands_it_the_counters_on_argv(self):
def test_runs_the_edited_file_before_trusting_it_and_hands_it_the_root_and_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("do it", cycle_number=1, failures=0)
loop.reload_if_edited("/r", "do it", cycle_number=1, failures=0)
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("do it", cycle_number=1, failures=2)
execv.assert_called_once_with(sys.executable, [sys.executable, script, loop.RELOAD_FLAG, "1", "2", "do it"])
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"])
self.assertEqual(dict(os.environ), environment)
class Main(unittest.TestCase):
def test_resumes_the_counters_a_reload_handed_over(self):
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", "do it"]))
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, "stdout", io.StringIO()))
mocks[0].return_value = os.getcwd()
mocks[3].side_effect = SystemExit(0)
with self.assertRaises(SystemExit):
loop.main()
mocks[1].assert_called_once_with(os.getcwd())
mocks[2].assert_called_once_with("do it", 3, 2)
self.assertEqual(mocks[3].call_args.args[0], os.getcwd())
mocks[0].assert_not_called()
mocks[1].assert_called_once_with("/r")
mocks[2].assert_called_once_with("/r", "do it", 3, 2)
self.assertEqual(mocks[3].call_args.args[0], "/r")
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", "do it"]))
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, "stderr", io.StringIO()))
sleep = stack.enter_context(unittest.mock.patch("time.sleep"))
with self.assertRaises(SystemExit) as stopped: