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
 
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
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
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
"""Attest run 1 shards and deterministically normalize them into E2 no-FIM data.

The E2 arm must not reopen upstream datasets. Run 1 did not preserve an ordered
raw-row manifest, and its source loader used different parser dispatch from the
corrected loader. The strongest available input is therefore the exact token
stream already used by run 1. Literal control strings created observationally
ambiguous EOS boundaries, so the receipt records a deterministic grammar
recovery and its limits rather than claiming unobservable original boundaries.

This tool has two CPU-only commands:

    python scripts/derive_no_fim.py attest \
      --index data/shards/index.json \
      --tokenizer tokenizer/code32k.json \
      --out config/run1_shard_integrity_receipt.json

    python scripts/derive_no_fim.py build \
      --config config/run2_no_fim.json
"""

import argparse
from datetime import datetime, timezone
import hashlib
import json
import os
import struct
import sys

import numpy as np
from tokenizers import Tokenizer

sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from data import canonical_json_sha256, file_sha256, stable_file_sha256
from scripts.hf_metadata import write_json_atomic
from scripts.prepare_data import require_fresh_output_dir


ALGORITHM = "run1_deterministic_no_fim_normalization_v1"
INDEX_SCHEMA_VERSION = 3
RECEIPT_SCHEMA_VERSION = 1
DEFAULT_CHUNK_BYTES = 8 * 1024 * 1024
SHARD_TOKENS = 100_000_000
KIND_CODES = {"l2r": 0, "fim_psm": 1, "fim_spm": 2}
UNIT_RECORD_DTYPE = np.dtype(
    [
        ("length", "<u4"),
        ("kind", "u1"),
        ("reserved", "u1", (3,)),
    ]
)


def load_json(path):
    with open(path, encoding="utf-8") as f:
        value = json.load(f)
    if not isinstance(value, dict):
        raise ValueError(f"{path}: top-level JSON must be an object")
    return value


def special_token_ids(tokenizer_path):
    tokenizer = Tokenizer.from_file(tokenizer_path)
    names = {
        "eos": "<|endoftext|>",
        "prefix": "<|fim_prefix|>",
        "middle": "<|fim_middle|>",
        "suffix": "<|fim_suffix|>",
    }
    values = {key: tokenizer.token_to_id(value) for key, value in names.items()}
    if any(value is None for value in values.values()):
        raise ValueError("tokenizer is missing an EOS or FIM special token")
    if len(set(values.values())) != len(values):
        raise ValueError("EOS and FIM special-token ids must be distinct")
    return values, tokenizer.get_vocab_size()


def _as_uint16_le(ids):
    value = np.asarray(ids, dtype="<u2")
    if value.ndim != 1:
        raise ValueError("token unit must be one-dimensional")
    return value


def fim_scan_state(ids, special_ids):
    """Classify a segment as L2R, complete FIM, or visibly incomplete FIM."""
    ids = _as_uint16_le(ids)
    prefix_id = special_ids["prefix"]
    middle_id = special_ids["middle"]
    suffix_id = special_ids["suffix"]
    positions = {
        "prefix": np.flatnonzero(ids == prefix_id),
        "middle": np.flatnonzero(ids == middle_id),
        "suffix": np.flatnonzero(ids == suffix_id),
    }
    counts = {key: int(value.size) for key, value in positions.items()}
    if counts == {"prefix": 0, "middle": 0, "suffix": 0}:
        return "l2r"
    if any(value > 1 for value in counts.values()):
        raise ValueError(f"malformed FIM sentinel counts: {counts}")
    if counts["prefix"] != 1:
        raise ValueError("FIM sentinel fragment has no prefix sentinel")

    prefix = int(positions["prefix"][0])
    if prefix != 0:
        raise ValueError("malformed FIM unit does not begin with prefix sentinel")
    if counts["suffix"] == 0:
        if counts["middle"]:
            raise ValueError("FIM middle sentinel appears before suffix sentinel")
        return "incomplete"

    suffix = int(positions["suffix"][0])
    if suffix == 1:
        if counts["middle"] == 0:
            return "incomplete"
        middle = int(positions["middle"][0])
        if middle <= suffix:
            raise ValueError("SPM middle sentinel precedes its suffix payload")
        if middle < 4 or ids.size - middle - 1 < 2:
            return "incomplete"
        return "complete"

    if suffix < 2:
        raise ValueError("malformed PSM unit has an empty prefix")
    if counts["middle"] == 0:
        return "incomplete"
    middle = int(positions["middle"][0])
    if middle <= suffix:
        raise ValueError("PSM middle sentinel precedes its suffix payload")
    if middle < suffix + 3 or middle >= ids.size - 1:
        return "incomplete"
    return "complete"


