File size: 80,170 Bytes
ebcde1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
"""Immutable input-manifest contract for the frozen M8 trade study.

The verifier intentionally inspects only JSON metadata, exact file hashes, and
Parquet footers/schemas.  It never materializes normalized trade rows.  Economic
row-level validation belongs to the acquisition/quality stage whose immutable
reports and findings are bound here.
"""

from __future__ import annotations

import hashlib
import json
import os
import re
import struct
import tempfile
from collections.abc import Mapping, Sequence
from dataclasses import dataclass
from datetime import UTC, datetime
from datetime import date as Date
from decimal import Decimal, InvalidOperation
from pathlib import Path
from types import MappingProxyType
from typing import Any, cast
from urllib.parse import parse_qsl, urlparse
from zipfile import BadZipFile, ZipFile

import pyarrow as pa  # type: ignore[import-untyped]
import pyarrow.parquet as pq  # type: ignore[import-untyped]

from microstructure.data.schemas import SCHEMA_VERSION, get_schema
from microstructure.m8_config import M8PeriodRole, M8StudyConfig
from microstructure.provenance import sha256_file

M8_INPUT_MANIFEST_VERSION = "1.2.0"
_STORAGE_MANIFEST_VERSION = "1.0.0"
_DIGEST = re.compile(r"[0-9a-f]{64}")
_UTC_TIMESTAMP = re.compile(
    r"(?P<second>\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2})"
    r"(?:\.(?P<fraction>\d{1,9}))?(?:Z|\+00:00)"
)
_MAX_JSON_BYTES = 8 * 1024 * 1024
_MAX_SYMBOL_METADATA_BYTES = 1 * 1024 * 1024
_MAX_ARCHIVE_CHECKSUM_BYTES = 4_096
_MAX_ZIP_DIRECTORY_BYTES = 256 * 1_024
_MAX_ZIP_TAIL_BYTES = 22 + 65_535
_NS_PER_DAY = 86_400 * 1_000_000_000
_EPOCH = datetime(1970, 1, 1, tzinfo=UTC)
_ZIP_CENTRAL_SIGNATURE = b"PK\x01\x02"
_ZIP_CENTRAL_STRUCT = struct.Struct("<4s6H3L5H2L")
_ZIP_EOCD_SIGNATURE = b"PK\x05\x06"
_ZIP_EOCD_STRUCT = struct.Struct("<4s4H2LH")
_ZIP64_EXTRA_FIELD_ID = 0x0001
_ZIP16_SENTINEL = 0xFFFF
_ZIP32_SENTINEL = 0xFFFFFFFF

_TOP_KEYS = frozenset(
    {
        "manifest_version",
        "artifact_kind",
        "config",
        "study",
        "symbol_metadata",
        "total_symbol_metadata_bytes",
        "total_raw_zip_bytes",
        "entries",
    }
)
_CONFIG_KEYS = frozenset({"semantic_sha256", "source_sha256", "protocol_version"})
_STUDY_KEYS = frozenset({"name", "evidence_tier", "source", "symbols", "periods"})
_STUDY_PERIOD_KEYS = frozenset({"date", "role"})
_SYMBOL_METADATA_KEYS = frozenset(
    {
        "symbol",
        "status",
        "tick_size",
        "lot_size",
        "observed_ts_ns",
        "raw_path",
        "raw_sha256",
        "raw_bytes",
        "source_uri",
        "source_manifest_path",
        "source_manifest_sha256",
        "source_manifest_bytes",
    }
)
_ARCHIVE_CHECKSUM_KEYS = frozenset(
    {
        "path",
        "sha256",
        "bytes",
        "source_uri",
        "source_manifest_path",
        "source_manifest_sha256",
        "source_manifest_bytes",
    }
)
_ENTRY_KEYS = frozenset(
    {
        "symbol",
        "date",
        "role",
        "complete",
        "requested_range_ns",
        "rows",
        "trade_id_range",
        "observed_range_ns",
        "scales",
        "raw",
        "normalized",
        "quality",
    }
)
_RANGE_KEYS = frozenset({"start", "end_exclusive"})
_OBSERVED_KEYS = frozenset({"start", "end_inclusive"})
_TRADE_ID_KEYS = frozenset({"first", "last", "contiguous_count"})
_SCALE_KEYS = frozenset({"tick_size", "lot_size"})
_CHECKSUM_KEYS = frozenset({"algorithm", "value"})
_RAW_KEYS = frozenset(
    {
        "zip_path",
        "zip_sha256",
        "zip_bytes",
        "uncompressed_bytes",
        "source_uri",
        "source_manifest_path",
        "source_manifest_sha256",
        "source_manifest_bytes",
        "checksum",
    }
)
_NORMALIZED_KEYS = frozenset(
    {
        "dataset_manifest_path",
        "dataset_manifest_sha256",
        "dataset_manifest_bytes",
        "rows",
        "parts",
    }
)
_PART_KEYS = frozenset(
    {
        "data_path",
        "data_sha256",
        "data_bytes",
        "sidecar_path",
        "sidecar_sha256",
        "sidecar_bytes",
        "rows",
        "write_ordinal",
        "observed_range_ns",
    }
)
_QUALITY_KEYS = frozenset(
    {
        "report_path",
        "report_sha256",
        "report_bytes",
        "findings_path",
        "findings_sha256",
        "findings_bytes",
        "errors",
        "warnings",
    }
)
_RAW_SIDECAR_KEYS = frozenset(
    {
        "manifest_version",
        "artifact_kind",
        "source",
        "source_uri",
        "downloaded_at_utc",
        "requested_range_ns",
        "checksum",
        "upstream_checksum_sha256",
        "bytes",
        "path",
        "response_headers",
    }
)
_DATASET_KEYS = frozenset(
    {
        "manifest_version",
        "dataset",
        "schema_version",
        "source",
        "source_uri",
        "downloaded_at_utc",
        "requested_range_ns",
        "artifacts",
        "rows",
    }
)
_DATASET_PART_KEYS = frozenset(
    {
        "data_path",
        "manifest_path",
        "data_sha256",
        "manifest_sha256",
        "rows",
        "write_ordinal",
        "observed_range_ns",
    }
)
_PART_SIDECAR_KEYS = frozenset(
    {
        "manifest_version",
        "artifact_kind",
        "dataset",
        "schema_name",
        "schema_version",
        "venue",
        "symbol",
        "partition_date",
        "write_ordinal",
        "source",
        "source_uri",
        "downloaded_at_utc",
        "requested_range_ns",
        "observed_range_ns",
        "source_checksum_sha256",
        "checksum",
        "rows",
        "bytes",
        "path",
        "transformations",
    }
)


class M8ManifestError(RuntimeError):
    """Raised when M8 input evidence is missing, unsafe, or inconsistent."""


@dataclass(frozen=True, slots=True)
class M8NormalizedPart:
    data_path: Path
    data_sha256: str
    data_bytes: int
    sidecar_path: Path
    sidecar_sha256: str
    sidecar_bytes: int
    rows: int
    write_ordinal: int
    observed_start_ns: int
    observed_end_inclusive_ns: int


@dataclass(frozen=True, slots=True)
class M8SymbolMetadata:
    symbol: str
    status: str
    tick_size: Decimal
    lot_size: Decimal
    observed_ts_ns: int
    raw_path: Path
    raw_sha256: str
    raw_bytes: int
    source_uri: str
    source_manifest_path: Path
    source_manifest_sha256: str
    source_manifest_bytes: int


@dataclass(frozen=True, slots=True)
class M8ArchiveEntry:
    symbol: str
    date: Date
    role: M8PeriodRole
    complete: bool
    rows: int
    first_trade_id: int
    last_trade_id: int
    observed_start_ns: int
    observed_end_inclusive_ns: int
    tick_size: Decimal
    lot_size: Decimal
    raw_zip_path: Path
    raw_zip_sha256: str
    raw_zip_bytes: int
    raw_uncompressed_bytes: int
    raw_source_uri: str
    raw_source_manifest_path: Path
    raw_source_manifest_sha256: str
    raw_source_manifest_bytes: int
    raw_checksum_path: Path
    raw_checksum_sha256: str
    raw_checksum_bytes: int
    raw_checksum_source_uri: str
    raw_checksum_source_manifest_path: Path
    raw_checksum_source_manifest_sha256: str
    raw_checksum_source_manifest_bytes: int
    normalized_dataset_manifest_path: Path
    normalized_dataset_manifest_sha256: str
    normalized_dataset_manifest_bytes: int
    normalized_parts: tuple[M8NormalizedPart, ...]
    quality_report_path: Path
    quality_report_sha256: str
    quality_report_bytes: int
    quality_findings_path: Path
    quality_findings_sha256: str
    quality_findings_bytes: int
    quality_errors: int
    quality_warnings: int


@dataclass(frozen=True, slots=True)
class M8InputManifest:
    root: Path
    path: Path
    sha256: str
    config_sha256: str
    config_source_sha256: str
    protocol_version: str
    symbol_metadata: tuple[M8SymbolMetadata, ...]
    entries: tuple[M8ArchiveEntry, ...]

    def metadata_for(self, symbol: str) -> M8SymbolMetadata:
        for metadata in self.symbol_metadata:
            if metadata.symbol == symbol:
                return metadata
        raise KeyError(f"no verified M8 symbol metadata for {symbol}")

    @property
    def ordered_part_paths(self) -> Mapping[tuple[str, str], tuple[Path, ...]]:
        """Return verified Parquet paths keyed in frozen period/symbol order."""

        paths = {
            (entry.symbol, entry.date.isoformat()): tuple(
                part.data_path for part in entry.normalized_parts
            )
            for entry in self.entries
        }
        return MappingProxyType(paths)

    def part_paths_for(self, symbol: str, date: Date | str) -> tuple[Path, ...]:
        date_text = date.isoformat() if isinstance(date, Date) else date
        try:
            return self.ordered_part_paths[(symbol, date_text)]
        except KeyError as exc:
            raise KeyError(f"no verified M8 parts for {symbol}/{date_text}") from exc


