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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
#!/usr/bin/env python3
"""Validate a downloaded or locally built PixCell dataset release."""

from __future__ import annotations

import argparse
import concurrent.futures
import io
import json
import math
import os
import subprocess
import sys
import tempfile
from collections import Counter, defaultdict
from pathlib import Path
from typing import Any

import pyarrow.parquet as pq
from huggingface_hub import DatasetCard
from PIL import Image

from _shared import (
    LEVELS,
    SAFE_PUBLIC_ID,
    SCHEMA_VERSION,
    ReleaseError,
    canonical_sha256,
    forbidden_release_imports,
    iter_release_files,
    normalized_d4_sha256,
    purity_violations,
    read_json,
    scan_text,
    scan_value_strings,
    sha256_bytes,
    sha256_file,
    verify_checksums,
)
from build_release import ROW_COLUMNS, release_digest_record


EXECUTION_RUNNER = r"""
import importlib.util
import json
import sys
from pathlib import Path

import numpy as np

program_path = Path(sys.argv[1]).resolve()
output_path = Path(sys.argv[2]).resolve()
spec = importlib.util.spec_from_file_location("pixcell_release_program", program_path)
if spec is None or spec.loader is None:
    raise RuntimeError("could not load program")
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
device = getattr(module, "device", None)
if not callable(device):
    raise RuntimeError("program has no callable device()")
component = device()
bbox = np.asarray(component.bbox_np(), dtype=float)
if bbox.shape != (2, 2):
    raise RuntimeError(f"unexpected bbox shape: {bbox.shape}")
polygons = component.get_polygons(by="tuple")
polygon_count = sum(len(items) for items in polygons.values())
gds_path = output_path.with_suffix(".gds")
component.write_gds(gds_path, with_metadata=False)
record = {
    "bbox_span_um": (bbox[1] - bbox[0]).tolist(),
    "bbox_um": bbox.tolist(),
    "gds_bytes": gds_path.stat().st_size,
    "polygon_count": polygon_count,
    "port_count": len(component.ports),
    "ports": [
        {
            "center": [float(value) for value in port.dcenter],
            "name": str(port.name),
            "orientation": float(port.orientation),
            "port_type": str(port.port_type),
            "width": float(port.dwidth),
        }
        for port in component.ports
    ],
}
output_path.write_text(json.dumps(record, sort_keys=True), encoding="utf-8")
"""


def _parquet_path(root: Path, level: str) -> Path:
    matches = sorted((root / "data" / level).glob("train-*.parquet"))
    if len(matches) != 1:
        raise ReleaseError(
            f"{level}: expected exactly one Parquet shard, found {len(matches)}"
        )
    return matches[0]


def _embedded_image(
    example_id: str,
    field: str,
    value: Any,
) -> tuple[bytes, Image.Image]:
    if not isinstance(value, dict) or not value.get("bytes"):
        raise ReleaseError(f"{example_id}: {field} is not embedded")
    payload = bytes(value["bytes"])
    try:
        with Image.open(io.BytesIO(payload)) as opened:
            opened.verify()
        opened = Image.open(io.BytesIO(payload)).convert("L")
    except OSError as exc:
        raise ReleaseError(f"{example_id}: invalid {field}") from exc
    extrema = opened.getextrema()
    if extrema[0] == extrema[1]:
        raise ReleaseError(f"{example_id}: blank {field}")
    return payload, opened


def _parse_json_field(example_id: str, field: str, value: str) -> Any:
    try:
        return json.loads(value)
    except (TypeError, json.JSONDecodeError) as exc:
        raise ReleaseError(f"{example_id}: malformed {field}") from exc


