File size: 112,016 Bytes
bc50ea0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""CPU smoke test for H3 chain timing, segments, checkpoints and resume.

Uses the adjacent ComfyUI checkout for its real node/runtime modules, but no
model or GPU.  It encodes two tiny H.264 segments, resumes clip 2 from clip 1's
safetensors checkpoint, and assembles both source-track and generated-audio
outputs with ffmpeg.
"""

import asyncio
import hashlib
import importlib.util
import json
import math
import os
import pathlib
import subprocess
import sys
import tempfile
import threading
import time
import wave
from datetime import datetime


ROOT = pathlib.Path(__file__).resolve().parents[1]
COMFY_CANDIDATES = [ROOT.parent / "Comfyui", ROOT.parent / "ComfyUI"]
if os.environ.get("COMFYUI_PATH"):
    COMFY_CANDIDATES.insert(0, pathlib.Path(os.environ["COMFYUI_PATH"]))
COMFY = next((path for path in COMFY_CANDIDATES
              if (path / "comfy" / "options.py").is_file()), None)
if COMFY is None:
    raise SystemExit("adjacent ComfyUI checkout not found")

sys.path.insert(0, str(COMFY))
sys.argv = ["h3-chain-smoke", "--cpu"]
import comfy.options  # noqa: E402

comfy.options.enable_args_parsing()
import folder_paths  # noqa: E402
import torch  # noqa: E402
import execution  # noqa: E402
import nodes as comfy_nodes  # noqa: E402
from PIL import Image as PILImage  # noqa: E402
from safetensors import safe_open  # noqa: E402


def load_package():
    spec = importlib.util.spec_from_file_location(
        "h3_chain_smoke_package",
        ROOT / "__init__.py",
        submodule_search_locations=[str(ROOT)],
    )
    package = importlib.util.module_from_spec(spec)
    sys.modules[spec.name] = package
    spec.loader.exec_module(package)
    return package, sys.modules[spec.name + ".chain_nodes"]


def av_latent(video_t=1, audio_t=9):
    return {
        "samples": [
            torch.zeros((1, 16, video_t, 2, 2), dtype=torch.float32),
            torch.zeros((1, 32, 2, audio_t), dtype=torch.float32),
        ]
    }


def audio_for_frames(frames, sample_rate=8000):
    samples = round(frames / 24.0 * sample_rate)
    return {
        "waveform": torch.zeros((1, 2, samples), dtype=torch.float32),
        "sample_rate": sample_rate,
    }


class FakeDynamicPrompt:
    def __init__(self, prompt):
        self.prompt = prompt

    def get_node(self, node_id):
        return self.prompt[str(node_id)]

    def get_display_node_id(self, node_id):
        return str(node_id)

    def get_original_prompt(self):
        return self.prompt


def main():
    package, chain = load_package()

    segment_prompt = {
        "26": {"class_type": "MiniMaxH3ChainSegmentSave", "inputs": {}},
        "27": {"class_type": "MiniMaxH3ChainReview", "inputs": {
            "segment": ["26", 0],
        }},
    }
    assert chain._has_downstream_review_gate(
        FakeDynamicPrompt(segment_prompt), "26")
    segment_prompt["27"]["inputs"]["segment"] = ["26", 1]
    assert not chain._has_downstream_review_gate(
        FakeDynamicPrompt(segment_prompt), "26")
    segment_prompt["27"]["class_type"] = "MiniMaxH3ChainLoopEnd"
    segment_prompt["27"]["inputs"]["segment"] = ["26", 0]
    assert not chain._has_downstream_review_gate(
        FakeDynamicPrompt(segment_prompt), "26")

    cleanup_input = chain.MiniMaxH3ChainLoopEnd.INPUT_TYPES()[
        "optional"]["between_scene_cleanup"]
    assert cleanup_input[0] == ["off", "unload_models", "fresh_scene"]
    cleanup_calls = []
    import comfy.memory_management as memory_management
    import comfy.model_management as model_management
    original_free_pins = model_management.free_pins
    original_unload = model_management.unload_all_models
    original_empty_cache = model_management.soft_empty_cache
    original_cleanup_models = model_management.cleanup_models_gc
    original_ram_release = memory_management.extra_ram_release
    original_collect = chain.gc.collect
    model_management.free_pins = lambda size, evict_active=False: (
        cleanup_calls.append(("pins", size, evict_active)) or 12_345)
    model_management.unload_all_models = lambda: cleanup_calls.append(
        ("unload",))
    model_management.soft_empty_cache = lambda force=False: cleanup_calls.append(
        ("empty", force))
    model_management.cleanup_models_gc = lambda: cleanup_calls.append(
        ("models_gc",))
    memory_management.extra_ram_release = (
        lambda target, free_active=False: (
            cleanup_calls.append(("ram", target, free_active)) or 54_321))
    chain.gc.collect = lambda: cleanup_calls.append(("gc",)) or 7
    try:
        untouched = chain._release_loop_boundary_resources("off", 1)
        assert cleanup_calls == [] and untouched["policy"] == "off"

        unloaded = chain._release_loop_boundary_resources(
            "unload_models", 1)
        assert cleanup_calls == [("unload",), ("empty", True)]
        assert unloaded["pinned_bytes"] == 0
        assert unloaded["cache_bytes"] == 0

        cleanup_calls.clear()
        fresh = chain._release_loop_boundary_resources("fresh_scene", 2)
        assert cleanup_calls == [
            ("ram", sys.maxsize, True),
            ("gc",),
            ("models_gc",),
            ("pins", sys.maxsize, True),
            ("unload",),
            ("empty", True),
        ]
        assert fresh["pinned_bytes"] == 12_345
        assert fresh["cache_bytes"] == 54_321
        assert fresh["collected_objects"] == 7
        try:
            chain._release_loop_boundary_resources("erase_everything", 2)
        except ValueError as exc:
            assert "between_scene_cleanup" in str(exc)
        else:
            raise AssertionError("unknown loop cleanup policy was accepted")
    finally:
        model_management.free_pins = original_free_pins
        model_management.unload_all_models = original_unload
        model_management.soft_empty_cache = original_empty_cache
        model_management.cleanup_models_gc = original_cleanup_models
        memory_management.extra_ram_release = original_ram_release
        chain.gc.collect = original_collect
    print("loop cleanup: cache-first eviction and model unload passed")

    fixed_now = datetime(2026, 8, 11, 14, 5, 9)
    assert chain._expand_filename_date(
        "render_%date:yyyy-MM-dd%_%hour%-%minute%-%second%", fixed_now
    ) == "render_2026-08-11_14-05-09"
    with tempfile.TemporaryDirectory() as version_dir:
        original = pathlib.Path(version_dir) / "render.mp4"
        original.touch()
        (pathlib.Path(version_dir) / "render_001.mp4").touch()
        assert chain._available_versioned_path(str(original)).endswith(
            "render_002.mp4"
        )
    print("assemble filenames: date expansion and collision versioning passed")

    async def review_route_check():
        token = "review-route-smoke"
        future = asyncio.get_running_loop().create_future()
        review_plan = package.NODE_CLASS_MAPPINGS[
            "MiniMaxH3ChainPlan"]().build(
                json.dumps({
                    "prompt_prefix": "Shared only.",
                    "shots": [{
                        "id": "review_route",
                        "prompt": "Original scene prompt.",
                        "length": 22,
                        "steps": 8,
                        "seed": "7",
                    }],
                }),
                "review_route_smoke", "", 64, 64, 22,
                "video", "head", "disabled", "generated_audio", 22,
                1.0, 8, 0, 18, 0, "guide")[0]
        chain._PENDING_REVIEWS[token] = {
            "future": future,
            "loop": asyncio.get_running_loop(),
            "public": {
                "token": token,
                "prompt_prefix": "Shared only.",
                "clip_index": 1,
            },
            "plan": review_plan,
            "current_seed": 7,
            "current_length": 22,
        }

        class Request:
            async def json(self):
                return {
                    "token": token,
                    "action": "retry",
                    "scene_prompt": "",
                    "seed": "18446744073709551615",
                }

        try:
            response = await chain._submit_review_decision(Request())
            assert response.status == 200
            decision = await future
            assert decision["action"] == "retry"
            assert decision["scene_prompt"] == ""
            assert decision["seed"] == 18446744073709551615
        finally:
            chain._PENDING_REVIEWS.pop(token, None)

    asyncio.run(review_route_check())
    print("review: async decision route preserves exact uint64 seeds")
    required = {
        "MiniMaxH3ChainPlan", "MiniMaxH3ChainScenePromptEditor",
        "MiniMaxH3ChainRunManager",
        "MiniMaxH3ChainFirstSceneImage",
        "MiniMaxH3ReferenceVideoPrepare",
        "MiniMaxH3ScheduledPictureReference",
        "MiniMaxH3ScheduledVideoReference",
        "MiniMaxH3ScheduledAudioReference",
        "MiniMaxH3ScheduledReferenceToVideo",
        "MiniMaxH3TaggedSceneOptions",
        "MiniMaxH3CurrentTaggedScenePack",
        "MiniMaxH3CurrentTaggedReferenceScene",
        "MiniMaxH3SceneDataExtract",
        "MiniMaxH3ChainExternalVideo",
        "MiniMaxH3ChainLoopStart",
        "MiniMaxH3ChainCurrent", "MiniMaxH3ChainContext",
        "MiniMaxH3DriftControlModelPatch",
        "MiniMaxH3ChainSegmentSave", "MiniMaxH3ChainLoopEnd",
        "MiniMaxH3ChainManifestLoad", "MiniMaxH3ChainExportPNG",
        "MiniMaxH3ChainLatentVideoAdapter",
        "MiniMaxH3ChainAssemble",
        "MiniMaxH3LoopTrim",
        "MiniMaxH3ContexLoopSeamProbe",
        "MiniMaxH3ContexTrimSourceAV",
        "MiniMaxH3ContexMaskedTarget",
        "MiniMaxH3ContexMaskGridPreview",
        "MiniMaxH3ContexMasterAudioMaskedAV",
    }
    assert required <= set(package.NODE_CLASS_MAPPINGS)
    upstream_ids = {
        "MiniMaxH3MotionContext",
        "MiniMaxH3MotionContextTrim",
        "MiniMaxH3MotionContextSaveLatent",
        "MiniMaxH3MotionContextLoadLatent",
        "MiniMaxH3SongMaskedAVContext",
        "MiniMaxH3ContexSongMaskedAVContext",
    }
    assert not upstream_ids.intersection(package.NODE_CLASS_MAPPINGS)
    retired_mask_pack_ids = {
        "MiniMaxH3TrimSourceAV",
        "MiniMaxH3PerRowMaskPatch",
        "MiniMaxH3SetGenerationMask",
        "MiniMaxH3MaskGridPreview",
    }
    assert not retired_mask_pack_ids.intersection(package.NODE_CLASS_MAPPINGS)
    layout_patch = sys.modules[package.__name__ + ".patch_layout"]
    payload_patch = sys.modules[package.__name__ + ".patch_payload"]
    for module in (layout_patch, payload_patch):
        assert module.MC_KEY == "motion_context_index"
        assert module.MC_AUDIO_KEY == "motion_context_audio_end_frame"
    assert layout_patch.PATCH_MARKER == "_h3_motion_context_layout_patch"
    assert payload_patch.PATCH_MARKER == "_h3_motion_context_payload_patch"
    print("coexistence: node ids are disjoint and runtime patch ABI is shared")
    assert package.WEB_DIRECTORY == "./web"
    assert (ROOT / "web" / "h3_chain_plan_editor.js").is_file()
    assert (ROOT / "web" / "h3_chain_plan_core.mjs").is_file()
    assert (ROOT / "web" / "h3_prompt_completion_core.mjs").is_file()
    assert (ROOT / "web" / "h3_chain_cancel_reroll.js").is_file()
    assert (ROOT / "web" / "h3_chain_cancel_reroll_core.mjs").is_file()
    assert (ROOT / "web" / "h3_chain_scene_prompt_editor.js").is_file()
    assert (ROOT / "web" / "h3_reference_autoconnect.js").is_file()
    assert (ROOT / "web" / "h3_reference_autoconnect_core.mjs").is_file()
    assert (ROOT / "web" / "h3_scene_data_extract.js").is_file()
    assert (ROOT / "web" / "h3_scene_data_core.mjs").is_file()
    workflow_path = (ROOT / "example_workflows" / "Archive" /
                     "Looping Seamless Chain Global Refs Example - MiniMax H3.json")
    workflow = json.loads(workflow_path.read_text(encoding="utf-8"))
    workflow_types = {node.get("type") for node in workflow["nodes"]}
    assert "MiniMaxH3LoopTrim" in workflow_types
    assert not upstream_ids.intersection(workflow_types)
    loop_nodes = [
        node for node in workflow["nodes"]
        if node.get("type") == "MiniMaxH3LoopTrim"
        or str(node.get("type", "")).startswith("MiniMaxH3Chain")
    ]
    assert loop_nodes and all(
        node.get("properties", {}).get("aux_id") ==
        "ethanfel/ComfyUI-MiniMaxH3-Contex-Loop"
        for node in loop_nodes)
    workflow_start = next(
        node for node in workflow["nodes"]
        if node.get("type") == "MiniMaxH3ChainLoopStart")
    assert workflow_start.get("widgets_values") == [1, ""]
    assert "SEGMENT" not in str(workflow_start.get("title", "")).upper()
    print("workflow: loop node ids and package metadata use the new namespace")

    def assert_workflow_links(payload):
        nodes = {int(node["id"]): node for node in payload["nodes"]}
        links = {int(link[0]): link for link in payload["links"]}
        assert len(nodes) == len(payload["nodes"])
        assert len(links) == len(payload["links"])
        for link_id, link in links.items():
            _, origin_id, origin_slot, target_id, target_slot, _ = link
            origin = nodes[int(origin_id)]
            target = nodes[int(target_id)]
            assert link_id in (origin["outputs"][int(origin_slot)].get("links") or [])
            assert target["inputs"][int(target_slot)].get("link") == link_id
        for node in nodes.values():
            for input_socket in node.get("inputs", []):
                link_id = input_socket.get("link")
                assert link_id is None or int(link_id) in links
            for output_socket in node.get("outputs", []):
                for link_id in output_socket.get("links") or []:
                    assert int(link_id) in links

    fl2va_path = (ROOT / "example_workflows" / "Archive" /
                  "Looping Core FL2VA V2 - MiniMax H3.json")
    fl2va = json.loads(fl2va_path.read_text(encoding="utf-8"))
    assert_workflow_links(fl2va)
    fl2va_types = {node.get("type") for node in fl2va["nodes"]}
    assert {
        "MiniMaxH3ImageToVideo",
        "MiniMaxH3ChainScenePromptEditor",
        "MiniMaxH3ChainReview",
    } <= fl2va_types
    assert not any(str(value).startswith("MiniMaxH3Scheduled")
                   for value in fl2va_types)
    assert "LoadAudio" not in fl2va_types
    fl_plan_node = next(node for node in fl2va["nodes"]
                        if node.get("type") == "MiniMaxH3ChainPlan")
    fl_plan = json.loads(fl_plan_node["widgets_values"][0])
    assert len(fl_plan["shots"]) == 1
    assert fl_plan["shots"][0]["length"] == 124
    fl_prompt = "\n".join(fl_plan["shots"][0]["prompt"])
    assert fl_prompt.startswith(
        "How the reference pictures align with the target video")
    fl_sections = [
        "integrated_multimodal_description:",
        "overall_soundscape:",
        "non_diegetic_music:",
    ]
    assert [fl_prompt.index(value) for value in fl_sections] == sorted(
        fl_prompt.index(value) for value in fl_sections)
    assert fl_plan_node["widgets_values"][9] == "generated_audio"
    fl_conditioner = next(node for node in fl2va["nodes"]
                          if node.get("type") == "MiniMaxH3ImageToVideo")
    assert {input_socket["name"] for input_socket in fl_conditioner["inputs"]
            if input_socket.get("link") is not None} >= {
        "first_frame", "last_frame", "prompt", "width", "height", "length",
    }
    fl_assemble = next(node for node in fl2va["nodes"]
                       if node.get("type") == "MiniMaxH3ChainAssemble"
                       and node.get("mode", 0) == 0)
    assert "%date:yyyy-MM-dd%" in fl_assemble["widgets_values"][1]
    print("workflow v2: scheduler-free core FL2VA editing/review graph passes")

    i2va_path = (ROOT / "example_workflows" / "Archive" /
                  "Looping Single Image I2VA 20s V2 - MiniMax H3.json")
    i2va = json.loads(i2va_path.read_text(encoding="utf-8"))
    assert_workflow_links(i2va)
    i2va_types = {node.get("type") for node in i2va["nodes"]}
    assert {
        "MiniMaxH3ImageToVideo",
        "MiniMaxH3ChainFirstSceneImage",
        "MiniMaxH3ChainScenePromptEditor",
        "MiniMaxH3ChainReview",
    } <= i2va_types
    assert "LoadAudio" not in i2va_types
    i2va_plan_node = next(node for node in i2va["nodes"]
                           if node.get("type") == "MiniMaxH3ChainPlan")
    i2va_plan = package.NODE_CLASS_MAPPINGS["MiniMaxH3ChainPlan"]().build(
        *i2va_plan_node["widgets_values"])[0]
    assert [shot["raw_frames"] for shot in i2va_plan["shots"]] == [243, 243]
    assert [shot["delivered_frames"] for shot in i2va_plan["shots"]] == [243, 238]
    assert i2va_plan["total_delivered_frames"] == 481
    i2va_conditioner = next(node for node in i2va["nodes"]
                            if node.get("type") == "MiniMaxH3ImageToVideo")
    i2va_inputs = {item["name"]: item.get("link")
                    for item in i2va_conditioner["inputs"]}
    assert i2va_inputs["first_frame"] is not None
    assert i2va_inputs["last_frame"] is None
    print("workflow v2: gated two-scene I2VA 20-second graph passes")

    scheduled_path = (ROOT / "example_workflows" / "Archive" /
                      "Looping Seamless Chain V2 - Scheduled Refs - MiniMax H3.json")
    scheduled = json.loads(scheduled_path.read_text(encoding="utf-8"))
    assert_workflow_links(scheduled)
    scheduled_types = {node.get("type") for node in scheduled["nodes"]}
    assert {
        "MiniMaxH3ScheduledPictureReference",
        "MiniMaxH3ScheduledVideoReference",
        "MiniMaxH3ScheduledAudioReference",
        "MiniMaxH3ScheduledReferenceToVideo",
        "MiniMaxH3ReferenceVideoPrepare",
        "MiniMaxH3ChainScenePromptEditor",
        "MiniMaxH3ChainReview",
    } <= scheduled_types
    assert "MiniMaxH3ReferenceToVideo" not in scheduled_types
    scheduled_plan_node = next(
        node for node in scheduled["nodes"]
        if node.get("type") == "MiniMaxH3ChainPlan")
    scheduled_plan = json.loads(scheduled_plan_node["widgets_values"][0])
    assert len(scheduled_plan["shots"]) == 14
    for index, shot in enumerate(scheduled_plan["shots"], start=1):
        text = "\n".join(shot["prompt"])
        assert "@hero_look" in text and "@song" in text
        assert any(line.startswith("@song is ") for line in shot["prompt"])
        assert ("@hero_face" in text) == (index <= 7)
        assert ("@performance" in text) == (4 <= index <= 6)
        assert (any(line.startswith("@performance provides ")
                    for line in shot["prompt"]) == (4 <= index <= 6))
        assert "<Picture 1>" not in text and "<Picture 2>" not in text
        assert "<Audio 1>" not in text
    picture_nodes = {
        node["widgets_values"][0]: node for node in scheduled["nodes"]
        if node.get("type") == "MiniMaxH3ScheduledPictureReference"
    }
    assert picture_nodes["hero_face"]["widgets_values"][1] == "1:7"
    assert picture_nodes["hero_look"]["widgets_values"][1] == "all"
    assert all(len(node["widgets_values"]) == 2
               for node in picture_nodes.values())
    video_schedule = next(
        node for node in scheduled["nodes"]
        if node.get("type") == "MiniMaxH3ScheduledVideoReference")
    assert video_schedule["widgets_values"][:2] == ["performance", "4:6"]
    assert video_schedule["widgets_values"][2] == "performance_audio"
    assert len(video_schedule["widgets_values"]) == 3
    audio_schedule = next(
        node for node in scheduled["nodes"]
        if node.get("type") == "MiniMaxH3ScheduledAudioReference")
    assert audio_schedule["widgets_values"][:2] == ["song", "all"]
    assert len(audio_schedule["widgets_values"]) == 2
    demo_schedule = chain._make_reference_schedule([
        {
            "kind": "picture", "tag": "hero_face", "scenes": "1:7",
            "ranges": ((1, 7),), "value": object(), "content_hash": "face",
        },
        {
            "kind": "picture", "tag": "hero_look", "scenes": "all",
            "ranges": (), "value": object(), "content_hash": "look",
        },
        {
            "kind": "video", "tag": "performance", "scenes": "4:6",
            "ranges": ((4, 6),), "value": object(), "audio": object(),
            "audio_tag": "performance_audio", "content_hash": "video",
            "audio_hash": "paired-audio",
        },
        {
            "kind": "audio", "tag": "song", "scenes": "all",
            "ranges": (), "value": object(), "content_hash": "song",
        },
    ])
    for scene in (1, 4, 8):
        source = "\n".join(scheduled_plan["shots"][scene - 1]["prompt"])
        compiled_demo, _mapping, _bindings = (
            chain._compile_scheduled_reference_prompt(
                demo_schedule, scene, 14, source))
        assert "@hero" not in compiled_demo
        assert "@performance" not in compiled_demo
        assert "@song" not in compiled_demo
        assert "{ref}" not in compiled_demo
        assert "defines <Subject 1>" not in compiled_demo
        assert "for scenes 1-7" not in compiled_demo
        assert compiled_demo.startswith("subject_definitions:\n<Subject 1>")
        if scene == 1:
            assert "<Picture 1>" in compiled_demo
            assert "<Picture 2>" in compiled_demo
            assert "<Audio 1> is the current frame-exact" in compiled_demo
        elif scene == 4:
            assert "<Video 1> provides a weak reference" in compiled_demo
            assert "<Audio 1> is the synchronized soundtrack" in compiled_demo
            assert "<Audio 2> is the current frame-exact" in compiled_demo
        else:
            assert "defined by <Picture 1>" in compiled_demo
            assert "<Picture 2>" not in compiled_demo
            assert "<Audio 1> is the current frame-exact" in compiled_demo
    scheduled_links = {int(link[0]): link for link in scheduled["links"]}
    fingerprint_input = next(
        item for item in scheduled_plan_node["inputs"]
        if item["name"] == "generation_fingerprint")
    fingerprint_link = scheduled_links[int(fingerprint_input["link"])]
    assert fingerprint_link[1] == video_schedule["id"]
    current_node = next(node for node in scheduled["nodes"]
                        if node.get("type") == "MiniMaxH3ChainCurrent")
    current_audio_link = next(
        item for item in current_node["outputs"]
        if item["name"] == "source_audio_slice")
    assert len(current_audio_link["links"]) == 1
    assert scheduled_links[current_audio_link["links"][0]][3] == audio_schedule["id"]
    scheduled_assemble = next(
        node for node in scheduled["nodes"]
        if node.get("type") == "MiniMaxH3ChainAssemble"
        and node.get("mode", 0) == 0)
    assert "%date:yyyy-MM-dd%" in scheduled_assemble["widgets_values"][1]
    print("workflow v2: scheduled picture/video/audio aliases and review graph pass")

    angle_workflow_path = (ROOT / "example_workflows" / "Archive" /
                           "Three-Angle Guitar Ref2VA - EXPERIMENTAL - MiniMax H3.json")
    angle_workflow = json.loads(
        angle_workflow_path.read_text(encoding="utf-8"))
    angle_types = {node.get("type") for node in angle_workflow["nodes"]}
    assert "MiniMaxH3ReferenceVideoPrepare" in angle_types
    assert "MiniMaxH3ReferenceToVideo" in angle_types
    assert "MiniMaxH3LoopTrim" in angle_types
    assert not any(str(value).startswith("MiniMaxH3ChainLoop")
                   for value in angle_types)
    angle_loader = next(node for node in angle_workflow["nodes"]
                        if node.get("type") == "LoadVideo")
    angle_prep = next(node for node in angle_workflow["nodes"]
                      if node.get("type") == "MiniMaxH3ReferenceVideoPrepare")
    angle_ref = next(node for node in angle_workflow["nodes"]
                     if node.get("type") == "MiniMaxH3ReferenceToVideo")
    assert angle_loader["widgets_values"][0] == "3ClbaJYWVO4_000030.mp4"
    assert angle_prep["widgets_values"] == [209, 24.0]
    prompt = angle_ref["widgets_values"][0]
    sections = ["subject_definitions:", "summary:", "retention_analysis:",
                "detailed_description:", "overall_soundscape:",
                "non_diegetic_music:"]
    positions = [prompt.index(section) for section in sections]
    assert positions == sorted(positions)
    assert "exactly three shots" in prompt
    assert "Tera Echo product card" in prompt
    links = {int(link[0]): link for link in angle_workflow["links"]}
    prep_audio_links = next(
        output["links"] for output in angle_prep["outputs"]
        if output["name"] == "source_audio")
    assert len(prep_audio_links) == 2
    assert {links[link_id][3] for link_id in prep_audio_links} == {110, 132}
    print("workflow: one-pass three-angle Ref2VA copies source audio exactly")

    # Every public socket/widget should explain its role in the graph, and
    # every output should describe what it carries. This keeps newly added
    # controls from silently regressing to opaque ComfyUI labels.
    for node_name, node_class in package.NODE_CLASS_MAPPINGS.items():
        schema = node_class.INPUT_TYPES()
        for section in ("required", "optional"):
            for input_name, input_spec in schema.get(section, {}).items():
                options = input_spec[1] if len(input_spec) > 1 else {}
                assert isinstance(options, dict) and str(
                    options.get("tooltip", "")).strip(), (
                        "%s.%s has no tooltip" % (node_name, input_name))
        output_tooltips = getattr(node_class, "OUTPUT_TOOLTIPS", ())
        assert len(output_tooltips) == len(node_class.RETURN_TYPES), (
            "%s output tooltip count is %d; expected %d" %
            (node_name, len(output_tooltips), len(node_class.RETURN_TYPES)))
        assert all(str(value).strip() for value in output_tooltips), (
            "%s has an empty output tooltip" % node_name)
    print("tooltips: every public input and output is documented")
    review_inputs = chain.MiniMaxH3ChainReview.INPUT_TYPES()
    partial_audio_tooltip = review_inputs["required"][
        "partial_audio_source"][1]["tooltip"]
    legacy_source_tooltip = review_inputs["optional"][
        "source_audio"][1]["tooltip"]
    assert "Source Timeline audio carried in state" in partial_audio_tooltip
    assert "none creates a silent partial" in partial_audio_tooltip
    assert "state predates recoverable source audio" in legacy_source_tooltip
    assert "legacy AUDIO connected at Loop Start" in legacy_source_tooltip
    assert "does not affect generation" in legacy_source_tooltip

    readable_prompts = chain._normalize_plan(
        json.dumps({
            "prompt_prefix": ["Shared identity.", "", "Shared wardrobe."],
            "shots": [{
                "id": "multiline",
                "prompt": [
                    "Use <Picture 1> for her facial identity.",
                    "Throughout every scene S1 wears the same dress.",
                    "<Subject 2> enters from camera right.",
                ],
                "length": 39,
            }],
        }),
        "readable", 32, 32, 22, "video", "head", "disabled",
        "source_track", 0, 15, 2, 1, 30,
    )
    assert readable_prompts["shots"][0]["prompt"] == (
        "Shared identity.\n\nShared wardrobe.\n\n"
        "Use <Picture 1> for her facial identity.\n"
        "Throughout every scene S1 wears the same dress.\n"
        "<Subject 2> enters from camera right."
    )
    prompt_editor = package.NODE_CLASS_MAPPINGS[
        "MiniMaxH3ChainScenePromptEditor"]()
    assert prompt_editor.passthrough(readable_prompts)[0] is readable_prompts
    run_manager = package.NODE_CLASS_MAPPINGS["MiniMaxH3ChainRunManager"]()
    run_manager_result = run_manager.passthrough(
        readable_prompts, True, True, False, "[]")
    assert run_manager_result == (readable_prompts, None)
    opening_image = object()
    first_scene_gate = package.NODE_CLASS_MAPPINGS[
        "MiniMaxH3ChainFirstSceneImage"]()
    assert first_scene_gate.select({"index": 1}, opening_image)[:2] == (
        opening_image, True)
    assert first_scene_gate.select({"index": 2}, opening_image)[:2] == (
        None, False)
    last_target = object()
    assert first_scene_gate.select(
        {"index": 1}, opening_image, last_target)[3] is last_target
    assert first_scene_gate.select(
        {"index": 2}, opening_image, last_target)[3] is last_target
    assert "last-frame target supplied" in first_scene_gate.select(
        {"index": 2}, opening_image, last_target)[2]
    shared_only = chain._normalize_plan(
        json.dumps({
            "prompt_prefix": ["Shared identity.", "", "Shared direction."],
            "shots": [{"id": "shared_only", "prompt": "", "length": 39}],
        }),
        "shared_only", 32, 32, 22, "video", "head", "disabled",
        "source_track", 0, 15, 2, 1, 30,
    )
    assert shared_only["shots"][0]["scene_prompt"] == ""
    assert shared_only["shots"][0]["prompt"] == (
        "Shared identity.\n\nShared direction.")
    shared_only_revision = chain._plan_with_review_revision(
        shared_only, 1, "", 123)
    assert shared_only_revision["shots"][0]["scene_prompt"] == ""
    assert shared_only_revision["shots"][0]["prompt"] == (
        "Shared identity.\n\nShared direction.")
    try:
        chain._normalize_plan(
            json.dumps({"shots": [{"id": "empty", "prompt": ""}]}),
            "empty", 32, 32, 22, "video", "head", "disabled",
            "source_track", 0, 15, 2, 1, 30,
        )
    except ValueError as exc:
        assert "scene prompt or shared prompt" in str(exc)
    else:
        raise AssertionError("plan accepted an empty scene and shared prompt")
    numeric_seed_plan = chain._normalize_plan(
        '{"shots":[{"prompt":"seed test","seed":18446744073709551615}]}',
        "numeric_seed", 32, 32, 22, "video", "head", "disabled",
        "source_track", 0, 15, 2, 1, 30,
    )
    string_seed_plan = chain._normalize_plan(
        '{"shots":[{"prompt":"seed test","seed":"18446744073709551615"}]}',
        "numeric_seed", 32, 32, 22, "video", "head", "disabled",
        "source_track", 0, 15, 2, 1, 30,
    )
    assert numeric_seed_plan["shots"][0]["seed"] == chain.MAX_SEED
    assert numeric_seed_plan["plan_hash"] == string_seed_plan["plan_hash"]
    shorthand_defaults = chain._normalize_plan(
        '{"duration_seconds":8,"steps":10,'
        '"shots":[{"prompt":"top-level defaults"}]}',
        "shorthand_defaults", 32, 32, 22, "video", "head", "disabled",
        "source_track", 0, 15, 20, 1, 30,
    )
    assert shorthand_defaults["shots"][0]["raw_frames"] == 192
    assert shorthand_defaults["shots"][0]["steps"] == 10
    try:
        chain._normalize_plan(
            json.dumps({"shots": [{"prompt": ["valid", 42]}]}),
            "bad_lines", 32, 32, 22, "video", "head", "disabled",
            "source_track", 0, 15, 2, 1, 30,
        )
    except ValueError as exc:
        assert "only strings" in str(exc)
    else:
        raise AssertionError("prompt line array accepted a non-string item")
    print("prompts: multiline and shared-only scenes pass; fully empty prompts fail")

    assert chain._parse_scene_range("", 8, 1) == (1, 8)
    assert chain._parse_scene_range("3", 8, 1) == (3, 3)
    assert chain._parse_scene_range(" 3 : 8 ", 8, 1) == (3, 8)
    assert chain._parse_scene_range("", 8, 4) == (4, 8)
    for invalid in ("1,3", "3:2", "0:2", "2:9", "abc"):
        try:
            chain._parse_scene_range(invalid, 8, 1)
        except ValueError:
            pass
        else:
            raise AssertionError("scene_range accepted %r" % invalid)
    print("scene range: blank, single scene, and one inclusive range validated")

    assert chain._parse_reference_selector("") == ()
    assert chain._parse_reference_selector("all") == ()
    assert chain._parse_reference_selector(" 1, 3, 5:8 ") == (
        (1, 1), (3, 3), (5, 8))
    assert chain._parse_reference_selector("1,2:4,3,8") == (
        (1, 4), (8, 8))
    for invalid in ("0", "4:2", "1;3", "hello"):
        try:
            chain._parse_reference_selector(invalid)
        except ValueError:
            pass
        else:
            raise AssertionError(
                "reference selector accepted %r" % invalid)

    picture = torch.zeros((2, 8, 8, 3), dtype=torch.float32)
    video = torch.zeros((22, 8, 8, 3), dtype=torch.float32)
    paired_audio = audio_for_frames(22)
    voice_audio = audio_for_frames(22)
    picture_node = chain.MiniMaxH3ScheduledPictureReference()
    video_node = chain.MiniMaxH3ScheduledVideoReference()
    audio_node = chain.MiniMaxH3ScheduledAudioReference()
    picture_schedule = picture_node.add(
        picture, "@hero", "1,3,5:8")[0]
    video_schedule = video_node.add(
        video, "performance", "2:4", "performance_sound",
        audio=paired_audio, previous=picture_schedule)[0]
    schedule, schedule_fingerprint, _status = audio_node.add(
        voice_audio, "voice", "3", previous=video_schedule)
    assert chain._generation_fingerprint_value(schedule_fingerprint)[0] == (
        schedule["fingerprint"])
    assert len(schedule["entries"]) == 3
    assert schedule["entries"][0]["value"].shape[0] == 1

    source_prompt = (
        "subject_definitions:\n"
        "<Subject 1> follows @hero and @performance.\n"
        "@performance_sound is synchronized with @performance.\n"
        "@voice provides voice timing.\n\n"
        "summary:\n"
        "Use @performance_sound and @voice in scene 3."
    )
    compiled, active_summary, bindings = (
        chain._compile_scheduled_reference_prompt(
            schedule, 3, 8, source_prompt))
    assert active_summary == (
        "scene 3/8: @hero -> <Picture 1>; "
        "@performance_sound -> <Audio 1>; "
        "@performance -> <Video 1>; @voice -> <Audio 2>")
    assert compiled.startswith(
        "subject_definitions:\n"
        "<Subject 1> follows <Picture 1> and <Video 1>.\n"
        "<Audio 1> is synchronized with <Video 1>.\n"
        "<Audio 2> provides voice timing.\n")
    assert "@hero" not in compiled
    assert "@performance" not in compiled
    assert "@voice" not in compiled
    assert bindings["aliases"] == {
        "hero": "<Picture 1>",
        "performance_sound": "<Audio 1>",
        "performance": "<Video 1>",
        "voice": "<Audio 2>",
    }

    expanded = chain.MiniMaxH3ScheduledReferenceToVideo().apply(
        "clip", "video-vae", "audio-vae", schedule, 3, 8,
        source_prompt, 960, 544, 124, "match")
    graph_node = next(iter(expanded["expand"].values()))
    assert graph_node["class_type"] == "MiniMaxH3ReferenceToVideo"
    graph_inputs = graph_node["inputs"]
    assert graph_inputs["prompt"] == compiled
    assert graph_inputs["ref_images.ref_image_0"] is schedule[
        "entries"][0]["value"]
    assert graph_inputs["ref_videos.ref_video_0"] is video
    assert graph_inputs[
        "ref_video_audios.ref_video_audio_0"] is paired_audio
    assert graph_inputs["ref_audios.ref_audio_0"] is voice_audio
    assert expanded["result"][2:] == (
        compiled, active_summary, schedule_fingerprint)

    sequential_video = torch.arange(
        500, dtype=torch.float32).reshape(500, 1, 1, 1).expand(-1, 8, 8, 3)
    sequential_audio = {
        "waveform": torch.arange(
            5000, dtype=torch.float32).reshape(1, 1, 5000),
        "sample_rate": 240,
    }
    sequential_schedule = video_node.add(
        sequential_video, "motion", "", "motion_audio", "sequential",
        audio=sequential_audio)[0]
    sequential_state = {
        "index": 2,
        "plan": {"shots": [
            {"raw_frames": 243, "generation_start_frame": 0},
            {"raw_frames": 243, "generation_start_frame": 221},
        ]},
    }
    sequential_expanded = chain.MiniMaxH3ScheduledReferenceToVideo().apply(
        "clip", "video-vae", "audio-vae", sequential_schedule, 2, 2,
        "Use @motion and @motion_audio.", 960, 544, 243, "match",
        state=sequential_state)
    sequential_inputs = next(iter(
        sequential_expanded["expand"].values()))["inputs"]
    sequential_video_slice = sequential_inputs["ref_videos.ref_video_0"]
    sequential_audio_slice = sequential_inputs[
        "ref_video_audios.ref_video_audio_0"]
    assert float(sequential_video_slice[0, 0, 0, 0]) == 221
    assert float(sequential_video_slice[-1, 0, 0, 0]) == 463
    assert float(sequential_audio_slice["waveform"][0, 0, 0]) == 2210
    assert "@motion sequential frames 221:464" in (
        sequential_expanded["result"][3])

    first_picture_schedule = picture_node.add(
        picture, "picture_1", "1")[0]
    renumbering_schedule = picture_node.add(
        picture, "picture_2", "",
        previous=first_picture_schedule)[0]
    scene_one_compiled, scene_one_summary, _ = (
        chain._compile_scheduled_reference_prompt(
            renumbering_schedule, 1, 2,
            "Use @picture_2 as the second identity reference.\n"
            "Follow @picture_2."))
    assert scene_one_summary == (
        "scene 1/2: @picture_1 -> <Picture 1>; "
        "@picture_2 -> <Picture 2>")
    assert "Use <Picture 2> as the second identity reference." in (
        scene_one_compiled)
    assert "Follow <Picture 2>." in scene_one_compiled
    scene_two_compiled, scene_two_summary, _ = (
        chain._compile_scheduled_reference_prompt(
            renumbering_schedule, 2, 2,
            "Use @picture_2 as the second identity reference.\n"
            "Follow @picture_2."))
    assert scene_two_summary == "scene 2/2: @picture_2 -> <Picture 1>"
    assert "Use <Picture 1> as the second identity reference." in (
        scene_two_compiled)
    assert "Follow <Picture 1>." in scene_two_compiled

    picture_only = picture_node.add(
        picture, "single", "1")[0]
    unreferenced = chain.MiniMaxH3ScheduledReferenceToVideo().apply(
        "clip", "video-vae", "audio-vae", picture_only, 2, 2,
        "A text-only second scene.", 960, 544, 124, "match")
    unreferenced_inputs = next(iter(unreferenced["expand"].values()))[
        "inputs"]
    assert not any(
        key.startswith(("ref_images.", "ref_videos.",
                        "ref_video_audios.", "ref_audios."))
        for key in unreferenced_inputs)
    assert unreferenced["result"][3] == (
        "scene 2/2: no scheduled references")
    try:
        chain._compile_scheduled_reference_prompt(
            picture_only, 2, 2, "Use @single here.")
    except ValueError as exc:
        assert "not active in scene 2" in str(exc)
    else:
        raise AssertionError("compiler accepted an inactive reference tag")
    try:
        chain._compile_scheduled_reference_prompt(
            picture_only, 1, 2, "Use @unknown here.")
    except ValueError as exc:
        assert "unknown scheduled reference tag" in str(exc)
    else:
        raise AssertionError("compiler accepted an unknown reference tag")
    warning_prompt, warning_summary, warning_bindings = (
        chain._compile_scheduled_reference_prompt(
            picture_only, 1, 2,
            "Keep @unknown and @unknown; resolve @single.",
            compliance_mode="soft"))
    assert warning_prompt == (
        "Keep @unknown and @unknown; resolve <Picture 1>.")
    assert len(warning_bindings["compliance_warnings"]) == 1
    assert "unknown scheduled reference tag @unknown" in warning_summary
    inactive_prompt, _, inactive_bindings = (
        chain._compile_scheduled_reference_prompt(
            picture_only, 2, 2, "Keep @single here.",
            compliance_mode="soft"))
    assert inactive_prompt == "Keep @single here."
    assert "not active in scene 2" in (
        inactive_bindings["compliance_warnings"][0])
    disabled_prompt, disabled_summary, disabled_bindings = (
        chain._compile_scheduled_reference_prompt(
            picture_only, 1, 2, "Leave @single and @unknown unchanged.",
            compliance_mode="disabled"))
    assert disabled_prompt == "Leave @single and @unknown unchanged."
    assert disabled_bindings["compliance_warnings"] == []
    assert "prompt compliance disabled" in disabled_summary
    try:
        audio_node.add(
            voice_audio, "hero", "", previous=picture_schedule)
    except ValueError as exc:
        assert "already in this chain" in str(exc)
    else:
        raise AssertionError("scheduler accepted a duplicate tag")
    try:
        video_node.add(
            video, "same", "1", "same", audio=paired_audio)
    except ValueError as exc:
        assert "must be different" in str(exc)
    else:
        raise AssertionError(
            "video scheduler accepted the same video and audio tag")
    try:
        chain._active_reference_bindings(picture_schedule, 1, 4)
    except ValueError as exc:
        assert "exceeds this plan's 4 scenes" in str(exc)
    else:
        raise AssertionError("scheduler accepted an out-of-plan selector")

    tagged_picture_node = chain.MiniMaxH3TaggedPictureReference()
    tagged_video_node = chain.MiniMaxH3TaggedVideoReference()
    tagged_audio_node = chain.MiniMaxH3TaggedAudioReference()
    assert "scenes" not in tagged_picture_node.INPUT_TYPES()["required"]
    assert "scenes" not in tagged_video_node.INPUT_TYPES()["required"]
    assert "scenes" not in tagged_audio_node.INPUT_TYPES()["required"]
    tagged = tagged_picture_node.add(picture, "hero_face")[0]
    tagged = tagged_picture_node.add(
        picture, "hero_look", previous=tagged)[0]
    tagged = tagged_audio_node.add(
        voice_audio, "voice", previous=tagged)[0]
    assert tagged["activation"] == "prompt"
    assert all(entry["activation"] == "prompt"
               for entry in tagged["entries"])
    tagged_prompt = (
        "<Subject 1> @S1 follows @hero_look. "
        "@voice defines vocal identity. @unregistered stays user-managed.")
    tagged_compiled, tagged_summary, tagged_bindings = (
        chain._compile_tagged_reference_prompt(
            tagged, 2, 4, tagged_prompt))
    assert tagged_compiled == (
        "<Subject 1> @S1 follows <Picture 1>. "
        "<Audio 1> defines vocal identity. @unregistered stays user-managed.")
    assert tagged_summary == (
        "scene 2/4: @hero_look -> <Picture 1>; @voice -> <Audio 1>")
    assert [entry["tag"] for entry in tagged_bindings["pictures"]] == [
        "hero_look"]
    assert [entry["tag"] for entry in tagged_bindings["audios"]] == [
        "voice"]
    assert "hero_face" not in tagged_bindings["aliases"]

    tagged_expanded = chain.MiniMaxH3TaggedReferenceToVideo().apply(
        "clip", "video-vae", "audio-vae", tagged, 2, 4,
        tagged_prompt, 960, 544, 124, "match")
    tagged_inputs = next(iter(tagged_expanded["expand"].values()))["inputs"]
    assert tagged_inputs["prompt"] == tagged_compiled
    assert tagged_inputs["ref_images.ref_image_0"] is tagged[
        "entries"][1]["value"]
    assert tagged_inputs["ref_audios.ref_audio_0"] is voice_audio
    assert "ref_images.ref_image_1" not in tagged_inputs
    assert chain._generation_fingerprint_value(
        tagged_expanded["result"][4])[0] == tagged["fingerprint"]

    tagged_motion = tagged_video_node.add(
        sequential_video, "motion", "motion_audio", "sequential",
        audio=sequential_audio)[0]
    prompt_driven_state = {
        "index": 3,
        "plan": {"shots": [
            {"raw_frames": 243, "generation_start_frame": 0,
             "prompt": "No motion reference in this opening."},
            {"raw_frames": 243, "generation_start_frame": 221,
             "prompt": "Begin @motion."},
            {"raw_frames": 243, "generation_start_frame": 442,
             "prompt": "Continue using @motion_audio."},
        ]},
    }
    tagged_motion_expanded = chain.MiniMaxH3TaggedReferenceToVideo().apply(
        "clip", "video-vae", "audio-vae", tagged_motion, 3, 3,
        "Continue using @motion_audio.", 960, 544, 243, "match",
        state=prompt_driven_state)
    tagged_motion_inputs = next(iter(
        tagged_motion_expanded["expand"].values()))["inputs"]
    assert float(tagged_motion_inputs[
        "ref_videos.ref_video_0"][0, 0, 0, 0]) == 221
    assert float(tagged_motion_inputs[
        "ref_video_audios.ref_video_audio_0"]["waveform"][0, 0, 0]) == 2210
    assert "origin scene 2" in tagged_motion_expanded["result"][3]

    tagged_motion_role = chain.MiniMaxH3TaggedMotionReference().add(
        sequential_video, "performance", "<Subject 1> and <Subject 2>",
        "the source performer's pose sequence and action timing", "384", "",
        "restart_each_scene")[0]
    motion_role_prompt = (
        "subject_definitions:\n"
        "<Subject 1> is the target character.\n"
        "<Subject 2> is the target partner.\n\n"
        "detailed_description:\n"
        "[Shot 1] <Subject 1> performs @performance.")
    motion_role_compiled, motion_role_summary, motion_role_bindings = (
        chain._compile_tagged_reference_prompt(
            tagged_motion_role, 1, 1, motion_role_prompt))
    assert "<Subject 3> is the reusable pose, action, and motion from " \
           "<Video 1>" in motion_role_compiled
    assert "<Subject 1> performs <Subject 3>." in motion_role_compiled
    assert "without importing the source identity, wardrobe, setting, " \
           "lighting, or composition" in motion_role_compiled
    assert motion_role_bindings["aliases"]["performance"] == "<Subject 3>"
    assert "@performance -> <Subject 3> motion from <Video 1>" in \
           motion_role_summary
    motion_role_expanded = chain.MiniMaxH3TaggedReferenceToVideo().apply(
        "clip", "video-vae", "audio-vae", tagged_motion_role, 1, 1,
        motion_role_prompt, 960, 544, 243, "match")
    motion_role_inputs = next(iter(
        motion_role_expanded["expand"].values()))["inputs"]
    assert motion_role_inputs["ref_videos.ref_video_0"] is sequential_video
    assert motion_role_inputs["prompt"] == motion_role_compiled

    no_tag_compiled, no_tag_summary, no_tag_bindings = (
        chain._compile_tagged_reference_prompt(
            tagged, 1, 4, "A scene without registered aliases; keep @S1."))
    assert no_tag_compiled.endswith("keep @S1.")
    assert no_tag_bindings["pictures"] == []
    assert no_tag_bindings["audios"] == []
    assert no_tag_summary.endswith("no tagged references used by prompt")
    print("reference schedule: disjoint selectors, stable tags, native label "
          "compilation, strict/warning-only compliance, dynamic Ref2VA "
          "sockets, prompt-driven tagged references, and validation pass")

    # ComfyUI rounds H3's 40 Hz audio grid to the nearest step. Depending on
    # frame length, the decoded stream can land 1/3 step above or below the
    # exact 24 fps picture duration. Match Tail must frame-lock both cases by
    # tiny time-conformance rather than inserting a silence tail.
    trim_node = package.NODE_CLASS_MAPPINGS["MiniMaxH3LoopTrim"]()
    short_images = torch.zeros((260, 1, 1, 3), dtype=torch.float32)
    short_samples = 346400  # 433 audio steps; exact 260f target is 346667
    short_audio = {
        "waveform": torch.ones((1, 2, short_samples), dtype=torch.float32),
        "sample_rate": 32000,
    }
    _, padded, _, retained = trim_node.trim(
        short_images, 0, short_audio, 24.0, True)
    assert retained == 0
    assert int(padded["waveform"].shape[-1]) == 346667
    assert torch.all(padded["waveform"] > 0.99)
    chain._validate_audio(padded, "260-frame regression", expected_frames=260)

    long_images = torch.zeros((124, 1, 1, 3), dtype=torch.float32)
    long_samples = 165600  # 207 audio steps; exact 124f target is 165333
    long_audio = {
        "waveform": torch.ones((1, 2, long_samples), dtype=torch.float32),
        "sample_rate": 32000,
    }
    _, truncated, _, retained = trim_node.trim(
        long_images, 0, long_audio, 24.0, True)
    assert retained == 0
    assert int(truncated["waveform"].shape[-1]) == 165333
    assert torch.all(truncated["waveform"] > 0.99)
    numbered = torch.arange(10, dtype=torch.float32).reshape(10, 1, 1, 1)
    delivered, _, with_overlap, retained = trim_node.trim(
        numbered, 4, retain_overlap_frames=2)
    assert delivered[:, 0, 0, 0].tolist() == list(range(4, 10))
    assert with_overlap[:, 0, 0, 0].tolist() == list(range(2, 10))
    assert retained == 2
    delivered, _, with_overlap, retained = trim_node.trim(
        numbered, 4, retain_overlap_frames=99)
    assert delivered[:, 0, 0, 0].tolist() == list(range(4, 10))
    assert with_overlap[:, 0, 0, 0].tolist() == list(range(10))
    assert retained == 4
    scene_state = {
        "index": 2,
        "plan": {
            "shots": [{}, {"video_blend_frames": 3}],
            "compatibility": {"video_blend_frames": 0},
        },
    }
    delivered, _, with_overlap, retained = trim_node.trim(
        numbered, 4, retain_overlap_frames=0, state=scene_state)
    assert delivered[:, 0, 0, 0].tolist() == list(range(4, 10))
    assert with_overlap[:, 0, 0, 0].tolist() == list(range(1, 10))
    assert retained == 3
    first_scene_state = {
        "index": 1,
        "plan": {
            "shots": [{
                "raw_frames": 6,
                "delivered_frames": 6,
                "video_blend_frames": 39,
            }],
            "compatibility": {"video_blend_frames": 39},
        },
    }
    delivered, _, with_overlap, retained = trim_node.trim(
        numbered[4:], 0, retain_overlap_frames=39, state=first_scene_state)
    assert delivered[:, 0, 0, 0].tolist() == list(range(4, 10))
    assert with_overlap[:, 0, 0, 0].tolist() == list(range(4, 10))
    assert retained == 0
    print("trim: AV tails frame-locked; optional visual overlap is clamped and "
          "scene state overrides the legacy Plan-default wire")

    real_st_load = chain._st_load
    try:
        loads = iter([
            {"delivered_audio": torch.ones((1, 2, 1667))},
            {"delivered_audio": torch.ones((1, 2, 1667))},
        ])
        chain._st_load = lambda _path: next(loads)
        cumulative_trimmed = chain._generated_audio({"segments": [
            {"index": 1, "checkpoint": "one", "sample_rate": 8000,
             "delivered_frames": 5},
            {"index": 2, "checkpoint": "two", "sample_rate": 8000,
             "delivered_frames": 5},
        ]})
        assert cumulative_trimmed["waveform"].shape[-1] == round(10 / 24 * 8000)

        loads = iter([
            {"delivered_audio": torch.ones((1, 2, 1333))},
            {"delivered_audio": torch.ones((1, 2, 1333))},
        ])
        cumulative_padded = chain._generated_audio({"segments": [
            {"index": 1, "checkpoint": "one", "sample_rate": 8000,
             "delivered_frames": 4},
            {"index": 2, "checkpoint": "two", "sample_rate": 8000,
             "delivered_frames": 4},
        ]})
        assert cumulative_padded["waveform"].shape[-1] == round(8 / 24 * 8000)
        assert torch.count_nonzero(cumulative_padded["waveform"][..., -1:]) == 0

        real_prelude_audio = chain._prelude_audio
        chain._prelude_audio = lambda _record: {
            "waveform": torch.ones((1, 2, 1667)), "sample_rate": 8000}
        try:
            joined = chain._audio_with_prelude(
                {"waveform": torch.ones((1, 2, 1667)),
                 "sample_rate": 8000},
                5, {"frame_count": 5})
        finally:
            chain._prelude_audio = real_prelude_audio
        assert joined["waveform"].shape[-1] == round(10 / 24 * 8000)
    finally:
        chain._st_load = real_st_load
    print("generated audio: per-scene rounding reconciled at cumulative frame "
          "boundaries for both trim and pad cases")

    giant_plan = chain._normalize_plan(
        json.dumps({
            "shots": [
                {"id": str(index), "prompt": "shot %d" % index}
                for index in range(1, 14)
            ] + [{"id": "14", "prompt": "outro", "duration_seconds": 5}]
        }),
        "timing", 960, 544, 22, "video", "head", "disabled",
        "source_track", 22, 15, 20, 123, 18,
    )
    assert giant_plan["shots"][0]["raw_frames"] == 362
    assert giant_plan["shots"][1]["generation_start_frame"] == 340
    assert giant_plan["shots"][-1]["raw_frames"] == 124
    assert giant_plan["shots"][-1]["generation_start_frame"] == 4420
    assert giant_plan["total_delivered_frames"] == 4544
    print("timing: 14 clips -> 4544 frames / 189.333s; frame-exact starts pass")

    assert chain._h3_frame_length(5 / 24 + 0.001) == 22
    assert chain._h3_frame_length(22 / 24 + 0.001) == 39
    try:
        chain._h3_frame_length(150.0)
    except ValueError as exc:
        assert "largest valid" in str(exc)
    else:
        raise AssertionError("duration-derived length exceeded H3's maximum")
    print("duration grid: always rounds up and rejects over-limit lengths")

    before_plan = chain._normalize_plan(
        json.dumps({"shots": ["first", "second", "third", "fourth"]}),
        "before", 32, 32, 1, "video", "before", "disabled",
        "generated_audio", 1, 0.1, 2, 1, 30,
    )
    assert [shot["delivered_frames"] for shot in before_plan["shots"]] == [5] * 4
    assert before_plan["shots"][1]["generation_start_frame"] == 5

    try:
        chain._normalize_plan(
            json.dumps({"shots": [
                {"prompt": "too short", "length": 5},
                {"prompt": "next", "length": 39},
            ]}),
            "short", 32, 32, 22, "video", "head", "disabled",
            "generated_audio", 22, 1, 2, 1, 30,
        )
    except ValueError as exc:
        assert "requires 22 second-block context frames" in str(exc)
        assert "scene 2's selected visual source" in str(exc)
    else:
        raise AssertionError("plan accepted an undersized predecessor context")

    plan = chain._normalize_plan(
        json.dumps({"shots": [
            {"id": "one", "prompt": "first", "length": 5, "seed": 1},
            {"id": "two", "prompt": "second", "length": 5, "seed": 2},
        ]}),
        "smoke", 32, 32, 1, "video", "head", "disabled",
        "source_track", 1, 1, 2, 1, 30,
    )
    assert [shot["delivered_frames"] for shot in plan["shots"]] == [5, 4]

    observed = {}

    class SmokePlan:
        @classmethod
        def INPUT_TYPES(cls):
            return {"required": {}}

        RETURN_TYPES = (chain.PLAN_TYPE,)
        FUNCTION = "make"

        def make(self):
            return (before_plan,)

    class SmokeBody:
        @classmethod
        def INPUT_TYPES(cls):
            return {"required": {"state": (chain.STATE_TYPE,)}}

        RETURN_TYPES = ("IMAGE", "LATENT", chain.SEGMENT_TYPE)
        FUNCTION = "render"

        def render(self, state):
            shot = state["plan"]["shots"][state["index"] - 1]
            images = torch.zeros(
                (shot["delivered_frames"], 32, 32, 3), dtype=torch.float32)
            segment = {"index": state["index"], "id": shot["id"]}
            return (images, av_latent(), segment)

    class SmokeSink:
        @classmethod
        def INPUT_TYPES(cls):
            return {"required": {"manifest": (chain.MANIFEST_TYPE,)}}

        RETURN_TYPES = ("STRING",)
        FUNCTION = "take"
        OUTPUT_NODE = True

        def take(self, manifest):
            observed["manifest"] = manifest
            return ("ok",)

    class SmokeServer:
        client_id = None
        last_node_id = None

        def send_sync(self, *args, **kwargs):
            pass

    runtime_nodes = dict(package.NODE_CLASS_MAPPINGS)
    runtime_nodes.update({
        "H3ChainSmokePlan": SmokePlan,
        "H3ChainSmokeBody": SmokeBody,
        "H3ChainSmokeSink": SmokeSink,
    })
    previous_nodes = {name: comfy_nodes.NODE_CLASS_MAPPINGS.get(name)
                      for name in runtime_nodes}
    comfy_nodes.NODE_CLASS_MAPPINGS.update(runtime_nodes)
    try:
        prompt = {
            "1": {"class_type": "H3ChainSmokePlan", "inputs": {}},
            "2": {"class_type": "MiniMaxH3ChainLoopStart", "inputs": {
                "plan": ["1", 0], "start_clip": 1,
                "scene_range": "1:2",
            }},
            "3": {"class_type": "H3ChainSmokeBody", "inputs": {
                "state": ["2", 1],
            }},
            "4": {"class_type": "MiniMaxH3ChainLoopEnd", "inputs": {
                "flow": ["2", 0], "state": ["2", 1],
                "images": ["3", 0], "sampled_latent": ["3", 1],
                "segment": ["3", 2],
            }},
            "5": {"class_type": "H3ChainSmokeSink", "inputs": {
                "manifest": ["4", 0],
            }},
        }
        executor = execution.PromptExecutor(
            SmokeServer(),
            cache_type=execution.CacheType.CLASSIC,
            cache_args={"ram": 0, "ram_inactive": 0},
        )
        executor.execute(prompt, "h3-chain-recursion-smoke", execute_outputs=["5"])
        assert executor.success
        assert observed["manifest"]["clip_count"] == 2
        assert len(observed["manifest"]["segments"]) == 2
        assert observed["manifest"]["format"] == "h3_chain_partial_manifest_v3"
        assert observed["manifest"]["planned_clip_count"] == 4
        print("runtime recursion: scene_range 1:2 stopped a four-clip plan at 2")
    finally:
        for name, previous in previous_nodes.items():
            if previous is None:
                comfy_nodes.NODE_CLASS_MAPPINGS.pop(name, None)
            else:
                comfy_nodes.NODE_CLASS_MAPPINGS[name] = previous

    previous_output = folder_paths.get_output_directory()
    with tempfile.TemporaryDirectory() as tempdir:
        folder_paths.set_output_directory(tempdir)
        try:
            source = audio_for_frames(9)
            changed_source = audio_for_frames(9)
            changed_source["waveform"][..., 0] = 1.0
            prepared_plan = chain._plan_with_source_audio(plan, source)
            assert prepared_plan["plan_hash"] == chain._fingerprint({
                "base_plan_hash": plan["plan_hash"],
                "source_audio_hash": chain._audio_fingerprint(source),
            })
            started = chain.MiniMaxH3ChainLoopStart().start(plan, 1, source)
            assert started[1]["plan"]["compatibility"]["source_audio_hash"]
            current_payload = chain.MiniMaxH3ChainCurrent().current(
                started[1], source)
            assert current_payload["ui"]["h3_chain_active_scene"] == [{
                "run_name": prepared_plan["run_name"],
                "clip_index": 1,
                "clip_count": 2,
                "end_clip": 2,
                "shot_id": prepared_plan["shots"][0]["id"],
                "seed": str(prepared_plan["shots"][0]["seed"]),
            }]
            current = current_payload["result"]
            assert current[1:3] == (1, 2)
            assert current[6:10] == (5, 2, 32, 32)
            assert int(current[12]["waveform"].shape[-1]) == round(5 / 24 * 8000)

            aligned_plan = chain._normalize_plan(
                json.dumps({"shots": [
                    {"id": "aligned", "prompt": "test", "length": 362},
                ]}),
                "aligned", 32, 32, 1, "video", "head", "disabled",
                "source_track", 1, 15, 2, 1, 30,
            )
            aligned_source = audio_for_frames(362, 32000)
            aligned_state = chain.MiniMaxH3ChainLoopStart().start(
                aligned_plan, 1, aligned_source)[1]
            frame_exact = chain.MiniMaxH3ChainCurrent().current(
                aligned_state, aligned_source,
                align_audio_reference=False)["result"]
            assert int(frame_exact[12]["waveform"].shape[-1]) == 482667
            grid_aligned = chain.MiniMaxH3ChainCurrent().current(
                aligned_state, aligned_source,
                align_audio_reference=True)["result"]
            assert int(grid_aligned[12]["waveform"].shape[-1]) == 482240
            assert "target 603 steps, safe 15.070000s" in grid_aligned[13]

            aligned_44k_source = audio_for_frames(362, 44100)
            aligned_44k_state = chain.MiniMaxH3ChainLoopStart().start(
                aligned_plan, 1, aligned_44k_source)[1]
            grid_aligned_44k = chain.MiniMaxH3ChainCurrent().current(
                aligned_44k_state, aligned_44k_source,
                align_audio_reference=True)["result"][12]
            aligned_44k_samples = int(
                grid_aligned_44k["waveform"].shape[-1])
            assert aligned_44k_samples == 664587
            assert math.ceil(aligned_44k_samples * 32000 / 44100) == 482240
            try:
                chain.MiniMaxH3ChainCurrent().current(
                    started[1], changed_source)
            except ValueError as exc:
                assert "different source waveform" in str(exc)
            else:
                raise AssertionError("Current Shot accepted a different source song")
            short_source = audio_for_frames(4)
            short_started = chain.MiniMaxH3ChainLoopStart().start(
                plan, 1, short_source)
            assert short_started[1]["plan"]["compatibility"][
                "source_audio_silent_padding"]
            short_current = chain.MiniMaxH3ChainCurrent().current(
                short_started[1], short_source)["result"]
            assert int(short_current[12]["waveform"].shape[-1]) == round(
                5 / 24 * 8000)
            assert not torch.count_nonzero(short_current[12]["waveform"])
            short_non_silent = audio_for_frames(4)
            short_non_silent["waveform"][..., 0] = 0.25
            try:
                chain.MiniMaxH3ChainLoopStart().start(
                    plan, 1, short_non_silent)
            except ValueError as exc:
                assert "source_audio_too_short" in str(exc)
                assert "Provide a source track at least" in str(exc)
            else:
                raise AssertionError("Loop Start accepted a short non-silent song")
            conditioning = [["cond", {}]]
            bypass_latent = av_latent()
            bypass = chain.MiniMaxH3ChainContext().apply(
                started[1], conditioning, None, bypass_latent)
            assert bypass[:3] == (conditioning, 0, False)
            assert bypass[3] is bypass_latent
            print("current/context: source window exact or 40 Hz aligned; short "
                  "silence pads safely")

            external_plan = chain._normalize_plan(
                json.dumps({"shots": [
                    {"id": "extension_one", "prompt": "continue", "length": 5},
                    {"id": "extension_two", "prompt": "continue again", "length": 5},
                ]}),
                "external_smoke", 32, 32, 1, "video", "head", "disabled",
                "source_plus_timeline", 5, 1, 2, 11, 30,
            )
            source_frames = torch.zeros((8, 32, 32, 3), dtype=torch.float32)
            for frame_index in range(8):
                source_frames[frame_index, ..., 0] = frame_index / 10.0
            source_video_audio = audio_for_frames(8)
            source_video_audio["waveform"].fill_(0.75)
            adapter = chain.MiniMaxH3ChainExternalVideo()
            external_context, external_status = adapter.prepare(
                external_plan, source_frames, 30.0, True, source_video_audio)
            assert "decoded IMAGE/AUDIO" in external_status
            assert "will be prepended" in external_status
            assert tuple(external_context["context_frames"].shape) == (
                1, 32, 32, 3)
            assert int(external_context["context_audio"][
                "waveform"].shape[-1]) == round(5 / 24 * 8000)
            assert abs(float(external_context["context_frames"][0, 0, 0, 0])
                       - 0.6) < 1e-6
            prelude = external_context["prelude"]
            assert prelude["frame_count"] == 6
            assert pathlib.Path(
                tempdir, prelude["video"]).is_file()
            assert pathlib.Path(
                tempdir, prelude["audio"]).is_file()

            class FakeVideoComponents:
                images = source_frames
                audio = source_video_audio
                frame_rate = 30

            class FakeNativeVideo:
                def get_components(self):
                    return FakeVideoComponents()

            ref_prep = chain.MiniMaxH3ReferenceVideoPrepare()
            ref_frames, ref_audio, ref_length, ref_status = ref_prep.prepare(
                5, 1.0, source_video=FakeNativeVideo())
            assert ref_length == 5
            assert tuple(ref_frames.shape) == (5, 32, 32, 3)
            assert abs(float(ref_frames[-1, 0, 0, 0]) - 0.5) < 1e-6
            assert int(ref_audio["waveform"].shape[-1]) == round(
                5 / 24 * 8000)
            assert torch.all(ref_audio["waveform"] == 0.75)
            assert "native VIDEO" in ref_status
            assert "5 frames at 24 fps" in ref_status
            ref_override_audio = audio_for_frames(8)
            ref_override_audio["waveform"].fill_(0.5)
            decoded_ref = ref_prep.prepare(
                5, 30.0, source_frames=source_frames,
                source_audio=ref_override_audio)
            assert "decoded IMAGE/AUDIO" in decoded_ref[3]
            assert torch.all(decoded_ref[1]["waveform"] == 0.5)
            try:
                ref_prep.prepare(
                    22, 30.0, source_frames=source_frames,
                    source_audio=source_video_audio)
            except ValueError as exc:
                assert "Choose a shorter H3-valid length" in str(exc)
            else:
                raise AssertionError(
                    "reference-video prep accepted an overlong source")
            print("reference prep: native/decoded video and exact audio copy pass")

            native_context, native_status = adapter.prepare(
                external_plan, source_fps=1.0, prepend_original=False,
                source_video=FakeNativeVideo())
            assert "native VIDEO" in native_status
            assert "30.000 fps" in native_status
            assert native_context["prelude"] is None
            assert abs(float(native_context[
                "context_frames"][0, 0, 0, 0]) - 0.6) < 1e-6
            assert int(native_context["context_audio"][
                "waveform"].shape[-1]) == round(5 / 24 * 8000)
            override_audio = audio_for_frames(8)
            override_audio["waveform"].fill_(0.5)
            overridden_context = adapter.prepare(
                external_plan, source_fps=24.0, prepend_original=False,
                source_audio=override_audio,
                source_video=FakeNativeVideo())[0]
            assert torch.allclose(
                overridden_context["context_audio"]["waveform"],
                torch.full_like(
                    overridden_context["context_audio"]["waveform"], 0.5))
            try:
                adapter.prepare(
                    external_plan, source_frames, 30.0, False,
                    source_video=FakeNativeVideo())
            except ValueError as exc:
                assert "both source_video and source_frames" in str(exc)
            else:
                raise AssertionError(
                    "existing-video adapter accepted both video input routes")
            try:
                adapter.prepare(
                    external_plan, source_fps=30.0, prepend_original=False)
            except ValueError as exc:
                assert "requires source_video or source_frames" in str(exc)
            else:
                raise AssertionError(
                    "existing-video adapter accepted no video input")

            extension_audio = audio_for_frames(8)
            extension_audio["waveform"].fill_(0.25)
            external_started = chain.MiniMaxH3ChainLoopStart().start(
                external_plan, 1, extension_audio,
                external_context=external_context)
            external_state1 = external_started[1]
            effective_external_plan = external_state1["plan"]
            assert [shot["delivered_frames"] for shot in
                    effective_external_plan["shots"]] == [4, 4]
            assert effective_external_plan["total_delivered_frames"] == 8
            assert external_state1["external_context"]
            assert tuple(external_state1["previous_frames"].shape) == (
                1, 32, 32, 3)
            first_current = chain.MiniMaxH3ChainCurrent().current(
                external_state1, extension_audio)["result"]
            external_current_state1 = first_current[0]
            first_slice = first_current[12]["waveform"]
            first_lead_samples = round(1 / 24 * 8000)
            assert int(first_slice.shape[-1]) == round(5 / 24 * 8000)
            assert torch.allclose(
                first_slice[..., :first_lead_samples],
                torch.full_like(first_slice[..., :first_lead_samples], 0.75))
            assert torch.allclose(
                first_slice[..., first_lead_samples:],
                torch.full_like(first_slice[..., first_lead_samples:], 0.25))

            context_call = {}

            class FakeExternalMotionContext:
                def apply(self, **kwargs):
                    context_call.update(kwargs)
                    return ("continued", 1)

            real_motion_context = chain.MiniMaxH3MotionContext
            chain.MiniMaxH3MotionContext = FakeExternalMotionContext
            try:
                external_conditioning = chain.MiniMaxH3ChainContext().apply(
                    external_current_state1, conditioning, None, av_latent(),
                    audio_vae="audio-vae")
            finally:
                chain.MiniMaxH3MotionContext = real_motion_context
            assert external_conditioning[:3] == ("continued", 1, True)
            assert external_conditioning[3] is context_call["latent"]
            assert context_call["context_latent"] is None
            assert context_call["audio_vae"] == "audio-vae"
            assert context_call["context_audio"] is external_current_state1[
                "previous_audio"]

            external_saver = chain.MiniMaxH3ChainSegmentSave()
            external_saver.save(
                external_current_state1,
                torch.zeros((4, 32, 32, 3), dtype=torch.float32),
                av_latent())["result"][0]
            external_state2 = chain._initial_state(
                effective_external_plan, 2)
            external_current_state2 = chain.MiniMaxH3ChainCurrent().current(
                external_state2, extension_audio)["result"][0]
            external_segment2 = external_saver.save(
                external_current_state2,
                torch.zeros((4, 32, 32, 3), dtype=torch.float32),
                av_latent())["result"][0]
            external_complete = dict(external_current_state2)
            external_complete["segments"] = (
                external_current_state2["segments"] + [external_segment2])
            external_manifest = chain._manifest_from_state(external_complete)
            assert external_manifest["prelude"]["frame_count"] == 6
            loaded_external = chain.MiniMaxH3ChainManifestLoad().load(
                external_plan, extension_audio, external_context)[0]
            assert loaded_external["plan_hash"] == external_manifest["plan_hash"]

            joined_audio = chain._audio_with_prelude(
                extension_audio, 8, prelude)
            assert int(joined_audio["waveform"].shape[-1]) == round(
                14 / 24 * 8000)
            prelude_samples = round(6 / 24 * 8000)
            assert torch.allclose(
                joined_audio["waveform"][..., :prelude_samples],
                torch.full_like(
                    joined_audio["waveform"][..., :prelude_samples], 0.75))
            external_result = chain.MiniMaxH3ChainAssemble().assemble(
                external_manifest, "source", "extended_with_original", 96,
                extension_audio)
            external_path = pathlib.Path(external_result["result"][0])
            assert external_path.is_file() and external_path.stat().st_size > 0
            external_duration = float(subprocess.check_output([
                "ffprobe", "-v", "error", "-show_entries", "format=duration",
                "-of", "default=nw=1:nk=1", str(external_path),
            ], text=True, encoding="utf-8", errors="replace").strip())
            assert abs(external_duration - 14 / 24) < 0.05
            assert "existing-video prelude" in external_result["ui"]["text"][0]
            external_original_which = chain.shutil.which
            chain.shutil.which = lambda executable: (
                None if executable == "ffmpeg"
                else external_original_which(executable))
            try:
                external_fallback = chain.MiniMaxH3ChainAssemble().assemble(
                    external_manifest, "source", "extended_pyav", 96,
                    extension_audio)
            finally:
                chain.shutil.which = external_original_which
            with chain.av.open(
                    external_fallback["result"][0], mode="r") as media:
                assert len(media.streams.video) == 1
                assert len(media.streams.audio) == 1
                assert sum(1 for _frame in media.decode(video=0)) == 14
            print("existing video: native VIDEO and decoded IMAGE/AUDIO routes "
                  "normalized 30 fps input, scene 1 continued with AV context, "
                  "and original prelude assembled with both media backends")

            saver = chain.MiniMaxH3ChainSegmentSave()
            generated_state = chain._initial_state(
                chain._plan_with_source_audio(before_plan, None), 1)
            try:
                saver.save(
                    generated_state,
                    torch.zeros((5, 32, 32, 3), dtype=torch.float32),
                    av_latent())
            except ValueError as exc:
                assert "requires decoded audio" in str(exc)
            else:
                raise AssertionError("generated_audio saved without decoded audio")
            try:
                saver.save(
                    generated_state,
                    torch.zeros((5, 32, 32, 3), dtype=torch.float32),
                    av_latent(), audio_for_frames(4))
            except ValueError as exc:
                assert "expected exactly" in str(exc)
            else:
                raise AssertionError("Segment Save accepted mistimed audio")
            state1 = current[0]
            images1 = torch.zeros((5, 32, 32, 3), dtype=torch.float32)
            queued_prompt = {
                "1700": {
                    "class_type": "MiniMaxH3ChainPlan",
                    "inputs": {
                        "plan_json": '{"shots":["stale"]}',
                        "run_name": "smoke",
                    },
                },
            }
            queued_workflow = {
                "nodes": [{
                    "id": 1700,
                    "type": "MiniMaxH3ChainPlan",
                    "widgets_values": [
                        '{"shots":["stale"]}', "smoke", "fingerprint",
                    ],
                }],
            }
            result1 = saver.save(
                state1, images1, av_latent(), audio_for_frames(5),
                prompt=queued_prompt,
                extra_pnginfo={"workflow": queued_workflow})
            segment1 = result1["result"][0]
            assert pathlib.Path(chain._absolute_output_path(
                segment1["segment"])).is_file()
            segment1_audio_path = pathlib.Path(chain._absolute_output_path(
                segment1["generated_audio"]))
            assert segment1_audio_path.is_file()
            assert segment1["generated_audio_sha256"] == chain._file_sha256(
                str(segment1_audio_path))
            with wave.open(str(segment1_audio_path), "rb") as saved_audio:
                assert saved_audio.getframerate() == 8000
                assert saved_audio.getnchannels() == 2
                assert saved_audio.getnframes() == round(5 / 24 * 8000)

            assert segment1["prompt_prefix"] == ""
            assert segment1["scene_prompt"] == "first"
            assert segment1["prompt"] == "first"
            prompt_path = pathlib.Path(chain._absolute_output_path(
                segment1["prompt_file"]))
            assert prompt_path.read_text(encoding="utf-8") == "first"
            assert segment1["prompt_file_sha256"] == chain._file_sha256(
                str(prompt_path))
            segment_metadata = json.loads(pathlib.Path(
                chain._absolute_output_path(segment1["metadata"])
            ).read_text(encoding="utf-8"))
            revision_metadata_path = pathlib.Path(
                chain._absolute_output_path(segment1["revision_metadata"]))
            assert segment_metadata["format"] == "h3_chain_segment_v3"
            assert segment_metadata["segment"]["prompt"] == "first"
            assert segment_metadata["archives"] == segment1["archives"]
            assert revision_metadata_path.is_file()
            assert json.loads(revision_metadata_path.read_text(
                encoding="utf-8"))["segment"]["revision"] == segment1["revision"]

            run_dir = pathlib.Path(tempdir, "h3_chains", "smoke")
            exact_text_path = run_dir / "exact-lf.txt"
            chain._atomic_text(str(exact_text_path), "line one\nline two")
            assert exact_text_path.read_bytes() == b"line one\nline two"

            legacy_prompt_path = run_dir / "legacy-windows.prompt.txt"
            legacy_prompt_path.write_bytes(b"line one\r\nline two")
            legacy_segment = dict(segment1)
            legacy_segment["prompt_file"] = chain._relative_output_path(
                str(legacy_prompt_path))
            legacy_segment["prompt_hash"] = hashlib.sha256(
                b"line one\nline two").hexdigest()
            legacy_segment.pop("prompt_file_sha256", None)
            chain._verify_segment_artifacts(legacy_segment, 1)
            legacy_prompt_path.write_bytes(b"line one\r\nchanged")
            try:
                chain._verify_segment_artifacts(legacy_segment, 1)
            except ValueError as exc:
                assert "prompt sidecar" in str(exc)
            else:
                raise AssertionError("changed legacy prompt sidecar was accepted")

            archived_plan = json.loads(
                (run_dir / "plan.json").read_text(encoding="utf-8"))
            archived_api = json.loads(
                (run_dir / "api_prompt.json").read_text(encoding="utf-8"))
            archived_workflow = json.loads(
                (run_dir / "workflow.json").read_text(encoding="utf-8"))
            assert archived_plan["format"] == "h3_chain_plan_archive_v1"
            assert archived_plan["shots"][0]["prompt"] == "first"
            assert json.loads(
                archived_api["1700"]["inputs"]["plan_json"]
            )["shots"][0]["prompt"] == "first"
            assert json.loads(
                archived_workflow["nodes"][0]["widgets_values"][0]
            )["shots"][0]["prompt"] == "first"
            embedded_tags = json.loads(subprocess.check_output([
                "ffprobe", "-v", "error", "-show_entries", "format_tags",
                "-of", "json",
                str(chain._absolute_output_path(segment1["segment"])),
            ], text=True, encoding="utf-8", errors="replace"))["format"]["tags"]
            assert embedded_tags["comment"] == "first"
            assert embedded_tags["h3_prompt"] == "first"
            assert json.loads(embedded_tags["workflow"])["nodes"][0][
                "type"] == "MiniMaxH3ChainPlan"
            assert json.loads(embedded_tags["prompt"])["1700"][
                "class_type"] == "MiniMaxH3ChainPlan"
            assert json.loads(embedded_tags["h3_plan"])["shots"][0][
                "prompt"] == "first"
            print("recovery metadata: MP4, prompt sidecar, plan, API prompt, "
                  "and workflow archive exact inputs")

            interrupted_manifest = chain.MiniMaxH3ChainManifestLoad().load(
                plan, source)
            assert interrupted_manifest[0]["format"] == (
                "h3_chain_partial_manifest_v3")
            assert interrupted_manifest[0]["clip_count"] == 1
            assert interrupted_manifest[0]["planned_clip_count"] == 2
            assert "partial manifest through clip 1/2" in (
                interrupted_manifest[2])
            assert pathlib.Path(
                tempdir, "h3_chains", "smoke", "partial",
                "through_clip_0001.manifest.json").is_file()
            print("manifest load: interrupted run restored through scene 1")

            segment1_path = pathlib.Path(
                chain._absolute_output_path(segment1["segment"]))
            checkpoint1_path = pathlib.Path(
                chain._absolute_output_path(segment1["checkpoint"]))
            with safe_open(checkpoint1_path, framework="pt", device="cpu") as saved:
                checkpoint_metadata = saved.metadata()
            assert checkpoint_metadata["format"] == "h3_chain_checkpoint_v3"
            assert checkpoint_metadata["prompt"] == "first"
            assert checkpoint_metadata["seed"] == "1"
            before_interruption = (
                segment1_path.read_bytes(), checkpoint1_path.read_bytes(),
                segment1_audio_path.read_bytes())
            real_st_save = chain._st_save

            def interrupted_save(*args, **kwargs):
                raise RuntimeError("simulated interrupted checkpoint write")

            chain._st_save = interrupted_save
            try:
                saver.save(
                    state1, torch.ones_like(images1), av_latent(),
                    audio_for_frames(5))
            except RuntimeError as exc:
                assert "simulated interrupted" in str(exc)
            else:
                raise AssertionError("simulated checkpoint interruption did not fire")
            finally:
                chain._st_save = real_st_save
            assert segment1_path.read_bytes() == before_interruption[0]
            assert checkpoint1_path.read_bytes() == before_interruption[1]
            assert segment1_audio_path.read_bytes() == before_interruption[2]
            assert chain._initial_state(prepared_plan, 2)["index"] == 2
            replacement = saver.save(
                state1, images1, av_latent(), audio_for_frames(5))["result"][0]
            assert replacement["segment"] != segment1["segment"]
            assert segment1_path.exists()
            assert checkpoint1_path.exists()
            assert segment1_audio_path.exists()
            assert prompt_path.exists()
            assert revision_metadata_path.exists()
            assert replacement["supersedes"] == segment1["revision_metadata"]
            active_metadata = json.loads(pathlib.Path(
                chain._absolute_output_path(replacement["metadata"])
            ).read_text(encoding="utf-8"))
            assert active_metadata["segment"]["revision"] == replacement["revision"]
            segment1 = replacement
            print("atomic save: interruption preserved old AV artifacts; retry "
                  "switched + retained prior revision")

            review_item, has_audio, warning = chain._review_video(
                prepared_plan, segment1, audio_for_frames(5))
            review_path = pathlib.Path(
                tempdir, review_item["subfolder"], review_item["filename"])
            assert has_audio and not warning and review_path.is_file()
            streams = subprocess.check_output([
                "ffprobe", "-v", "error", "-show_entries", "stream=codec_type",
                "-of", "csv=p=0", str(review_path),
            ], text=True, encoding="utf-8", errors="replace").splitlines()
            assert "video" in streams and "audio" in streams

            fallback_review_audio = audio_for_frames(5)
            fallback_review_audio["waveform"][..., 0] = 0.25
            original_which = chain.shutil.which
            chain.shutil.which = lambda executable: (
                None if executable == "ffmpeg" else original_which(executable))
            try:
                fallback_review, fallback_has_audio, fallback_warning = (
                    chain._review_video(
                        prepared_plan, segment1, fallback_review_audio))
            finally:
                chain.shutil.which = original_which
            fallback_review_path = pathlib.Path(
                tempdir, fallback_review["subfolder"],
                fallback_review["filename"])
            assert (fallback_has_audio and not fallback_warning and
                    fallback_review_path.is_file())
            with chain.av.open(
                    str(fallback_review_path), mode="r") as fallback_media:
                assert len(fallback_media.streams.video) == 1
                assert len(fallback_media.streams.audio) == 1
            print("review: persisted segment muxed with frame-exact audio via "
                  "ffmpeg and the PyAV fallback")

            async def approve_live_review():
                sent = []
                unload_calls = []

                class ReviewServerInstance:
                    client_id = "smoke-client"

                    def send_sync(self, event, payload, client_id):
                        sent.append((event, payload, client_id))

                class ReviewServer:
                    instance = ReviewServerInstance()

                original_server = chain.PromptServer
                import comfy.model_management as model_management
                original_unload = model_management.unload_all_models
                model_management.unload_all_models = lambda: unload_calls.append(True)
                chain.PromptServer = ReviewServer
                try:
                    task = asyncio.create_task(
                        chain.MiniMaxH3ChainReview().review(
                            state1, segment1, True, False, 0.0,
                            True, False, "none",
                            audio_for_frames(5), unique_id="review-node"))
                    for _ in range(100):
                        if chain._PENDING_REVIEWS:
                            break
                        await asyncio.sleep(0.01)
                    assert chain._PENDING_REVIEWS and sent
                    review_events = [
                        payload for event, payload, _client in sent
                        if event == "minimax_h3_context_loop_review"]
                    assert review_events[0]["run_name"] == prepared_plan["run_name"]
                    assert review_events[0]["preview_pending"]
                    assert review_events[0]["preview_revision"] == 0
                    assert not review_events[-1]["preview_pending"]
                    assert review_events[-1]["preview_revision"] == 1
                    assert review_events[-1]["has_audio"]
                    token = review_events[-1]["token"]

                    class ApproveRequest:
                        async def json(self):
                            return {"token": token, "action": "approve"}

                    response = await chain._submit_review_decision(
                        ApproveRequest())
                    assert response.status == 200
                    result = await asyncio.wait_for(task, timeout=5.0)
                    assert result["result"][0]["segment"] == segment1["segment"]
                    assert unload_calls == [True]
                    assert not chain._PENDING_REVIEWS

                    timeout_task = asyncio.create_task(
                        chain.MiniMaxH3ChainReview().review(
                            state1, segment1, True, True, 0.001,
                            False, False, "none",
                            audio_for_frames(5), unique_id="review-node"))
                    timeout_result = await asyncio.wait_for(
                        timeout_task, timeout=2.0)
                    assert "timed out" in timeout_result["result"][1]
                    assert any(event == "minimax_h3_context_loop_review_resolved"
                               for event, _payload, _client in sent)
                    assert not chain._PENDING_REVIEWS

                    # Review muxing is UI-only. Publish the pending token first
                    # and fall back to a silent, actionable review if audio
                    # preview preparation fails.
                    real_review_video = chain._review_video

                    def fail_audio_preview(plan_arg, segment_arg, audio_arg):
                        if audio_arg is None:
                            return real_review_video(
                                plan_arg, segment_arg, audio_arg)
                        raise RuntimeError("simulated review audio failure")

                    chain._review_video = fail_audio_preview
                    event_offset = len(sent)
                    try:
                        fallback_task = asyncio.create_task(
                            chain.MiniMaxH3ChainReview().review(
                                state1, segment1, True, False, 0.0,
                                False, False, "none",
                                audio_for_frames(5), unique_id="review-node"))
                        for _ in range(100):
                            if chain._PENDING_REVIEWS:
                                break
                            await asyncio.sleep(0.01)
                        assert chain._PENDING_REVIEWS
                        fallback_events = [
                            payload for event, payload, _client in sent[event_offset:]
                            if event == "minimax_h3_context_loop_review"]
                        assert fallback_events[0]["preview_pending"]
                        assert not fallback_events[-1]["preview_pending"]
                        assert not fallback_events[-1]["has_audio"]
                        assert "review is silent" in fallback_events[-1]["warning"]
                        fallback_token = fallback_events[-1]["token"]

                        class FallbackApproveRequest:
                            async def json(self):
                                return {"token": fallback_token,
                                        "action": "approve"}

                        fallback_response = await chain._submit_review_decision(
                            FallbackApproveRequest())
                        assert fallback_response.status == 200
                        fallback_result = await asyncio.wait_for(
                            fallback_task, timeout=5.0)
                        assert "approved clip" in fallback_result["result"][1]
                    finally:
                        chain._review_video = real_review_video
                    assert not chain._PENDING_REVIEWS
                finally:
                    chain.PromptServer = original_server
                    model_management.unload_all_models = original_unload

            asyncio.run(approve_live_review())

            def approve_cross_thread_review():
                sent = []
                result = []

                class ReviewServerInstance:
                    client_id = "cross-thread-smoke-client"

                    def send_sync(self, event, payload, client_id):
                        sent.append((event, payload, client_id))

                class ReviewServer:
                    instance = ReviewServerInstance()

                original_server = chain.PromptServer
                chain.PromptServer = ReviewServer
                try:
                    def execute_review():
                        result.append(asyncio.run(
                            chain.MiniMaxH3ChainReview().review(
                                state1, segment1, True, False, 0.0,
                                False, False, "none",
                                audio_for_frames(5), unique_id="review-node")))

                    worker = threading.Thread(target=execute_review, daemon=True)
                    worker.start()
                    for _ in range(200):
                        if chain._PENDING_REVIEWS:
                            break
                        time.sleep(0.01)
                    assert chain._PENDING_REVIEWS and sent
                    token = sent[-1][1]["token"]

                    class ApproveRequest:
                        async def json(self):
                            return {"token": token, "action": "approve"}

                    response = asyncio.run(
                        chain._submit_review_decision(ApproveRequest()))
                    assert response.status == 200
                    worker.join(timeout=5.0)
                    assert not worker.is_alive()
                    assert result and "approved clip" in result[0]["result"][1]
                    assert not chain._PENDING_REVIEWS
                finally:
                    chain.PromptServer = original_server

            approve_cross_thread_review()
            assert chain._review_timeout_seconds(0) == 0
            assert chain._review_timeout_seconds(1.5) == 90
            print("review: same-loop, cross-thread, timeout, and silent-preview "
                  "fallback approvals resume")

            revised = chain._plan_with_review_revision(
                prepared_plan, 2, "Revised second scene.", 999)
            assert revised["base_plan_hash"] == prepared_plan["base_plan_hash"]
            assert revised["shots"][1]["prompt"] == "Revised second scene."
            assert revised["shots"][1]["seed"] == 999
            assert (chain._history_hash(revised, 1) ==
                    chain._history_hash(prepared_plan, 1))
            assert (chain._history_hash(revised, 2) !=
                    chain._history_hash(prepared_plan, 2))
            print("review: prompt/seed retry preserves accepted predecessor history")

            fake_prompt = {
                "1": {"class_type": "MiniMaxH3ChainLoopStart", "inputs": {
                    "plan": plan, "start_clip": 1, "source_audio": source,
                }},
                "2": {"class_type": "MiniMaxH3ChainCurrent", "inputs": {
                    "state": ["1", 1],
                }},
                "3": {"class_type": "MiniMaxH3ChainSegmentSave", "inputs": {
                    "state": ["2", 0],
                }},
                "4": {"class_type": "MiniMaxH3ChainLoopEnd", "inputs": {
                    "flow": ["1", 0], "state": ["2", 0],
                    "images": ["3", 0], "sampled_latent": ["3", 0],
                    "segment": ["3", 0],
                }},
            }
            cleanup_boundaries = []
            original_boundary_cleanup = chain._release_loop_boundary_resources
            chain._release_loop_boundary_resources = (
                lambda policy, scene: cleanup_boundaries.append(
                    (policy, scene)) or {"policy": policy})
            try:
                expanded = chain.MiniMaxH3ChainLoopEnd().end(
                    ["1", 0], state1, images1, av_latent(), segment1,
                    between_scene_cleanup="fresh_scene",
                    dynprompt=FakeDynamicPrompt(fake_prompt), unique_id="4")
            finally:
                chain._release_loop_boundary_resources = (
                    original_boundary_cleanup)
            assert isinstance(expanded, dict) and expanded.get("expand")
            assert cleanup_boundaries == [("fresh_scene", 1)]
            cloned_starts = [node for node in expanded["expand"].values()
                             if node["class_type"] == "MiniMaxH3ChainLoopStart"]
            assert len(cloned_starts) == 1
            assert cloned_starts[0]["inputs"]["initial_state"]["index"] == 2
            assert all(isinstance(link, list) for link in expanded["result"])
            print("recursion: GraphBuilder cloned the typed H3 body for clip 2")

            retry_segment = dict(segment1)
            retry_segment["_h3_review_decision"] = {
                "action": "retry",
                "scene_prompt": "Try the opening again.",
                "seed": 1234,
            }
            retried = chain.MiniMaxH3ChainLoopEnd().end(
                ["1", 0], state1, images1, av_latent(), retry_segment,
                dynprompt=FakeDynamicPrompt(fake_prompt), unique_id="4")
            retried_starts = [
                node for node in retried["expand"].values()
                if node["class_type"] == "MiniMaxH3ChainLoopStart"
            ]
            retry_state = retried_starts[0]["inputs"]["initial_state"]
            assert retry_state["index"] == 1
            assert retry_state["segments"] == []
            assert retry_state["plan"]["shots"][0]["seed"] == 1234
            assert retry_state["plan"]["shots"][0]["prompt"] == "Try the opening again."
            print("review: rejected clip recurses at the same index")

            state2 = chain._initial_state(prepared_plan, 2)
            assert state2["resumed_from"] == 1
            assert len(state2["segments"]) == 1
            assert tuple(state2["previous_frames"].shape) == (1, 32, 32, 3)
            assert len(state2["previous_latent"]["samples"]) == 2
            print("resume: clip 2 restored clip 1 frame tail + AV latent")

            state2 = chain.MiniMaxH3ChainCurrent().current(
                state2, source)["result"][0]
            images2 = torch.zeros((4, 32, 32, 3), dtype=torch.float32)
            result2 = saver.save(
                state2, images2, av_latent(), audio_for_frames(4))
            segment2 = result2["result"][0]

            async def stop_with_partial_review():
                sent = []

                class ReviewServerInstance:
                    client_id = "partial-stop-smoke-client"

                    def send_sync(self, event, payload, client_id):
                        sent.append((event, payload, client_id))

                class ReviewServer:
                    instance = ReviewServerInstance()

                original_server = chain.PromptServer
                chain.PromptServer = ReviewServer
                try:
                    task = asyncio.create_task(
                        chain.MiniMaxH3ChainReview().review(
                            state2, segment2, True, False, 0.0,
                            False, True, "checkpointed", audio_for_frames(4),
                            source, unique_id="review-node"))
                    for _ in range(100):
                        if chain._PENDING_REVIEWS:
                            break
                        await asyncio.sleep(0.01)
                    assert chain._PENDING_REVIEWS and sent
                    token = sent[-1][1]["token"]

                    class StopRequest:
                        async def json(self):
                            return {"token": token, "action": "stop"}

                    response = await chain._submit_review_decision(StopRequest())
                    assert response.status == 200
                    result = await asyncio.wait_for(task, timeout=10.0)
                    assert "partial video" in result["result"][1]
                    resolved = [payload for event, payload, _client in sent
                                if event == "minimax_h3_context_loop_review_resolved"]
                    assert resolved and resolved[-1]["partial_video"]
                    item = resolved[-1]["partial_video"]
                    partial_path = pathlib.Path(
                        tempdir, item["subfolder"], item["filename"])
                    assert partial_path.is_file() and partial_path.stat().st_size > 0
                    streams = subprocess.check_output([
                        "ffprobe", "-v", "error", "-show_entries",
                        "stream=codec_type", "-of", "csv=p=0",
                        str(partial_path),
                    ], text=True, encoding="utf-8", errors="replace").splitlines()
                    assert "video" in streams and "audio" in streams
                finally:
                    chain.PromptServer = original_server

            asyncio.run(stop_with_partial_review())
            partial_manifest = pathlib.Path(
                tempdir, "h3_chains", "smoke", "partial",
                "through_clip_0002.manifest.json")
            assert partial_manifest.is_file()
            partial_data = json.loads(partial_manifest.read_text())
            assert partial_data["format"] == "h3_chain_partial_manifest_v3"
            assert partial_data["clip_count"] == 2
            assert partial_data["segments"][0]["prompt"] == "first"
            assert partial_data["segments"][1]["prompt"] == "second"
            assert partial_data["archives"]["workflow"].endswith(
                "/workflow.json")
            print("review stop: joined partial AV video and checkpoint manifest")

            # The route wrapper is covered by the Plan Studio backend test.
            # This smoke test has already opened and closed several event loops,
            # including a cross-thread review loop, so exercise the synchronous
            # listing core here instead of creating another short-lived loop.
            checkpoint_body = chain._saved_checkpoint_listing("smoke")
            assert [item["scene"] for item in checkpoint_body["checkpoints"]] == [1, 2]
            assert all(item["ready"] for item in checkpoint_body["checkpoints"])
            assert all(item["video"] for item in checkpoint_body["checkpoints"])
            assert checkpoint_body["checkpoints"][1]["partial_video"]
            print("checkpoint browser: discovered both saved resume slots")

            complete = dict(state2)
            complete["segments"] = state2["segments"] + [segment2]
            manifest = chain._manifest_from_state(complete)

            loaded_manifest = chain.MiniMaxH3ChainManifestLoad().load(
                plan, source)[0]
            assert loaded_manifest["plan_hash"] == manifest["plan_hash"]
            assert len(loaded_manifest["segments"]) == 2
            assert pathlib.Path(tempdir, "h3_chains", "smoke",
                                "manifest.json").is_file()
            manifest = loaded_manifest
            print("manifest load: completed chain restored without rerender")

            class FakeVideoVAE:
                def __init__(self):
                    self.calls = 0

                def decode(self, _video):
                    self.calls += 1
                    images = torch.zeros(
                        (1, 5, 4, 4, 3), dtype=torch.float32)
                    for frame in range(5):
                        images[:, frame, ..., 0] = (
                            self.calls * 10 + frame) / 255.0
                    return images

            fake_vae = FakeVideoVAE()
            png_result = chain.MiniMaxH3ChainExportPNG().export(
                manifest, fake_vae, "archive", 1, 1, True)
            png_dir = pathlib.Path(png_result["result"][0])
            png_files = sorted(png_dir.glob("frame_*.png"))
            assert png_result["result"][1] == 9
            assert len(png_files) == 9
            assert [path.name for path in (png_files[0], png_files[-1])] == [
                "frame_00000001.png", "frame_00000009.png"]
            png_export = json.loads(
                (png_dir / "export.json").read_text(encoding="utf-8"))
            assert png_export["complete"]
            assert png_export["frame_count"] == 9
            assert png_export["clips"][0]["first_frame_number"] == 1
            assert png_export["clips"][1]["first_frame_number"] == 6
            assert png_export["clips"][1]["trim_frames"] == 1
            with PILImage.open(png_files[0]) as first_png:
                assert json.loads(first_png.text["workflow"])["nodes"][0][
                    "type"] == "MiniMaxH3ChainPlan"
                assert json.loads(first_png.text["h3_manifest"])[
                    "clip_count"] == 2
                assert json.loads(first_png.text["h3_scene"])[
                    "prompt"] == "first"
            with PILImage.open(png_files[5]) as second_scene_png:
                assert second_scene_png.text["h3_clip_index"] == "2"
                assert json.loads(second_scene_png.text["h3_scene"])[
                    "prompt"] == "second"
            # The second clip's raw frame 1 is overlap and must be absent: its
            # first delivered PNG therefore carries fake decoded value 21.
            with PILImage.open(png_files[5]) as trimmed_png:
                assert trimmed_png.getpixel((0, 0))[0] == 21
            assert fake_vae.calls == 2
            print("PNG export: checkpoints re-decoded one scene at a time; "
                  "overlap trimmed and workflow metadata preserved")

            assembler = chain.MiniMaxH3ChainAssemble()
            source_result = assembler.assemble(
                manifest, "source", "source_final", 96, source)
            source_path = pathlib.Path(source_result["result"][0])
            assert source_path.is_file() and source_path.stat().st_size > 0
            generated_sidecar = source_path.with_suffix(".generated.wav")
            assert generated_sidecar.is_file()
            with wave.open(str(generated_sidecar), "rb") as saved_audio:
                assert saved_audio.getframerate() == 8000
                assert saved_audio.getnchannels() == 2
                assert saved_audio.getnframes() == round(9 / 24 * 8000)
            assert "generated audio ->" in source_result["ui"]["text"][0]
            source_tags = json.loads(subprocess.check_output([
                "ffprobe", "-v", "error", "-show_entries", "format_tags",
                "-of", "json", str(source_path),
            ], text=True, encoding="utf-8", errors="replace"))["format"]["tags"]
            assert json.loads(source_tags["workflow"])["nodes"][0][
                "type"] == "MiniMaxH3ChainPlan"
            assert json.loads(source_tags["h3_manifest"])["clip_count"] == 2
            duration = float(subprocess.check_output([
                "ffprobe", "-v", "error", "-show_entries", "format=duration",
                "-of", "default=nw=1:nk=1", str(source_path),
            ], text=True, encoding="utf-8", errors="replace").strip())
            assert abs(duration - 9 / 24) < 0.05
            short_silent_manifest = dict(manifest)
            short_silent_manifest["compatibility"] = dict(
                short_started[1]["plan"]["compatibility"])
            short_silent_manifest["source_timeline"] = dict(
                short_started[1]["plan"]["source_timeline"])
            short_silent_result = assembler.assemble(
                short_silent_manifest, "source", "short_silent_final", 96,
                short_source)
            short_silent_path = pathlib.Path(short_silent_result["result"][0])
            assert short_silent_path.is_file() and short_silent_path.stat().st_size > 0
            try:
                assembler.assemble(
                    manifest, "source", "wrong_source", 96, changed_source)
            except ValueError as exc:
                assert "different source waveform" in str(exc)
            else:
                raise AssertionError("Assemble accepted a different source song")

            generated_result = assembler.assemble(
                manifest, "generated", "generated_final", 96)
            generated_path = pathlib.Path(generated_result["result"][0])
            assert generated_path.is_file() and generated_path.stat().st_size > 0

            original_which = chain.shutil.which
            chain.shutil.which = lambda executable: (
                None if executable == "ffmpeg" else original_which(executable))
            try:
                fallback_result = assembler.assemble(
                    manifest, "generated", "pyav_fallback_final", 96)
            finally:
                chain.shutil.which = original_which
            fallback_path = pathlib.Path(fallback_result["result"][0])
            assert fallback_path.is_file() and fallback_path.stat().st_size > 0
            assert "PyAV fallback" in fallback_result["ui"]["text"][0]
            with chain.av.open(str(fallback_path), mode="r") as fallback_media:
                assert len(fallback_media.streams.video) == 1
                assert len(fallback_media.streams.audio) == 1
                assert json.loads(fallback_media.metadata["h3_manifest"])[
                    "clip_count"] == 2
                fallback_duration = (
                    float(fallback_media.duration) / float(chain.av.time_base))
                assert abs(fallback_duration - 9 / 24) < 0.05
                assert sum(1 for _frame in fallback_media.decode(video=0)) == 9
            print("segments: H.264 save + per-scene/combined generated WAVs + "
                  "source/generated audio assembly and PyAV fallback pass")

            changed = json.loads(json.dumps({"shots": [
                {"id": "one", "prompt": "changed", "length": 5, "seed": 1},
                {"id": "two", "prompt": "second", "length": 5, "seed": 2},
            ]}))
            changed_plan = chain._normalize_plan(
                json.dumps(changed), "smoke", 32, 32, 1, "video", "head",
                "disabled", "source_track", 1, 1, 2, 1, 30)
            try:
                chain._initial_state(
                    chain._plan_with_source_audio(changed_plan, source), 2)
            except ValueError as exc:
                assert "scene_generation.prompt_hash" in str(exc)
            else:
                raise AssertionError("resume accepted a changed predecessor")
            print("resume guard: changed predecessor rejected")
            unsafe_state = chain._initial_state(
                chain._plan_with_source_audio(changed_plan, source), 2,
                verify_resume_history=False)
            assert unsafe_state["resumed_from"] == 1
            assert unsafe_state["resume_history_verification_disabled"]
            assert unsafe_state["segments"][0]["prompt"] == "first"
            print(
                "resume override: incompatible Plan history reused the intact "
                "saved predecessor explicitly")

            changed_generation_plan = chain._normalize_plan(
                json.dumps({"shots": [
                    {"id": "one", "prompt": "first", "length": 5, "seed": 1},
                    {"id": "two", "prompt": "second", "length": 5, "seed": 2},
                ]}),
                "smoke", 32, 32, 1, "video", "head", "disabled",
                "source_track", 1, 1, 2, 1, 30, "model-and-refs-v2")
            try:
                chain._initial_state(chain._plan_with_source_audio(
                    changed_generation_plan, source), 2)
            except ValueError as exc:
                assert "global_generation.generation_fingerprint" in str(exc)
            else:
                raise AssertionError(
                    "resume accepted a changed generation fingerprint")
            print("resume guard: external generation fingerprint enforced")

            try:
                chain._initial_state(
                    chain._plan_with_source_audio(plan, changed_source), 2)
            except ValueError as exc:
                assert "scene_generation.source_reference_window" in str(exc)
            else:
                raise AssertionError("resume accepted changed source audio")
            print("resume guard: changed source track rejected")
        finally:
            folder_paths.set_output_directory(previous_output)

    print("chain smoke test passed")


if __name__ == "__main__":
    main()