def decode_fim_unit(ids, special_ids):
    """Return `(kind, raw_ids)` for one complete recovered run 1 unit."""
    ids = _as_uint16_le(ids)
    state = fim_scan_state(ids, special_ids)
    if state == "l2r":
        return "l2r", ids
    if state == "incomplete":
        raise ValueError("incomplete FIM unit")
    prefix_id = special_ids["prefix"]
    middle_id = special_ids["middle"]
    suffix_id = special_ids["suffix"]
    prefix = int(np.flatnonzero(ids == prefix_id)[0])
    middle = int(np.flatnonzero(ids == middle_id)[0])
    suffix = int(np.flatnonzero(ids == suffix_id)[0])
    if prefix != 0:
        raise AssertionError("complete FIM unit lost its prefix position")

    if suffix == 1:
        raw = _as_uint16_le(
            np.concatenate((ids[middle + 1:], ids[2:middle]))
        )
        if any(
            np.any(raw == special_ids[key])
            for key in ("prefix", "middle", "suffix")
        ):
            raise ValueError("decoded SPM raw unit retains a FIM sentinel")
        if not 16 <= raw.size <= 1024:
            raise ValueError("decoded SPM raw length is outside 16 to 1024")
        return "fim_spm", raw

    raw = _as_uint16_le(
        np.concatenate(
            (
                ids[1:suffix],
                ids[middle + 1:],
                ids[suffix + 1:middle],
            )
        )
    )
    if any(
        np.any(raw == special_ids[key])
        for key in ("prefix", "middle", "suffix")
    ):
        raise ValueError("decoded PSM raw unit retains a FIM sentinel")
    if not 16 <= raw.size <= 1024:
        raise ValueError("decoded PSM raw length is outside 16 to 1024")
    return "fim_psm", raw


