diff --git a/README.md b/README.md index 3847b9c0..4e1e67c6 100644 --- a/README.md +++ b/README.md @@ -219,7 +219,7 @@ Install puts only `loop-claude` on `PATH`; call the others by their path, such a ### Packages under `bin/` -`bin/` is the import root: each launcher puts it on `sys.path`, so a module imports another by its path under `bin/`, such as `from claude_code.entries import parse_entry`. Each tool's package holds its tests. Beside the packages, `bin/`'s root holds only the launchers, the tools of one file with their tests, `test_env.py`, `failure.py` and `setup.cfg`. `failure.py`'s `Failure`, which modules across `bin/` return for a failed step, belongs to no single package. `agent_cycle/`, `atlassian/`, `branch_chunks/`, `chunk_driver/`, `claude_code/`, `command_lines/`, `forge/`, `git_cli/`, `loop_effects/`, `loop_state/`, `machine_load/`, `model_routing/`, `notifier/`, `plan/`, `plan_store/`, `record_store/`, `release_plan/`, `review_rules/`, `rung_replay/`, `text_files/` and `transcript_archive/` serve more than one tool. No package imports one that imports it back, which `repo_checks/test_package_cycles.py` checks. +`bin/` is the import root: each launcher puts it on `sys.path`, so a module imports another by its path under `bin/`, such as `from claude_code.entries import parse_entry`. Each tool's package holds its tests. Beside the packages, `bin/`'s root holds only the launchers, the tools of one file with their tests, `test_env.py`, `failure.py` and `setup.cfg`. `failure.py`'s `Failure`, which modules across `bin/` return for a failed step, belongs to no single package. `agent_cycle/`, `atlassian/`, `branch_chunks/`, `chunk_driver/`, `claude_code/`, `command_lines/`, `forge/`, `git_cli/`, `loop_effects/`, `loop_state/`, `machine_load/`, `model_routing/`, `notifier/`, `plan/`, `plan_store/`, `record_store/`, `release_plan/`, `review_rules/`, `rung_replay/`, `text_files/` and `transcript_archive/` serve more than one tool. No package imports one that imports it back, and a package's `doubles.py` holds the test doubles that other packages' tests use, so no module imports a `test_*` module of another package; `repo_checks/test_package_cycles.py` checks both. - `adf_codec/` converts markdown to ADF, the rich text of Jira Cloud and Confluence, and back, in a throwaway container running a pinned `@larvit/adf-codec`. It builds the container's image inside a heavy slot. - `agent_cycle/` runs one cycle (`run.py`): a headless claude worker, then the judge of its transcript. It also holds the interrupt, the tmux window and the routing of the worker's rung. The tool that drives the cycles supplies the prompts and file names of its own workflow (`worker_prompts.py`), and reads which items a worker took in. `loop_claude` and `do_epic` drive it; `agent_cycle` doesn't import either. `doubles.py` holds the doubles its tests share with `chunk_driver`'s. diff --git a/bin/atlassian/test_client.py b/bin/atlassian/test_client.py index fbf91612..dd070c4b 100644 --- a/bin/atlassian/test_client.py +++ b/bin/atlassian/test_client.py @@ -10,7 +10,7 @@ sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) from atlassian import client from rest_client.json_request import ErrorAnswer from failure import Failure -from rest_client.test_json_request import Served +from rest_client.doubles import Served AUTHORIZATION = "Basic " + base64.b64encode(b"me@example.com:s3cret").decode() diff --git a/bin/branch_chunks/test_branch_chunks.py b/bin/branch_chunks/test_branch_chunks.py index 429900d3..e790d5ef 100644 --- a/bin/branch_chunks/test_branch_chunks.py +++ b/bin/branch_chunks/test_branch_chunks.py @@ -13,7 +13,7 @@ from project_config.templates import BARE, Titles from branch_chunks.fixes import blamed, fixing_changes, removed, removed_work, Traced, with_fixes from branch_chunks.cut import Branch, chunk_log, Commit, cut_branch, landing, NOT_WORK, shipped, Span from branch_chunks.read import branch_chunks, traced_to, unsupported -from project_config.test_project_config import configured +from project_config.doubles import configured from failure import Failure from git_cli.git_repo import git from test_env import isolated_env diff --git a/bin/chunk_driver/project/test_records.py b/bin/chunk_driver/project/test_records.py index 3ef42e98..bafe0a2d 100644 --- a/bin/chunk_driver/project/test_records.py +++ b/bin/chunk_driver/project/test_records.py @@ -6,7 +6,7 @@ import unittest.mock sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[2])) -from project_config.test_project_config import configured +from project_config.doubles import configured from failure import Failure from agent_cycle import judge from chunk_driver.project import records diff --git a/bin/chunk_preflight/test_chunk_preflight.py b/bin/chunk_preflight/test_chunk_preflight.py index f94dfb76..137aa08f 100644 --- a/bin/chunk_preflight/test_chunk_preflight.py +++ b/bin/chunk_preflight/test_chunk_preflight.py @@ -11,7 +11,6 @@ sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) from project_config.templates import BRANCH from release_plan.next_chunk import CarriedOver, NoItem -from chunk_preflight import choices from failure import Failure from chunk_preflight.gathered import Gathered, ItemFacts, NamedPath, Ran, Worktree from test_env import isolated_env @@ -21,7 +20,7 @@ from release_plan import next_chunk from chunk_preflight.facts import open_pulls, render from plan_store.todo_md import plan_of from chunk_preflight.choices import worktree_add -from project_config.test_project_config import write_config +from project_config.doubles import write_config BIN = pathlib.Path(__file__).resolve().parents[1] README = "# Project\n\n## Goals\n\n**Void:** code health, since the plan's scores weigh goal 1 alone.\n\n1. **Achieve:** Work.\n\n## Audience\n\nMe.\n" diff --git a/bin/do_epic/test_human_tasks.py b/bin/do_epic/test_human_tasks.py index 9ac6e426..631a3851 100644 --- a/bin/do_epic/test_human_tasks.py +++ b/bin/do_epic/test_human_tasks.py @@ -6,7 +6,7 @@ import unittest sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) -from project_config.test_project_config import configured +from project_config.doubles import configured from do_epic.test_main import DESCRIPTION, Description, DoEpicTest, MODELS, UNREAD from do_epic.epic_record import EpicRecord from failure import Failure diff --git a/bin/do_epic/test_main.py b/bin/do_epic/test_main.py index 0c36fe7c..a983c4b0 100644 --- a/bin/do_epic/test_main.py +++ b/bin/do_epic/test_main.py @@ -13,7 +13,7 @@ sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) from do_epic import chunk_prompt, main from chunk_driver.chunk_run import ChunkRun -from project_config.test_project_config import configured +from project_config.doubles import configured from do_epic.config import DoEpicConfig from project_config.settings import Epic, Finish from do_epic.epic_record import EpicRecord diff --git a/bin/do_epic/test_planning.py b/bin/do_epic/test_planning.py index 627cd16e..2f8e9ef9 100644 --- a/bin/do_epic/test_planning.py +++ b/bin/do_epic/test_planning.py @@ -9,7 +9,7 @@ import sys sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) -from project_config.test_project_config import configured +from project_config.doubles import configured from do_epic.test_main import Description, DoEpicTest, structured as says from do_epic.backend.protocol import EpicIssue from do_epic.epic_record import EpicRecord diff --git a/bin/do_epic/test_repos.py b/bin/do_epic/test_repos.py index 1e504ab8..fdd8d66e 100644 --- a/bin/do_epic/test_repos.py +++ b/bin/do_epic/test_repos.py @@ -10,7 +10,7 @@ from do_epic.config import DoEpicConfig from failure import Failure from do_epic.repos import locate, Located, Unsure from project_config.settings import ProjectConfig -from project_config.test_project_config import write_config +from project_config.doubles import write_config class FakeGit: diff --git a/bin/judge_replay/test_judge_replay.py b/bin/judge_replay/test_judge_replay.py index b39308f0..314a0637 100644 --- a/bin/judge_replay/test_judge_replay.py +++ b/bin/judge_replay/test_judge_replay.py @@ -14,7 +14,7 @@ from transcript_archive.outcomes import ANSWERED, FINISHED, INTERRUPTED, UNFINIS from claude_code.entries import AssistantTurn, HumanTurn, LoopPrompt, Text from judge_replay.replay import Case, case_from, report, reproduced, sample, verdict_kind from text_files import compressed -from project_config.test_project_config import configured +from project_config.doubles import configured from loop_effects.effects import Effects from failure import Failure from agent_cycle.verdicts import Finished, Interrupt, QuotedAnswer, Unfinished diff --git a/bin/larv_review/test_larv_review.py b/bin/larv_review/test_larv_review.py index c0126123..88782485 100644 --- a/bin/larv_review/test_larv_review.py +++ b/bin/larv_review/test_larv_review.py @@ -18,7 +18,7 @@ from review_rules.comprehension_history import appended, entries, HistoryChange, from larv_review.history_commands import appended_to from larv_review.plan import checkout_plan, Emptied, EMPTIED_WORDS, landed_towards, plan_reasons, scoring_run_due from review_rules import comprehension_history -from project_config.test_project_config import configured +from project_config.doubles import configured from larv_review.ruling import counted, ruling, split, surplus, votes from larv_review.history_commands import decided, rerank from project_config.settings import DEFAULT_FLOOR diff --git a/bin/larv_review/test_scoring_setup.py b/bin/larv_review/test_scoring_setup.py index 38602a6e..59f97a15 100644 --- a/bin/larv_review/test_scoring_setup.py +++ b/bin/larv_review/test_scoring_setup.py @@ -12,7 +12,7 @@ sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) from larv_review.scoring_setup import bullets, file_globs, prepare_scoring, project_values, removed_setup, score_bullets from failure import Failure from test_env import isolated_env -from project_config.test_project_config import write_config +from project_config.doubles import write_config TOOL = pathlib.Path(__file__).resolve().parents[1] / "larv-review" SKILL = pathlib.Path(__file__).resolve().parents[2] / "skills" / "comprehension-panel" / "SKILL.md" diff --git a/bin/loop_claude/chunk/test_chunk_items.py b/bin/loop_claude/chunk/test_chunk_items.py new file mode 100644 index 00000000..a5619e2d --- /dev/null +++ b/bin/loop_claude/chunk/test_chunk_items.py @@ -0,0 +1,56 @@ +import pathlib +import re +import sys +import unittest + +sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[2])) + +from loop_claude.chunk import chunk_items +from claude_code import entries +from failure import Failure +from loop_claude.chunk import fresh_prompt +from plan_store.todo_md import plan_of +from model_routing.route import Route +from model_routing.ladder import Rung +from loop_effects.doubles import said +from agent_cycle.verdicts import TakenIn + + +# Item 297 scores the higher L, item 298 the higher E + I. +SCORED = "## Items\n\n| ID | Release | Exempt | Item | L | E | I | R | S | Moves | Score |\n|---|---|---|---|---|---|---|---|---|---|---|\n| 297 | 0.8.0 | | **Do it.** | 4 | 2 | 3 | 7 | 2 | 1 by 9 | 25 |\n| 298 | 0.8.0 | | **Do more.** | 3 | 3 | 4 | 8 | 2 | 1 by 9 | 20 |\n" + + +class ChunkItems(unittest.TestCase): + def test_names_the_items_on_the_workers_latest_chunk_items_line(self): + earlier = said("Chunk items: 297 (L 3, E + I 5)") + latest = said("Starting.\n**Chunk items:** 297 (L 4, E + I 5), item 298 (L 3, E + I 7), #12, 298\nOn to the tests.") + self.assertEqual(chunk_items.named([earlier, latest]), (297, 298, 12)) + self.assertEqual(chunk_items.named([latest, earlier]), (297,)) + self.assertEqual(chunk_items.named([earlier, said("Chunk items: none")]), ()) + self.assertEqual(chunk_items.named([said("Chunk items: 297 and 298 (L 3, E + I 7)")]), (297, 298)) + + def test_reads_the_example_line_the_prompt_gives_the_worker(self): + example = re.search(r"`(Chunk items:[^`]*)`", fresh_prompt.NAME_THE_ITEMS)[1] + self.assertEqual(chunk_items.named([said(example)]), (297, 298)) + + def test_every_prompt_that_starts_or_resumes_a_chunk_asks_for_the_line(self): + self.assertIn("on one line, as in `Chunk items: ", fresh_prompt.WORKER.resumed("", Route(Rung("m")), None)) + + def test_reads_only_a_line_the_worker_opens_with_the_label_in_its_own_text(self): + prompt = entries.LoopPrompt(text="Chunk items: 297 (L 3, E + I 5)") + written = entries.AssistantTurn(parts=(entries.ToolCall("t", "Write", {"content": "Chunk items: 9"}),)) + mentioned = said("Once I know them, I write `Chunk items: 5`.") + self.assertEqual(chunk_items.named([prompt, written, mentioned]), ()) + + def test_takes_the_highest_l_and_e_plus_i_todo_md_scores_among_the_named_items(self): + self.assertEqual(chunk_items.taken_in([said("Chunk items: 297 (L 1, E + I 2), 298, 400")], plan_of(SCORED)), TakenIn(4, 7)) + self.assertEqual(chunk_items.taken_in([said("Chunk items: 400")], plan_of(SCORED)), TakenIn()) + self.assertEqual(chunk_items.taken_in([], plan_of(SCORED)), TakenIn()) + + def test_names_the_items_at_no_level_where_todo_md_cannot_be_read(self): + self.assertEqual(chunk_items.taken_in([said("Chunk items: 297")], Failure("no todo.md")), TakenIn()) + self.assertEqual(chunk_items.taken_in([said("Chunk items: 297")], plan_of("no items table")), TakenIn()) + + +if __name__ == "__main__": + unittest.main() diff --git a/bin/loop_claude/chunk/test_fresh_prompt.py b/bin/loop_claude/chunk/test_fresh_prompt.py new file mode 100644 index 00000000..0f6df9bc --- /dev/null +++ b/bin/loop_claude/chunk/test_fresh_prompt.py @@ -0,0 +1,57 @@ +import pathlib +import sys +import unittest + +sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[2])) + +from loop_claude.chunk import fresh_prompt +from loop_claude.doubles import OPUS_PROJECT +from chunk_driver import prompts +from model_routing.route import Route +from model_routing.ladder import Rung + + +class FreshPrompt(unittest.TestCase): + """loop-claude's first prompt of a chunk.""" + # What the preflight found, as the fresh prompt quotes it. + FACTS = "- A preflight fact." + + def test_names_the_route_in_a_fresh_prompt_only_where_an_item_routed_it(self): + routed = fresh_prompt.WORKER.fresh("Do it.", "## Goals", Route(Rung("m", "high"), 3, 4, 5), None, self.FACTS) + self.assertIn(f"Continue any open draft PR or worktree for the chunk before creating a new one. {prompts.READING_RULE} {fresh_prompt.NAME_THE_ITEMS}\n\nloop-claude runs you on m at high effort, routed by item 3's L of 4.", routed) + unrouted = fresh_prompt.WORKER.fresh("Do it.", "## Goals", Route(Rung("m")), None, self.FACTS) + self.assertIn(f"before creating a new one. {prompts.READING_RULE} {fresh_prompt.NAME_THE_ITEMS}\n\n{self.FACTS}\n\nThe chunk's", unrouted) + self.assertNotIn("loop-claude runs you on", unrouted) + + def test_has_the_routed_worker_count_its_items_size_again_and_puts_the_facts_before_the_scope(self): + routed = fresh_prompt.WORKER.fresh("Do it.", "## Goals", Route(Rung("m", "high"), 3, 4, 5), None, self.FACTS) + self.assertIn("item 3's size again", routed) + self.assertIn("escalation trigger 1", routed) + self.assertIn(f"\n\n{self.FACTS}\n\nThe chunk's", routed) + self.assertNotIn("size again", fresh_prompt.WORKER.fresh("Do it.", "## Goals", Route(Rung("m")), None, self.FACTS)) + + def test_gives_each_fresh_worker_the_reading_rule_once(self): + rule = "never read more than 80 lines of a file at once, nor take a tool output over 3,000 characters into your context" + for route in (Route(Rung("m", "high"), 3, 4, 5), Route(Rung("m"))): + with self.subTest(route=route): + self.assertEqual(fresh_prompt.WORKER.fresh("Do it.", "## Goals", route, None, self.FACTS).count(rule), 1) + + def test_quotes_what_the_preflight_printed_or_has_the_worker_find_the_facts_itself(self): + self.assertIn("\n\n- A fact.", fresh_prompt.preflight_facts("- A fact.")) + failed = fresh_prompt.preflight_failed("git could not start") + self.assertIn("yourself", failed) + self.assertIn("git could not start", failed) + + def test_has_each_fresh_worker_continue_an_open_draft_pr_or_worktree_once_whatever_the_preflight_found(self): + for facts in (fresh_prompt.preflight_facts("- A fact."), fresh_prompt.preflight_failed("git could not start")): + with self.subTest(facts=facts): + self.assertEqual(fresh_prompt.WORKER.fresh("Do it.", "## Goals", Route(Rung("m")), None, facts).count("Continue any open draft PR or worktree"), 1) + + def test_puts_the_rung_sentences_after_the_route_or_else_before_the_facts(self): + routed, unrouted = Route(OPUS_PROJECT.rung, 3, 2, 4), Route(OPUS_PROJECT.rung) + self.assertIn(f"routed by item 3's L of 2.{prompts.rungs_held(fresh_prompt.DRIVER, routed, OPUS_PROJECT)} After your first read", fresh_prompt.WORKER.fresh("Do it.", "## Goals", routed, OPUS_PROJECT, self.FACTS)) + self.assertIn(f"{fresh_prompt.NAME_THE_ITEMS}\n\n{prompts.rungs_held(fresh_prompt.DRIVER, unrouted, OPUS_PROJECT).lstrip()}\n\n{self.FACTS}", fresh_prompt.WORKER.fresh("Do it.", "## Goals", unrouted, OPUS_PROJECT, self.FACTS)) + + +if __name__ == "__main__": + unittest.main() diff --git a/bin/loop_claude/doubles.py b/bin/loop_claude/doubles.py new file mode 100644 index 00000000..74f6651a --- /dev/null +++ b/bin/loop_claude/doubles.py @@ -0,0 +1,22 @@ +"""Test doubles for loop-claude's suites: the shipped ladder's opus project, a plan and its release standing.""" +from model_routing.ladder import Ladder, ProjectRung, Rung +from release_plan.release_standing import LastCut +from model_routing.ladder_file import read_ladder, SHIPPED + +LADDER = read_ladder(SHIPPED) +assert isinstance(LADDER, Ladder), LADDER +OPUS_PROJECT = ProjectRung(LADDER, Rung("claude-opus-5-5", "medium")) + +# A plan read with no release cut yet. +NO_CUT = LastCut(None, "", 0, []) + +TODO = """# todo + +## Items + +| ID | Release | Exempt | Item | L | E | I | R | S | Moves | Score | +|---|---|---|---|---|---|---|---|---|---|---| +| 7 | 0.2.0 | question | **Ask first.** | 4 | 3 | 5 | 10 | 1 | 1 by 9 | 30 | +| 3 | 0.2.0 | | **Do the next thing.** | 4 | 2 | 3 | 7 | 3 | 1 by 9 | 25 | +| 1 | 0.3.0 | | **Do a later thing.** | 1 | 1 | 1 | 1 | 1 | 1 by 9 | 30 | +""" diff --git a/bin/loop_claude/loop/test_chunk_start.py b/bin/loop_claude/loop/test_chunk_start.py new file mode 100644 index 00000000..15e9fc6e --- /dev/null +++ b/bin/loop_claude/loop/test_chunk_start.py @@ -0,0 +1,149 @@ +import contextlib +import dataclasses +import io +import pathlib +import sys +import tempfile +import unittest +import unittest.mock + +sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[2])) + +from chunk_driver.doubles import a_loop +from loop_claude.loop import chunk_start +from project_config.doubles import configured +from release_plan.release_standing import DefaultPlan, LastCut, UNSEEN_RELEASES +from failure import Failure +from loop_effects.doubles import FakeEffects, ran, STAMP +from loop_claude.chunk import fresh_prompt +from loop_claude.doubles import NO_CUT, TODO +from loop_state import paths +from plan_store.todo_md import plan_of +from claude_code import session +from agent_cycle import tmux + + +class RouteSources(unittest.TestCase): + def test_routes_on_the_plan_item_and_says_why_it_routes_on_the_projects_rung(self): + self.assertIsNone(chunk_start.plan_item(DefaultPlan(plan_of(""), NO_CUT), None, FakeEffects().effects())) + self.assertEqual(chunk_start.plan_item(DefaultPlan(plan_of(TODO), LastCut("v0.1.0", "", 3, [TODO])), None, FakeEffects().effects()).id, 3) + self.assertIsNone(chunk_start.plan_item(DefaultPlan(plan_of(TODO), LastCut("v0.3.0", "", 3, [TODO])), None, FakeEffects().effects())) + + def test_routes_a_window_named_for_an_item_on_that_item_whatever_the_plan_takes_next(self): + plan = DefaultPlan(plan_of(TODO), LastCut("v0.1.0", "", 3, [TODO])) + later = next(item.id for item in plan.plan.items if item.id != 3) + self.assertEqual(chunk_start.plan_item(plan, later, FakeEffects().effects()).id, later) + # An item that left the plan, as once it shipped. + self.assertIsNone(chunk_start.plan_item(plan, 999, FakeEffects().effects())) + # A chunk start reads the default branch once, for routing and for scoring the items the worker names. + with unittest.mock.patch.object(chunk_start, "session_open", return_value=True), unittest.mock.patch.object(chunk_start, "fetched_plan", return_value=plan) as fetched: + loop = a_loop(FakeEffects().effects()) + self.assertEqual(chunk_start.chunk_start(loop), ("", plan)) + # An open session is no fresh chunk, so the human isn't told again of releases the plan cannot show. + fetched.assert_called_once_with(loop, False) + unreadable = Failure("git show origin/main:todo.md printed text that is not UTF-8") + for plan, said in ((DefaultPlan(plan_of(TODO.replace("| 4 | 2 | 3 |", "| x | 2 | 3 |")), NO_CUT), "routing on the project's rung: item 3's L 'x' is not a whole number from 1 to 4\n"), (DefaultPlan(unreadable, NO_CUT), f"routing on the project's rung: {unreadable.reason}\n"), (Failure("no origin remote"), "routing on the project's rung: no origin remote\n")): + with self.subTest(said=said): + with contextlib.redirect_stderr(io.StringIO()) as stderr: + self.assertIsNone(chunk_start.plan_item(plan, None, FakeEffects().effects())) + self.assertTrue(stderr.getvalue().startswith(f"{STAMP} {said}"), stderr.getvalue()) + + def test_says_why_the_items_the_worker_names_lift_no_rung(self): + unscored = plan_of(TODO.replace("| 4 | 2 | 3 |", "| x | 2 | 3 |")) + for default, said in ((DefaultPlan(unscored, NO_CUT), "the items the worker names lift no rung: item 3's L 'x' is not a whole number from 1 to 4\n"), (Failure("no git"), "the items the worker names lift no rung: no git\n"), (DefaultPlan(plan_of(TODO), NO_CUT), "")): + with self.subTest(said=said): + with contextlib.redirect_stderr(io.StringIO()) as stderr: + scored = chunk_start.scored_plan(default, FakeEffects().effects()) + self.assertEqual(stderr.getvalue(), f"{STAMP} {said}" if said else "") + self.assertEqual(scored, default.plan if isinstance(default, DefaultPlan) else default) + + def test_reads_the_changelog_the_default_branch_holds_through_the_loops_git(self): + def git(*args, **options): + answers = {"remote": "origin\n", "symbolic-ref": "origin/main\n", "show": f"what {args[-1]} holds\n"} + return answers.get(args[0], "1\n") + + with tempfile.TemporaryDirectory() as root: + folder = configured(self, pathlib.Path(root)) + effects = dataclasses.replace(FakeEffects().effects(), git=git) + held = chunk_start.fetched_plan(a_loop(effects, root=root, paths=paths.paths_from(folder.parents[1]))) + self.assertEqual(held.cut.changelog, "what origin/main:CHANGELOG.md holds\n") + + def test_reads_a_plan_outside_the_checkout_as_it_stands_and_stops_without_a_config(self): + with tempfile.TemporaryDirectory() as root: + folder = configured(self, pathlib.Path(root)) + loop = a_loop(FakeEffects(git=["origin\n", "origin/main\n"] * 2).effects(), root=root, paths=paths.paths_from(folder.parents[1])) + config = folder / "config.toml" + tracked = a_loop(FakeEffects(git=["origin\n", "origin/main\n", *[Failure("no git")] * 4]).effects(), root=root, paths=paths.paths_from(folder.parents[1])) + with contextlib.redirect_stderr(io.StringIO()) as stderr: + chunk_start.fetched_plan(tracked) + self.assertEqual(stderr.getvalue(), "", "a plan git tracks hides no release") + config.write_text(config.read_text().replace('"checkout:todo.md"', '"folder:todo.md"')) + (folder / "todo.md").write_text(TODO) + unread = Failure("no git") + standing = a_loop(FakeEffects(git=["origin\n", "origin/main\n", unread, unread, unread, unread]).effects(), root=root, paths=paths.paths_from(folder.parents[1])) + with contextlib.redirect_stderr(io.StringIO()) as stderr: + held = chunk_start.fetched_plan(standing) + self.assertEqual((held.plan, held.cut.todos_since, held.cut.changelog), (plan_of(TODO), [], ""), "the plan as it stands, whatever git answers") + self.assertEqual(stderr.getvalue(), f"{STAMP} {UNSEEN_RELEASES}\n", "a release review loop-claude cannot call due is never skipped silently") + quiet = a_loop(FakeEffects(git=["origin\n", "origin/main\n", unread, unread, unread, unread]).effects(), root=root, paths=paths.paths_from(folder.parents[1])) + with contextlib.redirect_stderr(io.StringIO()) as stderr: + chunk_start.fetched_plan(quiet, announce=False) + self.assertEqual(stderr.getvalue(), "", "said once per chunk, as it starts") + config.unlink() + with contextlib.redirect_stderr(io.StringIO()) as stderr: + self.assertIn("no config names the checkout", chunk_start.scored_plan(chunk_start.fetched_plan(loop), loop.effects).reason) + self.assertIn("the items the worker names lift no rung: no config names the checkout", stderr.getvalue()) + self.assertIn("no config names the checkout", chunk_start.fetched_plan(loop).reason) + + +class ChunkFacts(unittest.TestCase): + # The launcher beside which chunk_start finds /b/chunk-preflight. + LAUNCHER = pathlib.Path("/b/loop-claude") + + def test_runs_the_preflight_at_the_checkout_root_and_quotes_what_it_printed(self): + fake = FakeEffects(python=[ran("- A fact.\n")]) + said = chunk_start.chunk_facts(a_loop(fake.effects(), launcher=self.LAUNCHER)) + self.assertEqual(fake.made("python"), [(["/b/chunk-preflight", "--worktree", "--item", "7"], "/r")]) + self.assertTrue(said.endswith("\n\n- A fact."), said) + self.assertNotIn("yourself", said) + + def test_a_chunk_without_an_item_runs_the_preflight_without_naming_one(self): + fake = FakeEffects(python=[ran("- A fact.\n")]) + chunk_start.chunk_facts(a_loop(fake.effects(), launcher=self.LAUNCHER, window=tmux.Window("loop-proj", "no-item", "%4"))) + self.assertEqual(fake.made("python"), [(["/b/chunk-preflight", "--worktree"], "/r")]) + + def test_the_facts_end_with_the_items_the_projects_other_running_chunks_hold(self): + # The loop's own window, a stopped one and two that run, one of them waiting for the human. + fake = FakeEffects(python=[ran("- A fact.\n")], windows="item-7\t0\t@1\nitem-5\t1\t@2\nitem-12 ?\t0\t@3\nitem-9\t0\t@4\n") + said = chunk_start.chunk_facts(a_loop(fake.effects(), launcher=self.LAUNCHER)) + self.assertTrue(said.endswith("- A fact.\n\nOther chunks of this project, running or stopped, hold items 5, 9, 12. Leave those items out of this chunk, even where the facts above list them as items that may join it."), said) + self.assertEqual(fresh_prompt.held_by_others([9]), "\n\nOther chunks of this project, running or stopped, hold item 9. Leave those items out of this chunk, even where the facts above list them as items that may join it.") + self.assertTrue(fresh_prompt.held_by_others([4, 9]).startswith("\n\nOther chunks of this project, running or stopped, hold items 4, 9.")) + # A worker whose preflight failed is told them too. + fake = FakeEffects(python=[ran(code=1, stderr="git could not start\n")], windows="item-9\t0\t@4\n") + said = chunk_start.chunk_facts(a_loop(fake.effects(), launcher=self.LAUNCHER)) + self.assertTrue(said.endswith("Other chunks of this project, running or stopped, hold item 9. Leave those items out of this chunk, even where the facts above list them as items that may join it."), said) + + def test_has_the_worker_find_the_facts_itself_where_the_preflight_failed(self): + for failed, why in ((ran(code=1, stderr="git could not start\n"), "git could not start"), (ran(code=1), "exited 1")): + with self.subTest(why=why): + said = chunk_start.chunk_facts(a_loop(FakeEffects(python=[failed]).effects(), launcher=self.LAUNCHER)) + self.assertIn("yourself", said) + self.assertIn(why, said) + + def test_runs_the_preflight_only_where_the_session_is_not_open(self): + with tempfile.TemporaryDirectory() as home: + opened = session.session_named("A proj loop-claude", pathlib.Path(home, "projects")) + fresh = FakeEffects(python=[ran("- A fact.\n")]) + facts, _ = chunk_start.chunk_start(a_loop(fresh.effects(), session=opened, launcher=self.LAUNCHER)) + self.assertEqual((fresh.made("python"), "- A fact." in facts), ([(["/b/chunk-preflight", "--worktree", "--item", "7"], "/r")], True)) + transcript = pathlib.Path(home, "projects", "-fake", f"{opened.id}.jsonl") + transcript.parent.mkdir(parents=True) + transcript.write_text("") + resumed = FakeEffects() + facts, _ = chunk_start.chunk_start(a_loop(resumed.effects(), session=opened, launcher=self.LAUNCHER)) + self.assertEqual((resumed.made("python"), facts), ([], "")) + + +if __name__ == "__main__": + unittest.main() diff --git a/bin/loop_claude/loop/test_chunk_windows.py b/bin/loop_claude/loop/test_chunk_windows.py new file mode 100644 index 00000000..9376890f --- /dev/null +++ b/bin/loop_claude/loop/test_chunk_windows.py @@ -0,0 +1,252 @@ +import contextlib +import io +import json +import pathlib +import sys +import tempfile +import unittest +import unittest.mock + +sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[2])) + +from loop_effects.doubles import assistant, FakeEffects +from loop_claude.loop import chunk_windows +from project_config.doubles import configured +from failure import Failure +from loop_claude.loop import main +from release_plan import next_chunk +from loop_state import paths +from agent_cycle import tmux +from touch_predictions.doubles import plan as touch_plan + + +class ChunkWindows(unittest.TestCase): + def plan(self, *touched): + """A plan of items 1, 2 and on, each predicted to touch the path given for it.""" + items = touch_plan(*((number, "1.0.0", "", f"- `{path}`") for number, path in enumerate(touched, 1))) + return next_chunk.NextChunk(items=items, first=items[0], text="", branch_template="") + + def test_starts_the_next_item_and_those_picked_beside_it_up_to_the_limit(self): + # Item 2 overlaps item 1, so it waits for it; items 3 and 4 run beside it. + plan = self.plan("a.py", "a.py", "c.py", "d.py") + self.assertEqual(chunk_windows.may_start(plan, [], set(), 1), ["item-1"]) + self.assertEqual(chunk_windows.may_start(plan, [], set(), 2), ["item-1", "item-3"]) + self.assertEqual(chunk_windows.may_start(plan, [], set(), 9), ["item-1", "item-3", "item-4"]) + + def test_leaves_the_items_running_chunks_hold_and_starts_only_as_many_as_the_room_left(self): + plan = self.plan("a.py", "a.py", "c.py", "d.py") + self.assertEqual(chunk_windows.may_start(plan, ["item-1"], {1, 3}, 1), ["item-4"]) + self.assertEqual(chunk_windows.may_start(plan, ["item-1"], {1}, 2), ["item-3", "item-4"]) + self.assertEqual(chunk_windows.may_start(plan, ["item-1"], {1}, 0), []) + + def test_counts_the_running_chunks_against_the_limit_and_restarts_stopped_ones_first(self): + windows = [tmux.Listed("item-1", "", False), tmux.Listed("item-3", "", True), tmux.Listed("item-4", "", True)] + self.assertEqual(chunk_windows.room_of(windows, "item-1", False, 3), chunk_windows.Room(["item-1"], ["item-3", "item-4"], ["item-3", "item-4"], 0)) + self.assertEqual(chunk_windows.room_of(windows, "item-1", False, 2), chunk_windows.Room(["item-1"], ["item-3", "item-4"], ["item-3"], 0)) + self.assertEqual(chunk_windows.room_of(windows, "item-1", False, 5), chunk_windows.Room(["item-1"], ["item-3", "item-4"], ["item-3", "item-4"], 2)) + + def test_a_loop_whose_chunk_finished_leaves_its_room_and_restarts_no_stopped_chunk(self): + windows = [tmux.Listed("item-1", "", False), tmux.Listed("item-3", "", True)] + self.assertEqual(chunk_windows.room_of(windows, "item-1", True, 2), chunk_windows.Room([], ["item-3"], [], 2)) + + def test_leaves_no_room_where_more_chunks_run_than_the_limit(self): + windows = [tmux.Listed(f"item-{n}", "", False) for n in (1, 2, 3)] + self.assertEqual(chunk_windows.room_of(windows, "item-1", False, 2).left, 0) + + def test_starts_no_item_that_overlaps_a_running_chunks_items(self): + # Item 1 merged, so 2 is the plan's next item; it overlaps item 3, which still runs, at c.py. + items = touch_plan((2, "1.0.0", "", "- `a.py`\n- `c.py`"), (3, "1.0.0", "", "- `c.py`"), (4, "1.0.0", "", "- `d.py`")) + plan = next_chunk.NextChunk(items=items, first=items[0], text="", branch_template="") + self.assertEqual(chunk_windows.may_start(plan, ["item-3"], {3}, 2), ["item-4"]) + # A running chunk's item without a prediction leaves no other item free to start beside it. + unpredicted = touch_plan((2, "1.0.0", "", "- `a.py`"), (3, "1.0.0", "", None)) + self.assertEqual(chunk_windows.may_start(next_chunk.NextChunk(items=unpredicted, first=unpredicted[0], text="", branch_template=""), ["item-3"], {3}, 2), []) + # A held item that left the plan, as once it merged, overlaps nothing. + self.assertEqual(chunk_windows.may_start(plan, ["item-9"], {9}, 2), ["item-2", "item-4"]) + + def test_a_plan_without_an_item_starts_one_chunk_and_only_alone(self): + none = next_chunk.NoItem("release 1.0.0 has no items") + self.assertEqual(chunk_windows.may_start(none, [], set(), 3), ["no-item"]) + self.assertEqual(chunk_windows.may_start(none, ["item-1"], {1}, 3), []) + + def test_names_a_window_for_its_item_and_reads_the_item_back(self): + self.assertEqual(chunk_windows.window_name(372), "item-372") + self.assertEqual([chunk_windows.item_of(chunk) for chunk in ("item-372", "no-item", "item-", "item-3x", "xitem-3")], [372, None, None, None, None]) + # Only ASCII digits name an item, as window_name writes them. + self.assertIsNone(chunk_windows.item_of("item-\u0663")) + + def test_starts_only_the_next_item_where_the_touch_predictions_cannot_be_read(self): + with unittest.mock.patch.object(chunk_windows, "chunks_beside", return_value="item 1's Touches: list cannot be read"): + self.assertEqual(chunk_windows.may_start(self.plan("a.py", "c.py"), [], set(), 3), ["item-1"]) + + def test_a_chunk_whose_transcript_cannot_be_read_holds_the_item_it_started_from(self): + with tempfile.TemporaryDirectory() as temp: + transcripts = pathlib.Path(temp) + folder = transcripts / "-r" + folder.mkdir() + # A folder in place of the transcript file, so reading it fails. + (folder / f"{chunk_windows.chunk_session('proj', 'item-7', transcripts).id}.jsonl").mkdir() + self.assertEqual(chunk_windows.held_items(["item-7"], "proj", transcripts), {7}) + + def test_tells_a_chunk_the_items_the_projects_other_live_chunks_hold(self): + with tempfile.TemporaryDirectory() as temp: + home = pathlib.Path(temp) + held = paths.paths_from(home) + folder = held.claude_code_transcripts / "-r" + folder.mkdir(parents=True) + lines = [assistant({"type": "text", "text": "Chunk items: 8 (L 1, E + I 2), 11 (L 2, E + I 3)"})] + (folder / f"{chunk_windows.chunk_session('r', 'item-8', held.claude_code_transcripts).id}.jsonl").write_text("".join(json.dumps(line) + "\n" for line in lines)) + # Its own window, item-7, holds nothing for it; item-9's stopped chunk holds its item. + fake = FakeEffects(windows="item-7\t0\t@1\nitem-8 ?\t0\t@2\nitem-9\t1\t@3\nitem-10\t0\t@4\n") + self.assertEqual(chunk_windows.held_beside("item-7", "/r", held, fake.effects()), [8, 9, 10, 11]) + self.assertEqual(fake.tmux_calls, [["list-windows", "-t", "=loop-r", "-F", "#{window_name}\t#{pane_dead}\t#{window_id}"]]) + + +class StartChunks(unittest.TestCase): + """start_chunks: the windows a loop opens for the chunks that may start now.""" + LAUNCHER = pathlib.Path("/b/loop-claude") + + def setUp(self): + made = tempfile.TemporaryDirectory() + self.addCleanup(made.cleanup) + self.root = pathlib.Path(made.name, "r") + self.home = configured(self, self.root).parents[1] + self.session = "loop-" + str(self.root).lstrip("/").replace("/", "-").replace(".", "-") + # Items 1 and 2 overlap, so 2 waits; 3 runs beside 1. + self.plan = ChunkWindows.plan(self, "a.py", "a.py", "c.py") + + def limit(self, chunks): + config = self.home / ".claude-projects" / self.root.name / "config.toml" + config.write_text(config.read_text() + f"parallel-chunks = {chunks}\n") + + def start(self, fake, chunk=None, leaving=False, plan=None, env=None, owner=None): + """start_chunks' answer, for a session whose @loop-root names owner, by default this checkout.""" + fake.owner = str(self.root) if owner is None else owner + run = main.LoopRun(0, str(self.root), "ship it", "", chunk=chunk) + with unittest.mock.patch.object(chunk_windows, "next_plan", return_value=plan or self.plan) as planned, contextlib.redirect_stderr(io.StringIO()) as said: + effects = fake.effects() + started = chunk_windows.start_chunks(run, leaving, paths.paths_from(self.home), env or {}, self.LAUNCHER, effects) + self.said = said.getvalue() + self.planned = planned.call_count + if planned.call_count: + planned.assert_called_once_with(str(self.root), self.home, effects) + return started + + def starts(self, fake): + """The window starts of fake's tmux calls, each as (command, window name, what it runs).""" + return [(call[0], call[call.index("-n") + 1], call[call.index("--") + 1:]) for call in fake.tmux_calls if call[0] in ("new-session", "new-window")] + + def test_opens_a_window_for_each_chunk_that_may_start_the_first_making_the_session(self): + self.limit(2) + fake = FakeEffects() + self.assertEqual(self.start(fake, env={"LOOP_CLAUDE_WORKER_MODEL": "m", "PATH": "/bin"}), ["item-1", "item-3"]) + self.assertEqual(self.starts(fake), [("new-session", "item-1", [str(self.LAUNCHER), "--chunk", "item-1", "ship it"]), ("new-window", "item-3", [str(self.LAUNCHER), "--chunk", "item-3", "ship it"])]) + first = next(call for call in fake.tmux_calls if call[0] == "new-session") + self.assertEqual((first[first.index("-s") + 1], first[first.index("-c") + 1]), (self.session, str(self.root))) + # Only loop-claude's own variables are handed on, since a window gets the tmux server's environment. + self.assertEqual([word for word in first if "=" in word], ["LOOP_CLAUDE_WORKER_MODEL=m"]) + self.assertEqual(fake.tmux_calls[0], ["list-windows", "-t", f"={self.session}", "-F", "#{window_name}\t#{pane_dead}\t#{window_id}"]) + # The session names the checkout whose chunks it runs. + self.assertIn(["set-option", "-t", f"={self.session}:", "@loop-root", str(self.root)], fake.tmux_calls) + + def test_refuses_a_session_that_runs_another_checkouts_chunks(self): + for owner, named in (("/c/acme.api", "/c/acme.api"), ("", "another checkout")): + with self.subTest(owner=owner): + fake = FakeEffects(windows="item-5\t0\t@1\n") + self.assertEqual(self.start(fake, owner=owner), Failure(f"the tmux session {self.session} runs the chunks of {named}, whose key maps to the same session name; rename one of the two checkouts' folders")) + self.assertEqual(self.starts(fake), []) + self.assertIn(["show-options", "-qv", "-t", f"={self.session}:", "@loop-root"], fake.tmux_calls) + + def test_refuses_a_checkout_do_epic_runs_chunks_in(self): + fake = FakeEffects(windows=f"epic-ABC-12\tABC-34 ?\t0\t@4\t{self.root}\nepic-ABC-12\tABC-35\t0\t@5\t/c/other\nepic-ABC-12\tABC-36\t1\t@6\t{self.root}\nloop-x\titem-1\t0\t@7\t{self.root}\n") + self.assertEqual(self.start(fake), Failure(f"do-epic runs chunks in {self.root}: ABC-34 of epic-ABC-12; loop-claude doesn't start a chunk there until those chunks finish")) + self.assertEqual(self.starts(fake), []) + + def test_leaves_out_the_items_a_running_chunk_names_on_its_chunk_items_line(self): + self.limit(2) + transcripts = paths.paths_from(self.home).claude_code_transcripts + folder = transcripts / "-r" + folder.mkdir(parents=True) + opened = chunk_windows.chunk_session(str(self.root).lstrip("/"), "item-5", transcripts) + (folder / f"{opened.id}.jsonl").write_text(json.dumps(assistant({"type": "text", "text": "Chunk items: 5 (L 1, E + I 2), 1 (L 1, E + I 2)"})) + "\n") + self.assertEqual(self.start(FakeEffects(windows="item-5\t0\t@1\n")), ["item-3"]) + + def test_starts_one_chunk_without_parallel_chunks(self): + fake = FakeEffects() + self.assertEqual(self.start(fake), ["item-1"]) + + def test_starts_none_and_reads_no_plan_while_the_running_chunks_fill_the_limit(self): + self.limit(2) + fake = FakeEffects(windows="item-5\t0\t@1\nitem-6 ?\t0\t@2\n") + self.assertEqual((self.start(fake), self.planned, self.starts(fake)), ([], 0, [])) + # More chunks run than the limit allows, as after it was lowered; a stopped chunk still waits. + fake = FakeEffects(windows="item-5\t0\t@1\nitem-6\t0\t@2\nitem-7\t0\t@3\nitem-8\t1\t@4\nitem-9\t1\t@5\n") + self.assertEqual((self.start(fake), self.planned), ([], 0)) + + def test_a_finishing_chunks_own_window_counts_as_no_longer_running(self): + fake = FakeEffects(windows="item-5\t0\t@1\n") + self.assertEqual(self.start(fake, chunk="item-5"), []) + fake = FakeEffects(windows="item-5\t0\t@1\n") + self.assertEqual(self.start(fake, chunk="item-5", leaving=True), ["item-1"]) + # The session exists, since the finishing loop's window is still in it. + self.assertEqual(self.starts(fake)[0][0], "new-window") + + def test_the_humans_loop_claude_starts_a_stopped_chunk_again_first_closing_its_dead_window(self): + fake = FakeEffects(windows="item-5\t1\t@3\n") + self.assertEqual((self.start(fake), self.planned), (["item-5"], 0)) + self.assertIn(["kill-window", "-t", "@3"], fake.tmux_calls) + # Its dead window was the session's last, so the session is made again. + self.assertEqual(self.starts(fake)[0][0], "new-session") + self.limit(2) + fake = FakeEffects(windows="item-5\t1\t@3\n") + self.assertEqual(self.start(fake), ["item-5", "item-1"]) + + def test_a_plan_with_no_item_starts_nothing_beside_a_restarted_chunk(self): + self.limit(2) + fake = FakeEffects(windows="item-5\t1\t@3\n") + self.assertEqual(self.start(fake, plan=next_chunk.NoItem("release 1.0.0 has no items")), ["item-5"]) + + def test_a_finishing_chunk_leaves_a_stopped_chunk_and_its_items_to_the_human(self): + self.limit(2) + transcripts = paths.paths_from(self.home).claude_code_transcripts + folder = transcripts / "-r" + folder.mkdir(parents=True) + opened = chunk_windows.chunk_session(str(self.root).lstrip("/"), "item-5", transcripts) + (folder / f"{opened.id}.jsonl").write_text(json.dumps(assistant({"type": "text", "text": "Chunk items: 5 (L 1, E + I 2), 1 (L 1, E + I 2)"})) + "\n") + fake = FakeEffects(windows="item-7\t0\t@1\nitem-5\t1\t@3\n") + # Item 1 is the stopped chunk's, so item 3 starts; the stopped one doesn't count against parallel-chunks. + self.assertEqual(self.start(fake, chunk="item-7", leaving=True), ["item-3"]) + self.assertEqual([call for call in fake.tmux_calls if call[0] == "kill-window"], []) + fake = FakeEffects(windows="item-7\t0\t@1\nno-item\t1\t@3\n") + self.assertEqual(self.start(fake, chunk="item-7", leaving=True, plan=next_chunk.NoItem("release 1.0.0 has no items")), []) + + def test_starts_the_chunk_without_a_plan_item_where_the_plan_cannot_be_read(self): + fake = FakeEffects() + self.assertEqual(self.start(fake, plan=Failure("no origin/main")), ["no-item"]) + self.assertIn("starting a chunk without a plan item, since the plan cannot be read: no origin/main", self.said) + + def test_says_why_a_window_could_not_start(self): + with unittest.mock.patch.object(tmux, "start_window", return_value=Failure("tmux could not start the window")): + self.assertEqual(self.start(FakeEffects()), Failure("tmux could not start the window")) + + def test_says_why_it_cannot_take_the_lock_on_starting_windows(self): + (self.home / ".local" / "state").mkdir(parents=True) + (self.home / ".local" / "state" / "loop-claude").write_text("") + started = self.start(FakeEffects()) + self.assertIsInstance(started, Failure) + self.assertTrue(started.reason.startswith(f"cannot lock {self.home}/.local/state/loop-claude/"), started.reason) + + def test_a_chunk_holds_its_own_item_and_those_on_its_latest_chunk_items_line(self): + with tempfile.TemporaryDirectory() as temp: + transcripts = pathlib.Path(temp) + opened = chunk_windows.chunk_session("proj", "item-7", transcripts) + self.assertEqual(opened.name, "A proj loop-claude item-7") + folder = transcripts / "-r" + folder.mkdir() + lines = [assistant({"type": "text", "text": "Chunk items: 7 (L 1, E + I 2)"}), assistant({"type": "text", "text": "Chunk items: 7 (L 1, E + I 2), 9 (L 2, E + I 3)"})] + (folder / f"{opened.id}.jsonl").write_text("".join(json.dumps(line) + "\n" for line in lines)) + self.assertEqual(chunk_windows.held_items(["item-7", "item-8", "no-item"], "proj", transcripts), {7, 8, 9}) + + +if __name__ == "__main__": + unittest.main() diff --git a/bin/loop_claude/loop/test_main.py b/bin/loop_claude/loop/test_main.py new file mode 100644 index 00000000..0f37bee2 --- /dev/null +++ b/bin/loop_claude/loop/test_main.py @@ -0,0 +1,267 @@ +import contextlib +import dataclasses +import io +import json +import pathlib +import sys +import tempfile +import unittest +import unittest.mock + +sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[2])) + +from chunk_driver.doubles import a_loop +from loop_effects.doubles import CWD, FakeEffects, ran, STAMP +from failure import Failure +from agent_cycle.verdicts import Finished, Unfinished +from loop_claude.loop import main, reload, usage +from loop_claude.doubles import OPUS_PROJECT +from loop_state import paths +from model_routing.route import Route +from model_routing.ladder import Rung +from claude_code import session +from agent_cycle.stop import Stop +from chunk_driver import this_loop +from agent_cycle import tmux + + +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", "unfinished_evidence": ""})], [json.dumps({"cycle_number": 3, "root": "/r", "task": "", "unfinished_evidence": ""})], [json.dumps({"cycle_number": "3", "root": "/r", "task": "do it", "unfinished_evidence": ""})], [json.dumps({"cycle_number": 3, "root": "", "task": "do it", "unfinished_evidence": ""})], [json.dumps({"cycle_number": 3, "root": "/r", "task": "do it", "turns": 1, "unfinished_evidence": ""})], [json.dumps({"cycle_number": 3, "failures": 0, "root": "/r", "task": "do it", "unfinished_evidence": ""})]): + with self.subTest(handed=handed): + self.assertEqual(main.loop_run_from([main.RELOAD_FLAG, *handed], CWD, FakeEffects().effects()), Stop(1, f"the edited loop-claude cannot read the state the running one handed it, likely because the edit changed that handover; rerun loop-claude . Handed: {' '.join(handed)}")) + + def test_decodes_every_field_of_the_state_and_its_route(self): + state = main.LoopRun(cycle_number=2, root="/r", task="do it", unfinished_evidence="old", route=Route(Rung("claude-fable-5-2", "low"), 3, 4, 5, 1, 7, 9), unfinished_run=2) + self.assertEqual(reload.decode_handed(json.dumps(dataclasses.asdict(state))), state) + self.assertEqual(set(reload.LOOP_RUN_FIELDS), {field.name for field in dataclasses.fields(main.LoopRun)}) + + def test_rejects_a_field_of_another_type_or_an_unknown_field(self): + state = dataclasses.asdict(main.LoopRun(cycle_number=2, root="/r", task="do it", unfinished_evidence="", route=Route(Rung("claude-fable-5-2", "low"), 3, 4, 5, 1, 7, 9), unfinished_run=2)) + for path, wrong in ((("unfinished_run",), "2"), (("unfinished_run",), True), (("route", "unfinished"), 1.0), (("route", "escalated_at"), "9"), (("route", "item"), True), (("route", "rung"), "claude-opus-5-5"), (("route", "rung", "model"), None), (("route",), "r"), (("route", "extra"), 1), (("route", "model"), "claude-opus-5-5"), (("route", "effort"), "high")): + with self.subTest(path=path, wrong=wrong): + handed = json.loads(json.dumps(state)) + target = handed + for key in path[:-1]: + target = target[key] + target[path[-1]] = wrong + self.assertIsNone(reload.decode_handed(json.dumps(handed))) + + def test_rejects_a_state_or_rung_missing_a_required_field(self): + state = dataclasses.asdict(main.LoopRun(cycle_number=2, root="/r", task="do it", unfinished_evidence="", route=Route(Rung("claude-fable-5-2", "low")))) + for path in (("cycle_number",), ("root",), ("task",), ("unfinished_evidence",), ("route", "rung"), ("route", "rung", "model")): + with self.subTest(path=path): + handed = json.loads(json.dumps(state)) + target = handed + for key in path[:-1]: + target = target[key] + del target[path[-1]] + self.assertIsNone(reload.decode_handed(json.dumps(handed))) + + def test_decodes_a_rung_without_an_effort(self): + state = main.LoopRun(cycle_number=1, root="/r", task="do it", unfinished_evidence="", route=Route(Rung("some-model"))) + self.assertEqual(reload.decode_handed(json.dumps({**dataclasses.asdict(state), "route": {"rung": {"model": "some-model"}}})), state) + + def test_rejects_text_that_is_not_json(self): + self.assertIsNone(reload.decode_handed("not json")) + + def test_decodes_a_route_inside_the_state_or_none(self): + state = main.LoopRun(cycle_number=1, root="/r", task="do it", unfinished_evidence="", route=Route(Rung("claude-fable-5-2", "low"), 3, 4, 5, 1)) + self.assertEqual(reload.decode_handed(json.dumps(dataclasses.asdict(state))), state) + unrouted = dataclasses.replace(state, route=None) + self.assertEqual(reload.decode_handed(json.dumps(dataclasses.asdict(unrouted))), unrouted) + handed = dataclasses.asdict(state) + handed["route"]["rung"]["effort"] = 1 + self.assertIsNone(reload.decode_handed(json.dumps(handed))) + del handed["route"]["rung"] + self.assertIsNone(reload.decode_handed(json.dumps(handed))) + off_scale = dataclasses.asdict(state) + off_scale["route"]["likelihood"] = 9 + self.assertIsNone(reload.decode_handed(json.dumps(off_scale))) + older = dataclasses.asdict(state) + del older["route"]["escape_and_impact_taken_in"] + self.assertEqual(reload.decode_handed(json.dumps(older)), state) + taken = dataclasses.replace(state, route=dataclasses.replace(state.route, escape_and_impact_taken_in=7)) + self.assertEqual(reload.decode_handed(json.dumps(dataclasses.asdict(taken))), taken) + for taken_in in (9, 5): + with self.subTest(taken_in=taken_in): + handed = dataclasses.asdict(state) + handed["route"]["escape_and_impact_taken_in"] = taken_in + self.assertIsNone(reload.decode_handed(json.dumps(handed))) + + def test_decodes_a_state_from_before_a_field_with_a_default_joined_it(self): + self.assertEqual(reload.decode_handed(json.dumps({"cycle_number": 3, "root": "/r", "task": "do it", "unfinished_evidence": ""})), main.LoopRun(3, "/r", "do it", "")) + + def test_drops_the_items_taken_in_that_a_loop_started_before_0_11_0_hands_on(self): + state = main.LoopRun(cycle_number=1, root="/r", task="do it", unfinished_evidence="") + self.assertEqual(reload.decode_handed(json.dumps(dataclasses.asdict(state) | {"items_taken_in": [12, 14]})), state) + + def test_drops_the_slot_that_a_loop_started_before_chunks_ran_in_windows_hands_on(self): + state = main.LoopRun(cycle_number=1, root="/r", task="do it", unfinished_evidence="") + self.assertEqual(reload.decode_handed(json.dumps({key: value for key, value in dataclasses.asdict(state).items() if key != "chunk"} | {"slot": 1})), state) + + def test_stops_when_the_reload_cannot_start_python(self): + with tempfile.TemporaryDirectory() as root: + launcher = pathlib.Path(root, "loop-claude") + launcher.write_text("") + fake = FakeEffects(python=[ran()]) + self.assertEqual(reload.exec_if_edited(a_loop(fake.effects(), launcher=launcher), "stale", main.LoopRun(cycle_number=1, root="/r", task="do it", unfinished_evidence="")), Stop(1, "could not reload loop-claude: No such file or directory; rerun loop-claude ")) + + +class WindowStarts(unittest.TestCase): + """The loop-claude the human runs, which starts the windows, and the stop of a window's loop whose chunk finished.""" + HOME = pathlib.Path("/h") + + def run_loop(self, argv, fake, env=None, started=("item-3", "item-4")): + """run_loop's stop and what it said on stdout, with start_chunks answering `started`; self.asked holds what start_chunks was asked.""" + with unittest.mock.patch.object(main, "start_chunks", return_value=list(started) if isinstance(started, tuple) else started) as start, contextlib.redirect_stdout(io.StringIO()) as said: + stop = main.run_loop(argv, env or {}, paths.paths_from(self.HOME), pathlib.Path("/b/loop-claude"), CWD, fake.effects()) + self.asked = [call.args[:2] for call in start.call_args_list] + return stop, said.getvalue() + + def test_refuses_to_start_anything_on_a_tmux_older_than_needed(self): + fake = FakeEffects(git=["/r\n"], tmux_version="tmux 3.1c") + self.assertEqual(self.run_loop(["do it"], fake), (Stop(1, "loop-claude runs each chunk in a tmux window, and needs tmux 3.2 or later; it found tmux 3.1c"), "")) + self.assertEqual((fake.tmux_calls, self.asked), ([["-V"]], [])) + + def test_prints_the_help_without_tmux(self): + fake = FakeEffects(tmux_version="") + self.assertEqual(self.run_loop(["--help"], fake)[0], Stop(0, usage.HELP)) + self.assertEqual(fake.tmux_calls, []) + + def test_starts_the_chunks_then_attaches_to_the_session_or_inside_tmux_switches_to_it(self): + for env, shown in (({}, ["attach-session", "-t", "=loop-r"]), ({"TMUX": "/tmp/tmux-1000/default,1,0"}, ["switch-client", "-t", "=loop-r"])): + with self.subTest(shown=shown): + fake = FakeEffects(git=["/r\n"]) + stop, said = self.run_loop(["do", "it"], fake, env) + self.assertEqual((stop, said), (Stop(0, ""), f"{STAMP} started item-3, item-4 in the tmux session loop-r\n")) + self.assertEqual(fake.tmux_calls, [["-V"], shown]) + self.assertEqual(self.asked, [(main.LoopRun(0, "/r", "do it", ""), False)]) + + def test_hands_the_window_starts_its_paths_environment_launcher_and_effects(self): + fake = FakeEffects(git=["/r\n"], on_terminal=False) + effects, env, launcher = fake.effects(), {"LOOP_CLAUDE_X": "1"}, pathlib.Path("/b/loop-claude") + with unittest.mock.patch.object(main, "start_chunks", return_value=["item-3"]) as start, contextlib.redirect_stdout(io.StringIO()): + main.run_loop(["do it"], env, paths.paths_from(self.HOME), launcher, CWD, effects) + self.assertEqual(start.call_args.args, (main.LoopRun(0, "/r", "do it", ""), False, paths.paths_from(self.HOME), env, launcher, effects)) + + def test_a_window_loop_runs_its_chunk_in_its_own_session_and_window_and_hands_on_once_it_finished(self): + fake = FakeEffects(git=["/r\n"]) + effects, env = fake.effects(), {"TMUX_PANE": "%4"} + launcher = pathlib.Path("/b/loop-claude") + cycle = unittest.mock.Mock(project=OPUS_PROJECT) + unfinished = unittest.mock.Mock(verdict=unittest.mock.Mock(spec=Unfinished)) + finished = unittest.mock.Mock(verdict=unittest.mock.Mock(spec=Finished)) + closed = [main.LoopRun(1, "/r", "do it", "", chunk="item-3"), main.LoopRun(2, "/r", "do it", "", chunk="item-3")] + with unittest.mock.patch.object(main, "fingerprint", return_value=""), unittest.mock.patch.object(main, "exec_if_edited", return_value=None), unittest.mock.patch.object(main, "next_cycle", return_value=cycle) as next_cycle, \ + unittest.mock.patch.object(main, "run_cycle", side_effect=[unfinished, finished]), unittest.mock.patch.object(main, "closed_cycle", side_effect=closed), \ + unittest.mock.patch.object(main, "check_routing", return_value=None), unittest.mock.patch.object(main, "handed_on", return_value=Stop(0, "")) as handed_on: + stop = main.run_loop(["--chunk", "item-3", "do it"], env, paths.paths_from(self.HOME), launcher, CWD, effects) + 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=paths.paths_from(self.HOME), env=env, launcher=launcher, effects=effects) + self.assertEqual(stop, Stop(0, "")) + self.assertEqual([call.args for call in next_cycle.call_args_list], [(main.LoopRun(1, "/r", "do it", "", chunk="item-3"), loop), (main.LoopRun(2, "/r", "do it", "", chunk="item-3"), loop)]) + handed_on.assert_called_once_with(closed[1], loop) + + 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)) + + def test_a_window_loop_outside_a_checkout_stops(self): + self.assertIsInstance(main.loop_run_from(["--chunk", "item-3", "do it"], CWD, FakeEffects(git=[Failure("git cannot start")]).effects()), Stop) + + def test_says_that_nothing_started_where_the_session_already_runs_every_chunk_that_may(self): + self.assertEqual(self.run_loop(["do it"], FakeEffects(git=["/r\n"]), started=())[1], f"{STAMP} the tmux session loop-r runs every chunk that may run now, so `do it` started in no new window\n") + + def test_off_a_terminal_it_prints_how_to_attach(self): + fake = FakeEffects(git=["/r\n"], on_terminal=False) + stop, said = self.run_loop(["do it"], fake) + self.assertEqual(stop, Stop(0, "")) + self.assertTrue(said.endswith(f"{STAMP} not on a terminal; see the chunks with `tmux attach -t =loop-r`\n"), said) + self.assertEqual(fake.tmux_calls, [["-V"]]) + + def test_stops_where_no_chunk_could_start(self): + self.assertEqual(self.run_loop(["do it"], FakeEffects(git=["/r\n"]), started=Failure("no plan"))[0], Stop(1, "no plan")) + + def test_stops_where_tmux_cannot_show_the_session(self): + fake = FakeEffects(git=["/r\n"]) + effects = dataclasses.replace(fake.effects(), tmux_on_terminal=lambda args: 1) + with unittest.mock.patch.object(main, "start_chunks", return_value=["item-3"]), contextlib.redirect_stdout(io.StringIO()): + stop = main.run_loop(["do it"], {}, paths.paths_from(self.HOME), pathlib.Path("/b/loop-claude"), CWD, effects) + self.assertEqual(stop, Stop(1, "`tmux attach-session -t =loop-r` failed; the chunks go on in the tmux session")) + + def test_a_window_loop_outside_tmux_stops(self): + fake = FakeEffects(git=["/r\n"]) + self.assertEqual(self.run_loop(["--chunk", "item-3", "do it"], fake)[0], Stop(1, "--chunk is how loop-claude starts itself in a tmux window; run loop-claude ")) + self.assertEqual(self.asked, []) + + def test_reads_the_chunk_a_window_loop_is_started_with(self): + self.assertEqual(main.loop_run_from(["--chunk", "item-3", "do", "it"], CWD, FakeEffects(git=["/r\n"]).effects()), main.LoopRun(0, "/r", "do it", "", chunk="item-3")) + self.assertEqual(main.loop_run_from(["do", "it"], CWD, FakeEffects(git=["/r\n"]).effects()), main.LoopRun(0, "/r", "do it", "")) + + def test_a_finished_chunk_starts_each_chunk_that_may_start_and_stops_without_a_reason(self): + loop_run = main.LoopRun(4, "/r", "do it", "", chunk="item-7") + for started, said, stop in ((["item-9", "item-12"], "the chunk finished; started item-9, item-12", Stop(0, "")), ([], "the chunk finished; started no other, since the chunks running fill what may run now", Stop(0, "")), + (Failure("no plan"), "", Stop(1, "the chunk finished, but the next could not start: no plan"))): + with self.subTest(started=started), unittest.mock.patch.object(main, "start_chunks", return_value=started) as start, contextlib.redirect_stdout(io.StringIO()) as printed: + loop = a_loop(FakeEffects().effects(), env={"LOOP_CLAUDE_X": "1"}) + self.assertEqual(main.handed_on(loop_run, loop), stop) + self.assertEqual(printed.getvalue(), f"{STAMP} {said}\n" if said else "") + self.assertEqual(start.call_args.args, (loop_run, True, loop.paths, loop.env, loop.launcher, loop.effects)) + + +class StopDms(unittest.TestCase): + def run_main(self, argv, fake): + with tempfile.TemporaryDirectory() as home, contextlib.redirect_stderr(io.StringIO()) as said, contextlib.redirect_stdout(io.StringIO()): + code = main.main(argv, {}, paths.paths_from(pathlib.Path(home)), pathlib.Path(home, "loop-claude"), CWD, fake.effects()) + return code, said.getvalue() + + def test_dms_the_reason_of_a_stop_with_exit_2_such_as_a_chunk_left_unfinished_too_often(self): + fake = FakeEffects() + with unittest.mock.patch.object(main, "run_loop", return_value=Stop(2, "the human is needed to look at the chunk, but it stayed unfinished")): + self.assertEqual(self.run_main(["do it"], fake)[0], 2) + self.assertEqual(fake.dms, [f"loop-claude in {CWD} stopped: the human is needed to look at the chunk, but it stayed unfinished"]) + + def test_skips_the_dm_of_a_stop_without_a_reason_as_once_the_windows_started(self): + fake = FakeEffects() + with unittest.mock.patch.object(main, "run_loop", return_value=Stop(0, "")): + self.assertEqual(self.run_main(["do it"], fake), (0, "")) + self.assertEqual(fake.dms, []) + + def test_skips_the_dm_where_ctrl_c_stopped_the_loop(self): + fake = FakeEffects() + with unittest.mock.patch.object(main, "run_loop", side_effect=KeyboardInterrupt): + self.assertEqual(self.run_main(["do it"], fake)[0], 130) + self.assertEqual(fake.dms, []) + + def test_dms_a_crash_before_it_reaches_the_terminal(self): + fake = FakeEffects() + with unittest.mock.patch.object(main, "run_loop", side_effect=RuntimeError("boom")), self.assertRaises(RuntimeError): + self.run_main(["do it"], fake) + self.assertEqual(fake.dms, [f"loop-claude in {CWD} crashed: RuntimeError('boom')"]) + + def test_dms_the_reason_where_a_loop_given_a_task_stops_with_exit_1(self): + fake = FakeEffects() + code, said = self.run_main([main.RELOAD_FLAG, "3"], fake) + self.assertEqual(code, 1) + self.assertEqual(fake.dms, [f"loop-claude in {CWD} stopped: the edited loop-claude cannot read the state the running one handed it, likely because the edit changed that handover; rerun loop-claude . Handed: 3"]) + + def test_says_why_the_dm_of_a_stop_failed_after_the_reason(self): + fake = FakeEffects(dm_failures=[Failure("cannot reach https://chat.example: timed out")]) + self.assertTrue(self.run_main([main.RELOAD_FLAG, "3"], fake)[1].endswith(f"Handed: 3\n{STAMP} could not DM you: cannot reach https://chat.example: timed out\n")) + + def test_skips_the_dm_where_it_prints_its_help(self): + for argv, code in (([], 1), (["--help"], 0)): + with self.subTest(argv=argv): + fake = FakeEffects() + self.assertEqual(self.run_main(argv, fake)[0], code) + self.assertEqual(fake.dms, []) + + +class OwnLadder(unittest.TestCase): + def test_a_cycle_on_a_projects_own_ladder_names_its_file_and_says_where_the_workers_model_is_off_it(self): + file = pathlib.Path("/h/.claude-projects/acme/routing-ladder.json") + self.assertEqual(main.own_ladder(file, "claude-fable-5-2", False), f"routing on the project's own ladder, {file}.") + self.assertEqual(main.own_ladder(file, "claude-opus-5-5", True), f"routing on the project's own ladder, {file}. claude-opus-5-5 is not on it, so the worker runs on claude-opus-5-5 unrouted; LOOP_CLAUDE_WORKER_MODEL and LOOP_CLAUDE_JUDGE_MODEL name other models for a run.") + + +if __name__ == "__main__": + unittest.main() diff --git a/bin/loop_claude/loop/test_routing_check.py b/bin/loop_claude/loop/test_routing_check.py new file mode 100644 index 00000000..4c510b97 --- /dev/null +++ b/bin/loop_claude/loop/test_routing_check.py @@ -0,0 +1,154 @@ +import contextlib +import io +import json +import pathlib +import shutil +import sys +import tempfile +import unittest +import unittest.mock + +sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[2])) + +from chunk_driver.doubles import a_loop +from routing_figures import archive, state +from test_env import assert_not_root +from routing_figures.calls import Call +from failure import Failure +from loop_effects.doubles import FakeEffects, NOW +from transcript_archive import outcomes +from loop_state import paths +from loop_claude.loop import routing_check +from model_routing.ladder import Rung +from claude_code import session +from agent_cycle.stop import Stop +from text_files.utc_stamp import utc + + +# How git answers in a project's checkout it cannot read, such as one that is gone. +UNREAD = Failure("not a git repository") + + +class RoutingCheck(unittest.TestCase): + def setUp(self): + self.home = pathlib.Path(tempfile.mkdtemp()) + self.addCleanup(shutil.rmtree, self.home) + self.root = self.home / "archive" + self.paths = paths.paths_from(self.home) + self.session = session.session_named("A log loop-claude", self.paths.claude_code_transcripts) + + def loop(self, fake): + """The loop in /code/log.""" + return a_loop(fake.effects(), root="/code/log", session=self.session, paths=self.paths) + + def chunks(self, count, verdicts, project="log"): + """count finished chunks, each closed by verdicts verdicts, each with one Explore subagent whose report outgrew its searches.""" + for number in range(count): + folder = self.root / project / f"c{number}" + subagents = folder / "session" / "s" / "subagents" + subagents.mkdir(parents=True) + kept = "".join(f"2026-10-14T10:0{verdict}:00Z {outcomes.unfinished('w', 'e', 'j', '')}\n" for verdict in range(verdicts - 1)) + (folder / "copy.outcomes.md").write_text(f"Task: t\n\n{kept}2026-10-14T11:00:00Z {outcomes.finished('w', 'm', 'j')}\n") + launch = {"type": "assistant", "timestamp": "2026-10-14T09:00:00Z", "message": {"id": "m", "content": [{"type": "tool_use", "id": "a", "name": "Agent", "input": {"subagent_type": "Explore"}}]}} + report = {"type": "user", "message": {"content": [{"type": "tool_result", "tool_use_id": "a", "content": "long report"}]}} + (folder / "copy.jsonl").write_text(json.dumps(launch) + "\n" + json.dumps(report) + "\n") + (subagents / "agent-a.jsonl").write_text("") + (subagents / "agent-a.meta.json").write_text(json.dumps({"agentType": "Explore", "toolUseId": "a"})) + + def check(self, fake): + """What check_routing returns and says for a loop in /code/log.""" + with contextlib.redirect_stdout(io.StringIO()), contextlib.redirect_stderr(io.StringIO()) as said: + stop = routing_check.check_routing(self.root, Rung("m", "high"), self.loop(fake)) + return stop, said.getvalue() + + def test_measures_once_20_cycles_have_closed_since_the_last_measurement(self): + self.chunks(5, 3) + self.assertEqual(self.check(FakeEffects()), (None, "")) + self.assertFalse((self.root / "routing-figures.md").exists()) + self.chunks(6, 1, "other") + self.assertEqual(self.check(FakeEffects(on_terminal=False, git=[UNREAD] * 6))[0], None) + self.assertIn("at cycle 21 across", (self.root / "routing-figures.md").read_text()) + + def test_reads_each_projects_history_through_the_loops_git_and_names_each_it_cannot_read(self): + self.chunks(5, 4) + fake = FakeEffects(on_terminal=False, git=[UNREAD] * 3) + self.check(fake) + located = ["rev-parse", "--path-format=absolute", "--show-toplevel", "--git-common-dir"] + self.assertEqual(fake.made("git"), [located, located, ["remote"]], "the config is located for its title templates, then for where it keeps the plan") + self.assertIn("## Default branches unread\n\n- log: not a git repository\n", (self.root / "routing-figures.md").read_text()) + + def test_hands_the_terminal_over_once_for_each_new_call(self): + self.chunks(5, 4) + fake = FakeEffects(on_terminal_claude=[0], git=[UNREAD] * 3) + stop, said = self.check(fake) + self.assertIsNone(stop) + call = "Explore subagents' reports took more of the worker's context than their searches would have." + self.assertIn(f"- {call}", said) + self.assertIn(f"- {call}", (self.root / "routing-figures.md").read_text()) + ((args),) = fake.made("claude_on_terminal") + self.assertEqual(args[:6], ["--model", "m", "--effort", "high", "--permission-mode", "bypassPermissions"]) + self.assertIn(f"don't change anything until they answer:\n- {call}", args[6]) + self.assertEqual(fake.terminal_environments, [{"CLAUDE_CODE_SANDBOXED": "1"}]) + self.assertEqual(fake.terminal_folders, [str(self.home / "code" / "claude-config")], "the development clone, since ~/.claude holds only a release") + self.assertIn("Record what they decide in this checkout, the development clone", args[6]) + self.assertIsNone(state.mark_measured(self.root, 0)) + self.assertEqual(self.check(FakeEffects(git=[UNREAD] * 3))[0], None) + self.assertNotIn(call, self.check(FakeEffects())[1]) + + def test_stamps_the_figures_and_the_calls_raised_when_it_measures(self): + self.chunks(5, 4) + (self.root / "routing-calls-raised.md").write_text("# Routing calls raised\n\n- 2026-10-14T10:02:00Z Reset\n") + self.check(FakeEffects(on_terminal_claude=[0], git=[UNREAD] * 3)) + figures = (self.root / "routing-figures.md").read_text() + self.assertIn(f"Measured {utc(NOW)}, at cycle 20 across", figures) + self.assertIn("that finished after the reset of 2026-10-14T10:02:00Z", figures) + self.assertIn(f"- {utc(NOW)} Explore subagents'", (self.root / "routing-calls-raised.md").read_text()) + + def test_says_a_figure_it_cannot_read(self): + self.chunks(5, 4) + (self.root / "routing-state.json").mkdir() + self.assertIn("routing figures: could not read", self.check(FakeEffects())[1]) + + def test_goes_on_off_a_terminal_and_raises_the_call_again_next_time(self): + self.chunks(5, 4) + stop, said = self.check(FakeEffects(on_terminal=False, git=[UNREAD] * 3)) + self.assertIsNone(stop) + self.assertIn("Not on a terminal: the calls are raised again at the next measurement.", said) + self.assertIsNone(state.mark_measured(self.root, 0)) + self.assertIn("Not on a terminal", self.check(FakeEffects(on_terminal=False, git=[UNREAD] * 3))[1]) + + def test_leaves_the_measuring_to_a_loop_holding_the_lock(self): + self.chunks(5, 4) + with archive.locked(self.root): + self.assertEqual(self.check(FakeEffects()), (None, "")) + + def test_stops_when_claude_cannot_start_for_the_calls(self): + self.chunks(5, 4) + stop, _ = self.check(FakeEffects(on_terminal_claude=[OSError(2, "No such file or directory")], git=[UNREAD] * 3)) + self.assertEqual(stop, Stop(1, f"could not start claude for the routing calls in {self.home / 'code' / 'claude-config'}, the setup's development clone: No such file or directory")) + + def test_says_a_figure_it_cannot_write_and_goes_on(self): + assert_not_root(self) + self.chunks(5, 4) + (self.root / "routing-figures.lock").touch() + self.root.chmod(0o555) + self.addCleanup(self.root.chmod, 0o755) + stop, said = self.check(FakeEffects(git=[UNREAD] * 3)) + self.assertIsNone(stop) + self.assertIn("routing figures: could not write", said) + + def test_puts_every_new_call_to_the_human_and_says_one_it_cannot_keep(self): + new = [Call("reports", ("Explore",)), Call("rounds", ("x", "y"))] + fake = FakeEffects(on_terminal_claude=[0]) + with contextlib.redirect_stderr(io.StringIO()) as said, unittest.mock.patch.object(state, "mark_raised", return_value=Failure("full")): + self.assertIsNone(routing_check.raise_calls(self.root, new, Rung("m", "high"), self.loop(fake))) + ((args),) = fake.made("claude_on_terminal") + self.assertIn(f"don't change anything until they answer:\n- {new[0].sentence()}\n- {new[1].sentence()}\nThe figures", args[6]) + self.assertIn(f"call for a routing change:\n- {new[0].sentence()}\n- {new[1].sentence()}\n", said.getvalue()) + self.assertIn("routing figures: full", said.getvalue()) + [sent] = fake.dms + self.assertTrue(sent.startswith(f"loop-claude in {self.loop(fake).root} asks:\n\nloop-claude's routing figures call for a routing change:\n- {new[0].sentence()}\n"), sent) + + +if __name__ == "__main__": + unittest.main() diff --git a/bin/loop_claude/loop/test_usage.py b/bin/loop_claude/loop/test_usage.py new file mode 100644 index 00000000..a6da1337 --- /dev/null +++ b/bin/loop_claude/loop/test_usage.py @@ -0,0 +1,123 @@ +import ast +import pathlib +import sys +import unittest + +sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[2])) + +from claude_code.output_limits import BASH_CEILING_CHARACTERS +from chunk_driver import chunk_run +from agent_cycle import judge, retry, states, worker +from loop_claude.loop import main, usage +from loop_effects.effects import PYTHON_TIMEOUT +from chunk_driver.project import records +from claude_code import reply +from routing_figures import state + + +class HelpLimits(unittest.TestCase): + def test_help_wraps_at_100_columns(self): + self.assertEqual([line for line in usage.HELP.splitlines() if len(line) > 100], []) + + def test_help_states_the_limits_the_code_holds(self): + said = " ".join(usage.HELP.split()) + self.assertIn(f"The {['second', 'third', 'fourth', 'fifth'][states.QUOTED_ANSWERS_PER_CYCLE - 1]} question they answer in one cycle", said) + # The retry messages say these limits in words too. + self.assertEqual((retry.FAILURES_RUNNING, retry.RETRY_DELAY), (3, 60)) + self.assertIn("fails three times running, retried a minute apart", said) + self.assertIn(f"retried {retry.PAST_RESET // 60} minutes after the limit resets, or every {retry.UNKNOWN_RESET_POLL // 60} minutes", said) + self.assertIn(f"still running after {PYTHON_TIMEOUT} s", said) + self.assertIn(f"Once {state.CYCLES_PER_CHECK} cycles have closed across all projects", said) + self.assertIn(f"run with a {worker.COMPACT_WINDOW // 1000}k auto-compact window", said) + self.assertIn(f"{BASH_CEILING_CHARACTERS:,} characters", said) + self.assertIn(f"unfinished {chunk_run.UNFINISHED_LIMIT} cycles running", said) + self.assertIn(f"rerun to give it {chunk_run.UNFINISHED_LIMIT} more", said) + + +class ExitContract(unittest.TestCase): + def test_every_exit_1_stop_has_its_clause_in_help(self): + read, git, premises_ = "a transcript cannot be read or archived", "git cannot start or finds no checkout", "no config names the checkout, its config cannot be read, or the goals file holds no premises" + models_ = "the models file cannot be read or written" + names = (*records.RECORD_FILES, records.HUMAN_RULE_NAMES[0], f"{records.HUMAN_RULE_NAMES[1]}*.md", records.HUMAN_RULE_NAMES[2]) + unreadable = f"the goals file, or {', '.join(names[:-1])} or {names[-1]} where they exist, cannot be read or is not UTF-8" + # Every Stop whose code is not a literal 0, 2 or 130, in source order: path, function, code, start of reason, its --help clause. + clauses = [ + ("agent_cycle/interrupt.py", "claude_open", "1", "f'could not start claude for the interrupt:", "claude cannot start for an interrupt"), + ("agent_cycle/interrupt.py", "human_typed", "1", "entries.reason", read), + ("agent_cycle/interrupt.py", "human_typed", "1", "f'could not tell whether you typed anything:", read), + ("agent_cycle/retry.py", "retried", "1", "f'the {who} failed three times running:", "the worker or the judge fails three times running"), + ("agent_cycle/run.py", "archived_stop", "1", "f'{failure.reason}", read), + ("agent_cycle/run.py", "work", "1", "f'claude left no transcript for session", "claude leaves no transcript"), + ("agent_cycle/run.py", "rule", "1", "entries.reason", read), + ("agent_cycle/run.py", "rule", "1", "annotation.reason", read), + ("agent_cycle/run.py", "rule", "1", "failure.reason", read), + ("agent_cycle/run.py", "keep_rejection", "1", "unkept.reason", read), + ("agent_cycle/run.py", "close", "1", "annotation.reason", read), + ("agent_cycle/run.py", "close", "1", "failure.reason", read), + ("agent_cycle/run.py", "close", "1", "failure.reason", "a finished chunk's session cannot be deleted"), + ("chunk_driver/chunk_run.py", "closed_cycle", "1", "failure.reason", read), + ("chunk_driver/cycle_start.py", "project_routing", "1", "ladder_file.reason", premises_), + ("chunk_driver/cycle_start.py", "routing_on", "1", "ladder.reason", "the project's routing ladder cannot be read or holds a fault"), + ("chunk_driver/project/model_choice.py", "get_or_ask_model", "1", "models.reason", models_), + ("chunk_driver/project/model_choice.py", "get_or_ask_model", "1", "models.reason", models_), + ("chunk_driver/project/model_choice.py", "get_or_ask_model", "1", "f'could not save the model in", models_), + ("loop_claude/loop/checkout.py", "checkout_root", "1", "found.reason", git), + ("loop_claude/loop/main.py", "run_loop", "1", "reason", "tmux is missing or older than 3.2"), + ("loop_claude/loop/main.py", "run_loop", "1", "f'{CHUNK_FLAG} is how loop-claude starts itself", "--chunk is given outside a tmux window"), + ("loop_claude/loop/main.py", "windows_opened", "1", "started.reason", "a chunk's window cannot be started"), + ("loop_claude/loop/main.py", "windows_opened", "1", "f\"`tmux {' '.join(shown)}` failed", "the tmux session cannot be put on the terminal"), + ("loop_claude/loop/main.py", "handed_on", "1", "f'the chunk finished, but the next could not start", "a chunk's window cannot be started"), + ("loop_claude/loop/main.py", "next_cycle", "1", "premises.reason", premises_), + ("loop_claude/loop/main.py", "next_cycle", "1", "records.reason", unreadable), + ("loop_claude/loop/main.py", "loop_run_from", "0 if task else 1", "HELP", "no task is given"), + ("loop_claude/loop/main.py", "loop_run_from", "1", 'f"the edited loop-claude cannot read the state', "a reload cannot read the state the running script handed it"), + ("loop_claude/loop/reload.py", "exec_if_edited", "1", "f'could not reload loop-claude:", "python cannot start for a reload"), + ("loop_claude/loop/routing_check.py", "raise_calls", "1", "f\"could not start claude for the routing calls in {loop.paths.config_clone}", "claude cannot start for an interrupt"), + ] + root = pathlib.Path(main.__file__).parents[2] + found = [] + for path in sorted(path for package in ("agent_cycle", "chunk_driver", "loop_claude") for path in (root / package).rglob("*.py") if not path.name.startswith("test_")): + 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[0]), 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, code, reason) for _, function, code, reason in sorted(stops)] + self.assertEqual(len(found), len(clauses), found) + for (path, function, code, reason), (want_path, want_function, want_code, reason_start, _) in zip(found, clauses): + with self.subTest(path=path, function=function): + self.assertEqual((path, function, code), (want_path, want_function, want_code)) + self.assertTrue(reason.startswith(reason_start), reason) + help_exit_1 = " ".join(usage.HELP[usage.HELP.index("Exit 1"):usage.HELP.index("Exit 2")].split()) + self.assertEqual([clause for *_, clause in clauses if f"- {clause}" not in help_exit_1], []) + + +class JudgeFailures(unittest.TestCase): + def test_every_failed_judge_run_has_its_clause_in_help(self): + started, exited, unparsed = "claude cannot start", "exits non-zero", "prints no json reply" + field, unasked = "its reply misses or mistypes a field", "interrupts or quotes an answer without a question" + # Every Failure the judge's run returns, in source order of judge.py, then of the claude run it calls: function, start of reason, its --help clause. + clauses = [ + ("verdict_from", "'the judge returned no reply'", field), + ("verdict_from", 'f"the judge\'s reply has no {name}', field), + ("verdict_from", "'the judge interrupted without a question'", unasked), + ("verdict_from", "'the judge quoted an answer without a question'", unasked), + ("run_judge", "verdict.reason", field), + ("run_judge", "checked.reason", "quotes a passage the transcript does not hold"), + ("misquoted_evidence", "f'the judge quoted {verdict.evidence!r}, which is not in the transcript", "quotes a passage the transcript does not hold"), + ("claude_reply", "f'could not start claude:", started), + ("claude_reply", "proc.stderr.strip() or f'{who} exited", exited), + ("claude_reply", 'f"{who} printed no json reply', unparsed), + ] + found = [] + for module in (judge, reply): + functions = [node for node in ast.walk(ast.parse(pathlib.Path(module.__file__).read_text())) if isinstance(node, ast.FunctionDef)] + found += sorted((call.lineno, function.name, ast.unparse(call.args[0])) for function in functions for call in ast.walk(function) if isinstance(call, ast.Call) and getattr(call.func, "id", None) in ("Failure", "Rejected")) + self.assertEqual(len(found), len(clauses), found) + for (_, function, reason), (want_function, reason_start, _) in zip(found, clauses): + with self.subTest(function=function): + self.assertEqual(function, want_function) + self.assertTrue(reason.startswith(reason_start), reason) + judge_clause = " ".join(usage.HELP[usage.HELP.index("a judge when"):usage.HELP.index("A usage limit")].split()) + self.assertEqual([clause for *_, clause in clauses if clause not in judge_clause], []) + + +if __name__ == "__main__": + unittest.main() diff --git a/bin/loop_claude/test_black_box.py b/bin/loop_claude/test_black_box.py index 5a3a2881..020098ed 100644 --- a/bin/loop_claude/test_black_box.py +++ b/bin/loop_claude/test_black_box.py @@ -1,6 +1,5 @@ import dataclasses import datetime -import fcntl import hashlib import json import os @@ -19,7 +18,7 @@ import unittest sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) from test_env import assert_not_root, isolated_env -from project_config.test_project_config import write_config +from project_config.doubles import write_config BIN = pathlib.Path(__file__).resolve().parents[1] TASK = "Do the next chunk." diff --git a/bin/loop_claude/test_loop_claude.py b/bin/loop_claude/test_loop_claude.py deleted file mode 100644 index ace9f16d..00000000 --- a/bin/loop_claude/test_loop_claude.py +++ /dev/null @@ -1,998 +0,0 @@ -import ast -import contextlib -import dataclasses -import io -import json -import pathlib -import re -import shutil -import sys -import tempfile -import unittest -import unittest.mock - -sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) - -from chunk_driver.doubles import a_loop -from routing_figures import archive, state -from test_env import assert_not_root -from loop_effects.doubles import assistant, CWD, FakeEffects, NOW, ran, said, STAMP -from claude_code.output_limits import BASH_CEILING_CHARACTERS -from routing_figures.calls import Call -from loop_claude.chunk import chunk_items -from chunk_driver import chunk_run, prompts, this_loop -from loop_claude.loop import chunk_start, chunk_windows -from project_config.test_project_config import configured -from release_plan.release_standing import DefaultPlan, LastCut, UNSEEN_RELEASES -from claude_code import entries, reply, session -from failure import Failure -from agent_cycle.verdicts import Finished, TakenIn, Unfinished -from loop_claude.chunk import fresh_prompt -from agent_cycle import judge, retry, states, worker -import loop_claude.routed_item -from loop_claude.loop import main, reload, routing_check, usage -from release_plan import next_chunk -from transcript_archive import outcomes -from loop_state import paths -from plan_store.todo_md import plan_of -from model_routing.ladder import ProjectRung, Rung -from loop_effects.effects import PYTHON_TIMEOUT -from model_routing.ladder_file import read_ladder, SHIPPED -from chunk_driver.project import records -from model_routing.route import Route -from agent_cycle.stop import Stop -from agent_cycle import tmux -from touch_predictions.test_touch_predictions import plan as touch_plan -from text_files.utc_stamp import utc - -LADDER = read_ladder(SHIPPED) -OPUS_PROJECT = ProjectRung(LADDER, Rung("claude-opus-5-5", "medium")) - -# How git answers in a project's checkout it cannot read, such as one that is gone. -UNREAD = Failure("not a git repository") - - -class RoutingCheck(unittest.TestCase): - def setUp(self): - self.home = pathlib.Path(tempfile.mkdtemp()) - self.addCleanup(shutil.rmtree, self.home) - self.root = self.home / "archive" - self.paths = paths.paths_from(self.home) - self.session = session.session_named("A log loop-claude", self.paths.claude_code_transcripts) - - def loop(self, fake): - """The loop in /code/log.""" - return a_loop(fake.effects(), root="/code/log", session=self.session, paths=self.paths) - - def chunks(self, count, verdicts, project="log"): - """count finished chunks, each closed by verdicts verdicts, each with one Explore subagent whose report outgrew its searches.""" - for number in range(count): - folder = self.root / project / f"c{number}" - subagents = folder / "session" / "s" / "subagents" - subagents.mkdir(parents=True) - kept = "".join(f"2026-10-14T10:0{verdict}:00Z {outcomes.unfinished('w', 'e', 'j', '')}\n" for verdict in range(verdicts - 1)) - (folder / "copy.outcomes.md").write_text(f"Task: t\n\n{kept}2026-10-14T11:00:00Z {outcomes.finished('w', 'm', 'j')}\n") - launch = {"type": "assistant", "timestamp": "2026-10-14T09:00:00Z", "message": {"id": "m", "content": [{"type": "tool_use", "id": "a", "name": "Agent", "input": {"subagent_type": "Explore"}}]}} - report = {"type": "user", "message": {"content": [{"type": "tool_result", "tool_use_id": "a", "content": "long report"}]}} - (folder / "copy.jsonl").write_text(json.dumps(launch) + "\n" + json.dumps(report) + "\n") - (subagents / "agent-a.jsonl").write_text("") - (subagents / "agent-a.meta.json").write_text(json.dumps({"agentType": "Explore", "toolUseId": "a"})) - - def check(self, fake): - """What check_routing returns and says for a loop in /code/log.""" - with contextlib.redirect_stdout(io.StringIO()), contextlib.redirect_stderr(io.StringIO()) as said: - stop = routing_check.check_routing(self.root, Rung("m", "high"), self.loop(fake)) - return stop, said.getvalue() - - def test_measures_once_20_cycles_have_closed_since_the_last_measurement(self): - self.chunks(5, 3) - self.assertEqual(self.check(FakeEffects()), (None, "")) - self.assertFalse((self.root / "routing-figures.md").exists()) - self.chunks(6, 1, "other") - self.assertEqual(self.check(FakeEffects(on_terminal=False, git=[UNREAD] * 6))[0], None) - self.assertIn("at cycle 21 across", (self.root / "routing-figures.md").read_text()) - - def test_reads_each_projects_history_through_the_loops_git_and_names_each_it_cannot_read(self): - self.chunks(5, 4) - fake = FakeEffects(on_terminal=False, git=[UNREAD] * 3) - self.check(fake) - located = ["rev-parse", "--path-format=absolute", "--show-toplevel", "--git-common-dir"] - self.assertEqual(fake.made("git"), [located, located, ["remote"]], "the config is located for its title templates, then for where it keeps the plan") - self.assertIn("## Default branches unread\n\n- log: not a git repository\n", (self.root / "routing-figures.md").read_text()) - - def test_hands_the_terminal_over_once_for_each_new_call(self): - self.chunks(5, 4) - fake = FakeEffects(on_terminal_claude=[0], git=[UNREAD] * 3) - stop, said = self.check(fake) - self.assertIsNone(stop) - call = "Explore subagents' reports took more of the worker's context than their searches would have." - self.assertIn(f"- {call}", said) - self.assertIn(f"- {call}", (self.root / "routing-figures.md").read_text()) - ((args),) = fake.made("claude_on_terminal") - self.assertEqual(args[:6], ["--model", "m", "--effort", "high", "--permission-mode", "bypassPermissions"]) - self.assertIn(f"don't change anything until they answer:\n- {call}", args[6]) - self.assertEqual(fake.terminal_environments, [{"CLAUDE_CODE_SANDBOXED": "1"}]) - self.assertEqual(fake.terminal_folders, [str(self.home / "code" / "claude-config")], "the development clone, since ~/.claude holds only a release") - self.assertIn("Record what they decide in this checkout, the development clone", args[6]) - self.assertIsNone(state.mark_measured(self.root, 0)) - self.assertEqual(self.check(FakeEffects(git=[UNREAD] * 3))[0], None) - self.assertNotIn(call, self.check(FakeEffects())[1]) - - def test_stamps_the_figures_and_the_calls_raised_when_it_measures(self): - self.chunks(5, 4) - (self.root / "routing-calls-raised.md").write_text("# Routing calls raised\n\n- 2026-10-14T10:02:00Z Reset\n") - self.check(FakeEffects(on_terminal_claude=[0], git=[UNREAD] * 3)) - figures = (self.root / "routing-figures.md").read_text() - self.assertIn(f"Measured {utc(NOW)}, at cycle 20 across", figures) - self.assertIn("that finished after the reset of 2026-10-14T10:02:00Z", figures) - self.assertIn(f"- {utc(NOW)} Explore subagents'", (self.root / "routing-calls-raised.md").read_text()) - - def test_says_a_figure_it_cannot_read(self): - self.chunks(5, 4) - (self.root / "routing-state.json").mkdir() - self.assertIn("routing figures: could not read", self.check(FakeEffects())[1]) - - def test_goes_on_off_a_terminal_and_raises_the_call_again_next_time(self): - self.chunks(5, 4) - stop, said = self.check(FakeEffects(on_terminal=False, git=[UNREAD] * 3)) - self.assertIsNone(stop) - self.assertIn("Not on a terminal: the calls are raised again at the next measurement.", said) - self.assertIsNone(state.mark_measured(self.root, 0)) - self.assertIn("Not on a terminal", self.check(FakeEffects(on_terminal=False, git=[UNREAD] * 3))[1]) - - def test_leaves_the_measuring_to_a_loop_holding_the_lock(self): - self.chunks(5, 4) - with archive.locked(self.root): - self.assertEqual(self.check(FakeEffects()), (None, "")) - - def test_stops_when_claude_cannot_start_for_the_calls(self): - self.chunks(5, 4) - stop, _ = self.check(FakeEffects(on_terminal_claude=[OSError(2, "No such file or directory")], git=[UNREAD] * 3)) - self.assertEqual(stop, Stop(1, f"could not start claude for the routing calls in {self.home / 'code' / 'claude-config'}, the setup's development clone: No such file or directory")) - - def test_says_a_figure_it_cannot_write_and_goes_on(self): - assert_not_root(self) - self.chunks(5, 4) - (self.root / "routing-figures.lock").touch() - self.root.chmod(0o555) - self.addCleanup(self.root.chmod, 0o755) - stop, said = self.check(FakeEffects(git=[UNREAD] * 3)) - self.assertIsNone(stop) - self.assertIn("routing figures: could not write", said) - - def test_puts_every_new_call_to_the_human_and_says_one_it_cannot_keep(self): - new = [Call("reports", ("Explore",)), Call("rounds", ("x", "y"))] - fake = FakeEffects(on_terminal_claude=[0]) - with contextlib.redirect_stderr(io.StringIO()) as said, unittest.mock.patch.object(state, "mark_raised", return_value=Failure("full")): - self.assertIsNone(routing_check.raise_calls(self.root, new, Rung("m", "high"), self.loop(fake))) - ((args),) = fake.made("claude_on_terminal") - self.assertIn(f"don't change anything until they answer:\n- {new[0].sentence()}\n- {new[1].sentence()}\nThe figures", args[6]) - self.assertIn(f"call for a routing change:\n- {new[0].sentence()}\n- {new[1].sentence()}\n", said.getvalue()) - self.assertIn("routing figures: full", said.getvalue()) - [sent] = fake.dms - self.assertTrue(sent.startswith(f"loop-claude in {self.loop(fake).root} asks:\n\nloop-claude's routing figures call for a routing change:\n- {new[0].sentence()}\n"), sent) - - -# A plan read with no release cut yet. -NO_CUT = LastCut(None, "", 0, []) -def routed_item(todo, cut): - """loop_claude.routed_item.routed_item for the plan a todo.md's text holds.""" - return loop_claude.routed_item.routed_item(plan_of(todo), cut) - - -TODO = """# todo - -## Items - -| ID | Release | Exempt | Item | L | E | I | R | S | Moves | Score | -|---|---|---|---|---|---|---|---|---|---|---| -| 7 | 0.2.0 | question | **Ask first.** | 4 | 3 | 5 | 10 | 1 | 1 by 9 | 30 | -| 3 | 0.2.0 | | **Do the next thing.** | 4 | 2 | 3 | 7 | 3 | 1 by 9 | 25 | -| 1 | 0.3.0 | | **Do a later thing.** | 1 | 1 | 1 | 1 | 1 | 1 by 9 | 30 | -""" - - -class RoutedItem(unittest.TestCase): - def test_routes_on_the_next_releases_top_item_that_waits_on_no_question(self): - self.assertEqual(routed_item(TODO, NO_CUT).id, 3) - self.assertIsNone(routed_item(TODO.replace("| | **Do", "| question | **Do"), NO_CUT)) - self.assertIsNone(routed_item("# todo\n", NO_CUT)) - self.assertIsNone(routed_item(TODO[:TODO.index("| 7 |")], NO_CUT)) - old_shape = "## Items\n\n| ID | Release | Exempt | Item | R | S | C | A | G | Goals | Score |\n|---|---|---|---|---|---|---|---|---|---|---|\n| 3 | 0.2.0 | | **Do the next thing.** | 5 | 3 | 6 | 8 | 9 | 1 | 25 |\n" - self.assertIsNone(routed_item(old_shape, NO_CUT)) - self.assertEqual(routed_item(TODO.replace("| 4 | 2 | 3 |", "| x | 2 | 3 |"), NO_CUT), Failure("item 3's L 'x' is not a whole number from 1 to 4")) - - def test_routes_on_no_item_where_the_release_after_the_last_cut_has_none(self): - self.assertEqual(routed_item(TODO, LastCut("v0.1.0", "", 3, [TODO])).id, 3) - # Someone cut 0.2.0 while it held items: they count as 0.3.0's, the next release, and where no later release is known, no item routes. - self.assertEqual(routed_item(TODO, LastCut("v0.2.0", "", 3, [TODO])).id, 3) - self.assertIsNone(routed_item(re.sub(r"(?m)^\| \d+ \| 0\.3\.0 \|.*\n", "", TODO), LastCut("v0.2.0", "", 3, []))) - later_only = TODO[:TODO.index("| 7 |")] + TODO[TODO.index("| 1 | 0.3.0 |"):] - self.assertIsNone(routed_item(later_only, LastCut("v0.1.0", "", 3, [TODO]))) - self.assertIsNone(routed_item(later_only, LastCut("v0.1.0", "", 0, [TODO]))) - self.assertEqual(routed_item(later_only, LastCut("v0.2.0", "", 3, [later_only])).id, 1) - - def test_routes_past_an_item_a_waiting_item_blocks(self): - todo = "## Items\n\n| ID | Release | Blocked by | Exempt | Item | L | E | I | R | S | Moves | Score |\n|---|---|---|---|---|---|---|---|---|---|---|---|\n| 7 | 0.2.0 | | question | **Ask first.** | 4 | 3 | 5 | 10 | 1 | 1 by 9 | 30 |\n| 8 | 0.2.0 | 7 | | **Use the answer.** | 1 | 1 | 1 | 1 | 1 | 1 by 9 | 30 |\n| 9 | 0.2.0 | 8 | | **Build on it.** | 1 | 1 | 1 | 1 | 1 | 1 by 9 | 30 |\n| 3 | 0.2.0 | | | **Do the next thing.** | 4 | 2 | 3 | 7 | 3 | 1 by 9 | 25 |\n" - self.assertEqual(routed_item(todo, NO_CUT).id, 3) - self.assertEqual(routed_item(todo.replace("| question |", "| |"), NO_CUT).id, 7) - first = "| 7 | 0.2.0 | | question | **Ask first.** | 4 | 3 | 5 | 10 | 1 | 1 by 9 | 30 |\n" - self.assertEqual(routed_item(todo.replace(first, "") + first, NO_CUT).id, 3) - - def test_routes_nothing_on_an_item_without_e_or_i(self): - without_i = TODO.replace("| E | I | R |", "| E | R |").replace("|---|---|---|---|---|---|---|---|---|---|---|", "|---|---|---|---|---|---|---|---|---|---|").replace("| 4 | 3 | 5 | 10 |", "| 4 | 3 | 10 |").replace("| 4 | 2 | 3 | 7 |", "| 4 | 2 | 7 |").replace("| 1 | 1 | 1 | 1 |", "| 1 | 1 | 1 |") - self.assertIsNone(routed_item(without_i, NO_CUT)) - - -class RouteSources(unittest.TestCase): - def test_routes_on_the_plan_item_and_says_why_it_routes_on_the_projects_rung(self): - self.assertIsNone(chunk_start.plan_item(DefaultPlan(plan_of(""), NO_CUT), None, FakeEffects().effects())) - self.assertEqual(chunk_start.plan_item(DefaultPlan(plan_of(TODO), LastCut("v0.1.0", "", 3, [TODO])), None, FakeEffects().effects()).id, 3) - self.assertIsNone(chunk_start.plan_item(DefaultPlan(plan_of(TODO), LastCut("v0.3.0", "", 3, [TODO])), None, FakeEffects().effects())) - - def test_routes_a_window_named_for_an_item_on_that_item_whatever_the_plan_takes_next(self): - plan = DefaultPlan(plan_of(TODO), LastCut("v0.1.0", "", 3, [TODO])) - later = next(item.id for item in plan.plan.items if item.id != 3) - self.assertEqual(chunk_start.plan_item(plan, later, FakeEffects().effects()).id, later) - # An item that left the plan, as once it shipped. - self.assertIsNone(chunk_start.plan_item(plan, 999, FakeEffects().effects())) - # A chunk start reads the default branch once, for routing and for scoring the items the worker names. - with unittest.mock.patch.object(chunk_start, "session_open", return_value=True), unittest.mock.patch.object(chunk_start, "fetched_plan", return_value=plan) as fetched: - loop = a_loop(FakeEffects().effects()) - self.assertEqual(chunk_start.chunk_start(loop), ("", plan)) - # An open session is no fresh chunk, so the human isn't told again of releases the plan cannot show. - fetched.assert_called_once_with(loop, False) - unreadable = Failure("git show origin/main:todo.md printed text that is not UTF-8") - for plan, said in ((DefaultPlan(plan_of(TODO.replace("| 4 | 2 | 3 |", "| x | 2 | 3 |")), NO_CUT), "routing on the project's rung: item 3's L 'x' is not a whole number from 1 to 4\n"), (DefaultPlan(unreadable, NO_CUT), f"routing on the project's rung: {unreadable.reason}\n"), (Failure("no origin remote"), "routing on the project's rung: no origin remote\n")): - with self.subTest(said=said): - with contextlib.redirect_stderr(io.StringIO()) as stderr: - self.assertIsNone(chunk_start.plan_item(plan, None, FakeEffects().effects())) - self.assertTrue(stderr.getvalue().startswith(f"{STAMP} {said}"), stderr.getvalue()) - - def test_says_why_the_items_the_worker_names_lift_no_rung(self): - unscored = plan_of(TODO.replace("| 4 | 2 | 3 |", "| x | 2 | 3 |")) - for default, said in ((DefaultPlan(unscored, NO_CUT), "the items the worker names lift no rung: item 3's L 'x' is not a whole number from 1 to 4\n"), (Failure("no git"), "the items the worker names lift no rung: no git\n"), (DefaultPlan(plan_of(TODO), NO_CUT), "")): - with self.subTest(said=said): - with contextlib.redirect_stderr(io.StringIO()) as stderr: - scored = chunk_start.scored_plan(default, FakeEffects().effects()) - self.assertEqual(stderr.getvalue(), f"{STAMP} {said}" if said else "") - self.assertEqual(scored, default.plan if isinstance(default, DefaultPlan) else default) - - def test_reads_the_changelog_the_default_branch_holds_through_the_loops_git(self): - def git(*args, **options): - answers = {"remote": "origin\n", "symbolic-ref": "origin/main\n", "show": f"what {args[-1]} holds\n"} - return answers.get(args[0], "1\n") - - with tempfile.TemporaryDirectory() as root: - folder = configured(self, pathlib.Path(root)) - effects = dataclasses.replace(FakeEffects().effects(), git=git) - held = chunk_start.fetched_plan(a_loop(effects, root=root, paths=paths.paths_from(folder.parents[1]))) - self.assertEqual(held.cut.changelog, "what origin/main:CHANGELOG.md holds\n") - - def test_reads_a_plan_outside_the_checkout_as_it_stands_and_stops_without_a_config(self): - with tempfile.TemporaryDirectory() as root: - folder = configured(self, pathlib.Path(root)) - loop = a_loop(FakeEffects(git=["origin\n", "origin/main\n"] * 2).effects(), root=root, paths=paths.paths_from(folder.parents[1])) - config = folder / "config.toml" - tracked = a_loop(FakeEffects(git=["origin\n", "origin/main\n", *[Failure("no git")] * 4]).effects(), root=root, paths=paths.paths_from(folder.parents[1])) - with contextlib.redirect_stderr(io.StringIO()) as stderr: - chunk_start.fetched_plan(tracked) - self.assertEqual(stderr.getvalue(), "", "a plan git tracks hides no release") - config.write_text(config.read_text().replace('"checkout:todo.md"', '"folder:todo.md"')) - (folder / "todo.md").write_text(TODO) - unread = Failure("no git") - standing = a_loop(FakeEffects(git=["origin\n", "origin/main\n", unread, unread, unread, unread]).effects(), root=root, paths=paths.paths_from(folder.parents[1])) - with contextlib.redirect_stderr(io.StringIO()) as stderr: - held = chunk_start.fetched_plan(standing) - self.assertEqual((held.plan, held.cut.todos_since, held.cut.changelog), (plan_of(TODO), [], ""), "the plan as it stands, whatever git answers") - self.assertEqual(stderr.getvalue(), f"{STAMP} {UNSEEN_RELEASES}\n", "a release review loop-claude cannot call due is never skipped silently") - quiet = a_loop(FakeEffects(git=["origin\n", "origin/main\n", unread, unread, unread, unread]).effects(), root=root, paths=paths.paths_from(folder.parents[1])) - with contextlib.redirect_stderr(io.StringIO()) as stderr: - chunk_start.fetched_plan(quiet, announce=False) - self.assertEqual(stderr.getvalue(), "", "said once per chunk, as it starts") - config.unlink() - with contextlib.redirect_stderr(io.StringIO()) as stderr: - self.assertIn("no config names the checkout", chunk_start.scored_plan(chunk_start.fetched_plan(loop), loop.effects).reason) - self.assertIn("the items the worker names lift no rung: no config names the checkout", stderr.getvalue()) - self.assertIn("no config names the checkout", chunk_start.fetched_plan(loop).reason) - - -class ChunkWindows(unittest.TestCase): - def plan(self, *touched): - """A plan of items 1, 2 and on, each predicted to touch the path given for it.""" - items = touch_plan(*((number, "1.0.0", "", f"- `{path}`") for number, path in enumerate(touched, 1))) - return next_chunk.NextChunk(items=items, first=items[0], text="", branch_template="") - - def test_starts_the_next_item_and_those_picked_beside_it_up_to_the_limit(self): - # Item 2 overlaps item 1, so it waits for it; items 3 and 4 run beside it. - plan = self.plan("a.py", "a.py", "c.py", "d.py") - self.assertEqual(chunk_windows.may_start(plan, [], set(), 1), ["item-1"]) - self.assertEqual(chunk_windows.may_start(plan, [], set(), 2), ["item-1", "item-3"]) - self.assertEqual(chunk_windows.may_start(plan, [], set(), 9), ["item-1", "item-3", "item-4"]) - - def test_leaves_the_items_running_chunks_hold_and_starts_only_as_many_as_the_room_left(self): - plan = self.plan("a.py", "a.py", "c.py", "d.py") - self.assertEqual(chunk_windows.may_start(plan, ["item-1"], {1, 3}, 1), ["item-4"]) - self.assertEqual(chunk_windows.may_start(plan, ["item-1"], {1}, 2), ["item-3", "item-4"]) - self.assertEqual(chunk_windows.may_start(plan, ["item-1"], {1}, 0), []) - - def test_counts_the_running_chunks_against_the_limit_and_restarts_stopped_ones_first(self): - windows = [tmux.Listed("item-1", "", False), tmux.Listed("item-3", "", True), tmux.Listed("item-4", "", True)] - self.assertEqual(chunk_windows.room_of(windows, "item-1", False, 3), chunk_windows.Room(["item-1"], ["item-3", "item-4"], ["item-3", "item-4"], 0)) - self.assertEqual(chunk_windows.room_of(windows, "item-1", False, 2), chunk_windows.Room(["item-1"], ["item-3", "item-4"], ["item-3"], 0)) - self.assertEqual(chunk_windows.room_of(windows, "item-1", False, 5), chunk_windows.Room(["item-1"], ["item-3", "item-4"], ["item-3", "item-4"], 2)) - - def test_a_loop_whose_chunk_finished_leaves_its_room_and_restarts_no_stopped_chunk(self): - windows = [tmux.Listed("item-1", "", False), tmux.Listed("item-3", "", True)] - self.assertEqual(chunk_windows.room_of(windows, "item-1", True, 2), chunk_windows.Room([], ["item-3"], [], 2)) - - def test_leaves_no_room_where_more_chunks_run_than_the_limit(self): - windows = [tmux.Listed(f"item-{n}", "", False) for n in (1, 2, 3)] - self.assertEqual(chunk_windows.room_of(windows, "item-1", False, 2).left, 0) - - def test_starts_no_item_that_overlaps_a_running_chunks_items(self): - # Item 1 merged, so 2 is the plan's next item; it overlaps item 3, which still runs, at c.py. - items = touch_plan((2, "1.0.0", "", "- `a.py`\n- `c.py`"), (3, "1.0.0", "", "- `c.py`"), (4, "1.0.0", "", "- `d.py`")) - plan = next_chunk.NextChunk(items=items, first=items[0], text="", branch_template="") - self.assertEqual(chunk_windows.may_start(plan, ["item-3"], {3}, 2), ["item-4"]) - # A running chunk's item without a prediction leaves no other item free to start beside it. - unpredicted = touch_plan((2, "1.0.0", "", "- `a.py`"), (3, "1.0.0", "", None)) - self.assertEqual(chunk_windows.may_start(next_chunk.NextChunk(items=unpredicted, first=unpredicted[0], text="", branch_template=""), ["item-3"], {3}, 2), []) - # A held item that left the plan, as once it merged, overlaps nothing. - self.assertEqual(chunk_windows.may_start(plan, ["item-9"], {9}, 2), ["item-2", "item-4"]) - - def test_a_plan_without_an_item_starts_one_chunk_and_only_alone(self): - none = next_chunk.NoItem("release 1.0.0 has no items") - self.assertEqual(chunk_windows.may_start(none, [], set(), 3), ["no-item"]) - self.assertEqual(chunk_windows.may_start(none, ["item-1"], {1}, 3), []) - - def test_names_a_window_for_its_item_and_reads_the_item_back(self): - self.assertEqual(chunk_windows.window_name(372), "item-372") - self.assertEqual([chunk_windows.item_of(chunk) for chunk in ("item-372", "no-item", "item-", "item-3x", "xitem-3")], [372, None, None, None, None]) - # Only ASCII digits name an item, as window_name writes them. - self.assertIsNone(chunk_windows.item_of("item-\u0663")) - - def test_starts_only_the_next_item_where_the_touch_predictions_cannot_be_read(self): - with unittest.mock.patch.object(chunk_windows, "chunks_beside", return_value="item 1's Touches: list cannot be read"): - self.assertEqual(chunk_windows.may_start(self.plan("a.py", "c.py"), [], set(), 3), ["item-1"]) - - def test_a_chunk_whose_transcript_cannot_be_read_holds_the_item_it_started_from(self): - with tempfile.TemporaryDirectory() as temp: - transcripts = pathlib.Path(temp) - folder = transcripts / "-r" - folder.mkdir() - # A folder in place of the transcript file, so reading it fails. - (folder / f"{chunk_windows.chunk_session('proj', 'item-7', transcripts).id}.jsonl").mkdir() - self.assertEqual(chunk_windows.held_items(["item-7"], "proj", transcripts), {7}) - - def test_tells_a_chunk_the_items_the_projects_other_live_chunks_hold(self): - with tempfile.TemporaryDirectory() as temp: - home = pathlib.Path(temp) - held = paths.paths_from(home) - folder = held.claude_code_transcripts / "-r" - folder.mkdir(parents=True) - lines = [assistant({"type": "text", "text": "Chunk items: 8 (L 1, E + I 2), 11 (L 2, E + I 3)"})] - (folder / f"{chunk_windows.chunk_session('r', 'item-8', held.claude_code_transcripts).id}.jsonl").write_text("".join(json.dumps(line) + "\n" for line in lines)) - # Its own window, item-7, holds nothing for it; item-9's stopped chunk holds its item. - fake = FakeEffects(windows="item-7\t0\t@1\nitem-8 ?\t0\t@2\nitem-9\t1\t@3\nitem-10\t0\t@4\n") - self.assertEqual(chunk_windows.held_beside("item-7", "/r", held, fake.effects()), [8, 9, 10, 11]) - self.assertEqual(fake.tmux_calls, [["list-windows", "-t", "=loop-r", "-F", "#{window_name}\t#{pane_dead}\t#{window_id}"]]) - - -class StartChunks(unittest.TestCase): - """start_chunks: the windows a loop opens for the chunks that may start now.""" - LAUNCHER = pathlib.Path("/b/loop-claude") - - def setUp(self): - made = tempfile.TemporaryDirectory() - self.addCleanup(made.cleanup) - self.root = pathlib.Path(made.name, "r") - self.home = configured(self, self.root).parents[1] - self.session = "loop-" + str(self.root).lstrip("/").replace("/", "-").replace(".", "-") - # Items 1 and 2 overlap, so 2 waits; 3 runs beside 1. - self.plan = ChunkWindows.plan(self, "a.py", "a.py", "c.py") - - def limit(self, chunks): - config = self.home / ".claude-projects" / self.root.name / "config.toml" - config.write_text(config.read_text() + f"parallel-chunks = {chunks}\n") - - def start(self, fake, chunk=None, leaving=False, plan=None, env=None, owner=None): - """start_chunks' answer, for a session whose @loop-root names owner, by default this checkout.""" - fake.owner = str(self.root) if owner is None else owner - run = main.LoopRun(0, str(self.root), "ship it", "", chunk=chunk) - with unittest.mock.patch.object(chunk_windows, "next_plan", return_value=plan or self.plan) as planned, contextlib.redirect_stderr(io.StringIO()) as said: - effects = fake.effects() - started = chunk_windows.start_chunks(run, leaving, paths.paths_from(self.home), env or {}, self.LAUNCHER, effects) - self.said = said.getvalue() - self.planned = planned.call_count - if planned.call_count: - planned.assert_called_once_with(str(self.root), self.home, effects) - return started - - def starts(self, fake): - """The window starts of fake's tmux calls, each as (command, window name, what it runs).""" - return [(call[0], call[call.index("-n") + 1], call[call.index("--") + 1:]) for call in fake.tmux_calls if call[0] in ("new-session", "new-window")] - - def test_opens_a_window_for_each_chunk_that_may_start_the_first_making_the_session(self): - self.limit(2) - fake = FakeEffects() - self.assertEqual(self.start(fake, env={"LOOP_CLAUDE_WORKER_MODEL": "m", "PATH": "/bin"}), ["item-1", "item-3"]) - self.assertEqual(self.starts(fake), [("new-session", "item-1", [str(self.LAUNCHER), "--chunk", "item-1", "ship it"]), ("new-window", "item-3", [str(self.LAUNCHER), "--chunk", "item-3", "ship it"])]) - first = next(call for call in fake.tmux_calls if call[0] == "new-session") - self.assertEqual((first[first.index("-s") + 1], first[first.index("-c") + 1]), (self.session, str(self.root))) - # Only loop-claude's own variables are handed on, since a window gets the tmux server's environment. - self.assertEqual([word for word in first if "=" in word], ["LOOP_CLAUDE_WORKER_MODEL=m"]) - self.assertEqual(fake.tmux_calls[0], ["list-windows", "-t", f"={self.session}", "-F", "#{window_name}\t#{pane_dead}\t#{window_id}"]) - # The session names the checkout whose chunks it runs. - self.assertIn(["set-option", "-t", f"={self.session}:", "@loop-root", str(self.root)], fake.tmux_calls) - - def test_refuses_a_session_that_runs_another_checkouts_chunks(self): - for owner, named in (("/c/acme.api", "/c/acme.api"), ("", "another checkout")): - with self.subTest(owner=owner): - fake = FakeEffects(windows="item-5\t0\t@1\n") - self.assertEqual(self.start(fake, owner=owner), Failure(f"the tmux session {self.session} runs the chunks of {named}, whose key maps to the same session name; rename one of the two checkouts' folders")) - self.assertEqual(self.starts(fake), []) - self.assertIn(["show-options", "-qv", "-t", f"={self.session}:", "@loop-root"], fake.tmux_calls) - - def test_refuses_a_checkout_do_epic_runs_chunks_in(self): - fake = FakeEffects(windows=f"epic-ABC-12\tABC-34 ?\t0\t@4\t{self.root}\nepic-ABC-12\tABC-35\t0\t@5\t/c/other\nepic-ABC-12\tABC-36\t1\t@6\t{self.root}\nloop-x\titem-1\t0\t@7\t{self.root}\n") - self.assertEqual(self.start(fake), Failure(f"do-epic runs chunks in {self.root}: ABC-34 of epic-ABC-12; loop-claude doesn't start a chunk there until those chunks finish")) - self.assertEqual(self.starts(fake), []) - - def test_leaves_out_the_items_a_running_chunk_names_on_its_chunk_items_line(self): - self.limit(2) - transcripts = paths.paths_from(self.home).claude_code_transcripts - folder = transcripts / "-r" - folder.mkdir(parents=True) - opened = chunk_windows.chunk_session(str(self.root).lstrip("/"), "item-5", transcripts) - (folder / f"{opened.id}.jsonl").write_text(json.dumps(assistant({"type": "text", "text": "Chunk items: 5 (L 1, E + I 2), 1 (L 1, E + I 2)"})) + "\n") - self.assertEqual(self.start(FakeEffects(windows="item-5\t0\t@1\n")), ["item-3"]) - - def test_starts_one_chunk_without_parallel_chunks(self): - fake = FakeEffects() - self.assertEqual(self.start(fake), ["item-1"]) - - def test_starts_none_and_reads_no_plan_while_the_running_chunks_fill_the_limit(self): - self.limit(2) - fake = FakeEffects(windows="item-5\t0\t@1\nitem-6 ?\t0\t@2\n") - self.assertEqual((self.start(fake), self.planned, self.starts(fake)), ([], 0, [])) - # More chunks run than the limit allows, as after it was lowered; a stopped chunk still waits. - fake = FakeEffects(windows="item-5\t0\t@1\nitem-6\t0\t@2\nitem-7\t0\t@3\nitem-8\t1\t@4\nitem-9\t1\t@5\n") - self.assertEqual((self.start(fake), self.planned), ([], 0)) - - def test_a_finishing_chunks_own_window_counts_as_no_longer_running(self): - fake = FakeEffects(windows="item-5\t0\t@1\n") - self.assertEqual(self.start(fake, chunk="item-5"), []) - fake = FakeEffects(windows="item-5\t0\t@1\n") - self.assertEqual(self.start(fake, chunk="item-5", leaving=True), ["item-1"]) - # The session exists, since the finishing loop's window is still in it. - self.assertEqual(self.starts(fake)[0][0], "new-window") - - def test_the_humans_loop_claude_starts_a_stopped_chunk_again_first_closing_its_dead_window(self): - fake = FakeEffects(windows="item-5\t1\t@3\n") - self.assertEqual((self.start(fake), self.planned), (["item-5"], 0)) - self.assertIn(["kill-window", "-t", "@3"], fake.tmux_calls) - # Its dead window was the session's last, so the session is made again. - self.assertEqual(self.starts(fake)[0][0], "new-session") - self.limit(2) - fake = FakeEffects(windows="item-5\t1\t@3\n") - self.assertEqual(self.start(fake), ["item-5", "item-1"]) - - def test_a_plan_with_no_item_starts_nothing_beside_a_restarted_chunk(self): - self.limit(2) - fake = FakeEffects(windows="item-5\t1\t@3\n") - self.assertEqual(self.start(fake, plan=next_chunk.NoItem("release 1.0.0 has no items")), ["item-5"]) - - def test_a_finishing_chunk_leaves_a_stopped_chunk_and_its_items_to_the_human(self): - self.limit(2) - transcripts = paths.paths_from(self.home).claude_code_transcripts - folder = transcripts / "-r" - folder.mkdir(parents=True) - opened = chunk_windows.chunk_session(str(self.root).lstrip("/"), "item-5", transcripts) - (folder / f"{opened.id}.jsonl").write_text(json.dumps(assistant({"type": "text", "text": "Chunk items: 5 (L 1, E + I 2), 1 (L 1, E + I 2)"})) + "\n") - fake = FakeEffects(windows="item-7\t0\t@1\nitem-5\t1\t@3\n") - # Item 1 is the stopped chunk's, so item 3 starts; the stopped one doesn't count against parallel-chunks. - self.assertEqual(self.start(fake, chunk="item-7", leaving=True), ["item-3"]) - self.assertEqual([call for call in fake.tmux_calls if call[0] == "kill-window"], []) - fake = FakeEffects(windows="item-7\t0\t@1\nno-item\t1\t@3\n") - self.assertEqual(self.start(fake, chunk="item-7", leaving=True, plan=next_chunk.NoItem("release 1.0.0 has no items")), []) - - def test_starts_the_chunk_without_a_plan_item_where_the_plan_cannot_be_read(self): - fake = FakeEffects() - self.assertEqual(self.start(fake, plan=Failure("no origin/main")), ["no-item"]) - self.assertIn("starting a chunk without a plan item, since the plan cannot be read: no origin/main", self.said) - - def test_says_why_a_window_could_not_start(self): - with unittest.mock.patch.object(tmux, "start_window", return_value=Failure("tmux could not start the window")): - self.assertEqual(self.start(FakeEffects()), Failure("tmux could not start the window")) - - def test_says_why_it_cannot_take_the_lock_on_starting_windows(self): - (self.home / ".local" / "state").mkdir(parents=True) - (self.home / ".local" / "state" / "loop-claude").write_text("") - started = self.start(FakeEffects()) - self.assertIsInstance(started, Failure) - self.assertTrue(started.reason.startswith(f"cannot lock {self.home}/.local/state/loop-claude/"), started.reason) - - def test_a_chunk_holds_its_own_item_and_those_on_its_latest_chunk_items_line(self): - with tempfile.TemporaryDirectory() as temp: - transcripts = pathlib.Path(temp) - opened = chunk_windows.chunk_session("proj", "item-7", transcripts) - self.assertEqual(opened.name, "A proj loop-claude item-7") - folder = transcripts / "-r" - folder.mkdir() - lines = [assistant({"type": "text", "text": "Chunk items: 7 (L 1, E + I 2)"}), assistant({"type": "text", "text": "Chunk items: 7 (L 1, E + I 2), 9 (L 2, E + I 3)"})] - (folder / f"{opened.id}.jsonl").write_text("".join(json.dumps(line) + "\n" for line in lines)) - self.assertEqual(chunk_windows.held_items(["item-7", "item-8", "no-item"], "proj", transcripts), {7, 8, 9}) - - -class ChunkFacts(unittest.TestCase): - # The launcher beside which chunk_start finds /b/chunk-preflight. - LAUNCHER = pathlib.Path("/b/loop-claude") - - def test_runs_the_preflight_at_the_checkout_root_and_quotes_what_it_printed(self): - fake = FakeEffects(python=[ran("- A fact.\n")]) - said = chunk_start.chunk_facts(a_loop(fake.effects(), launcher=self.LAUNCHER)) - self.assertEqual(fake.made("python"), [(["/b/chunk-preflight", "--worktree", "--item", "7"], "/r")]) - self.assertTrue(said.endswith("\n\n- A fact."), said) - self.assertNotIn("yourself", said) - - def test_a_chunk_without_an_item_runs_the_preflight_without_naming_one(self): - fake = FakeEffects(python=[ran("- A fact.\n")]) - chunk_start.chunk_facts(a_loop(fake.effects(), launcher=self.LAUNCHER, window=tmux.Window("loop-proj", "no-item", "%4"))) - self.assertEqual(fake.made("python"), [(["/b/chunk-preflight", "--worktree"], "/r")]) - - def test_the_facts_end_with_the_items_the_projects_other_running_chunks_hold(self): - # The loop's own window, a stopped one and two that run, one of them waiting for the human. - fake = FakeEffects(python=[ran("- A fact.\n")], windows="item-7\t0\t@1\nitem-5\t1\t@2\nitem-12 ?\t0\t@3\nitem-9\t0\t@4\n") - said = chunk_start.chunk_facts(a_loop(fake.effects(), launcher=self.LAUNCHER)) - self.assertTrue(said.endswith("- A fact.\n\nOther chunks of this project, running or stopped, hold items 5, 9, 12. Leave those items out of this chunk, even where the facts above list them as items that may join it."), said) - self.assertEqual(fresh_prompt.held_by_others([9]), "\n\nOther chunks of this project, running or stopped, hold item 9. Leave those items out of this chunk, even where the facts above list them as items that may join it.") - self.assertTrue(fresh_prompt.held_by_others([4, 9]).startswith("\n\nOther chunks of this project, running or stopped, hold items 4, 9.")) - # A worker whose preflight failed is told them too. - fake = FakeEffects(python=[ran(code=1, stderr="git could not start\n")], windows="item-9\t0\t@4\n") - said = chunk_start.chunk_facts(a_loop(fake.effects(), launcher=self.LAUNCHER)) - self.assertTrue(said.endswith("Other chunks of this project, running or stopped, hold item 9. Leave those items out of this chunk, even where the facts above list them as items that may join it."), said) - - def test_has_the_worker_find_the_facts_itself_where_the_preflight_failed(self): - for failed, why in ((ran(code=1, stderr="git could not start\n"), "git could not start"), (ran(code=1), "exited 1")): - with self.subTest(why=why): - said = chunk_start.chunk_facts(a_loop(FakeEffects(python=[failed]).effects(), launcher=self.LAUNCHER)) - self.assertIn("yourself", said) - self.assertIn(why, said) - - def test_runs_the_preflight_only_where_the_session_is_not_open(self): - with tempfile.TemporaryDirectory() as home: - opened = session.session_named("A proj loop-claude", pathlib.Path(home, "projects")) - fresh = FakeEffects(python=[ran("- A fact.\n")]) - facts, _ = chunk_start.chunk_start(a_loop(fresh.effects(), session=opened, launcher=self.LAUNCHER)) - self.assertEqual((fresh.made("python"), "- A fact." in facts), ([(["/b/chunk-preflight", "--worktree", "--item", "7"], "/r")], True)) - transcript = pathlib.Path(home, "projects", "-fake", f"{opened.id}.jsonl") - transcript.parent.mkdir(parents=True) - transcript.write_text("") - resumed = FakeEffects() - facts, _ = chunk_start.chunk_start(a_loop(resumed.effects(), session=opened, launcher=self.LAUNCHER)) - self.assertEqual((resumed.made("python"), facts), ([], "")) - - -class FreshPrompt(unittest.TestCase): - """loop-claude's first prompt of a chunk.""" - # What the preflight found, as the fresh prompt quotes it. - FACTS = "- A preflight fact." - - def test_names_the_route_in_a_fresh_prompt_only_where_an_item_routed_it(self): - routed = fresh_prompt.WORKER.fresh("Do it.", "## Goals", Route(Rung("m", "high"), 3, 4, 5), None, self.FACTS) - self.assertIn(f"Continue any open draft PR or worktree for the chunk before creating a new one. {prompts.READING_RULE} {fresh_prompt.NAME_THE_ITEMS}\n\nloop-claude runs you on m at high effort, routed by item 3's L of 4.", routed) - unrouted = fresh_prompt.WORKER.fresh("Do it.", "## Goals", Route(Rung("m")), None, self.FACTS) - self.assertIn(f"before creating a new one. {prompts.READING_RULE} {fresh_prompt.NAME_THE_ITEMS}\n\n{self.FACTS}\n\nThe chunk's", unrouted) - self.assertNotIn("loop-claude runs you on", unrouted) - - def test_has_the_routed_worker_count_its_items_size_again_and_puts_the_facts_before_the_scope(self): - routed = fresh_prompt.WORKER.fresh("Do it.", "## Goals", Route(Rung("m", "high"), 3, 4, 5), None, self.FACTS) - self.assertIn("item 3's size again", routed) - self.assertIn("escalation trigger 1", routed) - self.assertIn(f"\n\n{self.FACTS}\n\nThe chunk's", routed) - self.assertNotIn("size again", fresh_prompt.WORKER.fresh("Do it.", "## Goals", Route(Rung("m")), None, self.FACTS)) - - def test_gives_each_fresh_worker_the_reading_rule_once(self): - rule = "never read more than 80 lines of a file at once, nor take a tool output over 3,000 characters into your context" - for route in (Route(Rung("m", "high"), 3, 4, 5), Route(Rung("m"))): - with self.subTest(route=route): - self.assertEqual(fresh_prompt.WORKER.fresh("Do it.", "## Goals", route, None, self.FACTS).count(rule), 1) - - def test_quotes_what_the_preflight_printed_or_has_the_worker_find_the_facts_itself(self): - self.assertIn("\n\n- A fact.", fresh_prompt.preflight_facts("- A fact.")) - failed = fresh_prompt.preflight_failed("git could not start") - self.assertIn("yourself", failed) - self.assertIn("git could not start", failed) - - def test_has_each_fresh_worker_continue_an_open_draft_pr_or_worktree_once_whatever_the_preflight_found(self): - for facts in (fresh_prompt.preflight_facts("- A fact."), fresh_prompt.preflight_failed("git could not start")): - with self.subTest(facts=facts): - self.assertEqual(fresh_prompt.WORKER.fresh("Do it.", "## Goals", Route(Rung("m")), None, facts).count("Continue any open draft PR or worktree"), 1) - - def test_puts_the_rung_sentences_after_the_route_or_else_before_the_facts(self): - routed, unrouted = Route(OPUS_PROJECT.rung, 3, 2, 4), Route(OPUS_PROJECT.rung) - self.assertIn(f"routed by item 3's L of 2.{prompts.rungs_held(fresh_prompt.DRIVER, routed, OPUS_PROJECT)} After your first read", fresh_prompt.WORKER.fresh("Do it.", "## Goals", routed, OPUS_PROJECT, self.FACTS)) - self.assertIn(f"{fresh_prompt.NAME_THE_ITEMS}\n\n{prompts.rungs_held(fresh_prompt.DRIVER, unrouted, OPUS_PROJECT).lstrip()}\n\n{self.FACTS}", fresh_prompt.WORKER.fresh("Do it.", "## Goals", unrouted, OPUS_PROJECT, self.FACTS)) - - -# Item 297 scores the higher L, item 298 the higher E + I. -SCORED = "## Items\n\n| ID | Release | Exempt | Item | L | E | I | R | S | Moves | Score |\n|---|---|---|---|---|---|---|---|---|---|---|\n| 297 | 0.8.0 | | **Do it.** | 4 | 2 | 3 | 7 | 2 | 1 by 9 | 25 |\n| 298 | 0.8.0 | | **Do more.** | 3 | 3 | 4 | 8 | 2 | 1 by 9 | 20 |\n" - - -class ChunkItems(unittest.TestCase): - def test_names_the_items_on_the_workers_latest_chunk_items_line(self): - earlier = said("Chunk items: 297 (L 3, E + I 5)") - latest = said("Starting.\n**Chunk items:** 297 (L 4, E + I 5), item 298 (L 3, E + I 7), #12, 298\nOn to the tests.") - self.assertEqual(chunk_items.named([earlier, latest]), (297, 298, 12)) - self.assertEqual(chunk_items.named([latest, earlier]), (297,)) - self.assertEqual(chunk_items.named([earlier, said("Chunk items: none")]), ()) - self.assertEqual(chunk_items.named([said("Chunk items: 297 and 298 (L 3, E + I 7)")]), (297, 298)) - - def test_reads_the_example_line_the_prompt_gives_the_worker(self): - example = re.search(r"`(Chunk items:[^`]*)`", fresh_prompt.NAME_THE_ITEMS)[1] - self.assertEqual(chunk_items.named([said(example)]), (297, 298)) - - def test_every_prompt_that_starts_or_resumes_a_chunk_asks_for_the_line(self): - self.assertIn("on one line, as in `Chunk items: ", fresh_prompt.WORKER.resumed("", Route(Rung("m")), None)) - - def test_reads_only_a_line_the_worker_opens_with_the_label_in_its_own_text(self): - prompt = entries.LoopPrompt(text="Chunk items: 297 (L 3, E + I 5)") - written = entries.AssistantTurn(parts=(entries.ToolCall("t", "Write", {"content": "Chunk items: 9"}),)) - mentioned = said("Once I know them, I write `Chunk items: 5`.") - self.assertEqual(chunk_items.named([prompt, written, mentioned]), ()) - - def test_takes_the_highest_l_and_e_plus_i_todo_md_scores_among_the_named_items(self): - self.assertEqual(chunk_items.taken_in([said("Chunk items: 297 (L 1, E + I 2), 298, 400")], plan_of(SCORED)), TakenIn(4, 7)) - self.assertEqual(chunk_items.taken_in([said("Chunk items: 400")], plan_of(SCORED)), TakenIn()) - self.assertEqual(chunk_items.taken_in([], plan_of(SCORED)), TakenIn()) - - def test_names_the_items_at_no_level_where_todo_md_cannot_be_read(self): - self.assertEqual(chunk_items.taken_in([said("Chunk items: 297")], Failure("no todo.md")), TakenIn()) - self.assertEqual(chunk_items.taken_in([said("Chunk items: 297")], plan_of("no items table")), TakenIn()) - - -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", "unfinished_evidence": ""})], [json.dumps({"cycle_number": 3, "root": "/r", "task": "", "unfinished_evidence": ""})], [json.dumps({"cycle_number": "3", "root": "/r", "task": "do it", "unfinished_evidence": ""})], [json.dumps({"cycle_number": 3, "root": "", "task": "do it", "unfinished_evidence": ""})], [json.dumps({"cycle_number": 3, "root": "/r", "task": "do it", "turns": 1, "unfinished_evidence": ""})], [json.dumps({"cycle_number": 3, "failures": 0, "root": "/r", "task": "do it", "unfinished_evidence": ""})]): - with self.subTest(handed=handed): - self.assertEqual(main.loop_run_from([main.RELOAD_FLAG, *handed], CWD, FakeEffects().effects()), Stop(1, f"the edited loop-claude cannot read the state the running one handed it, likely because the edit changed that handover; rerun loop-claude . Handed: {' '.join(handed)}")) - - def test_decodes_every_field_of_the_state_and_its_route(self): - state = main.LoopRun(cycle_number=2, root="/r", task="do it", unfinished_evidence="old", route=Route(Rung("claude-fable-5-2", "low"), 3, 4, 5, 1, 7, 9), unfinished_run=2) - self.assertEqual(reload.decode_handed(json.dumps(dataclasses.asdict(state))), state) - self.assertEqual(set(reload.LOOP_RUN_FIELDS), {field.name for field in dataclasses.fields(main.LoopRun)}) - - def test_rejects_a_field_of_another_type_or_an_unknown_field(self): - state = dataclasses.asdict(main.LoopRun(cycle_number=2, root="/r", task="do it", unfinished_evidence="", route=Route(Rung("claude-fable-5-2", "low"), 3, 4, 5, 1, 7, 9), unfinished_run=2)) - for path, wrong in ((("unfinished_run",), "2"), (("unfinished_run",), True), (("route", "unfinished"), 1.0), (("route", "escalated_at"), "9"), (("route", "item"), True), (("route", "rung"), "claude-opus-5-5"), (("route", "rung", "model"), None), (("route",), "r"), (("route", "extra"), 1), (("route", "model"), "claude-opus-5-5"), (("route", "effort"), "high")): - with self.subTest(path=path, wrong=wrong): - handed = json.loads(json.dumps(state)) - target = handed - for key in path[:-1]: - target = target[key] - target[path[-1]] = wrong - self.assertIsNone(reload.decode_handed(json.dumps(handed))) - - def test_rejects_a_state_or_rung_missing_a_required_field(self): - state = dataclasses.asdict(main.LoopRun(cycle_number=2, root="/r", task="do it", unfinished_evidence="", route=Route(Rung("claude-fable-5-2", "low")))) - for path in (("cycle_number",), ("root",), ("task",), ("unfinished_evidence",), ("route", "rung"), ("route", "rung", "model")): - with self.subTest(path=path): - handed = json.loads(json.dumps(state)) - target = handed - for key in path[:-1]: - target = target[key] - del target[path[-1]] - self.assertIsNone(reload.decode_handed(json.dumps(handed))) - - def test_decodes_a_rung_without_an_effort(self): - state = main.LoopRun(cycle_number=1, root="/r", task="do it", unfinished_evidence="", route=Route(Rung("some-model"))) - self.assertEqual(reload.decode_handed(json.dumps({**dataclasses.asdict(state), "route": {"rung": {"model": "some-model"}}})), state) - - def test_rejects_text_that_is_not_json(self): - self.assertIsNone(reload.decode_handed("not json")) - - def test_decodes_a_route_inside_the_state_or_none(self): - state = main.LoopRun(cycle_number=1, root="/r", task="do it", unfinished_evidence="", route=Route(Rung("claude-fable-5-2", "low"), 3, 4, 5, 1)) - self.assertEqual(reload.decode_handed(json.dumps(dataclasses.asdict(state))), state) - unrouted = dataclasses.replace(state, route=None) - self.assertEqual(reload.decode_handed(json.dumps(dataclasses.asdict(unrouted))), unrouted) - handed = dataclasses.asdict(state) - handed["route"]["rung"]["effort"] = 1 - self.assertIsNone(reload.decode_handed(json.dumps(handed))) - del handed["route"]["rung"] - self.assertIsNone(reload.decode_handed(json.dumps(handed))) - off_scale = dataclasses.asdict(state) - off_scale["route"]["likelihood"] = 9 - self.assertIsNone(reload.decode_handed(json.dumps(off_scale))) - older = dataclasses.asdict(state) - del older["route"]["escape_and_impact_taken_in"] - self.assertEqual(reload.decode_handed(json.dumps(older)), state) - taken = dataclasses.replace(state, route=dataclasses.replace(state.route, escape_and_impact_taken_in=7)) - self.assertEqual(reload.decode_handed(json.dumps(dataclasses.asdict(taken))), taken) - for taken_in in (9, 5): - with self.subTest(taken_in=taken_in): - handed = dataclasses.asdict(state) - handed["route"]["escape_and_impact_taken_in"] = taken_in - self.assertIsNone(reload.decode_handed(json.dumps(handed))) - - def test_decodes_a_state_from_before_a_field_with_a_default_joined_it(self): - self.assertEqual(reload.decode_handed(json.dumps({"cycle_number": 3, "root": "/r", "task": "do it", "unfinished_evidence": ""})), main.LoopRun(3, "/r", "do it", "")) - - def test_drops_the_items_taken_in_that_a_loop_started_before_0_11_0_hands_on(self): - state = main.LoopRun(cycle_number=1, root="/r", task="do it", unfinished_evidence="") - self.assertEqual(reload.decode_handed(json.dumps(dataclasses.asdict(state) | {"items_taken_in": [12, 14]})), state) - - def test_drops_the_slot_that_a_loop_started_before_chunks_ran_in_windows_hands_on(self): - state = main.LoopRun(cycle_number=1, root="/r", task="do it", unfinished_evidence="") - self.assertEqual(reload.decode_handed(json.dumps({key: value for key, value in dataclasses.asdict(state).items() if key != "chunk"} | {"slot": 1})), state) - - def test_stops_when_the_reload_cannot_start_python(self): - with tempfile.TemporaryDirectory() as root: - launcher = pathlib.Path(root, "loop-claude") - launcher.write_text("") - fake = FakeEffects(python=[ran()]) - self.assertEqual(reload.exec_if_edited(a_loop(fake.effects(), launcher=launcher), "stale", main.LoopRun(cycle_number=1, root="/r", task="do it", unfinished_evidence="")), Stop(1, "could not reload loop-claude: No such file or directory; rerun loop-claude ")) - - -class WindowStarts(unittest.TestCase): - """The loop-claude the human runs, which starts the windows, and the stop of a window's loop whose chunk finished.""" - HOME = pathlib.Path("/h") - - def run_loop(self, argv, fake, env=None, started=("item-3", "item-4")): - """run_loop's stop and what it said on stdout, with start_chunks answering `started`; self.asked holds what start_chunks was asked.""" - with unittest.mock.patch.object(main, "start_chunks", return_value=list(started) if isinstance(started, tuple) else started) as start, contextlib.redirect_stdout(io.StringIO()) as said: - stop = main.run_loop(argv, env or {}, paths.paths_from(self.HOME), pathlib.Path("/b/loop-claude"), CWD, fake.effects()) - self.asked = [call.args[:2] for call in start.call_args_list] - return stop, said.getvalue() - - def test_refuses_to_start_anything_on_a_tmux_older_than_needed(self): - fake = FakeEffects(git=["/r\n"], tmux_version="tmux 3.1c") - self.assertEqual(self.run_loop(["do it"], fake), (Stop(1, "loop-claude runs each chunk in a tmux window, and needs tmux 3.2 or later; it found tmux 3.1c"), "")) - self.assertEqual((fake.tmux_calls, self.asked), ([["-V"]], [])) - - def test_prints_the_help_without_tmux(self): - fake = FakeEffects(tmux_version="") - self.assertEqual(self.run_loop(["--help"], fake)[0], Stop(0, usage.HELP)) - self.assertEqual(fake.tmux_calls, []) - - def test_starts_the_chunks_then_attaches_to_the_session_or_inside_tmux_switches_to_it(self): - for env, shown in (({}, ["attach-session", "-t", "=loop-r"]), ({"TMUX": "/tmp/tmux-1000/default,1,0"}, ["switch-client", "-t", "=loop-r"])): - with self.subTest(shown=shown): - fake = FakeEffects(git=["/r\n"]) - stop, said = self.run_loop(["do", "it"], fake, env) - self.assertEqual((stop, said), (Stop(0, ""), f"{STAMP} started item-3, item-4 in the tmux session loop-r\n")) - self.assertEqual(fake.tmux_calls, [["-V"], shown]) - self.assertEqual(self.asked, [(main.LoopRun(0, "/r", "do it", ""), False)]) - - def test_hands_the_window_starts_its_paths_environment_launcher_and_effects(self): - fake = FakeEffects(git=["/r\n"], on_terminal=False) - effects, env, launcher = fake.effects(), {"LOOP_CLAUDE_X": "1"}, pathlib.Path("/b/loop-claude") - with unittest.mock.patch.object(main, "start_chunks", return_value=["item-3"]) as start, contextlib.redirect_stdout(io.StringIO()): - main.run_loop(["do it"], env, paths.paths_from(self.HOME), launcher, CWD, effects) - self.assertEqual(start.call_args.args, (main.LoopRun(0, "/r", "do it", ""), False, paths.paths_from(self.HOME), env, launcher, effects)) - - def test_a_window_loop_runs_its_chunk_in_its_own_session_and_window_and_hands_on_once_it_finished(self): - fake = FakeEffects(git=["/r\n"]) - effects, env = fake.effects(), {"TMUX_PANE": "%4"} - launcher = pathlib.Path("/b/loop-claude") - cycle = unittest.mock.Mock(project=OPUS_PROJECT) - unfinished = unittest.mock.Mock(verdict=unittest.mock.Mock(spec=Unfinished)) - finished = unittest.mock.Mock(verdict=unittest.mock.Mock(spec=Finished)) - closed = [main.LoopRun(1, "/r", "do it", "", chunk="item-3"), main.LoopRun(2, "/r", "do it", "", chunk="item-3")] - with unittest.mock.patch.object(main, "fingerprint", return_value=""), unittest.mock.patch.object(main, "exec_if_edited", return_value=None), unittest.mock.patch.object(main, "next_cycle", return_value=cycle) as next_cycle, \ - unittest.mock.patch.object(main, "run_cycle", side_effect=[unfinished, finished]), unittest.mock.patch.object(main, "closed_cycle", side_effect=closed), \ - unittest.mock.patch.object(main, "check_routing", return_value=None), unittest.mock.patch.object(main, "handed_on", return_value=Stop(0, "")) as handed_on: - stop = main.run_loop(["--chunk", "item-3", "do it"], env, paths.paths_from(self.HOME), launcher, CWD, effects) - 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=paths.paths_from(self.HOME), env=env, launcher=launcher, effects=effects) - self.assertEqual(stop, Stop(0, "")) - self.assertEqual([call.args for call in next_cycle.call_args_list], [(main.LoopRun(1, "/r", "do it", "", chunk="item-3"), loop), (main.LoopRun(2, "/r", "do it", "", chunk="item-3"), loop)]) - handed_on.assert_called_once_with(closed[1], loop) - - 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)) - - def test_a_window_loop_outside_a_checkout_stops(self): - self.assertIsInstance(main.loop_run_from(["--chunk", "item-3", "do it"], CWD, FakeEffects(git=[Failure("git cannot start")]).effects()), Stop) - - def test_says_that_nothing_started_where_the_session_already_runs_every_chunk_that_may(self): - self.assertEqual(self.run_loop(["do it"], FakeEffects(git=["/r\n"]), started=())[1], f"{STAMP} the tmux session loop-r runs every chunk that may run now, so `do it` started in no new window\n") - - def test_off_a_terminal_it_prints_how_to_attach(self): - fake = FakeEffects(git=["/r\n"], on_terminal=False) - stop, said = self.run_loop(["do it"], fake) - self.assertEqual(stop, Stop(0, "")) - self.assertTrue(said.endswith(f"{STAMP} not on a terminal; see the chunks with `tmux attach -t =loop-r`\n"), said) - self.assertEqual(fake.tmux_calls, [["-V"]]) - - def test_stops_where_no_chunk_could_start(self): - self.assertEqual(self.run_loop(["do it"], FakeEffects(git=["/r\n"]), started=Failure("no plan"))[0], Stop(1, "no plan")) - - def test_stops_where_tmux_cannot_show_the_session(self): - fake = FakeEffects(git=["/r\n"]) - effects = dataclasses.replace(fake.effects(), tmux_on_terminal=lambda args: 1) - with unittest.mock.patch.object(main, "start_chunks", return_value=["item-3"]), contextlib.redirect_stdout(io.StringIO()): - stop = main.run_loop(["do it"], {}, paths.paths_from(self.HOME), pathlib.Path("/b/loop-claude"), CWD, effects) - self.assertEqual(stop, Stop(1, "`tmux attach-session -t =loop-r` failed; the chunks go on in the tmux session")) - - def test_a_window_loop_outside_tmux_stops(self): - fake = FakeEffects(git=["/r\n"]) - self.assertEqual(self.run_loop(["--chunk", "item-3", "do it"], fake)[0], Stop(1, "--chunk is how loop-claude starts itself in a tmux window; run loop-claude ")) - self.assertEqual(self.asked, []) - - def test_reads_the_chunk_a_window_loop_is_started_with(self): - self.assertEqual(main.loop_run_from(["--chunk", "item-3", "do", "it"], CWD, FakeEffects(git=["/r\n"]).effects()), main.LoopRun(0, "/r", "do it", "", chunk="item-3")) - self.assertEqual(main.loop_run_from(["do", "it"], CWD, FakeEffects(git=["/r\n"]).effects()), main.LoopRun(0, "/r", "do it", "")) - - def test_a_finished_chunk_starts_each_chunk_that_may_start_and_stops_without_a_reason(self): - loop_run = main.LoopRun(4, "/r", "do it", "", chunk="item-7") - for started, said, stop in ((["item-9", "item-12"], "the chunk finished; started item-9, item-12", Stop(0, "")), ([], "the chunk finished; started no other, since the chunks running fill what may run now", Stop(0, "")), - (Failure("no plan"), "", Stop(1, "the chunk finished, but the next could not start: no plan"))): - with self.subTest(started=started), unittest.mock.patch.object(main, "start_chunks", return_value=started) as start, contextlib.redirect_stdout(io.StringIO()) as printed: - loop = a_loop(FakeEffects().effects(), env={"LOOP_CLAUDE_X": "1"}) - self.assertEqual(main.handed_on(loop_run, loop), stop) - self.assertEqual(printed.getvalue(), f"{STAMP} {said}\n" if said else "") - self.assertEqual(start.call_args.args, (loop_run, True, loop.paths, loop.env, loop.launcher, loop.effects)) - - -class StopDms(unittest.TestCase): - def run_main(self, argv, fake): - with tempfile.TemporaryDirectory() as home, contextlib.redirect_stderr(io.StringIO()) as said, contextlib.redirect_stdout(io.StringIO()): - code = main.main(argv, {}, paths.paths_from(pathlib.Path(home)), pathlib.Path(home, "loop-claude"), CWD, fake.effects()) - return code, said.getvalue() - - def test_dms_the_reason_of_a_stop_with_exit_2_such_as_a_chunk_left_unfinished_too_often(self): - fake = FakeEffects() - with unittest.mock.patch.object(main, "run_loop", return_value=Stop(2, "the human is needed to look at the chunk, but it stayed unfinished")): - self.assertEqual(self.run_main(["do it"], fake)[0], 2) - self.assertEqual(fake.dms, [f"loop-claude in {CWD} stopped: the human is needed to look at the chunk, but it stayed unfinished"]) - - def test_skips_the_dm_of_a_stop_without_a_reason_as_once_the_windows_started(self): - fake = FakeEffects() - with unittest.mock.patch.object(main, "run_loop", return_value=Stop(0, "")): - self.assertEqual(self.run_main(["do it"], fake), (0, "")) - self.assertEqual(fake.dms, []) - - def test_skips_the_dm_where_ctrl_c_stopped_the_loop(self): - fake = FakeEffects() - with unittest.mock.patch.object(main, "run_loop", side_effect=KeyboardInterrupt): - self.assertEqual(self.run_main(["do it"], fake)[0], 130) - self.assertEqual(fake.dms, []) - - def test_dms_a_crash_before_it_reaches_the_terminal(self): - fake = FakeEffects() - with unittest.mock.patch.object(main, "run_loop", side_effect=RuntimeError("boom")), self.assertRaises(RuntimeError): - self.run_main(["do it"], fake) - self.assertEqual(fake.dms, [f"loop-claude in {CWD} crashed: RuntimeError('boom')"]) - - def test_dms_the_reason_where_a_loop_given_a_task_stops_with_exit_1(self): - fake = FakeEffects() - code, said = self.run_main([main.RELOAD_FLAG, "3"], fake) - self.assertEqual(code, 1) - self.assertEqual(fake.dms, [f"loop-claude in {CWD} stopped: the edited loop-claude cannot read the state the running one handed it, likely because the edit changed that handover; rerun loop-claude . Handed: 3"]) - - def test_says_why_the_dm_of_a_stop_failed_after_the_reason(self): - fake = FakeEffects(dm_failures=[Failure("cannot reach https://chat.example: timed out")]) - self.assertTrue(self.run_main([main.RELOAD_FLAG, "3"], fake)[1].endswith(f"Handed: 3\n{STAMP} could not DM you: cannot reach https://chat.example: timed out\n")) - - def test_skips_the_dm_where_it_prints_its_help(self): - for argv, code in (([], 1), (["--help"], 0)): - with self.subTest(argv=argv): - fake = FakeEffects() - self.assertEqual(self.run_main(argv, fake)[0], code) - self.assertEqual(fake.dms, []) - - -class HelpLimits(unittest.TestCase): - def test_help_wraps_at_100_columns(self): - self.assertEqual([line for line in usage.HELP.splitlines() if len(line) > 100], []) - - def test_help_states_the_limits_the_code_holds(self): - said = " ".join(usage.HELP.split()) - self.assertIn(f"The {['second', 'third', 'fourth', 'fifth'][states.QUOTED_ANSWERS_PER_CYCLE - 1]} question they answer in one cycle", said) - # The retry messages say these limits in words too. - self.assertEqual((retry.FAILURES_RUNNING, retry.RETRY_DELAY), (3, 60)) - self.assertIn("fails three times running, retried a minute apart", said) - self.assertIn(f"retried {retry.PAST_RESET // 60} minutes after the limit resets, or every {retry.UNKNOWN_RESET_POLL // 60} minutes", said) - self.assertIn(f"still running after {PYTHON_TIMEOUT} s", said) - self.assertIn(f"Once {state.CYCLES_PER_CHECK} cycles have closed across all projects", said) - self.assertIn(f"run with a {worker.COMPACT_WINDOW // 1000}k auto-compact window", said) - self.assertIn(f"{BASH_CEILING_CHARACTERS:,} characters", said) - self.assertIn(f"unfinished {chunk_run.UNFINISHED_LIMIT} cycles running", said) - self.assertIn(f"rerun to give it {chunk_run.UNFINISHED_LIMIT} more", said) - - -class OwnLadder(unittest.TestCase): - def test_a_cycle_on_a_projects_own_ladder_names_its_file_and_says_where_the_workers_model_is_off_it(self): - file = pathlib.Path("/h/.claude-projects/acme/routing-ladder.json") - self.assertEqual(main.own_ladder(file, "claude-fable-5-2", False), f"routing on the project's own ladder, {file}.") - self.assertEqual(main.own_ladder(file, "claude-opus-5-5", True), f"routing on the project's own ladder, {file}. claude-opus-5-5 is not on it, so the worker runs on claude-opus-5-5 unrouted; LOOP_CLAUDE_WORKER_MODEL and LOOP_CLAUDE_JUDGE_MODEL name other models for a run.") - - -class ExitContract(unittest.TestCase): - def test_every_exit_1_stop_has_its_clause_in_help(self): - read, git, premises_ = "a transcript cannot be read or archived", "git cannot start or finds no checkout", "no config names the checkout, its config cannot be read, or the goals file holds no premises" - models_ = "the models file cannot be read or written" - names = (*records.RECORD_FILES, records.HUMAN_RULE_NAMES[0], f"{records.HUMAN_RULE_NAMES[1]}*.md", records.HUMAN_RULE_NAMES[2]) - unreadable = f"the goals file, or {', '.join(names[:-1])} or {names[-1]} where they exist, cannot be read or is not UTF-8" - # Every Stop whose code is not a literal 0, 2 or 130, in source order: path, function, code, start of reason, its --help clause. - clauses = [ - ("agent_cycle/interrupt.py", "claude_open", "1", "f'could not start claude for the interrupt:", "claude cannot start for an interrupt"), - ("agent_cycle/interrupt.py", "human_typed", "1", "entries.reason", read), - ("agent_cycle/interrupt.py", "human_typed", "1", "f'could not tell whether you typed anything:", read), - ("agent_cycle/retry.py", "retried", "1", "f'the {who} failed three times running:", "the worker or the judge fails three times running"), - ("agent_cycle/run.py", "archived_stop", "1", "f'{failure.reason}", read), - ("agent_cycle/run.py", "work", "1", "f'claude left no transcript for session", "claude leaves no transcript"), - ("agent_cycle/run.py", "rule", "1", "entries.reason", read), - ("agent_cycle/run.py", "rule", "1", "annotation.reason", read), - ("agent_cycle/run.py", "rule", "1", "failure.reason", read), - ("agent_cycle/run.py", "keep_rejection", "1", "unkept.reason", read), - ("agent_cycle/run.py", "close", "1", "annotation.reason", read), - ("agent_cycle/run.py", "close", "1", "failure.reason", read), - ("agent_cycle/run.py", "close", "1", "failure.reason", "a finished chunk's session cannot be deleted"), - ("chunk_driver/chunk_run.py", "closed_cycle", "1", "failure.reason", read), - ("chunk_driver/cycle_start.py", "project_routing", "1", "ladder_file.reason", premises_), - ("chunk_driver/cycle_start.py", "routing_on", "1", "ladder.reason", "the project's routing ladder cannot be read or holds a fault"), - ("chunk_driver/project/model_choice.py", "get_or_ask_model", "1", "models.reason", models_), - ("chunk_driver/project/model_choice.py", "get_or_ask_model", "1", "models.reason", models_), - ("chunk_driver/project/model_choice.py", "get_or_ask_model", "1", "f'could not save the model in", models_), - ("loop_claude/loop/checkout.py", "checkout_root", "1", "found.reason", git), - ("loop_claude/loop/main.py", "run_loop", "1", "reason", "tmux is missing or older than 3.2"), - ("loop_claude/loop/main.py", "run_loop", "1", "f'{CHUNK_FLAG} is how loop-claude starts itself", "--chunk is given outside a tmux window"), - ("loop_claude/loop/main.py", "windows_opened", "1", "started.reason", "a chunk's window cannot be started"), - ("loop_claude/loop/main.py", "windows_opened", "1", "f\"`tmux {' '.join(shown)}` failed", "the tmux session cannot be put on the terminal"), - ("loop_claude/loop/main.py", "handed_on", "1", "f'the chunk finished, but the next could not start", "a chunk's window cannot be started"), - ("loop_claude/loop/main.py", "next_cycle", "1", "premises.reason", premises_), - ("loop_claude/loop/main.py", "next_cycle", "1", "records.reason", unreadable), - ("loop_claude/loop/main.py", "loop_run_from", "0 if task else 1", "HELP", "no task is given"), - ("loop_claude/loop/main.py", "loop_run_from", "1", 'f"the edited loop-claude cannot read the state', "a reload cannot read the state the running script handed it"), - ("loop_claude/loop/reload.py", "exec_if_edited", "1", "f'could not reload loop-claude:", "python cannot start for a reload"), - ("loop_claude/loop/routing_check.py", "raise_calls", "1", "f\"could not start claude for the routing calls in {loop.paths.config_clone}", "claude cannot start for an interrupt"), - ] - root = pathlib.Path(main.__file__).parents[2] - found = [] - for path in sorted(path for package in ("agent_cycle", "chunk_driver", "loop_claude") for path in (root / package).rglob("*.py") if not path.name.startswith("test_")): - 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[0]), 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, code, reason) for _, function, code, reason in sorted(stops)] - self.assertEqual(len(found), len(clauses), found) - for (path, function, code, reason), (want_path, want_function, want_code, reason_start, _) in zip(found, clauses): - with self.subTest(path=path, function=function): - self.assertEqual((path, function, code), (want_path, want_function, want_code)) - self.assertTrue(reason.startswith(reason_start), reason) - help_exit_1 = " ".join(usage.HELP[usage.HELP.index("Exit 1"):usage.HELP.index("Exit 2")].split()) - self.assertEqual([clause for *_, clause in clauses if f"- {clause}" not in help_exit_1], []) - - -class JudgeFailures(unittest.TestCase): - def test_every_failed_judge_run_has_its_clause_in_help(self): - started, exited, unparsed = "claude cannot start", "exits non-zero", "prints no json reply" - field, unasked = "its reply misses or mistypes a field", "interrupts or quotes an answer without a question" - # Every Failure the judge's run returns, in source order of judge.py, then of the claude run it calls: function, start of reason, its --help clause. - clauses = [ - ("verdict_from", "'the judge returned no reply'", field), - ("verdict_from", 'f"the judge\'s reply has no {name}', field), - ("verdict_from", "'the judge interrupted without a question'", unasked), - ("verdict_from", "'the judge quoted an answer without a question'", unasked), - ("run_judge", "verdict.reason", field), - ("run_judge", "checked.reason", "quotes a passage the transcript does not hold"), - ("misquoted_evidence", "f'the judge quoted {verdict.evidence!r}, which is not in the transcript", "quotes a passage the transcript does not hold"), - ("claude_reply", "f'could not start claude:", started), - ("claude_reply", "proc.stderr.strip() or f'{who} exited", exited), - ("claude_reply", 'f"{who} printed no json reply', unparsed), - ] - found = [] - for module in (judge, reply): - functions = [node for node in ast.walk(ast.parse(pathlib.Path(module.__file__).read_text())) if isinstance(node, ast.FunctionDef)] - found += sorted((call.lineno, function.name, ast.unparse(call.args[0])) for function in functions for call in ast.walk(function) if isinstance(call, ast.Call) and getattr(call.func, "id", None) in ("Failure", "Rejected")) - self.assertEqual(len(found), len(clauses), found) - for (_, function, reason), (want_function, reason_start, _) in zip(found, clauses): - with self.subTest(function=function): - self.assertEqual(function, want_function) - self.assertTrue(reason.startswith(reason_start), reason) - judge_clause = " ".join(usage.HELP[usage.HELP.index("a judge when"):usage.HELP.index("A usage limit")].split()) - self.assertEqual([clause for *_, clause in clauses if clause not in judge_clause], []) - - -if __name__ == "__main__": - unittest.main() diff --git a/bin/loop_claude/test_routed_item.py b/bin/loop_claude/test_routed_item.py new file mode 100644 index 00000000..25458df4 --- /dev/null +++ b/bin/loop_claude/test_routed_item.py @@ -0,0 +1,53 @@ +import pathlib +import re +import sys +import unittest + +sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) + +from failure import Failure +from release_plan.release_standing import LastCut +import loop_claude.routed_item +from loop_claude.doubles import NO_CUT, TODO +from plan_store.todo_md import plan_of + + +def routed_item(todo, cut): + """loop_claude.routed_item.routed_item for the plan a todo.md's text holds.""" + return loop_claude.routed_item.routed_item(plan_of(todo), cut) + + +class RoutedItem(unittest.TestCase): + def test_routes_on_the_next_releases_top_item_that_waits_on_no_question(self): + self.assertEqual(routed_item(TODO, NO_CUT).id, 3) + self.assertIsNone(routed_item(TODO.replace("| | **Do", "| question | **Do"), NO_CUT)) + self.assertIsNone(routed_item("# todo\n", NO_CUT)) + self.assertIsNone(routed_item(TODO[:TODO.index("| 7 |")], NO_CUT)) + old_shape = "## Items\n\n| ID | Release | Exempt | Item | R | S | C | A | G | Goals | Score |\n|---|---|---|---|---|---|---|---|---|---|---|\n| 3 | 0.2.0 | | **Do the next thing.** | 5 | 3 | 6 | 8 | 9 | 1 | 25 |\n" + self.assertIsNone(routed_item(old_shape, NO_CUT)) + self.assertEqual(routed_item(TODO.replace("| 4 | 2 | 3 |", "| x | 2 | 3 |"), NO_CUT), Failure("item 3's L 'x' is not a whole number from 1 to 4")) + + def test_routes_on_no_item_where_the_release_after_the_last_cut_has_none(self): + self.assertEqual(routed_item(TODO, LastCut("v0.1.0", "", 3, [TODO])).id, 3) + # Someone cut 0.2.0 while it held items: they count as 0.3.0's, the next release, and where no later release is known, no item routes. + self.assertEqual(routed_item(TODO, LastCut("v0.2.0", "", 3, [TODO])).id, 3) + self.assertIsNone(routed_item(re.sub(r"(?m)^\| \d+ \| 0\.3\.0 \|.*\n", "", TODO), LastCut("v0.2.0", "", 3, []))) + later_only = TODO[:TODO.index("| 7 |")] + TODO[TODO.index("| 1 | 0.3.0 |"):] + self.assertIsNone(routed_item(later_only, LastCut("v0.1.0", "", 3, [TODO]))) + self.assertIsNone(routed_item(later_only, LastCut("v0.1.0", "", 0, [TODO]))) + self.assertEqual(routed_item(later_only, LastCut("v0.2.0", "", 3, [later_only])).id, 1) + + def test_routes_past_an_item_a_waiting_item_blocks(self): + todo = "## Items\n\n| ID | Release | Blocked by | Exempt | Item | L | E | I | R | S | Moves | Score |\n|---|---|---|---|---|---|---|---|---|---|---|---|\n| 7 | 0.2.0 | | question | **Ask first.** | 4 | 3 | 5 | 10 | 1 | 1 by 9 | 30 |\n| 8 | 0.2.0 | 7 | | **Use the answer.** | 1 | 1 | 1 | 1 | 1 | 1 by 9 | 30 |\n| 9 | 0.2.0 | 8 | | **Build on it.** | 1 | 1 | 1 | 1 | 1 | 1 by 9 | 30 |\n| 3 | 0.2.0 | | | **Do the next thing.** | 4 | 2 | 3 | 7 | 3 | 1 by 9 | 25 |\n" + self.assertEqual(routed_item(todo, NO_CUT).id, 3) + self.assertEqual(routed_item(todo.replace("| question |", "| |"), NO_CUT).id, 7) + first = "| 7 | 0.2.0 | | question | **Ask first.** | 4 | 3 | 5 | 10 | 1 | 1 by 9 | 30 |\n" + self.assertEqual(routed_item(todo.replace(first, "") + first, NO_CUT).id, 3) + + def test_routes_nothing_on_an_item_without_e_or_i(self): + without_i = TODO.replace("| E | I | R |", "| E | R |").replace("|---|---|---|---|---|---|---|---|---|---|---|", "|---|---|---|---|---|---|---|---|---|---|").replace("| 4 | 3 | 5 | 10 |", "| 4 | 3 | 10 |").replace("| 4 | 2 | 3 | 7 |", "| 4 | 2 | 7 |").replace("| 1 | 1 | 1 | 1 |", "| 1 | 1 | 1 |") + self.assertIsNone(routed_item(without_i, NO_CUT)) + + +if __name__ == "__main__": + unittest.main() diff --git a/bin/metric_bench/test_metric_bench.py b/bin/metric_bench/test_metric_bench.py index eef60c2f..381585d0 100644 --- a/bin/metric_bench/test_metric_bench.py +++ b/bin/metric_bench/test_metric_bench.py @@ -15,7 +15,7 @@ from review_rules.comprehension_history import entries, Place from failure import Failure from test_env import isolated_env from metric_bench.main import scored_runs -from project_config.test_project_config import write_config +from project_config.doubles import write_config TOOL = pathlib.Path(__file__).resolve().parents[1] / "metric-bench" diff --git a/bin/model_routing/test_ladder_file.py b/bin/model_routing/test_ladder_file.py index a4693b40..73ffef27 100644 --- a/bin/model_routing/test_ladder_file.py +++ b/bin/model_routing/test_ladder_file.py @@ -10,7 +10,7 @@ from failure import Failure from test_env import isolated_env from model_routing.ladder import Ladder, Rung from model_routing.ladder_file import NAME, project_ladder, project_ladder_file, read_ladder, SHIPPED -from project_config.test_project_config import write_config +from project_config.doubles import write_config FABLE_ONLY = '[\n\t{"model": "claude-fable-5-2", "effort": "low"},\n\t{"model": "claude-fable-5-2", "effort": "max"}\n]\n' FABLE_LADDER = Ladder((Rung("claude-fable-5-2", "low"), Rung("claude-fable-5-2", "max"))) diff --git a/bin/notifier/test_notifier.py b/bin/notifier/test_notifier.py index ee563e57..1961e536 100644 --- a/bin/notifier/test_notifier.py +++ b/bin/notifier/test_notifier.py @@ -10,7 +10,7 @@ import unittest sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) -from project_config.test_project_config import configured +from project_config.doubles import configured from failure import Failure from rest_client.json_request import Http from test_env import isolated_env diff --git a/bin/plan/doubles.py b/bin/plan/doubles.py new file mode 100644 index 00000000..962f0b62 --- /dev/null +++ b/bin/plan/doubles.py @@ -0,0 +1,87 @@ +"""Test doubles for the tools that read a plan: a goals file, a decision log and plans in todo.md's shapes.""" + +# Voids code health, so these tests weigh only the project's own goals; class CodeHealth weighs it. +VOID = "**Void:** code health, since a test project has no code to read.\n\n" + +README = "# P\n\n## Goals\n\n" + VOID + "1. **Achieve:** Fast.\n2. **Maintain:** Correct.\n 1. **Achieve:** Tested.\n\n## Audience\n\nMe.\n" + +DECISIONS = "## A choice\n\nStands until `todo.md` item 5 ends it.\n" + +TODO = """# todo + +## Scoring + +`Score = 100 * V / 2**S` + +`Bar = 2` + +`Next ID = 6` + +## Items + +| ID | Release | Blocked by | Exempt | Item | L | E | I | R | S | Moves | Score | +|---|---|---|---|---|---|---|---|---|---|---|---| +| 1 | 0.2.0 | | | **Ship the later thing.** | 1 | 1 | 1 | 1 | 1 | 1 by 9 | 0 | +| 2 | 0.1.0 | | | **Tighten the tests.** | 1 | 2 | 1 | 2 | 4 | 2.1 by 1 | 0 | +| 3 | 0.1.0 | | | **Make it fast, `a \\| b`.** | 2 | 1 | 2 | 3 | 7 | 2.1 by 8, 1 by 6 | 0 | +| 4 | 0.1.0 | | security | **Rotate the leaked key.** | 3 | 3 | 5 | 9 | 2 | 2.1 by 1 | 0 | +| 5 | 0.1.0 | | decision | **Move the writes out.** | 1 | 1 | 4 | 5 | 5 | 2.1 by 1 | 0 | + +## Details + +### 1. stale heading + +Later detail. + +### 3. Make it fast. + +Fast detail. + +## Notes + +Kept as written. +""" + +OLD_SHAPE = """# todo + +## Scoring + +`Score = -R - S/4 + 2*A + 2*G*W` + +`Bar = 9` + +`Next ID = 6` + +| Goal | W | +|---|---| +| 1 | 1.00 | +| 2 | 0.67 | +| 2.1 | 0.33 | + +## Items + +| ID | Release | Blocked by | Exempt | Item | R | S | C | A | G | Goals | Score | +|---|---|---|---|---|---|---|---|---|---|---|---| +| 1 | 0.2.0 | | | **Ship the later thing.** | 1 | 1 | 3 | 9 | 9 | 1 | 0 | +| 2 | 0.1.0 | | | **Tighten the tests.** | 2 | 4 | 3 | 5 | 5 | 2.1 | 0 | +| 3 | 0.1.0 | | | **Make it fast, `a \\| b`.** | 3 | 8 | 3 | 6 | 8 | 2.1, 1 | 0 | +| 4 | 0.1.0 | | security | **Rotate the leaked key.** | 9 | 2 | 3 | 1 | 1 | 2.1 | 0 | +| 5 | 0.1.0 | | decision | **Move the writes out.** | 5 | 5 | 3 | 4 | 5 | 2.1 | 0 | + +## Details + +### 1. stale heading + +Later detail. + +### 3. Make it fast. + +Fast detail. + +## Notes + +Kept as written. +""" + +# A scoring run of the project whose overall mean is {overall}. +SCORED = "# Comprehension history\n\n## 2026-10-08T12:00:00Z, PR #1 at aaaaaaa\n\n| Seat | Overall |\n|---|---|\n| Mid A | 6 |\n| Mean | {overall} |\n" diff --git a/bin/plan/test_blockers.py b/bin/plan/test_blockers.py index 587c9746..fb9a33ae 100644 --- a/bin/plan/test_blockers.py +++ b/bin/plan/test_blockers.py @@ -5,7 +5,7 @@ import unittest sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) -from plan.test_ranking import DECISIONS, README, TODO +from plan.doubles import DECISIONS, README, TODO from project_config.settings import DEFAULT_FLOOR from failure import Failure from plan import ranking, todo_table diff --git a/bin/plan/test_formula.py b/bin/plan/test_formula.py index 52d0f8e8..b130ecf5 100644 --- a/bin/plan/test_formula.py +++ b/bin/plan/test_formula.py @@ -4,7 +4,7 @@ import unittest sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) -from plan.test_ranking import DECISIONS, README, TODO +from plan.doubles import DECISIONS, README, TODO from project_config.settings import DEFAULT_FLOOR from failure import Failure from plan import ranking diff --git a/bin/plan/test_ranking.py b/bin/plan/test_ranking.py index 83ba4dae..d73f707b 100644 --- a/bin/plan/test_ranking.py +++ b/bin/plan/test_ranking.py @@ -9,7 +9,8 @@ import unittest sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) -from project_config.test_project_config import configured +from project_config.doubles import configured +from plan.doubles import DECISIONS, OLD_SHAPE, README, SCORED, TODO, VOID from project_config.settings import DEFAULT_FLOOR from failure import Failure from plan_store.stores import file_plan @@ -18,89 +19,6 @@ from project_config.main import located from repo_checks import ROOT from plan import todo_table -# Voids code health, so these tests weigh only the project's own goals; class CodeHealth weighs it. -VOID = "**Void:** code health, since a test project has no code to read.\n\n" - -README = "# P\n\n## Goals\n\n" + VOID + "1. **Achieve:** Fast.\n2. **Maintain:** Correct.\n 1. **Achieve:** Tested.\n\n## Audience\n\nMe.\n" - -DECISIONS = "## A choice\n\nStands until `todo.md` item 5 ends it.\n" - -TODO = """# todo - -## Scoring - -`Score = 100 * V / 2**S` - -`Bar = 2` - -`Next ID = 6` - -## Items - -| ID | Release | Blocked by | Exempt | Item | L | E | I | R | S | Moves | Score | -|---|---|---|---|---|---|---|---|---|---|---|---| -| 1 | 0.2.0 | | | **Ship the later thing.** | 1 | 1 | 1 | 1 | 1 | 1 by 9 | 0 | -| 2 | 0.1.0 | | | **Tighten the tests.** | 1 | 2 | 1 | 2 | 4 | 2.1 by 1 | 0 | -| 3 | 0.1.0 | | | **Make it fast, `a \\| b`.** | 2 | 1 | 2 | 3 | 7 | 2.1 by 8, 1 by 6 | 0 | -| 4 | 0.1.0 | | security | **Rotate the leaked key.** | 3 | 3 | 5 | 9 | 2 | 2.1 by 1 | 0 | -| 5 | 0.1.0 | | decision | **Move the writes out.** | 1 | 1 | 4 | 5 | 5 | 2.1 by 1 | 0 | - -## Details - -### 1. stale heading - -Later detail. - -### 3. Make it fast. - -Fast detail. - -## Notes - -Kept as written. -""" - -OLD_SHAPE = """# todo - -## Scoring - -`Score = -R - S/4 + 2*A + 2*G*W` - -`Bar = 9` - -`Next ID = 6` - -| Goal | W | -|---|---| -| 1 | 1.00 | -| 2 | 0.67 | -| 2.1 | 0.33 | - -## Items - -| ID | Release | Blocked by | Exempt | Item | R | S | C | A | G | Goals | Score | -|---|---|---|---|---|---|---|---|---|---|---|---| -| 1 | 0.2.0 | | | **Ship the later thing.** | 1 | 1 | 3 | 9 | 9 | 1 | 0 | -| 2 | 0.1.0 | | | **Tighten the tests.** | 2 | 4 | 3 | 5 | 5 | 2.1 | 0 | -| 3 | 0.1.0 | | | **Make it fast, `a \\| b`.** | 3 | 8 | 3 | 6 | 8 | 2.1, 1 | 0 | -| 4 | 0.1.0 | | security | **Rotate the leaked key.** | 9 | 2 | 3 | 1 | 1 | 2.1 | 0 | -| 5 | 0.1.0 | | decision | **Move the writes out.** | 5 | 5 | 3 | 4 | 5 | 2.1 | 0 | - -## Details - -### 1. stale heading - -Later detail. - -### 3. Make it fast. - -Fast detail. - -## Notes - -Kept as written. -""" - def numbered_goals(goals_text): """The goals ranked_goals numbers in a goals file, before it adds code health.""" @@ -256,10 +174,6 @@ class Rank(unittest.TestCase): self.assertIsInstance(self.ranked(TODO.replace(old, new)), Failure) -# A scoring run of the project whose overall mean is {overall}. -SCORED = "# Comprehension history\n\n## 2026-10-08T12:00:00Z, PR #1 at aaaaaaa\n\n| Seat | Overall |\n|---|---|\n| Mid A | 6 |\n| Mean | {overall} |\n" - - class CodeHealth(unittest.TestCase): readme = "## Goals\n\n1. **Achieve:** A.\n" todo = TODO.split("| 1 | 0.2.0", 1)[0].replace("`Bar = 2`", "`Bar = 0.1`") + "| 1 | 0.1.0 | | | **Read easier.** | 1 | 1 | 1 | 1 | 1 | code health by 1 | 0 |\n| 2 | 0.1.0 | | | **Do A.** | 1 | 1 | 1 | 1 | 1 | 1 by 1 | 0 |\n" diff --git a/bin/plan_store/test_stores.py b/bin/plan_store/test_stores.py index af711edb..f75c7327 100644 --- a/bin/plan_store/test_stores.py +++ b/bin/plan_store/test_stores.py @@ -17,7 +17,7 @@ from plan_store.fakes import MemoryPlan from record_store.fakes import MemoryStore from plan.todo_table import NoItemsTable from plan_store import plans, scoring, stores, todo_md -from plan.test_ranking import README, SCORED, VOID +from plan.doubles import README, SCORED, VOID from plan.release import release_of EMPTY = "# todo\n\n## Scoring\n\n`Score = 100 * V / 2**S`\n\n`Bar = 2`\n\n`Next ID = 1`\n\n## Items\n\n| ID | Release | Blocked by | Exempt | Item | L | E | I | R | S | Moves | Score |\n|---|---|---|---|---|---|---|---|---|---|---|---|\n" diff --git a/bin/project_config/doubles.py b/bin/project_config/doubles.py new file mode 100644 index 00000000..d38ab31e --- /dev/null +++ b/bin/project_config/doubles.py @@ -0,0 +1,46 @@ +"""Test doubles for the tools that read a project's config: a client's config text, and a checkout that a config names.""" +import os +import pathlib +import subprocess +import tempfile +import unittest +import unittest.mock + +from test_env import isolated_env + +CLIENT = """checkout = "~/code/acme-billing" +plan = "folder:todo.md" +working-rules = "folder:AGENTS.md" +decisions = "folder:docs/decisions.md" +score-anchors = "folder:docs/score-anchors.md" +comprehension-history = "folder:docs/comprehension-history.md" +reading-cost = "folder:docs/reading-cost.md" +changelog = "checkout:CHANGELOG.md" +goals-and-audience = "folder:goals.md" +principles = "~/.claude/principles" +standards = "~/.claude/skills/larv-review/standards" +""" + +JIRA_PLAN = "jira:https://acme.atlassian.net/browse/ABC-12" +JIRA_SCORING = 'jira-score-formula = "100 * V / (2**S + 8)"\njira-score-bar = 1\n' + + +def write_config(home: pathlib.Path, checkout: pathlib.Path, goals: str = "checkout:README.md") -> pathlib.Path: + """A config under home that names checkout, every setting inside it but goals-and-audience; for the tests of each tool that reads the config.""" + folder = home / ".claude-projects" / checkout.name + folder.mkdir(parents=True, exist_ok=True) + (folder / "config.toml").write_text(CLIENT.replace('"~/code/acme-billing"', f'"{checkout}"').replace('"folder:', '"checkout:').replace('"checkout:goals.md"', f'"{goals}"'), encoding="utf-8") + return folder + + +def configured(test: unittest.TestCase, checkout: pathlib.Path, goals: str = "checkout:README.md", home: pathlib.Path | None = None) -> pathlib.Path: + """Makes checkout a git checkout that a config names, under home, else a HOME of its own, for the rest of the test; the config's folder.""" + subprocess.run(["git", "init", "--quiet", str(checkout)], check=True, env=isolated_env()) + if home is None: + made = tempfile.TemporaryDirectory() + test.addCleanup(made.cleanup) + home = pathlib.Path(made.name) + patched = unittest.mock.patch.dict(os.environ, {"HOME": str(home)}) + patched.start() + test.addCleanup(patched.stop) + return write_config(home, checkout, goals) diff --git a/bin/project_config/test_project_config.py b/bin/project_config/test_project_config.py index 6bec16d9..00739d8e 100644 --- a/bin/project_config/test_project_config.py +++ b/bin/project_config/test_project_config.py @@ -1,5 +1,4 @@ import decimal -import os import pathlib import subprocess import sys @@ -10,6 +9,7 @@ import unittest.mock sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) from project_config.settings import by_name, env_files, Epic, Finish, held, JiraScoring, load, NOTIFIERS, OUTSIDE, Page, ProjectConfig, SETTINGS, Unnamed +from project_config.doubles import CLIENT, JIRA_PLAN, JIRA_SCORING from failure import Failure from test_env import isolated_env from project_config.main import located, title_templates @@ -17,46 +17,10 @@ from project_config.templates import Titles TOOL = pathlib.Path(__file__).resolve().parents[1] / "project-config" -CLIENT = """checkout = "~/code/acme-billing" -plan = "folder:todo.md" -working-rules = "folder:AGENTS.md" -decisions = "folder:docs/decisions.md" -score-anchors = "folder:docs/score-anchors.md" -comprehension-history = "folder:docs/comprehension-history.md" -reading-cost = "folder:docs/reading-cost.md" -changelog = "checkout:CHANGELOG.md" -goals-and-audience = "folder:goals.md" -principles = "~/.claude/principles" -standards = "~/.claude/skills/larv-review/standards" -""" - -JIRA_PLAN = "jira:https://acme.atlassian.net/browse/ABC-12" -JIRA_SCORING = 'jira-score-formula = "100 * V / (2**S + 8)"\njira-score-bar = 1\n' JIRA_CLIENT = CLIENT.replace("folder:todo.md", JIRA_PLAN) + 'env-file = "~/acme.env"\n' + JIRA_SCORING DECISIONS_PAGE = "confluence:https://acme.atlassian.net/wiki/spaces/ENG/pages/2293761/Decisions" -def write_config(home: pathlib.Path, checkout: pathlib.Path, goals: str = "checkout:README.md") -> pathlib.Path: - """A config under home that names checkout, every setting inside it but goals-and-audience; for the tests of each tool that reads the config.""" - folder = home / ".claude-projects" / checkout.name - folder.mkdir(parents=True, exist_ok=True) - (folder / "config.toml").write_text(CLIENT.replace('"~/code/acme-billing"', f'"{checkout}"').replace('"folder:', '"checkout:').replace('"checkout:goals.md"', f'"{goals}"'), encoding="utf-8") - return folder - - -def configured(test: unittest.TestCase, checkout: pathlib.Path, goals: str = "checkout:README.md", home: pathlib.Path | None = None) -> pathlib.Path: - """Makes checkout a git checkout that a config names, under home, else a HOME of its own, for the rest of the test; the config's folder.""" - subprocess.run(["git", "init", "--quiet", str(checkout)], check=True, env=isolated_env()) - if home is None: - made = tempfile.TemporaryDirectory() - test.addCleanup(made.cleanup) - home = pathlib.Path(made.name) - patched = unittest.mock.patch.dict(os.environ, {"HOME": str(home)}) - patched.start() - test.addCleanup(patched.stop) - return write_config(home, checkout, goals) - - class ConfigCase(unittest.TestCase): def setUp(self) -> None: made = tempfile.TemporaryDirectory() diff --git a/bin/project_record/test_project_record.py b/bin/project_record/test_project_record.py index 5a83f5fb..2f7a872f 100644 --- a/bin/project_record/test_project_record.py +++ b/bin/project_record/test_project_record.py @@ -7,7 +7,7 @@ import unittest sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) -from project_config.test_project_config import configured +from project_config.doubles import configured from record_store.confluence_page import ConfluencePage from failure import Failure from project_record import main diff --git a/bin/reading_cost/test_reading_cost.py b/bin/reading_cost/test_reading_cost.py index adab0ba5..101ab675 100644 --- a/bin/reading_cost/test_reading_cost.py +++ b/bin/reading_cost/test_reading_cost.py @@ -26,7 +26,7 @@ from record_store.fakes import MemoryStore from transcript_archive import outcomes from transcript_archive.location import project_archive from reading_cost.shell_reads import Reads, shell_reads -from project_config.test_project_config import write_config +from project_config.doubles import write_config TOOL = pathlib.Path(__file__).resolve().parents[1] / "reading-cost" diff --git a/bin/repo_checks/test_package_cycles.py b/bin/repo_checks/test_package_cycles.py index 51135a47..6b261ce8 100644 --- a/bin/repo_checks/test_package_cycles.py +++ b/bin/repo_checks/test_package_cycles.py @@ -57,6 +57,21 @@ def upward_imports(bin_folder: pathlib.Path, above: dict[str, tuple[str, ...]]) return sorted(found) +def imports_of_test_modules(bin_folder: pathlib.Path) -> list[str]: + """Each module under bin_folder that imports a test module of another package, as ` imports `. A module at bin_folder's root, such as test_env.py, belongs to no package.""" + found = [] + for module in sorted(bin_folder.rglob("*.py")): + package = module.relative_to(bin_folder).parts[0] + for node in ast.walk(ast.parse(module.read_text(encoding="utf-8"))): + names = [alias.name for alias in node.names] if isinstance(node, ast.Import) else [f"{node.module}.{alias.name}" for alias in node.names] if isinstance(node, ast.ImportFrom) and node.level == 0 and node.module else [] + for name in names: + top, *below = name.split(".") + tests = [part for part in below if part.startswith("test_")] + if tests and top != package: + found.append(f"{module.relative_to(bin_folder)} imports {name[:name.index(tests[0]) + len(tests[0])]}") + return sorted(set(found)) + + def cycle(imported: dict[str, set[str]]) -> list[str]: """One cycle among the packages, as the packages along it; empty where there is none.""" try: @@ -92,6 +107,24 @@ class PackageCycles(unittest.TestCase): (folder / package / name).write_text(source) self.assertEqual(driver_imports(folder, ("a",)), ["b/mod.py imports a", "b/test_mod.py imports a"]) + def test_no_module_imports_a_test_module_of_another_package(self): + self.assertEqual(imports_of_test_modules(ROOT / "bin"), [], "move what the importer needs into the doubles.py of the package that owns it") + + def test_a_test_module_imported_from_another_package_is_found_however_it_is_spelled(self): + with tempfile.TemporaryDirectory() as temp: + folder = pathlib.Path(temp) + sources = ( + ("a", "test_mod.py", "from a.test_other import x\nfrom test_env import y\nfrom a.doubles import z\n"), + ("b", "test_mod.py", "from a.test_mod import x\n"), + ("b", "mod.py", "def f():\n\timport a.deep.test_far\n"), + ("c", "test_mod.py", "from a import test_mod\nfrom a.test_mod import x, y\n"), + ) + for package, name, source in sources: + (folder / package).mkdir(exist_ok=True) + (folder / package / name).write_text(source) + (folder / "test_root.py").write_text("from a.test_mod import x\n") + self.assertEqual(imports_of_test_modules(folder), ["b/mod.py imports a.deep.test_far", "b/test_mod.py imports a.test_mod", "c/test_mod.py imports a.test_mod", "test_root.py imports a.test_mod"]) + def test_reads_an_import_deferred_into_a_function_and_leaves_out_tests(self): with tempfile.TemporaryDirectory() as temp: folder = pathlib.Path(temp) diff --git a/bin/rest_client/doubles.py b/bin/rest_client/doubles.py new file mode 100644 index 00000000..00a7500d --- /dev/null +++ b/bin/rest_client/doubles.py @@ -0,0 +1,50 @@ +"""Test doubles for the suites that send HTTP requests: a local server that answers by a script.""" +import http.server +import threading +import unittest + + +class Recorded(http.server.BaseHTTPRequestHandler): + """Answers each request with the server's next scripted status and body, recording what it got.""" + + def do_GET(self) -> None: + self.answer() + + def do_POST(self) -> None: + self.answer() + + def do_PUT(self) -> None: + self.answer() + + def answer(self) -> None: + length = int(self.headers.get("Content-Length") or 0) + self.server.got.append((self.command, self.path, dict(self.headers), self.rfile.read(length))) # type: ignore[attr-defined] + status, body = self.server.script.pop(0) # type: ignore[attr-defined] + if status is None: + # Raw bytes, as a server that breaks HTTP sends them. + self.wfile.write(body) + self.close_connection = True + return + self.send_response(status) + if 300 <= status < 400: + self.send_header("Location", "/elsewhere") + self.send_header("Content-Type", "application/json") + self.end_headers() + self.wfile.write(body) + + def log_message(self, format: str, *args: object) -> None: + pass + + +class Served(unittest.TestCase): + """A test case with a local server that answers by a script; it holds no test itself, so a test elsewhere may build on it.""" + + def serve(self, *script: tuple[int, bytes]) -> str: + server = http.server.HTTPServer(("127.0.0.1", 0), Recorded) + server.script = list(script) # type: ignore[attr-defined] + server.got = [] # type: ignore[attr-defined] + threading.Thread(target=server.serve_forever, daemon=True).start() + self.addCleanup(server.server_close) + self.addCleanup(server.shutdown) + self.server = server + return f"http://127.0.0.1:{server.server_port}" diff --git a/bin/rest_client/test_json_request.py b/bin/rest_client/test_json_request.py index 3e030eb9..52f4e917 100644 --- a/bin/rest_client/test_json_request.py +++ b/bin/rest_client/test_json_request.py @@ -1,9 +1,7 @@ -import http.server import json import pathlib import socket import sys -import threading import unittest sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) @@ -11,6 +9,7 @@ sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) from rest_client.json_request import ErrorAnswer from failure import Failure from rest_client import json_request +from rest_client.doubles import Served def message(answer: object) -> str: @@ -21,52 +20,6 @@ def send(method: str, url: str, token: str, body: object, seconds: float = 5) -> return json_request.http_json(method, url, f"Bearer {token}", body, seconds, message) # type: ignore[arg-type] -class Recorded(http.server.BaseHTTPRequestHandler): - """Answers each request with the server's next scripted status and body, recording what it got.""" - - def do_GET(self) -> None: - self.answer() - - def do_POST(self) -> None: - self.answer() - - def do_PUT(self) -> None: - self.answer() - - def answer(self) -> None: - length = int(self.headers.get("Content-Length") or 0) - self.server.got.append((self.command, self.path, dict(self.headers), self.rfile.read(length))) # type: ignore[attr-defined] - status, body = self.server.script.pop(0) # type: ignore[attr-defined] - if status is None: - # Raw bytes, as a server that breaks HTTP sends them. - self.wfile.write(body) - self.close_connection = True - return - self.send_response(status) - if 300 <= status < 400: - self.send_header("Location", "/elsewhere") - self.send_header("Content-Type", "application/json") - self.end_headers() - self.wfile.write(body) - - def log_message(self, format: str, *args: object) -> None: - pass - - -class Served(unittest.TestCase): - """A test case with a local server that answers by a script; it holds no test itself, so a test elsewhere may build on it.""" - - def serve(self, *script: tuple[int, bytes]) -> str: - server = http.server.HTTPServer(("127.0.0.1", 0), Recorded) - server.script = list(script) # type: ignore[attr-defined] - server.got = [] # type: ignore[attr-defined] - threading.Thread(target=server.serve_forever, daemon=True).start() - self.addCleanup(server.server_close) - self.addCleanup(server.shutdown) - self.server = server - return f"http://127.0.0.1:{server.server_port}" - - class HttpJson(Served): def test_sends_the_authorization_and_the_body_as_json_and_reads_the_json_answer(self): base = self.serve((201, b'{"id": "post-id"}')) diff --git a/bin/review_replay/test_review_replay.py b/bin/review_replay/test_review_replay.py index 4ea2d19d..cd6004c6 100644 --- a/bin/review_replay/test_review_replay.py +++ b/bin/review_replay/test_review_replay.py @@ -17,7 +17,7 @@ sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) from review_replay.rounds import authored_by, lost_inputs, recorded_findings, reviewed_path, Round, round_from, sample, stamp_seconds from review_replay import changes, main, recall, replay from text_files import compressed -from project_config.test_project_config import configured, write_config +from project_config.doubles import configured, write_config from loop_effects.effects import Effects, system_effects from claude_code.entries import entry_from from failure import Failure diff --git a/bin/routing_figures/test_routing_figures.py b/bin/routing_figures/test_routing_figures.py index 61803e64..c7b9e68a 100644 --- a/bin/routing_figures/test_routing_figures.py +++ b/bin/routing_figures/test_routing_figures.py @@ -21,7 +21,7 @@ from branch_chunks.cut import BranchChunk from routing_figures.calls import Call, calls, totals from routing_figures.chunk import Chunk, chunk_figures, ChunkFigures, Reports, Subagent from text_files import compressed -from project_config.test_project_config import configured, write_config +from project_config.doubles import configured, write_config from routing_figures.state import due from claude_code.entries import entry_from from failure import Failure diff --git a/bin/rule_hook/test_rule_hook.py b/bin/rule_hook/test_rule_hook.py index 38d8c1da..7e969d38 100644 --- a/bin/rule_hook/test_rule_hook.py +++ b/bin/rule_hook/test_rule_hook.py @@ -19,8 +19,8 @@ from rule_hook.rules import blocks, Breach, check, command_breaches, file_breach from rule_hook.main import breaches_of from rule_hook.call import Call, parsed_call from git_cli.git_command import commit_message, CommitCall, commits -from project_config.test_project_config import configured, write_config -from plan.test_ranking import DECISIONS, README, TODO +from project_config.doubles import configured, write_config +from plan.doubles import DECISIONS, README, TODO from rule_hook.git import guarded_git from test_env import isolated_env from rule_hook.override_log import LogLine, parsed_entry, this_chunks diff --git a/bin/score_outcomes/test_score_outcomes.py b/bin/score_outcomes/test_score_outcomes.py index a2ac7adc..fe9c8bfe 100644 --- a/bin/score_outcomes/test_score_outcomes.py +++ b/bin/score_outcomes/test_score_outcomes.py @@ -10,7 +10,7 @@ sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) from score_outcomes.anchors import anchor_lines, parse_anchors from branch_chunks.cut import BranchChunk, Landing -from project_config.test_project_config import configured, write_config +from project_config.doubles import configured, write_config from failure import Failure from score_outcomes.results import file_lines, NamedCode, nearest, rank_correlation, render, report, Result, results_of from review_rules.round_titles import Findings diff --git a/bin/subagent_rung/test_subagent_rung.py b/bin/subagent_rung/test_subagent_rung.py index 705ae10a..b7755473 100644 --- a/bin/subagent_rung/test_subagent_rung.py +++ b/bin/subagent_rung/test_subagent_rung.py @@ -9,7 +9,7 @@ sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) from test_env import isolated_env from subagent_rung import main from model_routing.ladder_file import read_ladder, SHIPPED -from project_config.test_project_config import write_config +from project_config.doubles import write_config LADDER = read_ladder(SHIPPED) TOOL = pathlib.Path(__file__).resolve().parents[1] / "subagent-rung" diff --git a/bin/test_rank_todo.py b/bin/test_rank_todo.py index a6460dda..ba56c8d8 100644 --- a/bin/test_rank_todo.py +++ b/bin/test_rank_todo.py @@ -7,12 +7,12 @@ import unittest sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent)) -from project_config.test_project_config import configured -from plan.test_ranking import DECISIONS, OLD_SHAPE, README, TODO +from project_config.doubles import configured +from plan.doubles import DECISIONS, OLD_SHAPE, README, TODO from project_config.settings import DEFAULT_FLOOR from plan import goals_file, ranking import rank_todo -from todo_item.test_todo_item import READABLE_TOUCHES, UNREADABLE_BULLETS +from todo_item.doubles import READABLE_TOUCHES, UNREADABLE_BULLETS class Main(unittest.TestCase): diff --git a/bin/todo_item/doubles.py b/bin/todo_item/doubles.py new file mode 100644 index 00000000..0131e48d --- /dev/null +++ b/bin/todo_item/doubles.py @@ -0,0 +1,13 @@ +"""Test doubles for the suites that read an item's detail: Touches bullets that touch-predictions can and cannot read.""" + +# The bullets six items held on 2026-11-18, which touch-predictions cannot read: `(small)` after a bare path, and a part not in backticks. +UNREADABLE_BULLETS = ( + "- `bin/judge_replay/main.py` (small)", + "- `bin/do_epic/main.py` (small)", + "- `principles/technical-principles.md`: the tests bullet group, `A valuable test checks behaviour` (small)", + "- `README.md`: loop-claude's section (small)", + "- `bin/rule_hook/rules.py`: the rule table (small)", + "- `settings.json`: the PreToolUse matcher (small)", +) + +READABLE_TOUCHES = "Why.\n\nTouches:\n\n- `bin/a.py`: `main`, `Plan.branch` (small)\n- `bin/run.sh`: lines 10-20\n- `docs/new.md`\n" diff --git a/bin/todo_item/test_todo_item.py b/bin/todo_item/test_todo_item.py index 0b9d2cd2..c95157ff 100644 --- a/bin/todo_item/test_todo_item.py +++ b/bin/todo_item/test_todo_item.py @@ -8,8 +8,8 @@ import unittest.mock sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent.parent)) -from project_config.test_project_config import configured, JIRA_PLAN, JIRA_SCORING -from plan.test_ranking import DECISIONS, README, TODO +from project_config.doubles import configured, JIRA_PLAN, JIRA_SCORING +from plan.doubles import DECISIONS, README, TODO from project_config.settings import DEFAULT_FLOOR from todo_item import edits, main from failure import Failure @@ -18,6 +18,7 @@ from project_config.main import located from plan_store.fakes import MemoryPlan from plan_store.todo_md import plan_of from plan import ranking +from todo_item.doubles import READABLE_TOUCHES, UNREADABLE_BULLETS from todo_item.table import table ADD = ["add", "--release", "0.1.0", "--l", "1", "--e", "1", "--i", "1", "--s", "1", "--moves", "1 by 9", "Count the `a | b` runs."] @@ -25,16 +26,6 @@ CHANGELOG = "# Changelog\n\n## Unreleased\n\n- Older news.\n\n## 0.1.0\n\n- Firs # ## Details of the ranked fixture, without the trailing ## Notes. HEADER = "| ID | Release | Blocked by | Exempt | Item | L | E | I | R | S | Moves |\n|---|---|---|---|---|---|---|---|---|---|---|\n" DETAILS = "## Details\n\n### 3. Make it fast, `a | b`.\n\nFast detail.\n\n### 1. Ship the later thing.\n\nLater detail.\n\n" -# The bullets six items held on 2026-11-18, which touch-predictions cannot read: `(small)` after a bare path, and a part not in backticks. -UNREADABLE_BULLETS = ( - "- `bin/judge_replay/main.py` (small)", - "- `bin/do_epic/main.py` (small)", - "- `principles/technical-principles.md`: the tests bullet group, `A valuable test checks behaviour` (small)", - "- `README.md`: loop-claude's section (small)", - "- `bin/rule_hook/rules.py`: the rule table (small)", - "- `settings.json`: the PreToolUse matcher (small)", -) -READABLE_TOUCHES = "Why.\n\nTouches:\n\n- `bin/a.py`: `main`, `Plan.branch` (small)\n- `bin/run.sh`: lines 10-20\n- `docs/new.md`\n" class Terminal(io.StringIO): diff --git a/bin/touch_predictions/doubles.py b/bin/touch_predictions/doubles.py new file mode 100644 index 00000000..22189131 --- /dev/null +++ b/bin/touch_predictions/doubles.py @@ -0,0 +1,12 @@ +"""Test doubles for the suites that read touch predictions: a plan whose items hold them.""" +from failure import Failure +from plan_store.todo_md import plan_of + + +def plan(*items: tuple[int, str, str, str]) -> list: + """A plan's items, each given as its ID, release, the IDs that block it and the bullets of its prediction, none for an item without one.""" + rows = "".join(f"| {id_} | {release} | {blocked} | | **Do {id_}.** | 1 | 1 | 1 | 1 | 1 | 1 by 1 | 10.0 |\n" for id_, release, blocked, _ in items) + details = "".join(f"### {id_}. Do {id_}.\n\nTouches:\n\n{bullets}\n\n" for id_, _, _, bullets in items if bullets) + found = plan_of(f"# todo\n\n## Scoring\n\n`Score = 100 * V / (2**S + 8)`\n\n`Bar = 0.1`\n\n`Next ID = 99`\n\n## Items\n\n| ID | Release | Blocked by | Exempt | Item | L | E | I | R | S | Moves | Score |\n|---|---|---|---|---|---|---|---|---|---|---|---|\n{rows}\n## Details\n\n{details}") + assert not isinstance(found, Failure), found + return found.items diff --git a/bin/touch_predictions/test_touch_predictions.py b/bin/touch_predictions/test_touch_predictions.py index e960c9a6..1d2e1659 100644 --- a/bin/touch_predictions/test_touch_predictions.py +++ b/bin/touch_predictions/test_touch_predictions.py @@ -15,9 +15,9 @@ from test_env import isolated_env from project_config.main import located from record_store.fakes import MemoryStore from plan.touches import Part -from plan_store.todo_md import plan_of +from touch_predictions.doubles import plan from project_config.settings import ProjectConfig -from project_config.test_project_config import write_config +from project_config.doubles import write_config PYTHON = """\"\"\"Docstring.\"\"\" import os @@ -132,15 +132,6 @@ class FilePartsTest(unittest.TestCase): self.assertIsNone(self.names("run.sh", "echo\n", 1)) -def plan(*items: tuple[int, str, str, str]) -> list: - """A plan's items, each given as its ID, release, the IDs that block it and the bullets of its prediction, none for an item without one.""" - rows = "".join(f"| {id_} | {release} | {blocked} | | **Do {id_}.** | 1 | 1 | 1 | 1 | 1 | 1 by 1 | 10.0 |\n" for id_, release, blocked, _ in items) - details = "".join(f"### {id_}. Do {id_}.\n\nTouches:\n\n{bullets}\n\n" for id_, _, _, bullets in items if bullets) - found = plan_of(f"# todo\n\n## Scoring\n\n`Score = 100 * V / (2**S + 8)`\n\n`Bar = 0.1`\n\n`Next ID = 99`\n\n## Items\n\n| ID | Release | Blocked by | Exempt | Item | L | E | I | R | S | Moves | Score |\n|---|---|---|---|---|---|---|---|---|---|---|---|\n{rows}\n## Details\n\n{details}") - assert not isinstance(found, Failure), found - return found.items - - class ChunksBesideTest(unittest.TestCase): def test_picks_in_the_plans_order_each_item_that_overlaps_none_before_it(self) -> None: items = plan((1, "0.2.0", "", "- `a.py`: `f`"), (2, "0.2.0", "", "- `a.py`: `g`\n- `b.py`"), (3, "0.2.0", "", "- `b.py`: `h`"), (4, "0.2.0", "", "- `a.py`: `f`"), (5, "0.2.0", "", "- `c.py`")) diff --git a/todo.md b/todo.md index 0ac0f7eb..c9701e5f 100644 --- a/todo.md +++ b/todo.md @@ -172,7 +172,7 @@ Touches: - `bin/agent_cycle/run.py`: `worker_environment` (small) - `bin/loop_claude/loop/usage.py`: `HELP` (small) - `bin/agent_cycle/test_run.py`: `Cycles` (small) -- `bin/loop_claude/test_loop_claude.py`: `HelpLimits` (small) +- `bin/loop_claude/loop/test_usage.py`: `HelpLimits` (small) ### 298. Send the judge only the decision entries and the transcript tail a verdict needs, and keep the cut only where `judge-replay` changes no verdict kind. @@ -329,7 +329,7 @@ A release's capacity is a sum of its items' cost, as the formula counts it. Each ### 165. Make this repo's tests assert the facts a prompt or help text carries in place of its sentences, and drop the tests that scan source or assert a constant. -A test audit across lilleman's projects on 2026-10-16 found 10 of 33 sampled tests in this repo pinning wording that is not the contract. Examples of these and other weak tests: many tests in `bin/loop_claude/test_loop_claude.py` pin whole sentences of the question, archive, prompt or help text, up to about 750 characters. `test_every_exit_1_stop_has_its_clause_in_help` and `test_every_failed_judge_run_has_its_clause_in_help` parse the source with `ast` and list each `Stop(...)` and `Failure(...)` call by function name. `bin/model_routing/test_ladder.py` asserts `len(LADDER) == 6`. Rewrite each to the technical principles' test bullet: assert the facts the text must carry, such as an item ID, a rung or a model argument, and delete a test no real bug could fail. Before and after rewriting a test, run the mutation tool on the code it covers: every mutant the old test killed, the new one kills. +A test audit across lilleman's projects on 2026-10-16 found 10 of 33 sampled tests in this repo pinning wording that is not the contract. Examples of these and other weak tests: many tests under `bin/loop_claude/` pin whole sentences of the question, archive, prompt or help text, up to about 750 characters. `test_every_exit_1_stop_has_its_clause_in_help` and `test_every_failed_judge_run_has_its_clause_in_help` parse the source with `ast` and list each `Stop(...)` and `Failure(...)` call by function name. `bin/model_routing/test_ladder.py` asserts `len(LADDER) == 6`. Rewrite each to the technical principles' test bullet: assert the facts the text must carry, such as an item ID, a rung or a model argument, and delete a test no real bug could fail. Before and after rewriting a test, run the mutation tool on the code it covers: every mutant the old test killed, the new one kills. ### 392. Group bin/'s packages by concern into seven folders (plan, records, integrations, loop, review, forge and host), so a newcomer finds a tool's neighbours in one place.