File size: 90,938 Bytes
a7b634e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
"""Behavioral tests for PostTrainBench reproduction (genuine RED).

Each test exercises real code paths with small deterministic fake HTTP
clients and monkeypatched oracle constants.  No ``inspect.getsource``,
no string searches, and no arity-error substitutes.

Categories 1-12 from worker-task.md.
"""

from __future__ import annotations

import copy
import hashlib
import json
import os
import re
import shutil
from pathlib import Path
from typing import Any
from unittest.mock import patch

import pytest

import posttrainbench_repro.constants as C
from posttrainbench_repro.acquisition import (
    _parse_link_next,
    _validate_next_url,
    acquire_all,
    compute_canonical_path_digest,
    compute_canonical_tree_digest,
    extract_trace_excerpts,
    fetch_allowlisted_file,
    fetch_github_metadata,
    fetch_hf_path_inventory,
    fetch_hf_tree_pages,
)
from posttrainbench_repro.audit import (
    audit_protocol,
    audit_reward_hacking,
    compute_coverage,
    evaluate_claims,
)
from posttrainbench_repro.pipeline import PROJECT_ROOT, generate_evidence, run_pipeline

SUBMISSION_DIR = Path(__file__).parent.parent

# ---------------------------------------------------------------------------
# Helpers: tiny fake HTTP transport
# ---------------------------------------------------------------------------


class FakeResponse:
    """Minimal httpx.Response replacement for tests."""

    def __init__(
        self,
        status_code: int = 200,
        json_data: Any = None,
        content: bytes = b"",
        headers: dict[str, str] | None = None,
    ):
        self.status_code = status_code
        self._json = json_data
        self.content = content
        self.headers = headers or {}

    def raise_for_status(self) -> None:
        if self.status_code >= 400:
            raise RuntimeError(f"HTTP {self.status_code}")

    def json(self) -> Any:
        if self._json is not None:
            return self._json
        return json.loads(self.content)


class RecordingClient:
    """Fake httpx.Client that records calls and replays canned responses."""

    def __init__(self) -> None:
        self.calls: list[str] = []
        self.responses: dict[str, FakeResponse] = {}
        self.prefix_responses: list[tuple[str, FakeResponse]] = []

    def register(self, url: str, resp: FakeResponse) -> None:
        self.responses[url] = resp

    def register_prefix(self, prefix: str, resp: FakeResponse) -> None:
        self.prefix_responses.append((prefix, resp))

    def get(self, url: str, **kwargs: Any) -> FakeResponse:
        self.calls.append(url)
        if url in self.responses:
            return self.responses[url]
        for prefix, resp in self.prefix_responses:
            if url.startswith(prefix):
                return resp
        raise RuntimeError(f"No canned response for {url}")

    def close(self) -> None:
        pass


# ---------------------------------------------------------------------------
# Build a minimal valid GitHub tree for tests
# ---------------------------------------------------------------------------

def _make_entries(count: int = 5) -> list[dict[str, Any]]:
    """Create N minimal tree entries for testing."""
    entries = []
    for i in range(count):
        entries.append({
            "path": f"file{i}.txt",
            "type": "blob",
            "sha": hashlib.sha1(f"file{i}".encode()).hexdigest(),
            "size": 100 + i,
        })
    return entries


def _valid_github_tree_response(
    entries: list[dict[str, Any]] | None = None,
    tree_id: str | None = None,
) -> FakeResponse:
    """Build a canned response for the GitHub tree endpoint."""
    if entries is None:
        entries = _make_entries()
    return FakeResponse(json_data={
        "sha": tree_id or C.GIT_TREE_ID,
        "tree": entries,
        "truncated": False,
    })


def _make_valid_acquired(
    extra_all_paths: list[str] | None = None,
) -> dict[str, Any]:
    """Build a minimal valid acquired dict for offline audit tests."""
    entries: list[dict[str, Any]] = []
    for d in C.EXPECTED_EVAL_DIRS:
        entries.append({"path": d, "type": "tree", "sha": "0" * 40})
    for path, (sha, _) in C.PINNED_BLOBS.items():
        entries.append({"path": path, "type": "blob", "sha": sha, "size": 100})

    blob_contents = {
        "src/commit_utils/single_task.sub": (
            b'num_gpus = 1\n'
            b'request_gpus = $(num_gpus)\n'
            b'requirements = TARGET.CUDADeviceName == "NVIDIA H100 80GB HBM3"\n'
        ),
        "src/run_task.sh": (
            b'#!/bin/bash\n'
            b'NUM_HOURS=${1:-10}\n'
            b'timeout $((NUM_HOURS * 60 + 5))m python run.py\n'
        ),
        "src/commit_utils/commit.sh": _REALISTIC_COMMIT_SH,
        "README.md": b"# PostTrainBench\n",
        "LICENSE": b"MIT License\n",
    }
    blobs = {}
    for path, (sha, raw_sha) in C.PINNED_BLOBS.items():
        raw_url = (
            f"https://raw.githubusercontent.com/{C.GITHUB_REPO}"
            f"/{C.GITHUB_PINNED_COMMIT}/{path}"
        )
        blobs[path] = {
            "git_object": sha,
            "raw_sha256": raw_sha,
            "size": 100,
            "raw_url": raw_url,
            "acquisition_command": f"GET {raw_url}",
        }

    tree_url = (
        f"https://api.github.com/repos/{C.GITHUB_REPO}"
        f"/git/trees/{C.GIT_TREE_ID}?recursive=1"
    )
    github = {
        "commit": C.GITHUB_PINNED_COMMIT,
        "tree_id": C.GIT_TREE_ID,
        "entry_count": len(entries),
        "canonical_tree_digest": C.GIT_TREE_DIGEST,
        "entries": entries,
        "blobs": blobs,
        "blob_contents": blob_contents,
        "tree_acquisition": {
            "url": tree_url,
            "acquisition_command": f"GET {tree_url}",
        },
    }

    # Build matching HF inventory
    hf_inv = _make_valid_hf_inventory(extra_all_paths)

    trace_excerpts = []
    for spec in C.TRACE_EXCERPTS:
        trace_excerpts.append({
            "record": spec["record"],
            "json_pointer": spec["pointer"],
            "text": spec["text"],
            "sha256": hashlib.sha256(str(spec["text"]).encode()).hexdigest(),
        })

    consumed_hf_files = []
    content_metadata = {
        C.CONTAMINATION_WITNESS_PATH: (
            C.CONTAMINATION_WITNESS_SHA256,
            len(C.CONTAMINATION_WITNESS_BYTES),
        ),
        C.TIME_TAKEN_WITNESS_PATH: (
            C.TIME_TAKEN_WITNESS_SHA256,
            len(C.TIME_TAKEN_WITNESS_BYTES),
        ),
        C.INSTRUCTION_MODEL_JUDGMENT_PATH: (
            C.INSTRUCTION_MODEL_JUDGMENT_SHA256,
            C.INSTRUCTION_MODEL_JUDGMENT_SIZE,
        ),
        C.INSTRUCTION_MODEL_TRACE_PATH: (
            C.INSTRUCTION_MODEL_TRACE_SHA256,
            C.INSTRUCTION_MODEL_TRACE_SIZE,
        ),
    }
    for path in sorted(C.HF_ALLOWLISTED_FILES):
        raw_url = (
            f"https://huggingface.co/datasets/{C.HF_DATASET_ID}"
            f"/raw/{C.HF_PINNED_REVISION}/{path}"
        )
        consumed_hf_files.append({
            "path": path,
            "raw_url": raw_url,
            "acquisition_command": f"GET {raw_url}",
            "sha256": content_metadata[path][0],
            "size": content_metadata[path][1],
            "oid": hf_inv["entry_metadata"][path]["oid"],
        })

    return {
        "github": github,
        "hf_inventory": hf_inv,
        "hf_consumed_files": consumed_hf_files,
        "contamination_content": C.CONTAMINATION_WITNESS_BYTES,
        "time_taken_content": C.TIME_TAKEN_WITNESS_BYTES,
        "instruction_judgment_content": C.INSTRUCTION_MODEL_JUDGMENT_BYTES,
        "instruction_trace_sha256": C.INSTRUCTION_MODEL_TRACE_SHA256,
        "instruction_trace_size": C.INSTRUCTION_MODEL_TRACE_SIZE,
        "trace_excerpts": trace_excerpts,
    }


def _make_valid_hf_inventory(
    extra_all_paths: list[str] | None = None,
) -> dict[str, Any]:
    """Build an HF inventory with 1,338 tasks / 47 roots."""
    benchmark_list = sorted(C.EXPECTED_BENCHMARKS)
    model_frags = list(C.EXPECTED_MODEL_FRAGMENTS.keys())
    dir_paths: list[str] = []
    run_roots: list[str] = []
    for i in range(47):
        root = f"agent{i}_10h_run1"
        run_roots.append(root)
        dir_paths.append(root)
    root_cell: set[tuple[str, str, str]] = set()
    task_count = 0
    for bi, bench in enumerate(benchmark_list):
        expected = C.EXPECTED_CELL_COUNTS[bench]
        for mi, frag in enumerate(model_frags):
            for ti in range(expected[mi]):
                root = run_roots[ti % 47]
                jid = 16800000 + bi * 10000 + mi * 1000 + ti
                dir_paths.append(f"{root}/{bench}_{frag}_{jid}")
                root_cell.add((root, bench, C.EXPECTED_MODEL_FRAGMENTS[frag]))
                task_count += 1
    dup_cells = [(benchmark_list[0], model_frags[0]), (benchmark_list[4], model_frags[0])]
    for bench, frag in dup_cells:
        mn = C.EXPECTED_MODEL_FRAGMENTS[frag]
        for i, p in enumerate(list(dir_paths)):
            if p.startswith(f"{run_roots[46]}/{bench}_{frag}_"):
                dir_paths.pop(i)
                jid = 99999000 + dup_cells.index((bench, frag))
                dir_paths.append(f"{run_roots[0]}/{bench}_{frag}_{jid}")
                root_cell.discard((run_roots[46], bench, mn))
                root_cell.add((run_roots[0], bench, mn))
                break
        task_count += 0  # count unchanged since we replaced, not added
    dir_paths.extend([
        "viewer_data",
        "viewer_data/cache",
        "viewer_data/cache/nested",
    ])
    file_paths = [f"{run_roots[0]}/file{i}.txt" for i in range(10)]
    file_paths.extend(
        f"viewer_data/auxiliary-{index:04d}.json"
        for index in range(2397)
    )
    all_paths = dir_paths + file_paths
    if extra_all_paths:
        all_paths.extend(extra_all_paths)
    entry_metadata = {
        path: {
            "type": "file",
            "oid": hashlib.sha1(path.encode("utf-8")).hexdigest(),
            "size": len(path.encode("utf-8")),
        }
        for path in C.HF_ALLOWLISTED_FILES
    }
    entry_metadata[C.CONTAMINATION_WITNESS_PATH]["size"] = len(
        C.CONTAMINATION_WITNESS_BYTES
    )
    entry_metadata[C.TIME_TAKEN_WITNESS_PATH]["size"] = len(
        C.TIME_TAKEN_WITNESS_BYTES
    )
    entry_metadata[C.INSTRUCTION_MODEL_JUDGMENT_PATH].update({
        "oid": C.INSTRUCTION_MODEL_JUDGMENT_GIT_OBJECT,
        "size": C.INSTRUCTION_MODEL_JUDGMENT_SIZE,
    })
    entry_metadata[C.INSTRUCTION_MODEL_TRACE_PATH].update({
        "oid": C.INSTRUCTION_MODEL_TRACE_GIT_OBJECT,
        "size": C.INSTRUCTION_MODEL_TRACE_SIZE,
    })
    tree_url = (
        f"https://huggingface.co/api/datasets/{C.HF_DATASET_ID}"
        f"/tree/{C.HF_PINNED_REVISION}"
        "?recursive=true&expand=false&limit=1000"
    )
    return {
        "revision": C.HF_PINNED_REVISION,
        "page_count": C.HF_TREE_TOTAL_PAGES,
        "total_entries": len(all_paths),
        "file_count": len(file_paths),
        "dir_count": len(dir_paths),
        "all_paths": all_paths,
        "file_paths": file_paths,
        "dir_paths": dir_paths,
        "canonical_all_digest": "mock",
        "canonical_file_digest": "mock",
        "canonical_dir_digest": "mock",
        "entry_metadata": entry_metadata,
        "tree_acquisition": {
            "initial_url": tree_url,
            "acquisition_command": (
                f"GET {tree_url}; follow Link rel=\"next\" until absent"
            ),
        },
    }