class UnitEvidence:
    """Streaming hashes and counts for recovered ordered raw units."""

    def __init__(self, eos_token_id, domain="unspecified"):
        self.eos_token_id = eos_token_id
        self.domain = domain.encode("utf-8")
        self.ordered = hashlib.sha256()
        self.destination = hashlib.sha256()
        self.source_wire = hashlib.sha256()
        for digest, label in (
            (self.ordered, b"RECOVERED_RAW"),
            (self.destination, b"DESTINATION"),
            (self.source_wire, b"SOURCE_WIRE"),
        ):
            digest.update(b"WISP_NO_FIM_V1\0" + label + b"\0")
            digest.update(struct.pack("<Q", len(self.domain)))
            digest.update(self.domain)
        self.units = 0
        self.source_tokens = 0
        self.derived_tokens = 0
        self.raw_tokens = 0
        self.empty_units = 0
        self.internal_eos_tokens = 0
        self.counts = {"l2r": 0, "fim_psm": 0, "fim_spm": 0}
        self._eos_bytes = np.asarray([eos_token_id], dtype="<u2").tobytes()
        self._source_wire_complete = True

    def add(
        self,
        source_ids,
        kind,
        raw,
        internal_eos_tokens=0,
    ):
        raw = _as_uint16_le(raw)
        if kind not in KIND_CODES:
            raise ValueError(f"unknown recovered unit kind: {kind}")
        if kind == "l2r":
            if not 1 <= raw.size <= 1024:
                raise ValueError("plain recovered raw length is outside 1 to 1024")
        elif not 16 <= raw.size <= 1024:
            raise ValueError("FIM recovered raw length is outside 16 to 1024")
        raw_bytes = raw.tobytes()
        ordinal = self.units
        kind_code = KIND_CODES[kind]
        self.ordered.update(
            b"U"
            + struct.pack("<QBQ", ordinal, kind_code, int(raw.size))
        )
        self.ordered.update(raw_bytes)
        destination_bytes = raw_bytes + self._eos_bytes
        self.destination.update(
            b"U"
            + struct.pack(
                "<QBQ",
                ordinal,
                kind_code,
                int(raw.size) + 1,
            )
        )
        self.destination.update(destination_bytes)
        if source_ids is None:
            source_length = int(raw.size) + (3 if kind != "l2r" else 0)
            self._source_wire_complete = False
        else:
            source_ids = _as_uint16_le(source_ids)
            source_length = int(source_ids.size)
            self.source_wire.update(
                b"U"
                + struct.pack(
                    "<QBQ",
                    ordinal,
                    kind_code,
                    source_length + 1,
                )
            )
            self.source_wire.update(source_ids.tobytes())
            self.source_wire.update(self._eos_bytes)
        self.units += 1
        self.source_tokens += int(source_length) + 1
        self.raw_tokens += int(raw.size)
        self.derived_tokens += int(raw.size) + 1
        self.empty_units += int(raw.size == 0)
        self.internal_eos_tokens += int(internal_eos_tokens)
        self.counts[kind] += 1

    def summary(self):
        removed = self.source_tokens - self.derived_tokens
        fim_units = self.counts["fim_psm"] + self.counts["fim_spm"]
        if removed != 3 * fim_units:
            raise RuntimeError(
                "recovered stream did not remove exactly three tokens per "
                "FIM unit"
            )
        footer = b"END" + struct.pack(
            "<QQQQQQ",
            self.units,
            self.source_tokens,
            self.raw_tokens,
            self.derived_tokens,
            self.counts["l2r"],
            fim_units,
        )
        ordered = self.ordered.copy()
        ordered.update(footer)
        destination = self.destination.copy()
        destination.update(footer)
        source_wire = self.source_wire.copy()
        source_wire.update(footer)
        result = {
            "units": self.units,
            "source_tokens": self.source_tokens,
            "raw_tokens": self.raw_tokens,
            "derived_tokens": self.derived_tokens,
            "removed_fim_tokens": removed,
            "empty_eos_segments": self.empty_units,
            "reassembled_internal_eos_tokens": self.internal_eos_tokens,
            "l2r_units": self.counts["l2r"],
            "fim_psm_units": self.counts["fim_psm"],
            "fim_spm_units": self.counts["fim_spm"],
            "ordered_raw_units_sha256": ordered.hexdigest(),
            "destination_units_sha256": destination.hexdigest(),
        }
        if self._source_wire_complete:
            result["source_wire_units_sha256"] = source_wire.hexdigest()
        return result


def _same_summary(left, right):
    return canonical_json_sha256(left) == canonical_json_sha256(right)


def _normalization_evidence(value):
    keys = (
        "units",
        "source_tokens",
        "raw_tokens",
        "derived_tokens",
        "removed_fim_tokens",
        "l2r_units",
        "fim_psm_units",
        "fim_spm_units",
        "ordered_raw_units_sha256",
        "destination_units_sha256",
    )
    return {key: value.get(key) for key in keys}


