File size: 32,960 Bytes
919fd68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Shared mmap-backed packed training row boundary.

This module is import-safe for both the learn loop and the corpus-training
readers: it owns no state and imports nothing from either, so neither side can
create an import cycle by returning these rows across the I/O boundary.
"""

from __future__ import annotations

import hashlib
import struct
from dataclasses import dataclass
from pathlib import Path
from typing import Any, cast

import torch


_TARGET_FREE_CALIBRATION_INPUT_IDS_DOMAIN = (
    b"nnf.resynthesis.target_free_calibration_input_ids.v1\x00"
)
_TARGET_FREE_CALIBRATION_INPUT_MASK_DOMAIN = (
    b"nnf.resynthesis.target_free_calibration_input_mask.v1\x00"
)
_TARGET_FREE_CALIBRATION_PACKET_DOMAIN = (
    b"nnf.resynthesis.target_free_calibration_prompt_packet.v1\x00"
)


def _domain_separated_tensor_sha256_t_boundary(
    value_t: torch.Tensor,
    *,
    domain: bytes,
) -> torch.Tensor:
    """Hash one exact tensor at an explicit authority boundary.

    Calibration, cold proof, and acceptance must compare one identity domain.
    Including dtype and geometry prevents equal raw bytes under a different
    tensor interpretation from acquiring the same authority.
    """

    if (
        not isinstance(value_t, torch.Tensor)
        or value_t.device.type != "cpu"
        or not value_t.is_contiguous()
        or not isinstance(domain, bytes)
        or not domain.endswith(b"\x00")
    ):
        raise ValueError("target-free calibration tensor authority differs")
    digest = hashlib.sha256()
    digest.update(domain)
    dtype = str(value_t.dtype).encode("ascii")
    digest.update(struct.pack("<Q", len(dtype)))
    digest.update(dtype)
    digest.update(struct.pack("<Q", value_t.ndim))
    for extent in value_t.shape:
        digest.update(struct.pack("<Q", extent))
    digest.update(value_t.reshape(-1).view(torch.uint8).numpy().tobytes())
    return torch.frombuffer(
        bytearray(digest.digest()),
        dtype=torch.uint8,
    ).clone()


def target_free_calibration_input_ids_sha256_t_boundary(
    input_ids_t: torch.Tensor,
) -> torch.Tensor:
    """Return the shared exact identity for calibration prompt token IDs."""

    return _domain_separated_tensor_sha256_t_boundary(
        input_ids_t,
        domain=_TARGET_FREE_CALIBRATION_INPUT_IDS_DOMAIN,
    )


def target_free_calibration_input_mask_sha256_t_boundary(
    input_mask_t: torch.Tensor,
) -> torch.Tensor:
    """Return the shared exact identity for a calibration prompt mask."""

    return _domain_separated_tensor_sha256_t_boundary(
        input_mask_t,
        domain=_TARGET_FREE_CALIBRATION_INPUT_MASK_DOMAIN,
    )


@dataclass(frozen=True, slots=True)
class AllKnowledgeTargetFreeCalibrationPromptPacket:
    """Tensor-only authority for one immutable, independently selected prompt.

    The prompt comes from a packed remaining-corpus snapshot, never historical
    branch receipts, stores, scopes, prompts, or tensors. Filesystem paths and
    JSON records remain outside this packet at explicit external-I/O
    boundaries; every content identity needed by the model path is a tensor.
    """

    input_ids_t: torch.Tensor
    input_mask_t: torch.Tensor
    global_cursor_t: torch.Tensor
    prompt_sha256_t: torch.Tensor
    window_sha256_t: torch.Tensor
    row_authority_sha256_t: torch.Tensor
    snapshot_file_sha256_t: torch.Tensor
    collection_authority_sha256_t: torch.Tensor
    federation_authority_sha256_t: torch.Tensor
    exclusion_collection_file_sha256_t: torch.Tensor
    excluded_prior_work_ids_sha256_t: torch.Tensor
    input_ids_sha256_t: torch.Tensor
    input_mask_sha256_t: torch.Tensor
    physical_source_ranges_sha256_t: torch.Tensor
    authority_sha256_t: torch.Tensor
    target_entered_forward_t: torch.Tensor

    def __post_init__(self) -> None:
        tensors = (
            self.input_ids_t,
            self.input_mask_t,
            self.global_cursor_t,
            self.prompt_sha256_t,
            self.window_sha256_t,
            self.row_authority_sha256_t,
            self.snapshot_file_sha256_t,
            self.collection_authority_sha256_t,
            self.federation_authority_sha256_t,
            self.exclusion_collection_file_sha256_t,
            self.excluded_prior_work_ids_sha256_t,
            self.input_ids_sha256_t,
            self.input_mask_sha256_t,
            self.physical_source_ranges_sha256_t,
            self.authority_sha256_t,
            self.target_entered_forward_t,
        )
        digest_tensors = (
            self.prompt_sha256_t,
            self.window_sha256_t,
            self.snapshot_file_sha256_t,
            self.collection_authority_sha256_t,
            self.federation_authority_sha256_t,
            self.exclusion_collection_file_sha256_t,
            self.excluded_prior_work_ids_sha256_t,
            self.input_ids_sha256_t,
            self.input_mask_sha256_t,
            self.physical_source_ranges_sha256_t,
            self.authority_sha256_t,
        )
        if (
            any(
                not isinstance(value_t, torch.Tensor)
                or value_t.device.type != "cpu"
                or not value_t.is_contiguous()
                for value_t in tensors
            )
            or self.input_ids_t.dtype != torch.long
            or self.input_ids_t.ndim != 2
            or self.input_ids_t.shape[0] != 1
            or self.input_ids_t.numel() < 1
            or bool(self.input_ids_t.lt(0).any())
            or self.input_mask_t.dtype != torch.bool
            or self.input_mask_t.shape != self.input_ids_t.shape
            or not bool(self.input_mask_t.all())
            or self.global_cursor_t.dtype != torch.int64
            or self.global_cursor_t.shape != (1,)
            or bool(self.global_cursor_t.lt(0).any())
            or any(
                value_t.dtype != torch.uint8
                or value_t.shape != (32,)
                for value_t in digest_tensors
            )
            or self.row_authority_sha256_t.dtype != torch.uint8
            or self.row_authority_sha256_t.shape != (2, 32)
            or self.target_entered_forward_t.dtype != torch.bool
            or self.target_entered_forward_t.shape != ()
            or bool(self.target_entered_forward_t)
            or not torch.equal(
                self.input_ids_sha256_t,
                target_free_calibration_input_ids_sha256_t_boundary(
                    self.input_ids_t
                ),
            )
            or not torch.equal(
                self.input_mask_sha256_t,
                target_free_calibration_input_mask_sha256_t_boundary(
                    self.input_mask_t
                ),
            )
            or not torch.equal(
                self.authority_sha256_t,
                target_free_calibration_prompt_authority_sha256_t_boundary(
                    self
                ),
            )
        ):
            raise ValueError(
                "all-knowledge target-free calibration prompt differs"
            )


def target_free_calibration_prompt_authority_sha256_t_boundary(
    packet: AllKnowledgeTargetFreeCalibrationPromptPacket,
) -> torch.Tensor:
    """Recompute one prompt packet's content authority without host metadata."""

    if not isinstance(
        packet,
        AllKnowledgeTargetFreeCalibrationPromptPacket,
    ):
        raise TypeError("target-free calibration prompt packet is malformed")
    return _target_free_calibration_prompt_fields_authority_sha256_t_boundary(
        (
            packet.input_ids_t,
            packet.input_mask_t,
            packet.global_cursor_t,
            packet.prompt_sha256_t,
            packet.window_sha256_t,
            packet.row_authority_sha256_t,
            packet.snapshot_file_sha256_t,
            packet.collection_authority_sha256_t,
            packet.federation_authority_sha256_t,
            packet.exclusion_collection_file_sha256_t,
            packet.excluded_prior_work_ids_sha256_t,
            packet.input_ids_sha256_t,
            packet.input_mask_sha256_t,
            packet.physical_source_ranges_sha256_t,
            packet.target_entered_forward_t,
        )
    )


