File size: 118,756 Bytes
3756a76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
"""Patcher for the pinned full TMAX Harbor source (PrimeIntellect-ai/prime-tasks, datasets/tmax).

    python data/tmax/patch.py --source tasks.parquet --output NEW_DIR

Writes NEW_DIR/tasks.parquet, the shared patch report (tasks.manifest.json,
tasks.archive.parquet) and tmax_patch_manifest.json with the image analysis.

Build compatibility (every task). The Apptainer builder differs from Docker in
two ways that stop every upstream TMAX Dockerfile before its first command:

* files copied to /tmp are hidden while RUN steps execute, so
  `RUN bash /tmp/post_install.sh` finds no script. The install scripts are
  copied to /opt/tmax-build instead, run from there, and that directory is
  removed by the last RUN step in place of the upstream `rm` of the script;
* ENV values are exported unexpanded, so `ENV PATH=...:$PATH` (v2 tasks) leaves
  no system directory on PATH. The value is written as Docker substitutes it
  for ubuntu:22.04.

The scripts themselves are unchanged and no script refers to its own path,
with two exceptions:

* ten v2 tasks install Ubuntu's nodejs/npm in base_install.sh and then
  NodeSource's nodejs in post_install.sh; dpkg refuses to overwrite
  /usr/include/node/common.gypi of libnode-dev, so those images kept Node.js 12
  instead of the requested version (task_000110 also left MongoDB unconfigured
  and failed the build). The Ubuntu Node.js packages are purged right before
  the NodeSource setup line;
* tasks that replace /usr/bin/python3 with a wrapper starting their services
  on the first python3 call: a service backgrounded without an output redirect
  keeps the verifier's `pytest | tee` pipe open until the verifier times out.
  Its output is sent to /dev/null.

TASK_REPAIRS fixes single upstream data bugs that make a task unsolvable, each
with its reason in the patch report.

Shared image. Upstream builds one image per task: environment/post_install.sh
first installs packages and then creates the task (files, small compiles, user,
permissions). The "shared image" transformation splits that script:

* the leading package installation stays in the image, written in a canonical
  form, so that all tasks with the same install commands share one image;
* the rest runs unchanged from setup_files/ when the container starts, after
  the v2 fixtures have been copied to the paths upstream copies them to.

The shared runner starts setup_files/setup.sh only for tasks it has a setup
trigger for (validation.stages.task_setup.detect). TMAX has no solution/, so
this patcher asks the runner whether a task with setup_files/setup.sh and no
solution/ gets a setup command. If not, no task is changed: files moved to
setup_files/ would never be executed and the tasks would start empty. The
analysis is still written to tmax_patch_manifest.json.

Instructions, tests/, task.toml and the install commands are never changed.
The Dockerfile constants below are the upstream texts; build compatibility is
applied last, to whatever Dockerfile a task then has.
"""

# Support both direct execution and python -m data.<source>.patch.
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parents[2]))

import argparse
from collections import Counter, defaultdict
import hashlib
import io
import json
import re
import shlex
import tarfile
import tempfile

DATASET = "PrimeIntellect-ai/prime-tasks/datasets/tmax"
REVISION = "27de1c1b1ed6279b607e105ff9cb598e177ad938"
SOURCE_URL = f"https://github.com/PrimeIntellect-ai/prime-tasks/tree/{REVISION}/datasets/tmax"
TASKS = 14600

LEGACY_DOCKERFILE = (
    "FROM ubuntu:22.04\n\n"
    "ENV DEBIAN_FRONTEND=noninteractive\n\n"
    "COPY post_install.sh /tmp/post_install.sh\n"
    "RUN bash /tmp/post_install.sh && rm /tmp/post_install.sh\n")
# The six upstream v2 Dockerfiles without their COPY _fixtures/... lines.
V2_DOCKERFILE = (
    "FROM ubuntu:22.04\n\n"
    "ENV DEBIAN_FRONTEND=noninteractive\n"
    "ENV LC_ALL=C.UTF-8\n"
    "ENV LANG=C.UTF-8\n"
    "ENV GOPATH=/home/user/go\n"
    "ENV PATH=/home/user/go/bin:/home/user/.cargo/bin:$PATH\n\n"
    "COPY base_install.sh /tmp/base_install.sh\n"
    "RUN bash /tmp/base_install.sh && rm /tmp/base_install.sh\n\n"
    "COPY post_install.sh /tmp/post_install.sh\n"
    "RUN bash /tmp/post_install.sh && rm /tmp/post_install.sh\n")
# base_install.sh of 5,134 of the 5,135 v2 tasks.
V2_BASE_INSTALL_SHA256 = "0e71b3e06fcf8cc6f9d95ba2e9664dfbba7786949a0e21b810ada563ae27a637"
FIXTURE_COPY = re.compile(r"^COPY (_fixtures(/\S+)) (\S+)\n", re.M)

# Tasks that stay on their upstream per-task image although the rules below would share
# them, with the measured reason (for example a setup time from --review-setup).
UNSHARED = {
    task: "Stage-1 setup review exceeded its timing budget while running PyInstaller. "
          "Keep the original task installation, including binary compilation, in the image "
          "instead of repeating it at container startup."
    for task in (
        "task_001114_f96b0aef", "task_002002_fa80653c", "task_002271_c43206b1",
        "task_003850_7712812b", "task_004476_43b115e3", "task_004647_5bcf5b9d",
    )
}
UNSHARED.update({
    task: "Stage-1 setup review exceeded its timing budget during expensive fixture generation. "
          "Generate the original fixtures once during image construction instead of at every startup."
    for task in (
        "task_000779_e7ae5af2", "task_000974_29b56612", "task_001324_6e21912a",
        "task_001703_4a3af2c3", "task_001732_12bce6f9", "task_001989_f4094e34",
        "task_004152_5799b089",
    )
})

# These upstream hooks execute on every Apptainer command, including uploads.
# Start the unchanged service once at the end of task setup instead.
SERVICE_SETUP = {
    "task_002392_b43fd016": ("99-app.sh", "/home/user/app.py", 8005),
    "task_003417_84201a39": ("99-start-server.sh", "/home/user/app.py", 5000),
    "task_004543_d2b19469": ("99-server.sh", "/app/server.py", 8192),
}
SERVICE_REASON = (
    "An Apptainer environment hook launched a server during every container command. "
    "The unchanged server now starts once at the end of task setup, with redirected I/O "
    "and a bounded TCP readiness check; no environment hook is installed."
)


def service_setup_compatible(files, task):
    hook, script, port = SERVICE_SETUP[task]
    name = "setup_files/task_install.sh"
    if name not in files:
        raise ValueError(f"{task}: service repair requires runtime setup")
    text, mode = files[name]
    pattern = (r"(?m)^    cat << 'EOF' > /\.singularity\.d/env/" + re.escape(hook)
               + r"\n.*?^EOF\n    chmod \+x /\.singularity\.d/env/" + re.escape(hook) + r"\n")
    text, count = re.subn(pattern, "", text.decode(), flags=re.S)
    if count != 1:
        raise ValueError(f"{task}: expected exactly one service environment hook")
    text += f'''
# Start once during setup; container exec must not start additional servers.
python3 - <<'TMAX_SERVICE_READY'
import socket
import subprocess
import time

def ready():
    try:
        with socket.create_connection(("127.0.0.1", {port}), timeout=0.2):
            return True
    except OSError:
        return False

if not ready():
    with open("/tmp/tmax-service-{port}.log", "ab", buffering=0) as log:
        process = subprocess.Popen(["python3", {script!r}], stdin=subprocess.DEVNULL,
                                   stdout=log, stderr=subprocess.STDOUT, start_new_session=True)
    deadline = time.monotonic() + 10
    while not ready():
        if process.poll() is not None or time.monotonic() >= deadline:
            process.terminate() if process.poll() is None else None
            raise SystemExit("Service failed readiness check; see /tmp/tmax-service-{port}.log")
        time.sleep(0.1)
TMAX_SERVICE_READY
'''
    files[name] = (text.encode(), mode)

# --- Apptainer build compatibility ---------------------------------------------------------

BUILD_DIR = "/opt/tmax-build"
BUILD_SCRIPT = re.compile(r"/tmp/((?:base|post)_install\.sh)\b")
LAST_RUN = f"RUN bash {BUILD_DIR}/post_install.sh && rm {BUILD_DIR}/post_install.sh\n"
# PATH from the image configuration of ubuntu:22.04 (linux/amd64).
UBUNTU_PATH = "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
V2_PATH = "ENV PATH=/home/user/go/bin:/home/user/.cargo/bin:"
COMPATIBILITY_LABEL = "apptainer-build-compatibility"
COMPATIBILITY_REASON = {
    "legacy": (
        "The Apptainer builder hides files copied to /tmp while RUN steps execute, so the image build "
        "stopped at `bash /tmp/post_install.sh: No such file or directory`. The unchanged script is "
        f"copied to {BUILD_DIR}, run from there, and the directory is removed by the same RUN step."),
    "v2": (
        "The Apptainer builder hides files copied to /tmp while RUN steps execute and exports ENV values "
        "unexpanded, so the image build stopped at `bash: not found` (literal $PATH) and at the missing "
        f"/tmp install scripts. The unchanged scripts are copied to {BUILD_DIR}, run from there, and the "
        "directory is removed by the last RUN step. ENV PATH carries the value Docker substitutes for "
        "ubuntu:22.04."),
}


def build_compatible(dockerfile):
    """The Dockerfile with its install scripts outside /tmp and $PATH written out."""
    text = BUILD_SCRIPT.sub(BUILD_DIR + r"/\1", dockerfile)
    if text.count(LAST_RUN) != 1 or not text.endswith(LAST_RUN) or "/tmp/" in text:
        raise ValueError("unexpected Dockerfile: install script steps")
    text = text[:-len(LAST_RUN)] + f"RUN bash {BUILD_DIR}/post_install.sh && rm -rf {BUILD_DIR}\n"
    text = text.replace(V2_PATH + "$PATH\n", V2_PATH + UBUNTU_PATH + "\n")
    if "$" in text:
        raise ValueError("unexpected Dockerfile: variable reference")
    return text


NODESOURCE_SETUP = re.compile(r"^(\s*)curl -fsSL https://deb\.nodesource\.com/setup_\d+\.x \| (?:sudo -E )?bash -\s*$",
                              re.M)
DISTRO_NODE = re.compile(r"^\s*nodejs npm\b", re.M)
NODE_PURGE = "apt-get purge -y nodejs npm libnode-dev libnode72\n"
NODE_LABEL = "nodesource-after-distro-node"
NODE_REASON = (
    "base_install.sh installs Ubuntu's nodejs/npm and post_install.sh then NodeSource's nodejs, which dpkg "
    "refuses (`trying to overwrite '/usr/include/node/common.gypi', which is also in package libnode-dev`), "
    "so the image kept Node.js 12. The Ubuntu Node.js packages are purged before the NodeSource setup.")


def nodesource_compatible(files):
    """post_install.sh with the distro Node.js purged before NodeSource's, or None if not needed."""
    base = files.get("environment/base_install.sh")
    post = files.get("environment/post_install.sh")
    if not base or not post or not DISTRO_NODE.search(base[0].decode()):
        return None
    text = post[0].decode()
    if len(NODESOURCE_SETUP.findall(text)) != 1:
        return None
    return NODESOURCE_SETUP.sub(lambda m: m.group(1) + NODE_PURGE + m.group(0), text).encode()


PYTHON_WRAPPER = re.compile(r"cat\s*<<\s*'?EOF'?\s*>\s*/usr/bin/python3\b")
UNREDIRECTED_BACKGROUND = re.compile(r"^(?P<command>[^#\n>]*[^\s&|>])\s*&\s*$", re.M)
WRAPPER_LABEL = "python3-wrapper-background-output"
WRAPPER_REASON = (
    "The task replaces /usr/bin/python3 with a wrapper that starts its services on the first python3 call, "
    "which in a no-op run is the verifier's `python3 -m pytest ... | tee`. A service started in the "
    "background without an output redirect keeps that pipe open, so tee never finishes and the verifier "
    "timed out after 120 s although the tests had failed within milliseconds. Its output now goes to /dev/null.")


def wrapper_output_compatible(files):
    """{name: script} with background services of a python3 wrapper task detached from stdout."""
    changed = {}
    for name in ("environment/post_install.sh", "setup_files/task_install.sh"):
        if name not in files:
            continue
        text = files[name][0].decode()
        if not PYTHON_WRAPPER.search(text):
            continue
        new = UNREDIRECTED_BACKGROUND.sub(lambda m: m["command"] + " >/dev/null 2>&1 &", text)
        if new != text:
            changed[name] = new.encode()
    return changed


# Task-specific data repairs: (file, upstream text, replacement, occurrences, reason).
TASK_REPAIRS = {
    "task_003383_a3d7928b": ("environment/post_install.sh",
                             "ALA A {i+1:4d}    ", "ALA A{i+1:4d}    ", 2,  # CA and CB lines
                             "The generated PDB lines carry one space too many after the chain ID, so every column "
                             "from the residue number on is shifted by one. The verifier and the task's vendored "
                             "parser both read the B-factor from columns 61-66 (line[60:66]) and failed on "
                             "`could not convert string to float: '0 22.4'` whatever the agent did. The residue "
                             "number now starts in column 23 as in the PDB format."),
}
# Small verifier repairs preserve the requested behavior and existing comparisons.
TASK_REPAIRS.update({'task_001217_56df613c': ('tests/test_final_state.py',
                          '        agent_success = (agent_proc.returncode == 0)',
                          '        assert agent_proc.returncode == 0, f"Agent parser crashed: '
                          '{agent_proc.stderr}"',
                          1,
                          'Require successful exit before comparing parser output; matching output printed '
                          'before a crash must not earn reward 1.'),
 'task_000161_a8d0d6f0': ('tests/test_final_state.py',
                          "repr(f + r'\\n')",
                          "repr(f + chr(92) + 'n')",
                          1,
                          'Python 3.10 compatibility: preserve the diagnostic text and TCP assertion while '
                          'removing a backslash from the f-string expression.'),
 'task_004436_d43bdd17': ('tests/test_final_state.py',
                          "line.rstrip('\\n')",
                          'line.rstrip(chr(10))',
                          1,
                          'Python 3.10 compatibility: preserve newline stripping and expected output while '
                          'removing a backslash from the f-string expression.')})

