File size: 108,216 Bytes
50d8c92
 
 
 
 
 
 
 
fdffd22
 
 
50d8c92
 
 
 
 
edc97dc
50d8c92
edc97dc
50d8c92
edc97dc
c331c82
50d8c92
 
 
 
 
 
edc97dc
 
50d8c92
edc97dc
fdffd22
 
 
 
50d8c92
 
edc97dc
 
 
 
 
 
 
 
 
 
 
 
 
 
50d8c92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2b388f0
 
 
50d8c92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2b388f0
50d8c92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
edc97dc
 
 
 
 
 
 
 
 
 
 
50d8c92
 
 
edc97dc
 
 
 
50d8c92
 
 
 
 
 
 
 
edc97dc
50d8c92
 
 
edc97dc
50d8c92
 
 
edc97dc
 
 
 
 
50d8c92
 
 
 
 
 
 
 
 
edc97dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c331c82
 
 
 
edc97dc
c331c82
edc97dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c331c82
 
edc97dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c331c82
 
 
edc97dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50d8c92
edc97dc
50d8c92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
edc97dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2b388f0
50d8c92
2b388f0
 
edc97dc
 
 
 
 
 
 
 
 
 
 
 
 
2b388f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e9e5be6
 
 
 
 
 
 
 
 
 
 
 
 
edc97dc
e9e5be6
 
 
 
edc97dc
 
 
e9e5be6
50d8c92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
edc97dc
 
 
50d8c92
edc97dc
 
 
 
 
50d8c92
 
2b388f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50d8c92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2b388f0
 
 
 
 
 
 
 
 
50d8c92
 
 
2b388f0
50d8c92
 
2b388f0
 
50d8c92
2b388f0
 
 
 
 
 
50d8c92
 
 
 
 
 
 
 
 
edc97dc
 
fdffd22
 
edc97dc
 
 
 
 
 
 
 
 
 
fdffd22
edc97dc
fdffd22
 
 
 
 
 
edc97dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50d8c92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
edc97dc
40e7b97
 
 
50d8c92
 
 
 
 
edc97dc
 
 
 
 
 
50d8c92
edc97dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50d8c92
 
 
edc97dc
50d8c92
 
 
edc97dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50d8c92
 
 
 
edc97dc
 
 
 
 
 
50d8c92
 
edc97dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40e7b97
 
 
edc97dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fdffd22
 
edc97dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50d8c92
 
 
edc97dc
 
 
 
 
 
 
 
50d8c92
 
 
 
 
 
 
 
e9e5be6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
edc97dc
 
 
 
 
 
 
 
 
 
 
 
e9e5be6
 
edc97dc
 
 
 
 
 
 
e9e5be6
 
 
 
 
 
 
 
 
 
 
edc97dc
 
 
 
 
 
 
 
 
 
 
e9e5be6
 
 
40e7b97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
edc97dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40e7b97
 
 
edc97dc
 
 
 
 
 
 
 
 
 
 
e9e5be6
 
 
 
 
 
edc97dc
e9e5be6
edc97dc
e9e5be6
 
 
 
 
edc97dc
 
e9e5be6
 
edc97dc
 
e9e5be6
edc97dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e9e5be6
edc97dc
 
 
 
 
 
e9e5be6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40e7b97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
edc97dc
40e7b97
 
edc97dc
 
40e7b97
 
 
 
 
edc97dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50d8c92
edc97dc
50d8c92
 
 
 
 
 
 
 
edc97dc
 
 
50d8c92
 
 
 
 
 
 
edc97dc
50d8c92
40e7b97
 
edc97dc
50d8c92
 
 
 
 
 
 
edc97dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40e7b97
 
edc97dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40e7b97
edc97dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40e7b97
edc97dc
 
 
 
40e7b97
edc97dc
 
 
 
 
 
 
40e7b97
 
edc97dc
40e7b97
edc97dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40e7b97
 
edc97dc
 
 
 
 
 
40e7b97
 
 
edc97dc
 
 
 
 
 
 
 
 
 
 
40e7b97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
edc97dc
 
 
 
 
 
 
 
 
 
 
40e7b97
 
 
 
 
 
edc97dc
 
40e7b97
 
 
 
 
 
 
 
edc97dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40e7b97
 
 
 
 
 
 
 
edc97dc
 
 
 
 
 
40e7b97
edc97dc
 
 
 
 
 
 
 
 
 
 
 
 
40e7b97
edc97dc
40e7b97
edc97dc
 
 
 
 
 
 
 
 
 
40e7b97
edc97dc
 
 
 
40e7b97
edc97dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40e7b97
 
edc97dc
 
 
40e7b97
 
edc97dc
 
 
40e7b97
 
 
edc97dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fdffd22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50d8c92
 
 
 
 
 
 
 
 
 
fdffd22
40e7b97
 
 
 
 
50d8c92
 
 
 
 
 
 
 
 
 
fdffd22
 
 
 
50d8c92
 
 
 
 
 
 
 
 
edc97dc
 
50d8c92
 
edc97dc
 
 
50d8c92
 
 
 
 
edc97dc
50d8c92
 
 
edc97dc
 
 
 
50d8c92
 
 
 
 
 
 
 
 
 
 
 
edc97dc
50d8c92
 
 
 
 
 
 
 
 
 
edc97dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40e7b97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
edc97dc
50d8c92
edc97dc
 
40e7b97
 
50d8c92
edc97dc
 
40e7b97
 
edc97dc
 
 
40e7b97
 
edc97dc
 
 
 
 
 
 
 
50d8c92
 
 
 
 
 
 
 
 
 
 
 
 
 
40e7b97
50d8c92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
edc97dc
 
50d8c92
edc97dc
 
 
 
 
 
50d8c92
 
 
 
 
 
 
 
 
 
 
 
edc97dc
 
 
50d8c92
e9e5be6
fdffd22
50d8c92
 
 
 
 
fdffd22
edc97dc
50d8c92
 
 
 
 
 
 
 
 
 
 
 
fdffd22
 
 
 
50d8c92
edc97dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40e7b97
edc97dc
 
 
 
 
40e7b97
 
 
 
 
 
 
 
edc97dc
 
40e7b97
 
 
edc97dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50d8c92
 
 
 
 
 
edc97dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50d8c92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
edc97dc
50d8c92
 
edc97dc
 
 
 
50d8c92
 
 
 
 
edc97dc
 
 
50d8c92
edc97dc
 
 
 
 
 
 
 
 
 
 
 
 
50d8c92
 
 
edc97dc
50d8c92
 
 
 
 
 
edc97dc
50d8c92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
"""Read and write MiniMax H3 RefMods, and turn one into a DiT reference block.

The file format is Luisa's (ComfyUI-MiniMaxH3Mod, format version 4 standalone,
version 5 bundle read-only). This module does not import that pack. A file
written here is a file her loader can open, and a file she wrote is a file
the rail can read.

The stored tensor is the VAE latent. Decoding it and encoding it again is a
different reference: the pooled latent would be gone. Callers hand the DiT
`visual_block()`, and show Qwen the pictures the builder encoded when the
file keeps them (`looks`), or a decode of the latent when it does not.
"""
from __future__ import annotations

import hashlib
import json
import math
import os
import re
import tempfile
import threading
from collections import OrderedDict

import torch
import torch.nn.functional as F

TAG = "HandTieClips"
META_KEY = "refmod_meta"
AUDIO_META_KEY = "audio_refmod_meta"
REFLORA_META_KEYS = ("h3_refmod_meta", "minimax_h3_refmod_meta")
FORMAT_VERSION = 4
BUNDLE_VERSION = 5
# The pictures the builder encoded, uint8 [N,H,W,3] beside `latent`. Luisa's
# loader reads `latent` by key and her bundle writer carries unknown tensors
# through, so the extra key does not cost the file its portability.
LOOKS_KEY = "looks"
SKIP_DIRS = {".git", "__pycache__", "graph_presets"}

CONCEPT_ROLES = (
    "generic",
    "identity",
    "wardrobe",
    "bodyshape",
    "helper",
    "pose_motion",
    "background",
    "style",
)
STEP_CURVES = ("full", "early_lock", "late_detail", "fade", "mid_peak")
MAX_COPIES = 3
MAX_STRENGTH = 3.0


def _fail(msg):
    raise ValueError(f"{TAG}: {msg}")

def source_problem(name, kinds=("image", "video")):
    """Why a mod_plan source cannot be used, as a sentence, or None.

    None means nothing is detectably wrong -- present, and a kind this slot
    takes. It does NOT mean a loader will succeed, so a caller that already
    watched one fail supplies its own `or "... could not be read"`. Having
    this return that sentence unconditionally made a perfectly good voice
    fail the build.

    A loader returning None means absent, wrong kind, or unreadable, and
    reporting all three as "not in h3_refs" sent someone looking for a file
    that was sitting right there. A RefMod is visual, so an audio pick is
    the likely mistake and gets told where a voice actually goes.
    """
    try:  # the offline checkers import this module without its package
        from . import media as _media
    except ImportError:
        import media as _media

    if not str(name or "").strip():
        return None
    if _media.resolve(name) is None:
        return f"{name} is not in h3_refs"
    kind = _media.kind_of(name)
    if kind not in kinds:
        if kind == "audio":
            return (f"{name} is a sound file. A RefMod's pictures are "
                    f"encoded; its voice goes in the voice slot.")
        want = " or ".join("picture" if k == "image" else k for k in kinds)
        return f"{name} is a {kind or 'unknown'} file, not a {want}"
    return None


def _ext_kind(name):
    """'image' | 'video' | 'audio' | None. `media.kind_of`, reached without
    importing it, so the parser stays free of the loaders."""
    try:
        from . import media as _media
    except ImportError:
        import media as _media
    return _media.kind_of(name)


def parse_mod_plan(text):
    """`(stills, clip, voice)` from the builder's `mod_plan` widget.

    `stills` is a list of reference filenames in the order they will occupy
    latent frames. `clip` is `{"file", "start", "end", "frames"}` or None.
    An empty widget is `([], None, "")`, which leaves the wired inputs in
    charge.

    `voice` is a sound file in h3_refs, and is the one field here that is
    not encoded into the latent -- a RefMod holds pictures. It is recorded
    so a reference row citing this mod inherits the voice instead of the
    author having to name it twice, which is what makes the file a
    character rather than a face. It may be written as `{"file", "start",
    "end"}` to trim it; `voice_window` reads that window, and this returns
    the name alone so every caller that only needs the file is unchanged.

    A still may be written as a bare string or as `{"file": ...}`; the panel
    writes the object form, a hand-typed plan is allowed either.
    """
    if isinstance(text, (dict, list)):
        data = text
    else:
        raw = (text or "").strip()
        if not raw:
            return [], None, ""
        try:
            data = json.loads(raw)
        except ValueError as exc:
            _fail(f"mod_plan is not valid JSON: {exc}")
    if isinstance(data, list):
        data = {"stills": data}
    if not isinstance(data, dict):
        _fail("mod_plan must be a JSON object or a list of stills")

    def _name(entry, where):
        if isinstance(entry, str):
            name = entry
        elif isinstance(entry, dict):
            name = entry.get("file") or entry.get("name") or ""
        else:
            _fail(f"{where} must be a filename or an object with file")
        name = str(name).strip()
        if not name:
            _fail(f"{where} has no file")
        if "\\" in name or "/" in name or name in (".", ".."):
            _fail(f"{where}: {name!r} must be a plain filename in h3_refs")
        return name

    stills_raw = data.get("stills") or []
    if not isinstance(stills_raw, list):
        _fail("mod_plan stills must be a list")
    stills, seen = [], set()
    for i, entry in enumerate(stills_raw):
        name = _name(entry, f"mod_plan still {i + 1}")
        if name in seen:
            _fail(f"mod_plan lists {name} twice. Each still is one latent frame.")
        seen.add(name)
        stills.append(name)

    clip_raw = data.get("clip")
    clip = None
    if clip_raw not in (None, "", {}, []):
        if isinstance(clip_raw, str):
            clip_raw = {"file": clip_raw}
        if not isinstance(clip_raw, dict):
            _fail("mod_plan clip must be a filename or an object")
        clip = {
            "file": _name(clip_raw, "mod_plan clip"),
            "start": max(0.0, float(clip_raw.get("start") or 0.0)),
            "end": max(0.0, float(clip_raw.get("end") or 0.0)),
            "frames": max(0, int(clip_raw.get("frames") or 0)),
        }
        if clip["end"] and clip["end"] <= clip["start"]:
            _fail(f"mod_plan clip window ends at {clip['end']}s, which is not "
                  f"after its {clip['start']}s start")
        if _ext_kind(clip["file"]) == "audio":
            _fail(f"mod_plan clip {clip['file']} is a sound file. A voice goes "
                  f"in the RefMod's voice slot, not in the clip slot.")

    voice = data.get("voice") or ""
    if voice not in (None, "", {}, []):
        voice = _name(voice, "mod_plan voice")
        if _ext_kind(voice) != "audio":
            _fail(f"mod_plan voice {voice} is not a sound file")
        voice_window(data)  # a reversed window fails here, before any encode
    else:
        voice = ""
    return stills, clip, voice


def tensor_digest(t):
    """Same bytes as store.tensor_digest. Kept here so a file read does not
    import the hop store."""
    a = t.detach().cpu().contiguous().numpy()
    h = hashlib.sha256()
    h.update(str(a.shape).encode())
    h.update(str(a.dtype).encode())
    h.update(a.tobytes())
    return h.hexdigest()[:16]


def register_folder():
    """Make `models/refmods` a folder_paths root. Safe to call more than once."""
    import folder_paths

    base = os.path.join(folder_paths.models_dir, "refmods")
    os.makedirs(base, exist_ok=True)
    try:
        known = [os.path.normcase(os.path.normpath(p))
                 for p in folder_paths.get_folder_paths("refmods")]
    except Exception:
        known = []
    if os.path.normcase(os.path.normpath(base)) not in known:
        folder_paths.add_model_folder_path("refmods", base)
    return base


def roots():
    """Every directory a RefMod may live in, the registered root first."""
    import folder_paths

    try:
        found = list(folder_paths.get_folder_paths("refmods"))
    except Exception:
        found = []
    base = os.path.join(folder_paths.models_dir, "refmods")
    if not found:
        found = [base]
    elif os.path.normcase(os.path.normpath(base)) not in {
            os.path.normcase(os.path.normpath(p)) for p in found}:
        found.insert(0, base)
    return found


