Rank each todo.md blocker just above the first item it blocks, allow no other order in todo.md, and route loop-claude on the first item that waits on nothing
This commit is contained in:
+75
-21
@@ -5,8 +5,10 @@ usage: rank_todo.py [--check] [todo.md]
|
||||
|
||||
Reads todo.md (default: ./todo.md), the README.md beside it, and docs/decisions.md when there is
|
||||
one. Rewrites each item's Score from the formula and sorts the items by release; within a release,
|
||||
security rows come first, then the rest by score from highest, then by ID. Orders the details to
|
||||
match. --check writes nothing and fails when the file is not ranked as it would be.
|
||||
security rows come first, then the rest by score from highest, then by ID. Then it moves each item's
|
||||
blockers, the items its Blocked by cell names, to just above the first item they block. The table's
|
||||
order is todo.md's only order. Orders the details to match. Adds the Blocked by column to a table
|
||||
without it. --check writes nothing and fails when the file is not ranked as it would be.
|
||||
|
||||
A valid todo.md; ## Details and each item's detail are optional:
|
||||
|
||||
@@ -26,9 +28,9 @@ A valid todo.md; ## Details and each item's detail are optional:
|
||||
|
||||
## Items
|
||||
|
||||
| ID | Release | Exempt | Item | R | S | C | A | G | Goals | Score |
|
||||
|---|---|---|---|---|---|---|---|---|---|---|
|
||||
| 1 | 0.1.0 | | **Make the first thing work.** | 2 | 3 | 4 | 8 | 9 | 1 | 0 |
|
||||
| ID | Release | Blocked by | Exempt | Item | R | S | C | A | G | Goals | Score |
|
||||
|---|---|---|---|---|---|---|---|---|---|---|---|
|
||||
| 1 | 0.1.0 | | | **Make the first thing work.** | 2 | 3 | 4 | 8 | 9 | 1 | 0 |
|
||||
|
||||
## Details
|
||||
|
||||
@@ -46,6 +48,11 @@ A valid todo.md; ## Details and each item's detail are optional:
|
||||
principle, question or security. An item docs/decisions.md cites as "`todo.md` item N" must be
|
||||
exempt, and a row marked decision must be cited there.
|
||||
- An item's Item cell is one bold tagline; its detail, if any, sits under its ID in ## Details.
|
||||
- An item's Blocked by cell is empty, or lists IDs such as `12, 14`: each must be done before the
|
||||
item starts, so no blocker may sit in a later release, and no item may block itself or form a
|
||||
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.
|
||||
|
||||
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
|
||||
@@ -58,7 +65,7 @@ import pathlib
|
||||
import re
|
||||
import sys
|
||||
|
||||
from todo_table import cells, Item, ITEMS_HEADER, parse_row, table_rows, WITHOUT_C
|
||||
from todo_table import BLOCKED_BY, cells, Item, ITEMS_HEADER, parse_row, table_rows, WITHOUT_BLOCKERS, WITHOUT_C
|
||||
from failure import Failure
|
||||
from markdown_text import FENCE, sections
|
||||
from file_writes import replace_file
|
||||
@@ -169,13 +176,18 @@ def rank(text: str, readme: str, decisions: str) -> Ranked | Failure:
|
||||
items, start = table
|
||||
if uncited := check_citations(items, decisions):
|
||||
return uncited
|
||||
scored = []
|
||||
by_score = []
|
||||
for item in items:
|
||||
value = score(scoring.formula, item, scoring.weights)
|
||||
if isinstance(value, Failure):
|
||||
return value
|
||||
scored.append((item, value))
|
||||
scored.sort(key=lambda pair: (pair[0].release, pair[0].exempt != "security", -pair[1], pair[0].id))
|
||||
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):
|
||||
@@ -185,7 +197,7 @@ def rank(text: str, readme: str, decisions: str) -> Ranked | Failure:
|
||||
|
||||
|
||||
def read_scoring(text: str, lines: list[str], readme: str) -> Scoring | Failure:
|
||||
if ITEMS_HEADER not in lines and "## Items" not in lines:
|
||||
if ITEMS_HEADER not in lines and WITHOUT_BLOCKERS not in lines and "## Items" not in lines:
|
||||
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:
|
||||
@@ -210,14 +222,15 @@ def read_scoring(text: str, lines: list[str], readme: str) -> Scoring | Failure:
|
||||
|
||||
def read_items(lines: list[str], scoring: Scoring) -> tuple[list[Item], int] | Failure:
|
||||
"""The items, and the index of the first row in lines."""
|
||||
table = table_rows(lines, ITEMS_HEADER)
|
||||
header = WITHOUT_BLOCKERS if WITHOUT_BLOCKERS in lines and ITEMS_HEADER not in lines else ITEMS_HEADER
|
||||
table = table_rows(lines, header)
|
||||
if isinstance(table, Failure):
|
||||
return table
|
||||
if stray_row(lines[table.start + len(table.rows):]):
|
||||
return Failure("## Items holds a row outside its table; keep the rows together under the header")
|
||||
if beside_table(lines, table.start - 2, 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)
|
||||
item = parse_item(row, scoring.weights, header)
|
||||
if isinstance(item, Failure):
|
||||
return item
|
||||
items.append(item)
|
||||
@@ -229,10 +242,52 @@ def read_items(lines: list[str], scoring: Scoring) -> tuple[list[Item], int] | F
|
||||
return items, table.start
|
||||
|
||||
|
||||
def stray_row(after: list[str]) -> bool:
|
||||
"""Whether a table row follows the table before the next ## heading."""
|
||||
def beside_table(lines: list[str], header: int, end: int) -> bool:
|
||||
"""Whether ## Items holds anything but its table, which starts at the header line and ends before end: the table's order is todo.md's only order."""
|
||||
heading = max((index for index in range(header) if lines[index].startswith("## ")), default=-1)
|
||||
after = lines[end:]
|
||||
section = after[:next((index for index, line in enumerate(after) if line.startswith("## ")), len(after))]
|
||||
return any(line.startswith("|") for line in section)
|
||||
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:
|
||||
"""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]] = []
|
||||
for item, _ in scored:
|
||||
if looped := place(item.id, held, position, ordered, []):
|
||||
return looped
|
||||
return ordered
|
||||
|
||||
|
||||
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."""
|
||||
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
|
||||
item, value = held[id_]
|
||||
blockers = sorted((blocker for blocker in set(item.blocked_by) if blocker in held), key=position.__getitem__)
|
||||
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
|
||||
|
||||
|
||||
def release_name(item: Item) -> str:
|
||||
return ".".join(str(part) for part in item.release)
|
||||
|
||||
|
||||
def check_citations(items: list[Item], decisions: str) -> Failure | None:
|
||||
@@ -360,8 +415,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]) -> Item | Failure:
|
||||
item = parse_row(row)
|
||||
def parse_item(row: str, weights: dict[str, float], header: str) -> Item | Failure:
|
||||
item = parse_row(row, header)
|
||||
if isinstance(item, Failure):
|
||||
return item
|
||||
if unknown := [goal for goal in item.goals if goal not in weights]:
|
||||
@@ -370,9 +425,8 @@ def parse_item(row: str, weights: dict[str, float]) -> Item | Failure:
|
||||
|
||||
|
||||
def render_item(item: Item, value: float) -> str:
|
||||
release = ".".join(str(part) for part in item.release)
|
||||
scores = " | ".join(str(item.scores[name]) for name in "RSCAG")
|
||||
return f"| {item.id} | {release} | {item.exempt} | {item.tagline} | {scores} | {', '.join(item.goals)} | {value:.1f} |"
|
||||
return f"| {item.id} | {release_name(item)} | {', '.join(map(str, item.blocked_by))} | {item.exempt} | {item.tagline} | {scores} | {', '.join(item.goals)} | {value:.1f} |"
|
||||
|
||||
|
||||
def rank_details(text: str, items: list[Item]) -> str | Failure:
|
||||
|
||||
Reference in New Issue
Block a user