def scan_shard(
    path,
    special_ids,
    expected_tokens=None,
    emit=None,
    split_evidence=None,
    evidence_domain="shard:unspecified",
    vocab_size=None,
    chunk_bytes=DEFAULT_CHUNK_BYTES,
):
    """Scan one shard once, hashing bytes and decoding every EOS segment."""
    if chunk_bytes < 2 or chunk_bytes % 2:
        raise ValueError("scan chunk size must be a positive even byte count")
    if os.path.islink(path):
        raise ValueError(f"{path}: shard cannot be a symbolic link")
    if not os.path.isfile(path):
        raise FileNotFoundError(path)
    before = os.stat(path, follow_symlinks=False)
    size = before.st_size
    if size % 2:
        raise ValueError(f"{path}: uint16 shard has an odd byte length")
    tokens = size // 2
    if expected_tokens is not None and tokens != expected_tokens:
        raise ValueError(
            f"{path}: {tokens} tokens != declared {expected_tokens}"
        )

    local = UnitEvidence(special_ids["eos"], evidence_domain)
    split_evidence = split_evidence or UnitEvidence(
        special_ids["eos"],
        "split:unspecified",
    )
    shard_digest = hashlib.sha256()
    carry = np.empty(0, dtype="<u2")
    pending = None
    pending_internal_eos = 0
    pending_start_segment = None
    pending_start_token = None
    eos_segment_ordinal = 0
    reassembly_groups = []
    token_offset = 0
    unit_ordinal = 0
    with open(path, "rb") as f:
        while True:
            block = f.read(chunk_bytes)
            if not block:
                break
            shard_digest.update(block)
            if len(block) % 2:
                raise ValueError(f"{path}: partial uint16 token in scan chunk")
            current = np.frombuffer(block, dtype="<u2")
            if (
                vocab_size is not None
                and current.size
                and int(current.max()) >= vocab_size
            ):
                raise ValueError(f"{path}: token id is outside tokenizer vocab")
            if carry.size:
                current = np.concatenate((carry, current))
            starts_at = token_offset - int(carry.size)
            boundaries = np.flatnonzero(current == special_ids["eos"])
            start = 0
            for boundary_value in boundaries:
                boundary = int(boundary_value)
                segment = current[start:boundary]
                if pending is not None:
                    segment = np.concatenate((pending, segment))
                try:
                    state = fim_scan_state(segment, special_ids)
                except ValueError as exc:
                    absolute = starts_at + start
                    raise ValueError(
                        f"{path}: malformed unit {unit_ordinal} at token "
                        f"{absolute}: {exc}"
                    ) from exc
                if state == "incomplete":
                    if segment.size >= 1027:
                        raise ValueError(
                            f"{path}: incomplete FIM unit {unit_ordinal} "
                            "exceeds the maximum framed length"
                        )
                    pending = np.concatenate(
                        (
                            segment,
                            np.asarray(
                                [special_ids["eos"]],
                                dtype="<u2",
                            ),
                        )
                    )
                    if pending_start_segment is None:
                        pending_start_segment = eos_segment_ordinal
                        pending_start_token = starts_at + start
                    pending_internal_eos += 1
                    start = boundary + 1
                    eos_segment_ordinal += 1
                    continue
                kind, raw = decode_fim_unit(segment, special_ids)
                local.add(
                    segment,
                    kind,
                    raw,
                    internal_eos_tokens=pending_internal_eos,
                )
                split_evidence.add(
                    segment,
                    kind,
                    raw,
                    internal_eos_tokens=pending_internal_eos,
                )
                if emit is not None:
                    emit(raw, special_ids["eos"], kind)
                if pending_internal_eos:
                    reassembly_groups.append(
                        {
                            "unit_ordinal": unit_ordinal,
                            "first_eos_segment": pending_start_segment,
                            "last_eos_segment": eos_segment_ordinal,
                            "internal_eos_tokens": pending_internal_eos,
                            "source_token_start": pending_start_token,
                            "writer_eos_token": starts_at + boundary,
                        }
                    )
                unit_ordinal += 1
                pending = None
                pending_internal_eos = 0
                pending_start_segment = None
                pending_start_token = None
                start = boundary + 1
                eos_segment_ordinal += 1
            carry = current[start:].copy()
            if carry.size > 1027:
                raise ValueError(
                    f"{path}: unterminated unit exceeds maximum wire length"
                )
            token_offset += len(block) // 2
    if pending is not None:
        raise ValueError(f"{path}: incomplete FIM unit at shard boundary")
    if carry.size:
        raise ValueError(
            f"{path}: shard does not end at an EOS-delimited unit boundary"
        )
    after = os.stat(path, follow_symlinks=False)
    if (
        before.st_dev,
        before.st_ino,
        before.st_size,
        before.st_mtime_ns,
    ) != (
        after.st_dev,
        after.st_ino,
        after.st_size,
        after.st_mtime_ns,
    ):
        raise RuntimeError(f"{path}: shard changed during integrity scan")
    summary = local.summary()
    return {
        "bytes": size,
        "tokens": tokens,
        "sha256": shard_digest.hexdigest(),
        "reassembly_groups": reassembly_groups,
        **summary,
    }