def _lora_roots():
    """Registered LoRA folders for unified RefLoRA discovery."""
    import folder_paths

    try:
        return list(folder_paths.get_folder_paths("loras"))
    except Exception:
        base = os.path.join(folder_paths.models_dir, "loras")
        return [base] if os.path.isdir(base) else []


def _clean_name(name):
    """A relative safetensors stem. `..` and an absolute path are refused."""
    raw = str(name or "").strip().replace("\\", "/")
    prefix = ""
    if raw.lower().startswith("lora:"):
        prefix = "lora:"
        raw = raw[5:]
    if raw.lower().endswith(".safetensors"):
        raw = raw[: -len(".safetensors")]
    raw = raw.strip("/")
    if not raw:
        return ""
    parts = raw.split("/")
    if any(p in ("", ".", "..") for p in parts):
        _fail(f"refmod name {name!r} is not a file under the refmods folder")
    return prefix + "/".join(parts)


def resolve(name):
    """Absolute `.safetensors` path for a refmod (or `lora:` RefLoRA) name, or None."""
    stem = _clean_name(name)
    if not stem:
        return None
    is_lora = stem.lower().startswith("lora:")
    inner = stem[5:] if is_lora else stem
    rel = inner.replace("/", os.sep) + ".safetensors"
    search_roots = _lora_roots() if is_lora else roots()
    for root in search_roots:
        base = os.path.normpath(root)
        full = os.path.normpath(os.path.join(base, rel))
        if full != base and not full.startswith(base + os.sep):
            continue
        if os.path.isfile(full):
            return full
    return None


def _clean_desc_text(text):
    s = str(text or "").strip()
    # Clean legacy widget-migration artifact ("0") or empty sentinels.
    if s in ("0", "none", "None", "null"):
        return ""
    return s