# ===================================================================
# 1. GitHub root-tree URL/object verification
# ===================================================================

class TestGitHubTree:
    """fetch_github_tree_entries must request the root tree object, not the commit."""

    def test_requests_tree_object_not_commit(self):
        """The request URL must use GIT_TREE_ID, not GITHUB_PINNED_COMMIT."""
        client = RecordingClient()
        tree_url = (
            f"https://api.github.com/repos/{C.GITHUB_REPO}"
            f"/git/trees/{C.GIT_TREE_ID}?recursive=1"
        )
        entries = _make_entries(C.GIT_TREE_ENTRY_COUNT)
        digest = compute_canonical_tree_digest(entries)
        client.register(tree_url, _valid_github_tree_response(entries))
        for path, (_, raw_sha) in C.PINNED_BLOBS.items():
            raw_url = (
                f"https://raw.githubusercontent.com/{C.GITHUB_REPO}"
                f"/{C.GITHUB_PINNED_COMMIT}/{path}"
            )
            content = b"x" * 10
            actual_sha = hashlib.sha256(content).hexdigest()
            client.register(raw_url, FakeResponse(content=content))
        with patch.object(C, "GIT_TREE_ENTRY_COUNT", len(entries)), \
             patch.object(C, "GIT_TREE_DIGEST", digest), \
             patch("posttrainbench_repro.acquisition.GIT_TREE_ENTRY_COUNT", len(entries)), \
             patch("posttrainbench_repro.acquisition.GIT_TREE_DIGEST", digest):
            # We need to also patch PINNED_BLOBS to match our fake content
            fake_blobs = {}
            for path, (git_sha, _) in C.PINNED_BLOBS.items():
                content = b"x" * 10
                fake_blobs[path] = (git_sha, hashlib.sha256(content).hexdigest())
            # Ensure entries contain the pinned blob paths with correct SHAs
            for path, (git_sha, _) in fake_blobs.items():
                entries.append({"path": path, "type": "blob", "sha": git_sha, "size": 10})
            digest2 = compute_canonical_tree_digest(entries)
            client.responses[tree_url] = _valid_github_tree_response(entries)
            with patch("posttrainbench_repro.acquisition.GIT_TREE_ENTRY_COUNT", len(entries)), \
                 patch("posttrainbench_repro.acquisition.GIT_TREE_DIGEST", digest2), \
                 patch("posttrainbench_repro.acquisition.PINNED_BLOBS", fake_blobs):
                result = fetch_github_metadata(client)
        assert tree_url in client.calls
        commit_url = f"https://api.github.com/repos/{C.GITHUB_REPO}/git/trees/{C.GITHUB_PINNED_COMMIT}?recursive=1"
        assert commit_url not in client.calls

    def test_wrong_tree_id_fails(self):
        """Returned tree ID mismatch raises."""
        client = RecordingClient()
        tree_url = (
            f"https://api.github.com/repos/{C.GITHUB_REPO}"
            f"/git/trees/{C.GIT_TREE_ID}?recursive=1"
        )
        client.register(tree_url, FakeResponse(json_data={
            "sha": "0000000000000000000000000000000000000000",
            "tree": [],
            "truncated": False,
        }))
        with pytest.raises(ValueError, match="Tree SHA mismatch"):
            fetch_github_metadata(client)

    def test_wrong_entry_count_fails(self):
        """Entry count mismatch raises."""
        client = RecordingClient()
        tree_url = (
            f"https://api.github.com/repos/{C.GITHUB_REPO}"
            f"/git/trees/{C.GIT_TREE_ID}?recursive=1"
        )
        entries = _make_entries(3)
        client.register(tree_url, _valid_github_tree_response(entries))
        with pytest.raises(ValueError, match="Expected"):
            fetch_github_metadata(client)

    def test_wrong_blob_object_sha_fails(self):
        """Blob with wrong Git object SHA in tree listing raises."""
        client = RecordingClient()
        tree_url = (
            f"https://api.github.com/repos/{C.GITHUB_REPO}"
            f"/git/trees/{C.GIT_TREE_ID}?recursive=1"
        )
        entries = _make_entries(C.GIT_TREE_ENTRY_COUNT - len(C.PINNED_BLOBS))
        for path, (_, raw_sha) in C.PINNED_BLOBS.items():
            entries.append({"path": path, "type": "blob", "sha": "bad" * 13 + "b", "size": 10})
        digest = compute_canonical_tree_digest(entries)
        client.register(tree_url, _valid_github_tree_response(entries))
        with patch("posttrainbench_repro.acquisition.GIT_TREE_ENTRY_COUNT", len(entries)), \
             patch("posttrainbench_repro.acquisition.GIT_TREE_DIGEST", digest):
            with pytest.raises(ValueError, match="blob SHA mismatch"):
                fetch_github_metadata(client)

    def test_wrong_blob_bytes_fails(self):
        """Blob with wrong raw SHA-256 raises."""
        client = RecordingClient()
        tree_url = (
            f"https://api.github.com/repos/{C.GITHUB_REPO}"
            f"/git/trees/{C.GIT_TREE_ID}?recursive=1"
        )
        entries = _make_entries(C.GIT_TREE_ENTRY_COUNT - len(C.PINNED_BLOBS))
        for path, (git_sha, raw_sha) in C.PINNED_BLOBS.items():
            entries.append({"path": path, "type": "blob", "sha": git_sha, "size": 10})
        digest = compute_canonical_tree_digest(entries)
        client.register(tree_url, _valid_github_tree_response(entries))
        for path in C.PINNED_BLOBS:
            raw_url = f"https://raw.githubusercontent.com/{C.GITHUB_REPO}/{C.GITHUB_PINNED_COMMIT}/{path}"
            client.register(raw_url, FakeResponse(content=b"TAMPERED CONTENT"))
        with patch("posttrainbench_repro.acquisition.GIT_TREE_ENTRY_COUNT", len(entries)), \
             patch("posttrainbench_repro.acquisition.GIT_TREE_DIGEST", digest):
            with pytest.raises(ValueError, match="SHA-256 mismatch"):
                fetch_github_metadata(client)

    def test_each_blob_fetched_once(self):
        """Each raw blob URL is requested at most once."""
        client = RecordingClient()
        tree_url = (
            f"https://api.github.com/repos/{C.GITHUB_REPO}"
            f"/git/trees/{C.GIT_TREE_ID}?recursive=1"
        )
        entries = []
        fake_blobs = {}
        for path, (git_sha, _) in C.PINNED_BLOBS.items():
            content = f"content-of-{path}".encode()
            sha = hashlib.sha256(content).hexdigest()
            fake_blobs[path] = (git_sha, sha)
            entries.append({"path": path, "type": "blob", "sha": git_sha, "size": len(content)})
            raw_url = f"https://raw.githubusercontent.com/{C.GITHUB_REPO}/{C.GITHUB_PINNED_COMMIT}/{path}"
            client.register(raw_url, FakeResponse(content=content))
        digest = compute_canonical_tree_digest(entries)
        client.register(tree_url, _valid_github_tree_response(entries))
        with patch("posttrainbench_repro.acquisition.GIT_TREE_ENTRY_COUNT", len(entries)), \
             patch("posttrainbench_repro.acquisition.GIT_TREE_DIGEST", digest), \
             patch("posttrainbench_repro.acquisition.PINNED_BLOBS", fake_blobs):
            fetch_github_metadata(client)
        raw_calls = [c for c in client.calls if "raw.githubusercontent" in c]
        assert len(raw_calls) == len(C.PINNED_BLOBS)
        assert len(set(raw_calls)) == len(raw_calls), "duplicate raw blob fetch"


# ===================================================================
# 2. Fail-closed: existing outputs preserved on acquisition failure
# ===================================================================

class TestFailClosed:
    """Production failure leaves existing outputs byte-identical."""

    def test_existing_outputs_preserved_on_failure(self, tmp_path):
        """If generate_evidence fails, pre-existing canonical outputs survive."""
        sentinel = b"ORIGINAL CONTENT"
        evidence = tmp_path / "evidence"
        evidence.mkdir()
        (evidence / "provenance.json").write_bytes(sentinel)
        (tmp_path / "index.html").write_bytes(sentinel)
        originals = {
            "evidence/provenance.json": sentinel,
            "index.html": sentinel,
        }
        with patch(
            "posttrainbench_repro.pipeline.acquire_all",
            side_effect=RuntimeError("fail"),
        ):
            with pytest.raises(RuntimeError):
                generate_evidence(output_root=tmp_path)
        for rel, expected in originals.items():
            assert (tmp_path / rel).read_bytes() == expected

    def test_no_new_outputs_on_failure(self, tmp_path):
        """No new canonical outputs are created on acquisition failure."""
        with patch(
            "posttrainbench_repro.pipeline.acquire_all",
            side_effect=RuntimeError("fail"),
        ):
            with pytest.raises(RuntimeError):
                generate_evidence(output_root=tmp_path)
        assert not (tmp_path / "evidence").exists()


# ===================================================================
# 3. Production calls the complete acquisition bundle
# ===================================================================

class TestProductionCallsAcquisition:
    """generate_evidence must call acquire_all and pass result to run_pipeline."""

    def test_acquire_all_called_and_result_used(self, tmp_path):
        """acquire_all is called; its output reaches run_pipeline."""
        acquired = _make_valid_acquired()
        call_log: list[str] = []
        original_acquire = acquire_all
        original_run = run_pipeline

        def fake_acquire(*a, **kw):
            call_log.append("acquire_all")
            return acquired

        def fake_run(acq, *, output_root):
            call_log.append("run_pipeline")
            assert acq is acquired
            assert output_root == tmp_path
            return {}

        with patch("posttrainbench_repro.pipeline.acquire_all", side_effect=fake_acquire), \
             patch("posttrainbench_repro.pipeline.run_pipeline", side_effect=fake_run):
            generate_evidence(output_root=tmp_path)
        assert call_log == ["acquire_all", "run_pipeline"]


# ===================================================================
# 4. HF pagination: success + adversarial cases
# ===================================================================

