"""Check atlas provenance, module partition, chapter anchors and revision-bound reviews.""" import argparse import hashlib import os import re from pathlib import Path from atlas import (SOURCE_COMMIT, SOURCE_URL, DECLARATION_SHA256, MODULE_SHA256, USED_MODULES, HEADLINE_COUNT, DECLARATION_COUNT, USED_DECLARATIONS, ROOT, need, obj, digest, extract) def load_atlas(folder): a = obj(folder / "atlas.json") example = folder.resolve() == ROOT / "examples" / "atlas" sources = a["sources"] need(a["schema"] == 1, "unsupported atlas schema") if example: need(a["source_commit"] == "FICTIONAL-EXAMPLE", "example atlas must be explicitly fictional") print("EXAMPLE ONLY: fictional declarations and reviews; not mathematical evidence") else: need(a["source_commit"] == SOURCE_COMMIT, "source commit mismatch") paths = {} for key, filename, expected in [("declarations", "declaration-usage.tsv", DECLARATION_SHA256), ("modules", "module-usage.tsv", MODULE_SHA256)]: s = sources[key] paths[key] = folder / s["path"] if not example: need(s["sha256"] == expected and s["url"] == SOURCE_URL + filename, f"{key}: provenance mismatch") need(digest(paths[key]) == s["sha256"], f"{key}: TSV sha256 mismatch") names, headlines, graph = extract(paths["declarations"], paths["modules"]) need(a["modules"] == graph and a["headlines"] == headlines, "atlas differs from TSV-derived module coverage/edges/headlines") if not example: need(len(graph) == USED_MODULES and len(headlines) == HEADLINE_COUNT, "module/headline count mismatch") need(len(names) == DECLARATION_COUNT and sum(m["used"] for m in graph) == USED_DECLARATIONS, "declaration count mismatch") print(f"atlas: {len(graph)} modules, {len(headlines)} headlines, sha256 ok") return names, headlines, graph, example def partition(data, graph, example): chapters = data["chapters"] need(isinstance(chapters, list) and chapters, "chapters must be a nonempty list") need(example or 20 <= len(chapters) <= 60, "production partition needs 20 to 60 chapters") by_id, owners = {}, {} for c in chapters: k = c["id"] need(isinstance(k, str) and re.fullmatch(r"[0-9]+", k) and k not in by_id, "invalid/duplicate chapter id") need(isinstance(c["writer"], str) and c["writer"].strip(), "chapter writer missing") need(isinstance(c["modules"], list), "chapter modules must be a list") need(k == "0" or c["modules"], "nonzero chapter needs at least one module") by_id[k] = c for m in c["modules"]: need(isinstance(m, str) and m not in owners, "module assigned more than once") owners[m] = k need("0" in by_id, "chapter 0 missing") need(set(owners) == {m["name"] for m in graph}, "assign every used module exactly once (no missing/extra modules)") deps = {k: set() for k in by_id} for m in graph: k = owners[m["name"]] deps[k].update(owners[d] for d in m["dependencies"] if owners[d] != k) # Kahn traversal avoids recursion limits even for unusually deep input. done = set() while True: ready = {k for k in deps if k not in done and deps[k] <= done} if not ready: break done.update(ready) need(len(done) == len(deps), "chapter dependency cycle") print(f"chapters: {len(chapters)} chapters, complete partition, acyclic") return by_id def commands(text, command, tex): if tex: text = re.sub(r"(?= 95 * len(assigned), f"chapter {k}: coverage < 95%") need(len(approved[k]) >= 2, f"chapter {k}: needs two distinct non-author approving reviewers") print(f"chapter {k}: not yet expert-reviewed; maintainer must confirm human sign-off") print(f"chapter {k}: 0 broken anchors, coverage >= 95%") def main(): p = argparse.ArgumentParser(description=__doc__) p.add_argument("route", nargs="?", type=Path) p.add_argument("--atlas", type=Path) a = p.parse_args() if a.atlas: folder = a.atlas.resolve() elif a.route and a.route.resolve().parent == ROOT / "examples": folder = ROOT / "examples" / "atlas" else: folder = Path(os.environ.get("SS_INPUT", ROOT)) names, headlines, graph, example = load_atlas(folder) if a.route: need(not example or a.route.resolve().parent == ROOT / "examples", "fictional atlas only allowed for bundled examples") check_route(a.route, names, headlines, graph, example) if __name__ == "__main__": try: main() except (ValueError, OSError, KeyError, TypeError, AttributeError, UnicodeError) as error: print(f"Invalid blueprint: {error}") raise SystemExit(1)