def _member_tokens(member):
    if not isinstance(member, dict):
        return 0
    if member.get("kind") == "audio":
        t = int(member.get("ref_audio_t") or member.get("latent_t") or 0)
        return 2 * t
    t = int(member.get("latent_t") or 1)
    h = int(member.get("latent_h") or 0)
    w = int(member.get("latent_w") or 0)
    return max(0, t * (h // 2) * (w // 2))


# LRU keyed by (path, mtime_ns, size). It used to clear() when full, which with
# more than 512 LoRAs emptied it partway through every listing() walk, so each
# walk reopened every header (issue #1). Entries are small; evict oldest only.
_HEADER_CACHE = OrderedDict()
_HEADER_CACHE_LOCK = threading.Lock()
_HEADER_CACHE_MAX = 8192


def _stat_sig(path):
    try:
        st = os.stat(path)
        return (os.path.normcase(os.path.normpath(path)), int(st.st_mtime_ns), int(st.st_size))
    except OSError:
        return None


def _invalidate_header_cache(path=None):
    with _HEADER_CACHE_LOCK:
        if path is None:
            _HEADER_CACHE.clear()
            return
        norm = os.path.normcase(os.path.normpath(path))
        for k in [k for k in _HEADER_CACHE if k[0] == norm]:
            _HEADER_CACHE.pop(k, None)


def _scan_safetensors_header(path):
    """Single safe_open inspection returning (is_reflora, meta, has_looks_key, has_audio_key)."""
    sig = _stat_sig(path)
    if sig is None:
        return (False, None, False, False)
    with _HEADER_CACHE_LOCK:
        hit = _HEADER_CACHE.get(sig)
        if hit is not None:
            _HEADER_CACHE.move_to_end(sig)
    if hit is not None:
        return hit

    from safetensors import safe_open
    is_reflora = False
    meta = None
    has_looks_key = False
    has_audio_key = False
    try:
        with safe_open(path, framework="pt") as handle:
            hdr_meta = handle.metadata() or {}
            keys = list(handle.keys())
            key_set = set(keys)
            is_reflora = any(
                k == "refmod.latent" or k == "refmod.samples" or k.startswith("refmod.ref_")
                for k in key_set
            )
            has_looks_key = any(
                k == LOOKS_KEY or k.startswith("looks_") or k.startswith("enc_")
                or k.startswith("refmod.looks")
                for k in key_set
            )
            has_audio_key = any("audio_latent" in k for k in key_set)
            raw = hdr_meta.get(META_KEY) or hdr_meta.get(AUDIO_META_KEY)
            if raw:
                parsed = json.loads(raw)
                meta = parsed if isinstance(parsed, dict) else None
            else:
                for rk in REFLORA_META_KEYS:
                    if hdr_meta.get(rk):
                        parsed = json.loads(hdr_meta[rk])
                        if isinstance(parsed, dict):
                            data = dict(parsed)
                            data["_reflora"] = True
                            if "kind" not in data:
                                if "refmod.latent" in key_set:
                                    shape = handle.get_slice("refmod.latent").get_shape()
                                    data["kind"] = "video" if (len(shape) >= 3 and shape[2] > 1) else "image"
                                    if len(shape) == 5:
                                        data.setdefault("latent_t", int(shape[2]))
                                        data.setdefault("latent_h", int(shape[3]))
                                        data.setdefault("latent_w", int(shape[4]))
                                else:
                                    data["kind"] = "image"
                            meta = data
                        break
                if meta is None and "refmod.latent" in key_set:
                    shape = handle.get_slice("refmod.latent").get_shape()
                    meta = {
                        "name": os.path.splitext(os.path.basename(path))[0],
                        "kind": "video" if (len(shape) >= 3 and shape[2] > 1) else "image",
                        "latent_t": int(shape[2]) if len(shape) == 5 else 1,
                        "latent_h": int(shape[3]) if len(shape) == 5 else 0,
                        "latent_w": int(shape[4]) if len(shape) == 5 else 0,
                        "_reflora": True,
                        "_format_version": 4,
                    }
    except Exception:
        return (False, None, False, False)

    res = (is_reflora, meta, has_looks_key, has_audio_key)
    with _HEADER_CACHE_LOCK:
        _HEADER_CACHE[sig] = res
        _HEADER_CACHE.move_to_end(sig)
        while len(_HEADER_CACHE) > _HEADER_CACHE_MAX:
            _HEADER_CACHE.popitem(last=False)
    return res


def _meta_summary(meta, full=None, _scanned=None):
    kind = str((meta or {}).get("kind") or "refmod")
    role = str((meta or {}).get("concept_type") or (meta or {}).get("role") or "generic").strip()
    if role not in CONCEPT_ROLES:
        role = "generic"
    step_curve = str((meta or {}).get("default_step_curve") or "full").strip()
    if step_curve not in STEP_CURVES:
        step_curve = "full"
    ver = int((meta or {}).get("_format_version") or 4)
    fmt = "reflora" if (meta or {}).get("_reflora") else (f"v{ver}" if ver >= 4 else "v4")
    has_audio_latent = False
    members_count = 1
    tokens = 0
    has_looks = bool(int((meta or {}).get("looks") or 0))
    if kind == "bundle":
        mem = [m for m in ((meta or {}).get("members") or []) if isinstance(m, dict)]
        members_count = len(mem)
        vis_mem = [m for m in mem if m.get("kind") in ("image", "video")]
        has_audio_latent = any(m.get("kind") == "audio" for m in mem)
        tokens = sum(_member_tokens(m) for m in vis_mem)
        if any(int(m.get("looks") or 0) > 0 for m in vis_mem):
            has_looks = True
    elif kind == "audio":
        has_audio_latent = True
        tokens = _member_tokens(meta)
    else:
        tokens = _member_tokens(meta)
    if full and not has_looks:
        sc = _scanned if _scanned is not None else _scan_safetensors_header(full)
        has_looks = bool(sc[2])
        if not has_audio_latent:
            has_audio_latent = bool(sc[3])
    return {
        "kind": kind,
        "role": role,
        "default_step_curve": step_curve,
        "format": fmt,
        "tokens": tokens,
        "members_count": members_count,
        "has_audio_latent": has_audio_latent,
        "has_looks": has_looks,
    }


def listing():
    """`[{name, kind, role, tokens, ...}]` for the editor. `name` has no extension."""
    rows = []
    seen = set()
    for root in roots():
        if not os.path.isdir(root):
            continue
        for dirpath, dirnames, filenames in os.walk(root):
            dirnames[:] = [d for d in dirnames if d not in SKIP_DIRS and not d.startswith(".")]
            for filename in filenames:
                if not filename.endswith(".safetensors"):
                    continue
                full = os.path.join(dirpath, filename)
                rel = os.path.relpath(full, root).replace("\\", "/")
                stem = rel[: -len(".safetensors")]
                if stem in seen:
                    continue
                seen.add(stem)
                try:
                    mtime = os.path.getmtime(full)
                except OSError:
                    continue
                sc = _scan_safetensors_header(full)
                meta = sc[1]
                info = _meta_summary(meta, full=full, _scanned=sc)
                rows.append((mtime, stem, info, description_of(meta),
                             _listed_voice(meta)))
    # Also surface unified RefLoRAs from models/loras/ that actually embed `refmod.*` latents.
    for root in _lora_roots():
        if not os.path.isdir(root):
            continue
        for dirpath, dirnames, filenames in os.walk(root):
            dirnames[:] = [d for d in dirnames if d not in SKIP_DIRS and not d.startswith(".")]
            for filename in filenames:
                if not filename.endswith(".safetensors"):
                    continue
                full = os.path.join(dirpath, filename)
                sc = _scan_safetensors_header(full)
                if not sc[0]:
                    continue
                rel = os.path.relpath(full, root).replace("\\", "/")
                stem = "lora:" + rel[: -len(".safetensors")]
                if stem in seen:
                    continue
                seen.add(stem)
                try:
                    mtime = os.path.getmtime(full)
                except OSError:
                    continue
                meta = sc[1]
                info = _meta_summary(meta, full=full, _scanned=sc)
                rows.append((mtime, stem, info, description_of(meta),
                             _listed_voice(meta)))
    rows.sort(reverse=True)
    # `voice` is what a row citing this mod inherits, so the rail can show
    # and trim the recording that will actually ride.
    return [{
        "name": n,
        "kind": info["kind"],
        "role": info["role"],
        "default_step_curve": info["default_step_curve"],
        "format": info["format"],
        "tokens": info["tokens"],
        "members_count": info["members_count"],
        "has_audio_latent": info["has_audio_latent"],
        "has_looks": info["has_looks"],
        "description": d,
        "voice": v,
    } for _m, n, info, d, v in rows]


def _listed_voice(meta):
    """The header's voice as `{"file", "start", "end"}`, or None."""
    plan = (meta or {}).get("mod_plan") if isinstance(meta, dict) else None
    if not isinstance(plan, dict):
        return None
    try:
        _stills, _clip, voice = parse_mod_plan(plan)
        start, end = voice_window(plan)
    except ValueError:
        return None
    if not voice:
        return None
    return {"file": voice, "start": start, "end": end}


def description_of(meta):
    """The caption a RefMod carries in its header, or "".

    Luisa's layout has had a `description` field all along; this pack wrote it
    empty until the builder grew one, and other builders fill it. The editor
    offers it as the row's `desc` -- it is never read on the render path, so a
    caption the person has not accepted reaches no prompt. A bundle's own
    field wins, then the member `load_visual` would pick.
    """
    if not isinstance(meta, dict):
        return ""
    text = _clean_desc_text(meta.get("description"))
    if text or meta.get("kind") != "bundle":
        return text
    for member in meta.get("members") or []:
        if isinstance(member, dict) and member.get("kind") in ("image", "video"):
            cand = _clean_desc_text(member.get("description"))
            if cand:
                return cand
    return ""


def stamp(names):
    """`name:mtime` for IS_CHANGED. A missing file stays a stable token."""
    bits = []
    for name in names:
        if not name:
            continue
        path = resolve(name)
        try:
            bits.append(f"{name}:{os.path.getmtime(path)}" if path else f"{name}:missing")
        except OSError:
            bits.append(f"{name}:missing")
    return "|".join(bits)


def _is_reflora_file(path):
    """Fast header check for unified LoRA + RefMod `.safetensors` containers."""
    return bool(_scan_safetensors_header(path)[0])


def read_meta(path):
    """Header JSON, or None. Supports v4/v5 `refmod_meta`, `audio_refmod_meta`, and RefLoRA metadata."""
    meta = _scan_safetensors_header(path)[1]
    return dict(meta) if isinstance(meta, dict) else None


def voice_window(plan):
    """`(start, end)` seconds of the mod_plan's voice. `(0, 0)` is the whole file.

    Same sentinel as `media.clip_window`: an end of 0 is the end of the file.
    Only the object form of `voice` carries a window.
    """
    if not isinstance(plan, dict):
        try:
            plan = json.loads(plan) if str(plan or "").strip() else {}
        except ValueError:
            return 0.0, 0.0
    entry = plan.get("voice") if isinstance(plan, dict) else None
    if not isinstance(entry, dict):
        return 0.0, 0.0
    try:
        start = max(0.0, float(entry.get("start") or 0.0))
        end = max(0.0, float(entry.get("end") or 0.0))
    except (TypeError, ValueError):
        _fail("mod_plan voice window must be numbers of seconds")
    if end and end <= start:
        _fail(f"mod_plan voice window ends at {end:g}s, which is not after "
              f"its {start:g}s start")
    return start, end


def stored_plan(name):
    """The `mod_plan` a saved RefMod was built from, or `{}`.

    Empty for a mod built through the sockets, for one written before this
    field existed, or for a file that is not a RefMod at all -- all three
    mean the same thing to a caller: nothing to reload.
    """
    path = resolve(name)
    if path is None:
        return {}
    meta = read_meta(path) or {}
    plan = meta.get("mod_plan")
    if not isinstance(plan, dict):
        return {}
    try:
        stills, clip, voice = parse_mod_plan(plan)
    except ValueError:
        # A header that no longer parses is not worth failing a panel over.
        return {}
    if not (stills or clip or voice):
        return {}
    out = {"stills": stills, "clip": clip, "voice": voice}
    if voice:
        try:
            start, end = voice_window(plan)
        except ValueError:
            start, end = 0.0, 0.0
        if start or end:
            out["voice_start"], out["voice_end"] = start, end
    return out


def stored_voice(name):
    """`{"file", "start", "end"}` for the voice a saved RefMod names, or `{}`.

    A reference row that cites the mod and leaves its own voice empty
    inherits this, window and all, so the character is assembled once in the
    builder and the rail only has to point at it.
    """
    plan = stored_plan(name)
    if not plan.get("voice"):
        return {}
    return {"file": str(plan["voice"]),
            "start": float(plan.get("voice_start") or 0.0),
            "end": float(plan.get("voice_end") or 0.0)}


def _load_tensor(path, key):
    from safetensors.torch import load_file

    # Clone releases the file mapping so Windows can overwrite the file later.
    return load_file(path, device="cpu")[key].clone()


def _load_looks(path, key=LOOKS_KEY):
    """The kept pictures (`[N,H,W,3]` uint8), or None when absent."""
    from safetensors import safe_open

    try:
        with safe_open(path, framework="pt", device="cpu") as handle:
            keys = set(handle.keys())
            if key not in keys:
                return None
            looks = handle.get_tensor(key).clone()
    except Exception:
        return None
    if looks.ndim == 3 and looks.shape[-1] == 3:
        looks = looks.unsqueeze(0)
    if looks.ndim != 4 or looks.shape[-1] != 3 or looks.shape[0] < 1:
        print(f"[{TAG}] {os.path.basename(path)}: {key} tensor is "
              f"{tuple(looks.shape)}, not [N,H,W,3]. Showing Qwen a decode.",
              flush=True)
        return None
    return looks


def _load_enc_jpegs(path, member_indices=None):
    """Decode Fantastic/H3RefModPicker `enc_N` JPEG byte tensors if present."""
    from safetensors import safe_open

    try:
        with safe_open(path, framework="pt", device="cpu") as handle:
            keys = [k for k in handle.keys() if k.startswith("enc_")]
            if not keys:
                return None
            keys.sort(key=lambda s: int(s.split("_", 1)[1]) if s.split("_", 1)[1].isdigit() else 9999)
            if member_indices is not None:
                want = {f"enc_{i}" for i in member_indices}
                # Strict: a member without its own enc_N has none. Falling back to
                # every enc_ key showed Qwen the other members' photos as this one.
                keys = [k for k in keys if k in want]
                if not keys:
                    return None
            from PIL import Image  # noqa: PLC0415
            import io  # noqa: PLC0415
            import numpy as np  # noqa: PLC0415
            frames = []
            for k in keys:
                raw = handle.get_tensor(k).numpy().tobytes()
                with Image.open(io.BytesIO(raw)) as im:
                    arr = np.array(im.convert("RGB"), dtype=np.uint8, copy=True)
                frames.append(torch.from_numpy(arr))
            if not frames:
                return None
            # Only stack if dimensions match; otherwise return list or first frame.
            h0, w0 = frames[0].shape[:2]
            if all(f.shape[:2] == (h0, w0) for f in frames):
                return torch.stack(frames, dim=0)
            return frames[0].unsqueeze(0)
    except Exception:
        return None


def _as_visual(meta, latent, where):
    kind = str(meta.get("kind") or "")
    if kind not in ("image", "video"):
        _fail(f"{where} is a '{kind or 'unknown'}' RefMod. This pass reads "
              f"image and video mods.")
    if latent.ndim != 5 or latent.shape[0] != 1 or latent.shape[1] != 24:
        _fail(f"{where} latent must be [1,24,T,H,W], got {tuple(latent.shape)}")
    _b, _c, t, h, w = latent.shape
    if t < 1 or h < 2 or w < 2 or h % 2 or w % 2:
        _fail(f"{where} latent grid {t}x{h}x{w} is not a usable visual ref")
    if kind == "image":
        latent = latent[:, :, :1]
        t = 1
    latent = latent.contiguous()
    return {
        "kind": "image" if t == 1 else "video",
        "latent": latent,
        "latent_t": int(t),
        "latent_h": int(h),
        "latent_w": int(w),
        "name": str(meta.get("name") or where),
        "mode": str(meta.get("mode") or ""),
        "role": str(meta.get("concept_type") or meta.get("role") or "generic"),
        "default_step_curve": (norm_step_curve(meta.get("default_step_curve"))
                               if meta.get("default_step_curve")
                               else (third_party_step_curve(meta) or "full")),
        "digest": tensor_digest(latent),
        "tokens": int(t) * (int(h) // 2) * (int(w) // 2),
    }


def _first_tensor(*cands):
    for t in cands:
        if t is not None:
            return t
    return None


def _extract_looks_from_handle(handle, keys, key=LOOKS_KEY):
    if key not in keys:
        return None
    looks = handle.get_tensor(key).clone()
    if looks.ndim == 3 and looks.shape[-1] == 3:
        looks = looks.unsqueeze(0)
    if looks.ndim != 4 or looks.shape[-1] != 3 or looks.shape[0] < 1:
        return None
    return looks


def _extract_enc_jpegs_from_handle(handle, keys, member_indices=None):
    enc_keys = [k for k in keys if k.startswith("enc_")]
    if not enc_keys:
        return None
    enc_keys.sort(key=lambda s: int(s.split("_", 1)[1]) if s.split("_", 1)[1].isdigit() else 9999)
    if member_indices is not None:
        want = {f"enc_{i}" for i in member_indices}
        # Strict, as in _load_enc_jpegs: never another member's photos.
        enc_keys = [k for k in enc_keys if k in want]
        if not enc_keys:
            return None
    try:
        from PIL import Image  # noqa: PLC0415
        import io  # noqa: PLC0415
        import numpy as np  # noqa: PLC0415
        frames = []
        for k in enc_keys:
            raw = handle.get_tensor(k).numpy().tobytes()
            with Image.open(io.BytesIO(raw)) as im:
                arr = np.array(im.convert("RGB"), dtype=np.uint8, copy=True)
            frames.append(torch.from_numpy(arr))
        if not frames:
            return None
        h0, w0 = frames[0].shape[:2]
        if all(f.shape[:2] == (h0, w0) for f in frames):
            return torch.stack(frames, dim=0)
        return frames[0].unsqueeze(0)
    except Exception:
        return None


def load_bundle_visuals(name, members_filter=None):
    """All visual blocks inside `name` (v4 standalone, v5 bundle, or RefLoRA).

    Returns a list of visual dicts. `members_filter` (optional list of 0-based
    indices) restricts a v5 bundle to only those visual members without rebuilding.
    """
    path = resolve(name)
    if path is None:
        return []
    meta = read_meta(path)
    if not isinstance(meta, dict):
        _fail(f"{name} has no {META_KEY} header. It is not a RefMod.")

    from safetensors import safe_open

    # RefLoRA container (`refmod.latent` / `refmod.samples` / `refmod.ref_*`)
    if meta.get("_reflora"):
        out = []
        with safe_open(path, framework="pt", device="cpu") as fh:
            keys = set(fh.keys())
            if "refmod.latent" in keys:
                lat = fh.get_tensor("refmod.latent").clone()
                item = _as_visual(meta, lat, name)
                looks = _first_tensor(
                    _extract_looks_from_handle(fh, keys, "refmod.looks"),
                    _extract_looks_from_handle(fh, keys, LOOKS_KEY),
                )
                item["looks"] = looks
                item["looks_digest"] = tensor_digest(looks) if looks is not None else None
                item["member_index"] = 0
                out.append(item)
            else:
                ref_keys = sorted(
                    [k for k in keys if k.startswith("refmod.ref_")],
                    key=lambda s: int(s.split("_")[-1]) if s.split("_")[-1].isdigit() else 9999,
                )
                for idx, rk in enumerate(ref_keys):
                    lat = fh.get_tensor(rk).clone()
                    if lat.ndim != 5 or lat.shape[1] != 24:
                        continue
                    k_meta = {"name": f"{name}#{idx}", "kind": "video" if lat.shape[2] > 1 else "image"}
                    item = _as_visual(k_meta, lat, name)
                    looks = _extract_looks_from_handle(fh, keys, f"refmod.looks_{idx}")
                    item["looks"] = looks
                    item["looks_digest"] = tensor_digest(looks) if looks is not None else None
                    item["member_index"] = idx
                    out.append(item)
        if not out:
            _fail(f"{name} RefLoRA has no MiniMax-H3 [1,24,T,H,W] visual latent.")
        return out

    if meta.get("kind") == "bundle":
        members = meta.get("members") or []
        if not isinstance(members, list):
            _fail(f"{name} bundle has no members list")
        wanted = None
        if members_filter:
            wanted = {int(x) for x in members_filter}
        out = []
        vis_indices = [i for i, m in enumerate(members) if isinstance(m, dict) and m.get("kind") in ("image", "video")]
        if not vis_indices:
            _fail(f"{name} has no visual member. Audio RefMods are not applied "
                  f"by this node.")
        with safe_open(path, framework="pt", device="cpu") as fh:
            keys = set(fh.keys())
            for i in vis_indices:
                if wanted is not None and i not in wanted:
                    continue
                member = members[i]
                latent = fh.get_tensor(f"ref_{i}").clone()
                loaded = _as_visual(member, latent, name)
                looks = _first_tensor(
                    _extract_looks_from_handle(fh, keys, f"looks_{i}"),
                    _extract_looks_from_handle(fh, keys, LOOKS_KEY) if len(vis_indices) == 1 else None,
                    # one visual member: its photos may be stored unnumbered-to-it
                    _extract_enc_jpegs_from_handle(fh, keys, [i] if len(vis_indices) > 1 else None),
                )
                loaded["looks"] = looks
                loaded["looks_digest"] = tensor_digest(looks) if looks is not None else None
                loaded["member_index"] = i
                if member.get("yaw") is not None:
                    loaded["yaw"] = float(member["yaw"])
                if loaded.get("role") == "generic" and meta.get("concept_type"):
                    loaded["role"] = str(meta.get("concept_type"))
                if loaded.get("default_step_curve") == "full" and meta.get("default_step_curve"):
                    loaded["default_step_curve"] = norm_step_curve(meta.get("default_step_curve"))
                if loaded.get("default_step_curve") == "full":
                    loaded["default_step_curve"] = third_party_step_curve(meta) or "full"
                out.append(loaded)
        if not out and wanted is not None:
            _fail(f"{name}: selected member indices {sorted(wanted)} matched no visual member.")
        return out

    if meta.get("kind") == "audio":
        _fail(f"{name} is an audio-only RefMod. Visual references require an image or video RefMod.")
    with safe_open(path, framework="pt", device="cpu") as fh:
        keys = set(fh.keys())
        latent = fh.get_tensor("latent").clone()
        loaded = _as_visual(meta, latent, name)
        looks = _first_tensor(
            _extract_looks_from_handle(fh, keys, LOOKS_KEY),
            _extract_enc_jpegs_from_handle(fh, keys),
        )
    loaded["looks"] = looks
    loaded["looks_digest"] = tensor_digest(looks) if looks is not None else None
    loaded["member_index"] = 0
    return [loaded]


def load_visual(name):
    """The first visual latent behind `name`, or None when the file is absent."""
    items = load_bundle_visuals(name)
    if not items:
        return None
    if len(items) > 1:
        print(f"[{TAG}] {name}: loaded primary visual member ({len(items)} total in bundle)", flush=True)
    return items[0]


def load_audio_latent(name):
    """Pre-encoded `[1,32,2,T]` audio latent inside `name`, or None."""
    path = resolve(name)
    if path is None:
        return None
    meta = read_meta(path)
    if not isinstance(meta, dict):
        return None
    from safetensors import safe_open
    try:
        with safe_open(path, framework="pt", device="cpu") as fh:
            keys = set(fh.keys())
            tensor = None
            src_name = ""
            sr = int(meta.get("sample_rate") or 32000)
            if meta.get("_reflora") and "refmod.audio_latent" in keys:
                tensor = fh.get_tensor("refmod.audio_latent").clone()
            elif meta.get("kind") == "bundle":
                for i, member in enumerate(meta.get("members") or []):
                    if isinstance(member, dict) and member.get("kind") == "audio" and f"ref_{i}" in keys:
                        tensor = fh.get_tensor(f"ref_{i}").clone()
                        sr = int(member.get("sample_rate") or sr)
                        src_name = os.path.basename(str(member.get("name") or "").strip())
                        break
            elif meta.get("kind") == "audio" and "latent" in keys:
                tensor = fh.get_tensor("latent").clone()
            if tensor is None or tensor.ndim != 4 or tensor.shape[1] != 32 or tensor.shape[2] != 2:
                return None
            return {
                "audio_latent": tensor.contiguous(),
                "ref_audio_t": int(tensor.shape[-1]),
                "sample_rate": sr,
                "digest": tensor_digest(tensor),
                "name": src_name,
            }
    except Exception:
        return None


def _blur_latent(z, factor=8):
    """Luisa's weakening target: a spatial/temporal low-pass, not noise and not zero."""
    if z.dim() == 4:
        b, c, stereo, t = z.shape
        if t <= 1:
            return z
        flat = z.reshape(b * c * stereo, 1, t).float()
        down = F.adaptive_avg_pool1d(flat, max(1, t // factor))
        return F.interpolate(down, size=t, mode="linear", align_corners=False).reshape_as(z).to(dtype=z.dtype)
    if z.dim() != 5:
        _fail(f"cannot blur a RefMod latent of shape {tuple(z.shape)}")
    t, h, w = z.shape[2], z.shape[3], z.shape[4]
    down = F.adaptive_avg_pool3d(z.float(), (t, max(1, h // factor), max(1, w // factor)))
    up = F.interpolate(down, size=(t, h, w), mode="trilinear", align_corners=False)
    return up.to(dtype=z.dtype)


def refine_cutout(mask, frames, low=0.1, pocket=0.005):
    """A matting model's soft mask, made solid inside and clean at the edge.

    The two halves of a cutout fail differently, so they are treated apart.

    Inside, a matting model is unsure as well as at the edge -- on a
    character sheet the robes came back as grey patches, and every grey
    pixel is subject half-lost. So the question is turned round: backdrop
    is what is confidently empty (`< low`) AND touches the frame's border
    or is a pocket larger than `pocket` of it. Everything else, shrunk
    back from its outline, is a core held at full weight.

    At the edge, the model's own soft alpha is kept -- that is what it is
    good at -- except that on a plain sheet an edge pixel the colour of
    the sheet is let go. That removes the white rim a cutout leaves round
    a figure, the thin panel dividers a sheet grid is drawn with, and the
    wedges of sheet caught between hair and shoulder. What the model was
    sure of inside the figure is never keyed, so a white collar stays.
    """
    import numpy as np  # noqa: PLC0415
    from scipy import ndimage  # noqa: PLC0415

    out = []
    for raw_t, px_t in zip(mask, frames):
        raw = raw_t.float().cpu().numpy()
        px = px_t[..., :3].float().cpu().numpy()
        labels, count = ndimage.label(raw < low)
        if count:
            edge = np.unique(np.concatenate(
                [labels[0], labels[-1], labels[:, 0], labels[:, -1]]))
            sizes = np.bincount(labels.ravel(), minlength=count + 1)
            backdrop_ids = set(int(i) for i in edge if i) | set(
                int(i) for i in np.nonzero(sizes >= pocket * raw.size)[0] if i)
            backdrop = np.isin(labels, sorted(backdrop_ids))
        else:
            backdrop = np.zeros_like(raw, dtype=bool)
        inset = max(1, round(min(raw.shape) / 100))
        core = ndimage.binary_erosion(~backdrop, iterations=inset)
        m = np.maximum(raw, core.astype(np.float32))

        sure = backdrop & (raw < 0.02)
        if sure.sum() > 0.01 * raw.size:
            sheet = np.median(px[sure], axis=0)
            spread = float(np.abs(px[sure] - sheet).mean())
            if spread < 0.06:  # a plain sheet, not a photographed room
                near = np.abs(px - sheet).max(axis=-1)
                key = np.clip((near - 0.04) / 0.12, 0.0, 1.0)
                m = np.where(core & (raw >= 0.9), m, m * key)
        out.append(torch.from_numpy(m.astype(np.float32)))
    return torch.stack(out)


def pull_push_fill(frames, mask):
    """Every pixel coloured from the subject nearest it, smoothly.

    The standard pull-push hole fill: pool colour-times-weight and weight
    down to one pixel, then walk back up, keeping a level's own colour in
    proportion to how much subject it holds and taking the coarser guess
    for the rest. The backdrop ends up a soft extension of the subject's
    own edge colours -- no structure of its own, and none of its tone.
    """
    c = frames[..., :3].float().movedim(-1, 1)          # [T, 3, H, W]
    w = mask.float().to(c.device).unsqueeze(1)          # [T, 1, H, W]
    colours, weights = [c * w], [w]
    while max(colours[-1].shape[-2:]) > 1:
        colours.append(F.avg_pool2d(colours[-1], 2, ceil_mode=True))
        weights.append(F.avg_pool2d(weights[-1], 2, ceil_mode=True))
    fill = colours[-1] / weights[-1].clamp_min(1e-6)
    for cl, wl in zip(reversed(colours[:-1]), reversed(weights[:-1])):
        coarse = F.interpolate(fill, size=cl.shape[-2:], mode="bilinear",
                               align_corners=False)
        own = cl / wl.clamp_min(1e-6)
        fill = wl * own + (1.0 - wl) * coarse
    return fill.movedim(1, -1).clamp(0.0, 1.0)


def _blur_bg_pixels(px, mask):
    """Smooth heavy spatial blur of background pixels (Fantastic-style) for zero edge halos."""
    c = px[..., :3].float().movedim(-1, 1)  # [T, 3, H, W]
    _, _, h, w = c.shape
    scale = max(8, round(min(h, w) / 24))
    bh, bw = max(2, h // scale), max(2, w // scale)
    down = F.adaptive_avg_pool2d(c, (bh, bw))
    blurred = F.interpolate(down, size=(h, w), mode="bicubic", align_corners=False).movedim(1, -1).clamp(0.0, 1.0)
    # Softly expand mask perimeter so hairline edges don't glow against dark backgrounds
    m_dil = F.max_pool2d(mask.float().to(c.device).unsqueeze(1), kernel_size=7, stride=1, padding=3).squeeze(1)
    edge_soft = F.avg_pool2d(m_dil.unsqueeze(1), kernel_size=9, stride=1, padding=4).squeeze(1)
    return blurred, edge_soft


def isolate_subject(frames, mask, retention=0.0, mode="pull_push"):
    """`[T,H,W,C]` canvas frames with the backdrop replaced, before encode.

    `mode`:
      * `pull_push` (default legacy): edge-color pull-push fill
      * `blur_bg`: blurred background (keeps natural lighting balance with zero pull-push color streaks)
      * `neutral_gray`: 50% studio neutral backdrop
    """
    m = mask.float().to(frames.device)
    if m.shape[0] == 1 and frames.shape[0] > 1:
        m = m.expand(frames.shape[0], -1, -1)
    if tuple(m.shape) != tuple(frames.shape[:3]):
        _fail(f"subject mask {tuple(m.shape)} does not cover frames "
              f"{tuple(frames.shape[:3])}")
    r = min(1.0, max(0.0, float(retention)))
    if r >= 1.0 or float(m.amax()) <= 0.0:
        return frames
    px = frames[..., :3].float()
    bg_mode = str(mode or "pull_push").strip().lower()
    if bg_mode == "blur_bg":
        bg, soft_m = _blur_bg_pixels(px, m)
        keep = (r + (1.0 - r) * torch.maximum(m, soft_m * 0.65)).unsqueeze(-1)
    elif bg_mode == "neutral_gray":
        bg = torch.full_like(px, 0.5)
        keep = (r + (1.0 - r) * m).unsqueeze(-1)
    else:
        bg = pull_push_fill(px, m)
        keep = (r + (1.0 - r) * m).unsqueeze(-1)
    out = keep * px + (1.0 - keep) * bg
    return out.to(frames.dtype)


CROP_MIN_EDGE = 320


def ensure_min_size(frames, mask, min_edge=CROP_MIN_EDGE):
    """Upscale `frames` `[N,H,W,C]` (and `mask` `[N,H,W]`) so the short edge is at
    least `min_edge`: a crop of a small subject came out 64-px wide, and the H3
    VAE tiler cut a zero-size edge tile from it."""
    import torch.nn.functional as _F  # noqa: PLC0415
    h, w = int(frames.shape[1]), int(frames.shape[2])
    if min(h, w) >= int(min_edge):
        return frames, mask
    s = float(min_edge) / float(min(h, w))
    th, tw = max(int(min_edge), int(round(h * s))), max(int(min_edge), int(round(w * s)))
    f = _F.interpolate(frames.movedim(-1, 1).float(), size=(th, tw), mode="bicubic",
                       align_corners=False).movedim(1, -1).clamp(0, 1).to(frames.dtype)
    m = mask
    if mask is not None:
        m = _F.interpolate(mask.float().unsqueeze(1), size=(th, tw), mode="bilinear",
                           align_corners=False).squeeze(1).clamp(0, 1).to(mask.dtype)
    return f, m


def crop_to_subject(frames, mask, margin=1.6, min_edge=CROP_MIN_EDGE):
    """Subject-centered bounding-box crop with margin (1.4-2.0x), rounded to 32px
    grid, never narrower or shorter than `min_edge` (or the frame)."""
    import numpy as np  # noqa: PLC0415

    m_f = mask.float().to(frames.device)
    if m_f.shape[0] == 1 and frames.shape[0] > 1:
        m_f = m_f.expand(frames.shape[0], -1, -1)
    union = (m_f > 0.25).any(dim=0).cpu().numpy()
    ys, xs = np.nonzero(union)
    if len(ys) < 16:
        return frames, mask
    h, w = int(frames.shape[1]), int(frames.shape[2])
    y0, y1 = int(ys.min()), int(ys.max()) + 1
    x0, x1 = int(xs.min()), int(xs.max()) + 1
    bw = max(64, x1 - x0)
    bh = max(64, y1 - y0)
    marg = max(1.1, float(margin or 1.6))
    floor = max(64, int(min_edge or 0))
    cw = min(w, max(floor, int(round(bw * marg / 32.0)) * 32))
    ch = min(h, max(floor, int(round(bh * marg / 32.0)) * 32))
    if cw >= w and ch >= h:
        return frames, mask
    cx = (x0 + x1) // 2
    cy = (y0 + y1) // 2
    lx = max(0, min(w - cw, cx - cw // 2))
    ty = max(0, min(h - ch, cy - ch // 2))
    return frames[:, ty:ty + ch, lx:lx + cw], mask[:, ty:ty + ch, lx:lx + cw]


def mask_to_canvas(mask, tw, th, fit_mode="crop"):
    """`[T,H,W]` MASK fit onto the canvas the way `to_canvas` fits pixels."""
    import comfy.utils

    if mask.dim() == 2:
        mask = mask.unsqueeze(0)
    if mask.dim() != 3:
        _fail(f"a subject mask must be [T,H,W], got {tuple(mask.shape)}")
    crop = "center" if fit_mode == "crop" else "disabled"
    m = comfy.utils.common_upscale(mask.float().unsqueeze(1), int(tw), int(th),
                                   "bilinear", crop)
    return m.squeeze(1).clamp(0.0, 1.0)


CUTOUT_MODEL = "isnet-general-use"
_cutout_session = None
_sam_predictor = None
_sam_path_loaded = ""


def _find_sam_checkpoint():
    import folder_paths

    cands = []
    try:
        for p in folder_paths.get_folder_paths("sams"):
            cands.append(p)
    except Exception:
        pass
    cands.append(os.path.join(folder_paths.models_dir, "sams"))
    for root in cands:
        if not os.path.isdir(root):
            continue
        for fn in sorted(os.listdir(root)):
            if fn.lower().endswith((".pth", ".pt", ".safetensors")):
                return os.path.join(root, fn)
    return ""


def _cutout_sam(frames):
    """Subject mask via ComfyUI's installed SAM (`models/sams/`)."""
    global _sam_predictor, _sam_path_loaded
    import numpy as np  # noqa: PLC0415
    from segment_anything import SamPredictor, sam_model_registry  # noqa: PLC0415

    ckpt = _find_sam_checkpoint()
    if not ckpt:
        raise FileNotFoundError("No SAM checkpoint in models/sams/")
    if _sam_predictor is None or _sam_path_loaded != ckpt:
        low = os.path.basename(ckpt).lower()
        arch = "vit_h" if "vit_h" in low else ("vit_l" if "vit_l" in low else "vit_b")
        sam = sam_model_registry[arch](checkpoint=ckpt)
        dev = "cuda" if torch.cuda.is_available() else "cpu"
        sam.to(device=dev)
        _sam_predictor = SamPredictor(sam)
        _sam_path_loaded = ckpt

    out = []
    for frame in frames:
        rgb = (frame[..., :3].float().clamp(0, 1) * 255).round().byte().cpu().numpy()
        h, w = rgb.shape[:2]
        _sam_predictor.set_image(rgb)
        # Foreground center + upper-center points + central box prompt
        pts = np.array([[w * 0.5, h * 0.45], [w * 0.5, h * 0.25], [w * 0.5, h * 0.70]])
        lbls = np.array([1, 1, 1])
        box = np.array([w * 0.08, h * 0.04, w * 0.92, h * 0.96])
        masks, scores, _ = _sam_predictor.predict(
            point_coords=pts, point_labels=lbls, box=box, multimask_output=True)
        best = int(np.argmax(scores))
        out.append(torch.from_numpy(masks[best].astype(np.float32)))
    return torch.stack(out)


def cutout_masks(frames, backend="auto_rembg"):
    """`[T,H,W]` subject masks for `[T,H,W,C]` canvas frames.

    `backend`: `"auto_rembg"` (default, uses `rembg`) or `"auto_sam"`
    (uses SAM from `models/sams/`, falling back to `rembg` if unavailable).
    """
    if str(backend or "").strip().lower() == "auto_sam":
        try:
            return _cutout_sam(frames)
        except Exception as exc:
            print(f"[{TAG}] SAM cutout unavailable ({exc}); falling back to rembg", flush=True)

    global _cutout_session
    try:
        from rembg import new_session, remove  # noqa: PLC0415
    except ImportError:
        _fail("subject_mask=auto needs rembg (pip install rembg), or wire a "
              "MASK into the builder's mask socket")
    import numpy as np  # noqa: PLC0415
    from PIL import Image  # noqa: PLC0415

    if _cutout_session is None:
        _cutout_session = new_session(CUTOUT_MODEL)
    out = []
    for frame in frames:
        px = (frame[..., :3].float().clamp(0, 1) * 255).round().byte().cpu().numpy()
        m = remove(Image.fromarray(px), session=_cutout_session, only_mask=True)
        out.append(torch.from_numpy(np.asarray(m.convert("L"), dtype=np.float32) / 255.0))
    return torch.stack(out)


# A third-party RefMod (MiniMaxH3Mod's `refmod_config`) stores its step curve as
# (direction, shape, value): the envelope's direction over denoising progress, how it
# travels between its endpoints, and the non-zero endpoint. HTC evaluates those natively
# (own implementation of the documented envelopes) instead of mapping to a named curve.
_TRIPLE_DIRS = {"constant": "constant", "concept_at_start": "concept_at_start",
                "concept_at_middle": "concept_at_middle", "concept_at_end": "concept_at_end",
                "concept_at_ends": "concept_at_ends", "decrease": "concept_at_start",
                "increase": "concept_at_end"}
_TRIPLE_SHAPES = ("linear", "ease", "sigmoid", "tanh", "quadratic", "cubic",
                  "exponential", "stair", "elastic", "bump", "dip")


def norm_step_curve(curve):
    """A step curve as HTC uses it: a STEP_CURVES name, a `[direction, shape, value]`
    triple, or "full" for anything else."""
    if isinstance(curve, (list, tuple)) and len(curve) == 3:
        d = _TRIPLE_DIRS.get(str(curve[0]).strip().lower())
        sh = str(curve[1]).strip().lower()
        try:
            v = float(curve[2])
        except (TypeError, ValueError):
            return "full"
        if d and sh in _TRIPLE_SHAPES:
            if d == "constant" and sh == "linear" and v >= 1.0:
                return "full"
            return [d, sh, v]
        return "full"
    c = str(curve or "full").strip().lower()
    return c if c in STEP_CURVES else "full"


def third_party_step_curve(meta):
    """`[direction, shape, value]` from a RefMod's `refmod_config` JSON, or None."""
    raw = (meta or {}).get("refmod_config")
    try:
        cfg = json.loads(raw) if isinstance(raw, str) and raw.strip() else (raw or {})
    except ValueError:
        return None
    sc = norm_step_curve((cfg or {}).get("step_curve")) if isinstance(cfg, dict) else "full"
    return sc if isinstance(sc, list) else None


def _ease(shape, x):
    import math
    if shape == "ease":
        return x * x * (3.0 - 2.0 * x)
    if shape == "sigmoid":
        return 1.0 / (1.0 + math.exp(-12.0 * (x - 0.5)))
    if shape == "tanh":
        return 0.5 * (math.tanh(8.0 * (x - 0.5)) + 1.0)
    if shape == "quadratic":
        return x * x
    if shape == "cubic":
        return x ** 3
    if shape == "exponential":
        return 2.0 ** x - 1.0
    if shape == "stair":
        return min(1.0, math.floor(x * 4) / 3.0)
    if shape == "elastic":
        if x <= 0.0 or x >= 1.0:
            return max(0.0, min(1.0, x))
        return 2.0 ** (-10.0 * x) * math.sin((x * 10.0 - 0.75) * (2.0 * math.pi / 3.0)) + 1.0
    if shape == "bump":
        return 1.0 - abs(2.0 * x - 1.0)
    if shape == "dip":
        return abs(2.0 * x - 1.0)
    return x


def _triple_value(spec, x):
    d, sh, v = spec
    v = float(v)
    if d == "constant":
        return v if sh == "linear" else max(0.0, min(1.0, v * _ease(sh, x)))
    y = _ease(sh, x)
    if d == "concept_at_end":
        w = y
    elif d == "concept_at_start":
        w = 1.0 - y
    elif d == "concept_at_middle":
        w = 1.0 - abs(2.0 * y - 1.0)
    else:  # concept_at_ends
        w = abs(2.0 * y - 1.0)
    return max(0.0, min(1.0, v * w))


def step_curve_value(curve_name, progress):
    """Strength in [0, 1] at denoising `progress` (0 = noisy start, 1 = clean end).
    `curve_name` is a STEP_CURVES name or a third-party `[direction, shape, value]`."""
    import math

    if isinstance(curve_name, (list, tuple)):
        spec = norm_step_curve(curve_name)
        if isinstance(spec, list):
            return _triple_value(spec, min(1.0, max(0.0, float(progress))))
        return 1.0
    c = str(curve_name or "full").strip().lower()
    x = min(1.0, max(0.0, float(progress)))
    if c == "early_lock":
        # Strong composition/anatomy anchor for first ~45%, smooth floor 0.15 late.
        e = 0.5 * (1.0 + math.cos(math.pi * min(1.0, x / 0.72)))
        return 0.15 + 0.85 * e
    if c == "late_detail":
        # Gentle early framing (0.25), ramps to 1.0 in the detail half.
        e = 0.5 * (1.0 - math.cos(math.pi * max(0.0, min(1.0, (x - 0.15) / 0.65))))
        return 0.25 + 0.75 * e
    if c == "fade":
        return max(0.0, 1.0 - x)
    if c == "mid_peak":
        return 0.25 + 0.75 * (1.0 - abs(2.0 * x - 1.0))
    return 1.0


def apply_strength(latent, strength):
    """`w * latent + (1-w) * blur`. `>= 1` returns the same tensor."""
    w = float(strength)
    if w >= 1.0:
        return latent
    if w <= 0.0:
        return None
    return w * latent + (1.0 - w) * _blur_latent(latent)


def visual_block(loaded, strength, step_curve="full"):
    """Single `minimax_refs` block for this mod, or None when strength drops it."""
    latent = apply_strength(loaded["latent"], min(1.0, float(strength)))
    if latent is None:
        return None
    block = {
        "kind": loaded["kind"],
        "latent_h": int(latent.shape[3]),
        "latent_w": int(latent.shape[4]),
        "latent": latent,
        "refmod": True,
    }
    sc = norm_step_curve(step_curve or loaded.get("default_step_curve") or "full")
    if sc != "full":
        block["step_curve"] = sc
    if loaded["kind"] == "video":
        block["latent_t"] = int(latent.shape[2])
        block["ref_audio_t"] = 0
        block["audio_latent"] = None
    return block


def visual_blocks(loaded, strength, step_curve="full"):
    """One or more `minimax_refs` blocks. `strength > 1.0` stacks copies + residual blur."""
    import math

    w = min(MAX_STRENGTH, max(0.0, float(strength)))
    if w <= 0.0:
        return []
    if w <= 1.0:
        b = visual_block(loaded, w, step_curve=step_curve)
        return [b] if b is not None else []
    n_full = min(MAX_COPIES, int(math.floor(w)))
    rem = round(w - n_full, 4)
    out = []
    for _ in range(n_full):
        b = visual_block(loaded, 1.0, step_curve=step_curve)
        if b is not None:
            out.append(b)
    if rem >= 0.05 and len(out) < MAX_COPIES:
        b = visual_block(loaded, rem, step_curve=step_curve)
        if b is not None:
            out.append(b)
    return out


def audio_block(loaded_audio, strength=1.0, step_curve="full"):
    """`minimax_refs` standalone audio block from pre-encoded `[1,32,2,T]` latent."""
    if not loaded_audio or loaded_audio.get("audio_latent") is None:
        return None
    z = apply_strength(loaded_audio["audio_latent"], min(1.0, float(strength)))
    if z is None:
        return None
    block = {
        "kind": "audio",
        "ref_audio_t": int(z.shape[-1]),
        "audio_latent": z,
        "refmod": True,
    }
    sc = norm_step_curve(step_curve or "full")
    if sc != "full":
        block["step_curve"] = sc
    return block


_STEP_CURVE_SEQ = 0


def attach_step_curve_wrapper(model):
    """Attach per-RefMod denoising step-curve mixing (zero cost when no block carries `step_curve`)."""
    global _STEP_CURVE_SEQ
    if model is None or not hasattr(model, "add_wrapper_with_key"):
        return model
    from contextvars import ContextVar
    import comfy.patcher_extension as _pe

    _STEP_CURVE_SEQ += 1
    key = f"htc_refmod_step_curve_{_STEP_CURVE_SEQ}"
    start_var = ContextVar(key, default=1000.0)
    shift_var = ContextVar(key + "_shift", default=1.0)
    blur_cache = {}

    def _cached_blur(z):
        ck = (id(z), z.device, z.dtype)
        hit = blur_cache.get(ck)
        if hit is not None:
            return hit
        if len(blur_cache) > 64:
            blur_cache.clear()
        b = _blur_latent(z)
        blur_cache[ck] = b
        return b

    def _schedule(executor, noise, latent_image, sampler, sigmas, *args, **kwargs):
        blur_cache.clear()
        try:
            sampling = executor.class_obj.model_patcher.get_model_object("model_sampling")
            start = float(sampling.timestep(sigmas[0]))
        except Exception:
            start = 1000.0
        tok = start_var.set(start)
        tok_s = shift_var.set(_sampling_shift(executor))
        try:
            return executor(noise, latent_image, sampler, sigmas, *args, **kwargs)
        finally:
            start_var.reset(tok)
            shift_var.reset(tok_s)
            blur_cache.clear()

    def _wrapper(executor, x, timestep, context, transformer_options={}, **kwargs):
        payload = kwargs.get("minimax_payload")
        refs = (payload or {}).get("refs") or []
        if not payload or not any(isinstance(r, dict) and r.get("step_curve") for r in refs):
            return executor(x, timestep, context, transformer_options=transformer_options, **kwargs)
        prog = _denoise_progress(transformer_options, timestep, start_var.get(),
                                 shift_var.get())
        mixed = dict(payload)
        weights = []
        kfs = payload.get("keyframes") or []
        for field, latent_key in (("cond_video_latents", "latent"),
                                  ("cond_audio_latents", "audio_latent")):
            cond = payload.get(field)
            if not cond:
                continue
            out = list(cond)
            idx = sum(1 for k in kfs if k.get(latent_key) is not None)
            changed = False
            for r in refs:
                if r.get(latent_key) is None:
                    continue
                if idx >= len(out):
                    break
                sc = r.get("step_curve")
                if sc and sc != "full":
                    w = step_curve_value(sc, prog)
                    weights.append(w)
                    if w < 0.999 and not REFMOD_VROW_CURVE:
                        z = out[idx]
                        out[idx] = (w * z + (1.0 - w) * _cached_blur(z)).to(dtype=z.dtype)
                        changed = True
                idx += 1
            if changed:
                mixed[field] = out
        # the step cache (h3_cache) must not reuse a residual across moved weights
        transformer_options = dict(transformer_options or {})
        transformer_options["_htc_step_curve_w"] = tuple(weights)
        return executor(x, timestep, context, transformer_options=transformer_options,
                        **dict(kwargs, minimax_payload=mixed))

    out = model.clone()
    out.add_wrapper_with_key(_pe.WrappersMP.OUTER_SAMPLE, key, _schedule)
    out.add_wrapper_with_key(_pe.WrappersMP.DIFFUSION_MODEL, key, _wrapper)
    return out


_VFADE_SEQ = 0

# CP-10: a RefMod step curve scales its block's V rows in attention (clean release at
# w -> 0, layout untouched) instead of mixing its latent toward a blur. Off until its
# 8-step A/B (lever refmod.REFMOD_VROW_CURVE).
REFMOD_VROW_CURVE = False


def _denoise_progress(transformer_options, timestep, start, shift=1.0):
    """Denoising progress, 0 = noisy start, 1 = clean end, by STEP.

    `1 - timestep / start` is progress in raw sigma, and a flow-shifted
    schedule spends almost all of its steps at high sigma: on the 8-step PDD
    grid at shift 12 the last step is still sigma 0.63, so that progress
    stopped at 0.368 and a fade starting at 0.55 or a `late_detail` curve
    never ran. The step index in `sample_sigmas` is the truth (the same
    reading as sparse_attn's schedule); without it the sigma is un-shifted
    back onto the uniform grid first.
    """
    try:
        from . import sparse_attn as _sa  # noqa: PLC0415
        idx, n = _sa.resolve_step(transformer_options or {})
        if idx is not None and n > 1:
            return _sa.schedule_progress(idx, n)
    except Exception:  # noqa: BLE001 -- fall back to the sigma reading
        pass
    t = timestep.flatten()[0] if hasattr(timestep, "flatten") else timestep
    sigma = min(1.0, max(0.0, float(t) / max(float(start), 1e-8)))
    s = float(shift or 1.0)
    base = sigma / (s + sigma * (1.0 - s)) if s > 0 else sigma
    return 1.0 - min(1.0, max(0.0, base))


def _sampling_shift(executor):
    try:
        ms = executor.class_obj.model_patcher.get_model_object("model_sampling")
        return float(getattr(ms, "shift", 1.0) or 1.0)
    except Exception:  # noqa: BLE001
        return 1.0


def _cosine_fade_strength(prog: float, fade_start: float = 0.55, end_strength: float = 0.20) -> float:
    p = min(1.0, max(0.0, float(prog)))
    fs = min(0.99, max(0.0, float(fade_start)))
    es = min(1.0, max(0.0, float(end_strength)))
    if p <= fs:
        return 1.0
    u = (p - fs) / max(1e-6, 1.0 - fs)
    return es + (1.0 - es) * 0.5 * (1.0 + math.cos(math.pi * u))


def _ref_video_v_spans(payload, context, video_x, audio_x, scale: float, prog=None):
    """Exact `(start, stop, v_scale)` spans of `video`/`video_audio` ref blocks in `PackedLayout`.

    `prog` given (REFMOD_VROW_CURVE): every picture/clip block that carries a
    `step_curve` gets its own scale, `step_curve_value(curve, prog)`, and `scale` is unused."""
    if not payload:
        return ()
    refs = payload.get("refs") or ()
    if prog is None:
        if float(scale) >= 0.9999:
            return ()
        if not any(isinstance(r, dict) and r.get("kind") in ("video", "video_audio")
                   and not r.get("_keep_full_v") for r in refs):
            return ()
    elif not any(isinstance(r, dict) and r.get("step_curve") not in (None, "", "full")
                 for r in refs):
        return ()
    try:
        from comfy.ldm.minimax.model import PackedLayout  # noqa: PLC0415
    except Exception:
        return ()
    text_len = int(context.shape[1]) if hasattr(context, "shape") and context.ndim >= 2 else 0
    lt, lh, lw = int(video_x.shape[2]), int(video_x.shape[3]), int(video_x.shape[4])
    at = int(audio_x.shape[-1]) if audio_x is not None else 0
    layout = payload.get("layout")
    if layout is None or getattr(layout, "signature", None) != (text_len, lt, lh, lw, at):
        layout = PackedLayout(text_len, lt, lh, lw, at,
                              keyframes=payload.get("keyframes"), refs=refs)
    ref_img_segs = [(int(a), int(b)) for a, b, kind in getattr(layout, "segments", ()) if kind == "ref_img"]
    spans = []
    ri = 0
    for blk in refs:
        if not isinstance(blk, dict):
            continue
        knd = blk.get("kind")
        if knd in ("image", "video", "video_audio"):
            if ri >= len(ref_img_segs):
                break
            if prog is not None:
                sc = blk.get("step_curve")
                if sc not in (None, "", "full"):
                    w = step_curve_value(sc, prog)
                    if w < 0.9999:
                        a, b = ref_img_segs[ri]
                        spans.append((a, b, float(w)))
            elif knd in ("video", "video_audio") and not blk.get("_keep_full_v"):
                a, b = ref_img_segs[ri]
                spans.append((a, b, float(scale)))
            ri += 1
    return tuple(spans)


def attach_ref_video_fade_wrapper(model, fade_start=0.55, end_strength=0.20, mode="fade"):
    """Half-cosine fade on native `<Video N>` reference block attention `V` slices.

    Preserves `<Picture N>` stills, `<Audio N>`, keyframes, and target streams,
    and works inside both dense attention AND `HTCSparseAttention` (`ck.sol_attn_chunked`).
    """
    global _VFADE_SEQ
    if model is None or not hasattr(model, "add_wrapper_with_key"):
        return model
    from contextvars import ContextVar  # noqa: PLC0415
    import comfy.patcher_extension as _pe  # noqa: PLC0415
    from comfy.ldm.modules.attention import AttentionTensorContainer, optimized_attention  # noqa: PLC0415

    _VFADE_SEQ += 1
    key = f"htc_ref_{'vrow_curve' if mode == 'curve' else 'video_fade'}_{_VFADE_SEQ}"
    start_var = ContextVar(key, default=1000.0)
    shift_var = ContextVar(key + "_shift", default=1.0)
    fs = float(fade_start)
    es = float(end_strength)

    def _schedule(executor, noise, latent_image, sampler, sigmas, *args, **kwargs):
        try:
            sampling = executor.class_obj.model_patcher.get_model_object("model_sampling")
            start = float(sampling.timestep(sigmas[0]))
        except Exception:
            start = 1000.0
        tok = start_var.set(start)
        tok_s = shift_var.set(_sampling_shift(executor))
        try:
            return executor(noise, latent_image, sampler, sigmas, *args, **kwargs)
        finally:
            start_var.reset(tok)
            shift_var.reset(tok_s)

    def _scale_dense_v(v, spans, *, skip_reshape: bool):
        seq_len = int(v.shape[2] if skip_reshape else v.shape[1])
        out = None
        for a, b, s in spans:
            if float(s) >= 0.9999:
                continue
            lo = max(0, int(a))
            hi = min(seq_len, int(b))
            if hi <= lo:
                continue
            if out is None:
                out = v.clone()
            if skip_reshape:
                out[:, :, lo:hi, :].mul_(float(s))
            else:
                out[:, lo:hi, :].mul_(float(s))
        return out if out is not None else v

    def _wrapper(executor, x, timestep, context, transformer_options={}, **kwargs):
        payload = kwargs.get("minimax_payload")
        prog = _denoise_progress(transformer_options, timestep, start_var.get(),
                                 shift_var.get())
        v_strength = _cosine_fade_strength(prog, fade_start=fs, end_strength=es)
        vx = x[0] if isinstance(x, (list, tuple)) else x
        ax = x[1] if isinstance(x, (list, tuple)) and len(x) > 1 else None
        spans = _ref_video_v_spans(payload, context, vx, ax, v_strength,
                                   prog=prog if mode == "curve" else None)
        if not spans:
            return executor(x, timestep, context, transformer_options=transformer_options, **kwargs)
        to = dict(transformer_options or {})
        # A second V-row wrapper (SWAP fade + RefMod curves) adds its spans to the
        # first's; the innermost override applies them all once.
        to["_htc_ref_video_v_spans"] = tuple(to.get("_htc_ref_video_v_spans") or ()) + tuple(spans)
        prev_ov = to.get("optimized_attention_override")

        def _fade_override(func, q, k, v, heads, mask=None, attn_precision=None,
                           skip_reshape=False, skip_output_reshape=False, **akw):
            inner_to = dict(akw.get("transformer_options") or to)
            cur_spans = inner_to.get("_htc_ref_video_v_spans")
            if cur_spans and not inner_to.get("_htc_v_spans_applied"):
                inner_to["_htc_v_spans_applied"] = True
                if isinstance(v, AttentionTensorContainer):
                    vt = _scale_dense_v(v.take(), cur_spans, skip_reshape=skip_reshape)
                    v = AttentionTensorContainer(vt)
                else:
                    v = _scale_dense_v(v, cur_spans, skip_reshape=skip_reshape)
                akw["transformer_options"] = inner_to
            call_fn = prev_ov if prev_ov is not None else (func or optimized_attention)
            if prev_ov is not None:
                if isinstance(q, AttentionTensorContainer) and hasattr(prev_ov, "container_function"):
                    return prev_ov.container_function(
                        q, k, v, heads, mask=mask, attn_precision=attn_precision,
                        skip_reshape=skip_reshape, skip_output_reshape=skip_output_reshape, **akw)
                if isinstance(q, AttentionTensorContainer):
                    return prev_ov(
                        func or optimized_attention, q.take(), k.take(),
                        (v.take() if isinstance(v, AttentionTensorContainer) else v),
                        heads, mask=mask, attn_precision=attn_precision,
                        skip_reshape=skip_reshape, skip_output_reshape=skip_output_reshape, **akw)
                return prev_ov(
                    func or optimized_attention, q, k, v, heads, mask=mask,
                    attn_precision=attn_precision, skip_reshape=skip_reshape,
                    skip_output_reshape=skip_output_reshape, **akw)
            return call_fn(
                q, k, v, heads, mask=mask, attn_precision=attn_precision,
                skip_reshape=skip_reshape, skip_output_reshape=skip_output_reshape, **akw)

        _fade_override.container_function = lambda q, k, v, heads, **akw: _fade_override(None, q, k, v, heads, **akw)
        to["optimized_attention_override"] = _fade_override
        return executor(x, timestep, context, transformer_options=to, **kwargs)

    out = model.clone()
    out.add_wrapper_with_key(_pe.WrappersMP.OUTER_SAMPLE, key, _schedule)
    out.add_wrapper_with_key(_pe.WrappersMP.DIFFUSION_MODEL, key, _wrapper)
    return out


def qwen_stride():
    """Frames between the samples core shows Qwen of a reference video.

    Core's `FPS // 2`: a reference clip reaches the text encoder at 2 fps.
    Read from core so a change there moves this with it.
    """
    try:
        from comfy_extras.nodes_minimax_h3 import FPS
    except Exception:
        FPS = 24
    return max(1, int(FPS) // 2)


def looks_from(canvas, clip=False):
    """The pictures to keep for Qwen from one encoded source, as uint8.

    A still keeps itself. A clip keeps the frames core would sample for Qwen
    anyway, so a mod does not carry a whole clip's pixels to show two a
    second of it.
    """
    frames = canvas[::qwen_stride()] if clip else canvas
    return (frames[..., :3].float().clamp(0, 1) * 255.0).round().to(torch.uint8).cpu()


def hold_looks(looks):
    """Lay the kept pictures out as a reference clip Qwen sees one each of.

    Core snaps a reference video down to 17k+5 frames and samples it every
    `qwen_stride()` frames, so each picture is held for one stride and the
    last is held until the snapped length still reaches it. The last picture
    may be sampled twice; no earlier one is skipped.
    """
    stride = qwen_stride()
    n = int(looks.shape[0])
    need = max(5, stride * (n - 1) + 1)
    length = 5
    while length < need:
        length += 17
    idx = torch.clamp(torch.arange(length) // stride, max=n - 1)
    return looks[idx]


def presentation_pixels(vae, latent, cap_mp=0.0, looks=None, kind="image"):
    """IMAGE pixels for the text encoder.

    With `looks`, the pictures the builder encoded: sharp, one Qwen frame per
    picture. Without them, a decode of the stored latent -- the only view a
    mod written elsewhere, or before looks existed, can give. A decode of a
    stacked multi-still latent is also a clip the VAE invents between the
    stills, which is not what anybody photographed.

    Image mods come back `[1,H,W,C]`. Video mods come back `[T,H,W,C]`.
    `cap_mp` shrinks this view only. The latent handed to the DiT is the
    stored one.
    """
    try:
        from . import media as _media
    except ImportError:
        import media as _media

    if looks is not None:
        if kind == "image":
            return _cap(looks[:1].float() / 255.0, cap_mp, _media)
        # Cap the unique pictures, THEN hold them: holding first resized up
        # to 39 copies of the same few frames in float32 (same pixels out).
        return hold_looks(_cap(looks.float() / 255.0, cap_mp, _media))
    pixels = vae.decode(latent)
    if pixels.ndim == 5 and pixels.shape[0] == 1 and pixels.shape[1] == 1:
        pixels = pixels[:, 0]
    elif pixels.ndim == 5 and pixels.shape[0] == 1:
        pixels = pixels[0]
    elif pixels.ndim == 5:
        pixels = pixels.reshape(-1, pixels.shape[-3], pixels.shape[-2], pixels.shape[-1])
    if pixels.ndim != 4 or pixels.shape[-1] != 3:
        _fail(f"VAE decode of a RefMod came back as {tuple(pixels.shape)}, "
              f"which is not an image batch")
    return _cap(pixels, cap_mp, _media)


def _cap(pixels, cap_mp, _media):
    h, w = int(pixels.shape[1]), int(pixels.shape[2])
    tw, th = _media._mp_cap_size(w, h, cap_mp)
    if (tw, th) != (w, h):
        samples = pixels.movedim(-1, 1).float()
        samples = F.interpolate(samples, size=(th, tw), mode="bicubic", align_corners=False)
        pixels = samples.movedim(1, -1).clamp_(0, 1)
    return pixels


def splice_blocks(cond, hop_images, hop_videos, image_ids, video_ids,
                  hop_audios=None, audio_ids=None):
    """Replace the blocks `ReferenceToVideo` just encoded for RefMod slots.

    `image_ids` / `video_ids` / `audio_ids` map `id(tensor)` to a RefMod block.
    Picture order is `hop_images`, then `hop_videos`, then `hop_audios`, which is
    the exact order Core builds `minimax_refs`.
    """
    import node_helpers

    images = list((hop_images or {}).values())
    videos = list((hop_videos or {}).values())
    audios = list((hop_audios or {}).values())
    # The maps are built once per run. A later hop that does not cite the
    # mod has no pixel of ours, and ReferenceToVideo then emits no
    # minimax_refs. That hop is finished. Splicing it was the hop-2 crash.
    has_img = any(id(t) in (image_ids or {}) for t in images)
    has_vid = any(id(t) in (video_ids or {}) for t in videos)
    has_aud = any(id(a) in (audio_ids or {}) for a in audios)
    if not (has_img or has_vid or has_aud):
        return cond
    blocks = None
    for entry in cond or []:
        meta = entry[1] if len(entry) > 1 else None
        if isinstance(meta, dict) and meta.get("minimax_refs"):
            blocks = meta["minimax_refs"]
            break
    if not blocks:
        _fail("This hop cites a RefMod but the conditioning has no "
              "minimax_refs. The video VAE has to be connected.")
    blocks = [dict(b) for b in blocks]

    def _prepare(index, block_or_list, expected):
        if index >= len(blocks):
            _fail(f"RefMod block {index} is past the {len(blocks)} reference "
                  f"blocks the encoder built")
        got = blocks[index].get("kind")
        if expected == "image" and got != "image":
            _fail(f"reference block {index} is '{got}', not the image a RefMod "
                  f"still has to replace")
        if expected == "video" and got not in ("video", "video_audio"):
            _fail(f"reference block {index} is '{got}', not the video a RefMod "
                  f"clip has to replace")
        if expected == "audio" and got != "audio":
            _fail(f"reference block {index} is '{got}', not the audio slot a "
                  f"RefMod audio latent has to replace")
        items = list(block_or_list) if isinstance(block_or_list, (list, tuple)) else [block_or_list]
        out_list = []
        for copy_i, blk in enumerate(items):
            placed = dict(blk)
            if expected == "audio":
                old_a = blocks[index].get("audio_latent")
                if torch.is_tensor(old_a) and torch.is_tensor(placed.get("audio_latent")):
                    placed["audio_latent"] = placed["audio_latent"].to(
                        device=old_a.device, dtype=old_a.dtype)
            else:
                old = blocks[index].get("latent")
                if torch.is_tensor(old) and torch.is_tensor(placed.get("latent")):
                    placed["latent"] = placed["latent"].to(device=old.device, dtype=old.dtype)
                # Carry encoded audio across on the first copy when a video clip carries sound.
                if got == "video_audio" and copy_i == 0:
                    placed["kind"] = "video_audio"
                    placed["ref_audio_t"] = blocks[index].get("ref_audio_t", 0)
                    placed["audio_latent"] = blocks[index].get("audio_latent")
            out_list.append(placed)
        return out_list

    # The k-th picture / clip / sound is found by KIND, not by position: Core packs
    # pictures, then clips, then sounds (same indices as before), but a Viggle Remake
    # conditioning puts its source <Video 1> first, and positions were then off by one.
    by_kind = {"image": [], "video": [], "audio": []}
    for idx, b in enumerate(blocks):
        knd = b.get("kind")
        knd = "video" if knd in ("video", "video_audio") else knd
        if knd in by_kind:
            by_kind[knd].append(idx)
    n_img = len(images)
    n_vis = n_img + len(videos)

    def _at(kind, n, fallback):
        lst = by_kind.get(kind) or []
        return lst[n] if n < len(lst) else fallback

    replacements = {}
    for i, tensor in enumerate(images):
        blk = (image_ids or {}).get(id(tensor))
        if blk is not None:
            k_i = _at("image", i, i)
            replacements[k_i] = _prepare(k_i, blk, "image")
    for j, tensor in enumerate(videos):
        blk = (video_ids or {}).get(id(tensor))
        if blk is not None:
            k_v = _at("video", j, n_img + j)
            replacements[k_v] = _prepare(k_v, blk, "video")
    for k, aud in enumerate(audios):
        blk = (audio_ids or {}).get(id(aud))
        if blk is not None:
            k_a = _at("audio", k, n_vis + k)
            replacements[k_a] = _prepare(k_a, blk, "audio")

    new_blocks = []
    for idx, orig in enumerate(blocks):
        if idx in replacements:
            new_blocks.extend(replacements[idx])
        else:
            new_blocks.append(orig)
    return node_helpers.conditioning_set_values(cond, {"minimax_refs": new_blocks})


# -- builder math ----------------------------------------------------------

def fit_token_budget(latent, budget, label):
    """Drop near-duplicate frames, then resample time, until tokens fit."""
    budget = int(budget or 0)
    if budget <= 0:
        return latent
    h, w = int(latent.shape[3]), int(latent.shape[4])
    per = (h // 2) * (w // 2)
    t = int(latent.shape[2])
    if per > budget:
        _fail(f"{label}: one frame costs {per} tokens, above the {budget} budget. "
              f"Raise max_tokens, or build from a smaller picture.")
    if per * t <= budget:
        return latent
    flat = latent[0].float()
    kept = [0]
    prev = flat[:, 0]
    for i in range(1, t):
        cur = flat[:, i]
        denom = (cur.abs().mean() + prev.abs().mean()) / 2 + 1e-6
        if (cur - prev).abs().mean() / denom >= 0.02:
            kept.append(i)
            prev = cur
    if len(kept) < t:
        latent = latent[:, :, kept]
        t = int(latent.shape[2])
    if per * t > budget:
        fit_t = max(1, budget // per)
        idx = torch.linspace(0, t - 1, fit_t).round().long()
        latent = latent[:, :, idx]
    return latent


def metadata_for(name, kind, latent, mode, source, source_shape, pool, steps,
                 plan=None, description="", mask=None, looks=0,
                 concept_type="generic", default_step_curve="full"):
    t, h, w = (int(latent.shape[2]), int(latent.shape[3]), int(latent.shape[4]))
    role = str(concept_type or "generic").strip()
    if role not in CONCEPT_ROLES:
        role = "generic"
    sc = str(default_step_curve or "full").strip()
    if sc not in STEP_CURVES:
        sc = "full"
    return {
        "name": name,
        "kind": kind,
        "latent_h": h,
        "latent_w": w,
        "latent_t": t,
        "mode": mode,
        "source": source,
        "source_shape": source_shape,
        "pool": pool,
        "optimize_steps": int(steps or 0),
        "tags": [],
        "description": _clean_desc_text(description),
        "concept_type": role,
        "default_step_curve": sc,
        "mod_plan": plan if isinstance(plan, dict) else {},
        "subject_mask": mask if isinstance(mask, dict) else {},
        "looks": int(looks or 0),
        "_format_version": FORMAT_VERSION,
        "sample_rate": 32000,
    }


def save_latent(directory, name, latent, meta, looks=None):
    """Atomic write (v4 standalone). Returns the stem the rail stores in `file`."""
    from safetensors.torch import save_file

    stem = _clean_name(name)
    if not stem:
        _fail("refmod name is empty")
    directory = os.path.normpath(directory)
    dest = os.path.join(directory, stem.replace("/", os.sep) + ".safetensors")
    os.makedirs(os.path.dirname(dest), exist_ok=True)
    fd, temporary = tempfile.mkstemp(prefix=".refmod-", suffix=".tmp",
                                     dir=os.path.dirname(dest) or ".")
    os.close(fd)
    try:
        tensors = {"latent": latent.detach().cpu().contiguous()}
        if looks is not None:
            tensors[LOOKS_KEY] = looks.to(torch.uint8).cpu().contiguous()
        save_file(tensors, temporary, metadata={META_KEY: json.dumps(meta)})
        os.replace(temporary, dest)
        _invalidate_header_cache(dest)
    finally:
        if os.path.exists(temporary):
            os.unlink(temporary)
    return stem


def encode_png_bytes_tensor(pixels_hwc) -> torch.Tensor:
    """Lossless 8-bit PNG byte tensor `[N]` (`uint8`) for compact v5 RefMod storage."""
    import io  # noqa: PLC0415
    import numpy as np  # noqa: PLC0415
    from PIL import Image  # noqa: PLC0415

    arr = pixels_hwc
    if torch.is_tensor(arr):
        if arr.ndim == 4:
            arr = arr[0]
        arr = arr[..., :3].detach().cpu()
        if arr.dtype != torch.uint8:
            arr = (arr.float().clamp(0.0, 1.0) * 255.0).round().to(torch.uint8)
        arr = arr.numpy()
    buf = io.BytesIO()
    Image.fromarray(arr).save(buf, format="PNG", optimize=True)
    return torch.frombuffer(bytearray(buf.getvalue()), dtype=torch.uint8).clone()


def infer_yaw_from_label(text: str) -> float | None:
    """Best-effort camera azimuth `yaw` in `(-180, 180]` from angle/pose words."""
    s = str(text or "").lower()
    if not s:
        return None
    if re.search(r"(?:^|[\W_])(?:back|rear|behind)(?:$|[\W_])", s):
        return 180.0
    if re.search(r"(?:^|[\W_])(?:left[_\s-]*profile|left[_\s-]*side)(?:$|[\W_])", s):
        return -90.0
    if re.search(r"(?:^|[\W_])(?:right[_\s-]*profile|right[_\s-]*side)(?:$|[\W_])", s):
        return 90.0
    if re.search(r"(?:^|[\W_])(?:three[_\s-]*quarter[_\s-]*left|3/4[_\s-]*left)(?:$|[\W_])", s):
        return -45.0
    if re.search(r"(?:^|[\W_])(?:three[_\s-]*quarter[_\s-]*right|3/4[_\s-]*right|three[_\s-]*quarter|3/4)(?:$|[\W_])", s):
        return 45.0
    if re.search(r"(?:^|[\W_])(?:front|frontal|headshot|portrait|closeup|straight)(?:$|[\W_])", s):
        return 0.0
    return None


def infer_shot_yaw(text: str) -> float | None:
    """Explicit camera-azimuth angle from shot beat/prose (avoids narrative false positives)."""
    s = str(text or "").lower()
    if not s:
        return None
    if re.search(r"\b(?:back view|rear view|from behind|over[- ]the[- ]shoulder)\b", s):
        return 180.0
    if re.search(r"\b(?:left[- ]profile|left side view|side profile left)\b", s):
        return -90.0
    if re.search(r"\b(?:right[- ]profile|right side view|side profile right)\b", s):
        return 90.0
    if re.search(r"\b(?:three[- ]quarter left|3/4 left)\b", s):
        return -45.0
    if re.search(r"\b(?:three[- ]quarter right|3/4 right)\b", s):
        return 45.0
    if re.search(r"\b(?:front view|frontal view|facing camera|straight[- ]on view)\b", s):
        return 0.0
    return None


def select_members_by_yaw(members, target_yaw: float = 0.0, max_views: int = 2):
    """Select the `max_views` nearest visual member indices by circular yaw distance."""
    vis = [(i, m) for i, m in enumerate(members or []) if isinstance(m, dict) and m.get("kind") != "audio"]
    if len(vis) <= max(1, int(max_views)):
        return [i for i, _m in vis]
    yaws, unknown = [], []
    for i, m in vis:
        y = m.get("yaw")
        if y is None:
            y = infer_yaw_from_label(m.get("name") or "")
        if y is None:
            unknown.append(i)
        else:
            yaws.append((i, float(y)))
    if not yaws:
        return [i for i, _m in vis]
    # Partial match: the views with a known yaw are ranked; one unlabeled view no
    # longer sends every view to the encoder. Unlabeled ones only fill free places.
    k = max(1, int(max_views))
    tgt = float(target_yaw)
    ranked = sorted(yaws, key=lambda item: abs(((item[1] - tgt + 180.0) % 360.0) - 180.0))
    pick = [i for i, _y in ranked[:k]]
    pick += unknown[:max(0, k - len(pick))]
    return sorted(pick)


def save_v5_bundle(directory, name, members_specs, *, description="",
                   concept_type="identity", default_step_curve="full",
                   plan=None, mask=None, compact_png=False):
    """Atomic v5 bundle write (`ref_0..ref_N` + `looks_0..looks_N` + optional audio).

    Compatible with ComfyUI-MiniMaxH3Mod v0.2.6 `load_bundle` AND HTC multi-member
    `load_bundle_visuals` / `load_audio_latent`.
    """
    from safetensors.torch import save_file

    stem = _clean_name(name)
    if not stem:
        _fail("refmod name is empty")
    if not members_specs:
        _fail("v5 bundle needs at least one member")
    role = str(concept_type or "identity").strip()
    if role not in CONCEPT_ROLES:
        role = "generic"
    sc = str(default_step_curve or "full").strip()
    if sc not in STEP_CURVES:
        sc = "full"

    tensors = {}
    members_meta = []
    for i, spec in enumerate(members_specs):
        lat = spec["latent"].detach().cpu().clone().contiguous()
        tensors[f"ref_{i}"] = lat
        kind = str(spec["kind"])
        if kind == "audio":
            members_meta.append({
                "name": str(spec.get("name") or f"{stem}_voice"),
                "kind": "audio",
                "ref_audio_t": int(lat.shape[-1]),
                "sample_rate": int(spec.get("sample_rate") or 32000),
                "description": _clean_desc_text(spec.get("description") or description),
                "concept_type": role,
                "_format_version": FORMAT_VERSION,
            })
        else:
            looks = spec.get("looks")
            n_looks = 0
            if looks is not None:
                lk = looks.to(torch.uint8).cpu().contiguous()
                if compact_png and int(lk.shape[0]) == 1 and kind == "image":
                    tensors[f"enc_{i}"] = encode_png_bytes_tensor(lk[0])
                else:
                    tensors[f"looks_{i}"] = lk
                    if len(members_specs) == 1:
                        tensors[LOOKS_KEY] = lk
                n_looks = int(lk.shape[0])
            t, h, w = int(lat.shape[2]), int(lat.shape[3]), int(lat.shape[4])
            yaw_val = spec.get("yaw")
            if yaw_val is None:
                yaw_val = infer_yaw_from_label(spec.get("name") or "")
            m_entry = {
                "name": str(spec.get("name") or f"{stem}_{i}"),
                "kind": kind,
                "latent_h": h,
                "latent_w": w,
                "latent_t": t,
                "mode": str(spec.get("mode") or "encode"),
                "source": str(spec.get("source") or kind),
                "source_shape": str(spec.get("source_shape") or f"{t}x{h}x{w}"),
                "pool": str(spec.get("pool") or "encode"),
                "optimize_steps": 0,
                "tags": [],
                "description": _clean_desc_text(spec.get("description") or description),
                "concept_type": role,
                "default_step_curve": sc,
                "looks": n_looks,
                "_format_version": FORMAT_VERSION,
                "sample_rate": 32000,
            }
            if yaw_val is not None:
                m_entry["yaw"] = float(yaw_val)
            members_meta.append(m_entry)

    bundle_meta = {
        "_format_version": BUNDLE_VERSION,
        "kind": "bundle",
        "name": stem,
        "description": _clean_desc_text(description),
        "concept_type": role,
        "default_step_curve": sc,
        "mod_plan": plan if isinstance(plan, dict) else {},
        "subject_mask": mask if isinstance(mask, dict) else {},
        "members": members_meta,
    }
    directory = os.path.normpath(directory)
    dest = os.path.join(directory, stem.replace("/", os.sep) + ".safetensors")
    os.makedirs(os.path.dirname(dest), exist_ok=True)
    fd, temporary = tempfile.mkstemp(prefix=".refmod-", suffix=".tmp",
                                     dir=os.path.dirname(dest) or ".")
    os.close(fd)
    try:
        save_file(tensors, temporary, metadata={META_KEY: json.dumps(bundle_meta)})
        os.replace(temporary, dest)
        _invalidate_header_cache(dest)
    finally:
        if os.path.exists(temporary):
            os.unlink(temporary)
    return stem


def update_meta(name, patch):
    """Header-only RefMod metadata edit (no VAE re-encode). Returns updated meta."""
    from safetensors import safe_open
    from safetensors.torch import save_file

    path = resolve(name)
    if path is None:
        _fail(f"refmod {name!r} not found")
    with safe_open(path, framework="pt", device="cpu") as fh:
        raw_hdr = dict(fh.metadata() or {})
        tensors = {k: fh.get_tensor(k).clone() for k in fh.keys()}
    # Write back under the key the header was read from. A RefLoRA keeps its
    # meta under h3_refmod_meta / minimax_h3_refmod_meta (or none at all); adding a
    # bare `refmod_meta` would win the next read and drop its kind and shape.
    meta_key = next((k for k in (META_KEY, AUDIO_META_KEY) + REFLORA_META_KEYS
                     if raw_hdr.get(k)), None)
    if meta_key is None:
        if not _is_reflora_file(path):
            _fail(f"{name} has no {META_KEY} header. It is not a RefMod.")
        meta_key = REFLORA_META_KEYS[0]
        meta = {k: v for k, v in (read_meta(path) or {}).items() if k != "_reflora"}
    else:
        meta = json.loads(raw_hdr[meta_key])
    if not isinstance(meta, dict):
        meta = {}
    if "description" in patch:
        meta["description"] = _clean_desc_text(patch["description"])
    if "concept_type" in patch or "role" in patch:
        r = str(patch.get("concept_type") or patch.get("role") or "generic").strip()
        if r in CONCEPT_ROLES:
            meta["concept_type"] = r
    if "default_step_curve" in patch:
        sc = str(patch["default_step_curve"] or "full").strip()
        if sc in STEP_CURVES:
            meta["default_step_curve"] = sc
    if "voice" in patch:
        mp = dict(meta.get("mod_plan") or {})
        v = str(patch.get("voice") or "").strip()
        vs = float(patch.get("voice_start") or 0.0)
        ve = float(patch.get("voice_end") or 0.0)
        if v:
            mp["voice"] = {"file": v, "start": vs, "end": ve} if (vs or ve) else v
        else:
            mp.pop("voice", None)
        meta["mod_plan"] = mp
    if meta.get("kind") == "bundle":
        for m in meta.get("members") or []:
            if isinstance(m, dict):
                if "description" in patch:
                    m["description"] = meta["description"]
                if "concept_type" in meta:
                    m["concept_type"] = meta["concept_type"]
                if "default_step_curve" in meta:
                    m["default_step_curve"] = meta["default_step_curve"]

    raw_hdr[meta_key] = json.dumps(meta)
    fd, temporary = tempfile.mkstemp(prefix=".refmod-", suffix=".tmp",
                                     dir=os.path.dirname(path) or ".")
    os.close(fd)
    try:
        save_file(tensors, temporary, metadata=raw_hdr)
        os.replace(temporary, path)
        _invalidate_header_cache(path)
    finally:
        if os.path.exists(temporary):
            os.unlink(temporary)
    return meta


def remove_member(name, member_idx):
    """Remove member `member_idx` from a v5 bundle without re-encoding."""
    from safetensors import safe_open
    from safetensors.torch import save_file

    path = resolve(name)
    if path is None:
        _fail(f"refmod {name!r} not found")
    with safe_open(path, framework="pt", device="cpu") as fh:
        raw_hdr = dict(fh.metadata() or {})
        meta = json.loads(raw_hdr.get(META_KEY) or "{}")
        if meta.get("kind") != "bundle":
            _fail(f"{name} is not a multi-member bundle")
        members = list(meta.get("members") or [])
        idx = int(member_idx)
        if idx < 0 or idx >= len(members):
            _fail(f"member index {idx} out of range (0..{len(members) - 1})")
        if len(members) <= 1:
            _fail("cannot remove the last remaining member from a bundle")
        old_tensors = {k: fh.get_tensor(k).clone() for k in fh.keys()}

    # Per-member tensors are renumbered with their member: latent, kept photos
    # (`looks_N`) and compact PNG photos (`enc_N`). Anything else rides through.
    per_member = re.compile(r"^(ref|looks|enc)_(\d+)$")
    new_members = []
    new_tensors = {k: v for k, v in old_tensors.items() if not per_member.match(k)}
    ni = 0
    for oi, m in enumerate(members):
        if oi == idx:
            continue
        new_members.append(m)
        for pre in ("ref", "looks", "enc"):
            if f"{pre}_{oi}" in old_tensors:
                new_tensors[f"{pre}_{ni}"] = old_tensors[f"{pre}_{oi}"]
        ni += 1
    meta["members"] = new_members
    raw_hdr[META_KEY] = json.dumps(meta)
    fd, temporary = tempfile.mkstemp(prefix=".refmod-", suffix=".tmp",
                                     dir=os.path.dirname(path) or ".")
    os.close(fd)
    try:
        save_file(new_tensors, temporary, metadata=raw_hdr)
        os.replace(temporary, path)
        _invalidate_header_cache(path)
    finally:
        if os.path.exists(temporary):
            os.unlink(temporary)
    return meta


def inspect_refmod(name):
    """Rich header+member inspection for UI without heavy decoding."""
    path = resolve(name)
    if path is None:
        return None
    meta = read_meta(path)
    if not isinstance(meta, dict):
        return None
    summary = _meta_summary(meta, full=path)
    voice = _listed_voice(meta)
    members_out = []
    if meta.get("kind") == "bundle":
        for i, m in enumerate(meta.get("members") or []):
            if not isinstance(m, dict):
                continue
            members_out.append({
                "index": i,
                "name": str(m.get("name") or f"member_{i}"),
                "kind": str(m.get("kind") or "image"),
                "tokens": _member_tokens(m),
                "shape": [int(m.get("latent_t") or 1), int(m.get("latent_h") or 0), int(m.get("latent_w") or 0)],
                "has_looks": bool(int(m.get("looks") or 0)) or summary["has_looks"],
            })
    else:
        members_out.append({
            "index": 0,
            "name": str(meta.get("name") or name),
            "kind": summary["kind"],
            "tokens": summary["tokens"],
            "shape": [int(meta.get("latent_t") or 1), int(meta.get("latent_h") or 0), int(meta.get("latent_w") or 0)],
            "has_looks": summary["has_looks"],
        })
    return {
        "name": _clean_name(name),
        "description": description_of(meta),
        "voice": voice,
        "mod_plan": stored_plan(name),
        "subject_mask": meta.get("subject_mask") or {},
        "members": members_out,
        **summary,
    }


def thumb_bytes(name, member_idx=0, width=192):
    """Return JPEG bytes for a RefMod thumbnail from `looks`/`enc_` (no VAE decode)."""
    import io  # noqa: PLC0415
    import numpy as np  # noqa: PLC0415
    from PIL import Image  # noqa: PLC0415

    path = resolve(name)
    if path is None:
        return None
    meta = read_meta(path) or {}
    idx = max(0, int(member_idx or 0))
    w = max(48, min(384, int(width or 192)))
    try:
        st = os.stat(path)
        sig = f"{path}|{int(st.st_mtime)}|{st.st_size}|{idx}|{w}"
    except OSError:
        sig = f"{path}|{idx}|{w}"
    hkey = hashlib.sha1(sig.encode("utf-8", "ignore")).hexdigest()[:24]
    try:
        import folder_paths
        cdir = os.path.join(folder_paths.get_temp_directory(), "h3_refmod_thumbs")
    except Exception:
        cdir = os.path.join(tempfile.gettempdir(), "h3_refmod_thumbs")
    os.makedirs(cdir, exist_ok=True)
    cached = os.path.join(cdir, f"{hkey}.jpg")
    if os.path.isfile(cached) and os.path.getsize(cached) > 0:
        with open(cached, "rb") as fh:
            return fh.read()

    looks = None
    if meta.get("_reflora"):
        looks = _first_tensor(
            _load_looks(path, f"refmod.looks_{idx}"),
            _load_looks(path, "refmod.looks"),
            _load_looks(path, LOOKS_KEY),
        )
    elif meta.get("kind") == "bundle":
        looks = _first_tensor(
            _load_looks(path, f"looks_{idx}"),
            _load_looks(path, LOOKS_KEY) if idx == 0 else None,
            _load_enc_jpegs(path, [idx]),
        )
    else:
        looks = _first_tensor(_load_looks(path, LOOKS_KEY), _load_enc_jpegs(path))

    if looks is None:
        return None
    frame_idx = min(idx, int(looks.shape[0]) - 1) if meta.get("kind") != "bundle" else 0
    arr = looks[frame_idx].detach().cpu().numpy()
    im = Image.fromarray(arr.astype(np.uint8))
    im.thumbnail((w, w), Image.Resampling.LANCZOS)
    buf = io.BytesIO()
    im.save(buf, format="JPEG", quality=84, optimize=True)
    data = buf.getvalue()
    try:
        tmp = cached + ".tmp"
        with open(tmp, "wb") as fh:
            fh.write(data)
        os.replace(tmp, cached)
    except OSError:
        pass
    return data


def vision_data_urls(name, member_indices=None, max_images=4, max_side=1024):
    """JPEG data-URLs (`data:image/jpeg;base64,...`) from a RefMod's stored `looks`."""
    import base64  # noqa: PLC0415
    import io  # noqa: PLC0415
    import numpy as np  # noqa: PLC0415
    from PIL import Image  # noqa: PLC0415

    path = resolve(name)
    if path is None:
        return []
    meta = read_meta(path) or {}
    cap_n = max(1, int(max_images or 4))
    side = max(256, min(2048, int(max_side or 1024)))
    urls = []

    def _arr_to_url(arr):
        im = Image.fromarray(arr.astype(np.uint8))
        if im.mode != "RGB":
            im = im.convert("RGB")
        if max(im.size) > side:
            im.thumbnail((side, side), Image.Resampling.LANCZOS)
        buf = io.BytesIO()
        im.save(buf, format="JPEG", quality=88)
        return "data:image/jpeg;base64," + base64.b64encode(buf.getvalue()).decode("ascii")

    if meta.get("kind") == "bundle":
        members = [m for m in (meta.get("members") or []) if isinstance(m, dict)]
        vis_indices = [i for i, m in enumerate(members) if m.get("kind") in ("image", "video")]
        if member_indices:
            wanted = [i for i in member_indices if i in vis_indices]
            if wanted:
                vis_indices = wanted
        for idx in vis_indices[:cap_n]:
            lk = _first_tensor(
                _load_looks(path, f"looks_{idx}"),
                _load_looks(path, LOOKS_KEY) if idx == 0 else None,
                _load_enc_jpegs(path, [idx]),
            )
            if lk is not None and int(lk.shape[0]) > 0:
                urls.append(_arr_to_url(lk[0].detach().cpu().numpy()))
    else:
        if meta.get("_reflora"):
            lk = _first_tensor(
                _load_looks(path, "refmod.looks_0"),
                _load_looks(path, "refmod.looks"),
                _load_looks(path, LOOKS_KEY),
            )
        else:
            lk = _first_tensor(_load_looks(path, LOOKS_KEY), _load_enc_jpegs(path))
        if lk is not None and int(lk.shape[0]) > 0:
            n_f = int(lk.shape[0])
            picks = [0] if (cap_n == 1 or n_f == 1) else sorted({round(j * (n_f - 1) / max(1, min(cap_n, n_f) - 1)) for j in range(min(cap_n, n_f))})
            for fi in picks:
                urls.append(_arr_to_url(lk[fi].detach().cpu().numpy()))

    if not urls:
        plan = stored_plan(name) or {}
        stills = [s for s in (plan.get("stills") or []) if isinstance(s, str) and s.strip()]
        if stills:
            try:
                from . import media as _media
            except ImportError:
                import media as _media
            for s in stills[:cap_n]:
                u = _media.vision_data_url(s, max_side=side)
                if u:
                    urls.append(u)
    return urls


def vision_data_url(name, member_idx=0, max_side=1024):
    """Single JPEG data-URL from a RefMod's stored `looks` (or None)."""
    idx = None if member_idx is None else [max(0, int(member_idx or 0))]
    urls = vision_data_urls(name, member_indices=idx, max_images=1, max_side=max_side)
    return urls[0] if urls else None


def preview_mask_png(image_name, subject_mask="auto_rembg", background_retention=0.0,
                     background_mode="blur_bg", crop_margin=0.0, max_side=512):
    """Render side-by-side / isolated PNG preview bytes for `image_name` in `h3_refs/`."""
    import io  # noqa: PLC0415
    import numpy as np  # noqa: PLC0415
    from PIL import Image  # noqa: PLC0415

    try:
        from . import media as _media
    except ImportError:
        import media as _media

    px = _media.load_image(image_name)
    if px is None:
        _fail(f"{image_name} could not be loaded from h3_refs")
    tw, th = canvas_size(px, short=min(512, int(max_side or 512)))
    canvas = to_canvas(px, tw, th, fit_mode="crop")
    mode = str(subject_mask or "off").strip().lower()
    if mode in ("auto", "auto_rembg", "auto_sam"):
        backend = "auto_sam" if mode == "auto_sam" else "auto_rembg"
        m = refine_cutout(cutout_masks(canvas, backend=backend), canvas)
        if float(crop_margin or 0.0) > 0.0:
            canvas, m = crop_to_subject(canvas, m, margin=float(crop_margin))
        out = isolate_subject(canvas, m, retention=float(background_retention or 0.0),
                              mode=background_mode)
    else:
        out = canvas
    arr = (out[0, ..., :3].float().clamp(0, 1) * 255.0).round().byte().cpu().numpy()
    buf = io.BytesIO()
    Image.fromarray(arr).save(buf, format="PNG")
    return buf.getvalue()


def canvas_for_hw(h, w, short=1024):
    """The `(tw, th)` a source of this size lands on. Never upscales.

    Split out from `canvas_size` so the panel's estimate can ask about a
    file's dimensions without decoding it, and get the same answer the
    build will.
    """
    h, w = int(h), int(w)
    if h < 1 or w < 1:
        _fail(f"a source cannot be {w}x{h}")
    scale = 1.0 if min(h, w) <= short else short / float(min(h, w))
    tw = max(32, int(round(w * scale) / 32) * 32)
    th = max(32, int(round(h * scale) / 32) * 32)
    tw = min(tw, max(32, w // 32 * 32))
    th = min(th, max(32, h // 32 * 32))
    return tw, th


def canvas_size(frames, short=1024):
    """The `(tw, th)` `fit_canvas` would pick for `[T,H,W,C]`."""
    if frames.ndim != 4 or frames.shape[-1] < 3:
        _fail(f"builder frames must be [T,H,W,C], got {tuple(frames.shape)}")
    return canvas_for_hw(int(frames.shape[1]), int(frames.shape[2]), short)


def frame_tokens(tw, th, factor=16):
    """DiT tokens one latent frame of this canvas costs.

    The H3 VAE is 16x spatial and the DiT patchifies 2x2, so a 1024x1280
    canvas is an 80x64 latent and 1280 tokens -- which is what a real
    built mod measures.
    """
    return max(1, (int(th) // factor // 2) * (int(tw) // factor // 2))


def to_canvas(frames, tw, th, fit_mode="crop"):
    """Resize `[T,H,W,C]` onto an exact canvas.

    `fit_mode`:
      * `crop` (default): aspect-preserving center cover-crop (never stretches!).
      * `pad`: aspect-preserving fit inside `(tw, th)` padded with edge neutral.
      * `stretch`: legacy resize (`disabled` crop).
    """
    import comfy.utils

    if frames.ndim != 4 or frames.shape[-1] < 3:
        _fail(f"builder frames must be [T,H,W,C], got {tuple(frames.shape)}")
    tw, th = int(tw), int(th)
    fh, fw = int(frames.shape[1]), int(frames.shape[2])
    mode = str(fit_mode or "crop").strip().lower()
    samples = frames[..., :3].movedim(-1, 1)
    if mode == "pad" and (fw * th != fh * tw):
        scale = min(tw / float(fw), th / float(fh))
        rw = max(1, min(tw, int(round(fw * scale))))
        rh = max(1, min(th, int(round(fh * scale))))
        scaled = comfy.utils.common_upscale(samples, rw, rh, "lanczos", "disabled")
        pad_l = (tw - rw) // 2
        pad_r = tw - rw - pad_l
        pad_t = (th - rh) // 2
        pad_b = th - rh - pad_t
        padded = F.pad(scaled, (pad_l, pad_r, pad_t, pad_b), mode="constant", value=0.5)
        return padded.movedim(1, -1)
    crop_arg = "disabled" if mode == "stretch" else "center"
    samples = comfy.utils.common_upscale(samples, tw, th, "lanczos", crop_arg)
    return samples.movedim(1, -1)


def fit_canvas(frames, short=1024, fit_mode="crop"):
    """Resize `[T,H,W,C]` onto the 32px grid, short edge at most `short`.

    Never upscales. A still and a clip share this so the VAE sees the same
    class of canvas the reference pipeline does.
    """
    tw, th = canvas_size(frames, short)
    return to_canvas(frames, tw, th, fit_mode=fit_mode)


def stack_latents(parts, label):
    """Concatenate `[1,24,T,H,W]` encodes along time into one visual mod.

    This is how several stills and a clip become a single RefMod rather
    than a version-5 bundle: `load_visual` reads one latent, and `_as_visual`
    already calls anything with `T > 1` a video mod. A bundle would hand the
    rail its first member and drop the rest.
    """
    parts = [p for p in parts if p is not None]
    if not parts:
        _fail(f"{label}: nothing to encode")
    head = parts[0]
    for i, p in enumerate(parts[1:], start=1):
        if p.shape[3:] != head.shape[3:]:
            _fail(f"{label}: source {i} encoded to {tuple(p.shape[2:])}, which "
                  f"does not stack onto {tuple(head.shape[2:])}. Sources must "
                  f"share a canvas.")
    if len(parts) == 1:
        return parts[0]
    return torch.cat([p.to(dtype=head.dtype, device=head.device) for p in parts], dim=2)


def snap_video_frames(frames):
    """Core's reference-video length: at least 5, and `n % 17 == 5`."""
    n = int(frames.shape[0])
    if n < 5:
        _fail(f"a video RefMod needs at least 5 frames (got {n})")
    while n % 17 != 5:
        n -= 1
    if n < 5:
        _fail(f"a video RefMod needs at least 5 frames after the 17k+5 snap "
              f"(started at {int(frames.shape[0])})")
    return frames[:n]