blob: efa8d2b4cf68a3861938545001c2fd38978a4083 [file]
#!/usr/bin/env python3
#
# Copyright 2026 The Fuchsia Authors
#
# Licensed under a BSD-style license <LICENSE-BSD>, Apache License, Version 2.0
# <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>, or the MIT
# license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option.
# This file may not be copied, modified, or distributed except according to
# those terms.
"""Fail fast on mechanical PR-hygiene defects before human or LLM review.
Checks the commits and diff in `merge_base(--base, --head)..--head` plus the
tree at `--head`:
- `commit-message`: subject and 72-column body layout, well-formed
`Agent-authored-by:` trailers (at most one per user, in the final trailer
block), and no `gherrit-pr-id` lines or review-round IDs. Only commits
touching Anneal paths are checked, unless `--skip-commit-messages` (for
PRs whose authenticated author is a bot) skips them.
- `commit-author` (`--expected-author`): each in-scope commit's author and
committer.
- `diff-check`: `git diff --check` on Anneal paths.
- `wrap`: added Markdown and comment lines that exceed 80 columns even though
wrapping could fix them. Inline code spans split across a line wrap are
reported as warnings.
- `header`: the Fuchsia copyright and license header on new Anneal files.
- `review-round-id`: review-round IDs in added comments and Markdown (code
may carry them as data, such as Gate 2 finding-ID fixtures).
- `trusted-citation`: unpinned or abbreviated `rust-lang/{rust,reference}`
citations and rendered-documentation links in `trusted/lean/` and
`trusted/rustc-capture/`.
- `agents-budget` and `code-review-rules-parity`: the cumulative `AGENTS.md`
byte budget and the verbatim `Code Review Rules` mirror in
`.gemini/styleguide.md`.
- `skill-hygiene`: public-skill lexicon and leak hygiene across `SKILL.md`
files under `skills/` and `trusted/cargo-anneal/` (Scope 1), plus
installed-bundle path and lexicon checks on bundled user-facing skills
(Scope 2). Non-waivable.
- `rustc-workaround`: malformed or unresolved (`#PENDING`) upstream `rustc`
workaround anchors in Anneal source files at `--head` (see
`docs/rustc-workarounds.md`; `ci/audit_rustc_workarounds.py` prints the
anchor inventory).
- `third-party-notices`: every committed third-party root (`vendor/<name>/`,
`site/static/fonts/<name>/`, and non-root third-party license/notice files
outside `docs/` and `site/templates/`) has a dedicated `### `<root>/``
heading in `THIRD_PARTY_NOTICES.md`. Non-waivable.
- `unstable-review-date`: 90-day `review_by` horizon on newly set or updated
`docs/unstable/<id>.toml` entries and diff-scoped blocking of expired
unstable features.
- `experimental-size`, `experimental-expiry`: 80-file / 5,000-line cap (pure
deletions exempt) and 45-day `expires_on` bound on
`experimental/<slug>/EXPERIMENT.toml`. Non-waivable.
- `pr-body`, `pr-title` (`--pr-body-file`, `--pr-title`): a single-commit PR's
description and title match its commit message.
- `receipt` (`--receipt-file`): a Review Receipt has a `Human review surface`
block and one `anneal-receipt:v1` JSON footer that binds the checked
comparison tuple and tree and lists every changed review-surface path
(`--emit-receipt-footer` prints it).
- `pr-title-policy` (`--forbid-pr-title-regex`): the PR title, which becomes
the squash commit subject of a multi-commit PR, does not match a regex
that the merge queue would reject anyway.
- `skip-pr-hygiene`: waiver trailers are well formed and used.
- `acknowledgments` (advisory): a soft warning, never a finding, when a PR
introduces new modules, crates, tools, assets, pins, or net-new external
paper/GitHub URLs without updating `ACKNOWLEDGMENTS.md`.
- `review-size` (advisory): a warning, never a finding, when more than
`REVIEW_SIZE_BUDGET` changed lines remain after excluding generated, lock,
vendored, binary, and machine-checked fixture proof files; such changes
should be split into a linear stack of PRs. `--json` reports the counts
as `review_size`.
The PR's tip commit may waive every finding of some checks with one or more
single-line trailers in its final trailer block:
Skip-pr-hygiene: <check>[,<check>...] -- <reason>
Only `commit-message`, `diff-check`, `wrap`, `header`, `review-round-id`,
and `trusted-citation` are waivable; there is no wildcard. The reason needs
at least five non-whitespace characters, every named check must waive at
least one finding, and no other commit in the range may carry the trailer,
because squash merges keep every commit message. Waived findings are
reported as `::warning::` annotations and, when `GITHUB_STEP_SUMMARY` is
set, in a step summary table.
The script only reads Git objects; it never checks out, builds, or executes
candidate content. Reviewers can therefore run a trusted base copy of this
script against an untrusted candidate head. Without `--base`, only the tree
checks run. Exits 0 when no unwaived findings remain, 1 otherwise, and 2 on
usage or Git errors.
"""
from __future__ import annotations
import argparse
import codecs
from dataclasses import dataclass, field
import datetime
import html
import io
import json
import os
from pathlib import Path, PurePosixPath
import re
import subprocess
import sys
import tokenize
import tomllib
from typing import Callable
SCHEMA = "anneal-pr-hygiene/v1"
SUBJECT_LIMIT = 72
BODY_LIMIT = 72
PROSE_LIMIT = 80
AGENTS_BUDGET = 32768
DEFAULT_GLOBAL_AGENTS_BYTES = "1024"
ANNEAL = ""
# Pinned upstream content keeps its own conventions.
EXCLUDED_PREFIXES = ("vendor/",)
# Commits touching non-excluded paths follow Anneal's commit-message rules.
COMMIT_SCOPE_PREFIXES = (ANNEAL,)
CRR_BEGIN = "<!-- code-review-rules:begin -->"
CRR_END = "<!-- code-review-rules:end -->"
CRR_FILES = ("AGENTS.md", ".gemini/styleguide.md")
SHA40_RE = re.compile(r"(?<![0-9a-f])[0-9a-f]{40}(?![0-9a-f])")
REVIEW_ROUND_RE = re.compile(r"\bG\d-R\d+\b")
GHERRIT_RE = re.compile(r"^\s*gherrit-pr-id\s*:", re.IGNORECASE)
GITHUB_SQUASH_SUFFIX_RE = re.compile(r" \(#\d+\)$")
AGENT_TRAILER_KEY_RE = re.compile(r"^\s*agent-authored-by\s*:", re.IGNORECASE)
AGENT_TRAILER_RE = re.compile(
r"^Agent-authored-by: AI agent acting on (\S(?:.*\S)?)'s behalf"
r"(?: \((content only|commit message only)\))?$"
)
TRAILER_RE = re.compile(r"^[A-Za-z][A-Za-z0-9-]*:[ \t]+\S")
WAIVER_KEY = "Skip-pr-hygiene"
# Any spelling, so a miscased or indented waiver fails instead of being
# ignored.
WAIVER_KEY_RE = re.compile(r"^\s*skip-pr-hygiene\s*:", re.IGNORECASE)
# Checks a waiver may name. Tree invariants, commit identities, PR metadata,
# receipts, and the waiver check itself are never waivable.
WAIVABLE_CHECKS = (
"commit-message", "diff-check", "wrap", "header", "review-round-id",
"trusted-citation",
)
WAIVER_MIN_REASON = 5
FENCE_RE = re.compile(r"^([ \t]*)(`{3,}|~{3,})(.*)$")
def _fence(line: str) -> re.Match[str] | None:
"""Match a code fence line; a backtick fence's info has no backticks.
Without that CommonMark rule, a line starting with an inline span such
as ```` ```x``` ```` would open a fence that hides the rest of a file.
"""
m = FENCE_RE.match(line)
if m and m.group(2)[0] == "`" and "`" in m.group(3):
return None
return m
TABLE_DELIM_RE = re.compile(
r"^\s*\|?\s*:?-{3,}:?\s*(?:\|\s*:?-{3,}:?\s*)+\|?\s*$"
)
URL_SCHEME_RE = re.compile(r"[A-Za-z][A-Za-z0-9+.-]*://")
HEADER_RE = re.compile(
r"Copyright (\d{4}) The Fuchsia Authors Licensed under a BSD-style license "
r"<LICENSE-BSD>, Apache License, Version 2\.0 <LICENSE-APACHE or "
r"https://www\.apache\.org/licenses/LICENSE-2\.0>, or the MIT license "
r"<LICENSE-MIT or https://opensource\.org/licenses/MIT>, at your option\. "
r"This file may not be copied, modified, or distributed except according "
r"to those terms\."
)
# File kinds whose comments (or, for Markdown, prose) follow the 80-column
# rule, and which need the license header when new.
HASH_COMMENT_EXTS = {".py", ".sh", ".toml", ".yml", ".yaml", ".nix"}
SLASH_COMMENT_EXTS = {".rs"}
LEAN_EXTS = {".lean"}
MARKDOWN_EXTS = {".md"}
HEADER_EXTS = (
HASH_COMMENT_EXTS | SLASH_COMMENT_EXTS | LEAN_EXTS | MARKDOWN_EXTS
)
# Files whose format forbids a leading comment or that are rendered verbatim
# as a package README.
HEADER_EXEMPT_NAMES = {"SKILL.md", "CLAUDE.md"}
HEADER_EXEMPT_PATHS = {"README.md"}
# The package README keeps one source line per paragraph.
WRAP_EXEMPT_PATHS = {"README.md"}
LICENSE_LINE_RE = re.compile(
r"Copyright \d{4} The Fuchsia Authors|<LICENSE-(?:BSD|APACHE|MIT)"
r"|This file may not be copied, modified, or distributed"
)
TRUSTED_CITATION_PREFIXES = (
"trusted/lean/",
"trusted/rustc-capture/",
)
UPSTREAM_LINK_RE = re.compile(
r"github\.com/rust-lang/(rust|reference)/(?:blob|tree|raw|commit|blame)/"
r"([^/\s)>\]`'\"]+)"
)
UPSTREAM_RAW_RE = re.compile(
r"raw\.githubusercontent\.com/rust-lang/(rust|reference)/([^/\s)>\]`'\"]+)"
)
RENDERED_DOCS_RE = re.compile(r"\b(doc\.rust-lang\.org|rust-lang\.github\.io)/")
ABBREVIATED_CITE_RE = re.compile(
r"rust-lang/(?:rust|reference)(?:@|`?\s+(?:at\s+)?(?:commit\s+)?`?)"
r"([0-9a-f]{7,39})(?![0-9a-zA-Z])"
)
# Human review surface paths (Code Review Rules), relative to `anneal/`.
HRS_ANNEAL_PREFIXES = ("trusted/", "ci/", ".github/", "skills/", "vendor/")
HRS_ANNEAL_FILES = {
"Cargo.toml", "Cargo.lock", "flake.nix", "flake.lock",
"META_PRINCIPLES.md", "PRINCIPLES.md", "THREAT_MODEL.md",
"docs/trust-model.md", "docs/codex-review.md",
"docs/review-policy-design.md", "docs/rustc-workarounds.md",
"experimental/README.md",
"theory/bundle-manifest.json",
}
# Zerocopy root paths that Anneal audits as part of its boundary, including
# the root `ci/` that holds trusted CI configuration such as
# `ci/checks.toml`.
HRS_ROOT_PREFIXES = (".github/", "skills/", "ci/")
# Editor, agent, toolchain, and build configs anywhere in the repository.
HRS_CONFIG_DIRS = {
".agents", ".claude", ".gemini", ".codex", ".cursor", ".vscode",
".devcontainer", ".idea", ".zed", ".cargo",
}
HRS_CONFIG_NAMES = {
"AGENTS.md", "AGENTS.override.md", "CLAUDE.md", "GEMINI.md",
"lean-toolchain", "lake-manifest.json", ".mcp.json", ".cursorrules",
".windsurfrules", ".clinerules", ".envrc", ".gitattributes",
".gitmodules",
}
TRUST_TIER_RE = re.compile(r"--\s*anneal-trust-tier:")
# Advisory review-size budget, in changed (added plus deleted) lines. Review
# rounds grow with PR size, and a finding anywhere in a PR blocks all of it,
# so large changes should land as a linear stack of smaller PRs.
REVIEW_SIZE_BUDGET = 800
# Changed lines that reviewers do not read line by line and that the budget
# therefore excludes. These mirror Gate 2's classification: regenerated
# artifacts, lock files, pinned upstream content, and proof modules under
# machine-checked fixture roots. Fixture files are excluded by location
# alone; whether Gate 2 actually reviews one as fixture-light also depends
# on its content.
SIZE_GENERATED_PATHS = frozenset({
"trusted/cargo-anneal/src/bundled_resources.rs",
"trusted/root-manifest.sha256",
})
SIZE_GENERATED_NAMES = frozenset({"bundled_theory.json"})
SIZE_LOCK_NAMES = frozenset({
"Cargo.lock", "flake.lock", "lake-manifest.json", "foundation-lock.json",
})
FIXTURE_ROOTS = (
"tests/cargo-anneal/tests/fixtures/",
"tests/corpora/scalar_specs/",
"zerocopy/proofs/",
)
FIXTURE_NAME_RE = re.compile(r"^[A-Za-z0-9_]*(?:Proof|Lemmas)\.lean$")
FIXTURE_NEVER_SEGMENT_RE = re.compile(
r"(?:^|/)(?:[^/]*Spec[^/]*|[^/]*Expected[^/]*|ci|\.github|\.gemini"
r"|skills|trusted)(?:/|$)"
)
FIXTURE_CORPORA_RE = re.compile(
r"^tests/corpora/scalar_specs/[^/]+/Proof\.lean$")
FIXTURE_ZEROCOPY_RE = re.compile(r"^zerocopy/proofs/[^/]+/[^/]+\.lean$")
SIZE_CATEGORIES = (
"generated", "lock", "vendor", "fixture", "experimental", "binary",
)
EXPERIMENTAL_MAX_FILES = 80
EXPERIMENTAL_MAX_LINES = 5000
EXPERIMENTAL_MAX_EXPIRY_DAYS = 45
UNSTABLE_MAX_REVIEW_DAYS = 90
_EXP_TOML_RE = re.compile(r"^experimental/[^/]+/EXPERIMENT\.toml$")
_UNSTABLE_TOML_RE = re.compile(r"^docs/unstable/([a-z][a-z0-9_]*)\.toml$")
_ISO_DATE_RE = re.compile(r"^\d{4}-\d{2}-\d{2}$")
_UNSTABLE_DIR_PREFIXES = (
"theory/Anneal/Unstable/",
"Specs/Unstable/",
"specs/Specs/Unstable/",
)
_UNSTABLE_RUST_CODE_SITES: dict[str, str] = {
"trusted/cargo-anneal/src/unstable.rs": "UNSTABLE_FEATURES",
"tools/cargo-anneal/cargo-anneal/src/unstable.rs": "UNSTABLE_FEATURES",
"tools/cargo-anneal/src/unstable.rs": "UNSTABLE_FEATURES",
"trusted/rustc-capture/src/lib.rs": "CAPTURE_UNSTABLE_FEATURES",
"trusted/mir-dump/src/lib.rs": "CAPTURE_UNSTABLE_FEATURES",
"trusted/anneal-lower/src/lib.rs": "CAPTURE_UNSTABLE_FEATURES",
"tools/anneal-spec/lower/src/parse.rs": "PREVIEW_UNSTABLE_FEATURES",
"tools/anneal-spec/lower/src/lib.rs": "PREVIEW_UNSTABLE_FEATURES",
}
_UNSTABLE_CODE_SITE_FILES = frozenset(
{"trusted/locks/cartridges.lock.json", *_UNSTABLE_RUST_CODE_SITES}
)
_TRUST_TIER_CANDIDATE_RE = re.compile(
r"--\s*anneal-trust-tier:\s*candidate:([a-z][a-z0-9_]*)\b"
)
def _cartridge_stability_snapshot(
content: str | None,
) -> tuple[bool, list[tuple[str, object]] | None]:
"""Extract sorted `(path, stability_value)` pairs structurally from JSON.
Returns `(True, None)` when absent, `(False, None)` on malformed JSON or
duplicate keys (fail closed), and `(True, bindings)` otherwise.
"""
if content is None:
return True, None
def _reject_dupes(pairs: list[tuple[str, object]]) -> dict[str, object]:
out: dict[str, object] = {}
for k, v in pairs:
if k in out:
raise ValueError(f"duplicate JSON key `{k}`")
out[k] = v
return out
try:
data = json.loads(content, object_pairs_hook=_reject_dupes)
except (json.JSONDecodeError, ValueError, TypeError):
return False, None
if not isinstance(data, dict):
return False, None
bindings: list[tuple[str, object]] = []
def _walk(obj: object, prefix: str) -> None:
if isinstance(obj, dict):
for k, v in obj.items():
if k == "stability":
bindings.append(
(f"{prefix}/stability", json.dumps(v, sort_keys=True))
)
elif isinstance(v, (dict, list)):
_walk(v, f"{prefix}/{k}")
elif isinstance(obj, list):
for idx, item in enumerate(obj):
if (
prefix.endswith("/cartridges")
and isinstance(item, dict)
and isinstance(item.get("id"), str)
):
_walk(item, f"{prefix}[id={item['id']}]")
elif isinstance(item, (dict, list)):
_walk(item, f"{prefix}[{idx}]")
_walk(data, "")
bindings.sort()
return True, bindings
def _cartridges_lock_changed_candidates(
git: Git,
merge_base: str,
head: str,
files: dict[str, ChangedFile],
) -> tuple[bool, set[str], dict[str, tuple[str, ...]]]:
"""Inspect `trusted/locks/cartridges.lock.json` structurally.
Returns `(fail_closed_all, changed_candidate_fids, candidate_lean_prefixes)`:
- `changed_candidate_fids` contains every `candidate:<id>` whose lock entry
was added, removed, or modified in any field (regardless of `-U0` diff
context) when the lock file itself was touched by the PR.
- `candidate_lean_prefixes` maps every `candidate:<id>` declared in either
`merge_base` or `head` to its `trusted/lean/<Namespace>/` path prefixes
so edits to candidate Lean cartridges under `trusted/lean/` are detected
even when `cartridges.lock.json` is untouched.
"""
target = "trusted/locks/cartridges.lock.json"
lock_touched = any(
f.path == target or f.old_path == target for f in files.values()
)
def _parse_lock(
content: str | None,
) -> tuple[bool, dict[str, str], dict[str, set[str]]]:
if content is None:
return True, {}, {}
def _reject_dupes(pairs: list[tuple[str, object]]) -> dict[str, object]:
seen: dict[str, object] = {}
for k, v in pairs:
if k in seen:
raise ValueError(f"duplicate JSON key `{k}`")
seen[k] = v
return seen
try:
data = json.loads(content, object_pairs_hook=_reject_dupes)
except (json.JSONDecodeError, ValueError, TypeError):
return False, {}, {}
if not isinstance(data, dict):
return False, {}, {}
carts = data.get("cartridges")
if not isinstance(carts, list):
return False, {}, {}
out: dict[str, str] = {}
prefixes: dict[str, set[str]] = {}
for idx, entry in enumerate(carts):
if not isinstance(entry, dict):
return False, {}, {}
key = entry.get("id")
ekey = f"id:{key}" if isinstance(key, str) else f"idx:{idx}"
if ekey in out:
return False, {}, {}
out[ekey] = json.dumps(entry, sort_keys=True)
stab = entry.get("stability")
ns = entry.get("namespace_prefix")
if (
isinstance(stab, str)
and stab.startswith("candidate:")
and isinstance(ns, str)
and ns
):
fid = stab[len("candidate:"):]
rel_ns = ns.replace(".", "/")
prefixes.setdefault(fid, set()).update((
f"trusted/lean/{rel_ns}/",
f"trusted/lean/{rel_ns}.lean",
f"trusted/correspondence/{rel_ns}/",
f"trusted/correspondence/{rel_ns}.lean",
))
return True, out, prefixes
def _cand_fid(serialized: str) -> str | None:
entry = json.loads(serialized)
stab = entry.get("stability")
if isinstance(stab, str) and stab.startswith("candidate:"):
return stab[len("candidate:"):]
return None
base_ok, base_map, base_prefixes = _parse_lock(
git.blob_text(merge_base, target)
)
head_ok, head_map, head_prefixes = _parse_lock(git.blob_text(head, target))
merged_prefixes: dict[str, tuple[str, ...]] = {}
for fid in set(base_prefixes) | set(head_prefixes):
merged_prefixes[fid] = tuple(
sorted(base_prefixes.get(fid, set()) | head_prefixes.get(fid, set()))
)
if not lock_touched:
return False, set(), merged_prefixes
if not base_ok or not head_ok:
return True, set(), merged_prefixes
changed_fids: set[str] = set()
for ekey in set(base_map) | set(head_map):
b_val = base_map.get(ekey)
h_val = head_map.get(ekey)
if b_val != h_val:
for raw in (b_val, h_val):
if raw is not None:
fid = _cand_fid(raw)
if fid:
changed_fids.add(fid)
return False, changed_fids, merged_prefixes
def _trusted_lean_touched_candidates(
git: Git,
merge_base: str,
head: str,
files: dict[str, ChangedFile],
) -> set[str]:
"""Find `candidate:<id>` trust-tier headers in modified `.lean` files."""
touched_fids: set[str] = set()
for f in files.values():
candidates_to_read: list[tuple[str, str]] = []
if f.path.endswith(".lean"):
if f.status != "D":
candidates_to_read.append((head, f.path))
if f.status in ("M", "D"):
candidates_to_read.append((merge_base, f.path))
if f.old_path is not None and f.old_path.endswith(".lean"):
candidates_to_read.append((merge_base, f.old_path))
for rev, rel in candidates_to_read:
blob = git.blob_text(rev, rel)
if blob is None:
continue
for m in _TRUST_TIER_CANDIDATE_RE.finditer(blob):
touched_fids.add(m.group(1))
return touched_fids
def _decode_rust_string_body(raw_inner: str) -> str:
"""Decode Rust string escape sequences identically to registry checks."""
out: list[str] = []
i = 0
n = len(raw_inner)
while i < n:
if raw_inner[i] == "\\" and i + 1 < n:
esc = raw_inner[i + 1]
if esc == "\n":
i += 2
while i < n and raw_inner[i] in " \t\r":
i += 1
continue
if esc == "x" and i + 3 < n:
try:
out.append(chr(int(raw_inner[i + 2 : i + 4], 16)))
i += 4
continue
except ValueError:
pass
if esc == "u" and i + 2 < n and raw_inner[i + 2] == "{":
close = raw_inner.find("}", i + 3)
if close > i + 3:
hex_str = raw_inner[i + 3 : close].replace("_", "")
try:
out.append(chr(int(hex_str, 16)))
i = close + 1
continue
except ValueError:
pass
out.append(
{
"n": "\n",
"r": "\r",
"t": "\t",
"0": "\0",
"\\": "\\",
'"': '"',
"'": "'",
}.get(esc, esc)
)
i += 2
continue
out.append(raw_inner[i])
i += 1
return "".join(out)
def _lex_rust_table_tokens(text: str) -> list[tuple[str, str]]:
"""Lightweight Rust tokenizer stripping comments and keeping strings/chars."""
tokens: list[tuple[str, str]] = []
i = 0
n = len(text)
while i < n:
ch = text[i]
if ch.isspace():
i += 1
continue
if ch == "/" and i + 1 < n and text[i + 1] == "/":
i += 2
while i < n and text[i] != "\n":
i += 1
continue
if ch == "/" and i + 1 < n and text[i + 1] == "*":
depth = 1
i += 2
while i < n and depth > 0:
if text[i] == "/" and i + 1 < n and text[i + 1] == "*":
depth += 1
i += 2
elif text[i] == "*" and i + 1 < n and text[i + 1] == "/":
depth -= 1
i += 2
else:
i += 1
continue
# Raw string literal `r#*"..."#*` / `br#*"..."#*` / `cr#*"..."#*`.
rpos = i + 1 if ch in ("b", "c") and i + 1 < n and text[i + 1] == "r" else i
if rpos < n and text[rpos] == "r":
p = rpos + 1
hashes = 0
while p < n and text[p] == "#":
hashes += 1
p += 1
if p < n and text[p] == '"':
closing = '"' + "#" * hashes
end = text.find(closing, p + 1)
if end < 0:
tokens.append(("STR", text[p + 1 :]))
break
tokens.append(("STR", text[p + 1 : end]))
i = end + len(closing)
continue
if ch in ("b", "c") and i + 1 < n and text[i + 1] == '"':
j = i + 2
while j < n:
if text[j] == "\\":
j += 2
continue
if text[j] == '"':
tokens.append(
("STR", _decode_rust_string_body(text[i + 2 : j]))
)
j += 1
break
j += 1
i = j
continue
if ch == '"':
j = i + 1
while j < n:
if text[j] == "\\":
j += 2
continue
if text[j] == '"':
tokens.append(
("STR", _decode_rust_string_body(text[i + 1 : j]))
)
j += 1
break
j += 1
i = j
continue
if ch == "b" and i + 1 < n and text[i + 1] == "'":
j = i + 2
if j < n and text[j] == "\\":
j += 2
while j < n and text[j] != "'" and text[j] != "\n":
j += 1
if j < n and text[j] == "'":
tokens.append(("CHAR", text[i : j + 1]))
i = j + 1
continue
elif j + 1 < n and text[j + 1] == "'" and text[j] != "\n":
tokens.append(("CHAR", text[i : j + 2]))
i = j + 2
continue
if ch == "'" and i + 1 < n:
if text[i + 1] == "\\":
j = i + 3
while j < n and text[j] != "'" and text[j] != "\n":
j += 1
if j < n and text[j] == "'":
tokens.append(("CHAR", text[i : j + 1]))
i = j + 1
continue
elif i + 2 < n and text[i + 2] == "'" and text[i + 1] != "\n":
tokens.append(("CHAR", text[i : i + 3]))
i += 3
continue
j = i + 1
while j < n and (text[j].isalnum() or text[j] == "_"):
j += 1
tokens.append(("OTHER", text[i:j]))
i = j
continue
if ch == "r" and i + 1 < n and text[i + 1] == "#":
j = i + 2
while j < n and (text[j].isalnum() or text[j] == "_"):
j += 1
if j > i + 2:
tokens.append(("RAW_IDENT", text[i + 2 : j]))
i = j
continue
if ch.isalpha() or ch == "_":
j = i + 1
while j < n and (text[j].isalnum() or text[j] == "_"):
j += 1
tokens.append(("IDENT", text[i:j]))
i = j
continue
if ch == ":" and i + 1 < n and text[i + 1] == ":":
tokens.append(("PUNCT", "::"))
i += 2
continue
tokens.append(("PUNCT", ch))
i += 1
return tokens
def _parse_rust_feature_entries(
content: str | None, const_name: str
) -> tuple[bool, dict[str, tuple[tuple[str, str], ...]]]:
"""Map feature ID -> normalized element tokens for a Rust feature table."""
if content is None:
return True, {}
tokens = _lex_rust_table_tokens(content)
n = len(tokens)
entries: dict[str, tuple[tuple[str, str], ...]] = {}
i = 0
while i < n:
name_idx: int | None = None
if tokens[i] == ("IDENT", "const") and i + 1 < n:
name_idx = i + 1
elif tokens[i] == ("IDENT", "static") and i + 1 < n:
name_idx = (
i + 2
if tokens[i + 1] == ("IDENT", "mut") and i + 2 < n
else i + 1
)
if (
name_idx is not None
and tokens[name_idx][0] in ("IDENT", "RAW_IDENT")
and tokens[name_idx][1] == const_name
):
j = name_idx + 1
depth = 0
eq_idx: int | None = None
while j < n:
tok = tokens[j]
if tok[0] == "PUNCT" and tok[1] in "([{<":
depth += 1
elif tok[0] == "PUNCT" and tok[1] in ")]}>":
depth = max(0, depth - 1)
elif depth == 0 and tok == ("PUNCT", "="):
eq_idx = j
break
elif depth == 0 and tok == ("PUNCT", ";"):
break
j += 1
if eq_idx is None:
return False, {}
pos = eq_idx + 1
if pos < n and tokens[pos] == ("PUNCT", "&"):
pos += 1
if pos >= n or tokens[pos] != ("PUNCT", "["):
return False, {}
bdepth = 1
k = pos + 1
while k < n and bdepth > 0:
if tokens[k] == ("PUNCT", "["):
bdepth += 1
elif tokens[k] == ("PUNCT", "]"):
bdepth -= 1
k += 1
if bdepth != 0:
return False, {}
body = tokens[pos + 1 : k - 1]
# Split top-level comma-separated elements.
elems: list[list[tuple[str, str]]] = []
cur: list[tuple[str, str]] = []
edepth = 0
for tok in body:
if tok[0] == "PUNCT" and tok[1] in "([{":
edepth += 1
cur.append(tok)
elif tok[0] == "PUNCT" and tok[1] in ")]}":
edepth = max(0, edepth - 1)
cur.append(tok)
elif edepth == 0 and tok == ("PUNCT", ","):
if cur:
elems.append(cur)
cur = []
else:
cur.append(tok)
if cur:
elems.append(cur)
for elem in elems:
fid: str | None = None
if len(elem) == 1 and elem[0][0] == "STR":
fid = elem[0][1]
elif elem[-1] == ("PUNCT", "}"):
fdepth = 0
open_b: int | None = None
for idx in range(len(elem) - 1, -1, -1):
if elem[idx] == ("PUNCT", "}"):
fdepth += 1
elif elem[idx] == ("PUNCT", "{"):
fdepth -= 1
if fdepth == 0:
open_b = idx
break
if open_b is not None:
inner = elem[open_b + 1 : -1]
sdepth = 0
fstart = 0
for idx, tok in enumerate(inner + [("PUNCT", ",")]):
if tok[0] == "PUNCT" and tok[1] in "([{":
sdepth += 1
elif tok[0] == "PUNCT" and tok[1] in ")]}":
sdepth = max(0, sdepth - 1)
elif sdepth == 0 and tok == ("PUNCT", ","):
fld = inner[fstart:idx]
if (
len(fld) == 3
and fld[0][0] in ("IDENT", "RAW_IDENT")
and fld[0][1] == "id"
and fld[1] == ("PUNCT", ":")
and fld[2][0] == "STR"
):
fid = fld[2][1]
break
fstart = idx + 1
if fid is None or fid in entries:
return False, {}
entries[fid] = tuple(elem)
i = k
continue
i += 1
return True, entries
def _rust_code_site_changed_candidates(
git: Git,
merge_base: str,
head: str,
files: dict[str, ChangedFile],
) -> tuple[bool, set[str]]:
"""Detect added, removed, or modified entries in monitored Rust tables."""
changed_fids: set[str] = set()
for rel, const_name in _UNSTABLE_RUST_CODE_SITES.items():
if not any(f.path == rel or f.old_path == rel for f in files.values()):
continue
base_ok, base_map = _parse_rust_feature_entries(
git.blob_text(merge_base, rel), const_name
)
head_ok, head_map = _parse_rust_feature_entries(
git.blob_text(head, rel), const_name
)
if not base_ok or not head_ok:
return True, set()
for fid in set(base_map) | set(head_map):
if base_map.get(fid) != head_map.get(fid):
changed_fids.add(fid)
return False, changed_fids
THIRD_PARTY_NOTICES_PATH = "THIRD_PARTY_NOTICES.md"
# Fuchsia-copyrighted root licenses whose blob SHAs identify first-party copies;
# `LICENSE-APACHE` is generic ASF text and is only exempt when paired with one
# of these in the same directory or inside an explicit first-party crate dir.
FIRST_PARTY_COPYRIGHTED_LICENSE_PATHS = (
"LICENSE-BSD",
"LICENSE-MIT",
)
GENERIC_FIRST_PARTY_LICENSE_PATHS = ("LICENSE-APACHE",)
FIRST_PARTY_LICENSE_DIRS = frozenset({"trusted/cargo-anneal"})
THIRD_PARTY_LICENSE_NAME_RE = re.compile(
r"^(?:OFL(?:[._-][A-Za-z0-9._-]+)?"
r"|(?:LICEN[CS]E|COPYING|COPYRIGHT|NOTICE|UNLICENSE)"
r"(?:[._-][A-Za-z0-9._-]+)?)$",
re.IGNORECASE,
)
# Exclude code, config, template, and binary extensions instead of allowlisting
# suffixes so dotted versioned license files (`LICENSE-2.0`,
# `LICENSE-APACHE-2.0`, `LICENSE.BSD-3-Clause`, `OFL-1.1.txt`) are not silently
# skipped.
THIRD_PARTY_NON_LICENSE_SUFFIXES = frozenset({
".rs", ".lean", ".py", ".sh", ".bash", ".zsh",
".c", ".cc", ".cpp", ".cxx", ".h", ".hh", ".hpp", ".go",
".js", ".mjs", ".cjs", ".ts", ".tsx", ".jsx",
".css", ".scss", ".html", ".htm", ".xml", ".svg",
".json", ".jsonl", ".toml", ".yaml", ".yml", ".nix", ".lock",
".cmake", ".mk", ".in", ".am", ".ac",
".o", ".a", ".so", ".dylib", ".dll", ".exe", ".wasm",
".png", ".jpg", ".jpeg", ".gif", ".ico", ".pdf",
".ttf", ".otf", ".woff", ".woff2", ".zip", ".tar", ".gz", ".zst",
})
THIRD_PARTY_FALLBACK_EXCLUDED_PREFIXES = ("docs/", "site/templates/")
ACK_PATH = "ACKNOWLEDGMENTS.md"
ACK_ACADEMIC_URL_RE = re.compile(
r"https?://(?:arxiv\.org|doi\.org|dl\.acm\.org|link\.springer\.com|"
r"eprint\.iacr\.org)/[^\s)>\]`'\"]+"
)
ACK_GITHUB_URL_RE = re.compile(
r"https://github\.com/([A-Za-z0-9][A-Za-z0-9_.-]*)/"
r"([A-Za-z0-9][A-Za-z0-9_.-]*)"
)
ACK_GITHUB_ALLOWLIST = frozenset({
"rust-lang/rust",
"rust-lang/reference",
"rust-lang/miri",
"rust-lang/unsafe-code-guidelines",
"leanprover/lean4",
})
ACK_URL_EXTS = frozenset({".rs", ".lean", ".py", ".md", ".css"})
ACK_URL_EXCLUDED_PREFIXES = (
"vendor/",
"tests/cargo-anneal/tests/fixtures/",
"tests/corpora/",
)
ACK_URL_EXCLUDED_PATHS = frozenset({
"ACKNOWLEDGMENTS.md",
"THIRD_PARTY_NOTICES.md",
})
class GitError(Exception):
"""A Git command failed; the check cannot complete."""
@dataclass(frozen=True)
class Finding:
check: str
message: str
path: str | None = None
line: int | None = None
def render(self) -> str:
where = ""
if self.path is not None:
where = self.path if self.line is None else f"{self.path}:{self.line}"
where += ": "
return f"[{self.check}] {where}{self.message}"
def to_json(self) -> dict[str, object]:
return {
"check": self.check,
"path": self.path,
"line": self.line,
"message": self.message,
}
@dataclass(frozen=True)
class OptOut:
"""One `Skip-pr-hygiene:` trailer on the PR's tip commit."""
checks: tuple[str, ...]
reason: str
commit: str
line: int
def to_json(self) -> dict[str, object]:
return {
"checks": list(self.checks),
"reason": self.reason,
"commit": self.commit,
"line": self.line,
}
@dataclass(frozen=True)
class Waived:
finding: Finding
reason: str
def to_json(self) -> dict[str, object]:
return {**self.finding.to_json(), "reason": self.reason}
@dataclass
class Report:
findings: list[Finding] = field(default_factory=list)
warnings: list[Finding] = field(default_factory=list)
waived: list[Waived] = field(default_factory=list)
opt_outs: list[OptOut] = field(default_factory=list)
EMPTY_TREE = "4b825dc642cb6eb9a060e54bf8d69288fbee4904"
# Pin every setting that could hide content, change output formats, or run
# code, so neither ambient configuration nor candidate `.gitattributes` can
# weaken a check. Attributes come from the empty tree.
GIT_OPTIONS = (
"--literal-pathspecs",
"--no-pager",
f"--attr-source={EMPTY_TREE}",
"-c", "core.hooksPath=/dev/null",
"-c", "core.fsmonitor=false",
"-c", "core.quotePath=false",
"-c", "core.attributesFile=/dev/null",
"-c", "core.whitespace=blank-at-eol,blank-at-eof,space-before-tab",
"-c", "diff.noprefix=false",
"-c", "diff.mnemonicPrefix=false",
"-c", "diff.relative=false",
"-c", "color.ui=false",
"-c", "log.showSignature=false",
)
class Git:
"""Read-only access to one repository's objects."""
def __init__(self, repo: Path):
self.repo = repo
self.env = {
k: v for k, v in os.environ.items() if not k.startswith("GIT_")
}
self.env["GIT_CONFIG_NOSYSTEM"] = "1"
self.env["GIT_CONFIG_GLOBAL"] = os.devnull
self.env["LC_ALL"] = "C"
def run(self, *args: str, ok_codes: tuple[int, ...] = (0,),
stdin: bytes | None = None) -> bytes:
res = subprocess.run(
["git", *GIT_OPTIONS, *args], cwd=self.repo, env=self.env,
input=stdin, capture_output=True, check=False,
)
if res.returncode not in ok_codes:
stderr = res.stderr.decode("utf-8", "replace").strip()
raise GitError(f"`git {' '.join(args)}` failed: {stderr}")
return res.stdout
def text(self, *args: str, ok_codes: tuple[int, ...] = (0,)) -> str:
return self.run(*args, ok_codes=ok_codes).decode("utf-8", "replace")
def toplevel(self) -> Path:
try:
return Path(self.text("rev-parse", "--show-toplevel").strip())
except GitError as e:
raise GitError(f"not a Git repository: {self.repo}") from e
def commit(self, ref: str) -> str:
return self.text("rev-parse", "--verify", "--end-of-options",
f"{ref}^{{commit}}").strip()
def tree(self, commit: str) -> str:
return self.text("rev-parse", "--verify", f"{commit}^{{tree}}").strip()
def blob_text(self, commit: str, path: str) -> str | None:
"""Return the file at `commit:path`, or `None` if it is absent."""
try:
data = self.run("cat-file", "blob", f"{commit}:{path}")
except GitError:
return None
return data.decode("utf-8", "replace")
# ---------------------------------------------------------------------------
# Line-width analysis shared by commit bodies, Markdown, and comments.
def _code_span_end(text: str, start: int) -> int | None:
"""Return the index just past the code span opening at `start`, if any."""
run = 0
while start + run < len(text) and text[start + run] == "`":
run += 1
i = start + run
while i < len(text):
if text[i] == "`":
j = i
while j < len(text) and text[j] == "`":
j += 1
if j - i == run:
return j
i = j
else:
i += 1
return None
def unmatched_backtick(text: str) -> int | None:
"""Return the length of the first unclosed backtick run in `text`."""
i = 0
while i < len(text):
if text[i] != "`":
i += 1
continue
end = _code_span_end(text, i)
if end is None:
run = 0
while i + run < len(text) and text[i + run] == "`":
run += 1
return run
i = end
return None
def units(text: str) -> list[tuple[int, int]]:
"""Split `text` into unbreakable units as `(start, end)` index pairs.
Units are separated by spaces outside inline code spans, so a code span
(which must not be split across a wrap) is never broken.
"""
out: list[tuple[int, int]] = []
i, n = 0, len(text)
while i < n:
while i < n and text[i] in " \t":
i += 1
if i >= n:
break
start = i
while i < n and text[i] not in " \t":
if text[i] == "`":
end = _code_span_end(text, i)
if end is not None:
i = end
continue
i += 1
out.append((start, i))
return out
def wrap_violation(line: str, prefix_len: int, limit: int) -> bool:
"""Return whether `line` is too long although wrapping could fix it.
`prefix_len` is the width of the leading indentation, comment marker, or
list marker that a continuation line would repeat. A line is exempt when
the unit crossing the limit starts the line or would not fit on a
continuation line by itself (for example a long URL or code span).
"""
line = line.rstrip()
if len(line) <= limit:
return False
body = line[prefix_len:]
for idx, (start, end) in enumerate(units(body)):
if prefix_len + end <= limit:
continue
if idx == 0:
return False
return prefix_len + (end - start) <= limit
return False
_LIST_MARKER_RE = re.compile(r"^(\s*(?:>\s?)*(?:[-*+]|\d+[.)])\s+)")
_QUOTE_PREFIX_RE = re.compile(r"^(\s*(?:>\s?)+)")
def prose_prefix_len(text: str) -> int:
"""Width of leading whitespace, blockquote markers, and list marker."""
m = _LIST_MARKER_RE.match(text)
if m:
return len(m.group(1))
m = _QUOTE_PREFIX_RE.match(text)
if m:
return len(m.group(1))
return len(text) - len(text.lstrip(" \t"))
# ---------------------------------------------------------------------------
# Line classification: which lines of a file are prose subject to wrapping.
@dataclass(frozen=True)
class ProseLine:
"""A prose line: `prefix` is the width a continuation line repeats."""
prefix: int
text: str
block: int
def _markdown_prose(lines: list[str]) -> dict[int, ProseLine]:
out: dict[int, ProseLine] = {}
skip: set[int] = set()
n = len(lines)
i = 0
if n and lines[0].strip() == "---":
j = 1
while j < n and lines[j].strip() != "---":
j += 1
skip.update(range(0, min(j + 1, n)))
i = j + 1
# Tables: a delimiter row plus contiguous non-blank rows around it.
for k, line in enumerate(lines):
if TABLE_DELIM_RE.match(line):
a = k
while a > 0 and lines[a - 1].strip():
a -= 1
b = k
while b + 1 < n and lines[b + 1].strip():
b += 1
skip.update(range(a, b + 1))
block = 0
fence: tuple[str, int] | None = None
in_list = False
in_code = False
prev_blank = True
while i < n:
line = lines[i]
stripped = line.strip()
if fence is not None:
m = _fence(line)
if (m and m.group(2)[0] == fence[0]
and len(m.group(2)) >= fence[1] and not m.group(3).strip()):
fence = None
i += 1
continue
m = _fence(line)
if m:
fence = (m.group(2)[0], len(m.group(2)))
block += 1
i += 1
prev_blank = False
continue
if not stripped:
block += 1
prev_blank = True
i += 1
continue
lead = len(line) - len(line.lstrip(" \t"))
# An indented code block runs until a non-blank line indented less
# than four columns.
in_code = lead >= 4 and (in_code or (prev_blank and not in_list))
if _LIST_MARKER_RE.match(line):
in_list = True
elif prev_blank and lead == 0:
in_list = False
indented_code = in_code
# Headings, tables, HTML, link definitions, and display math cannot
# be rewrapped without changing their rendering.
if (i in skip or indented_code
or stripped.startswith(("#", "|", "<", "$")) or "$$" in line
or re.match(r"^\s*\[[^\]]+\]:\s*\S", line)):
block += 1
else:
out[i + 1] = ProseLine(prose_prefix_len(line), line, block)
prev_blank = False
i += 1
return out
def _comment_prose(
lines: list[str], is_comment: list[bool], marker_re: re.Pattern[str]
) -> dict[int, ProseLine]:
"""Prose lines among comment lines, skipping fenced code in doc comments."""
out: dict[int, ProseLine] = {}
block = 0
fence: tuple[str, int] | None = None
prev_comment = False
for idx, line in enumerate(lines):
if not is_comment[idx]:
fence = None
if prev_comment:
block += 1
prev_comment = False
continue
prev_comment = True
m = marker_re.match(line)
prefix = m.end() if m else len(line) - len(line.lstrip(" \t"))
content = line[prefix:]
fm = _fence(content)
if fence is not None:
if fm and fm.group(2)[0] == fence[0] and len(fm.group(2)) >= fence[1]:
fence = None
block += 1
continue
if fm:
fence = (fm.group(2)[0], len(fm.group(2)))
block += 1
continue
cs = content.strip()
if (not cs or cs.startswith(("|", "#", "<"))
or TABLE_DELIM_RE.match(content)):
block += 1
continue
out[idx + 1] = ProseLine(
prefix + prose_prefix_len(content), line, block
)
return out
_HASH_MARKER_RE = re.compile(r"^\s*#+ ?")
_SLASH_MARKER_RE = re.compile(r"^\s*//[/!]? ?")
_LEAN_MARKER_RE = re.compile(r"^\s*(?:/--?!?|--+)? ?")
def _python_comments(lines: list[str]) -> list[bool]:
"""Mark lines holding only a comment, using Python's own tokenizer."""
flags = [False] * len(lines)
source = "".join(ln + "\n" for ln in lines)
try:
for tok in tokenize.generate_tokens(io.StringIO(source).readline):
row, col = tok.start
if (tok.type == tokenize.COMMENT
and not lines[row - 1][:col].strip()
and not (row == 1 and tok.string.startswith("#!"))):
flags[row - 1] = True
except (tokenize.TokenError, SyntaxError):
# Unparseable input yields no prose lines rather than guesses.
return [False] * len(lines)
return flags
def _hash_comments(lines: list[str], ext: str) -> list[bool]:
"""Mark `#` comment lines, skipping Nix indented strings."""
if ext == ".py":
return _python_comments(lines)
flags: list[bool] = []
in_string: str | None = None
for idx, line in enumerate(lines):
stripped = line.lstrip()
is_comment = (
in_string is None
and stripped.startswith("#")
and not (idx == 0 and stripped.startswith("#!"))
)
flags.append(is_comment)
if is_comment:
continue
if ext == ".nix":
# `''` delimits indented strings; `'''`, `''$`, and `''\` escape.
delims = len(re.findall(r"(?<!')''(?![$'\\])", line))
if delims % 2 == 1:
in_string = None if in_string else "''"
return flags
_LEAN_CHAR_RE = re.compile(r"'(?:\\.[0-9a-fA-F]*|[^\\'])'")
def _lean_comments(lines: list[str]) -> list[bool]:
"""Mark lines inside Lean block comments or starting with a comment."""
flags: list[bool] = []
depth = 0
in_string = False
for line in lines:
stripped = line.lstrip()
flags.append(
depth > 0 or stripped.startswith("--") or stripped.startswith("/-")
)
i, n = 0, len(line)
while i < n:
two = line[i:i + 2]
if depth > 0:
if two == "/-":
depth += 1
i += 2
elif two == "-/":
depth -= 1
i += 2
else:
i += 1
elif in_string:
if line[i] == "\\":
i += 2
elif line[i] == '"':
in_string = False
i += 1
else:
i += 1
elif two == "--":
break
elif two == "/-":
depth += 1
i += 2
elif line[i] == '"':
in_string = True
i += 1
elif line[i] == "'" and (m := _LEAN_CHAR_RE.match(line, i)):
i = m.end()
else:
i += 1
in_string = False
return flags
_RUST_CHAR_RE = re.compile(r"'(?:\\(?:u\{[0-9a-fA-F]{1,6}\}|x[0-9a-fA-F]{2}|.)|[^\\'])'")
_RUST_RAW_START_RE = re.compile(r"[bc]?r(#*)\"")
def _rust_comments(lines: list[str]) -> list[bool]:
"""Mark Rust lines that start in or with a comment, outside literals.
`//@` lines are test-harness directives, not prose.
"""
flags: list[bool] = []
depth = 0 # nested `/* */` comment depth
# Closing delimiter of an open string literal, and whether backslash
# escapes apply (they do not in raw strings, even `r"..."`).
string: str | None = None
escapes = False
for line in lines:
stripped = line.lstrip()
flags.append(string is None and (depth > 0 or (
stripped.startswith("//") and not stripped.startswith("//@"))))
i, n = 0, len(line)
while i < n:
if string is not None:
if escapes and line[i] == "\\":
i += 2
elif line.startswith(string, i):
i += len(string)
string = None
else:
i += 1
elif depth > 0:
if line.startswith("/*", i):
depth += 1
i += 2
elif line.startswith("*/", i):
depth -= 1
i += 2
else:
i += 1
elif line.startswith("//", i):
break
elif line.startswith("/*", i):
depth += 1
i += 2
elif line[i] == '"':
string, escapes = '"', True
i += 1
elif line[i] == "'" and (m := _RUST_CHAR_RE.match(line, i)):
i = m.end()
elif (m := _RUST_RAW_START_RE.match(line, i)) and (
i == 0 or not (line[i - 1].isalnum() or line[i - 1] == "_")):
string, escapes = '"' + m.group(1), False
i = m.end()
else:
i += 1
return flags
def _markdown_unfenced(lines: list[str]) -> list[bool]:
"""Mark Markdown lines outside fenced code blocks."""
flags: list[bool] = []
fence: tuple[str, int] | None = None
for line in lines:
m = _fence(line)
if fence is not None:
flags.append(False)
if (m and m.group(2)[0] == fence[0]
and len(m.group(2)) >= fence[1] and not m.group(3).strip()):
fence = None
elif m:
flags.append(False)
fence = (m.group(2)[0], len(m.group(2)))
else:
flags.append(True)
return flags
def text_lines(path: str, content: str) -> set[int]:
"""1-based numbers of comment lines, or unfenced Markdown lines.
These hold prose that should never cite ephemeral reviews, including
headings and tables that the wrapping check skips. Code and string
literals may carry such IDs as data.
"""
lines = content.split("\n")
ext = Path(path).suffix
if ext in MARKDOWN_EXTS:
flags = _markdown_unfenced(lines)
elif ext in HASH_COMMENT_EXTS:
flags = _hash_comments(lines, ext)
elif ext in SLASH_COMMENT_EXTS:
flags = _rust_comments(lines)
elif ext in LEAN_EXTS:
flags = _lean_comments(lines)
else:
return set()
return {i + 1 for i, f in enumerate(flags) if f}
def prose_lines(path: str, content: str) -> dict[int, ProseLine]:
"""Return the 1-based line numbers of `content` that are wrapped prose."""
if path in WRAP_EXEMPT_PATHS:
return {}
# Git numbers lines by LF only; `str.splitlines` would also split on
# form feeds and other separators and shift every later line.
lines = [ln.expandtabs(8) for ln in content.split("\n")]
ext = Path(path).suffix
if ext in MARKDOWN_EXTS:
prose = _markdown_prose(lines)
elif ext in HASH_COMMENT_EXTS:
prose = _comment_prose(lines, _hash_comments(lines, ext),
_HASH_MARKER_RE)
elif ext in SLASH_COMMENT_EXTS:
prose = _comment_prose(lines, _rust_comments(lines), _SLASH_MARKER_RE)
elif ext in LEAN_EXTS:
prose = _comment_prose(lines, _lean_comments(lines), _LEAN_MARKER_RE)
else:
return {}
# The license header is verbatim text, whatever its indentation.
return {
n: pl for n, pl in prose.items()
if not LICENSE_LINE_RE.search(pl.text)
}
def wrap_findings(
path: str, content: str, added: set[int]
) -> tuple[list[Finding], list[Finding]]:
"""Prose findings and warnings on the added lines of one file.
Findings cover overlong lines and review-round IDs. Inline code spans
split across a wrap are warnings only: CommonMark renders them correctly
and merged Anneal docs use them often, so failing on them would reject
accepted practice.
"""
findings: list[Finding] = []
warnings: list[Finding] = []
lines = content.split("\n")
texts = text_lines(path, content)
for lineno in sorted(added & texts):
if REVIEW_ROUND_RE.search(lines[lineno - 1]):
findings.append(Finding(
"review-round-id",
"remove the review-round ID; it is meaningless outside the "
"review that produced it",
path, lineno,
))
prose = prose_lines(path, content)
for lineno in sorted(added):
pl = prose.get(lineno)
if pl is None:
continue
text = pl.text
if wrap_violation(text, pl.prefix, PROSE_LIMIT):
findings.append(Finding(
"wrap",
f"{len(text.rstrip())} columns; wrap prose at "
f"{PROSE_LIMIT} columns",
path, lineno,
))
for lineno, pl in sorted(prose.items()):
nxt = prose.get(lineno + 1)
if nxt is None or nxt.block != pl.block:
continue
if lineno not in added and lineno + 1 not in added:
continue
run = unmatched_backtick(pl.text[pl.prefix:])
if run is None:
continue
closer = unmatched_backtick(nxt.text[nxt.prefix:])
if closer == run:
warnings.append(Finding(
"wrap",
"inline code span is split across a line wrap; prefer "
"keeping it on one line",
path, lineno,
))
return findings, warnings
# ---------------------------------------------------------------------------
# Commit messages and PR descriptions.
def _message_lines(message: str) -> list[str]:
return [ln.expandtabs(8) for ln in
message.replace("\r\n", "\n").rstrip("\n").split("\n")]
@dataclass(frozen=True)
class MessageLayout:
"""Where a commit message's final trailer block and code lines are.
Indices are 0-based into `_message_lines(message)`.
"""
trailer_start: int
final_is_trailers: bool
code_lines: frozenset[int]
def message_layout(lines: list[str]) -> MessageLayout:
"""Locate the final trailer block and the fenced or indented code lines.
Trailer checks share this so that a commit documenting a trailer's
syntax in a code block does not trigger them.
"""
end = len(lines)
trailer_start = end
while trailer_start > 1 and lines[trailer_start - 1].strip():
trailer_start -= 1
final_is_trailers = trailer_start > 1 and _is_trailer_block(
lines[trailer_start:end])
fence: tuple[str, int] | None = None
prev_blank = True
code_lines: set[int] = set()
for idx in range(2, end):
line = lines[idx]
m = _fence(line)
if fence is not None:
if m and m.group(2)[0] == fence[0] and len(m.group(2)) >= fence[1]:
fence = None
code_lines.add(idx)
prev_blank = False
continue
if m:
fence = (m.group(2)[0], len(m.group(2)))
code_lines.add(idx)
prev_blank = False
continue
is_code = prev_blank and line.startswith(" ")
prev_blank = not line.strip() or is_code
if is_code:
code_lines.add(idx)
return MessageLayout(trailer_start, final_is_trailers,
frozenset(code_lines))
def commit_message_findings(sha: str, message: str) -> list[Finding]:
short = sha[:12]
where = f"commit {short}"
out: list[Finding] = []
def add(msg: str, line: int | None = None) -> None:
out.append(Finding("commit-message", msg, where, line))
lines = _message_lines(message)
subject = GITHUB_SQUASH_SUFFIX_RE.sub("", lines[0])
if not subject.strip():
add("empty subject line", 1)
if len(subject) > SUBJECT_LIMIT:
add(f"subject is {len(subject)} columns (limit {SUBJECT_LIMIT})", 1)
if subject.rstrip().endswith("."):
add("subject ends with a period", 1)
if len(lines) > 1 and lines[1].strip():
add("separate the subject from the body with one blank line", 2)
# Trailers in the final paragraph are exempt from wrapping.
layout = message_layout(lines)
trailer_start = layout.trailer_start
final_is_trailers = layout.final_is_trailers
code_lines = layout.code_lines
for idx in range(2, len(lines)):
line = lines[idx]
lineno = idx + 1
if GHERRIT_RE.match(line):
add("remove the `gherrit-pr-id` line", lineno)
if REVIEW_ROUND_RE.search(line):
add("remove the review-round ID; commit messages must not "
"reference ephemeral reviews", lineno)
if idx in code_lines:
continue
is_table = line.lstrip().startswith("|")
is_trailer = final_is_trailers and idx >= trailer_start
if is_table or is_trailer:
continue
if wrap_violation(line, prose_prefix_len(line), BODY_LIMIT):
add(f"body line is {len(line.rstrip())} columns; wrap at "
f"{BODY_LIMIT}", lineno)
if REVIEW_ROUND_RE.search(lines[0]):
add("remove the review-round ID from the subject", 1)
# `authorship-disclosure`: at most one trailer per represented user, in
# the final trailer block. Examples inside code blocks do not count.
seen_users: set[str] = set()
for i, ln in enumerate(lines):
if i in code_lines or not AGENT_TRAILER_KEY_RE.match(ln):
continue
m = AGENT_TRAILER_RE.match(ln)
if not m:
add("malformed trailer; use `Agent-authored-by: AI agent acting "
"on <user>'s behalf` with an optional ` (content only)` or "
"` (commit message only)` suffix", i + 1)
elif m.group(1).casefold() in seen_users:
add(f"use at most one `Agent-authored-by:` trailer for "
f"{m.group(1)}", i + 1)
else:
seen_users.add(m.group(1).casefold())
if not final_is_trailers or i < trailer_start:
add("put `Agent-authored-by:` in the final trailer block", i + 1)
return out
def parse_waivers(
commits: list[Commit], head: str
) -> tuple[list[OptOut], list[Finding]]:
"""Parse `Skip-pr-hygiene:` trailers across every commit in the range.
Only the tip commit may waive: a squash merge keeps every commit
message, so a trailer left on an earlier commit would reach `main`
without waiving anything. Mentions inside code blocks are ignored, as
for `Agent-authored-by:`. Problems are `skip-pr-hygiene` findings,
which no trailer can waive.
"""
opt_outs: list[OptOut] = []
errors: list[Finding] = []
waivable = ", ".join(f"`{c}`" for c in WAIVABLE_CHECKS)
for c in commits:
lines = _message_lines(c.message)
layout = message_layout(lines)
where = f"commit {c.sha[:12]}"
for i, ln in enumerate(lines):
if i in layout.code_lines or not WAIVER_KEY_RE.match(ln):
continue
def err(msg: str) -> None:
errors.append(Finding("skip-pr-hygiene", msg, where, i + 1))
if c.sha != head:
err(f"only the PR's tip commit may carry `{WAIVER_KEY}:`; "
"remove it here (rebase rather than merging the base "
"branch)")
continue
if not layout.final_is_trailers or i < layout.trailer_start:
err(f"put `{WAIVER_KEY}:` in the final trailer block")
continue
prefix = f"{WAIVER_KEY}: "
if not ln.startswith(prefix):
err(f"spell the trailer exactly `{prefix}<check>[,<check>...]"
" -- <reason>`")
continue
if i + 1 < len(lines) and lines[i + 1][:1] in (" ", "\t"):
err(f"keep `{WAIVER_KEY}:` on one line; folded trailers are "
"not accepted")
continue
names_text, sep, reason = ln[len(prefix):].partition(" -- ")
if not sep:
err("separate the checks from the reason with ` -- `")
continue
names = [n.strip() for n in names_text.split(",")]
if any(not n or re.search(r"\s", n) for n in names):
err("list checks as `<check>[,<check>...]`")
continue
bad = [n for n in dict.fromkeys(names) if n not in WAIVABLE_CHECKS]
if bad:
err(f"cannot waive {', '.join(f'`{n}`' for n in bad)}; "
f"name only {waivable} (there is no wildcard)")
continue
if len(re.sub(r"\s", "", reason)) < WAIVER_MIN_REASON:
err(f"give a reason of at least {WAIVER_MIN_REASON} "
"non-whitespace characters")
continue
opt_outs.append(OptOut(tuple(dict.fromkeys(names)),
reason.strip(), c.sha, i + 1))
return opt_outs, errors
def apply_waivers(report: Report, opt_outs: list[OptOut],
errors: list[Finding]) -> None:
"""Move waived findings aside and reject waivers that waive nothing."""
reasons: dict[str, list[str]] = {}
for o in opt_outs:
for name in o.checks:
reasons.setdefault(name, [])
if o.reason not in reasons[name]:
reasons[name].append(o.reason)
kept: list[Finding] = []
for f in report.findings:
if f.check in reasons:
report.waived.append(Waived(f, "; ".join(reasons[f.check])))
else:
kept.append(f)
used = {w.finding.check for w in report.waived}
unused: list[Finding] = []
for o in opt_outs:
for name in o.checks:
if name not in used:
unused.append(Finding(
"skip-pr-hygiene",
f"`{name}` has no findings to waive; remove it from the "
"trailer",
f"commit {o.commit[:12]}", o.line,
))
report.findings = kept + errors + unused
report.opt_outs = list(opt_outs)
def pr_title_policy_findings(pattern: re.Pattern[str],
title: str) -> list[Finding]:
"""Reject a PR title that the merge queue's message policy would reject.
A multi-commit PR's title becomes its squash commit subject, so this
fails on the PR instead of later in the merge queue.
"""
m = pattern.search(title)
if m is None:
return []
return [Finding(
"pr-title-policy",
f"PR title {title.strip()!r} matches the forbidden pattern "
f"`{pattern.pattern}` at {m.group(0)!r}",
)]
ISSUE_LINE_RE = re.compile(
r"^(?:close[sd]?|fix(?:e[sd])?|resolve[sd]?|part of|makes progress on|"
r"refs?|see)\s+(?:[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+)?#\d+\b",
re.IGNORECASE,
)
_LIST_RE = re.compile(r"^([ \t]*)(?:[-*+]|\d+[.)])[ \t]+(.*)$")
_HEADING_RE = re.compile(r"^[ \t]*#{1,6}(?:[ \t]|$)")
_HR_RE = re.compile(
r"^[ \t]*(?:(?:-[ \t]*){3,}|(?:\*[ \t]*){3,}|(?:_[ \t]*){3,})$"
)
_SETEXT_RE = re.compile(r"^[ \t]*(?:=+|-+)[ \t]*$")
_TABLE_RE = re.compile(r"^[ \t]*\|.*\|[ \t]*$")
_LINK_DEF_RE = re.compile(r"^[ \t]*\[[^\]\n]+\]:[ \t]*\S")
_HTML_COMMENT_RE = re.compile(r"^[ \t]*<!--")
_BQ_RE = re.compile(r"^([ \t]*>[ \t]?)(.*)$")
def _is_trailer_block(block: list[str]) -> bool:
if not block:
return False
saw_header = False
for line in block:
if TRAILER_RE.match(line) or ISSUE_LINE_RE.match(line):
saw_header = True
elif saw_header and line.startswith((" ", "\t")):
continue
else:
return False
return saw_header
def _is_block_starter(line: str) -> bool:
return bool(
_fence(line) or _LIST_RE.match(line) or _HEADING_RE.match(line)
or _HR_RE.match(line) or _TABLE_RE.match(line)
or _LINK_DEF_RE.match(line) or _HTML_COMMENT_RE.match(line)
or ISSUE_LINE_RE.match(line)
)
def _join(out: list[str], lines: list[str], i: int, end: int,
stop: Callable[[int], bool]) -> int:
"""Join soft-wrapped lines from `i`, honoring Markdown hard breaks."""
acc = lines[i]
i += 1
while i < end and not stop(i):
if acc.endswith(" ") or acc.endswith("\\"):
out.append(acc)
acc = lines[i]
else:
acc = f"{acc.rstrip()} {lines[i].strip()}"
i += 1
out.append(acc.rstrip())
return i
def commit_body_to_pr_body(body: str, prose_disclosure: bool = True) -> str:
"""Unwrap a 72-column commit body into a GitHub PR description.
Joins soft-wrapped prose in paragraphs, list items, and blockquotes;
preserves blank lines, code blocks, headings, thematic breaks, tables,
link definitions, HTML comments, hard breaks, issue-reference lines, and
the final trailer block; and, with `prose_disclosure`, converts the
`Agent-authored-by:` trailer into the prose notice that the
`authorship-disclosure` skill requires in PR descriptions.
"""
lines = body.replace("\r\n", "\n").splitlines()
out: list[str] = []
i, end = 0, len(lines)
while end > 0 and not lines[end - 1].strip():
end -= 1
if end == 0:
return ""
trailer_start = end
while trailer_start > 0 and lines[trailer_start - 1].strip():
trailer_start -= 1
final_is_trailers = _is_trailer_block(lines[trailer_start:end])
list_indent: int | None = None
while i < end:
line = lines[i]
if final_is_trailers and i == trailer_start:
notices: list[str] = []
kept: list[str] = []
for tl in lines[trailer_start:end]:
m = AGENT_TRAILER_RE.match(tl) if prose_disclosure else None
if m:
if m.group(2) != "content only":
notices.append(
f"*Authored by an AI agent acting on "
f"{m.group(1)}'s behalf.*"
)
else:
kept.append(tl)
out.extend(kept)
if notices:
if kept:
out.append("")
out.extend(notices)
break
if not line.strip():
out.append("")
i += 1
continue
m_fence = _fence(line)
if m_fence:
fc, fl = m_fence.group(2)[0], len(m_fence.group(2))
close_re = re.compile(rf"^[ \t]*{re.escape(fc)}{{{fl},}}[ \t]*$")
out.append(line)
i += 1
while i < end:
out.append(lines[i])
if close_re.match(lines[i]):
i += 1
break
i += 1
continue
if ((not out or out[-1] == "") and list_indent is None
and line.startswith((" ", "\t"))):
limit = trailer_start if final_is_trailers else end
while i < limit and (lines[i].startswith((" ", "\t"))
or not lines[i].strip()):
out.append(lines[i])
i += 1
continue
if (_HEADING_RE.match(line) or _HR_RE.match(line)
or _TABLE_RE.match(line) or _LINK_DEF_RE.match(line)
or _HTML_COMMENT_RE.match(line) or ISSUE_LINE_RE.match(line)):
list_indent = None
out.append(line)
i += 1
continue
m_bq = _BQ_RE.match(line)
if m_bq and (list_indent is None
or len(line) - len(line.lstrip(" ")) < list_indent):
list_indent = None
bq: list[str] = []
while i < end and _BQ_RE.match(lines[i]):
bq.append(_BQ_RE.match(lines[i]).group(2))
i += 1
inner = commit_body_to_pr_body("\n".join(bq), False)
for il in inner.splitlines():
out.append(f"> {il}" if il else ">")
continue
def stop_item(j: int) -> bool:
return not lines[j].strip() or _is_block_starter(lines[j])
m_list = _LIST_RE.match(line)
if m_list:
list_indent = len(line) - len(m_list.group(2))
i = _join(out, lines, i, end, stop_item)
continue
lead = len(line) - len(line.lstrip(" "))
if list_indent is not None and lead >= list_indent:
i = _join(out, lines, i, end, stop_item)
continue
list_indent = None
if i + 1 < end and _SETEXT_RE.match(lines[i + 1]):
out.extend([line, lines[i + 1]])
i += 2
continue
def stop_para(j: int) -> bool:
nxt = lines[j]
return (not nxt.strip() or _is_block_starter(nxt)
or bool(_BQ_RE.match(nxt))
or (j + 1 < end and bool(_SETEXT_RE.match(lines[j + 1]))))
i = _join(out, lines, i, end, stop_para)
return "\n".join(out).strip() + "\n"
def _normalize_body(text: str) -> list[str]:
lines = text.replace("\r\n", "\n").replace("\r", "\n").split("\n")
lines = [ln.rstrip() for ln in lines]
while lines and not lines[-1]:
lines.pop()
while lines and not lines[0]:
lines.pop(0)
return lines
def pr_body_findings(expected: str, actual: str) -> list[Finding]:
exp, act = _normalize_body(expected), _normalize_body(actual)
if exp == act:
return []
for idx in range(max(len(exp), len(act))):
e = exp[idx] if idx < len(exp) else "<end of body>"
a = act[idx] if idx < len(act) else "<end of body>"
if e != a:
return [Finding(
"pr-body",
"PR description differs from the unwrapped commit body "
f"(regenerate it with --emit-pr-body); first difference at "
f"line {idx + 1}: expected {e!r}, found {a!r}",
)]
return []
# ---------------------------------------------------------------------------
# Diff parsing.
@dataclass
class ChangedFile:
path: str
status: str
old_path: str | None = None
added: set[int] = field(default_factory=set)
added_text: dict[int, str] = field(default_factory=dict)
removed_text: list[str] = field(default_factory=list)
# Whether an added or removed line holds an `-- anneal-trust-tier:`.
trust_tier_changed: bool = False
def in_anneal_scope(path: str) -> bool:
return (path.startswith(COMMIT_SCOPE_PREFIXES)
and not path.startswith(EXCLUDED_PREFIXES))
def _patch_path(token: str, prefix: str = "b/") -> str | None:
"""Decode a `---`/`+++` path from a patch header (`None` for /dev/null).
Git C-quotes paths with special characters and appends a tab to paths
containing spaces.
"""
if token.endswith("\t"):
token = token[:-1]
if token.startswith('"') and token.endswith('"') and len(token) >= 2:
try:
raw, _ = codecs.escape_decode(
token[1:-1].encode("utf-8", "surrogateescape"))
except ValueError as e:
raise GitError(f"cannot decode patch path {token!r}") from e
token = raw.decode("utf-8", "surrogateescape")
if token == "/dev/null":
return None
if not token.startswith(prefix):
raise GitError(f"unexpected patch path {token!r}")
return token[len(prefix):]
_HUNK_RE = re.compile(r"^@@ -\d+(?:,(\d+))? \+(\d+)(?:,(\d+))? @@")
def changed_files(git: Git, base: str, head: str) -> dict[str, ChangedFile]:
files: dict[str, ChangedFile] = {}
raw = git.run("diff", "--no-ext-diff", "--no-textconv", "-M",
"--name-status", "-z", base, head, "--")
parts = raw.decode("utf-8", "surrogateescape").split("\0")
i = 0
while i < len(parts) and parts[i]:
status = parts[i]
if status[0] in "RC":
old, path = parts[i + 1], parts[i + 2]
i += 3
else:
old, path = None, parts[i + 1]
i += 2
files[path] = ChangedFile(
path, status[0], old_path=old if status[0] == "R" else None)
# `--text` keeps binary or `-diff` content visible. Hunk line counts
# delimit hunks, so added lines that look like headers stay content.
patch = git.run("diff", "--no-ext-diff", "--no-textconv", "--text", "-M",
"-U0", "--no-color", base, head, "--")
current: ChangedFile | None = None
# Owner of removed lines: `current`, or the deleted file itself.
removed_owner: ChangedFile | None = None
old_left = new_left = 0
new_line = 0
for line in patch.decode("utf-8", "surrogateescape").split("\n"):
if old_left > 0 or new_left > 0:
tag = line[:1]
if tag == "+":
if current is not None:
current.added.add(new_line)
current.added_text[new_line] = line[1:]
if TRUST_TIER_RE.search(line[1:]):
current.trust_tier_changed = True
new_line += 1
new_left -= 1
elif tag == "-":
if removed_owner is not None:
removed_owner.removed_text.append(line[1:])
if TRUST_TIER_RE.search(line[1:]):
removed_owner.trust_tier_changed = True
old_left -= 1
elif tag == " ":
new_line += 1
old_left -= 1
new_left -= 1
elif tag != "\\":
raise GitError(f"malformed patch line {line[:80]!r}")
continue
if line.startswith("diff --git "):
current = removed_owner = None
elif line.startswith("--- "):
old = _patch_path(line[4:], "a/")
# A deletion is listed under its old path.
removed_owner = files.get(old) if old is not None else None
elif line.startswith("+++ "):
path = _patch_path(line[4:])
current = files.get(path) if path is not None else None
if path is not None and current is None:
raise GitError(f"patch names unlisted path {path!r}")
if current is not None:
removed_owner = current
elif line.startswith("@@"):
m = _HUNK_RE.match(line)
if not m:
raise GitError(f"malformed hunk header {line[:80]!r}")
old_left = int(m.group(1) or "1")
new_line = int(m.group(2))
new_left = int(m.group(3) or "1")
# Structurally inspect `cartridges.lock.json` blobs so multiline JSON,
# unicode-escaped keys, renames, and corrupt JSON cannot evade HRS.
for cf in files.values():
path_is_lock = Path(cf.path).name == "cartridges.lock.json"
old_is_lock = (
cf.old_path is not None
and Path(cf.old_path).name == "cartridges.lock.json"
)
if not (path_is_lock or old_is_lock):
continue
base_path = cf.old_path if cf.old_path is not None else cf.path
if cf.status == "D":
base_ok, base_snap = _cartridge_stability_snapshot(
git.blob_text(base, cf.path)
)
if not base_ok or bool(base_snap):
cf.trust_tier_changed = True
elif cf.status == "A":
head_ok, head_snap = _cartridge_stability_snapshot(
git.blob_text(head, cf.path)
)
if not head_ok or bool(head_snap):
cf.trust_tier_changed = True
elif cf.status == "R" and not path_is_lock:
base_ok, base_snap = _cartridge_stability_snapshot(
git.blob_text(base, base_path)
)
if not base_ok or bool(base_snap):
cf.trust_tier_changed = True
elif cf.status == "R" and not old_is_lock:
head_ok, head_snap = _cartridge_stability_snapshot(
git.blob_text(head, cf.path)
)
if not head_ok or bool(head_snap):
cf.trust_tier_changed = True
else:
base_ok, base_snap = _cartridge_stability_snapshot(
git.blob_text(base, base_path)
)
head_ok, head_snap = _cartridge_stability_snapshot(
git.blob_text(head, cf.path)
)
if not base_ok or not head_ok or base_snap != head_snap:
cf.trust_tier_changed = True
return files
def size_exclusion(path: str) -> str | None:
"""Return the review-size category that excludes `path`, if any."""
name = Path(path).name
if path in SIZE_GENERATED_PATHS or name in SIZE_GENERATED_NAMES:
return "generated"
if name in SIZE_LOCK_NAMES:
return "lock"
if path.startswith(EXCLUDED_PREFIXES):
return "vendor"
if path.startswith("experimental/") and path != "experimental/README.md":
return "experimental"
if (path.startswith(FIXTURE_ROOTS)
and ".." not in Path(path).parts and "\\" not in path
and not FIXTURE_NEVER_SEGMENT_RE.search(path)
and FIXTURE_NAME_RE.fullmatch(name)
and (not path.startswith("tests/corpora/")
or FIXTURE_CORPORA_RE.fullmatch(path))
and (not path.startswith("zerocopy/proofs/")
or FIXTURE_ZEROCOPY_RE.fullmatch(path))):
return "fixture"
return None
@dataclass(frozen=True)
class ReviewSize:
"""Changed lines in a diff, split into reviewable and excluded lines."""
reviewable_lines: int
reviewable_files: int
# Category -> (lines, files); binary files count files only.
excluded: dict[str, tuple[int, int]]
budget: int = REVIEW_SIZE_BUDGET
@property
def over_budget(self) -> bool:
return self.reviewable_lines > self.budget
def to_json(self) -> dict[str, object]:
return {
"budget": self.budget,
"reviewable_lines": self.reviewable_lines,
"reviewable_files": self.reviewable_files,
"excluded": {k: {"lines": v[0], "files": v[1]}
for k, v in self.excluded.items()},
"over_budget": self.over_budget,
}
def review_size(numstat: list[tuple[str, int | None]],
budget: int = REVIEW_SIZE_BUDGET) -> ReviewSize:
"""Classify `(path, changed lines or None if binary)` pairs."""
lines = files = 0
excluded = {k: (0, 0) for k in SIZE_CATEGORIES}
for path, changed in numstat:
category = "binary" if changed is None else size_exclusion(path)
if category is None:
lines += changed or 0
files += 1
else:
n, m = excluded[category]
excluded[category] = (n + (changed or 0), m + 1)
return ReviewSize(lines, files, excluded, budget)
def diff_numstat(git: Git, base: str, head: str
) -> list[tuple[str, int | None]]:
"""Return `(path, added + deleted lines)` per changed file.
Binary files report `None`. Renames are detected as in `changed_files`
and are listed under their new path.
"""
raw = git.run("diff", "--no-ext-diff", "--no-textconv", "-M",
"--numstat", "-z", base, head, "--")
if not raw:
return []
# Every record ends in a NUL, so anything else is truncated output.
if not raw.endswith(b"\0"):
raise GitError("truncated numstat output")
parts = raw[:-1].decode("utf-8", "surrogateescape").split("\0")
out: list[tuple[str, int | None]] = []
i = 0
while i < len(parts):
fields = parts[i].split("\t", 2)
if len(fields) != 3:
raise GitError(f"malformed numstat entry {parts[i][:80]!r}")
added, deleted, path = fields
if path:
i += 1
else:
# A rename or copy: the old and new paths follow.
if (i + 2 >= len(parts) or not parts[i + 1]
or not parts[i + 2]):
raise GitError("truncated numstat rename entry")
path = parts[i + 2]
i += 3
if added == "-" and deleted == "-":
out.append((path, None))
elif all(n.isascii() and n.isdecimal() for n in (added, deleted)):
out.append((path, int(added) + int(deleted)))
else:
raise GitError(f"malformed numstat counts for {path!r}")
return out
def review_size_warning(size: ReviewSize) -> Finding | None:
"""An advisory warning when reviewable lines exceed the budget."""
if not size.over_budget:
return None
parts = [f"{m} binary file(s)" if k == "binary"
else f"{n} {k} line(s) in {m} file(s)"
for k, (n, m) in size.excluded.items() if m]
excluded = ", ".join(parts) if parts else "none"
return Finding(
"review-size",
f"{size.reviewable_lines} reviewable changed lines in "
f"{size.reviewable_files} file(s) exceed the {size.budget}-line "
f"review budget (excluded: {excluded}); consider splitting this "
"change into a linear stack of PRs (`gh stack link --base main "
"<pr>...`). This is advisory and does not fail the check.",
)
# ---------------------------------------------------------------------------
# Per-check logic over the diff and tree.
def _leading_comment(path: str, lines: list[str]) -> list[str]:
"""Return the file's leading comment block, after any shebang."""
ext = Path(path).suffix
shebang = ext in HASH_COMMENT_EXTS and lines and lines[0].startswith("#!")
i = 1 if shebang else 0
while i < len(lines) and not lines[i].strip():
i += 1
block: list[str] = []
if ext in MARKDOWN_EXTS:
if i < len(lines) and lines[i].lstrip().startswith("<!--"):
for ln in lines[i:]:
if "-->" in ln:
block.append(ln.split("-->", 1)[0])
return block
block.append(ln)
elif i < len(lines) and lines[i].strip() == "+++":
# Zola TOML front matter requires `+++` on line 1; accept a leading
# `#` header block immediately inside a closed `+++` fence.
j = i + 1
while j < len(lines) and not lines[j].strip():
j += 1
for ln in lines[j:]:
if not ln.lstrip().startswith("#"):
break
block.append(ln)
if block and any(
ln.strip() == "+++" for ln in lines[j + len(block):]
):
return block
# Missing or unclosed: no leading comment.
return []
markers = {
**{e: ("#",) for e in HASH_COMMENT_EXTS},
**{e: ("//",) for e in SLASH_COMMENT_EXTS},
**{e: ("--",) for e in LEAN_EXTS},
}.get(ext, ())
for ln in lines[i:]:
if not ln.lstrip().startswith(markers):
break
block.append(ln)
return block
def header_findings(path: str, content: str, max_year: int) -> list[Finding]:
"""Check that a new file opens with the license header as a comment."""
cleaned = []
for ln in _leading_comment(path, content.split("\n")):
ln = re.sub(r"^\s*(?:<!--|//[/!]?|#+|--)", "", ln)
cleaned.append(ln.replace("-->", "").strip())
joined = re.sub(r"\s+", " ", " ".join(c for c in cleaned if c))
m = HEADER_RE.search(joined)
if not m:
return [Finding(
"header",
"new file does not open with the Fuchsia copyright and license "
"header comment (see `PRINCIPLES.md`)",
path, 1,
)]
year = int(m.group(1))
if not 2019 <= year <= max_year:
return [Finding(
"header", f"copyright year {year} is not a plausible creation "
f"year (expected 2019..{max_year})", path, 1,
)]
return []
def needs_header(path: str) -> bool:
name = Path(path).name
return (
Path(path).suffix in HEADER_EXTS
and name not in HEADER_EXEMPT_NAMES
and path not in HEADER_EXEMPT_PATHS
)
def trusted_citation_findings(
path: str, content: str, added_text: dict[int, str]
) -> list[Finding]:
full_shas = set(SHA40_RE.findall(content))
out: list[Finding] = []
for lineno, text in sorted(added_text.items()):
for rx in (UPSTREAM_LINK_RE, UPSTREAM_RAW_RE):
for m in rx.finditer(text):
ref = re.split(r"[#?]", m.group(2))[0].rstrip(".,;:")
if not re.fullmatch(r"[0-9a-f]{40}", ref):
out.append(Finding(
"trusted-citation",
f"`rust-lang/{m.group(1)}` link pins `{ref}`; "
"cite a full 40-hex commit SHA",
path, lineno,
))
for m in RENDERED_DOCS_RE.finditer(text):
out.append(Finding(
"trusted-citation",
f"`{m.group(1)}` serves unpinned rendered docs; quote and "
"cite `rust-lang/rust` or `rust-lang/reference` at a pinned "
"commit SHA",
path, lineno,
))
for m in ABBREVIATED_CITE_RE.finditer(text):
prefix = m.group(1)
if not any(sha.startswith(prefix) for sha in full_shas):
out.append(Finding(
"trusted-citation",
f"abbreviated upstream SHA `{prefix}` does not resolve "
"to a full 40-hex SHA cited in this file",
path, lineno,
))
return out
def is_hrs_path(path: str, trust_tier_changed: bool = False) -> bool:
"""Whether `path` is on the Human review surface.
`trust_tier_changed` says the diff adds or removes an
`-- anneal-trust-tier:` directive in a `.lean` file or changes a
`"stability"` field in a `cartridges.lock.json` file.
"""
parts = path.split("/")
name = parts[-1]
if (any(p in HRS_CONFIG_DIRS for p in parts)
or name in HRS_CONFIG_NAMES or name.startswith("lakefile")
or name.startswith("rust-toolchain")
or ((name.endswith(".lean") or name == "cartridges.lock.json")
and trust_tier_changed)):
return True
# Proof-unit specifications and requests, which choose the roots,
# sources, and specification (including the inline specifications a
# generated one is lowered from); only `Proof.lean` is kernel-checked.
if path.startswith("zerocopy/proofs/") and (
name in ("request.json", "spec_sources.json")
or (name.endswith(".lean")
and not (name == "Proof.lean" and len(parts) == 4))):
return True
if path.startswith(ANNEAL):
rel = path[len(ANNEAL):]
return rel.startswith(HRS_ANNEAL_PREFIXES) or rel in HRS_ANNEAL_FILES
return path.startswith(HRS_ROOT_PREFIXES)
RECEIPT_FOOTER_RE = re.compile(
r"<!--\s*anneal-receipt:v1\s+(.*?)\s*-->", re.DOTALL)
# Any footer-like marker, so malformed or other-version footers are counted
# rather than ignored.
RECEIPT_MARKER_RE = re.compile(r"<!--\s*anneal-receipt:", re.IGNORECASE)
HTML_COMMENT_RE = re.compile(r"<!--.*?(?:-->|\Z)", re.DOTALL)
RECEIPT_KEYS = ("target_base", "merge_base", "head", "tree",
"human_review_surface")
SHA40_FULL_RE = re.compile(r"[0-9a-f]{40}")
def receipt_footer(target_base: str, merge_base: str, head: str, tree: str,
hrs_paths: list[str]) -> str:
"""Render the machine-checked footer that a Review Receipt carries."""
data = {"target_base": target_base, "merge_base": merge_base,
"head": head, "tree": tree,
"human_review_surface": sorted(hrs_paths)}
# Escape `>` so no path can close the HTML comment early.
text = json.dumps(data).replace(">", "\\u003e")
return f"<!-- anneal-receipt:v1 {text} -->\n"
def _reject_duplicate_keys(pairs: list[tuple[str, object]]) -> dict:
keys = [k for k, _ in pairs]
dups = sorted({k for k in keys if keys.count(k) > 1})
if dups:
raise ValueError(f"duplicate key(s) {', '.join(dups)}")
return dict(pairs)
def receipt_findings(
text: str, target_base: str, merge_base: str, head: str, tree: str,
hrs_paths: list[str], changed_paths: set[str],
) -> list[Finding]:
"""Check a Review Receipt's `anneal-receipt:v1` footer.
Prose cannot be checked mechanically without guessing at wording such
as "`ci/` is untouched", so the receipt binds its comparison tuple and
lists its Human review surface paths in one JSON footer (see
`--emit-receipt-footer`). Every changed review-surface path must be
listed, and every listed path must be a path this range changes. The
prose `Human review surface` block stays for humans to judge.
"""
def finding(msg: str) -> Finding:
return Finding("receipt", msg)
out: list[Finding] = []
# The prose block must be visible, not inside an HTML comment.
if not re.search(r"human review surface", HTML_COMMENT_RE.sub("", text),
re.IGNORECASE):
out.append(finding("Review Receipt lacks a visible `Human review "
"surface` block"))
markers = len(RECEIPT_MARKER_RE.findall(text))
footers = RECEIPT_FOOTER_RE.findall(text)
if markers != 1 or len(footers) != 1:
out.append(finding(
f"Review Receipt has {markers} `anneal-receipt:` marker(s); it "
"needs exactly one well-formed `anneal-receipt:v1` footer (print "
"it with --emit-receipt-footer)"))
return out
try:
data = json.loads(footers[0], object_pairs_hook=_reject_duplicate_keys)
except ValueError as e:
return out + [finding(f"`anneal-receipt:v1` footer is not valid "
f"JSON: {e}")]
if not isinstance(data, dict) or set(data) != set(RECEIPT_KEYS):
return out + [finding(
"`anneal-receipt:v1` footer must be an object with exactly the "
"keys " + ", ".join(f"`{k}`" for k in RECEIPT_KEYS))]
expected = {"target_base": target_base, "merge_base": merge_base,
"head": head, "tree": tree}
for key, sha in expected.items():
value = data[key]
if not (isinstance(value, str) and SHA40_FULL_RE.fullmatch(value)):
out.append(finding(f"footer `{key}` must be a full 40-hex SHA"))
elif value != sha:
out.append(finding(
f"footer `{key}` is `{value[:12]}`, but the checked "
f"{key} is `{sha[:12]}`"))
listed = data["human_review_surface"]
if not (isinstance(listed, list)
and all(isinstance(p, str) for p in listed)):
return out + [finding(
"footer `human_review_surface` must be a list of paths")]
missing = sorted(set(hrs_paths) - set(listed))
if missing:
out.append(finding(
"footer `human_review_surface` omits changed review-surface "
"path(s): " + ", ".join(f"`{p}`" for p in missing)))
unknown = sorted(set(listed) - changed_paths)
if unknown:
out.append(finding(
"footer `human_review_surface` lists path(s) this range does not "
"change: " + ", ".join(f"`{p}`" for p in unknown)))
return out
def agents_budget_findings(git: Git, head: str) -> list[Finding]:
reserve = os.environ.get("GLOBAL_AGENTS_BYTES", DEFAULT_GLOBAL_AGENTS_BYTES)
if not (reserve.isascii() and reserve.isdecimal()) or len(
reserve.lstrip("0") or "0") > 5:
return [Finding("agents-budget",
f"invalid GLOBAL_AGENTS_BYTES: {reserve!r}")]
listing = git.run("ls-tree", "-r", "-l", "-z", head)
total = 0
names = []
for entry in listing.decode("utf-8", "surrogateescape").split("\0"):
if not entry:
continue
meta, path = entry.split("\t", 1)
if Path(path).name not in ("AGENTS.md", "AGENTS.override.md"):
continue
if path.startswith(EXCLUDED_PREFIXES):
continue
size = meta.split()[3]
if size.isdigit():
total += int(size)
names.append(path)
if total + int(reserve) >= AGENTS_BUDGET:
return [Finding(
"agents-budget",
f"{' + '.join(sorted(names))} = {total} B plus the {reserve} B "
f"global reserve reaches the {AGENTS_BUDGET} B shared instruction "
"budget (see `docs/review-policy-design.md#drift-checks`)",
)]
return []
def _crr_block(text: str) -> tuple[str | None, str | None]:
lines = text.split("\n")
begins = [i for i, ln in enumerate(lines) if CRR_BEGIN in ln]
ends = [i for i, ln in enumerate(lines) if CRR_END in ln]
if len(begins) != 1 or len(ends) != 1 or ends[0] <= begins[0]:
return None, (f"expected one `{CRR_BEGIN}` followed by one "
f"`{CRR_END}`")
return "\n".join(lines[begins[0]:ends[0] + 1]), None
def crr_parity_findings(git: Git, head: str) -> list[Finding]:
blocks = []
for path in CRR_FILES:
content = git.blob_text(head, path)
if content is None:
return [Finding("code-review-rules-parity", "file is missing",
path)]
block, err = _crr_block(content)
if err:
return [Finding("code-review-rules-parity", err, path)]
blocks.append(block)
if blocks[0] != blocks[1]:
a, b = blocks[0].split("\n"), blocks[1].split("\n")
first = next((i for i in range(min(len(a), len(b))) if a[i] != b[i]),
min(len(a), len(b)))
return [Finding(
"code-review-rules-parity",
f"`Code Review Rules` block differs from `{CRR_FILES[0]}` at "
f"block line {first + 1}; keep the mirrors byte-identical",
CRR_FILES[1],
)]
return []
# ---------------------------------------------------------------------------
# Upstream `rustc` workaround anchors (`docs/rustc-workarounds.md`).
# Split so this file never contains a literal tag for the scan to flag.
WORKAROUND_TAG = "RUSTC-" "WORKAROUND"
WORKAROUND_PENDING = "#PENDING"
# Markdown is out of scope so prose and examples may name the tag freely.
WORKAROUND_MARKERS = {
".rs": ("//", "///", "//!"),
**{e: ("#",) for e in (".py", ".sh", ".nix", ".toml", ".yml", ".yaml")},
".lean": ("--",),
}
# Any spelling a typo might produce (`_` or no separator) counts as an
# attempted tag, so a misspelled anchor fails instead of silently vanishing.
WORKAROUND_LIKE_RE = re.compile(r"RUSTC[-_]?WORKAROUND")
WORKAROUND_ANCHOR_RE = re.compile(
r"^[ \t]*(?P<marker>//[/!]?|#|--)[ \t]" + re.escape(WORKAROUND_TAG)
+ r"\((?P<ref>[^()\s]*)\)(?P<rest>.*)$"
)
WORKAROUND_ISSUE_RE = re.compile(r"#([1-9][0-9]{0,8})")
@dataclass(frozen=True)
class WorkaroundAnchor:
path: str
line: int
issue: int
summary: str
def to_json(self) -> dict[str, object]:
return {"path": self.path, "line": self.line, "issue": self.issue,
"summary": self.summary}
def workaround_in_scope(path: str) -> bool:
"""Whether the anchor scan covers `path` (relative to the repo root)."""
return (path.startswith(ANNEAL)
and not path.startswith(EXCLUDED_PREFIXES)
and Path(path).suffix in WORKAROUND_MARKERS)
def scan_workarounds(
path: str, text: str,
) -> tuple[list[WorkaroundAnchor], list[Finding]]:
"""Return one file's well-formed anchors and its malformed tags.
An anchor is a comment line, in the file's comment syntax, holding the
tag, a parenthesized positive `#NNNN` issue number, `: `, and a
non-empty summary. Every other tag-like spelling is a finding,
including the `#PENDING` placeholder.
"""
markers = WORKAROUND_MARKERS[Path(path).suffix]
anchors: list[WorkaroundAnchor] = []
out: list[Finding] = []
tag = WORKAROUND_TAG
for lineno, line in enumerate(text.split("\n"), start=1):
hits = WORKAROUND_LIKE_RE.findall(line)
if not hits:
continue
def bad(message: str) -> None:
out.append(Finding("rustc-workaround", message, path, lineno))
if len(hits) > 1:
bad(f"more than one `{tag}` tag on one line")
continue
m = WORKAROUND_ANCHOR_RE.match(line)
if m is None:
bad(f"malformed anchor; expected a comment line `<marker> {tag}"
"(#NNNN): <summary>` (see `docs/rustc-workarounds.md`)")
continue
if m.group("marker") not in markers:
bad(f"`{m.group('marker')}` is not a comment marker for "
f"`{Path(path).suffix}` files (use "
+ " or ".join(f"`{c}`" for c in markers) + ")")
continue
ref = m.group("ref")
if ref == WORKAROUND_PENDING:
bad(f"unresolved `{tag}({WORKAROUND_PENDING})`; file the "
"tracking issue and cite its number before Gate 1 converges")
continue
issue = WORKAROUND_ISSUE_RE.fullmatch(ref)
if issue is None:
bad(f"`{ref}` is not an issue reference; cite `#NNNN`")
continue
rest = m.group("rest")
summary = rest[2:].strip() if rest.startswith(": ") else ""
if not summary:
bad("anchor needs `: <summary>` after the issue reference")
continue
anchors.append(
WorkaroundAnchor(path, lineno, int(issue.group(1)), summary))
return anchors, out
def tree_workarounds(
git: Git, head: str,
) -> tuple[list[WorkaroundAnchor], list[Finding]]:
"""Scan every in-scope regular file in the tree at `head`."""
listing = git.run("ls-tree", "-r", "-z", "--full-tree", head, "--")
entries: list[tuple[str, str]] = []
for entry in listing.decode("utf-8", "surrogateescape").split("\0"):
if not entry:
continue
meta, path = entry.split("\t", 1)
mode, kind, sha = meta.split()
# Symlinks and gitlinks hold no source text to scan.
if (kind == "blob" and mode in ("100644", "100755")
and workaround_in_scope(path)):
entries.append((path, sha))
anchors: list[WorkaroundAnchor] = []
out: list[Finding] = []
if not entries:
return anchors, out
batch = git.run("cat-file", "--batch", stdin="".join(
f"{sha}\n" for _, sha in entries).encode())
pos = 0
for path, sha in entries:
nl = batch.find(b"\n", pos)
header = batch[pos:nl].decode("ascii", "replace").split()
if (nl < 0 or len(header) != 3 or header[0] != sha
or header[1] != "blob" or not header[2].isdigit()):
raise GitError(f"unexpected `git cat-file` output for {path!r}")
size = int(header[2])
data = batch[nl + 1:nl + 1 + size]
pos = nl + 1 + size + 1
a, f = scan_workarounds(path, data.decode("utf-8", "replace"))
anchors += a
out += f
return anchors, out
# ---------------------------------------------------------------------------
# Third-party notices (blocking) and advisory acknowledgments check.
def third_party_notices_findings(git: Git, head: str) -> list[Finding]:
"""Require dedicated `### `<root>/`` headings in `THIRD_PARTY_NOTICES.md`.
Scans the full Git tree at `head` so vendored trees (`vendor/<name>/`),
bundled font families (`site/static/fonts/<name>/`), and non-root
third-party license/notice files outside `docs/` and `site/templates/`
cannot silently bypass OSPO notices. First-party root license copies are
excluded by Git blob SHA.
"""
raw = git.run("ls-tree", "-r", "-z", "--full-tree", head)
blobs: dict[str, str] = {}
for entry in raw.decode("utf-8", "surrogateescape").split("\0"):
if not entry:
continue
meta, path = entry.split("\t", 1)
_, kind, sha = meta.split()
if kind == "blob":
blobs[path] = sha
fuchsia_shas = {
blobs[p] for p in FIRST_PARTY_COPYRIGHTED_LICENSE_PATHS if p in blobs
}
generic_apache_shas = {
blobs[p] for p in GENERIC_FIRST_PARTY_LICENSE_PATHS if p in blobs
}
dir_has_fuchsia_license: set[str] = set()
for path, sha in blobs.items():
if "/" in path and sha in fuchsia_shas:
dir_has_fuchsia_license.add(path.rsplit("/", 1)[0])
roots: set[str] = set()
for path in blobs:
parts = path.split("/")
if parts[0] == "vendor" and len(parts) >= 3:
roots.add(f"vendor/{parts[1]}/")
elif parts[:3] == ["site", "static", "fonts"] and len(parts) >= 5:
roots.add(f"site/static/fonts/{parts[3]}/")
fixed_roots = tuple(sorted(roots))
for path, sha in blobs.items():
parts = path.split("/")
if len(parts) < 2:
continue
if path.startswith(fixed_roots):
continue
if path.startswith(THIRD_PARTY_FALLBACK_EXCLUDED_PREFIXES):
continue
parent_dir = "/".join(parts[:-1])
if parent_dir in FIRST_PARTY_LICENSE_DIRS:
continue
name = parts[-1]
if not THIRD_PARTY_LICENSE_NAME_RE.match(name):
continue
if PurePosixPath(name).suffix.lower() in THIRD_PARTY_NON_LICENSE_SUFFIXES:
continue
if sha in fuchsia_shas:
continue
if (sha in generic_apache_shas
and parent_dir in dir_has_fuchsia_license):
continue
roots.add(parent_dir + "/")
notices = git.blob_text(head, THIRD_PARTY_NOTICES_PATH)
if notices is None:
return [Finding(
"third-party-notices",
f"missing `{THIRD_PARTY_NOTICES_PATH}`",
THIRD_PARTY_NOTICES_PATH,
)]
findings: list[Finding] = []
for root in sorted(roots):
heading_re = re.compile(
r"^###\s+`" + re.escape(root) + r"`", re.MULTILINE
)
if heading_re.search(notices) is None:
findings.append(Finding(
"third-party-notices",
f"committed third-party root `{root}` lacks a dedicated "
f"`### `{root}`` heading in `{THIRD_PARTY_NOTICES_PATH}`",
THIRD_PARTY_NOTICES_PATH,
))
return findings
def _ack_structural_hit(path: str) -> bool:
"""Return whether newly added `path` is a structural introduction signal."""
parts = path.split("/")
if parts[0] == "vendor" and len(parts) >= 3:
return True
if path.startswith(("site/static/fonts/", "site/static/images/")):
return True
if (len(parts) == 3 and parts[:2] == ["site", "static"]
and path.endswith(".svg")):
return True
if (len(parts) == 3 and parts[:2] == ["theory", "Anneal"]
and path.endswith(".lean") and not path.endswith("Canaries.lean")):
return True
if parts[-1] == "Cargo.toml":
return True
if (len(parts) == 3 and parts[0] == "tools"
and parts[2] in ("README.md", "main.cc", "main.c", "main.rs")):
return True
if (len(parts) == 4 and parts[:2] == [".github", "actions"]
and parts[3] == "action.yml"):
return True
if len(parts) == 3 and parts[:2] == ["ci", "pins"]:
return True
return False
def _ack_url_in_scope(path: str) -> bool:
if Path(path).suffix not in ACK_URL_EXTS:
return False
if path.startswith(ACK_URL_EXCLUDED_PREFIXES):
return False
if path in ACK_URL_EXCLUDED_PATHS:
return False
return not path.split("/")[-1].startswith("CHANGELOG")
def _extract_ack_urls(text: str) -> dict[str, str]:
"""Return `{normalized_key: display_url}` for external paper/GitHub URLs."""
found: dict[str, str] = {}
for m in ACK_ACADEMIC_URL_RE.finditer(text):
url = m.group(0).rstrip(".,;:")
found.setdefault(url.lower(), url)
for m in ACK_GITHUB_URL_RE.finditer(text):
owner = m.group(1)
repo = re.sub(r"\.git$", "", m.group(2).rstrip(".,;:"))
if not repo or owner in (".", "..") or repo in (".", ".."):
continue
if owner.lower() == "google":
continue
slug_low = f"{owner.lower()}/{repo.lower()}"
if slug_low in ACK_GITHUB_ALLOWLIST:
continue
found.setdefault(
f"github.com/{slug_low}", f"github.com/{owner}/{repo}"
)
return found
def acknowledgments_warning(
git: Git, merge_base: str, files: dict[str, ChangedFile],
) -> Finding | None:
"""Return a soft advisory warning when new modules/URLs skip acks.
Never returns a blocking finding. Suppressed immediately when
`ACKNOWLEDGMENTS.md` is modified in the PR.
"""
if ACK_PATH in files or any(
f.old_path == ACK_PATH for f in files.values()
):
return None
structural = sorted(
f.path for f in files.values()
if f.status == "A" and _ack_structural_hit(f.path)
)
added_urls: dict[str, str] = {}
removed_keys: set[str] = set()
for f in files.values():
if f.status != "D" and _ack_url_in_scope(f.path):
for line in f.added_text.values():
for key, disp in _extract_ack_urls(line).items():
added_urls.setdefault(key, disp)
rem_path = f.old_path or f.path
if f.removed_text and _ack_url_in_scope(rem_path):
for line in f.removed_text:
removed_keys.update(_extract_ack_urls(line))
base_credited: set[str] = set()
for ref_path in (ACK_PATH, "README.md"):
blob = git.blob_text(merge_base, ref_path)
if blob is not None:
base_credited.update(_extract_ack_urls(blob))
net_urls = sorted(
disp for key, disp in added_urls.items()
if key not in removed_keys and key not in base_credited
)
if not structural and not net_urls:
return None
details: list[str] = []
if structural:
shown = ", ".join(f"`{p}`" for p in structural[:4])
extra = (f" (+{len(structural) - 4} more)"
if len(structural) > 4 else "")
details.append(f"new module/asset path(s): {shown}{extra}")
if net_urls:
shown = ", ".join(f"`{u}`" for u in net_urls[:4])
extra = f" (+{len(net_urls) - 4} more)" if len(net_urls) > 4 else ""
details.append(f"net-new external URL(s): {shown}{extra}")
return Finding(
"acknowledgments",
f"PR adds {'; '.join(details)} without updating `{ACK_PATH}`; if an "
"external paper, tool, library, or asset offered substantial, "
f"distinctive help, consider adding an entry to `{ACK_PATH}` "
"(this check is advisory and does not fail the job)",
)
# ---------------------------------------------------------------------------
# Driver.
@dataclass
class Commit:
sha: str
author: str
committer: str
message: str
paths: list[str]
def load_commits(git: Git, merge_base: str, head: str) -> list[Commit]:
"""Load each commit in the range with NUL-delimited Git output."""
shas = git.text("rev-list", "--reverse", f"{merge_base}..{head}").split()
commits: list[Commit] = []
for sha in shas:
meta = git.run("show", "-s", "--format=%an <%ae>%x00%cn <%ce>%x00%B",
sha).decode("utf-8", "replace")
author, committer, message = meta.split("\x00", 2)
names = git.run("diff-tree", "-m", "-r", "--root", "--no-commit-id",
"--name-only", "--no-renames", "-z", sha)
paths = [p for p in names.decode("utf-8", "surrogateescape")
.split("\0") if p]
commits.append(Commit(sha, author, committer, message, paths))
return commits
def _is_graduation_move(f: ChangedFile) -> bool:
"""True when `f` moves a file out of `experimental/` or `Unstable/`."""
if f.status != "R" or f.old_path is None:
return False
if f.old_path.startswith("experimental/") and not f.path.startswith(
"experimental/"
):
return True
for prefix in _UNSTABLE_DIR_PREFIXES:
if f.old_path.startswith(prefix) and not f.path.startswith(prefix):
return True
return False
def _parse_unstable_toml(
content: str,
) -> tuple[str | None, datetime.date | None, str | None]:
"""Parse `review_by` and `gate_test` from a `docs/unstable/<id>.toml`."""
try:
data = tomllib.loads(content)
except tomllib.TOMLDecodeError as exc:
return str(exc), None, None
rev_date: datetime.date | None = None
raw = data.get("review_by")
if isinstance(raw, datetime.date) and not isinstance(raw, datetime.datetime):
rev_date = raw
elif isinstance(raw, str) and _ISO_DATE_RE.fullmatch(raw):
try:
rev_date = datetime.date.fromisoformat(raw)
except ValueError:
rev_date = None
gate_test: str | None = None
raw_gt = data.get("gate_test")
if isinstance(raw_gt, str) and raw_gt.strip():
gt_path = Path(raw_gt.strip())
if not gt_path.is_absolute() and ".." not in gt_path.parts:
gate_test = gt_path.as_posix()
return None, rev_date, gate_test
def unstable_review_date_findings(
git: Git,
merge_base: str,
head: str,
files: dict[str, ChangedFile],
today: datetime.date,
) -> list[Finding]:
"""Enforce 90-day `review_by` bounds and diff-scoped expiry blocking."""
out: list[Finding] = []
max_review = today + datetime.timedelta(days=UNSTABLE_MAX_REVIEW_DAYS)
lock_fail_closed, lock_changed_fids, candidate_lean_prefixes = (
_cartridges_lock_changed_candidates(git, merge_base, head, files)
)
rust_fail_closed, rust_changed_fids = _rust_code_site_changed_candidates(
git, merge_base, head, files
)
lean_tier_fids = _trusted_lean_touched_candidates(
git, merge_base, head, files
)
listing = git.run("ls-tree", "-r", "--name-only", "-z", head, "--",
"docs/unstable")
toml_paths = [
p for p in listing.decode("utf-8", "surrogateescape").split("\0")
if p and _UNSTABLE_TOML_RE.fullmatch(p)
]
for path in sorted(toml_paths):
m = _UNSTABLE_TOML_RE.fullmatch(path)
if m is None:
continue
fid = m.group(1)
content = git.blob_text(head, path)
if content is None:
continue
toml_err, rev_date, gate_test = _parse_unstable_toml(content)
if toml_err is not None:
if path in files:
out.append(Finding("unstable-review-date",
f"invalid TOML: {toml_err}", path))
continue
cf = files.get(path)
if cf is not None:
if rev_date is None:
out.append(Finding(
"unstable-review-date",
"`review_by` must be a valid ISO date (YYYY-MM-DD)",
path,
))
continue
if cf.status == "M":
base_content = git.blob_text(merge_base, path)
if base_content is None:
review_changed = True
else:
base_err, base_date, _ = _parse_unstable_toml(base_content)
review_changed = base_err is not None or base_date != rev_date
else:
review_changed = True
if review_changed and rev_date > max_review:
out.append(Finding(
"unstable-review-date",
f"`review_by` ({rev_date.isoformat()}) exceeds the "
f"{UNSTABLE_MAX_REVIEW_DAYS}-day maximum "
f"({max_review.isoformat()})",
path,
))
continue
if rev_date is not None and rev_date < today:
efid = re.escape(fid)
structured_pat = re.compile(
rf"(?:candidate:{efid}\b"
rf"|(?:Anneal|Specs)\.Unstable\.{efid}\b"
rf"|anneal::feature\(\s*\"?{efid}\"?\s*\)"
rf"|(?:^|[\s`'\"(=])(?:-Z(?:=|\s*)?"
rf"|(?:--anneal-unstable|--allow-anneal-unstable)(?:=|\s+))"
rf"{efid}\b"
rf"|docs/unstable/{efid}\.(?:toml|md)\b)"
)
code_site_pat = re.compile(rf'"(?:candidate:)?{efid}"')
feature_prefixes = (
f"docs/unstable/{fid}.",
f"theory/Anneal/Unstable/{fid}/",
f"theory/Anneal/Unstable/{fid}.lean",
f"Specs/Unstable/{fid}/",
f"Specs/Unstable/{fid}.lean",
f"specs/Specs/Unstable/{fid}/",
f"specs/Specs/Unstable/{fid}.lean",
*candidate_lean_prefixes.get(fid, ()),
)
touches_feature = (
lock_fail_closed
or rust_fail_closed
or fid in lock_changed_fids
or fid in rust_changed_fids
or fid in lean_tier_fids
)
for f in files.values():
if touches_feature:
break
if f.path.startswith(feature_prefixes) or (
f.old_path is not None
and f.old_path.startswith(feature_prefixes)
):
touches_feature = True
break
if gate_test is not None and (
f.path == gate_test or f.old_path == gate_test
):
touches_feature = True
break
if any(
structured_pat.search(ln) for ln in f.added_text.values()
):
touches_feature = True
break
if (
f.path in _UNSTABLE_CODE_SITE_FILES
or f.old_path in _UNSTABLE_CODE_SITE_FILES
) and any(
code_site_pat.search(ln) for ln in f.added_text.values()
):
touches_feature = True
break
if touches_feature:
out.append(Finding(
"unstable-review-date",
f"unstable feature `{fid}` passed `review_by` "
f"({rev_date.isoformat()}); renew, graduate, or delete it",
path,
))
return out
def _parse_experiment_toml(
content: str,
) -> tuple[str | None, datetime.date | None]:
"""Parse `experimental/<slug>/EXPERIMENT.toml` and extract `expires_on`."""
try:
data = tomllib.loads(content)
except tomllib.TOMLDecodeError as e:
return str(e), None
raw = data.get("expires_on")
exp_date: datetime.date | None = None
if isinstance(raw, datetime.date) and not isinstance(
raw, datetime.datetime
):
exp_date = raw
elif isinstance(raw, str) and _ISO_DATE_RE.fullmatch(raw):
try:
exp_date = datetime.date.fromisoformat(raw)
except ValueError:
exp_date = None
return None, exp_date
def experimental_pr_findings(
git: Git,
merge_base: str,
head: str,
files: dict[str, ChangedFile],
numstat: list[tuple[str, int | None]],
today: datetime.date,
) -> list[Finding]:
"""Enforce experimental PR size caps and dated `expires_on` bounds."""
out: list[Finding] = []
exp_files = 0
exp_lines = 0
for path, changed in numstat:
if not path.startswith("experimental/"):
continue
cf = files.get(path)
if cf is not None and cf.status == "D":
# Pure deletions (cleanup or graduation) never trip size caps.
continue
exp_files += 1
exp_lines += changed or 0
if exp_files > EXPERIMENTAL_MAX_FILES:
out.append(Finding(
"experimental-size",
f"{exp_files} changed files under `experimental/` exceed the "
f"{EXPERIMENTAL_MAX_FILES}-file cap",
))
if exp_lines > EXPERIMENTAL_MAX_LINES:
out.append(Finding(
"experimental-size",
f"{exp_lines} changed lines under `experimental/` exceed the "
f"{EXPERIMENTAL_MAX_LINES}-line cap",
))
max_expiry = today + datetime.timedelta(days=EXPERIMENTAL_MAX_EXPIRY_DAYS)
for f in files.values():
if f.status == "D" or not _EXP_TOML_RE.fullmatch(f.path):
continue
content = git.blob_text(head, f.path)
if content is None:
continue
toml_err, exp_date = _parse_experiment_toml(content)
if toml_err is not None:
out.append(Finding(
"experimental-expiry", f"invalid TOML: {toml_err}", f.path,
))
continue
if exp_date is None:
out.append(Finding(
"experimental-expiry",
"`expires_on` must be a valid ISO date (YYYY-MM-DD)",
f.path,
))
continue
if f.status == "M":
base_content = git.blob_text(merge_base, f.path)
if base_content is None:
expiry_changed = True
else:
base_err, base_date = _parse_experiment_toml(base_content)
expiry_changed = base_err is not None or base_date != exp_date
else:
expiry_changed = True
if not expiry_changed:
continue
if exp_date < today:
out.append(Finding(
"experimental-expiry",
f"`expires_on` ({exp_date.isoformat()}) is in the past "
f"(today is {today.isoformat()})",
f.path,
))
elif exp_date > max_expiry:
out.append(Finding(
"experimental-expiry",
f"`expires_on` ({exp_date.isoformat()}) exceeds the "
f"{EXPERIMENTAL_MAX_EXPIRY_DAYS}-day maximum "
f"({max_expiry.isoformat()})",
f.path,
))
return out
SKILL_HYGIENE_GLOBS: tuple[re.Pattern[str], ...] = (
re.compile(r"^skills/[^/]+/.+(?i:\.md)\Z", re.ASCII | re.DOTALL),
re.compile(
r"^trusted/cargo-anneal/skills/[^/]+/.+(?i:\.md)\Z",
re.ASCII | re.DOTALL,
),
re.compile(
r"^trusted/cargo-anneal/assets/skills/[^/]+/.+(?i:\.md)\Z",
re.ASCII | re.DOTALL,
),
)
_SKILL_HYGIENE_ROOT_PREFIXES: tuple[str, ...] = (
"skills/",
"trusted/cargo-anneal/skills/",
"trusted/cargo-anneal/assets/skills/",
)
_SKILL_HYGIENE_ANCESTOR_PATHS: frozenset[str] = frozenset({
"skills",
"trusted",
"trusted/cargo-anneal",
"trusted/cargo-anneal/skills",
"trusted/cargo-anneal/assets",
"trusted/cargo-anneal/assets/skills",
})
SKILL_HYGIENE_SCOPE2_SKILLS: frozenset[str] = frozenset({
"cargo-anneal",
"spec-authoring",
"proof-scaling",
"boundary-audit",
})
# Generic public-sink lexicon and leak patterns aligned with
# `ci/pr_preflight.py::PUBLIC_LEAK_PATTERNS`. Organization- or host-specific
# patterns belong out of tree via `ANNEAL_PREFLIGHT_EXTRA_LEAK_REGEX` or
# `ANNEAL_PREFLIGHT_EXTRA_LEAK_FILE`.
SKILL_HYGIENE_SCOPE1: tuple[tuple[str, re.Pattern[str]], ...] = (
("s1:lexicon:DisclosedGap", re.compile(r"\bDisclosedGap\b", re.ASCII)),
("s1:lexicon:Ring-N", re.compile(r"\bRing[- ][01]\b", re.ASCII)),
(
"s1:leak:shortlink",
re.compile(
r"(?:(?<![A-Za-z0-9_./-])|(?<=://))"
r"\b(?:b|cl|yaqs|xid|fxr)/\d{4,}\b",
re.ASCII,
),
),
(
"s1:leak:go",
re.compile(
r"(?:(?<![A-Za-z0-9_./-])|(?<=://))\bgo/"
r"(?![A-Za-z0-9_-]+\.(?:rs|py|go|lean|md|toml|json|yaml|yml|"
r"sh|c|h|cc|cpp|hpp|txt|lock)\b)"
r"[A-Za-z0-9][A-Za-z0-9_-]{2,}",
re.ASCII,
),
),
(
"s1:leak:conversation_uri",
re.compile(r"conversation" r"://", re.IGNORECASE | re.ASCII),
),
)
_SKILL_TAG_CHARS = r"[^/ \t\n\r\f\x0b\x60)\]|\"']+"
_SKILL_ANNEAL_PATH_RE = re.compile(
r"anneal/(?:docs|trusted|ci|theory)/",
re.ASCII,
)
_SKILL_PINNED_ZC_ANNEAL_RE = re.compile(
rf"^https://github\.com/google/zerocopy/(?:blob|tree)/"
rf"anneal-v{_SKILL_TAG_CHARS}/$",
re.ASCII,
)
_SKILL_PINNED_REPO_ROOT_RE = re.compile(
rf"^(?:https://github\.com/google/anneal/(?:blob|tree)/"
rf"anneal-v{_SKILL_TAG_CHARS}/|"
rf"https://github\.com/google/zerocopy/(?:blob|tree)/"
rf"anneal-v{_SKILL_TAG_CHARS}/anneal/)$",
re.ASCII,
)
_SKILL_ANNEAL_REPO_URL_RE = re.compile(
rf"https://github\.com/google/anneal/(?:blob|tree)/(?=({_SKILL_TAG_CHARS}))",
re.ASCII,
)
# Bare `Pass 1` / `Pass 2` internal review-stage shorthand is banned in
# standalone bundled skills; hyphenated `Pass-1` (as in `Pass-1`
# `TrustedBoundary`) is intentionally sanctioned for the Lean theory phase.
_SKILL_SCOPE2_PRE: tuple[tuple[str, re.Pattern[str]], ...] = (
("s2:lexicon:bare-Pass-N", re.compile(r"\bPass [12]\b", re.ASCII)),
)
_SKILL_THEORY_MANIFEST_RE = re.compile(
r"(?<!share/anneal/)theory/bundle-manifest\.json",
re.ASCII,
)
_SKILL_THEORY_PATH_RE = re.compile(
r"(?<!anneal/)(?<![A-Za-z0-9_.-])theory/",
re.ASCII,
)
_SKILL_SCOPE2_POST: tuple[tuple[str, re.Pattern[str]], ...] = (
("s2:path:../../skills", re.compile(r"\.\./\.\./skills", re.ASCII)),
(
"s2:path:../../THREAT_MODEL.md",
re.compile(r"\.\./\.\./THREAT_MODEL\.md", re.ASCII),
),
)
_SKILL_LOCAL_DOC_RE = re.compile(r"(?:docs/alpha\.md|THREAT_MODEL\.md)", re.ASCII)
_SKILL_PINNED_DOC_PREFIX_RE = re.compile(
rf"^(?:https://github\.com/google/anneal/blob/anneal-v{_SKILL_TAG_CHARS}/|"
rf"https://github\.com/google/zerocopy/blob/"
rf"anneal-v{_SKILL_TAG_CHARS}/anneal/)$",
re.ASCII,
)
def _skill_token_prefix(line: str, pos: int) -> str:
start = max((line.rfind(ch, 0, pos) for ch in " \t(\x60[\"'|"), default=-1) + 1
return line[start:pos].lstrip("<")
def _skill_has_unbundled_anneal_path(line: str) -> bool:
for m in _SKILL_ANNEAL_PATH_RE.finditer(line):
if line[:m.start()].endswith("share/"):
continue
if _SKILL_PINNED_ZC_ANNEAL_RE.match(_skill_token_prefix(line, m.start())):
continue
return True
return False
def _skill_has_unpinned_anneal_repo_url(line: str) -> bool:
for m in _SKILL_ANNEAL_REPO_URL_RE.finditer(line):
ref = m.group(1)
if "<" not in ref:
ref = ref.rstrip(">")
if not ref:
continue
if not (ref.startswith("anneal-v") and len(ref) > len("anneal-v")):
return True
return False
def _skill_has_unbundled_theory_match(line: str, pat: re.Pattern[str]) -> bool:
for m in pat.finditer(line):
if _SKILL_PINNED_REPO_ROOT_RE.match(_skill_token_prefix(line, m.start())):
continue
return True
return False
def _skill_count_unpinned_local_docs(line: str) -> int:
count = 0
for m in _SKILL_LOCAL_DOC_RE.finditer(line):
if (
m.group(0) == "THREAT_MODEL.md"
and line[:m.start()].endswith("../../")
):
continue
if not _SKILL_PINNED_DOC_PREFIX_RE.match(
_skill_token_prefix(line, m.start())):
count += 1
return count
def skill_hygiene_line_findings(line: str, scope2: bool) -> list[str]:
"""Return rule labels triggered on one skill Markdown line.
Keep semantics in lockstep with `bundled_skills_public_and_lexicon_hygiene`
in `trusted/cargo-anneal/src/cli.rs` (both use ASCII token boundaries).
"""
out: list[str] = []
for label, pat in SKILL_HYGIENE_SCOPE1:
if pat.search(line):
out.append(label)
if scope2:
for label, pat in _SKILL_SCOPE2_PRE:
if pat.search(line):
out.append(label)
if _skill_has_unbundled_anneal_path(line):
out.append("s2:path:anneal/{docs,trusted,ci,theory}")
if _skill_has_unpinned_anneal_repo_url(line):
out.append("s2:path:unpinned-repo-url")
if _skill_has_unbundled_theory_match(line, _SKILL_THEORY_MANIFEST_RE):
out.append("s2:path:theory/bundle-manifest")
if _skill_has_unbundled_theory_match(line, _SKILL_THEORY_PATH_RE):
out.append("s2:path:theory/")
for label, pat in _SKILL_SCOPE2_POST:
if pat.search(line):
out.append(label)
for _ in range(_skill_count_unpinned_local_docs(line)):
out.append("s2:path:unpinned-local-doc")
return out
def _skill_name_from_path(path: str) -> str:
parts = path.split("/")
if path.startswith("trusted/cargo-anneal/assets/skills/"):
return parts[4]
if path.startswith("trusted/cargo-anneal/skills/"):
return parts[3]
return parts[1]
def skill_hygiene_findings(git: Git, head: str) -> list[Finding]:
"""Enforce Scope-1 and Scope-2 hygiene across committed skill Markdown."""
listing = git.run("ls-tree", "-r", "-z", "--full-tree", head, "--")
entries: list[tuple[str, str, str]] = []
out: list[Finding] = []
for rec in listing.decode("utf-8", "surrogateescape").split("\0"):
if not rec or "\t" not in rec:
continue
meta, path = rec.split("\t", 1)
in_skill_tree = (
path in _SKILL_HYGIENE_ANCESTOR_PATHS
or path.startswith(_SKILL_HYGIENE_ROOT_PREFIXES)
)
if not in_skill_tree:
continue
parts = meta.split(" ", 2)
mode, kind = (parts[0], parts[1]) if len(parts) >= 2 else ("", "")
if kind != "blob" or mode not in ("100644", "100755"):
out.append(Finding(
"skill-hygiene:non-regular-entry",
f"expected regular file blob, got {mode} {kind}",
path,
1,
))
continue
if any(g.match(path) for g in SKILL_HYGIENE_GLOBS):
entries.append((mode, kind, path))
scope2_skills = set(SKILL_HYGIENE_SCOPE2_SKILLS)
for _, _, path in entries:
if path.startswith("trusted/cargo-anneal/"):
scope2_skills.add(_skill_name_from_path(path))
for _, _, path in entries:
content = git.blob_text(head, path)
if content is None:
out.append(Finding(
"skill-hygiene:non-regular-entry",
"could not read skill blob text",
path,
1,
))
continue
scope2 = _skill_name_from_path(path) in scope2_skills
lines = content.split("\n")
if lines and lines[-1] == "":
lines.pop()
for lineno, raw_line in enumerate(lines, 1):
line = raw_line[:-1] if raw_line.endswith("\r") else raw_line
labels = skill_hygiene_line_findings(line, scope2)
line_has_leak = any(lbl.startswith("s1:leak:") for lbl in labels)
for label in labels:
msg = (
f"matched {label} (line redacted)"
if line_has_leak
else line.strip()[:120]
)
out.append(Finding(
f"skill-hygiene:{label}",
msg,
path,
lineno,
))
return out
def check(
git: Git, base: str | None, head_ref: str, *,
pr_body: str | None = None, pr_title: str | None = None,
receipt: str | None = None, expected_author: str | None = None,
skip_commit_messages: bool = False,
forbid_pr_title: tuple[re.Pattern[str], str] | None = None,
) -> tuple[Report, dict[str, object]]:
"""Run every check; `forbid_pr_title` is a (pattern, title) pair."""
report = Report()
head = git.commit(head_ref)
tree = git.tree(head)
meta: dict[str, object] = {"head": head, "tree": tree}
report.findings += agents_budget_findings(git, head)
report.findings += crr_parity_findings(git, head)
report.findings += skill_hygiene_findings(git, head)
report.findings += tree_workarounds(git, head)[1]
report.findings += third_party_notices_findings(git, head)
if forbid_pr_title is not None:
report.findings += pr_title_policy_findings(*forbid_pr_title)
if base is None:
if (pr_body is not None or receipt is not None
or pr_title is not None or expected_author is not None):
raise GitError("--pr-body-file, --pr-title, --receipt-file, and "
"--expected-author require --base")
return report, meta
target_base = git.commit(base)
merge_base = git.text("merge-base", target_base, head).strip()
if not merge_base:
raise GitError("--base and --head share no merge base")
meta.update(target_base=target_base, merge_base=merge_base)
# Commit dates are author-controlled, so bound header years and expiry
# windows by the checker's own UTC clock.
today = datetime.datetime.now(datetime.timezone.utc).date()
max_year = today.year
commits = load_commits(git, merge_base, head)
meta["commits"] = [c.sha for c in commits]
for c in commits:
if not any(in_anneal_scope(p) for p in c.paths):
report.warnings.append(Finding(
"commit-message", "skipped: touches no Anneal paths",
f"commit {c.sha[:12]}",
))
continue
if expected_author is not None:
for role, ident in (("author", c.author),
("committer", c.committer)):
if ident != expected_author:
report.findings.append(Finding(
"commit-author",
f"{role} is `{ident}`, expected `{expected_author}`",
f"commit {c.sha[:12]}",
))
if skip_commit_messages:
report.warnings.append(Finding(
"commit-message", "skipped: --skip-commit-messages",
f"commit {c.sha[:12]}",
))
continue
report.findings += commit_message_findings(c.sha, c.message)
files = changed_files(git, merge_base, head)
scoped = [f for f in files.values() if in_anneal_scope(f.path)]
check_paths = [f.path for f in scoped if f.status != "D"]
if check_paths:
res = git.run(
"diff", "--no-ext-diff", "--no-textconv", "--check", merge_base,
head, "--", *check_paths, ok_codes=(0, 2),
).decode("utf-8", "replace")
parsed = 0
for line in res.splitlines():
m = re.match(r"^(.+?):(\d+): (.*)$", line)
if m:
parsed += 1
report.findings.append(Finding(
"diff-check", m.group(3), m.group(1), int(m.group(2))
))
if res.strip() and not parsed:
raise GitError(f"unparsed `git diff --check` output: {res[:200]}")
for f in scoped:
if f.status == "D":
continue
graduated_move = _is_graduation_move(f)
needs_content = (
Path(f.path).suffix in HEADER_EXTS
or f.path.startswith(TRUSTED_CITATION_PREFIXES)
)
if not needs_content or not (
f.added or f.status == "A" or graduated_move
):
continue
content = git.blob_text(head, f.path)
if content is None:
raise GitError(f"cannot read {head[:12]}:{f.path}")
if (f.status == "A" or graduated_move) and needs_header(f.path):
report.findings += header_findings(f.path, content, max_year)
raw_lines = content.split("\n")
if raw_lines and raw_lines[-1] == "":
raw_lines = raw_lines[:-1]
check_lines = (
set(range(1, len(raw_lines) + 1))
if graduated_move
else f.added
)
wrap_errs, wrap_warns = wrap_findings(f.path, content, check_lines)
report.findings += wrap_errs
report.warnings += wrap_warns
if f.path.startswith(TRUSTED_CITATION_PREFIXES):
citation_lines = (
{i: ln for i, ln in enumerate(raw_lines, 1)}
if graduated_move
else f.added_text
)
report.findings += trusted_citation_findings(
f.path, content, citation_lines)
if ((f.status == "A" or graduated_move)
and Path(f.path).suffix in (".lean", ".rs")
and not SHA40_RE.search(content)):
report.warnings.append(Finding(
"trusted-citation",
"new trusted file cites no pinned upstream SHA; code "
"modeling Rust semantics or `rustc` structures must "
"quote and cite `rust-lang/{rust,reference}`",
f.path,
))
single = commits[0] if len(commits) == 1 else None
if pr_body is not None:
if single is None:
report.warnings.append(Finding(
"pr-body", f"skipped: {len(commits)} commits; compare the "
"description with the squash message by hand",
))
else:
body = single.message.split("\n", 1)[1] if "\n" in (
single.message) else ""
report.findings += pr_body_findings(
commit_body_to_pr_body(body), pr_body)
if pr_title is not None:
if single is None:
report.warnings.append(Finding(
"pr-title", f"skipped: {len(commits)} commits",
))
else:
subject = single.message.split("\n", 1)[0]
if pr_title.strip() != subject.strip():
report.findings.append(Finding(
"pr-title",
f"PR title {pr_title.strip()!r} differs from the commit "
f"subject {subject.strip()!r}",
))
# A rename changes its source too: moving a file out of the review
# surface changes that surface.
changed = ({p.path for p in files.values()}
| {p.old_path for p in files.values() if p.old_path})
hrs = sorted(
{p.path for p in files.values()
if is_hrs_path(p.path, p.trust_tier_changed)}
| {p.old_path for p in files.values()
if p.old_path is not None
and is_hrs_path(p.old_path, p.trust_tier_changed)}
)
meta["human_review_surface"] = hrs
raw_numstat = diff_numstat(git, merge_base, head)
# Anti-laundering: moving a file out of `experimental/` or `Unstable/`
# counts 100% of its lines as reviewable additions rather than a rename.
adjusted_numstat: list[tuple[str, int | None]] = []
for path, changed_lines in raw_numstat:
cf = files.get(path)
if (
cf is not None
and _is_graduation_move(cf)
and changed_lines is not None
):
blob = git.blob_text(head, path)
if blob is not None:
blob_lines = blob.split("\n")
if blob_lines and blob_lines[-1] == "":
blob_lines = blob_lines[:-1]
changed_lines = max(changed_lines, len(blob_lines))
adjusted_numstat.append((path, changed_lines))
report.findings += experimental_pr_findings(
git, merge_base, head, files, adjusted_numstat, today,
)
report.findings += unstable_review_date_findings(
git, merge_base, head, files, today)
size = review_size(adjusted_numstat)
meta["review_size"] = size.to_json()
size_warning = review_size_warning(size)
if size_warning is not None:
report.warnings.append(size_warning)
ack_warning = acknowledgments_warning(git, merge_base, files)
if ack_warning is not None:
report.warnings.append(ack_warning)
if receipt is not None:
report.findings += receipt_findings(
receipt, target_base, merge_base, head, tree, hrs, changed)
apply_waivers(report, *parse_waivers(commits, head))
return report, meta
def _read(path: str) -> str:
if path == "-":
return sys.stdin.read()
return Path(path).read_text(encoding="utf-8")
def _escape_command_data(text: str) -> str:
"""Escape a GitHub Actions workflow command's message."""
return (text.replace("%", "%25").replace("\r", "%0D")
.replace("\n", "%0A"))
def _escape_command_property(text: str) -> str:
return (_escape_command_data(text).replace(":", "%3A")
.replace(",", "%2C"))
def waiver_annotation(w: Waived) -> str:
"""Render a waived finding as a `::warning::` workflow command.
Commit-message findings name a commit rather than a file, so they carry
no `file` property.
"""
f = w.finding
props = ["title=" + _escape_command_property(
f"PR hygiene waived: {f.check}")]
if f.path is not None and f.check != "commit-message":
props.insert(0, "file=" + _escape_command_property(f.path))
if f.line is not None:
props.insert(1, f"line={f.line}")
msg = f"{f.render()} (waived by {WAIVER_KEY}: {w.reason})"
return f"::warning {','.join(props)}::{_escape_command_data(msg)}"
def _summary_cell(text: str) -> str:
text = html.escape(" ".join(text.split()), quote=False)
return text.replace("|", "&#124;")
def waiver_summary(waived: list[Waived]) -> str:
"""Render waived findings as a Markdown step summary table."""
rows = [
"",
f"### PR hygiene: {len(waived)} waived finding(s)",
"",
"| Check | Location | Finding | Reason |",
"| --- | --- | --- | --- |",
]
for w in waived:
f = w.finding
where = f.path or ""
if f.path is not None and f.line is not None:
where = f"{f.path}:{f.line}"
rows.append("| " + " | ".join(_summary_cell(t) for t in (
f.check, where, f.message, w.reason)) + " |")
return "\n".join(rows) + "\n"
def main(argv: list[str]) -> int:
parser = argparse.ArgumentParser(
description=__doc__.split("\n\n", 1)[0],
)
parser.add_argument("--repo", default=".",
help="any path inside the repository (default: .)")
parser.add_argument("--base", help="target base ref (enables diff, "
"commit, PR, and receipt checks)")
parser.add_argument("--head", default="HEAD", help="head ref to check")
parser.add_argument("--pr-body-file",
help="PR description to compare ('-' for stdin)")
parser.add_argument("--pr-title", help="PR title to compare")
parser.add_argument("--forbid-pr-title-regex", metavar="REGEX",
help="fail if the PR title (--pr-title, else the "
"PR_TITLE environment variable) matches REGEX")
parser.add_argument("--receipt-file",
help="Review Receipt comment to check ('-' for stdin)")
parser.add_argument("--expected-author", metavar="'NAME <EMAIL>'",
help="require this commit author and committer")
parser.add_argument("--skip-commit-messages", action="store_true",
help="skip commit-message checks (for PRs whose "
"authenticated author is a bot)")
parser.add_argument("--emit-pr-body", action="store_true",
help="print the expected PR description for --head "
"and exit")
parser.add_argument("--emit-receipt-footer", action="store_true",
help="print the `anneal-receipt:v1` footer for "
"--base and --head and exit")
parser.add_argument("--json", action="store_true",
help="emit a machine-readable report")
args = parser.parse_args(argv)
try:
repo = Path(args.repo).resolve()
git = Git(Git(repo).toplevel())
if args.emit_pr_body:
head = git.commit(args.head)
body = git.text("log", "-1", "--format=%b", head)
sys.stdout.write(commit_body_to_pr_body(body))
return 0
# An empty value (say, from an unset shell variable) must fail
# rather than silently skip a check.
for flag, value in (("--pr-body-file", args.pr_body_file),
("--receipt-file", args.receipt_file),
("--expected-author", args.expected_author)):
if value is not None and not value.strip():
raise GitError(f"{flag} must not be empty")
pr_body = (_read(args.pr_body_file)
if args.pr_body_file is not None else None)
receipt = (_read(args.receipt_file)
if args.receipt_file is not None else None)
forbid_pr_title = None
if args.forbid_pr_title_regex is not None:
if not args.forbid_pr_title_regex:
raise GitError("--forbid-pr-title-regex must not be empty")
try:
pattern = re.compile(args.forbid_pr_title_regex)
except re.error as e:
raise GitError(
f"invalid --forbid-pr-title-regex: {e}") from e
title = (args.pr_title if args.pr_title is not None
else os.environ.get("PR_TITLE"))
if title is None or not title.strip():
raise GitError("--forbid-pr-title-regex needs a non-empty "
"PR title in --pr-title or PR_TITLE")
forbid_pr_title = (pattern, title)
report, meta = check(
git, args.base, args.head, pr_body=pr_body,
pr_title=args.pr_title, receipt=receipt,
expected_author=args.expected_author,
skip_commit_messages=args.skip_commit_messages,
forbid_pr_title=forbid_pr_title,
)
if args.emit_receipt_footer:
if args.base is None:
raise GitError("--emit-receipt-footer requires --base")
sys.stdout.write(receipt_footer(
meta["target_base"], meta["merge_base"], meta["head"],
meta["tree"], meta["human_review_surface"]))
return 0
except (GitError, OSError, UnicodeDecodeError) as e:
if args.json:
print(json.dumps({"schema": SCHEMA, "ok": False, "error": str(e)}))
else:
print(f"check_pr_hygiene: error: {e}", file=sys.stderr)
return 2
ok = not report.findings
# Waivers are loud: one annotation per waived finding, plus a step
# summary table in GitHub Actions. With --json, stdout is the report.
annotations = sys.stderr if args.json else sys.stdout
for w in report.waived:
print(waiver_annotation(w), file=annotations)
# Surface advisory checks on the PR without failing the job.
if os.environ.get("GITHUB_ACTIONS") == "true":
advisory_titles = {
"review-size": "PR review-size budget",
"acknowledgments": "Acknowledgments check",
}
for w in report.warnings:
raw_title = advisory_titles.get(w.check)
if raw_title is not None:
title = _escape_command_property(raw_title)
print(f"::warning title={title}::"
f"{_escape_command_data(w.message)}", file=annotations)
summary_path = os.environ.get("GITHUB_STEP_SUMMARY")
if summary_path and report.waived:
try:
with open(summary_path, "a", encoding="utf-8") as f:
f.write(waiver_summary(report.waived))
except OSError as e:
print(f"check_pr_hygiene: error: cannot write step summary: {e}",
file=sys.stderr)
return 2
if args.json:
print(json.dumps({
"schema": SCHEMA,
"ok": ok,
**meta,
"findings": [f.to_json() for f in report.findings],
"warnings": [w.to_json() for w in report.warnings],
"waived_findings": [w.to_json() for w in report.waived],
"opt_outs": [o.to_json() for o in report.opt_outs],
}, indent=2))
else:
for w in report.warnings:
print(f"warning: {w.render()}", file=sys.stderr)
for f in report.findings:
print(f.render(), file=sys.stderr)
waived = (f" ({len(report.waived)} waived)" if report.waived
else "")
if ok:
print(f"PR hygiene checks passed{waived}.")
else:
print(f"{len(report.findings)} PR hygiene finding(s){waived}.",
file=sys.stderr)
return 0 if ok else 1
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))