"""Bounded, offline declaration atlas and chapter-map checks (Python stdlib).""" import gzip import hashlib import json import re from pathlib import Path MAX_BYTES = 32 * 1024 * 1024 MAX_EXPANDED = 256 * 1024 * 1024 NAME = re.compile(r"[^\s{}\\,;]+") CATEGORIES = {"standard-in-literature", "mechanical", "new", "unclassified"} TARGETS = ["riemannHypothesis", "riemannZeta_eq_zero_re", "LFunction_ne_zero_of_half_lt_re", "LFunction_eq_zero_re", "LFunction_eq_zero_re_of_isPrimitive"] def need(ok, message): if not ok: raise ValueError(message) def read(path, limit=MAX_BYTES): need(path.is_file() and path.stat().st_size <= limit, f"{path.name}: missing or exceeds byte limit") return path.read_bytes() def obj(path): return json.loads(read(path, 1_000_000)) def atlas(folder): manifest = folder / "atlas-manifest.json" if not manifest.exists(): return None m = obj(manifest) name = m.get("file", "") need(isinstance(name, str) and name in {"atlas.jsonl", "atlas.jsonl.gz"}, "manifest.file must be atlas.jsonl[.gz]") raw = read(folder / name) need(hashlib.sha256(raw).hexdigest() == m.get("sha256"), "atlas sha256 mismatch") rows = {} # Bounded streaming decompression; never download a route-supplied URL. import io stream = gzip.GzipFile(fileobj=io.BytesIO(raw)) if name.endswith('.gz') else io.BytesIO(raw) total = 0 with stream: while True: line = stream.readline(1_000_001) if not line: break total += len(line) need(len(line) <= 1_000_000 and total <= MAX_EXPANDED, "atlas exceeds expanded/line limit") d = json.loads(line) n = d.get("name") need(isinstance(n, str) and NAME.fullmatch(n) and n not in rows, "invalid/duplicate atlas declaration") for k in ("module", "kind", "type"): need(isinstance(d.get(k), str) and bool(d[k].strip()), f"{n}: missing {k}") deps = d.get("dependencies") need(isinstance(deps, list) and all(isinstance(v, str) and NAME.fullmatch(v) for v in deps), f"{n}: invalid dependencies") need(len(deps) == len(set(deps)), f"{n}: duplicate dependencies") need(type(d.get("source_lines")) is int and d['source_lines'] >= 0, f"{n}: invalid source_lines") rows[n] = d need(bool(rows), "empty atlas") need(type(m.get('declarations')) is int and m['declarations'] == len(rows), "manifest declaration count mismatch") return rows def cone(rows, targets): need(isinstance(targets, list) and bool(targets) and all(isinstance(n, str) and n in rows for n in targets), "unknown cone target") seen = set() todo = list(targets) while todo: n = todo.pop() if n in seen: continue seen.add(n) todo.extend(d for d in rows[n]['dependencies'] if d in rows and d not in seen) return seen def triage(rows, data, example=False): targets = data.get('targets') need(example or targets == TARGETS, "cone must target exactly the five headline theorems in SOURCE order") names = cone(rows, targets) classes = data.get('classifications') need(isinstance(classes, dict) and set(classes) == names, "classify every cone declaration exactly once") need(all(isinstance(c, str) and c in CATEGORIES for c in classes.values()), "unknown triage category") counts = {c: sum(v == c for v in classes.values()) for c in CATEGORIES} total = len(names) need(counts['unclassified'] * 100 < total * 5, "unclassified must be < 5%") # Conservative rounding: never display 5.0% as < 5%. pc = lambda c: f"{100 * counts[c] / total:.1f}" line = (f"cone: {total} decls, {sum(rows[n]['source_lines'] for n in names)} lines; " f"standard {pc('standard-in-literature')}%, mechanical {pc('mechanical')}%, " f"new {pc('new')}%, unclassified < 5%") return names, line def chapters(rows, data, cone_names): cs = data.get('chapters') need(isinstance(cs, list) and bool(cs), "chapters must be a nonempty list") by_id, owner = {}, {} for c in cs: ident = c.get('id') need(isinstance(ident, str) and re.fullmatch(r'[A-Za-z0-9_-]+', ident) and ident not in by_id, "invalid/duplicate chapter id") need(isinstance(c.get('writer'), str) and bool(c['writer'].strip()), "chapter writer required") decls, interface = c.get('declarations'), c.get('interface') need(isinstance(decls, list) and bool(decls) and all(isinstance(n, str) and n in cone_names and n not in owner for n in decls), "chapter has unknown/duplicate declarations") need(len(decls) == len(set(decls)), "duplicate declaration in chapter") need(isinstance(interface, list) and bool(interface) and all(isinstance(n, str) and n in decls for n in interface) and len(interface) == len(set(interface)), "invalid chapter interface") by_id[ident] = c owner.update({n: ident for n in decls}) need(set(owner) == cone_names, "chapters must partition the full cone") edges = {k: set() for k in by_id} for n, chapter in owner.items(): for dep in rows[n]['dependencies']: other = owner.get(dep) if other and other != chapter: need(dep in by_id[other]['interface'], f"cross-chapter dependency {dep} missing from interface") edges[chapter].add(other) # Iterative topological elimination avoids recursion-depth dependence. pending = {k: set(v) for k, v in edges.items()} while pending: ready = {k for k, v in pending.items() if not v} need(bool(ready), "chapter dependency graph is cyclic") pending = {k: v - ready for k, v in pending.items() if k not in ready} return by_id