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:
2026-10-15 23:08:41 +02:00
parent 5b7da27a5a
commit 157ca50733
12 changed files with 173 additions and 113 deletions
+55 -26
View File
@@ -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)