tests / unittest (push) Failing after 13s
Stage 1 flags the living documents a pull request makes stale (no model, nothing sent). Stage 2, on repositories that opt in with drafter: claude, has Claude Code draft the edits in a sandboxed copy of the tree and opens them as a pull request into the feature branch. A monthly pass re-reads the top-level documents against the whole repository. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
197 lines
7.5 KiB
Python
197 lines
7.5 KiB
Python
"""The five flag kinds, whole-repository mode and monthly grouping."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import posixpath
|
|
from collections import Counter, defaultdict
|
|
from collections.abc import Iterable, Mapping
|
|
from dataclasses import dataclass
|
|
|
|
from docs_review.changes import Change
|
|
from docs_review.mentions import Mentions, doc_mentions
|
|
|
|
BOILERPLATE_MIN_DOCS = 3
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class Flag:
|
|
kind: str # touched, uncovered, dead, stale_generated, unguided
|
|
doc: str | None
|
|
subject: str
|
|
detail: str
|
|
|
|
|
|
def _in_source_dirs(path: str, source_dirs: tuple[str, ...]) -> bool:
|
|
return any(path.startswith(d.rstrip("/") + "/") for d in source_dirs)
|
|
|
|
|
|
def _unique_basenames(
|
|
changes: Iterable[Change], repo_files: Iterable[str] | None = None
|
|
) -> frozenset[str]:
|
|
"""Basenames carried by exactly one path among the changes and, when given, every
|
|
repository file: a basename two files share cannot say which one a doc means."""
|
|
paths = {c.path for c in changes} | {c.old_path for c in changes if c.old_path}
|
|
paths |= set(repo_files or ())
|
|
counts = Counter(posixpath.basename(p) for p in paths)
|
|
return frozenset(b for b, n in counts.items() if n == 1)
|
|
|
|
|
|
def _mentions_path(m: Mentions, path: str, unique: frozenset[str]) -> bool:
|
|
base = posixpath.basename(path)
|
|
return path in m.paths or (base in unique and base in m.basenames)
|
|
|
|
|
|
def _boilerplate_paths(
|
|
mentions: Mapping[str, Mentions], changes: tuple[Change, ...], unique: frozenset[str],
|
|
) -> frozenset[str]:
|
|
"""Modified paths more than half the documents mention, and at least
|
|
BOILERPLATE_MIN_DOCS of them: a setup line every chapter repeats says nothing about
|
|
which chapter a change to that file makes stale."""
|
|
out = set()
|
|
for c in changes:
|
|
if c.status != "M":
|
|
continue
|
|
n = sum(_mentions_path(m, c.path, unique) for m in mentions.values())
|
|
if n >= BOILERPLATE_MIN_DOCS and n * 2 > len(mentions):
|
|
out.add(c.path)
|
|
return frozenset(out)
|
|
|
|
|
|
def _path_flags(
|
|
doc: str, m: Mentions, changes: tuple[Change, ...], unique: frozenset[str],
|
|
doc_changed: bool, boilerplate: frozenset[str] = frozenset(),
|
|
) -> list[Flag]:
|
|
flags = []
|
|
for c in changes:
|
|
if c.status == "M" and (doc_changed or c.path in boilerplate):
|
|
continue
|
|
if c.status == "M" and _mentions_path(m, c.path, unique):
|
|
flags.append(Flag("touched", doc, c.path, "changed"))
|
|
elif c.status == "D" and _mentions_path(m, c.path, unique):
|
|
flags.append(Flag("dead", doc, c.path, "removed"))
|
|
elif c.status == "R" and c.old_path and _mentions_path(m, c.old_path, unique):
|
|
flags.append(Flag("dead", doc, c.old_path, f"renamed to {c.path}"))
|
|
return flags
|
|
|
|
|
|
def _changed_input_flags(
|
|
generated_texts: Mapping[str, str], changes: tuple[Change, ...],
|
|
unique: frozenset[str], changed_paths: frozenset[str],
|
|
) -> list[Flag]:
|
|
flags = []
|
|
for doc, text in generated_texts.items():
|
|
if doc in changed_paths:
|
|
continue
|
|
m = doc_mentions(text, doc)
|
|
for c in changes:
|
|
for path in (c.path, c.old_path):
|
|
if path and _mentions_path(m, path, unique):
|
|
flags.append(Flag("stale_generated", doc, path, f"input changed: {path}"))
|
|
break
|
|
return flags
|
|
|
|
|
|
def _guided_dirs(m: Mentions, guide_path: str) -> frozenset[str]:
|
|
"""Directories the guide describes as a whole: each directory it names, and the
|
|
directory of each README.md it names other than its own and the repository's."""
|
|
readme_dirs = {
|
|
posixpath.dirname(p) for p in m.paths
|
|
if posixpath.basename(p) == "README.md" and p != guide_path and posixpath.dirname(p)
|
|
}
|
|
return m.dirs | frozenset(readme_dirs)
|
|
|
|
|
|
def _unguided_flags(
|
|
doc_texts: Mapping[str, str], guide: tuple[str, str] | None,
|
|
scope: frozenset[str] | None,
|
|
) -> list[Flag]:
|
|
if guide is None:
|
|
return []
|
|
guide_path, guide_text = guide
|
|
m = doc_mentions(guide_text, guide_path)
|
|
dirs = _guided_dirs(m, guide_path)
|
|
return [
|
|
Flag("unguided", doc, doc, f"not described in {guide_path}")
|
|
for doc in doc_texts
|
|
if doc != guide_path and doc not in m.paths
|
|
and (scope is None or doc in scope)
|
|
and not any(doc.startswith(d + "/") for d in dirs)
|
|
]
|
|
|
|
|
|
def _sort_key(f: Flag) -> tuple[str, str, str]:
|
|
return (f.kind, f.doc or "", f.subject)
|
|
|
|
|
|
def compute_flags(
|
|
changes: tuple[Change, ...],
|
|
names_added: frozenset[str],
|
|
names_removed: frozenset[str],
|
|
doc_texts: Mapping[str, str],
|
|
generated_texts: Mapping[str, str],
|
|
source_dirs: tuple[str, ...],
|
|
guide: tuple[str, str] | None,
|
|
*,
|
|
repo_files: Iterable[str] | None = None,
|
|
unguided_scope: frozenset[str] | None = None,
|
|
) -> tuple[Flag, ...]:
|
|
"""All flags, sorted by kind, doc, subject. A document the change set edits gets
|
|
no touched flag; it still gets dead flags for removed or renamed paths. A modified
|
|
path most documents mention (see _boilerplate_paths) raises no touched flag. With
|
|
repo_files, a basename matches only when it is unique across the repository. With
|
|
unguided_scope, only those documents are checked for unguided; None checks all."""
|
|
changes = tuple(changes)
|
|
changed_paths = frozenset(c.path for c in changes)
|
|
unique = _unique_basenames(changes, repo_files)
|
|
changed_names = names_added | names_removed
|
|
mentions = {doc: doc_mentions(text, doc) for doc, text in doc_texts.items()}
|
|
boilerplate = _boilerplate_paths(mentions, changes, unique)
|
|
|
|
flags: list[Flag] = []
|
|
for doc, m in mentions.items():
|
|
doc_changed = doc in changed_paths
|
|
flags.extend(_path_flags(doc, m, changes, unique, doc_changed, boilerplate))
|
|
if not doc_changed:
|
|
flags.extend(
|
|
Flag("touched", doc, n, "name changed")
|
|
for n in sorted(m.names & changed_names)
|
|
)
|
|
for c in changes:
|
|
if c.status == "A" and _in_source_dirs(c.path, source_dirs):
|
|
if not any(_mentions_path(m, c.path, unique) for m in mentions.values()):
|
|
flags.append(Flag("uncovered", None, c.path, "new file"))
|
|
flags.extend(_changed_input_flags(generated_texts, changes, unique, changed_paths))
|
|
flags.extend(_unguided_flags(doc_texts, guide, unguided_scope))
|
|
return tuple(sorted(set(flags), key=_sort_key))
|
|
|
|
|
|
def whole_repo_changes(
|
|
repo_files: Iterable[str], source_dirs: tuple[str, ...]
|
|
) -> tuple[Change, ...]:
|
|
"""Treat every repository file as added, so uncovered finds all unlisted sources."""
|
|
return tuple(Change("A", p, None) for p in sorted(repo_files))
|
|
|
|
|
|
def _group_key(path: str) -> tuple[str, str]:
|
|
directory, base = posixpath.split(path)
|
|
head, sep, _ = base.partition("_")
|
|
return directory, (head + sep) if sep else base
|
|
|
|
|
|
def group_uncovered(flags: Iterable[Flag]) -> tuple[Flag, ...]:
|
|
"""Collapse uncovered files sharing a directory and filename prefix."""
|
|
groups: dict[tuple[str, str], list[Flag]] = defaultdict(list)
|
|
out: list[Flag] = []
|
|
for f in flags:
|
|
if f.kind == "uncovered":
|
|
groups[_group_key(f.subject)].append(f)
|
|
else:
|
|
out.append(f)
|
|
for (directory, prefix), members in groups.items():
|
|
if len(members) == 1:
|
|
out.append(Flag("uncovered", None, members[0].subject, "1 file"))
|
|
else:
|
|
subject = posixpath.join(directory, prefix + "*") if directory else prefix + "*"
|
|
out.append(Flag("uncovered", None, subject, f"{len(members)} files"))
|
|
return tuple(sorted(out, key=_sort_key))
|