ARCHIVED = {'task_003787_64f41fe9': {'category': 'nop-reward-one',
                          'reason': 'Only test_dummy exists and asserts True. Reviewed against CLAIX '
                                    'superset2-run-c stage-3 evidence on 2026-10-18; archived with user '
                                    'authorization. Original payload retained.'},
 'task_004430_d62c7294': {'category': 'runtime-nested-docker',
                          'reason': 'E               subprocess.CalledProcessError: Command '
                                    "'['docker-compose', '-f', '/app/docker-compose.yml', 'up', '-d']' "
                                    'returned non-zero exit status 1. Reviewed against CLAIX superset2-run-c '
                                    'stage-3 evidence on 2026-10-18; archived with user authorization. '
                                    'Original payload retained.'},
 'task_000269_902c4ab4': {'category': 'verifier-has-no-tests',
                          'reason': 'test_final_state.py contains example/application functions, no pytest '
                                    'tests; collected zero items. Reviewed against CLAIX superset2-run-c '
                                    'stage-3 evidence on 2026-10-18; archived with user authorization. '
                                    'Original payload retained.'},
 'task_003634_5d3c43fa': {'category': 'nop-reward-one',
                          'reason': 'Load-test helper prints hard-coded successful=5000 and '
                                    'throughput=1250.4 without exercising services. Reviewed against CLAIX '
                                    'superset2-run-c stage-3 evidence on 2026-10-18; archived with user '
                                    'authorization. Original payload retained.'},
 'task_000411_42185240': {'category': 'verifier-has-no-tests',
                          'reason': 'test_final_state.py contains example/application functions, no pytest '
                                    'tests; collected zero items. Reviewed against CLAIX superset2-run-c '
                                    'stage-3 evidence on 2026-10-18; archived with user authorization. '
                                    'Original payload retained.'},
 'task_003081_352aefca': {'category': 'verifier-has-no-tests',
                          'reason': 'test_final_state.py contains example/application functions, no pytest '
                                    'tests; collected zero items. Reviewed against CLAIX superset2-run-c '
                                    'stage-3 evidence on 2026-10-18; archived with user authorization. '
                                    'Original payload retained.'},
 'task_002385_86e1c6e3': {'category': 'nop-reward-one',
                          'reason': 'Scaffold and unfinished oracle both emit empty output, so equivalence '
                                    'passes; CMake builds no meaningful target. Reviewed against CLAIX '
                                    'superset2-run-c stage-3 evidence on 2026-10-18; archived with user '
                                    'authorization. Original payload retained.'},
 'task_001860_c56b811b': {'category': 'nop-reward-one',
                          'reason': 'Both corpus tests skip because corpus directories are missing; pytest '
                                    'exits zero and reward is 1. Reviewed against CLAIX superset2-run-c '
                                    'stage-3 evidence on 2026-10-18; archived with user authorization. '
                                    'Original payload retained.'},
 'task_000130_0f11589b': {'category': 'nop-reward-one',
                          'reason': 'Corpus directories are empty, loops make no assertions about real '
                                    'examples; empty extraction tree also passes. Reviewed against CLAIX '
                                    'superset2-run-c stage-3 evidence on 2026-10-18; archived with user '
                                    'authorization. Original payload retained.'},
 'task_003252_72cbd39e': {'category': 'verifier-has-no-tests',
                          'reason': 'test_final_state.py contains example/application functions, no pytest '
                                    'tests; collected zero items. Reviewed against CLAIX superset2-run-c '
                                    'stage-3 evidence on 2026-10-18; archived with user authorization. '
                                    'Original payload retained.'}}

