coremlit 0.1.1

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

use crate::audio::whisper::{
  constants::DEFAULT_LANGUAGE_CODE,
  options::DecodingOptions,
  task_facts::{SpanKnowledge, TaskFacts, TaskFactsAccumulator},
  transcribe::LanguageObservation,
};

pub mod writer;

// ---------------------------------------------------------------------
// WordTiming
// ---------------------------------------------------------------------

/// A single word's decoded text, timing, and DTW alignment confidence
/// (Swift `WordTiming`, `Models.swift:622-641`).
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct WordTiming {
  word: String,
  tokens: Vec<u32>,
  start: f32,
  end: f32,
  probability: f32,
}

impl WordTiming {
  /// Builds a word timing from its already-known fields. DTW alignment
  /// always has all five in hand at once, and Swift's `init` has no
  /// default parameters either (`Models.swift:634`) — so there is no
  /// partial or zero-value construction path.
  pub fn new(
    word: impl Into<String>,
    tokens: impl Into<Vec<u32>>,
    start: f32,
    end: f32,
    probability: f32,
  ) -> Self {
    Self {
      word: word.into(),
      tokens: tokens.into(),
      start,
      end,
      probability,
    }
  }

  /// The word's decoded text.
  #[inline(always)]
  pub fn word(&self) -> &str {
    self.word.as_str()
  }

  /// Token ids that decode to this word.
  #[inline(always)]
  pub const fn tokens_slice(&self) -> &[u32] {
    self.tokens.as_slice()
  }

  /// Start time, in seconds.
  #[inline(always)]
  pub const fn start(&self) -> f32 {
    self.start
  }

  /// End time, in seconds.
  #[inline(always)]
  pub const fn end(&self) -> f32 {
    self.end
  }

  /// Sets the start time, in seconds.
  #[inline(always)]
  pub const fn set_start(&mut self, start: f32) -> &mut Self {
    self.start = start;
    self
  }

  /// Sets the end time, in seconds.
  #[inline(always)]
  pub const fn set_end(&mut self, end: f32) -> &mut Self {
    self.end = end;
    self
  }

  /// DTW alignment confidence.
  #[inline(always)]
  pub const fn probability(&self) -> f32 {
    self.probability
  }

  /// `end - start` (Swift `WordTiming.duration`, `Models.swift:630-632`).
  #[inline(always)]
  pub const fn duration(&self) -> f32 {
    self.end - self.start
  }
}

// ---------------------------------------------------------------------
// TranscriptionSegment
// ---------------------------------------------------------------------

/// Default [`TranscriptionSegment::temperature`] (Swift
/// `Models.swift:601` — NOT `f32::default()`).
pub const DEFAULT_SEGMENT_TEMPERATURE: f32 = 1.0;
/// Default [`TranscriptionSegment::compression_ratio`] (Swift
/// `Models.swift:603` — NOT `f32::default()`).
pub const DEFAULT_SEGMENT_COMPRESSION_RATIO: f32 = 1.0;

#[cfg(feature = "serde")]
fn default_segment_temperature() -> f32 {
  DEFAULT_SEGMENT_TEMPERATURE
}
#[cfg(feature = "serde")]
fn default_segment_compression_ratio() -> f32 {
  DEFAULT_SEGMENT_COMPRESSION_RATIO
}

/// One transcribed segment of a window (Swift `TranscriptionSegment`,
/// `Models.swift:574-620`).
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct TranscriptionSegment {
  /// Segment index within the transcription.
  #[cfg_attr(feature = "serde", serde(default))]
  id: usize,
  /// Seek position, in samples, this segment started decoding from.
  #[cfg_attr(feature = "serde", serde(default))]
  seek: usize,
  /// Start time, in seconds.
  #[cfg_attr(feature = "serde", serde(default))]
  start: f32,
  /// End time, in seconds.
  #[cfg_attr(feature = "serde", serde(default))]
  end: f32,
  /// Decoded text.
  #[cfg_attr(feature = "serde", serde(default))]
  text: String,
  /// Sampled token ids.
  #[cfg_attr(
    feature = "serde",
    serde(default, skip_serializing_if = "Vec::is_empty")
  )]
  tokens: Vec<u32>,
  /// Per-step `(token id, log probability)`.
  #[cfg_attr(
    feature = "serde",
    serde(default, skip_serializing_if = "Vec::is_empty")
  )]
  token_log_probs: Vec<(u32, f32)>,
  /// Sampling temperature this segment was decoded at.
  ///
  /// Bridged through the finite-float `serde` helper (codex round 3, F6) — the
  /// one segment float that is: it is what `provenance`'s
  /// `unanimous_temperature` reads for the effective temperature it records, so
  /// a non-finite value silently changing across a round
  /// trip would corrupt that record. The descriptive telemetry floats beside
  /// it (`avg_logprob`, `compression_ratio`, `no_speech_prob`) are left as-is:
  /// `compression_ratio` legitimately reaches `f32::INFINITY` on empty text.
  #[cfg_attr(
    feature = "serde",
    serde(
      default = "default_segment_temperature",
      with = "crate::audio::whisper::options::finite_f32"
    )
  )]
  temperature: f32,
  /// Average sampled-token log probability.
  #[cfg_attr(feature = "serde", serde(default))]
  avg_logprob: f32,
  /// Compression ratio of `text` (repetition signal).
  #[cfg_attr(
    feature = "serde",
    serde(default = "default_segment_compression_ratio")
  )]
  compression_ratio: f32,
  /// Probability this segment contains no speech.
  #[cfg_attr(feature = "serde", serde(default))]
  no_speech_prob: f32,
  /// Word-level timings from DTW alignment. Empty means word timestamps
  /// were not computed (golden empty-means-absent).
  #[cfg_attr(
    feature = "serde",
    serde(default, skip_serializing_if = "Vec::is_empty")
  )]
  words: Vec<WordTiming>,
}

impl Default for TranscriptionSegment {
  fn default() -> Self {
    Self::new()
  }
}

impl TranscriptionSegment {
  /// A segment matching Swift's all-default `init`
  /// (`Models.swift:593-606`).
  pub const fn new() -> Self {
    Self {
      id: 0,
      seek: 0,
      start: 0.0,
      end: 0.0,
      text: String::new(),
      tokens: Vec::new(),
      token_log_probs: Vec::new(),
      temperature: DEFAULT_SEGMENT_TEMPERATURE,
      avg_logprob: 0.0,
      compression_ratio: DEFAULT_SEGMENT_COMPRESSION_RATIO,
      no_speech_prob: 0.0,
      words: Vec::new(),
    }
  }

  // -- id -------------------------------------------------------------
  /// Segment index within the transcription.
  #[inline(always)]
  pub const fn id(&self) -> usize {
    self.id
  }
  /// Builder form of [`Self::set_id`].
  #[must_use]
  #[inline(always)]
  pub const fn with_id(mut self, id: usize) -> Self {
    self.set_id(id);
    self
  }
  /// Sets [`Self::id`] in place.
  #[inline(always)]
  pub const fn set_id(&mut self, id: usize) -> &mut Self {
    self.id = id;
    self
  }

  // -- seek -------------------------------------------------------------
  /// Seek position, in samples, this segment started decoding from.
  #[inline(always)]
  pub const fn seek(&self) -> usize {
    self.seek
  }
  /// Builder form of [`Self::set_seek`].
  #[must_use]
  #[inline(always)]
  pub const fn with_seek(mut self, seek: usize) -> Self {
    self.set_seek(seek);
    self
  }
  /// Sets [`Self::seek`] in place.
  #[inline(always)]
  pub const fn set_seek(&mut self, seek: usize) -> &mut Self {
    self.seek = seek;
    self
  }

  // -- start -------------------------------------------------------------
  /// Start time, in seconds.
  #[inline(always)]
  pub const fn start(&self) -> f32 {
    self.start
  }
  /// Builder form of [`Self::set_start`].
  #[must_use]
  #[inline(always)]
  pub const fn with_start(mut self, start: f32) -> Self {
    self.set_start(start);
    self
  }
  /// Sets [`Self::start`] in place.
  #[inline(always)]
  pub const fn set_start(&mut self, start: f32) -> &mut Self {
    self.start = start;
    self
  }

  // -- end -------------------------------------------------------------
  /// End time, in seconds.
  #[inline(always)]
  pub const fn end(&self) -> f32 {
    self.end
  }
  /// Builder form of [`Self::set_end`].
  #[must_use]
  #[inline(always)]
  pub const fn with_end(mut self, end: f32) -> Self {
    self.set_end(end);
    self
  }
  /// Sets [`Self::end`] in place.
  #[inline(always)]
  pub const fn set_end(&mut self, end: f32) -> &mut Self {
    self.end = end;
    self
  }

  // -- text -------------------------------------------------------------
  /// Decoded text.
  #[inline(always)]
  pub fn text(&self) -> &str {
    self.text.as_str()
  }
  /// Builder form of [`Self::set_text`].
  #[must_use]
  #[inline(always)]
  pub fn with_text(mut self, text: impl Into<String>) -> Self {
    self.set_text(text);
    self
  }
  /// Sets [`Self::text`] in place.
  #[inline(always)]
  pub fn set_text(&mut self, text: impl Into<String>) -> &mut Self {
    self.text = text.into();
    self
  }

  // -- tokens (Vec<u32>) -------------------------------------------------
  /// Sampled token ids.
  #[inline(always)]
  pub const fn tokens_slice(&self) -> &[u32] {
    self.tokens.as_slice()
  }
  /// Builder form of [`Self::set_tokens`].
  #[must_use]
  #[inline(always)]
  pub fn with_tokens(mut self, tokens: impl Into<Vec<u32>>) -> Self {
    self.set_tokens(tokens);
    self
  }
  /// Sets [`Self::tokens_slice`] in place.
  #[inline(always)]
  pub fn set_tokens(&mut self, tokens: impl Into<Vec<u32>>) -> &mut Self {
    self.tokens = tokens.into();
    self
  }

  // -- token_log_probs (Vec<(u32,f32)>) -----------------------------------
  /// Per-step `(token id, log probability)`.
  #[inline(always)]
  pub const fn token_log_probs_slice(&self) -> &[(u32, f32)] {
    self.token_log_probs.as_slice()
  }
  /// Builder form of [`Self::set_token_log_probs`].
  #[must_use]
  #[inline(always)]
  pub fn with_token_log_probs(mut self, token_log_probs: impl Into<Vec<(u32, f32)>>) -> Self {
    self.set_token_log_probs(token_log_probs);
    self
  }
  /// Sets [`Self::token_log_probs_slice`] in place.
  #[inline(always)]
  pub fn set_token_log_probs(&mut self, token_log_probs: impl Into<Vec<(u32, f32)>>) -> &mut Self {
    self.token_log_probs = token_log_probs.into();
    self
  }

  // -- temperature -------------------------------------------------------
  /// Sampling temperature this segment was decoded at.
  #[inline(always)]
  pub const fn temperature(&self) -> f32 {
    self.temperature
  }
  /// Builder form of [`Self::set_temperature`].
  #[must_use]
  #[inline(always)]
  pub const fn with_temperature(mut self, temperature: f32) -> Self {
    self.set_temperature(temperature);
    self
  }
  /// Sets [`Self::temperature`] in place.
  #[inline(always)]
  pub const fn set_temperature(&mut self, temperature: f32) -> &mut Self {
    self.temperature = temperature;
    self
  }

  // -- avg_logprob -------------------------------------------------------
  /// Average sampled-token log probability.
  #[inline(always)]
  pub const fn avg_logprob(&self) -> f32 {
    self.avg_logprob
  }
  /// Builder form of [`Self::set_avg_logprob`].
  #[must_use]
  #[inline(always)]
  pub const fn with_avg_logprob(mut self, avg_logprob: f32) -> Self {
    self.set_avg_logprob(avg_logprob);
    self
  }
  /// Sets [`Self::avg_logprob`] in place.
  #[inline(always)]
  pub const fn set_avg_logprob(&mut self, avg_logprob: f32) -> &mut Self {
    self.avg_logprob = avg_logprob;
    self
  }

  // -- compression_ratio ---------------------------------------------------
  /// Compression ratio of [`Self::text`] (repetition signal).
  #[inline(always)]
  pub const fn compression_ratio(&self) -> f32 {
    self.compression_ratio
  }
  /// Builder form of [`Self::set_compression_ratio`].
  #[must_use]
  #[inline(always)]
  pub const fn with_compression_ratio(mut self, compression_ratio: f32) -> Self {
    self.set_compression_ratio(compression_ratio);
    self
  }
  /// Sets [`Self::compression_ratio`] in place.
  #[inline(always)]
  pub const fn set_compression_ratio(&mut self, compression_ratio: f32) -> &mut Self {
    self.compression_ratio = compression_ratio;
    self
  }

  // -- no_speech_prob -------------------------------------------------------
  /// Probability this segment contains no speech.
  #[inline(always)]
  pub const fn no_speech_prob(&self) -> f32 {
    self.no_speech_prob
  }
  /// Builder form of [`Self::set_no_speech_prob`].
  #[must_use]
  #[inline(always)]
  pub const fn with_no_speech_prob(mut self, no_speech_prob: f32) -> Self {
    self.set_no_speech_prob(no_speech_prob);
    self
  }
  /// Sets [`Self::no_speech_prob`] in place.
  #[inline(always)]
  pub const fn set_no_speech_prob(&mut self, no_speech_prob: f32) -> &mut Self {
    self.no_speech_prob = no_speech_prob;
    self
  }

  // -- words (Vec<WordTiming>) ---------------------------------------------
  /// Word-level timings from DTW alignment. Empty means word timestamps
  /// were not computed.
  #[inline(always)]
  pub const fn words_slice(&self) -> &[WordTiming] {
    self.words.as_slice()
  }

  /// Mutable view of the per-word timings (fixed length; use
  /// [`Self::set_words`] to replace the collection). Word times carry no
  /// cross-field invariant, so in-place mutation is safe to expose — the
  /// chunker's seek re-anchoring shifts them directly.
  #[inline(always)]
  pub const fn words_slice_mut(&mut self) -> &mut [WordTiming] {
    self.words.as_mut_slice()
  }
  /// Builder form of [`Self::set_words`].
  #[must_use]
  #[inline(always)]
  pub fn with_words(mut self, words: impl Into<Vec<WordTiming>>) -> Self {
    self.set_words(words);
    self
  }
  /// Sets [`Self::words_slice`] in place.
  #[inline(always)]
  pub fn set_words(&mut self, words: impl Into<Vec<WordTiming>>) -> &mut Self {
    self.words = words.into();
    self
  }

  /// `end - start` (Swift `TranscriptionSegment.duration`,
  /// `Models.swift:588-591`).
  #[inline(always)]
  pub const fn duration(&self) -> f32 {
    self.end - self.start
  }
}

// ---------------------------------------------------------------------
// TranscriptionTimings
// ---------------------------------------------------------------------