class TestHFPagination:
    """Pagination: success, early-term, page 113, short, cycle, foreign, wrong rev, etc."""

    def _make_page(self, n: int, ptype: str = "file") -> list[dict[str, Any]]:
        return [{"path": f"p{i}", "type": ptype} for i in range(n)]

    def _base_url(self) -> str:
        return (
            f"https://huggingface.co/api/datasets/{C.HF_DATASET_ID}"
            f"/tree/{C.HF_PINNED_REVISION}"
        )

    def test_success_two_pages(self):
        """Two-page pagination returns correct count."""
        client = RecordingClient()
        base = self._base_url()
        p2_url = f"{base}?cursor=p2"
        page1 = self._make_page(5)
        page2 = self._make_page(3)

        client.register_prefix(base, FakeResponse(
            json_data=page1,
            headers={"link": f'<{p2_url}>; rel="next"'},
        ))
        client.responses[p2_url] = FakeResponse(json_data=page2)

        with patch("posttrainbench_repro.acquisition.HF_TREE_TOTAL_PAGES", 2), \
             patch("posttrainbench_repro.acquisition.HF_TREE_PAGE_SIZE", 5):
            entries, pages = fetch_hf_tree_pages(client)
        assert pages == 2
        assert len(entries) == 8

    def test_page_113_rejected(self):
        """Page 113 (exceeding 112 max) is rejected."""
        client = RecordingClient()
        base = self._base_url()
        call_count = [0]

        class CycleResponse:
            status_code = 200
            def raise_for_status(self): pass
            def json(self_):
                call_count[0] += 1
                return [{"path": f"e{call_count[0]}_{i}", "type": "file"} for i in range(1000)]
            @property
            def headers(self_):
                return {"link": f'<{base}?cursor=c{call_count[0]+1}>; rel="next"'}

        client.register_prefix(base, CycleResponse())
        for i in range(200):
            client.responses[f"{base}?cursor=c{i+1}"] = CycleResponse()

        with pytest.raises(RuntimeError, match="exceeded"):
            fetch_hf_tree_pages(client)

    def test_short_interior_page_rejected(self):
        """Non-final page with < 1000 entries is rejected."""
        client = RecordingClient()
        base = self._base_url()
        p2_url = f"{base}?cursor=p2"
        p3_url = f"{base}?cursor=p3"
        client.register_prefix(base, FakeResponse(
            json_data=self._make_page(500),
            headers={"link": f'<{p2_url}>; rel="next"'},
        ))
        client.responses[p2_url] = FakeResponse(json_data=self._make_page(3))
        with pytest.raises(RuntimeError, match="[Ss]hort"):
            fetch_hf_tree_pages(client)

    def test_cursor_cycle_rejected(self):
        """Revisiting the same URL is rejected."""
        client = RecordingClient()
        base = self._base_url()
        # First page points back to itself via next link
        first_url = f"{base}?recursive=true&expand=false&limit=1000"
        client.register_prefix(base, FakeResponse(
            json_data=self._make_page(1000),
            headers={"link": f'<{first_url}>; rel="next"'},
        ))
        with pytest.raises(RuntimeError, match="[Cc]ycle"):
            fetch_hf_tree_pages(client)

    def test_foreign_host_rejected(self):
        """Next URL pointing to evil.com is rejected."""
        with pytest.raises(ValueError, match="[Ff]oreign"):
            _validate_next_url("https://evil.com/api/datasets/foo/tree/abc")

    def test_wrong_revision_rejected(self):
        """Next URL with wrong revision is rejected."""
        wrong_url = (
            f"https://huggingface.co/api/datasets/{C.HF_DATASET_ID}"
            f"/tree/0000000000000000000000000000000000000000?cursor=x"
        )
        with pytest.raises(ValueError, match="[Ww]rong"):
            _validate_next_url(wrong_url)

    def test_same_prefix_path_suffix_rejected(self):
        """Path that starts with the tree endpoint but has extra path segments."""
        tricky_url = (
            f"https://huggingface.co/api/datasets/{C.HF_DATASET_ID}"
            f"/tree/{C.HF_PINNED_REVISION}/evil"
        )
        # This should be rejected since path must EQUAL the tree endpoint, not just startswith
        with pytest.raises(ValueError):
            _validate_next_url(tricky_url)

    def test_duplicate_path_rejected(self):
        """Duplicate paths in inventory are rejected."""
        client = RecordingClient()
        base = self._base_url()
        duped = [
            {"path": "same/path", "type": "file"},
            {"path": "same/path", "type": "file"},
            {"path": "other", "type": "file"},
        ]
        client.register_prefix(base, FakeResponse(json_data=duped))
        with patch("posttrainbench_repro.acquisition.HF_TREE_TOTAL_PAGES", 1), \
             patch("posttrainbench_repro.acquisition.HF_TREE_TOTAL_ENTRIES", 3), \
             patch("posttrainbench_repro.acquisition.HF_TREE_FILE_COUNT", 3), \
             patch("posttrainbench_repro.acquisition.HF_TREE_DIR_COUNT", 0):
            with pytest.raises(ValueError, match="[Dd]uplicate"):
                fetch_hf_path_inventory(client)

    def test_conflicting_type_rejected(self):
        """Same path with different types is rejected."""
        client = RecordingClient()
        base = self._base_url()
        mixed = [
            {"path": "x", "type": "file"},
            {"path": "x", "type": "directory"},
        ]
        client.register_prefix(base, FakeResponse(json_data=mixed))
        with patch("posttrainbench_repro.acquisition.HF_TREE_TOTAL_PAGES", 1):
            with pytest.raises(ValueError, match="[Cc]onflict"):
                fetch_hf_path_inventory(client)

    def test_unknown_type_rejected(self):
        """Entries with type other than file/directory are rejected."""
        client = RecordingClient()
        base = self._base_url()
        bad = [{"path": "x", "type": "symlink"}]
        client.register_prefix(base, FakeResponse(json_data=bad))
        with pytest.raises(ValueError, match="[Uu]nknown"):
            fetch_hf_tree_pages(client)

    def test_count_mismatch_rejected(self):
        """Total entry count mismatch is rejected."""
        client = RecordingClient()
        base = self._base_url()
        entries = [{"path": f"f{i}", "type": "file"} for i in range(5)]
        client.register_prefix(base, FakeResponse(json_data=entries))
        with patch("posttrainbench_repro.acquisition.HF_TREE_TOTAL_PAGES", 1), \
             patch("posttrainbench_repro.acquisition.HF_TREE_TOTAL_ENTRIES", 999):
            with pytest.raises(ValueError, match="Expected"):
                fetch_hf_path_inventory(client)

    def test_digest_mismatch_rejected(self):
        """Canonical path digest mismatch is rejected."""
        client = RecordingClient()
        base = self._base_url()
        entries = [{"path": f"f{i}", "type": "file"} for i in range(5)]
        client.register_prefix(base, FakeResponse(json_data=entries))
        with patch("posttrainbench_repro.acquisition.HF_TREE_TOTAL_PAGES", 1), \
             patch("posttrainbench_repro.acquisition.HF_TREE_TOTAL_ENTRIES", 5), \
             patch("posttrainbench_repro.acquisition.HF_TREE_FILE_COUNT", 5), \
             patch("posttrainbench_repro.acquisition.HF_TREE_DIR_COUNT", 0):
            with pytest.raises(ValueError, match="digest mismatch"):
                fetch_hf_path_inventory(client)


# ===================================================================
# 5. Disallowed file rejected before I/O
# ===================================================================

class TestAllowlist:
    """Requesting a non-allowlisted file raises before the client is called."""

    def test_disallowed_path_no_io(self):
        """Client.get is never called for a disallowed path."""
        client = RecordingClient()
        with pytest.raises(ValueError, match="allowlist"):
            fetch_allowlisted_file("evil/path.txt", client=client)
        assert len(client.calls) == 0


# ===================================================================
# 6. Allowlisted file bytes/hash/object verification
# ===================================================================

class TestAllowlistedFileVerification:
    """All four allowlisted paths must be verified for bytes and hashes."""

    def test_contamination_wrong_bytes_fails(self):
        """Wrong contamination bytes raise."""
        client = RecordingClient()
        url = (
            f"https://huggingface.co/datasets/{C.HF_DATASET_ID}"
            f"/raw/{C.HF_PINNED_REVISION}/{C.CONTAMINATION_WITNESS_PATH}"
        )
        client.register(url, FakeResponse(content=b"WRONG"))
        with pytest.raises(ValueError):
            from posttrainbench_repro.acquisition import _verify_bytes
            data = fetch_allowlisted_file(C.CONTAMINATION_WITNESS_PATH, client=client)
            _verify_bytes(data, C.CONTAMINATION_WITNESS_SHA256, "test")


# ===================================================================
# 7. JSONL trace excerpt extraction
# ===================================================================

def _build_trace_with_preamble(
    line_records: dict[int, dict],
    preamble_count: int = 13,
) -> bytes:
    """Build a trace with non-JSON preamble lines and JSON records at one-based positions.

    line_records maps one-based physical line numbers to JSON dicts.
    Lines not in line_records get placeholder JSON or preamble text.
    """
    max_line = max(line_records.keys()) if line_records else preamble_count
    lines: list[str] = []
    for line_num in range(1, max_line + 1):
        if line_num <= preamble_count:
            lines.append(f"# preamble line {line_num}")
        elif line_num in line_records:
            lines.append(json.dumps(line_records[line_num]))
        else:
            lines.append(json.dumps({"part": {"text": f"filler for line {line_num}"}}))
    return "\n".join(lines).encode("utf-8")


class TestTraceExcerpts:
    """Trace parsing: real JSONL, missing/malformed/moved/mutated records."""

    def _build_trace(self, records: dict[int, dict]) -> bytes:
        """Build a JSONL trace with 13 preamble lines and records at one-based positions."""
        return _build_trace_with_preamble(records)

    def test_valid_extraction(self):
        """Valid trace extracts correct text and hash."""
        records = {}
        for spec in C.TRACE_EXCERPTS:
            # Wrap excerpt in prefix/suffix so it's a substring match
            records[spec["record"]] = _build_nested(
                spec["pointer"], "CTX " + spec["text"] + " END"
            )
        trace = self._build_trace(records)
        results = extract_trace_excerpts(trace)
        assert len(results) == len(C.TRACE_EXCERPTS)
        for i, spec in enumerate(C.TRACE_EXCERPTS):
            assert results[i]["text"] == spec["text"]
            assert results[i]["sha256"] == spec["sha256"]

    def test_missing_record_fails(self):
        """Trace with too few records raises."""
        trace = b'{"part": {"text": "only one"}}\n'
        with pytest.raises((IndexError, ValueError)):
            extract_trace_excerpts(trace)

    def test_malformed_json_fails(self):
        """Non-JSON record raises."""
        lines = ["# preamble"] * 13 + ["NOT JSON"] * 600
        trace = "\n".join(lines).encode()
        with pytest.raises(ValueError, match="[Mm]alformed"):
            extract_trace_excerpts(trace)

    def test_mutated_text_fails(self):
        """Changed text at correct position fails hash verification."""
        spec = C.TRACE_EXCERPTS[0]
        record = _build_nested(spec["pointer"], "TAMPERED TEXT")
        records = {spec["record"]: record}
        # Add other excerpts so the trace is long enough
        for s in C.TRACE_EXCERPTS[1:]:
            records[s["record"]] = _build_nested(
                s["pointer"], "P " + s["text"] + " S"
            )
        trace = self._build_trace(records)
        with pytest.raises(ValueError, match="not found|substring|mismatch"):
            extract_trace_excerpts(trace)

    def test_moved_record_fails(self):
        """Record at wrong position fails."""
        spec = C.TRACE_EXCERPTS[0]
        wrong_pos = spec["record"] + 1
        record = _build_nested(spec["pointer"], "P " + spec["text"] + " S")
        records = {wrong_pos: record}
        records[spec["record"]] = {"different": "structure"}
        trace = self._build_trace(records)
        # The record at spec["record"] won't have the right pointer/text
        with pytest.raises((ValueError, KeyError)):
            extract_trace_excerpts(trace)