def validated_target_free_calibration_prompt_packet_boundary(
    packet: AllKnowledgeTargetFreeCalibrationPromptPacket,
) -> AllKnowledgeTargetFreeCalibrationPromptPacket:
    """Return an isolated, fully revalidated copy of one prompt packet."""

    if not isinstance(
        packet,
        AllKnowledgeTargetFreeCalibrationPromptPacket,
    ):
        raise TypeError("target-free calibration prompt packet is malformed")
    return AllKnowledgeTargetFreeCalibrationPromptPacket(
        input_ids_t=packet.input_ids_t.detach().cpu().clone().contiguous(),
        input_mask_t=packet.input_mask_t.detach().cpu().clone().contiguous(),
        global_cursor_t=(
            packet.global_cursor_t.detach().cpu().clone().contiguous()
        ),
        prompt_sha256_t=(
            packet.prompt_sha256_t.detach().cpu().clone().contiguous()
        ),
        window_sha256_t=(
            packet.window_sha256_t.detach().cpu().clone().contiguous()
        ),
        row_authority_sha256_t=(
            packet.row_authority_sha256_t.detach()
            .cpu()
            .clone()
            .contiguous()
        ),
        snapshot_file_sha256_t=(
            packet.snapshot_file_sha256_t.detach()
            .cpu()
            .clone()
            .contiguous()
        ),
        collection_authority_sha256_t=(
            packet.collection_authority_sha256_t.detach()
            .cpu()
            .clone()
            .contiguous()
        ),
        federation_authority_sha256_t=(
            packet.federation_authority_sha256_t.detach()
            .cpu()
            .clone()
            .contiguous()
        ),
        exclusion_collection_file_sha256_t=(
            packet.exclusion_collection_file_sha256_t.detach()
            .cpu()
            .clone()
            .contiguous()
        ),
        excluded_prior_work_ids_sha256_t=(
            packet.excluded_prior_work_ids_sha256_t.detach()
            .cpu()
            .clone()
            .contiguous()
        ),
        input_ids_sha256_t=(
            packet.input_ids_sha256_t.detach().cpu().clone().contiguous()
        ),
        input_mask_sha256_t=(
            packet.input_mask_sha256_t.detach().cpu().clone().contiguous()
        ),
        physical_source_ranges_sha256_t=(
            packet.physical_source_ranges_sha256_t.detach()
            .cpu()
            .clone()
            .contiguous()
        ),
        authority_sha256_t=(
            packet.authority_sha256_t.detach().cpu().clone().contiguous()
        ),
        target_entered_forward_t=(
            packet.target_entered_forward_t.detach()
            .cpu()
            .clone()
            .contiguous()
        ),
    )