VERIFIER_REPAIRS = {'task_000838_449deb25': {'edits': [('subprocess.run(["bash", script_path], check=False)',
                                     '_start_services(["bash", script_path], 8080)',
                                     1),
                                    ('    subprocess.run(["pkill", "-f", "nginx"], check=False)',
                                     '    # Verifier-owned process groups are cleaned up by '
                                     '_tmax_cleanup_services.',
                                     1),
                                    ('    subprocess.run(["pkill", "-f", "gunicorn"], check=False)',
                                     '    # Verifier-owned process groups are cleaned up by '
                                     '_tmax_cleanup_services.',
                                     1),
                                    ('    subprocess.run(["pkill", "-f", "redis-server"], check=False)',
                                     '    # Verifier-owned process groups are cleaned up by '
                                     '_tmax_cleanup_services.',
                                     1)],
                          'append': '\n'
                                    '# Verifier-owned services must not hold pytest output pipes open.\n'
                                    'import signal\n'
                                    'import socket\n'
                                    'import pytest\n'
                                    '\n'
                                    '_tmax_services = []\n'
                                    '\n'
                                    'def _start_services(command, port, cwd=None):\n'
                                    '    import time\n'
                                    "    log = open('/tmp/tmax-verifier-services.log', 'ab', "
                                    'buffering=0)\n'
                                    '    process = subprocess.Popen(command, cwd=cwd, '
                                    'stdin=subprocess.DEVNULL,\n'
                                    '                               stdout=log, '
                                    'stderr=subprocess.STDOUT,\n'
                                    '                               start_new_session=True)\n'
                                    '    log.close()\n'
                                    '    _tmax_services.append(process)\n'
                                    '    deadline = time.monotonic() + 10\n'
                                    '    while time.monotonic() < deadline:\n'
                                    '        try:\n'
                                    "            with socket.create_connection(('127.0.0.1', port), "
                                    'timeout=0.2):\n'
                                    '                return\n'
                                    '        except OSError:\n'
                                    '            time.sleep(0.1)\n'
                                    "    pytest.fail('Service did not become ready within 10 seconds; "
                                    "see /tmp/tmax-verifier-services.log')\n"
                                    '\n'
                                    "@pytest.fixture(scope='module', autouse=True)\n"
                                    'def _tmax_cleanup_services():\n'
                                    '    yield\n'
                                    '    for process in reversed(_tmax_services):\n'
                                    '        try:\n'
                                    '            os.killpg(process.pid, signal.SIGTERM)\n'
                                    '        except ProcessLookupError:\n'
                                    '            pass\n'
                                    '        try:\n'
                                    '            process.wait(timeout=2)\n'
                                    '        except subprocess.TimeoutExpired:\n'
                                    '            pass\n'
                                    '        try:\n'
                                    '            os.killpg(process.pid, signal.SIGKILL)\n'
                                    '        except ProcessLookupError:\n'
                                    '            pass\n'
                                    '        process.wait(timeout=2)\n'
                                    '    _tmax_services.clear()\n',
                          'reason': 'Start verifier services asynchronously with redirected output, a '
                                    'bounded readiness check, and process-group cleanup. Preserve all '
                                    'behavioral assertions.'},
 'task_001471_f1a04288': {'edits': [('subprocess.run(["/app/start_services.sh"], capture_output=True)',
                                     '_start_services(["/app/start_services.sh"], 8443)',
                                     1)],
                          'append': '\n'
                                    '# Verifier-owned services must not hold pytest output pipes open.\n'
                                    'import signal\n'
                                    'import socket\n'
                                    'import pytest\n'
                                    '\n'
                                    '_tmax_services = []\n'
                                    '\n'
                                    'def _start_services(command, port, cwd=None):\n'
                                    '    import time\n'
                                    "    log = open('/tmp/tmax-verifier-services.log', 'ab', "
                                    'buffering=0)\n'
                                    '    process = subprocess.Popen(command, cwd=cwd, '
                                    'stdin=subprocess.DEVNULL,\n'
                                    '                               stdout=log, '
                                    'stderr=subprocess.STDOUT,\n'
                                    '                               start_new_session=True)\n'
                                    '    log.close()\n'
                                    '    _tmax_services.append(process)\n'
                                    '    deadline = time.monotonic() + 10\n'
                                    '    while time.monotonic() < deadline:\n'
                                    '        try:\n'
                                    "            with socket.create_connection(('127.0.0.1', port), "
                                    'timeout=0.2):\n'
                                    '                return\n'
                                    '        except OSError:\n'
                                    '            time.sleep(0.1)\n'
                                    "    pytest.fail('Service did not become ready within 10 seconds; "
                                    "see /tmp/tmax-verifier-services.log')\n"
                                    '\n'
                                    "@pytest.fixture(scope='module', autouse=True)\n"
                                    'def _tmax_cleanup_services():\n'
                                    '    yield\n'
                                    '    for process in reversed(_tmax_services):\n'
                                    '        try:\n'
                                    '            os.killpg(process.pid, signal.SIGTERM)\n'
                                    '        except ProcessLookupError:\n'
                                    '            pass\n'
                                    '        try:\n'
                                    '            process.wait(timeout=2)\n'
                                    '        except subprocess.TimeoutExpired:\n'
                                    '            pass\n'
                                    '        try:\n'
                                    '            os.killpg(process.pid, signal.SIGKILL)\n'
                                    '        except ProcessLookupError:\n'
                                    '            pass\n'
                                    '        process.wait(timeout=2)\n'
                                    '    _tmax_services.clear()\n',
                          'reason': 'Start verifier services asynchronously with redirected output, a '
                                    'bounded readiness check, and process-group cleanup. Preserve all '
                                    'behavioral assertions.'},
 'task_003553_1185f7b8': {'edits': [('subprocess.run(["bash", "/app/start_services.sh"], check=False)',
                                     '_start_services(["bash", "/app/start_services.sh"], 8080)',
                                     1)],
                          'append': '\n'
                                    '# Verifier-owned services must not hold pytest output pipes open.\n'
                                    'import signal\n'
                                    'import socket\n'
                                    'import pytest\n'
                                    '\n'
                                    '_tmax_services = []\n'
                                    '\n'
                                    'def _start_services(command, port, cwd=None):\n'
                                    '    import time\n'
                                    "    log = open('/tmp/tmax-verifier-services.log', 'ab', "
                                    'buffering=0)\n'
                                    '    process = subprocess.Popen(command, cwd=cwd, '
                                    'stdin=subprocess.DEVNULL,\n'
                                    '                               stdout=log, '
                                    'stderr=subprocess.STDOUT,\n'
                                    '                               start_new_session=True)\n'
                                    '    log.close()\n'
                                    '    _tmax_services.append(process)\n'
                                    '    deadline = time.monotonic() + 10\n'
                                    '    while time.monotonic() < deadline:\n'
                                    '        try:\n'
                                    "            with socket.create_connection(('127.0.0.1', port), "
                                    'timeout=0.2):\n'
                                    '                return\n'
                                    '        except OSError:\n'
                                    '            time.sleep(0.1)\n'
                                    "    pytest.fail('Service did not become ready within 10 seconds; "
                                    "see /tmp/tmax-verifier-services.log')\n"
                                    '\n'
                                    "@pytest.fixture(scope='module', autouse=True)\n"
                                    'def _tmax_cleanup_services():\n'
                                    '    yield\n'
                                    '    for process in reversed(_tmax_services):\n'
                                    '        try:\n'
                                    '            os.killpg(process.pid, signal.SIGTERM)\n'
                                    '        except ProcessLookupError:\n'
                                    '            pass\n'
                                    '        try:\n'
                                    '            process.wait(timeout=2)\n'
                                    '        except subprocess.TimeoutExpired:\n'
                                    '            pass\n'
                                    '        try:\n'
                                    '            os.killpg(process.pid, signal.SIGKILL)\n'
                                    '        except ProcessLookupError:\n'
                                    '            pass\n'
                                    '        process.wait(timeout=2)\n'
                                    '    _tmax_services.clear()\n',
                          'reason': 'Start verifier services asynchronously with redirected output, a '
                                    'bounded readiness check, and process-group cleanup. Preserve all '
                                    'behavioral assertions.'},
 'task_003701_a5286da6': {'edits': [("subprocess.run(['bash', '/app/start_services.sh'], "
                                     'capture_output=True)',
                                     "_start_services(['bash', '/app/start_services.sh'], 8080)",
                                     1)],
                          'append': '\n'
                                    '# Verifier-owned services must not hold pytest output pipes open.\n'
                                    'import signal\n'
                                    'import socket\n'
                                    'import pytest\n'
                                    '\n'
                                    '_tmax_services = []\n'
                                    '\n'
                                    'def _start_services(command, port, cwd=None):\n'
                                    '    import time\n'
                                    "    log = open('/tmp/tmax-verifier-services.log', 'ab', "
                                    'buffering=0)\n'
                                    '    process = subprocess.Popen(command, cwd=cwd, '
                                    'stdin=subprocess.DEVNULL,\n'
                                    '                               stdout=log, '
                                    'stderr=subprocess.STDOUT,\n'
                                    '                               start_new_session=True)\n'
                                    '    log.close()\n'
                                    '    _tmax_services.append(process)\n'
                                    '    deadline = time.monotonic() + 10\n'
                                    '    while time.monotonic() < deadline:\n'
                                    '        try:\n'
                                    "            with socket.create_connection(('127.0.0.1', port), "
                                    'timeout=0.2):\n'
                                    '                return\n'
                                    '        except OSError:\n'
                                    '            time.sleep(0.1)\n'
                                    "    pytest.fail('Service did not become ready within 10 seconds; "
                                    "see /tmp/tmax-verifier-services.log')\n"
                                    '\n'
                                    "@pytest.fixture(scope='module', autouse=True)\n"
                                    'def _tmax_cleanup_services():\n'
                                    '    yield\n'
                                    '    for process in reversed(_tmax_services):\n'
                                    '        try:\n'
                                    '            os.killpg(process.pid, signal.SIGTERM)\n'
                                    '        except ProcessLookupError:\n'
                                    '            pass\n'
                                    '        try:\n'
                                    '            process.wait(timeout=2)\n'
                                    '        except subprocess.TimeoutExpired:\n'
                                    '            pass\n'
                                    '        try:\n'
                                    '            os.killpg(process.pid, signal.SIGKILL)\n'
                                    '        except ProcessLookupError:\n'
                                    '            pass\n'
                                    '        process.wait(timeout=2)\n'
                                    '    _tmax_services.clear()\n',
                          'reason': 'Start verifier services asynchronously with redirected output, a '
                                    'bounded readiness check, and process-group cleanup. Preserve all '
                                    'behavioral assertions.'},
 'task_003884_d9647f63': {'edits': [('subprocess.run(["bash", "start_services.sh"], '
                                     'cwd="/home/user/iot_pipeline", capture_output=True)',
                                     '_start_services(["bash", "start_services.sh"], 8080, '
                                     'cwd="/home/user/iot_pipeline")',
                                     1)],
                          'append': '\n'
                                    '# Verifier-owned services must not hold pytest output pipes open.\n'
                                    'import signal\n'
                                    'import socket\n'
                                    'import pytest\n'
                                    '\n'
                                    '_tmax_services = []\n'
                                    '\n'
                                    'def _start_services(command, port, cwd=None):\n'
                                    '    import time\n'
                                    "    log = open('/tmp/tmax-verifier-services.log', 'ab', "
                                    'buffering=0)\n'
                                    '    process = subprocess.Popen(command, cwd=cwd, '
                                    'stdin=subprocess.DEVNULL,\n'
                                    '                               stdout=log, '
                                    'stderr=subprocess.STDOUT,\n'
                                    '                               start_new_session=True)\n'
                                    '    log.close()\n'
                                    '    _tmax_services.append(process)\n'
                                    '    deadline = time.monotonic() + 10\n'
                                    '    while time.monotonic() < deadline:\n'
                                    '        try:\n'
                                    "            with socket.create_connection(('127.0.0.1', port), "
                                    'timeout=0.2):\n'
                                    '                return\n'
                                    '        except OSError:\n'
                                    '            time.sleep(0.1)\n'
                                    "    pytest.fail('Service did not become ready within 10 seconds; "
                                    "see /tmp/tmax-verifier-services.log')\n"
                                    '\n'
                                    "@pytest.fixture(scope='module', autouse=True)\n"
                                    'def _tmax_cleanup_services():\n'
                                    '    yield\n'
                                    '    for process in reversed(_tmax_services):\n'
                                    '        try:\n'
                                    '            os.killpg(process.pid, signal.SIGTERM)\n'
                                    '        except ProcessLookupError:\n'
                                    '            pass\n'
                                    '        try:\n'
                                    '            process.wait(timeout=2)\n'
                                    '        except subprocess.TimeoutExpired:\n'
                                    '            pass\n'
                                    '        try:\n'
                                    '            os.killpg(process.pid, signal.SIGKILL)\n'
                                    '        except ProcessLookupError:\n'
                                    '            pass\n'
                                    '        process.wait(timeout=2)\n'
                                    '    _tmax_services.clear()\n',
                          'reason': 'Start verifier services asynchronously with redirected output, a '
                                    'bounded readiness check, and process-group cleanup. Preserve all '
                                    'behavioral assertions.'},
 'task_008794_911eb529': {'edits': [('    with tempfile.TemporaryDirectory() as temp_dir:\n'
                                     '        clone_dir = os.path.join(temp_dir, "test_clone")\n'
                                     '\n'
                                     '        # Clone the repo\n'
                                     '        res = subprocess.run(["git", "clone", '
                                     '"/home/user/deploy.git", clone_dir], capture_output=True)\n'
                                     '        assert res.returncode == 0, "Failed to clone '
                                     'deploy.git."\n'
                                     '\n'
                                     '        # Configure git identity for the test clone\n'
                                     '        subprocess.run(["git", "-C", clone_dir, "config", '
                                     '"user.email", "test@test.com"])\n'
                                     '        subprocess.run(["git", "-C", clone_dir, "config", '
                                     '"user.name", "Test User"])\n'
                                     '\n'
                                     '        # Test valid commit\n'
                                     '        mounts_file = os.path.join(clone_dir, "mounts.fstab")\n'
                                     '        with open(mounts_file, "w") as f:\n'
                                     '            f.write("overlay /var/lib/docker/overlay2 overlay '
                                     'defaults 0 0\\n")\n'
                                     '\n'
                                     '        subprocess.run(["git", "-C", clone_dir, "add", '
                                     '"mounts.fstab"])\n'
                                     '        subprocess.run(["git", "-C", clone_dir, "commit", "-m", '
                                     '"Valid mount"])\n'
                                     '\n'
                                     '        res = subprocess.run(["git", "-C", clone_dir, "push", '
                                     '"origin", "master"], capture_output=True, text=True)\n'
                                     '        assert res.returncode == 0, f"Pushing a valid mount '
                                     'failed: {res.stderr}"\n'
                                     '\n'
                                     '        # Test invalid commit\n'
                                     '        with open(mounts_file, "w") as f:\n'
                                     '            f.write("none /etc tmpfs defaults 0 0\\n")\n'
                                     '\n'
                                     '        subprocess.run(["git", "-C", clone_dir, "add", '
                                     '"mounts.fstab"])\n'
                                     '        subprocess.run(["git", "-C", clone_dir, "commit", "-m", '
                                     '"Invalid mount"])\n'
                                     '\n'
                                     '        res = subprocess.run(["git", "-C", clone_dir, "push", '
                                     '"origin", "master"], capture_output=True, text=True)\n'
                                     '        assert res.returncode != 0, "Pushing an invalid mount '
                                     '(/etc) succeeded, but it should have failed."\n'
                                     '\n'
                                     '    # Stop SMTP server\n'
                                     '    smtp_stop.set()\n'
                                     '    t.join(timeout=2)\n'
                                     '\n',
                                     '    try:\n'
                                     '        with tempfile.TemporaryDirectory() as temp_dir:\n'
                                     '            clone_dir = os.path.join(temp_dir, "test_clone")\n'
                                     '    \n'
                                     '            # Clone the repo\n'
                                     '            res = subprocess.run(["git", "clone", '
                                     '"/home/user/deploy.git", clone_dir], capture_output=True)\n'
                                     '            assert res.returncode == 0, "Failed to clone '
                                     'deploy.git."\n'
                                     '    \n'
                                     '            # Configure git identity for the test clone\n'
                                     '            subprocess.run(["git", "-C", clone_dir, "config", '
                                     '"user.email", "test@test.com"])\n'
                                     '            subprocess.run(["git", "-C", clone_dir, "config", '
                                     '"user.name", "Test User"])\n'
                                     '    \n'
                                     '            # Test valid commit\n'
                                     '            mounts_file = os.path.join(clone_dir, '
                                     '"mounts.fstab")\n'
                                     '            with open(mounts_file, "w") as f:\n'
                                     '                f.write("overlay /var/lib/docker/overlay2 overlay '
                                     'defaults 0 0\\n")\n'
                                     '    \n'
                                     '            subprocess.run(["git", "-C", clone_dir, "add", '
                                     '"mounts.fstab"])\n'
                                     '            subprocess.run(["git", "-C", clone_dir, "commit", '
                                     '"-m", "Valid mount"])\n'
                                     '    \n'
                                     '            res = subprocess.run(["git", "-C", clone_dir, "push", '
                                     '"origin", "master"], capture_output=True, text=True)\n'
                                     '            assert res.returncode == 0, f"Pushing a valid mount '
                                     'failed: {res.stderr}"\n'
                                     '    \n'
                                     '            # Test invalid commit\n'
                                     '            with open(mounts_file, "w") as f:\n'
                                     '                f.write("none /etc tmpfs defaults 0 0\\n")\n'
                                     '    \n'
                                     '            subprocess.run(["git", "-C", clone_dir, "add", '
                                     '"mounts.fstab"])\n'
                                     '            subprocess.run(["git", "-C", clone_dir, "commit", '
                                     '"-m", "Invalid mount"])\n'
                                     '    \n'
                                     '            res = subprocess.run(["git", "-C", clone_dir, "push", '
                                     '"origin", "master"], capture_output=True, text=True)\n'
                                     '            assert res.returncode != 0, "Pushing an invalid mount '
                                     '(/etc) succeeded, but it should have failed."\n'
                                     '    finally:\n'
                                     '        smtp_stop.set()\n'
                                     '        t.join(timeout=3)\n'
                                     '\n',
                                     1)],
                          'append': '',
                          'reason': 'Stop and join the mock SMTP server in finally even if the missing '
                                    'repository or a failed push raises an assertion.'},
 'task_005253_3ad2c682': {'edits': [('input=input_bytes, capture_output=True, text=True',
                                     'input=input_str, capture_output=True, text=True',
                                     2),
                                    ('            s.listen(1)',
                                     '            s.settimeout(2)\n            s.listen(1)',
                                     2),
                                    ('            conn, addr = s.accept()',
                                     '            conn, addr = s.accept()\n'
                                     '            conn.settimeout(2)',
                                     2),
                                    ('t1.join(timeout=1)', 't1.join(timeout=5)', 1),
                                    ('t2.join(timeout=1)', 't2.join(timeout=5)', 1)],
                          'append': '',
                          'reason': 'Match text-mode subprocess input type and bound socket '
                                    'accept/receive waits so failed pipeline execution cannot leave '
                                    'verifier threads alive.'},
 'task_003778_2be1fc19': {'edits': [('            while os.path.exists(f"/proc/{pid}"):\n'
                                     '                time.sleep(0.1)',
                                     '            deadline = time.monotonic() + 45\n'
                                     '            while os.path.exists(f"/proc/{pid}"):\n'
                                     '                try:\n'
                                     '                    with open(f"/proc/{pid}/stat") as status:\n'
                                     '                        if status.read().rsplit(")", '
                                     '1)[1].split()[0] == "Z":\n'
                                     '                            break\n'
                                     '                except FileNotFoundError:\n'
                                     '                    break\n'
                                     '                assert time.monotonic() < deadline, "Writer did '
                                     'not finish within 45 seconds"\n'
                                     '                time.sleep(0.1)',
                                     1)],
                          'append': '',
                          'reason': 'Treat zombie writers as finished and bound the wait for the '
                                    '30-second writer to 45 seconds; retain all archive integrity '
                                    'checks.'}}

ARCHIVED.update({'task_003753_b34e0b3c': {'category': 'verifier-placeholder-oracle',
                          'reason': 'Oracle shell script contains only a shebang, while verifier '
                                    'repeatedly waits for network connections it never creates. No '
                                    'usable reference behavior for equivalence grading; original '
                                    'payload retained.'},
 'task_004301_f3e3fc72': {'category': 'verifier-process-lifecycle',
                          'reason': 'Verifier runs killall python3, killing its own pytest process and '
                                    'unrelated service processes. Its lifecycle check needs a reliable '
                                    'ownership model before reuse; original payload retained.'}})

