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>
73 lines
2.8 KiB
Python
73 lines
2.8 KiB
Python
"""Paths and names a document mentions, resolved relative to the document."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import posixpath
|
|
import re
|
|
from dataclasses import dataclass
|
|
|
|
# Each directory alternative is fixed by its first two characters (./ or ../, else an
|
|
# optional dot then a word character), so a run of ../ matches only one way and cannot
|
|
# backtrack exponentially. The optional dot keeps .gitea/ and .github/ directories.
|
|
_PATH_TOKEN = re.compile(
|
|
r"(?<![\w/.-])((?:\.\.?/|\.?[\w-][\w.-]*/)*[\w.-]+"
|
|
r"\.(?:R|r|py|sh|qmd|md|Rmd|yml|yaml|toml|sql|mjs|js|json|csv))\b"
|
|
)
|
|
# A directory is a run of segments ending in a slash, written as the whole of a code span
|
|
# or as a link target: bare prose such as "and/or" never counts. At least one segment
|
|
# must start with a word character (after an optional dot), so `../` alone is not one.
|
|
_DIR_BODY = r"((?:\.\.?/)*(?:\.?[\w-][\w.-]*/)+)"
|
|
_DIR_TOKEN = re.compile(rf"`{_DIR_BODY}`|\]\({_DIR_BODY}\)")
|
|
_BACKTICK_NAME = re.compile(r"`([A-Za-z.][\w.]{3,})(?:\(\))?`")
|
|
_CALL_NAME = re.compile(r"\b([A-Za-z.][\w.]{3,})\(")
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class Mentions:
|
|
paths: frozenset[str]
|
|
basenames: frozenset[str]
|
|
names: frozenset[str]
|
|
dirs: frozenset[str] = frozenset()
|
|
|
|
|
|
def _resolve(token: str, doc_dir: str) -> str | None:
|
|
"""Normalize a path relative to a directory; None if it leaves the repository."""
|
|
resolved = posixpath.normpath(posixpath.join(doc_dir, token))
|
|
return None if resolved == ".." or resolved.startswith("../") else resolved
|
|
|
|
|
|
def _resolve_both(token: str, doc_dir: str) -> set[str]:
|
|
"""The token as written and resolved against doc_dir, where each stays inside."""
|
|
out = set()
|
|
for base in ("", doc_dir):
|
|
resolved = _resolve(token, base)
|
|
if resolved is not None:
|
|
out.add(resolved)
|
|
return out
|
|
|
|
|
|
def doc_mentions(text: str, doc_path: str) -> Mentions:
|
|
"""What the document refers to.
|
|
|
|
Each path token counts as written (when it stays inside the repository) and as
|
|
resolved against the document's own directory. Directory tokens (a trailing slash,
|
|
in backticks or a link target) resolve the same way, without the slash; one that
|
|
resolves to the repository root is dropped. Names count only in backticks or
|
|
followed by an opening parenthesis.
|
|
"""
|
|
doc_dir = posixpath.dirname(doc_path)
|
|
paths: set[str] = set()
|
|
for token in _PATH_TOKEN.findall(text):
|
|
paths |= _resolve_both(token, doc_dir)
|
|
dirs: set[str] = set()
|
|
for code, link in _DIR_TOKEN.findall(text):
|
|
dirs |= _resolve_both(code or link, doc_dir)
|
|
dirs.discard(".")
|
|
names = set(_BACKTICK_NAME.findall(text)) | set(_CALL_NAME.findall(text))
|
|
return Mentions(
|
|
paths=frozenset(paths),
|
|
basenames=frozenset(posixpath.basename(p) for p in paths),
|
|
names=frozenset(names),
|
|
dirs=frozenset(dirs),
|
|
)
|