def _validate_row(
    row: dict[str, Any],
    *,
    expected_level: str,
    expected_order: int,
) -> dict[str, Any]:
    example_id = str(row.get("id", ""))
    if not example_id:
        raise ReleaseError(f"{expected_level}[{expected_order}]: missing id")
    if SAFE_PUBLIC_ID.fullmatch(example_id) is None:
        raise ReleaseError(f"{example_id!r}: unsafe public id")
    row_hits = scan_value_strings(example_id, row)
    if row_hits:
        raise ReleaseError("; ".join(row_hits))
    if row["schema_version"] != SCHEMA_VERSION:
        raise ReleaseError(f"{example_id}: schema version drift")
    if row["level"] != expected_level.upper():
        raise ReleaseError(f"{example_id}: wrong level {row['level']}")
    if row["curriculum_order"] != expected_order:
        raise ReleaseError(f"{example_id}: non-contiguous curriculum order")
    if row["code_language"] != "python":
        raise ReleaseError(f"{example_id}: unsupported code language")
    lowered_instruction = row["instruction"].lower()
    for field in ("label", "family"):
        value = str(row[field]).strip().lower()
        if value and value in lowered_instruction:
            raise ReleaseError(
                f"{example_id}: class metadata leaked into instruction via {field}"
            )
    code = row["code"]
    try:
        compile(code, f"{example_id}/ground_truth.py", "exec")
    except SyntaxError as exc:
        raise ReleaseError(f"{example_id}: code does not compile: {exc}") from exc
    violations = purity_violations(code)
    if violations:
        raise ReleaseError(f"{example_id}: purity violations: {violations}")
    forbidden = forbidden_release_imports(code)
    if forbidden:
        raise ReleaseError(f"{example_id}: non-standalone imports: {forbidden}")
    if sha256_bytes(code.encode("utf-8")) != row["code_sha256"]:
        raise ReleaseError(f"{example_id}: code hash mismatch")
    for label, text in (
        ("code", code),
        ("instruction", row["instruction"]),
        ("label", row["label"]),
        ("family", row["family"]),
        ("parameters", row["parameters_json"]),
        ("topology", row["topology_json"]),
        ("metadata", row["metadata_json"]),
    ):
        hits = scan_text(f"{example_id}/{label}", text)
        if hits:
            raise ReleaseError("; ".join(hits))
    if (
        "admission_status" in row["metadata_json"]
        or "quarantine" in row["metadata_json"].lower()
    ):
        raise ReleaseError(f"{example_id}: internal admission state leaked")
    for field in (
        "port_signature_json",
        "parameters_json",
        "topology_json",
        "metadata_json",
    ):
        _parse_json_field(example_id, field, row[field])
    footprint = row["footprint_um"]
    if len(footprint) != 2 or any(float(value) <= 0 for value in footprint):
        raise ReleaseError(f"{example_id}: invalid footprint")

    image_bytes, image = _embedded_image(example_id, "image", row["image"])
    target_bytes, _ = _embedded_image(
        example_id,
        "target_image",
        row["target_image"],
    )
    if sha256_bytes(image_bytes) != row["image_sha256"]:
        raise ReleaseError(f"{example_id}: image hash mismatch")
    if sha256_bytes(target_bytes) != row["target_image_sha256"]:
        raise ReleaseError(f"{example_id}: target image hash mismatch")
    d4_hash = normalized_d4_sha256(image)
    if d4_hash != row["normalized_d4_sha256"]:
        raise ReleaseError(f"{example_id}: D4 witness mismatch")
    return {
        "id": example_id,
        "level": expected_level,
        "image_sha256": row["image_sha256"],
        "normalized_d4_sha256": d4_hash,
        "leakage_group_id": row["leakage_group_id"],
        "code": code,
        "footprint_um": [float(value) for value in footprint],
        "port_signature": _parse_json_field(
            example_id,
            "port_signature_json",
            row["port_signature_json"],
        ),
        "topology": _parse_json_field(
            example_id,
            "topology_json",
            row["topology_json"],
        ),
    }


