From 24ebeb0f9434983e4455b235c474587b69240e5a Mon Sep 17 00:00:00 2001 From: Lilleman auf Larv Date: Fri, 25 Sep 2026 18:11:27 +0200 Subject: [PATCH] Run the claude children at the root, and pin the root the tests hand on --- bin/loop-claude | 17 ++++++++--------- bin/test_loop_claude.py | 11 ++++++++--- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/bin/loop-claude b/bin/loop-claude index e3112eff..e02f07a1 100755 --- a/bin/loop-claude +++ b/bin/loop-claude @@ -261,8 +261,8 @@ def worker_args(task, name, session_id, resuming): ] -def run_worker(args): - proc = subprocess.Popen(["claude", *args], stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, text=True) +def run_worker(root, args): + proc = subprocess.Popen(["claude", *args], cwd=root, stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, text=True) done = False for raw in proc.stdout: event = parse(raw) or {} @@ -297,7 +297,7 @@ def grounded(verdict, transcript): return not verdict["ask"] or bool(verdict["evidence"]) and verdict["evidence"] in transcript -def judge(task, premises, transcript): +def judge(root, task, premises, transcript): args = [ "-p", "--json-schema", json.dumps(JUDGE_SCHEMA), @@ -309,7 +309,7 @@ def judge(task, premises, transcript): ] prompt = judge_prompt(task, premises, transcript) for attempt in range(3): - proc = subprocess.run(["claude", *args], capture_output=True, input=prompt, text=True) + proc = subprocess.run(["claude", *args], capture_output=True, cwd=root, input=prompt, text=True) verdict = (parse(proc.stdout) or {}).get("structured_output") if proc.returncode == 0 else None if not complete(verdict): failure = proc.stderr.strip() or "the judge returned no verdict" @@ -329,23 +329,23 @@ def interrupt(root, session_id, path, question): return "stdin or stdout is not a terminal" trust(checkouts(root)) before = human_turns(entries(path)) - subprocess.run(["claude", "--permission-mode", "bypassPermissions", "--resume", str(session_id)]) + subprocess.run(["claude", "--permission-mode", "bypassPermissions", "--resume", str(session_id)], cwd=root) return "" if human_turns(entries(path)) > before else "the interrupt ended without the human typing anything" def run_cycle(root, task, premises, name, session_id): - if failure := run_worker(worker_args(task, name, session_id, resuming=session_open(session_id))): + if failure := run_worker(root, worker_args(task, name, session_id, resuming=session_open(session_id))): return failure path = session_file(session_id) if not path: sys.exit(f"claude left no transcript for session {session_id}") - verdict = judge(task, premises, render(entries(path))) + verdict = judge(root, task, premises, render(entries(path))) while verdict["ask"]: archive(root, path, f"Task: {task}\n\nVerdict: needs input\n\n{verdict['question']}") if failure := interrupt(root, session_id, path, verdict["question"]): print(failure, file=sys.stderr) sys.exit(2) - verdict = judge(task, premises, render(entries(path))) + verdict = judge(root, task, premises, render(entries(path))) if verdict["finished"]: archive(root, path, f"Task: {task}\n\nVerdict: finished") end_session(path) @@ -377,7 +377,6 @@ def main(): print(__doc__.strip()) sys.exit(0 if task else 1) root = checkout_root() - os.chdir(root) while True: reload_if_edited(task, cycle_number, failures) cycle_number += 1 diff --git a/bin/test_loop_claude.py b/bin/test_loop_claude.py index cfc62731..5f10464a 100644 --- a/bin/test_loop_claude.py +++ b/bin/test_loop_claude.py @@ -100,6 +100,8 @@ class CheckoutRoot(unittest.TestCase): os.makedirs("a/b") with contextlib.chdir("a/b"): self.assertEqual(loop.checkout_root(), root) + subprocess.run(["git", "init", "-q", "other"], check=True) + with contextlib.chdir("other"): self.assertEqual(loop.checkouts(root), [root]) @@ -193,10 +195,12 @@ class Interrupt(unittest.TestCase): with ( unittest.mock.patch("sys.stdin.isatty", return_value=True), unittest.mock.patch("sys.stdout.isatty", return_value=True), - unittest.mock.patch.multiple(loop, checkouts=unittest.mock.DEFAULT, trust=unittest.mock.DEFAULT), + unittest.mock.patch.multiple(loop, checkouts=unittest.mock.DEFAULT, trust=unittest.mock.DEFAULT) as mocks, unittest.mock.patch("subprocess.run") as run, ): self.assertEqual(loop.interrupt(root, SESSION_ID, transcript, "q?"), "the interrupt ended without the human typing anything") + mocks["checkouts"].assert_called_with(root) + self.assertEqual(run.call_args.kwargs["cwd"], root) run.side_effect = lambda *args, **kwargs: open(transcript, "a").write(json.dumps(human("typed")) + "\n") self.assertEqual(loop.interrupt(root, SESSION_ID, transcript, "q?"), "") @@ -257,7 +261,7 @@ class Verdict(unittest.TestCase): STUB_CLAUDE = """#!/usr/bin/env python3 import json, os, sys args = sys.argv[1:] -open(os.environ["STUB_CALLS"], "a").write(json.dumps(args) + "\\n") +open(os.environ["STUB_CALLS"], "a").write(json.dumps([os.getcwd(), *args]) + "\\n") if "--json-schema" in args: plan = open(os.environ["STUB_VERDICTS"]).read().split() open(os.environ["STUB_VERDICTS"], "w").write(" ".join(plan[1:])) @@ -288,7 +292,7 @@ class RunWorker(unittest.TestCase): for failure, script in scripts.items(): open(stub, "w").write(f"#!/usr/bin/env python3\nimport json\nimport os\nimport sys\n{script}\n") os.chmod(stub, 0o700) - self.assertEqual(loop.run_worker([]), failure) + self.assertEqual(loop.run_worker(bin, []), failure) class RunCycle(unittest.TestCase): @@ -319,6 +323,7 @@ class RunCycle(unittest.TestCase): if plan.startswith("unquoted"): self.assertIn("asking: q?", stopped.exception.code) self.assertEqual(open(verdicts).read(), "") + self.assertEqual({json.loads(line)[0] for line in open(calls)}, {home}) starts = [args[args.index("-p") + 1:][:3] for args in map(json.loads, open(calls)) if "--json-schema" not in args] self.assertEqual([start[1] for start in starts], ["--name", "--resume", "--name", "--resume", "--resume", "--resume"]) self.assertEqual(starts[1][2], SESSION_ID)