File size: 132,149 Bytes
3fd7410
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
#!/usr/bin/env python3
"""Step 02: test models with interactive step-loop (simulated or Gemini API)."""

from __future__ import annotations

import argparse
from datetime import datetime, timezone
import base64
import csv
import hashlib
import json
import math
import os
import pickle
import random
import re
import subprocess
import sys
import time
import platform
import zlib
from concurrent.futures import ThreadPoolExecutor
from pathlib import Path
from time import perf_counter
from typing import Any, Dict, List, Optional
try:
    from tqdm import tqdm
except Exception:
    def tqdm(iterable=None, **kwargs):  # type: ignore
        return iterable
try:
    import numpy as np
except Exception:
    np = None
try:
    import cv2
except Exception:
    cv2 = None

from llm_client import API_KEY, BASE_URL, DEFAULT_MODEL, chat_completions
from benchmark_core import (
    ACTIONS,
    Pose,
    apply_action,
    get_last_render_backend,
    iou,
    now_ms,
    read_json,
    render_views_fast,
    resolve_paths,
    write_json,
    write_png_gray,
)
from render_protocol import (
    RENDER_PARAMETER_SCHEMA,
    STANDARD_DISPLAY_SIZE,
    STANDARD_IMAGE_SIZE,
    STANDARD_POINT_BATCH_SIZE,
    STANDARD_PANEL_SEPARATOR,
    STANDARD_PANEL_SEPARATOR_VALUE,
    require_standard_square_size,
    require_cuda_backend,
)

BASE_ACTIONS = [a for a in ACTIONS if a != "STOP"]
ALL_ACTIONS = BASE_ACTIONS + ["STOP"]
VALID_STEP_ANGLES = [30, 10, 5, 1]
PROMPT_PROFILES = ["baseline", "geometry_audit", "projection_first", "game_manual_plus"]
METRIC_FEEDBACK_MODES = ["full", "threshold", "none"]
METRIC_BLIND_SENTENCE = (
    "The task ends automatically when the requirement is met. "
    "If it continues, the current result has not yet met the requirement."
)
RENDER_SEMAPHORE_ENV = "PROJECTION_RENDER_SEMAPHORE"
RENDER_SEMAPHORE_COUNT_ENV = "PROJECTION_RENDER_SEMAPHORE_COUNT"


def render_views_limited(*args, **kwargs):
    """Serialize CUDA rendering across parallel task subprocesses on Windows."""
    semaphore_name = os.environ.get(RENDER_SEMAPHORE_ENV, "").strip()
    if not semaphore_name or os.name != "nt":
        result = render_views_fast(*args, **kwargs)
        require_cuda_backend(get_last_render_backend(), context="LLM benchmark")
        return result

    import ctypes

    count = max(1, int(os.environ.get(RENDER_SEMAPHORE_COUNT_ENV, "1")))
    kernel32 = ctypes.WinDLL("kernel32", use_last_error=True)
    kernel32.CreateSemaphoreW.restype = ctypes.c_void_p
    kernel32.CreateSemaphoreW.argtypes = [ctypes.c_void_p, ctypes.c_long, ctypes.c_long, ctypes.c_wchar_p]
    kernel32.WaitForSingleObject.argtypes = [ctypes.c_void_p, ctypes.c_ulong]
    kernel32.ReleaseSemaphore.argtypes = [ctypes.c_void_p, ctypes.c_long, ctypes.c_void_p]
    kernel32.CloseHandle.argtypes = [ctypes.c_void_p]
    handle = kernel32.CreateSemaphoreW(None, count, count, semaphore_name)
    if not handle:
        raise OSError(ctypes.get_last_error(), "CreateSemaphoreW failed")
    try:
        wait_result = kernel32.WaitForSingleObject(handle, 0xFFFFFFFF)
        if wait_result != 0:
            raise OSError(ctypes.get_last_error(), f"WaitForSingleObject failed: {wait_result}")
        try:
            result = render_views_fast(*args, **kwargs)
            require_cuda_backend(get_last_render_backend(), context="LLM benchmark")
            return result
        finally:
            if not kernel32.ReleaseSemaphore(handle, 1, None):
                raise OSError(ctypes.get_last_error(), "ReleaseSemaphore failed")
    finally:
        kernel32.CloseHandle(handle)


def read_selected_tasks_and_ids(
    path: Path, max_tasks: int, split_path: Optional[Path] = None
) -> tuple[List[Dict], List[str]]:
    split_ids: Optional[List[str]] = None
    wanted_ids: Optional[set[str]] = None
    if split_path is not None:
        split = read_json(split_path)
        split_ids = [str(value) for value in split.get("sample_ids", [])]
        if len(split_ids) != len(set(split_ids)):
            raise RuntimeError(f"Duplicate task ID in split file: {split_path}")
        wanted_ids = set(split_ids)

    # Keep only selected tasks in memory. The canonical JSONL is over 1 GB;
    # loading every task is unnecessary when evaluating a frozen split.
    selected_by_id: Dict[str, Dict] = {}
    all_task_ids: List[str] = []
    with path.open("r", encoding="utf-8") as f:
        for line in f:
            s = line.strip()
            if not s:
                continue
            task = json.loads(s)
            sample_id = str(task.get("sample_id", "")).strip()
            if sample_id:
                all_task_ids.append(sample_id)
                if wanted_ids is None or sample_id in wanted_ids:
                    if sample_id in selected_by_id:
                        raise RuntimeError(f"Duplicate task ID in tasks file: {sample_id}")
                    selected_by_id[sample_id] = task
    if split_ids is not None:
        missing = [sample_id for sample_id in split_ids if sample_id not in selected_by_id]
        if missing:
            raise RuntimeError(f"Split IDs missing from tasks file: {missing[:5]}")
        selected = [selected_by_id[sample_id] for sample_id in split_ids]
    else:
        selected = [selected_by_id[sample_id] for sample_id in all_task_ids]
    if max_tasks > 0:
        selected = selected[:max_tasks]
    return selected, all_task_ids


def resolve_optional_path(root: Path, value: str) -> Optional[Path]:
    if not str(value or "").strip():
        return None
    path = Path(value)
    return path if path.is_absolute() else root / path


def resolve_stop_outcome(score: float, tau_success: float) -> tuple[bool, str]:
    if score >= tau_success:
        return True, "threshold_reached"
    return False, "stop_no_further_idea"


def select_history_slice(items: List, history_window: int) -> List:
    if history_window < 0:
        return list(items)
    if history_window == 0:
        return []
    return list(items[-history_window:])


def resolve_metric_feedback_mode(mode: str, hide_iou_feedback: bool = False) -> str:
    if hide_iou_feedback:
        return "none"
    value = str(mode or "full").strip().lower()
    if value not in METRIC_FEEDBACK_MODES:
        raise ValueError(f"Unsupported metric feedback mode: {mode}")
    return value


def build_metric_guidance(mode: str, current_iou: float, tau_success: float) -> str:
    mode = resolve_metric_feedback_mode(mode)
    if mode == "full":
        return f"Current IoU before the next action: {current_iou:.6f}. Success threshold: {tau_success:.6f}."
    if mode == "threshold":
        return "The success condition is IoU >= 0.9."
    return METRIC_BLIND_SENTENCE


def metric_feedback_result_dir(mode: str) -> str:
    return {
        "none": "no_iou",
        "threshold": "iou_threshold_0.9",
        "full": "iou_feedback",
    }[resolve_metric_feedback_mode(mode)]


def render_environment_metadata() -> Dict[str, Any]:
    metadata: Dict[str, Any] = {
        "renderer": "benchmark_core.render_views_fast",
        "render_parameters": RENDER_PARAMETER_SCHEMA,
        "python_executable": sys.executable,
        "platform": platform.platform(),
        "python_version": platform.python_version(),
        "numpy_version": getattr(np, "__version__", "unavailable"),
        "opencv_version": getattr(cv2, "__version__", "unavailable"),
        "cuda_visible_devices": os.environ.get("CUDA_VISIBLE_DEVICES", ""),
        "num_threads": os.environ.get("OMP_NUM_THREADS", ""),
        "mkl_num_threads": os.environ.get("MKL_NUM_THREADS", ""),
    }
    try:
        import torch

        metadata["torch_version"] = str(torch.__version__)
        metadata["torch_cuda_version"] = str(torch.version.cuda or "")
        metadata["cuda_available"] = bool(torch.cuda.is_available())
        metadata["cuda_device_count"] = int(torch.cuda.device_count())
        metadata["cuda_device"] = torch.cuda.get_device_name(0) if torch.cuda.is_available() else ""
        metadata["torch_import_error"] = ""
    except Exception as exc:
        metadata["torch_version"] = "unavailable"
        metadata["torch_cuda_version"] = ""
        metadata["cuda_available"] = False
        metadata["cuda_device_count"] = 0
        metadata["cuda_device"] = ""
        metadata["torch_import_error"] = f"{type(exc).__name__}: {exc}"
    return metadata


def mask_sha256(mask) -> str:
    if np is not None and isinstance(mask, np.ndarray):
        return hashlib.sha256(mask.tobytes(order="C")).hexdigest()
    return hashlib.sha256(json.dumps(mask, separators=(",", ":")).encode("utf-8")).hexdigest()


def parse_allowed_angles(text: str) -> List[int]:
    vals: List[int] = []
    for p in str(text or "").split(","):
        s = p.strip()
        if not s:
            continue
        try:
            v = int(s)
        except Exception:
            continue
        if v in VALID_STEP_ANGLES and v not in vals:
            vals.append(v)
    return vals if vals else list(VALID_STEP_ANGLES)


def build_prompt_text_v1(
    *,
    effective_mode: str,
    prompt_profile: str,
    token_set_text: str,
    angle_set_text: str,
    step: int,
    step_limit: int,
    current_iou: float,
    tau_success: float,
    last_action: str,
    history_actions: List[str],
    history_window: int,
    recent_state_summaries: List[str],
    stop_caution: bool = False,
    hide_iou_feedback: bool = False,
    legacy_stop_as_success: bool = False,
) -> str:
    history_selected_actions = select_history_slice(history_actions, int(history_window))
    history_selected_summaries = select_history_slice(recent_state_summaries, int(history_window))
    history_label = "all previous states" if int(history_window) < 0 else f"up to {max(0, int(history_window))} previous states"
    observation_text = "images" if hide_iou_feedback else "images and feedback"
    task_intro = (
        "Task: interactive 3D-to-2D alignment game.\n"
        "Goal: rotate the current 3D object so that CURRENT projection matches TARGET projection as closely as possible.\n"
        f"This is a turn-based game: in each turn you observe the {observation_text}, then choose exactly one rotation action.\n"
        "After your action, the environment updates the object pose and gives you a new observation on the next turn.\n"
        "Your objective is to maximize alignment quality within the available turn budget.\n"
    )
    stop_protocol = (
        "Termination protocol:\n"
        "- The environment automatically ends the task successfully as soon as the hidden IoU reaches 0.9.\n"
        "- Do not use STOP to indicate that the projections appear aligned.\n"
        "- Use STOP only when you have no remaining actionable idea and believe further legal rotations cannot improve alignment.\n"
        if not legacy_stop_as_success
        else ""
    )

    input_definition = (
        "Image meaning:\n"
        "- TARGET projection: desired silhouette to match.\n"
        "- CURRENT oblique view: current 3D appearance from an oblique camera.\n"
        "- CURRENT projection: current silhouette that should align to TARGET.\n"
        "- The fixed panel order is always TARGET | CURRENT oblique | CURRENT projection.\n"
    )

    if hide_iou_feedback:
        feedback_definition = (
            "Turn budget before you choose the next action:\n"
            f"- Used steps: {max(0, step - 1)} / {step_limit}\n"
            f"- Remaining steps: {max(0, step_limit - step + 1)}\n"
            + ("- STOP means no remaining actionable idea, not visual success.\n" if not legacy_stop_as_success else "- Stop only when the silhouettes are visually already aligned.\n")
        )
    else:
        feedback_definition = (
            "Environment feedback before you choose the next action:\n"
            f"- Current IoU: {current_iou:.6f}\n"
            f"- Success threshold: {tau_success:.6f}\n"
            f"- Used steps: {max(0, step - 1)} / {step_limit}\n"
            f"- Remaining steps: {max(0, step_limit - step + 1)}\n"
            + ("- Higher IoU is better. The environment automatically ends the task successfully at IoU 0.9; STOP means no remaining actionable idea.\n" if not legacy_stop_as_success else "- Higher IoU is better. Stop only when current IoU is already at or above the success threshold or the silhouettes are visually already aligned.\n")
            + (
                "- Before choosing STOP, compare Current IoU with the Success threshold. If Current IoU is far below the threshold, do not assume success from visual impression; choose a non-STOP rotation to keep searching.\n"
                if stop_caution
                else ""
            )
            + "- The IoU shown here is the score of the CURRENT state before you choose the next action.\n"
        )

    score_rules = "" if hide_iou_feedback else (
        "- IoU is the official score used by the environment. Larger IoU means CURRENT projection overlaps TARGET projection better.\n"
        "- You may use both the images and the score feedback to decide the next move.\n"
        "- Success is determined only by the environment score threshold, not by language confidence.\n"
    )
    rule_definition = (
        "Game rules:\n"
        "- You control only the CURRENT object, never the TARGET.\n"
        "- Each turn you may output exactly one action token and one allowed angle.\n"
        "- The camera is fixed. Rotation tokens are defined in the observer/camera coordinate system.\n"
        "- The environment is deterministic: the next observation depends only on the current pose and your chosen action.\n"
        + score_rules
    )

    action_definition = "".join(
        [
            "Action tokens:\n",
            "- PITCH_POS / PITCH_NEG: observer-frame pitch rotation by +angle / -angle around the camera-fixed horizontal axis.\n",
            "- Intuition: PITCH_POS pitches the CURRENT object upward relative to the observer; PITCH_NEG pitches it downward relative to the observer.\n",
            "- YAW_POS / YAW_NEG: observer-frame yaw rotation by +angle / -angle around the camera-fixed vertical axis.\n",
            "- Intuition: YAW_POS turns the CURRENT object toward the observer's right; YAW_NEG turns it toward the observer's left.\n",
            "- ROLL_POS / ROLL_NEG: observer-frame roll rotation by +angle / -angle around the viewing axis.\n",
            "- In the image plane, ROLL_POS is clockwise and ROLL_NEG is counter-clockwise.\n",
            "- STOP: choose this only when you have no remaining actionable idea and believe further legal rotations cannot improve alignment.\n"
            if not legacy_stop_as_success
            else "- STOP: choose this only when you believe CURRENT projection is already aligned with TARGET.\n",
            (
                "- STOP caution: if the reported IoU is still far below the success threshold, treat STOP as premature and prefer another legal rotation.\n"
                if stop_caution and not hide_iou_feedback
                else ""
            ),
        ]
    )

    score_guidance = "" if hide_iou_feedback else "- Use the IoU feedback as evidence about how well the current state matches the target.\n"
    guidance_definition = (
        "Decision goal:\n"
        "- Your job is not to describe the image, but to choose the single next move that you believe will improve alignment.\n"
        "- Use the TARGET vs CURRENT projection comparison to judge mismatch.\n"
        "- Use the oblique view to understand the 3D pose and how a rotation might change the projection.\n"
        + score_guidance
        + "- Treat each turn as a fresh decision based on the latest observation and feedback.\n"
        + "- Think freely about strategy, but obey the action set and angle set exactly.\n"
    )

    history_definition = (
        "History semantics:\n"
        "- The history is a temporal sequence, not a bag of examples.\n"
        "- The order is always: previous state image -> action taken at that state -> next state image -> next action -> next state image ...\n"
        "- Therefore each historical panel shows a real past state before the following recorded action in the history list.\n"
        "- Read recent panels from oldest to newest. They are consecutive earlier turns leading up to the current state.\n"
        "- If you receive N recent panels, then the actions listed alongside them describe the transitions between those past states and the later states.\n"
        "- Use history to infer how the environment responds to actions, but act for the current final panel only.\n"
    )

    output_schema = (
        "Output must be JSON object only: {\"cot\":\"<your brief reasoning>\",\"type\":\"<TOKEN>\",\"angle\":<ANGLE>}\n"
        "Requirements:\n"
        f"- type must be one of: {token_set_text}.\n"
        f"- angle must be one of: {angle_set_text} (even when type is STOP).\n"
        "- cot should briefly explain why this action is chosen for the current step.\n"
        "- Any token outside this set is INVALID and will be treated as a failed answer.\n"
        "Valid examples:\n"
        "{\"cot\":\"Current view is tilted; adjust pitch.\",\"type\":\"PITCH_NEG\",\"angle\":30}\n"
        "{\"cot\":\"Need fine correction.\",\"type\":\"YAW_POS\",\"angle\":5}\n"
        "{\"cot\":\"Current and target silhouettes are nearly identical.\",\"type\":\"STOP\",\"angle\":10}\n"
        "Invalid example (DO NOT USE):\n"
        "{\"cot\":\"...\",\"type\":\"R_X+\",\"angle\":7}\n"
    )

    if effective_mode == "panel":
        layout_line = "Single panel layout: left=TARGET projection, middle=CURRENT oblique view, right=CURRENT projection.\n"
    else:
        layout_line = (
            "Image 1: target projection silhouette.\n"
            "Image 2: current oblique RGB-like view.\n"
            "Image 3: current projection silhouette.\n"
        )

    baseline_first_line = (
        "1. The right CURRENT projection is the matching view that must match the left TARGET projection.\n"
        if hide_iou_feedback
        else "1. The right CURRENT projection is the scoring view that must match the left TARGET projection.\n"
    )
    game_manual_plus = [
        "Interpretation notes:\n",
        "1. TARGET is fixed; only CURRENT can change from turn to turn.\n",
        "2. The oblique image is not the matching view; it is an auxiliary 3D cue for understanding pose.\n",
        "3. The objective is to match TARGET projection and CURRENT projection.\n",
    ] if hide_iou_feedback else [
        "Interpretation notes:\n",
        "1. TARGET is fixed; only CURRENT can change from turn to turn.\n",
        "2. The oblique image is not the scoring view; it is an auxiliary 3D cue for understanding pose.\n",
        "3. The scoring objective is overlap between TARGET projection and CURRENT projection.\n",
        "4. The reported IoU belongs to the present state before your next action, so use it together with the current images.\n",
        "5. Recent history is context, but the current images and current score are the authoritative game state.\n",
    ]
    profile_lines = {
        "baseline": [
            "Baseline note:\n",
            baseline_first_line,
            "2. The middle oblique image is only auxiliary 3D pose information for choosing the next move.\n",
        ],
        "geometry_audit": [
            "Decision procedure:\n",
            "1. Compare CURRENT projection against TARGET and identify the dominant mismatch.\n",
            "2. Use CURRENT oblique view only to infer which axis/direction can reduce that mismatch.\n",
            "3. Use 30 for obvious large mismatch, 10/5/1 for refinement.\n",
            "4. Use STOP only when silhouettes are nearly identical.\n",
        ],
        "projection_first": [
            "Priority rule:\n",
            "1. First trust the two silhouettes (TARGET vs CURRENT projection).\n",
            "2. Use the oblique view only as a tie-breaker for axis sign or depth ambiguity.\n",
            "3. If unsure, prefer a conservative corrective move over STOP.\n",
        ],
        "game_manual_plus": game_manual_plus,
    }
    return "".join(
        [
            task_intro,
            stop_protocol,
            output_schema,
            layout_line,
            input_definition,
            feedback_definition,
            rule_definition,
            action_definition,
            guidance_definition,
            history_definition,
            "".join(profile_lines.get(prompt_profile, [])),
            f"Current step: {step}.\n",
            f"Step limit: {step_limit}.\n",
            f"Last action: {last_action if last_action else 'NONE'}.\n",
            f"History window requested: {history_label}.\n",
            f"Recent actions in temporal order: {', '.join(history_selected_actions) if history_selected_actions else 'NONE'}.\n",
            "Recent step summaries:\n",
            "".join(f"- {line}\n" for line in history_selected_summaries) if history_selected_summaries else "- NONE\n",
        ]
    )


