Run the claude children at the root, and pin the root the tests hand on

This commit is contained in:
2026-09-25 18:11:27 +02:00
parent d64eaef9e7
commit 24ebeb0f94
2 changed files with 16 additions and 12 deletions
+8 -9
View File
@@ -261,8 +261,8 @@ def worker_args(task, name, session_id, resuming):
] ]
def run_worker(args): def run_worker(root, args):
proc = subprocess.Popen(["claude", *args], stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, text=True) proc = subprocess.Popen(["claude", *args], cwd=root, stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, text=True)
done = False done = False
for raw in proc.stdout: for raw in proc.stdout:
event = parse(raw) or {} 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 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 = [ args = [
"-p", "-p",
"--json-schema", json.dumps(JUDGE_SCHEMA), "--json-schema", json.dumps(JUDGE_SCHEMA),
@@ -309,7 +309,7 @@ def judge(task, premises, transcript):
] ]
prompt = judge_prompt(task, premises, transcript) prompt = judge_prompt(task, premises, transcript)
for attempt in range(3): 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 verdict = (parse(proc.stdout) or {}).get("structured_output") if proc.returncode == 0 else None
if not complete(verdict): if not complete(verdict):
failure = proc.stderr.strip() or "the judge returned no 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" return "stdin or stdout is not a terminal"
trust(checkouts(root)) trust(checkouts(root))
before = human_turns(entries(path)) 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" 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): 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 return failure
path = session_file(session_id) path = session_file(session_id)
if not path: if not path:
sys.exit(f"claude left no transcript for session {session_id}") 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"]: while verdict["ask"]:
archive(root, path, f"Task: {task}\n\nVerdict: needs input\n\n{verdict['question']}") archive(root, path, f"Task: {task}\n\nVerdict: needs input\n\n{verdict['question']}")
if failure := interrupt(root, session_id, path, verdict["question"]): if failure := interrupt(root, session_id, path, verdict["question"]):
print(failure, file=sys.stderr) print(failure, file=sys.stderr)
sys.exit(2) sys.exit(2)
verdict = judge(task, premises, render(entries(path))) verdict = judge(root, task, premises, render(entries(path)))
if verdict["finished"]: if verdict["finished"]:
archive(root, path, f"Task: {task}\n\nVerdict: finished") archive(root, path, f"Task: {task}\n\nVerdict: finished")
end_session(path) end_session(path)
@@ -377,7 +377,6 @@ def main():
print(__doc__.strip()) print(__doc__.strip())
sys.exit(0 if task else 1) sys.exit(0 if task else 1)
root = checkout_root() root = checkout_root()
os.chdir(root)
while True: while True:
reload_if_edited(task, cycle_number, failures) reload_if_edited(task, cycle_number, failures)
cycle_number += 1 cycle_number += 1
+8 -3
View File
@@ -100,6 +100,8 @@ class CheckoutRoot(unittest.TestCase):
os.makedirs("a/b") os.makedirs("a/b")
with contextlib.chdir("a/b"): with contextlib.chdir("a/b"):
self.assertEqual(loop.checkout_root(), root) self.assertEqual(loop.checkout_root(), root)
subprocess.run(["git", "init", "-q", "other"], check=True)
with contextlib.chdir("other"):
self.assertEqual(loop.checkouts(root), [root]) self.assertEqual(loop.checkouts(root), [root])
@@ -193,10 +195,12 @@ class Interrupt(unittest.TestCase):
with ( with (
unittest.mock.patch("sys.stdin.isatty", return_value=True), unittest.mock.patch("sys.stdin.isatty", return_value=True),
unittest.mock.patch("sys.stdout.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, unittest.mock.patch("subprocess.run") as run,
): ):
self.assertEqual(loop.interrupt(root, SESSION_ID, transcript, "q?"), "the interrupt ended without the human typing anything") 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") run.side_effect = lambda *args, **kwargs: open(transcript, "a").write(json.dumps(human("typed")) + "\n")
self.assertEqual(loop.interrupt(root, SESSION_ID, transcript, "q?"), "") self.assertEqual(loop.interrupt(root, SESSION_ID, transcript, "q?"), "")
@@ -257,7 +261,7 @@ class Verdict(unittest.TestCase):
STUB_CLAUDE = """#!/usr/bin/env python3 STUB_CLAUDE = """#!/usr/bin/env python3
import json, os, sys import json, os, sys
args = sys.argv[1:] 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: if "--json-schema" in args:
plan = open(os.environ["STUB_VERDICTS"]).read().split() plan = open(os.environ["STUB_VERDICTS"]).read().split()
open(os.environ["STUB_VERDICTS"], "w").write(" ".join(plan[1:])) open(os.environ["STUB_VERDICTS"], "w").write(" ".join(plan[1:]))
@@ -288,7 +292,7 @@ class RunWorker(unittest.TestCase):
for failure, script in scripts.items(): for failure, script in scripts.items():
open(stub, "w").write(f"#!/usr/bin/env python3\nimport json\nimport os\nimport sys\n{script}\n") open(stub, "w").write(f"#!/usr/bin/env python3\nimport json\nimport os\nimport sys\n{script}\n")
os.chmod(stub, 0o700) os.chmod(stub, 0o700)
self.assertEqual(loop.run_worker([]), failure) self.assertEqual(loop.run_worker(bin, []), failure)
class RunCycle(unittest.TestCase): class RunCycle(unittest.TestCase):
@@ -319,6 +323,7 @@ class RunCycle(unittest.TestCase):
if plan.startswith("unquoted"): if plan.startswith("unquoted"):
self.assertIn("asking: q?", stopped.exception.code) self.assertIn("asking: q?", stopped.exception.code)
self.assertEqual(open(verdicts).read(), "") 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] 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([start[1] for start in starts], ["--name", "--resume", "--name", "--resume", "--resume", "--resume"])
self.assertEqual(starts[1][2], SESSION_ID) self.assertEqual(starts[1][2], SESSION_ID)