/// Default [`TranscriptionTimings::pipeline_start`]/
/// [`TranscriptionTimings::first_token_time`]: a "not yet reached"
/// sentinel (Swift `Double.greatestFiniteMagnitude`, `Models.swift:810-
/// 811`).
pub const DEFAULT_PIPELINE_TIME_SENTINEL: f64 = f64::MAX;
/// Default [`TranscriptionTimings::input_audio_seconds`] floor (Swift
/// `0.001`, `Models.swift:812` — never a bare zero denominator).
pub const DEFAULT_INPUT_AUDIO_SECONDS: f64 = 0.001;

#[cfg(feature = "serde")]
fn default_pipeline_time_sentinel() -> f64 {
  DEFAULT_PIPELINE_TIME_SENTINEL
}
#[cfg(feature = "serde")]
fn default_input_audio_seconds() -> f64 {
  DEFAULT_INPUT_AUDIO_SECONDS
}

/// Pipeline timing accumulator (Swift `TranscriptionTimings`,
/// `Models.swift:730-844`): every stage duration/count Swift collects,
/// plus three projections computed from them
/// ([`Self::tokens_per_second`]/[`Self::real_time_factor`]/
/// [`Self::speed_factor`]). Every setter mutates in place and returns
/// `&mut Self` (no `with_*` builders — later pipeline stages accumulate
/// into a live `TranscriptionTimings` across many windows rather than
/// rebuilding one from scratch).
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct TranscriptionTimings {
  /// Absolute time the pipeline started; the sentinel means "not yet
  /// started".
  #[cfg_attr(feature = "serde", serde(default = "default_pipeline_time_sentinel"))]
  pipeline_start: f64,
  /// Absolute time the first token was produced; the sentinel means "not
  /// yet reached".
  #[cfg_attr(feature = "serde", serde(default = "default_pipeline_time_sentinel"))]
  first_token_time: f64,
  /// Length of the input audio, in seconds.
  #[cfg_attr(feature = "serde", serde(default = "default_input_audio_seconds"))]
  input_audio_seconds: f64,
  /// Total time loading all models.
  #[cfg_attr(feature = "serde", serde(default))]
  model_loading: f64,
  /// Time spent in the prewarm load/unload pass.
  #[cfg_attr(feature = "serde", serde(default))]
  prewarm_load_time: f64,
  /// Time spent loading the audio encoder model.
  #[cfg_attr(feature = "serde", serde(default))]
  encoder_load_time: f64,
  /// Time spent loading the text decoder model.
  #[cfg_attr(feature = "serde", serde(default))]
  decoder_load_time: f64,
  /// Time spent specializing the encoder for the Neural Engine.
  #[cfg_attr(feature = "serde", serde(default))]
  encoder_specialization_time: f64,
  /// Time spent specializing the decoder for the Neural Engine.
  #[cfg_attr(feature = "serde", serde(default))]
  decoder_specialization_time: f64,
  /// Time spent loading the tokenizer.
  #[cfg_attr(feature = "serde", serde(default))]
  tokenizer_load_time: f64,
  /// Time spent loading audio input.
  #[cfg_attr(feature = "serde", serde(default))]
  audio_loading: f64,
  /// Time spent padding/trimming decode windows to length — the per-window
  /// [`audio::pad_or_trim`](crate::audio::whisper::audio::pad_or_trim) in the
  /// seek loop, matching what Swift's `audioProcessing` bucket wraps
  /// (`TranscribeTask.swift`: slice + pad/trim + window preprocess). It does
  /// NOT include VAD chunking (`chunk_all`), which is untimed here and in
  /// Swift, nor mel extraction (tracked separately by [`Self::logmels`]).
  #[cfg_attr(feature = "serde", serde(default))]
  audio_processing: f64,
  /// Time spent computing mel-spectrogram features.
  #[cfg_attr(feature = "serde", serde(default))]
  logmels: f64,
  /// Time spent running the audio encoder.
  #[cfg_attr(feature = "serde", serde(default))]
  encoding: f64,
  /// Time spent initializing decoder state/matrices.
  #[cfg_attr(feature = "serde", serde(default))]
  decoding_init: f64,
  /// Total time spent in the decode loop, across all windows.
  #[cfg_attr(feature = "serde", serde(default))]
  decoding_loop: f64,
  /// Time spent on decoder model inference (predicting logits).
  #[cfg_attr(feature = "serde", serde(default))]
  decoding_predictions: f64,
  /// Time spent applying logits filters.
  #[cfg_attr(feature = "serde", serde(default))]
  decoding_filtering: f64,
  /// Time spent sampling the next token.
  #[cfg_attr(feature = "serde", serde(default))]
  decoding_sampling: f64,
  /// Time spent evaluating and retrying temperature fallbacks.
  #[cfg_attr(feature = "serde", serde(default))]
  decoding_fallback: f64,
  /// Time spent on window-level bookkeeping (includes word-timestamp
  /// time).
  #[cfg_attr(feature = "serde", serde(default))]
  decoding_windowing: f64,
  /// Time spent updating the KV cache.
  #[cfg_attr(feature = "serde", serde(default))]
  decoding_kv_caching: f64,
  /// Time spent computing word-level timestamps (DTW alignment).
  #[cfg_attr(feature = "serde", serde(default))]
  decoding_word_timestamps: f64,
  /// Time spent in the decode loop outside of model inference.
  #[cfg_attr(feature = "serde", serde(default))]
  decoding_non_prediction: f64,
  /// Number of audio-processing passes run (divisor for
  /// [`Self::audio_processing`]).
  #[cfg_attr(feature = "serde", serde(default))]
  total_audio_processing_runs: f64,
  /// Number of mel-spectrogram passes run (divisor for [`Self::logmels`]).
  #[cfg_attr(feature = "serde", serde(default))]
  total_logmel_runs: f64,
  /// Number of encoder passes run (divisor for [`Self::encoding`]).
  #[cfg_attr(feature = "serde", serde(default))]
  total_encoding_runs: f64,
  /// Number of decode-loop steps (tokens sampled) across all windows.
  #[cfg_attr(feature = "serde", serde(default))]
  total_decoding_loops: f64,
  /// Number of KV-cache updates run (divisor for
  /// [`Self::decoding_kv_caching`]).
  #[cfg_attr(feature = "serde", serde(default))]
  total_kv_update_runs: f64,
  /// Number of word-timestamp alignment passes run.
  #[cfg_attr(feature = "serde", serde(default))]
  total_timestamp_alignment_runs: f64,
  /// Zero-based attempt index of the most recent temperature fallback — a
  /// faithful port of Swift's `totalDecodingFallbacks = Double(i)`
  /// (`TranscribeTask.swift`), *assigned* (not accumulated) on each
  /// fallback, so despite the name it is NOT a fallback count. The first
  /// fallback writes `0.0`, which is also this field's initial value, so
  /// `0.0` cannot distinguish "never fell back" from "one fallback at
  /// attempt 0"; a true count would be `attempt + 1`. A multi-window task
  /// overwrites per window, keeping only the last window's index, and
  /// merged results sum these per-result values.
  /// [`TaskFacts::drew_from_rng`](crate::audio::whisper::task_facts::TaskFacts::drew_from_rng)
  /// is the unambiguous "a fallback occurred" signal only when the base
  /// temperature is zero (the default) — attempt 0 then always argmaxes, so
  /// any draw implies a fallback raised the temperature first. Under a
  /// configured non-zero base temperature (e.g.
  /// `DecodingOptions::new().with_temperature(0.5)`) attempt 0 itself draws
  /// with zero fallbacks, and a degenerate retry (zero tokens decoded, or
  /// `temperature_increment_on_fallback == 0.0`) can fall back without ever
  /// drawing.
  #[cfg_attr(feature = "serde", serde(default))]
  total_decoding_fallbacks: f64,
  /// Number of 30 s windows decoded.
  #[cfg_attr(feature = "serde", serde(default))]
  total_decoding_windows: f64,
  /// Total end-to-end pipeline duration.
  #[cfg_attr(feature = "serde", serde(default))]
  full_pipeline: f64,
}

impl Default for TranscriptionTimings {
  fn default() -> Self {
    Self::new()
  }
}

impl TranscriptionTimings {
  /// A fresh timings accumulator (Swift `TranscriptionTimings.init`,
  /// `Models.swift:778-843`): every duration/count starts at zero except
  /// the two "not yet reached" sentinels and the audio-seconds floor.
  pub const fn new() -> Self {
    Self {
      pipeline_start: DEFAULT_PIPELINE_TIME_SENTINEL,
      first_token_time: DEFAULT_PIPELINE_TIME_SENTINEL,
      input_audio_seconds: DEFAULT_INPUT_AUDIO_SECONDS,
      model_loading: 0.0,
      prewarm_load_time: 0.0,
      encoder_load_time: 0.0,
      decoder_load_time: 0.0,
      encoder_specialization_time: 0.0,
      decoder_specialization_time: 0.0,
      tokenizer_load_time: 0.0,
      audio_loading: 0.0,
      audio_processing: 0.0,
      logmels: 0.0,
      encoding: 0.0,
      decoding_init: 0.0,
      decoding_loop: 0.0,
      decoding_predictions: 0.0,
      decoding_filtering: 0.0,
      decoding_sampling: 0.0,
      decoding_fallback: 0.0,
      decoding_windowing: 0.0,
      decoding_kv_caching: 0.0,
      decoding_word_timestamps: 0.0,
      decoding_non_prediction: 0.0,
      total_audio_processing_runs: 0.0,
      total_logmel_runs: 0.0,
      total_encoding_runs: 0.0,
      total_decoding_loops: 0.0,
      total_kv_update_runs: 0.0,
      total_timestamp_alignment_runs: 0.0,
      total_decoding_fallbacks: 0.0,
      total_decoding_windows: 0.0,
      full_pipeline: 0.0,
    }
  }

  // -- pipeline_start ------------------------------------------------------
  /// Absolute time the pipeline started; the sentinel means "not yet started".
  #[inline(always)]
  pub const fn pipeline_start(&self) -> f64 {
    self.pipeline_start
  }
  /// Sets [`Self::pipeline_start`] in place.
  #[inline(always)]
  pub const fn set_pipeline_start(&mut self, pipeline_start: f64) -> &mut Self {
    self.pipeline_start = pipeline_start;
    self
  }

  // -- first_token_time ----------------------------------------------------
  /// Absolute time the first token was produced; the sentinel means "not yet reached".
  #[inline(always)]
  pub const fn first_token_time(&self) -> f64 {
    self.first_token_time
  }
  /// Sets [`Self::first_token_time`] in place.
  #[inline(always)]
  pub const fn set_first_token_time(&mut self, first_token_time: f64) -> &mut Self {
    self.first_token_time = first_token_time;
    self
  }

  // -- input_audio_seconds -------------------------------------------------
  /// Length of the input audio, in seconds.
  #[inline(always)]
  pub const fn input_audio_seconds(&self) -> f64 {
    self.input_audio_seconds
  }
  /// Sets [`Self::input_audio_seconds`] in place.
  #[inline(always)]
  pub const fn set_input_audio_seconds(&mut self, input_audio_seconds: f64) -> &mut Self {
    self.input_audio_seconds = input_audio_seconds;
    self
  }

  // -- model_loading -------------------------------------------------------
  /// Total time loading all models.
  #[inline(always)]
  pub const fn model_loading(&self) -> f64 {
    self.model_loading
  }
  /// Sets [`Self::model_loading`] in place.
  #[inline(always)]
  pub const fn set_model_loading(&mut self, model_loading: f64) -> &mut Self {
    self.model_loading = model_loading;
    self
  }

  // -- prewarm_load_time ---------------------------------------------------
  /// Time spent in the prewarm load/unload pass.
  #[inline(always)]
  pub const fn prewarm_load_time(&self) -> f64 {
    self.prewarm_load_time
  }
  /// Sets [`Self::prewarm_load_time`] in place.
  #[inline(always)]
  pub const fn set_prewarm_load_time(&mut self, prewarm_load_time: f64) -> &mut Self {
    self.prewarm_load_time = prewarm_load_time;
    self
  }

  // -- encoder_load_time ---------------------------------------------------
  /// Time spent loading the audio encoder model.
  #[inline(always)]
  pub const fn encoder_load_time(&self) -> f64 {
    self.encoder_load_time
  }
  /// Sets [`Self::encoder_load_time`] in place.
  #[inline(always)]
  pub const fn set_encoder_load_time(&mut self, encoder_load_time: f64) -> &mut Self {
    self.encoder_load_time = encoder_load_time;
    self
  }

  // -- decoder_load_time ---------------------------------------------------
  /// Time spent loading the text decoder model.
  #[inline(always)]
  pub const fn decoder_load_time(&self) -> f64 {
    self.decoder_load_time
  }
  /// Sets [`Self::decoder_load_time`] in place.
  #[inline(always)]
  pub const fn set_decoder_load_time(&mut self, decoder_load_time: f64) -> &mut Self {
    self.decoder_load_time = decoder_load_time;
    self
  }

  // -- encoder_specialization_time -----------------------------------------
  /// Time spent specializing the encoder for the Neural Engine.
  #[inline(always)]
  pub const fn encoder_specialization_time(&self) -> f64 {
    self.encoder_specialization_time
  }
  /// Sets [`Self::encoder_specialization_time`] in place.
  #[inline(always)]
  pub const fn set_encoder_specialization_time(
    &mut self,
    encoder_specialization_time: f64,
  ) -> &mut Self {
    self.encoder_specialization_time = encoder_specialization_time;
    self
  }

  // -- decoder_specialization_time -----------------------------------------
  /// Time spent specializing the decoder for the Neural Engine.
  #[inline(always)]
  pub const fn decoder_specialization_time(&self) -> f64 {
    self.decoder_specialization_time
  }
  /// Sets [`Self::decoder_specialization_time`] in place.
  #[inline(always)]
  pub const fn set_decoder_specialization_time(
    &mut self,
    decoder_specialization_time: f64,
  ) -> &mut Self {
    self.decoder_specialization_time = decoder_specialization_time;
    self
  }

  // -- tokenizer_load_time -------------------------------------------------
  /// Time spent loading the tokenizer.
  #[inline(always)]
  pub const fn tokenizer_load_time(&self) -> f64 {
    self.tokenizer_load_time
  }
  /// Sets [`Self::tokenizer_load_time`] in place.
  #[inline(always)]
  pub const fn set_tokenizer_load_time(&mut self, tokenizer_load_time: f64) -> &mut Self {
    self.tokenizer_load_time = tokenizer_load_time;
    self
  }

  // -- audio_loading -------------------------------------------------------
  /// Time spent loading audio input.
  #[inline(always)]
  pub const fn audio_loading(&self) -> f64 {
    self.audio_loading
  }
  /// Sets [`Self::audio_loading`] in place.
  #[inline(always)]
  pub const fn set_audio_loading(&mut self, audio_loading: f64) -> &mut Self {
    self.audio_loading = audio_loading;
    self
  }

  // -- audio_processing ----------------------------------------------------
  /// Time spent padding/trimming decode windows to length — the per-window
  /// [`audio::pad_or_trim`](crate::audio::whisper::audio::pad_or_trim) in the
  /// seek loop, matching what Swift's `audioProcessing` bucket wraps
  /// (`TranscribeTask.swift`: slice + pad/trim + window preprocess). It does
  /// NOT include VAD chunking (`chunk_all`), which is untimed here and in
  /// Swift, nor mel extraction (tracked separately by [`Self::logmels`]).
  #[inline(always)]
  pub const fn audio_processing(&self) -> f64 {
    self.audio_processing
  }
  /// Sets [`Self::audio_processing`] in place.
  #[inline(always)]
  pub const fn set_audio_processing(&mut self, audio_processing: f64) -> &mut Self {
    self.audio_processing = audio_processing;
    self
  }