def scan_index(index_path, tokenizer_path):
    index = load_json(index_path)
    special_ids, vocab_size = special_token_ids(tokenizer_path)
    tokenizer_sha256 = file_sha256(tokenizer_path)
    if index.get("vocab_size") != vocab_size:
        raise ValueError("source index vocab size differs from tokenizer")
    root = os.path.dirname(os.path.abspath(index_path))
    split_receipts = {}
    for split in ("train", "val"):
        entries = index.get("splits", {}).get(split)
        if not isinstance(entries, list) or not entries:
            raise ValueError(f"source index split {split} is empty")
        paths = [entry.get("path") for entry in entries]
        if len(set(paths)) != len(paths):
            raise ValueError(f"source index split {split} repeats a shard path")
        split_evidence = UnitEvidence(
            special_ids["eos"],
            f"split:{split}:tokenizer:{tokenizer_sha256}",
        )
        shard_receipts = []
        for entry in entries:
            relative = entry.get("path")
            if (
                not isinstance(relative, str)
                or not relative
                or os.path.isabs(relative)
                or os.path.normpath(relative) != relative
                or relative == ".."
                or relative.startswith(".." + os.sep)
            ):
                raise ValueError(
                    f"source index split {split} has unsafe shard path"
                )
            path = os.path.join(root, relative)
            shard = scan_shard(
                path,
                special_ids,
                expected_tokens=entry.get("tokens"),
                split_evidence=split_evidence,
                evidence_domain=(
                    f"shard:{split}:{relative}:tokenizer:{tokenizer_sha256}"
                ),
                vocab_size=vocab_size,
            )
            shard_receipts.append({"path": relative, **shard})
        summary = split_evidence.summary()
        declared_total = entries[0].get("total_tokens")
        if summary["source_tokens"] != declared_total:
            raise ValueError(
                f"source split {split} scan total differs from index"
            )
        split_receipts[split] = {
            **summary,
            "shards": shard_receipts,
            "shard_manifest_sha256": canonical_json_sha256(
                [
                    {
                        key: shard[key]
                        for key in ("path", "tokens", "bytes", "sha256")
                    }
                    for shard in shard_receipts
                ]
            ),
        }
    return index, special_ids, split_receipts


def build_attestation(index_path, tokenizer_path):
    index, special_ids, splits = scan_index(index_path, tokenizer_path)
    reassembly_groups = sum(
        len(shard["reassembly_groups"])
        for shard in splits["train"]["shards"]
        + splits["val"]["shards"]
    )
    restored_internal_eos = sum(
        split["reassembled_internal_eos_tokens"]
        for split in splits.values()
    )
    return {
        "schema_version": RECEIPT_SCHEMA_VERSION,
        "created_at": datetime.now(timezone.utc).isoformat(),
        "status": "complete",
        "algorithm": ALGORITHM,
        "dtype": "uint16_le",
        "source_index": {
            "path": index_path,
            "sha256": file_sha256(index_path),
            "schema_version": index.get("schema_version", 1),
        },
        "tokenizer": {
            "path": tokenizer_path,
            "sha256": file_sha256(tokenizer_path),
            "vocab_size": index["vocab_size"],
        },
        "special_token_ids": special_ids,
        "attestation_script": {
            "path": os.path.relpath(
                os.path.abspath(__file__),
                os.getcwd(),
            ),
            "sha256": file_sha256(__file__),
        },
        "boundary_recovery": {
            "exact_original_units_proven": False,
            "mode": "deterministic_visible_grammar_normalization",
            "rule": (
                "Start only at a partial prefix-bearing EOS segment, append "
                "following same-shard segments, restore each intervening EOS "
                "as payload, and stop at the first complete valid FIM grammar."
            ),
            "detectable_reassembly_groups": reassembly_groups,
            "restored_internal_eos_tokens": restored_internal_eos,
            "cross_shard_reassembly_allowed": False,
        },
        "splits": splits,
        "limitations": [
            (
                "Literal EOS tokens in L2R content and after the final FIM "
                "marker can be observationally indistinguishable from writer "
                "boundaries."
            ),
            (
                "A raw untransformed segment that exactly mimics one valid FIM "
                "frame is structurally indistinguishable from a generated frame."
            ),
            (
                "This post-build receipt binds current token bytes and visible "
                "grammar, not raw source rows, build-time shard hashes, or exact "
                "original unit boundaries."
            ),
        ],
    }