# User-requested exclusion of the remaining 21 incomplete no-op runs.
ARCHIVED.update({'task_000459_7f1989b6': {'category': 'verifier-incomplete-no-op',
                          'reason': 'Verifier executes the slow 50,000-line baseline shell analyzer '
                                    'before agent checks; baseline forks awk/bc repeatedly. Archived at '
                                    'user request after an incomplete no-op verification. This '
                                    'exclusion does not establish that the task is unsolvable; original '
                                    'payload preserved for possible repair.'},
 'task_000529_475e45c2': {'category': 'verifier-incomplete-no-op',
                          'reason': 'Iterative log traversal follows cycles without tracking visited '
                                    'nodes. Archived at user request after an incomplete no-op '
                                    'verification. This exclusion does not establish that the task is '
                                    'unsolvable; original payload preserved for possible repair.'},
 'task_000796_17407051': {'category': 'verifier-incomplete-no-op',
                          'reason': 'Audio-expression verifier evaluates malicious expressions for many '
                                    'samples. Requires an isolated container reproduction, never host '
                                    'execution. Archived at user request after an incomplete no-op '
                                    'verification. This exclusion does not establish that the task is '
                                    'unsolvable; original payload preserved for possible repair.'},
 'task_000875_69abe1f8': {'category': 'verifier-incomplete-no-op',
                          'reason': 'Cargo dependency resolution and grpcio compilation consume '
                                    'verifier time; distinguish dependency setup from intended code '
                                    'failure. Archived at user request after an incomplete no-op '
                                    'verification. This exclusion does not establish that the task is '
                                    'unsolvable; original payload preserved for possible repair.'},
 'task_001268_1fe5b31e': {'category': 'verifier-incomplete-no-op',
                          'reason': '5,000 oracle/Python subprocess pairs and missing agent output; '
                                    'cannot infer a valid zero without bounded execution evidence. '
                                    'Archived at user request after an incomplete no-op verification. '
                                    'This exclusion does not establish that the task is unsolvable; '
                                    'original payload preserved for possible repair.'},
 'task_001277_0e5959f9': {'category': 'verifier-incomplete-no-op',
                          'reason': 'flatten_data follows cyclic aliases without a visited set; direct '
                                    'verifier call. Archived at user request after an incomplete no-op '
                                    'verification. This exclusion does not establish that the task is '
                                    'unsolvable; original payload preserved for possible repair.'},
 'task_001288_305375d1': {'category': 'verifier-incomplete-no-op',
                          'reason': 'decode.sh does not advance its loop index; unbounded fuzz '
                                    'subprocess. Archived at user request after an incomplete no-op '
                                    'verification. This exclusion does not establish that the task is '
                                    'unsolvable; original payload preserved for possible repair.'},
 'task_002029_b6ef6bbe': {'category': 'verifier-incomplete-no-op',
                          'reason': 'Opposite lock acquisition order deadlocks request threads; '
                                    'non-daemon threads survive failed assertions. Archived at user '
                                    'request after an incomplete no-op verification. This exclusion '
                                    'does not establish that the task is unsolvable; original payload '
                                    'preserved for possible repair.'},
 'task_002122_d1cd6d33': {'category': 'verifier-incomplete-no-op',
                          'reason': 'compute_collatz(-99) loops indefinitely inside pytest after two '
                                    'earlier failing tests. Archived at user request after an '
                                    'incomplete no-op verification. This exclusion does not establish '
                                    'that the task is unsolvable; original payload preserved for '
                                    'possible repair.'},
 'task_002876_647e57ed': {'category': 'verifier-incomplete-no-op',
                          'reason': 'Large-input diff parser performance check lacks a bound; measure '
                                    'slow supplied implementation versus verifier budget. Archived at '
                                    'user request after an incomplete no-op verification. This '
                                    'exclusion does not establish that the task is unsolvable; original '
                                    'payload preserved for possible repair.'},
 'task_003084_f8ad38b2': {'category': 'verifier-incomplete-no-op',
                          'reason': 'Recursive SQL graph reporting may expand paths exponentially; '
                                    'distinguish oracle cost from the intended algorithm defect. '
                                    'Archived at user request after an incomplete no-op verification. '
                                    'This exclusion does not establish that the task is unsolvable; '
                                    'original payload preserved for possible repair.'},
 'task_003195_7aab506a': {'category': 'verifier-incomplete-no-op',
                          'reason': 'Oracle depends on Redis/Flask and repeats per-line network calls; '
                                    'missing service readiness can exhaust budget before comparison. '
                                    'Archived at user request after an incomplete no-op verification. '
                                    'This exclusion does not establish that the task is unsolvable; '
                                    'original payload preserved for possible repair.'},
 'task_003871_5da1102f': {'category': 'verifier-incomplete-no-op',
                          'reason': 'Reference Go engine repeatedly performs expensive joins before '
                                    'comparing the agent; oracle runtime needs '
                                    'measurement/optimization. Archived at user request after an '
                                    'incomplete no-op verification. This exclusion does not establish '
                                    'that the task is unsolvable; original payload preserved for '
                                    'possible repair.'},
 'task_003968_13d74d72': {'category': 'verifier-incomplete-no-op',
                          'reason': 'AWK integrator increases step size after excessive error without '
                                    'advancing time; unbounded subprocess. Archived at user request '
                                    'after an incomplete no-op verification. This exclusion does not '
                                    'establish that the task is unsolvable; original payload preserved '
                                    'for possible repair.'},
 'task_004179_0ee52583': {'category': 'verifier-incomplete-no-op',
                          'reason': 'Python service wrapper and 100 corpus launches require runtime '
                                    'isolation; current baseline already redirects wrapper service '
                                    'output. Archived at user request after an incomplete no-op '
                                    'verification. This exclusion does not establish that the task is '
                                    'unsolvable; original payload preserved for possible repair.'},
 'task_004542_aa05384f': {'category': 'verifier-incomplete-no-op',
                          'reason': 'Planted zero-length TLV bug loops while producing output; partial '
                                    'verifier output shows failures, but exact process termination '
                                    'needs isolation. Archived at user request after an incomplete '
                                    'no-op verification. This exclusion does not establish that the '
                                    'task is unsolvable; original payload preserved for possible '
                                    'repair.'},
 'task_004938_8820a703': {'category': 'verifier-incomplete-no-op',
                          'reason': 'resolve_deps recurses through A:B B:C C:A without cycle detection; '
                                    'unbounded verifier subprocess. Archived at user request after an '
                                    'incomplete no-op verification. This exclusion does not establish '
                                    'that the task is unsolvable; original payload preserved for '
                                    'possible repair.'},
 'task_006209_6d0ae0bc': {'category': 'verifier-incomplete-no-op',
                          'reason': 'Adaptive integrator has inverted error/tolerance control and no '
                                    'safe termination for stalled progress. Archived at user request '
                                    'after an incomplete no-op verification. This exclusion does not '
                                    'establish that the task is unsolvable; original payload preserved '
                                    'for possible repair.'},
 'task_007809_50edd342': {'category': 'verifier-incomplete-no-op',
                          'reason': 'collatz_steps accepts zero/negative input and never terminates; '
                                    'direct verifier call. Archived at user request after an incomplete '
                                    'no-op verification. This exclusion does not establish that the '
                                    'task is unsolvable; original payload preserved for possible '
                                    'repair.'},
 'task_008338_67e08864': {'category': 'verifier-incomplete-no-op',
                          'reason': 'Verifier calls unsafe C in-process via ctypes; recorded '
                                    'segmentation fault matches planted buffer-overflow bug. Requires '
                                    'subprocess isolation. Archived at user request after an incomplete '
                                    'no-op verification. This exclusion does not establish that the '
                                    'task is unsolvable; original payload preserved for possible '
                                    'repair.'},
 'task_009889_d25e0c72': {'category': 'verifier-incomplete-no-op',
                          'reason': 'Integrator uses inverted error/tolerance logic and stalls in '
                                    'nested pytest. Archived at user request after an incomplete no-op '
                                    'verification. This exclusion does not establish that the task is '
                                    'unsolvable; original payload preserved for possible repair.'}})

# Remaining exclusions confirmed by the user after the full failure reconciliation.
ARCHIVED.update({'task_009304_b85ef35a': {'category': 'task-setup-permission-failure',
                          'reason': 'Task setup fails because chmod -x does not remove executable '
                                    'permission from /home/user/repo.git/hooks/post-receive under the '
                                    'validation runtime. The intended broken state is not established, '
                                    'so this is not an accepted no-op zero. Archived at user request; '
                                    'original payload retained.'},
 'task_001341_b75b0a18': {'category': 'verifier-incomplete-no-op',
                          'reason': 'The Python wrapper starts mongod during verifier invocation; after '
                                    'the wrapper repair, job 5110666 still timed out in stage 3 after '
                                    '120 seconds without a reward. Archived at user request; original '
                                    'payload retained.'},
 'task_002132_096278fa': {'category': 'nop-reward-one',
                          'reason': 'The setup sed patterns do not match arrow 1.2.3 parser.py, so the '
                                    'intended bugs are never planted. The unchanged vendored library '
                                    'matches the oracle and earns no-op reward 1. Archived at user '
                                    'request; original payload retained.'}})

REPAIR_LABEL = "task-data-repair"


SETUP_SH = """#!/bin/bash
# Replays the task-specific part of upstream post_install.sh at container start.
marker=/var/lib/tmax-setup.done
[ -e "$marker" ] && exit 0
cd /
export HOME=/root DEBIAN_FRONTEND=noninteractive
if [ -d /setup_files/fixtures ]; then cp -R --preserve=mode,timestamps /setup_files/fixtures/. /; fi
bash /setup_files/task_install.sh
status=$?
rm -rf /setup_files/task_install.sh /setup_files/fixtures /setup_files/downloads
[ "$status" -eq 0 ] && mkdir -p /var/lib && touch "$marker"
exit "$status"
"""
SETUP_COMMAND = "bash /setup_files/setup.sh"

LABELS = ["shared-image", "task-setup-at-runtime"]
SUPERSET_LABEL = "superset-image"
SUPERSET_REASON = (
    "Package installation from post_install.sh is replaced by the {family} superset image, which "
    "installs the union of the packages of all {family} tasks without version pins, so this task's "
    "image also contains packages it did not ask for ({size} tasks share it). The task-specific part "
    "of post_install.sh (initial files, fixtures, users, permissions) runs unchanged from setup_files "
    "at container start instead of at image build.")
REASON = (
    "Package installation from post_install.sh is kept in an image shared by all tasks with the "
    "same install commands (group {group}, {size} tasks). The task-specific part of post_install.sh "
    "(initial files, fixtures, users, permissions) runs unchanged from setup_files at container "
    "start instead of at image build.")


# --- post_install.sh: logical lines -------------------------------------------------------

HEREDOC = re.compile(r"(?<!<)<<(?!<)(-?)\s*(?:'([^']+)'|\"([^\"]+)\"|\\?([A-Za-z_][A-Za-z0-9_]*))")
FILE_HEREDOC = re.compile(r"\s*(?:cat\b[^|]*(?<![<>&])>|tee\b[^|]*$)")


def logical_lines(text):
    """Yield (command, heredoc bodies, raw text) per shell line.

    Backslash continuations are joined and here-document bodies are attached to
    the line that opens them, so a body is never read as a command. Joining the
    raw texts gives back the input.
    """
    lines = re.findall(r"[^\n]*\n|[^\n]+", text)
    i = 0
    while i < len(lines):
        start = i
        command = lines[i]
        i += 1
        while command.rstrip("\n").endswith("\\") and i < len(lines):
            command = command.rstrip("\n")[:-1] + " " + lines[i]
            i += 1
        bodies = []
        for dash, single, double, bare in HEREDOC.findall(command):
            end, body = single or double or bare, []
            while i < len(lines):
                line = lines[i]
                i += 1
                probe = line.rstrip("\n")
                if (probe.lstrip("\t") if dash else probe) == end:
                    break
                body.append(line)
            bodies.append("".join(body))
        yield command, bodies, "".join(lines[start:i])


# --- post_install.sh: install prefix ------------------------------------------------------

APT_FLAGS = {"-y", "-q", "-qq", "-yq", "-qy", "-yqq", "-qqy", "--yes", "--assume-yes",
             "--no-install-recommends", "--fix-missing"}
APT_PACKAGE = re.compile(r"[a-z0-9][a-z0-9+.:=~-]*")
PIP_FLAGS = {"--no-cache-dir", "--break-system-packages", "-q", "--quiet", "-U", "--upgrade",
             "--no-input", "--prefer-binary", "--ignore-installed", "--force-reinstall",
             "--disable-pip-version-check", "--root-user-action=ignore"}
PIP_TIMEOUT = re.compile(r"--default-timeout=\d+")
PIP_INDEX_FLAGS = {"--index-url", "-i", "--extra-index-url"}
PIP_INDEXES = {"https://download.pytorch.org/whl/cpu"}
_SPEC = r"(?:==|>=|<=|~=|!=|<|>)[A-Za-z0-9.*+!-]+"
PIP_REQUIREMENT = re.compile(
    rf"[A-Za-z0-9][A-Za-z0-9._-]*(?:\[[A-Za-z0-9,._-]+\])?(?:{_SPEC}(?:,{_SPEC})*)?")


def _canonical(head, flags, packages):
    return " ".join([*head, *sorted(set(flags)), *(shlex.quote(p) for p in sorted(set(packages)))])


def _install_command(tokens):
    """Canonical text of one allowed install command, or None."""
    head = []
    if tokens[:1] == ["DEBIAN_FRONTEND=noninteractive"]:
        head, tokens = tokens[:1], tokens[1:]
    if not tokens:
        return None
    if not head and tokens == ["export", "DEBIAN_FRONTEND=noninteractive"]:
        return " ".join(tokens)
    if not head and tokens == ["rm", "-rf", "/var/lib/apt/lists/*"]:
        return " ".join(tokens)
    if tokens[0] in ("apt-get", "apt"):
        rest = tokens[1:]
        action = next((t for t in rest if not t.startswith("-")), None)
        if action not in ("update", "install", "clean"):
            return None
        rest.remove(action)
        flags = [t for t in rest if t.startswith("-")]
        packages = [t for t in rest if not t.startswith("-")]
        if set(flags) - APT_FLAGS or any(not APT_PACKAGE.fullmatch(p) for p in packages):
            return None
        if (action == "install") != bool(packages):
            return None
        return _canonical([*head, tokens[0], action], flags, packages)
    if tokens[:3] in (["python3", "-m", "pip"], ["python", "-m", "pip"]):
        tool, rest = tokens[:3], tokens[3:]
    elif tokens[0] in ("pip", "pip3"):
        tool, rest = tokens[:1], tokens[1:]
    else:
        return None
    if head or rest[:1] != ["install"]:
        return None
    flags, packages, rest = [], [], rest[1:]
    while rest:
        token = rest.pop(0)
        name, _, value = token.partition("=")
        if name in PIP_INDEX_FLAGS and (value or rest):
            value = value or rest.pop(0)
            if value not in PIP_INDEXES:
                return None
            flags.append(f"{name} {value}")
        elif token.startswith("-"):
            if token not in PIP_FLAGS and not PIP_TIMEOUT.fullmatch(token):
                return None
            flags.append(token)
        elif PIP_REQUIREMENT.fullmatch(token):
            packages.append(token)
        else:
            return None
    if not packages:
        return None
    return _canonical([*tool, "install"], flags, packages)


