diff --git a/bin/score_outcomes/anchors.py b/bin/score_outcomes/anchors.py index 6fadc749..df2ef97b 100644 --- a/bin/score_outcomes/anchors.py +++ b/bin/score_outcomes/anchors.py @@ -6,13 +6,14 @@ from score_outcomes.outcomes import Outcome, render # Where a project names the shipped items that show each level of L, E and I. ANCHORS_PATH = "docs/score-anchors.md" -# A level, such as `L 1`. -LEVEL = re.compile(r"([A-Z]) ([0-9]+)") +# A level, such as `L 1`; every scale tops out below 10. +LEVEL = re.compile(r"([A-Z]) ([1-9])") +# Each level the anchors table names, such as `L 1`, with its items' IDs, in the file's order. +Anchors = list[tuple[str, tuple[int, ...]]] -def parse_anchors(text: str) -> list[tuple[str, tuple[int, ...]]] | Failure: - """Each level the anchors table names, such as `L 1`, with its items' IDs, in the file's order.""" - found: list[tuple[str, tuple[int, ...]]] = [] +def parse_anchors(text: str) -> Anchors | Failure: + found: Anchors = [] for row in (line.strip() for line in text.splitlines() if line.strip().startswith("|")): pair = cells(row, 2) if isinstance(pair, Failure): @@ -20,7 +21,7 @@ def parse_anchors(text: str) -> list[tuple[str, tuple[int, ...]]] | Failure: if pair == ["Level", "Items"] or all(re.fullmatch(r":?-+:?", cell) for cell in pair): continue level, ids = LEVEL.fullmatch(pair[0]), parse_ids(pair[1]) - if not level or not 1 <= int(level[2]) <= LEVELS.get(level[1], 0) or not ids: + if not level or int(level[2]) > LEVELS.get(level[1], 0) or not ids: scales = ", ".join(f"{name} 1 to {top}" for name, top in LEVELS.items()) return Failure(f"{ANCHORS_PATH} cannot read the row {row}; each row names a level, {scales}, and the IDs of shipped items, such as | L 1 | 12, 14 |") if pair[0] in dict(found): @@ -29,8 +30,10 @@ def parse_anchors(text: str) -> list[tuple[str, tuple[int, ...]]] | Failure: return found -def anchor_lines(found: list[tuple[str, tuple[int, ...]]], outcomes: list[Outcome]) -> list[str]: - """Each level, then each of its anchors with what its chunk measured; where the project names no level, where to name them.""" +def anchor_lines(found: Anchors | Failure, outcomes: list[Outcome]) -> list[str]: + """Each level, then each of its anchors with what its chunk measured; why none shows where the file cannot be read, and where to name them where it names no level.""" + if isinstance(found, Failure): + return [f"- None: {found.reason}"] if not found: return [f"- None: the project's {ANCHORS_PATH} names no level; ~/.claude/rules/todo-items.md says how to choose them."] by_id = {outcome.item.id: outcome for outcome in outcomes} diff --git a/bin/score_outcomes/main.py b/bin/score_outcomes/main.py index 291fd517..7a046752 100644 --- a/bin/score_outcomes/main.py +++ b/bin/score_outcomes/main.py @@ -30,9 +30,9 @@ scored before L, E and I, the newest first among equals. Each line gives what th measured, which items shipped with it, and its tagline. Then the project's anchors, the shipped items its docs/score-anchors.md names for each level of L, -E and I, read from the checkout's working tree, each line as above, or that no merge shipped it. Exit 1 -on a row that names no level on its scale, a level named twice, or an Items cell that is not a list -of IDs. +E and I, read from the checkout's working tree, each line as above, or that no merge shipped it. A +row that names no level on its scale, a level named twice, or an Items cell that is not a list of +IDs leaves the anchors out, saying why, and exits 1 after the rest of the report. Then the median of the outcome R and S each predict at each value they were guessed at: R against 🔴 findings, S against lines changed. And how well L, E and I each order chunks by review rounds @@ -48,7 +48,7 @@ import pathlib import re import sys -from score_outcomes.anchors import anchor_lines, ANCHORS_PATH, parse_anchors +from score_outcomes.anchors import anchor_lines, Anchors, ANCHORS_PATH, parse_anchors from git_repo import default_branch, file_at, full_history, git, top from failure import Failure from todo_table import LEVELS, risk, TOP_SCORE @@ -78,17 +78,15 @@ def main(argv: list[str]) -> int: if isinstance(outcomes, Failure): return failed(outcomes) anchored = read_anchors() - if isinstance(anchored, Failure): - return failed(anchored) - given = dict(zip(GIVEN, map(int, argv))) - print(report_text(outcomes, anchored, given)) - return 0 + print(report_text(outcomes, anchored, dict(zip(GIVEN, map(int, argv))) if argv else None)) + # Anchors the tool cannot read leave the rest of the report standing, and still fail the run. + return failed(anchored) if isinstance(anchored, Failure) else 0 -def report_text(outcomes: list[Outcome], anchored: list[tuple[str, tuple[int, ...]]], given: dict[str, int]) -> str: +def report_text(outcomes: list[Outcome], anchored: Anchors | Failure, given: dict[str, int] | None) -> str: """The report: the finished items nearest the given scores, where any are given, then the anchors, then the medians.""" sections = [] - if given: + if given is not None: r = risk(given["L"], given["E"], given["I"]) near = [render(outcome) for outcome in nearest(outcomes, given | {"R": r})] or ["- None: no merge commit on the default branch shipped an item from todo.md. A project that merges by squash or fast-forward has none."] sections.append([f"Finished items nearest L {given['L']}, E {given['E']}, I {given['I']}, so R {r}, and S {given['S']}, closest first:", "", *near]) @@ -102,7 +100,7 @@ def failed(failure: Failure) -> int: return 1 -def read_anchors() -> list[tuple[str, tuple[int, ...]]] | Failure: +def read_anchors() -> Anchors | Failure: """The checkout's anchors as its working tree holds them, so a branch that edits them shows its edit; none where it has no such file.""" root = top() if isinstance(root, Failure): diff --git a/bin/score_outcomes/test_score_outcomes.py b/bin/score_outcomes/test_score_outcomes.py index 1312ebf2..8c0f4eed 100644 --- a/bin/score_outcomes/test_score_outcomes.py +++ b/bin/score_outcomes/test_score_outcomes.py @@ -12,6 +12,7 @@ from failure import Failure from test_env import isolated_env from todo_table import Item from score_outcomes.outcomes import nearest, Outcome, outcomes_of, rank_correlation, render, report, shipped +from score_outcomes.main import report_text TOOL = pathlib.Path(__file__).resolve().parents[1] / "score-outcomes" HEADER = "| ID | Release | Exempt | Item | R | S | C | A | G | Goals | Score |\n|---|---|---|---|---|---|---|---|---|---|---|\n" @@ -128,16 +129,57 @@ class Anchors(unittest.TestCase): def test_reads_each_levels_items_in_the_files_order(self): self.assertEqual(parse_anchors(ANCHORS_TEXT), [("L 1", (4, 9)), ("I 3", (5,))]) - def test_refuses_a_level_off_its_scale_a_score_without_levels_a_repeated_level_or_an_unreadable_row(self): - for row in ("| L 5 | 4 |", "| E 0 | 4 |", "| S 3 | 4 |", "| L 1 | 7 |", "| L 2 | four |", "| L 2 | |", "| L 2 | 4,5 |", "| L 2 | 4 | 5 |"): + def test_reads_each_level_at_the_top_of_its_scale(self): + self.assertEqual(parse_anchors(ANCHORS_TEXT + "| L 4 | 4 |\n| E 3 | 4 |\n| I 5 | 4 |\n")[2:], [("L 4", (4,)), ("E 3", (4,)), ("I 5", (4,))]) + + def test_refuses_a_level_off_its_scale_a_score_without_levels_or_an_unreadable_row_naming_the_row(self): + for row in ("| L 5 | 4 |", "| E 0 | 4 |", "| S 1 | 4 |", "| L 01 | 4 |", "| L 2 | four |", "| L 2 | |", "| L 2 | 4,5 |", "| L 2 | 4 | 5 |"): with self.subTest(row): - self.assertIsInstance(parse_anchors(ANCHORS_TEXT + row + "\n"), Failure) + failure = parse_anchors(ANCHORS_TEXT + row + "\n") + self.assertIsInstance(failure, Failure) + self.assertIn("docs/score-anchors.md", failure.reason) + self.assertIn(row, failure.reason) + + def test_refuses_a_repeated_level_naming_it(self): + self.assertEqual(parse_anchors(ANCHORS_TEXT + "| L 1 | 7 |\n"), Failure("docs/score-anchors.md names L 1 twice")) def test_lists_each_anchor_with_what_its_chunk_measured_and_says_which_it_cannot_find(self): lines = anchor_lines([("L 1", (4, 9))], [outcome(4, 2, 3, None, findings=(1, 2))]) - self.assertEqual(anchor_lines([], []), ["- None: the project's docs/score-anchors.md names no level; ~/.claude/rules/todo-items.md says how to choose them."]) self.assertEqual(lines, ["L 1:", "- Item 4, R 2, S 3: 10 lines in 1 file, 1 round past each pass's first, 1 🔴, 2 🟡; merged 2026-10-13T10:00:00Z in aaaaaaa. **Item 4.**", "- Item 9: no merge commit on the default branch shipped it."]) + def test_points_to_the_rules_where_the_project_names_no_anchors(self): + self.assertIn("docs/score-anchors.md", anchor_lines([], [])[0]) + self.assertIn("~/.claude/rules/todo-items.md", anchor_lines([], [])[0]) + + def test_says_why_it_shows_no_anchors_it_cannot_read(self): + self.assertEqual(anchor_lines(Failure("docs/score-anchors.md names L 1 twice"), []), ["- None: docs/score-anchors.md names L 1 twice"]) + + +class ReportText(unittest.TestCase): + def test_lists_the_nearest_by_r_then_the_anchors_then_the_medians(self): + found = [outcome(1, 5, 3, None, merged="2026-10-13T10:00:00Z"), outcome(2, 1, 3, None, merged="2026-10-12T10:00:00Z")] + too_few = "chunks that shipped one item record" + self.assertEqual(report_text(found, [("L 1", (2,))], {"L": 1, "E": 1, "I": 1, "S": 3}), f"""Finished items nearest L 1, E 1, I 1, so R 1, and S 3, closest first: + +- Item 2, R 1, S 3: 10 lines in 1 file, 1 round past each pass's first, 0 🔴, 0 🟡; merged 2026-10-12T10:00:00Z in aaaaaaa. **Item 2.** +- Item 1, R 5, S 3: 10 lines in 1 file, 1 round past each pass's first, 0 🔴, 0 🟡; merged 2026-10-13T10:00:00Z in aaaaaaa. **Item 1.** + +Anchors in docs/score-anchors.md, each level's shipped items: + +L 1: +- Item 2, R 1, S 3: 10 lines in 1 file, 1 round past each pass's first, 0 🔴, 0 🟡; merged 2026-10-12T10:00:00Z in aaaaaaa. **Item 2.** + +Median outcome at each guessed score, among chunks that shipped one item: + +- R, 🔴 findings: 2 {too_few} R and its outcome; the report needs 10. +- S, lines changed: 2 {too_few} S and its outcome; the report needs 10. +- L, by review rounds past each pass's first and 🔴 findings: 0 {too_few} L and both outcomes; the report needs 10. +- E, by review rounds past each pass's first and 🔴 findings: 0 {too_few} E and both outcomes; the report needs 10. +- I, by review rounds past each pass's first and 🔴 findings: 0 {too_few} I and both outcomes; the report needs 10.""") + + def test_lists_no_nearest_items_where_no_scores_are_given(self): + self.assertTrue(report_text([], [], None).startswith("Anchors in docs/score-anchors.md")) + class ScoreOutcomesTool(unittest.TestCase): def setUp(self): @@ -193,6 +235,7 @@ class ScoreOutcomesTool(unittest.TestCase): ran = self.run_tool() self.assertEqual(ran.returncode, 1) self.assertIn("docs/score-anchors.md", ran.stderr) + self.assertIn("Median outcome at each guessed score", ran.stdout) def test_takes_each_score_at_the_top_of_its_scale(self): ran = self.run_tool("4", "3", "5", "10")