class DerivedShardWriter:
    """Write one destination shard plus a fixed-width unit-boundary sidecar."""

    def __init__(self, out_dir, split, name):
        self.out_dir = out_dir
        self.split = split
        self.name = name
        self.buf = []
        self.records = []
        self.total = 0

    def add_raw(self, raw, eos_token_id, kind):
        raw = _as_uint16_le(raw)
        unit = np.empty(raw.size + 1, dtype="<u2")
        unit[:-1] = raw
        unit[-1] = eos_token_id
        self.buf.append(unit)
        self.records.append((int(unit.size), KIND_CODES[kind]))
        self.total += int(unit.size)

    def flush(self):
        if not self.buf:
            raise ValueError(f"derived shard {self.name} has no units")
        values = np.concatenate(self.buf).astype("<u2", copy=False)
        path = os.path.join(self.out_dir, self.name)
        if os.path.lexists(path):
            raise FileExistsError(f"refusing to replace derived shard: {path}")
        digest = hashlib.sha256()
        digest.update(memoryview(values).cast("B"))
        with open(path, "xb") as f:
            values.tofile(f)
            f.flush()
            os.fsync(f.fileno())
        sidecar_name = self.name + ".units.bin"
        sidecar_path = os.path.join(self.out_dir, sidecar_name)
        if os.path.lexists(sidecar_path):
            raise FileExistsError(
                f"refusing to replace unit sidecar: {sidecar_path}"
            )
        records = np.zeros(len(self.records), dtype=UNIT_RECORD_DTYPE)
        records["length"] = [value[0] for value in self.records]
        records["kind"] = [value[1] for value in self.records]
        sidecar_digest = hashlib.sha256()
        sidecar_digest.update(memoryview(records).cast("B"))
        with open(sidecar_path, "xb") as f:
            records.tofile(f)
            f.flush()
            os.fsync(f.fileno())
        entry = {
            "path": self.name,
            "tokens": int(values.size),
            "bytes": int(values.nbytes),
            "sha256": digest.hexdigest(),
            "unit_sidecar": {
                "path": sidecar_name,
                "records": int(records.size),
                "bytes": int(records.nbytes),
                "sha256": sidecar_digest.hexdigest(),
                "record_format": "uint32_length_uint8_kind_3_zero_bytes",
            },
        }
        self.buf = []
        self.records = []
        return entry


def verify_derived_shard(
    out_dir,
    split,
    entry,
    special_ids,
    tokenizer_sha256,
    split_evidence=None,
):
    """Independently reread one destination shard and its unit sidecar."""
    path = os.path.join(out_dir, entry["path"])
    sidecar = entry["unit_sidecar"]
    sidecar_path = os.path.join(out_dir, sidecar["path"])
    if stable_file_sha256(path) != entry["sha256"]:
        raise ValueError(f"derived shard hash differs: {entry['path']}")
    if stable_file_sha256(sidecar_path) != sidecar["sha256"]:
        raise ValueError(
            f"derived unit-sidecar hash differs: {sidecar['path']}"
        )
    values = np.memmap(path, dtype="<u2", mode="r")
    records = np.memmap(
        sidecar_path,
        dtype=UNIT_RECORD_DTYPE,
        mode="r",
    )
    if values.size != entry["tokens"] or values.nbytes != entry["bytes"]:
        raise ValueError(f"derived shard size differs: {entry['path']}")
    if (
        records.size != sidecar["records"]
        or records.nbytes != sidecar["bytes"]
        or np.any(records["reserved"] != 0)
    ):
        raise ValueError(
            f"derived unit-sidecar structure differs: {sidecar['path']}"
        )
    if int(records["length"].sum(dtype=np.uint64)) != int(values.size):
        raise ValueError(
            f"derived unit-sidecar lengths differ: {sidecar['path']}"
        )
    if np.any(records["kind"] > max(KIND_CODES.values())):
        raise ValueError(
            f"derived unit-sidecar kind differs: {sidecar['path']}"
        )
    for key in ("prefix", "middle", "suffix"):
        if np.any(values == special_ids[key]):
            raise ValueError(
                f"derived shard retains FIM sentinel: {entry['path']}"
            )

    domain = (
        f"shard:{split}:{entry['path']}:tokenizer:{tokenizer_sha256}"
    )
    evidence = UnitEvidence(special_ids["eos"], domain)
    split_evidence = split_evidence or UnitEvidence(
        special_ids["eos"],
        f"split:{split}:tokenizer:{tokenizer_sha256}",
    )
    kinds = {value: key for key, value in KIND_CODES.items()}
    offset = 0
    for record in records:
        length = int(record["length"])
        end = offset + length
        if length < 2 or end > values.size:
            raise ValueError(
                f"derived unit-sidecar boundary differs: {sidecar['path']}"
            )
        unit = np.asarray(values[offset:end])
        if int(unit[-1]) != special_ids["eos"]:
            raise ValueError(
                f"derived unit lacks writer EOS: {sidecar['path']}"
            )
        kind = kinds[int(record["kind"])]
        raw = unit[:-1]
        evidence.add(None, kind, raw)
        split_evidence.add(None, kind, raw)
        offset = end
    if offset != values.size:
        raise ValueError(
            f"derived unit-sidecar does not cover shard: {sidecar['path']}"
        )
    return evidence.summary()