def _build_nested(pointer: str, value: Any) -> dict:
    """Build a nested dict matching a JSON pointer with the given leaf value."""
    parts = pointer.lstrip("/").split("/")
    result: dict = {}
    current = result
    for i, part in enumerate(parts):
        if i == len(parts) - 1:
            current[part] = value
        else:
            current[part] = {}
            current = current[part]
    return result


# ===================================================================
# 8. API cluster 16804408 present → abort (not "unavailable")
# ===================================================================

class TestAPIClusterPresent:
    """If cluster 16804408 is found in inventory, audit_reward_hacking must abort."""

    def test_cluster_present_aborts(self):
        """Presence of API cluster in inventory must raise or refuse 'unavailable'."""
        acquired = _make_valid_acquired(
            extra_all_paths=[
                f"some_root/task_{C.API_MISUSE_TASK_CLUSTER}_dir",
            ]
        )
        # If the function does NOT raise, it must NOT emit "unavailable"
        try:
            rh = audit_reward_hacking(acquired)
            # If it returns, the status must NOT be "unavailable" if cluster is found
            api = rh["using_discovered_api_key"]
            if api["inventory_proof"]["matching_paths"] > 0:
                assert api["status"] != "unavailable", \
                    "Must not emit 'unavailable' when cluster paths exist"
        except (ValueError, RuntimeError):
            pass  # Raising is also acceptable


# ===================================================================
# 9. Duplicate counting: global-cell vs per-root/cell
# ===================================================================

class TestDuplicateCounting:
    """Duplicate counting per (root, bench, model), not global cell."""

    def test_same_bench_model_different_roots(self):
        """Same bench/model in DIFFERENT roots = no duplicates."""
        dirs = [
            "rootA_10h_run1",
            "rootA_10h_run1/humaneval_Qwen_Qwen3-1.7B-Base_100",
            "rootB_10h_run1",
            "rootB_10h_run1/humaneval_Qwen_Qwen3-1.7B-Base_200",
        ]
        inv = {"dir_paths": dirs}
        cov = compute_coverage(inv)
        assert cov["duplicate_job_pairs"] == 0

    def test_multiple_extra_in_one_root_cell(self):
        """Three tasks in one root/cell = 2 duplicate pairs."""
        dirs = [
            "rootA_10h_run1",
            "rootA_10h_run1/humaneval_Qwen_Qwen3-1.7B-Base_100",
            "rootA_10h_run1/humaneval_Qwen_Qwen3-1.7B-Base_101",
            "rootA_10h_run1/humaneval_Qwen_Qwen3-1.7B-Base_102",
        ]
        inv = {"dir_paths": dirs}
        cov = compute_coverage(inv)
        assert cov["duplicate_job_pairs"] == 2

    def test_mixed_roots_with_duplicates(self):
        """Mix of roots with and without duplicates."""
        dirs = [
            "rootA_10h_run1",
            "rootA_10h_run1/humaneval_Qwen_Qwen3-1.7B-Base_100",
            "rootA_10h_run1/humaneval_Qwen_Qwen3-1.7B-Base_101",
            "rootB_10h_run1",
            "rootB_10h_run1/humaneval_Qwen_Qwen3-1.7B-Base_200",
            "rootB_10h_run1/gsm8k_Qwen_Qwen3-1.7B-Base_300",
        ]
        inv = {"dir_paths": dirs}
        cov = compute_coverage(inv)
        assert cov["duplicate_job_pairs"] == 1  # only rootA humaneval


# ===================================================================
# 10. Protocol audit: mutated values fail
# ===================================================================

class TestProtocolMutated:
    """Mutated GPU count, device, timeout, eval dirs, scheduler branch all fail."""

    def _base_blobs(self) -> dict[str, bytes]:
        return {
            "src/commit_utils/single_task.sub": (
                b'num_gpus = 1\n'
                b'request_gpus = $(num_gpus)\n'
                b'requirements = TARGET.CUDADeviceName == "NVIDIA H100 80GB HBM3"\n'
            ),
            "src/run_task.sh": (
                b'#!/bin/bash\n'
                b'NUM_HOURS=${1:-10}\n'
                b'timeout $((NUM_HOURS * 60 + 5))m python run.py\n'
            ),
            "src/commit_utils/commit.sh": (
                b'#!/bin/bash\n'
                b'if [ "$SCHEDULER" = "htcondor_mpi-is" ]; then\n'
                b'  NUM_HOURS=100\n'
                b'  num_gpus=8\n'
                b'  submit_job\n'
                b'fi\n'
            ),
        }

    def _base_entries(self) -> list[dict[str, Any]]:
        entries = []
        for d in C.EXPECTED_EVAL_DIRS:
            entries.append({"path": d, "type": "tree", "sha": "0" * 40})
        return entries

    def test_missing_gpu_count_fails(self):
        blobs = self._base_blobs()
        blobs["src/commit_utils/single_task.sub"] = b"# no gpu spec\n"
        with pytest.raises(ValueError, match="num_gpus"):
            audit_protocol(blobs, self._base_entries())

    def test_missing_device_fails(self):
        blobs = self._base_blobs()
        blobs["src/commit_utils/single_task.sub"] = b"num_gpus = 1\nrequest_gpus = $(num_gpus)\n"
        with pytest.raises(ValueError, match="CUDADeviceName"):
            audit_protocol(blobs, self._base_entries())

    def test_missing_timeout_fails(self):
        blobs = self._base_blobs()
        blobs["src/run_task.sh"] = b"#!/bin/bash\nNUM_HOURS=${1:-10}\necho hello\n"
        with pytest.raises(ValueError, match="timeout"):
            audit_protocol(blobs, self._base_entries())

    def test_missing_eval_dirs_fails(self):
        blobs = self._base_blobs()
        entries = [{"path": "src/other", "type": "tree", "sha": "0" * 40}]
        with pytest.raises(ValueError, match="eval"):
            audit_protocol(blobs, entries)

    def test_comment_only_metr_insufficient(self):
        """A bare comment mention of htcondor_mpi-is without active code."""
        blobs = self._base_blobs()
        blobs["src/commit_utils/commit.sh"] = (
            b"#!/bin/bash\n"
            b"# htcondor_mpi-is was considered but not used\n"
            b"echo default\n"
        )
        with pytest.raises(ValueError, match="commit.sh|scheduler|branch"):
            audit_protocol(blobs, self._base_entries())


# ===================================================================
# 11. Malformed audit results cannot emit partial-support
# ===================================================================

class TestMalformedAuditRejectsStatus:
    """Malformed coverage/protocol/reward-hacking rejects partial-support."""

    def test_wrong_task_count_rejects_claims(self):
        """Coverage with wrong task count prevents evaluate_claims from succeeding."""
        coverage = {"recognized_task_count": 999}
        protocol = {"commit_sh_analysis": {"htcondor_branch": {"ten_hour_jobs": 7, "one_hour_jobs": 1}}}
        rh = {}
        with pytest.raises(ValueError, match="task count"):
            evaluate_claims(coverage, protocol, rh)


# ===================================================================
# 12. Simulated write/replace failure rolls back
# ===================================================================

class TestTransactionalRollback:
    """Write failure during output publication rolls back all outputs."""

    def test_write_failure_rolls_back(self, tmp_path):
        """If writing one output fails, all prior outputs are rolled back."""
        acquired = _make_valid_acquired()
        sentinel = b"SENTINEL"
        evidence = tmp_path / "evidence"
        evidence.mkdir()
        (evidence / "provenance.json").write_bytes(sentinel)

        write_count = [0]
        original_write = Path.write_text

        def failing_write(self, content, *args, **kwargs):
            write_count[0] += 1
            if "poster.html" in str(self):
                raise OSError("disk full")
            return original_write(self, content, *args, **kwargs)

        with patch.object(Path, "write_text", failing_write):
            with pytest.raises(OSError):
                run_pipeline(acquired, output_root=tmp_path)

        # Original provenance.json should be restored
        assert (evidence / "provenance.json").read_bytes() == sentinel


# ===================================================================
# 13. One-based trace line semantics with preamble lines
# ===================================================================


class TestTraceOneBased:
    """Trace extraction uses one-based physical lines including 13 preamble lines.

    The approved excerpts are substrings within resolved fields, not full values.
    """

    def _excerpt_spec(self, idx: int = 0) -> dict:
        return C.TRACE_EXCERPTS[idx]

    def _build_full_trace(self) -> bytes:
        """Build a trace with all 3 excerpts at their correct one-based lines."""
        records = {}
        for spec in C.TRACE_EXCERPTS:
            # The resolved field contains the excerpt as a substring
            # plus prefix and suffix text around it
            prefix = "PREFIX TEXT: some context here. "
            suffix = " SUFFIX text after."
            full_value = prefix + spec["text"] + suffix
            records[spec["record"]] = _build_nested(spec["pointer"], full_value)
        return _build_trace_with_preamble(records)

    def test_valid_one_based_extraction(self):
        """Extracts correct substring from one-based physical lines."""
        trace = self._build_full_trace()
        results = extract_trace_excerpts(trace)
        assert len(results) == 3
        for i, spec in enumerate(C.TRACE_EXCERPTS):
            assert results[i]["text"] == spec["text"]
            assert results[i]["sha256"] == spec["sha256"]

    def test_wrong_physical_line_fails(self):
        """Record at wrong line number fails extraction."""
        spec = self._excerpt_spec(0)
        prefix = "PREFIX "
        full_value = prefix + spec["text"] + " SUFFIX"
        # Put the record at line 501 instead of 500
        records = {spec["record"] + 1: _build_nested(spec["pointer"], full_value)}
        # Put a valid but wrong record at line 500
        records[spec["record"]] = {"different": "structure"}
        trace = _build_trace_with_preamble(records)
        with pytest.raises((KeyError, ValueError)):
            extract_trace_excerpts(trace)

    def test_missing_substring_fails(self):
        """Resolved field without the approved substring fails."""
        spec = self._excerpt_spec(0)
        # The field value doesn't contain the approved excerpt
        records = {spec["record"]: _build_nested(spec["pointer"], "completely different text")}
        # Must also have the other records
        for s in C.TRACE_EXCERPTS[1:]:
            prefix = "P "
            records[s["record"]] = _build_nested(s["pointer"], prefix + s["text"] + " S")
        trace = _build_trace_with_preamble(records)
        with pytest.raises(ValueError, match="substring|not found|mismatch"):
            extract_trace_excerpts(trace)

    def test_wrong_pointer_fails(self):
        """Wrong JSON pointer at correct line fails."""
        spec = self._excerpt_spec(0)
        # Put value at a different pointer path
        records = {spec["record"]: {"wrong": {"path": spec["text"]}}}
        trace = _build_trace_with_preamble(records)
        with pytest.raises((KeyError, ValueError)):
            extract_trace_excerpts(trace)

    def test_malformed_target_json_fails(self):
        """Malformed JSON at the target line fails."""
        lines = ["# preamble"] * 13
        for i in range(14, 532):
            lines.append(json.dumps({"part": {"text": f"filler {i}"}}))
        # Make line 500 malformed
        lines[499] = "NOT VALID JSON {{{{"
        trace = "\n".join(lines).encode()
        with pytest.raises(ValueError, match="[Mm]alformed"):
            extract_trace_excerpts(trace)

    def test_changed_excerpt_hash_fails(self):
        """Changed excerpt text still in the field fails hash check."""
        spec = self._excerpt_spec(0)
        # Similar but different text
        tampered = spec["text"][:-1] + "X"
        records = {spec["record"]: _build_nested(spec["pointer"], tampered)}
        for s in C.TRACE_EXCERPTS[1:]:
            records[s["record"]] = _build_nested(s["pointer"], "P " + s["text"] + " S")
        trace = _build_trace_with_preamble(records)
        with pytest.raises(ValueError):
            extract_trace_excerpts(trace)

    def test_preamble_not_treated_as_json(self):
        """The 13 preamble lines must not be parsed as JSON records."""
        # Put a valid JSON record at preamble line 5 with the excerpt
        spec = self._excerpt_spec(0)
        lines = []
        for i in range(1, 532):
            if i <= 13:
                if i == 5:
                    # Sneaky: valid JSON at a preamble position
                    lines.append(json.dumps(_build_nested(spec["pointer"], spec["text"])))
                else:
                    lines.append(f"# preamble {i}")
            elif i == spec["record"]:
                lines.append(json.dumps({"wrong": "structure"}))
            else:
                lines.append(json.dumps({"part": {"text": f"filler {i}"}}))
        for s in C.TRACE_EXCERPTS[1:]:
            while len(lines) < s["record"]:
                lines.append(json.dumps({"part": {"text": "pad"}}))
            lines[s["record"] - 1] = json.dumps(
                _build_nested(s["pointer"], "P " + s["text"] + " S")
            )
        trace = "\n".join(lines).encode()
        # Line 500 has wrong structure, so this should fail
        with pytest.raises((KeyError, ValueError)):
            extract_trace_excerpts(trace)