def _day_bounds_ns(day: Date) -> tuple[int, int]:
    start = datetime(day.year, day.month, day.day, tzinfo=UTC)
    delta = start - _EPOCH
    start_ns = (delta.days * 86_400 + delta.seconds) * 1_000_000_000
    return start_ns, start_ns + _NS_PER_DAY


def _object(value: object, label: str) -> Mapping[str, Any]:
    if not isinstance(value, Mapping):
        raise M8ManifestError(f"{label} must be a JSON object")
    if not all(isinstance(key, str) for key in value):
        raise M8ManifestError(f"{label} contains a non-string key")
    return cast(Mapping[str, Any], value)


def _array(value: object, label: str) -> list[Any]:
    if not isinstance(value, list):
        raise M8ManifestError(f"{label} must be a JSON array")
    return value


def _text(value: object, label: str) -> str:
    if type(value) is not str:
        raise M8ManifestError(f"{label} must be a string")
    return value


def _integer(value: object, label: str, *, minimum: int | None = None) -> int:
    if type(value) is not int:
        raise M8ManifestError(f"{label} must be an integer")
    result = value
    if minimum is not None and result < minimum:
        raise M8ManifestError(f"{label} must be at least {minimum}")
    return result


def _boolean(value: object, label: str) -> bool:
    if type(value) is not bool:
        raise M8ManifestError(f"{label} must be a boolean")
    return value


def _digest(value: object, label: str) -> str:
    digest = _text(value, label)
    if _DIGEST.fullmatch(digest) is None:
        raise M8ManifestError(f"{label} must be a lowercase SHA-256 digest")
    return digest


def _decimal(value: object, label: str) -> Decimal:
    raw = _text(value, label)
    try:
        result = Decimal(raw)
    except InvalidOperation as exc:
        raise M8ManifestError(f"{label} must be a decimal string") from exc
    if not result.is_finite() or result <= 0:
        raise M8ManifestError(f"{label} must be finite and positive")
    return result


def _exact_keys(value: Mapping[str, Any], expected: frozenset[str], label: str) -> None:
    observed = frozenset(value)
    missing = sorted(expected - observed)
    extra = sorted(observed - expected)
    if missing or extra:
        details: list[str] = []
        if missing:
            details.append("missing=" + ",".join(missing))
        if extra:
            details.append("extra=" + ",".join(extra))
        raise M8ManifestError(f"{label} keys are invalid ({'; '.join(details)})")


def _parse_date(value: object, label: str) -> Date:
    raw = _text(value, label)
    try:
        parsed = Date.fromisoformat(raw)
    except ValueError as exc:
        raise M8ManifestError(f"{label} must be an ISO UTC date") from exc
    if parsed.isoformat() != raw:
        raise M8ManifestError(f"{label} must use canonical YYYY-MM-DD form")
    return parsed


def _parse_role(value: object, label: str) -> M8PeriodRole:
    raw = _text(value, label)
    allowed = {"train", "validation", "primary_test", "replication_test"}
    if raw not in allowed:
        raise M8ManifestError(f"{label} has an unsupported role: {raw!r}")
    return cast(M8PeriodRole, raw)


def _utc_ns_from_iso(value: object, label: str) -> int:
    raw = _text(value, label)
    matched = _UTC_TIMESTAMP.fullmatch(raw)
    if matched is None:
        raise M8ManifestError(
            f"{label} must be an ISO-8601 UTC timestamp with at most nanosecond precision"
        )
    try:
        second = datetime.strptime(matched.group("second"), "%Y-%m-%dT%H:%M:%S").replace(tzinfo=UTC)
    except ValueError as exc:
        raise M8ManifestError(f"{label} is not a valid UTC timestamp") from exc
    fraction = (matched.group("fraction") or "").ljust(9, "0")
    delta = second - _EPOCH
    return (delta.days * 86_400 + delta.seconds) * 1_000_000_000 + int(fraction or "0")


def _read_json_object(path: Path, label: str) -> Mapping[str, Any]:
    try:
        size = path.stat().st_size
    except OSError as exc:
        raise M8ManifestError(f"cannot stat {label} at {path}: {exc}") from exc
    if size > _MAX_JSON_BYTES:
        raise M8ManifestError(f"{label} exceeds the {_MAX_JSON_BYTES}-byte JSON limit")

    def reject_duplicates(pairs: list[tuple[str, Any]]) -> dict[str, Any]:
        result: dict[str, Any] = {}
        for key, value in pairs:
            if key in result:
                raise M8ManifestError(f"{label} contains duplicate JSON key {key!r}")
            result[key] = value
        return result

    try:
        with path.open(encoding="utf-8") as handle:
            parsed = json.load(handle, object_pairs_hook=reject_duplicates)
    except M8ManifestError:
        raise
    except (OSError, UnicodeDecodeError, json.JSONDecodeError) as exc:
        raise M8ManifestError(f"cannot parse {label} at {path}: {exc}") from exc
    return _object(parsed, label)


def _resolve_contained_file(root: Path, value: str | Path, label: str) -> Path:
    declared = Path(value)
    candidate = declared if declared.is_absolute() else root / declared
    resolved_root = root.resolve()
    resolved = candidate.resolve()
    if not resolved.is_relative_to(resolved_root):
        raise M8ManifestError(f"{label} escapes the M8 input root: {declared}")
    if not resolved.is_file():
        raise M8ManifestError(f"missing {label}: {resolved}")
    return resolved


def _declared_file(root: Path, value: object, label: str) -> Path:
    raw = _text(value, label)
    if Path(raw).is_absolute():
        raise M8ManifestError(f"{label} must be relative to the M8 input root")
    return _resolve_contained_file(root, raw, label)


def _relative_file(root: Path, value: Path, label: str) -> str:
    resolved = _resolve_contained_file(root, value, label)
    return resolved.relative_to(root.resolve()).as_posix()


def _verify_file(path: Path, sha256: str, byte_count: int, label: str) -> None:
    if path.stat().st_size != byte_count:
        raise M8ManifestError(f"{label} byte count does not match")
    observed = sha256_file(path)
    if observed != sha256:
        raise M8ManifestError(f"{label} SHA-256 mismatch: expected {sha256}, observed {observed}")


def _range(value: object, label: str) -> tuple[int, int]:
    raw = _object(value, label)
    _exact_keys(raw, _RANGE_KEYS, label)
    start = _integer(raw["start"], f"{label}.start")
    end = _integer(raw["end_exclusive"], f"{label}.end_exclusive")
    if end <= start:
        raise M8ManifestError(f"{label} must be non-empty")
    return start, end


def _observed_range(value: object, label: str) -> tuple[int, int]:
    raw = _object(value, label)
    _exact_keys(raw, _OBSERVED_KEYS, label)
    start = _integer(raw["start"], f"{label}.start")
    end = _integer(raw["end_inclusive"], f"{label}.end_inclusive")
    if end < start:
        raise M8ManifestError(f"{label} ends before it starts")
    return start, end


def _verify_day_range(observed: tuple[int, int], day_range: tuple[int, int], label: str) -> None:
    if observed[0] < day_range[0] or observed[1] >= day_range[1]:
        raise M8ManifestError(f"{label} falls outside its declared UTC day")


def _expected_entry_order(config: M8StudyConfig) -> tuple[tuple[str, Date, M8PeriodRole], ...]:
    return tuple(
        (symbol, period.date, period.role)
        for period in config.periods
        for symbol in config.study.symbols
    )


def _ordered_symbol_metadata(
    config: M8StudyConfig, values: Sequence[M8SymbolMetadata]
) -> tuple[M8SymbolMetadata, ...]:
    expected_symbols = config.study.symbols
    observed: dict[str, M8SymbolMetadata] = {}
    for metadata in values:
        if metadata.symbol in observed:
            raise M8ManifestError(f"duplicate M8 symbol metadata for {metadata.symbol}")
        if metadata.symbol not in expected_symbols:
            raise M8ManifestError(f"extra M8 symbol metadata for {metadata.symbol}")
        observed[metadata.symbol] = metadata
    missing = [symbol for symbol in expected_symbols if symbol not in observed]
    if missing:
        raise M8ManifestError("missing M8 symbol metadata: " + ", ".join(missing))
    if len(observed) != 2:
        raise M8ManifestError("the frozen M8 study requires exactly two symbol metadata entries")
    return tuple(observed[symbol] for symbol in expected_symbols)


def _ordered_entries(
    config: M8StudyConfig, entries: Sequence[M8ArchiveEntry]
) -> tuple[M8ArchiveEntry, ...]:
    expected = _expected_entry_order(config)
    expected_keys = {(symbol, day): role for symbol, day, role in expected}
    observed: dict[tuple[str, Date], M8ArchiveEntry] = {}
    for entry in entries:
        key = (entry.symbol, entry.date)
        if key in observed:
            raise M8ManifestError(
                f"duplicate M8 archive entry for {entry.symbol}/{entry.date.isoformat()}"
            )
        expected_role = expected_keys.get(key)
        if expected_role is None:
            raise M8ManifestError(
                f"extra M8 archive entry for {entry.symbol}/{entry.date.isoformat()}"
            )
        if entry.role != expected_role:
            raise M8ManifestError(
                f"role mismatch for {entry.symbol}/{entry.date.isoformat()}: "
                f"expected {expected_role}, observed {entry.role}"
            )
        observed[key] = entry
    missing = [
        f"{symbol}/{day.isoformat()}"
        for symbol, day, _role in expected
        if (symbol, day) not in observed
    ]
    if missing:
        raise M8ManifestError("missing M8 archive entries: " + ", ".join(missing))
    if len(observed) != 8:
        raise M8ManifestError("the frozen M8 study requires exactly eight symbol/date entries")
    return tuple(observed[(symbol, day)] for symbol, day, _role in expected)


def _symbol_metadata_payload(root: Path, metadata: M8SymbolMetadata) -> dict[str, object]:
    return {
        "symbol": metadata.symbol,
        "status": metadata.status,
        "tick_size": format(metadata.tick_size, "f"),
        "lot_size": format(metadata.lot_size, "f"),
        "observed_ts_ns": metadata.observed_ts_ns,
        "raw_path": _relative_file(root, metadata.raw_path, "exchangeInfo raw body"),
        "raw_sha256": metadata.raw_sha256,
        "raw_bytes": metadata.raw_bytes,
        "source_uri": metadata.source_uri,
        "source_manifest_path": _relative_file(
            root,
            metadata.source_manifest_path,
            "exchangeInfo source sidecar",
        ),
        "source_manifest_sha256": metadata.source_manifest_sha256,
        "source_manifest_bytes": metadata.source_manifest_bytes,
    }