def validate_derivation_contract(config, receipt):
    contract = config.get("data_build_contract")
    expected = {
        "schema_version": 2,
        "strategy": ALGORITHM,
        "source_index": receipt["source_index"],
        "source_integrity_receipt": config.get("data_integrity", {}).get(
            "receipt"
        ),
        "source_integrity_receipt_sha256": config.get(
            "data_integrity", {}
        ).get("sha256"),
        "require_fresh_output_dir": True,
    }
    if contract != expected:
        raise ValueError(
            "no-FIM derivation request differs from config contract:\n"
            f"expected {contract!r}\n"
            f"actual {expected!r}"
        )
    if config.get("fim_rate") != 0.0:
        raise ValueError("no-FIM derivation config fim_rate must be 0.0")
    if "sources" in config:
        raise ValueError(
            "no-FIM derivation config must not contain executable sources"
        )
    return contract


def build_no_fim(config_path):
    config = load_json(config_path)
    integrity = config.get("data_integrity", {})
    if integrity.get("role") != "source":
        raise ValueError("run 2 data_integrity role must be source")
    receipt_path = integrity.get("receipt")
    receipt = load_json(receipt_path)
    if file_sha256(receipt_path) != integrity.get("sha256"):
        raise ValueError("run 1 source integrity receipt hash differs")
    if receipt.get("status") != "complete" or receipt.get(
        "algorithm"
    ) != ALGORITHM:
        raise ValueError("run 1 source integrity receipt is incomplete")
    contract = validate_derivation_contract(config, receipt)

    tokenizer_path = config["tokenizer_path"]
    special_ids, vocab_size = special_token_ids(tokenizer_path)
    if receipt.get("special_token_ids") != special_ids:
        raise ValueError("source receipt special-token ids differ")
    if receipt.get("tokenizer", {}).get("sha256") != file_sha256(
        tokenizer_path
    ):
        raise ValueError("source receipt tokenizer differs")

    out_dir = require_fresh_output_dir(config["data_dir"])
    os.makedirs(out_dir, exist_ok=True)
    source_index_path = receipt["source_index"]["path"]
    if file_sha256(source_index_path) != receipt["source_index"]["sha256"]:
        raise ValueError("run 1 source index hash differs")
    source_index = load_json(source_index_path)
    source_root = os.path.dirname(os.path.abspath(source_index_path))
    tokenizer_sha256 = file_sha256(tokenizer_path)
    output_splits = {}
    split_evidence_out = {}
    for split in ("train", "val"):
        split_evidence = UnitEvidence(
            special_ids["eos"],
            f"split:{split}:tokenizer:{tokenizer_sha256}",
        )
        verified_split_evidence = UnitEvidence(
            special_ids["eos"],
            f"split:{split}:tokenizer:{tokenizer_sha256}",
        )
        expected_split = receipt["splits"][split]
        source_entries = source_index["splits"][split]
        if [entry["path"] for entry in source_entries] != [
            entry["path"] for entry in expected_split["shards"]
        ]:
            raise ValueError(f"run 1 split {split} shard order differs")
        output_entries = []
        for source_entry, expected_shard in zip(
            source_entries,
            expected_split["shards"],
        ):
            path = os.path.join(source_root, source_entry["path"])
            writer = DerivedShardWriter(
                out_dir,
                split,
                source_entry["path"],
            )
            actual_shard = {
                "path": source_entry["path"],
                **scan_shard(
                    path,
                    special_ids,
                    expected_tokens=source_entry["tokens"],
                    emit=writer.add_raw,
                    split_evidence=split_evidence,
                    evidence_domain=(
                        f"shard:{split}:{source_entry['path']}:"
                        f"tokenizer:{tokenizer_sha256}"
                    ),
                    vocab_size=vocab_size,
                ),
            }
            if not _same_summary(actual_shard, expected_shard):
                raise ValueError(
                    f"run 1 source shard changed: {source_entry['path']}"
                )
            output_entry = writer.flush()
            verified = verify_derived_shard(
                out_dir,
                split,
                output_entry,
                special_ids,
                tokenizer_sha256,
                split_evidence=verified_split_evidence,
            )
            if _normalization_evidence(verified) != (
                _normalization_evidence(actual_shard)
            ):
                raise ValueError(
                    f"derived shard evidence differs: "
                    f"{source_entry['path']}"
                )
            output_entry["source"] = {
                "path": source_entry["path"],
                "tokens": actual_shard["tokens"],
                "sha256": actual_shard["sha256"],
                "source_wire_units_sha256": actual_shard[
                    "source_wire_units_sha256"
                ],
                "ordered_raw_units_sha256": actual_shard[
                    "ordered_raw_units_sha256"
                ],
            }
            output_entry["normalization"] = _normalization_evidence(
                verified
            )
            output_entries.append(output_entry)
        actual_split = split_evidence.summary()
        verified_split = verified_split_evidence.summary()
        expected_summary = {
            key: value
            for key, value in expected_split.items()
            if key not in ("shards", "shard_manifest_sha256")
        }
        if not _same_summary(actual_split, expected_summary):
            raise ValueError(f"run 1 split {split} evidence changed")
        if _normalization_evidence(verified_split) != (
            _normalization_evidence(actual_split)
        ):
            raise ValueError(
                f"independent derived split {split} evidence differs"
            )
        output_total = sum(entry["tokens"] for entry in output_entries)
        if output_total != actual_split["derived_tokens"]:
            raise ValueError(f"derived split {split} token total differs")
        for entry in output_entries:
            entry["total_tokens"] = output_total
        output_splits[split] = output_entries
        split_evidence_out[split] = actual_split

    index = {
        "schema_version": INDEX_SCHEMA_VERSION,
        "vocab_size": vocab_size,
        "fim_rate": 0.0,
        "fim_chunk": config["fim_chunk"],
        "splits": output_splits,
        "build": {
            "completed": True,
            "strategy": ALGORITHM,
            "fresh_output_directory": True,
            "config_path": config_path,
            "config_canonical_sha256": canonical_json_sha256(config),
            "tokenizer_path": tokenizer_path,
            "tokenizer_sha256": file_sha256(tokenizer_path),
            "source_index": receipt["source_index"],
            "source_integrity_receipt": {
                "path": receipt_path,
                "sha256": integrity["sha256"],
            },
            "special_token_ids": special_ids,
            "derivation_script": {
                "path": os.path.relpath(
                    os.path.abspath(__file__),
                    os.getcwd(),
                ),
                "sha256": file_sha256(__file__),
            },
            "split_evidence": split_evidence_out,
            "contract": contract,
        },
    }
    if (
        file_sha256(receipt_path) != integrity["sha256"]
        or file_sha256(source_index_path)
        != receipt["source_index"]["sha256"]
        or file_sha256(tokenizer_path) != tokenizer_sha256
    ):
        raise RuntimeError("no-FIM derivation input changed before publication")
    index_path = os.path.join(out_dir, "index.json")
    write_json_atomic(index_path, index)
    return index_path, index


def main():
    parser = argparse.ArgumentParser()
    subparsers = parser.add_subparsers(dest="command", required=True)
    attest = subparsers.add_parser("attest")
    attest.add_argument("--index", required=True)
    attest.add_argument("--tokenizer", required=True)
    attest.add_argument("--out", required=True)
    build = subparsers.add_parser("build")
    build.add_argument("--config", required=True)
    cli = parser.parse_args()

    if cli.command == "attest":
        if os.path.lexists(cli.out):
            raise FileExistsError(
                f"refusing to replace integrity receipt: {cli.out}"
            )
        receipt = build_attestation(cli.index, cli.tokenizer)
        write_json_atomic(cli.out, receipt)
        print(
            f"wrote {cli.out}: "
            f"{receipt['splits']['train']['source_tokens']:,} train source "
            "tokens"
        )
        return

    index_path, index = build_no_fim(cli.config)
    print(
        f"wrote {index_path}: "
        f"{index['splits']['train'][0]['total_tokens']:,} train / "
        f"{index['splits']['val'][0]['total_tokens']:,} val tokens"
    )


if __name__ == "__main__":
    main()