  // -- logmels -------------------------------------------------------------
  /// Time spent computing mel-spectrogram features.
  #[inline(always)]
  pub const fn logmels(&self) -> f64 {
    self.logmels
  }
  /// Sets [`Self::logmels`] in place.
  #[inline(always)]
  pub const fn set_logmels(&mut self, logmels: f64) -> &mut Self {
    self.logmels = logmels;
    self
  }

  // -- encoding ------------------------------------------------------------
  /// Time spent running the audio encoder.
  #[inline(always)]
  pub const fn encoding(&self) -> f64 {
    self.encoding
  }
  /// Sets [`Self::encoding`] in place.
  #[inline(always)]
  pub const fn set_encoding(&mut self, encoding: f64) -> &mut Self {
    self.encoding = encoding;
    self
  }

  // -- decoding_init -------------------------------------------------------
  /// Time spent initializing decoder state/matrices.
  #[inline(always)]
  pub const fn decoding_init(&self) -> f64 {
    self.decoding_init
  }
  /// Sets [`Self::decoding_init`] in place.
  #[inline(always)]
  pub const fn set_decoding_init(&mut self, decoding_init: f64) -> &mut Self {
    self.decoding_init = decoding_init;
    self
  }

  // -- decoding_loop -------------------------------------------------------
  /// Total time spent in the decode loop, across all windows.
  #[inline(always)]
  pub const fn decoding_loop(&self) -> f64 {
    self.decoding_loop
  }
  /// Sets [`Self::decoding_loop`] in place.
  #[inline(always)]
  pub const fn set_decoding_loop(&mut self, decoding_loop: f64) -> &mut Self {
    self.decoding_loop = decoding_loop;
    self
  }

  // -- decoding_predictions ------------------------------------------------
  /// Time spent on decoder model inference (predicting logits).
  #[inline(always)]
  pub const fn decoding_predictions(&self) -> f64 {
    self.decoding_predictions
  }
  /// Sets [`Self::decoding_predictions`] in place.
  #[inline(always)]
  pub const fn set_decoding_predictions(&mut self, decoding_predictions: f64) -> &mut Self {
    self.decoding_predictions = decoding_predictions;
    self
  }

  // -- decoding_filtering --------------------------------------------------
  /// Time spent applying logits filters.
  #[inline(always)]
  pub const fn decoding_filtering(&self) -> f64 {
    self.decoding_filtering
  }
  /// Sets [`Self::decoding_filtering`] in place.
  #[inline(always)]
  pub const fn set_decoding_filtering(&mut self, decoding_filtering: f64) -> &mut Self {
    self.decoding_filtering = decoding_filtering;
    self
  }

  // -- decoding_sampling ---------------------------------------------------
  /// Time spent sampling the next token.
  #[inline(always)]
  pub const fn decoding_sampling(&self) -> f64 {
    self.decoding_sampling
  }
  /// Sets [`Self::decoding_sampling`] in place.
  #[inline(always)]
  pub const fn set_decoding_sampling(&mut self, decoding_sampling: f64) -> &mut Self {
    self.decoding_sampling = decoding_sampling;
    self
  }

  // -- decoding_fallback ---------------------------------------------------
  /// Time spent evaluating and retrying temperature fallbacks.
  #[inline(always)]
  pub const fn decoding_fallback(&self) -> f64 {
    self.decoding_fallback
  }
  /// Sets [`Self::decoding_fallback`] in place.
  #[inline(always)]
  pub const fn set_decoding_fallback(&mut self, decoding_fallback: f64) -> &mut Self {
    self.decoding_fallback = decoding_fallback;
    self
  }

  // -- decoding_windowing --------------------------------------------------
  /// Time spent on window-level bookkeeping (includes word-timestamp time).
  #[inline(always)]
  pub const fn decoding_windowing(&self) -> f64 {
    self.decoding_windowing
  }
  /// Sets [`Self::decoding_windowing`] in place.
  #[inline(always)]
  pub const fn set_decoding_windowing(&mut self, decoding_windowing: f64) -> &mut Self {
    self.decoding_windowing = decoding_windowing;
    self
  }

  // -- decoding_kv_caching -------------------------------------------------
  /// Time spent updating the KV cache.
  #[inline(always)]
  pub const fn decoding_kv_caching(&self) -> f64 {
    self.decoding_kv_caching
  }
  /// Sets [`Self::decoding_kv_caching`] in place.
  #[inline(always)]
  pub const fn set_decoding_kv_caching(&mut self, decoding_kv_caching: f64) -> &mut Self {
    self.decoding_kv_caching = decoding_kv_caching;
    self
  }

  // -- decoding_word_timestamps --------------------------------------------
  /// Time spent computing word-level timestamps (DTW alignment).
  #[inline(always)]
  pub const fn decoding_word_timestamps(&self) -> f64 {
    self.decoding_word_timestamps
  }
  /// Sets [`Self::decoding_word_timestamps`] in place.
  #[inline(always)]
  pub const fn set_decoding_word_timestamps(&mut self, decoding_word_timestamps: f64) -> &mut Self {
    self.decoding_word_timestamps = decoding_word_timestamps;
    self
  }

  // -- decoding_non_prediction ---------------------------------------------
  /// Time spent in the decode loop outside of model inference.
  #[inline(always)]
  pub const fn decoding_non_prediction(&self) -> f64 {
    self.decoding_non_prediction
  }
  /// Sets [`Self::decoding_non_prediction`] in place.
  #[inline(always)]
  pub const fn set_decoding_non_prediction(&mut self, decoding_non_prediction: f64) -> &mut Self {
    self.decoding_non_prediction = decoding_non_prediction;
    self
  }

  // -- total_audio_processing_runs -----------------------------------------
  /// Number of audio-processing passes run.
  #[inline(always)]
  pub const fn total_audio_processing_runs(&self) -> f64 {
    self.total_audio_processing_runs
  }
  /// Sets [`Self::total_audio_processing_runs`] in place.
  #[inline(always)]
  pub const fn set_total_audio_processing_runs(
    &mut self,
    total_audio_processing_runs: f64,
  ) -> &mut Self {
    self.total_audio_processing_runs = total_audio_processing_runs;
    self
  }

  // -- total_logmel_runs ---------------------------------------------------
  /// Number of mel-spectrogram passes run.
  #[inline(always)]
  pub const fn total_logmel_runs(&self) -> f64 {
    self.total_logmel_runs
  }
  /// Sets [`Self::total_logmel_runs`] in place.
  #[inline(always)]
  pub const fn set_total_logmel_runs(&mut self, total_logmel_runs: f64) -> &mut Self {
    self.total_logmel_runs = total_logmel_runs;
    self
  }

  // -- total_encoding_runs -------------------------------------------------
  /// Number of encoder passes run.
  #[inline(always)]
  pub const fn total_encoding_runs(&self) -> f64 {
    self.total_encoding_runs
  }
  /// Sets [`Self::total_encoding_runs`] in place.
  #[inline(always)]
  pub const fn set_total_encoding_runs(&mut self, total_encoding_runs: f64) -> &mut Self {
    self.total_encoding_runs = total_encoding_runs;
    self
  }

  // -- total_decoding_loops ------------------------------------------------
  /// Number of decode-loop steps (tokens sampled) across all windows.
  #[inline(always)]
  pub const fn total_decoding_loops(&self) -> f64 {
    self.total_decoding_loops
  }
  /// Sets [`Self::total_decoding_loops`] in place.
  #[inline(always)]
  pub const fn set_total_decoding_loops(&mut self, total_decoding_loops: f64) -> &mut Self {
    self.total_decoding_loops = total_decoding_loops;
    self
  }

  // -- total_kv_update_runs ------------------------------------------------
  /// Number of KV-cache updates run.
  #[inline(always)]
  pub const fn total_kv_update_runs(&self) -> f64 {
    self.total_kv_update_runs
  }
  /// Sets [`Self::total_kv_update_runs`] in place.
  #[inline(always)]
  pub const fn set_total_kv_update_runs(&mut self, total_kv_update_runs: f64) -> &mut Self {
    self.total_kv_update_runs = total_kv_update_runs;
    self
  }

  // -- total_timestamp_alignment_runs --------------------------------------
  /// Number of word-timestamp alignment passes run.
  #[inline(always)]
  pub const fn total_timestamp_alignment_runs(&self) -> f64 {
    self.total_timestamp_alignment_runs
  }
  /// Sets [`Self::total_timestamp_alignment_runs`] in place.
  #[inline(always)]
  pub const fn set_total_timestamp_alignment_runs(
    &mut self,
    total_timestamp_alignment_runs: f64,
  ) -> &mut Self {
    self.total_timestamp_alignment_runs = total_timestamp_alignment_runs;
    self
  }

  // -- total_decoding_fallbacks --------------------------------------------
  /// Zero-based attempt index of the most recent temperature fallback — a
  /// faithful port of Swift's `totalDecodingFallbacks = Double(i)`
  /// (`TranscribeTask.swift`), *assigned* (not accumulated) on each
  /// fallback, so despite the name it is NOT a fallback count. The first
  /// fallback writes `0.0`, which is also this field's initial value, so
  /// `0.0` cannot distinguish "never fell back" from "one fallback at
  /// attempt 0"; a true count would be `attempt + 1`. A multi-window task
  /// overwrites per window, keeping only the last window's index, and
  /// merged results sum these per-result values.
  /// [`TaskFacts::drew_from_rng`](crate::audio::whisper::task_facts::TaskFacts::drew_from_rng)
  /// is the unambiguous "a fallback occurred" signal only when the base
  /// temperature is zero (the default) — attempt 0 then always argmaxes, so
  /// any draw implies a fallback raised the temperature first. Under a
  /// configured non-zero base temperature (e.g.
  /// `DecodingOptions::new().with_temperature(0.5)`) attempt 0 itself draws
  /// with zero fallbacks, and a degenerate retry (zero tokens decoded, or
  /// `temperature_increment_on_fallback == 0.0`) can fall back without ever
  /// drawing.
  #[inline(always)]
  pub const fn total_decoding_fallbacks(&self) -> f64 {
    self.total_decoding_fallbacks
  }
  /// Sets [`Self::total_decoding_fallbacks`] in place.
  #[inline(always)]
  pub const fn set_total_decoding_fallbacks(&mut self, total_decoding_fallbacks: f64) -> &mut Self {
    self.total_decoding_fallbacks = total_decoding_fallbacks;
    self
  }

  // -- total_decoding_windows ----------------------------------------------
  /// Number of 30 s windows decoded.
  #[inline(always)]
  pub const fn total_decoding_windows(&self) -> f64 {
    self.total_decoding_windows
  }
  /// Sets [`Self::total_decoding_windows`] in place.
  #[inline(always)]
  pub const fn set_total_decoding_windows(&mut self, total_decoding_windows: f64) -> &mut Self {
    self.total_decoding_windows = total_decoding_windows;
    self
  }

  // -- full_pipeline -------------------------------------------------------
  /// Total end-to-end pipeline duration.
  #[inline(always)]
  pub const fn full_pipeline(&self) -> f64 {
    self.full_pipeline
  }
  /// Sets [`Self::full_pipeline`] in place.
  #[inline(always)]
  pub const fn set_full_pipeline(&mut self, full_pipeline: f64) -> &mut Self {
    self.full_pipeline = full_pipeline;
    self
  }

  /// Sampled tokens per second (Swift `tokensPerSecond`,
  /// `Models.swift:766-768`): `total_decoding_loops / full_pipeline`,
  /// guarded to `0.0` when `full_pipeline` is `0.0` (Swift's unguarded
  /// division would yield `NaN`/`inf`).
  #[inline(always)]
  pub const fn tokens_per_second(&self) -> f64 {
    if self.full_pipeline == 0.0 {
      0.0
    } else {
      self.total_decoding_loops / self.full_pipeline
    }
  }

  /// Wall-clock seconds per second of input audio (Swift
  /// `realTimeFactor`, `Models.swift:770-772`): `full_pipeline /
  /// input_audio_seconds`, guarded to `0.0` when `input_audio_seconds` is
  /// `0.0`.
  #[inline(always)]
  pub const fn real_time_factor(&self) -> f64 {
    if self.input_audio_seconds == 0.0 {
      0.0
    } else {
      self.full_pipeline / self.input_audio_seconds
    }
  }

  /// Inverse of [`Self::real_time_factor`] (Swift `speedFactor`,
  /// `Models.swift:774-776`): `input_audio_seconds / full_pipeline`,
  /// guarded to `0.0` when `full_pipeline` is `0.0`.
  #[inline(always)]
  pub const fn speed_factor(&self) -> f64 {
    if self.full_pipeline == 0.0 {
      0.0
    } else {
      self.input_audio_seconds / self.full_pipeline
    }
  }
}

// ---------------------------------------------------------------------
// TranscriptionResult
// ---------------------------------------------------------------------