def build_system_prompt_v1(token_set_text: str, angle_set_text: str, history_window: int, legacy_stop_as_success: bool = False) -> str:
    history_label = "all previous states" if int(history_window) < 0 else f"up to {history_window} previous states"
    return (
        "You are playing a turn-based 3D alignment game. "
        "Return one JSON object only: {\"cot\":\"...\",\"type\":\"TOKEN\",\"angle\":30}. "
        f"TOKEN must be exactly one of: {token_set_text}. "
        f"ANGLE must be exactly one of: {angle_set_text}. "
        "The CURRENT object is controlled in observer coordinates: "
        "YAW_POS=turn toward observer right, YAW_NEG=turn toward observer left, "
        "PITCH_POS=pitch upward, PITCH_NEG=pitch downward, "
        "ROLL_POS=clockwise in the image plane, ROLL_NEG=counter-clockwise in the image plane. "
        f"If history is provided, it is a chronological sequence of {history_label}. "
        "Interpret it as state -> action -> next state -> action -> next state, with panels shown oldest to newest before the current state. "
        + ("The environment automatically terminates successfully when hidden IoU reaches 0.9. Do not use STOP to signal visual alignment; use STOP only when no actionable improvement idea remains. " if not legacy_stop_as_success else "")
        + "Keep cot concise so your entire output completion stays under 4000 output tokens. "
        "No extra text."
    )


def build_system_prompt_v2(
    token_set_text: str,
    angle_set_text: str,
    *,
    hide_iou_feedback: bool = False,
    legacy_stop_as_success: bool = False,
) -> str:
    stop_text = (
        "The environment ends automatically when the requirement is met. Use STOP only when no actionable improvement idea remains; "
        "do not use STOP merely because the silhouettes appear aligned."
        if not legacy_stop_as_success
        else "Use STOP only when CURRENT projection appears aligned with TARGET."
    )
    return (
        "You are playing a deterministic turn-based 3D-to-2D alignment game. "
        "Rotate only the CURRENT object so its projection matches TARGET within the step budget. "
        "Each observation provides images and the current task feedback. "
        "Each panel is one image ordered left-to-right as TARGET projection | CURRENT oblique view | CURRENT projection. "
        "The oblique view is an auxiliary 3D pose cue; compare TARGET with CURRENT projection for alignment. "
        "Rotations use observer/camera coordinates: PITCH_POS=pitch upward, PITCH_NEG=pitch downward, "
        "YAW_POS=turn toward observer right, YAW_NEG=turn toward observer left, "
        "ROLL_POS=clockwise in the image plane, ROLL_NEG=counter-clockwise. "
        f"Allowed action tokens: {token_set_text}. Allowed angles: {angle_set_text}. "
        "History is chronological. Each historical panel is the state before the action written immediately beside it. "
        f"{stop_text} "
        "Return exactly one JSON object and no extra text: "
        '{"cot":"brief current-step reasoning","type":"TOKEN","angle":ANGLE}. '
        "The type and angle must come from the allowed sets."
    )


def build_prompt_text_v2(
    *,
    step: int,
    step_limit: int,
    current_iou: float,
    tau_success: float,
    metric_guidance: str,
    hide_iou_feedback: bool = False,
) -> str:
    lines = [
        f"Current turn: {step}/{step_limit}.",
        f"Used steps: {max(0, step - 1)}.",
        f"Remaining steps: {max(0, step_limit - step + 1)}.",
    ]
    lines.append(metric_guidance)
    return "\n".join(lines) + "\n"


def resolve_effective_allowed_angles(task_angles, global_allowed_angles: List[int]) -> List[int]:
    task_vals: List[int] = []
    for x in task_angles or []:
        try:
            v = int(x)
        except Exception:
            continue
        if v in VALID_STEP_ANGLES and v not in task_vals:
            task_vals.append(v)
    effective = [v for v in global_allowed_angles if v in VALID_STEP_ANGLES]
    if not effective:
        effective = list(VALID_STEP_ANGLES)
    for v in task_vals:
        if v not in effective:
            effective.append(v)
    return effective


def resolve_effective_tau(task: Dict, cfg: Dict) -> float:
    if "tau_success" in cfg:
        try:
            return float(cfg["tau_success"])
        except Exception:
            pass
    return float(task.get("tau_success", 0.9))


def _inverse_action(action: str) -> str:
    mapping = {
        "PITCH_POS": "PITCH_NEG",
        "PITCH_NEG": "PITCH_POS",
        "YAW_POS": "YAW_NEG",
        "YAW_NEG": "YAW_POS",
        "ROLL_POS": "ROLL_NEG",
        "ROLL_NEG": "ROLL_POS",
    }
    return mapping.get(action, "")


def normalize_action_and_angle(action: str, angle: int, allowed_angles: List[int]) -> tuple[str, int]:
    a = str(action or "").strip().upper()
    if a.endswith("_SMALL"):
        a = a.replace("_SMALL", "")
        angle = 10
    if a not in ALL_ACTIONS:
        return "", 0
    try:
        ang = int(angle)
    except Exception:
        ang = allowed_angles[0]
    if ang not in allowed_angles:
        return "", 0
    return a, ang


def apply_action_with_angle(current: Pose, action: str, angle: int) -> Pose:
    return apply_action(current, action, int(angle))


def choose_angle_simulated(delta_abs: float, allowed_angles: List[int]) -> int:
    if 30 in allowed_angles and delta_abs >= 20:
        return 30
    if 10 in allowed_angles and delta_abs >= 8:
        return 10
    if 5 in allowed_angles:
        return 5
    if 1 in allowed_angles:
        return 1
    return allowed_angles[0]


def choose_action_simulated(
    rng: random.Random,
    current: Pose,
    target: Pose,
    stream_type: str,
    recent_actions: List[str],
    allowed_actions: List[str],
    allowed_angles: List[int],
) -> tuple[str, int]:
    follow_prob = 0.70
    oscillation_prob = 0.14
    if stream_type not in ("single", ""):
        follow_prob = 0.62
        oscillation_prob = 0.18

    if recent_actions:
        last = recent_actions[-1]
        opposite = _inverse_action(last)
        if opposite and rng.random() < oscillation_prob:
            return opposite, choose_angle_simulated(999.0, allowed_angles)

    deltas = {
        "roll": ((target.roll - current.roll + 180) % 360) - 180,
        "pitch": ((target.pitch - current.pitch + 180) % 360) - 180,
        "yaw": ((target.yaw - current.yaw + 180) % 360) - 180,
    }
    best_axis = max(deltas, key=lambda k: abs(deltas[k]))
    if abs(deltas[best_axis]) <= 0:
        return "STOP", allowed_angles[0]

    axis_to_actions = {
        "roll": ("ROLL_POS", "ROLL_NEG"),
        "pitch": ("PITCH_POS", "PITCH_NEG"),
        "yaw": ("YAW_POS", "YAW_NEG"),
    }
    pos_action, neg_action = axis_to_actions[best_axis]
    preferred_main = pos_action if deltas[best_axis] > 0 else neg_action
    if preferred_main not in allowed_actions:
        non_stop_actions = [a for a in allowed_actions if a != "STOP"]
        if not non_stop_actions:
            return "STOP", allowed_angles[0]
        fallback = rng.choice(non_stop_actions)
        return fallback, choose_angle_simulated(abs(deltas[best_axis]), allowed_angles)
    preferred = preferred_main
    preferred_angle = choose_angle_simulated(abs(deltas[best_axis]), allowed_angles)

    if rng.random() < follow_prob:
        return preferred, preferred_angle
    non_stop_actions = [a for a in allowed_actions if a != "STOP"]
    if not non_stop_actions:
        return "STOP", allowed_angles[0]
    return rng.choice(non_stop_actions), preferred_angle


def parse_action_token(text: str) -> str:
    if not text:
        return ""
    legacy_small = [
        "PITCH_POS_SMALL",
        "PITCH_NEG_SMALL",
        "YAW_POS_SMALL",
        "YAW_NEG_SMALL",
        "ROLL_POS_SMALL",
        "ROLL_NEG_SMALL",
    ]
    pattern = r"\b(" + "|".join(re.escape(t) for t in (ALL_ACTIONS + legacy_small)) + r")\b"
    tokens = re.findall(pattern, text.upper())
    return tokens[-1] if tokens else ""


def _extract_action_and_cot_from_obj(obj: Dict, allowed_actions: List[str], allowed_angles: List[int]) -> tuple[str, str, int]:
    token = ""
    cot = ""
    angle = allowed_angles[0]
    for k in ("type", "action", "token"):
        if k in obj:
            token = parse_action_token(str(obj.get(k, "")))
            if token:
                break
    if "angle" in obj:
        try:
            angle = int(obj.get("angle"))
        except Exception:
            angle = allowed_angles[0]
    token, angle = normalize_action_and_angle(token, angle, allowed_angles)
    if token and token not in allowed_actions:
        token = ""
    for k in ("cot", "reasoning", "analysis", "thought", "why"):
        if k in obj:
            cot = str(obj.get(k, "")).strip()
            if cot:
                break
    return token, cot, angle


def parse_action_response(text: str, allowed_actions: List[str], allowed_angles: List[int]) -> tuple[str, str, int]:
    """Parse action token/cot/angle from JSON first, then fallback to token regex."""
    if not text:
        return "", "", allowed_angles[0]

    content = text.strip()
    try:
        obj = json.loads(content)
        if isinstance(obj, dict):
            token, cot, angle = _extract_action_and_cot_from_obj(obj, allowed_actions, allowed_angles)
            if token:
                if token in allowed_actions:
                    return token, cot, angle
    except Exception:
        pass

    m = re.search(r"```(?:json)?\s*([\s\S]*?)\s*```", content, flags=re.IGNORECASE)
    if m:
        try:
            obj = json.loads(m.group(1).strip())
            if isinstance(obj, dict):
                token, cot, angle = _extract_action_and_cot_from_obj(obj, allowed_actions, allowed_angles)
                if token:
                    if token in allowed_actions:
                        return token, cot, angle
        except Exception:
            pass

    token = parse_action_token(content)
    if token.endswith("_SMALL"):
        token = token.replace("_SMALL", "")
        angle = 10
    else:
        angle = allowed_angles[0]
    token, angle = normalize_action_and_angle(token, angle, allowed_angles)
    if token in allowed_actions:
        return token, "", angle
    return "", "", allowed_angles[0]


