diff --git a/bin/agent_cycle/test_run.py b/bin/agent_cycle/test_run.py index 64440dc0..99f2320a 100644 --- a/bin/agent_cycle/test_run.py +++ b/bin/agent_cycle/test_run.py @@ -80,6 +80,13 @@ class Cycles(unittest.TestCase): self.assertEqual(run.run_cycle(self.cycle(fake)).verdict, Finished("working")) self.assertEqual(self.archived_outcomes(), [f"Task: Do it.\n\n2026-09-21T14:13:20Z Verdict: finished\n\nEvidence:\n> working\n\nConfig: {CONFIG}\nJudge: judge-model\nWorker: {OFF_LADDER}\n"]) + def test_says_where_it_archived_the_transcript(self): + fake = FakeEffects(streams=[Worker(self.paths.claude_code_transcripts.parent)], claude=[judged("finished")]) + with contextlib.redirect_stderr(io.StringIO()) as said: + run.run_cycle(self.cycle(fake)) + copy, = self.archive.glob("*/*.jsonl") + self.assertIn(f"transcript: {copy}\n", said.getvalue()) + def test_archives_the_cost_the_judges_reply_records_beside_its_verdict(self): fake = FakeEffects(streams=[Worker(self.paths.claude_code_transcripts.parent)], claude=[judged("finished", cost=0.25)]) self.assertEqual(run.run_cycle(self.cycle(fake)).verdict, Finished("working", judged=Judged(cost=0.25))) diff --git a/bin/atlassian/test_jira.py b/bin/atlassian/test_jira.py index 5133e6e7..180ed046 100644 --- a/bin/atlassian/test_jira.py +++ b/bin/atlassian/test_jira.py @@ -24,6 +24,13 @@ class TheHeadOfAnEntry(unittest.TestCase): self.assertEqual(found, Entry("ABC-1", {"issuelinks": [BLOCKED, outward, relates]}, (Link("ABC-2", "9"),), {"L": 2})) self.assertEqual(entry_of(raw(), "plan"), Entry("ABC-1", {"issuelinks": []}, (), {})) + def test_finds_a_blocked_by_link_listed_after_a_link_of_another_kind(self): + outward = {"id": "3", "type": {"inward": "is blocked by"}, "outwardIssue": {"key": "ABC-3"}} + relates = {"id": "4", "type": {"inward": "relates to"}, "inwardIssue": {"key": "ABC-4"}} + for other in (outward, relates): + with self.subTest(other=other): + self.assertEqual(jira.blocked_by_links({"issuelinks": [other, BLOCKED]}), (Link("ABC-2", "9"),)) + def test_an_entry_without_an_issue_key_is_a_failure(self): for wrong in ([], {}, {"key": 7}, {"key": "abc"}): with self.subTest(wrong=wrong): diff --git a/bin/chunk_driver/project/test_model_choice.py b/bin/chunk_driver/project/test_model_choice.py index c7ea56c9..54562e26 100644 --- a/bin/chunk_driver/project/test_model_choice.py +++ b/bin/chunk_driver/project/test_model_choice.py @@ -1,5 +1,7 @@ +import contextlib import dataclasses import fcntl +import io import json import pathlib import sys @@ -24,6 +26,52 @@ class Overridden(unittest.TestCase): self.assertEqual(model_choice.overridden({"LOOP_CLAUDE_JUDGE_MODEL": "j", "LOOP_CLAUDE_WORKER_MODEL": "w"}, "saved"), model_choice.Models("j", "w")) +class ModelsOfARun(unittest.TestCase): + def setUp(self): + temp = tempfile.TemporaryDirectory() + self.addCleanup(temp.cleanup) + self.paths = paths_from(pathlib.Path(temp.name)) + self.paths.models_file.parent.mkdir() + + def test_a_run_that_names_both_models_reads_no_models_file_and_asks_nothing(self): + self.paths.models_file.write_text("not json") + fake = FakeEffects() + env = {"LOOP_CLAUDE_JUDGE_MODEL": "j", "LOOP_CLAUDE_WORKER_MODEL": "w"} + self.assertEqual(model_choice.get_or_ask_models("proj", self.paths, env, LOOP_CLAUDE, fake.effects()), model_choice.Models("j", "w")) + self.assertEqual(fake.calls, []) + + def test_a_run_that_names_one_model_takes_the_other_from_the_models_file(self): + self.paths.models_file.write_text(json.dumps({"proj": "saved"})) + fake = FakeEffects() + self.assertEqual(model_choice.get_or_ask_models("proj", self.paths, {"LOOP_CLAUDE_JUDGE_MODEL": "j"}, LOOP_CLAUDE, fake.effects()), model_choice.Models("j", "saved")) + self.assertEqual(model_choice.get_or_ask_models("proj", self.paths, {"LOOP_CLAUDE_WORKER_MODEL": "w"}, LOOP_CLAUDE, fake.effects()), model_choice.Models("saved", "w")) + self.assertEqual(fake.calls, []) + + def test_a_run_that_names_one_model_asks_for_the_projects_where_none_is_saved_and_saves_the_pick(self): + fake, said = FakeEffects(inputs=["my-model"]), io.StringIO() + with contextlib.redirect_stderr(said): + models = model_choice.get_or_ask_models("proj", self.paths, {"LOOP_CLAUDE_JUDGE_MODEL": "j"}, LOOP_CLAUDE, fake.effects()) + self.assertEqual(models, model_choice.Models("j", "my-model")) + self.assertEqual(json.loads(self.paths.models_file.read_text()), {"proj": "my-model"}) + self.assertIn(" proj runs on my-model\n", said.getvalue()) + + def test_off_a_terminal_it_stops_naming_the_driver_to_rerun_and_the_file_to_merge_the_model_into(self): + fake = FakeEffects(on_terminal=False) + stop = model_choice.get_or_ask_models("proj", self.paths, {}, LOOP_CLAUDE, fake.effects()) + assert isinstance(stop, Stop) + self.assertEqual(stop.code, 2) + self.assertIn(f"pick proj's model, or merge {{\"proj\": \"\"}} into {self.paths.models_file}", stop.reason) + self.assertIn(LOOP_CLAUDE, stop.reason) + self.assertFalse(self.paths.models_file.exists()) + + def test_stops_with_exit_1_where_the_models_file_maps_no_projects_to_model_names(self): + for held in ("[]", '{"proj": 5}', '{"proj": ""}'): + with self.subTest(held=held): + self.paths.models_file.write_text(held) + stop = model_choice.get_or_ask_models("proj", self.paths, {}, LOOP_CLAUDE, FakeEffects().effects()) + self.assertEqual(stop, Stop(1, f"could not read {self.paths.models_file}: not an object mapping projects to model names")) + + class SavingAModel(unittest.TestCase): def setUp(self): temp = tempfile.TemporaryDirectory() @@ -37,6 +85,12 @@ class SavingAModel(unittest.TestCase): self.assertIsNone(model_choice.save_model("proj", "model-b", self.paths, replace_file)) self.assertEqual(json.loads(self.paths.models_file.read_text()), {"other": "model-x", "proj": "model-b"}) + def test_writes_the_file_for_a_human_to_merge_into_a_project_per_tab_indented_line_in_the_order_of_their_keys(self): + self.paths.models_file.parent.mkdir() + self.paths.models_file.write_text(json.dumps({"zebra": "model-z", "other": "model-x"})) + self.assertIsNone(model_choice.save_model("proj", "model-b", self.paths, replace_file)) + self.assertEqual(self.paths.models_file.read_text(), '{\n\t"other": "model-x",\n\t"proj": "model-b",\n\t"zebra": "model-z"\n}') + def lock_is_held(self) -> bool: with open(self.lock, "a") as other: try: @@ -91,6 +145,8 @@ class ModelPrompt(unittest.TestCase): def test_reads_a_number_as_the_offered_model_and_anything_else_as_a_name(self): self.assertEqual(model_choice.model_picked(" 2 ", self.OFFERED), "claude-haiku-4-5") + self.assertEqual(model_choice.model_picked("1", self.OFFERED), "claude-opus-5-5") + self.assertEqual(model_choice.model_picked("0", self.OFFERED), Failure("pick 1 to 2")) 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")) diff --git a/bin/chunk_driver/test_chunk_run.py b/bin/chunk_driver/test_chunk_run.py index 1479dd10..fd991ba9 100644 --- a/bin/chunk_driver/test_chunk_run.py +++ b/bin/chunk_driver/test_chunk_run.py @@ -20,6 +20,7 @@ from text_files.file_writes import replace_file from model_routing.route import Route from agent_cycle import run from claude_code import session +from agent_cycle.stop import Stop OPUS_PROJECT = ProjectRung(read_ladder(SHIPPED), Rung("claude-opus-5-5", "medium")) @@ -71,6 +72,33 @@ class Steps(unittest.TestCase): stop = chunk_run.closed_cycle(chunk_run.ChunkRun(cycle_number=1, task="Do it.", unfinished_evidence=""), OPUS_PROJECT, run.Concluded(Unfinished("working", taken_in=TakenIn(highest_likelihood=4)), before), blocked, "a-driver", a_loop(FakeEffects().effects(), session=opened)) self.assertEqual((stop.code, stop.reason.split(":")[0]), (1, "could not archive the transcript")) + def outcomes_after(self, unfinished_run: int, taken_in: TakenIn) -> tuple[object, str]: + """What closed_cycle answers for a-driver's chunk `Do it.`, after an unfinished verdict that follows unfinished_run others, and the outcomes it archived for a transcript not archived before.""" + with tempfile.TemporaryDirectory() as home: + opened = session.session_named("A proj a-driver", pathlib.Path(home, "projects")) + archive = pathlib.Path(home, "archive") + transcript = pathlib.Path(home, "projects", "-fake", f"{opened.id}.jsonl") + transcript.parent.mkdir(parents=True) + transcript.write_text(json.dumps({"type": "queue-operation", "timestamp": "1"}) + "\n") + before = Route(self.FABLE_LOW, 3, 3, 4) + with contextlib.redirect_stderr(io.StringIO()): + closed = chunk_run.closed_cycle(chunk_run.ChunkRun(cycle_number=1, task="Do it.", unfinished_evidence="", unfinished_run=unfinished_run), OPUS_PROJECT, run.Concluded(Unfinished("working", taken_in=taken_in), before), archive, "a-driver", a_loop(FakeEffects().effects(), session=opened)) + return closed, "".join(file.read_text() for file in archive.rglob("*") if file.is_file() and file.suffix != ".jsonl") + + def test_a_step_up_archived_first_for_its_transcript_names_the_chunks_task(self): + closed, outcomes_kept = self.outcomes_after(0, TakenIn(highest_likelihood=4)) + self.assertEqual(closed.route.rung, self.FABLE_MEDIUM) + self.assertIn("Do it.\n", outcomes_kept) + + def test_the_sixth_unfinished_verdict_running_stops_for_the_human_naming_the_driver_to_rerun_and_archives_why_with_the_task(self): + reason = "the human is needed to go on with the chunk, but the judge ruled it unfinished 6 cycles running; finish the chunk yourself, or rerun a-driver to give it 6 more cycles" + closed, outcomes_kept = self.outcomes_after(5, TakenIn()) + self.assertEqual(closed, Stop(2, reason)) + self.assertIn("Do it.\n", outcomes_kept) + self.assertIn(f"Stopped: {reason}\n", outcomes_kept) + closed, outcomes_kept = self.outcomes_after(4, TakenIn()) + self.assertEqual((closed.unfinished_run, outcomes_kept), (5, "")) + def test_archives_a_higher_e_plus_i_taken_in_so_a_rerun_keeps_it(self): with tempfile.TemporaryDirectory() as home: opened = session.session_named("A proj a-driver", pathlib.Path(home, "projects")) diff --git a/bin/chunk_driver/test_cycle_start.py b/bin/chunk_driver/test_cycle_start.py index 0358d9f0..182f87f3 100644 --- a/bin/chunk_driver/test_cycle_start.py +++ b/bin/chunk_driver/test_cycle_start.py @@ -1,4 +1,5 @@ import contextlib +import dataclasses import functools import io import json @@ -12,16 +13,23 @@ import unittest sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) from chunk_driver.doubles import a_loop +from project_config.doubles import configured +from agent_cycle.run import Cycle from chunk_driver import cycle_start from failure import Failure from loop_effects.doubles import FakeEffects, NOW, STAMP from git_cli import git_repo +from chunk_driver.project.model_choice import Models from transcript_archive import outcomes, write +from loop_state.paths import paths_from from model_routing.ladder import ProjectRung, Rung from model_routing.ladder_file import read_ladder, SHIPPED from text_files.file_writes import replace_file from model_routing.route import Route, WorkerLine from claude_code import session +from agent_cycle.stop import Stop +from agent_cycle.verdicts import TakenIn +from agent_cycle.worker_prompts import WorkerPrompts OPUS_PROJECT = ProjectRung(read_ladder(SHIPPED), Rung("claude-opus-5-5", "medium")) @@ -49,6 +57,100 @@ class SetupVersion(unittest.TestCase): self.assertEqual(cycle_start.setup_version(pathlib.Path("/r"), fake_git), "unknown: fatal: bad HEAD hint: repair it") +class ProjectRouting(unittest.TestCase): + def setUp(self): + temp = tempfile.TemporaryDirectory() + self.addCleanup(temp.cleanup) + self.home = pathlib.Path(temp.name) + self.paths = paths_from(self.home) + self.checkout = self.home / "code" / "acme" + self.checkout.mkdir(parents=True) + self.config = configured(self, self.checkout, home=self.home) + self.paths.models_file.parent.mkdir(exist_ok=True) + + def routing(self, fake=None, env=None): + return cycle_start.project_routing(a_loop((fake or FakeEffects()).effects(), root=str(self.checkout), paths=self.paths, env=env or {}), "a-driver") + + def test_routes_on_the_projects_saved_model_and_names_the_shipped_ladder_or_the_projects_own_as_its_file(self): + self.paths.models_file.write_text(json.dumps({"acme": "claude-opus-5-5"})) + shipped = read_ladder(SHIPPED) + self.assertEqual(self.routing(), cycle_start.Routing(Models("claude-opus-5-5", "claude-opus-5-5"), shipped, SHIPPED)) + self.assertEqual(self.routing(env={"LOOP_CLAUDE_JUDGE_MODEL": "j"}), cycle_start.Routing(Models("j", "claude-opus-5-5"), shipped, SHIPPED)) + own = self.config / "routing-ladder.json" + own.write_text('[{"model": "claude-fable-5-2", "effort": "low"}]', encoding="utf-8") + routing = self.routing() + assert isinstance(routing, cycle_start.Routing) + self.assertEqual((routing.ladder_file, [str(rung) for rung in routing.ladder.rungs]), (own, ["claude-fable-5-2 at low effort"])) + + def test_stops_with_exit_1_naming_the_ladder_file_that_cannot_be_read(self): + self.paths.models_file.write_text(json.dumps({"acme": "claude-opus-5-5"})) + own = self.config / "routing-ladder.json" + own.write_text("not json", encoding="utf-8") + stop = self.routing() + assert isinstance(stop, Stop) + self.assertEqual(stop.code, 1) + self.assertIn(str(own), stop.reason) + + def test_stops_with_exit_1_naming_the_config_that_cannot_be_read(self): + self.paths.models_file.write_text(json.dumps({"acme": "claude-opus-5-5"})) + (self.config / "config.toml").write_text("not toml", encoding="utf-8") + stop = self.routing() + assert isinstance(stop, Stop) + self.assertEqual(stop.code, 1) + self.assertIn(str(self.config / "config.toml"), stop.reason) + + def test_asks_for_the_model_where_none_is_saved_and_off_a_terminal_stops_naming_the_driver_to_rerun(self): + stop = self.routing(FakeEffects(on_terminal=False)) + assert isinstance(stop, Stop) + self.assertEqual(stop.code, 2) + self.assertIn("pick acme's model", stop.reason) + self.assertIn("rerun a-driver on a terminal", stop.reason) + with contextlib.redirect_stderr(io.StringIO()): + self.assertEqual(self.routing(FakeEffects(inputs=["claude-opus-5-5"])), cycle_start.Routing(Models("claude-opus-5-5", "claude-opus-5-5"), read_ladder(SHIPPED), SHIPPED)) + + +class CycleStart(unittest.TestCase): + ROUTING = cycle_start.Routing(Models("judge-model", "claude-opus-5-5"), OPUS_PROJECT.ladder, SHIPPED) + + def test_announces_the_route_the_chunks_run_holds_and_else_the_one_its_archive_last_names(self): + def never(): + raise AssertionError("not asked") + + kept = Route(Rung("claude-opus-5-5", "xhigh"), 3, 4, 5) + with tempfile.TemporaryDirectory() as home: + opened = session.session_named("A proj a-driver", pathlib.Path(home, "projects")) + loop = a_loop(FakeEffects().effects(), session=opened) + archive = pathlib.Path(home, "archive") + with contextlib.redirect_stdout(io.StringIO()) as said: + self.assertEqual(cycle_start.announced_route(4, kept, self.ROUTING, archive, never, loop), kept) + self.assertEqual(said.getvalue(), f"\n{STAMP} === cycle 4, worker on claude-opus-5-5 at xhigh effort ===\n") + transcript = pathlib.Path(home, "projects", "-fake", f"{opened.id}.jsonl") + transcript.parent.mkdir(parents=True) + transcript.write_text(json.dumps({"type": "queue-operation", "timestamp": "1"}) + "\n") + with contextlib.redirect_stderr(io.StringIO()): + self.assertIsNone(write.archive(archive, str(transcript), "Do it.", outcomes.unfinished(kept.worker_line(), "working", "j", ""), NOW, replace=replace_file)) + with contextlib.redirect_stdout(io.StringIO()): + self.assertEqual(cycle_start.announced_route(1, None, self.ROUTING, archive, never, loop), kept) + + def test_builds_the_cycle_from_the_loop_the_routing_and_what_the_entry_point_knows(self): + asked = [] + + def git(*args, cwd=None): + asked.append(cwd) + return "v0.1.0\n" if args[0] == "describe" else "abc\n" + + effects = dataclasses.replace(FakeEffects().effects(), git=git) + loop, route, archive = a_loop(effects), Route(Rung("claude-opus-5-5", "xhigh"), 3, 4, 5), pathlib.Path("/t/r") + prompts = WorkerPrompts("a-driver", lambda *args: "fresh", lambda *args: "resumed", lambda *args: "", "the sources", "the destinations", "the records named") + def taken_in(entries): + return TakenIn() + + cycle = cycle_start.cycle_of(loop, self.ROUTING, route, archive, task="the task", facts="the facts", premises="the premises", records="the records", unfinished_evidence="the evidence", prompts=prompts, taken_in=taken_in) + self.assertEqual(cycle, Cycle(config="v0.1.0 (abc)", effects=effects, facts="the facts", judge=Rung("judge-model"), premises="the premises", project=OPUS_PROJECT, project_archive=archive, records="the records", root="/r", + route=route, session=loop.session, task="the task", prompts=prompts, taken_in=taken_in, unfinished_evidence="the evidence", window=loop.window)) + self.assertEqual(asked, ["/h/.claude", "/h/.claude"]) + + class Routes(unittest.TestCase): OPUS = Rung("claude-opus-5-5", "medium") SONNET = Rung("claude-sonnet-5-5", "high") diff --git a/bin/do_epic/main.py b/bin/do_epic/main.py index f7f21ef9..977e5f07 100644 --- a/bin/do_epic/main.py +++ b/bin/do_epic/main.py @@ -268,7 +268,6 @@ def chunk_run(key: str, epic: EpicRun) -> Stop: if isinstance(cycle, EpicReopened): say(f"the epic {epic.key} holds an open issue again, so its finishing chunk ends until none is open", epic.effects, stdout=True) return handed_on(key, epic) - run = dataclasses.replace(run, task=cycle.task) concluded = run_cycle(cycle) if isinstance(concluded, Stop): return concluded diff --git a/bin/do_epic/test_main.py b/bin/do_epic/test_main.py index 76a1f4df..346e6737 100644 --- a/bin/do_epic/test_main.py +++ b/bin/do_epic/test_main.py @@ -193,7 +193,7 @@ class ACycleOfAChunkWithoutARepo(DoEpicTest): self.assertEqual(code, 1) self.assertRegex(err, f"could not make the chunk's folder {self.home}/.local/state/do-epic/ABC-12: \\w") - def cycle(self, env: dict[str, str] | None = None) -> main.Cycle: + def cycle(self, env: dict[str, str] | None = None, run: ChunkRun = ChunkRun(1)) -> main.Cycle: fake = FakeEffects(git=[Failure("no git"), Failure("no git")]) paths = paths_from(self.home) epic = self.epic_run(fake, env) @@ -203,7 +203,7 @@ class ACycleOfAChunkWithoutARepo(DoEpicTest): self.assertIsNone(main.folder_made(root)) loop = Loop(root=root, session=main.session_named("A ABC-12 do-epic ABC-21", paths.claude_code_transcripts), window=tmux.Window("epic-ABC-12", "ABC-21", "%1"), paths=paths, env=env or {}, launcher=pathlib.Path("/b/do-epic"), effects=fake.effects()) with contextlib.redirect_stdout(io.StringIO()): - found = main.next_cycle(ChunkRun(1), loop, repo is not None, epic) + found = main.next_cycle(run, loop, repo is not None, epic) assert isinstance(found, main.Cycle), found return found @@ -216,6 +216,16 @@ class ACycleOfAChunkWithoutARepo(DoEpicTest): self.assertEqual(self.cycle({"LOOP_CLAUDE_WORKER_MODEL": "off-the-ladder"}).route, Route(Rung("off-the-ladder"))) self.assertEqual(self.cycle({"LOOP_CLAUDE_JUDGE_MODEL": "claude-fable-5-2"}).judge.model, "claude-fable-5-2") + def test_keeps_the_route_and_the_evidence_its_run_holds(self) -> None: + kept = Route(Rung("claude-opus-5-5", "xhigh"), 21, 3, 5) + cycle = self.cycle(run=ChunkRun(2, unfinished_evidence="the evidence", route=kept)) + self.assertEqual((cycle.route, cycle.unfinished_evidence), (kept, "the evidence")) + + def test_a_chunk_that_was_not_parked_starts_on_the_drivers_own_prompts_with_no_facts(self) -> None: + cycle = self.cycle() + self.assertIs(cycle.prompts, chunk_prompt.WORKER) + self.assertEqual(cycle.facts, "") + def test_works_in_the_epics_folder_and_archives_apart_from_loop_claude(self) -> None: cycle = self.cycle() self.assertEqual((cycle.root, cycle.project_archive, cycle.records), (str(self.home / ".local/state/do-epic/ABC-12"), self.home / ".claude/do-epic-transcripts/ABC-12", "")) @@ -392,6 +402,14 @@ class AChunksRun(DoEpicTest): self.assertEqual(code, 2, out) self.assertIn("the judge ruled it unfinished 6 cycles running; finish the chunk yourself, or rerun do-epic to give it 6 more cycles", err + out) + def test_every_archived_transcript_opens_with_the_chunks_task_through_step_ups_and_the_stop_for_the_human(self) -> None: + fake = FakeEffects(streams=[self.worker()] * 6, claude=[judged("unfinished", escalation="working")] + [judged("unfinished")] * 5, git=UNREAD) + code, out, _ = self.run_main(["--chunk", "ABC-21", "ABC-12"], fake, MODELS) + self.assertEqual(code, 2, out) + outcomes = [path.read_text(encoding="utf-8") for path in (self.home / ".claude/do-epic-transcripts/ABC-12").glob("*/*.outcomes.md")] + self.assertTrue(any("Z Stepped up\n" in text for text in outcomes) and any("Z Stopped: " in text for text in outcomes), outcomes) + self.assertEqual([text for text in outcomes if not text.startswith("Task: Work the Jira issue ABC-21 of the epic ABC-12 as one chunk")], []) + def test_a_finished_chunk_moves_its_issue_to_done_before_the_chunk_it_blocks_starts(self) -> None: store = MemoryEpic("ABC-12", [self.issues[0], self.issues[1], issue("ABC-22", "do-epic-claude", blockers=("ABC-21",))]) self.backend = lambda config, key: (store, EpicRecord(Description(self.description))) # type: ignore[method-assign,arg-type] @@ -507,6 +525,13 @@ class AChunksRun(DoEpicTest): self.assertEqual(code, 0, err) self.assertIn("=== cycle 1, worker on claude-fable-5-2 at low effort ===\n", out) + def test_a_chunk_in_a_repo_without_a_saved_model_stops_off_a_terminal_naming_do_epic_as_what_to_rerun(self) -> None: + self.repo_chunk() + code, out, err = self.run_main(["--chunk", "ABC-20", "ABC-12"], FakeEffects(git=UNREAD, on_terminal=False), {"TMUX_PANE": "%1"}) + self.assertEqual((code, out), (2, "")) + self.assertIn("pick api's model", err) + self.assertIn("rerun do-epic on a terminal", err) + def test_a_chunk_in_a_repo_stops_where_the_repos_ladder_or_records_cannot_be_read(self) -> None: folder = self.repo_chunk() (folder / "routing-ladder.json").write_text("[", encoding="utf-8") diff --git a/bin/do_epic/test_scheduler.py b/bin/do_epic/test_scheduler.py index c255053b..cd853477 100644 --- a/bin/do_epic/test_scheduler.py +++ b/bin/do_epic/test_scheduler.py @@ -296,6 +296,10 @@ class StartingAnEpicsChunks(unittest.TestCase): self.assertTrue((self.home / ".local/state/loop-claude/api/starting").is_file()) self.assertTrue((self.home / ".local/state/do-epic/ABC-12/starting").is_file()) + def test_a_parked_chunk_resumes_under_the_starting_lock_of_its_repos_checkout(self) -> None: + self.assertIsNone(self.resumed(self.epic(1, api=1), Tmux(("epic-ABC-12", "ABC-20 awaits an approval", False, "")))) + self.assertEqual([lock.parent.name for lock in (self.home / ".local/state/loop-claude").glob("*/starting")], ["api"]) + def test_the_waits_keep_the_trackers_rank_order(self) -> None: run = Tmux(("epic-ABC-12", "ABC-20", True, str(self.home / "code/api")), ("epic-ABC-12", "ABC-22", False, str(self.home / "code/api"))) found = self.start(self.epic(1, api=1), [issue("ABC-20", "repo:api"), issue("ABC-21"), issue("ABC-22", "repo:api")], run, own="ABC-22") diff --git a/bin/forge/test_reviews.py b/bin/forge/test_reviews.py index dc618052..6f1b6671 100644 --- a/bin/forge/test_reviews.py +++ b/bin/forge/test_reviews.py @@ -34,8 +34,14 @@ class ThePullRequestAsViewed(unittest.TestCase): printed = json.dumps(PULL | {"reviews": [ASKED, pending], "comments": [{"author": {"login": "me"}, "createdAt": "2026-11-02T11:00:00Z"}, {"author": {"login": "rev"}, "createdAt": "2026-11-02T12:00:00Z"}]}) self.assertEqual(reviews.pull_view(printed), PullView(False, CHANGES_REQUESTED, (Review("rev", CHANGES_REQUESTED, "2026-11-02T10:00:00Z", "abc"), Review("me", "PENDING", "", "")), ("", "2026-11-02T11:00:00Z"))) + def test_keeps_each_comment_of_the_authors_whoever_commented_before_it(self): + printed = json.dumps(PULL | {"comments": [{"author": {"login": "rev"}, "createdAt": "2026-11-02T12:00:00Z"}, {"author": {"login": "me"}, "createdAt": "2026-11-02T13:00:00Z"}]}) + viewed = reviews.pull_view(printed) + assert isinstance(viewed, PullView) + self.assertEqual(viewed.said, ("2026-11-02T13:00:00Z",)) + def test_an_answer_in_another_shape_is_a_failure_naming_what_gh_printed(self): - unshaped = ("not json", "[]", json.dumps(PULL | {"reviews": None}), json.dumps(PULL | {"author": None}), json.dumps(PULL | {"reviews": [ASKED | {"commit": None}]}), json.dumps(PULL | {"reviews": [ASKED | {"submittedAt": 7}]}), json.dumps(PULL | {"comments": [{"author": {"login": 7}, "createdAt": "2026-11-02T11:00:00Z"}]}), json.dumps(PULL | {"comments": [{"author": {"login": PULL["author"]["login"]}, "createdAt": None}]}), json.dumps(PULL | {"reviews": [{key: value for key, value in ASKED.items() if key != "submittedAt"}]}), json.dumps({key: value for key, value in PULL.items() if key != "isDraft"})) + unshaped = ("not json", "x" * 300, "[]", json.dumps(PULL | {"reviews": [ASKED | {"author": {"login": 7}}]}), json.dumps(PULL | {"reviews": None}), json.dumps(PULL | {"author": None}), json.dumps(PULL | {"reviews": [ASKED | {"commit": None}]}), json.dumps(PULL | {"reviews": [ASKED | {"submittedAt": 7}]}), json.dumps(PULL | {"comments": [{"author": {"login": 7}, "createdAt": "2026-11-02T11:00:00Z"}]}), json.dumps(PULL | {"comments": [{"author": {"login": PULL["author"]["login"]}, "createdAt": None}]}), json.dumps(PULL | {"reviews": [{key: value for key, value in ASKED.items() if key != "submittedAt"}]}), json.dumps({key: value for key, value in PULL.items() if key != "isDraft"})) for printed in unshaped: with self.subTest(printed=printed): self.assertEqual(reviews.pull_view(printed), Failure(f"gh pr view printed no review decision: {printed[:200]}")) @@ -51,6 +57,7 @@ class WhetherTheWorkerAnswered(unittest.TestCase): self.assertTrue(reviews.answered(self.view(), "def")) self.assertTrue(reviews.answered(self.view(said=("2026-11-02T10:30:00Z",)), "abc")) self.assertFalse(reviews.answered(self.view(said=("2026-11-02T09:00:00Z", "")), "abc")) + self.assertFalse(reviews.answered(self.view(said=("2026-11-02T10:00:00Z",)), "abc"), "a word at the review's own instant is not after it") self.assertTrue(reviews.answered(self.view(Review("me", "COMMENTED", "2026-11-02T10:30:00Z", "")), "abc")) self.assertFalse(reviews.answered(PullView(False, "", (), ()), "abc")) @@ -78,7 +85,7 @@ class TheThreadsThatWait(unittest.TestCase): self.assertEqual(reviews.waiting_threads("7", lambda args: printed), ["a.py", "b.py"]) def test_an_answer_in_another_shape_is_a_failure_naming_what_gh_printed(self): - for printed in ("not json", "{}", json.dumps({"data": {"repository": {"pullRequest": {"author": {"login": "me"}, "reviewThreads": {"nodes": [{"isResolved": False}]}}}}}), json.dumps({"data": {"repository": {"pullRequest": {"author": {"login": "me"}, "reviewThreads": {"nodes": [{"isResolved": False, "path": "a.py", "comments": {"nodes": [{"author": {"login": 7}}]}}]}}}}}), json.dumps({"data": {"repository": {"pullRequest": {"author": {"login": "me"}, "reviewThreads": {"nodes": [{"path": "a.py", "comments": {"nodes": []}}]}}}}})): + for printed in ("not json", "x" * 300, "{}", json.dumps({"data": {"repository": {"pullRequest": {"author": {"login": 7}, "reviewThreads": {"nodes": []}}}}}), json.dumps({"data": {"repository": {"pullRequest": {"author": {"login": "me"}, "reviewThreads": {"nodes": [{"isResolved": False}]}}}}}), json.dumps({"data": {"repository": {"pullRequest": {"author": {"login": "me"}, "reviewThreads": {"nodes": [{"isResolved": False, "path": "a.py", "comments": {"nodes": [{"author": {"login": 7}}]}}]}}}}}), json.dumps({"data": {"repository": {"pullRequest": {"author": {"login": "me"}, "reviewThreads": {"nodes": [{"path": "a.py", "comments": {"nodes": []}}]}}}}})): with self.subTest(printed=printed): self.assertEqual(reviews.waiting_threads("7", lambda args: printed), Failure(f"gh api graphql printed no review threads: {printed[:200]}")) @@ -96,6 +103,24 @@ class TheDecisionOnGitea(unittest.TestCase): self.assertEqual(self.decision({"title": "Fix it"}), "") self.assertEqual(self.decision({"title": 7}), Failure("tea api printed no title of pull request 7: {'title': 7}")) self.assertEqual(self.decision([]), Failure("tea api printed no title of pull request 7: []")) + self.assertEqual(self.decision("y" * 300), Failure(f"tea api printed no title of pull request 7: {'y' * 200}")) + + +class TheDecisionOnGitHub(unittest.TestCase): + def test_reads_the_review_decision_and_empty_where_no_review_is_asked_for(self): + asked = [] + self.assertEqual(reviews.github_decision("7", lambda args: asked.append(args) or '{"reviewDecision": "APPROVED"}'), "APPROVED") + self.assertEqual(asked, [["gh", "pr", "view", "7", "--json", "reviewDecision"]]) + self.assertEqual(reviews.github_decision("7", printing({"reviewDecision": None})), "") + self.assertEqual(reviews.github_decision("7", printing({"reviewDecision": ""})), "") + + def test_an_answer_without_a_decision_is_a_failure_naming_the_first_200_characters_gh_printed(self): + for printed in ("not json", "{}", "[]", " " + "x" * 300): + with self.subTest(printed=printed): + self.assertEqual(reviews.github_decision("7", lambda args: printed), Failure(f"gh pr view printed no review decision: {printed.strip()[:200]}")) + + def test_a_command_that_fails_is_that_failure(self): + self.assertEqual(reviews.github_decision("7", lambda args: Failure("gh cannot start")), Failure("gh cannot start")) if __name__ == "__main__": diff --git a/bin/git_cli/test_git_repo.py b/bin/git_cli/test_git_repo.py index 5ad51a55..9f58fda4 100644 --- a/bin/git_cli/test_git_repo.py +++ b/bin/git_cli/test_git_repo.py @@ -1,4 +1,5 @@ import contextlib +import os import pathlib import subprocess import sys @@ -35,6 +36,14 @@ class Git(unittest.TestCase): self.assertTrue(done.reason.startswith("git log could not start: "), done.reason) self.assertIn("/code/gone", done.reason) + def test_runs_git_in_the_environment_given_and_only_guarded_stops_the_fetch_of_missing_objects(self): + shown = ("-c", "alias.shown=!echo \"$SHOWN_TO_GIT/$GIT_NO_LAZY_FETCH\"", "shown") + given = {**{name: value for name, value in os.environ.items() if name != "GIT_NO_LAZY_FETCH"}, "SHOWN_TO_GIT": "given"} + self.assertEqual(git_repo.git(*shown, env=given), "given/\n") + self.assertEqual(git_repo.git(*shown, env=given, guarded=True), "given/1\n") + self.assertNotIn("SHOWN_TO_GIT", os.environ) + self.assertEqual(git_repo.git(*shown, guarded=True), "/1\n") + class Guarded(unittest.TestCase): """A checkout whose own config may name a program for git to run.""" diff --git a/bin/judge_replay/test_judge_replay.py b/bin/judge_replay/test_judge_replay.py index 3560ed2e..67eca5ed 100644 --- a/bin/judge_replay/test_judge_replay.py +++ b/bin/judge_replay/test_judge_replay.py @@ -5,6 +5,7 @@ import pathlib import subprocess import sys import tempfile +import time import unittest sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) @@ -134,11 +135,13 @@ class Main(unittest.TestCase): (chunk / f"t{n}.outcomes.md").write_text(f"Task: Do it.\n\n2026-10-14T10:00:0{n}Z {verdict}\n\nEvidence:\n> e\n") self.calls = [] self.prompts = [] + self.schemas = [] def run_main(self, argv, reply): def claude(args, cwd, prompt): self.calls.append((args[args.index("--model") + 1], args[args.index("--effort") + 1], cwd)) self.prompts.append(prompt) + self.schemas.append(args[args.index("--json-schema") + 1]) result = reply(Rung(args[args.index("--model") + 1], args[args.index("--effort") + 1]), "said Verdict: interrupt" in prompt.rsplit("latest last:", 1)[-1]) return subprocess.CompletedProcess(args, 0, json.dumps(result), "") @@ -224,6 +227,48 @@ class Main(unittest.TestCase): runs = [json.loads(path.read_text(encoding="utf-8").splitlines()[0]) for path in folder.glob("*.jsonl")] self.assertEqual({run["prompt"] for run in runs}, set(self.prompts)) + def test_says_where_it_could_not_keep_a_run_and_still_prints_the_report(self): + replays = self.home / ".claude" / "loop-claude-transcripts" / "replays" + replays.write_text("") + code, out, err = self.run_main(["claude-opus-5-5"], lambda rung, interrupted: self.verdict(interrupted, f"said {INTERRUPTED if interrupted else FINISHED}")) + self.assertEqual(code, 0, err) + self.assertIn(f"could not keep the run in {replays}/judge-replay-claude-opus-5-5-20260921T141320Z: ", err) + self.assertIn("The reference judge, claude-opus-5-5 at xhigh effort, reproduced 2.", out) + + def test_says_how_many_verdicts_it_replays_on_each_rung(self): + code, _, err = self.run_main(["claude-opus-5-5"], lambda rung, interrupted: self.verdict(interrupted, f"said {INTERRUPTED if interrupted else FINISHED}")) + self.assertEqual(code, 0, err) + self.assertEqual(err.splitlines()[:2], ["replaying 2 verdicts on claude-opus-5-5 at xhigh effort", "replaying 2 verdicts on claude-fable-5-2 at low effort"]) + + def test_replays_four_verdicts_at_once(self): + chunk = self.home / ".claude" / "loop-claude-transcripts" / "proj" / "c1" + for n in range(2, 8): + (chunk / f"t{n}.jsonl").write_text(json.dumps({"type": "assistant", "message": {"content": [{"type": "text", "text": f"said {FINISHED} {n}"}]}}) + "\n") + (chunk / f"t{n}.outcomes.md").write_text(f"Task: Do it.\n\n2026-10-14T10:00:0{n}Z {FINISHED}\n\nEvidence:\n> e\n") + running, most = [], [] + + def reply(rung, interrupted): + running.append(rung) + most.append(len(running)) + time.sleep(0.05) + running.remove(rung) + return self.verdict(interrupted, f"said {INTERRUPTED if interrupted else FINISHED}") + + code, _, err = self.run_main(["claude-opus-5-5"], reply) + self.assertEqual(code, 0, err) + self.assertIn("replaying 8 verdicts on ", err) + self.assertEqual(max(most), 4) + + def test_the_prompt_and_the_reply_schema_of_a_rebuilt_run_name_loop_claude_and_the_records_and_show_the_transcript_once(self): + code, _, err = self.run_main(["claude-opus-5-5"], lambda rung, interrupted: self.verdict(interrupted, f"said {INTERRUPTED if interrupted else FINISHED}")) + self.assertEqual(code, 0, err) + for prompt, schema in zip(self.prompts, self.schemas, strict=True): + self.assertIn("\n\nThe records: the project's working rules and decisions, then the human's rules for every project:\n", prompt) + self.assertIn("what loop-claude told it on [loop] lines, latest last:\n", prompt) + self.assertEqual(prompt.count("said Verdict: "), 1) + self.assertIn("loop-claude", schema) + self.assertNotIn("None", schema) + def test_hands_the_judge_the_prompt_the_archive_kept_and_rebuilds_the_prompt_of_a_verdict_with_none(self): kept = self.home / ".claude" / "loop-claude-transcripts" / "proj" / "c1" prompt = "From the goals file, OLD premises\n\nThe records: none\n\nThe task the worker was given:\nDo it.\n\nWhat the worker did and said, what the human typed on [human] lines and what loop-claude told it on [loop] lines, latest last:\nsaid Verdict: finished\n\nOLD rules" @@ -235,6 +280,8 @@ class Main(unittest.TestCase): self.assertIn("The reference judge, claude-opus-5-5 at xhigh effort, reproduced 2.", out) # The first replay, on the finished verdict, reads the archived prompt; the second, on the interrupt, is rebuilt from the records of today. self.assertEqual(self.prompts[0], prompt) + # The reply's schema names loop-claude to the judge of either prompt. + self.assertEqual([schema for schema in self.schemas if "loop-claude" not in schema or "None" in schema], []) self.assertEqual(self.calls[0][2], self.calls[1][2]) self.assertTrue(self.calls[0][2].endswith("/code/proj")) self.assertIn("The task the worker was given:\nDo it.", self.prompts[1]) diff --git a/bin/loop_claude/loop/test_main.py b/bin/loop_claude/loop/test_main.py index 8e955dda..6dc90446 100644 --- a/bin/loop_claude/loop/test_main.py +++ b/bin/loop_claude/loop/test_main.py @@ -177,6 +177,51 @@ class WindowStarts(unittest.TestCase): self.assertEqual(cycles, [(main.LoopRun(1, "/r", "do it", "", chunk="item-3"), loop), (main.LoopRun(2, "/r", "do it", "", chunk="item-3"), loop)]) self.assertEqual(started, [(main.LoopRun(2, "/r", "do it", "", chunk="item-3"), True, loop.paths, env, launcher, effects)]) + def window_loop(self, cycles, env, **doubles): + """run_loop's stop for item-3's window loop, and each step it took with what it handed that step. Each step answers as its double in `doubles` does, else as a cycle without a stop: next_cycle answers the next of `cycles`, run_cycle an unfinished verdict, and closed_cycle the run with one more unfinished verdict.""" + fake, calls, cycles = FakeEffects(git=["/r\n"]), [], list(cycles) + effects, launcher, home = fake.effects(), pathlib.Path("/b/loop-claude"), paths.paths_from(self.HOME) + self.unfinished = unittest.mock.Mock(verdict=unittest.mock.Mock(spec=Unfinished)) + answers = {"fingerprint": lambda *args: "print-1", "exec_if_edited": lambda *args: None, "next_cycle": lambda *args: cycles.pop(0), "run_cycle": lambda *args: self.unfinished, + "closed_cycle": lambda run, *args: dataclasses.replace(run, unfinished_run=run.unfinished_run + 1), "check_routing": lambda *args: None, **doubles} + + def step(name): + return lambda *args: calls.append((name, *args)) or answers[name](*args) + + stop = main.run_loop(["--chunk", "item-3", "do it"], env, home, launcher, CWD, effects, steps(lambda *args: self.fail(args), **{name: step(name) for name in answers})) + self.loop = this_loop.Loop(root="/r", session=session.session_named("A r loop-claude item-3", self.HOME / ".claude" / "projects"), window=tmux.Window("loop-r", "item-3", "%4"), paths=home, env=env, launcher=launcher, effects=effects) + return stop, calls + + def test_a_window_loop_hands_each_step_of_a_cycle_what_the_steps_before_it_made(self): + env = {"TMUX_PANE": "%4", "LOOP_CLAUDE_TRANSCRIPTS": "/t"} + on_ladder = unittest.mock.Mock(project=OPUS_PROJECT, project_archive=pathlib.Path("/t/r")) + off_ladder = unittest.mock.Mock(project=None, project_archive=pathlib.Path("/t/r"), route=unittest.mock.Mock(rung=Rung("my-model"))) + stop, calls = self.window_loop([on_ladder, off_ladder, Stop(2, "the human is needed")], env) + self.assertEqual(stop, Stop(2, "the human is needed")) + before, first, second, third = (main.LoopRun(number, "/r", "do it", "", unfinished_run=max(number - 1, 0), chunk="item-3") for number in range(4)) + self.assertEqual(calls, [ + ("fingerprint", pathlib.Path("/b/loop-claude")), + ("exec_if_edited", self.loop, "print-1", before), + ("next_cycle", first, self.loop), + ("run_cycle", on_ladder), + ("closed_cycle", first, OPUS_PROJECT, self.unfinished, pathlib.Path("/t/r"), "loop-claude", self.loop), + ("check_routing", pathlib.Path("/t"), Rung("claude-opus-5-5", "medium"), self.loop), + ("exec_if_edited", self.loop, "print-1", dataclasses.replace(first, unfinished_run=1)), + ("next_cycle", second, self.loop), + ("run_cycle", off_ladder), + ("closed_cycle", second, None, self.unfinished, pathlib.Path("/t/r"), "loop-claude", self.loop), + ("check_routing", pathlib.Path("/t"), Rung("my-model"), self.loop), + ("exec_if_edited", self.loop, "print-1", dataclasses.replace(second, unfinished_run=2)), + ("next_cycle", third, self.loop), + ]) + + def test_a_window_loop_stops_with_the_stop_of_the_step_that_made_one(self): + cycle = unittest.mock.Mock(project=OPUS_PROJECT, project_archive=pathlib.Path("/t/r")) + for name, taken in (("exec_if_edited", 2), ("run_cycle", 4), ("closed_cycle", 5), ("check_routing", 6)): + with self.subTest(step=name): + stop, calls = self.window_loop([cycle], {"TMUX_PANE": "%4"}, **{name: lambda *args: Stop(1, f"{name} stopped")}) + self.assertEqual((stop, len(calls), calls[-1][0]), (Stop(1, f"{name} stopped"), taken, name)) + def test_a_window_loop_given_no_task_prints_the_help(self): self.assertEqual(main.loop_run_from(["--chunk", "item-3"], CWD, FakeEffects(git=["/r\n"]).effects()), Stop(1, usage.HELP)) diff --git a/bin/loop_effects/test_effects.py b/bin/loop_effects/test_effects.py index b788d399..de024575 100644 --- a/bin/loop_effects/test_effects.py +++ b/bin/loop_effects/test_effects.py @@ -119,6 +119,18 @@ class TmuxEffect(unittest.TestCase): pathlib.Path(folder, "tmux").write_text(f"#!{sys.executable}\nimport sys\nsys.stdout.buffer.write(b'caf\\xe9')\n") self.assertEqual(effects.tmux(["list-windows"]).stdout, "caf\ufffd") + def test_runs_tmux_without_this_processs_input(self): + with tempfile.TemporaryDirectory() as folder: + effects = self.fake_tmux(folder) + pathlib.Path(folder, "tmux").write_text(f"#!{sys.executable}\nimport sys\nprint(repr(sys.stdin.read()))\n") + pathlib.Path(folder, "typed").write_text("what the human types\n") + kept = os.dup(0) + self.addCleanup(os.close, kept) + self.addCleanup(os.dup2, kept, 0) + with open(pathlib.Path(folder, "typed")) as typed: + os.dup2(typed.fileno(), 0) + self.assertEqual(effects.tmux(["list-windows"]).stdout, "''\n") + def test_reads_a_tmux_that_cannot_start_as_exit_127(self): with tempfile.TemporaryDirectory() as folder: done = system_effects(pathlib.Path.home(), os.environ | {"PATH": folder}).tmux(["-V"]) diff --git a/bin/plan_store/test_jira_epic.py b/bin/plan_store/test_jira_epic.py index 46b6bbc4..6f1e3b5b 100644 --- a/bin/plan_store/test_jira_epic.py +++ b/bin/plan_store/test_jira_epic.py @@ -180,6 +180,11 @@ class JiraPlan(unittest.TestCase): self.assertIsInstance(failure, Failure) self.assertTrue(failure.reason.startswith("ABC-1 is not in the shape Jira Cloud's REST API version 3 documents: "), failure) # type: ignore[union-attr] + def test_an_issue_that_lacks_a_field_fails_the_read_naming_the_field(self) -> None: + good = issue("ABC-1") + lacking = good | {"fields": {name: value for name, value in good["fields"].items() if name != "fixVersions"}} + self.assertEqual(self.read(lacking), Failure("ABC-1 is not in the shape Jira Cloud's REST API version 3 documents: KeyError('fixVersions')")) # type: ignore[arg-type] + def test_converts_every_description_in_one_run_and_none_for_an_issue_without_one(self): found = self.items(issue("ABC-1", description=adf("One.")), issue("ABC-2"), issue("ABC-3", description=adf("Three.")), issue("ABC-4", description=adf("Four."))) self.assertEqual([item.detail for item in found], ["One.", "", "Three.", "Four."]) diff --git a/bin/review_replay/test_review_replay.py b/bin/review_replay/test_review_replay.py index feb9c22c..bca01233 100644 --- a/bin/review_replay/test_review_replay.py +++ b/bin/review_replay/test_review_replay.py @@ -9,6 +9,7 @@ import shutil import subprocess import sys import tempfile +import time import unittest sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) @@ -284,6 +285,43 @@ class Main(unittest.TestCase): self.assertEqual({run["prompt"] for run in runs if "--agent" in run["args"]}, {prompt for *_, prompt, _ in self.reviews}) self.assertTrue(any("--json-schema" in run["args"] for run in runs)) + def test_says_where_it_could_not_keep_a_run_and_still_prints_the_report(self): + (self.archive / "replays").write_text("") + code, out, err = self.run_main(["stability", "claude-opus-5-5"], lambda rung: {"found": [0, 1]}) + self.assertEqual(code, 0, err) + self.assertIn(f"could not keep the run in {self.archive}/replays/review-replay-stability-claude-opus-5-5-20260921T141320Z: ", err) + self.assertTrue(out.startswith("Replayed 1 stability rounds"), out) + + def test_makes_each_clone_in_the_scratch_folder_under_a_name_that_says_whose_it_is(self): + scratch = pathlib.Path(self.enterContext(tempfile.TemporaryDirectory())) + self.limits["scratch"] = scratch + for argv, replays in ((["stability", "claude-opus-5-5"], 4), (["stability", "claude-opus-5-5", "--omit-claude-md"], 4)): + with self.subTest(argv=argv): + self.reviews.clear() + code, _, err = self.run_main(argv, lambda rung: {"found": [0, 1]}) + self.assertEqual(code, 0, err) + clones = [pathlib.Path(cwd) for *_, cwd in self.reviews] + self.assertEqual(([clone.parent for clone in clones], [clone.name[:14] for clone in clones]), ([scratch] * replays, ["review-replay-"] * replays)) + self.assertEqual(list(scratch.iterdir()), [], "each clone is removed") + + def test_replays_no_more_rounds_at_once_than_asked_on_any_rung(self): + self.write_chunk("proj", "c2", "2026-10-14T10:30:00Z") + self.limits["at_once"] = 1 + for argv, rungs in ((["stability", "claude-opus-5-5"], {REFERENCE, Rung("claude-opus-5-5", "low"), Rung("claude-sonnet-5-5", "high")}), (["stability", "claude-opus-5-5", "--omit-claude-md"], {REFERENCE})): + with self.subTest(argv=argv): + running, most = [], {} + + def review(rung): + running.append(rung) + most[rung] = max(most.get(rung, 0), len(running)) + time.sleep(0.05) + running.remove(rung) + return {"type": "result", "result": f"review on {rung.model} {rung.effort}"} + + code, _, err = self.run_main(argv, lambda rung: {"found": [0, 1]}, review=review) + self.assertEqual(code, 0, err) + self.assertEqual(most, dict.fromkeys(rungs, 1)) + def test_without_claude_md_replays_each_round_three_times_with_it_and_once_without_on_the_project_rung(self): finds = iter(([0, 1], [0], [0, 1], [1])) code, out, err = self.run_main(["stability", "claude-opus-5-5", "--omit-claude-md"], lambda rung: {"found": next(finds)}) diff --git a/bin/routing_figures/test_routing_figures.py b/bin/routing_figures/test_routing_figures.py index badf3b78..aa6efc96 100644 --- a/bin/routing_figures/test_routing_figures.py +++ b/bin/routing_figures/test_routing_figures.py @@ -490,6 +490,14 @@ class Archive(unittest.TestCase): self.assertIsNone(state.mark_raised(self.root, [a], "2026-10-15T02:00:00Z", replace_file)) self.assertEqual(state.raised(self.root), state.Raised("2026-10-15T01:00:00Z", frozenset({a}))) + def test_adds_a_raised_call_after_the_files_text_less_only_the_newlines_that_end_it(self): + call = Call("reports", ("Explore",)) + for ending in ("what the human wrote XX", "a line that breaks in markdown ", "a line that ends in a tab\t"): + with self.subTest(ending=ending): + (self.root / state.RAISED).write_text(f"# Routing calls raised\n\n{ending}\n\n\n") + self.assertIsNone(state.mark_raised(self.root, [call], "2026-10-15T00:00:00Z", replace_file)) + self.assertEqual((self.root / state.RAISED).read_text(), f"# Routing calls raised\n\n{ending}\n\n- 2026-10-15T00:00:00Z {call.sentence()}\n") + def test_fails_when_it_cannot_keep_the_state(self): assert_not_root(self) self.root.chmod(0o555) diff --git a/bin/rung_replay/test_rung_replay.py b/bin/rung_replay/test_rung_replay.py index 21dd083b..f4b276f1 100644 --- a/bin/rung_replay/test_rung_replay.py +++ b/bin/rung_replay/test_rung_replay.py @@ -5,6 +5,7 @@ import pathlib import subprocess import sys import tempfile +import time import unittest sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) @@ -60,6 +61,21 @@ class Replayed(unittest.TestCase): self.assertNotIn(("b", high), asked) self.assertEqual(search(["a"], [MIDDLE], {REFERENCE: {"a": set()}}, [high]), (Replay([set()], [], None, []), [("a", REFERENCE)])) + def test_says_what_it_replays_on_each_rung_and_replays_no_more_cases_at_once_than_asked(self): + high = Rung("claude-fable-5-2", "low") + said, running, most = [], [], {} + + def replay_one(case, rung): + running.append(case) + most[rung] = max(most.get(rung, 0), len(running)) + time.sleep(0.02) + running.remove(case) + return {0} + + replayed(["a", "b", "c"], REFERENCE, [MIDDLE], replay_one, lambda _, reply: sorted(reply), "verdicts", said.append, 1, [high]) + self.assertEqual(said, [f"replaying 3 verdicts on {REFERENCE}", f"replaying 3 verdicts on {high}", f"replaying 3 verdicts on {MIDDLE}"]) + self.assertEqual(most, {REFERENCE: 1, high: 1, MIDDLE: 1}) + def test_stops_at_a_usage_limit(self): limit = Limited(None, "limit") self.assertEqual(search(["a"], [MIDDLE], {REFERENCE: {"a": {0}}, MIDDLE: {"a": {0}}, CHEAP: limit}, [CHEAP])[0], limit) diff --git a/bin/text_files/doubles.py b/bin/text_files/doubles.py index c8563b8b..1c6850e2 100644 --- a/bin/text_files/doubles.py +++ b/bin/text_files/doubles.py @@ -1,4 +1,4 @@ -"""What the suites that compress a file share: compress, bound to what really compresses, writes and flushes.""" +"""What the suites that compress a file share: compress with gzip.compress, replace_file and os.fsync bound.""" import functools import gzip import os diff --git a/bin/text_files/test_compressed.py b/bin/text_files/test_compressed.py index 479866f5..e1acd86f 100644 --- a/bin/text_files/test_compressed.py +++ b/bin/text_files/test_compressed.py @@ -26,6 +26,19 @@ class Compress(unittest.TestCase): self.assertEqual(packed.read_bytes(), gzip.compress(b'{"a": 1}\n', mtime=0)) self.assertEqual(packed.stat().st_mtime, 2000) + def test_asks_for_a_gzip_whose_header_holds_no_time_whatever_the_packer_would_write_unasked(self): + with tempfile.TemporaryDirectory() as root: + plain = pathlib.Path(root, "t.jsonl") + plain.write_bytes(b"a\n") + asked = [] + + def pack(data, **kwargs): + asked.append(kwargs) + return gzip.compress(data, **kwargs) + + self.assertIsNone(compress(plain, pack=pack)) + self.assertEqual(asked, [{"mtime": 0}]) + def test_syncs_the_gzip_and_its_folder_before_removing_the_file(self): with tempfile.TemporaryDirectory() as root: plain = pathlib.Path(root, "t.jsonl")