def _collision_report(rows: list[dict[str, Any]]) -> dict[str, Any]:
    exact: dict[str, list[str]] = defaultdict(list)
    d4: dict[str, list[str]] = defaultdict(list)
    for row in rows:
        exact[row["image_sha256"]].append(row["id"])
        d4[row["normalized_d4_sha256"]].append(row["id"])
    return {
        "exact_duplicate_group_count": sum(len(group) > 1 for group in exact.values()),
        "exact_duplicate_groups": sorted(
            sorted(group) for group in exact.values() if len(group) > 1
        ),
        "d4_equivalent_group_count": sum(len(group) > 1 for group in d4.values()),
        "d4_equivalent_groups": sorted(
            sorted(group) for group in d4.values() if len(group) > 1
        ),
    }


def _expected_port_count(signature: Any) -> int | None:
    if isinstance(signature, dict):
        count = signature.get("count")
        if isinstance(count, int):
            return count
        return None
    if not isinstance(signature, str):
        return None
    pieces = signature.lower().split("x")
    if len(pieces) != 2:
        return 0 if signature.lower() in {"none", "0"} else None
    try:
        return int(pieces[0]) + int(pieces[1])
    except ValueError:
        return None


def _close(left: float, right: float, *, tolerance: float = 0.002) -> bool:
    return math.isclose(left, right, rel_tol=0.0, abs_tol=tolerance)


def _orientation_delta(left: float, right: float) -> float:
    return abs((left - right + 180.0) % 360.0 - 180.0)


def _runtime_ports(
    example_id: str,
    result: dict[str, Any],
) -> list[dict[str, Any]]:
    ports = result.get("ports")
    if not isinstance(ports, list):
        raise ReleaseError(f"{example_id}: execution omitted runtime port records")
    if result.get("port_count") != len(ports):
        raise ReleaseError(f"{example_id}: runtime port records/count disagree")
    normalized: list[dict[str, Any]] = []
    for index, port in enumerate(ports):
        if not isinstance(port, dict):
            raise ReleaseError(f"{example_id}: runtime port {index} is malformed")
        center = port.get("center")
        if (
            not isinstance(center, list)
            or len(center) != 2
            or any(
                not isinstance(value, (int, float)) or not math.isfinite(float(value))
                for value in center
            )
        ):
            raise ReleaseError(
                f"{example_id}: runtime port {index} has an invalid center"
            )
        width = port.get("width")
        if (
            not isinstance(width, (int, float))
            or not math.isfinite(float(width))
            or float(width) <= 0
        ):
            raise ReleaseError(
                f"{example_id}: runtime port {index} has an invalid width"
            )
        orientation = port.get("orientation")
        if not isinstance(orientation, (int, float)) or not math.isfinite(
            float(orientation)
        ):
            raise ReleaseError(
                f"{example_id}: runtime port {index} has an invalid orientation"
            )
        name = port.get("name")
        if not isinstance(name, str) or not name:
            raise ReleaseError(f"{example_id}: runtime port {index} has no name")
        normalized.append(
            {
                "center": [float(value) for value in center],
                "name": name,
                "orientation": float(orientation) % 360.0,
                "port_type": str(port.get("port_type", "")),
                "width": float(width),
            }
        )
    names = [port["name"] for port in normalized]
    if len(names) != len(set(names)):
        raise ReleaseError(f"{example_id}: runtime port names are not unique")
    return normalized


