Fix the round loop's judge retry, reload and archive modes in loop-claude
This commit is contained in:
+78
-5
@@ -27,7 +27,7 @@ def human(content):
|
||||
|
||||
class Sections(unittest.TestCase):
|
||||
def test_keeps_only_headed_sections_with_a_body(self):
|
||||
text = "# Title\n\nintro\n\n## Goals\n\n1. one\n2. two\n\n## Empty\n\n## Audience\nme\n"
|
||||
text = "# Title\n\nintro\n\n## Goals\n\n1. one\n2. two\n\n## Empty\n\n## Audience\nme\n# Appendix\nnot audience\n"
|
||||
self.assertEqual(loop.sections(text), {"Goals": "1. one\n2. two", "Audience": "me"})
|
||||
|
||||
|
||||
@@ -50,10 +50,25 @@ class HumanTurns(unittest.TestCase):
|
||||
human("<command-name>/model</command-name>"),
|
||||
{**human("peer message"), "isMeta": True},
|
||||
human([{"type": "text", "text": "[Request interrupted by user]"}]),
|
||||
human([]),
|
||||
assistant({"type": "text", "text": "said"}),
|
||||
]), 2)
|
||||
|
||||
|
||||
class Project(unittest.TestCase):
|
||||
def test_names_a_checkout_under_code_by_its_relative_path_and_any_other_by_its_absolute_one(self):
|
||||
with unittest.mock.patch.dict(os.environ, {"HOME": "/h"}):
|
||||
with unittest.mock.patch("os.getcwd", return_value="/h/code/a/b"):
|
||||
self.assertEqual(loop.session_name(), "A ~/code/a/b auto-mode")
|
||||
self.assertEqual(loop.project_path(), "code/a/b")
|
||||
with unittest.mock.patch("os.getcwd", return_value="/h/code"):
|
||||
self.assertEqual(loop.session_name(), "A ~/code auto-mode")
|
||||
self.assertEqual(loop.project_path(), "code")
|
||||
with unittest.mock.patch("os.getcwd", return_value="/srv/x"):
|
||||
self.assertEqual(loop.session_name(), "A /srv/x auto-mode")
|
||||
self.assertEqual(loop.project_path(), "srv/x")
|
||||
|
||||
|
||||
class WorkerArgs(unittest.TestCase):
|
||||
def test_a_fresh_round_names_the_session_and_asks_to_continue_open_work(self):
|
||||
args = loop.worker_args("the task", "A name", SESSION_ID, resuming=False)
|
||||
@@ -85,15 +100,18 @@ class JudgePrompt(unittest.TestCase):
|
||||
|
||||
|
||||
class Archive(unittest.TestCase):
|
||||
def test_keeps_the_copy_as_private_as_the_transcript(self):
|
||||
with tempfile.TemporaryDirectory() as root:
|
||||
def test_keeps_the_copy_and_every_folder_it_creates_as_private_as_the_transcript(self):
|
||||
with tempfile.TemporaryDirectory() as root, unittest.mock.patch("os.getcwd", return_value="/srv/a/b"):
|
||||
transcript = os.path.join(root, "session.jsonl")
|
||||
open(transcript, "w").write(json.dumps(assistant({"type": "text", "text": "said"})) + "\n")
|
||||
loop.ARCHIVE = os.path.join(root, "archive")
|
||||
loop.ARCHIVE = os.path.join(root, "deep", "archive")
|
||||
loop.archive(transcript, "note")
|
||||
[copy] = [os.path.join(folder, name) for folder, _, names in os.walk(loop.ARCHIVE) for name in names]
|
||||
self.assertEqual(stat.S_IMODE(os.stat(copy).st_mode), 0o600)
|
||||
self.assertEqual(stat.S_IMODE(os.stat(os.path.dirname(copy)).st_mode), 0o700)
|
||||
folder = os.path.dirname(copy)
|
||||
while folder != root:
|
||||
self.assertEqual(stat.S_IMODE(os.stat(folder).st_mode), 0o700, folder)
|
||||
folder = os.path.dirname(folder)
|
||||
self.assertIn("said", open(copy).read())
|
||||
|
||||
|
||||
@@ -139,5 +157,60 @@ class Verdict(unittest.TestCase):
|
||||
self.assertFalse(loop.complete(None))
|
||||
|
||||
|
||||
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")
|
||||
if "--json-schema" in args:
|
||||
plan = open(os.environ["STUB_VERDICTS"]).read().split()
|
||||
open(os.environ["STUB_VERDICTS"], "w").write(" ".join(plan[1:]))
|
||||
if plan[0] == "fail":
|
||||
sys.exit(1)
|
||||
print(json.dumps({"structured_output": {"ask": False, "evidence": "", "finished": plan[0] == "finished", "question": ""}}))
|
||||
else:
|
||||
flag = "--session-id" if "--session-id" in args else "--resume"
|
||||
folder = os.path.expanduser("~/.claude/projects/p")
|
||||
os.makedirs(folder, exist_ok=True)
|
||||
open(os.path.join(folder, args[args.index(flag) + 1] + ".jsonl"), "a").write(json.dumps({"type": "assistant", "message": {"content": [{"type": "text", "text": "worked"}]}}) + "\\n")
|
||||
print(json.dumps({"type": "result"}))
|
||||
"""
|
||||
|
||||
|
||||
class RunRound(unittest.TestCase):
|
||||
def test_resumes_until_the_judge_calls_the_chunk_finished_and_rejudges_a_judge_failure(self):
|
||||
with tempfile.TemporaryDirectory() as home:
|
||||
stub = os.path.join(home, "claude")
|
||||
open(stub, "w").write(STUB_CLAUDE)
|
||||
os.chmod(stub, 0o700)
|
||||
calls, verdicts = os.path.join(home, "calls"), os.path.join(home, "verdicts")
|
||||
open(verdicts, "w").write("fail continue finished continue")
|
||||
loop.ARCHIVE = os.path.join(home, "archive")
|
||||
environment = {"HOME": home, "PATH": f"{home}:{os.environ['PATH']}", "STUB_CALLS": calls, "STUB_VERDICTS": verdicts}
|
||||
with unittest.mock.patch.dict(os.environ, environment), unittest.mock.patch("time.sleep") as sleep:
|
||||
for _ in range(3):
|
||||
self.assertTrue(loop.run_round("task", "premises", "A name", SESSION_ID))
|
||||
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"])
|
||||
self.assertEqual(starts[1][2], SESSION_ID)
|
||||
self.assertEqual(sleep.call_count, 1)
|
||||
self.assertEqual(open(verdicts).read(), "")
|
||||
self.assertEqual(len(os.listdir(loop.ARCHIVE)), 1)
|
||||
|
||||
|
||||
class Reload(unittest.TestCase):
|
||||
def test_runs_the_edited_file_before_trusting_it(self):
|
||||
with tempfile.TemporaryDirectory() as root:
|
||||
script = os.path.join(root, "loop-claude")
|
||||
open(script, "w").write(loop.SOURCE.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(round_number=1, failures=0)
|
||||
execv.assert_not_called()
|
||||
open(script, "w").write(loop.SOURCE.replace("ROUND = ", "ROUND = \"x\" or "))
|
||||
with unittest.mock.patch.object(loop, "__file__", script), unittest.mock.patch("os.execv") as execv:
|
||||
loop.reload_if_edited(round_number=1, failures=2)
|
||||
execv.assert_called_once()
|
||||
self.assertEqual((os.environ.pop(loop.ROUND), os.environ.pop(loop.FAILURES)), ("1", "2"))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user