def image_to_data_url(path: Path) -> str:
    data = path.read_bytes()
    b64 = base64.b64encode(data).decode("ascii")
    return f"data:image/png;base64,{b64}"


def image_array_to_data_url(mask) -> str:
    if np is not None and cv2 is not None and isinstance(mask, np.ndarray):
        arr = mask
        if arr.dtype != np.uint8:
            arr = np.clip(arr, 0, 255).astype(np.uint8)
        ok, buf = cv2.imencode(".png", arr)
        if ok:
            return "data:image/png;base64," + base64.b64encode(buf.tobytes()).decode("ascii")
    h = len(mask)
    w = len(mask[0]) if h else 0
    raw = bytearray()
    for y in range(h):
        raw.append(0)
        row = mask[y]
        for x in range(w):
            v = row[x]
            iv = int(v)
            if iv in (0, 1):
                iv *= 255
            raw.append(max(0, min(255, iv)))
    compressed = zlib.compress(bytes(raw))

    def chunk(tag: bytes, data: bytes) -> bytes:
        return (
            len(data).to_bytes(4, "big")
            + tag
            + data
            + (zlib.crc32(tag + data) & 0xFFFFFFFF).to_bytes(4, "big")
        )

    png = bytearray(b"\x89PNG\r\n\x1a\n")
    ihdr = (
        w.to_bytes(4, "big")
        + h.to_bytes(4, "big")
        + b"\x08"
        + b"\x00"
        + b"\x00"
        + b"\x00"
        + b"\x00"
    )
    png.extend(chunk(b"IHDR", ihdr))
    png.extend(chunk(b"IDAT", compressed))
    png.extend(chunk(b"IEND", b""))
    return "data:image/png;base64," + base64.b64encode(bytes(png)).decode("ascii")


def save_data_url_image(path: Path, data_url: str) -> bool:
    try:
        prefix, b64 = str(data_url).split(",", 1)
    except ValueError:
        return False
    if ";base64" not in prefix:
        return False
    try:
        raw = base64.b64decode(b64)
    except Exception:
        return False
    path.parent.mkdir(parents=True, exist_ok=True)
    path.write_bytes(raw)
    return True


def save_llm_debug_bundle(
    debug_dir: Path,
    *,
    payload: Dict[str, Any],
    messages: List[Dict[str, Any]],
    raw_response_text: str = "",
    raw_response_json: Optional[Dict[str, Any]] = None,
    error_text: str = "",
) -> None:
    debug_dir.mkdir(parents=True, exist_ok=True)

    sanitized_messages: List[Dict[str, Any]] = []
    image_counter = 0
    for msg_idx, message in enumerate(messages, start=1):
        role = str(message.get("role", "unknown"))
        content = message.get("content", "")
        if isinstance(content, str):
            text_path = debug_dir / f"message_{msg_idx:02d}_{role}.txt"
            text_path.write_text(content, encoding="utf-8")
            sanitized_messages.append(
                {
                    "role": role,
                    "content_type": "text",
                    "text_file": text_path.name,
                }
            )
            continue

        if not isinstance(content, list):
            fallback_path = debug_dir / f"message_{msg_idx:02d}_{role}.txt"
            fallback_path.write_text(str(content), encoding="utf-8")
            sanitized_messages.append(
                {
                    "role": role,
                    "content_type": "unknown",
                    "text_file": fallback_path.name,
                }
            )
            continue

        sanitized_items: List[Dict[str, Any]] = []
        for item_idx, item in enumerate(content, start=1):
            if not isinstance(item, dict):
                item_path = debug_dir / f"message_{msg_idx:02d}_{role}_item_{item_idx:02d}.txt"
                item_path.write_text(str(item), encoding="utf-8")
                sanitized_items.append({"type": "unknown", "text_file": item_path.name})
                continue
            item_type = str(item.get("type", ""))
            if item_type == "text":
                item_path = debug_dir / f"message_{msg_idx:02d}_{role}_item_{item_idx:02d}.txt"
                item_path.write_text(str(item.get("text", "")), encoding="utf-8")
                sanitized_items.append({"type": "text", "text_file": item_path.name})
            elif item_type == "image_url":
                image_counter += 1
                image_name = f"image_{image_counter:02d}.png"
                image_path = debug_dir / image_name
                image_url = item.get("image_url", {})
                image_saved = False
                if isinstance(image_url, dict):
                    image_saved = save_data_url_image(image_path, str(image_url.get("url", "")))
                sanitized_items.append(
                    {
                        "type": "image_url",
                        "image_file": image_name if image_saved else "",
                        "image_saved": bool(image_saved),
                    }
                )
            else:
                item_path = debug_dir / f"message_{msg_idx:02d}_{role}_item_{item_idx:02d}.json"
                item_path.write_text(json.dumps(item, ensure_ascii=False, indent=2), encoding="utf-8")
                sanitized_items.append({"type": item_type or "unknown", "raw_file": item_path.name})
        sanitized_messages.append({"role": role, "content_type": "list", "items": sanitized_items})

    request_summary = {
        "model": payload.get("model", ""),
        "temperature": payload.get("temperature", 0.0),
        "seed": payload.get("seed", None),
        "max_tokens": payload.get("max_tokens", 0),
        "response_format": payload.get("response_format", {}),
        "messages": sanitized_messages,
    }
    (debug_dir / "request_summary.json").write_text(
        json.dumps(request_summary, ensure_ascii=False, indent=2),
        encoding="utf-8",
    )
    if raw_response_text:
        (debug_dir / "response.txt").write_text(str(raw_response_text), encoding="utf-8")
    if raw_response_json is not None:
        (debug_dir / "response.json").write_text(
            json.dumps(raw_response_json, ensure_ascii=False, indent=2),
            encoding="utf-8",
        )
    if error_text:
        (debug_dir / "error.txt").write_text(str(error_text), encoding="utf-8")


def _to_uint8_image(mask):
    if np is not None and isinstance(mask, np.ndarray):
        arr = mask
        if arr.dtype != np.uint8:
            arr = np.clip(arr, 0, 255).astype(np.uint8)
        return arr
    if np is None:
        return None
    h = len(mask)
    w = len(mask[0]) if h else 0
    arr = np.zeros((h, w), dtype=np.uint8)
    for y in range(h):
        row = mask[y]
        for x in range(w):
            iv = int(row[x])
            if iv in (0, 1):
                iv *= 255
            arr[y, x] = max(0, min(255, iv))
    return arr


def compose_triptych_data_url(target_img, oblique_img, proj_img) -> str:
    if np is None or cv2 is None:
        raise RuntimeError("compose_triptych_data_url requires numpy and opencv.")
    panel = compose_triptych_image(target_img, oblique_img, proj_img)
    ok, buf = cv2.imencode(".png", panel)
    if not ok:
        raise RuntimeError("Failed to encode panel PNG.")
    return "data:image/png;base64," + base64.b64encode(buf.tobytes()).decode("ascii")


def compose_triptych_image(target_img, oblique_img, proj_img):
    if np is None or cv2 is None:
        raise RuntimeError("compose_triptych_image requires numpy and opencv.")
    t = _to_uint8_image(target_img)
    o = _to_uint8_image(oblique_img)
    p = _to_uint8_image(proj_img)
    if t is None or o is None or p is None:
        raise RuntimeError("Failed to prepare panel images.")
    if t.ndim == 2:
        t = cv2.cvtColor(t, cv2.COLOR_GRAY2BGR)
    if o.ndim == 2:
        o = cv2.cvtColor(o, cv2.COLOR_GRAY2BGR)
    if p.ndim == 2:
        p = cv2.cvtColor(p, cv2.COLOR_GRAY2BGR)
    h = max(t.shape[0], o.shape[0], p.shape[0])
    w = max(t.shape[1], o.shape[1], p.shape[1])
    def fit(img):
        if img.shape[0] != h or img.shape[1] != w:
            return cv2.resize(img, (w, h), interpolation=cv2.INTER_NEAREST)
        return img
    t, o, p = fit(t), fit(o), fit(p)
    sep = np.full(
        (h, STANDARD_PANEL_SEPARATOR, 3),
        STANDARD_PANEL_SEPARATOR_VALUE,
        dtype=np.uint8,
    )
    return np.concatenate([t, sep, o, sep, p], axis=1)


def save_triptych_panel(
    path: Path,
    panel_img,
) -> None:
    if np is None or cv2 is None:
        return
    ensure_dir = path.parent.mkdir
    ensure_dir(parents=True, exist_ok=True)
    cv2.imwrite(str(path), panel_img)


def is_zero_temperature_unsupported(error_text: str) -> bool:
    text = str(error_text or "").lower()
    if "temperature" not in text:
        return False
    unsupported_markers = (
        "unsupported",
        "not support",
        "does not support",
        "must be greater than 0",
        "minimum is",
        "minimum value",
        "invalid value",
        "only the default",
    )
    return any(marker in text for marker in unsupported_markers)