def _validate_l3_ports(
    example_id: str,
    signature: Any,
    ports: list[dict[str, Any]],
) -> None:
    if not isinstance(signature, dict) or not isinstance(signature.get("ports"), list):
        return
    expected_ports = signature["ports"]
    expected_by_name = {
        str(port.get("name")): port
        for port in expected_ports
        if isinstance(port, dict) and port.get("name") is not None
    }
    if len(expected_by_name) != len(expected_ports):
        raise ReleaseError(f"{example_id}: malformed declared L3 port records")
    observed_by_name = {port["name"]: port for port in ports}
    if set(observed_by_name) != set(expected_by_name):
        raise ReleaseError(
            f"{example_id}: runtime L3 port names disagree with declaration"
        )
    declared_names = signature.get("names")
    if isinstance(declared_names, list) and set(map(str, declared_names)) != set(
        observed_by_name
    ):
        raise ReleaseError(f"{example_id}: declared L3 port name list drifted")
    for name, expected in expected_by_name.items():
        observed = observed_by_name[name]
        center = expected.get("center")
        if (
            not isinstance(center, list)
            or len(center) != 2
            or any(
                not isinstance(value, (int, float)) or not math.isfinite(float(value))
                for value in center
            )
        ):
            raise ReleaseError(f"{example_id}: malformed declared L3 port {name}")
        if any(
            not _close(observed_value, float(expected_value))
            for observed_value, expected_value in zip(
                observed["center"],
                center,
                strict=True,
            )
        ):
            raise ReleaseError(f"{example_id}: runtime L3 port {name} center drifted")
        width = expected.get("width")
        orientation = expected.get("orientation")
        if not isinstance(width, (int, float)) or not _close(
            observed["width"],
            float(width),
        ):
            raise ReleaseError(f"{example_id}: runtime L3 port {name} width drifted")
        if (
            not isinstance(orientation, (int, float))
            or _orientation_delta(
                observed["orientation"],
                float(orientation),
            )
            > 0.01
        ):
            raise ReleaseError(
                f"{example_id}: runtime L3 port {name} orientation drifted"
            )


def _validate_l4_ports(
    example_id: str,
    topology: Any,
    result: dict[str, Any],
    ports: list[dict[str, Any]],
) -> None:
    for index, port in enumerate(ports):
        for other in ports[index + 1 :]:
            if (
                all(
                    _close(left, right, tolerance=0.001)
                    for left, right in zip(
                        port["center"],
                        other["center"],
                        strict=True,
                    )
                )
                and _orientation_delta(
                    port["orientation"],
                    other["orientation"],
                )
                <= 1e-6
                and _close(port["width"], other["width"], tolerance=0.001)
            ):
                raise ReleaseError(
                    f"{example_id}: runtime L4 ports contain a duplicated "
                    "physical terminal"
                )
    if not isinstance(topology, dict):
        raise ReleaseError(f"{example_id}: malformed L4 topology contract")
    expected = topology.get("expected_optical_edge_ports")
    if not isinstance(expected, dict):
        raise ReleaseError(f"{example_id}: L4 topology lacks edge-port contract")
    allowed_sides = {"left", "right", "top", "bottom"}
    if any(side not in allowed_sides for side in expected) or any(
        not isinstance(count, int) or count < 0 for count in expected.values()
    ):
        raise ReleaseError(f"{example_id}: malformed L4 edge-port contract")
    bbox = result.get("bbox_um")
    if (
        not isinstance(bbox, list)
        or len(bbox) != 2
        or any(not isinstance(point, list) or len(point) != 2 for point in bbox)
    ):
        raise ReleaseError(f"{example_id}: execution omitted a valid runtime bbox")
    xmin, ymin = (float(value) for value in bbox[0])
    xmax, ymax = (float(value) for value in bbox[1])
    if any(not math.isfinite(value) for value in (xmin, ymin, xmax, ymax)) or not (
        xmin < xmax and ymin < ymax
    ):
        raise ReleaseError(f"{example_id}: execution emitted an invalid runtime bbox")
    side_contract = {
        "left": (0, min, xmin, 1, ymin, ymax, (-1.0, 0.0)),
        "right": (0, max, xmax, 1, ymin, ymax, (1.0, 0.0)),
        "top": (1, max, ymax, 0, xmin, xmax, (0.0, 1.0)),
        "bottom": (1, min, ymin, 0, xmin, xmax, (0.0, -1.0)),
    }
    strongest_side_options: list[tuple[str, list[str]]] = []
    for port in ports:
        angle = math.radians(port["orientation"])
        direction = (math.cos(angle), math.sin(angle))
        tangent = (-direction[1], direction[0])
        endpoints = [
            [
                port["center"][axis] + sign * tangent[axis] * port["width"] / 2
                for axis in (0, 1)
            ]
            for sign in (-1.0, 1.0)
        ]
        matches = []
        for side, (
            axis,
            extreme,
            boundary,
            span_axis,
            span_min,
            span_max,
            outward_normal,
        ) in side_contract.items():
            outward_component = sum(
                value * normal
                for value, normal in zip(direction, outward_normal, strict=True)
            )
            edge_coordinate = extreme(point[axis] for point in endpoints)
            span_coordinates = [point[span_axis] for point in endpoints]
            if (
                _close(edge_coordinate, boundary)
                and max(span_coordinates) >= span_min - 0.002
                and min(span_coordinates) <= span_max + 0.002
                and outward_component > 1e-6
            ):
                matches.append((outward_component, side))
        if not matches:
            raise ReleaseError(
                f"{example_id}: runtime L4 port {port['name']} has no outward "
                "terminal cross-section on the component boundary"
            )
        strongest = max(component for component, _side in matches)
        options = sorted(
            side
            for component, side in matches
            if math.isclose(component, strongest, rel_tol=0.0, abs_tol=1e-9)
        )
        strongest_side_options.append((port["name"], options))

    remaining = {side: int(expected.get(side, 0)) for side in sorted(allowed_sides)}

    def assign(index: int) -> bool:
        if index == len(strongest_side_options):
            return all(count == 0 for count in remaining.values())
        _name, options = strongest_side_options[index]
        for side in options:
            if remaining[side] <= 0:
                continue
            remaining[side] -= 1
            if assign(index + 1):
                return True
            remaining[side] += 1
        return False

    if not assign(0):
        raise ReleaseError(
            f"{example_id}: runtime L4 terminal cross-sections cannot satisfy "
            f"the declared edge-port histogram {dict(expected)}"
        )