# ===================================================================
# 14. HF entry metadata: object/size validation for allowlisted files
# ===================================================================

class TestHFEntryMetadata:
    """Validate HF entry object IDs and sizes for allowlisted files."""

    def test_judgment_wrong_object_fails(self):
        """Wrong Git object for judgment file in HF inventory raises."""
        acquired = _make_valid_acquired()
        inv = acquired["hf_inventory"]
        inv["entry_metadata"][C.INSTRUCTION_MODEL_JUDGMENT_PATH]["oid"] = (
            "0000000000000000000000000000000000000000"
        )
        with pytest.raises(ValueError, match="object|oid|mismatch"):
            from posttrainbench_repro.acquisition import _validate_hf_entry_metadata
            _validate_hf_entry_metadata(inv)

    def test_trace_wrong_size_fails(self):
        """Wrong size for trace file in HF inventory raises."""
        acquired = _make_valid_acquired()
        inv = acquired["hf_inventory"]
        inv["entry_metadata"][C.INSTRUCTION_MODEL_TRACE_PATH]["size"] = 999
        with pytest.raises(ValueError, match="size"):
            from posttrainbench_repro.acquisition import _validate_hf_entry_metadata
            _validate_hf_entry_metadata(inv)

    def test_missing_allowlisted_path_fails(self):
        """Allowlisted path not present in HF inventory raises."""
        acquired = _make_valid_acquired()
        inv = acquired["hf_inventory"]
        inv["entry_metadata"] = {}  # No entries
        with pytest.raises(ValueError, match="not found|missing"):
            from posttrainbench_repro.acquisition import _validate_hf_entry_metadata
            _validate_hf_entry_metadata(inv)

    def test_allowlisted_path_is_directory_fails(self):
        """Allowlisted path that is a directory instead of file raises."""
        acquired = _make_valid_acquired()
        inv = acquired["hf_inventory"]
        inv["entry_metadata"][C.INSTRUCTION_MODEL_JUDGMENT_PATH]["type"] = (
            "directory"
        )
        with pytest.raises(ValueError, match="type|file|directory"):
            from posttrainbench_repro.acquisition import _validate_hf_entry_metadata
            _validate_hf_entry_metadata(inv)


# ===================================================================
# 15. Coverage gating: exact inventory metadata and computed coverage
# ===================================================================

class TestCoverageGating:
    """Coverage must require exact inventory metadata and computed coverage."""

    def test_wrong_root_count_in_evaluate_claims(self):
        """Wrong recognized_root_count prevents claims."""
        coverage = {
            "recognized_task_count": C.EXPECTED_TASK_COUNT,
            "recognized_root_count": 46,  # Wrong
            "recognized_root_cell_pairs": C.EXPECTED_ROOT_CELL_PAIRS,
            "duplicate_job_pairs": C.EXPECTED_DUPLICATE_PAIRS,
            "missing_root_cell_pairs": C.EXPECTED_MISSING_PAIRS,
        }
        protocol = {"commit_sh_analysis": {"htcondor_branch": {"ten_hour_jobs": 7, "one_hour_jobs": 1}}}
        rh = _make_minimal_rh()
        with pytest.raises(ValueError, match="root"):
            evaluate_claims(coverage, protocol, rh)

    def test_wrong_duplicate_count_in_evaluate_claims(self):
        """Wrong duplicate_job_pairs prevents claims."""
        coverage = {
            "recognized_task_count": C.EXPECTED_TASK_COUNT,
            "recognized_root_count": C.EXPECTED_ROOT_COUNT,
            "recognized_root_cell_pairs": C.EXPECTED_ROOT_CELL_PAIRS,
            "duplicate_job_pairs": 99,  # Wrong
            "missing_root_cell_pairs": C.EXPECTED_MISSING_PAIRS,
        }
        protocol = {"commit_sh_analysis": {"htcondor_branch": {"ten_hour_jobs": 7, "one_hour_jobs": 1}}}
        rh = _make_minimal_rh()
        with pytest.raises(ValueError, match="duplicate"):
            evaluate_claims(coverage, protocol, rh)

    def test_wrong_root_cell_pairs_in_evaluate_claims(self):
        """Wrong root_cell_pairs prevents claims."""
        coverage = {
            "recognized_task_count": C.EXPECTED_TASK_COUNT,
            "recognized_root_count": C.EXPECTED_ROOT_COUNT,
            "recognized_root_cell_pairs": 1300,  # Wrong
            "duplicate_job_pairs": C.EXPECTED_DUPLICATE_PAIRS,
            "missing_root_cell_pairs": C.EXPECTED_MISSING_PAIRS,
        }
        protocol = {"commit_sh_analysis": {"htcondor_branch": {"ten_hour_jobs": 7, "one_hour_jobs": 1}}}
        rh = _make_minimal_rh()
        with pytest.raises(ValueError, match="pair"):
            evaluate_claims(coverage, protocol, rh)


def _make_minimal_rh() -> dict[str, Any]:
    """Minimal reward-hacking dict for evaluate_claims tests."""
    return {
        "training_on_test_sets": {"status": "partial-support"},
        "downloading_instruction_tuned_checkpoint": {"status": "partial-support"},
        "using_discovered_api_key": {
            "status": "unavailable",
            "inventory_proof": {"matching_paths": 0},
        },
    }


# ===================================================================
# 16. Reward-hacking: verified bytes, not constant substitution
# ===================================================================

class TestRewardHackingVerifiedBytes:
    """audit_reward_hacking must output supplied verified bytes, not constants."""

    def test_contamination_uses_supplied_bytes(self):
        """Contamination witness_bytes must come from acquired, not constants."""
        acquired = _make_valid_acquired()
        # Change the acquired contamination content to something different
        acquired["contamination_content"] = b"different contamination\n"
        rh = audit_reward_hacking(acquired)
        contam = rh["training_on_test_sets"]
        # If using constants, it would be "contamination detected\n"
        # If using supplied bytes, it would be "different contamination\n"
        assert contam["witness_bytes"] != "contamination detected\n", \
            "Must use supplied bytes, not constants"


# ===================================================================
# 17. Pointer completeness: behavioral test for rendered output
# ===================================================================

class TestPointerCompleteness:
    """Rendered HTML must contain resolvable JSON pointers for every result."""

    def test_poster_has_claim_status_pointers(self):
        """Poster must have JSON pointers for each claim status."""
        acquired = _make_valid_acquired()
        from posttrainbench_repro.audit import get_provenance
        from posttrainbench_repro.render import render_poster_html
        provenance = get_provenance(acquired)
        coverage = compute_coverage(acquired["hf_inventory"])
        protocol = audit_protocol(
            acquired["github"]["blob_contents"],
            acquired["github"]["entries"],
        )
        coverage_output = {**coverage, "protocol": protocol}
        rh = audit_reward_hacking(acquired)
        claims = evaluate_claims(coverage, protocol, rh)
        poster = render_poster_html(provenance, coverage_output, rh, claims)
        assert "evidence/claims.json#/claim_1" in poster
        assert "evidence/claims.json#/claim_2" in poster
        assert "evidence/reward_hacking.json#/training_on_test_sets" in poster
        assert "evidence/reward_hacking.json#/using_discovered_api_key" in poster
        assert "evidence/coverage.json#/cell_counts" in poster

    def test_poster_has_count_pointers(self):
        """Poster must have JSON pointers for coverage counts."""
        acquired = _make_valid_acquired()
        from posttrainbench_repro.audit import get_provenance
        from posttrainbench_repro.render import render_poster_html
        provenance = get_provenance(acquired)
        coverage = compute_coverage(acquired["hf_inventory"])
        protocol = audit_protocol(
            acquired["github"]["blob_contents"],
            acquired["github"]["entries"],
        )
        coverage_output = {**coverage, "protocol": protocol}
        rh = audit_reward_hacking(acquired)
        claims = evaluate_claims(coverage, protocol, rh)
        poster = render_poster_html(provenance, coverage_output, rh, claims)
        assert "evidence/coverage.json#/recognized_task_count" in poster
        assert "evidence/coverage.json#/duplicate_job_pairs" in poster


# ===================================================================
# 18. _analyze_commit_sh: condor_submit_bid syntax parsing
# ===================================================================