/// Final transcription output (Swift `TranscriptionResultStruct`,
/// `Models.swift:543-563` — the value-type sibling of Swift's lock-guarded
/// `TranscriptionResult` reference type; see this module's doc comment
/// for why this port uses it instead).
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct TranscriptionResult {
  /// Full transcribed text (all segments concatenated).
  #[cfg_attr(feature = "serde", serde(default))]
  text: String,
  /// Transcribed segments, in order.
  #[cfg_attr(
    feature = "serde",
    serde(default, skip_serializing_if = "Vec::is_empty")
  )]
  segments: Vec<TranscriptionSegment>,
  /// Detected or configured spoken language (ISO code). Empty means
  /// undetermined (golden empty-means-absent).
  ///
  /// The Swift-faithful DISPLAY language, folded LAST-WRITE-WINS over the
  /// decode's per-window language probes — plus, for a window that did not
  /// probe, that window's own decoded language and finally the `"en"` fallback.
  /// Those probes are no longer discarded by the fold:
  /// [`Self::language_observations_slice`] carries them (coremlit issue #107).
  #[cfg_attr(
    feature = "serde",
    serde(default, skip_serializing_if = "String::is_empty")
  )]
  language: String,
  /// Every language probe the decode ran AND kept, spanned, in window order —
  /// the EVIDENCE the two folded languages were folded from (coremlit issue
  /// #107).
  ///
  /// One entry per decode window whose automatic-language probe ran and belonged
  /// to the attempt whose decode was kept; a window with no probe (detection
  /// off, a configured language, a monolingual model) or whose kept attempt's
  /// probe failed contributes nothing, so an empty list means "no kept probe",
  /// never "probed and discarded". Spans share the segments' time base and are
  /// re-anchored with them by
  /// [`chunker::apply_result_seek_offset`](crate::audio::whisper::audio::chunker::apply_result_seek_offset);
  /// a merge concatenates the children's lists in result order.
  ///
  /// **Relationship to the folded languages.** [`Self::language`] is
  /// last-write-wins over these and
  /// [`TaskFacts::observed_language`](crate::audio::whisper::task_facts::TaskFacts::observed_language)
  /// first-genuine-wins over them; BOTH keep the exact rules they had before
  /// this list existed, and this list is additive evidence, never an input to
  /// either. Neither is a pure function of it, so do not re-derive them here: a
  /// configured or fallback display language probes nothing at all, and a decode
  /// that PREDICTS a `<|lang|>` token feeds `observed_language` with no probe
  /// behind it. What the list does carry, and the scalars cannot, is the
  /// per-window geography — where each probe fell and how confident it was.
  #[cfg_attr(
    feature = "serde",
    serde(default, skip_serializing_if = "Vec::is_empty")
  )]
  language_observations: Vec<LanguageObservation>,
  /// Aggregated pipeline timings.
  #[cfg_attr(feature = "serde", serde(default))]
  timings: TranscriptionTimings,
  /// Seek position, in seconds, transcription stopped at. `None` when
  /// transcription ran to the end of the input.
  #[cfg_attr(
    feature = "serde",
    serde(default, skip_serializing_if = "Option::is_none")
  )]
  seek_time: Option<f32>,
  /// The decode-time facts this transcription **run controlled** — whether it
  /// drew from the token sampler, the language it genuinely observed, whether a
  /// progress callback truncated it, whether it swallowed a child error, the
  /// worker coordinates its RNG streams rode, and the segment-id span its decode
  /// allocated — as one carried record (coremlit issue #14, codex round 6).
  /// Replaces the six separate fields these facts used to scatter across, whose
  /// per-fact plumbing lost or fabricated three of them at aggregation
  /// boundaries; see
  /// [`TaskFacts`](crate::audio::whisper::task_facts::TaskFacts) for the history and the one
  /// merge law [`merge_transcription_results_with_options`] now folds them by.
  ///
  /// [`Provenance::for_result`](crate::audio::whisper::provenance::Provenance::for_result) reads
  /// this record whole; [`Self::new`] starts it
  /// [`TaskFacts::unknown`](crate::audio::whisper::task_facts::TaskFacts::unknown) (a hand-built
  /// result drew nothing, witnessed no language, was not truncated, and rode an
  /// **unknown** worker coordinate — never a fabricated `0`), and the pipeline
  /// sets the observed value via [`Self::with_task_facts`].
  ///
  /// Required on deserialize: the reproducibility facts inside it must never
  /// silently default to their optimistic values (see that type's serde
  /// contract).
  task_facts: TaskFacts,
}

impl TranscriptionResult {
  /// Builds a result from its four required fields (Swift
  /// `TranscriptionResultStruct.init`, `Models.swift:550-562`, has no
  /// defaults for these either); [`Self::seek_time`] starts `None` and
  /// [`Self::task_facts`] starts [`TaskFacts::unknown`].
  ///
  /// A result assembled by hand therefore records an **unknown** sampling draw,
  /// **no** observed language, an **unknown** callback truncation, an **unknown**
  /// swallowed child error, an **unknown** worker coordinate (never a fabricated
  /// `0`, R6-F2), and an untracked id span — explicit unknown throughout, never
  /// the optimistic `Some(false)` a "nothing happened" default would forge (F1).
  /// That is the right default for a caller inventing a transcript, and it is not
  /// a hole in the reproducibility guarantee:
  /// [`Provenance::for_result`](crate::audio::whisper::provenance::Provenance::for_result) TRUSTS
  /// this carried record whole rather than scanning the surviving segments' own
  /// temperatures (inferring the draw from the survivors was the bug it
  /// replaced), so an unknown draw is treated CONSERVATIVELY as non-reproducible,
  /// never optimistically waved through. What only the carried
  /// [`TaskFacts::drew_from_rng`](crate::audio::whisper::task_facts::TaskFacts::drew_from_rng)
  /// flag can carry is a sampled window whose segments are *gone* — and only
  /// the decode path can know about those, setting the observed facts via
  /// [`Self::with_task_facts`].
  pub fn new(
    text: impl Into<String>,
    segments: impl Into<Vec<TranscriptionSegment>>,
    language: impl Into<String>,
    timings: TranscriptionTimings,
  ) -> Self {
    Self {
      text: text.into(),
      segments: segments.into(),
      language: language.into(),
      // A hand-built result ran no probe, so it witnessed none. The pipeline
      // sets the real list via `with_language_observations`.
      language_observations: Vec::new(),
      timings,
      seek_time: None,
      // A hand-built result controlled no decode, so it cannot OBSERVE whether a
      // draw, a callback truncation, or a swallowed child error occurred: it
      // records explicit UNKNOWN for each (never the optimistic Some(false)),
      // witnessed no language, rode an UNKNOWN worker coordinate (never a
      // fabricated 0), and tracked no id span. The pipeline sets the observed
      // record via `with_task_facts`.
      task_facts: TaskFacts::unknown(),
    }
  }

  // -- text -------------------------------------------------------------
  /// Full transcribed text.
  #[inline(always)]
  pub fn text(&self) -> &str {
    self.text.as_str()
  }
  /// Builder form of [`Self::set_text`].
  #[must_use]
  #[inline(always)]
  pub fn with_text(mut self, text: impl Into<String>) -> Self {
    self.set_text(text);
    self
  }
  /// Sets [`Self::text`] in place.
  #[inline(always)]
  pub fn set_text(&mut self, text: impl Into<String>) -> &mut Self {
    self.text = text.into();
    self
  }

  // -- segments (Vec<TranscriptionSegment>) --------------------------------
  /// Transcribed segments, in order.
  #[inline(always)]
  pub const fn segments_slice(&self) -> &[TranscriptionSegment] {
    self.segments.as_slice()
  }

  /// Mutable view of the segments (fixed length; use
  /// [`Self::set_segments`] to replace the collection). Segment timings
  /// carry no cross-field invariant with the rest of the result, so
  /// in-place mutation is safe to expose — chunk re-anchoring shifts them
  /// directly.
  #[inline(always)]
  pub const fn segments_slice_mut(&mut self) -> &mut [TranscriptionSegment] {
    self.segments.as_mut_slice()
  }
  /// Builder form of [`Self::set_segments`].
  #[must_use]
  #[inline(always)]
  pub fn with_segments(mut self, segments: impl Into<Vec<TranscriptionSegment>>) -> Self {
    self.set_segments(segments);
    self
  }
  /// Sets [`Self::segments_slice`] in place.
  #[inline(always)]
  pub fn set_segments(&mut self, segments: impl Into<Vec<TranscriptionSegment>>) -> &mut Self {
    self.segments = segments.into();
    self
  }

  // -- language -----------------------------------------------------------
  /// Detected or configured spoken language (ISO code); empty means
  /// undetermined.
  #[inline(always)]
  pub fn language(&self) -> &str {
    self.language.as_str()
  }
  /// Builder form of [`Self::set_language`].
  #[must_use]
  #[inline(always)]
  pub fn with_language(mut self, language: impl Into<String>) -> Self {
    self.set_language(language);
    self
  }
  /// Sets [`Self::language`] in place.
  #[inline(always)]
  pub fn set_language(&mut self, language: impl Into<String>) -> &mut Self {
    self.language = language.into();
    self
  }

  // -- language_observations (Vec<LanguageObservation>) ---------------------
  /// Every language probe the decode ran and kept, spanned, in window order —
  /// the evidence [`Self::language`] and
  /// [`TaskFacts::observed_language`](crate::audio::whisper::task_facts::TaskFacts::observed_language)
  /// were folded from, and empty when no probe was kept. See the field's doc
  /// for the fold relationship and the span's time base.
  #[inline(always)]
  pub const fn language_observations_slice(&self) -> &[LanguageObservation] {
    self.language_observations.as_slice()
  }
  /// Mutable view of the observations (fixed length; use
  /// [`Self::set_language_observations`] to replace the collection) — chunk
  /// re-anchoring shifts their spans directly, exactly as it does the
  /// segments' through [`Self::segments_slice_mut`].
  #[inline(always)]
  pub const fn language_observations_slice_mut(&mut self) -> &mut [LanguageObservation] {
    self.language_observations.as_mut_slice()
  }
  /// Builder form of [`Self::set_language_observations`].
  #[must_use]
  #[inline(always)]
  pub fn with_language_observations(
    mut self,
    language_observations: impl Into<Vec<LanguageObservation>>,
  ) -> Self {
    self.set_language_observations(language_observations);
    self
  }
  /// Sets [`Self::language_observations_slice`] in place.
  #[inline(always)]
  pub fn set_language_observations(
    &mut self,
    language_observations: impl Into<Vec<LanguageObservation>>,
  ) -> &mut Self {
    self.language_observations = language_observations.into();
    self
  }

  // -- task_facts ----------------------------------------------------------
  /// The decode-time facts this transcription run **controlled** — the RNG
  /// draw, the observed language, the early-stop truncation, the worker
  /// coordinates, and the allocated id span — as one record.
  /// [`Provenance::for_result`](crate::audio::whisper::provenance::Provenance::for_result)
  /// reads it whole. See the field's doc.
  #[inline(always)]
  pub const fn task_facts(&self) -> &TaskFacts {
    &self.task_facts
  }
  /// Mutable access to [`Self::task_facts`] — the pipeline uses it to
  /// [`merge`](crate::audio::whisper::task_facts::TaskFacts::merge) a shared sink's recovered
  /// facts (a dropped-because-errored VAD chunk's draw/observation/early stop)
  /// into a merged transcript in place.
  #[inline(always)]
  pub const fn task_facts_mut(&mut self) -> &mut TaskFacts {
    &mut self.task_facts
  }
  /// Builder form of [`Self::set_task_facts`].
  #[must_use]
  #[inline(always)]
  pub fn with_task_facts(mut self, task_facts: TaskFacts) -> Self {
    self.set_task_facts(task_facts);
    self
  }
  /// Assigns [`Self::task_facts`] directly — the pipeline passes the record it
  /// accumulated across the decode.
  #[inline(always)]
  pub fn set_task_facts(&mut self, task_facts: TaskFacts) -> &mut Self {
    self.task_facts = task_facts;
    self
  }

  // -- timings --------------------------------------------------------------
  /// Aggregated pipeline timings.
  #[inline(always)]
  pub const fn timings(&self) -> &TranscriptionTimings {
    &self.timings
  }
  /// Builder form of [`Self::set_timings`].
  #[must_use]
  #[inline(always)]
  pub fn with_timings(mut self, timings: TranscriptionTimings) -> Self {
    self.set_timings(timings);
    self
  }
  /// Sets [`Self::timings`] in place.
  #[inline(always)]
  pub fn set_timings(&mut self, timings: TranscriptionTimings) -> &mut Self {
    self.timings = timings;
    self
  }

  // -- seek_time (Option<f32>) ----------------------------------------------
  /// Seek position, in seconds, transcription stopped at. `None` when
  /// transcription ran to the end of the input.
  #[inline(always)]
  pub const fn seek_time(&self) -> Option<f32> {
    self.seek_time
  }
  /// Builder form of [`Self::set_seek_time`].
  #[must_use]
  #[inline(always)]
  pub const fn with_seek_time(mut self, seek_time: f32) -> Self {
    self.set_seek_time(seek_time);
    self
  }
  /// Sets [`Self::seek_time`] to `Some(seek_time)`.
  #[inline(always)]
  pub const fn set_seek_time(&mut self, seek_time: f32) -> &mut Self {
    self.seek_time = Some(seek_time);
    self
  }
  /// Builder form of [`Self::update_seek_time`].
  #[must_use]
  #[inline(always)]
  pub const fn maybe_seek_time(mut self, seek_time: Option<f32>) -> Self {
    self.update_seek_time(seek_time);
    self
  }
  /// Assigns [`Self::seek_time`] directly.
  #[inline(always)]
  pub const fn update_seek_time(&mut self, seek_time: Option<f32>) -> &mut Self {
    self.seek_time = seek_time;
    self
  }
  /// Sets [`Self::seek_time`] to `None`.
  #[inline(always)]
  pub const fn clear_seek_time(&mut self) -> &mut Self {
    self.seek_time = None;
    self
  }

  // -- all_words ----------------------------------------------------------
  /// Every word timing across every segment, flattened in segment order —
  /// ports the `TranscriptionResult.allWords` extension (`Models.swift:
  /// 566-570`, `segments.compactMap { $0.words }.flatMap { $0 }`). This
  /// port's [`TranscriptionSegment::words_slice`] is never optional
  /// (empty-means-absent, this module's own doc comment), so a segment
  /// with no words simply contributes zero elements here — no
  /// `compactMap`-equivalent filter is needed to reproduce Swift's
  /// nil-dropping.
  pub fn all_words(&self) -> Vec<WordTiming> {
    self
      .segments
      .iter()
      .flat_map(TranscriptionSegment::words_slice)
      .cloned()
      .collect()
  }
}

// ---------------------------------------------------------------------
// DecodingResult
// ---------------------------------------------------------------------