def install_line(command):
    """Canonical form of a line that only installs packages; "" for blank/comment; else None."""
    text = command.strip()
    if not text or text.startswith("#"):
        return ""
    try:
        lexer = shlex.shlex(text, posix=True, punctuation_chars=True)
        lexer.whitespace_split = True
        tokens = list(lexer)
    except ValueError:
        return None
    parts, current = [], []
    for token in tokens:
        if token in ("&&", ";"):
            parts += [current, token]
            current = []
        elif token and not set(token) - set("();<>|&"):
            return None
        else:
            current.append(token)
    if current:
        parts.append(current)
    result = []
    for part in parts:
        if isinstance(part, str):
            result.append(" &&" if part == "&&" else ";")
            continue
        canonical = _install_command(part) if part else None
        if canonical is None:
            return None
        result.append((" " if result else "") + canonical)
    return "".join(result).rstrip(";") if result and result[-1] != " &&" else None


def split_post_install(text):
    """Return (canonical install script, task suffix byte for byte)."""
    prefix, suffix, installs = [], [], False
    for command, bodies, raw in logical_lines(text):
        if not suffix and not bodies:
            canonical = install_line(command)
            if canonical is not None:
                if canonical:
                    prefix.append(canonical)
                    installs = installs or " install " in canonical
                continue
        suffix.append(raw)
    return ("\n".join(prefix) + "\n" if installs else ""), "".join(suffix)


# --- post_install.sh: what the task suffix may contain ------------------------------------

# Commands that install packages or reach the network. Such steps stay in the image.
NETWORK = [
    ("package-manager", re.compile(
        r"\b(?:apt|apt-get|apt-key|aptitude|dpkg|pip3?|pipx|npm|npx|yarn|pnpm|gem|bundle|conda|snap|"
        r"add-apt-repository|rustup)\b|-m\s+pip\b")),
    ("download", re.compile(r"\b(?:curl|wget|urlopen|urlretrieve)\b|requests\.(?:get|post)\b|"
                            r"\bgit\s+(?:clone|fetch|pull|submodule)\b")),
    ("cargo", re.compile(r"\bcargo\s+(?:install|build|fetch|run|test|update|add|check)\b")),
    # Compiling local code with the image's Go needs no network; fetching modules does.
    ("go", re.compile(r"\bgo\s+(?:get|install|mod\s+(?:tidy|download|vendor|graph|verify|why)|work\s+sync)\b")),
]
_AT_COMMAND = r"(?:^|&&|\|\||;|\(|\bsudo\s|\bexec\s|\bthen\s|\bdo\s)\s*"
# Only a docker call counts; docker-compose.yml written as a file or a docker group does not.
NETWORK.append(("docker", re.compile(_AT_COMMAND + r"docker(?:d|-compose)?\b", re.M)))
# Processes that would keep running in the task container. At image build they end with the build.
SERVICES = [
    ("service", re.compile(
        r"\b(?:systemctl|pg_ctl|pg_ctlcluster|pg_createcluster|initdb|mysqld_safe|mysql_install_db|"
        r"supervisord|nohup|setsid|disown)\b|/etc/init\.d/|"
        + _AT_COMMAND + r"(?:service|redis-server|nginx|postgres|mongod|mysqld|mariadbd|sshd|"
        r"cron|crond|memcached|dockerd)\b(?!\s*=)", re.M)),
    ("background-process", re.compile(r"(?<![&><|\\])&(?![&>\d])")),
    # May start a login session or user processes; left to the upstream build.
    ("runs-as-user", re.compile(r"\bsu\s+(?:-l?\s+)?\w+\s+-c\b|\bsudo\s+-u\b|\brunuser\b")),
]
# The suffix must not depend on being a part of /tmp/post_install.sh.
SELF_REFERENCE = re.compile(r"post_install|base_install|\$0\b|BASH_SOURCE")


def writes_text_only(command):
    """True for `echo|printf TEXT > FILE` (or >>): the text is data, nothing in it runs now."""
    if "$(" in command or "`" in command:
        return False
    try:
        lexer = shlex.shlex(command.strip(), posix=True, punctuation_chars=True)
        lexer.whitespace_split = True
        tokens = list(lexer)
    except ValueError:
        return False
    operators = [i for i, token in enumerate(tokens) if token and not set(token) - set("();<>|&")]
    return (len(tokens) >= 3 and tokens[0] in ("echo", "printf") and len(operators) == 1
            and operators[0] == len(tokens) - 2 and tokens[-2] in (">", ">>"))


def suffix_findings(suffix):
    """Reasons why a task suffix has to stay in the image build (empty: it may run as setup)."""
    scanned = []
    for command, bodies, _ in logical_lines(suffix):
        # A here-document written to a file is data. One fed to an interpreter is code.
        if FILE_HEREDOC.match(command):
            # The header names the target file; scan it only when it also runs something.
            if re.search(r"&&|\|\||;|\|", command.split("<<", 1)[0] + command.rsplit(">", 1)[-1]):
                scanned.append(command)
            continue
        if writes_text_only(command):
            continue
        scanned.append(command)
        scanned.extend(bodies)
    text = "".join(scanned)
    found = [name for name, pattern in (*NETWORK, *SERVICES) if pattern.search(text)]
    # A go.mod with dependencies makes a later go build fetch modules, even when the file is
    # written by a here-document.
    if "go" not in found and "go.mod" in suffix and re.search(r"^\s*require\b", suffix, re.M):
        found.append("go")
    if SELF_REFERENCE.search(text):
        found.append("self-reference")
    if not suffix.strip():
        found.append("no-task-part")
    return found


# --- tasks --------------------------------------------------------------------------------

def read_task(blob):
    """{name: (content, mode)}, or None when the archive holds more than regular files."""
    files = {}
    with tarfile.open(fileobj=io.BytesIO(blob), mode="r:*") as archive:
        for member in archive:
            if member.isdir():
                continue
            if not member.isfile() or member.name in files:
                return None
            files[member.name] = (archive.extractfile(member).read(), member.mode)
    return files


def classify(files):
    """Describe how one task could share an image: family, findings, canonical prefix, suffix."""
    info = {"family": None, "findings": []}
    if files is None:
        return {**info, "findings": ["unsupported-archive"]}
    dockerfile = files["environment/Dockerfile"][0].decode()
    environment = {name for name in files if name.startswith("environment/")}
    fixtures = {}
    if dockerfile == LEGACY_DOCKERFILE:
        info["family"] = "legacy"
        expected = {"environment/Dockerfile", "environment/post_install.sh"}
    else:
        info["family"] = "v2"
        copies = FIXTURE_COPY.findall(dockerfile)
        fixtures = {f"environment/{source}": target for source, target, destination in copies
                    if target == destination}
        if (FIXTURE_COPY.sub("", dockerfile).replace("\n\n\n", "\n\n") != V2_DOCKERFILE
                or len(fixtures) != len(copies)):
            return {**info, "findings": ["unknown-dockerfile"]}
        if hashlib.sha256(files["environment/base_install.sh"][0]).hexdigest() != V2_BASE_INSTALL_SHA256:
            info["findings"].append("base-install-variant")
        expected = {"environment/Dockerfile", "environment/post_install.sh",
                    "environment/base_install.sh", *fixtures}
    if environment != expected:
        return {**info, "findings": ["unexpected-environment-files"]}
    if any(name.startswith(("setup_files/", "solution/")) or name == "tests/setup.sh" for name in files):
        return {**info, "findings": ["has-setup-or-solution"]}
    try:
        script = files["environment/post_install.sh"][0].decode()
    except UnicodeDecodeError:
        return {**info, "findings": ["undecodable-script"]}
    prefix, suffix = split_post_install(script)
    if not prefix:
        info["findings"].append("no-install-prefix")
    info["findings"] += suffix_findings(suffix)
    downloads, rust = {}, False
    if info["findings"] and set(info["findings"]) <= {"download", "package-manager"}:
        rewritten = offline_suffix(suffix)
        if rewritten and not suffix_findings(rewritten[0]):
            suffix, downloads, rust = rewritten
            info["findings"] = []
    return {**info, "prefix": prefix, "suffix": suffix, "fixtures": fixtures, "downloads": downloads,
            "rust": rust}


def shared_files(files, info):
    """The task with its package installation in a shared image and the rest in setup_files/."""
    result = {name: value for name, value in files.items() if name not in info["fixtures"]}
    if info["family"] == "v2":
        result["environment/Dockerfile"] = (V2_DOCKERFILE.encode(), files["environment/Dockerfile"][1])
    result["environment/post_install.sh"] = (info["prefix"].encode(), files["environment/post_install.sh"][1])
    for name, target in info["fixtures"].items():
        result["setup_files/fixtures" + target] = files[name]
    result["setup_files/task_install.sh"] = (info["suffix"].encode(), 0o644)
    for name, content in info.get("downloads", {}).items():
        result[f"setup_files/downloads/{name}"] = (content, 0o644)
    result["setup_files/setup.sh"] = (SETUP_SH.encode(), 0o755)
    return result


# --- downloads and Rust: rewritten so that setup needs no network ---------------------------
#
# A plain file download (`wget URL -O FILE`, `curl -L URL -o FILE`) is fetched once by
# `fetch-downloads` into a content-addressed cache, shipped in setup_files/downloads/ and
# copied to the same destination at setup. The standard rustup installer line is dropped
# from setup because the superset image installs the same stable toolchain for root.

DOWNLOAD_DIR = "/setup_files/downloads"
DOWNLOAD_MAX_BYTES = 20 * 1024 * 1024
DOWNLOAD_CACHE = None  # set by main(): Path with index.json {url: {sha256, size}} and <sha256> files
WGET_FLAGS = {"-q", "--quiet", "-nv", "--no-verbose", "-c", "--continue", "-N", "--no-check-certificate"}
WGET_VALUE_FLAGS = re.compile(r"--(?:tries|timeout|progress|retry-connrefused)(?:=\S+)?")
CURL_FLAG_LETTERS = set("sSLfk")
CURL_FLAGS = {"--silent", "--show-error", "--location", "--fail", "--insecure", "--create-dirs",
              "--tlsv1.2", "--proto=https", "--compressed"}
RUSTUP_INSTALL = re.compile(
    r"\s*curl --proto '?=https'? --tlsv1\.2 -sSf https://sh\.rustup\.rs \| sh -s -- -y"
    r"(?: --no-modify-path| --profile (?:minimal|default))*\s*")
RUST_IN_IMAGE = ": # The stable Rust toolchain for root comes from the shared image\n"
# apt's rustc is part of the union; rustup installs beside it, as the task lines did.
RUSTUP_IMAGE_INSTALL = ("export HOME=/root RUSTUP_INIT_SKIP_PATH_CHECK=yes && curl --proto '=https' --tlsv1.2 "
                        "-sSf https://sh.rustup.rs | sh -s -- -y\n")


def download_command(command):
    """(url, destination, create_dirs) of a plain file download; destination is relative to the
    working directory exactly as the tool resolves it. None for anything else."""
    text = command.strip()
    if re.search(r"[|;&<>`]|\$\(", text):
        return None
    try:
        tokens = shlex.split(text)
    except ValueError:
        return None
    if not tokens or tokens[0] not in ("wget", "curl"):
        return None
    tool, rest, url, out, directory, remote = tokens[0], tokens[1:], None, None, None, False
    while rest:
        token = rest.pop(0)
        if token.startswith(("http://", "https://")):
            if url:
                return None
            url = token
        elif tool == "wget" and (token in ("-O", "--output-document") or re.fullmatch(r"-[qnc]*O", token)):
            out = rest.pop(0) if rest else None
        elif tool == "wget" and token.startswith("--output-document="):
            out = token.split("=", 1)[1]
        elif tool == "wget" and token in ("-P", "--directory-prefix"):
            directory = rest.pop(0) if rest else None
        elif tool == "wget" and (token in WGET_FLAGS or WGET_VALUE_FLAGS.fullmatch(token)):
            pass
        elif tool == "curl" and re.fullmatch(r"-[sSLfk]*o", token) or token == "--output":
            out = rest.pop(0) if rest else None
        elif tool == "curl" and (re.fullmatch(r"-[sSLfk]*O", token) or token == "--remote-name"):
            remote = True
        elif tool == "curl" and (token in CURL_FLAGS or re.fullmatch(r"-[sSLfk]+", token)):
            pass
        elif tool == "curl" and token == "--retry" and rest and rest[0].isdigit():
            rest.pop(0)
        else:
            return None
    if not url or re.search(r"[?#]", url) or out == "-":
        return None
    name = url.rstrip("/").rsplit("/", 1)[-1]
    if out is None:
        if tool == "curl" and not remote or not name or url.endswith("/"):
            return None
        out = f"{directory}/{name}" if directory else name
    return url, out, tool == "curl" and "--create-dirs" in tokens


def cached_download(url):
    """The cached bytes of url, or None when absent, oversized or failing its checksum."""
    if DOWNLOAD_CACHE is None:
        return None
    record = json.loads((DOWNLOAD_CACHE / "index.json").read_text()).get(url) if (
        DOWNLOAD_CACHE / "index.json").exists() else None
    if not record or record["size"] > DOWNLOAD_MAX_BYTES:
        return None
    content = (DOWNLOAD_CACHE / record["sha256"]).read_bytes()
    if hashlib.sha256(content).hexdigest() != record["sha256"]:
        raise ValueError(f"download cache corrupt for {url}")
    return content