def call_openai_compatible_chat(
    *,
    base_url: str,
    api_key: str,
    model: str,
    image_mode: str,
    target_image_data_url: str,
    current_oblique_data_url: str,
    current_image_data_url: str,
    panel_image_data_url: str,
    history_actions: List[str],
    recent_panel_data_urls: List[str],
    recent_state_summaries: List[str],
    history_window: int,
    step: int,
    step_limit: int,
    current_iou: float,
    tau_success: float,
    last_action: str,
    allowed_actions: List[str],
    allowed_angles: List[int],
    timeout_s: int,
    prompt_profile: str = "baseline",
    prompt_protocol: str = "v2",
    metric_feedback_mode: str = "full",
    api_temperature: float = 0.0,
    api_seed: int = 42,
    api_max_tokens: int = 8192,
    api_response_format: str = "json",
    api_orchestration: str = "direct",
    agent_tool_mode: str = "none",
    agent_workspace=None,
    magentic_max_rounds: int = 4,
    magentic_max_resets: int = 1,
    stop_caution: bool = False,
    hide_iou_feedback: bool = False,
    legacy_stop_as_success: bool = False,
    debug_dir: Optional[Path] = None,
    ledger_path: str = "",
) -> Dict[str, str]:
    metric_feedback_mode = resolve_metric_feedback_mode(metric_feedback_mode, hide_iou_feedback)
    if api_orchestration in {"magentic", "react"} and metric_feedback_mode != "none":
        raise ValueError("Agent orchestrations are black-box protocols and require metric_feedback_mode=none")
    if api_orchestration in {"magentic", "react"} and agent_tool_mode == "python":
        if agent_workspace is None:
            raise ValueError("Agent Python mode requires a task-local workspace")
        if image_mode != "panel" or not panel_image_data_url:
            raise ValueError("Agent Python mode requires the single 320x320 panel observation")
    metric_guidance = build_metric_guidance(metric_feedback_mode, current_iou, tau_success)
    effective_mode = image_mode
    history_panels = select_history_slice(recent_panel_data_urls, int(history_window))
    if effective_mode == "panel" and not panel_image_data_url:
        effective_mode = "triple"
    token_set_text = ", ".join(allowed_actions)
    angle_set_text = ", ".join(str(x) for x in allowed_angles)
    if prompt_protocol == "v1":
        prompt_text = build_prompt_text_v1(
            effective_mode=effective_mode,
            prompt_profile=prompt_profile,
            token_set_text=token_set_text,
            angle_set_text=angle_set_text,
            step=step,
            step_limit=step_limit,
            current_iou=current_iou,
            tau_success=tau_success,
            last_action=last_action,
            history_actions=history_actions,
            history_window=history_window,
            recent_state_summaries=recent_state_summaries,
            stop_caution=stop_caution,
            hide_iou_feedback=hide_iou_feedback,
            legacy_stop_as_success=legacy_stop_as_success,
        )
        system_prompt = build_system_prompt_v1(
            token_set_text,
            angle_set_text,
            history_window,
            legacy_stop_as_success,
        )
    else:
        prompt_text = build_prompt_text_v2(
            step=step,
            step_limit=step_limit,
            current_iou=current_iou,
            tau_success=tau_success,
            metric_guidance=metric_guidance,
            hide_iou_feedback=hide_iou_feedback,
        )
        system_prompt = build_system_prompt_v2(
            token_set_text,
            angle_set_text,
            hide_iou_feedback=hide_iou_feedback,
            legacy_stop_as_success=legacy_stop_as_success,
        )

    messages = [
        {
            "role": "system",
            "content": system_prompt,
        }
    ]
    user_content = [{"type": "text", "text": prompt_text}]
    if history_panels and prompt_protocol == "v1":
        user_content.append(
            {
                "type": "text",
                "text": "Recent panels are previous states in chronological order from oldest to newest before the current state. Each label such as 'N steps ago' is measured relative to the current state. Read them as a real timeline: state, then the recorded action, then the next state. The final image block below is the current state to act on now.",
            }
        )
        for idx, url in enumerate(history_panels, start=1):
            steps_ago = len(history_panels) - idx + 1
            user_content.append({"type": "text", "text": f"{steps_ago} steps ago (older -> newer history state before the recorded action):"})
            user_content.append({"type": "image_url", "image_url": {"url": url}})
    elif history_panels:
        history_summaries = select_history_slice(recent_state_summaries, int(history_window))
        summary_offset = len(history_summaries) - len(history_panels)
        for idx, url in enumerate(history_panels):
            summary_idx = idx + summary_offset
            action_summary = history_summaries[summary_idx] if summary_idx >= 0 else "action=UNKNOWN angle=UNKNOWN"
            user_content.append(
                {
                    "type": "text",
                    "text": f"Historical state {idx + 1}; action after this state: {action_summary}",
                }
            )
            user_content.append({"type": "image_url", "image_url": {"url": url}})
    user_content.append({"type": "text", "text": "Current state to act on now:"})
    if effective_mode == "panel":
        user_content.append({"type": "image_url", "image_url": {"url": panel_image_data_url}})
    else:
        user_content.append({"type": "image_url", "image_url": {"url": target_image_data_url}})
        user_content.append({"type": "image_url", "image_url": {"url": current_oblique_data_url}})
        user_content.append({"type": "image_url", "image_url": {"url": current_image_data_url}})
    messages.append({"role": "user", "content": user_content})
    payload = {
        "model": model,
        "temperature": float(api_temperature),
        "messages": messages,
    }
    if api_seed > 0:
        payload["seed"] = int(api_seed)
    if str(api_response_format or "json").lower() != "none":
        payload["response_format"] = {"type": "json_object"}
    if api_max_tokens > 0:
        payload["max_tokens"] = int(api_max_tokens)
    temperature_fallback_used = False
    response_format_fallback_used = False
    agent_mode = api_orchestration in {"magentic", "react"}
    for _ in range(3):
        try:
            if api_orchestration in {"magentic", "react"}:
                from agent_framework_magentic import run_magentic_chat, run_react_chat

                run_agent_chat = run_magentic_chat if api_orchestration == "magentic" else run_react_chat
                agent_kwargs = {
                    "messages": messages,
                    "model": model,
                    "api_key": api_key,
                    "base_url": base_url,
                    "temperature": float(payload.get("temperature", 0.0)),
                    "max_tokens": int(payload.get("max_tokens", 0) or 0),
                    "seed": int(payload.get("seed", 0) or 0),
                    "response_format": "json" if "response_format" in payload else "none",
                    "timeout_s": int(timeout_s),
                    "python_tool": agent_workspace.execute_python if agent_tool_mode == "python" else None,
                    "image_tool": agent_workspace.view_image if agent_tool_mode == "python" else None,
                    "workspace": agent_workspace if agent_tool_mode == "python" else None,
                    "ledger_path": ledger_path,
                }
                if api_orchestration == "magentic":
                    agent_kwargs["max_rounds"] = int(magentic_max_rounds)
                    agent_kwargs["max_resets"] = int(magentic_max_resets)
                agent_result = run_agent_chat(
                    **agent_kwargs,
                )
                data = {
                    "choices": [{"message": {"content": agent_result["raw_text"]}}],
                    "usage": agent_result.get("usage", {}),
                    "orchestration_trace": agent_result.get("trace", []),
                    "input_transport": agent_result.get("input_transport", ""),
                    "tools_registered": bool(agent_result.get("tools_registered", False)),
                    "tool_protocol": str(agent_result.get("tool_protocol", "")),
                }
            else:
                data = chat_completions(payload, timeout=timeout_s, api_key=api_key, base_url=base_url)
            break
        except Exception as e:
            error_text = str(e)
            error_lower = error_text.lower()
            if agent_mode and api_orchestration == "magentic" and _ < 2:
                messages.append({
                    "role": "user",
                    "content": (
                        "The previous orchestration attempt failed before producing a valid action. "
                        "Recover the task context and return exactly one valid action JSON object. "
                        "Do not repeat the failed tool call. Framework diagnostic: "
                        + error_text[:2000]
                    ),
                })
                payload["messages"] = messages
                continue
            if (
                not temperature_fallback_used
                and not agent_mode
                and float(payload.get("temperature", 0.0)) == 0.0
                and is_zero_temperature_unsupported(error_text)
            ):
                payload["temperature"] = 1e-7
                temperature_fallback_used = True
                continue
            if (
                not response_format_fallback_used
                and "response_format" in payload
                and ("response_format" in error_lower or "json_object" in error_lower)
            ):
                payload.pop("response_format", None)
                response_format_fallback_used = True
                continue
            if debug_dir is not None:
                save_llm_debug_bundle(
                    debug_dir,
                    payload=payload,
                    messages=messages,
                    error_text=error_text,
                )
            raise
    choices = data.get("choices", [])
    if not choices:
        if debug_dir is not None:
            save_llm_debug_bundle(
                debug_dir,
                payload=payload,
                messages=messages,
                raw_response_json=data if isinstance(data, dict) else None,
                error_text=f"No choices in response: {json.dumps(data, ensure_ascii=False)[:300]}",
            )
        raise RuntimeError(f"No choices in response: {json.dumps(data, ensure_ascii=False)[:300]}")

    message = choices[0].get("message", {})
    content = message.get("content", "")
    if isinstance(content, list):
        text_parts = []
        for c in content:
            if isinstance(c, dict) and c.get("type") == "text":
                text_parts.append(c.get("text", ""))
        content = "\n".join(text_parts)

    content_text = str(content).strip()
    if not content_text:
        reasoning_content = str(message.get("reasoning_content", "") or "").strip()
        if reasoning_content:
            content_text = reasoning_content
    if debug_dir is not None:
        save_llm_debug_bundle(
            debug_dir,
            payload=payload,
            messages=messages,
            raw_response_text=content_text,
            raw_response_json=data if isinstance(data, dict) else None,
        )
    if not content_text:
        error_text = "Empty model response content"
        if debug_dir is not None:
            save_llm_debug_bundle(
                debug_dir,
                payload=payload,
                messages=messages,
                raw_response_text=content_text,
                raw_response_json=data if isinstance(data, dict) else None,
                error_text=error_text,
            )
        raise RuntimeError(error_text)
    token, cot, angle = parse_action_response(content_text, allowed_actions, allowed_angles)
    if not token and api_orchestration == "magentic":
        previous_usage = data.get("usage", {}) if isinstance(data, dict) else {}
        previous_trace = data.get("orchestration_trace", []) if isinstance(data, dict) else []
        messages.append({
            "role": "user",
            "content": (
                "Your previous response was not a valid environment action. Return exactly one JSON "
                "object with cot, type, and angle. Do not include Markdown, tool-call JSON, or any "
                "additional object. Invalid response: " + content_text[:2000]
            ),
        })
        agent_kwargs["messages"] = messages
        corrected = run_agent_chat(**agent_kwargs)
        corrected_usage = corrected.get("usage", {})
        usage = {}
        for key in ("prompt_tokens", "completion_tokens", "total_tokens"):
            usage[key] = int(previous_usage.get(key, 0) or 0) + int(corrected_usage.get(key, 0) or 0)
        content_text = str(corrected.get("raw_text", "") or "").strip()
        data = {
            "choices": [{"message": {"content": content_text}}],
            "usage": usage,
            "orchestration_trace": previous_trace + corrected.get("trace", []),
            "input_transport": corrected.get("input_transport", ""),
            "tools_registered": bool(corrected.get("tools_registered", False)),
            "tool_protocol": str(corrected.get("tool_protocol", "")),
        }
        if debug_dir is not None:
            save_llm_debug_bundle(
                debug_dir,
                payload=payload,
                messages=messages,
                raw_response_text=content_text,
                raw_response_json=data,
            )
        token, cot, angle = parse_action_response(content_text, allowed_actions, allowed_angles)
    if not token:
        error_text = f"Cannot parse action token from content: {content_text}"
        if debug_dir is not None:
            save_llm_debug_bundle(
                debug_dir,
                payload=payload,
                messages=messages,
                raw_response_text=content_text,
                raw_response_json=data if isinstance(data, dict) else None,
                error_text=error_text,
            )
        raise RuntimeError(f"Cannot parse action token from content: {content_text}")
    delivered_text = content_text
    if agent_mode:
        delivered_text = json.dumps(
            {"cot": cot, "type": token, "angle": int(angle)},
            ensure_ascii=False,
            separators=(",", ":"),
        )
    return {
        "action": token,
        "angle": int(angle),
        "raw_text": delivered_text,
        "cot": cot,
        "system_prompt": system_prompt,
        "prompt_text": prompt_text,
        "usage": data.get("usage", {}) if isinstance(data, dict) else {},
        "tools_registered": bool(data.get("tools_registered", False)) if isinstance(data, dict) else False,
        "tool_protocol": str(data.get("tool_protocol", "")) if isinstance(data, dict) else "",
    }


def parse_args() -> argparse.Namespace:
    parser = argparse.ArgumentParser(description="Step 02 test models")
    parser.add_argument("--root", type=str, default=".")
    parser.add_argument("--config", type=str, default="data/run_config.json")
    parser.add_argument("--formal-config", type=str, default="src/final_benchmark_config.json")
    parser.add_argument("--tasks-file", type=str, default="data/tasks_674.jsonl")
    parser.add_argument("--split-file", type=str, default="data/test_100.json", help="JSON split file; test.json and train.json are supported")
    parser.add_argument("--output-dir", type=str, default="", help="Default: result/<model name>/<metric feedback protocol>")
    parser.add_argument("--attempt-tag", type=str, default="", help="Optional tag for current run")
    parser.add_argument("--attempts", type=str, default="", help="Batch mode, e.g. 0,16,32,64,128,256")
    parser.add_argument("--ablation-root", type=str, default="", help="Root dir containing attempts_xx folders")
    parser.add_argument("--seed", type=int, default=0, help="Override random seed (0 means use config seed)")
    parser.add_argument("--model-name", type=str, default="", help="Display model name in report")
    parser.add_argument("--checkpoint-path", type=str, default="", help="Optional model checkpoint path or identifier for report metadata")

    parser.add_argument("--backend", type=str, default="simulated", choices=["simulated", "gemini_api"])
    parser.add_argument("--api-key", type=str, default=os.environ.get("OPENAI_API_KEY", ""))
    parser.add_argument("--base-url", type=str, default=BASE_URL)
    parser.add_argument("--api-model", type=str, default=DEFAULT_MODEL)
    parser.add_argument("--api-image-mode", type=str, default="panel", choices=["panel", "triple"], help="panel=single triptych image, triple=three separate images")
    parser.add_argument("--api-display-width", type=int, default=STANDARD_IMAGE_SIZE, help="Square API-visible render width")
    parser.add_argument("--api-display-height", type=int, default=STANDARD_IMAGE_SIZE, help="Square API-visible render height")
    parser.add_argument("--prompt-profile", type=str, default="baseline", choices=PROMPT_PROFILES, help="Prompt template profile for API-based evaluation")
    parser.add_argument("--prompt-protocol", type=str, default="v2", choices=["v1", "v2"], help="v1=archived verbose prompt, v2=compact formal prompt")
    parser.add_argument("--history-window", type=int, default=-1, help="How many previous states/panels to include as chronological history context; use -1 for full history")
    parser.add_argument("--include-history-cot", dest="include_history_cot", action="store_true", help="Include each prior model cot with its historical action (formal default)")
    parser.add_argument("--no-history-cot", dest="include_history_cot", action="store_false", help="Omit prior model cot for ablation runs")
    parser.add_argument("--api-temperature", type=float, default=0.0, help="Sampling temperature for API backend")
    parser.add_argument("--api-seed", type=int, default=42, help="Best-effort model sampling seed passed to the API; 0 omits it")
    parser.add_argument("--api-max-tokens", type=int, default=8192, help="Output/completion max_tokens passed to the API backend")
    parser.add_argument("--api-response-format", type=str, default="json", choices=["json", "none"], help="Use response_format=json_object or omit it for providers/models that do not support JSON mode")
    parser.add_argument("--api-orchestration", type=str, default="direct", choices=["direct", "magentic", "react"], help="API orchestration over the same benchmark observations")
    parser.add_argument("--agent-tool-mode", type=str, default="python", choices=["none", "python"], help="Task-local analysis tool for agent orchestrations; direct mode ignores it")
    parser.add_argument(
        "--magentic-max-rounds",
        type=int,
        default=4,
        help="Maximum Magentic coordination rounds per environment step",
    )
    parser.add_argument(
        "--magentic-max-resets",
        type=int,
        default=1,
        help="Maximum Magentic replanning resets per environment step",
    )
    parser.add_argument("--stop-caution", action="store_true", help="Add an explicit warning against STOP when IoU is far below threshold")
    parser.add_argument("--hide-iou-feedback", action="store_true", help="Compatibility alias for --metric-feedback-mode none")
    parser.add_argument("--metric-feedback-mode", type=str, default="none", choices=METRIC_FEEDBACK_MODES, help="Formal default is none; full and threshold are auxiliary protocols only")
    parser.add_argument("--legacy-stop-as-success", action="store_true", help="Use the legacy ambiguous STOP-as-visual-success prompt")
    parser.add_argument("--stagnation-patience", type=int, default=12, help="Stop after this many executed rotations without a strict new historical-best IoU; 0 disables")
    parser.add_argument("--min-best-iou-gain", type=float, default=0.0, help="Strict gain required to reset stagnation patience")
    parser.add_argument("--timeout", type=int, default=1500)
    parser.add_argument("--api-retries", type=int, default=2, help="Retry count on API/network failure")
    parser.add_argument("--api-retry-backoff", type=float, default=1.6, help="Exponential backoff base seconds")
    parser.add_argument("--max-tasks", type=int, default=0, help="0 means all tasks")
    parser.add_argument("--max-step-factor", type=float, default=5.0, help="Deprecated compatibility flag; no longer used for step budget")
    parser.add_argument("--step-limit-multiplier", type=float, default=3.0, help="Multiply task T_max to get step budget (formal benchmark: 3x = 48 steps)")
    parser.add_argument("--max-step-cap", type=int, default=0, help="Optional hard cap; 0 means no cap")
    parser.add_argument("--min-step-limit", type=int, default=1, help="Minimum max-steps budget per task")
    parser.add_argument("--allowed-angles", type=str, default="30,10,5,1", help="Allowed step angles, subset of 30,10,5,1")
    parser.add_argument("--save-frames", action="store_true", help="Save per-step PNG frames to disk")
    parser.add_argument("--save-llm-debug", action="store_true", help="Save full per-step LLM request/response debug bundle")
    parser.add_argument("--debug-timing", action="store_true", help="Print detailed per-stage and per-step timings")
    parser.add_argument("--task-workers", type=int, default=2, help="Number of independent task subprocesses")
    parser.add_argument("--render-workers", type=int, default=1, help="Maximum concurrent CUDA render calls across task subprocesses")
    parser.add_argument("--no-resume", dest="resume", action="store_false", help="Disable resume mode and rerun all tasks")
    parser.set_defaults(resume=True, include_history_cot=True)
    args = parser.parse_args()
    require_standard_square_size(
        (args.api_display_width, args.api_display_height),
        name="--api-display-size",
    )
    return args


def _without_cli_options(argv: List[str], options_with_values: set[str]) -> List[str]:
    cleaned: List[str] = []
    skip_value = False
    for item in argv:
        if skip_value:
            skip_value = False
            continue
        if item in options_with_values:
            skip_value = True
            continue
        if any(item.startswith(f"{option}=") for option in options_with_values):
            continue
        cleaned.append(item)
    return cleaned