/// Per-window decode output (Swift `DecodingResult`,
/// `Models.swift:383-439`). Three Swift fields have no place here:
/// `cache` (KV-cache tensors are a backend-layer concern, not a result
/// value type), `timings` (window timings roll up into
/// [`TranscriptionResult::timings`] instead of nesting per window), and
/// `fallback` (the fallback decision is a pure function of this type,
/// [`needs_fallback`], rather than a stored, mutually-recursive field).
///
/// One field goes the other way, beyond Swift's own set:
/// [`Self::first_token_log_prob`]. Swift computes the first sampled
/// token's log probability only transiently, inside the decode loop
/// (`TextDecoder.swift:662-667`), to build a local `isFirstTokenLogProbTooLow`
/// bool that never leaves the function. This port's decode loop
/// ([`crate::audio::whisper::decode::decode_text`]) has no such back door — its only
/// output is this struct — so the raw value is stored here instead,
/// letting a later fallback-ladder caller recompute the threshold
/// comparison itself and pass it to [`needs_fallback`] (that function's
/// own doc comment's "assumption (b)").
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct DecodingResult {
  /// Detected or configured spoken language (ISO code). Empty means
  /// undetermined (golden empty-means-absent).
  #[cfg_attr(
    feature = "serde",
    serde(default, skip_serializing_if = "String::is_empty")
  )]
  language: String,
  /// Per-language detection probabilities. Empty means language
  /// detection did not run.
  #[cfg_attr(
    feature = "serde",
    serde(default, skip_serializing_if = "Vec::is_empty")
  )]
  language_probs: Vec<(String, f32)>,
  /// The language the model actually PREDICTED (an ISO code), or `None` when it
  /// predicted none — a genuine in-loop language detection. Distinct from the
  /// Swift-faithful display [`Self::language`], which reports the FIRST language
  /// token in the WHOLE sequence (the forced prefill `<|en|>` included): this is
  /// the first `<|lang|>` token the model emitted at or after the forced prompt,
  /// so the two disagree exactly when a forced `<|en|>` prefill is followed by a
  /// differently-predicted language.
  ///
  /// `None` for a configured language (an input, not a detection), for the
  /// [`DEFAULT_LANGUAGE_CODE`] display fallback, and when the predicted region
  /// holds no language token at all (a zero-iteration decode forces `<|en|>`
  /// into the prompt but predicts nothing). The pipeline promotes THIS predicted
  /// code into the result's
  /// [`TaskFacts::observed_language`](crate::audio::whisper::task_facts::TaskFacts::observed_language);
  /// recording the display
  /// [`Self::language`] there instead would misreport a forced `<|en|>` as the
  /// detection when a different language was predicted after it (coremlit issue
  /// #14, codex round 5) — the reason this carries the predicted STRING and not
  /// a mere "observed" boolean. Not a Swift field — Swift's `DecodingResult` has
  /// no detection-provenance concept, the same Rust-only extension rationale as
  /// the sibling [`Self::first_token_log_prob`] (see this struct's doc).
  #[cfg_attr(
    feature = "serde",
    serde(default, skip_serializing_if = "Option::is_none")
  )]
  observed_language: Option<String>,
  /// Sampled token ids for this window.
  #[cfg_attr(
    feature = "serde",
    serde(default, skip_serializing_if = "Vec::is_empty")
  )]
  tokens: Vec<u32>,
  /// Per-step `(token id, log probability)`, in decode order; the first
  /// entry is the first sampled token ([`needs_fallback`] reads it).
  #[cfg_attr(
    feature = "serde",
    serde(default, skip_serializing_if = "Vec::is_empty")
  )]
  token_log_probs: Vec<(u32, f32)>,
  /// Decoded text for this window.
  #[cfg_attr(feature = "serde", serde(default))]
  text: String,
  /// Average sampled-token log probability.
  #[cfg_attr(feature = "serde", serde(default))]
  avg_logprob: f32,
  /// Probability this window contains no speech.
  #[cfg_attr(feature = "serde", serde(default))]
  no_speech_prob: f32,
  /// Sampling temperature this window was decoded at.
  #[cfg_attr(feature = "serde", serde(default))]
  temperature: f32,
  /// Compression ratio of `text` (repetition signal).
  #[cfg_attr(feature = "serde", serde(default))]
  compression_ratio: f32,
  /// The first sampled token's raw log probability (not a Swift field —
  /// see this struct's doc comment).
  #[cfg_attr(feature = "serde", serde(default))]
  first_token_log_prob: f32,
  /// Whether a progress callback requested an early stop that TRUNCATED this
  /// window's decode (`Some(false)` past the prefill steps) — a caller CONTROL
  /// action, distinct from every ordinary termination (EOT, the token-context
  /// cap, a too-low first-token log-prob). The pipeline carries this out to the
  /// result's [`TaskFacts::early_stopped`](crate::audio::whisper::task_facts::TaskFacts::early_stopped),
  /// which
  /// [`Provenance::is_reproducible`](crate::audio::whisper::provenance::Provenance::is_reproducible)
  /// reads: a transcript an unrecorded callback truncated cannot be reproduced
  /// from the recorded options and seed alone (coremlit issue #14, codex round
  /// 5). Not a Swift field — the same Rust-only extension rationale as the
  /// sibling [`Self::first_token_log_prob`].
  #[cfg_attr(feature = "serde", serde(default))]
  early_stopped: bool,
}

impl Default for DecodingResult {
  fn default() -> Self {
    Self::new()
  }
}

impl DecodingResult {
  /// A zero-value result (Swift `DecodingResult.emptyResults`,
  /// `Models.swift:397-410`).
  pub const fn new() -> Self {
    Self {
      language: String::new(),
      language_probs: Vec::new(),
      observed_language: None,
      tokens: Vec::new(),
      token_log_probs: Vec::new(),
      text: String::new(),
      avg_logprob: 0.0,
      no_speech_prob: 0.0,
      temperature: 0.0,
      compression_ratio: 0.0,
      first_token_log_prob: 0.0,
      early_stopped: false,
    }
  }

  // -- language ---------------------------------------------------------
  /// Detected or configured spoken language (ISO code); empty means
  /// undetermined.
  #[inline(always)]
  pub fn language(&self) -> &str {
    self.language.as_str()
  }
  /// Builder form of [`Self::set_language`].
  #[must_use]
  #[inline(always)]
  pub fn with_language(mut self, language: impl Into<String>) -> Self {
    self.set_language(language);
    self
  }
  /// Sets [`Self::language`] in place.
  #[inline(always)]
  pub fn set_language(&mut self, language: impl Into<String>) -> &mut Self {
    self.language = language.into();
    self
  }

  // -- language_probs (Vec<(String,f32)>) ----------------------------------
  /// Per-language detection probabilities. Empty means language
  /// detection did not run.
  #[inline(always)]
  pub const fn language_probs_slice(&self) -> &[(String, f32)] {
    self.language_probs.as_slice()
  }
  /// Builder form of [`Self::set_language_probs`].
  #[must_use]
  #[inline(always)]
  pub fn with_language_probs(mut self, language_probs: impl Into<Vec<(String, f32)>>) -> Self {
    self.set_language_probs(language_probs);
    self
  }
  /// Sets [`Self::language_probs_slice`] in place.
  #[inline(always)]
  pub fn set_language_probs(&mut self, language_probs: impl Into<Vec<(String, f32)>>) -> &mut Self {
    self.language_probs = language_probs.into();
    self
  }

  // -- observed_language (Option<String>) ----------------------------------
  /// The language the model actually PREDICTED (an ISO code), or `None`. A
  /// genuine in-loop detection, SEPARATE from the Swift-faithful display
  /// [`Self::language`]; see the field doc for how the pipeline promotes this
  /// into the result's
  /// [`TaskFacts::observed_language`](crate::audio::whisper::task_facts::TaskFacts::observed_language),
  /// and why it is not the display language.
  #[inline(always)]
  pub fn observed_language(&self) -> Option<&str> {
    self.observed_language.as_deref()
  }
  /// Builder form of [`Self::update_observed_language`].
  #[must_use]
  #[inline(always)]
  pub fn maybe_observed_language(mut self, observed_language: Option<String>) -> Self {
    self.update_observed_language(observed_language);
    self
  }
  /// Assigns [`Self::observed_language`] directly — the finalized decode passes
  /// the predicted language code, or `None` when nothing was predicted.
  #[inline(always)]
  pub fn update_observed_language(&mut self, observed_language: Option<String>) -> &mut Self {
    self.observed_language = observed_language;
    self
  }

  // -- tokens (Vec<u32>) ---------------------------------------------------
  /// Sampled token ids for this window.
  #[inline(always)]
  pub const fn tokens_slice(&self) -> &[u32] {
    self.tokens.as_slice()
  }
  /// Builder form of [`Self::set_tokens`].
  #[must_use]
  #[inline(always)]
  pub fn with_tokens(mut self, tokens: impl Into<Vec<u32>>) -> Self {
    self.set_tokens(tokens);
    self
  }
  /// Sets [`Self::tokens_slice`] in place.
  #[inline(always)]
  pub fn set_tokens(&mut self, tokens: impl Into<Vec<u32>>) -> &mut Self {
    self.tokens = tokens.into();
    self
  }

  // -- token_log_probs (Vec<(u32,f32)>) -------------------------------------
  /// Per-step `(token id, log probability)`; the first entry is the
  /// first sampled token.
  #[inline(always)]
  pub const fn token_log_probs_slice(&self) -> &[(u32, f32)] {
    self.token_log_probs.as_slice()
  }
  /// Builder form of [`Self::set_token_log_probs`].
  #[must_use]
  #[inline(always)]
  pub fn with_token_log_probs(mut self, token_log_probs: impl Into<Vec<(u32, f32)>>) -> Self {
    self.set_token_log_probs(token_log_probs);
    self
  }
  /// Sets [`Self::token_log_probs_slice`] in place.
  #[inline(always)]
  pub fn set_token_log_probs(&mut self, token_log_probs: impl Into<Vec<(u32, f32)>>) -> &mut Self {
    self.token_log_probs = token_log_probs.into();
    self
  }

  // -- text -----------------------------------------------------------------
  /// Decoded text for this window.
  #[inline(always)]
  pub fn text(&self) -> &str {
    self.text.as_str()
  }
  /// Builder form of [`Self::set_text`].
  #[must_use]
  #[inline(always)]
  pub fn with_text(mut self, text: impl Into<String>) -> Self {
    self.set_text(text);
    self
  }
  /// Sets [`Self::text`] in place.
  #[inline(always)]
  pub fn set_text(&mut self, text: impl Into<String>) -> &mut Self {
    self.text = text.into();
    self
  }

  // -- avg_logprob ------------------------------------------------------------
  /// Average sampled-token log probability.
  #[inline(always)]
  pub const fn avg_logprob(&self) -> f32 {
    self.avg_logprob
  }
  /// Builder form of [`Self::set_avg_logprob`].
  #[must_use]
  #[inline(always)]
  pub const fn with_avg_logprob(mut self, avg_logprob: f32) -> Self {
    self.set_avg_logprob(avg_logprob);
    self
  }
  /// Sets [`Self::avg_logprob`] in place.
  #[inline(always)]
  pub const fn set_avg_logprob(&mut self, avg_logprob: f32) -> &mut Self {
    self.avg_logprob = avg_logprob;
    self
  }

  // -- no_speech_prob -----------------------------------------------------------
  /// Probability this window contains no speech.
  #[inline(always)]
  pub const fn no_speech_prob(&self) -> f32 {
    self.no_speech_prob
  }
  /// Builder form of [`Self::set_no_speech_prob`].
  #[must_use]
  #[inline(always)]
  pub const fn with_no_speech_prob(mut self, no_speech_prob: f32) -> Self {
    self.set_no_speech_prob(no_speech_prob);
    self
  }
  /// Sets [`Self::no_speech_prob`] in place.
  #[inline(always)]
  pub const fn set_no_speech_prob(&mut self, no_speech_prob: f32) -> &mut Self {
    self.no_speech_prob = no_speech_prob;
    self
  }

  // -- temperature ----------------------------------------------------------
  /// Sampling temperature this window was decoded at.
  #[inline(always)]
  pub const fn temperature(&self) -> f32 {
    self.temperature
  }
  /// Builder form of [`Self::set_temperature`].
  #[must_use]
  #[inline(always)]
  pub const fn with_temperature(mut self, temperature: f32) -> Self {
    self.set_temperature(temperature);
    self
  }
  /// Sets [`Self::temperature`] in place.
  #[inline(always)]
  pub const fn set_temperature(&mut self, temperature: f32) -> &mut Self {
    self.temperature = temperature;
    self
  }

  // -- compression_ratio ------------------------------------------------------
  /// Compression ratio of [`Self::text`] (repetition signal).
  #[inline(always)]
  pub const fn compression_ratio(&self) -> f32 {
    self.compression_ratio
  }
  /// Builder form of [`Self::set_compression_ratio`].
  #[must_use]
  #[inline(always)]
  pub const fn with_compression_ratio(mut self, compression_ratio: f32) -> Self {
    self.set_compression_ratio(compression_ratio);
    self
  }
  /// Sets [`Self::compression_ratio`] in place.
  #[inline(always)]
  pub const fn set_compression_ratio(&mut self, compression_ratio: f32) -> &mut Self {
    self.compression_ratio = compression_ratio;
    self
  }

  // -- first_token_log_prob ------------------------------------------------
  /// The first sampled token's raw log probability (not a Swift field —
  /// see this struct's doc comment).
  #[inline(always)]
  pub const fn first_token_log_prob(&self) -> f32 {
    self.first_token_log_prob
  }
  /// Builder form of [`Self::set_first_token_log_prob`].
  #[must_use]
  #[inline(always)]
  pub const fn with_first_token_log_prob(mut self, first_token_log_prob: f32) -> Self {
    self.set_first_token_log_prob(first_token_log_prob);
    self
  }
  /// Sets [`Self::first_token_log_prob`] in place.
  #[inline(always)]
  pub const fn set_first_token_log_prob(&mut self, first_token_log_prob: f32) -> &mut Self {
    self.first_token_log_prob = first_token_log_prob;
    self
  }

  // -- early_stopped (bool) ------------------------------------------------
  /// Whether a progress callback truncated this window's decode with an early
  /// stop. See the field doc; the pipeline carries this to the result's
  /// [`TaskFacts::early_stopped`](crate::audio::whisper::task_facts::TaskFacts::early_stopped).
  #[inline(always)]
  pub const fn early_stopped(&self) -> bool {
    self.early_stopped
  }
  /// Builder form of [`Self::update_early_stopped`].
  #[must_use]
  #[inline(always)]
  pub const fn maybe_early_stopped(mut self, early_stopped: bool) -> Self {
    self.update_early_stopped(early_stopped);
    self
  }
  /// Assigns [`Self::early_stopped`] directly — the decode loop passes whether
  /// its early-stop latch fired.
  #[inline(always)]
  pub const fn update_early_stopped(&mut self, early_stopped: bool) -> &mut Self {
    self.early_stopped = early_stopped;
    self
  }
}

// ---------------------------------------------------------------------
// TranscriptionProgress
// ---------------------------------------------------------------------

/// Live per-step decode progress (Swift `TranscriptionProgress`,
/// `Models.swift:643-661`), delivered to a
/// [`TranscriptionProgressCallback`](crate::audio::whisper::decode::TranscriptionProgressCallback)
/// after every non-completed decode step, prefill steps included.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct TranscriptionProgress {
  /// Timings accumulated so far this run.
  #[cfg_attr(feature = "serde", serde(default))]
  timings: TranscriptionTimings,
  /// Decoded text so far.
  #[cfg_attr(feature = "serde", serde(default))]
  text: String,
  /// Sampled token ids so far (prompt included).
  #[cfg_attr(
    feature = "serde",
    serde(default, skip_serializing_if = "Vec::is_empty")
  )]
  tokens: Vec<u32>,
  /// Sampling temperature, once known.
  #[cfg_attr(
    feature = "serde",
    serde(default, skip_serializing_if = "Option::is_none")
  )]
  temperature: Option<f32>,
  /// Average sampled-token log probability so far, once known.
  #[cfg_attr(
    feature = "serde",
    serde(default, skip_serializing_if = "Option::is_none")
  )]
  avg_logprob: Option<f32>,
  /// Compression ratio of [`Self::text`] so far, once known.
  #[cfg_attr(
    feature = "serde",
    serde(default, skip_serializing_if = "Option::is_none")
  )]
  compression_ratio: Option<f32>,
  /// Which decode window this progress update belongs to.
  #[cfg_attr(feature = "serde", serde(default))]
  window_id: usize,
}

impl TranscriptionProgress {
  /// Builds a progress update from its three always-known fields (Swift
  /// `TranscriptionProgress.init`, `Models.swift:652-660`, defaults every
  /// other parameter to `nil`/`0`); the optional trio starts `None` and
  /// [`Self::window_id`] starts `0`.
  pub fn new(
    timings: TranscriptionTimings,
    text: impl Into<String>,
    tokens: impl Into<Vec<u32>>,
  ) -> Self {
    Self {
      timings,
      text: text.into(),
      tokens: tokens.into(),
      temperature: None,
      avg_logprob: None,
      compression_ratio: None,
      window_id: 0,
    }
  }

