File size: 11,190 Bytes
2e89071
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e483b07
2e89071
 
 
 
 
e483b07
 
 
 
 
 
 
2e89071
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e483b07
 
2e89071
 
 
 
e483b07
 
 
2e89071
 
e483b07
2e89071
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e483b07
 
 
2e89071
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e483b07
 
2e89071
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e483b07
2e89071
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
"""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 <commit>:<path>` 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()