import re from failure import Failure from plan.todo_table import Item def check_citations(items: list[Item], decisions: str) -> Failure | None: """Holds todo.md and docs/decisions.md to each other: every item a decision cites is in the plan and exempt from the bar, and every item marked decision is cited.""" cited = set(cited_items(decisions)) if gone := sorted(cited - {item.id for item in items}): return Failure(f"docs/decisions.md cites \"`todo.md` item {gone[0]}\", which todo.md no longer holds; revise that decision to say what holds now") for item in sorted(items, key=lambda item: item.id): if item.id in cited and not item.exempt: return Failure(f"docs/decisions.md cites \"`todo.md` item {item.id}\"; mark it decision, or another reason the bar does not apply") if item.exempt == "decision" and item.id not in cited: return Failure(f"item {item.id} is marked decision, but docs/decisions.md does not cite \"`todo.md` item {item.id}\"; cite it there, or mark it with another reason the bar does not apply") return None def cited_items(decisions: str) -> list[int]: return [int(id_) for cited in re.findall(r"`todo\.md` items? (\d+(?:(?:, | and )\d+)*)", decisions) for id_ in re.findall(r"\d+", cited)]