def _part_payload(root: Path, part: M8NormalizedPart) -> dict[str, object]:
    return {
        "data_path": _relative_file(root, part.data_path, "normalized Parquet part"),
        "data_sha256": part.data_sha256,
        "data_bytes": part.data_bytes,
        "sidecar_path": _relative_file(root, part.sidecar_path, "normalized part sidecar"),
        "sidecar_sha256": part.sidecar_sha256,
        "sidecar_bytes": part.sidecar_bytes,
        "rows": part.rows,
        "write_ordinal": part.write_ordinal,
        "observed_range_ns": {
            "start": part.observed_start_ns,
            "end_inclusive": part.observed_end_inclusive_ns,
        },
    }


def _entry_payload(root: Path, entry: M8ArchiveEntry) -> dict[str, object]:
    day_start, day_end = _day_bounds_ns(entry.date)
    parts = sorted(entry.normalized_parts, key=lambda item: item.write_ordinal)
    return {
        "symbol": entry.symbol,
        "date": entry.date.isoformat(),
        "role": entry.role,
        "complete": entry.complete,
        "requested_range_ns": {"start": day_start, "end_exclusive": day_end},
        "rows": entry.rows,
        "trade_id_range": {
            "first": entry.first_trade_id,
            "last": entry.last_trade_id,
            "contiguous_count": entry.rows,
        },
        "observed_range_ns": {
            "start": entry.observed_start_ns,
            "end_inclusive": entry.observed_end_inclusive_ns,
        },
        "scales": {
            "tick_size": format(entry.tick_size, "f"),
            "lot_size": format(entry.lot_size, "f"),
        },
        "raw": {
            "zip_path": _relative_file(root, entry.raw_zip_path, "raw ZIP"),
            "zip_sha256": entry.raw_zip_sha256,
            "zip_bytes": entry.raw_zip_bytes,
            "uncompressed_bytes": entry.raw_uncompressed_bytes,
            "source_uri": entry.raw_source_uri,
            "source_manifest_path": _relative_file(
                root, entry.raw_source_manifest_path, "raw source sidecar"
            ),
            "source_manifest_sha256": entry.raw_source_manifest_sha256,
            "source_manifest_bytes": entry.raw_source_manifest_bytes,
            "checksum": {
                "path": _relative_file(
                    root,
                    entry.raw_checksum_path,
                    "official archive CHECKSUM",
                ),
                "sha256": entry.raw_checksum_sha256,
                "bytes": entry.raw_checksum_bytes,
                "source_uri": entry.raw_checksum_source_uri,
                "source_manifest_path": _relative_file(
                    root,
                    entry.raw_checksum_source_manifest_path,
                    "official archive CHECKSUM source sidecar",
                ),
                "source_manifest_sha256": entry.raw_checksum_source_manifest_sha256,
                "source_manifest_bytes": entry.raw_checksum_source_manifest_bytes,
            },
        },
        "normalized": {
            "dataset_manifest_path": _relative_file(
                root,
                entry.normalized_dataset_manifest_path,
                "normalized dataset manifest",
            ),
            "dataset_manifest_sha256": entry.normalized_dataset_manifest_sha256,
            "dataset_manifest_bytes": entry.normalized_dataset_manifest_bytes,
            "rows": entry.rows,
            "parts": [_part_payload(root, part) for part in parts],
        },
        "quality": {
            "report_path": _relative_file(root, entry.quality_report_path, "quality report"),
            "report_sha256": entry.quality_report_sha256,
            "report_bytes": entry.quality_report_bytes,
            "findings_path": _relative_file(root, entry.quality_findings_path, "quality findings"),
            "findings_sha256": entry.quality_findings_sha256,
            "findings_bytes": entry.quality_findings_bytes,
            "errors": entry.quality_errors,
            "warnings": entry.quality_warnings,
        },
    }


def _manifest_payload(
    config: M8StudyConfig,
    root: Path,
    entries: Sequence[M8ArchiveEntry],
    symbol_metadata: Sequence[M8SymbolMetadata],
) -> dict[str, object]:
    ordered = _ordered_entries(config, entries)
    ordered_metadata = _ordered_symbol_metadata(config, symbol_metadata)
    entry_payloads = [_entry_payload(root, entry) for entry in ordered]
    return {
        "manifest_version": M8_INPUT_MANIFEST_VERSION,
        "artifact_kind": "m8_multidate_trade_input",
        "config": {
            "semantic_sha256": config.hash,
            "source_sha256": config.source_sha256,
            "protocol_version": config.study.protocol_version,
        },
        "study": {
            "name": config.study.name,
            "evidence_tier": config.study.evidence_tier,
            "source": config.study.source,
            "symbols": list(config.study.symbols),
            "periods": [
                {"date": period.date.isoformat(), "role": period.role} for period in config.periods
            ],
        },
        "symbol_metadata": [
            _symbol_metadata_payload(root, metadata) for metadata in ordered_metadata
        ],
        "total_symbol_metadata_bytes": sum(metadata.raw_bytes for metadata in ordered_metadata),
        "total_raw_zip_bytes": sum(entry.raw_zip_bytes for entry in ordered),
        "entries": entry_payloads,
    }


def _verify_exchange_info_uri(source_uri: str, symbol: str, label: str) -> None:
    try:
        parsed = urlparse(source_uri)
        port = parsed.port
    except ValueError as exc:
        raise M8ManifestError(f"{label} has an invalid network location") from exc
    try:
        query = parse_qsl(
            parsed.query,
            keep_blank_values=True,
            strict_parsing=True,
            max_num_fields=2,
        )
    except ValueError as exc:
        raise M8ManifestError(f"{label} has an invalid query") from exc
    if (
        parsed.scheme != "https"
        or parsed.netloc != "data-api.binance.vision"
        or parsed.hostname != "data-api.binance.vision"
        or port is not None
        or parsed.username is not None
        or parsed.password is not None
        or parsed.path != "/api/v3/exchangeInfo"
        or parsed.params
        or parsed.fragment
        or query != [("symbol", symbol)]
        or any(ord(character) < 0x20 or ord(character) == 0x7F for character in source_uri)
    ):
        raise M8ManifestError(
            f"{label} is not the exact official exchangeInfo request for {symbol}"
        )


def _verify_null_requested_range(value: object, label: str) -> None:
    requested = _object(value, label)
    _exact_keys(requested, _RANGE_KEYS, label)
    if requested["start"] is not None or requested["end_exclusive"] is not None:
        raise M8ManifestError(f"{label} must have null API range bounds")


def _verify_exchange_info_payload(
    raw_path: Path,
    *,
    symbol: str,
    status: str,
    tick_size: Decimal,
    lot_size: Decimal,
    label: str,
) -> None:
    payload = _read_json_object(raw_path, f"{label} raw body")
    symbols = _array(payload.get("symbols"), f"{label} raw body symbols")
    if len(symbols) != 1:
        raise M8ManifestError(f"{label} raw body must contain exactly the requested symbol")
    item = _object(symbols[0], f"{label} raw body symbol")
    if _text(item.get("symbol"), f"{label} raw body symbol name") != symbol:
        raise M8ManifestError(f"{label} raw body returned a different symbol")
    if _text(item.get("status"), f"{label} raw body status") != status:
        raise M8ManifestError(f"{label} raw body status does not match")

    filters: dict[str, Mapping[str, Any]] = {}
    for index, raw_filter in enumerate(_array(item.get("filters"), f"{label} raw body filters")):
        filter_value = _object(raw_filter, f"{label} raw body filters[{index}]")
        filter_type = _text(
            filter_value.get("filterType"),
            f"{label} raw body filters[{index}].filterType",
        )
        if filter_type in filters:
            raise M8ManifestError(f"{label} raw body has duplicate {filter_type} filters")
        filters[filter_type] = filter_value
    try:
        price_filter = filters["PRICE_FILTER"]
        lot_filter = filters["LOT_SIZE"]
    except KeyError as exc:
        raise M8ManifestError(
            f"{label} raw body lacks PRICE_FILTER or LOT_SIZE provenance"
        ) from exc
    source_tick = _decimal(price_filter.get("tickSize"), f"{label} raw PRICE_FILTER.tickSize")
    source_lot = _decimal(lot_filter.get("stepSize"), f"{label} raw LOT_SIZE.stepSize")
    if source_tick != tick_size or source_lot != lot_size:
        raise M8ManifestError(f"{label} declared scales do not match its raw exchangeInfo body")