  // -- timings --------------------------------------------------------------
  /// Timings accumulated so far this run.
  #[inline(always)]
  pub const fn timings(&self) -> &TranscriptionTimings {
    &self.timings
  }
  /// Builder form of [`Self::set_timings`].
  #[must_use]
  #[inline(always)]
  pub fn with_timings(mut self, timings: TranscriptionTimings) -> Self {
    self.set_timings(timings);
    self
  }
  /// Sets [`Self::timings`] in place.
  #[inline(always)]
  pub fn set_timings(&mut self, timings: TranscriptionTimings) -> &mut Self {
    self.timings = timings;
    self
  }

  // -- text -------------------------------------------------------------
  /// Decoded text so far.
  #[inline(always)]
  pub fn text(&self) -> &str {
    self.text.as_str()
  }
  /// Builder form of [`Self::set_text`].
  #[must_use]
  #[inline(always)]
  pub fn with_text(mut self, text: impl Into<String>) -> Self {
    self.set_text(text);
    self
  }
  /// Sets [`Self::text`] in place.
  #[inline(always)]
  pub fn set_text(&mut self, text: impl Into<String>) -> &mut Self {
    self.text = text.into();
    self
  }

  // -- tokens (Vec<u32>) -------------------------------------------------
  /// Sampled token ids so far (prompt included).
  #[inline(always)]
  pub const fn tokens_slice(&self) -> &[u32] {
    self.tokens.as_slice()
  }
  /// Builder form of [`Self::set_tokens`].
  #[must_use]
  #[inline(always)]
  pub fn with_tokens(mut self, tokens: impl Into<Vec<u32>>) -> Self {
    self.set_tokens(tokens);
    self
  }
  /// Sets [`Self::tokens_slice`] in place.
  #[inline(always)]
  pub fn set_tokens(&mut self, tokens: impl Into<Vec<u32>>) -> &mut Self {
    self.tokens = tokens.into();
    self
  }

  // -- temperature (Option<f32>) -------------------------------------------
  /// Sampling temperature, once known.
  #[inline(always)]
  pub const fn temperature(&self) -> Option<f32> {
    self.temperature
  }
  /// Builder form of [`Self::set_temperature`].
  #[must_use]
  #[inline(always)]
  pub const fn with_temperature(mut self, temperature: f32) -> Self {
    self.set_temperature(temperature);
    self
  }
  /// Sets [`Self::temperature`] to `Some(temperature)`.
  #[inline(always)]
  pub const fn set_temperature(&mut self, temperature: f32) -> &mut Self {
    self.temperature = Some(temperature);
    self
  }
  /// Builder form of [`Self::update_temperature`].
  #[must_use]
  #[inline(always)]
  pub const fn maybe_temperature(mut self, temperature: Option<f32>) -> Self {
    self.update_temperature(temperature);
    self
  }
  /// Assigns [`Self::temperature`] directly.
  #[inline(always)]
  pub const fn update_temperature(&mut self, temperature: Option<f32>) -> &mut Self {
    self.temperature = temperature;
    self
  }
  /// Sets [`Self::temperature`] to `None`.
  #[inline(always)]
  pub const fn clear_temperature(&mut self) -> &mut Self {
    self.temperature = None;
    self
  }

  // -- avg_logprob (Option<f32>) -------------------------------------------
  /// Average sampled-token log probability so far, once known.
  #[inline(always)]
  pub const fn avg_logprob(&self) -> Option<f32> {
    self.avg_logprob
  }
  /// Builder form of [`Self::set_avg_logprob`].
  #[must_use]
  #[inline(always)]
  pub const fn with_avg_logprob(mut self, avg_logprob: f32) -> Self {
    self.set_avg_logprob(avg_logprob);
    self
  }
  /// Sets [`Self::avg_logprob`] to `Some(avg_logprob)`.
  #[inline(always)]
  pub const fn set_avg_logprob(&mut self, avg_logprob: f32) -> &mut Self {
    self.avg_logprob = Some(avg_logprob);
    self
  }
  /// Builder form of [`Self::update_avg_logprob`].
  #[must_use]
  #[inline(always)]
  pub const fn maybe_avg_logprob(mut self, avg_logprob: Option<f32>) -> Self {
    self.update_avg_logprob(avg_logprob);
    self
  }
  /// Assigns [`Self::avg_logprob`] directly.
  #[inline(always)]
  pub const fn update_avg_logprob(&mut self, avg_logprob: Option<f32>) -> &mut Self {
    self.avg_logprob = avg_logprob;
    self
  }
  /// Sets [`Self::avg_logprob`] to `None`.
  #[inline(always)]
  pub const fn clear_avg_logprob(&mut self) -> &mut Self {
    self.avg_logprob = None;
    self
  }

  // -- compression_ratio (Option<f32>) -------------------------------------
  /// Compression ratio of [`Self::text`] so far, once known.
  #[inline(always)]
  pub const fn compression_ratio(&self) -> Option<f32> {
    self.compression_ratio
  }
  /// Builder form of [`Self::set_compression_ratio`].
  #[must_use]
  #[inline(always)]
  pub const fn with_compression_ratio(mut self, compression_ratio: f32) -> Self {
    self.set_compression_ratio(compression_ratio);
    self
  }
  /// Sets [`Self::compression_ratio`] to `Some(compression_ratio)`.
  #[inline(always)]
  pub const fn set_compression_ratio(&mut self, compression_ratio: f32) -> &mut Self {
    self.compression_ratio = Some(compression_ratio);
    self
  }
  /// Builder form of [`Self::update_compression_ratio`].
  #[must_use]
  #[inline(always)]
  pub const fn maybe_compression_ratio(mut self, compression_ratio: Option<f32>) -> Self {
    self.update_compression_ratio(compression_ratio);
    self
  }
  /// Assigns [`Self::compression_ratio`] directly.
  #[inline(always)]
  pub const fn update_compression_ratio(&mut self, compression_ratio: Option<f32>) -> &mut Self {
    self.compression_ratio = compression_ratio;
    self
  }
  /// Sets [`Self::compression_ratio`] to `None`.
  #[inline(always)]
  pub const fn clear_compression_ratio(&mut self) -> &mut Self {
    self.compression_ratio = None;
    self
  }

  // -- window_id ------------------------------------------------------------
  /// Which decode window this progress update belongs to.
  #[inline(always)]
  pub const fn window_id(&self) -> usize {
    self.window_id
  }
  /// Builder form of [`Self::set_window_id`].
  #[must_use]
  #[inline(always)]
  pub const fn with_window_id(mut self, window_id: usize) -> Self {
    self.set_window_id(window_id);
    self
  }
  /// Sets [`Self::window_id`] in place.
  #[inline(always)]
  pub const fn set_window_id(&mut self, window_id: usize) -> &mut Self {
    self.window_id = window_id;
    self
  }
}

// ---------------------------------------------------------------------
// FallbackReason / needs_fallback
// ---------------------------------------------------------------------

/// Reason a decoding window must be retried at the next (higher)
/// temperature — the `needsFallback: true` outcomes of Swift's
/// `DecodingFallback.init?` (`Models.swift:357-381`). Swift's fourth
/// `fallbackReason`, `"silence"` (`Models.swift:370`), carries
/// `needsFallback: false` — it never requires a retry, so
/// [`needs_fallback`] folds it into `None` rather than modeling it as a
/// variant here (see that function's doc comment).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, derive_more::Display, derive_more::IsVariant)]
#[display("{}", self.as_str())]
#[non_exhaustive]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
pub enum FallbackReason {
  /// The first sampled token's log probability fell below
  /// `DecodingOptions::first_token_logprob_threshold` (Swift
  /// `Models.swift:367`).
  FirstTokenLogProbThreshold,
  /// The window's text compression ratio exceeded
  /// `DecodingOptions::compression_ratio_threshold` — too repetitive
  /// (Swift `Models.swift:373`).
  CompressionRatioThreshold,
  /// The window's average sampled-token log probability fell below
  /// `DecodingOptions::logprob_threshold` (Swift `Models.swift:376`).
  LogProbThreshold,
}

impl FallbackReason {
  /// Stable name matching Swift's `DecodingFallback.fallbackReason`
  /// string exactly (`Models.swift:367,373,376`).
  #[inline(always)]
  pub const fn as_str(&self) -> &'static str {
    match self {
      Self::FirstTokenLogProbThreshold => "firstTokenLogProbThreshold",
      Self::CompressionRatioThreshold => "compressionRatioThreshold",
      Self::LogProbThreshold => "logProbThreshold",
    }
  }
}

/// Ports Swift's `DecodingFallback.init?(options:isFirstTokenLogProbTooLow:
/// noSpeechProb:compressionRatio:avgLogProb:)`
/// (`argmax-oss-swift/Sources/WhisperKit/Core/Models.swift:357-381`).
///
/// **The caller (the decode loop) computes `first_token_log_prob_too_low`**
/// by comparing the first sampled token's log probability against
/// `options.first_token_logprob_threshold()` *inside* the decode loop
/// (`TextDecoder.swift:662-667`). This value is not recoverable from
/// `decoding.token_log_probs_slice()` because the prefill phase does not
/// append the first token's log probability to that vector — the first
/// entry stored there is actually a placeholder or undefined for prefill
/// results. Therefore, the caller must compute this flag from the
/// loop-local first sampled token's log probability and thread it through
/// to this function as a parameter (mirroring Swift's signature).
///
/// **Decision order matters** (this is the Swift source's own comment,
/// `Models.swift:365`) and every comparison is strict, exactly matching
/// the source:
///
/// 1. `first_token_log_prob_too_low` is `true` -> `Some(FirstTokenLogProbThreshold)`.
/// 2. else, if `no_speech_prob` `>` threshold -> **silence**, returns
///    `None` unconditionally. This step does *not* also consult
///    `avg_logprob` — an earlier exploration of this port assumed it
///    did; `Models.swift:368-370` shows it does not, and the source
///    wins.
/// 3. else, if `compression_ratio` `>` threshold -> `Some(CompressionRatioThreshold)`.
/// 4. else, if `avg_logprob` `<` threshold -> `Some(LogProbThreshold)`.
/// 5. else `None` — a clean result, or every configured threshold was
///    `None` (disabled).
///
/// Any threshold left `None` in `options` disables its own check and
/// falls through to the next step, matching Swift's `if let threshold =
/// options.xThreshold` optional-binding guards.
pub fn needs_fallback(
  first_token_log_prob_too_low: bool,
  decoding: &DecodingResult,
  options: &DecodingOptions,
) -> Option<FallbackReason> {
  if first_token_log_prob_too_low {
    return Some(FallbackReason::FirstTokenLogProbThreshold);
  }
  if let Some(threshold) = options.no_speech_threshold()
    && decoding.no_speech_prob() > threshold
  {
    return None;
  }
  if let Some(threshold) = options.compression_ratio_threshold()
    && decoding.compression_ratio() > threshold
  {
    return Some(FallbackReason::CompressionRatioThreshold);
  }
  if let Some(threshold) = options.logprob_threshold()
    && decoding.avg_logprob() < threshold
  {
    return Some(FallbackReason::LogProbThreshold);
  }
  None
}

// ---------------------------------------------------------------------
// format_segments
// ---------------------------------------------------------------------

/// Renders `segments` as display lines, one per segment — ports
/// `TranscriptionUtilities.formatSegments`
/// (`Utilities/TranscriptionUtilities.swift:16-27`), timestamp formatting
/// included (`Logging.formatTimestamp`, `Utilities/Logging.swift:50-52`,
/// `String(format: "%.2f", _)`, i.e. Rust's `{:.2}`).
///
/// When `with_timestamps` is `true`, each line is `"[{start:.2} -->
/// {end:.2}] {text}"`; when `false`, each line is `text` verbatim. Note
/// the bracketed timestamp carries its own trailing space (Swift's own
/// string interpolation builds `"[...] "`), so a segment `text` that
/// itself starts with a leading space renders with two spaces before the
/// first word — faithful to Swift, not a bug.
pub fn format_segments(segments: &[TranscriptionSegment], with_timestamps: bool) -> Vec<String> {
  segments
    .iter()
    .map(|segment| {
      if with_timestamps {
        format!(
          "[{:.2} --> {:.2}] {}",
          segment.start(),
          segment.end(),
          segment.text()
        )
      } else {
        segment.text().to_string()
      }
    })
    .collect()
}

// ---------------------------------------------------------------------
// merge_transcription_results
// ---------------------------------------------------------------------

/// Sums `f(result.timings())` over `results` — the "work time"/count merge
/// rule (Swift `.reduce(0, +)`).
fn sum_timing(results: &[TranscriptionResult], f: impl Fn(&TranscriptionTimings) -> f64) -> f64 {
  results.iter().map(|r| f(r.timings())).sum()
}

/// Maximum of `f(result.timings())` over `results`, `0.0` if `results` is
/// empty — the "load time" merge rule (Swift `.max() ?? 0`).
fn max_timing(results: &[TranscriptionResult], f: impl Fn(&TranscriptionTimings) -> f64) -> f64 {
  results
    .iter()
    .map(|r| f(r.timings()))
    .max_by(f64::total_cmp)
    .unwrap_or(0.0)
}

/// Minimum of `f(result.timings())` over `results`, `0.0` if `results` is
/// empty — the "earliest pipeline mark" merge rule (Swift `.min() ?? 0`).
fn min_timing(results: &[TranscriptionResult], f: impl Fn(&TranscriptionTimings) -> f64) -> f64 {
  results
    .iter()
    .map(|r| f(r.timings()))
    .min_by(f64::total_cmp)
    .unwrap_or(0.0)
}

