"""Offline evidence checks for the Phase 0 map and later blueprint chapters.""" import hashlib import os import re import sys from pathlib import Path from atlas import atlas, chapters, need, obj, read, triage, NAME ROOT = Path(__file__).resolve().parent def tex_commands(text, command): # Restricted blueprint dialect; comments end at an unescaped percent sign. text = re.sub(r'(?') route = Path(sys.argv[1]).resolve() need(route.is_dir(), 'route folder does not exist') # Read route map if present; otherwise the pushed repository map, never a download. input_root = Path(os.environ.get('SS_INPUT', str(ROOT))).resolve() def locate(name): return route / name if (route / name).exists() else input_root / name atlas_folder = route if (route / 'atlas-manifest.json').exists() else input_root rows = atlas(atlas_folder) example = route == ROOT / 'examples' if example: print('EXAMPLE ONLY: fictional declarations and reviews, not mathematical evidence') messages = [] cone_names = None triage_file = locate('triage.json') if triage_file.exists(): need(rows is not None, 'triage needs a local hash-checked atlas') cone_names, status = triage(rows, obj(triage_file), example) print(status) messages.append(status) map_file = locate('chapters.json') chapter_map = None if map_file.exists(): need(rows is not None and cone_names is not None, 'chapters need atlas and triage.json') chapter_map = chapters(rows, obj(map_file), cone_names) messages.append(f'chapters: {len(chapter_map)} chapters, complete partition, acyclic') print(messages[-1]) files = sorted(route.glob('*.tex')) need(len(files) <= 1000, 'too many TeX files') anchors, labels, uses = {}, set(), set() for path in files: text = read(path, 1_000_000).decode('utf-8') anchors[path.stem] = set(tex_commands(text, 'lean')) local_labels = tex_commands(text, 'label') need(len(local_labels) == len(set(local_labels)) and not labels.intersection(local_labels), 'duplicate TeX label') labels.update(local_labels) uses.update(tex_commands(text, 'uses')) need(not uses - labels, 'broken uses labels: ' + ', '.join(sorted(uses - labels))) if rows is not None: for chapter, names in anchors.items(): need(not names - rows.keys(), f'chapter {chapter}: unknown Lean anchors: ' + ', '.join(sorted(names - rows.keys()))) reviews = list((route / 'reviews').rglob('*.md')) if (route / 'reviews').exists() else [] need(len(reviews) <= 2000, 'too many reviews') approved, experts, seen = {}, {}, set() for path in reviews: review = frontmatter(path) chapter, reviewer = review['chapter'], review['reviewer'].casefold() need(chapter in anchors and chapter_map is not None and chapter in chapter_map, 'review refers to unknown chapter/writer') need(review['author'].casefold() == chapter_map[chapter]['writer'].casefold(), 'review author differs from chapter writer') need((chapter, reviewer) not in seen, 'one current review per reviewer/chapter; replace older verdicts') seen.add((chapter, reviewer)) # Git may translate LF to CRLF on Windows. Bind reviews to canonical LF bytes. actual = hashlib.sha256(read(route / f'{chapter}.tex', 1_000_000).replace(b'\r\n', b'\n')).hexdigest() need(review['tex_sha256'] == actual, 'stale review: TeX hash differs') if review['verdict'] == 'approve': approved.setdefault(chapter, set()).add(reviewer) if review['expert'] == 'yes': experts.setdefault(chapter, set()).add(reviewer) for chapter, names in anchors.items(): if chapter_map is None or rows is None: print(f'chapter {chapter}: not yet expert-reviewed; coverage unchecked') continue need(chapter in chapter_map, f'unknown chapter id {chapter}') declarations = set(chapter_map[chapter]['declarations']) missing_interface = set(chapter_map[chapter]['interface']) - names need(not missing_interface, f'chapter {chapter}: interface anchors missing') covered = len(names & declarations) need(covered * 100 >= 95 * len(declarations), f'chapter {chapter}: coverage < 95%') print(f'chapter {chapter}: coverage {covered}/{len(declarations)}; {len(approved.get(chapter, set()))} approving reviews') if not experts.get(chapter): print(f'chapter {chapter}: not yet expert-reviewed') else: print(f'chapter {chapter}: expert approval declared; maintainer confirms identity') print(f'chapter {chapter}: not yet expert-reviewed until identity is confirmed') messages.append(f'chapter {chapter}: 0 broken anchors, coverage >= 95%') if rows is None and files: print('atlas not yet published: anchors unchecked') print('blueprint: syntax only; coverage unchecked') elif messages: print(messages[-1]) elif files: print('blueprint: anchors checked; coverage unchecked') elif rows is not None: print(f'atlas: {len(rows)} declarations, sha256 ok') else: print('Nothing to check yet') if __name__ == '__main__': try: main() except (ValueError, OSError, KeyError, TypeError, AttributeError, UnicodeError, EOFError) as error: print(f'Invalid blueprint: {error}') sys.exit(1)