def _run_parallel_tasks(args: argparse.Namespace, root: Path) -> None:
    if args.task_workers < 1 or args.render_workers < 1:
        raise ValueError("--task-workers and --render-workers must be positive integers")
    tasks_path = Path(args.tasks_file) if args.tasks_file else resolve_paths(root)["tasks"]
    if not tasks_path.is_absolute():
        tasks_path = root / tasks_path
    split_path = resolve_optional_path(root, args.split_file)
    tasks, _ = read_selected_tasks_and_ids(tasks_path, args.max_tasks, split_path)
    if not tasks:
        raise RuntimeError("No tasks selected")

    safe_model_name = re.sub(r"[^A-Za-z0-9._-]+", "_", args.api_model if args.backend == "gemini_api" else args.model_name).strip("._") or "simulated"
    default_output_root = root / "result" / safe_model_name
    if args.backend == "gemini_api":
        default_output_root /= metric_feedback_result_dir(
            resolve_metric_feedback_mode(args.metric_feedback_mode, args.hide_iou_feedback)
        )
    output_root = Path(args.output_dir) if args.output_dir else default_output_root
    if not output_root.is_absolute():
        output_root = root / output_root
    shard_root = output_root / "parallel_shards"
    shard_root.mkdir(parents=True, exist_ok=True)
    worker_count = min(args.task_workers, len(tasks))
    shards: List[List[Dict]] = [[] for _ in range(worker_count)]
    for index, task in enumerate(tasks):
        shards[index % worker_count].append(task)

    removable = {
        "--tasks-file", "--output-dir", "--max-tasks",
        "--task-workers", "--render-workers", "--split-file",
    }
    base_argv = _without_cli_options(sys.argv[1:], removable)
    semaphore_name = f"Local\\projection-render-{os.getpid()}"

    def run_shard(index: int, shard_tasks: List[Dict]) -> tuple[int, Path]:
        shard_dir = shard_root / f"worker_{index:02d}"
        shard_dir.mkdir(parents=True, exist_ok=True)
        shard_tasks_path = shard_dir / "tasks.jsonl"
        with shard_tasks_path.open("w", encoding="utf-8") as f:
            for task in shard_tasks:
                f.write(json.dumps(task, ensure_ascii=False) + "\n")
        cmd = [
            sys.executable, str(Path(__file__).resolve()), *base_argv,
            "--tasks-file", str(shard_tasks_path),
            "--split-file", "",
            "--output-dir", str(shard_dir),
            "--max-tasks", "0",
            "--task-workers", "1",
            "--render-workers", "1",
        ]
        if not args.resume:
            cmd.append("--no-resume")
        env = os.environ.copy()
        env["PROJECTION_PREFLIGHT_DONE"] = "1"
        env[RENDER_SEMAPHORE_ENV] = semaphore_name
        env[RENDER_SEMAPHORE_COUNT_ENV] = str(args.render_workers)
        stdout_path = shard_dir / "run.stdout.log"
        stderr_path = shard_dir / "run.stderr.log"
        with stdout_path.open("w", encoding="utf-8") as stdout_f, stderr_path.open("w", encoding="utf-8") as stderr_f:
            completed = subprocess.run(cmd, cwd=str(root), env=env, stdout=stdout_f, stderr=stderr_f)
        return completed.returncode, shard_dir

    with ThreadPoolExecutor(max_workers=worker_count) as executor:
        results = list(executor.map(lambda pair: run_shard(*pair), enumerate(shards)))
    failures = [(code, path) for code, path in results if code != 0]
    if failures:
        details = ", ".join(f"{path.name}: exit {code}" for code, path in failures)
        raise RuntimeError(f"Parallel task worker failed: {details}")

    rows: List[Dict] = []
    for _, shard_dir in results:
        csv_path = shard_dir / "reports" / "model_raw.csv"
        with csv_path.open("r", encoding="utf-8", newline="") as f:
            rows.extend(csv.DictReader(f))
    order = {str(task["sample_id"]): index for index, task in enumerate(tasks)}
    rows.sort(key=lambda row: order.get(str(row.get("sample_id", "")), len(order)))
    reports_dir = output_root / "reports"
    logs_dir = output_root / "logs"
    reports_dir.mkdir(parents=True, exist_ok=True)
    logs_dir.mkdir(parents=True, exist_ok=True)
    write_csv_rows(reports_dir / "model_raw.csv", rows)

    prompt_tokens = sum(int(row.get("prompt_tokens", 0) or 0) for row in rows)
    completion_tokens = sum(int(row.get("completion_tokens", 0) or 0) for row in rows)
    summary = {
        "model_name": args.model_name.strip() or args.api_model,
        "model_version": args.api_model,
        "backend": args.backend,
        "samples": len(rows),
        "success_rate": round(sum(int(row.get("success", 0) or 0) for row in rows) / len(rows), 6),
        "prompt_tokens": prompt_tokens,
        "completion_tokens": completion_tokens,
        "total_tokens": prompt_tokens + completion_tokens,
        "output": _rel_path(reports_dir / "model_raw.csv", root),
        "prompt_protocol": args.prompt_protocol,
        "metric_feedback_mode": resolve_metric_feedback_mode(args.metric_feedback_mode, args.hide_iou_feedback),
        "hide_iou_feedback": bool(args.hide_iou_feedback),
        "history_window": int(args.history_window),
        "api_temperature": float(args.api_temperature),
        "api_seed": int(args.api_seed),
        "api_max_tokens": int(args.api_max_tokens),
        "api_stream": False,
        "task_workers": int(args.task_workers),
        "render_workers": int(args.render_workers),
        "render_environment": render_environment_metadata(),
        "worker_outputs": [_rel_path(path, root) for _, path in results],
    }
    write_json(reports_dir / "step_02_summary.json", summary)
    print(json.dumps(summary, ensure_ascii=False, indent=2))


def _rel_path(path: Path, root: Path) -> str:
    try:
        return str(path.relative_to(root)).replace("\\", "/")
    except Exception:
        return str(path).replace("\\", "/")


def compute_api_retry_sleep(
    error_text: str,
    attempt_idx: int,
    backoff: float,
    *,
    rng: Optional[random.Random] = None,
) -> tuple[float, str]:
    """Use slower retries for gateway failures that may already be billable."""
    text = error_text.lower()
    jitter_rng = rng if rng is not None else random
    jitter = 0.8 + jitter_rng.random() * 0.4
    base_sleep = max(0.1, backoff) * (2 ** attempt_idx) * jitter
    if "429" in text or "上游负载已饱和" in error_text or "rate limit" in text:
        return max(65.0, min(300.0, base_sleep * 8.0)), "gateway_busy"
    if "remote end closed" in text or "without response" in text or "connection reset" in text:
        return max(65.0, min(180.0, base_sleep * 6.0)), "connection_closed"
    return min(120.0, base_sleep), "api_error"


def encode_rng_state(state) -> str:
    return base64.b64encode(zlib.compress(pickle.dumps(state))).decode("ascii")


def decode_rng_state(text: str):
    return pickle.loads(zlib.decompress(base64.b64decode(text.encode("ascii"))))


def load_checkpoint(path: Path) -> Dict:
    if not path.exists():
        return {}
    return read_json(path)


def write_csv_rows(path: Path, rows: List[Dict]) -> None:
    path.parent.mkdir(parents=True, exist_ok=True)
    with path.open("w", encoding="utf-8", newline="") as f:
        if not rows:
            return
        fieldnames: List[str] = []
        seen = set()
        for row in rows:
            for key in row.keys():
                if key not in seen:
                    seen.add(key)
                    fieldnames.append(key)
        writer = csv.DictWriter(f, fieldnames=fieldnames)
        writer.writeheader()
        writer.writerows(rows)


def step2_config_fingerprint(
    cfg: Dict,
    args: argparse.Namespace,
    root: Path,
    tasks_path: Path,
    model_name: str,
    all_task_ids: List[str],
    global_allowed_angles: List[int],
) -> str:
    payload = {
        "backend": args.backend,
        "model_name": model_name,
        "checkpoint_path": str(args.checkpoint_path),
        "api_model": args.api_model if args.backend == "gemini_api" else "",
        "base_url": args.base_url if args.backend == "gemini_api" else "",
        "api_image_mode": args.api_image_mode if args.backend == "gemini_api" else "",
        "api_display_width": int(args.api_display_width) if args.backend == "gemini_api" else 0,
        "api_display_height": int(args.api_display_height) if args.backend == "gemini_api" else 0,
        "prompt_profile": args.prompt_profile if args.backend == "gemini_api" else "",
        "prompt_protocol": args.prompt_protocol if args.backend == "gemini_api" else "",
        "metric_feedback_mode": resolve_metric_feedback_mode(args.metric_feedback_mode, args.hide_iou_feedback) if args.backend == "gemini_api" else "",
        "history_window": int(args.history_window) if args.backend == "gemini_api" else 0,
        "include_history_cot": bool(args.include_history_cot) if args.backend == "gemini_api" else False,
        "api_temperature": float(args.api_temperature) if args.backend == "gemini_api" else 0.0,
        "api_seed": int(args.api_seed) if args.backend == "gemini_api" else 0,
        "api_max_tokens": int(args.api_max_tokens) if args.backend == "gemini_api" else 0,
        "api_response_format": str(args.api_response_format) if args.backend == "gemini_api" else "",
        "api_orchestration": str(args.api_orchestration) if args.backend == "gemini_api" else "",
        "magentic_max_rounds": int(args.magentic_max_rounds) if args.backend == "gemini_api" else 0,
        "magentic_max_resets": int(args.magentic_max_resets) if args.backend == "gemini_api" else 0,
        "stop_caution": bool(args.stop_caution) if args.backend == "gemini_api" else False,
        "hide_iou_feedback": bool(args.hide_iou_feedback) if args.backend == "gemini_api" else False,
        "stop_protocol": "legacy_stop_as_visual_success" if args.legacy_stop_as_success else "auto_success_stop_no_further_idea_v1",
        "stagnation_patience": int(args.stagnation_patience),
        "min_best_iou_gain": float(args.min_best_iou_gain),
        "attempt_tag": args.attempt_tag,
        "seed": int(args.seed) if args.seed else int(cfg["random_seed"]),
        "tasks_file": _rel_path(tasks_path, root),
        "all_task_ids": list(all_task_ids),
        "allowed_angles": list(global_allowed_angles),
        "step_limit_multiplier": float(args.step_limit_multiplier),
        "max_step_cap": int(args.max_step_cap),
        "min_step_limit": int(args.min_step_limit),
        "api_display_width": int(args.api_display_width) if args.backend == "gemini_api" else 0,
        "api_display_height": int(args.api_display_height) if args.backend == "gemini_api" else 0,
        "save_frames": bool(args.save_frames),
        "save_llm_debug": bool(args.save_llm_debug) if args.backend == "gemini_api" else False,
        "tau_success": cfg.get("tau_success", None),
    }
    text = json.dumps(payload, ensure_ascii=False, sort_keys=True, separators=(",", ":"))
    return hashlib.sha256(text.encode("utf-8")).hexdigest()


def _parse_attempts(text: str) -> List[int]:
    vals: List[int] = []
    for x in text.split(","):
        s = x.strip()
        if not s:
            continue
        vals.append(int(s))
    return vals


def _find_latest_ablation_root(root: Path) -> Optional[Path]:
    base = root / "artifacts" / "ablation"
    if not base.exists():
        return None
    dirs = [p for p in base.glob("holes_*") if p.is_dir()]
    if not dirs:
        return None
    dirs.sort(key=lambda p: p.stat().st_mtime, reverse=True)
    return dirs[0]


def _run_attempt_batch(args: argparse.Namespace, project_root: Path) -> None:
    attempts = _parse_attempts(args.attempts)
    if not attempts:
        raise RuntimeError("attempts is empty")
    ablation_root = Path(args.ablation_root).resolve() if args.ablation_root else _find_latest_ablation_root(project_root)
    if ablation_root is None or not ablation_root.exists():
        raise RuntimeError("Cannot find ablation root. Please provide --ablation-root.")

    run_summaries: List[Dict] = []
    script_path = (project_root / "step_02_test_models.py").resolve()
    base_cfg_abs = (project_root / args.config).resolve()

    for n in attempts:
        attempt_root = ablation_root / f"attempts_{n}"
        tasks_file = attempt_root / "tasks" / "tasks.jsonl"
        if not tasks_file.exists():
            print(f"[step02][attempts] skip attempts_{n}: tasks file not found: {tasks_file}")
            continue
        out_dir = attempt_root / "step2" / args.backend
        out_dir.mkdir(parents=True, exist_ok=True)

        cmd = [
            sys.executable,
            str(script_path),
            "--root",
            str(attempt_root),
            "--config",
            str(base_cfg_abs),
            "--tasks-file",
            str(tasks_file),
            "--output-dir",
            str(out_dir),
            "--attempt-tag",
            f"attempts_{n}",
            "--backend",
            args.backend,
            "--seed",
            str(args.seed),
            "--model-name",
            args.model_name,
            "--checkpoint-path",
            args.checkpoint_path,
            "--base-url",
            args.base_url,
            "--api-model",
            args.api_model,
            "--api-image-mode",
            args.api_image_mode,
            "--api-display-width",
            str(args.api_display_width),
            "--api-display-height",
            str(args.api_display_height),
            "--prompt-profile",
            args.prompt_profile,
            "--prompt-protocol",
            args.prompt_protocol,
            "--history-window",
            str(args.history_window),
            "--api-temperature",
            str(args.api_temperature),
            "--api-seed",
            str(args.api_seed),
            "--api-max-tokens",
            str(args.api_max_tokens),
            "--api-response-format",
            str(args.api_response_format),
            "--api-orchestration",
            str(args.api_orchestration),
            "--agent-tool-mode",
            str(args.agent_tool_mode),
            "--metric-feedback-mode",
            str(args.metric_feedback_mode),
            "--magentic-max-rounds",
            str(args.magentic_max_rounds),
            "--magentic-max-resets",
            str(args.magentic_max_resets),
            "--timeout",
            str(args.timeout),
            "--api-retries",
            str(args.api_retries),
            "--api-retry-backoff",
            str(args.api_retry_backoff),
            "--max-tasks",
            str(args.max_tasks),
            "--max-step-factor",
            str(args.max_step_factor),
            "--step-limit-multiplier",
            str(args.step_limit_multiplier),
            "--max-step-cap",
            str(args.max_step_cap),
            "--min-step-limit",
            str(args.min_step_limit),
            "--allowed-angles",
            str(args.allowed_angles),
            "--stagnation-patience",
            str(args.stagnation_patience),
            "--min-best-iou-gain",
            str(args.min_best_iou_gain),
        ]
        if bool(args.stop_caution):
            cmd.append("--stop-caution")
        if bool(args.hide_iou_feedback):
            cmd.append("--hide-iou-feedback")
        if bool(args.legacy_stop_as_success):
            cmd.append("--legacy-stop-as-success")
        if args.api_key:
            cmd.extend(["--api-key", args.api_key])
        if args.save_frames:
            cmd.append("--save-frames")
        if args.save_llm_debug:
            cmd.append("--save-llm-debug")
        if args.debug_timing:
            cmd.append("--debug-timing")
        if not args.resume:
            cmd.append("--no-resume")

        print(f"[step02][attempts] run attempts_{n} backend={args.backend}")
        cp = subprocess.run(cmd, cwd=str(project_root))
        summary_path = out_dir / "reports" / "step_02_summary.json"
        entry = {
            "attempts": n,
            "status_code": int(cp.returncode),
            "summary_path": _rel_path(summary_path, project_root),
        }
        if summary_path.exists():
            try:
                data = read_json(summary_path)
                entry["success_rate"] = data.get("success_rate", 0.0)
                entry["samples"] = data.get("samples", 0)
                entry["output"] = data.get("output", "")
            except Exception:
                pass
        run_summaries.append(entry)

    batch_summary = {
        "backend": args.backend,
        "attempts_requested": attempts,
        "ablation_root": _rel_path(ablation_root, project_root),
        "runs": run_summaries,
    }
    batch_summary_path = ablation_root / f"step2_{args.backend}_batch_summary.json"
    write_json(batch_summary_path, batch_summary)
    print(json.dumps(batch_summary, ensure_ascii=False, indent=2))
    print(f"[step02][attempts] summary: {batch_summary_path}")


