Answer the architect's first round
This commit is contained in:
+48
-48
@@ -1,5 +1,4 @@
|
||||
import dataclasses
|
||||
import io
|
||||
import json
|
||||
import os
|
||||
import pathlib
|
||||
@@ -38,16 +37,27 @@ def queued(prompt, kind):
|
||||
class FakeEffects:
|
||||
"""Effects that record each call and answer from a script, one answer per call."""
|
||||
|
||||
def __init__(self, runs=(), popens=(), inputs=(), on_terminal=True):
|
||||
def __init__(self, claude=(), on_terminal_claude=(), git=(), inputs=(), python=(), streams=(), on_terminal=True):
|
||||
self.calls = []
|
||||
self.runs, self.popens, self.inputs = list(runs), list(popens), list(inputs)
|
||||
self.scripts = {"claude": list(claude), "claude_on_terminal": list(on_terminal_claude), "git": list(git), "input": list(inputs), "python": list(python), "stream_claude": list(streams)}
|
||||
self.terminal = on_terminal
|
||||
|
||||
def effects(self):
|
||||
return Effects(execv=self.execv, input=self.input, on_terminal=lambda: self.terminal, popen=self.popen, run=self.run, sleep=self.sleep)
|
||||
return Effects(
|
||||
claude=lambda args, cwd, stdin: self.answer("claude", args),
|
||||
claude_on_terminal=lambda args, cwd: self.answer("claude_on_terminal", args),
|
||||
execv=self.execv,
|
||||
git=lambda args, cwd: self.answer("git", args),
|
||||
input=lambda prompt: self.answer("input", prompt),
|
||||
on_terminal=lambda: self.terminal,
|
||||
python=lambda args: self.answer("python", args),
|
||||
sleep=lambda seconds: self.calls.append(("sleep", seconds)),
|
||||
stream_claude=lambda args, cwd: self.answer("stream_claude", args),
|
||||
)
|
||||
|
||||
def answer(self, script, what):
|
||||
answer = script.pop(0)
|
||||
def answer(self, seam, what):
|
||||
self.calls.append((seam, what))
|
||||
answer = self.scripts[seam].pop(0)
|
||||
if isinstance(answer, BaseException):
|
||||
raise answer
|
||||
return answer(what) if callable(answer) else answer
|
||||
@@ -56,20 +66,8 @@ class FakeEffects:
|
||||
self.calls.append(("execv", args))
|
||||
raise OSError(2, "No such file or directory")
|
||||
|
||||
def input(self, prompt):
|
||||
self.calls.append(("input", prompt))
|
||||
return self.answer(self.inputs, prompt)
|
||||
|
||||
def popen(self, args, **options):
|
||||
self.calls.append(("popen", args))
|
||||
return self.answer(self.popens, args)
|
||||
|
||||
def run(self, args, **options):
|
||||
self.calls.append(("run", args))
|
||||
return self.answer(self.runs, args)
|
||||
|
||||
def sleep(self, seconds):
|
||||
self.calls.append(("sleep", seconds))
|
||||
def made(self, seam):
|
||||
return [what for called, what in self.calls if called == seam]
|
||||
|
||||
|
||||
def ran(stdout="", code=0, stderr=""):
|
||||
@@ -96,34 +94,31 @@ class Worker:
|
||||
return Streamed(self.code)
|
||||
|
||||
|
||||
class Streamed(io.StringIO):
|
||||
class Streamed:
|
||||
def __init__(self, code):
|
||||
super().__init__(json.dumps({"type": "result"}) + "\n")
|
||||
self.stdout, self.code = self, code
|
||||
self.code = code
|
||||
|
||||
def lines(self):
|
||||
return [json.dumps({"type": "result"}) + "\n"]
|
||||
|
||||
def wait(self):
|
||||
return self.code
|
||||
|
||||
|
||||
class Transitions(unittest.TestCase):
|
||||
def test_a_run_resumes_only_while_its_transcript_exists(self):
|
||||
self.assertEqual(states.opening(""), states.Fresh())
|
||||
self.assertEqual(states.opening("/t.jsonl"), states.Resuming())
|
||||
|
||||
def test_each_state_moves_on_its_events_and_only_on_them(self):
|
||||
for state, event, after in (
|
||||
(states.Fresh(), states.WorkerRan("/t"), states.Judging("/t")),
|
||||
(states.Resuming(), states.WorkerRan("/t"), states.Judging("/t")),
|
||||
(states.Working(), states.WorkerRan("/t"), states.Judging("/t")),
|
||||
(states.Judging("/t"), states.Ruled(Interrupt("e", "Which one?")), states.Interrupting("/t", "Which one?")),
|
||||
(states.Judging("/t"), states.Ruled(Finished("e")), states.Finishing("/t")),
|
||||
(states.Judging("/t"), states.Ruled(Unfinished("e")), states.Resuming()),
|
||||
(states.Judging("/t"), states.Ruled(Unfinished("e")), states.Ended()),
|
||||
(states.Interrupting("/t", "q"), states.Answered(), states.Judging("/t")),
|
||||
(states.Finishing("/t"), states.Archived(), states.Fresh()),
|
||||
(states.Finishing("/t"), states.Archived(), states.Ended()),
|
||||
):
|
||||
with self.subTest(state=state, event=event):
|
||||
self.assertEqual(states.transition(state, event), after)
|
||||
with self.assertRaises(ValueError):
|
||||
states.transition(states.Fresh(), states.Answered())
|
||||
states.transition(states.Working(), states.Answered())
|
||||
|
||||
|
||||
class Cycles(unittest.TestCase):
|
||||
@@ -141,17 +136,17 @@ class Cycles(unittest.TestCase):
|
||||
return sorted(path.read_text() for path in (self.paths.archive / "proj").glob("*.verdict.md"))
|
||||
|
||||
def test_a_finished_chunk_is_archived_and_its_session_deleted(self):
|
||||
fake = FakeEffects(popens=[Worker(self.paths.claude_code_transcripts.parent)], runs=[judged("finished")])
|
||||
fake = FakeEffects(streams=[Worker(self.paths.claude_code_transcripts.parent)], claude=[judged("finished")])
|
||||
self.assertIsNone(steps.run_cycle(self.cycle(fake)))
|
||||
self.assertEqual(self.archived_verdicts(), ["Task: Do it.\n\nVerdict: finished\n"])
|
||||
self.assertEqual(session.transcript_file(self.session), "")
|
||||
|
||||
def test_an_unfinished_chunk_keeps_its_session_and_the_next_cycle_resumes_it(self):
|
||||
fake = FakeEffects(popens=[Worker(self.paths.claude_code_transcripts.parent)] * 2, runs=[judged("unfinished")] * 2)
|
||||
fake = FakeEffects(streams=[Worker(self.paths.claude_code_transcripts.parent)] * 2, claude=[judged("unfinished")] * 2)
|
||||
self.assertIsNone(steps.run_cycle(self.cycle(fake)))
|
||||
self.assertTrue(session.transcript_file(self.session))
|
||||
self.assertIsNone(steps.run_cycle(self.cycle(fake)))
|
||||
workers = [args for kind, args in fake.calls if kind == "popen"]
|
||||
workers = fake.made("stream_claude")
|
||||
self.assertIn("--session-id", workers[0])
|
||||
self.assertIn("--resume", workers[1])
|
||||
self.assertFalse((self.paths.archive / "proj").exists())
|
||||
@@ -160,14 +155,15 @@ class Cycles(unittest.TestCase):
|
||||
def answer(args):
|
||||
with open(session.transcript_file(self.session), "a") as handle:
|
||||
handle.write(json.dumps(human("the second")) + "\n")
|
||||
return ran()
|
||||
fake = FakeEffects(popens=[Worker(self.paths.claude_code_transcripts.parent)], runs=[judged("interrupt"), ran(), answer, judged("unfinished")])
|
||||
return 0
|
||||
fake = FakeEffects(streams=[Worker(self.paths.claude_code_transcripts.parent)], claude=[judged("interrupt"), judged("unfinished")], on_terminal_claude=[answer], git=[ran()])
|
||||
self.assertIsNone(steps.run_cycle(self.cycle(fake)))
|
||||
self.assertEqual(self.archived_verdicts(), ["Task: Do it.\n\nVerdict: interrupt\n\nWhich one?\n"])
|
||||
self.assertEqual([args[0] for kind, args in fake.calls if kind == "run"], ["claude", "git", "claude", "claude"])
|
||||
self.assertIn(str(self.session.id), fake.made("claude_on_terminal")[0])
|
||||
self.assertEqual(len(fake.made("claude")), 2)
|
||||
|
||||
def test_stops_when_the_worker_leaves_no_transcript(self):
|
||||
fake = FakeEffects(popens=[lambda args: Streamed(0)])
|
||||
fake = FakeEffects(streams=[Streamed(0)])
|
||||
self.assertEqual(steps.run_cycle(self.cycle(fake)), Stop(1, f"claude left no transcript for session {self.session.id}"))
|
||||
|
||||
|
||||
@@ -189,9 +185,13 @@ class Interrupts(unittest.TestCase):
|
||||
self.assertEqual(stop, Stop(2, "the human is needed to answer the question above, but stdin or stdout is not a terminal; rerun loop-claude on a terminal"))
|
||||
self.assertEqual(fake.calls, [])
|
||||
|
||||
def test_counts_only_turns_typed_after_the_handover(self):
|
||||
self.assertFalse(interrupt.typed_since(1, [human("a")]))
|
||||
self.assertTrue(interrupt.typed_since(1, [human("a"), human("b")]))
|
||||
def test_stops_when_nothing_was_typed_in_the_handover(self):
|
||||
with tempfile.TemporaryDirectory() as home:
|
||||
path = pathlib.Path(home, "t.jsonl")
|
||||
path.write_text(json.dumps(human("a")) + "\n")
|
||||
fake = FakeEffects(on_terminal_claude=[0], git=[ran()])
|
||||
stop = interrupt.interrupt(home, uuid.uuid4(), "m", str(path), "Which one?", paths.paths_from(pathlib.Path(home), {}), fake.effects())
|
||||
self.assertEqual(stop, Stop(2, "the human is needed to answer the question above, but the interrupt ended with nothing typed"))
|
||||
|
||||
|
||||
class ModelPrompt(unittest.TestCase):
|
||||
@@ -208,7 +208,7 @@ class ModelPrompt(unittest.TestCase):
|
||||
with tempfile.TemporaryDirectory() as empty:
|
||||
fake = FakeEffects(inputs=["", "my-model"])
|
||||
self.assertEqual(models.ask_model("proj", "/m.json", pathlib.Path(empty), fake.effects()), "my-model")
|
||||
self.assertEqual(len(fake.calls), 2)
|
||||
self.assertEqual(len(fake.made("input")), 2)
|
||||
fake = FakeEffects(inputs=[EOFError()])
|
||||
self.assertEqual(models.ask_model("proj", "/m.json", pathlib.Path(empty), fake.effects()), Stop(2, "the human is needed to pick proj's model, but the input ended"))
|
||||
|
||||
@@ -313,11 +313,11 @@ class Verdict(unittest.TestCase):
|
||||
self.assertEqual(judge.verdict_from(None), Failure("the judge returned no verdict"))
|
||||
|
||||
|
||||
class LoopState(unittest.TestCase):
|
||||
class LoopRuns(unittest.TestCase):
|
||||
def test_stops_naming_the_reload_when_it_hands_over_anything_but_the_state_with_a_task(self):
|
||||
for handed in (["3", "2", "/r", "do it"], ["3"], ["[1]"], [json.dumps({"cycle_number": 3, "root": "/r"})], [json.dumps({"cycle_number": 3, "root": "/r", "task": ""})], [json.dumps({"cycle_number": "3", "root": "/r", "task": "do it"})], [json.dumps({"cycle_number": 3, "root": "", "task": "do it"})], [json.dumps({"cycle_number": 3, "root": "/r", "task": "do it", "turns": 1})], [json.dumps({"cycle_number": 3, "failures": 0, "root": "/r", "task": "do it"})]):
|
||||
with self.subTest(handed=handed):
|
||||
self.assertEqual(main.loop_state([main.RELOAD_FLAG, *handed], CWD, FakeEffects().effects()), Stop(1, f"the reload cannot read the loop's state from {handed}; rerun loop-claude <task>"))
|
||||
self.assertEqual(main.loop_run_from([main.RELOAD_FLAG, *handed], CWD, FakeEffects().effects()), Stop(1, f"the reload cannot read the loop's state from {handed}; rerun loop-claude <task>"))
|
||||
|
||||
def test_decodes_a_state_whose_annotations_are_strings(self):
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
@@ -331,13 +331,13 @@ class LoopState(unittest.TestCase):
|
||||
with tempfile.TemporaryDirectory() as root:
|
||||
launcher = pathlib.Path(root, "loop-claude")
|
||||
launcher.write_text("")
|
||||
fake = FakeEffects(runs=[ran()])
|
||||
self.assertEqual(reload.exec_if_edited(launcher, "stale", main.LoopState(1, "/r", "do it"), fake.effects()), Stop(1, "could not reload loop-claude: No such file or directory; rerun loop-claude <task>"))
|
||||
fake = FakeEffects(python=[ran()])
|
||||
self.assertEqual(reload.exec_if_edited(launcher, "stale", main.LoopRun(1, "/r", "do it"), fake.effects()), Stop(1, "could not reload loop-claude: No such file or directory; rerun loop-claude <task>"))
|
||||
|
||||
|
||||
class Checkouts(unittest.TestCase):
|
||||
def test_lists_the_root_alone_when_git_cannot_start(self):
|
||||
fake = FakeEffects(runs=[FileNotFoundError(2, "No such file or directory")])
|
||||
fake = FakeEffects(git=[FileNotFoundError(2, "No such file or directory")])
|
||||
self.assertEqual(checkout.checkouts("/r", fake.effects()), ["/r"])
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user