|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Validate that non-axiom trusted surfaces are documented. |
| 3 | +
|
| 4 | +The Lean kernel axiom registry is intentionally separate from Verity's broader |
| 5 | +trusted surface. This checker covers mechanisms that do not create project |
| 6 | +Lean axioms, but still matter for audit soundness: |
| 7 | +
|
| 8 | +* `native_decide` |
| 9 | +* `@[implemented_by ...]` |
| 10 | +* `partial def` |
| 11 | +* explicit ECM assumption strings in `axioms := [...]` |
| 12 | +""" |
| 13 | + |
| 14 | +from __future__ import annotations |
| 15 | + |
| 16 | +import re |
| 17 | +from pathlib import Path |
| 18 | + |
| 19 | +from property_utils import ROOT, report_errors, scrub_lean_code, strip_lean_comments |
| 20 | + |
| 21 | +SCAN_ROOTS = ("Compiler", "Verity", "Contracts", "Benchmark") |
| 22 | +DOC_PATHS = ("AXIOMS.md", "TRUST_ASSUMPTIONS.md") |
| 23 | +DOC_DIRS = ("docs",) |
| 24 | + |
| 25 | +ECM_AXIOM_STRING_RE = re.compile(r'"([^"]+)"') |
| 26 | + |
| 27 | +MECHANISM_PATTERNS = ( |
| 28 | + ("native_decide", re.compile(r"\bnative_decide\b")), |
| 29 | + ("@[implemented_by", re.compile(r"@\[\s*implemented_by\b")), |
| 30 | + ("partial def", re.compile(r"\bpartial\s+def\b")), |
| 31 | +) |
| 32 | + |
| 33 | + |
| 34 | +def _iter_lean_files(root: Path = ROOT) -> list[Path]: |
| 35 | + files: list[Path] = [] |
| 36 | + for subdir in SCAN_ROOTS: |
| 37 | + base = root / subdir |
| 38 | + if base.exists(): |
| 39 | + files.extend(sorted(base.rglob("*.lean"))) |
| 40 | + return files |
| 41 | + |
| 42 | + |
| 43 | +def _read_doc_corpus(root: Path = ROOT) -> str: |
| 44 | + chunks: list[str] = [] |
| 45 | + for rel in DOC_PATHS: |
| 46 | + path = root / rel |
| 47 | + if path.exists(): |
| 48 | + chunks.append(path.read_text(encoding="utf-8")) |
| 49 | + for rel in DOC_DIRS: |
| 50 | + base = root / rel |
| 51 | + if base.exists(): |
| 52 | + for path in sorted(base.rglob("*.md")): |
| 53 | + chunks.append(path.read_text(encoding="utf-8")) |
| 54 | + return "\n".join(chunks) |
| 55 | + |
| 56 | + |
| 57 | +def collect_trust_surface(root: Path = ROOT) -> tuple[dict[str, int], dict[str, tuple[str, int]]]: |
| 58 | + mechanisms = {name: 0 for name, _ in MECHANISM_PATTERNS} |
| 59 | + ecm_axioms: dict[str, tuple[str, int]] = {} |
| 60 | + |
| 61 | + for path in _iter_lean_files(root): |
| 62 | + raw_text = path.read_text(encoding="utf-8") |
| 63 | + text = scrub_lean_code(raw_text) |
| 64 | + text_without_comments = strip_lean_comments(raw_text) |
| 65 | + rel = str(path.relative_to(root)) |
| 66 | + for name, pattern in MECHANISM_PATTERNS: |
| 67 | + mechanisms[name] += len(pattern.findall(text)) |
| 68 | + for match in re.finditer(r"axioms\s*:=\s*\[(.*?)\]", text_without_comments, flags=re.DOTALL): |
| 69 | + line_no = text_without_comments.count("\n", 0, match.start()) + 1 |
| 70 | + for axiom_name in ECM_AXIOM_STRING_RE.findall(match.group(1)): |
| 71 | + ecm_axioms.setdefault(axiom_name, (rel, line_no)) |
| 72 | + |
| 73 | + return mechanisms, ecm_axioms |
| 74 | + |
| 75 | + |
| 76 | +def main() -> int: |
| 77 | + docs = _read_doc_corpus() |
| 78 | + mechanisms, ecm_axioms = collect_trust_surface() |
| 79 | + |
| 80 | + errors: list[str] = [] |
| 81 | + for name, count in mechanisms.items(): |
| 82 | + if count > 0 and name not in docs: |
| 83 | + errors.append( |
| 84 | + f"{name}: {count} occurrence(s) found in Lean sources but missing from trust docs" |
| 85 | + ) |
| 86 | + |
| 87 | + for axiom_name, (rel, line) in sorted(ecm_axioms.items()): |
| 88 | + if axiom_name not in docs: |
| 89 | + errors.append( |
| 90 | + f"{axiom_name}: ECM assumption from {rel}:{line} is missing from trust docs" |
| 91 | + ) |
| 92 | + |
| 93 | + report_errors(errors, "Trust-surface registry check failed") |
| 94 | + print( |
| 95 | + "OK: trust-surface registry documents " |
| 96 | + f"{len(ecm_axioms)} ECM assumption(s), " |
| 97 | + + ", ".join(f"{name}={count}" for name, count in mechanisms.items()) |
| 98 | + ) |
| 99 | + return 0 |
| 100 | + |
| 101 | + |
| 102 | +if __name__ == "__main__": |
| 103 | + raise SystemExit(main()) |
0 commit comments