def _verify_symbol_metadata(
    root: Path,
    value: object,
    expected_symbol: str,
    index: int,
) -> M8SymbolMetadata:
    label = f"symbol_metadata[{index}]"
    metadata = _object(value, label)
    _exact_keys(metadata, _SYMBOL_METADATA_KEYS, label)
    symbol = _text(metadata["symbol"], f"{label}.symbol")
    if symbol != expected_symbol:
        raise M8ManifestError(f"{label} is out of frozen symbol order")
    status = _text(metadata["status"], f"{label}.status")
    if status != "TRADING":
        raise M8ManifestError(f"{label} must prove that {symbol} was TRADING")
    tick_size = _decimal(metadata["tick_size"], f"{label}.tick_size")
    lot_size = _decimal(metadata["lot_size"], f"{label}.lot_size")
    observed_ts_ns = _integer(metadata["observed_ts_ns"], f"{label}.observed_ts_ns", minimum=1)

    raw_path = _declared_file(root, metadata["raw_path"], f"{label} raw body")
    raw_sha = _digest(metadata["raw_sha256"], f"{label} raw SHA")
    raw_bytes = _integer(metadata["raw_bytes"], f"{label} raw bytes", minimum=1)
    if raw_bytes > _MAX_SYMBOL_METADATA_BYTES:
        raise M8ManifestError(
            f"{label} raw body exceeds the {_MAX_SYMBOL_METADATA_BYTES}-byte metadata limit"
        )
    _verify_file(raw_path, raw_sha, raw_bytes, f"{label} raw body")

    source_uri = _text(metadata["source_uri"], f"{label}.source_uri")
    _verify_exchange_info_uri(source_uri, symbol, f"{label}.source_uri")
    source_manifest_path = _declared_file(
        root,
        metadata["source_manifest_path"],
        f"{label} source sidecar",
    )
    source_manifest_sha = _digest(metadata["source_manifest_sha256"], f"{label} source sidecar SHA")
    source_manifest_bytes = _integer(
        metadata["source_manifest_bytes"],
        f"{label} source sidecar bytes",
        minimum=1,
    )
    _verify_file(
        source_manifest_path,
        source_manifest_sha,
        source_manifest_bytes,
        f"{label} source sidecar",
    )
    if raw_path == source_manifest_path:
        raise M8ManifestError(f"{label} raw body and source sidecar must be distinct files")

    sidecar = _read_json_object(source_manifest_path, f"{label} source sidecar")
    _exact_keys(sidecar, _RAW_SIDECAR_KEYS, f"{label} source sidecar")
    expected_claims: dict[str, object] = {
        "manifest_version": _STORAGE_MANIFEST_VERSION,
        "artifact_kind": "raw_source",
        "source": "binance_spot_public_api",
        "source_uri": source_uri,
        "bytes": raw_bytes,
        "path": raw_path.name,
    }
    for key, expected in expected_claims.items():
        if sidecar[key] != expected:
            raise M8ManifestError(f"{label} source sidecar {key} claim does not match")
    _verify_null_requested_range(
        sidecar["requested_range_ns"], f"{label} source sidecar requested range"
    )
    checksum = _object(sidecar["checksum"], f"{label} source sidecar checksum")
    _exact_keys(checksum, _CHECKSUM_KEYS, f"{label} source sidecar checksum")
    if (
        checksum["algorithm"] != "sha256"
        or _digest(checksum["value"], f"{label} source sidecar checksum value") != raw_sha
    ):
        raise M8ManifestError(f"{label} source sidecar checksum does not match")
    if sidecar["upstream_checksum_sha256"] is not None:
        raise M8ManifestError(f"{label} API source sidecar must not claim an upstream checksum")
    downloaded_ts_ns = _utc_ns_from_iso(
        sidecar["downloaded_at_utc"], f"{label} source sidecar download timestamp"
    )
    if downloaded_ts_ns != observed_ts_ns:
        raise M8ManifestError(f"{label} observed timestamp does not match its source sidecar")
    response_headers = _object(
        sidecar["response_headers"], f"{label} source sidecar response headers"
    )
    if not all(type(header_value) is str for header_value in response_headers.values()):
        raise M8ManifestError(f"{label} source sidecar response headers must be strings")

    _verify_exchange_info_payload(
        raw_path,
        symbol=symbol,
        status=status,
        tick_size=tick_size,
        lot_size=lot_size,
        label=label,
    )
    return M8SymbolMetadata(
        symbol=symbol,
        status=status,
        tick_size=tick_size,
        lot_size=lot_size,
        observed_ts_ns=observed_ts_ns,
        raw_path=raw_path,
        raw_sha256=raw_sha,
        raw_bytes=raw_bytes,
        source_uri=source_uri,
        source_manifest_path=source_manifest_path,
        source_manifest_sha256=source_manifest_sha,
        source_manifest_bytes=source_manifest_bytes,
    )


def _verify_official_daily_uri(
    source_uri: str,
    *,
    symbol: str,
    expected_name: str,
    label: str,
) -> None:
    try:
        parsed_uri = urlparse(source_uri)
        port = parsed_uri.port
    except ValueError as exc:
        raise M8ManifestError(f"{label} has an invalid network location") from exc
    expected_uri_path = f"/data/spot/daily/aggTrades/{symbol}/{expected_name}"
    expected_uri = f"https://data.binance.vision{expected_uri_path}"
    if (
        source_uri != expected_uri
        or parsed_uri.scheme != "https"
        or parsed_uri.netloc != "data.binance.vision"
        or parsed_uri.hostname != "data.binance.vision"
        or port is not None
        or parsed_uri.username is not None
        or parsed_uri.password is not None
        or parsed_uri.path != expected_uri_path
        or parsed_uri.params
        or parsed_uri.query
        or parsed_uri.fragment
    ):
        raise M8ManifestError(f"{label} is not the exact official daily archive URI")


def _verify_official_checksum(
    *,
    entry_label: str,
    root: Path,
    value: object,
    symbol: str,
    day: Date,
    day_range: tuple[int, int],
    zip_path: Path,
    zip_sha256: str,
    archive_source_uri: str,
    archive_sidecar_path: Path,
) -> tuple[Path, str, int, str, Path, str, int]:
    label = f"{entry_label} official CHECKSUM"
    checksum = _object(value, label)
    _exact_keys(checksum, _ARCHIVE_CHECKSUM_KEYS, label)
    archive_name = f"{symbol}-aggTrades-{day.isoformat()}.zip"
    checksum_name = f"{archive_name}.CHECKSUM"

    checksum_path = _declared_file(root, checksum["path"], f"{label} raw body")
    if checksum_path.name != checksum_name:
        raise M8ManifestError(f"{label} filename does not match the official archive basename")
    checksum_sha = _digest(checksum["sha256"], f"{label} raw SHA")
    checksum_bytes = _integer(checksum["bytes"], f"{label} raw bytes", minimum=1)
    if checksum_bytes > _MAX_ARCHIVE_CHECKSUM_BYTES:
        raise M8ManifestError(
            f"{label} exceeds the {_MAX_ARCHIVE_CHECKSUM_BYTES}-byte checksum limit"
        )
    _verify_file(checksum_path, checksum_sha, checksum_bytes, f"{label} raw body")
    try:
        checksum_body = checksum_path.read_bytes()
    except OSError as exc:
        raise M8ManifestError(f"cannot read {label} raw body: {exc}") from exc
    expected_line = f"{zip_sha256}  {archive_name}".encode("ascii")
    if checksum_body not in {
        expected_line,
        expected_line + b"\n",
        expected_line + b"\r\n",
    }:
        raise M8ManifestError(f"{label} body must be exactly one ZIP SHA-256/basename line")

    source_uri = _text(checksum["source_uri"], f"{label}.source_uri")
    if source_uri != f"{archive_source_uri}.CHECKSUM":
        raise M8ManifestError(f"{label} URI is not bound to its ZIP URI")
    _verify_official_daily_uri(
        source_uri,
        symbol=symbol,
        expected_name=checksum_name,
        label=f"{label}.source_uri",
    )
    sidecar_path = _declared_file(
        root,
        checksum["source_manifest_path"],
        f"{label} source sidecar",
    )
    sidecar_sha = _digest(checksum["source_manifest_sha256"], f"{label} source sidecar SHA")
    sidecar_bytes = _integer(
        checksum["source_manifest_bytes"],
        f"{label} source sidecar bytes",
        minimum=1,
    )
    _verify_file(sidecar_path, sidecar_sha, sidecar_bytes, f"{label} source sidecar")
    if checksum_path in {zip_path, archive_sidecar_path, sidecar_path} or sidecar_path in {
        zip_path,
        archive_sidecar_path,
    }:
        raise M8ManifestError(f"{label} evidence paths must be distinct")

    sidecar = _read_json_object(sidecar_path, f"{label} source sidecar")
    _exact_keys(sidecar, _RAW_SIDECAR_KEYS, f"{label} source sidecar")
    expected_claims: dict[str, object] = {
        "manifest_version": _STORAGE_MANIFEST_VERSION,
        "artifact_kind": "raw_source",
        "source": "binance_spot_daily_aggtrades_archive_checksum",
        "source_uri": source_uri,
        "bytes": checksum_bytes,
        "path": checksum_name,
    }
    for key, expected in expected_claims.items():
        if sidecar[key] != expected:
            raise M8ManifestError(f"{label} source sidecar {key} claim does not match")
    if _range(sidecar["requested_range_ns"], f"{label} requested range") != day_range:
        raise M8ManifestError(f"{label} source sidecar does not claim the full UTC day")
    sidecar_checksum = _object(sidecar["checksum"], f"{label} source sidecar checksum")
    _exact_keys(sidecar_checksum, _CHECKSUM_KEYS, f"{label} source sidecar checksum")
    if (
        sidecar_checksum["algorithm"] != "sha256"
        or _digest(sidecar_checksum["value"], f"{label} source sidecar checksum value")
        != checksum_sha
    ):
        raise M8ManifestError(f"{label} source sidecar checksum claim does not match")
    if sidecar["upstream_checksum_sha256"] is not None:
        raise M8ManifestError(f"{label} source sidecar must not claim an upstream checksum")
    if _utc_ns_from_iso(sidecar["downloaded_at_utc"], f"{label} download timestamp") < 1:
        raise M8ManifestError(f"{label} download timestamp must be after the Unix epoch")
    response_headers = _object(sidecar["response_headers"], f"{label} response headers")
    if not all(type(header_value) is str for header_value in response_headers.values()):
        raise M8ManifestError(f"{label} response headers must be strings")
    return (
        checksum_path,
        checksum_sha,
        checksum_bytes,
        source_uri,
        sidecar_path,
        sidecar_sha,
        sidecar_bytes,
    )