/// Merges several per-chunk/per-window [`TranscriptionResult`]s into one.
/// Ports `TranscriptionUtilities.mergeTranscriptionResults`
/// (`TranscriptionUtilities.swift:76-160`), minus the streaming-only
/// `confirmedWords` parameter (Plan 4 territory): this port always takes
/// Swift's plain-text-join `else` branch (:82-84), and `results` is a
/// plain slice rather than Swift's `[TranscriptionResult?]` — there is no
/// per-element "missing result" case to `compactMap` away here, so every
/// entry of `results` participates (Swift's `validResults`).
///
/// - [`TranscriptionResult::text`][]: every result's text, joined with `"
///   "` (:82-84; empty for empty `results`, matching `[].joined(separator:
///   " ") == ""`). **An empty-text result is joined as a bare separator,
///   not skipped** — faithfully, because Swift's `validResults`
///   `compactMap`s away only *nil* elements (`:80`), never empty-text ones,
///   so `["a", "", "b"].joined(separator: " ")` is `"a  b"` there too. A
///   zero-segment, empty-text result is reachable on its own — any audio
///   shorter than [`DecodingOptions::window_clip_time`](crate::audio::whisper::options::DecodingOptions::window_clip_time)
///   runs no window at all and returns one — and this port keeps the
///   quirk rather than "fixing" it, exactly like the segment re-`id`
///   below. This function is therefore the merge for
///   [`DecodingOptions::drop_blank_audio`](crate::audio::whisper::options::DecodingOptions::drop_blank_audio)
///   `== false` — exact Swift — by definition;
///   [`merge_transcription_results_with_options`] is the entry point that
///   skips the empties instead, for the callers whose own options made an
///   emptied result routine. Both share one implementation.
/// - [`TranscriptionResult::segments_slice`][]: every result's segments,
///   concatenated in order, each re-`id`'d to `result_index +
///   segment_index` (:89-94) — a faithful bug-for-bug port: upstream
///   renumbers segments this way, not sequentially across the whole
///   merged list (verified against source, not "fixed"). Swift's
///   `previousSeek`/local `seekTime` bookkeeping in this same loop
///   (:90-99) is not ported: it is dead code in the source itself — every
///   value it computes is either overwritten next iteration or never read
///   again, and the returned `TranscriptionResult(...)` call at the end
///   never consumes it either. `results` are expected to already carry
///   correct per-segment/per-result seek anchoring from
///   [`crate::audio::whisper::audio::chunker::apply_result_seek_offset`] before reaching
///   this function, exactly as Swift's `updateSeekOffsetsForResults`
///   re-anchors chunk results before its own call into this merge.
/// - [`TranscriptionResult::language`][]: the first result's language, or
///   [`DEFAULT_LANGUAGE_CODE`] if `results` is empty (:104).
/// - [`TranscriptionResult::language_observations_slice`][]: every child's
///   observations, concatenated in result order — no Swift counterpart (this
///   list is coremlit issue #107's own addition). Nothing is deduplicated or
///   re-spanned: children are expected to already carry timeline-correct spans
///   from [`crate::audio::whisper::audio::chunker::apply_result_seek_offset`],
///   exactly as their segments are. Overlapping children therefore yield
///   overlapping observations — the streaming finalize, whose children are
///   successive re-transcriptions of one growing buffer, repeats a window's
///   probe once per hypothesis that re-decoded it, precisely as it repeats that
///   window's segments.
/// - [`TranscriptionResult::timings`][]: [`TranscriptionTimings::model_loading`]/
///   [`prewarm_load_time`](TranscriptionTimings::prewarm_load_time)/
///   [`encoder_load_time`](TranscriptionTimings::encoder_load_time)/
///   [`decoder_load_time`](TranscriptionTimings::decoder_load_time)/
///   [`tokenizer_load_time`](TranscriptionTimings::tokenizer_load_time)
///   take the max across results; every work-time/count field sums
///   (:106-152); [`pipeline_start`](TranscriptionTimings::pipeline_start)/
///   [`first_token_time`](TranscriptionTimings::first_token_time) take the
///   min; [`input_audio_seconds`](TranscriptionTimings::input_audio_seconds)
///   sums; [`full_pipeline`](TranscriptionTimings::full_pipeline) is
///   `user_pipeline_duration.min(system_pipeline_duration)`, where
///   `user_pipeline_duration` is the wall-clock span from the earliest
///   `pipeline_start` to the latest `pipeline_start + full_pipeline`
///   across results, and `system_pipeline_duration` is the sum of every
///   result's own `full_pipeline`. **Documented deviation — the
///   all-sentinel case is special-cased**, because this port's inputs
///   differ from Swift's: Swift's `TranscribeTask.run` stamps a real
///   `CFAbsoluteTimeGetCurrent()` into `pipelineStart`
///   (`TranscribeTask.swift:65`), so its merge always sees finite starts;
///   neither this crate's [`TranscribeTask::run`](crate::audio::whisper::transcribe::TranscribeTask::run)
///   nor [`crate::audio::whisper::decode::detect_language`] ever populates
///   `pipeline_start`/`first_token_time` (no absolute wall clock exists
///   in this sync port to stamp them with — see `crate::audio::whisper::decode`'s module
///   doc), so every real result's `pipeline_start` is still
///   [`DEFAULT_PIPELINE_TIME_SENTINEL`] (`f64::MAX`), where the verbatim
///   Swift subtraction silently degenerates to `0.0` and would zero out
///   the merged `full_pipeline` entirely (see the in-body comment for the
///   exact float arithmetic). When no result carries a real
///   `pipeline_start`, `user_pipeline_duration` is therefore treated as
///   unbounded, collapsing the formula to `system_pipeline_duration` —
///   the sum, the honest value for sync sequential composition. Results
///   that do carry real stamps still merge through Swift's formula
///   verbatim.
///   [`encoder_specialization_time`](TranscriptionTimings::encoder_specialization_time)/
///   [`decoder_specialization_time`](TranscriptionTimings::decoder_specialization_time)
///   are **not** carried into the merged timings — Swift's own
///   `TranscriptionTimings(...)` call inside `mergeTranscriptionResults`
///   omits both parameters, so they take that initializer's `0` default
///   regardless of what the source results held; ported faithfully
///   (left at [`TranscriptionTimings::new`]'s own `0.0` default) rather
///   than folded into the max-for-load-times group they would naively
///   belong to.
pub fn merge_transcription_results(results: &[TranscriptionResult]) -> TranscriptionResult {
  // `false` — Swift's own join: every text participates, an empty one as a
  // bare separator. See this function's doc for why that is not "fixed".
  merge_results(results, false)
}

/// Merges `results` exactly as [`merge_transcription_results`] does, but
/// takes the [`DecodingOptions`] they were decoded under and applies the
/// one merge rule those options govern: when
/// [`DecodingOptions::drop_blank_audio`] is set (**the default**), a result
/// whose text is empty contributes **nothing to the text join** — not the
/// bare `" "` separator Swift's join gives it.
///
/// This is the entry point for folding a
/// [`WhisperKit::transcribe_all`](crate::audio::whisper::transcribe::WhisperKit::transcribe_all)
/// batch, and the one
/// [`WhisperKit::transcribe`](crate::audio::whisper::transcribe::WhisperKit::transcribe)'s
/// VAD branch uses for its own chunk results. Hand it the same `options`
/// the results were decoded with and the merged text cannot contradict
/// them.
///
/// # Why the option has to reach the merge at all
///
/// [`DecodingOptions::drop_blank_audio`] makes an **empty result routine**:
/// a wholly-silent VAD chunk — the chunker is *contiguous*, so silence is
/// cut around, never skipped — decodes to nothing but
/// [`BLANK_AUDIO_MARKER`](crate::audio::whisper::constants::BLANK_AUDIO_MARKER), the filter
/// removes that one segment, and the chunk is left with no text at all.
/// Joined Swift's way, every such chunk lands in the transcript as a bare
/// separator: a doubled space between two speech runs, a leading or
/// trailing one at the clip's edges. [`merge_transcription_results`] cannot
/// simply filter them out, because an empty-text result is **not** unique
/// to the drop — any audio shorter than
/// [`DecodingOptions::window_clip_time`] runs no window and returns one,
/// which predates this option entirely — and Swift joins *those* as bare
/// separators too, so filtering there would silently change the
/// `drop_blank_audio == false` path, whose whole purpose is to be
/// byte-for-byte Swift. The rule therefore travels with the option that
/// created the need for it, and this is where the two meet.
///
/// # What is skipped is *empty text*, not *blank audio*
///
/// The merge cannot see **why** a result came back empty, and deliberately
/// does not ask. With `drop_blank_audio` set, an empty result from
/// **short audio** is skipped from the join exactly like an emptied blank
/// chunk. That is the intended reading of the option — *blank-dropping
/// means empty chunks do not pollute the text* — not an accidental
/// over-reach: a caller who asked not to see `[BLANK_AUDIO]` has no more
/// use for a bare separator standing in for a sub-second clip than for one
/// standing in for silence. Callers who want Swift's join for every input,
/// empties included, call [`merge_transcription_results`] — or clear the
/// option, which makes this function that one exactly.
///
/// # Every result is still merged
///
/// Only the *join* skips empties. Segment concatenation and every timing
/// reduction run over **all** of `results` either way, so the merged
/// segments' text and words, and every timing field — the summed
/// [`input_audio_seconds`](TranscriptionTimings::input_audio_seconds) and
/// [`audio_processing`](TranscriptionTimings::audio_processing), the
/// [`real_time_factor`](TranscriptionTimings::real_time_factor) derived
/// from them, all of it — are byte-identical to
/// [`merge_transcription_results`]'s on the same input, whichever way the
/// option is set. Dropping an emptied result from the merge *input* instead
/// would take its metrics out with it, quietly corrupting the sums (and the
/// RTF) to fix a spacing bug.
///
/// The option is **not** confined to [`TranscriptionResult::text`], though.
/// It also selects the segment **id mapping**: a running injective base when
/// dropping, Swift's `result_index + segment_index` when not (see the section
/// below). So the merged segments' ids — but nothing else about them — can
/// differ between the two settings even when every text is non-empty. (The
/// [`confirmed_words`](merge_transcription_results_with_words) door notes the
/// same, and depends on it.)
///
/// # The segment id mapping depends on the option, too
///
/// With the drop ON (the default), each chunk's survivors are re-`id`'d onto a
/// running base that advances by the chunk's decoded id span — injective across
/// chunks, each chunk's local gaps preserved. With it OFF, the ids are exactly
/// Swift's `result_index + segment_index`, which **duplicates** ids across a
/// multi-segment chunk (pinned parity). Two chunks with local ids `[0, 2]` and
/// `[0, 1]` therefore merge to `[0, 2, 3, 4]` dropped but `[0, 1, 1, 2]` not:
///
/// ```
/// use coremlit::audio::whisper::options::DecodingOptions;
/// use coremlit::audio::whisper::result::{
///   TranscriptionResult, TranscriptionSegment, TranscriptionTimings,
///   merge_transcription_results_with_options,
/// };
///
/// // Two chunks whose survivors sit at local ids [0, 2] and [0, 1].
/// let chunk = |ids: &[usize]| {
///   let segments: Vec<TranscriptionSegment> = ids
///     .iter()
///     .map(|&id| {
///       let mut s = TranscriptionSegment::new();
///       s.set_id(id).set_text(" w");
///       s
///     })
///     .collect();
///   TranscriptionResult::new(" w w", segments, "en", TranscriptionTimings::new())
/// };
/// let results = [chunk(&[0, 2]), chunk(&[0, 1])];
/// let ids =
///   |r: &TranscriptionResult| r.segments_slice().iter().map(|s| s.id()).collect::<Vec<_>>();
///
/// // Dropping ON (the default): a running injective base -> [0, 2, 3, 4].
/// let dropped = merge_transcription_results_with_options(&results, &DecodingOptions::new());
/// assert_eq!(ids(&dropped), [0, 2, 3, 4]);
///
/// // Dropping OFF: Swift's result_index + segment_index -> [0, 1, 1, 2] (ids collide).
/// let swift = merge_transcription_results_with_options(
///   &results,
///   &DecodingOptions::new().maybe_drop_blank_audio(false),
/// );
/// assert_eq!(ids(&swift), [0, 1, 1, 2]);
/// ```
///
/// # Panics
///
/// When [`DecodingOptions::drop_blank_audio`] is set, each chunk's segment ids
/// are re-mapped onto a running base advancing by the chunk's id span, so the
/// merged ids stay injective while preserving each chunk's local gaps. That
/// arithmetic is checked: a hand-built segment id near [`usize::MAX`] panics
/// deliberately rather than wrapping into a colliding id. Pipeline-produced
/// ids are small decode ordinals and never approach this.
pub fn merge_transcription_results_with_options(
  results: &[TranscriptionResult],
  options: &DecodingOptions,
) -> TranscriptionResult {
  merge_results(results, options.drop_blank_audio())
}

/// Each child's **effective** span knowledge for the drop-ON merge: its carried
/// [`TaskFacts::decoded_span`] **floored by its own survivors' extent** — `max
/// local id + 1`, or `0` when none survived. This is the round-9 F2 trust rule
/// lifted from a bare number to a [`SpanKnowledge`](crate::audio::whisper::task_facts::SpanKnowledge):
/// a carried bound legitimately EXCEEDS the extent when a filter dropped ordinals
/// after allocating them (the whole reason it is carried), but a carried bound
/// BELOW the extent under-counts — a hand-built or deserialized inconsistency —
/// so the extent is the trusted floor, never blindly the carried value.
///
/// - [`Exact(n)`](crate::audio::whisper::task_facts::SpanKnowledge::Exact) stays `Exact(n)` when
///   the extent does not exceed `n`; when the survivors OUT-count `n` the exact
///   claim is contradicted, so it degrades to `AtLeast(extent)` — a bound, no
///   longer a fabricated exact total.
/// - [`AtLeast(k)`](crate::audio::whisper::task_facts::SpanKnowledge::AtLeast) floors to
///   `AtLeast(max(k, extent))`; a [wholly-unknown](crate::audio::whisper::task_facts::SpanKnowledge::wholly_unknown)
///   `AtLeast(0)` therefore relies on the extent outright.
///
/// This ONE value drives BOTH the drop-ON id-base advance AND the drop-ON stored
/// fold (codex round 13, M1): the base advances by its
/// [lower bound](crate::audio::whisper::task_facts::SpanKnowledge::lower_bound), and the merged
/// result STORES the fold of these same effective spans — so every staging
/// materializes the same floors at the same points and the stored fact after any
/// partial merge carries them. Folding the RAW carried spans instead left the
/// store staging-dependent: a left-staged `merge([merge([A, B]), T])` stored a
/// bound below the floors the one-shot ids had already committed to, so a further
/// re-merge (its survivor extent too small to recover the lost floor) renumbered a
/// trailing chunk onto an id the one-shot merge left free.
///
/// **`None` when the survivors' extent overflows `usize`** (a hand-built segment
/// id at [`usize::MAX`]): the extent is then unrepresentable. At the drop-ON
/// id-base advance a `None` is the documented DELIBERATE panic — the span there
/// drives an injective id mapping that cannot proceed past `usize::MAX` without a
/// colliding wraparound (see [`merge_transcription_results_with_options`]'s own
/// `# Panics`). The drop-OFF fold never calls this at all: its ids are Swift's
/// `result_index + segment_index` and never consult the span, so it folds the RAW
/// carried spans and never touches the overflowing extent (codex round 8, F4).
fn effective_span_knowledge(result: &TranscriptionResult) -> Option<SpanKnowledge> {
  // The survivors' own extent — `max local id + 1`, `0` when none survived, and
  // `None` (propagated out of this function) when a `usize::MAX` survivor
  // overflows it (genuinely unrepresentable).
  let survivor_extent = match result
    .segments_slice()
    .iter()
    .map(TranscriptionSegment::id)
    .max()
  {
    None => 0,
    Some(max_local_id) => max_local_id.checked_add(1)?,
  };
  // Floor the carried lower bound at the survivor extent (F2, codex round 9): the
  // extent is a floor the carried bound may exceed but must never fall below. An
  // exact count the extent does not out-count stays exact; otherwise the value
  // degrades to an at-least bound AT the floor rather than a contradicted exact.
  let carried = result.task_facts().decoded_span();
  let floored = carried.lower_bound().max(survivor_extent);
  Some(if carried.is_exact() && floored == carried.lower_bound() {
    SpanKnowledge::Exact(floored)
  } else {
    SpanKnowledge::AtLeast(floored)
  })
}