def offline_suffix(suffix):
    """(suffix, {name: bytes}, rust) with downloads and the rustup installer rewritten, or None."""
    parts, downloads, rust, changed = [], {}, False, False
    # The image installs Rust in root's default locations only.
    default_rust = not re.search(r"\b(?:RUSTUP_HOME|CARGO_HOME)\s*=", suffix)
    for command, bodies, raw in logical_lines(suffix):
        if default_rust and not bodies and RUSTUP_INSTALL.fullmatch(command.rstrip("\n")):
            parts.append(RUST_IN_IMAGE)
            rust = changed = True
            continue
        download = None if bodies else download_command(command)
        content = cached_download(download[0]) if download else None
        if content is None:
            parts.append(raw)
            continue
        url, destination, create_dirs = download
        name = hashlib.sha256(content).hexdigest()[:16] + "-" + re.sub(r"[^A-Za-z0-9._-]", "_",
                                                                         url.rsplit("/", 1)[-1])
        downloads[name] = content
        target = shlex.quote(destination)
        mkdir = f'mkdir -p "$(dirname {target})" && ' if create_dirs else ""
        parts.append(f"{mkdir}cp {DOWNLOAD_DIR}/{name} {target}\n")
        changed = True
    return ("".join(parts), downloads, rust) if changed else None


def fetch_downloads_main():
    """Fill the download cache with every plain download the patcher could ship."""
    import urllib.request
    import pyarrow.parquet as pq
    parser = argparse.ArgumentParser()
    parser.add_argument("--source", type=Path, required=True)
    parser.add_argument("--cache", type=Path, required=True)
    args = parser.parse_args()
    args.cache.mkdir(parents=True, exist_ok=True)
    index_path = args.cache / "index.json"
    index = json.loads(index_path.read_text()) if index_path.exists() else {}
    urls = set()
    for batch in pq.ParquetFile(args.source).iter_batches(batch_size=64):
        for row in batch.to_pylist():
            files = read_task(row["task_binary"])
            if files is None or "environment/post_install.sh" not in files:
                continue
            _, suffix = split_post_install(files["environment/post_install.sh"][0].decode(errors="replace"))
            for command, bodies, _ in logical_lines(suffix):
                download = None if bodies else download_command(command)
                if download:
                    urls.add(download[0])
    failures = {}
    for url in sorted(urls - set(index)):
        try:
            request = urllib.request.Request(url, headers={"User-Agent": "Wget/1.21.2"})
            with urllib.request.urlopen(request, timeout=60) as response:
                content = response.read(DOWNLOAD_MAX_BYTES + 1)
        except Exception as exc:
            failures[url] = repr(exc)
            continue
        digest = hashlib.sha256(content).hexdigest()
        if len(content) <= DOWNLOAD_MAX_BYTES:
            (args.cache / digest).write_bytes(content)
        index[url] = {"sha256": digest, "size": len(content)}
        index_path.write_text(json.dumps(index, indent=1, sort_keys=True) + "\n")
    (args.cache / "failures.json").write_text(json.dumps(failures, indent=1, sort_keys=True) + "\n")
    print(json.dumps({"urls": len(urls), "cached": len(index), "failed": len(failures)}))


# --- superset images ----------------------------------------------------------------------
#
# Grouping by identical install commands still leaves thousands of images. Instead, all
# regular tasks of a family share one image with the union of the family's packages. Extra
# packages a task did not ask for are accepted. Tasks pinning a pip version keep exact grouping,
# since one image cannot hold two versions. Instructions that mention installing a package the
# superset already contains are recorded in the manifest for the no-op review.

PIP_INDEX = "https://download.pytorch.org/whl/cpu"
PIP_VERSION = re.compile(r"[=<>~!]")
# Words that are package names but appear in instructions as ordinary words or tools.
COMMON_WORDS = set(
    "file bash build time zip make sed grep tables parallel rename tar patch expect pip perl less tree "
    "jq bc vim nano curl wget git python3 python3-pip pytest ca-certificates coreutils procps gcc g++ "
    "unzip gzip findutils util-linux".split())


def prefix_packages(prefix):
    """(apt packages, {normalized pip name: requirement}) of a canonical install prefix."""
    apt, pip = set(), {}
    for line in prefix.splitlines():
        for part in re.split(r" && |; ", line):
            tokens = shlex.split(part)
            if tokens[:1] == ["DEBIAN_FRONTEND=noninteractive"]:
                tokens = tokens[1:]
            if tokens[:2] in (["apt-get", "install"], ["apt", "install"]):
                apt.update(token for token in tokens[2:] if not token.startswith("-"))
            elif "install" in tokens and (tokens[0] in ("pip", "pip3") or tokens[:3] in (
                    ["python3", "-m", "pip"], ["python", "-m", "pip"])):
                rest = tokens[tokens.index("install") + 1:]
                for i, token in enumerate(rest):
                    if token.startswith("-") or (i and rest[i - 1] in PIP_INDEX_FLAGS):
                        continue
                    name = re.split(r"[=<>~!\[]", token)[0].lower().replace("_", "-")
                    pip[name] = token if PIP_VERSION.search(token) or "[" in token else name
    return apt, pip