def _verify_raw_source_sidecar(
    *,
    config: M8StudyConfig,
    entry_label: str,
    day_range: tuple[int, int],
    symbol: str,
    day: Date,
    zip_path: Path,
    zip_sha256: str,
    zip_bytes: int,
    source_uri: str,
    sidecar_path: Path,
) -> None:
    expected_name = f"{symbol}-aggTrades-{day.isoformat()}.zip"
    if zip_path.name != expected_name:
        raise M8ManifestError(f"{entry_label} raw ZIP filename is not the frozen daily archive")
    _verify_official_daily_uri(
        source_uri,
        symbol=symbol,
        expected_name=expected_name,
        label=f"{entry_label} source URI",
    )

    sidecar = _read_json_object(sidecar_path, f"{entry_label} raw source sidecar")
    _exact_keys(sidecar, _RAW_SIDECAR_KEYS, f"{entry_label} raw source sidecar")
    if sidecar["manifest_version"] != _STORAGE_MANIFEST_VERSION:
        raise M8ManifestError(f"{entry_label} raw source sidecar version is unsupported")
    if sidecar["artifact_kind"] != "raw_source":
        raise M8ManifestError(f"{entry_label} raw source sidecar kind is invalid")
    if sidecar["source"] != config.study.source or sidecar["source_uri"] != source_uri:
        raise M8ManifestError(f"{entry_label} raw source sidecar lineage does not match")
    if _range(sidecar["requested_range_ns"], f"{entry_label} raw requested range") != day_range:
        raise M8ManifestError(f"{entry_label} raw source sidecar is not a complete UTC day")
    checksum = _object(sidecar["checksum"], f"{entry_label} raw checksum")
    _exact_keys(checksum, _CHECKSUM_KEYS, f"{entry_label} raw checksum")
    if (
        checksum.get("algorithm") != "sha256"
        or _digest(checksum.get("value"), f"{entry_label} raw checksum value") != zip_sha256
    ):
        raise M8ManifestError(f"{entry_label} raw source checksum claim does not match")
    if (
        _digest(
            sidecar["upstream_checksum_sha256"],
            f"{entry_label} upstream checksum",
        )
        != zip_sha256
    ):
        raise M8ManifestError(f"{entry_label} official upstream checksum does not match")
    if _integer(sidecar["bytes"], f"{entry_label} raw sidecar bytes", minimum=1) != zip_bytes:
        raise M8ManifestError(f"{entry_label} raw source byte claim does not match")
    sidecar_filename = _text(sidecar["path"], f"{entry_label} raw sidecar path")
    if Path(sidecar_filename).name != sidecar_filename or sidecar_filename != expected_name:
        raise M8ManifestError(f"{entry_label} raw source sidecar path does not match")
    if _utc_ns_from_iso(sidecar["downloaded_at_utc"], f"{entry_label} download timestamp") < 1:
        raise M8ManifestError(f"{entry_label} download timestamp must be after the Unix epoch")
    response_headers = _object(sidecar["response_headers"], f"{entry_label} response headers")
    if not all(type(header_value) is str for header_value in response_headers.values()):
        raise M8ManifestError(f"{entry_label} response headers must be strings")


def _verify_zip_archive(
    *,
    config: M8StudyConfig,
    entry_label: str,
    symbol: str,
    day: Date,
    zip_path: Path,
    uncompressed_bytes: int,
) -> None:
    expected_member = f"{symbol}-aggTrades-{day.isoformat()}.csv"
    try:
        _preflight_zip_central_directory(
            zip_path,
            entry_label=entry_label,
            expected_member=expected_member,
            expected_uncompressed_bytes=uncompressed_bytes,
            max_uncompressed_bytes=config.study.max_archive_uncompressed_bytes,
        )
        with ZipFile(zip_path) as archive:
            members = archive.infolist()
            if len(members) != 1:
                raise M8ManifestError(f"{entry_label} archive must contain exactly one CSV member")
            member = members[0]
            member_path = Path(member.filename)
            if (
                member.is_dir()
                or member.filename != expected_member
                or member_path.is_absolute()
                or ".." in member_path.parts
            ):
                raise M8ManifestError(f"{entry_label} archive member is not the frozen daily CSV")
            if member.flag_bits & 0x1:
                raise M8ManifestError(f"{entry_label} archive member must not be encrypted")
            if member.file_size != uncompressed_bytes:
                raise M8ManifestError(
                    f"{entry_label} expanded-byte claim does not match ZIP metadata"
                )
            if member.file_size > config.study.max_archive_uncompressed_bytes:
                raise M8ManifestError(
                    f"{entry_label} archive exceeds the frozen expanded-byte ceiling"
                )
    except M8ManifestError:
        raise
    except (BadZipFile, OSError, ValueError) as exc:
        raise M8ManifestError(
            f"{entry_label} raw archive is not a valid bounded ZIP: {exc}"
        ) from exc


def _zip_extra_field_ids(extra: bytes, *, entry_label: str) -> tuple[int, ...]:
    """Parse bounded central-directory extra fields without opening a ZIP member."""

    field_ids: list[int] = []
    offset = 0
    while offset < len(extra):
        if len(extra) - offset < 4:
            raise M8ManifestError(
                f"{entry_label} archive central-directory extra field is truncated"
            )
        field_id, field_bytes = struct.unpack_from("<HH", extra, offset)
        offset += 4
        if field_bytes > len(extra) - offset:
            raise M8ManifestError(
                f"{entry_label} archive central-directory extra field exceeds its bounds"
            )
        field_ids.append(field_id)
        offset += field_bytes
    return tuple(field_ids)


def _preflight_zip_central_directory(
    path: Path,
    *,
    entry_label: str,
    expected_member: str,
    expected_uncompressed_bytes: int,
    max_uncompressed_bytes: int,
) -> None:
    """Bound and authenticate the one-entry ZIP directory before ``ZipFile`` parses it.

    Only the EOCD record and the declared central-directory bytes are read.  In
    particular, this preflight never seeks to or decompresses the CSV member.
    ZIP64 is deliberately unsupported because the frozen daily-archive limits
    fit in the classic ZIP fields and accepting ZIP64 would add an unnecessary
    parser surface before the economic-data boundary.
    """

    try:
        size = path.stat().st_size
        if size < _ZIP_EOCD_STRUCT.size:
            raise M8ManifestError(f"{entry_label} archive is too small to contain a ZIP EOCD")
        tail_bytes = min(size, _MAX_ZIP_TAIL_BYTES)
        with path.open("rb") as source:
            source.seek(size - tail_bytes)
            tail = source.read(tail_bytes)
        relative_eocd = tail.rfind(_ZIP_EOCD_SIGNATURE)
        if relative_eocd < 0 or len(tail) - relative_eocd < _ZIP_EOCD_STRUCT.size:
            raise M8ManifestError(f"{entry_label} archive ZIP EOCD is missing or truncated")
        (
            _signature,
            disk_number,
            central_disk,
            entries_on_disk,
            entries_total,
            central_bytes,
            central_offset,
            comment_bytes,
        ) = _ZIP_EOCD_STRUCT.unpack_from(tail, relative_eocd)
        eocd_offset = size - tail_bytes + relative_eocd
        if eocd_offset + _ZIP_EOCD_STRUCT.size + comment_bytes != size:
            raise M8ManifestError(
                f"{entry_label} archive ZIP EOCD has trailing or malformed bounds"
            )
        if (
            disk_number == _ZIP16_SENTINEL
            or central_disk == _ZIP16_SENTINEL
            or entries_on_disk == _ZIP16_SENTINEL
            or entries_total == _ZIP16_SENTINEL
            or central_bytes == _ZIP32_SENTINEL
            or central_offset == _ZIP32_SENTINEL
        ):
            raise M8ManifestError(f"{entry_label} archive ZIP64 is not permitted")
        if disk_number != 0 or central_disk != 0:
            raise M8ManifestError(f"{entry_label} archive must be a single-disk ZIP")
        if entries_on_disk != 1 or entries_total != 1:
            raise M8ManifestError(
                f"{entry_label} archive must contain exactly one central-directory member"
            )
        if central_bytes < _ZIP_CENTRAL_STRUCT.size:
            raise M8ManifestError(
                f"{entry_label} archive central directory is smaller than one entry"
            )
        if central_bytes > _MAX_ZIP_DIRECTORY_BYTES:
            raise M8ManifestError(
                f"{entry_label} archive central directory exceeds its metadata byte ceiling"
            )
        if central_offset + central_bytes != eocd_offset:
            raise M8ManifestError(f"{entry_label} archive central-directory bounds are malformed")

        with path.open("rb") as source:
            source.seek(central_offset)
            directory = source.read(central_bytes)
        if len(directory) != central_bytes:
            raise M8ManifestError(f"{entry_label} archive central directory is truncated")
        (
            signature,
            _version_made,
            _version_needed,
            flags,
            compression,
            _modified_time,
            _modified_date,
            _crc32,
            compressed_bytes,
            uncompressed_bytes,
            filename_bytes,
            extra_bytes,
            member_comment_bytes,
            member_disk,
            _internal_attributes,
            _external_attributes,
            local_header_offset,
        ) = _ZIP_CENTRAL_STRUCT.unpack_from(directory)
        if signature != _ZIP_CENTRAL_SIGNATURE:
            raise M8ManifestError(f"{entry_label} archive central-directory signature is invalid")
        if (
            compressed_bytes == _ZIP32_SENTINEL
            or uncompressed_bytes == _ZIP32_SENTINEL
            or local_header_offset == _ZIP32_SENTINEL
            or member_disk == _ZIP16_SENTINEL
        ):
            raise M8ManifestError(f"{entry_label} archive ZIP64 entry is not permitted")
        if member_disk != 0:
            raise M8ManifestError(f"{entry_label} archive member starts on another disk")
        declared_entry_bytes = (
            _ZIP_CENTRAL_STRUCT.size + filename_bytes + extra_bytes + member_comment_bytes
        )
        if declared_entry_bytes != central_bytes:
            raise M8ManifestError(
                f"{entry_label} archive central directory does not contain exactly one entry"
            )
        filename_start = _ZIP_CENTRAL_STRUCT.size
        filename_end = filename_start + filename_bytes
        extra_end = filename_end + extra_bytes
        filename = directory[filename_start:filename_end]
        extra = directory[filename_end:extra_end]
        try:
            decoded_filename = filename.decode("ascii")
        except UnicodeDecodeError as exc:
            raise M8ManifestError(
                f"{entry_label} archive central member name is not ASCII"
            ) from exc
        if decoded_filename != expected_member:
            raise M8ManifestError(
                f"{entry_label} archive central member is not the frozen daily CSV"
            )
        if _ZIP64_EXTRA_FIELD_ID in _zip_extra_field_ids(extra, entry_label=entry_label):
            raise M8ManifestError(f"{entry_label} archive ZIP64 extra field is not permitted")
        if flags & 0x1:
            raise M8ManifestError(f"{entry_label} archive member must not be encrypted")
        if compression not in {0, 8}:
            raise M8ManifestError(f"{entry_label} archive member uses unsupported compression")
        if local_header_offset >= central_offset:
            raise M8ManifestError(
                f"{entry_label} archive local-header offset is outside the payload region"
            )
        if uncompressed_bytes != expected_uncompressed_bytes:
            raise M8ManifestError(
                f"{entry_label} expanded-byte claim does not match ZIP central metadata"
            )
        if uncompressed_bytes < 1 or uncompressed_bytes > max_uncompressed_bytes:
            raise M8ManifestError(f"{entry_label} archive exceeds the frozen expanded-byte ceiling")
    except M8ManifestError:
        raise
    except (OSError, struct.error, ValueError) as exc:
        raise M8ManifestError(
            f"{entry_label} raw archive has invalid bounded ZIP metadata: {exc}"
        ) from exc