# Realistic commit.sh fixture matching the actual pinned syntax
_REALISTIC_COMMIT_SH = b"""\
#!/bin/bash
source src/commit_utils/set_env_vars.sh

models=(
    # "google/gemma-3-4b-pt"
    "Qwen/Qwen3-4B-Base"
    # "Qwen/Qwen3-1.7B-Base"
    # "HuggingFaceTB/SmolLM3-3B-Base"
)

evals=(
    # "aime2025"
    # "arenahardwriting"
    # "bfcl"
    # "gpqamain"
    # "gsm8k"
    # "humaneval"
    "healthbench"
)
export POST_TRAIN_BENCH_EXPERIMENT_NAME="_METR"
for model in "${models[@]}"; do
    for eval in "${evals[@]}"; do
        echo ""
        echo $model on $eval
        if [ "${POST_TRAIN_BENCH_JOB_SCHEDULER}" = "htcondor_mpi-is" ]; then
            # Proprietary (API)
            # condor_submit_bid 100 -a "agent=codex" -a "agent_config=gpt-5.1-codex-max" -a "eval=$eval" -a "model_to_train=$model" -a "num_hours=10" src/commit_utils/single_task.sub
            condor_submit_bid 100 -a "agent=claude" -a "agent_config=claude-opus-4-6" -a "eval=$eval" -a "model_to_train=$model" -a "num_hours=100" -a "num_gpus=8" src/commit_utils/single_task.sub
        elif [ "${POST_TRAIN_BENCH_JOB_SCHEDULER}" = "htcondor" ]; then
            condor_submit_bid 100 -a "agent=claude" -a "agent_config=claude-opus-4-6" -a "eval=$eval" -a "model_to_train=$model" -a "num_hours=10" src/commit_utils/single_task.sub
            condor_submit_bid 100 -a "agent=claude" -a "agent_config=claude-sonnet-4-5" -a "eval=$eval" -a "model_to_train=$model" -a "num_hours=10" src/commit_utils/single_task.sub
            condor_submit_bid 100 -a "agent=codex" -a "agent_config=o3" -a "eval=$eval" -a "model_to_train=$model" -a "num_hours=10" src/commit_utils/single_task.sub
            condor_submit_bid 100 -a "agent=opencode" -a "agent_config=kimi-k2.5" -a "eval=$eval" -a "model_to_train=$model" -a "num_hours=10" src/commit_utils/single_task.sub
            condor_submit_bid 100 -a "agent=opencode" -a "agent_config=gemini-2.5-pro" -a "eval=$eval" -a "model_to_train=$model" -a "num_hours=10" src/commit_utils/single_task.sub
            condor_submit_bid 100 -a "agent=aider" -a "agent_config=o3" -a "eval=$eval" -a "model_to_train=$model" -a "num_hours=10" src/commit_utils/single_task.sub
            condor_submit_bid 100 -a "agent=aider" -a "agent_config=claude-opus-4-6" -a "eval=$eval" -a "model_to_train=$model" -a "num_hours=10" src/commit_utils/single_task.sub
            condor_submit_bid 100 -a "agent=claude" -a "agent_config=claude-opus-4-6" -a "eval=$eval" -a "model_to_train=$model" -a "num_hours=1" src/commit_utils/single_task.sub
        else
            echo "Unsupported scheduler: ${POST_TRAIN_BENCH_JOB_SCHEDULER}"
        fi
    done
done
"""


class TestCommitShAnalysis:
    """_analyze_commit_sh must parse actual condor_submit_bid syntax."""

    def test_models_array_parsed(self):
        """Active models=(...) entry must be detected."""
        from posttrainbench_repro.audit import _analyze_commit_sh
        result = _analyze_commit_sh(_REALISTIC_COMMIT_SH.decode())
        assert "Qwen/Qwen3-4B-Base" in result["current_models_in_arrays"]

    def test_evals_array_parsed(self):
        """Active evals=(...) entry must be detected."""
        from posttrainbench_repro.audit import _analyze_commit_sh
        result = _analyze_commit_sh(_REALISTIC_COMMIT_SH.decode())
        assert "healthbench" in result["current_benchmarks_in_arrays"]

    def test_mpi_is_branch_100h_8gpu(self):
        """htcondor_mpi-is branch must report one 100h/8-GPU MPI job."""
        from posttrainbench_repro.audit import _analyze_commit_sh
        result = _analyze_commit_sh(_REALISTIC_COMMIT_SH.decode())
        mpi = result.get("htcondor_mpi_is_branch", {})
        assert mpi.get("hours") == 100
        assert mpi.get("gpus") == 8

    def test_htcondor_branch_seven_10h_one_1h(self):
        """htcondor branch must report 7 ten-hour and 1 one-hour jobs."""
        from posttrainbench_repro.audit import _analyze_commit_sh
        result = _analyze_commit_sh(_REALISTIC_COMMIT_SH.decode())
        branch = result.get("htcondor_branch", {})
        assert branch.get("ten_hour_jobs") == 7
        assert branch.get("one_hour_jobs") == 1

    def test_commented_models_excluded(self):
        """Commented models must not appear in arrays."""
        from posttrainbench_repro.audit import _analyze_commit_sh
        result = _analyze_commit_sh(_REALISTIC_COMMIT_SH.decode())
        models = result["current_models_in_arrays"]
        assert "google/gemma-3-4b-pt" not in models
        assert "Qwen/Qwen3-1.7B-Base" not in models

    def test_commented_condor_excluded(self):
        """Commented condor_submit_bid lines must not be counted."""
        from posttrainbench_repro.audit import _analyze_commit_sh
        result = _analyze_commit_sh(_REALISTIC_COMMIT_SH.decode())
        # Only 1 active line in htcondor_mpi-is branch
        mpi = result.get("htcondor_mpi_is_branch", {})
        assert mpi.get("active_jobs") == 1 or mpi.get("hours") == 100

    def test_wrong_model_detected(self):
        """Mutated model must abort rather than produce evidence."""
        from posttrainbench_repro.audit import _analyze_commit_sh
        mutated = _REALISTIC_COMMIT_SH.replace(
            b'"Qwen/Qwen3-4B-Base"', b'"Qwen/Qwen3-1.7B-Base"'
        )
        with pytest.raises(ValueError, match="model"):
            _analyze_commit_sh(mutated.decode())


# ===================================================================
# 19. coverage.json inventory key
# ===================================================================

class TestCoverageInventoryKey:
    """coverage.json must include complete inventory counts/digests."""

    def test_coverage_has_inventory(self):
        """compute_coverage must produce 'inventory' key."""
        acquired = _make_valid_acquired()
        coverage = compute_coverage(acquired["hf_inventory"])
        assert "inventory" in coverage

    def test_coverage_inventory_has_digests(self):
        """inventory must have all/file/dir digests."""
        acquired = _make_valid_acquired()
        coverage = compute_coverage(acquired["hf_inventory"])
        inv = coverage.get("inventory", {})
        assert "all_entries_digest" in inv
        assert "file_entries_digest" in inv
        assert "dir_entries_digest" in inv

    def test_coverage_inventory_has_counts(self):
        """inventory must have page/entry/file/dir counts."""
        acquired = _make_valid_acquired()
        coverage = compute_coverage(acquired["hf_inventory"])
        inv = coverage.get("inventory", {})
        assert "total_entries" in inv
        assert "file_count" in inv
        assert "dir_count" in inv

    def test_coverage_inventory_has_rejected_siblings(self):
        """inventory must have rejected siblings oracle count/digest."""
        acquired = _make_valid_acquired()
        coverage = compute_coverage(acquired["hf_inventory"])
        inv = coverage.get("inventory", {})
        assert "rejected_siblings_count" in inv
        assert "rejected_siblings_digest" in inv


# ===================================================================
# 20. Updated fixture: realistic commit.sh for protocol tests
# ===================================================================

class TestProtocolWithRealisticFixture:
    """Protocol audit using realistic condor_submit_bid fixture."""

    def _make_blobs_with_realistic_commit_sh(self) -> dict[str, bytes]:
        return {
            "src/commit_utils/single_task.sub": (
                b'num_gpus = 1\n'
                b'request_gpus = $(num_gpus)\n'
                b'requirements = TARGET.CUDADeviceName == "NVIDIA H100 80GB HBM3"\n'
            ),
            "src/run_task.sh": (
                b'#!/bin/bash\n'
                b'NUM_HOURS=${1:-10}\n'
                b'timeout $((NUM_HOURS * 60 + 5))m python run.py\n'
            ),
            "src/commit_utils/commit.sh": _REALISTIC_COMMIT_SH,
            "README.md": b"# PostTrainBench\n",
            "LICENSE": b"MIT License\n",
        }

    def _base_entries(self) -> list[dict[str, Any]]:
        entries = []
        for d in C.EXPECTED_EVAL_DIRS:
            entries.append({"path": d, "type": "tree", "sha": "0" * 40})
        return entries

    def test_protocol_accepts_realistic_fixture(self):
        """Protocol audit must succeed with realistic commit.sh syntax."""
        blobs = self._make_blobs_with_realistic_commit_sh()
        entries = self._base_entries()
        result = audit_protocol(blobs, entries)
        analysis = result["commit_sh_analysis"]
        assert analysis["htcondor_branch"]["ten_hour_jobs"] == 7
        assert analysis["htcondor_branch"]["one_hour_jobs"] == 1
        assert "Qwen/Qwen3-4B-Base" in analysis["current_models_in_arrays"]


# ===================================================================
# 21. Final acquisition and authority contract
# ===================================================================

