Answer the architecture pass's first round
This commit is contained in:
+49
-38
@@ -19,7 +19,7 @@ from loop_claude.cycle.verdicts import Finished, Interrupt, Unfinished
|
||||
from loop_claude.effects import Effects
|
||||
from loop_claude.failure import Failure
|
||||
from loop_claude.loop import main, reload
|
||||
from loop_claude.project import checkout, models, premises
|
||||
from loop_claude.project import checkout, model_choice, premises
|
||||
from loop_claude.stop import Stop
|
||||
|
||||
CWD = pathlib.Path("/c")
|
||||
@@ -145,7 +145,7 @@ class Cycles(unittest.TestCase):
|
||||
self.session = session.session_for("proj", self.paths)
|
||||
|
||||
def cycle(self, fake):
|
||||
return run.Cycle(effects=fake.effects(), models=models.Models("judge-model", "worker-model"), paths=self.paths, premises="## Goals", project_key="proj", root=str(self.home), session=self.session, task="Do it.")
|
||||
return run.Cycle(effects=fake.effects(), models=model_choice.Models("judge-model", "worker-model"), paths=self.paths, premises="## Goals", project_key="proj", root=str(self.home), session=self.session, task="Do it.")
|
||||
|
||||
def archived_verdicts(self):
|
||||
return sorted(path.read_text() for path in (self.paths.transcript_archive / "proj").glob("*.verdict.md"))
|
||||
@@ -228,19 +228,19 @@ class ModelPrompt(unittest.TestCase):
|
||||
OFFERED = [("claude-opus-5-5", "Opus 5.5"), ("claude-haiku-4-5", "Haiku 4.5")]
|
||||
|
||||
def test_reads_a_number_as_the_offered_model_and_anything_else_as_a_name(self):
|
||||
self.assertEqual(models.model_picked(" 2 ", self.OFFERED), "claude-haiku-4-5")
|
||||
self.assertEqual(models.model_picked("opus", self.OFFERED), "opus")
|
||||
self.assertEqual(models.model_picked("3", []), "3")
|
||||
self.assertEqual(models.model_picked("3", self.OFFERED), Failure("pick 1 to 2"))
|
||||
self.assertIsNone(models.model_picked(" ", self.OFFERED))
|
||||
self.assertEqual(model_choice.model_picked(" 2 ", self.OFFERED), "claude-haiku-4-5")
|
||||
self.assertEqual(model_choice.model_picked("opus", self.OFFERED), "opus")
|
||||
self.assertEqual(model_choice.model_picked("3", []), "3")
|
||||
self.assertEqual(model_choice.model_picked("3", self.OFFERED), Failure("pick 1 to 2"))
|
||||
self.assertIsNone(model_choice.model_picked(" ", self.OFFERED))
|
||||
|
||||
def test_asks_again_until_an_answer_picks_a_model(self):
|
||||
with tempfile.TemporaryDirectory() as empty:
|
||||
fake = FakeEffects(inputs=["", "my-model"])
|
||||
self.assertEqual(models.ask_for_model("proj", pathlib.Path("/m.json"), pathlib.Path(empty), fake.effects()), "my-model")
|
||||
self.assertEqual(model_choice.ask_for_model("proj", pathlib.Path("/m.json"), pathlib.Path(empty), fake.effects()), "my-model")
|
||||
self.assertEqual(len(fake.made("input")), 2)
|
||||
fake = FakeEffects(inputs=[EOFError()])
|
||||
self.assertEqual(models.ask_for_model("proj", pathlib.Path("/m.json"), pathlib.Path(empty), fake.effects()), Stop(2, "the human is needed to pick proj's model, but the input ended"))
|
||||
self.assertEqual(model_choice.ask_for_model("proj", pathlib.Path("/m.json"), pathlib.Path(empty), fake.effects()), Stop(2, "the human is needed to pick proj's model, but the input ended"))
|
||||
|
||||
|
||||
class WorkerArgs(unittest.TestCase):
|
||||
@@ -307,9 +307,9 @@ class PartLine(unittest.TestCase):
|
||||
|
||||
class Parse(unittest.TestCase):
|
||||
def test_yields_only_json_objects(self):
|
||||
self.assertEqual(entries.parse('{"type": "user"}'), {"type": "user"})
|
||||
self.assertIsNone(entries.parse("not json"))
|
||||
self.assertIsNone(entries.parse("[1, 2]"))
|
||||
self.assertEqual(entries.parse_entry('{"type": "user"}'), {"type": "user"})
|
||||
self.assertIsNone(entries.parse_entry("not json"))
|
||||
self.assertIsNone(entries.parse_entry("[1, 2]"))
|
||||
|
||||
|
||||
class MarkTrusted(unittest.TestCase):
|
||||
@@ -368,40 +368,51 @@ class LoopRuns(unittest.TestCase):
|
||||
|
||||
class ExitContract(unittest.TestCase):
|
||||
def test_every_exit_1_stop_has_its_clause_in_help(self):
|
||||
read, task, git, premises_ = "a transcript cannot be read or archived", "no task is given", "git cannot start or finds no checkout", "README.md holds no premises"
|
||||
models_, retries = "the models file cannot be read or written", "the worker or the judge fails three times running"
|
||||
# Per function, the --help clause of each Stop it returns with an exit code other than 0, 2 or 130, in source order.
|
||||
clauses = {
|
||||
("cycle/interrupt.py", "interrupt"): (read, read, "claude cannot start for an interrupt"),
|
||||
("cycle/retry.py", "retried"): (retries,),
|
||||
("cycle/run.py", "ask"): (read,),
|
||||
("cycle/run.py", "close"): (read, "a finished chunk's session cannot be deleted"),
|
||||
("cycle/run.py", "rule"): (read,),
|
||||
("cycle/run.py", "work"): ("claude leaves no transcript",),
|
||||
("loop/main.py", "loop_run_from"): (task, "a reload cannot read the cycle number, checkout root and task"),
|
||||
("loop/reload.py", "exec_if_edited"): ("python cannot start for a reload",),
|
||||
("project/checkout.py", "checkout_root"): (git, git),
|
||||
("project/models.py", "read_models_file"): (models_, models_),
|
||||
("project/models.py", "saved_model"): (models_,),
|
||||
("project/premises.py", "read_premises"): (premises_,) * 5,
|
||||
}
|
||||
read, git, premises_ = "a transcript cannot be read or archived", "git cannot start or finds no checkout", "README.md holds no premises"
|
||||
models_ = "the models file cannot be read or written"
|
||||
# Each Stop with an exit code other than 0, 2 or 130, in source order, by the start of its reason, with its --help clause.
|
||||
clauses = [
|
||||
("cycle/interrupt.py", "interrupt", "entries.reason", read),
|
||||
("cycle/interrupt.py", "interrupt", "f'could not start claude for the interrupt:", "claude cannot start for an interrupt"),
|
||||
("cycle/interrupt.py", "interrupt", "f'could not tell whether you typed anything:", read),
|
||||
("cycle/retry.py", "retried", "f'the {who} failed three times running:", "the worker or the judge fails three times running"),
|
||||
("cycle/run.py", "work", "f'claude left no transcript for session", "claude leaves no transcript"),
|
||||
("cycle/run.py", "rule", "entries.reason", read),
|
||||
("cycle/run.py", "ask", "failure.reason", read),
|
||||
("cycle/run.py", "close", "failure.reason", read),
|
||||
("cycle/run.py", "close", "failure.reason", "a finished chunk's session cannot be deleted"),
|
||||
("loop/main.py", "loop_run_from", "__doc__.strip()", "no task is given"),
|
||||
("loop/main.py", "loop_run_from", "f'the reload cannot read the cycle number,", "a reload cannot read the cycle number, checkout root and task"),
|
||||
("loop/reload.py", "exec_if_edited", "f'could not reload loop-claude:", "python cannot start for a reload"),
|
||||
("project/checkout.py", "checkout_root", "f'could not start git:", git),
|
||||
("project/checkout.py", "checkout_root", "found.stderr.strip()", git),
|
||||
("project/model_choice.py", "get_or_ask_model", "f'could not save the model in", models_),
|
||||
("project/model_choice.py", "read_models_file", 'f"could not read {saved}: {getattr(', models_),
|
||||
("project/model_choice.py", "read_models_file", "f'could not read {saved}: not an object", models_),
|
||||
("project/premises.py", "read_premises", "f'README.md in {root}:", premises_),
|
||||
("project/premises.py", "read_premises", "f'README.md is not UTF-8", premises_),
|
||||
("project/premises.py", "read_premises", "f'README.md has no ##", premises_),
|
||||
("project/premises.py", "read_premises", "f'README.md has ## {name} more than once", premises_),
|
||||
("project/premises.py", "read_premises", "f'README.md has nothing under", premises_),
|
||||
]
|
||||
root = pathlib.Path(main.__file__).parents[1]
|
||||
found = {}
|
||||
found = []
|
||||
for path in sorted(root.rglob("*.py")):
|
||||
for function in ast.walk(ast.parse(path.read_text())):
|
||||
if isinstance(function, ast.FunctionDef):
|
||||
stops = [call for call in ast.walk(function) if isinstance(call, ast.Call) and getattr(call.func, "id", None) == "Stop" and not (isinstance(call.args[0], ast.Constant) and call.args[0].value in (0, 2, 130))]
|
||||
if stops:
|
||||
found[(str(path.relative_to(root)), function.name)] = len(stops)
|
||||
self.assertEqual(found, {site: len(said) for site, said in clauses.items()})
|
||||
functions = [node for node in ast.walk(ast.parse(path.read_text())) if isinstance(node, ast.FunctionDef)]
|
||||
stops = [(call.lineno, function.name, ast.unparse(call.args[1])) for function in functions for call in ast.walk(function) if isinstance(call, ast.Call) and getattr(call.func, "id", None) == "Stop" and not (isinstance(call.args[0], ast.Constant) and call.args[0].value in (0, 2, 130))]
|
||||
found += [(str(path.relative_to(root)), function, reason) for _, function, reason in sorted(stops)]
|
||||
self.assertEqual(len(found), len(clauses), found)
|
||||
for (path, function, reason), (want_path, want_function, reason_start, _) in zip(found, clauses):
|
||||
self.assertEqual((path, function), (want_path, want_function))
|
||||
self.assertTrue(reason.startswith(reason_start), f"{path} {function}: {reason}")
|
||||
help_exit_1 = main.__doc__[main.__doc__.index("Exit 1"):main.__doc__.index("Exit 2")]
|
||||
self.assertEqual([clause for said in clauses.values() for clause in said if f"- {clause}" not in help_exit_1], [])
|
||||
self.assertEqual([clause for *_, clause in clauses if f"- {clause}" not in help_exit_1], [])
|
||||
|
||||
|
||||
class Checkouts(unittest.TestCase):
|
||||
def test_lists_the_root_alone_when_git_cannot_start(self):
|
||||
fake = FakeEffects(git=[FileNotFoundError(2, "No such file or directory")])
|
||||
self.assertEqual(checkout.checkouts("/r", fake.effects()), ["/r"])
|
||||
self.assertEqual(checkout.worktrees("/r", fake.effects()), ["/r"])
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user