def _verify_quality(
    *,
    entry_label: str,
    rows: int,
    quality: Mapping[str, Any],
    root: Path,
) -> tuple[Path, str, int, Path, str, int, int, int]:
    _exact_keys(quality, _QUALITY_KEYS, f"{entry_label}.quality")
    report_sha = _digest(quality["report_sha256"], f"{entry_label} quality report SHA")
    report_bytes = _integer(
        quality["report_bytes"], f"{entry_label} quality report bytes", minimum=1
    )
    report_path = _declared_file(root, quality["report_path"], f"{entry_label} quality report")
    _verify_file(report_path, report_sha, report_bytes, f"{entry_label} quality report")
    findings_sha = _digest(quality["findings_sha256"], f"{entry_label} quality findings SHA")
    findings_bytes = _integer(
        quality["findings_bytes"], f"{entry_label} quality findings bytes", minimum=0
    )
    findings_path = _declared_file(
        root, quality["findings_path"], f"{entry_label} quality findings"
    )
    _verify_file(findings_path, findings_sha, findings_bytes, f"{entry_label} quality findings")
    errors = _integer(quality["errors"], f"{entry_label} quality errors", minimum=0)
    warnings = _integer(quality["warnings"], f"{entry_label} quality warnings", minimum=0)
    if errors != 0 or warnings != 0:
        raise M8ManifestError(f"{entry_label} has quality findings and is not complete evidence")
    if findings_bytes != 0:
        raise M8ManifestError(f"{entry_label} zero-finding JSONL must be empty")

    report = _read_json_object(report_path, f"{entry_label} quality report")
    if report.get("dataset") != "trades":
        raise M8ManifestError(f"{entry_label} quality report dataset is not trades")
    if _integer(report.get("rows_checked"), f"{entry_label} rows checked", minimum=1) != rows:
        raise M8ManifestError(f"{entry_label} quality report row count does not match")
    summary = _object(report.get("summary"), f"{entry_label} quality summary")
    if _integer(summary.get("errors"), f"{entry_label} report errors", minimum=0) != errors:
        raise M8ManifestError(f"{entry_label} quality error count does not match")
    if _integer(summary.get("warnings"), f"{entry_label} report warnings", minimum=0) != warnings:
        raise M8ManifestError(f"{entry_label} quality warning count does not match")
    retained = _array(report.get("findings"), f"{entry_label} retained findings")
    if retained:
        raise M8ManifestError(f"{entry_label} zero-finding quality report retained findings")
    if report.get("mutation_policy") != "observations were not changed or repaired":
        raise M8ManifestError(f"{entry_label} quality report mutation policy is invalid")
    return (
        report_path,
        report_sha,
        report_bytes,
        findings_path,
        findings_sha,
        findings_bytes,
        errors,
        warnings,
    )


def _verify_part(
    *,
    config: M8StudyConfig,
    entry_label: str,
    part_index: int,
    part_value: object,
    dataset_artifact_value: object,
    root: Path,
    dataset_root: Path,
    symbol: str,
    day: Date,
    day_range: tuple[int, int],
    source_uri: str,
    raw_sha256: str,
) -> M8NormalizedPart:
    label = f"{entry_label} normalized part {part_index}"
    part = _object(part_value, label)
    _exact_keys(part, _PART_KEYS, label)
    dataset_artifact = _object(dataset_artifact_value, f"{label} dataset descriptor")
    _exact_keys(dataset_artifact, _DATASET_PART_KEYS, f"{label} dataset descriptor")

    ordinal = _integer(part["write_ordinal"], f"{label}.write_ordinal", minimum=0)
    if (
        ordinal != part_index
        or _integer(
            dataset_artifact["write_ordinal"],
            f"{label} dataset write ordinal",
            minimum=0,
        )
        != ordinal
    ):
        raise M8ManifestError(f"{label} write ordinals are not contiguous and ordered")
    rows = _integer(part["rows"], f"{label}.rows", minimum=1)
    if _integer(dataset_artifact["rows"], f"{label} dataset rows", minimum=1) != rows:
        raise M8ManifestError(f"{label} dataset row count does not match")

    data_sha = _digest(part["data_sha256"], f"{label} data SHA")
    sidecar_sha = _digest(part["sidecar_sha256"], f"{label} sidecar SHA")
    if _digest(dataset_artifact["data_sha256"], f"{label} dataset data SHA") != data_sha:
        raise M8ManifestError(f"{label} dataset data checksum does not match")
    if _digest(dataset_artifact["manifest_sha256"], f"{label} dataset sidecar SHA") != sidecar_sha:
        raise M8ManifestError(f"{label} dataset sidecar checksum does not match")

    data_bytes = _integer(part["data_bytes"], f"{label} data bytes", minimum=1)
    sidecar_bytes = _integer(part["sidecar_bytes"], f"{label} sidecar bytes", minimum=1)
    data_path = _declared_file(root, part["data_path"], f"{label} data")
    sidecar_path = _declared_file(root, part["sidecar_path"], f"{label} sidecar")
    nested_data_path = _declared_file(
        dataset_root,
        dataset_artifact["data_path"],
        f"{label} dataset data path",
    )
    nested_sidecar_path = _declared_file(
        dataset_root,
        dataset_artifact["manifest_path"],
        f"{label} dataset sidecar path",
    )
    if data_path != nested_data_path or sidecar_path != nested_sidecar_path:
        raise M8ManifestError(f"{label} explicit paths do not match the dataset manifest")
    _verify_file(data_path, data_sha, data_bytes, f"{label} data")
    _verify_file(sidecar_path, sidecar_sha, sidecar_bytes, f"{label} sidecar")

    observed = _observed_range(part["observed_range_ns"], f"{label} observed range")
    dataset_observed = _observed_range(
        dataset_artifact["observed_range_ns"], f"{label} dataset observed range"
    )
    if observed != dataset_observed:
        raise M8ManifestError(f"{label} observed ranges do not match")
    _verify_day_range(observed, day_range, f"{label} observed range")

    sidecar = _read_json_object(sidecar_path, f"{label} sidecar")
    _exact_keys(sidecar, _PART_SIDECAR_KEYS, f"{label} sidecar")
    expected_claims: dict[str, object] = {
        "manifest_version": _STORAGE_MANIFEST_VERSION,
        "artifact_kind": "normalized_parquet",
        "dataset": "trades",
        "schema_name": "trades",
        "schema_version": SCHEMA_VERSION,
        "venue": "binance_spot",
        "symbol": symbol,
        "partition_date": day.isoformat(),
        "write_ordinal": ordinal,
        "source": config.study.source,
        "source_uri": source_uri,
        "source_checksum_sha256": raw_sha256,
        "rows": rows,
        "bytes": data_bytes,
    }
    for key, expected in expected_claims.items():
        if sidecar[key] != expected:
            raise M8ManifestError(f"{label} sidecar {key} claim does not match")
    if _range(sidecar["requested_range_ns"], f"{label} sidecar requested range") != day_range:
        raise M8ManifestError(f"{label} sidecar requested range does not match")
    if _observed_range(sidecar["observed_range_ns"], f"{label} sidecar observed") != observed:
        raise M8ManifestError(f"{label} sidecar observed range does not match")
    checksum = _object(sidecar["checksum"], f"{label} sidecar checksum")
    if (
        checksum.get("algorithm") != "sha256"
        or _digest(checksum.get("value"), f"{label} sidecar checksum value") != data_sha
    ):
        raise M8ManifestError(f"{label} sidecar data checksum does not match")
    expected_nested_data = _text(dataset_artifact["data_path"], f"{label} nested data path")
    if sidecar["path"] != expected_nested_data:
        raise M8ManifestError(f"{label} sidecar path claim does not match")

    try:
        parquet = pq.ParquetFile(data_path)
        if parquet.metadata.num_rows != rows:
            raise M8ManifestError(f"{label} Parquet footer row count does not match")
        if not parquet.schema_arrow.equals(get_schema("trades"), check_metadata=True):
            raise M8ManifestError(f"{label} Parquet schema is not normalized trades")
    except M8ManifestError:
        raise
    except (OSError, pa.ArrowException, ValueError) as exc:
        raise M8ManifestError(f"cannot inspect {label} Parquet footer: {exc}") from exc

    return M8NormalizedPart(
        data_path=data_path,
        data_sha256=data_sha,
        data_bytes=data_bytes,
        sidecar_path=sidecar_path,
        sidecar_sha256=sidecar_sha,
        sidecar_bytes=sidecar_bytes,
        rows=rows,
        write_ordinal=ordinal,
        observed_start_ns=observed[0],
        observed_end_inclusive_ns=observed[1],
    )


