"""Rebuild lab-source.zip for the OpenMind Jailbreak Lab HF Space. The zip is the "full lab source" the Space page unpacks into Pyodide (/lab). It is a CURATED subset of the repo, not the whole tree: the inclusion list below is the spec — the next audit checks the zip against this list, not against a guess. Usage: python3 scripts/build_space_zip.py [--commit 42ef9e5] [--out lab-source.zip] Sources: * REPO_FILES: bytes taken from `git show :` in the repo checkout (default ~/workspace/openmind-jailbreak-lab-git). * lab_bridge.py: the Space bridge, from this build dir (not in the repo). * data/cases.json: Space-generated snapshot of the 76 Guardrail Lab cases (same shape as lab_bridge.list_cases()). Verified against a fresh export of CASES from the pinned commit before it is zipped; it is NOT tracked in the repo (open question: track a generated copy there with a generator + pin). Post-build self-checks (fail the build if any fails): * 109 entries, no __pycache__ / *.pyc anywhere. * `import modules.guardrail_lab.campaign` succeeds from the unpacked zip (this caught the missing campaign_history.py on 2026-11-19). * data/benchmark_v0.8.0.jsonl in the zip is byte-identical to the repo blob at the pinned commit (same for the loose copy). * data/cases.json ids match CASES, in order. * manual_reviews/ + RESULTS_TEMPLATE.csv ship too (added 2026-11-20): the shipped docs link to them relatively (guardrail README, original_source_corpus README, omjb INSTALLATION.md, and src/evaluate.py's header comment), and the corpus README tells the browser user the blank source-review template is kept at manual_reviews/original_source_review_template.json — all of those were broken while the files were excluded. """ import argparse import hashlib import json import subprocess import sys import tempfile import zipfile from pathlib import Path BUILD_DIR = Path(__file__).resolve().parent.parent DEFAULT_REPO = Path("/home/hatch/workspace/openmind-jailbreak-lab-git") DEFAULT_COMMIT = "42ef9e5" # Fixed timestamp so rebuilds of the same inputs are byte-identical. ZIP_DATE = (1980, 1, 1, 0, 0, 0) # Repo files shipped in the zip (curated subset). Deliberately NOT shipped: # tests/, docs/, .github/, root docs other than REGRESSION_SET.md and # RESULTS_TEMPLATE.csv, assets/, data/benchmark_v0.3-0.7.0, # data/benchmark_v0.8.0-pairs.json, data/discovery_seed_cases.example.jsonl, # data/schema.json, scripts/build_benchmark_v0.7.0.py, # scripts/build_benchmark_v0.8.0.py, scripts/import_external_prompt_archive.py, # scripts/regenerate_source_corpus.py. None of those is imported by any # shipped file (verified by AST import scan, 2026-11-19). Consequence: # shipped markdown still links relatively to docs/, .github/ and the root # INSTALLATION.md — those links resolve in the repo, not in the Space. REPO_FILES = [ "REGRESSION_SET.md", "RESULTS_TEMPLATE.csv", "data/CHECKSUMS.txt", "data/benchmark_regression.jsonl", "data/benchmark_v0.8.0.jsonl", "data/known_bypasses.json", "data/source_corpus/README.md", "data/source_corpus/manifest.json", "data/source_corpus/safe_adaptations.jsonl", "data/source_corpus/source_inventory.jsonl", "modules/__init__.py", "modules/ai_scanner/README.md", "modules/ai_scanner/__init__.py", "modules/ai_scanner/__main__.py", "modules/ai_scanner/cli.py", "modules/ai_scanner/probes.py", "modules/ai_scanner/providers.py", "modules/ai_scanner/runner.py", "modules/guardrail_lab/ADAPTIVE_ENGINE.md", "modules/guardrail_lab/CAMPAIGN_HISTORY.md", "modules/guardrail_lab/DISCOVERY_ENGINE.md", "modules/guardrail_lab/EXTERNAL_ARCHIVE_IMPORT.md", "modules/guardrail_lab/EXTERNAL_PATTERN_CORPUS.md", "modules/guardrail_lab/INTEGRATION.md", "modules/guardrail_lab/PROVIDER_MATRIX.md", "modules/guardrail_lab/README.md", "modules/guardrail_lab/REGISTRY.md", "modules/guardrail_lab/__init__.py", "modules/guardrail_lab/__main__.py", "modules/guardrail_lab/adaptive.py", "modules/guardrail_lab/campaign.py", "modules/guardrail_lab/campaign_history.py", "modules/guardrail_lab/cases.py", "modules/guardrail_lab/cli.py", "modules/guardrail_lab/compare.py", "modules/guardrail_lab/demo.py", "modules/guardrail_lab/discovery.py", "modules/guardrail_lab/measurement.py", "modules/guardrail_lab/openai_compatible.py", "modules/guardrail_lab/preflight.py", "modules/guardrail_lab/registry.py", "modules/guardrail_lab/runner.py", "modules/guardrail_lab/source_cases.py", "modules/guardrail_lab/source_registry.py", "modules/jb002/README.md", "modules/jb002/__init__.py", "modules/jb002/__main__.py", "modules/jb002/cases.json", "modules/jb002/cli.py", "modules/jb002/record.schema.json", "manual_reviews/README.md", "manual_reviews/original_source_review_template.json", "manual_reviews/review_template.json", "modules/original_source_corpus/README.md", "modules/original_source_corpus/__init__.py", "modules/original_source_corpus/__main__.py", "omjb/INSTALLATION.md", "omjb/README.md", "omjb/__init__.py", "omjb/async_harness.py", "omjb/cli.py", "omjb/corpus_io.py", "omjb/detectors.py", "omjb/downloader_sources.py", "omjb/downloaders.py", "omjb/export.py", "omjb/guard.py", "omjb/guard_eval.py", "omjb/guard_review.py", "omjb/guardrail_adapter.py", "omjb/integration.py", "omjb/mutation_engine/__init__.py", "omjb/mutation_engine/adapter.py", "omjb/mutation_engine/budget.py", "omjb/mutation_engine/chat_completions.py", "omjb/mutation_engine/cli.py", "omjb/mutation_engine/config.py", "omjb/mutation_engine/core.py", "omjb/mutation_engine/decoder_experiment.py", "omjb/mutation_engine/dedup.py", "omjb/mutation_engine/engine.py", "omjb/mutation_engine/regions.py", "omjb/mutation_engine/replay.py", "omjb/mutation_engine/rng.py", "omjb/mutation_engine/scheduler.py", "omjb/mutation_engine/spec.py", "omjb/mutation_engine/transforms.py", "omjb/mutations.py", "omjb/pipeline.py", "omjb/probes.py", "omjb/provenance.py", "omjb/provider_eval.py", "omjb/providers.py", "omjb/remote_cli.py", "omjb/report.py", "omjb/runner.py", "omjb/stats.py", "omjb/storage.py", "omjb/targets.py", "scripts/regression_report.py", "src/__init__.py", "src/compare_runs.py", "src/evaluate.py", "src/judge_fixtures.py", "src/quality_audit.py", "src/semantic_review.py", "src/validate.py", ] SPACE_FILES = [ "lab_bridge.py", # Space bridge (not in the repo, by design) "data/cases.json", # Space-generated CASES snapshot (verified below) ] def git_show(repo, commit, path): proc = subprocess.run( ["git", "-C", str(repo), "show", f"{commit}:{path}"], capture_output=True, check=False) if proc.returncode != 0: raise SystemExit(f"git show {commit}:{path} failed: " f"{proc.stderr.decode().strip()}") return proc.stdout def verify_cases_json(repo, commit): """Loose data/cases.json must match a fresh CASES export at the commit.""" loose = json.loads((BUILD_DIR / "data" / "cases.json").read_text()) with tempfile.TemporaryDirectory() as tmp: archive = subprocess.run( ["git", "-C", str(repo), "archive", "--format=zip", commit], capture_output=True, check=True).stdout import io zf = zipfile.ZipFile(io.BytesIO(archive)) zf.extractall(tmp) code = ( "import json, sys; sys.path.insert(0, sys.argv[1]);" "from modules.guardrail_lab.cases import CASES;" "print(json.dumps([{'case_id': c.case_id, 'category': c.category," "'prompt': c.prompt, 'expected_signal': c.expected_signal," "'canary': c.canary, 'control': c.control," "'source_id': c.source_id, 'source_path': c.source_path," "'source_sha256': c.source_sha256} for c in CASES]," "ensure_ascii=False))" ) proc = subprocess.run( [sys.executable, "-c", code, tmp], capture_output=True, text=True, check=True) fresh = json.loads(proc.stdout) if loose != fresh: raise SystemExit( f"data/cases.json does not match a fresh CASES export at " f"{commit} ({len(loose)} vs {len(fresh)} rows)") print(f"cases.json verified: {len(loose)} rows match CASES at {commit}") def main(): ap = argparse.ArgumentParser() ap.add_argument("--commit", default=DEFAULT_COMMIT) ap.add_argument("--repo", default=str(DEFAULT_REPO)) ap.add_argument("--out", default=str(BUILD_DIR / "lab-source.zip")) args = ap.parse_args() repo = Path(args.repo) verify_cases_json(repo, args.commit) entries = {} for path in REPO_FILES: entries[path] = git_show(repo, args.commit, path) for path in SPACE_FILES: entries[path] = (BUILD_DIR / path).read_bytes() if len(entries) != 109: raise SystemExit(f"inclusion list has {len(entries)} entries, expected 109") out_path = Path(args.out) with zipfile.ZipFile(out_path, "w", zipfile.ZIP_DEFLATED) as zf: for path in sorted(entries): if "__pycache__" in path or path.endswith(".pyc"): raise SystemExit(f"refusing to ship bytecode: {path}") info = zipfile.ZipInfo(path, date_time=ZIP_DATE) info.compress_type = zipfile.ZIP_DEFLATED info.external_attr = 0o644 << 16 zf.writestr(info, entries[path]) digest = hashlib.sha256(out_path.read_bytes()).hexdigest() print(f"wrote {out_path} ({len(entries)} entries) sha256={digest}") # Post-build self-checks against the fresh zip. with tempfile.TemporaryDirectory() as tmp: with zipfile.ZipFile(out_path) as zf: names = zf.namelist() assert len(names) == 109, names assert not [n for n in names if "__pycache__" in n or n.endswith(".pyc")] zf.extractall(tmp) check = subprocess.run( [sys.executable, "-c", "import sys; sys.path.insert(0, sys.argv[1]);" "import modules.guardrail_lab.campaign;" "print('campaign imports OK')", tmp], capture_output=True, text=True) if check.returncode != 0: raise SystemExit("post-build check failed: " + check.stderr) print(check.stdout.strip()) bench = Path(tmp, "data/benchmark_v0.8.0.jsonl").read_bytes() want = git_show(repo, args.commit, "data/benchmark_v0.8.0.jsonl") assert bench == want, "benchmark_v0.8.0.jsonl in zip != repo blob" loose = (BUILD_DIR / "data/benchmark_v0.8.0.jsonl").read_bytes() assert loose == want, "loose benchmark_v0.8.0.jsonl != repo blob" print("benchmark_v0.8.0.jsonl: zip == loose == repo blob") print("self-checks passed") if __name__ == "__main__": main()