106 lines
5.9 KiB
Python
106 lines
5.9 KiB
Python
"""The order do-epic works an epic's items in: by score, then each blocker above what it blocks (docs/decisions.md → `do-epic: drives one Jira epic …`).
|
|
|
|
An item scores by the epic's formula over one chain of goals: the epic's goals, then code health, then the goals of the item's repo. plan/goals_file.py's goal_weights weighs the chain as it weighs one project's goals, so a goal of the epic outweighs every goal of a repo. An item without a repo has only the epic's goals.
|
|
|
|
The epic and each repo number their goals from 1, so an item's Moves names a goal by its scope: `epic 1.1 by 8, code health by 5, repo 2 by 4`."""
|
|
import ast
|
|
import dataclasses
|
|
import decimal
|
|
|
|
from failure import Failure
|
|
from plan import ranking
|
|
from plan.blockers import blockers_first
|
|
from plan.formula import parse_formula
|
|
from plan.goals_file import goal_weights, Mark, numbered, ranked_goals
|
|
from plan.todo_table import CODE_HEALTH, EPIC_GOAL, Item, REPO_GOAL, SHAPE
|
|
from review_rules.comprehension_history import latest_overall
|
|
|
|
MARK_IT = "is marked neither **Maintain:** nor **Achieve:**; mark it, as ~/.claude/rules/goals-and-audience.md says"
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class RepoPremises:
|
|
"""What a repo's own plan is ranked by: its goals file, its decision log and comprehension history, each as text, and its readability floor."""
|
|
goals_text: str
|
|
decisions: str
|
|
history: str
|
|
floor: decimal.Decimal
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class EpicItem:
|
|
"""One of an epic's open items. An epic item has no release, so every item of one epic holds the same one. repo is the name of the repo it is worked in; None where it has none."""
|
|
item: Item
|
|
repo: str | None
|
|
|
|
|
|
def ranked(items: list[EpicItem], formula: str, epic_goals: str, repos: dict[str, RepoPremises]) -> list[tuple[Item, float]] | Failure:
|
|
"""Each item with its score: a question first, then a security finding, then by score from highest, then by ID, and then each blocker above every item it blocks. formula is the epic's, epic_goals the text under its ## Goals, and repos the premises of each repo an item is in, by name."""
|
|
parsed = parse_formula(formula, ranking.variables(SHAPE))
|
|
if isinstance(parsed, Failure):
|
|
return parsed
|
|
scorings: dict[str | None, ranking.Scoring] = {}
|
|
scored = []
|
|
for each in items:
|
|
name = f"item {each.item.id}"
|
|
if each.repo not in scorings:
|
|
if each.repo is not None and each.repo not in repos:
|
|
return Failure(f"{name} is in the repo {each.repo}, whose goals were not read")
|
|
scoring = chain_scoring(parsed, epic_goals, each.repo, repos[each.repo] if each.repo else None)
|
|
if isinstance(scoring, Failure):
|
|
return scoring
|
|
scorings[each.repo] = scoring
|
|
value = unnamed_goal(each, scorings[each.repo], name) or ranking.item_score(each.item, scorings[each.repo], name)
|
|
if isinstance(value, Failure):
|
|
return value
|
|
scored.append((each.item, value))
|
|
scored.sort(key=lambda pair: (ranking.FIRST.get(pair[0].exempt, len(ranking.FIRST)), -pair[1], pair[0].id))
|
|
return blockers_first(scored)
|
|
|
|
|
|
def chain_scoring(formula: ast.Expression, epic_goals: str, repo: str | None, premises: RepoPremises | None) -> ranking.Scoring | Failure:
|
|
"""How an item in repo scores: the chain's goals, each achieve goal at its weight. The bar is 0, since the bar decides what is filed, not the order of what was."""
|
|
goals = chain(epic_goals, repo, premises)
|
|
if isinstance(goals, Failure):
|
|
return goals
|
|
if unmarked := next((goal for goal, mark in goals if mark is None), None):
|
|
whose, _, number = unmarked.partition(" ")
|
|
return Failure(f"{'the epic' if whose == 'epic' else repo}'s goal {number} {MARK_IT}")
|
|
overall = latest_overall(premises.history) if premises and CODE_HEALTH in dict(goals) else None
|
|
if isinstance(overall, Failure):
|
|
return Failure(f"{repo}: {overall.reason}")
|
|
# goal_weights fails only on a goal without a mark, which the check above already named by whose it is.
|
|
weights = goal_weights(goals, overall, premises.floor if premises else decimal.Decimal(0))
|
|
return weights if isinstance(weights, Failure) else ranking.Scoring(SHAPE, formula, 0, 0, dict(goals), weights)
|
|
|
|
|
|
def chain(epic_goals: str, repo: str | None, premises: RepoPremises | None) -> list[tuple[str, Mark]] | Failure:
|
|
"""Every goal an item may name, in rank order, each with its mark: the epic's goals as `epic <number>`, then, for an item in repo, which has these premises, code health unless the repo's goals file voids it, and the repo's goals as `repo <number>`."""
|
|
epic = numbered(epic_goals)
|
|
if isinstance(epic, Failure):
|
|
return Failure(epic.reason.replace("the goals file's", "the epic's"))
|
|
scoped = [(f"{EPIC_GOAL}{number}", mark) for number, mark in epic]
|
|
if premises is None:
|
|
return scoped
|
|
own = ranked_goals(premises.goals_text, premises.decisions)
|
|
if isinstance(own, Failure):
|
|
return Failure(f"{repo}: {own.reason}")
|
|
return [*scoped, *((goal if goal == CODE_HEALTH else f"{REPO_GOAL}{goal}", mark) for goal, mark in own)]
|
|
|
|
|
|
def unnamed_goal(each: EpicItem, scoring: ranking.Scoring, name: str) -> Failure | None:
|
|
"""Why the item may not name one of the goals it names, in the epic's terms; None where its chain holds each."""
|
|
for goal, _ in each.item.moves:
|
|
if goal in scoring.goals:
|
|
continue
|
|
if not goal.startswith((EPIC_GOAL, REPO_GOAL)) and goal != CODE_HEALTH:
|
|
return Failure(f"{name} names goal {goal} without saying whose it is; write `{EPIC_GOAL}{goal}` for the epic's goal or `{REPO_GOAL}{goal}` for its repo's")
|
|
if goal.startswith(EPIC_GOAL):
|
|
return Failure(f"{name} names {goal}, which the epic's ## Goals does not number")
|
|
if each.repo is None:
|
|
return Failure(f"{name} names {goal}, but has no repo whose {'code' if goal == CODE_HEALTH else 'goal'} that could be; name only the epic's goals, as in `{EPIC_GOAL}1 by 5`")
|
|
if goal == CODE_HEALTH:
|
|
return Failure(f"{name} names {CODE_HEALTH}, which {each.repo}'s goals file voids")
|
|
return Failure(f"{name} names {goal}, which the ## Goals of {each.repo}'s goals file does not number")
|
|
return None
|