def _validate_runtime_port_contract(
    row: dict[str, Any],
    result: dict[str, Any],
) -> None:
    example_id = row["id"]
    expected_ports = _expected_port_count(row["port_signature"])
    if expected_ports is not None and result["port_count"] != expected_ports:
        raise ReleaseError(
            f"{example_id}: port count mismatch "
            f"expected={expected_ports} observed={result['port_count']}"
        )
    ports = _runtime_ports(example_id, result)
    level = row["level"]
    if level == "l3":
        _validate_l3_ports(example_id, row["port_signature"], ports)
    elif level == "l4":
        _validate_l4_ports(example_id, row["topology"], result, ports)


def _execute_one(
    row: dict[str, Any],
    workspace: Path,
    timeout_seconds: float,
) -> dict[str, Any]:
    example_id = row["id"]
    task_dir = workspace / example_id
    task_dir.mkdir(parents=True, exist_ok=False)
    program_path = task_dir / "ground_truth.py"
    result_path = task_dir / "result.json"
    program_path.write_text(row["code"], encoding="utf-8")
    runner_path = workspace / "_runner.py"
    environment = dict(os.environ)
    environment.update(
        {
            "MPLCONFIGDIR": str(task_dir / ".mpl"),
            "XDG_CACHE_HOME": str(task_dir / ".cache"),
        }
    )
    completed = subprocess.run(
        [sys.executable, str(runner_path), str(program_path), str(result_path)],
        cwd=task_dir,
        env=environment,
        capture_output=True,
        text=True,
        timeout=timeout_seconds,
        check=False,
    )
    if completed.returncode != 0:
        tail = (completed.stderr or completed.stdout)[-2000:]
        raise ReleaseError(
            f"{example_id}: execution failed ({completed.returncode}): {tail}"
        )
    result = read_json(result_path)
    if result["gds_bytes"] <= 100 or result["polygon_count"] <= 0:
        raise ReleaseError(f"{example_id}: execution emitted empty geometry")
    observed = [float(value) for value in result["bbox_span_um"]]
    if len(observed) != 2 or any(
        not math.isfinite(value) or value <= 0 for value in observed
    ):
        raise ReleaseError(f"{example_id}: execution emitted an invalid GDS bbox")
    _validate_runtime_port_contract(row, result)
    return {"id": example_id, **result}


