"""Check repository JSON, document links, manifest paths, and local schema references.""" import ast import json import re from pathlib import Path from urllib.parse import unquote from jsonschema import Draft202012Validator, FormatChecker ROOT = Path(__file__).resolve().parents[1] SKIP = {'build', '.git', '__pycache__', '.test-work', '.venv'} def validate_contracts(root=ROOT): def load(name): return json.loads((root/name).read_text(encoding='utf-8-sig')) for document,schema in [('repository.json','repository'),('repository-variables.json','repository-variables'),('content-index.json','content-index')]: Draft202012Validator(load('schemas/'+schema+'.schema.json'), format_checker=FormatChecker()).validate(load(document)) manifest,variables,index=load('repository.json'),load('repository-variables.json'),load('content-index.json') assert variables['identity']['authors']==manifest['authors'], 'Author mismatch' assert variables['identity']['publisher']==manifest['publisher'], 'Publisher mismatch' assert variables['identity']['maintainer']['name']==manifest['maintenance']['maintainer'], 'Maintainer mismatch' for key,value in variables['repository'].items(): assert manifest.get(key)==value, 'Repository identity mismatch: '+key for key,value in variables['rights'].items(): assert manifest['rights'][key]==value, 'Rights mismatch: '+key version=(root/'VERSION').read_text(encoding='utf-8-sig').strip() assert manifest['version']==version, 'Manifest/version mismatch' citation=(root/'CITATION.cff').read_text(encoding='utf-8') assert re.search(r'(?m)^version:\s*[\"\x27]?'+re.escape(version)+r'[\"\x27]?\s*$',citation), 'Citation version mismatch' assert 'license: MIT' in citation and manifest['rights']['license']=='MIT', 'Citation/license mismatch' assert manifest['canonical_url'] in citation, 'Citation URL mismatch' for record in manifest['authors']: assert record['name'] in (root/'AUTHOR.md').read_text(encoding='utf-8'), 'Author notice mismatch' license_text = (root/manifest['rights']['license_file']).read_text(encoding='utf-8') assert re.search(r'Copyright \(c\) \d{4}', license_text), 'Missing copyright notice' assert any(record['name'] in license_text for record in manifest['authors']), 'License attribution mismatch' ids=[item['content_id'] for item in index['items']] paths=[item['path'] for item in index['items']] assert len(ids)==len(set(ids)) and len(paths)==len(set(paths)), 'Duplicate content identity/path' for item in index['items']: target=(root/item['path']).resolve() assert target.is_relative_to(root.resolve()) and target.is_file(), 'Missing/unsafe indexed path' assert set(item.get('related_content_ids',[])) <= set(ids), 'Unresolved content relation' for declared in [v for v in manifest['entrypoints'].values() if v] + [c['path'] for c in manifest['contents']] + manifest['ai_retrieval']['read_first'] + manifest['ai_retrieval']['retrieve_as_needed']: target=(root/declared).resolve() assert target.is_relative_to(root.resolve()) and target.exists(), 'Missing/unsafe declared path: '+declared return {'version':version,'license':manifest['rights']['license'],'indexed_documents':len(ids)} def validate_public_template(root=ROOT): """Bounded repository hygiene checks; not a general PII detector.""" operational_roots = {'tasks', 'task-records', 'runtime', 'private', 'recordings', 'structured'} generated_names = {'media-intake.json', 'structure-plan.json', 'build-receipt.json', 'approval.json'} text_suffixes = {'.md', '.py', '.json', '.txt', '.vtt', '.cff'} machine_path = re.compile(r'(?