/// The single merge implementation behind [`merge_transcription_results`]
/// and [`merge_transcription_results_with_options`].
///
/// `skip_empty_texts` governs the **text join and the segment id mapping**:
/// every result participates in the segment concatenation and in every timing
/// reduction regardless of it, so the two entry points can differ only in
/// [`TranscriptionResult::text`] and in the segments' **ids** (a running
/// injective base when dropping, Swift's `result_index + segment_index` when
/// not — see [`merge_transcription_results_with_options`]'s own doc). Keeping
/// that promise structural — one body, one `filter`, reached through both
/// doors — is the point of the split: the alternative (a second join written
/// out at the call site that happens to know the option) is exactly how the
/// two drifted apart before.
fn merge_results(results: &[TranscriptionResult], skip_empty_texts: bool) -> TranscriptionResult {
  let text = results
    .iter()
    .map(TranscriptionResult::text)
    .filter(|text| !(skip_empty_texts && text.is_empty()))
    .collect::<Vec<_>>()
    .join(" ");

  let mut segments = Vec::new();
  // Running id base for the drop-ON (`skip_empty_texts`) mapping. Each chunk's
  // survivors are re-`id`'d to `id_base + segment.id()` (its **decode
  // ordinal**, its position within its own chunk), and `id_base` then advances
  // by that chunk's decoded id SPAN. This is INJECTIVE across chunks (no two
  // chunks' id windows overlap) while preserving each chunk's own local gaps: a
  // blank dropped mid-chunk leaves `[.., 2]` where segment 1 was, and that hole
  // survives inside the chunk's window as the audit trail `drop_blank_audio`
  // promises. The earlier `result_index + segment.id()` COLLIDED the moment a
  // chunk had more than one segment — `[0,1] + [0,1]` renumbered to `[0,1,1,2]`,
  // and a blank-dropped `[0,2] + [0,1]` to `[0,2,1,2]` — because `result_index`
  // advances by 1 per chunk regardless of how many ids the chunk actually
  // spans. `id_base` advances by the real span instead. With dropping OFF the
  // false path stays EXACTLY Swift's `resultIndex + segmentIndex`, byte-for-byte
  // (its duplicate ids are pinned parity), and `id_base` is left untouched.
  //
  // The span is the chunk's DECODED ordinal count (`TaskFacts::decoded_span`),
  // carried on the result, NOT the surviving segments' `max local id + 1`: a
  // chunk whose segments were ALL dropped (a blank-only VAD chunk) survives with
  // zero segments yet still consumed ordinals, and inferring the span from the
  // survivors would collapse it to 0 — indistinguishable from a genuinely
  // zero-window chunk — so the NEXT chunk's survivors would renumber down onto
  // this one's window (coremlit issue #14, codex round 5). A hand-built or
  // deserialized result carries no span; the merge then falls back to the
  // survivors' own extent, its pre-existing behavior.
  let mut id_base = 0usize;
  for (result_index, result) in results.iter().enumerate() {
    for (segment_index, segment) in result.segments_slice().iter().enumerate() {
      let id = if skip_empty_texts {
        // Checked: a hand-built `usize::MAX` id is adversarial input, so a
        // deliberate documented panic beats a silent wraparound collision (see
        // this function's `# Panics`).
        id_base.checked_add(segment.id()).expect(
          "drop_blank_audio segment-id mapping overflowed usize (a segment id near usize::MAX)",
        )
      } else {
        result_index + segment_index
      };
      segments.push(segment.clone().with_id(id));
    }
    if skip_empty_texts {
      // Advance past this chunk's DECODED id window by exactly the effective
      // span's lower bound — the carried span floored at the survivors' own extent
      // ([`effective_span_knowledge`]). That floor keeps a re-merge from
      // under-counting the survivors it is renumbering, and the STORED fold below
      // now folds this SAME effective span, so a staged re-merge stores the very
      // floor it advanced by (codex round 13, M1). Here — and ONLY here, where the
      // span drives an injective id mapping — a `None` span (a `usize::MAX`
      // survivor whose extent overflowed) OR an overflowing base is the documented
      // adversarial-input panic; the drop-OFF fold, which never uses the span for
      // ids, keeps the checked `None` instead (codex round 8, F4).
      id_base = effective_span_knowledge(result)
        .and_then(|span| id_base.checked_add(span.lower_bound()))
        .expect("drop_blank_audio segment-id base overflowed usize (a segment id near usize::MAX)");
    }
  }

  let language = results.first().map_or_else(
    || DEFAULT_LANGUAGE_CODE.to_string(),
    |first| first.language().to_string(),
  );

  // Every child's kept probes, concatenated in result order (coremlit issue
  // #107). The merged DISPLAY language above stays the first child's, unchanged
  // — this list is the evidence, not a re-derivation of it, and a merge that
  // silently dropped it would put the fold's information loss back at the VAD
  // and streaming doors the merge exists to serve.
  let language_observations: Vec<LanguageObservation> = results
    .iter()
    .flat_map(|result| result.language_observations_slice().iter().cloned())
    .collect();

  let earliest_pipeline_start = min_timing(results, TranscriptionTimings::pipeline_start);
  let latest_pipeline_end = results
    .iter()
    .map(|r| r.timings().pipeline_start() + r.timings().full_pipeline())
    .max_by(f64::total_cmp)
    .unwrap_or(0.0);
  // With NO real pipeline_start stamped anywhere (every value still the
  // f64::MAX sentinel — what every result this sync port produces looks
  // like), the wall-clock span is unknowable and the subtraction below
  // would silently compute 0.0, NOT the intended sum: `f64::MAX +
  // full_pipeline` ABSORBS back to exactly f64::MAX (the ULP at that
  // magnitude is ~2e292, far above any real duration; it does not
  // overflow to infinity), so `latest - earliest` is `f64::MAX - f64::MAX
  // == 0.0` and the min() would zero out full_pipeline. An unknowable
  // user duration is unbounded, not zero — INFINITY hands min() to the
  // summed work time. A mixed batch (some real stamps) needs no guard:
  // `earliest` is then finite and `latest` is ~f64::MAX, so the huge span
  // already loses the min() to the sum on its own.
  let user_pipeline_duration = if earliest_pipeline_start == DEFAULT_PIPELINE_TIME_SENTINEL {
    f64::INFINITY
  } else {
    latest_pipeline_end - earliest_pipeline_start
  };
  let system_pipeline_duration = sum_timing(results, TranscriptionTimings::full_pipeline);

  let mut timings = TranscriptionTimings::new();
  timings
    .set_model_loading(max_timing(results, TranscriptionTimings::model_loading))
    .set_prewarm_load_time(max_timing(results, TranscriptionTimings::prewarm_load_time))
    .set_encoder_load_time(max_timing(results, TranscriptionTimings::encoder_load_time))
    .set_decoder_load_time(max_timing(results, TranscriptionTimings::decoder_load_time))
    .set_tokenizer_load_time(max_timing(
      results,
      TranscriptionTimings::tokenizer_load_time,
    ))
    .set_audio_loading(sum_timing(results, TranscriptionTimings::audio_loading))
    .set_audio_processing(sum_timing(results, TranscriptionTimings::audio_processing))
    .set_logmels(sum_timing(results, TranscriptionTimings::logmels))
    .set_encoding(sum_timing(results, TranscriptionTimings::encoding))
    .set_decoding_init(sum_timing(results, TranscriptionTimings::decoding_init))
    .set_decoding_loop(sum_timing(results, TranscriptionTimings::decoding_loop))
    .set_decoding_predictions(sum_timing(
      results,
      TranscriptionTimings::decoding_predictions,
    ))
    .set_decoding_filtering(sum_timing(
      results,
      TranscriptionTimings::decoding_filtering,
    ))
    .set_decoding_sampling(sum_timing(results, TranscriptionTimings::decoding_sampling))
    .set_decoding_fallback(sum_timing(results, TranscriptionTimings::decoding_fallback))
    .set_decoding_windowing(sum_timing(
      results,
      TranscriptionTimings::decoding_windowing,
    ))
    .set_decoding_kv_caching(sum_timing(
      results,
      TranscriptionTimings::decoding_kv_caching,
    ))
    .set_decoding_word_timestamps(sum_timing(
      results,
      TranscriptionTimings::decoding_word_timestamps,
    ))
    .set_decoding_non_prediction(sum_timing(
      results,
      TranscriptionTimings::decoding_non_prediction,
    ))
    .set_total_audio_processing_runs(sum_timing(
      results,
      TranscriptionTimings::total_audio_processing_runs,
    ))
    .set_total_logmel_runs(sum_timing(results, TranscriptionTimings::total_logmel_runs))
    .set_total_encoding_runs(sum_timing(
      results,
      TranscriptionTimings::total_encoding_runs,
    ))
    .set_total_decoding_loops(sum_timing(
      results,
      TranscriptionTimings::total_decoding_loops,
    ))
    .set_total_kv_update_runs(sum_timing(
      results,
      TranscriptionTimings::total_kv_update_runs,
    ))
    .set_total_timestamp_alignment_runs(sum_timing(
      results,
      TranscriptionTimings::total_timestamp_alignment_runs,
    ))
    .set_total_decoding_fallbacks(sum_timing(
      results,
      TranscriptionTimings::total_decoding_fallbacks,
    ))
    .set_total_decoding_windows(sum_timing(
      results,
      TranscriptionTimings::total_decoding_windows,
    ))
    .set_input_audio_seconds(sum_timing(
      results,
      TranscriptionTimings::input_audio_seconds,
    ))
    .set_full_pipeline(user_pipeline_duration.min(system_pipeline_duration))
    .set_pipeline_start(earliest_pipeline_start)
    .set_first_token_time(min_timing(results, TranscriptionTimings::first_token_time));

  // The task facts, folded left-to-right through the ONE merge law
  // ([`TaskFacts::merge`]) rather than the four scattered `.any()`/`.find_map()`/
  // `.first()` reductions this replaced — the consolidation that closes R6-F1/F2/F3
  // (coremlit issue #14, codex round 6). Over the children in order it:
  //
  // - **OR**s the RNG-draw and early-stop facts: a chunk the blank-audio drop
  //   emptied contributes NO segments to the merge, so its accepted temperature
  //   is invisible in the merged segment list — the fact has to travel with the
  //   result, never be read back off the output it no longer appears in. A
  //   callback truncated the merge if it truncated ANY chunk (R6-F1's merge side).
  // - keeps the **first** genuine language observation (a later `Some` cannot
  //   overwrite an earlier one), deliberately NOT agreeing with the merged
  //   DISPLAY `language` (the first result's, keeping its Swift-compat `"en"`
  //   fallback).
  // - **concatenates** the worker schedules in order, so `[0, 2]` stays distinct
  //   from `[0, 1]` instead of collapsing to the first child's coordinate (R6-F2).
  // - folds each child's **span** under [`SpanKnowledge::merge`]. WHICH span it
  //   folds depends on the id mapping this merge performs (codex round 13, M1):
  //   * **drop-ON** folds each child's EFFECTIVE span — its carried span floored
  //     at its own survivor extent ([`effective_span_knowledge`]), the SAME value
  //     the id-base advanced by — so the stored fact carries the floors the ids
  //     already committed to, and a staged re-merge numbers identically to a
  //     one-shot one (R6-F3). Folding the RAW carried spans here instead stored a
  //     bound BELOW those floors, and a further re-merge whose survivor extent
  //     could not recover them renumbered a trailing chunk onto a consumed id.
  //   * **drop-OFF** folds each child's RAW carried span: its ids are Swift's
  //     `result_index + segment_index` and never consult the span, so the fold
  //     must not touch the (possibly `usize::MAX`-overflowing) survivor extent
  //     (codex round 8, F4).
  //   Either way the sum is checked-add over exacts (an overflow degrades to a
  //   saturated `AtLeast(usize::MAX)`, never a wrapped or fabricated exact) and
  //   saturating once any child is a mere `AtLeast`, associative by construction.
  //   Unlike the pre-round-12 absorbing-`None` fold, an unknown child no longer
  //   erases a known sibling's ordinals: `AtLeast(0) + Exact(1) = AtLeast(1)`, so
  //   the known lower bound survives to drive a staged re-merge's id base.
  //
  // Folded through [`TaskFactsAccumulator`], NOT seeded at `TaskFacts::unknown()`:
  // under the Kleene OR (codex round 8, F2) `unknown()` is no longer the merge
  // identity — seeding there would null a first child's observed-clean
  // `Some(false)` to `None` — so the accumulator takes the first contributor
  // verbatim and folds the rest, and yields `unknown()` only for an empty
  // `results`.
  let mut task_facts = TaskFactsAccumulator::new();
  for result in results {
    if skip_empty_texts {
      // Drop-ON: fold the EFFECTIVE span — the floored value this child's id-base
      // advance used above — so the stored fact materializes the same floors the
      // ids did and a staged re-merge numbers identically (codex round 13, M1).
      // The `usize::MAX`-survivor overflow cannot reach here: the id loop above
      // already panicked on it, so the saturated fallback only keeps this a total
      // function and never actually materializes on the drop-ON path.
      let effective =
        effective_span_knowledge(result).unwrap_or(SpanKnowledge::AtLeast(usize::MAX));
      task_facts.merge(&result.task_facts().clone().with_decoded_span(effective));
    } else {
      // Drop-OFF: fold the RAW carried span; the Swift `result_index +
      // segment_index` ids never consult it, so the fold must not touch the
      // (possibly overflowing) survivor extent (codex round 8, F4).
      task_facts.merge(result.task_facts());
    }
  }
  let task_facts = task_facts.into_facts();

  TranscriptionResult::new(text, segments, language, timings)
    .with_language_observations(language_observations)
    .with_task_facts(task_facts)
}

// ---------------------------------------------------------------------
// merge_transcription_results_with_words
// ---------------------------------------------------------------------

/// Merges `results` under `options`, then overrides the merged text with
/// `confirmed_words` — ports the `confirmedWords:` branch of
/// `TranscriptionUtilities.mergeTranscriptionResults`
/// (`Utilities/TranscriptionUtilities.swift:76-82`): `words.map { $0.word
/// }.joined()`, i.e. every confirmed word's text concatenated with **no**
/// separator (word strings carry their own leading spaces, e.g. `" And"`).
/// Everything else — segments, language, every timing field — is
/// byte-identical to [`merge_transcription_results_with_options`]'s own
/// output.
///
/// # Why this needs the options after all
///
/// The `confirmed_words` override discards the merged **text join**, so it is
/// tempting to think [`DecodingOptions::drop_blank_audio`] is unobservable
/// here and to delegate to the plain, options-blind merge. It is not:
/// dropping now governs the **segment id mapping** too, not just the text
/// (see [`merge_transcription_results_with_options`]). Delegating to the plain
/// (drop-OFF) merge collapsed a survivor id gap `[0, 2]` back to a dense
/// `[0, 1]`, and `LocalAgreement::finalize` — the
/// default streaming path — inherited that loss at finalization. Threading the
/// same `options` the results were decoded under keeps the merged **segments**
/// honoring the drop even when the merged text does not come from them.
pub fn merge_transcription_results_with_words(
  results: &[TranscriptionResult],
  confirmed_words: &[WordTiming],
  options: &DecodingOptions,
) -> TranscriptionResult {
  let mut merged = merge_results(results, options.drop_blank_audio());
  let text: String = confirmed_words.iter().map(WordTiming::word).collect();
  merged.set_text(text);
  merged
}

#[cfg(test)]
mod tests;