Answer architecture round 1: 3 🔴, 7 🟡; read the items table by column name, let an item wait while its blocker is held in any order, fill this plan's blockers, file the judge's bundled rung as item 153
This commit is contained in:
@@ -34,7 +34,7 @@ def said(overall: str, locality: str = "same") -> str:
|
||||
|
||||
|
||||
def item(id_: int, release: tuple[int, int, int], tagline: str = "**Do it.**") -> Item:
|
||||
return Item(id_, release, "", tagline, {"R": 1, "S": 1, "C": 3, "A": 1, "G": 1}, ("1",))
|
||||
return Item(id_, release, "", tagline, {"R": 1, "S": 1, "C": 3, "A": 1, "G": 1}, ("1",), ())
|
||||
|
||||
|
||||
def todo(*rows: str) -> str:
|
||||
|
||||
@@ -43,8 +43,8 @@ for the worker and the interrupt; with both set, nothing is asked.
|
||||
|
||||
Where that model is on the routing ladder (bin/model_routing/ladder.py), the worker and the
|
||||
interrupt run on one of the ladder's rungs: a model at an effort. As a chunk starts, the R + C of
|
||||
todo.md's top item in the next release that waits on nothing picks the rung: an item waits when
|
||||
marked question, or when an item that waits blocks it. It
|
||||
todo.md's top item in the next release that does not wait picks the rung: an item waits while it is
|
||||
marked question, or while an item that blocks it is still in the table. It
|
||||
counts from the model's cheapest rung, and an R + C of 8 or less picks a rung below it; without
|
||||
such an item, or with a C missing, the chunk runs on the model's cheapest rung. The worker's first
|
||||
prompt names the rung and the item, and asks the worker to name each item the chunk takes, with its
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
"""Which ladder rung a chunk's worker and judge run on: docs/decisions.md → `routing:`."""
|
||||
import dataclasses
|
||||
|
||||
from model_routing.ladder import climbed, floored, highest, LADDER, picked, position, project_rung, Rung, stepped_up
|
||||
from model_routing.ladder import climbed, highest, LADDER, picked, position, project_rung, Rung, stepped_up
|
||||
from failure import Failure
|
||||
from todo_table import Item, items
|
||||
from todo_table import Item, items, waiting
|
||||
from model_routing.route import Route
|
||||
|
||||
UNFINISHED_PER_STEP = 2
|
||||
@@ -14,21 +14,14 @@ JUDGE_REPLAYED = ("claude-opus-5-5",)
|
||||
|
||||
|
||||
def routed_item(todo: str) -> Item | Failure | None:
|
||||
"""The next release's first row that waits on nothing, in the table's order, which rank_todo.py keeps with each blocker above the items it blocks; None when there is none, or it has no C.
|
||||
|
||||
An item waits on a question it is marked with, or on a blocker that waits.
|
||||
"""
|
||||
"""The next release's first row that waits on nothing, as todo_table.waiting says, in the table's order; None when there is none, or it has no C."""
|
||||
found = items(todo)
|
||||
if isinstance(found, Failure) or not found:
|
||||
return found if isinstance(found, Failure) else None
|
||||
next_release = min(item.release for item in found)
|
||||
waiting: set[int] = set()
|
||||
for item in found:
|
||||
if item.exempt == "question" or waiting.intersection(item.blocked_by):
|
||||
waiting.add(item.id)
|
||||
elif item.release == next_release:
|
||||
return item if item.risk_and_complexity else None
|
||||
return None
|
||||
waits = waiting(found)
|
||||
takes = next((item for item in found if item.release == next_release and item.id not in waits), None)
|
||||
return takes if takes and takes.risk_and_complexity else None
|
||||
|
||||
|
||||
def first_route(anchor: Rung, item: Item | None) -> Route:
|
||||
@@ -89,13 +82,13 @@ def taken_in(anchor: Rung, route: Route, bundled: int) -> Rung:
|
||||
|
||||
|
||||
def judge_rung(judge_model: str, route: Route) -> Rung:
|
||||
"""The rung the route's item picks from the judge model's cheapest rung, never below that rung, whatever rung the worker stepped to.
|
||||
"""item_rung counted from the judge model's cheapest rung, never below that rung, whatever rung the worker stepped to.
|
||||
|
||||
An item the worker takes in besides moves the worker's rung but not the judge's.
|
||||
An item the judge reports bundled lifts the worker, through taken_in, but not the judge, until todo.md item 153 has the judge follow it.
|
||||
|
||||
A model off the ladder, or not in JUDGE_REPLAYED, gets no effort flag, so claude runs it at the effort its settings give.
|
||||
"""
|
||||
anchor = project_rung(judge_model)
|
||||
if anchor is None or judge_model not in JUDGE_REPLAYED:
|
||||
return Rung(judge_model)
|
||||
return floored(anchor, route.risk_and_complexity) if route.item else anchor
|
||||
return highest(anchor, item_rung(anchor, route))
|
||||
|
||||
@@ -512,6 +512,8 @@ class Routing(unittest.TestCase):
|
||||
todo = "## Items\n\n| ID | Release | Blocked by | Exempt | Item | R | S | C | A | G | Goals | Score |\n|---|---|---|---|---|---|---|---|---|---|---|---|\n| 7 | 0.2.0 | | question | **Ask first.** | 9 | 1 | 9 | 9 | 9 | 1 | 30 |\n| 8 | 0.2.0 | 7 | | **Use the answer.** | 1 | 1 | 1 | 9 | 9 | 1 | 30 |\n| 9 | 0.2.0 | 8 | | **Build on it.** | 1 | 1 | 1 | 9 | 9 | 1 | 30 |\n| 3 | 0.2.0 | | | **Do the next thing.** | 5 | 3 | 6 | 8 | 9 | 1 | 25 |\n"
|
||||
self.assertEqual(routing.routed_item(todo).id, 3)
|
||||
self.assertEqual(routing.routed_item(todo.replace("| question |", "| |")).id, 7)
|
||||
unranked = todo.replace("| 7 | 0.2.0 | | question | **Ask first.** | 9 | 1 | 9 | 9 | 9 | 1 | 30 |\n", "") + "| 7 | 0.2.0 | | question | **Ask first.** | 9 | 1 | 9 | 9 | 9 | 1 | 30 |\n"
|
||||
self.assertEqual(routing.routed_item(unranked).id, 3)
|
||||
|
||||
def test_starts_a_chunk_on_the_rung_its_items_r_plus_c_picks_below_the_projects_too(self):
|
||||
self.assertEqual(routing.first_route(self.OPUS, routing.routed_item(TODO)), Route(self.HIGH, 3, 11))
|
||||
|
||||
+63
-43
@@ -53,6 +53,8 @@ A valid todo.md; ## Details and each item's detail are optional:
|
||||
loop. An ID at or above Next ID fails; one below it that the table no longer holds was finished
|
||||
or deleted, and is dropped.
|
||||
- ## Items holds the table and nothing else: an order goes in the scores or the Blocked by column.
|
||||
- The table holds the columns above, a table from before Blocked by or C joined read by column name;
|
||||
a column this version does not know fails, rather than be dropped.
|
||||
|
||||
Exit 1, saying why, on bad arguments, a file that cannot be read or written, a todo.md, README.md or
|
||||
docs/decisions.md that breaks the rules above, an item that is not exempt scoring under the bar, or
|
||||
@@ -65,7 +67,7 @@ import pathlib
|
||||
import re
|
||||
import sys
|
||||
|
||||
from todo_table import BLOCKED_BY, cells, Item, ITEMS_HEADER, parse_row, table_rows, WITHOUT_BLOCKERS, WITHOUT_C
|
||||
from todo_table import BLOCKED_BY, cells, COLUMNS, DIVIDER, HEADER_START, Item, ITEMS_HEADER, items_table, parse_row, Table, table_rows
|
||||
from failure import Failure
|
||||
from markdown_text import FENCE, sections
|
||||
from file_writes import replace_file
|
||||
@@ -173,9 +175,24 @@ def rank(text: str, readme: str, decisions: str) -> Ranked | Failure:
|
||||
table = read_items(lines, scoring)
|
||||
if isinstance(table, Failure):
|
||||
return table
|
||||
items, start = table
|
||||
items, found = table
|
||||
if uncited := check_citations(items, decisions):
|
||||
return uncited
|
||||
scored = ordered(items, scoring)
|
||||
if isinstance(scored, Failure):
|
||||
return scored
|
||||
# Every table gets the header of COLUMNS, so one from before BLOCKED_BY joined gains the column.
|
||||
lines[found.header:found.start] = [ITEMS_HEADER, DIVIDER]
|
||||
lines[found.start:found.start + len(scored)] = [render_item(item, value) for item, value in scored]
|
||||
ranked = rank_details("\n".join(lines), [item for item, _ in scored])
|
||||
if isinstance(ranked, Failure):
|
||||
return ranked
|
||||
under_bar = [under_the_bar(item, value, scoring.bar, items) for item, value in scored if not item.exempt and round(value, 1) < scoring.bar]
|
||||
return Ranked(ranked, under_bar)
|
||||
|
||||
|
||||
def ordered(items: list[Item], scoring: Scoring) -> list[tuple[Item, float]] | Failure:
|
||||
"""Each item with its score, by release, then security first, then by score from highest, then by ID; then each blocker just above the first item it blocks."""
|
||||
by_score = []
|
||||
for item in items:
|
||||
value = score(scoring.formula, item, scoring.weights)
|
||||
@@ -183,25 +200,18 @@ def rank(text: str, readme: str, decisions: str) -> Ranked | Failure:
|
||||
return value
|
||||
by_score.append((item, value))
|
||||
by_score.sort(key=lambda pair: (pair[0].release, pair[0].exempt != "security", -pair[1], pair[0].id))
|
||||
scored = blockers_first(by_score, scoring.next_id)
|
||||
if isinstance(scored, Failure):
|
||||
return scored
|
||||
# A table without BLOCKED_BY gets it here.
|
||||
lines[start - 2:start] = [ITEMS_HEADER, "|" + "---|" * ITEMS_HEADER.count(" | ") + "---|"]
|
||||
lines[start:start + len(scored)] = [render_item(item, value) for item, value in scored]
|
||||
ranked = rank_details("\n".join(lines), [item for item, _ in scored])
|
||||
if isinstance(ranked, Failure):
|
||||
return ranked
|
||||
under_bar = [f"item {item.id} scores {value:.1f}, under the bar {scoring.bar:g}; delete it and any item that needs it, or mark it with a reason the bar does not apply" for item, value in scored if not item.exempt and round(value, 1) < scoring.bar]
|
||||
return Ranked(ranked, under_bar)
|
||||
return blockers_first(by_score)
|
||||
|
||||
|
||||
def under_the_bar(item: Item, value: float, bar: float, items: list[Item]) -> str:
|
||||
blocks = [str(other.id) for other in items if item.id in other.blocked_by]
|
||||
blocked = f" and the items it blocks, {', '.join(blocks)}" if blocks else ""
|
||||
return f"item {item.id} scores {value:.1f}, under the bar {bar:g}; delete it{blocked}, or mark it with a reason the bar does not apply"
|
||||
|
||||
|
||||
def read_scoring(text: str, lines: list[str], readme: str) -> Scoring | Failure:
|
||||
if ITEMS_HEADER not in lines and WITHOUT_BLOCKERS not in lines and "## Items" not in lines:
|
||||
if "## Items" not in lines and items_table(lines) is None:
|
||||
return Failure("todo.md is not in the scored-table format; rank_todo.py --help shows a valid one")
|
||||
# A table without C fails, so no chunk starts on an unscored item.
|
||||
if WITHOUT_C in lines:
|
||||
return Failure("todo.md's items table has no C column; add one after S, and score each item's C from 1 to 10 per rank_todo.py --help")
|
||||
goals = readme_goals(readme)
|
||||
if isinstance(goals, Failure):
|
||||
return goals
|
||||
@@ -220,17 +230,24 @@ def read_scoring(text: str, lines: list[str], readme: str) -> Scoring | Failure:
|
||||
return Scoring(formula, bar, int(next_id), weights)
|
||||
|
||||
|
||||
def read_items(lines: list[str], scoring: Scoring) -> tuple[list[Item], int] | Failure:
|
||||
"""The items, and the index of the first row in lines."""
|
||||
header = WITHOUT_BLOCKERS if WITHOUT_BLOCKERS in lines and ITEMS_HEADER not in lines else ITEMS_HEADER
|
||||
table = table_rows(lines, header)
|
||||
def read_items(lines: list[str], scoring: Scoring) -> tuple[list[Item], Table] | Failure:
|
||||
"""The items, and the table they were read from."""
|
||||
table = items_table(lines)
|
||||
if table is None:
|
||||
return Failure(f"## Items holds no table whose header opens {HEADER_START}; rank_todo.py --help shows a valid one")
|
||||
if isinstance(table, Failure):
|
||||
return table
|
||||
if beside_table(lines, table.start - 2, table.start + len(table.rows)):
|
||||
# A table without C fails, so no chunk starts on an unscored item.
|
||||
if "C" not in table.columns:
|
||||
return Failure("todo.md's items table has no C column; add one after S, and score each item's C from 1 to 10 per rank_todo.py --help")
|
||||
# Ranking writes COLUMNS, so it would drop a column it does not know.
|
||||
if unknown := [column for column in table.columns if column not in COLUMNS]:
|
||||
return Failure(f"todo.md's items table has the column {', '.join(unknown)}, which this rank_todo.py does not know; move this machine to the release that added it")
|
||||
if beside_table(lines, table.header, table.start + len(table.rows)):
|
||||
return Failure(f"## Items holds text beside its table; an order goes in the scores or the {BLOCKED_BY} column, and a note in the item's detail")
|
||||
items = []
|
||||
for row in table.rows:
|
||||
item = parse_item(row, scoring.weights, header)
|
||||
item = parse_item(row, scoring.weights, table.columns)
|
||||
if isinstance(item, Failure):
|
||||
return item
|
||||
items.append(item)
|
||||
@@ -239,7 +256,10 @@ def read_items(lines: list[str], scoring: Scoring) -> tuple[list[Item], int] | F
|
||||
return Failure("two items share an ID")
|
||||
if ids and max(ids) >= scoring.next_id:
|
||||
return Failure(f"item {max(ids)} is not under Next ID {scoring.next_id}; raise Next ID past every ID")
|
||||
return items, table.start
|
||||
for item in items:
|
||||
if unheld := [blocker for blocker in item.blocked_by if blocker >= scoring.next_id]:
|
||||
return Failure(f"item {item.id} is blocked by item {unheld[0]}, which is not under Next ID {scoring.next_id}; name an item the table holds or held")
|
||||
return items, table
|
||||
|
||||
|
||||
def beside_table(lines: list[str], header: int, end: int) -> bool:
|
||||
@@ -250,40 +270,40 @@ def beside_table(lines: list[str], header: int, end: int) -> bool:
|
||||
return any(line.strip() for line in lines[heading + 1:header] + section)
|
||||
|
||||
|
||||
def blockers_first(scored: list[tuple[Item, float]], next_id: int) -> list[tuple[Item, float]] | Failure:
|
||||
def blockers_first(scored: list[tuple[Item, float]]) -> list[tuple[Item, float]] | Failure:
|
||||
"""scored, each blocker moved up to just above the first item it blocks; a blocker the table no longer holds was finished or deleted, and is dropped."""
|
||||
held = {item.id: (item, value) for item, value in scored}
|
||||
position = {item.id: index for index, (item, _) in enumerate(scored)}
|
||||
for item, _ in scored:
|
||||
for blocker in item.blocked_by:
|
||||
if blocker >= next_id:
|
||||
return Failure(f"item {item.id} is blocked by item {blocker}, which is not under Next ID {next_id}; name an item the table holds or held")
|
||||
if blocker == item.id:
|
||||
return Failure(f"item {item.id} is blocked by itself; drop {item.id} from its {BLOCKED_BY}")
|
||||
if blocker in held and held[blocker][0].release > item.release:
|
||||
later, earlier = release_name(held[blocker][0]), release_name(item)
|
||||
return Failure(f"item {item.id} is blocked by item {blocker}, which release {later} ships after release {earlier}; move item {blocker} to release {earlier} or earlier, or item {item.id} to release {later} or later")
|
||||
ordered: list[tuple[Item, float]] = []
|
||||
result: list[tuple[Item, float]] = []
|
||||
for item, _ in scored:
|
||||
if looped := place(item.id, held, position, ordered, []):
|
||||
return looped
|
||||
return ordered
|
||||
placed = place(item.id, held, position, {placed.id for placed, _ in result}, [])
|
||||
if isinstance(placed, Failure):
|
||||
return placed
|
||||
result += placed
|
||||
return result
|
||||
|
||||
|
||||
def place(id_: int, held: dict[int, tuple[Item, float]], position: dict[int, int], ordered: list[tuple[Item, float]], path: list[int]) -> Failure | None:
|
||||
"""Appends to ordered the item id_ after its blockers, each in score order; path holds the items waiting on this one, to find a loop."""
|
||||
def place(id_: int, held: dict[int, tuple[Item, float]], position: dict[int, int], placed: set[int], path: list[int]) -> list[tuple[Item, float]] | Failure:
|
||||
"""The item id_ preceded by those of its blockers not yet placed, each in score order, and theirs before them; path holds the items waiting on this one, to find a loop."""
|
||||
if id_ in path:
|
||||
loop = sorted(path[path.index(id_):])
|
||||
return Failure(f"items {', '.join(map(str, loop[:-1]))} and {loop[-1]} block each other in a loop; drop one of the blockers")
|
||||
if any(placed.id == id_ for placed, _ in ordered):
|
||||
return None
|
||||
if id_ in placed:
|
||||
return []
|
||||
item, value = held[id_]
|
||||
blockers = sorted((blocker for blocker in set(item.blocked_by) if blocker in held), key=position.__getitem__)
|
||||
added: list[tuple[Item, float]] = []
|
||||
for blocker in blockers:
|
||||
if looped := place(blocker, held, position, ordered, [*path, id_]):
|
||||
return looped
|
||||
ordered.append((dataclasses.replace(item, blocked_by=tuple(sorted(blockers))), value))
|
||||
return None
|
||||
before = place(blocker, held, position, placed | {entry.id for entry, _ in added}, [*path, id_])
|
||||
if isinstance(before, Failure):
|
||||
return before
|
||||
added += before
|
||||
return [*added, (dataclasses.replace(item, blocked_by=tuple(sorted(blockers))), value)]
|
||||
|
||||
|
||||
def release_name(item: Item) -> str:
|
||||
@@ -415,8 +435,8 @@ def parse_weights(lines: list[str], goals: list[str]) -> dict[str, float] | Fail
|
||||
return weights
|
||||
|
||||
|
||||
def parse_item(row: str, weights: dict[str, float], header: str) -> Item | Failure:
|
||||
item = parse_row(row, header)
|
||||
def parse_item(row: str, weights: dict[str, float], columns: tuple[str, ...]) -> Item | Failure:
|
||||
item = parse_row(row, columns)
|
||||
if isinstance(item, Failure):
|
||||
return item
|
||||
if unknown := [goal for goal in item.goals if goal not in weights]:
|
||||
|
||||
@@ -23,7 +23,7 @@ def todo(*rows: str, header: str = HEADER) -> str:
|
||||
|
||||
|
||||
def item(id_: int, scores: dict[str, int]) -> Item:
|
||||
return Item(id_, (0, 1, 0), "", f"**Item {id_}.**", scores, ("1",))
|
||||
return Item(id_, (0, 1, 0), "", f"**Item {id_}.**", scores, ("1",), ())
|
||||
|
||||
|
||||
def outcome(id_: int, r: int, s: int, c: int | None, lines: int = 10, rounds: int = 1, findings: tuple[int, int] | None = (0, 0), merged: str = "2026-10-13T10:00:00Z", shipped_with: tuple[int, ...] = ()) -> Outcome:
|
||||
|
||||
+12
-1
@@ -11,6 +11,7 @@ sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent))
|
||||
from failure import Failure
|
||||
import rank_todo
|
||||
from repo_checks.test_python_floor import ROOT
|
||||
import todo_table
|
||||
|
||||
README = "# P\n\n## Goals\n\n1. Fast.\n2. Correct.\n 1. Tested.\n\n## Audience\n\nMe.\n"
|
||||
|
||||
@@ -81,7 +82,9 @@ class Rank(unittest.TestCase):
|
||||
|
||||
def test_names_each_item_under_the_bar_that_is_not_exempt(self):
|
||||
ranked = self.ranked(TODO.replace("`Bar = 9`", "`Bar = 11`"))
|
||||
self.assertEqual(ranked.under_bar, ["item 2 scores 10.3, under the bar 11; delete it and any item that needs it, or mark it with a reason the bar does not apply"])
|
||||
self.assertEqual(ranked.under_bar, ["item 2 scores 10.3, under the bar 11; delete it, or mark it with a reason the bar does not apply"])
|
||||
blocking = self.ranked(TODO.replace("`Bar = 9`", "`Bar = 11`").replace("| 3 | 0.1.0 | |", "| 3 | 0.1.0 | 2 |"))
|
||||
self.assertEqual(blocking.under_bar, ["item 2 scores 10.3, under the bar 11; delete it and the items it blocks, 3, or mark it with a reason the bar does not apply"])
|
||||
self.assertEqual(self.ranked(ranked.text.replace("| 2 | 0.1.0 | | |", "| 2 | 0.1.0 | | principle |")).under_bar, [])
|
||||
self.assertEqual(self.ranked(ranked.text.replace("| 2 | 0.1.0 | | |", "| 2 | 0.1.0 | | owner |")).under_bar, [])
|
||||
|
||||
@@ -167,6 +170,14 @@ class Blockers(unittest.TestCase):
|
||||
without = re.sub(r"\| (\d) \| (0\.[12]\.0) \| \| ", r"| \1 | \2 | ", without)
|
||||
self.assertEqual(self.ranked(without), self.ranked(TODO))
|
||||
|
||||
def test_refuses_a_column_it_does_not_know_rather_than_drop_it_and_asks_for_c_beside_blocked_by(self):
|
||||
extra = TODO.replace("| Goals | Score |\n|---|", "| Goals | Score | Later |\n|---|---|").replace(" | 0 |\n", " | 0 | x |\n")
|
||||
self.assertEqual(self.ranked(extra), Failure("todo.md's items table has the column Later, which this rank_todo.py does not know; move this machine to the release that added it"))
|
||||
self.assertEqual([item.id for item in todo_table.items(extra)], [1, 2, 3, 4, 5])
|
||||
without_c = TODO.replace("| R | S | C |", "| R | S |").replace("|---|---|---|---|---|---|---|---|---|---|---|---|", "|---|---|---|---|---|---|---|---|---|---|---|")
|
||||
without_c = re.sub(r"(\*\* \| \d+ \| \d+) \| \d+ \|", r"\1 |", without_c)
|
||||
self.assertEqual(self.ranked(without_c).reason, "todo.md's items table has no C column; add one after S, and score each item's C from 1 to 10 per rank_todo.py --help")
|
||||
|
||||
def test_refuses_text_under_items_beside_the_table(self):
|
||||
for text in ("Release 0.1.0 takes item 5 first.\n\n", ""):
|
||||
ordered = TODO.replace("## Items\n\n", f"## Items\n\n{text}") if text else TODO.replace("| 1 | 0.2.0 | | | **Ship the later thing.** | 1 | 1 | 3 | 9 | 9 | 1 | 0 |\n", "| 1 | 0.2.0 | | | **Ship the later thing.** | 1 | 1 | 3 | 9 | 9 | 1 | 0 |\n\nTake item 5 first.\n")
|
||||
|
||||
+55
-26
@@ -8,13 +8,14 @@ TOP_SCORE = 10
|
||||
EXEMPTIONS = ("", "decision", "defect", "owner", "principle", "question", "security")
|
||||
# The column naming the items that block an item: each must be done before the item starts.
|
||||
BLOCKED_BY = "Blocked by"
|
||||
# The columns rank_todo writes, in order. A reader takes cells by column name, so it reads a table from before BLOCKED_BY or C joined, or with a column a later release adds.
|
||||
COLUMNS = ("ID", "Release", BLOCKED_BY, "Exempt", "Item", "R", "S", "C", "A", "G", "Goals", "Score")
|
||||
ITEMS_HEADER = "| " + " | ".join(COLUMNS) + " |"
|
||||
# A table from before BLOCKED_BY joined lacks it; items() parses it as blocked by nothing, and rank_todo adds the column.
|
||||
WITHOUT_BLOCKERS = "| " + " | ".join(column for column in COLUMNS if column != BLOCKED_BY) + " |"
|
||||
# Tables in git history from before C joined lack it too; items() still parses them, though rank_todo refuses one in a checkout.
|
||||
WITHOUT_C = "| " + " | ".join(column for column in COLUMNS if column not in (BLOCKED_BY, "C")) + " |"
|
||||
HEADERS = (ITEMS_HEADER, WITHOUT_BLOCKERS, WITHOUT_C)
|
||||
DIVIDER = "|" + "---|" * len(COLUMNS)
|
||||
# The columns without which no row can be read; a missing BLOCKED_BY reads as blocked by nothing, a missing C as no C.
|
||||
NEEDED = ("ID", "Release", "Exempt", "Item", "R", "S", "A", "G", "Goals")
|
||||
# How the items table's header opens, which tells it from the goal weights table.
|
||||
HEADER_START = "| ID | Release |"
|
||||
|
||||
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
@@ -26,7 +27,7 @@ class Item:
|
||||
scores: dict[str, int]
|
||||
goals: tuple[str, ...]
|
||||
# The IDs of the items that block it.
|
||||
blocked_by: tuple[int, ...] = ()
|
||||
blocked_by: tuple[int, ...]
|
||||
|
||||
@property
|
||||
def risk_and_complexity(self) -> int:
|
||||
@@ -36,39 +37,65 @@ class Item:
|
||||
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
class Table:
|
||||
"""Body rows of a markdown table, and the index of the first row in the file's lines."""
|
||||
start: int
|
||||
"""A markdown table: the index of its header in the file's lines, its column names, and its body rows."""
|
||||
header: int
|
||||
columns: tuple[str, ...]
|
||||
rows: list[str]
|
||||
|
||||
@property
|
||||
def start(self) -> int:
|
||||
"""The index of the first body row, below the header and the divider."""
|
||||
return self.header + 2
|
||||
|
||||
|
||||
def items(todo: str) -> list[Item] | None | Failure:
|
||||
"""Each row of a todo.md's items table, with or without BLOCKED_BY and C; None where todo.md holds no items table."""
|
||||
lines = todo.splitlines()
|
||||
headers = [header for header in HEADERS if header in lines]
|
||||
if not headers:
|
||||
return None
|
||||
table = table_rows(lines, headers[0])
|
||||
if isinstance(table, Failure):
|
||||
"""Each row of a todo.md's items table; None where todo.md holds no items table."""
|
||||
table = items_table(todo.splitlines())
|
||||
if table is None or isinstance(table, Failure):
|
||||
return table
|
||||
found = []
|
||||
for row in table.rows:
|
||||
item = parse_row(row, headers[0])
|
||||
item = parse_row(row, table.columns)
|
||||
if isinstance(item, Failure):
|
||||
return item
|
||||
found.append(item)
|
||||
return found
|
||||
|
||||
|
||||
def items_table(lines: list[str]) -> Table | None | Failure:
|
||||
"""The table whose header opens with HEADER_START; None where there is none."""
|
||||
headers = [line for line in lines if line.startswith(HEADER_START)]
|
||||
if not headers:
|
||||
return None
|
||||
if len(headers) > 1:
|
||||
return Failure(f"todo.md holds {len(headers)} tables whose header opens {HEADER_START}; keep one items table")
|
||||
table = table_rows(lines, headers[0])
|
||||
if isinstance(table, Failure):
|
||||
return table
|
||||
if missing := [column for column in NEEDED if column not in table.columns]:
|
||||
return Failure(f"todo.md's items table has no {', '.join(missing)} column; rank_todo.py --help shows a valid one")
|
||||
return table
|
||||
|
||||
|
||||
def waiting(found: list[Item]) -> set[int]:
|
||||
"""The IDs of the items that wait: each marked question, or blocked by an item the table still holds. It holds in any order of the rows."""
|
||||
held = {item.id for item in found}
|
||||
return {item.id for item in found if item.exempt == "question" or held.intersection(item.blocked_by)}
|
||||
|
||||
|
||||
def table_rows(lines: list[str], header: str) -> Table | Failure:
|
||||
if lines.count(header) != 1:
|
||||
return Failure(f"todo.md needs the table header {header} once")
|
||||
start = lines.index(header) + 2
|
||||
if start > len(lines) or not re.fullmatch(r"\|(-+\|)+", lines[start - 1].replace(" ", "")):
|
||||
at = lines.index(header)
|
||||
if at + 2 > len(lines) or not re.fullmatch(r"\|(-+\|)+", lines[at + 1].replace(" ", "")):
|
||||
return Failure(f"the table under {header} has no |---| divider line")
|
||||
end = start
|
||||
end = at + 2
|
||||
while end < len(lines) and lines[end].startswith("|"):
|
||||
end += 1
|
||||
return Table(start, lines[start:end])
|
||||
columns = cells(header, header.count("|") - 1)
|
||||
if isinstance(columns, Failure):
|
||||
return columns
|
||||
return Table(at, tuple(columns), lines[at + 2:end])
|
||||
|
||||
|
||||
def cells(row: str, count: int) -> list[str] | Failure:
|
||||
@@ -76,13 +103,12 @@ def cells(row: str, count: int) -> list[str] | Failure:
|
||||
return found if len(found) == count else Failure(f"this row needs {count} cells: {row}")
|
||||
|
||||
|
||||
def parse_row(row: str, header: str = ITEMS_HEADER) -> Item | Failure:
|
||||
"""An item row under header, every cell checked but the goals it cites."""
|
||||
names = header.strip("| ").split(" | ")
|
||||
found = cells(row, len(names))
|
||||
def parse_row(row: str, columns: tuple[str, ...] = COLUMNS) -> Item | Failure:
|
||||
"""An item row under columns, every cell checked but the goals it cites."""
|
||||
found = cells(row, len(columns))
|
||||
if isinstance(found, Failure):
|
||||
return found
|
||||
named = dict(zip(names, found))
|
||||
named = dict(zip(columns, found))
|
||||
id_, release, exempt, tagline = named["ID"], named["Release"], named["Exempt"], named["Item"]
|
||||
if not re.fullmatch(r"[0-9]+", id_) or int(id_) < 1:
|
||||
return Failure(f"the ID {id_!r} is not a positive whole number: {row}")
|
||||
@@ -102,6 +128,9 @@ def parse_row(row: str, header: str = ITEMS_HEADER) -> Item | Failure:
|
||||
blockers = named.get(BLOCKED_BY, "")
|
||||
if not re.fullmatch(r"([0-9]+(, [0-9]+)*)?", blockers):
|
||||
return Failure(f"item {id_}'s {BLOCKED_BY} {blockers!r} is not empty or IDs separated by \", \"")
|
||||
blocked_by = tuple(int(blocker) for blocker in blockers.split(", ") if blocker)
|
||||
if int(id_) in blocked_by:
|
||||
return Failure(f"item {id_} is blocked by itself; drop {id_} from its {BLOCKED_BY}")
|
||||
cited = tuple(goal.strip() for goal in named["Goals"].split(","))
|
||||
major, minor, patch = (int(part) for part in release.split("."))
|
||||
return Item(int(id_), (major, minor, patch), exempt, tagline, scores, cited, tuple(int(blocker) for blocker in blockers.split(", ") if blocker))
|
||||
return Item(int(id_), (major, minor, patch), exempt, tagline, scores, cited, blocked_by)
|
||||
|
||||
Reference in New Issue
Block a user