def execute_programs(
    rows: list[dict[str, Any]],
    *,
    jobs: int,
    timeout_seconds: float,
) -> list[dict[str, Any]]:
    with tempfile.TemporaryDirectory(prefix="pixcell-release-execution-") as raw:
        workspace = Path(raw)
        (workspace / "_runner.py").write_text(EXECUTION_RUNNER, encoding="utf-8")
        with concurrent.futures.ThreadPoolExecutor(max_workers=max(1, jobs)) as pool:
            futures = {
                pool.submit(_execute_one, row, workspace, timeout_seconds): row["id"]
                for row in rows
            }
            results: list[dict[str, Any]] = []
            for future in concurrent.futures.as_completed(futures):
                example_id = futures[future]
                try:
                    results.append(future.result())
                except Exception as exc:
                    for pending in futures:
                        pending.cancel()
                    if isinstance(exc, ReleaseError):
                        raise
                    raise ReleaseError(f"{example_id}: execution error: {exc}") from exc
    return sorted(results, key=lambda item: item["id"])


def validate_release(
    root: Path,
    *,
    execute: bool = False,
    jobs: int = 4,
    timeout_seconds: float = 90.0,
) -> dict[str, Any]:
    root = root.expanduser().resolve()
    verify_checksums(root)
    for path in iter_release_files(root):
        if path.suffix.lower() not in {
            ".cff",
            ".json",
            ".md",
            ".py",
            ".txt",
            ".yaml",
            ".yml",
        }:
            continue
        text = path.read_text(encoding="utf-8")
        hits = scan_text(path.relative_to(root).as_posix(), text)
        if hits:
            raise ReleaseError("; ".join(hits))
        if path.suffix == ".py":
            forbidden = forbidden_release_imports(text)
            if forbidden:
                raise ReleaseError(
                    f"{path.relative_to(root)}: forbidden package imports {forbidden}"
                )
    card_path = root / "README.md"
    card_text = card_path.read_text(encoding="utf-8")
    if any(mark in card_text for mark in ("—", "–", "·")):
        raise ReleaseError("dataset card contains decorative dash or separator marks")
    card = DatasetCard.load(str(card_path))
    card_data = card.data.to_dict()
    if card_data.get("license") != "mit":
        raise ReleaseError("dataset card must declare the MIT license")
    expected_card_metadata = {
        "pretty_name": "PixCell Dataset",
        "language": ["en", "code"],
        "annotations_creators": ["expert-generated", "machine-generated"],
        "source_datasets": ["original"],
        "task_categories": ["image-text-to-text"],
        "size_categories": ["1K<n<10K"],
        "tags": [
            "image",
            "text",
            "code",
            "python",
            "code-generation",
            "vision-language",
            "photonics",
            "gds",
            "gdsfactory",
            "image-to-code",
            "multimodal",
            "synthetic",
            "curriculum-learning",
            "datasets",
        ],
    }
    for field, expected in expected_card_metadata.items():
        if card_data.get(field) != expected:
            raise ReleaseError(f"dataset card metadata drift for {field}")
    configs = card_data.get("configs")
    if not isinstance(configs, list):
        raise ReleaseError("dataset card has no configurations")
    config_names = [record.get("config_name") for record in configs]
    if config_names != ["all", "core-v1", *LEVELS, "depth-v1"]:
        raise ReleaseError(f"dataset card configuration drift: {config_names}")
    if configs[0].get("default") is not True:
        raise ReleaseError("dataset card's all configuration is not default")
    core_paths = [f"data/{level}/train-*.parquet" for level in LEVELS]
    expected_data_files = {
        "all": [{"split": "train", "path": core_paths}],
        "core-v1": [{"split": "train", "path": core_paths}],
        **{
            level: [
                {
                    "split": "train",
                    "path": f"data/{level}/train-*.parquet",
                }
            ]
            for level in LEVELS
        },
        "depth-v1": [
            {"split": "train", "path": "depth-v1/data/train-*.parquet"},
            {
                "split": "validation",
                "path": "depth-v1/data/validation-*.parquet",
            },
        ],
    }
    for record in configs:
        name = str(record["config_name"])
        if record.get("data_files") != expected_data_files[name]:
            raise ReleaseError(f"dataset card data_files drift for {name}")
    expected_dataset_info = [
        {
            "config_name": "all",
            "splits": [{"name": "train", "num_examples": 738}],
        },
        {
            "config_name": "core-v1",
            "splits": [{"name": "train", "num_examples": 738}],
        },
        *[
            {
                "config_name": level,
                "splits": [
                    {
                        "name": "train",
                        "num_examples": count,
                    }
                ],
            }
            for level, count in zip(LEVELS, (177, 215, 118, 120, 108), strict=True)
        ],
        {
            "config_name": "depth-v1",
            "splits": [
                {"name": "train", "num_examples": 3468},
                {"name": "validation", "num_examples": 1092},
            ],
        },
    ]
    if card_data.get("dataset_info") != expected_dataset_info:
        raise ReleaseError("dataset card split metadata drift")
    catalog = read_json(root / "metadata" / "catalog.json")
    if catalog.get("schema_version") != SCHEMA_VERSION:
        raise ReleaseError("catalog schema version drift")
    if catalog.get("accepted_only") is not True:
        raise ReleaseError("catalog is not accepted-only")
    if catalog.get("columns") != list(ROW_COLUMNS):
        raise ReleaseError("catalog columns drifted")
    if catalog.get("configurations") != ["all", "core-v1", *LEVELS]:
        raise ReleaseError("catalog configuration drift")
    freeze = read_json(root / "factory" / "core-v1" / "freeze.json")
    if freeze.get("core_name") != "core-v1":
        raise ReleaseError("core-v1 freeze name drift")
    if freeze.get("release_digest_sha256") != catalog.get("release_digest_sha256"):
        raise ReleaseError("catalog differs from frozen core-v1 digest")
    if freeze.get("row_count") != catalog.get("total_rows"):
        raise ReleaseError("catalog differs from frozen core-v1 row count")
    source_bundle = freeze.get("source_bundle")
    if not isinstance(source_bundle, dict):
        raise ReleaseError("core-v1 freeze has no source bundle")
    source_archive = root / str(source_bundle.get("path", ""))
    if not source_archive.is_file():
        raise ReleaseError("core-v1 source archive is missing")
    if sha256_file(source_archive) != source_bundle.get("sha256"):
        raise ReleaseError("core-v1 source archive digest drift")
    grammar = freeze.get("grammar_catalog")
    if not isinstance(grammar, dict):
        raise ReleaseError("core-v1 freeze has no grammar catalog")
    grammar_path = root / str(grammar.get("path", ""))
    if not grammar_path.is_file():
        raise ReleaseError("core-v1 grammar catalog is missing")
    if sha256_file(grammar_path) != grammar.get("sha256"):
        raise ReleaseError("core-v1 grammar catalog digest drift")
    grammar_catalog = read_json(grammar_path)
    if grammar_catalog.get("row_count") != catalog.get(
        "total_rows"
    ) or grammar_catalog.get("representation_count") != catalog.get(
        "total_representation_ids"
    ):
        raise ReleaseError("core-v1 grammar catalog count drift")
    policy = freeze.get("admission_policy")
    if not isinstance(policy, dict):
        raise ReleaseError("core-v1 freeze has no admission policy")
    policy_path = root / str(policy.get("path", ""))
    if not policy_path.is_file() or sha256_file(policy_path) != policy.get("sha256"):
        raise ReleaseError("core-v1 admission policy digest drift")
    reproducibility = freeze.get("reproducibility_report")
    if not isinstance(reproducibility, dict):
        raise ReleaseError("core-v1 freeze has no reproducibility report")
    reproducibility_path = root / str(reproducibility.get("path", ""))
    if not reproducibility_path.is_file() or sha256_file(
        reproducibility_path
    ) != reproducibility.get("sha256"):
        raise ReleaseError("core-v1 reproducibility report digest drift")

    validated: list[dict[str, Any]] = []
    digest_records: list[dict[str, Any]] = []
    level_counts: dict[str, int] = {}
    curriculum_order = 0
    for level in LEVELS:
        path = _parquet_path(root, level)
        table = pq.read_table(path)
        if table.schema.names != list(ROW_COLUMNS):
            raise ReleaseError(f"{level}: Parquet column order/schema drift")
        if b"huggingface" not in (table.schema.metadata or {}):
            raise ReleaseError(f"{level}: Hugging Face feature metadata is missing")
        rows = table.to_pylist()
        digest_records.extend(release_digest_record(row) for row in rows)
        level_counts[level] = len(rows)
        expected_count = catalog["levels"][level]["row_count"]
        if len(rows) != expected_count:
            raise ReleaseError(
                f"{level}: row count {len(rows)} != catalog {expected_count}"
            )
        validated.extend(
            _validate_row(
                row,
                expected_level=level,
                expected_order=curriculum_order + index,
            )
            for index, row in enumerate(rows)
        )
        curriculum_order += len(rows)
    ids = [row["id"] for row in validated]
    if len(ids) != len(set(ids)):
        duplicates = [item for item, count in Counter(ids).items() if count > 1]
        raise ReleaseError(f"duplicate release IDs: {duplicates}")
    if len(validated) != catalog["total_rows"]:
        raise ReleaseError("catalog total row count drift")
    release_digest = canonical_sha256(digest_records)
    if release_digest != catalog["release_digest_sha256"]:
        raise ReleaseError("release digest drift")

    recomputed = _collision_report(validated)
    catalog_collisions = catalog["collision_report"]
    for key in (
        "exact_duplicate_group_count",
        "exact_duplicate_groups",
        "d4_equivalent_group_count",
        "d4_equivalent_groups",
    ):
        if recomputed[key] != catalog_collisions[key]:
            raise ReleaseError(f"collision report drift: {key}")
    for group in recomputed["exact_duplicate_groups"]:
        leakage_ids = {
            row["leakage_group_id"] for row in validated if row["id"] in group
        }
        if len(leakage_ids) != 1:
            raise ReleaseError(f"exact image aliases cross leakage boundaries: {group}")

    execution_results: list[dict[str, Any]] = []
    if execute:
        execution_results = execute_programs(
            validated,
            jobs=jobs,
            timeout_seconds=timeout_seconds,
        )
    return {
        "schema_version": SCHEMA_VERSION,
        "release_digest_sha256": catalog["release_digest_sha256"],
        "total_rows": len(validated),
        "level_counts": level_counts,
        "compiled_programs": len(validated),
        "purity_clean_programs": len(validated),
        "embedded_images": 2 * len(validated),
        "executed_programs": len(execution_results),
        "collision_report": recomputed,
    }


def _parser() -> argparse.ArgumentParser:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument(
        "--root",
        type=Path,
        default=Path(__file__).resolve().parents[1],
        help="Dataset package root (default: parent of scripts/).",
    )
    parser.add_argument(
        "--execute-programs",
        action="store_true",
        help=(
            "Execute every program and verify emitted GDS and runtime port contracts."
        ),
    )
    parser.add_argument("--jobs", type=int, default=min(8, os.cpu_count() or 4))
    parser.add_argument("--timeout-seconds", type=float, default=90.0)
    return parser


def main() -> None:
    args = _parser().parse_args()
    report = validate_release(
        args.root,
        execute=args.execute_programs,
        jobs=args.jobs,
        timeout_seconds=args.timeout_seconds,
    )
    print(json.dumps(report, indent=2, sort_keys=True))


if __name__ == "__main__":
    main()