#!/usr/bin/env python3 """Phase 0: validate sources/manifest.json. Checks structure, enums, policy consistency, and internal count consistency. Fails (exit 1) on any violation - same fail-the-build philosophy as verify.py. """ import json import sys from pathlib import Path MANIFEST = Path(__file__).resolve().parent.parent / "sources" / "manifest.json" REQUIRED_KEYS = [ "id", "category", "traditions", "title_en", "work_class", "license", "redistribution", "access", "format", "provenance_plan", "planned_use", "proposed_state", "flags", ] CATEGORIES = {"quran_text", "morphology", "tafsir", "hadith", "rijal"} WORK_CLASSES = {"classical", "modern", "translation", "infrastructure"} LICENSE_VALUES = { "public_domain", "public_domain_base_text", "public_domain_full_text_only", "public_domain_underlying_text_transcription_hosted", "custom_verbatim_attribution", "custom_verbatim_attribution_labeled_GPL", "mit_code_cc_by_4_data", "in_copyright", "in_copyright_rights_holder_forbids_ai_database_use", "unknown", } LICENSE_VERIFIED = {"page_fetched", "legal_analysis", "not_verified"} HF_REDISTRIBUTION = {"allowed", "allowed_with_attribution", "blocked", "unknown"} PROPOSED_STATES = {"use", "use_with_notice", "exclude_until_resolved", "exclude_entirely"} USE_STATES = {"use", "use_with_notice"} errors, warnings = [], [] with MANIFEST.open(encoding="utf-8") as f: m = json.load(f) for key in ["schema_version", "generated_on", "status", "policy", "sources", "summary_counts", "decisions_requested", "unresolved_summary"]: if key not in m: errors.append(f"manifest missing top-level key: {key}") ids = set() for s in m.get("sources", []): sid = s.get("id", "") if sid in ids: errors.append(f"duplicate source id: {sid}") ids.add(sid) for key in REQUIRED_KEYS: if key not in s: errors.append(f"{sid}: missing required key '{key}'") if s.get("category") not in CATEGORIES: errors.append(f"{sid}: bad category {s.get('category')!r}") if s.get("work_class") not in WORK_CLASSES: errors.append(f"{sid}: bad work_class {s.get('work_class')!r}") if not s.get("traditions") or not isinstance(s["traditions"], list): errors.append(f"{sid}: traditions must be a non-empty list") lic = s.get("license", {}) if lic.get("value") not in LICENSE_VALUES: errors.append(f"{sid}: bad license.value {lic.get('value')!r}") if lic.get("verified") not in LICENSE_VERIFIED: errors.append(f"{sid}: bad license.verified {lic.get('verified')!r}") if lic.get("verified") == "page_fetched" and not lic.get("evidence_urls"): errors.append(f"{sid}: page_fetched license requires evidence_urls") if lic.get("verified") == "not_verified" and lic.get("value") not in {"unknown", "in_copyright"}: errors.append(f"{sid}: not_verified license must be 'unknown' or 'in_copyright', got {lic.get('value')!r}") red = s.get("redistribution", {}) if red.get("huggingface") not in HF_REDISTRIBUTION: errors.append(f"{sid}: bad redistribution.huggingface {red.get('huggingface')!r}") if not red.get("basis"): errors.append(f"{sid}: redistribution.basis is required (reasoning must be recorded)") acc = s.get("access", {}) if acc.get("method") not in {"bulk_download", "api", "programmatic_page_fetch", "manual_transcription", "not_identified"}: errors.append(f"{sid}: bad access.method {acc.get('method')!r}") if acc.get("terms_verified") is True and not acc.get("urls"): errors.append(f"{sid}: terms_verified=true requires access.urls") pp = s.get("provenance_plan", {}) if "recorded_fields" not in pp: errors.append(f"{sid}: provenance_plan.recorded_fields missing") if pp.get("retrieval_date") is not None: warnings.append(f"{sid}: retrieval_date set during Phase 0 - nothing should be fetched yet") if pp.get("sha256") is not None: warnings.append(f"{sid}: sha256 set during Phase 0 - nothing should be fetched yet") state = s.get("proposed_state") if state not in PROPOSED_STATES: errors.append(f"{sid}: bad proposed_state {state!r}") # --- policy consistency --- if lic.get("value") == "unknown" and state not in {"exclude_until_resolved", "exclude_entirely"}: errors.append(f"{sid}: policy says license=unknown must be excluded from published dataset") if red.get("huggingface") == "blocked" and state in USE_STATES: errors.append(f"{sid}: HF redistribution blocked but proposed_state is {state}") if s.get("work_class") == "translation" and state in USE_STATES: errors.append(f"{sid}: copyrighted translation proposed for use - violates translation policy") if s.get("category") == "quran_text" and "custom_verbatim" not in lic.get("value", ""): errors.append(f"{sid}: verse text source must carry the verbatim-attribution license terms") # --- counts --- counts = {"use": 0, "use_with_notice": 0, "exclude_until_resolved": 0, "exclude_entirely": 0} for s in m["sources"]: counts[s["proposed_state"]] += 1 sc = m.get("summary_counts", {}) if sc.get("total_sources") != len(m["sources"]): errors.append(f"summary_counts.total_sources={sc.get('total_sources')} but actual={len(m['sources'])}") for state in counts: key = f"proposed_{state}" # manifest uses 'proposed_use' style for use-states and bare state names for excludes recorded = sc.get(key, sc.get(state)) if recorded != counts[state]: errors.append(f"summary_counts[{key or state}]={recorded} but actual={counts[state]}") # --- review gate --- if m.get("status") != "PROPOSED_AWAITING_USER_REVIEW": warnings.append(f"manifest status is {m.get('status')!r} - should stay PROPOSED until user approval") print(f"Manifest: {MANIFEST}") print(f"Sources: {len(m['sources'])} | states: {counts}") print(f"Decisions requested: {len(m.get('decisions_requested', []))}") if warnings: print("\nWARNINGS:") for w in warnings: print(f" [warn] {w}") if errors: print("\nFAILED:") for e in errors: print(f" [error] {e}") sys.exit(1) print("\nPASS: manifest.json structure, enums and policy consistency verified.")