diff --git a/bin/test_loop_claude_black_box.py b/bin/test_loop_claude_black_box.py index c98d30d3..66410f87 100644 --- a/bin/test_loop_claude_black_box.py +++ b/bin/test_loop_claude_black_box.py @@ -44,17 +44,23 @@ def session(): return argv[argv.index(flag) + 1] +def transcript(): + return pathlib.Path.home() / ".claude" / "projects" / "-fake" / f"{{session()}}.jsonl" + + def append(*entries): - path = pathlib.Path.home() / ".claude" / "projects" / "-fake" / f"{{session()}}.jsonl" - path.parent.mkdir(parents=True, exist_ok=True) - with open(path, "a") as transcript: - transcript.writelines(json.dumps(entry) + "\\n" for entry in entries) + transcript().parent.mkdir(parents=True, exist_ok=True) + with open(transcript(), "a") as handle: + handle.writelines(json.dumps(entry) + "\\n" for entry in entries) if role == "worker": said = {{"type": "assistant", "message": {{"content": [{{"type": "text", "text": step.get("say", "working")}}]}}}} if step.get("transcript", True): - append({{"type": "user", "promptSource": "sdk", "message": {{"content": argv[argv.index("-p") + 1]}}}}, said) + append({{"type": "user", "promptSource": "sdk", "message": {{"content": argv[argv.index("-p") + 1]}}}}) + with open(transcript(), "ab") as handle: + handle.write(step.get("raw", "").encode("latin-1")) + append(said) if "append" in step: with open(os.environ["FAKE_CLAUDE_EDIT"], "a") as script: script.write(step["append"]) @@ -71,6 +77,8 @@ elif role == "judge": print(step.get("stderr", ""), file=sys.stderr) elif "type" in step: append({{"type": "user", "message": {{"content": step["type"]}}}}) +if step.get("unreadable"): + transcript().chmod(0) sys.exit(step.get("exit", 0)) """ @@ -311,6 +319,31 @@ class Cycles(unittest.TestCase): self.assertEqual(run.roles(), ["worker", "judge", "judge", "worker"]) self.assertIn("the judge quoted 'never said', nothing in the transcript, for the question: A or B?", run.stderr) + def test_skips_a_malformed_transcript_line(self): + checkout = Checkout(self) + raw = "\xff not UTF-8\nnot JSON\n[1]\n" + json.dumps({"type": "assistant", "message": "not an object"}) + "\n" + json.dumps({"type": "user", "message": None}) + "\n" + run = checkout.run([worker(raw=raw, say="past the noise"), judge()]) + self.assertEqual((run.code, run.roles()), (130, ["worker", "judge", "worker"])) + self.assertIn("past the noise", run.calls[1]["stdin"]) + + @unittest.skipIf(os.geteuid() == 0, "root reads an unreadable file") + def test_exits_1_when_the_transcript_cannot_be_read(self): + checkout = Checkout(self) + run = checkout.run([worker(unreadable=True)]) + self.assertEqual((run.code, run.roles()), (1, ["worker"])) + self.assertRegex(run.stderr.strip().splitlines()[-1], r"^could not read the transcript .*\.jsonl: Permission denied$") + + def test_retries_a_worker_claude_cannot_start(self): + checkout = Checkout(self) + only_git = checkout.temp / "only-git" + only_git.mkdir() + (only_git / "git").symlink_to(shutil.which("git")) + checkout.env["PATH"] = str(only_git) + run = checkout.run([]) + self.assertEqual((run.code, run.calls), (1, [])) + self.assertEqual(run.sleeps, [60, 60]) + self.assertEqual(run.stderr.strip().splitlines()[-1], "the worker failed three times running: could not start claude: No such file or directory") + def test_exits_1_when_claude_leaves_no_transcript(self): checkout = Checkout(self) run = checkout.run([worker(transcript=False)]) @@ -359,6 +392,13 @@ class Interrupt(unittest.TestCase): self.assertEqual((run.code, run.roles()), (2, ["worker", "judge", "interrupt"])) self.assertEqual(run.stderr.strip().splitlines()[-1], "the interrupt ended without the human typing anything") + @unittest.skipIf(os.geteuid() == 0, "root reads an unreadable file") + def test_exits_1_when_the_transcript_cannot_be_read_after_the_interrupt(self): + checkout = Checkout(self) + run = checkout.run([worker(say="A or B?"), judge(interrupt=True, evidence="A or B?", question="A or B?"), interrupt(type="B", unreadable=True)], terminal_input="") + self.assertEqual((run.code, run.roles()), (1, ["worker", "judge", "interrupt"])) + self.assertRegex(run.stderr.strip().splitlines()[-1], r"^could not tell whether you typed anything: could not read the transcript .*\.jsonl: Permission denied$") + class Models(unittest.TestCase): def catalog(self, checkout):