class TestFinalAcquisitionContract:
    """Acquisition must retain and validate the exact consumed authorities."""

    def _entry_metadata(self) -> dict[str, dict[str, Any]]:
        metadata = {
            path: {
                "type": "file",
                "oid": hashlib.sha1(path.encode("utf-8")).hexdigest(),
                "size": len(path.encode("utf-8")),
            }
            for path in C.HF_ALLOWLISTED_FILES
        }
        metadata[C.INSTRUCTION_MODEL_JUDGMENT_PATH].update({
            "oid": C.INSTRUCTION_MODEL_JUDGMENT_GIT_OBJECT,
            "size": C.INSTRUCTION_MODEL_JUDGMENT_SIZE,
        })
        metadata[C.INSTRUCTION_MODEL_TRACE_PATH].update({
            "oid": C.INSTRUCTION_MODEL_TRACE_GIT_OBJECT,
            "size": C.INSTRUCTION_MODEL_TRACE_SIZE,
        })
        return metadata

    def test_inventory_retains_allowlisted_entry_metadata(self, monkeypatch):
        """A valid complete-tree response retains type, oid, and size."""
        import posttrainbench_repro.acquisition as A

        metadata = self._entry_metadata()
        entries = [
            {"path": path, **entry}
            for path, entry in sorted(metadata.items())
        ]
        paths = [entry["path"] for entry in entries]
        digest = compute_canonical_path_digest(paths)

        monkeypatch.setattr(A, "fetch_hf_tree_pages", lambda client=None: (entries, 1))
        monkeypatch.setattr(A, "HF_TREE_TOTAL_PAGES", 1)
        monkeypatch.setattr(A, "HF_TREE_TOTAL_ENTRIES", 4)
        monkeypatch.setattr(A, "HF_TREE_FILE_COUNT", 4)
        monkeypatch.setattr(A, "HF_TREE_DIR_COUNT", 0)
        monkeypatch.setattr(C, "CANONICAL_ALL_ENTRIES_SHA256", digest)
        monkeypatch.setattr(C, "CANONICAL_FILES_SHA256", digest)
        monkeypatch.setattr(
            C,
            "CANONICAL_DIRS_SHA256",
            compute_canonical_path_digest([]),
        )

        inventory = A.fetch_hf_path_inventory()
        assert inventory["entry_metadata"] == metadata

    def test_acquire_validates_metadata_before_content_download(self, monkeypatch):
        """Missing allowlisted metadata aborts before any content request."""
        import posttrainbench_repro.acquisition as A

        metadata = self._entry_metadata()
        metadata.pop(C.TIME_TAKEN_WITNESS_PATH)
        monkeypatch.setattr(A, "fetch_github_metadata", lambda client=None: {})
        monkeypatch.setattr(
            A,
            "fetch_hf_path_inventory",
            lambda client=None: {"entry_metadata": metadata},
        )

        def forbidden_download(*args, **kwargs):
            pytest.fail("allowlisted content download happened before validation")

        monkeypatch.setattr(A, "fetch_allowlisted_file", forbidden_download)
        with pytest.raises(ValueError, match="allowlisted|Allowlisted|metadata"):
            A.acquire_all(client=RecordingClient())

    def test_acquire_returns_deterministic_consumed_hf_metadata(self, monkeypatch):
        """The returned bundle identifies every downloaded HF file without bytes."""
        import posttrainbench_repro.acquisition as A

        trace = b"x" * C.INSTRUCTION_MODEL_TRACE_SIZE
        contents = {
            C.CONTAMINATION_WITNESS_PATH: C.CONTAMINATION_WITNESS_BYTES,
            C.TIME_TAKEN_WITNESS_PATH: C.TIME_TAKEN_WITNESS_BYTES,
            C.INSTRUCTION_MODEL_JUDGMENT_PATH: C.INSTRUCTION_MODEL_JUDGMENT_BYTES,
            C.INSTRUCTION_MODEL_TRACE_PATH: trace,
        }
        metadata = self._entry_metadata()
        metadata[C.CONTAMINATION_WITNESS_PATH]["size"] = len(
            C.CONTAMINATION_WITNESS_BYTES
        )
        metadata[C.TIME_TAKEN_WITNESS_PATH]["size"] = len(
            C.TIME_TAKEN_WITNESS_BYTES
        )
        monkeypatch.setattr(A, "fetch_github_metadata", lambda client=None: {})
        monkeypatch.setattr(
            A,
            "fetch_hf_path_inventory",
            lambda client=None: {"entry_metadata": metadata},
        )
        monkeypatch.setattr(
            A,
            "fetch_allowlisted_file",
            lambda path, client=None: contents[path],
        )
        monkeypatch.setattr(A, "_verify_bytes", lambda *args: None)
        monkeypatch.setattr(A, "extract_trace_excerpts", lambda trace_bytes: [])

        acquired = A.acquire_all(client=RecordingClient())
        consumed = acquired["hf_consumed_files"]
        assert [item["path"] for item in consumed] == sorted(C.HF_ALLOWLISTED_FILES)
        for item in consumed:
            path = item["path"]
            assert item["raw_url"].endswith(
                f"/raw/{C.HF_PINNED_REVISION}/{path}"
            )
            assert item["sha256"] == hashlib.sha256(contents[path]).hexdigest()
            assert item["size"] == len(contents[path])
            assert item["oid"] == metadata[path]["oid"]
            assert "content" not in item

    def test_provenance_records_every_exact_acquisition(self):
        """Provenance exposes the exact GitHub and HF request authorities."""
        from posttrainbench_repro.audit import get_provenance

        acquired = _make_valid_acquired()
        tree_url = (
            f"https://api.github.com/repos/{C.GITHUB_REPO}"
            f"/git/trees/{C.GIT_TREE_ID}?recursive=1"
        )
        acquired["github"]["tree_acquisition"] = {
            "url": tree_url,
            "acquisition_command": f"GET {tree_url}",
        }
        for path, meta in acquired["github"]["blobs"].items():
            raw_url = (
                f"https://raw.githubusercontent.com/{C.GITHUB_REPO}"
                f"/{C.GITHUB_PINNED_COMMIT}/{path}"
            )
            meta["raw_url"] = raw_url
            meta["acquisition_command"] = f"GET {raw_url}"

        hf_tree_url = (
            f"https://huggingface.co/api/datasets/{C.HF_DATASET_ID}"
            f"/tree/{C.HF_PINNED_REVISION}"
            "?recursive=true&expand=false&limit=1000"
        )
        acquired["hf_inventory"]["tree_acquisition"] = {
            "initial_url": hf_tree_url,
            "acquisition_command": (
                f"GET {hf_tree_url}; follow Link rel=\"next\" until absent"
            ),
        }
        acquired["hf_consumed_files"] = [
            {
                "path": path,
                "raw_url": (
                    f"https://huggingface.co/datasets/{C.HF_DATASET_ID}"
                    f"/raw/{C.HF_PINNED_REVISION}/{path}"
                ),
                "acquisition_command": (
                    "GET "
                    f"https://huggingface.co/datasets/{C.HF_DATASET_ID}"
                    f"/raw/{C.HF_PINNED_REVISION}/{path}"
                ),
                "sha256": hashlib.sha256(path.encode("utf-8")).hexdigest(),
                "size": len(path),
                "oid": hashlib.sha1(path.encode("utf-8")).hexdigest(),
            }
            for path in sorted(C.HF_ALLOWLISTED_FILES)
        ]

        provenance = get_provenance(acquired)
        assert provenance["source"]["tree_acquisition"] == (
            acquired["github"]["tree_acquisition"]
        )
        for meta in provenance["source"]["consumed_blobs"].values():
            assert meta["raw_url"].startswith("https://raw.githubusercontent.com/")
            assert meta["acquisition_command"] == f"GET {meta['raw_url']}"
        assert provenance["dataset"]["tree_acquisition"] == (
            acquired["hf_inventory"]["tree_acquisition"]
        )
        assert provenance["dataset"]["consumed_files"] == (
            acquired["hf_consumed_files"]
        )


def _valid_final_claim_inputs() -> tuple[
    dict[str, Any], dict[str, Any], dict[str, Any]
]:
    """Return exact valid claim inputs for one-field mutation tests."""
    acquired = _make_valid_acquired()
    acquired["github"]["blob_contents"][
        "src/commit_utils/commit.sh"
    ] = _REALISTIC_COMMIT_SH
    coverage = compute_coverage(acquired["hf_inventory"])
    protocol = audit_protocol(
        acquired["github"]["blob_contents"],
        acquired["github"]["entries"],
    )
    reward = audit_reward_hacking(acquired)
    return coverage, protocol, reward


def _mutate_path(obj: Any, path: tuple[Any, ...], value: Any) -> None:
    current = obj
    for part in path[:-1]:
        current = current[part]
    current[path[-1]] = value


_FINAL_GATE_MUTATIONS = [
    ("coverage", ("cell_counts", "aime2025", 0), 46),
    ("coverage", ("matrix", 0, "count"), 46),
    ("protocol", ("num_gpus_default",), 2),
    ("protocol", ("cuda_device_requirement",), "wrong GPU"),
    ("protocol", ("request_gpus_binding",), "wrong binding"),
    ("protocol", ("receives_num_hours",), False),
    ("protocol", ("solve_timeout_formula",), "wrong formula"),
    ("protocol", ("timeout_grace_minutes",), 4),
    ("protocol", ("timeout_formula_found",), False),
    ("protocol", ("task_dir_10h_suffix",), False),
    ("protocol", ("evaluation_dirs_present",), C.EXPECTED_EVAL_DIRS[:-1]),
    (
        "protocol",
        ("commit_sh_analysis", "current_models_in_arrays"),
        ["Qwen/Qwen3-1.7B-Base"],
    ),
    (
        "protocol",
        ("commit_sh_analysis", "current_benchmarks_in_arrays"),
        ["aime2025"],
    ),
    (
        "protocol",
        ("commit_sh_analysis", "htcondor_mpi_is_branch", "hours"),
        10,
    ),
    (
        "protocol",
        ("commit_sh_analysis", "htcondor_mpi_is_branch", "gpus"),
        1,
    ),
    (
        "protocol",
        ("commit_sh_analysis", "htcondor_branch", "ten_hour_jobs"),
        6,
    ),
    (
        "protocol",
        ("commit_sh_analysis", "htcondor_branch", "one_hour_jobs"),
        0,
    ),
    ("reward", ("training_on_test_sets", "status"), "unavailable"),
    (
        "reward",
        ("downloading_instruction_tuned_checkpoint", "status"),
        "unavailable",
    ),
    ("reward", ("using_discovered_api_key", "status"), "partial-support"),
    (
        "reward",
        ("using_discovered_api_key", "inventory_proof", "matching_paths"),
        1,
    ),
    (
        "reward",
        (
            "downloading_instruction_tuned_checkpoint",
            "trace",
            "sha256",
        ),
        "0" * 64,
    ),
    (
        "reward",
        ("downloading_instruction_tuned_checkpoint", "trace", "size"),
        1,
    ),
    (
        "reward",
        (
            "downloading_instruction_tuned_checkpoint",
            "safe_excerpts",
            0,
            "text",
        ),
        "wrong excerpt",
    ),
    (
        "reward",
        (
            "downloading_instruction_tuned_checkpoint",
            "safe_excerpts",
            0,
            "sha256",
        ),
        "0" * 64,
    ),
]


@pytest.mark.parametrize(
    ("target", "path", "value"),
    _FINAL_GATE_MUTATIONS,
    ids=[f"{target}-{path[-1]}" for target, path, _ in _FINAL_GATE_MUTATIONS],
)
def test_claim_gate_rejects_every_mutated_authority(target, path, value):
    """Any changed coverage, protocol, or reward authority aborts claims."""
    coverage, protocol, reward = _valid_final_claim_inputs()
    objects = {
        "coverage": copy.deepcopy(coverage),
        "protocol": copy.deepcopy(protocol),
        "reward": copy.deepcopy(reward),
    }
    _mutate_path(objects[target], path, value)
    with pytest.raises(ValueError):
        evaluate_claims(
            objects["coverage"],
            objects["protocol"],
            objects["reward"],
        )


# ===================================================================
# 22. Final reviewer blockers: isolation, auxiliary data, pointers
# ===================================================================

class TestExplicitOutputIsolation:
    """Pipeline-writing tests must choose an isolated output directory."""

    def test_run_pipeline_writes_only_to_explicit_output_root(self, tmp_path):
        """An explicit output root receives every canonical output."""
        isolated_root = tmp_path / "isolated"
        outputs = run_pipeline(
            _make_valid_acquired(),
            output_root=isolated_root,
        )
        assert set(outputs) == set(C.CANONICAL_OUTPUTS)
        assert all(path.is_relative_to(isolated_root) for path in outputs.values())
        assert all(path.exists() for path in outputs.values())

    def test_generate_evidence_forwards_explicit_output_root(self, tmp_path):
        """The live entry point forwards its caller-selected output root."""
        isolated_root = tmp_path / "isolated"
        with patch(
            "posttrainbench_repro.pipeline.acquire_all",
            return_value=_make_valid_acquired(),
        ):
            outputs = generate_evidence(output_root=isolated_root)
        assert all(path.is_relative_to(isolated_root) for path in outputs.values())


def _inventory_with_viewer_data(file_count: int = 2397) -> dict[str, Any]:
    inventory = _make_valid_hf_inventory()
    inventory["dir_paths"] = [
        path
        for path in inventory["dir_paths"]
        if path != "viewer_data" and not path.startswith("viewer_data/")
    ]
    inventory["file_paths"] = [
        path
        for path in inventory["file_paths"]
        if not path.startswith("viewer_data/")
    ]
    inventory["all_paths"] = [
        path
        for path in inventory["all_paths"]
        if path != "viewer_data" and not path.startswith("viewer_data/")
    ]
    inventory["dir_paths"].extend([
        "viewer_data",
        "viewer_data/cache",
        "viewer_data/cache/nested",
    ])
    viewer_files = [
        f"viewer_data/auxiliary-{index:04d}.json"
        for index in range(file_count)
    ]
    inventory["file_paths"].extend(viewer_files)
    inventory["all_paths"].extend(viewer_files)
    inventory["total_entries"] = C.HF_TREE_TOTAL_ENTRIES
    inventory["file_count"] = len(inventory["file_paths"])
    inventory["dir_count"] = len(inventory["dir_paths"])
    return inventory