# Packages that cannot be installed together with the rest of the union on ubuntu:22.04
# (apt-get install -s): gcc-multilib conflicts with the aarch64 cross compilers.
APT_CONFLICTS = {"gcc-multilib"}
# pip resolves the whole union by backtracking to ancient, unbuildable releases, so the
# superset installs embedded locks instead. Regenerate whenever the union changes,
# then copy the exact output into SUPERSET_PIP_LOCKS:
#   uv pip compile REQUIREMENTS --python-version 3.10 --python-platform x86_64-manylinux_2_35 \
#     --extra-index-url https://download.pytorch.org/whl/cpu --index-strategy unsafe-best-match \
#     --no-header --no-annotate -o /tmp/tmax-superset-FAMILY.lock.txt
SUPERSET_PIP_LOCKS = {
    "legacy": """agate==1.14.2
agate-dbf==0.2.4
agate-excel==0.4.2
agate-sql==0.7.3
aiohappyeyeballs==2.7.1
aiohttp==3.14.5
aiosignal==1.4.0
aiosmtpd==1.4.6
alembic==1.20.0
annotated-doc==0.0.5
annotated-types==0.8.0
anyio==4.15.1
argon2-cffi==25.1.0
argon2-cffi-bindings==26.1.0
arrow==1.4.0
arviz==0.23.4
astropy==6.1.7
astropy-iers-data==0.2026.10.12.1.3.27
asttokens==3.0.2
async-lru==2.4.0
async-timeout==5.0.1
asyncio==4.0.0
atpublic==8.0.1
attrs==26.1.0
babel==2.18.0
backports-asyncio-runner==1.2.0
bandit==1.9.4
bcrypt==5.0.0
beautifulsoup4==4.15.0
biopython==1.88
bleach==6.4.0
blinker==1.9.0
blosc2==4.3.3
build==1.6.1
cachetools==7.2.2
capstone==5.0.9
certifi==2026.7.22
cffi==2.1.1
cftime==1.6.5
chardet==7.6.0
charset-normalizer==3.5.2
click==8.5.0
cloudpickle==3.1.2
colored-traceback==0.4.2
comm==0.2.3
cons==0.4.7
contourpy==1.3.2
cramjam==2.11.0
cryptography==50.0.2
csvkit==2.2.0
cycler==0.12.1
cython==3.3.0
dask==2026.8.0
databricks-sdk==0.153.0
dateparser==1.4.3
dbfread==2.0.7
debugpy==1.8.22
decorator==5.3.1
defusedxml==0.7.1
deprecated==1.3.1
docker==7.2.0
dpkt==1.9.8
duckdb==1.5.6
emcee==3.1.6
emoji==2.16.0
entrypoints==0.4.2
et-xmlfile==2.0.0
etuples==0.3.10
exceptiongroup==1.3.1
executing==2.3.0
faiss-cpu==1.15.1
fastapi==0.143.3
fastjsonschema==2.22.2
fastparquet==2026.9.0
filelock==4.1.0
flask==3.1.3
flask-cors==6.0.5
flask-limiter==4.1.1
fonttools==4.65.0
formulaic==1.2.2
fqdn==1.6.0
frozenlist==1.8.0
fsspec==2026.9.0
gitdb==4.0.12
gitpython==3.2.1
google-auth==2.62.0
graphene==3.4.3
graphql-core==3.2.13
graphql-relay==3.2.0
greenlet==3.5.6
grpcio==1.84.0
grpcio-reflection==1.84.0
grpcio-tools==1.84.0
gunicorn==26.2.0
h11==0.16.0
h5netcdf==1.8.1
h5py==3.16.0
hf-xet==1.7.0
httpcore==1.0.9
httpx==0.28.1
huey==3.4.0
huggingface-hub==1.33.0
hypothesis==6.168.5
idna==3.20
importlib-metadata==9.0.1
iniconfig==2.3.1
interface-meta==2.0.1
intervaltree==3.2.1
invoke==3.0.3
ipykernel==7.3.0
ipython==8.39.0
ipywidgets==8.1.9
isodate==0.7.2
isoduration==20.11.0
itsdangerous==2.2.0
jedi==0.20.1
jinja2==3.1.6
joblib==1.6.0
json5==0.17.3
jsonpointer==3.2.1
jsonschema==4.26.0
jsonschema-specifications==2025.9.1
jupyter==1.1.1
jupyter-builder==1.2.3
jupyter-client==8.10.0
jupyter-console==6.6.3
jupyter-core==5.9.1
jupyter-events==0.12.1
jupyter-lsp==2.3.1
jupyter-server==2.21.1
jupyter-server-terminals==0.5.4
jupyterlab==4.6.4
jupyterlab-pygments==0.3.0
jupyterlab-server==2.28.1
jupyterlab-widgets==3.0.17
kiwisolver==1.5.1
kuzu==0.11.3
lark==1.3.1
leather==0.4.1
levenshtein==0.27.4
limits==5.8.0
locket==1.0.0
logical-unification==0.4.7
lxml==6.1.3
mako==1.4.3
markdown-it-py==4.2.0
markupsafe==3.0.4
matplotlib==3.10.9
matplotlib-inline==0.2.2
maturin==1.15.0
mdurl==0.1.2
memory-profiler==0.61.0
minikanren==1.0.5
mistune==3.3.4
mlflow==3.17.1
mlflow-skinny==3.17.1
mlflow-tracing==3.17.1
mongomock==4.3.0
mpi4py==4.1.2
mpmath==1.3.0
msgpack==1.2.3
multidict==7.1.0
multipledispatch==1.0.0
narwhals==2.27.1
nbclient==0.11.0
nbconvert==7.17.2
nbformat==5.11.1
ndindex==1.10.1
nest-asyncio2==1.7.4
netcdf4==1.7.4
networkx==3.4.2
notebook==7.6.3
notebook-shim==0.2.4
numexpr==2.14.1
numpy==2.2.6
olefile==0.47
openpyxl==3.1.5
opentelemetry-api==1.45.1
opentelemetry-proto==1.45.1
opentelemetry-sdk==1.45.1
opentelemetry-semantic-conventions==0.66b1
ordered-set==4.1.0
overrides==7.7.0
packaging==26.3
pandas==2.3.3
pandocfilters==1.5.1
papermill==2.7.0
paramiko==5.0.0
parsedatetime==2.6
parso==0.8.7
partd==1.4.2
passlib==1.7.4
patsy==1.0.3
pexpect==4.9.0
pillow==12.3.0
pip==26.2.1
platformdirs==4.12.4
pluggy==1.7.0
plumbum==2.0.2
prettytable==3.18.0
prometheus-client==0.26.0
prompt-toolkit==3.0.53
propcache==0.5.4
protobuf==7.36.2
psutil==7.2.2
ptyprocess==0.7.0
pure-eval==0.2.4
pwntools==4.15.0
py-cpuinfo==9.0.0
pyarrow==25.0.1
pyasn1==0.6.4
pyasn1-modules==0.4.2
pycparser==3.11
pycryptodome==4.0.0
pydantic==2.14.1
pydantic-core==2.50.1
pyelftools==0.33
pyerfa==2.0.1.5
pygments==2.21.0
pyjwt==2.15.1
pymc==5.25.1
pynacl==1.6.2
pyopenssl==26.4.0
pyparsing==3.3.3
pyproject-hooks==1.3.3
pyserial==3.5
pysocks==1.7.1
pytensor==2.31.7
pytest==9.1.1
pytest-asyncio==1.4.0
pytest-json-report==1.5.0
pytest-metadata==3.1.1
pytest-mock==3.16.0
pytest-timeout==2.4.0
python-dateutil==2.9.0.post0
python-dotenv==1.2.4
python-json-logger==4.2.0
python-levenshtein==0.27.4
python-slugify==9.1.3
pytimeparse==1.1.8
pytz==2026.5
pyyaml==6.0.3
pyzmq==27.2.0
rapidfuzz==3.14.5
rdflib==7.6.0
referencing==0.37.0
regex==2026.9.29
requests==2.34.2
rfc3339-validator==0.1.4
rfc3986-validator==0.1.1
rfc3987-syntax==1.1.0
rich==15.0.0
ropgadget==7.7
rpds-py==0.30.0
rpyc==6.0.2
safetensors==0.8.0
scapy==2.8.0
scikit-learn==1.7.2
scipy==1.15.3
seaborn==0.13.2
semantic-version==2.10.0
semver==3.1.0
send2trash==2.1.0
sentence-transformers==6.1.0
sentinels==1.1.1
setuptools==84.0.0
setuptools-rust==1.13.0
shellingham==1.5.4
six==1.17.0
skops==0.16.0
smmap==5.0.3
sortedcontainers==2.4.0
soupsieve==2.10
sqlalchemy==2.0.55
sqlparse==0.6.0
stack-data==0.6.3
starlette==1.7.0
statsmodels==0.15.0
stevedore==5.8.0
supervisor==4.3.0
sympy==1.14.0
tables==3.10.1
tenacity==9.2.1
terminado==0.18.1
text-unidecode==1.3
threadpoolctl==3.7.0
tinycss2==1.5.1
tokenizers==0.23.3
tomli==2.5.0
toolz==1.2.0
torch==2.14.1+cpu
tornado==6.5.10
tqdm==4.70.1
traitlets==5.16.1
transformers==5.20.1
typer==0.27.3
typing-extensions==4.16.0
typing-inspection==0.4.4
tzdata==2026.5
tzlocal==5.4.4
unicorn==2.1.2
unix-ar==0.2.1
uri-template==1.3.0
urllib3==2.8.0
uvicorn==0.54.0
watchdog==6.0.0
wcwidth==0.9.2
webcolors==25.10.0
webencodings==0.6.1
websocket-client==1.9.2
websockets==16.1.1
werkzeug==3.1.9
wheel==0.48.0
widgetsnbextension==4.0.16
wrapt==2.5.1
xarray==2025.6.1
xarray-einstats==0.8.0
xlrd==2.0.2
xmltodict==1.0.4
yarl==1.25.1
z3-solver==5.3.1.0
zipp==4.1.1
zstandard==0.25.0
""",
    "v2": """aiohappyeyeballs==2.7.1
aiohttp==3.14.5
aiosignal==1.4.0
aiosmtpd==1.4.6
alembic==1.20.0
altgraph==0.17.5
amqp==5.4.1
annotated-doc==0.0.5
annotated-types==0.8.0
anyio==4.15.1
argcomplete==3.7.2
argon2-cffi==25.1.0
argon2-cffi-bindings==26.1.0
arrow==1.4.0
asttokens==3.0.2
async-lru==2.4.0
async-timeout==5.0.1
atpublic==8.0.1
attrs==26.1.0
audioread==3.1.0
av==17.1.0
babel==2.18.0
bcrypt==5.0.0
beautifulsoup4==4.15.0
billiard==4.3.1
biopython==1.88
bleach==6.4.0
blinker==1.9.0
boto3==1.43.114
botocore==1.43.114
cachetools==7.2.2
capstone==5.0.9
category-encoders==2.8.1
celery==5.6.3
certifi==2026.7.22
cffi==2.1.1
cftime==1.6.5
chardet==7.6.0
charset-normalizer==3.5.2
click==8.1.8
click-default-group==1.2.4
click-didyoumean==0.3.1
click-plugins==1.1.1.2
click-repl==0.4.1
cloudpickle==3.1.2
coloredlogs==15.0.1
comm==0.2.3
contourpy==1.3.2
cramjam==2.11.0
croniter==6.2.4
cryptography==50.0.2
ctranslate2==4.8.3
cycler==0.12.1
cython==3.3.0
databricks-sdk==0.153.0
debugpy==1.8.22
decorator==5.3.1
defusedxml==0.7.1
deprecated==1.3.1
dnspython==2.8.0
docker==7.2.0
dpkt==1.9.8
duckdb==1.5.6
edge-tts==7.3.1
emcee==3.1.6
emoji==2.16.0
entrypoints==0.4.2
exceptiongroup==1.3.1
executing==2.3.0
faiss-cpu==1.15.1
fastapi==0.143.3
faster-whisper==1.2.1
fastjsonschema==2.22.2
fastparquet==2026.9.0
fbm==0.3.0
ffmpeg-python==0.2.0
filelock==4.1.0
flask==3.1.3
flask-cors==6.0.5
flask-limiter==4.1.1
flatbuffers==25.12.19
fonttools==4.65.0
formulaic==1.2.2
fqdn==1.6.0
frozenlist==1.8.0
fsspec==2026.9.0
future==1.0.0
gitdb==4.0.12
gitpython==3.2.1
google-auth==2.62.0
graphene==3.4.3
graphql-core==3.2.13
graphql-relay==3.2.0
greenlet==3.5.6
grpcio==1.84.0
grpcio-tools==1.84.0
gtts==2.5.4
gunicorn==26.2.0
h11==0.16.0
h5py==3.16.0
hf-xet==1.7.0
httpcore==1.0.9
httpx==0.28.1
huey==3.4.0
huggingface-hub==1.16.1
humanfriendly==10.0
hypothesis==6.168.5
idna==3.20
imageio==2.38.0
imageio-ffmpeg==0.6.0
importlib-metadata==9.0.1
iniconfig==2.3.1
interface-meta==2.0.1
invoke==3.0.3
ipykernel==7.3.0
ipython==8.39.0
ipywidgets==8.1.9
isodate==0.7.2
isoduration==20.11.0
itsdangerous==2.2.0
jedi==0.20.1
jinja2==3.1.6
jiwer==4.0.0
jmespath==1.1.0
joblib==1.6.0
json5==0.17.3
jsonpatch==1.35
jsonpointer==3.2.1
jsonschema==4.26.0
jsonschema-specifications==2025.9.1
jupyter==1.1.1
jupyter-builder==1.2.3
jupyter-client==8.10.0
jupyter-console==6.6.3
jupyter-core==5.9.1
jupyter-events==0.12.1
jupyter-lsp==2.3.1
jupyter-server==2.21.1
jupyter-server-terminals==0.5.4
jupyterlab==4.6.4
jupyterlab-pygments==0.3.0
jupyterlab-server==2.28.1
jupyterlab-widgets==3.0.17
kiwisolver==1.5.1
kombu==5.6.2
kuzu==0.11.3
lark==1.3.1
lazy-loader==0.6
levenshtein==0.27.4
librosa==0.11.0
lief==1.0.0
limits==5.8.0
llvmlite==0.50.0
mako==1.4.3
markdown==3.10.3
markdown-it-py==4.2.0
markupsafe==3.0.4
matplotlib==3.10.9
matplotlib-inline==0.2.2
maturin==1.15.0
mdurl==0.1.2
minio==7.2.20
mistune==3.3.4
mlflow==3.17.1
mlflow-skinny==3.17.1
mlflow-tracing==3.17.1
more-itertools==11.1.0
mpi4py==4.1.2
mpmath==1.3.0
msgpack==1.2.3
multidict==7.1.0
narwhals==2.27.1
nbclient==0.11.0
nbconvert==7.17.2
nbformat==5.11.1
neo4j==6.4.1
nest-asyncio2==1.7.4
netcdf4==1.7.4
networkx==3.4.2
notebook==7.6.3
notebook-shim==0.2.4
numba==0.68.0
numpy==2.2.6
onnxruntime==1.23.2
openai-whisper==20250625
opencv-python==5.0.0.93
opencv-python-headless==5.0.0.93
opentelemetry-api==1.45.1
opentelemetry-proto==1.45.1
opentelemetry-sdk==1.45.1
opentelemetry-semantic-conventions==0.66b1
ordered-set==4.1.0
overrides==7.7.0
packaging==26.3
pandas==2.3.3
pandocfilters==1.5.1
papermill==2.7.0
paramiko==5.0.0
parso==0.8.7
patsy==1.0.3
pexpect==4.9.0
pillow==12.3.0
pip==26.2.1
platformdirs==4.12.4
pluggy==1.7.0
pocketsphinx==5.1.1
polars==2.0.0
polars-runtime-32==2.0.0
pooch==1.9.0
prettytable==3.18.0
prometheus-client==0.26.0
prompt-toolkit==3.0.53
propcache==0.5.4
protobuf==7.36.2
psutil==7.2.2
psycopg2-binary==2.9.13
ptyprocess==0.7.0
pure-eval==0.2.4
pyarrow==25.0.1
pyasn1==0.6.4
pyasn1-modules==0.4.2
pybind11==3.1.0
pycparser==3.11
pycryptodome==4.0.0
pydantic==2.14.1
pydantic-core==2.50.1
pydub==0.25.1
pyelftools==0.33
pyftpdlib==2.2.0
pygments==2.21.0
pyinstaller==6.22.3
pyinstaller-hooks-contrib==2026.8
pyjwt==2.15.1
pymongo==4.18.3
pynacl==1.6.2
pyopenssl==26.4.0
pyparsing==3.3.3
pytesseract==0.3.13
pytest==9.1.1
python-barcode==0.16.1
python-dateutil==2.9.0.post0
python-dotenv==1.2.4
python-json-logger==4.2.0
python-levenshtein==0.27.4
python-multipart==0.0.32
pytz==2026.5
pyyaml==6.0.3
pyzbar==0.1.9
pyzmq==27.2.0
qrcode==8.2
rapidfuzz==3.14.5
rdflib==7.6.0
redis==8.1.0
referencing==0.37.0
regex==2026.9.29
requests==2.34.2
rfc3339-validator==0.1.4
rfc3986-validator==0.1.1
rfc3987-syntax==1.1.0
rich==15.0.0
rpds-py==0.30.0
rq==2.12.0
ruamel-yaml==0.19.1
s3transfer==0.19.2
safetensors==0.8.0
scapy==2.8.0
scikit-image==0.25.2
scikit-learn==1.7.2
scipy==1.15.3
seaborn==0.13.2
semantic-version==2.10.0
semver==3.1.0
send2trash==2.1.0
sentence-transformers==6.1.0
setuptools==84.0.0
setuptools-rust==1.13.0
setuptools-scm==10.3.4
shellingham==1.5.4
simplejson==4.2.0
six==1.17.0
skops==0.16.0
smmap==5.0.3
sortedcontainers==2.4.0
sounddevice==0.5.6
soundfile==0.14.0
soupsieve==2.10
soxr==1.1.0
speechrecognition==3.17.0
sqlalchemy==2.0.55
sqlglot==30.23.0
sqlite-fts4==1.0.3
sqlparse==0.6.0
stack-data==0.6.3
starlette==1.7.0
statsmodels==0.15.0
supervisor==4.3.0
sympy==1.14.0
tabulate==0.10.0
tenacity==9.2.1
terminado==0.18.1
thefuzz==0.22.1
threadpoolctl==3.7.0
tifffile==2025.5.10
tiktoken==0.14.0
tinycss2==1.5.1
tokenizers==0.23.3
tomli==2.5.0
tomlkit==0.15.2
torch==2.14.1+cpu
torchaudio==2.11.0+cpu
torchvision==0.29.1+cpu
tornado==6.5.10
tqdm==4.70.1
traitlets==5.16.1
transformers==5.17.0
triton==3.8.0
typer==0.27.3
typing-extensions==4.16.0
typing-inspection==0.4.4
tzdata==2026.5
tzlocal==5.4.4
uri-template==1.3.0
urllib3==2.8.0
uvicorn==0.54.0
vcs-versioning==2.6.0
vine==5.1.0
waitress==3.0.2
watchdog==6.0.0
wcwidth==0.9.2
webcolors==25.10.0
webencodings==0.6.1
websocket-client==1.9.2
websockets==16.1.1
werkzeug==3.1.9
wheel==0.48.0
whisper-ctranslate2==0.5.8
widgetsnbextension==4.0.16
wrapt==2.5.1
xmltodict==1.0.4
xxhash==4.0.1
yarl==1.25.1
yq==4.4.5
zipp==4.1.1
""",
}


def superset_prefix(family, apt, pip):
    """Install script of a superset image. Unlike the upstream scripts it stops at the first
    failing command, so a broken superset cannot pass as a built image."""
    lock = SUPERSET_PIP_LOCKS[family]
    normalized = lambda name: re.sub(r"[-_.]+", "-", re.split(r"[=<>~!\[ ;]", name)[0].lower())
    locked = {normalized(line) for line in lock.splitlines() if line.strip() and not line.startswith("#")}
    missing = {normalized(name) for name in pip} - locked
    if missing:
        raise ValueError(f"{family} superset lock lacks {sorted(missing)}; regenerate it (see SUPERSET_PIP_LOCKS)")
    return ("set -e -o pipefail\n"
            f"apt-get update && apt-get install -y {' '.join(sorted(apt))}\n"
            "python3 -m pip install --no-cache-dir -U pip\n"
            f"cat > {BUILD_DIR}/superset-requirements.txt <<'EOF'\n{lock.rstrip()}\nEOF\n"
            # apt's python3-* packages (sympy, pexpect) cannot be uninstalled by pip; the lock
            # goes to /usr/local, which precedes /usr/lib/python3 on sys.path.
            f"python3 -m pip install --no-cache-dir --ignore-installed --extra-index-url {PIP_INDEX} "
            f"-r {BUILD_DIR}/superset-requirements.txt\n")


def install_mentions(instruction, extras):
    """Extra packages the instruction mentions near the word install."""
    text = instruction.lower()
    return sorted(name for name in extras if len(name) > 2 and name not in COMMON_WORDS and re.search(
        r"install\w*[^.\n]{0,80}(?<![a-z0-9_.-])" + re.escape(name) + r"(?![a-z0-9_-])", text))


def runtime_setup_trigger():
    """True when the shared runner runs setup_files/setup.sh for a task without solution/."""
    from validation.stages.task_setup import detect
    with tempfile.TemporaryDirectory() as directory:
        task = Path(directory) / "task"
        (task / "setup_files").mkdir(parents=True)
        (task / "setup_files/setup.sh").write_text(SETUP_SH)
        try:
            return detect(task) == SETUP_COMMAND
        except Exception:
            return False


def payload_hash(files):
    digest = hashlib.sha256()
    for name in sorted(name for name in files if name.startswith("environment/")):
        digest.update(name.removeprefix("environment/").encode() + b"\0" + hashlib.sha256(files[name][0]).digest())
    return digest.hexdigest()