def _target_free_calibration_prompt_fields_authority_sha256_t_boundary(
    fields_t: tuple[torch.Tensor, ...],
) -> torch.Tensor:
    """Hash the exact ordered tensor fields that form prompt authority."""

    digest = hashlib.sha256()
    digest.update(_TARGET_FREE_CALIBRATION_PACKET_DOMAIN)
    for value_t in fields_t:
        tensor_digest_t = _domain_separated_tensor_sha256_t_boundary(
            value_t,
            domain=_TARGET_FREE_CALIBRATION_PACKET_DOMAIN,
        )
        digest.update(tensor_digest_t.numpy().tobytes())
    return torch.frombuffer(
        bytearray(digest.digest()),
        dtype=torch.uint8,
    ).clone()


def build_all_knowledge_target_free_calibration_prompt_packet_boundary(
    *,
    input_ids_t: torch.Tensor,
    input_mask_t: torch.Tensor,
    global_cursor_t: torch.Tensor,
    prompt_sha256_t: torch.Tensor,
    window_sha256_t: torch.Tensor,
    row_authority_sha256_t: torch.Tensor,
    snapshot_file_sha256_t: torch.Tensor,
    collection_authority_sha256_t: torch.Tensor,
    federation_authority_sha256_t: torch.Tensor,
    exclusion_collection_file_sha256_t: torch.Tensor,
    excluded_prior_work_ids_sha256_t: torch.Tensor,
    physical_source_ranges_sha256_t: torch.Tensor,
    target_entered_forward_t: torch.Tensor,
) -> AllKnowledgeTargetFreeCalibrationPromptPacket:
    """Seal one already validated prompt selection into its tensor packet."""

    input_ids_t = input_ids_t.detach().cpu().clone().contiguous()
    input_mask_t = input_mask_t.detach().cpu().clone().contiguous()
    global_cursor_t = global_cursor_t.detach().cpu().clone().contiguous()
    prompt_sha256_t = prompt_sha256_t.detach().cpu().clone().contiguous()
    window_sha256_t = window_sha256_t.detach().cpu().clone().contiguous()
    row_authority_sha256_t = (
        row_authority_sha256_t.detach().cpu().clone().contiguous()
    )
    snapshot_file_sha256_t = (
        snapshot_file_sha256_t.detach().cpu().clone().contiguous()
    )
    collection_authority_sha256_t = (
        collection_authority_sha256_t.detach().cpu().clone().contiguous()
    )
    federation_authority_sha256_t = (
        federation_authority_sha256_t.detach().cpu().clone().contiguous()
    )
    exclusion_collection_file_sha256_t = (
        exclusion_collection_file_sha256_t.detach()
        .cpu()
        .clone()
        .contiguous()
    )
    excluded_prior_work_ids_sha256_t = (
        excluded_prior_work_ids_sha256_t.detach()
        .cpu()
        .clone()
        .contiguous()
    )
    physical_source_ranges_sha256_t = (
        physical_source_ranges_sha256_t.detach()
        .cpu()
        .clone()
        .contiguous()
    )
    target_entered_forward_t = (
        target_entered_forward_t.detach().cpu().clone().contiguous()
    )
    input_ids_sha256_t = (
        target_free_calibration_input_ids_sha256_t_boundary(input_ids_t)
    )
    input_mask_sha256_t = (
        target_free_calibration_input_mask_sha256_t_boundary(input_mask_t)
    )
    fields_t = (
        input_ids_t,
        input_mask_t,
        global_cursor_t,
        prompt_sha256_t,
        window_sha256_t,
        row_authority_sha256_t,
        snapshot_file_sha256_t,
        collection_authority_sha256_t,
        federation_authority_sha256_t,
        exclusion_collection_file_sha256_t,
        excluded_prior_work_ids_sha256_t,
        input_ids_sha256_t,
        input_mask_sha256_t,
        physical_source_ranges_sha256_t,
        target_entered_forward_t,
    )
    return AllKnowledgeTargetFreeCalibrationPromptPacket(
        input_ids_t=input_ids_t,
        input_mask_t=input_mask_t,
        global_cursor_t=global_cursor_t,
        prompt_sha256_t=prompt_sha256_t,
        window_sha256_t=window_sha256_t,
        row_authority_sha256_t=row_authority_sha256_t,
        snapshot_file_sha256_t=snapshot_file_sha256_t,
        collection_authority_sha256_t=collection_authority_sha256_t,
        federation_authority_sha256_t=federation_authority_sha256_t,
        exclusion_collection_file_sha256_t=(
            exclusion_collection_file_sha256_t
        ),
        excluded_prior_work_ids_sha256_t=(
            excluded_prior_work_ids_sha256_t
        ),
        input_ids_sha256_t=input_ids_sha256_t,
        input_mask_sha256_t=input_mask_sha256_t,
        physical_source_ranges_sha256_t=(
            physical_source_ranges_sha256_t
        ),
        authority_sha256_t=(
            _target_free_calibration_prompt_fields_authority_sha256_t_boundary(
                fields_t
            )
        ),
        target_entered_forward_t=target_entered_forward_t,
    )