def main() -> None:
    args = parse_args()
    metric_feedback_mode = resolve_metric_feedback_mode(args.metric_feedback_mode, args.hide_iou_feedback)
    if metric_feedback_mode == "none":
        args.hide_iou_feedback = True
    root = Path(args.root).resolve()
    if args.backend == "gemini_api" and metric_feedback_mode == "none" and os.environ.get("PROJECTION_PREFLIGHT_DONE") != "1":
        from formal_preflight import validate_formal_run

        tasks_path_for_preflight = resolve_optional_path(root, args.tasks_file)
        split_path_for_preflight = resolve_optional_path(root, args.split_file)
        formal_config_path = resolve_optional_path(root, args.formal_config)
        if tasks_path_for_preflight is None or split_path_for_preflight is None or formal_config_path is None:
            raise RuntimeError("Formal API runs require --formal-config, --tasks-file, and --split-file")
        preflight = validate_formal_run(
            root,
            formal_config_path=formal_config_path,
            tasks_path=tasks_path_for_preflight,
            split_path=split_path_for_preflight,
            prompt_protocol=args.prompt_protocol,
            metric_feedback_mode=metric_feedback_mode,
            hide_iou_feedback=bool(args.hide_iou_feedback),
            stagnation_patience=int(args.stagnation_patience),
            max_tokens=int(args.api_max_tokens),
            timeout_seconds=int(args.timeout),
            history_window=int(args.history_window),
            include_history_cot=bool(args.include_history_cot),
            legacy_stop_as_success=bool(args.legacy_stop_as_success),
            api_temperature=float(args.api_temperature),
            api_seed=int(args.api_seed),
            display_size=(int(args.api_display_width), int(args.api_display_height)),
        )
        print(json.dumps({"formal_preflight": preflight}, ensure_ascii=False), flush=True)
    if args.task_workers < 1 or args.render_workers < 1:
        raise ValueError("--task-workers and --render-workers must be positive integers")
    if args.task_workers > 1:
        if args.attempts.strip():
            raise ValueError("--task-workers cannot be combined with --attempts batch mode")
        _run_parallel_tasks(args, root)
        return
    if args.attempts.strip():
        _run_attempt_batch(args, root)
        return
    paths = resolve_paths(root)

    cfg_path = Path(args.config)
    if not cfg_path.is_absolute():
        cfg_path = root / cfg_path
    cfg = read_json(cfg_path)
    debug_timing = bool(args.debug_timing or cfg.get("debug_timing", False))
    seed = int(args.seed) if args.seed else int(cfg["random_seed"])
    rng = random.Random(seed)
    render_metadata = render_environment_metadata()

    if args.backend == "gemini_api":
        model_name = args.model_name.strip() or args.api_model
    else:
        model_name = args.model_name.strip() or cfg.get("model_name", "simulated_mllm_v1")
    allowed_actions = list(ALL_ACTIONS)
    global_allowed_angles = parse_allowed_angles(args.allowed_angles)

    tasks_path = Path(args.tasks_file) if args.tasks_file else paths["tasks"]
    if not tasks_path.is_absolute():
        tasks_path = root / tasks_path

    default_eval_size = STANDARD_IMAGE_SIZE
    default_display = STANDARD_DISPLAY_SIZE
    safe_model_name = re.sub(r"[^A-Za-z0-9._-]+", "_", args.api_model if args.backend == "gemini_api" else args.model_name).strip("._") or "simulated"
    default_output_root = root / "result" / safe_model_name
    if args.backend == "gemini_api":
        default_output_root /= metric_feedback_result_dir(metric_feedback_mode)
    output_root = Path(args.output_dir) if args.output_dir else default_output_root
    if not output_root.is_absolute():
        output_root = root / output_root
    output_logs = output_root / "logs"
    output_reports = output_root / "reports"
    out_csv = output_reports / "model_raw.csv"

    split_path = resolve_optional_path(root, args.split_file)
    tasks, all_task_ids = read_selected_tasks_and_ids(tasks_path, args.max_tasks, split_path)
    selected_task_ids = [str(t.get("sample_id", "")).strip() for t in tasks if str(t.get("sample_id", "")).strip()]

    checkpoint_path = output_reports / "step_02_checkpoint.json"
    cfg_fp = step2_config_fingerprint(
        cfg=cfg,
        args=args,
        root=root,
        tasks_path=tasks_path,
        model_name=model_name,
        all_task_ids=all_task_ids,
        global_allowed_angles=global_allowed_angles,
    )
    checkpoint = load_checkpoint(checkpoint_path) if args.resume else {}
    if args.resume and checkpoint:
        if checkpoint.get("config_fingerprint", "") != cfg_fp:
            raise RuntimeError(
                "Step02 checkpoint configuration mismatch. "
                "Delete reports/step_02_checkpoint.json or rerun with --no-resume."
            )
        if str(checkpoint.get("tasks_file", "")) != _rel_path(tasks_path, root):
            raise RuntimeError(
                "Step02 checkpoint tasks file mismatch. "
                "Delete reports/step_02_checkpoint.json or rerun with --no-resume."
            )

    existing_rows: List[Dict] = []
    csv_completed_ids = set()
    if args.resume and out_csv.exists():
        try:
            with out_csv.open("r", encoding="utf-8") as f:
                existing_rows = list(csv.DictReader(f))
            csv_completed_ids = {
                str(r.get("sample_id", "")).strip()
                for r in existing_rows
                if str(r.get("sample_id", "")).strip()
            }
        except Exception:
            existing_rows = []
            csv_completed_ids = set()

    checkpoint_completed_ids = {
        str(x).strip()
        for x in checkpoint.get("completed_sample_ids", [])
        if str(x).strip()
    }
    completed_ids = set(csv_completed_ids)

    if completed_ids:
        tasks = [t for t in tasks if str(t.get("sample_id", "")).strip() not in completed_ids]
    print(
        f"[step02][resume] enabled={bool(args.resume)} "
        f"existing_csv={len(existing_rows)} checkpoint_done={len(checkpoint_completed_ids)} "
        f"pending={len(tasks)} skipped={len(completed_ids)}"
    )

    traj_root = output_logs / "model_traj"
    traj_root.mkdir(parents=True, exist_ok=True)
    frame_root = output_logs / "model_frames"
    if args.save_frames:
        frame_root.mkdir(parents=True, exist_ok=True)
    llm_debug_root = output_logs / "llm_debug"
    if args.save_llm_debug and args.backend == "gemini_api":
        llm_debug_root.mkdir(parents=True, exist_ok=True)
    agent_active_root = output_logs / "agent_workspaces_active"
    agent_archive_root = output_logs / "agent_workspaces"

    rows: List[Dict] = list(existing_rows)
    completed_ids_runtime = set(csv_completed_ids)
    if args.resume and checkpoint and checkpoint.get("rng_state"):
        rng.setstate(decode_rng_state(str(checkpoint.get("rng_state", ""))))
    timing_report: Dict[str, object] = {
        "debug_timing": bool(debug_timing),
        "backend": args.backend,
        "tasks": [],
        "aggregate": {
            "task_setup_s": 0.0,
            "target_render_s": 0.0,
            "target_encode_s": 0.0,
            "step_render_s": 0.0,
            "step_iou_s": 0.0,
            "step_frame_io_s": 0.0,
            "step_encode_s": 0.0,
            "step_policy_api_s": 0.0,
            "step_policy_sim_s": 0.0,
            "step_event_write_s": 0.0,
            "step_apply_action_s": 0.0,
            "step_total_s": 0.0,
        },
    }
    checkpoint = {
        "version": 1,
        "config_fingerprint": cfg_fp,
        "backend": args.backend,
            "model_name": model_name,
            "checkpoint_path": str(args.checkpoint_path),
        "tasks_file": _rel_path(tasks_path, root),
        "output_csv": _rel_path(out_csv, root),
        "raw_events_file": _rel_path(output_logs / "model_events.jsonl", root),
        "trajectory_dir": _rel_path(output_logs / "model_traj", root),
        "attempt_tag": args.attempt_tag,
        "seed": seed,
        "selected_task_count": len(selected_task_ids),
        "selected_task_ids": list(selected_task_ids),
        "completed_count": len(completed_ids_runtime),
        "completed_sample_ids": sorted(completed_ids_runtime),
        "existing_rows_count": len(rows),
        "last_completed_sample_id": checkpoint.get("last_completed_sample_id", "") if checkpoint else "",
        "allowed_angles": list(global_allowed_angles),
        "prompt_profile": args.prompt_profile if args.backend == "gemini_api" else "",
        "prompt_protocol": args.prompt_protocol if args.backend == "gemini_api" else "",
        "hide_iou_feedback": bool(args.hide_iou_feedback) if args.backend == "gemini_api" else False,
        "stop_protocol": "legacy_stop_as_visual_success" if args.legacy_stop_as_success else "auto_success_stop_no_further_idea_v1",
        "stagnation_patience": int(args.stagnation_patience),
        "min_best_iou_gain": float(args.min_best_iou_gain),
        "step_limit_multiplier": float(args.step_limit_multiplier),
        "max_step_cap": int(args.max_step_cap),
        "min_step_limit": int(args.min_step_limit),
        "api_display_width": int(args.api_display_width) if args.backend == "gemini_api" else 0,
        "api_display_height": int(args.api_display_height) if args.backend == "gemini_api" else 0,
        "save_frames": bool(args.save_frames),
        "save_llm_debug": bool(args.save_llm_debug) if args.backend == "gemini_api" else False,
        "llm_debug_dir": _rel_path(llm_debug_root, root) if (args.save_llm_debug and args.backend == "gemini_api") else "",
        "rng_state": encode_rng_state(rng.getstate()),
    }
    write_json(checkpoint_path, checkpoint)
    raw_events_path = output_logs / "model_events.jsonl"
    raw_events_path.parent.mkdir(parents=True, exist_ok=True)
    events_mode = "a" if (args.resume and raw_events_path.exists()) else "w"
    with raw_events_path.open(events_mode, encoding="utf-8") as events_f:
        for task in tqdm(tasks, desc=f"Step02 {args.backend}", unit="task"):
            t_task_start = perf_counter()
            t_setup = perf_counter()
            sample_id = task["sample_id"]
            agent_workspace = None
            if args.backend == "gemini_api" and args.api_orchestration in {"magentic", "react"} and args.agent_tool_mode == "python":
                from agent_python_workspace import AgentPythonWorkspace

                if args.api_image_mode != "panel":
                    raise ValueError("metric_blind_agent_python_v2 requires --api-image-mode panel")
                agent_workspace = AgentPythonWorkspace(agent_active_root, agent_archive_root, sample_id)
            target_pose = Pose.from_dict(task["target_pose"])
            current = Pose.from_dict(task["init_pose"])
            task["cuda_point_batch_size"] = STANDARD_POINT_BATCH_SIZE
            theta = int(task["theta"])
            task_allowed_angles = resolve_effective_allowed_angles(task.get("allowed_angles", []), global_allowed_angles)
            tau = resolve_effective_tau(task, cfg)
            eval_size = STANDARD_IMAGE_SIZE
            display_size = STANDARD_DISPLAY_SIZE
            base_tmax = max(1, int(task.get("T_max", 1)))
            step_limit = max(args.min_step_limit, int(math.ceil(base_tmax * max(1.0, float(args.step_limit_multiplier)))))
            if args.max_step_cap > 0:
                step_limit = min(step_limit, args.max_step_cap)
            dt_setup = perf_counter() - t_setup
            timing_report["aggregate"]["task_setup_s"] += dt_setup

            t_target_render = perf_counter()
            target_mask_disp, target_mask_eval, _ = render_views_limited(
                task,
                target_pose,
                display_size=display_size,
                eval_size=eval_size,
                need_oblique=False,
            )
            render_backends = {get_last_render_backend()}
            target_eval_sha256 = mask_sha256(target_mask_eval)
            initial_eval_sha256 = ""
            initial_iou_full_precision: Optional[float] = None
            dt_target_render = perf_counter() - t_target_render
            timing_report["aggregate"]["target_render_s"] += dt_target_render
            recent_actions: List[str] = []
            recent_panel_data_urls: List[str] = []
            recent_state_summaries: List[str] = []
            trajectory: List[Dict] = []

            t_target_encode = perf_counter()
            target_img_arr = _to_uint8_image(target_mask_disp)
            target_data_url = image_array_to_data_url(target_mask_disp) if args.backend == "gemini_api" else ""
            dt_target_encode = perf_counter() - t_target_encode
            timing_report["aggregate"]["target_encode_s"] += dt_target_encode
            sample_frame_dir = frame_root / sample_id if args.save_frames else None
            if sample_frame_dir is not None:
                sample_frame_dir.mkdir(parents=True, exist_ok=True)
            sample_llm_debug_dir = llm_debug_root / sample_id if (args.save_llm_debug and args.backend == "gemini_api") else None
            if sample_llm_debug_dir is not None:
                sample_llm_debug_dir.mkdir(parents=True, exist_ok=True)

            terminated_reason = "max_steps"
            success = False
            final_iou = 0.0
            previous_iou: Optional[float] = None
            best_iou = float("-inf")
            nonimproving_rotations = 0
            budget_exhaustion_mode = ""
            last_action = ""
            api_fallback_count = 0
            api_error_count = 0
            api_error_last = ""
            sample_prompt_tokens = 0
            sample_completion_tokens = 0
            sample_total_tokens = 0
            task_timing: Dict[str, object] = {
                "sample_id": sample_id,
                "setup_s": round(dt_setup, 6),
                "target_render_s": round(dt_target_render, 6),
                "target_encode_s": round(dt_target_encode, 6),
                "step_limit": step_limit,
                "steps": [],
            }

            start_ms = now_ms()
            for step in range(1, step_limit + 1):
                t_step_total = perf_counter()
                t_step_render = perf_counter()
                current_mask_disp, current_mask_eval, current_oblique = render_views_limited(
                    task,
                    current,
                    display_size=display_size,
                    eval_size=eval_size,
                    need_oblique=True,
                )
                render_backends.add(get_last_render_backend())
                dt_step_render = perf_counter() - t_step_render
                timing_report["aggregate"]["step_render_s"] += dt_step_render
                t_step_iou = perf_counter()
                score = iou(target_mask_eval, current_mask_eval)
                if step == 1:
                    initial_eval_sha256 = mask_sha256(current_mask_eval)
                    initial_iou_full_precision = float(score)
                dt_step_iou = perf_counter() - t_step_iou
                timing_report["aggregate"]["step_iou_s"] += dt_step_iou
                final_iou = score
                if score > best_iou + float(args.min_best_iou_gain):
                    best_iou = score
                    nonimproving_rotations = 0
                else:
                    nonimproving_rotations += 1
                if (
                    score < tau
                    and int(args.stagnation_patience) > 0
                    and nonimproving_rotations >= int(args.stagnation_patience)
                ):
                    budget_exhaustion_mode = "early_stop"
                    terminated_reason = "early_stop"
                    break

                panel_frame_rel = ""
                panel_meta_rel = ""
                panel_text_rel = ""
                current_proj_data_url = ""
                current_oblique_data_url = ""
                panel_data_url = ""
                panel_img = None
                dt_step_frame_io = 0.0
                dt_step_encode = 0.0
                t_step_io = perf_counter()
                if args.save_frames and np is not None and cv2 is not None and target_img_arr is not None:
                    panel_img = compose_triptych_image(target_img_arr, current_oblique, current_mask_disp)
                if args.backend == "gemini_api":
                    if args.api_image_mode == "panel" and target_img_arr is not None and np is not None and cv2 is not None:
                        panel_data_url = compose_triptych_data_url(target_img_arr, current_oblique, current_mask_disp)
                    else:
                        current_proj_data_url = image_array_to_data_url(current_mask_disp)
                        current_oblique_data_url = image_array_to_data_url(current_oblique)
                if agent_workspace is not None and score < tau:
                    agent_workspace.prepare_step(panel_data_url, step)
                dt_step_io = perf_counter() - t_step_io
                if args.save_frames:
                    dt_step_frame_io = dt_step_io
                elif args.backend == "gemini_api":
                    dt_step_encode = dt_step_io
                timing_report["aggregate"]["step_frame_io_s"] += dt_step_frame_io
                timing_report["aggregate"]["step_encode_s"] += dt_step_encode

                dt_policy_api = 0.0
                dt_policy_sim = 0.0
                model_response_text = ""
                model_cot_text = ""
                model_system_prompt = ""
                model_prompt_text = ""
                step_prompt_tokens = 0
                step_completion_tokens = 0
                step_total_tokens = 0
                step_llm_debug_dir_rel = ""
                action_angle = task_allowed_angles[0]
                if score >= tau:
                    action = "STOP"
                    model_response_text = "AUTO_STOP_THRESHOLD_REACHED"
                    model_cot_text = "AUTO_STOP_THRESHOLD_REACHED"
                    model_prompt_text = "AUTO_STOP_THRESHOLD_REACHED"
                    success = True
                    terminated_reason = "threshold_reached"
                else:
                    if args.backend == "gemini_api":
                        api_key = args.api_key.strip() or API_KEY.strip()
                        t_policy = perf_counter()
                        retries = max(0, int(args.api_retries))
                        backoff = max(0.1, float(args.api_retry_backoff))
                        action = "STOP"
                        for attempt_idx in range(retries + 1):
                            try:
                                debug_dir_this_attempt = None
                                if sample_llm_debug_dir is not None:
                                    debug_dir_this_attempt = sample_llm_debug_dir / f"step_{step:03d}" / f"attempt_{attempt_idx + 1:02d}"
                                api_result = call_openai_compatible_chat(
                                    base_url=args.base_url,
                                    api_key=api_key,
                                    model=args.api_model,
                                    image_mode=args.api_image_mode,
                                    target_image_data_url=target_data_url,
                                    current_oblique_data_url=current_oblique_data_url,
                                    current_image_data_url=current_proj_data_url,
                                    panel_image_data_url=panel_data_url,
                                    history_actions=recent_actions,
                                    recent_panel_data_urls=recent_panel_data_urls,
                                    recent_state_summaries=recent_state_summaries,
                                    history_window=int(args.history_window),
                                    step=step,
                                    step_limit=step_limit,
                                    current_iou=score,
                                    tau_success=tau,
                                    last_action=last_action,
                                    allowed_actions=allowed_actions,
                                    allowed_angles=task_allowed_angles,
                                    timeout_s=args.timeout,
                                    prompt_profile=args.prompt_profile,
                                    prompt_protocol=args.prompt_protocol,
                                    metric_feedback_mode=metric_feedback_mode,
                                    api_temperature=args.api_temperature,
                                    api_seed=args.api_seed,
                                    api_max_tokens=args.api_max_tokens,
                                    api_response_format=args.api_response_format,
                                    api_orchestration=args.api_orchestration,
                                    agent_tool_mode=args.agent_tool_mode,
                                    agent_workspace=agent_workspace,
                                    magentic_max_rounds=args.magentic_max_rounds,
                                    magentic_max_resets=args.magentic_max_resets,
                                    stop_caution=bool(args.stop_caution),
                                    hide_iou_feedback=bool(args.hide_iou_feedback),
                                    legacy_stop_as_success=bool(args.legacy_stop_as_success),
                                    debug_dir=debug_dir_this_attempt,
                                    ledger_path=str(sample_llm_debug_dir / "api_calls.jsonl") if sample_llm_debug_dir is not None else "",
                                )
                                action = api_result["action"]
                                action_angle = int(api_result.get("angle", task_allowed_angles[0]))
                                model_response_text = api_result.get("raw_text", "")
                                model_cot_text = api_result.get("cot", "")
                                model_system_prompt = api_result.get("system_prompt", "")
                                model_prompt_text = api_result.get("prompt_text", "")
                                usage = api_result.get("usage", {}) or {}
                                step_prompt_tokens = int(usage.get("prompt_tokens", 0) or 0)
                                step_completion_tokens = int(usage.get("completion_tokens", 0) or 0)
                                step_total_tokens = int(usage.get("total_tokens", 0) or 0)
                                if debug_dir_this_attempt is not None:
                                    step_llm_debug_dir_rel = str(debug_dir_this_attempt.relative_to(root)).replace("\\", "/")
                                sample_prompt_tokens += step_prompt_tokens
                                sample_completion_tokens += step_completion_tokens
                                sample_total_tokens += step_total_tokens
                                break
                            except Exception as e:
                                if attempt_idx < retries:
                                    sleep_s, retry_reason = compute_api_retry_sleep(str(e), attempt_idx, backoff)
                                    print(
                                        f"[step02][api-retry] sample={sample_id} step={step} "
                                        f"attempt={attempt_idx + 1}/{retries + 1} "
                                        f"reason={retry_reason} sleep={sleep_s:.1f}s error={e}",
                                        flush=True,
                                    )
                                    if sample_llm_debug_dir is not None:
                                        retry_record = {
                                            "timestamp": datetime.now(timezone.utc).isoformat(),
                                            "sample_id": str(sample_id),
                                            "environment_step": int(step),
                                            "attempt": int(attempt_idx + 1),
                                            "max_attempts": int(retries + 1),
                                            "reason": str(retry_reason),
                                            "sleep_seconds": float(sleep_s),
                                            "error_type": type(e).__name__,
                                        }
                                        with (sample_llm_debug_dir / "retry_waits.jsonl").open("a", encoding="utf-8") as retry_file:
                                            retry_file.write(json.dumps(retry_record, ensure_ascii=False) + "\n")
                                    time.sleep(sleep_s)
                                else:
                                    api_error_count += 1
                                    api_error_last = str(e)
                                    raise RuntimeError(
                                        f"API retries exhausted for sample={sample_id} step={step}: {api_error_last}"
                                    ) from e
                        dt_policy_api = perf_counter() - t_policy
                    else:
                        t_policy = perf_counter()
                        action, action_angle = choose_action_simulated(
                            rng,
                            current,
                            target_pose,
                            task["stream_type"],
                            recent_actions,
                            allowed_actions,
                            task_allowed_angles,
                        )
                        model_response_text = f"SIMULATED_POLICY: action={action}, angle={action_angle}"
                        model_cot_text = ""
                        model_prompt_text = ""
                        dt_policy_sim = perf_counter() - t_policy
                timing_report["aggregate"]["step_policy_api_s"] += dt_policy_api
                timing_report["aggregate"]["step_policy_sim_s"] += dt_policy_sim

                if args.save_frames and sample_frame_dir is not None and panel_img is not None:
                    panel_frame_path = sample_frame_dir / f"panel_{step:03d}.png"
                    save_triptych_panel(
                        panel_frame_path,
                        panel_img,
                    )
                    panel_frame_rel = str(panel_frame_path.relative_to(root)).replace("\\", "/")
                    panel_text_path = sample_frame_dir / f"panel_{step:03d}.txt"
                    panel_text_path.write_text(
                        f"action={action}\n"
                        f"action_angle={int(action_angle)}\n"
                        f"iou={score:.6f}\n"
                        f"tau_success={tau:.6f}\n"
                        f"used_steps={max(0, step - 1)}\n"
                        f"step_limit={step_limit}\n"
                        f"model_prompt={model_prompt_text}\n"
                        f"model_cot={model_cot_text}\n"
                        f"model_response_raw={model_response_text}\n",
                        encoding="utf-8",
                    )
                    panel_text_rel = str(panel_text_path.relative_to(root)).replace("\\", "/")
                    panel_meta_path = sample_frame_dir / f"panel_{step:03d}.json"
                    write_json(
                        panel_meta_path,
                        {
                            "sample_id": sample_id,
                            "step": step,
                            "action": action,
                            "action_angle": int(action_angle),
                            "model_response_raw": model_response_text,
                            "model_cot": model_cot_text,
                            "model_prompt": model_prompt_text,
                            "iou": round(score, 6),
                            "tau_success": round(tau, 6),
                            "used_steps": max(0, step - 1),
                            "step_limit": step_limit,
                            "pose_before_action": current.as_dict(),
                            "backend": args.backend,
                            "api_image_mode": args.api_image_mode if args.backend == "gemini_api" else "",
                            "llm_debug_dir": step_llm_debug_dir_rel,
                            "panel_response_file": panel_text_rel,
                        },
                    )
                    panel_meta_rel = str(panel_meta_path.relative_to(root)).replace("\\", "/")

                t_event = perf_counter()
                record = {
                    "timestamp_ms": now_ms(),
                    "sample_id": sample_id,
                    "step": step,
                    "action": action,
                    "action_angle": int(action_angle),
                    "pose": current.as_dict(),
                    "iou": round(score, 6),
                    "iou_full_precision": float(score),
                    "iou_display": f"{score:.3f}",
                    "render_backend": get_last_render_backend(),
                    "target_frame": task["target_image"],
                    "oblique_frame": "",
                    "projection_frame": "",
                    "panel_frame": panel_frame_rel,
                    "panel_meta": panel_meta_rel,
                    "panel_response_file": panel_text_rel,
                    "model_response_raw": model_response_text,
                    "model_cot": model_cot_text,
                    "model_system_prompt": model_system_prompt,
                    "model_prompt": model_prompt_text,
                    "backend": args.backend,
                    "agent_tool_mode": args.agent_tool_mode if args.api_orchestration in {"magentic", "react"} else "none",
                    "agent_tool_protocol": "python_workspace_view_image_v4" if agent_workspace is not None else "",
                    "agent_tool_stats": agent_workspace.stats() if agent_workspace is not None else {},
                    "prompt_profile": args.prompt_profile if args.backend == "gemini_api" else "",
                    "prompt_protocol": args.prompt_protocol if args.backend == "gemini_api" else "",
                    "metric_feedback_mode": metric_feedback_mode if args.backend == "gemini_api" else "",
                    "metric_feedback_mode": metric_feedback_mode if args.backend == "gemini_api" else "",
                    "hide_iou_feedback": bool(args.hide_iou_feedback) if args.backend == "gemini_api" else False,
                    "stop_protocol": "legacy_stop_as_visual_success" if args.legacy_stop_as_success else "auto_success_stop_no_further_idea_v1",
                    "prompt_tokens": int(step_prompt_tokens) if args.backend == "gemini_api" else 0,
                    "completion_tokens": int(step_completion_tokens) if args.backend == "gemini_api" else 0,
                    "total_tokens": int(step_total_tokens) if args.backend == "gemini_api" else 0,
                    "llm_debug_dir": step_llm_debug_dir_rel,
                }
                trajectory.append(record)
                events_f.write(json.dumps(record, ensure_ascii=False) + "\n")
                dt_event = perf_counter() - t_event
                timing_report["aggregate"]["step_event_write_s"] += dt_event
                if panel_data_url:
                    recent_panel_data_urls.append(panel_data_url)
                    if args.history_window == 0:
                        recent_panel_data_urls = []
                    elif args.history_window > 0 and len(recent_panel_data_urls) > args.history_window:
                        recent_panel_data_urls = recent_panel_data_urls[-args.history_window:]
                state_summary = f"step={step} action={action} angle={int(action_angle)}"
                if args.include_history_cot and model_cot_text:
                    state_summary += f" cot={model_cot_text}"
                if metric_feedback_mode == "full":
                    state_summary += f" iou={score:.6f}"
                recent_state_summaries.append(state_summary)
                if args.history_window == 0:
                    recent_state_summaries = []
                elif args.history_window > 0 and len(recent_state_summaries) > args.history_window:
                    recent_state_summaries = recent_state_summaries[-args.history_window:]

                dt_apply = 0.0

                if action == "STOP":
                    success, terminated_reason = resolve_stop_outcome(score, tau)
                else:
                    t_apply = perf_counter()
                    current = apply_action_with_angle(current, action, int(action_angle))
                    recent_actions.append(action)
                    if args.history_window == 0:
                        recent_actions = []
                    elif args.history_window > 0 and len(recent_actions) > args.history_window:
                        recent_actions = recent_actions[-args.history_window:]
                    last_action = action
                    dt_apply = perf_counter() - t_apply
                timing_report["aggregate"]["step_apply_action_s"] += dt_apply
                dt_step_total = perf_counter() - t_step_total
                timing_report["aggregate"]["step_total_s"] += dt_step_total
                previous_iou = score

                if debug_timing:
                    step_timing = {
                        "step": step,
                        "render_s": round(dt_step_render, 6),
                        "iou_s": round(dt_step_iou, 6),
                        "frame_io_s": round(dt_step_frame_io, 6),
                        "encode_s": round(dt_step_encode, 6),
                        "policy_api_s": round(dt_policy_api, 6),
                        "policy_sim_s": round(dt_policy_sim, 6),
                        "event_write_s": round(dt_event, 6),
                        "apply_action_s": round(dt_apply, 6),
                        "total_s": round(dt_step_total, 6),
                        "action": action,
                        "angle": int(action_angle),
                        "iou": round(score, 6),
                    }
                    task_timing["steps"].append(step_timing)
                    print(
                        f"[timing][step02][{sample_id}][step={step}] "
                        f"render_ms={dt_step_render * 1000:.2f} "
                        f"iou_ms={dt_step_iou * 1000:.2f} "
                        f"encode_ms={dt_step_encode * 1000:.2f} "
                        f"policy_api_ms={dt_policy_api * 1000:.2f} "
                        f"policy_sim_ms={dt_policy_sim * 1000:.2f} "
                        f"event_ms={dt_event * 1000:.2f} "
                        f"apply_ms={dt_apply * 1000:.2f} "
                        f"total_ms={dt_step_total * 1000:.2f} "
                        f"action={action} iou={score:.4f}"
                    )
                if action == "STOP":
                    break

            if trajectory and trajectory[-1]["action"] != "STOP" and len(trajectory) >= step_limit:
                _, final_mask_eval, _ = render_views_limited(
                    task,
                    current,
                    display_size=display_size,
                    eval_size=eval_size,
                    need_oblique=False,
                )
                final_iou = iou(target_mask_eval, final_mask_eval)
                success = bool(final_iou >= tau)
                if success:
                    terminated_reason = "threshold_reached"
            if terminated_reason == "max_steps" and not budget_exhaustion_mode:
                budget_exhaustion_mode = "fixed_step_limit"

            elapsed = now_ms() - start_ms
            dt_task_total = perf_counter() - t_task_start
            steps = len(trajectory)
            agent_workspace_archive = ""
            agent_tool_stats = {}
            if agent_workspace is not None:
                agent_tool_stats = agent_workspace.stats()
                agent_workspace_archive = _rel_path(agent_workspace.archive(), root)
            row = {
                "sample_id": sample_id,
                "stream_type": task["stream_type"],
                "model_name": model_name,
                "checkpoint_path": str(args.checkpoint_path),
                "backend": args.backend,
                "agent_tool_mode": args.agent_tool_mode if args.api_orchestration in {"magentic", "react"} else "none",
                "agent_tool_protocol": str(agent_tool_stats.get("protocol", "")),
                "agent_tool_calls": int(agent_tool_stats.get("calls", 0)),
                "agent_tool_failed_calls": int(agent_tool_stats.get("failed_calls", 0)),
                "agent_tool_runtime_s": float(agent_tool_stats.get("runtime_s", 0.0)),
                "agent_workspace_archive": agent_workspace_archive,
                "success": int(success),
                "steps": steps,
                "step_limit": step_limit,
                "iou_final": round(final_iou, 6),
                "iou_final_display": f"{final_iou:.3f}",
                "projection_distance_final": round(1.0 - final_iou, 6),
                "tau_success": round(tau, 6),
                "latency_ms": elapsed,
                "terminated_reason": terminated_reason,
                "seed": seed,
                "api_seed": int(args.api_seed) if args.backend == "gemini_api" else 0,
                "api_fallback_count": api_fallback_count,
                "api_error_count": api_error_count,
                "api_error_last": api_error_last,
                "prompt_tokens": int(sample_prompt_tokens) if args.backend == "gemini_api" else 0,
                "completion_tokens": int(sample_completion_tokens) if args.backend == "gemini_api" else 0,
                "total_tokens": int(sample_total_tokens) if args.backend == "gemini_api" else 0,
                "trajectory_path": _rel_path(traj_root / f"{sample_id}.json", root),
                "attempt_tag": args.attempt_tag,
                "api_image_mode": args.api_image_mode if args.backend == "gemini_api" else "",
                "prompt_profile": args.prompt_profile if args.backend == "gemini_api" else "",
                "prompt_protocol": args.prompt_protocol if args.backend == "gemini_api" else "",
                "metric_feedback_mode": metric_feedback_mode if args.backend == "gemini_api" else "",
                "render_environment": json.dumps(render_metadata, sort_keys=True, separators=(",", ":")),
                "hide_iou_feedback": bool(args.hide_iou_feedback) if args.backend == "gemini_api" else False,
                "stop_protocol": "legacy_stop_as_visual_success" if args.legacy_stop_as_success else "auto_success_stop_no_further_idea_v1",
                "stagnation_patience": int(args.stagnation_patience),
                "min_best_iou_gain": float(args.min_best_iou_gain),
                "nonimproving_rotations_at_end": int(nonimproving_rotations),
                "budget_exhaustion_mode": budget_exhaustion_mode,
                "history_window": int(args.history_window) if args.backend == "gemini_api" else 0,
                "step_limit_multiplier": float(args.step_limit_multiplier),
                "api_display_width": int(args.api_display_width) if args.backend == "gemini_api" else 0,
                "api_display_height": int(args.api_display_height) if args.backend == "gemini_api" else 0,
                "model_version": args.api_model if args.backend == "gemini_api" else model_name,
                "allowed_angles": ",".join(str(x) for x in task_allowed_angles),
                "render_backends": ",".join(sorted(render_backends)),
                "target_eval_sha256": target_eval_sha256,
                "initial_eval_sha256": initial_eval_sha256,
                "initial_iou_full_precision": initial_iou_full_precision,
                "initial_iou_display": f"{float(initial_iou_full_precision or 0.0):.3f}",
                "target_pose": json.dumps(target_pose.as_dict(), separators=(",", ":")),
                "init_pose": json.dumps(Pose.from_dict(task["init_pose"]).as_dict(), separators=(",", ":")),
                "eval_size": eval_size,
                "llm_debug_dir": _rel_path(sample_llm_debug_dir, root) if sample_llm_debug_dir is not None else "",
            }
            rows.append(row)
            if debug_timing:
                task_timing["task_total_s"] = round(dt_task_total, 6)
                task_timing["steps_executed"] = steps
                task_timing["terminated_reason"] = terminated_reason
                timing_report["tasks"].append(task_timing)
                print(
                    f"[timing][step02][task={sample_id}] "
                    f"setup_ms={dt_setup * 1000:.2f} "
                    f"target_render_ms={dt_target_render * 1000:.2f} "
                    f"target_encode_ms={dt_target_encode * 1000:.2f} "
                    f"steps={steps} task_total_ms={dt_task_total * 1000:.2f} "
                    f"reason={terminated_reason}"
                )

            write_json(
                traj_root / f"{sample_id}.json",
                {
                    "sample_id": sample_id,
                    "model_name": model_name,
                    "checkpoint_path": str(args.checkpoint_path),
                    "backend": args.backend,
                    "seed": seed,
                    "agent_tool_mode": args.agent_tool_mode if args.api_orchestration in {"magentic", "react"} else "none",
                    "agent_tool_protocol": str(agent_tool_stats.get("protocol", "")),
                    "agent_tool_stats": agent_tool_stats,
                    "agent_workspace_archive": agent_workspace_archive,
                    "api_seed": int(args.api_seed) if args.backend == "gemini_api" else 0,
                    "api_fallback_count": api_fallback_count,
                    "api_error_count": api_error_count,
                    "api_error_last": api_error_last,
                    "prompt_profile": args.prompt_profile if args.backend == "gemini_api" else "",
                    "prompt_protocol": args.prompt_protocol if args.backend == "gemini_api" else "",
                    "hide_iou_feedback": bool(args.hide_iou_feedback) if args.backend == "gemini_api" else False,
                    "stop_protocol": "legacy_stop_as_visual_success" if args.legacy_stop_as_success else "auto_success_stop_no_further_idea_v1",
                    "stagnation_patience": int(args.stagnation_patience),
                    "min_best_iou_gain": float(args.min_best_iou_gain),
                    "nonimproving_rotations_at_end": int(nonimproving_rotations),
                    "budget_exhaustion_mode": budget_exhaustion_mode,
                    "render_backends": sorted(render_backends),
                    "render_environment": render_metadata,
                    "target_eval_sha256": target_eval_sha256,
                    "initial_eval_sha256": initial_eval_sha256,
                    "initial_iou_full_precision": initial_iou_full_precision,
                    "initial_iou_display": f"{float(initial_iou_full_precision or 0.0):.3f}",
                    "target_pose": target_pose.as_dict(),
                    "init_pose": Pose.from_dict(task["init_pose"]).as_dict(),
                    "eval_size": eval_size,
                    "llm_debug_dir": _rel_path(sample_llm_debug_dir, root) if sample_llm_debug_dir is not None else "",
                    "records": trajectory,
                },
            )
            write_csv_rows(out_csv, rows)
            completed_ids_runtime.add(sample_id)
            checkpoint = {
                "version": 1,
                "config_fingerprint": cfg_fp,
                "backend": args.backend,
                "model_name": model_name,
                "tasks_file": _rel_path(tasks_path, root),
                "output_csv": _rel_path(out_csv, root),
                "raw_events_file": _rel_path(raw_events_path, root),
                "trajectory_dir": _rel_path(traj_root, root),
                "attempt_tag": args.attempt_tag,
                "seed": seed,
                "api_seed": int(args.api_seed) if args.backend == "gemini_api" else 0,
                "selected_task_count": len(selected_task_ids),
                "selected_task_ids": list(selected_task_ids),
                "completed_count": len(completed_ids_runtime),
                "completed_sample_ids": sorted(completed_ids_runtime),
                "existing_rows_count": len(rows),
                "last_completed_sample_id": sample_id,
                "allowed_angles": list(global_allowed_angles),
                "prompt_profile": args.prompt_profile if args.backend == "gemini_api" else "",
                "prompt_protocol": args.prompt_protocol if args.backend == "gemini_api" else "",
                "hide_iou_feedback": bool(args.hide_iou_feedback) if args.backend == "gemini_api" else False,
                "stop_protocol": "legacy_stop_as_visual_success" if args.legacy_stop_as_success else "auto_success_stop_no_further_idea_v1",
                "stagnation_patience": int(args.stagnation_patience),
                "min_best_iou_gain": float(args.min_best_iou_gain),
                "history_window": int(args.history_window) if args.backend == "gemini_api" else 0,
                "api_display_width": int(args.api_display_width) if args.backend == "gemini_api" else 0,
                "api_display_height": int(args.api_display_height) if args.backend == "gemini_api" else 0,
                "model_version": args.api_model if args.backend == "gemini_api" else model_name,
                "step_limit_multiplier": float(args.step_limit_multiplier),
                "max_step_cap": int(args.max_step_cap),
                "min_step_limit": int(args.min_step_limit),
                "save_frames": bool(args.save_frames),
                "save_llm_debug": bool(args.save_llm_debug) if args.backend == "gemini_api" else False,
                "llm_debug_dir": _rel_path(llm_debug_root, root) if (args.save_llm_debug and args.backend == "gemini_api") else "",
                "rng_state": encode_rng_state(rng.getstate()),
            }
            write_json(checkpoint_path, checkpoint)

    def _to_int(v) -> int:
        try:
            return int(v)
        except Exception:
            try:
                return int(float(v))
            except Exception:
                return 0

    write_csv_rows(out_csv, rows)

    summary = {
        "model_name": model_name,
        "model_version": args.api_model if args.backend == "gemini_api" else model_name,
        "checkpoint_path": str(args.checkpoint_path),
        "backend": args.backend,
        "seed": seed,
        "samples": len(rows),
        "resume_enabled": bool(args.resume),
        "existing_samples_before_run": len(existing_rows),
        "new_samples_this_run": max(0, len(rows) - len(existing_rows)),
        "pending_samples_run": len(tasks),
        "checkpoint_file": _rel_path(checkpoint_path, root),
        "checkpoint_completed_count": len(completed_ids_runtime),
        "success_rate": round(sum(_to_int(r.get("success", 0)) for r in rows) / len(rows), 6) if rows else 0.0,
        "prompt_tokens": sum(_to_int(r.get("prompt_tokens", 0)) for r in rows) if rows else 0,
        "completion_tokens": sum(_to_int(r.get("completion_tokens", 0)) for r in rows) if rows else 0,
        "total_tokens": sum(_to_int(r.get("total_tokens", 0)) for r in rows) if rows else 0,
        "total_api_fallback_count": sum(_to_int(r.get("api_fallback_count", 0)) for r in rows) if rows else 0,
        "total_api_error_count": sum(_to_int(r.get("api_error_count", 0)) for r in rows) if rows else 0,
        "output": _rel_path(out_csv, root),
        "attempt_tag": args.attempt_tag,
        "api_image_mode": args.api_image_mode if args.backend == "gemini_api" else "",
        "prompt_profile": args.prompt_profile if args.backend == "gemini_api" else "",
        "prompt_protocol": args.prompt_protocol if args.backend == "gemini_api" else "",
        "metric_feedback_mode": metric_feedback_mode if args.backend == "gemini_api" else "",
        "render_environment": render_metadata,
        "hide_iou_feedback": bool(args.hide_iou_feedback) if args.backend == "gemini_api" else False,
        "stop_protocol": "legacy_stop_as_visual_success" if args.legacy_stop_as_success else "auto_success_stop_no_further_idea_v1",
        "stagnation_patience": int(args.stagnation_patience),
        "min_best_iou_gain": float(args.min_best_iou_gain),
        "history_window": int(args.history_window) if args.backend == "gemini_api" else 0,
        "api_temperature": float(args.api_temperature) if args.backend == "gemini_api" else 0.0,
        "api_seed": int(args.api_seed) if args.backend == "gemini_api" else 0,
        "api_max_tokens": int(args.api_max_tokens) if args.backend == "gemini_api" else 0,
        "api_response_format": str(args.api_response_format) if args.backend == "gemini_api" else "",
        "api_orchestration": str(args.api_orchestration) if args.backend == "gemini_api" else "",
        "agent_tool_mode": str(args.agent_tool_mode) if args.api_orchestration in {"magentic", "react"} else "none",
        "agent_tool_protocol": "python_workspace_view_image_v4" if args.api_orchestration in {"magentic", "react"} and args.agent_tool_mode == "python" else "",
        "api_stream": False,
        "magentic_max_rounds": int(args.magentic_max_rounds) if args.backend == "gemini_api" else 0,
        "magentic_max_resets": int(args.magentic_max_resets) if args.backend == "gemini_api" else 0,
        "timeout_seconds": int(args.timeout) if args.backend == "gemini_api" else 0,
        "api_retries": int(args.api_retries) if args.backend == "gemini_api" else 0,
        "api_retry_backoff_seconds": float(args.api_retry_backoff) if args.backend == "gemini_api" else 0.0,
        "step_limit_multiplier": float(args.step_limit_multiplier),
        "api_display_width": int(args.api_display_width) if args.backend == "gemini_api" else 0,
        "api_display_height": int(args.api_display_height) if args.backend == "gemini_api" else 0,
        "allowed_angles": ",".join(str(x) for x in global_allowed_angles),
        "save_llm_debug": bool(args.save_llm_debug) if args.backend == "gemini_api" else False,
        "llm_debug_dir": _rel_path(llm_debug_root, root) if (args.save_llm_debug and args.backend == "gemini_api") else "",
    }
    if debug_timing:
        timing_path = output_reports / "step_02_timing.json"
        timing_report["aggregate"] = {
            k: round(v, 6) if isinstance(v, float) else v
            for k, v in timing_report["aggregate"].items()
        }
        write_json(timing_path, timing_report)
        summary["timing_report"] = _rel_path(timing_path, root)
        agg = timing_report["aggregate"]
        print(
            "[timing][step02][summary] "
            f"task_setup_s={agg['task_setup_s']:.4f} "
            f"target_render_s={agg['target_render_s']:.4f} "
            f"target_encode_s={agg['target_encode_s']:.4f} "
            f"step_render_s={agg['step_render_s']:.4f} "
            f"step_iou_s={agg['step_iou_s']:.4f} "
            f"step_frame_io_s={agg['step_frame_io_s']:.4f} "
            f"step_encode_s={agg['step_encode_s']:.4f} "
            f"step_policy_api_s={agg['step_policy_api_s']:.4f} "
            f"step_policy_sim_s={agg['step_policy_sim_s']:.4f} "
            f"step_event_write_s={agg['step_event_write_s']:.4f} "
            f"step_apply_action_s={agg['step_apply_action_s']:.4f} "
            f"step_total_s={agg['step_total_s']:.4f}"
        )
    write_json(output_reports / "step_02_summary.json", summary)
    print(json.dumps(summary, ensure_ascii=False, indent=2))


if __name__ == "__main__":
    main()