def main():
    import pyarrow as pa
    import pyarrow.parquet as pq
    from data.utils.full_source.harbor_parquet import pack_task
    from data.utils.patch_reporting import write_patch_report

    parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0])
    parser.add_argument("--source", type=Path, required=True)
    parser.add_argument("--output", type=Path, required=True)
    parser.add_argument("--download-cache", type=Path,
                        help="cache filled by fetch-downloads; without it downloads stay in per-task images")
    parser.add_argument("--runtime-setup", choices=["auto", "off", "force"], default="auto",
                        help="auto: share images only if the runner has a setup trigger for tasks "
                             "without solution/. force: for static inspection only; without the "
                             "trigger such tasks start without their initial files")
    args = parser.parse_args()
    global DOWNLOAD_CACHE
    DOWNLOAD_CACHE = args.download_cache
    source = args.source.resolve()
    files = [source] if source.is_file() else sorted(source.glob("*.parquet"))
    if len(files) != 1 or files[0].suffix != ".parquet":
        raise ValueError(f"expected the one pinned TMAX Parquet file in {source}")
    source = files[0]
    args.output.mkdir(parents=True, exist_ok=True)
    target = args.output / "tasks.parquet"
    if target.exists():
        raise FileExistsError(target)

    # Pass 1: classify every task and form the image groups.
    tasks, groups, payloads = {}, defaultdict(list), {}
    reader = pq.ParquetFile(source)
    for batch in reader.iter_batches(batch_size=64):
        for row in batch.to_pylist():
            if row["path"] in tasks:
                raise ValueError(f"duplicate task ID: {row['path']}")
            content = read_task(row["task_binary"])
            info = classify(content)
            if row["path"] in UNSHARED:
                info["findings"].append("listed-unshared")
            payloads[row["path"]] = payload_hash(content) if content else row["path"]
            tasks[row["path"]] = {key: info.get(key) for key in ("family", "findings", "group", "prefix", "rust")}
            tasks[row["path"]]["download_bytes"] = sum(map(len, info.get("downloads", {}).values()))
            if not info["findings"]:
                apt, pip = prefix_packages(info["prefix"])
                tasks[row["path"]].update(apt=apt, pip=pip, instruction=content["instruction.md"][0].decode(
                    errors="replace") if "instruction.md" in content else "")
    if len(tasks) != TASKS:
        raise ValueError(f"expected {TASKS} tasks in the pinned source, found {len(tasks)}")
    if set(UNSHARED) - set(tasks):
        raise ValueError("UNSHARED names tasks absent from the source")

    # Superset images: the union of packages of the family's tasks without version pins.
    supersets, exact_reasons = {}, Counter()
    for family in ("legacy", "v2"):
        members = [info for info in tasks.values() if info["family"] == family and not info["findings"]]
        unpinned = [info for info in members if all(value == name for name, value in info["pip"].items())]
        for info in unpinned:
            if info["apt"] & APT_CONFLICTS:
                info["exact"] = "apt-package-conflicts-with-superset"
        unpinned = [info for info in unpinned if not info.get("exact")]
        apt = set().union(*(info["apt"] for info in unpinned))
        pip = set().union(*(info["pip"] for info in unpinned))
        supersets[family] = superset_prefix(family, apt, pip)
        if any(info["rust"] for info in unpinned):
            supersets[family] += RUSTUP_IMAGE_INSTALL
        for info in members:
            if info not in unpinned and info["rust"]:
                info["findings"].append("rustup-without-superset")
            elif info not in unpinned and not info.get("exact"):
                info["exact"] = "pinned-pip-version"
            elif mentioned := install_mentions(info["instruction"], (apt - info["apt"]) | (pip - set(info["pip"]))):
                # Mostly "you may install X if needed" or installs into a fresh venv. Kept on the
                # superset; a task the extra packages solve fails the no-op stage and is then
                # listed in UNSHARED with its measured reason.
                info["mentioned"] = mentioned
    for task, info in tasks.items():
        if info["findings"]:
            continue
        if info.get("exact"):
            exact_reasons[info["exact"]] += 1
        else:
            info["superset"] = True
            info["prefix"] = supersets[info["family"]]
        info["group"] = hashlib.sha256((info["family"] + "\0" + info["prefix"]).encode()).hexdigest()[:12]
        groups[info["group"]].append(task)
    shared = {task: group for group, members in groups.items() if len(members) > 1 for task in members}

    trigger = runtime_setup_trigger()
    apply = args.runtime_setup == "force" or (args.runtime_setup == "auto" and trigger)

    # Pass 2: write the tasks.
    before = set(payloads.values())
    if_applied = {payloads[task] for task in tasks if task not in shared} | set(shared.values())
    after = set()
    compatible, node, wrapper, repaired = {}, {}, {}, {}
    temporary = target.with_suffix(".parquet.tmp")
    with pq.ParquetWriter(temporary, reader.schema_arrow, compression="zstd") as writer:
        for batch in reader.iter_batches(batch_size=64):
            table = batch.to_pydict()
            for i, task in enumerate(table["path"]):
                content = read_task(table["task_binary"][i])
                if apply and task in shared:
                    content = shared_files(content, {**classify(content), "prefix": tasks[task]["prefix"]})
                elif content and (post := nodesource_compatible(content)):
                    content["environment/post_install.sh"] = (post, content["environment/post_install.sh"][1])
                    node[task] = True
                if apply and task in shared and task in SERVICE_SETUP:
                    service_setup_compatible(content, task)
                if content and task in TASK_REPAIRS:
                    name, old, new, count, _ = TASK_REPAIRS[task]
                    text = content[name][0].decode()
                    if text.count(old) != count:
                        raise ValueError(f"{task}: repair text not found {count} times in {name}")
                    content[name] = (text.replace(old, new).encode(), content[name][1])
                    repaired[task] = True
                if content and task in VERIFIER_REPAIRS:
                    name = "tests/test_final_state.py"
                    text = content[name][0].decode()
                    repair = VERIFIER_REPAIRS[task]
                    for old, new, count in repair["edits"]:
                        if text.count(old) != count:
                            raise ValueError(f"{task}: verifier repair anchor count differs")
                        text = text.replace(old, new)
                    text += repair["append"]
                    content[name] = (text.encode(), content[name][1])
                    repaired[task] = True
                for name, script in (wrapper_output_compatible(content) if content else {}).items():
                    content[name] = (script, content[name][1])
                    wrapper[task] = True
                if content and tasks[task]["family"] and "unknown-dockerfile" not in tasks[task]["findings"]:
                    dockerfile, mode = content["environment/Dockerfile"]
                    content["environment/Dockerfile"] = (build_compatible(dockerfile.decode()).encode(), mode)
                    compatible[task] = tasks[task]["family"]
                    table["task_binary"][i] = pack_task(content)
                after.add(payload_hash(content) if content else task)
            kept = [i for i, task in enumerate(table["path"]) if task not in ARCHIVED]
            writer.write_table(pa.table({key: [values[i] for i in kept] for key, values in table.items()}, schema=reader.schema_arrow))
    temporary.replace(target)
    if pq.ParquetFile(target).metadata.num_rows != TASKS - len(ARCHIVED):
        raise ValueError("task count changed")

    changed = shared if apply else {}
    labels = {task: [COMPATIBILITY_LABEL] for task in compatible}
    reasons = {task: [COMPATIBILITY_REASON[family]] for task, family in compatible.items()}
    for task, reason in UNSHARED.items():
        labels.setdefault(task, []).append("compile-during-image-build")
        reasons.setdefault(task, []).append(reason)
    for task in SERVICE_SETUP:
        if task in changed:
            labels.setdefault(task, []).append("service-start-once")
            reasons.setdefault(task, []).append(SERVICE_REASON)
    for task in node:
        labels.setdefault(task, []).append(NODE_LABEL)
        reasons.setdefault(task, []).append(NODE_REASON)
    for task in wrapper:
        labels.setdefault(task, []).append(WRAPPER_LABEL)
        reasons.setdefault(task, []).append(WRAPPER_REASON)
    for task in repaired:
        labels.setdefault(task, []).append(REPAIR_LABEL)
        reasons.setdefault(task, []).append(VERIFIER_REPAIRS[task]["reason"] if task in VERIFIER_REPAIRS else TASK_REPAIRS[task][4])
    for task, group in changed.items():
        labels.setdefault(task, []).extend(LABELS)
        if tasks[task].get("superset"):
            labels[task] = [label if label != "shared-image" else SUPERSET_LABEL for label in labels[task]]
            reasons.setdefault(task, []).append(SUPERSET_REASON.format(
                family=tasks[task]["family"], size=len(groups[group])))
        else:
            reasons.setdefault(task, []).append(REASON.format(group=group, size=len(groups[group])))
    write_patch_report(
        source, target, source={"dataset": DATASET, "url": SOURCE_URL, "revision": REVISION},
        dropped=ARCHIVED, patcher=__file__,
        change_labels={task: value for task, value in labels.items() if task not in ARCHIVED},
        change_reasons={task: " ".join(parts) for task, parts in reasons.items() if task not in ARCHIVED})

    def family_counts(selected):
        return dict(sorted(Counter(tasks[task]["family"] or "unknown" for task in selected).items()))
    regular = [task for task, info in tasks.items() if not info["findings"]]
    shared_groups = {group: members for group, members in groups.items() if len(members) > 1}
    manifest = {
        "dataset": DATASET, "source_revision": REVISION,
        "runtime_setup": {
            "mode": args.runtime_setup, "runner_has_setup_trigger": trigger, "applied": apply,
            "note": None if apply else (
                "No task was moved to a shared image: validation.stages.task_setup.detect returns no setup command "
                "for a task with setup_files/setup.sh and no solution/solve.sh, so files moved to "
                "setup_files/ would not be executed. The counts below describe what the shared-image "
                "change would do once the runner provides that trigger.")},
        "counts": {
            "tasks": len(tasks) - len(ARCHIVED), "archived": len(ARCHIVED), "changed": len(set(changed) - set(ARCHIVED)), "build_compatible": len(compatible), "nodesource_purge": len(node), "python3_wrapper_output": len(wrapper), "task_repairs": len(repaired),
            "tasks_by_family": family_counts(tasks),
            "regular": len(regular), "regular_by_family": family_counts(regular),
            "irregular": len(tasks) - len(regular),
            "irregular_by_finding": dict(Counter(
                finding for info in tasks.values() for finding in info["findings"]).most_common()),
            "regular_in_single_task_group": len(regular) - len(shared),
            "shareable": len(shared), "shareable_by_family": family_counts(shared),
            "shared_groups": len(shared_groups),
            "superset_tasks": dict(sorted(Counter(info["family"] for info in tasks.values()
                                                  if info.get("superset")).items())),
            "exact_group_reasons": dict(exact_reasons.most_common()),
            "offline_downloads": {"tasks": sum(1 for info in tasks.values() if info["download_bytes"] and not info["findings"]),
                                  "bytes": sum(info["download_bytes"] for info in tasks.values() if not info["findings"])},
            "rust_from_image": sum(1 for info in tasks.values() if info["rust"] and not info["findings"]),
            "unique_images_before": len(before), "unique_images_after": len(after),
            "unique_images_if_applied": len(if_applied),
        },
        "groups": [{"group": group, "family": tasks[members[0]]["family"], "tasks": len(members),
                    "install_script": tasks[members[0]]["prefix"]}
                   for group, members in sorted(shared_groups.items(), key=lambda item: (-len(item[1]), item[0]))],
        "tasks": {task: {"family": info["family"],
                         "image": ("superset" if info.get("superset") and task in shared else
                                   "shared" if task in shared else "per-task"),
                         **({"exact_reason": info["exact"]} if info.get("exact") else {}),
                         **({"mentioned": info["mentioned"]} if info.get("mentioned") else {}),
                         **({"group": info["group"]} if info["group"] else {}),
                         **({"findings": info["findings"]} if info["findings"] else {})}
                  for task, info in sorted(tasks.items())},
    }
    (args.output / "tmax_patch_manifest.json").write_text(json.dumps(manifest, indent=1) + "\n")
    print(json.dumps({"runtime_setup": manifest["runtime_setup"], "counts": manifest["counts"]}, indent=1))


# Prepare pilot
"""Select a reproducible twenty-task TMAX Harbor registry subset."""

import random


def prepare_pilot_main():
    parser = argparse.ArgumentParser()
    parser.add_argument("registry", type=Path)
    parser.add_argument("output", type=Path)
    parser.add_argument("--size", type=int, default=20)
    parser.add_argument("--seed", type=int, default=42)
    args = parser.parse_args()
    datasets = json.loads(args.registry.read_text())
    source = next(dataset for dataset in datasets if dataset["name"] == "tmax")
    selected = sorted(random.Random(args.seed).sample(source["tasks"], args.size), key=lambda task: task["name"])
    subset = {**source, "name": "tmax-pilot", "tasks": selected}
    args.output.parent.mkdir(parents=True, exist_ok=True)
    args.output.write_text(json.dumps([subset], indent=2) + "\n")
    manifest = {
        "source": "PrimeIntellect-ai/prime-envs registry tmax@2026-07-01",
        "seed": args.seed,
        "task_count": len(selected),
        "tasks": selected,
    }
    args.output.with_name("selection.json").write_text(json.dumps(manifest, indent=2) + "\n")


def prepare_pilot_cli():
    prepare_pilot_main()


if __name__ == "__main__":
    from data.utils.cli import dispatch
    dispatch({'prepare-pilot': prepare_pilot_cli, 'fetch-downloads': fetch_downloads_main})
    main()