@dataclass(frozen=True, slots=True)
class PackedTokenPhysicalSourceRange:
    """Cold provenance for bytes copied from one immutable packed object.

    ``object_compressed_*`` identifies the physical zstd frame range that had
    to be read. ``source_uncompressed_*`` identifies the exact bytes selected
    from that frame's logical token stream, and ``packet_*`` binds those bytes
    to their position in the coalesced ``PackedTokenBatchPacket`` arena. This
    is external-I/O evidence only; it never enters model forward or routing.
    """

    component_sha256: str
    payload_work_id: str
    object_path: str
    object_sha256: str
    object_byte_count: int
    object_compressed_byte_start: int
    object_compressed_byte_end: int
    source_uncompressed_byte_start: int
    source_uncompressed_byte_end: int
    packet_byte_start: int
    packet_byte_end: int
    global_cursor: int

    def __post_init__(self) -> None:
        for digest, label in (
            (self.component_sha256, "component"),
            (self.payload_work_id, "payload WorkID"),
            (self.object_sha256, "object"),
        ):
            try:
                decoded = bytes.fromhex(digest)
            except ValueError as error:
                raise ValueError(
                    f"packed token physical {label} digest differs"
                ) from error
            if len(decoded) != 32:
                raise ValueError(
                    f"packed token physical {label} digest differs"
                )
        if not self.object_path or not Path(self.object_path).is_absolute():
            raise ValueError("packed token physical object path differs")
        integer_values = (
            self.object_byte_count,
            self.object_compressed_byte_start,
            self.object_compressed_byte_end,
            self.source_uncompressed_byte_start,
            self.source_uncompressed_byte_end,
            self.packet_byte_start,
            self.packet_byte_end,
            self.global_cursor,
        )
        if any(
            not isinstance(value, int)
            or isinstance(value, bool)
            or value < 0
            for value in integer_values
        ):
            raise ValueError("packed token physical byte geometry differs")
        if (
            self.object_byte_count < 1
            or self.object_compressed_byte_end
            <= self.object_compressed_byte_start
            or self.object_compressed_byte_end > self.object_byte_count
            or self.source_uncompressed_byte_end
            <= self.source_uncompressed_byte_start
            or self.packet_byte_end <= self.packet_byte_start
            or (
                self.source_uncompressed_byte_end
                - self.source_uncompressed_byte_start
            )
            != self.packet_byte_end - self.packet_byte_start
            or self.source_uncompressed_byte_start % 4
            or self.source_uncompressed_byte_end % 4
            or self.packet_byte_start % 4
            or self.packet_byte_end % 4
        ):
            raise ValueError("packed token physical byte ranges differ")


