"""Verify local release hashes, retained originals, and derived data. No network.""" from pathlib import Path, PurePosixPath import hashlib import json import sys ROOT = Path(__file__).resolve().parent def safe_path(name): p = PurePosixPath(name) if p.is_absolute() or not p.parts or any(x in ('..', '.') for x in p.parts) or '\\' in name or ':' in name: raise ValueError(f'Unsafe release path: {name!r}') local = ROOT.joinpath(*p.parts) if local.is_symlink() or not local.resolve().is_relative_to(ROOT.resolve()): raise ValueError(f'Path escapes release: {name!r}') return local def verify(): manifest = json.loads((ROOT/'release_manifest.json').read_text(encoding='utf-8')) files = manifest['files'] if not files: raise ValueError('Empty manifest') for name, entry in files.items(): raw = safe_path(name).read_bytes() if len(raw) != entry['bytes'] or hashlib.sha256(raw).hexdigest() != entry['sha256']: raise ValueError(f'Release checksum mismatch: {name}') inventory = json.loads((ROOT/'provenance/source_inventory.json').read_text(encoding='utf-8')) for entry in inventory['files']: raw = safe_path(entry['preserved_path']).read_bytes() if hashlib.sha256(raw).hexdigest() != entry['sha256'] or len(raw) != entry['bytes']: raise ValueError(f'Original source changed: {entry["archive_path"]}') counts = {} for name in ('research_corpus','claims','review_tasks'): rows = [json.loads(line) for line in (ROOT/f'data/{name}.jsonl').read_text(encoding='utf-8').splitlines()] if len({row['id'] for row in rows}) != len(rows): raise ValueError(f'Duplicate record IDs: {name}') schema = json.loads((ROOT/f'schemas/{name}.schema.json').read_text(encoding='utf-8')) for row in rows: if set(row) != set(schema['required']): raise ValueError(f'Record fields differ from schema: {name}') for key, spec in schema['properties'].items(): value = row[key] expected = spec['type'] if expected == 'string' and not isinstance(value,str): raise ValueError(f'Expected string in {name}.{key}') if expected == 'integer' and (not isinstance(value,int) or isinstance(value,bool)): raise ValueError(f'Expected integer in {name}.{key}') if name == 'research_corpus': raw = safe_path(row['source_path']).read_bytes() text = raw.decode('utf-8') start,end = row['char_start'],row['char_end'] if not 0 <= start < end <= len(text) or row['text'] != text[start:end]: raise ValueError(f'Corpus slice mismatch: {row["id"]}') if hashlib.sha256(raw).hexdigest() != row['source_sha256']: raise ValueError(f'Corpus source mismatch: {row["id"]}') elif name == 'claims': raw = safe_path(row['source_path']).read_bytes() if hashlib.sha256(raw).hexdigest() != row['source_sha256']: raise ValueError(f'Claim source mismatch: {row["id"]}') elif name == 'review_tasks': for source in json.loads(row['required_sources']): if not safe_path(source).is_file(): raise ValueError(f'Missing task source: {source}') counts[name] = len(rows) import csv from tools.build_agent_data import typed_csv for path in sorted((ROOT/'results').glob('*.csv')): with path.open(newline='',encoding='utf-8') as f: source_rows = [{key:typed_csv(value) for key,value in row.items()} for row in csv.DictReader(f)] derived_rows = [json.loads(line) for line in (ROOT/f'data/{path.stem}.jsonl').read_text(encoding='utf-8').splitlines()] if source_rows != derived_rows: raise ValueError(f'Derived experiment table differs: {path.stem}') counts[path.stem] = len(source_rows) if counts != json.loads((ROOT/'data/counts.json').read_text(encoding='utf-8')): raise ValueError('Dataset count mismatch') print(f'PASS: {len(files)} release files; {len(inventory["files"])} original files; dataset source slices and counts verified.') print('Checksums establish local integrity, not authorship, mathematical truth, or publication priority.') return manifest if __name__ == '__main__': try: verify() except (OSError, ValueError, KeyError, TypeError) as exc: print(f'FAIL: {exc}',file=sys.stderr) raise SystemExit(1)