def _verify_normalized(
    *,
    config: M8StudyConfig,
    entry_label: str,
    normalized: Mapping[str, Any],
    root: Path,
    symbol: str,
    day: Date,
    day_range: tuple[int, int],
    entry_rows: int,
    entry_observed: tuple[int, int],
    source_uri: str,
    raw_sha256: str,
) -> tuple[Path, str, int, tuple[M8NormalizedPart, ...]]:
    _exact_keys(normalized, _NORMALIZED_KEYS, f"{entry_label}.normalized")
    dataset_sha = _digest(
        normalized["dataset_manifest_sha256"], f"{entry_label} dataset manifest SHA"
    )
    dataset_bytes = _integer(
        normalized["dataset_manifest_bytes"],
        f"{entry_label} dataset manifest bytes",
        minimum=1,
    )
    dataset_path = _declared_file(
        root,
        normalized["dataset_manifest_path"],
        f"{entry_label} normalized dataset manifest",
    )
    _verify_file(
        dataset_path,
        dataset_sha,
        dataset_bytes,
        f"{entry_label} normalized dataset manifest",
    )
    if dataset_path.parent.name != "_manifests":
        raise M8ManifestError(f"{entry_label} dataset manifest is not under _manifests")
    dataset_root = dataset_path.parent.parent.resolve()
    if not dataset_root.is_relative_to(root.resolve()):
        raise M8ManifestError(f"{entry_label} dataset root escapes the M8 input root")

    declared_rows = _integer(normalized["rows"], f"{entry_label} normalized rows", minimum=1)
    if declared_rows != entry_rows:
        raise M8ManifestError(f"{entry_label} normalized row count does not match")
    top_parts = _array(normalized["parts"], f"{entry_label} normalized parts")
    if not top_parts:
        raise M8ManifestError(f"{entry_label} must declare at least one normalized part")

    dataset = _read_json_object(dataset_path, f"{entry_label} dataset manifest")
    _exact_keys(dataset, _DATASET_KEYS, f"{entry_label} dataset manifest")
    expected_claims: dict[str, object] = {
        "manifest_version": _STORAGE_MANIFEST_VERSION,
        "dataset": "trades",
        "schema_version": SCHEMA_VERSION,
        "source": config.study.source,
        "source_uri": source_uri,
        "rows": entry_rows,
    }
    for key, expected in expected_claims.items():
        if dataset[key] != expected:
            raise M8ManifestError(f"{entry_label} dataset manifest {key} claim does not match")
    if _range(dataset["requested_range_ns"], f"{entry_label} dataset requested range") != day_range:
        raise M8ManifestError(f"{entry_label} dataset requested range does not match")
    dataset_parts = _array(dataset["artifacts"], f"{entry_label} dataset artifacts")
    if len(dataset_parts) != len(top_parts):
        raise M8ManifestError(f"{entry_label} dataset part count does not match")

    parts = tuple(
        _verify_part(
            config=config,
            entry_label=entry_label,
            part_index=index,
            part_value=top_part,
            dataset_artifact_value=dataset_part,
            root=root,
            dataset_root=dataset_root,
            symbol=symbol,
            day=day,
            day_range=day_range,
            source_uri=source_uri,
            raw_sha256=raw_sha256,
        )
        for index, (top_part, dataset_part) in enumerate(zip(top_parts, dataset_parts, strict=True))
    )
    if len({part.data_path for part in parts}) != len(parts):
        raise M8ManifestError(f"{entry_label} contains duplicate normalized part paths")
    if len({part.sidecar_path for part in parts}) != len(parts):
        raise M8ManifestError(f"{entry_label} contains duplicate normalized sidecar paths")
    if sum(part.rows for part in parts) != entry_rows:
        raise M8ManifestError(f"{entry_label} normalized part rows do not sum to entry rows")
    aggregate_observed = (
        min(part.observed_start_ns for part in parts),
        max(part.observed_end_inclusive_ns for part in parts),
    )
    if aggregate_observed != entry_observed:
        raise M8ManifestError(f"{entry_label} normalized part bounds do not match entry bounds")
    return dataset_path, dataset_sha, dataset_bytes, parts


def _verify_entry(
    config: M8StudyConfig,
    root: Path,
    value: object,
    expected: tuple[str, Date, M8PeriodRole],
    symbol_metadata: M8SymbolMetadata,
    index: int,
) -> M8ArchiveEntry:
    label = f"entries[{index}]"
    entry = _object(value, label)
    _exact_keys(entry, _ENTRY_KEYS, label)
    symbol = _text(entry["symbol"], f"{label}.symbol")
    day = _parse_date(entry["date"], f"{label}.date")
    role = _parse_role(entry["role"], f"{label}.role")
    if (symbol, day, role) != expected:
        raise M8ManifestError(
            f"{label} is out of frozen order or has an unexpected symbol/date/role"
        )
    if not _boolean(entry["complete"], f"{label}.complete"):
        raise M8ManifestError(f"{label} is not a complete full-day archive")
    day_range = _day_bounds_ns(day)
    if _range(entry["requested_range_ns"], f"{label}.requested_range_ns") != day_range:
        raise M8ManifestError(f"{label} does not claim the exact full UTC day")

    rows = _integer(entry["rows"], f"{label}.rows", minimum=1)
    trade_ids = _object(entry["trade_id_range"], f"{label}.trade_id_range")
    _exact_keys(trade_ids, _TRADE_ID_KEYS, f"{label}.trade_id_range")
    first_trade_id = _integer(trade_ids["first"], f"{label}.first_trade_id", minimum=0)
    last_trade_id = _integer(trade_ids["last"], f"{label}.last_trade_id", minimum=0)
    contiguous_count = _integer(
        trade_ids["contiguous_count"], f"{label}.contiguous_count", minimum=1
    )
    if contiguous_count != rows or last_trade_id - first_trade_id + 1 != rows:
        raise M8ManifestError(f"{label} aggregate-trade IDs are not a contiguous row count")

    observed = _observed_range(entry["observed_range_ns"], f"{label}.observed_range_ns")
    _verify_day_range(observed, day_range, f"{label} observed event bounds")
    scales = _object(entry["scales"], f"{label}.scales")
    _exact_keys(scales, _SCALE_KEYS, f"{label}.scales")
    tick_size = _decimal(scales["tick_size"], f"{label}.tick_size")
    lot_size = _decimal(scales["lot_size"], f"{label}.lot_size")
    if (
        symbol_metadata.symbol != symbol
        or tick_size != symbol_metadata.tick_size
        or lot_size != symbol_metadata.lot_size
    ):
        raise M8ManifestError(
            f"{label} scales do not match verified exchangeInfo metadata for {symbol}"
        )

    raw = _object(entry["raw"], f"{label}.raw")
    _exact_keys(raw, _RAW_KEYS, f"{label}.raw")
    raw_zip_path = _declared_file(root, raw["zip_path"], f"{label} raw ZIP")
    raw_zip_sha = _digest(raw["zip_sha256"], f"{label} raw ZIP SHA")
    raw_zip_bytes = _integer(raw["zip_bytes"], f"{label} raw ZIP bytes", minimum=1)
    raw_uncompressed_bytes = _integer(
        raw["uncompressed_bytes"], f"{label} raw expanded bytes", minimum=1
    )
    if raw_zip_bytes > config.study.max_archive_compressed_bytes:
        raise M8ManifestError(f"{label} raw ZIP exceeds the frozen compressed-byte ceiling")
    if raw_uncompressed_bytes > config.study.max_archive_uncompressed_bytes:
        raise M8ManifestError(f"{label} archive exceeds the frozen expanded-byte ceiling")
    _verify_file(raw_zip_path, raw_zip_sha, raw_zip_bytes, f"{label} raw ZIP")
    _verify_zip_archive(
        config=config,
        entry_label=label,
        symbol=symbol,
        day=day,
        zip_path=raw_zip_path,
        uncompressed_bytes=raw_uncompressed_bytes,
    )
    source_uri = _text(raw["source_uri"], f"{label}.source_uri")
    raw_sidecar_path = _declared_file(
        root, raw["source_manifest_path"], f"{label} raw source sidecar"
    )
    raw_sidecar_sha = _digest(raw["source_manifest_sha256"], f"{label} raw source sidecar SHA")
    raw_sidecar_bytes = _integer(
        raw["source_manifest_bytes"], f"{label} raw source sidecar bytes", minimum=1
    )
    _verify_file(
        raw_sidecar_path,
        raw_sidecar_sha,
        raw_sidecar_bytes,
        f"{label} raw source sidecar",
    )
    _verify_raw_source_sidecar(
        config=config,
        entry_label=label,
        day_range=day_range,
        symbol=symbol,
        day=day,
        zip_path=raw_zip_path,
        zip_sha256=raw_zip_sha,
        zip_bytes=raw_zip_bytes,
        source_uri=source_uri,
        sidecar_path=raw_sidecar_path,
    )
    (
        raw_checksum_path,
        raw_checksum_sha,
        raw_checksum_bytes,
        raw_checksum_source_uri,
        raw_checksum_sidecar_path,
        raw_checksum_sidecar_sha,
        raw_checksum_sidecar_bytes,
    ) = _verify_official_checksum(
        entry_label=label,
        root=root,
        value=raw["checksum"],
        symbol=symbol,
        day=day,
        day_range=day_range,
        zip_path=raw_zip_path,
        zip_sha256=raw_zip_sha,
        archive_source_uri=source_uri,
        archive_sidecar_path=raw_sidecar_path,
    )

    normalized = _object(entry["normalized"], f"{label}.normalized")
    dataset_path, dataset_sha, dataset_bytes, parts = _verify_normalized(
        config=config,
        entry_label=label,
        normalized=normalized,
        root=root,
        symbol=symbol,
        day=day,
        day_range=day_range,
        entry_rows=rows,
        entry_observed=observed,
        source_uri=source_uri,
        raw_sha256=raw_zip_sha,
    )
    quality = _object(entry["quality"], f"{label}.quality")
    (
        quality_report_path,
        quality_report_sha,
        quality_report_bytes,
        quality_findings_path,
        quality_findings_sha,
        quality_findings_bytes,
        quality_errors,
        quality_warnings,
    ) = _verify_quality(entry_label=label, rows=rows, quality=quality, root=root)

    return M8ArchiveEntry(
        symbol=symbol,
        date=day,
        role=role,
        complete=True,
        rows=rows,
        first_trade_id=first_trade_id,
        last_trade_id=last_trade_id,
        observed_start_ns=observed[0],
        observed_end_inclusive_ns=observed[1],
        tick_size=tick_size,
        lot_size=lot_size,
        raw_zip_path=raw_zip_path,
        raw_zip_sha256=raw_zip_sha,
        raw_zip_bytes=raw_zip_bytes,
        raw_uncompressed_bytes=raw_uncompressed_bytes,
        raw_source_uri=source_uri,
        raw_source_manifest_path=raw_sidecar_path,
        raw_source_manifest_sha256=raw_sidecar_sha,
        raw_source_manifest_bytes=raw_sidecar_bytes,
        raw_checksum_path=raw_checksum_path,
        raw_checksum_sha256=raw_checksum_sha,
        raw_checksum_bytes=raw_checksum_bytes,
        raw_checksum_source_uri=raw_checksum_source_uri,
        raw_checksum_source_manifest_path=raw_checksum_sidecar_path,
        raw_checksum_source_manifest_sha256=raw_checksum_sidecar_sha,
        raw_checksum_source_manifest_bytes=raw_checksum_sidecar_bytes,
        normalized_dataset_manifest_path=dataset_path,
        normalized_dataset_manifest_sha256=dataset_sha,
        normalized_dataset_manifest_bytes=dataset_bytes,
        normalized_parts=parts,
        quality_report_path=quality_report_path,
        quality_report_sha256=quality_report_sha,
        quality_report_bytes=quality_report_bytes,
        quality_findings_path=quality_findings_path,
        quality_findings_sha256=quality_findings_sha,
        quality_findings_bytes=quality_findings_bytes,
        quality_errors=quality_errors,
        quality_warnings=quality_warnings,
    )