@dataclass(frozen=True, slots=True)
class PackedTokenShardGeometryPacket:
    """Tensor-native single-window geometry cached once per sealed shard."""

    token_offsets_t: torch.Tensor
    prompt_lengths_t: torch.Tensor
    prompt_sha256_t: torch.Tensor
    window_sha256_t: torch.Tensor

    def __post_init__(self) -> None:
        row_count = self.prompt_lengths_t.numel()
        if (
            self.token_offsets_t.device.type != "cpu"
            or self.token_offsets_t.dtype != torch.int64
            or self.token_offsets_t.ndim != 1
            or self.token_offsets_t.numel() != row_count + 1
            or not self.token_offsets_t.is_contiguous()
            or not torch.equal(
                self.token_offsets_t[:1],
                torch.zeros((1,), dtype=torch.int64),
            )
            or not bool(
                torch.all(
                    self.token_offsets_t[1:]
                    > self.token_offsets_t[:-1]
                )
            )
            or self.prompt_lengths_t.device.type != "cpu"
            or self.prompt_lengths_t.dtype != torch.int32
            or self.prompt_lengths_t.ndim != 1
            or not self.prompt_lengths_t.is_contiguous()
            or not bool(torch.all(self.prompt_lengths_t > 0))
            or not bool(
                torch.all(
                    self.prompt_lengths_t.to(dtype=torch.int64)
                    < (
                        self.token_offsets_t[1:]
                        - self.token_offsets_t[:-1]
                    )
                )
            )
        ):
            raise ValueError(
                "packed token shard geometry differs"
            )
        for digest_t, label in (
            (self.prompt_sha256_t, "prompt"),
            (self.window_sha256_t, "window"),
        ):
            if (
                digest_t.device.type != "cpu"
                or digest_t.dtype != torch.uint8
                or digest_t.shape != (row_count, 32)
                or not digest_t.is_contiguous()
            ):
                raise ValueError(
                    f"packed token shard {label} authority differs"
                )

    def __len__(self) -> int:
        return self.prompt_lengths_t.numel()


