#!/usr/bin/env python3 from __future__ import annotations import csv import gzip import hashlib import json import re from pathlib import Path ROOT = Path(__file__).resolve().parent def sha256(path: Path) -> str: digest = hashlib.sha256() with path.open("rb") as handle: for block in iter(lambda: handle.read(1024 * 1024), b""): digest.update(block) return digest.hexdigest() def lfs_pointer(path: Path): if path.stat().st_size > 1024: return None text = path.read_text(encoding="utf-8", errors="replace") match = re.fullmatch( r"version https://git-lfs.github.com/spec/v1\n" r"oid sha256:([0-9a-f]{64})\nsize ([0-9]+)\n?", text, ) if not match: return None return match.group(1), int(match.group(2)) def content_sha256(path: Path) -> str: pointer = lfs_pointer(path) return pointer[0] if pointer else sha256(path) errors: list[str] = [] runs = list( csv.DictReader( (ROOT / "trajectories/TRAJECTORY_INDEX.csv").open(encoding="utf-8") ) ) papers = list( csv.DictReader((ROOT / "PAPER_INDEX.csv").open(encoding="utf-8-sig")) ) project_ids = {row["project"] for row in runs} task_ids = {row["task_id"] for row in runs} settings = {(row["base_model"], row["agent"]) for row in runs} if len(project_ids) != 47: errors.append("trajectory index is not 47 unique projects") if len(runs) != 423 or len(task_ids) != 423: errors.append("trajectory index is not 423 unique assignments") if len(settings) != 9: errors.append("trajectory index is not nine unique model-agent settings") paper_ids = {row["project_id"] for row in papers} bundled_papers = [row for row in papers if row["pdf_path"]] link_only_papers = [row for row in papers if not row["pdf_path"]] paper_paths = {row["pdf_path"] for row in bundled_papers} if len(papers) != 47 or len(paper_ids) != 47: errors.append("paper index is not 47 unique projects") if paper_ids != project_ids: errors.append("paper index project IDs do not match the trajectory index") if len(bundled_papers) != 43: errors.append("paper index does not contain 43 bundled PDFs") if len(link_only_papers) != 4: errors.append("paper index does not contain four link-only papers") actual_papers = { str(path.relative_to(ROOT)) for path in (ROOT / "task_pool").glob("*.pdf") } if actual_papers != paper_paths: errors.append("paper files do not match the paper index") for row in bundled_papers: paper = ROOT / row["pdf_path"] if not paper.is_file(): errors.append(f"missing paper: {row['project_id']}") continue if paper.read_bytes()[:5] != b"%PDF-": errors.append(f"invalid PDF header: {row['project_id']}") if sha256(paper) != row["sha256"]: errors.append(f"paper hash mismatch: {row['project_id']}") if paper.stat().st_size != int(row["bytes"]): errors.append(f"paper size mismatch: {row['project_id']}") if not row["source_url"].startswith("https://"): errors.append(f"paper source is not HTTPS: {row['project_id']}") if row["redistribution_status"] != "bundled_license_verified": errors.append(f"invalid bundled status: {row['project_id']}") if not row["license_evidence_url"].startswith("https://"): errors.append(f"missing license evidence: {row['project_id']}") for row in link_only_papers: if row["redistribution_status"] != "link_only_no_downstream_license": errors.append(f"invalid link-only status: {row['project_id']}") if not row["source_url"].startswith("https://"): errors.append(f"link-only source is not HTTPS: {row['project_id']}") if any(row[key] for key in ("sha256", "bytes", "page_count")): errors.append(f"link-only entry has bundled metadata: {row['project_id']}") strong_patterns = { "local_path": re.compile(r"/Users/ssp|/lustre/prod_glm_volumes"), "api_key": re.compile(r"(?)[A-Za-z0-9][A-Za-z0-9._~+/-]{7,}" ), "private_key": re.compile(r"BEGIN (?:RSA |EC |OPENSSH )?PRIVATE KEY"), } lfs_pointer_count = 0 for row in runs: log = ROOT / row["trajectory_path"] if not log.is_file(): errors.append(f"missing trajectory: {row['task_id']}") continue if log.name != "agent.log.gz": errors.append(f"unexpected trajectory filename: {row['task_id']}") pointer = lfs_pointer(log) if pointer: lfs_pointer_count += 1 if pointer[0] != row["released_log_sha256"]: errors.append(f"trajectory LFS hash mismatch: {row['task_id']}") continue if sha256(log) != row["released_log_sha256"]: errors.append(f"trajectory hash mismatch: {row['task_id']}") continue try: with gzip.open(log, "rt", encoding="utf-8", errors="replace") as handle: for line_number, line in enumerate(handle, 1): matched = False for name, pattern in strong_patterns.items(): if pattern.search(line): errors.append( f"residual {name}: {row['task_id']}:{line_number}" ) matched = True break if matched: break except (OSError, EOFError) as exc: errors.append(f"unreadable gzip: {row['task_id']}: {exc}") manifest = json.loads((ROOT / "DATASET_MANIFEST.json").read_text(encoding="utf-8")) if manifest.get("task_count") != 47: errors.append("manifest task count is not 47") if manifest.get("model_agent_setting_count") != 9: errors.append("manifest setting count is not nine") if manifest.get("trajectory_file_count") != 423: errors.append("manifest trajectory count is not 423") if manifest.get("paper_index_count") != len(papers): errors.append("manifest paper index count does not match") if manifest.get("paper_pdf_count") != len(bundled_papers): errors.append("manifest bundled paper count does not match") if manifest.get("paper_link_only_count") != len(link_only_papers): errors.append("manifest link-only paper count does not match") checksum_file = ROOT / "DATASET_SHA256SUMS" for raw in checksum_file.read_text(encoding="utf-8").splitlines(): expected, relative = raw.split(" ", 1) path = ROOT / relative if not path.is_file() or content_sha256(path) != expected: errors.append(f"dataset checksum mismatch: {relative}") if errors: print("verification FAILED") for error in errors[:50]: print("-", error) raise SystemExit(1) print( "verification OK: " f"projects={len(project_ids)} settings={len(settings)} " f"trajectories={len(runs)} papers={len(papers)} " f"bundled={len(bundled_papers)} link_only={len(link_only_papers)} " f"lfs_pointers={lfs_pointer_count}" )