diff --git a/bin/score_outcomes/anchors.py b/bin/score_outcomes/anchors.py index 88f5ad16..6fadc749 100644 --- a/bin/score_outcomes/anchors.py +++ b/bin/score_outcomes/anchors.py @@ -1,36 +1,38 @@ import re +from todo_table import cells, LEVELS, parse_ids from failure import Failure -from todo_table import LEVELS from score_outcomes.outcomes import Outcome, render # Where a project names the shipped items that show each level of L, E and I. -ANCHORS = "docs/score-anchors.md" -# One row of its table: a level, such as `L 1`, and the IDs of the items that show it. -ROW = re.compile(r"\|\s*([A-Z]) ([0-9]+)\s*\|\s*([0-9]+(?:\s*,\s*[0-9]+)*)\s*\|") -# The table's header and separator rows. -FRAME = re.compile(r"\|\s*Level\s*\|\s*Items\s*\||\|[-|\s]+\|") +ANCHORS_PATH = "docs/score-anchors.md" +# A level, such as `L 1`. +LEVEL = re.compile(r"([A-Z]) ([0-9]+)") -def anchors(text: str) -> list[tuple[str, tuple[int, ...]]] | Failure: +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, ...]]] = [] for row in (line.strip() for line in text.splitlines() if line.strip().startswith("|")): - if FRAME.fullmatch(row): + pair = cells(row, 2) + if isinstance(pair, Failure): + return Failure(f"{ANCHORS_PATH}: {pair.reason}") + if pair == ["Level", "Items"] or all(re.fullmatch(r":?-+:?", cell) for cell in pair): continue - matched = ROW.fullmatch(row) - if not matched or not 1 <= int(matched[2]) <= LEVELS.get(matched[1], 0): + 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: scales = ", ".join(f"{name} 1 to {top}" for name, top in LEVELS.items()) - return Failure(f"{ANCHORS} cannot read the row {row}; each row names a level, {scales}, and the IDs of shipped items, such as | L 1 | 12, 14 |") - level = f"{matched[1]} {matched[2]}" - if level in dict(found): - return Failure(f"{ANCHORS} names {level} twice") - found.append((level, tuple(int(id_) for id_ in matched[3].split(",")))) + 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): + return Failure(f"{ANCHORS_PATH} names {pair[0]} twice") + found.append((pair[0], ids)) 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.""" + """Each level, then each of its anchors with what its chunk measured; where the project names no level, where to name them.""" + 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} lines = [] for level, ids in found: diff --git a/bin/score_outcomes/main.py b/bin/score_outcomes/main.py index 9c404e8d..291fd517 100644 --- a/bin/score_outcomes/main.py +++ b/bin/score_outcomes/main.py @@ -48,7 +48,7 @@ import pathlib import re import sys -from score_outcomes.anchors import anchor_lines, ANCHORS, anchors +from score_outcomes.anchors import anchor_lines, 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 @@ -80,20 +80,23 @@ def main(argv: list[str]) -> int: anchored = read_anchors() if isinstance(anchored, Failure): return failed(anchored) - if argv: - given = dict(zip(GIVEN, map(int, argv))) - given["R"] = risk(given["L"], given["E"], given["I"]) - print(f"Finished items nearest L {given['L']}, E {given['E']}, I {given['I']}, so R {given['R']}, and S {given['S']}, closest first:\n") - print("\n".join(render(outcome) for outcome in nearest(outcomes, given)) 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.") - print() - print(f"Anchors in {ANCHORS}, each level's shipped items:\n") - print("\n".join(anchor_lines(anchored, outcomes)) or f"- None: the project's {ANCHORS} names no level; ~/.claude/rules/todo-items.md says how to choose them.") - print() - print("Median outcome at each guessed score, among chunks that shipped one item:\n") - print("\n".join(report(outcomes))) + given = dict(zip(GIVEN, map(int, argv))) + print(report_text(outcomes, anchored, given)) return 0 +def report_text(outcomes: list[Outcome], anchored: list[tuple[str, tuple[int, ...]]], given: dict[str, int]) -> str: + """The report: the finished items nearest the given scores, where any are given, then the anchors, then the medians.""" + sections = [] + if given: + 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]) + sections.append([f"Anchors in {ANCHORS_PATH}, each level's shipped items:", "", *anchor_lines(anchored, outcomes)]) + sections.append(["Median outcome at each guessed score, among chunks that shipped one item:", "", *report(outcomes)]) + return "\n\n".join("\n".join(section) for section in sections) + + def failed(failure: Failure) -> int: print(f"score-outcomes: {failure.reason}", file=sys.stderr) return 1 @@ -105,11 +108,11 @@ def read_anchors() -> list[tuple[str, tuple[int, ...]]] | Failure: if isinstance(root, Failure): return root try: - return anchors((pathlib.Path(root) / ANCHORS).read_text()) + return parse_anchors((pathlib.Path(root) / ANCHORS_PATH).read_text()) except FileNotFoundError: return [] except (OSError, UnicodeDecodeError) as failure: - return Failure(f"cannot read {ANCHORS}: {failure}") + return Failure(f"cannot read {ANCHORS_PATH}: {failure}") def read_outcomes(default: str) -> list[Outcome] | Failure: diff --git a/bin/score_outcomes/test_score_outcomes.py b/bin/score_outcomes/test_score_outcomes.py index 56e027d2..1312ebf2 100644 --- a/bin/score_outcomes/test_score_outcomes.py +++ b/bin/score_outcomes/test_score_outcomes.py @@ -7,7 +7,7 @@ import unittest sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) -from score_outcomes.anchors import anchor_lines, anchors +from score_outcomes.anchors import anchor_lines, parse_anchors from failure import Failure from test_env import isolated_env from todo_table import Item @@ -121,20 +121,21 @@ class Render(unittest.TestCase): self.assertIn("1 round past each pass's first, 0 🔴, 0 🟡; merged 2026-10-13T10:00:00Z in aaaaaaa, with item 5. ", render(outcome(4, 2, 3, 4, rounds=1, shipped_with=(5,)))) -ANCHORS = "# Score anchors\n\nWhy these.\n\n| Level | Items |\n|---|---|\n| L 1 | 4, 9 |\n| I 3 | 5 |\n" +ANCHORS_TEXT = "# Score anchors\n\nWhy these.\n\n| Level | Items |\n|---|---|\n| L 1 | 4, 9 |\n| I 3 | 5 |\n" class Anchors(unittest.TestCase): def test_reads_each_levels_items_in_the_files_order(self): - self.assertEqual(anchors(ANCHORS), [("L 1", (4, 9)), ("I 3", (5,))]) + 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 | |"): + 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 |"): with self.subTest(row): - self.assertIsInstance(anchors(ANCHORS + row + "\n"), Failure) + self.assertIsInstance(parse_anchors(ANCHORS_TEXT + row + "\n"), Failure) 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."]) @@ -180,20 +181,15 @@ class ScoreOutcomesTool(unittest.TestCase): self.git("switch", "-q", "main") self.git("merge", "-q", "--no-ff", "-m", "Merge chunk", "chunk") (self.checkout / "docs").mkdir() - (self.checkout / "docs" / "score-anchors.md").write_text(ANCHORS) + (self.checkout / "docs" / "score-anchors.md").write_text(ANCHORS_TEXT) ran = self.run_tool("1", "2", "1", "3") self.assertEqual(ran.returncode, 0, ran.stderr) self.assertIn("Anchors in docs/score-anchors.md, each level's shipped items:\n\nL 1:\n- Item 4, R 2, S 3, C 10: 1 line in 1 file", ran.stdout) self.assertIn("I 3:\n- Item 5: no merge commit on the default branch shipped it.", ran.stdout) - def test_says_where_anchors_go_when_the_project_has_none(self): - ran = self.run_tool() - self.assertEqual(ran.returncode, 0, ran.stderr) - self.assertIn("- None: the project's docs/score-anchors.md names no level; ~/.claude/rules/todo-items.md says how to choose them.", ran.stdout) - def test_refuses_anchors_it_cannot_read(self): (self.checkout / "docs").mkdir() - (self.checkout / "docs" / "score-anchors.md").write_text(ANCHORS + "| L 9 | 4 |\n") + (self.checkout / "docs" / "score-anchors.md").write_text(ANCHORS_TEXT + "| L 9 | 4 |\n") ran = self.run_tool() self.assertEqual(ran.returncode, 1) self.assertIn("docs/score-anchors.md", ran.stderr) diff --git a/bin/todo_table.py b/bin/todo_table.py index a8d07e08..60a4b0c5 100644 --- a/bin/todo_table.py +++ b/bin/todo_table.py @@ -230,10 +230,17 @@ def parse_scores(id_: str, named: dict[str, str], names: tuple[str, ...]) -> dic return scores -def parse_blockers(id_: str, cell: str) -> tuple[int, ...] | Failure: +def parse_ids(cell: str) -> tuple[int, ...] | None: + """The item IDs a cell lists, such as `12, 14`, none where it is empty; None where it holds anything else.""" if not re.fullmatch(r"([0-9]+(, [0-9]+)*)?", cell): + return None + return tuple(int(id_) for id_ in cell.split(", ") if id_) + + +def parse_blockers(id_: str, cell: str) -> tuple[int, ...] | Failure: + blocked_by = parse_ids(cell) + if blocked_by is None: return Failure(f"item {id_}'s {BLOCKED_BY} {cell!r} is not empty or IDs separated by \", \"") - blocked_by = tuple(int(blocker) for blocker in cell.split(", ") if blocker) if int(id_) in blocked_by: return Failure(f"item {id_} is blocked by itself; drop {id_} from its {BLOCKED_BY}") return blocked_by