def _verify_payload(
    config: M8StudyConfig, root: Path, payload: Mapping[str, Any]
) -> tuple[tuple[M8SymbolMetadata, ...], tuple[M8ArchiveEntry, ...]]:
    _exact_keys(payload, _TOP_KEYS, "M8 input manifest")
    if payload["manifest_version"] != M8_INPUT_MANIFEST_VERSION:
        raise M8ManifestError("unsupported M8 input manifest version")
    if payload["artifact_kind"] != "m8_multidate_trade_input":
        raise M8ManifestError("unexpected M8 input manifest artifact kind")

    config_claim = _object(payload["config"], "M8 input manifest config")
    _exact_keys(config_claim, _CONFIG_KEYS, "M8 input manifest config")
    expected_config = {
        "semantic_sha256": config.hash,
        "source_sha256": config.source_sha256,
        "protocol_version": config.study.protocol_version,
    }
    if dict(config_claim) != expected_config:
        raise M8ManifestError("M8 input manifest is bound to a different frozen configuration")

    study = _object(payload["study"], "M8 input manifest study")
    _exact_keys(study, _STUDY_KEYS, "M8 input manifest study")
    expected_periods = [
        {"date": period.date.isoformat(), "role": period.role} for period in config.periods
    ]
    observed_periods = _array(study["periods"], "M8 input manifest study periods")
    for index, raw_period in enumerate(observed_periods):
        period = _object(raw_period, f"M8 input manifest study periods[{index}]")
        _exact_keys(period, _STUDY_PERIOD_KEYS, f"M8 input manifest study periods[{index}]")
    expected_study: dict[str, object] = {
        "name": config.study.name,
        "evidence_tier": config.study.evidence_tier,
        "source": config.study.source,
        "symbols": list(config.study.symbols),
        "periods": expected_periods,
    }
    if dict(study) != expected_study:
        raise M8ManifestError("M8 input manifest study scope differs from the frozen protocol")

    raw_symbol_metadata = _array(payload["symbol_metadata"], "M8 input manifest symbol metadata")
    if len(raw_symbol_metadata) != len(config.study.symbols) or len(raw_symbol_metadata) != 2:
        raise M8ManifestError("M8 input manifest must contain exactly two symbol metadata entries")
    symbol_metadata = tuple(
        _verify_symbol_metadata(root, value, expected_symbol, index)
        for index, (value, expected_symbol) in enumerate(
            zip(raw_symbol_metadata, config.study.symbols, strict=True)
        )
    )
    if len({metadata.symbol for metadata in symbol_metadata}) != len(symbol_metadata):
        raise M8ManifestError("M8 input manifest contains duplicate symbol metadata")
    if len({metadata.raw_path for metadata in symbol_metadata}) != len(symbol_metadata):
        raise M8ManifestError("M8 input manifest reuses an exchangeInfo raw body")
    if len({metadata.source_manifest_path for metadata in symbol_metadata}) != len(symbol_metadata):
        raise M8ManifestError("M8 input manifest reuses an exchangeInfo source sidecar")
    total_symbol_metadata_bytes = _integer(
        payload["total_symbol_metadata_bytes"],
        "M8 total symbol metadata bytes",
        minimum=1,
    )
    observed_metadata_total = sum(metadata.raw_bytes for metadata in symbol_metadata)
    if total_symbol_metadata_bytes != observed_metadata_total:
        raise M8ManifestError("M8 total symbol metadata byte claim does not match")
    if total_symbol_metadata_bytes > len(symbol_metadata) * _MAX_SYMBOL_METADATA_BYTES:
        raise M8ManifestError("M8 symbol metadata exceeds its bounded total byte ceiling")
    metadata_by_symbol = {metadata.symbol: metadata for metadata in symbol_metadata}

    raw_entries = _array(payload["entries"], "M8 input manifest entries")
    expected_order = _expected_entry_order(config)
    if len(raw_entries) != len(expected_order) or len(raw_entries) != 8:
        raise M8ManifestError("M8 input manifest must contain exactly eight entries")
    entries = tuple(
        _verify_entry(
            config,
            root,
            value,
            expected,
            metadata_by_symbol[expected[0]],
            index,
        )
        for index, (value, expected) in enumerate(zip(raw_entries, expected_order, strict=True))
    )
    if len({(entry.symbol, entry.date) for entry in entries}) != len(entries):
        raise M8ManifestError("M8 input manifest contains duplicate symbol/date entries")
    total_raw_zip_bytes = _integer(
        payload["total_raw_zip_bytes"], "M8 total raw ZIP bytes", minimum=1
    )
    observed_total = sum(entry.raw_zip_bytes for entry in entries)
    if total_raw_zip_bytes != observed_total:
        raise M8ManifestError("M8 total raw ZIP byte claim does not match its entries")
    if total_raw_zip_bytes > config.study.max_total_download_bytes:
        raise M8ManifestError("M8 raw archives exceed the frozen total-download ceiling")
    return symbol_metadata, entries


def _encoded_manifest(payload: Mapping[str, Any]) -> bytes:
    return (
        json.dumps(payload, indent=2, sort_keys=True, allow_nan=False, ensure_ascii=False) + "\n"
    ).encode("utf-8")


def write_m8_input_manifest(
    config: M8StudyConfig,
    root: str | Path,
    entries: Sequence[M8ArchiveEntry],
    symbol_metadata: Sequence[M8SymbolMetadata],
    *,
    output_dir: str | Path | None = None,
) -> M8InputManifest:
    """Validate and atomically publish one deterministic content-addressed manifest."""

    input_root = Path(root).resolve()
    if not input_root.is_dir():
        raise M8ManifestError(f"M8 input root does not exist: {input_root}")
    payload = _manifest_payload(config, input_root, entries, symbol_metadata)
    _verify_payload(config, input_root, payload)
    encoded = _encoded_manifest(payload)
    digest = hashlib.sha256(encoded).hexdigest()
    directory = (
        (input_root / "_manifests").resolve()
        if output_dir is None
        else (
            Path(output_dir) if Path(output_dir).is_absolute() else input_root / output_dir
        ).resolve()
    )
    if not directory.is_relative_to(input_root):
        raise M8ManifestError("M8 manifest output directory escapes the input root")
    directory.mkdir(parents=True, exist_ok=True)
    destination = directory / f"m8-input.manifest-{digest[:20]}.json"
    if destination.exists():
        if destination.read_bytes() != encoded:
            raise M8ManifestError(f"immutable M8 manifest collision at {destination}")
    else:
        descriptor, temporary_name = tempfile.mkstemp(
            dir=directory,
            prefix=f".{destination.name}.",
            suffix=".tmp",
        )
        temporary = Path(temporary_name)
        try:
            with os.fdopen(descriptor, "wb") as handle:
                handle.write(encoded)
                handle.flush()
                os.fsync(handle.fileno())
            os.replace(temporary, destination)
        except BaseException:
            temporary.unlink(missing_ok=True)
            raise
    return read_m8_input_manifest(
        config,
        input_root,
        destination,
        manifest_sha256=digest,
    )


def read_m8_input_manifest(
    config: M8StudyConfig,
    root: str | Path,
    manifest_path: str | Path,
    *,
    manifest_sha256: str,
) -> M8InputManifest:
    """Read and fully verify a manifest and every declared evidence artifact."""

    input_root = Path(root).resolve()
    if not input_root.is_dir():
        raise M8ManifestError(f"M8 input root does not exist: {input_root}")
    expected_sha = _digest(manifest_sha256, "M8 input manifest SHA")
    path = _resolve_contained_file(input_root, Path(manifest_path), "M8 input manifest")
    observed_sha = sha256_file(path)
    if observed_sha != expected_sha:
        raise M8ManifestError(
            f"M8 input manifest SHA-256 mismatch: expected {expected_sha}, observed {observed_sha}"
        )
    expected_name = f"m8-input.manifest-{expected_sha[:20]}.json"
    if path.name != expected_name:
        raise M8ManifestError("M8 input manifest filename is not content-addressed")
    payload = _read_json_object(path, "M8 input manifest")
    symbol_metadata, entries = _verify_payload(config, input_root, payload)
    return M8InputManifest(
        root=input_root,
        path=path,
        sha256=expected_sha,
        config_sha256=config.hash,
        config_source_sha256=config.source_sha256,
        protocol_version=config.study.protocol_version,
        symbol_metadata=symbol_metadata,
        entries=entries,
    )


def verify_m8_input_manifest(
    config: M8StudyConfig,
    root: str | Path,
    manifest_path: str | Path,
    *,
    manifest_sha256: str,
) -> M8InputManifest:
    """Alias with an explicit verification name for CLI/pipeline call sites."""

    return read_m8_input_manifest(
        config,
        root,
        manifest_path,
        manifest_sha256=manifest_sha256,
    )


__all__ = [
    "M8_INPUT_MANIFEST_VERSION",
    "M8ArchiveEntry",
    "M8InputManifest",
    "M8ManifestError",
    "M8NormalizedPart",
    "M8SymbolMetadata",
    "read_m8_input_manifest",
    "verify_m8_input_manifest",
    "write_m8_input_manifest",
]