@dataclass(frozen=True, slots=True)
class PackedTokenBatchPacket:
    """Tensor-native packed rows at the immutable corpus I/O boundary.

    ``token_ids_t`` stores each row as its prompt immediately followed by its
    answer. ``row_offsets_t`` and ``prompt_lengths_t`` recover those two spans
    without per-row Python objects. Authority rows store the component SHA-256
    followed by the payload WorkID SHA-256; ``row_authority_index_t`` binds
    each token row to one such pair.
    """

    token_ids_t: torch.Tensor
    row_offsets_t: torch.Tensor
    prompt_lengths_t: torch.Tensor
    prompt_sha256_t: torch.Tensor
    window_sha256_t: torch.Tensor
    authority_sha256_t: torch.Tensor
    row_authority_index_t: torch.Tensor
    global_cursor_start_t: torch.Tensor
    task_intent_targets_t: torch.Tensor | None = None

    def __post_init__(self) -> None:
        tensors = (
            self.token_ids_t,
            self.row_offsets_t,
            self.prompt_lengths_t,
            self.prompt_sha256_t,
            self.window_sha256_t,
            self.authority_sha256_t,
            self.row_authority_index_t,
            self.global_cursor_start_t,
        )
        if any(
            value.device.type != "cpu" or not value.is_contiguous()
            for value in tensors
        ):
            raise ValueError(
                "packed token batch packet tensors must be contiguous CPU tensors"
            )
        if self.token_ids_t.dtype != torch.int32 or self.token_ids_t.ndim != 1:
            raise ValueError("packed token batch token arena differs")
        if (
            self.row_offsets_t.dtype != torch.int64
            or self.row_offsets_t.ndim != 1
            or self.row_offsets_t.numel() < 2
        ):
            raise ValueError("packed token batch row offsets differ")
        if not torch.equal(
            self.row_offsets_t[:1],
            torch.zeros((1,), dtype=torch.int64),
        ) or not torch.equal(
            self.row_offsets_t[-1:],
            torch.tensor(
                [self.token_ids_t.numel()],
                dtype=torch.int64,
            ),
        ):
            raise ValueError("packed token batch token frontier differs")
        row_widths_t = self.row_offsets_t[1:] - self.row_offsets_t[:-1]
        if not bool(torch.all(row_widths_t > 0)):
            raise ValueError("packed token batch row widths differ")
        row_count = self.row_offsets_t.numel() - 1
        if (
            self.prompt_lengths_t.dtype != torch.int32
            or self.prompt_lengths_t.shape != (row_count,)
            or not bool(torch.all(self.prompt_lengths_t > 0))
            or not bool(
                torch.all(
                    self.prompt_lengths_t.to(dtype=torch.int64)
                    < row_widths_t
                )
            )
        ):
            raise ValueError("packed token batch prompt geometry differs")
        for digest_t, label in (
            (self.prompt_sha256_t, "prompt"),
            (self.window_sha256_t, "window"),
        ):
            if digest_t.dtype != torch.uint8 or digest_t.shape != (row_count, 32):
                raise ValueError(f"packed token batch {label} authority differs")
        if (
            self.authority_sha256_t.dtype != torch.uint8
            or self.authority_sha256_t.ndim != 3
            or self.authority_sha256_t.shape[0] < 1
            or self.authority_sha256_t.shape[1:] != (2, 32)
        ):
            raise ValueError("packed token batch authority table differs")
        if (
            self.row_authority_index_t.dtype != torch.int32
            or self.row_authority_index_t.shape != (row_count,)
            or not bool(torch.all(self.row_authority_index_t >= 0))
            or not bool(
                torch.all(
                    self.row_authority_index_t
                    < self.authority_sha256_t.shape[0]
                )
            )
        ):
            raise ValueError("packed token batch row authority indices differ")
        if (
            self.global_cursor_start_t.dtype != torch.int64
            or self.global_cursor_start_t.shape != (1,)
            or not bool(torch.all(self.global_cursor_start_t >= 0))
        ):
            raise ValueError("packed token batch global cursor differs")
        if self.task_intent_targets_t is not None and (
            self.task_intent_targets_t.device.type != "cpu"
            or self.task_intent_targets_t.dtype != torch.float32
            or self.task_intent_targets_t.ndim != 2
            or self.task_intent_targets_t.shape[0] != row_count
            or self.task_intent_targets_t.shape[1] < 1
            or not self.task_intent_targets_t.is_contiguous()
            or not bool(torch.isfinite(self.task_intent_targets_t).all())
        ):
            raise ValueError(
                "packed token batch task-intent arena differs"
            )

    def __len__(self) -> int:
        return self.row_offsets_t.numel() - 1

    def prompt_ids_boundary(self, row_index: int) -> torch.Tensor:
        """Return one prompt view at an explicit Python consumer boundary."""

        if (
            isinstance(row_index, bool)
            or row_index < 0
            or row_index >= len(self)
        ):
            raise IndexError("packed token batch row is unavailable")
        row_start = int(self.row_offsets_t[row_index])
        prompt_end = row_start + int(self.prompt_lengths_t[row_index])
        return self.token_ids_t[row_start:prompt_end]

    def answer_ids_boundary(self, row_index: int) -> torch.Tensor:
        """Return one answer view at an explicit Python consumer boundary."""

        if (
            isinstance(row_index, bool)
            or row_index < 0
            or row_index >= len(self)
        ):
            raise IndexError("packed token batch row is unavailable")
        row_start = int(self.row_offsets_t[row_index])
        answer_start = row_start + int(self.prompt_lengths_t[row_index])
        row_end = int(self.row_offsets_t[row_index + 1])
        return self.token_ids_t[answer_start:row_end]

    def task_intent_targets_boundary(
        self,
        row_index: int,
    ) -> torch.Tensor | None:
        """Return one loss-side intent view at an explicit consumer boundary.

        Intent labels remain separate from ``token_ids_t`` and therefore
        cannot enter the prompt forward.  Keeping their rows in this packet
        still gives the loss boundary the same cursor, authority, and reorder
        semantics as the token arena.
        """

        if (
            isinstance(row_index, bool)
            or row_index < 0
            or row_index >= len(self)
        ):
            raise IndexError("packed token batch row is unavailable")
        if self.task_intent_targets_t is None:
            return None
        return self.task_intent_targets_t[row_index]