class TestViewerDataAccounting:
    """The verified full inventory visibly accounts for auxiliary viewer data."""

    def test_viewer_data_is_emitted_but_never_counted_as_task_root(self):
        coverage = compute_coverage(_inventory_with_viewer_data())
        assert coverage["excluded_auxiliary_data"] == {
            "top_level_path": "viewer_data",
            "present": True,
            "file_count": 2397,
            "directory_count": 3,
            "counted_as_task_root": False,
        }
        assert coverage["recognized_root_count"] == C.EXPECTED_ROOT_COUNT
        assert "viewer_data" not in {
            row.get("root")
            for row in coverage.get("matrix", [])
        }

    def test_verified_inventory_with_wrong_viewer_file_count_aborts(self):
        with pytest.raises(ValueError, match="viewer_data.*2,397|2397"):
            compute_coverage(_inventory_with_viewer_data(file_count=2396))


class TestProtocolSourceReferences:
    """Every source-derived protocol observation names verified source lines."""

    def test_protocol_observations_have_pinned_blob_line_references(self):
        acquired = _make_valid_acquired()
        protocol = audit_protocol(
            acquired["github"]["blob_contents"],
            acquired["github"]["entries"],
        )
        references = protocol["source_references"]
        expected = {
            "num_gpus_default",
            "cuda_device_requirement",
            "request_gpus_binding",
            "receives_num_hours",
            "solve_timeout_formula",
            "timeout_grace_minutes",
            "commit_sh_analysis.current_models_in_arrays",
            "commit_sh_analysis.current_benchmarks_in_arrays",
            "commit_sh_analysis.htcondor_mpi_is_branch",
            "commit_sh_analysis.htcondor_branch",
        }
        assert expected <= set(references)
        for key in expected:
            reference = references[key]
            source = acquired["github"]["blob_contents"][reference["path"]]
            git_object_sha1, raw_sha256 = C.PINNED_BLOBS[reference["path"]]
            assert reference["commit"] == C.GITHUB_PINNED_COMMIT
            assert reference["git_object_sha1"] == git_object_sha1
            assert reference["raw_sha256"] == raw_sha256
            source_lines = source.decode("utf-8").splitlines()
            assert reference["lines"]
            assert all(1 <= line <= len(source_lines) for line in reference["lines"])
            assert all(
                not source_lines[line - 1].lstrip().startswith("#")
                for line in reference["lines"]
            )

    def test_evaluation_directory_references_are_verified_tree_entries(self):
        acquired = _make_valid_acquired()
        protocol = audit_protocol(
            acquired["github"]["blob_contents"],
            acquired["github"]["entries"],
        )
        reference = protocol["source_references"]["evaluation_dirs_present"]
        assert reference == {
            "kind": "git-tree-entries",
            "paths": C.EXPECTED_EVAL_DIRS,
        }


def _resolve_evidence_pointer(
    documents: dict[str, Any],
    pointer: str,
) -> Any:
    document_name, separator, fragment = pointer.partition("#")
    assert separator == "#"
    current = documents[document_name]
    if not fragment:
        return current
    assert fragment.startswith("/")
    for token in fragment[1:].split("/"):
        token = token.replace("~1", "/").replace("~0", "~")
        if isinstance(current, list):
            current = current[int(token)]
        else:
            current = current[token]
    return current


def _rendered_evidence_documents() -> tuple[
    dict[str, str],
    dict[str, Any],
]:
    from posttrainbench_repro.audit import get_provenance
    from posttrainbench_repro.render import (
        render_index_html,
        render_poster_html,
        render_report_html,
    )

    acquired = _make_valid_acquired()
    coverage = compute_coverage(acquired["hf_inventory"])
    protocol = audit_protocol(
        acquired["github"]["blob_contents"],
        acquired["github"]["entries"],
    )
    coverage_output = {**coverage, "protocol": protocol}
    reward = audit_reward_hacking(acquired)
    claims = evaluate_claims(coverage, protocol, reward)
    provenance = get_provenance(acquired)
    documents = {
        "evidence/provenance.json": provenance,
        "evidence/coverage.json": coverage_output,
        "evidence/reward_hacking.json": reward,
        "evidence/claims.json": claims,
    }
    pages = {
        "index": render_index_html(
            provenance,
            coverage_output,
            reward,
            claims,
        ),
        "report": render_report_html(
            provenance,
            coverage_output,
            reward,
            claims,
        ),
        "poster": render_poster_html(
            provenance,
            coverage_output,
            reward,
            claims,
        ),
    }
    return pages, documents


_CLAIM_POINTERS = {
    f"evidence/claims.json#/{claim}/{field}"
    for claim in ("claim_1", "claim_2")
    for field in ("status", "text", "sha256", "summary")
}
_COUNT_POINTERS = {
    f"evidence/coverage.json#/{field}"
    for field in (
        "accepted_benchmark_count",
        "accepted_model_count",
        "recognized_task_count",
        "recognized_root_count",
        "recognized_root_cell_pairs",
        "duplicate_job_pairs",
        "missing_root_cell_pairs",
        "excluded_dirs_count",
    )
} | {
    "evidence/coverage.json#/excluded_auxiliary_data/file_count",
    "evidence/coverage.json#/excluded_auxiliary_data/directory_count",
}
_MATRIX_POINTERS = {
    f"evidence/coverage.json#/cell_counts/{benchmark}/{model_index}"
    for benchmark in sorted(C.EXPECTED_BENCHMARKS)
    for model_index in range(4)
}
_PROTOCOL_POINTERS = {
    f"evidence/coverage.json#/protocol/{field}"
    for field in (
        "num_gpus_default",
        "cuda_device_requirement",
        "request_gpus_binding",
        "receives_num_hours",
        "solve_timeout_formula",
        "timeout_grace_minutes",
        "evaluation_dir_count",
    )
} | {
    "evidence/coverage.json#/protocol/commit_sh_analysis/current_models_in_arrays",
    "evidence/coverage.json#/protocol/commit_sh_analysis/current_benchmarks_in_arrays",
    "evidence/coverage.json#/protocol/commit_sh_analysis/htcondor_mpi_is_branch/hours",
    "evidence/coverage.json#/protocol/commit_sh_analysis/htcondor_mpi_is_branch/gpus",
    "evidence/coverage.json#/protocol/commit_sh_analysis/htcondor_branch/ten_hour_jobs",
    "evidence/coverage.json#/protocol/commit_sh_analysis/htcondor_branch/one_hour_jobs",
}
_REWARD_STATUS_POINTERS = {
    f"evidence/reward_hacking.json#/{mode}/status"
    for mode in (
        "training_on_test_sets",
        "downloading_instruction_tuned_checkpoint",
        "using_discovered_api_key",
    )
}
_REWARD_DETAIL_POINTERS = {
    "evidence/reward_hacking.json#/training_on_test_sets/witness_path",
    "evidence/reward_hacking.json#/training_on_test_sets/witness_sha256",
    "evidence/reward_hacking.json#/training_on_test_sets/witness_bytes",
    "evidence/reward_hacking.json#/training_on_test_sets/observation_type",
    "evidence/reward_hacking.json#/downloading_instruction_tuned_checkpoint/judgment_path",
    "evidence/reward_hacking.json#/downloading_instruction_tuned_checkpoint/judgment_sha256",
    "evidence/reward_hacking.json#/downloading_instruction_tuned_checkpoint/judgment_bytes",
    "evidence/reward_hacking.json#/downloading_instruction_tuned_checkpoint/trace/size",
    "evidence/reward_hacking.json#/using_discovered_api_key/unavailability_reason",
} | {
    (
        "evidence/reward_hacking.json#/"
        f"downloading_instruction_tuned_checkpoint/safe_excerpts/{index}/{field}"
    )
    for index in range(3)
    for field in ("record", "json_pointer", "text", "sha256")
}


@pytest.mark.parametrize(
    ("page_name", "expected"),
    [
        (
            "index",
            _CLAIM_POINTERS
            | _COUNT_POINTERS
            | _MATRIX_POINTERS
            | _PROTOCOL_POINTERS
            | _REWARD_STATUS_POINTERS
            | _REWARD_DETAIL_POINTERS,
        ),
        (
            "report",
            _CLAIM_POINTERS
            | _COUNT_POINTERS
            | _MATRIX_POINTERS
            | _PROTOCOL_POINTERS
            | _REWARD_STATUS_POINTERS
            | _REWARD_DETAIL_POINTERS,
        ),
        (
            "poster",
            _CLAIM_POINTERS
            | _COUNT_POINTERS
            | _MATRIX_POINTERS
            | _REWARD_STATUS_POINTERS,
        ),
    ],
)
def test_every_visible_result_has_an_adjacent_resolvable_pointer(
    page_name,
    expected,
):
    pages, documents = _rendered_evidence_documents()
    page = pages[page_name]
    bindings = set(re.findall(r'data-evidence-pointer="([^"]+)"', page))
    assert expected <= bindings
    for pointer in bindings:
        _resolve_evidence_pointer(documents, pointer)
        assert pointer in page


def test_viewer_data_is_visible_with_resolvable_counts_on_every_page():
    pages, _ = _rendered_evidence_documents()
    for page in pages.values():
        assert "viewer_data" in page
        assert (
            'data-evidence-pointer="'
            'evidence/coverage.json#/excluded_auxiliary_data/file_count"'
        ) in page
        assert (
            'data-evidence-pointer="'
            'evidence/coverage.json#/excluded_auxiliary_data/directory_count"'
        ) in page


def _assert_adjacent_code_pointer(
    page: str,
    *,
    value: str,
    pointer: str,
) -> None:
    pattern = (
        rf'<span class="evidence-result" '
        rf'data-evidence-pointer="{re.escape(pointer)}">'
        rf"\s*<code>{re.escape(value)}</code>\s*"
        rf'<span class="ptr">{re.escape(pointer)}</span>\s*</span>'
    )
    assert re.search(pattern, page)


def test_index_ids_have_exact_adjacent_provenance_pointers():
    pages, _ = _rendered_evidence_documents()
    _assert_adjacent_code_pointer(
        pages["index"],
        value="UnjxMTe57e",
        pointer="evidence/provenance.json#/paper_id",
    )
    _assert_adjacent_code_pointer(
        pages["index"],
        value="cb04ab1a-a526-4137-862b-a26d68563737",
        pointer="evidence/provenance.json#/attempt_id",
    )


def test_poster_arxiv_has_exact_adjacent_provenance_pointer():
    pages, _ = _rendered_evidence_documents()
    _assert_adjacent_code_pointer(
        pages["poster"],
        value="2603.08640v2",
        pointer="evidence/provenance.json#/arxiv_id",
    )


def test_protocol_values_render_adjacent_source_references():
    pages, _ = _rendered_evidence_documents()
    for page_name in ("index", "report"):
        page = pages[page_name]
        for key in (
            "num_gpus_default",
            "cuda_device_requirement",
            "request_gpus_binding",
            "solve_timeout_formula",
            "commit_sh_analysis.htcondor_mpi_is_branch",
            "commit_sh_analysis.htcondor_branch",
        ):
            pointer = (
                "evidence/coverage.json#/protocol/source_references/"
                + key.replace("~", "~0").replace("/", "~1")
            )
            assert f'data-evidence-pointer="{pointer}"' in page
        assert C.GITHUB_PINNED_COMMIT in page