State code health as every project's first achieve goal, weighed four times while the comprehension overall is under 7.0, unless a README voids it

This commit is contained in:
2026-10-18 19:29:09 +02:00
parent e6223f7126
commit 2a961f267c
15 changed files with 375 additions and 216 deletions
+65 -30
View File
@@ -3,8 +3,8 @@
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,
Reads todo.md (default: ./todo.md), the README.md beside it, and docs/decisions.md and
docs/comprehension-history.md when there are. Rewrites each item's Score from the formula and sorts the items by release; within a release,
question rows come first, then security rows, then the rest, each group by score from highest, then
by ID. Then it moves each blocker, an item another's Blocked by cell names, that ranks below an item
it blocks to just above the first such item. Orders the details to match. Writes each item's R, and
@@ -37,11 +37,15 @@ A valid todo.md; ## Details and each item's detail are optional:
- The formula holds numbers, + - * / ** and parentheses over L, E, I, R, S and V.
- V sums, over the goals in an item's Moves cell, each goal's weight times how far the item moves it.
README.md marks each goal **Maintain:** or **Achieve:**. The achieve goals, in reading order and a
sub-goal such as 1.1 included, each weigh 0.97 times the one before, and the weights sum to 1.
With 5 achieve goals the first weighs 0.212 and the last 0.188. With 15 they weigh 0.082 and
0.053. Only an item marked defect may name a maintain goal, the one it restores, which weighs as
the heaviest achieve goal.
README.md marks each goal **Maintain:** or **Achieve:**. The achieve goals rank code health first,
the goal every project shares, which a Moves cell names as `code health by 4`. README.md's achieve
goals follow in reading order, a sub-goal such as 1.1 included. Each weighs 0.97 times the one
before, and the weights sum to 1. With 5 achieve goals the first weighs 0.212 and the last 0.188.
With 15 they weigh 0.082 and 0.053. Code health then weighs 4 times that while the overall of
docs/comprehension-history.md's newest scoring run is under 7.0, or there is none. A line
`**Void:** code health, since <reason>.` in README.md's ## Goals drops code health, and an item
may not name it. Only an item marked defect may name a maintain goal, the one it restores, which
weighs as the heaviest achieve goal.
- The bar is judged on the Score as shown, to one decimal.
- A new item takes Next ID, and Next ID then goes up by one; every ID stays below it.
- An item's L, E and I are levels that ~/.claude/rules/todo-items.md defines: L from 1 to 4, E from 1
@@ -70,25 +74,32 @@ A valid todo.md; ## Details and each item's detail are optional:
reader. No item may sit under a marked release, and a line goes once a tag at or past its first
release is cut. A release whose items all moved elsewhere without a line reads as finished.
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
Exit 1, saying why, on bad arguments, a file that cannot be read or written, a todo.md, README.md,
docs/decisions.md or comprehension history that breaks the rules above, an item that is not exempt scoring under the bar, or
--check finding the file unranked.
"""
import ast
import dataclasses
import decimal
import math
import pathlib
import re
import sys
import typing
from todo_table import BLOCKED_BY, cells, DETAILS, HEADER_START, Item, item_details, items_table, MOVES, OLD_SHAPE, parse_row, renames, risk, Shape, SHAPE, shape_of, Table, table_rows, version
from todo_table import BLOCKED_BY, cells, CODE_HEALTH, DETAILS, HEADER_START, Item, item_details, items_table, MOVES, OLD_SHAPE, parse_row, renames, risk, Shape, SHAPE, shape_of, Table, table_rows, version
from failure import Failure
from larv_review.scoring import FLOOR
from comprehension_history import latest_overall
from markdown_text import listed, sections
from file_writes import replace_file
# What each achieve goal weighs of the one above it; docs/decisions.md records the choice.
GOAL_STEP = 0.97
# How many times its weight code health weighs while the comprehension panel's overall is under FLOOR, or unmeasured; lilleman's choice, which docs/decisions.md records.
UNDER_FLOOR = 4
# The line of README.md's ## Goals that voids code health, as ~/.claude/rules/goals-and-audience.md spells it.
VOID = re.compile(r"^\*\*Void:\*\* (.*)$", re.M)
LARGEST_FORMULA = 200
LONGEST_FORMULA = 4 * LARGEST_FORMULA
OPERATORS = (ast.Add, ast.BinOp, ast.Div, ast.Expression, ast.Load, ast.Mult, ast.Pow, ast.Sub, ast.UAdd, ast.UnaryOp, ast.USub)
@@ -106,7 +117,7 @@ class Scoring:
formula: ast.Expression
bar: float
next_id: int
# README.md's goals in reading order, each with its mark.
# The goals an item may name, in rank order, each with its mark: code health first, unless README.md voids it or the table is in OLD_SHAPE, then README.md's goals in reading order.
goals: dict[str, Mark]
# The weight of each goal an item may name: in OLD_SHAPE every goal, at the weight todo.md states; else each goal goal_weights weighs.
weights: dict[str, float]
@@ -172,10 +183,10 @@ def parse_args(argv: list[str]) -> tuple[bool, pathlib.Path] | Failure:
return "--check" in argv, pathlib.Path(paths[0] if paths else "todo.md")
def read_all(todo: pathlib.Path) -> tuple[str, str, str] | Failure:
"""todo.md, the README.md beside it, and its docs/decisions.md, empty when there is none."""
def read_all(todo: pathlib.Path) -> tuple[str, str, str, str] | Failure:
"""todo.md, the README.md beside it, and its docs/decisions.md and docs/comprehension-history.md, each empty when there is none."""
texts = []
for path, missing in ((todo, "every project keeps a todo.md beside its README.md, and rank_todo.py --help shows one"), (todo.parent / "README.md", "rank_todo.py weighs goals from the README.md beside todo.md"), (todo.parent / "docs" / "decisions.md", None)):
for path, missing in ((todo, "every project keeps a todo.md beside its README.md, and rank_todo.py --help shows one"), (todo.parent / "README.md", "rank_todo.py weighs goals from the README.md beside todo.md"), (todo.parent / "docs" / "decisions.md", None), (todo.parent / "docs" / "comprehension-history.md", None)):
try:
texts.append(path.read_text(encoding="utf-8"))
except FileNotFoundError:
@@ -186,7 +197,7 @@ def read_all(todo: pathlib.Path) -> tuple[str, str, str] | Failure:
return Failure(f"{path}: {failure.strerror}")
except UnicodeDecodeError:
return Failure(f"{path}: not UTF-8")
return texts[0], texts[1], texts[2]
return texts[0], texts[1], texts[2], texts[3]
def write(path: pathlib.Path, text: str) -> Failure | None:
@@ -194,13 +205,13 @@ def write(path: pathlib.Path, text: str) -> Failure | None:
return Failure(f"{path}: {unwritten.reason}") if unwritten else None
def rank(text: str, readme: str, decisions: str) -> Ranked | Failure:
def rank(text: str, readme: str, decisions: str, history: str) -> Ranked | Failure:
lines = text.split("\n")
table = read_table(lines)
if isinstance(table, Failure):
return table
shape = shape_of(table.columns)
scoring = read_scoring(text, lines, readme, shape)
scoring = read_scoring(text, lines, readme, history, shape)
if isinstance(scoring, Failure):
return scoring
items = read_items(table, scoring)
@@ -240,7 +251,7 @@ def notes(items: list[Item], scoring: Scoring) -> list[str]:
if scoring.shape is OLD_SHAPE:
return [RESHAPE] + dropped
# Printed so the under-bar lines it causes do not read as items of low value.
unweighed = [] if any(scoring.weights.values()) else ["README.md marks no goal **Achieve:**, so every item scores V 0"]
unweighed = [] if any(scoring.weights.values()) else ["README.md voids code health and marks no goal **Achieve:**, so every item scores V 0"]
return unweighed + dropped
@@ -272,7 +283,7 @@ def under_the_bar(item: Item, value: float, bar: float, items: list[Item]) -> st
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, shape: Shape) -> Scoring | Failure:
def read_scoring(text: str, lines: list[str], readme: str, history: str, shape: Shape) -> Scoring | Failure:
goals = readme_goals(readme)
if isinstance(goals, Failure):
return goals
@@ -285,16 +296,28 @@ def read_scoring(text: str, lines: list[str], readme: str, shape: Shape) -> Scor
next_id = stated(text, "Next ID")
if isinstance(next_id, Failure) or not re.fullmatch(r"[0-9]+", next_id):
return next_id if isinstance(next_id, Failure) else Failure(f"Next ID {next_id!r} is not a whole number")
numbers = [number for number, _ in goals]
weighed = weigh(lines, readme, history, goals, shape)
if isinstance(weighed, Failure):
return weighed
return Scoring(shape, formula, bar, int(next_id), *weighed)
def weigh(lines: list[str], readme: str, history: str, goals: list[tuple[str, Mark]], shape: Shape) -> tuple[dict[str, Mark], dict[str, float]] | Failure:
"""The goals an item may name, in rank order, and each one's weight. In OLD_SHAPE they are README.md's goals at the weights todo.md states. Otherwise code health comes first, unless README.md voids it, then README.md's goals, at the weights goal_weights gives them."""
if shape is OLD_SHAPE:
weights = parse_weights(lines, numbers)
elif WEIGHTS_HEADER in lines:
weights = Failure(f"todo.md holds a goal weights table beside a {MOVES} column; drop the table, since rank_todo.py weighs each achieve goal by its position in README.md")
else:
weights = goal_weights(goals)
if isinstance(weights, Failure):
return weights
return Scoring(shape, formula, bar, int(next_id), dict(goals), weights)
stated_weights = parse_weights(lines, [number for number, _ in goals])
return stated_weights if isinstance(stated_weights, Failure) else (dict(goals), stated_weights)
if WEIGHTS_HEADER in lines:
return Failure(f"todo.md holds a goal weights table beside a {MOVES} column; drop the table, since rank_todo.py weighs each achieve goal by its position in README.md")
voided = voids_code_health(readme)
if isinstance(voided, Failure):
return voided
overall = latest_overall(history)
if isinstance(overall, Failure):
return overall
ranked = goals if voided else [(CODE_HEALTH, "Achieve"), *goals]
weights = goal_weights(ranked, overall)
return weights if isinstance(weights, Failure) else (dict(ranked), weights)
def read_table(lines: list[str]) -> Table | Failure:
@@ -432,13 +455,23 @@ def readme_goals(readme: str) -> list[tuple[str, Mark]] | Failure:
return goals
def goal_weights(goals: list[tuple[str, Mark]]) -> dict[str, float] | Failure:
"""Each achieve goal's weight: the one ranked k of n in reading order weighs GOAL_STEP ** (k - 1) / (1 + GOAL_STEP + ... + GOAL_STEP ** (n - 1)), so the weights sum to 1. Each maintain goal weighs as the heaviest achieve goal, since a broken one outranks any achieve goal; only a defect that restores it may name it. Every weight is 0 where README.md marks no goal **Achieve:**, so every item scores V 0 and only an exempt one passes the bar."""
def voids_code_health(readme: str) -> bool | Failure:
"""Whether README.md's ## Goals voids code health; a void of anything else fails."""
voids = VOID.findall("".join(sections(readme).get("Goals", [])))
if other := [void for void in voids if not void.startswith(f"{CODE_HEALTH}, since ")]:
return Failure(f"README.md's ## Goals voids `{other[0]}`; only code health may be voided, as `**Void:** {CODE_HEALTH}, since <reason>.`")
return bool(voids)
def goal_weights(goals: list[tuple[str, Mark]], overall: decimal.Decimal | None) -> dict[str, float] | Failure:
"""Each achieve goal's weight: the one ranked k of n weighs GOAL_STEP ** (k - 1) / (1 + GOAL_STEP + ... + GOAL_STEP ** (n - 1)), so the weights sum to 1. Code health, where goals holds it, then weighs UNDER_FLOOR times that while the overall is under FLOOR or None, and the other goals keep their weights. Each maintain goal weighs as the heaviest achieve goal, since a broken one outranks any achieve goal; only a defect that restores it may name it. Every weight is 0 where README.md marks no goal **Achieve:**, so every item scores V 0 and only an exempt one passes the bar."""
if unmarked := [number for number, mark in goals if mark is None]:
return Failure(f"README.md's goal {unmarked[0]} is marked neither **Maintain:** nor **Achieve:**; mark it, as ~/.claude/rules/goals-and-audience.md says, so rank_todo.py knows which goals score")
achieve = [number for number, mark in goals if mark == "Achieve"]
total = sum(GOAL_STEP**rank for rank in range(len(achieve)))
weights = {number: GOAL_STEP**rank / total for rank, number in enumerate(achieve)}
if CODE_HEALTH in weights and (overall is None or overall < FLOOR):
weights[CODE_HEALTH] *= UNDER_FLOOR
heaviest = max(weights.values(), default=0.0)
return weights | {number: heaviest for number, mark in goals if mark == "Maintain"}
@@ -570,6 +603,8 @@ def parse_item(row: str, scoring: Scoring, table: Table) -> Item | Failure:
return Failure(f"item {item.id} cites goal {', '.join(unknown)}, which the goal weights do not list")
return item
for goal, _ in item.moves:
if goal == CODE_HEALTH and goal not in scoring.goals:
return Failure(f"item {item.id} names {CODE_HEALTH}, which README.md voids; drop it from the item's {MOVES}, or the void from README.md")
if goal not in scoring.goals:
return Failure(f"item {item.id} cites goal {goal}, which README.md's ## Goals does not number")
if scoring.goals[goal] == "Maintain" and item.exempt != "defect":