class PackedTokenTrainingRow(dict[str, Any]):
    """One metadata row backed by mmap token tensors at the I/O boundary."""

    batch_packet: PackedTokenBatchPacket | None
    batch_packet_row_index: int | None

    def __init__(
        self,
        metadata: dict[str, Any],
        *,
        prompt_ids: torch.Tensor,
        answer_ids: torch.Tensor,
        corrected_prompt_ids: torch.Tensor,
        task_intent_targets: torch.Tensor | None,
        source_row_sha256: str,
        batch_packet: PackedTokenBatchPacket | None = None,
        batch_packet_row_index: int | None = None,
    ) -> None:
        super().__init__(metadata)
        if (batch_packet is None) != (batch_packet_row_index is None):
            raise ValueError("packed token row batch binding is incomplete")
        if batch_packet is not None and (
            not isinstance(batch_packet_row_index, int)
            or isinstance(batch_packet_row_index, bool)
            or batch_packet_row_index < 0
            or batch_packet_row_index >= len(batch_packet)
        ):
            raise ValueError("packed token row batch index differs")
        self["prompt_ids"] = prompt_ids
        self["answer_ids"] = answer_ids
        if task_intent_targets is not None:
            self["task_intent_targets"] = task_intent_targets
        self.corrected_prompt_ids_t = corrected_prompt_ids
        self.source_row_sha256 = source_row_sha256
        self.batch_packet = batch_packet
        self.batch_packet_row_index = batch_packet_row_index

    @staticmethod
    def _integer_list(value: torch.Tensor) -> list[int]:
        return [
            int(token)
            for token in value.to(device="cpu", dtype=torch.long).tolist()
        ]

    def legacy_row_boundary(self) -> dict[str, Any]:
        """Materialize Python sequences only for legacy single-row paths."""

        prompt_ids = self._integer_list(cast(torch.Tensor, self["prompt_ids"]))
        answer_ids = self._integer_list(cast(torch.Tensor, self["answer_ids"]))
        corrected_prompt_ids = self._integer_list(self.corrected_prompt_ids_t)
        row = {
            key: value
            for key, value in self.items()
            if key not in {"prompt_ids", "answer_ids", "task_intent_targets"}
        }
        row.update(
            {
                "prompt_ids": prompt_ids,
                "answer_ids": answer_ids,
                "input_ids": [*prompt_ids, *answer_ids],
                "target_ids": [-100] * len(prompt_ids) + answer_ids,
                "corrected_prompt_ids": corrected_prompt_ids,
                "corrected_input_ids": [
                    *corrected_prompt_ids,
                    *answer_ids,
                ],
                "corrected_target_ids": (
                    [-100] * len(corrected_prompt_ids) + answer_ids
                ),
            }
        )
        task_intent_targets = self.get("task_intent_targets")
        if isinstance(task_intent_targets, torch.Tensor):
            row["task_intent_targets"] = [
                float(value)
                for value in task_intent_targets.to(
                    device="cpu",
                    dtype=torch.float32,
                ).tolist()
            ]
        return row

    def bmpas_tensor_row_boundary(self) -> dict[str, Any]:
        """Tensor-native row for BMPAS train_fn — no Python list materialization."""

        prompt_t = cast(torch.Tensor, self["prompt_ids"]).to(dtype=torch.long)
        answer_t = cast(torch.Tensor, self["answer_ids"]).to(dtype=torch.long)
        corrected_t = self.corrected_prompt_ids_t.to(dtype=torch.long)
        input_ids = torch.cat((prompt_t, answer_t), dim=0)
        target_ids = torch.cat(
            (
                torch.full((prompt_t.numel(),), -100, dtype=torch.long),
                answer_t,
            ),
            dim=0,
        )
        corrected_input = torch.cat((corrected_t, answer_t), dim=0)
        corrected_target = torch.cat(
            (
                torch.full((corrected_t.numel(),), -100, dtype=torch.long),
                answer_t,
            ),
            dim=0,
        )
        row: dict[str, Any] = {
            key: value
            for key, value in self.items()
            if key not in {"prompt_ids", "answer_ids", "task_intent_targets"}
        }
        row.update(
            {
                "prompt_ids": prompt_t,
                "answer_ids": answer_t,
                "input_ids": input_ids,
                "target_ids": target_ids,
                "corrected_prompt_ids": corrected_t,
                "corrected_input_ids": corrected_input,
                "corrected_target_ids": corrected_target,
                "_bmpas_tensor_native": True,
            }
        )
        task_intent_targets = self.get("task_intent_targets")
        if isinstance(task_intent_targets, torch.Tensor):
            row["task_intent_targets"] = task_intent_targets
        return row

    def __eq__(self, other: object) -> bool:
        if isinstance(other, PackedTokenTrainingRow):
            return self.source_row_sha256 == other.source_row_sha256
        if isinstance(other, dict):
            return self.legacy_row_boundary() == other
        return False