faculties 0.21.0

An office suite for AI agents: kanban, wiki, files, messaging, and a Lissajous-backed viewer — all persisted in a TribleSpace pile.
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
//! Explicit host speech runtime. No discovery or finite session operation
//! invokes this module's device/model entrypoints. Callers own cancellation;
//! it is observed between physical frames, never by closing Soma's microphone.
#![cfg_attr(not(feature = "duplex"), allow(dead_code))]

#[cfg(any(feature = "duplex", test))]
use super::operations::*;
use crate::{clock, out::Out};
use anyhow::{bail, Context, Result};
use std::collections::VecDeque;
#[cfg(any(feature = "duplex", test))]
use std::io::Write as _;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::mpsc::{self, SyncSender};
use std::sync::{Arc, Condvar, Mutex};
use std::time::{Duration, Instant};

/// Samples in one model frame at 24 kHz (80 ms).
///
/// Taken FROM Soma's wire format rather than agreed with it: the model's frame
/// and the body's frame are the same frame, and two constants that merely
/// happen to match are a drift waiting to happen.
pub const FRAME_SAMPLES: usize = soma_client::FRAME_SAMPLES;
/// The model's canonical sample rate — likewise the body's.
pub const SAMPLE_RATE: u32 = soma_client::SAMPLE_RATE;
const _: () = assert!(FRAME_SAMPLES * 1_000 == 80 * SAMPLE_RATE as usize);

/// Where the body is, unless told otherwise. The same default the rest of the
/// suite uses (`voice`, `body`, `hear`).
pub const DEFAULT_SOMA: &str = soma_client::DEFAULT_BASE;
/// How far the capture ring may run ahead before the loop discards the
/// backlog. The model's step count IS its clock, so a loop that falls behind
/// the world cannot catch up by stepping faster — it can only skip forward.
const MAX_BACKLOG_FRAMES: usize = 8;

/// Text-stream ids carrying no surface text: 0 EPAD, 1 BOS, 2 EOS, 3 PAD.
const N_TEXT_SPECIALS: i64 = 4;
const TEXT_PAD: i64 = 3;
/// `<epad>` — END of a pad run, i.e. the frame that says a word starts next.
/// The model's own text stream marks a word onset this way, so a schedule that
/// pads with `<pad>` right up to the word gives it no warning that the silence
/// is ending. Measured: with a two-frame gap, padding all the way puts the
/// word onset at rank 113 in the model's own logits; ending the gap with
/// `<epad>` is what a natural stream looks like there.
const TEXT_EPAD: i64 = 0;

/// Frames of unbroken padding that close an utterance.
pub const DEFAULT_UTTERANCE_GAP: usize = 10;
/// Frames above the speech floor before the far end counts as talking.
const VOICE_ONSET_FRAMES: usize = 3;
/// Frames below it before their turn counts as over.
const VOICE_RELEASE_FRAMES: usize = 12;
/// RMS below this is room noise, not speech.
const VOICE_FLOOR: f32 = 0.012;

pub const DEFAULT_SYSTEM: &str = "You are a warm, direct conversational partner. \
Keep replies short and spoken — no lists, no markdown. Answer in the language \
you were addressed in.";

#[derive(Clone, Debug)]
pub struct RunOptions {
    /// Model weight pile.
    pub weights: PathBuf,
    /// Packaged voice prompt the session speaks with.
    pub voice_prompt: PathBuf,
    /// Text tokenizer file, overriding the copy inside `--weights`.
    ///
    /// Needed because the two halves of the weight pile drifted apart: the
    /// codec loader wants a `mary-model-bundles` collection and the pile-side
    /// SPM loader still wants the `mary-model-graph` the bundle migration
    /// replaced, so no pile on this machine satisfies both. Whatever is loaded
    /// is checked against the model's own `TEXT_CARD` below, so a wrong
    /// tokenizer is a loud failure rather than gibberish. `mary`'s own
    /// PersonaPlex bins take the same flag.
    pub spm: Option<PathBuf>,
    /// Base URL of the Soma that owns the microphone. This process opens no
    /// capture device: it subscribes, so `hear` can be reading the same frames
    /// at the same time. Not needed with `--no-input`.
    pub soma: String,
    /// EXACT name of the playback device.
    pub output: String,
    /// Weight format for the temporal stack. `q8` is the default because on
    /// an Apple GPU it is not slower than `q4` — at these shapes the matvecs
    /// are dispatch-bound rather than bandwidth-bound, and the hardware has
    /// no FP4 units, so 4-bit is pure dequantization overhead there — while
    /// costing far less fidelity. `q4` is expected to pay on parts that DO
    /// have FP4 hardware.
    pub fmt: WeightFormat,
    /// Whether the model may hold up its own end of the conversation.
    /// `listen` forces its text stream to padding, so it hears everything,
    /// may backchannel, and says only what is injected. `converse` lets it
    /// generate its own words.
    pub floor: Floor,
    /// Spoken system prompt.
    pub system: String,
    /// Sampling temperature; 0 selects greedy decoding.
    pub temp: f32,
    /// Sampling seed.
    pub seed: u64,
    /// Gap frames inserted at each word boundary in a forced line (the last
    /// of them `<epad>`). Where the gaps GO is set by `--cadence`; this sets
    /// how long they are, and therefore the SPEAKING RATE.
    ///
    /// Only applies to the SCHEDULED cadences. Under the default `model`
    /// cadence there is no schedule to space out — the gaps are the model's.
    ///
    /// 3 is the default because it is the only value that satisfies all three
    /// things we can measure. It puts the schedule at 69% `<pad>`, matching
    /// the ~65% density the model's own text stream runs at; it keeps the
    /// forced tokens inside the model's own distribution (word onset p50 rank
    /// 10, continuation p50 rank 1); and it produces 2.83-3.00 words per
    /// second, which is ordinary English speech. Shorter gaps score just as
    /// well on rank — onset p50 4 at gap 1 — but talk at 3.6-4.0 words per
    /// second, which is a rushed delivery that no rank statistic can see.
    pub pace: usize,
    /// Write one line per frame — index, the stream-0 token, and what kind of
    /// token it was — so the audio can be read against what the text stream
    /// was doing at that instant. The way to find out whether the model puts
    /// anything in the gaps it is given: breath, a filled pause, laughter.
    pub trace: Option<PathBuf>,
    /// Under `--cadence model`, the fewest frames allowed between one word
    /// ONSET and the next. `0` (the default) imposes nothing and leaves the
    /// rhythm entirely the model's.
    ///
    /// A floor, never a schedule: it can only DELAY a word the model wanted
    /// to start, never bring one forward, so the long pauses it chooses stay
    /// exactly as long. Within-word pieces are untouched — those run
    /// consecutively in real speech and stretching them is the original bug.
    /// It was built to trade back some of the speed of model timing (~4.0
    /// words/s against ordinary English's 2.5-3.0) without flattening the
    /// variation. **Measured, it does not work, and the reason is worth
    /// keeping.** A floor of 3 was predicted to lift the fastest third of
    /// onset gaps and land a mean of 3.92 frames. It produced a mean of
    /// **8.38**, a shortest gap of 4 rather than 3, and the same two
    /// sentences took 33.2 s against model timing's 18.3 s — slower than the
    /// fixed schedule it was meant to improve on.
    ///
    /// Holding PAD on a frame where the model asked for a word does not delay
    /// that word by a frame. The PAD enters the model's own history and
    /// conditions what follows, so it drops into a pause and then EXTENDS it
    /// on its own: the intervention compounds instead of applying once. That
    /// is the same property that makes forcing work at all — a token we
    /// substitute is one it then owns — pointing the other way.
    ///
    /// Left in, defaulted off, as the reproduction for that finding. The
    /// summary counts every frame it holds back. Note also that lengthening
    /// pauses this way trips `--utterance-gap`, which chops one line into
    /// several transcript entries (the audio stays whole).
    pub min_word_gap: usize,
    /// Under `--cadence model`, how many frames the model may hold silence
    /// mid-line before we start the next word for it. A backstop, not a
    /// rhythm: every time it fires we have taken the timing back, and the run
    /// summary reports how often that happened.
    pub nudge_after: usize,
    /// Who decides WHEN each word lands. `model` (the default) decides
    /// nothing: the model samples stream 0 itself and we substitute our words
    /// onto the frames it chose to speak, so the pauses and their lengths are
    /// its own. The rest impose a schedule and are kept as controls for the
    /// rhythm and rank numbers this command reports — `word-onset` puts a
    /// fixed gap between words, `uniform` puts one after every word piece
    /// (which splits multi-piece words across silence), `dense` uses none.
    pub cadence: Cadence,
    /// Feed the model digital silence on the input channel while it speaks,
    /// so an endpoint without echo cancellation does not hear itself.
    pub gate: bool,
    /// Padding-only frames that close an utterance.
    pub utterance_gap: usize,
    /// Pile to record the durable transcript on. Without it nothing is
    /// recorded beyond the session directory.
    pub pile: Option<PathBuf>,
    /// Signing key for the transcript pile.
    pub key: Option<PathBuf>,
    /// Stop after this many frames instead of running until interrupted.
    pub frames: Option<usize>,
    /// Also tee everything spoken to this WAV file.
    pub wav: Option<PathBuf>,
    /// Frames of context the codec decoder re-decodes on every hop. Deep by
    /// default — the decoder's transformer has a 250-frame window and no
    /// streaming state, so a shallow context makes the hop boundary audible.
    pub decode_context: usize,
    /// New frames emitted per decode call.
    pub decode_hop: usize,
    /// Do not subscribe to the capture stream and omit the model's user-audio
    /// embeddings. This is the GENERATION-ONLY channel — the model speaks and
    /// is not listened to — and it is also what to use on a handsfree endpoint
    /// whose microphone is already held open by something else. With no
    /// microphone the SPEAKER becomes the frame clock (see `--lead`).
    pub no_input: bool,
    /// Generation-only clock: frames of audio the model may run ahead of the
    /// speaker before it waits. The floor is one decode hop, since the device
    /// reports its queue a hop at a time; two hops is what keeps a hop of
    /// audio in front of the device at all times.
    pub lead: Option<usize>,
    /// Half-duplex pause file, held for exactly as long as this channel is
    /// AUDIBLE IN THE ROOM.
    ///
    /// Needed only because the microphone is now SHARED: another consumer of
    /// the same Soma frames (`hear listen --pause-file <the same path>`) would
    /// otherwise transcribe our own voice back to us. Inside this binary
    /// turn-taking needs no file at all — `--gate` feeds the model digital
    /// silence while it speaks, in process, on the frame clock.
    ///
    /// The window is held past the last generated frame by whatever audio is
    /// still in flight to the speaker, because the mouth is audible LATER than
    /// the model is generating. It is a SOFTWARE hold: nothing here or in
    /// `hear` closes a device.
    pub pause_file: Option<PathBuf>,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum WeightFormat {
    Q4,
    Q8,
    F16,
}
impl WeightFormat {
    pub fn parse(value: &str) -> Result<Self> {
        Ok(match value {
            "q4" => Self::Q4,
            "q8" => Self::Q8,
            "f16" => Self::F16,
            _ => bail!("unknown weight format {value} (expected q4, q8 or f16)"),
        })
    }
}
impl Floor {
    pub fn parse(value: &str) -> Result<Self> {
        Ok(match value {
            "listen" => Self::Listen,
            "converse" => Self::Converse,
            _ => bail!("unknown floor policy {value} (expected listen or converse)"),
        })
    }
}
impl Cadence {
    pub fn parse(value: &str) -> Result<Self> {
        Ok(match value {
            "model" => Self::Model,
            "word-onset" => Self::WordOnset,
            "uniform" => Self::Uniform,
            "dense" => Self::Dense,
            _ => bail!("unknown cadence {value} (expected model, word-onset, uniform or dense)"),
        })
    }
    pub fn name(self) -> &'static str {
        match self {
            Self::Model => "model",
            Self::WordOnset => "word-onset",
            Self::Uniform => "uniform",
            Self::Dense => "dense",
        }
    }
}
impl RunOptions {
    pub fn new(weights: PathBuf, voice_prompt: PathBuf, output: String) -> Self {
        Self {
            weights,
            voice_prompt,
            output,
            spm: None,
            soma: DEFAULT_SOMA.into(),
            fmt: WeightFormat::Q8,
            floor: Floor::Listen,
            system: DEFAULT_SYSTEM.into(),
            temp: 0.8,
            seed: 12_345_678,
            pace: 3,
            trace: None,
            min_word_gap: 0,
            nudge_after: 25,
            cadence: Cadence::Model,
            gate: false,
            utterance_gap: DEFAULT_UTTERANCE_GAP,
            pile: None,
            key: None,
            frames: None,
            wav: None,
            decode_context: DEFAULT_DECODE_CONTEXT,
            decode_hop: DEFAULT_DECODE_HOP,
            no_input: false,
            lead: None,
            pause_file: None,
        }
    }
    /// Pure admission checks before resetting session files or opening devices.
    pub fn validate(&self) -> Result<()> {
        if self.output.trim().is_empty() {
            bail!("an exact named output device is required");
        }
        if !self.temp.is_finite() {
            bail!("sampling temperature must be finite");
        }
        if self.decode_hop == 0 {
            bail!("decode_hop must be positive");
        }
        if self.utterance_gap == 0 {
            bail!("utterance_gap must be positive");
        }
        self.decode_context
            .checked_add(self.decode_hop)
            .context("decode window overflow")?;
        if self.lead.is_none() {
            self.decode_hop
                .checked_mul(2)
                .context("default speaker lead overflow")?;
        }
        Ok(())
    }
}
#[derive(Clone, Debug)]
pub struct EarReport {
    pub frames: usize,
    pub wall_seconds: f64,
    pub first_frame: Option<u64>,
    pub last_frame: u64,
    pub skipped: usize,
    pub mean_rms: Option<f64>,
    pub peak_rms: Option<f32>,
    pub ended: Option<String>,
}
#[derive(Clone, Debug)]
pub struct RunReport {
    pub frames: usize,
    pub wall_seconds: f64,
    pub step_times_ms: Vec<f64>,
    pub over_budget: usize,
    pub playback_underruns: u64,
    pub speaker_stalls: usize,
    pub output_dropped: u64,
    pub input_skipped: usize,
    pub forced_onset_ranks: Vec<usize>,
    pub forced_continuation_ranks: Vec<usize>,
    pub forced_padding_ranks: Vec<usize>,
    pub word_gaps: Vec<usize>,
    pub model_chose: usize,
    pub nudged: usize,
    pub held_back: usize,
}
#[derive(Clone, Debug)]
pub struct PlaybackDevice {
    pub name: String,
    pub configuration: std::result::Result<PlaybackConfiguration, String>,
}
#[derive(Clone, Debug)]
pub struct PlaybackConfiguration {
    pub channels: u16,
    pub sample_rate: u32,
    pub sample_format: String,
}
#[cfg(feature = "audio")]
pub fn devices() -> Result<Vec<PlaybackDevice>> {
    use rodio::cpal::traits::{DeviceTrait, HostTrait};
    let mut devices = Vec::new();
    for device in rodio::cpal::default_host()
        .output_devices()
        .context("enumerate playback devices")?
    {
        devices.push(PlaybackDevice {
            name: device.name().unwrap_or_else(|_| "<unnamed>".into()),
            configuration: device
                .default_output_config()
                .map(|config| PlaybackConfiguration {
                    channels: config.channels(),
                    sample_rate: config.sample_rate(),
                    sample_format: format!("{:?}", config.sample_format()),
                })
                .map_err(|error| error.to_string()),
        });
    }
    Ok(devices)
}
#[cfg(not(feature = "audio"))]
pub fn devices() -> Result<Vec<PlaybackDevice>> {
    bail!("audio device support is not compiled into this build (enable the `audio` feature)")
}

// ── the ear: Soma's frames, and this loop owns no device ───────────────────

/// The far end's voice, as Soma delivers it.
///
/// **THIS BINARY OPENS NO DEVICE.** A capture device can be held by exactly
/// one process, so exactly one process picks it BY NAME and holds it: Soma.
/// Everything else subscribes. That is not tidiness — while this loop opened
/// the microphone itself, `hear` and `duplex` could not run at the same time,
/// and a live transcript and a spoken channel were mutually exclusive by
/// physics. Now they are two consumers of one body.
///
/// DEVICES ARE ADDRESSED BY NAME, NEVER BY INDEX AND NEVER VIA THE SYSTEM
/// DEFAULT — a Bluetooth connect silently renumbers CoreAudio and an
/// index-addressed stream lands on a dead virtual channel at -91 dB with
/// nothing in the logs. This binary keeps that rule BY SUBTRACTION: it names
/// no device at all and inherits Soma's one named choice.
///
/// NEVER CLOSE THE MICROPHONE STREAM. Dropping this ear detaches a consumer;
/// it does not close anything. On a handsfree (HFP) endpoint the duplex
/// channel exists only while something holds the microphone open, and Soma is
/// what holds it — for the life of the body, not the life of this process.
///
/// THE READ IS THE CLOCK. The reader thread blocks in `SomaCapture::next_frame`
/// until the physical device has produced the next exact 80 ms frame, and the
/// generation loop blocks on the condvar until the reader hands one over. There
/// is no sleep, no timer and no polling interval on the path — the period is
/// the hardware's, one layer removed. (The device-owning version of this ear
/// polled its ring every 4 ms; blocking on the body is strictly better.)
struct Ear {
    ring: Arc<(Mutex<EarRing>, Condvar)>,
    stop: Arc<AtomicBool>,
    thread: Option<std::thread::JoinHandle<()>>,
    soma: String,
}

#[derive(Default)]
struct EarRing {
    frames: VecDeque<[f32; FRAME_SAMPLES]>,
    /// Frames discarded because this loop could not keep the body's pace. The
    /// model's step count IS its clock, so a loop that falls behind the world
    /// cannot catch up by stepping faster — it can only skip forward.
    skipped: usize,
    /// Where this ear joined the body's clock, and where it has got to. Soma
    /// fans one microphone out, so these are the BODY's coordinates, shared
    /// with every other consumer of the same frames — which is what lets two
    /// of them say they heard the same instant.
    first_frame: Option<u64>,
    last_frame: u64,
    /// Why the stream ended, if it has. Never a silent stop: missing speech
    /// must not read as silence.
    ended: Option<String>,
}

impl Ear {
    /// Subscribe to the body's microphone. Opening is done here, on the
    /// caller's thread, so a body that is not running says so immediately
    /// rather than one frame into the session.
    fn open(soma: &str) -> Result<Self> {
        let mut capture = soma_client::SomaCapture::open(soma)
            .with_context(|| format!("subscribe to Soma's microphone at {soma}"))?;
        let ring: Arc<(Mutex<EarRing>, Condvar)> =
            Arc::new((Mutex::new(EarRing::default()), Condvar::new()));
        let stop = Arc::new(AtomicBool::new(false));
        let thread = {
            let ring = Arc::clone(&ring);
            let stop = Arc::clone(&stop);
            std::thread::Builder::new()
                .name("duplex-ear".into())
                .spawn(move || {
                    let (lock, ready) = &*ring;
                    while !stop.load(Ordering::Relaxed) {
                        // Blocks on the body, which blocks on the device.
                        match capture.next_frame() {
                            Ok(frame) => {
                                let mut ring = lock.lock().expect("ear ring");
                                ring.first_frame.get_or_insert(frame.frame_index);
                                ring.last_frame = frame.frame_index;
                                ring.frames.push_back(frame.samples);
                                drop(ring);
                                ready.notify_all();
                            }
                            Err(error) => {
                                let mut ring = lock.lock().expect("ear ring");
                                ring.ended = Some(format!("{error:#}"));
                                drop(ring);
                                ready.notify_all();
                                return;
                            }
                        }
                    }
                    let mut ring = lock.lock().expect("ear ring");
                    ring.ended.get_or_insert_with(|| "ear closed".into());
                    drop(ring);
                    ready.notify_all();
                })
                .context("spawn ear thread")?
        };
        Ok(Self {
            ring,
            stop,
            thread: Some(thread),
            soma: soma.to_string(),
        })
    }

    /// Pull exactly one frame, blocking on the body's clock.
    fn next_frame(&self) -> Option<[f32; FRAME_SAMPLES]> {
        let (lock, ready) = &*self.ring;
        let mut ring = lock.lock().expect("ear ring");
        loop {
            // Stay current: a backlog means this loop is slower than the
            // world, and the model cannot step faster to catch up.
            let backlog = ring.frames.len();
            if backlog > MAX_BACKLOG_FRAMES {
                let drop_frames = backlog - 2;
                ring.frames.drain(..drop_frames);
                ring.skipped += drop_frames;
            }
            if let Some(frame) = ring.frames.pop_front() {
                return Some(frame);
            }
            // Buffered frames are handed over before the ending, so a stream
            // that died never swallows audio it already delivered.
            if ring.ended.is_some() {
                return None;
            }
            ring = ready.wait(ring).expect("ear ring");
        }
    }

    fn skipped(&self) -> usize {
        self.ring.0.lock().expect("ear ring").skipped
    }

    /// This ear's place on the body's clock: where it joined and where it is.
    /// The join point is `None` until the first frame actually arrives —
    /// reporting a zero there would claim the body's clock had just started,
    /// which is exactly the thing a shared microphone makes untrue.
    fn clock(&self) -> (Option<u64>, u64) {
        let ring = self.ring.0.lock().expect("ear ring");
        (ring.first_frame, ring.last_frame)
    }

    fn ended(&self) -> Option<String> {
        self.ring.0.lock().expect("ear ring").ended.clone()
    }
}

impl Drop for Ear {
    /// Detaches this consumer. It does NOT close the microphone — that is
    /// Soma's, held for the life of the body.
    fn drop(&mut self) {
        self.stop.store(true, Ordering::Relaxed);
        if let Some(thread) = self.thread.take() {
            let _ = thread.join();
        }
    }
}

fn rms(samples: &[f32]) -> f32 {
    if samples.is_empty() {
        return 0.0;
    }
    (samples.iter().map(|s| s * s).sum::<f32>() / samples.len() as f32).sqrt()
}

/// The capture seam's gate, without the model: read the body's frames through
/// the SAME ear `run` uses and report what arrives.
///
/// This is what makes the simultaneity claim checkable without paying for
/// weights — run it beside `hear listen` against one Soma and both will report
/// frames off the same clock. It is also how to tell "the body is not
/// producing audio" apart from "the model is not answering".
pub fn ear(soma: &str, frames: usize, stop: &AtomicBool, out: &mut Out<'_>) -> Result<EarReport> {
    let ear = Ear::open(soma)?;
    out.line(format!(
        "duplex: ear open on {} — {} Hz, {} sample frames, this process owns no device",
        ear.soma,
        soma_client::SAMPLE_RATE,
        soma_client::FRAME_SAMPLES
    ))?;
    let start = Instant::now();
    let mut read = 0usize;
    let mut peak = 0f32;
    let mut total = 0f64;
    while (frames == 0 || read < frames) && !stop.load(Ordering::Relaxed) {
        let Some(frame) = ear.next_frame() else {
            break;
        };
        let level = rms(&frame);
        peak = peak.max(level);
        total += level as f64;
        read += 1;
        if read % 12 == 0 {
            let (first, last) = ear.clock();
            out.line(format!(
                "  [ear] body frame {last} (joined at {}) | {read} read | \
                 {} skipped | rms {level:.4}",
                first.unwrap_or(0),
                ear.skipped()
            ))?;
        }
    }
    let wall = start.elapsed().as_secs_f64();
    let (first, last) = ear.clock();
    out.line(format!(
        "duplex: {read} frames in {wall:.1}s — {:.2}x realtime | body frames {}..{last} | \
         {} skipped | ear rms mean {:.4} peak {:.4}",
        (read as f64 * 0.08) / wall.max(1e-9),
        first.unwrap_or(0),
        ear.skipped(),
        total / read.max(1) as f64,
        peak
    ))?;
    if let Some(reason) = ear.ended() {
        out.line(format!("duplex: the body's stream ended — {reason}"))?;
    }
    Ok(EarReport {
        frames: read,
        wall_seconds: wall,
        first_frame: first,
        last_frame: last,
        skipped: ear.skipped(),
        mean_rms: (read > 0).then_some(total / read as f64),
        peak_rms: (read > 0).then_some(peak),
        ended: ear.ended(),
    })
}

// ── playback: decode away from the frame clock, sink opened by name ────────

/// Frames the decoder keeps as context so a chunk boundary is not audible.
/// The codec decoder has no streaming state, so context is re-decoded on
/// every hop: the decoder does `(context + hop) / hop` times realtime work.
/// At 25 and 2 that is 13.5x, which is why these are knobs and not constants.
/// Frames of already-spoken context the codec decoder re-decodes before the
/// new ones, so that the frames it emits are not decoded from a cold start.
///
/// **This has to be deep, and 8 was far too shallow.** The Mimi decoder has
/// no streaming state, so every hop re-runs the whole graph from zero — and
/// that graph contains an 8-layer transformer with a CAUSAL SLIDING WINDOW OF
/// 250 FRAMES. Handing it 8 frames of context truncates its attention by a
/// factor of thirty, so the same codes decode to a different waveform
/// depending on which hop they land in, and the splice between hops is
/// audible. Measured as the median sample-to-sample jump AT a hop boundary
/// over the same statistic away from one, in loud regions of matched runs:
///
/// | temporal fmt | context | boundary ÷ interior |
/// |---|---|---|
/// | q4  |  8 | 2.63 |
/// | q8  |  8 | 2.23 |
/// | f16 |  8 | 1.06 |
/// | q8  | 64 | **0.95** |
///
/// At 64 the boundary is statistically indistinguishable from the interior,
/// i.e. the splice is gone. The convolutional stack needs only a handful of
/// frames (its receptive field is ~3-4 frames at 12.5 Hz), so this depth buys
/// the TRANSFORMER's context, not the convs'. It costs about 6 ms per frame
/// on the decode thread, which is off the frame clock and affordable: a q8
/// session at context 64 measured p50 35.6 ms of an 80 ms budget, 1.00x
/// realtime with nothing over budget.
pub const DEFAULT_DECODE_CONTEXT: usize = 64;
/// New frames emitted per decode call.
pub const DEFAULT_DECODE_HOP: usize = 4;
/// The model may lead the speaker by at most this much. Growing latency is a
/// fault to report, not something to hide in an unbounded queue.
const DECODE_QUEUE_FRAMES: usize = 16;
/// Frames buffered before playback starts. The model produces at realtime, so
/// this covers device start-up, not a production deficit.
const PREBUFFER_FRAMES: usize = 5;
/// Longest the generation clock will wait on the speaker before producing the
/// next frame anyway. A device that stops consuming is a fault to surface, not
/// a reason to wedge the loop forever.
const PACE_DEADLINE: Duration = Duration::from_millis(500);

/// HOW LONG OUR VOICE IS IN THE ROOM, which is not how long the model is
/// generating.
///
/// The mouth is audible LATER than the model is generating: between the two sit
/// the codec decoder and the speaker's own queue. So a window that closed when
/// generation stopped would un-deafen the other consumers of the same
/// microphone while our last words were still coming out of the speaker, and
/// `hear` would transcribe our own voice back to us.
///
/// The tail is not guessed: it is whatever the mouth still has IN FLIGHT at the
/// moment generation stops, counted down one frame per frame, because the
/// speaker consumes one frame per frame. Erring long is the safe direction —
/// the cost is a little extra deafness, the cost of erring short is the loop
/// hearing itself.
///
/// This gates SOFTWARE only. Nothing here touches a device, and the consumer on
/// the other side of the pause file keeps reading frames throughout and simply
/// discards them: the hold stops the model, never the person.
#[derive(Default)]
struct AudibleWindow {
    tail: usize,
}

impl AudibleWindow {
    /// `speaking` is the model's own speech signal for this frame; `in_flight`
    /// is what the mouth has handed the speaker but the speaker has not played.
    /// Returns whether our voice is in the room right now.
    fn observe(&mut self, speaking: bool, in_flight: usize) -> bool {
        if speaking {
            self.tail = in_flight + PREBUFFER_FRAMES;
            return true;
        }
        let sounding = self.tail > 0;
        self.tail = self.tail.saturating_sub(1);
        sounding
    }
}

/// How a line of text is laid out across frames on the inner-monologue
/// stream. The control for the rank measurement below — see
/// [`cadence_schedule`].
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Cadence {
    /// Gap frames at WORD ONSETS only; word pieces run consecutively.
    WordOnset,
    /// Gap frames after EVERY piece, word-internal ones included.
    Uniform,
    /// One token per frame, no gaps at all.
    Dense,
    /// No schedule at all: the model picks the moments, we supply the words.
    Model,
}

/// Lay a line of text out across frames for the model's stream 0.
///
/// **Placement, not density, is what makes a forced token feel native.** The
/// model's own text stream runs word PIECES consecutively and puts its gaps at
/// WORD ONSETS. A schedule that pads after every piece looks correctly sparse
/// on average while being off-distribution *inside* every multi-piece word —
/// a place the model has no gap in its training distribution at all, so it is
/// dragged exactly where it is most confident. That is a different failure
/// from packing too densely and the average PAD fraction cannot see it: a
/// uniform gap of 2 is 67% PAD, SPARSER than the word-onset schedule this
/// model was measured to endorse (~42% PAD), and still wrong.
///
/// The measurement that separates them is the forced token's rank in the
/// model's own logits for that frame — rank 0 means it wanted the token
/// anyway, a large rank means it is being fought. `duplex run` reports the
/// distribution; `--cadence` selects between the three layouts so the claim
/// stays checkable rather than asserted. Measured on two sentences, median
/// rank of a WITHIN-WORD piece:
///
/// | layout | continuation | onset |
/// |---|---|---|
/// | uniform, gap 2 | **24125** | 73 |
/// | word onset, gap 3, `<epad>`-terminated | **1** | 10 |
///
/// A uniform gap is not merely suboptimal — 24125 of 32000 is as hard as this
/// model can be fought, on every multi-piece word, for a whole utterance.
///
/// The gap ENDS with `<epad>`, not `<pad>`, because that is how the model's
/// own stream announces an oncoming word; padding right up to the onset costs
/// an order of magnitude of onset rank (113 vs 7 at gap 2).
///
/// Rank alone does not pick the gap LENGTH: every gap from 1 to 3 sits in the
/// endorsed regime, but only 3 speaks at a natural rate. See `--pace`.
///
/// All of which is why the DEFAULT is [`Cadence::Model`] and none of these:
/// every fixed gap is a metronome, and picking its length is guesswork at a
/// distribution the model already holds. These layouts remain as controls, so
/// the rhythm and rank numbers above stay reproducible.
///
/// SPM marks a word onset with a leading U+2581 on the piece.
#[cfg(feature = "duplex")]
fn cadence_schedule(
    spm: &mary::models::personaplex::spm::SpmTokenizer,
    line: &str,
    gap: usize,
    cadence: Cadence,
) -> Vec<i64> {
    const WORD_MARK: &[u8] = "\u{2581}".as_bytes();
    let ids = spm.encode(line);
    // Nothing to lay out: under `Model` the queue is just the words, and the
    // gaps come from the model one frame at a time.
    if cadence == Cadence::Model {
        return ids;
    }
    let mut out = Vec::with_capacity(ids.len() * (1 + gap));
    for (k, &t) in ids.iter().enumerate() {
        out.push(t);
        let pad_here = match cadence {
            Cadence::Model | Cadence::Dense => false,
            Cadence::Uniform => true,
            Cadence::WordOnset => ids
                .get(k + 1)
                .map(|&n| spm.piece_bytes(n).starts_with(WORD_MARK))
                .unwrap_or(false),
        };
        if pad_here {
            // ... <pad> ... <epad> word: the last gap frame announces the
            // onset rather than looking like more silence.
            for g in 0..gap {
                out.push(if g + 1 == gap { TEXT_EPAD } else { TEXT_PAD });
            }
        }
    }
    out
}

#[cfg(feature = "duplex")]
type Codes = [u32; mary::models::personaplex::mimi::config::NUM_CODEBOOKS];

#[cfg(feature = "duplex")]
use std::sync::atomic::AtomicU64;
#[cfg(feature = "duplex")]
use std::sync::mpsc::{Receiver, TrySendError};

#[cfg(feature = "duplex")]
struct Mouth {
    frames: Option<SyncSender<Codes>>,
    worker: Option<std::thread::JoinHandle<Result<()>>>,
    dropped: Arc<AtomicU64>,
    underruns: Arc<AtomicU64>,
    /// Frames the speaker has finished playing. The lead is derived from this
    /// and the push count rather than published directly, so that a stale
    /// reading errs towards WAITING — see [`Mouth::pace`].
    played: Arc<AtomicU64>,
    /// Frames handed to the mouth. Owned by the generation loop, which is the
    /// only pusher.
    pushed: std::cell::Cell<u64>,
    /// Whether the prebuffer is full and the device is actually consuming.
    playing: Arc<AtomicBool>,
    cancel: Arc<AtomicBool>,
}

#[cfg(feature = "duplex")]
impl Mouth {
    fn spawn(
        weights: PathBuf,
        device: String,
        wav: Option<PathBuf>,
        context_frames: usize,
        hop_frames: usize,
    ) -> Result<Self> {
        let (frame_tx, frame_rx) = mpsc::sync_channel(DECODE_QUEUE_FRAMES);
        let (ready_tx, ready_rx) = mpsc::channel::<std::result::Result<(), String>>();
        let dropped = Arc::new(AtomicU64::new(0));
        let underruns = Arc::new(AtomicU64::new(0));
        let played = Arc::new(AtomicU64::new(0));
        let playing = Arc::new(AtomicBool::new(false));
        let cancel = Arc::new(AtomicBool::new(false));
        let worker = {
            let underruns = Arc::clone(&underruns);
            let played = Arc::clone(&played);
            let playing = Arc::clone(&playing);
            let cancel = Arc::clone(&cancel);
            std::thread::Builder::new()
                .name("duplex-mouth".into())
                .spawn(move || {
                    mouth_worker(
                        weights,
                        device,
                        wav,
                        frame_rx,
                        ready_tx,
                        underruns,
                        played,
                        playing,
                        cancel,
                        context_frames,
                        hop_frames,
                    )
                })
                .context("spawn playback thread")?
        };
        let mouth = Self {
            frames: Some(frame_tx),
            worker: Some(worker),
            dropped,
            underruns,
            played,
            pushed: std::cell::Cell::new(0),
            playing,
            cancel,
        };
        match ready_rx.recv_timeout(Duration::from_secs(900)) {
            Ok(Ok(())) => {}
            Ok(Err(message)) => bail!("playback: {message}"),
            Err(error) => bail!("playback did not come up: {error}"),
        }
        Ok(mouth)
    }

    fn push(&self, codes: Codes) {
        let Some(sender) = self.frames.as_ref() else {
            return;
        };
        match sender.try_send(codes) {
            Ok(()) => self.pushed.set(self.pushed.get() + 1),
            Err(TrySendError::Full(_)) | Err(TrySendError::Disconnected(_)) => {
                self.dropped.fetch_add(1, Ordering::Relaxed);
            }
        }
    }

    fn dropped(&self) -> u64 {
        self.dropped.load(Ordering::Relaxed)
    }

    fn underruns(&self) -> u64 {
        self.underruns.load(Ordering::Relaxed)
    }

    /// Frames of audio handed to the speaker that it has not played yet.
    ///
    /// Derived rather than published, and that direction is load-bearing: the
    /// decode thread cannot publish while it is INSIDE a decode call, so any
    /// reading here may be stale. Because `pushed` is owned by the caller and
    /// only `played` can lag, a stale reading OVERSTATES the lead and the
    /// pacer waits — the safe way to be wrong. Publishing the lead directly
    /// failed the other way: a lead frozen below the target let the loop
    /// free-run for a whole decode call and overflow the frame queue (138 of
    /// 900 frames dropped in one measured session).
    fn lead(&self) -> u64 {
        self.pushed
            .get()
            .saturating_sub(self.played.load(Ordering::Relaxed))
    }

    /// THE SPEAKER IS THE CLOCK. Block until the device has drained the
    /// model's lead below `target` frames, then let the next frame be
    /// generated.
    ///
    /// A generation-only session has no microphone to take its period from,
    /// and a `sleep(FRAME - work)` is NOT a substitute: `thread::sleep`
    /// overshoots by a millisecond or four every time and the error only ever
    /// accumulates in one direction, so the loop produces 80 ms of audio every
    /// ~84 ms — a ~5% production deficit that drains any prebuffer and then
    /// stutters forever. (Measured: 400 frames of audio took 33.6 s of wall
    /// clock and underran 11 times, with the model itself using 37 ms of its
    /// 80 ms budget.) Waiting on the DEVICE instead takes the period from the
    /// hardware that will actually play the samples, so there is no second
    /// clock to drift against — the same reason the duplex loop takes its
    /// period from the microphone.
    ///
    /// Returns `false` if the deadline expired with the device still full,
    /// which means playback has stalled rather than that the model is fast.
    fn pace(&self, target: u64) -> bool {
        // Before the prebuffer is full nothing is being consumed, so there is
        // nothing to pace against: fill it as fast as the model can.
        if !self.playing.load(Ordering::Relaxed) {
            return true;
        }
        let deadline = Instant::now() + PACE_DEADLINE;
        while self.lead() >= target {
            if Instant::now() >= deadline {
                return false;
            }
            std::thread::sleep(Duration::from_millis(2));
        }
        true
    }

    fn finish(mut self) -> Result<()> {
        self.frames.take();
        match self.worker.take() {
            Some(worker) => match worker.join() {
                Ok(result) => result,
                Err(_) => bail!("playback thread panicked"),
            },
            None => Ok(()),
        }
    }
}

#[cfg(feature = "duplex")]
impl Drop for Mouth {
    fn drop(&mut self) {
        // Early return cancels pending playback, then closes the receive wait
        // before joining. A currently executing codec/device call must return;
        // this is scoped ownership, not forced interruption of native code.
        self.cancel.store(true, Ordering::Relaxed);
        self.frames.take();
        if let Some(worker) = self.worker.take() {
            let _ = worker.join();
        }
    }
}

#[cfg(feature = "duplex")]
#[allow(clippy::too_many_arguments)]
fn mouth_worker(
    weights: PathBuf,
    device_name: String,
    wav: Option<PathBuf>,
    frames: Receiver<Codes>,
    ready: mpsc::Sender<std::result::Result<(), String>>,
    underruns: Arc<AtomicU64>,
    played: Arc<AtomicU64>,
    playing: Arc<AtomicBool>,
    cancel: Arc<AtomicBool>,
    context_frames: usize,
    hop_frames: usize,
) -> Result<()> {
    use mary::models::personaplex::mimi::config as codec_cfg;
    use mary::models::personaplex::mimi::MimiDecoder;
    use rodio::buffer::SamplesBuffer;
    use std::num::NonZero;

    set_interactive_qos();
    let setup = || -> Result<(MimiDecoder, rodio::MixerDeviceSink, rodio::Player)> {
        let loader = mary::persist::personaplex_loader(&weights)
            .with_context(|| format!("load the codec from {}", weights.display()))?;
        let decoder = MimiDecoder::load(&loader);
        // Pay the first-call allocation before the session is live.
        let _ = decoder.decode(&vec![
            [0; codec_cfg::NUM_CODEBOOKS];
            context_frames + hop_frames
        ]);
        let (sink, player) = open_named_sink(&device_name)?;
        Ok((decoder, sink, player))
    };
    let (decoder, _sink, player) = match setup() {
        Ok(value) => {
            let _ = ready.send(Ok(()));
            value
        }
        Err(error) => {
            let _ = ready.send(Err(format!("{error:#}")));
            return Err(error);
        }
    };

    let mono = NonZero::new(1u16).expect("1 is nonzero");
    let rate = NonZero::new(SAMPLE_RATE).expect("24000 is nonzero");
    let mut receipt = wav.map(WavWriter::create).transpose()?;
    let mut history: VecDeque<Codes> = VecDeque::new();
    let mut pending = 0usize;
    let mut emitted = 0usize;
    let mut started = false;

    player.pause();
    let mut emit = |history: &VecDeque<Codes>, pending: usize| {
        let chunk: Vec<Codes> = history.iter().copied().collect();
        let context = chunk.len() - pending;
        let pcm = decoder.decode(&chunk);
        let from = context * FRAME_SAMPLES;
        let to = (from + pending * FRAME_SAMPLES).min(pcm.len());
        let slice = pcm[from..to].to_vec();
        if let Some(receipt) = receipt.as_mut() {
            let _ = receipt.write(&slice);
        }
        player.append(SamplesBuffer::new(mono, rate, slice));
    };

    // Frames the device has FINISHED, which is what the generation clock
    // paces against (see [`Mouth::pace`]). `player.len()` counts QUEUED
    // SOURCES and the one being played counts until its last sample is gone,
    // so `emitted - len·hop` UNDERSTATES what has been played by at most one
    // hop — the direction that makes the pacer wait rather than run ahead.
    let publish = |player: &rodio::Player, emitted: usize| {
        played.store(
            (emitted.saturating_sub(player.len() * hop_frames)) as u64,
            Ordering::Relaxed,
        );
    };

    loop {
        if cancel.load(Ordering::Relaxed) {
            player.stop();
            return Ok(());
        }
        // A timed receive so the lead stays live while the model is thinking:
        // a producer that only republished on its own writes would tell the
        // pacer the queue is full for as long as it takes to fill it.
        let frame = match frames.recv_timeout(Duration::from_millis(2)) {
            Ok(frame) => frame,
            Err(mpsc::RecvTimeoutError::Timeout) => {
                publish(&player, emitted);
                continue;
            }
            Err(mpsc::RecvTimeoutError::Disconnected) => break,
        };
        history.push_back(frame);
        pending += 1;
        if pending < hop_frames {
            publish(&player, emitted);
            continue;
        }
        // An empty queue after playback started means the decoder fell behind
        // the device — the rebuffering stutter, counted rather than hidden.
        if started && player.empty() {
            underruns.fetch_add(1, Ordering::Relaxed);
        }
        emit(&history, pending);
        emitted += pending;
        pending = 0;
        while history.len() > context_frames {
            history.pop_front();
        }
        if !started && emitted >= PREBUFFER_FRAMES {
            player.play();
            started = true;
            playing.store(true, Ordering::Relaxed);
        }
        publish(&player, emitted);
    }
    if pending > 0 {
        emit(&history, pending);
    }
    if !started {
        player.play();
    }
    // Let the device drain rather than cutting the last words.
    let deadline = Instant::now() + Duration::from_secs(10);
    while !player.empty() && Instant::now() < deadline && !cancel.load(Ordering::Relaxed) {
        std::thread::sleep(Duration::from_millis(20));
    }
    if cancel.load(Ordering::Relaxed) {
        player.stop();
    }
    drop(receipt);
    Ok(())
}

#[cfg(feature = "audio")]
fn open_named_sink(name: &str) -> Result<(rodio::MixerDeviceSink, rodio::Player)> {
    use rodio::cpal::traits::{DeviceTrait, HostTrait};
    let host = rodio::cpal::default_host();
    let device = host
        .output_devices()
        .context("enumerate playback devices")?
        .find(|candidate| candidate.name().map(|n| n == name).unwrap_or(false))
        .with_context(|| format!("playback device '{name}' not found (disconnected?)"))?;
    let mut sink = rodio::DeviceSinkBuilder::from_device(device)
        .with_context(|| format!("prepare playback device '{name}'"))?
        .open_stream()
        .with_context(|| format!("open playback device '{name}'"))?;
    let player = rodio::Player::connect_new(sink.mixer());
    sink.log_on_drop(false);
    Ok((sink, player))
}

// ── the durable transcript ─────────────────────────────────────────────────

/// Appends completed model utterances to the pile's Voice collection, off the
/// frame clock. A slow or failing pile delays the ledger, never the audio.
struct Ledger {
    lines: Option<SyncSender<String>>,
    worker: Option<std::thread::JoinHandle<()>>,
    warnings: mpsc::Receiver<String>,
    dropped_warnings: Arc<std::sync::atomic::AtomicUsize>,
    dropped_lines: std::cell::Cell<usize>,
}
impl Ledger {
    fn spawn(pile: Option<PathBuf>, key: Option<PathBuf>) -> Result<Self> {
        let (warning_tx, warnings) = mpsc::sync_channel(16);
        let dropped_warnings = Arc::new(std::sync::atomic::AtomicUsize::new(0));
        let Some(pile) = pile else {
            return Ok(Self {
                lines: None,
                worker: None,
                warnings,
                dropped_warnings,
                dropped_lines: std::cell::Cell::new(0),
            });
        };
        let (tx, rx) = mpsc::sync_channel::<String>(64);
        let dropped = Arc::clone(&dropped_warnings);
        let worker = std::thread::Builder::new()
            .name("duplex-ledger".into())
            .spawn(move || {
                while let Ok(text) = rx.recv() {
                    if let Err(error) = record_utterance(&pile, key.as_deref(), &text) {
                        if warning_tx
                            .try_send(format!("duplex: could not record utterance: {error:#}"))
                            .is_err()
                        {
                            dropped.fetch_add(1, Ordering::Relaxed);
                        }
                    }
                }
            })
            .context("spawn transcript ledger")?;
        Ok(Self {
            lines: Some(tx),
            worker: Some(worker),
            warnings,
            dropped_warnings,
            dropped_lines: std::cell::Cell::new(0),
        })
    }
    fn record(&self, text: &str) {
        if let Some(lines) = &self.lines {
            if lines.try_send(text.to_owned()).is_err() {
                self.dropped_lines.set(self.dropped_lines.get() + 1);
            }
        }
    }
    fn report(&self, out: &mut Out<'_>) -> Result<()> {
        for warning in self.warnings.try_iter() {
            out.line(warning)?;
        }
        let dropped = self.dropped_warnings.swap(0, Ordering::Relaxed);
        if dropped > 0 {
            out.line(format!(
                "duplex: {dropped} additional ledger errors exceeded the diagnostic queue"
            ))?;
        }
        let dropped = self.dropped_lines.replace(0);
        if dropped > 0 {
            out.line(format!(
                "duplex: {dropped} utterances could not enter the durable transcript queue"
            ))?;
        }
        Ok(())
    }
    fn finish(mut self, out: &mut Out<'_>) -> Result<()> {
        self.lines.take();
        if let Some(worker) = self.worker.take() {
            worker
                .join()
                .map_err(|_| anyhow::anyhow!("transcript ledger thread panicked"))?;
        }
        self.report(out)
    }
}

impl Drop for Ledger {
    fn drop(&mut self) {
        // The sender is closed FIRST; otherwise the worker could stay blocked
        // in recv forever. Already queued utterances finish before we return.
        self.lines.take();
        if let Some(worker) = self.worker.take() {
            let _ = worker.join();
        }
    }
}

#[cfg(any(feature = "duplex", test))]
fn spoken_line(session: &Path, line: &Line, ledger: &Ledger, out: &mut Out<'_>) -> Result<()> {
    let transcript = append_line(session, line);
    ledger.record(&line.text);
    transcript.context("record generated utterance in the session transcript")?;
    out.line(format!("  [{}] {}", line.speaker, line.text))
}

/// Schedule the consumed prefix even when cleanup of a later queue file
/// failed. Such failures are recoverable; identical consecutive failures are
/// reported once, not on every control poll. This is only an in-memory
/// handoff: an Out failure still terminates run, and does not make queued or
/// partially generated speech durable.
#[cfg(any(feature = "duplex", test))]
fn accept_inject_drain(
    drained: InjectDrain,
    last_cleanup_failure: &mut Option<String>,
    mut schedule: impl FnMut(&str),
    out: &mut Out<'_>,
) -> Result<()> {
    for line in &drained.lines {
        schedule(line);
    }
    for line in drained.lines {
        out.line(format!("duplex: to say — {line}"))?;
    }
    let failure = drained.cleanup_failure.map(|error| format!("{error:#}"));
    if failure != *last_cleanup_failure {
        if let Some(error) = &failure {
            out.line(format!("duplex: inject queue cleanup failed — {error}"))?;
        }
        *last_cleanup_failure = failure;
    }
    Ok(())
}

/// One utterance, one commit — the exact record shape `voice shout` writes, so
/// nothing that reads the collection has to learn a new one.
fn record_utterance(pile_path: &Path, key: Option<&Path>, text: &str) -> Result<()> {
    use crate::schemas::voice::{CHANNEL_SHOUT, COLLECTION_SCOPE_ID};
    use triblespace::core::collection::CollectionStoreExt;
    use triblespace::core::metadata;
    use triblespace::prelude::*;

    let stamp = clock::point_now()?;
    let mut fragment = crate::voice::utterance_fragment(CHANNEL_SHOUT, text, None, stamp)?;

    crate::storage::Storage::new(pile_path.to_owned(), key.map(Path::to_owned))
        .with_store(|pile, signer, runtime| {
        let collection = crate::collection_names::open_configured_acquiring(
            pile, COLLECTION_SCOPE_ID, signer.verifying_key(), runtime,
        )?;
        crate::voice::validate_staged_payloads(&mut fragment)?;
        fragment.describe_with(entity! { metadata::description: "duplex spoke" });
        crate::collection_names::require_command_write_admission_acquiring(
            pile,
            collection,
            signer,
            "Duplex",
            "voice route show",
            runtime,
        )?;
        pile.commit(collection, signer, fragment)
            .context("commit the utterance")?;
        drop(
            runtime.block_on(crate::storage::ensure_downstream(
                pile, collection, signer,
            ))
            .context("Duplex utterance was committed, but ensuring its derived views failed")?,
        );
        Ok(())
    })
}

// ── the loop ───────────────────────────────────────────────────────────────

#[cfg(not(feature = "duplex"))]
pub fn run(
    _session: &Path,
    _args: &RunOptions,
    _stop: &AtomicBool,
    _out: &mut Out<'_>,
) -> Result<RunReport> {
    bail!("the channel is not compiled into this build (build with --features duplex)")
}

/// What the model is allowed to do with its own text stream.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Floor {
    /// Text stream forced to padding: it hears, it may backchannel, it
    /// generates no words. Words arrive only by injection.
    Listen,
    /// The model holds up its own end.
    Converse,
}

/// Which PersonaPlex token-step contract one frame uses. Keeping this choice
/// explicit prevents generation-only mode from quietly falling back to a
/// learned silence frame, while preserving both ordinary duplex paths.
#[cfg(feature = "duplex")]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum PersonaPlexStepApi {
    Duplex,
    DuplexArbitrated,
    OutputOnly,
    OutputOnlyArbitrated,
}

#[cfg(feature = "duplex")]
impl PersonaPlexStepApi {
    fn select(output_only: bool, arbitrated: bool) -> Self {
        match (output_only, arbitrated) {
            (false, false) => Self::Duplex,
            (false, true) => Self::DuplexArbitrated,
            (true, false) => Self::OutputOnly,
            (true, true) => Self::OutputOnlyArbitrated,
        }
    }
}

#[cfg(feature = "duplex")]
pub fn run(
    session: &Path,
    args: &RunOptions,
    stop: &AtomicBool,
    out: &mut Out<'_>,
) -> Result<RunReport> {
    use mary::models::personaplex::config as model_cfg;
    use mary::models::personaplex::pipeline::{agent_codes, RealtimePipeline, SILENCE};
    use mary::models::personaplex::prompt::Prompt;
    use mary::models::personaplex::sampling::SamplingConfig;
    use mary::models::personaplex::temporal_metal::WeightFmt;

    args.validate()?;
    let fmt = match args.fmt {
        WeightFormat::Q4 => WeightFmt::Q4,
        WeightFormat::Q8 => WeightFmt::Q8,
        WeightFormat::F16 => WeightFmt::F16,
    };
    let cadence = args.cadence;
    let floor_policy = args.floor;
    ensure_session(session)?;
    // A fresh run starts a fresh transcript; the durable copy is the pile.
    let _ = std::fs::remove_file(session.join(TRANSCRIPT_FILE));
    let _ = std::fs::remove_file(session.join(HOLD_FILE));
    write_cursor(session, 0)?;

    set_interactive_qos();

    // The mouth loads its own codec while the model loads, so the two long
    // loads overlap instead of queueing.
    out.line(format!("duplex: bringing up '{}' …", args.output))?;
    let mouth = Mouth::spawn(
        args.weights.clone(),
        args.output.clone(),
        args.wav.clone(),
        args.decode_context,
        args.decode_hop,
    )?;

    out.line(format!(
        "duplex: loading the model from {} …",
        args.weights.display()
    ))?;
    let load_start = Instant::now();
    let source = mary::persist::personaplex_bundle(&args.weights)
        .with_context(|| format!("load the model from {}", args.weights.display()))?
        .into_runtime_source();
    let mut pipeline = RealtimePipeline::load_auto(&source, fmt, true);
    if args.temp <= 0.0 {
        pipeline.set_greedy();
    } else {
        pipeline.set_sampling(
            SamplingConfig {
                temp: args.temp,
                top_k: 250,
                top_p: 0.95,
            },
            args.seed,
        );
    }
    let spm = match args.spm.as_deref() {
        Some(path) => mary::models::personaplex::spm::SpmTokenizer::load(path),
        None => mary::persist::load_spm_tokenizer_from_pile(&args.weights)
            .context("load the text tokenizer from the weight pile (or pass --spm)")?,
    };
    if spm.vocab_size() != model_cfg::TEXT_CARD {
        bail!(
            "tokenizer vocabulary {} does not match the model's {} — wrong tokenizer",
            spm.vocab_size(),
            model_cfg::TEXT_CARD
        );
    }
    let prompt = Prompt::build(&args.voice_prompt, &spm, &args.system);
    pipeline.run_prompt(&prompt);
    out.line(format!(
        "duplex: model ready in {:.1}s ({} prompt steps)",
        load_start.elapsed().as_secs_f64(),
        prompt.total_steps()
    ))?;

    // Generation-only pacing: how far the model may run ahead of the speaker.
    // The device reports its queue one decode hop at a time, so a target below
    // one hop can never be met and a target of exactly one hop lets the queue
    // reach the device's last sample; two hops keeps a full hop in front of it.
    let nudge_after = args.nudge_after;
    let min_word_gap = args.min_word_gap;
    let mut trace_out = match args.trace.as_ref() {
        Some(path) => {
            let mut f = std::fs::File::create(path)
                .with_context(|| format!("open the frame trace {}", path.display()))?;
            use std::io::Write;
            writeln!(f, "frame\ttoken\tclass\tsource")?;
            Some(f)
        }
        None => None,
    };
    let lead_target = args.lead.unwrap_or(2 * args.decode_hop).max(1) as u64;
    let mut stalled = 0usize;

    // The ear is subscribed last, and this process opens no device: Soma holds
    // the microphone for the life of the BODY, so on a handsfree endpoint the
    // channel is Soma's open, not ours, and it outlives this session.
    let ear = if args.no_input {
        out.line(format!(
            "duplex: generation only — no ear, the speaker is the clock \
             (lead {lead_target} frames = {} ms)",
            lead_target * 80
        ))?;
        None
    } else {
        out.line(format!(
            "duplex: subscribing to the body's microphone at {} …",
            args.soma
        ))?;
        let ear = Ear::open(&args.soma)?;
        out.line(format!(
            "duplex: ear open — {} Hz, {} sample frames; this process owns no device, so \
             `hear` can read the same frames",
            SAMPLE_RATE, FRAME_SAMPLES
        ))?;
        Some(ear)
    };

    let ledger = Ledger::spawn(args.pile.clone(), args.key.clone())?;
    let mut encoder_state = pipeline.encoder.stream_state();
    let mut queued: VecDeque<i64> = VecDeque::new();
    let mut injected_text: VecDeque<String> = VecDeque::new();
    let mut spoken = String::new();
    let mut spoken_was_injected = false;
    let mut pad_run = 0usize;
    let mut speaking_hangover = 0usize;

    // The SHARED-MICROPHONE guard. Inside this binary turn-taking needs no
    // file — `--gate` feeds the model silence while it speaks, in process, on
    // the frame clock. The file exists for the OTHER consumers of the same
    // Soma frames: while it is there, `hear` keeps reading and discards, so it
    // does not transcribe our own voice back to us. A stale one would deafen
    // them permanently, so never trust the last run to have exited cleanly.
    if let Some(path) = args.pause_file.as_deref() {
        if crate::turntaking::clear_stale(path) {
            out.line(format!(
                "duplex: cleared a stale pause file at {}",
                path.display()
            ))?;
        }
        out.line(format!(
            "duplex: holding {} while audible, so a `hear` on the same body stays deaf to us",
            path.display()
        ))?;
    }
    let mut audible: Option<crate::turntaking::PauseGuard> = None;
    let mut audible_window = AudibleWindow::default();

    let mut seq = 0u64;
    let mut held = false;
    let mut far_loud = 0usize;
    let mut far_quiet = 0usize;
    let mut far_talking = false;
    let mut far_started_ms = 0u64;
    let mut heard_peak = 0f32;
    let mut heard_total = 0f64;
    let mut heard_frames = 0usize;

    // Is the model being DRAGGED? At each forced frame, where the token we are
    // about to force sits in the model's own ranking from the previous frame.
    // Rank 0 means it would have chosen that token itself; a large rank means
    // the schedule is off its distribution. This is the probe's measure, and
    // it is the honest one — an ear cannot tell "wrong words" from "right
    // words in a shape the model never sees", and a waveform statistic cannot
    // either. Kept always-on: one pass over the logit row is ~0.1% of a frame.
    // Split by what the token IS, because the two halves mean different
    // things. The first piece of a word is unpredictable BY CONSTRUCTION — the
    // model cannot know which word we chose — so a high rank there is the
    // price of forcing arbitrary text, not evidence of a bad schedule. A
    // within-word continuation is the opposite: the model is confident about
    // how a word it has already started will finish, so a high rank THERE is
    // the schedule genuinely fighting it. That is the number a cadence change
    // should move.
    let mut rank_onset: Vec<usize> = Vec::new();
    let mut rank_cont: Vec<usize> = Vec::new();
    let mut rank_pad: Vec<usize> = Vec::new();
    // The RHYTHM actually produced: frames from one word to the next. A fixed
    // schedule makes this a constant by construction; letting the model choose
    // the moments is only worth anything if it is not.
    let mut word_gaps: Vec<usize> = Vec::new();
    let mut since_word = 0usize;
    // Free timing, watched: how often the model picked the moment itself, and
    // how often it sat on PAD long enough that we had to start the word for
    // it. A nudge is us imposing rhythm again, so it is counted, not hidden.
    let mut model_chose = 0usize;
    let mut nudged = 0usize;
    let mut wait_frames = 0usize;
    // Frames we held PAD because the model wanted the next word sooner than
    // `--min-word-gap` allows, and frames since the last onset went out.
    let mut held_back = 0usize;
    let mut since_onset = usize::MAX / 2;

    let mut frame_index = 0usize;
    let mut step_total = 0f64;
    let mut step_max = 0f64;
    let mut over_budget = 0usize;
    let mut step_times: Vec<f64> = Vec::new();
    let mut last_control_poll = Instant::now();
    let mut last_inject_cleanup_failure = None;

    out.line(format!(
        "duplex: live. `duplex read` to catch up, `duplex say` to speak, Ctrl-C to stop."
    ))?;
    let session_start = Instant::now();
    loop {
        if stop.load(Ordering::Relaxed) {
            break;
        }
        if let Some(limit) = args.frames {
            if frame_index >= limit {
                break;
            }
        }

        // The control plane is polled four times a second, far below the pace
        // anything can be spoken at, and never on the frame's critical path.
        if last_control_poll.elapsed() >= Duration::from_millis(250) {
            last_control_poll = Instant::now();
            ledger.report(out)?;
            let now_held = floor_held(session)?;
            if now_held != held {
                out.line(format!(
                    "duplex: floor {}",
                    if now_held {
                        "TAKEN — silent"
                    } else {
                        "released"
                    }
                ))?;
                held = now_held;
            }
            accept_inject_drain(
                drain_inject(session)?,
                &mut last_inject_cleanup_failure,
                |line| {
                    queued.extend(cadence_schedule(&spm, line, args.pace, cadence));
                    injected_text.push_back(line.to_owned());
                },
                out,
            )?;
        }

        // THE MICROPHONE IS THE CLOCK — one layer removed, and still no timer:
        // the ear blocks until the body hands over the frame the device just
        // produced.
        let samples = match ear.as_ref() {
            Some(ear) => match ear.next_frame() {
                Some(frame) => Some(frame),
                None => {
                    match ear.ended() {
                        Some(reason) => {
                            out.line(format!("duplex: the body's stream ended — {reason}"))?
                        }
                        None => out.line("duplex: the body stopped producing frames")?,
                    }
                    break;
                }
            },
            None => {
                // No microphone means no clock on the input side — so THE
                // SPEAKER IS THE CLOCK. Never a `sleep(FRAME - work)`: that
                // is a second clock, it overshoots in one direction only, and
                // the deficit lands as rebuffering. See [`Mouth::pace`].
                if !mouth.pace(lead_target) {
                    stalled += 1;
                }
                None
            }
        };

        // The far end's turn structure is the only thing this model can
        // honestly report about them: presence and duration, never words.
        if let Some(frame) = samples.as_ref() {
            let level = rms(frame);
            heard_peak = heard_peak.max(level);
            heard_total += level as f64;
            heard_frames += 1;
            if level >= VOICE_FLOOR {
                far_loud += 1;
                far_quiet = 0;
            } else {
                far_quiet += 1;
                far_loud = 0;
            }
            let _ = level;
            if !far_talking && far_loud >= VOICE_ONSET_FRAMES {
                far_talking = true;
                far_started_ms = now_millis()?;
            } else if far_talking && far_quiet >= VOICE_RELEASE_FRAMES {
                far_talking = false;
                let seconds = (now_millis()?.saturating_sub(far_started_ms)) as f64 / 1000.0;
                seq += 1;
                let line = Line {
                    seq,
                    at_ms: far_started_ms,
                    speaker: Speaker::Far.tag().into(),
                    text: format!("[spoke for {seconds:.1}s — no transcription available]"),
                };
                let _ = append_line(session, &line);
            }
        }

        let step_start = Instant::now();
        let heard: Option<[i64; 8]> = match samples {
            // While the model speaks, an endpoint without echo cancellation
            // would hear itself; gate in software, never by touching the
            // device.
            Some(_) if args.gate && speaking_hangover > 0 => Some(SILENCE),
            Some(frame) => {
                let codes = pipeline
                    .encoder
                    .encode_stream_frame(&mut encoder_state, &frame);
                Some(std::array::from_fn(|q| codes[q] as i64))
            }
            None => None,
        };

        // The three states of the mouth.
        //   held      — silent: text padded AND agent audio forced to silence.
        //   speaking  — the injected line, paced to the text stream's cadence.
        //   otherwise — the floor policy decides whether it may find its own
        //               words; under `listen` it may only backchannel.
        let was_speaking_injected = !queued.is_empty();
        // Arbitration only while a line is draining and the floor is ours.
        // A held floor is an explicit instruction to be silent and must not be
        // handed back to the model's judgement.
        let free_timing = cadence == Cadence::Model && was_speaking_injected && !held;
        let (forced_text, forced_audio) = if held {
            (Some(TEXT_PAD), Some(&SILENCE))
        } else if free_timing {
            // The queue is drained inside the arbiter, at the moment the model
            // asks for a word — not here, on a schedule.
            (None, None)
        } else if !queued.is_empty() {
            // `queued` is a FRAME schedule, not a token list: the gaps are
            // already in it, placed by `cadence_schedule`.
            (queued.pop_front(), None)
        } else {
            match floor_policy {
                Floor::Listen => (Some(TEXT_PAD), None),
                Floor::Converse => (None, None),
            }
        };

        // Two ways to put a word on stream 0, and they divide the labour
        // differently. Forcing a token supplies WHAT is said and WHEN; under
        // `--cadence model` we supply only the what, and the model keeps the
        // when — its own pauses, their lengths, its own `<epad>` placement.
        let step_api = PersonaPlexStepApi::select(args.no_input, free_timing);
        let trace = if free_timing {
            let queue = &mut queued;
            let waited = &mut wait_frames;
            let chose = &mut model_chose;
            let nudges = &mut nudged;
            let held = &mut held_back;
            let gap = &mut since_onset;
            let spm_ref = &spm;
            let onset = move |t: i64| {
                t >= N_TEXT_SPECIALS && spm_ref.piece_bytes(t).starts_with("\u{2581}".as_bytes())
            };
            let mut decide = |_logits: &[f32], sampled: i64| -> i64 {
                if sampled >= N_TEXT_SPECIALS {
                    // It decided to speak. The moment is its own; the word is
                    // ours — unless a floor is set and it wants to start the
                    // next WORD sooner than that allows. Then we hold, which
                    // is us imposing rhythm again, so it is counted.
                    let next_onset = queue.front().copied().map(onset).unwrap_or(false);
                    if min_word_gap > 0 && next_onset && *gap < min_word_gap {
                        *gap += 1;
                        *held += 1;
                        return TEXT_PAD;
                    }
                    *waited = 0;
                    *chose += 1;
                    let t = queue.pop_front().unwrap_or(sampled);
                    if onset(t) {
                        *gap = 0;
                    } else {
                        *gap += 1;
                    }
                    t
                } else if *waited >= nudge_after {
                    // It has held silence longer than we are willing to wait.
                    // Start the word for it and count that we did.
                    *waited = 0;
                    *nudges += 1;
                    let t = queue.pop_front().unwrap_or(sampled);
                    if onset(t) {
                        *gap = 0;
                    } else {
                        *gap += 1;
                    }
                    t
                } else {
                    // PAD or EPAD: leave it exactly as sampled. This is the
                    // whole point — the pause is the model's.
                    *waited += 1;
                    *gap += 1;
                    sampled
                }
            };
            match (step_api, heard.as_ref()) {
                (PersonaPlexStepApi::DuplexArbitrated, Some(heard)) => {
                    pipeline.step_arbitrated(Some(heard), forced_audio, &mut decide)
                }
                (PersonaPlexStepApi::OutputOnlyArbitrated, None) => {
                    pipeline.step_output_only_arbitrated(forced_audio, &mut decide)
                }
                _ => unreachable!("PersonaPlex input protocol changed within one frame"),
            }
        } else {
            match (step_api, heard.as_ref()) {
                (PersonaPlexStepApi::Duplex, Some(heard)) => {
                    pipeline.step(Some(heard), forced_audio, forced_text)
                }
                (PersonaPlexStepApi::OutputOnly, None) => {
                    pipeline.step_output_only(forced_audio, forced_text)
                }
                _ => unreachable!("PersonaPlex input protocol changed within one frame"),
            }
        };
        // What actually went to the depformer this frame, whichever path chose
        // it. Under arbitration this is the substituted token, not the sample.
        let chosen: Option<i64> = if free_timing {
            Some(trace.next_text)
        } else {
            forced_text
        };
        let elapsed = step_start.elapsed().as_secs_f64() * 1e3;
        step_total += elapsed;
        step_max = step_max.max(elapsed);
        step_times.push(elapsed);
        if elapsed > 80.0 {
            over_budget += 1;
        }

        // Is the model being DRAGGED? Where the token we forced sits in the
        // model's own ranking for that frame. Rank 0 means it wanted the token
        // anyway; a large rank means the schedule is off its distribution. An
        // ear cannot tell "wrong words" from "right words in a shape the model
        // never sees", and no waveform statistic can either — this can.
        //
        // It is THIS step's logit row, not the previous one: the step both
        // reads the row and consumes the token it was handed, so the row and
        // the token are the same frame's. That is not an assumption — both
        // pairings were scored side by side, and only this one puts a
        // within-word continuation where it obviously belongs (p50 rank 1,
        // against 14910 for the previous row). Kept always-on: one pass over
        // the logit row is ~0.1% of a frame.
        //
        // Split by what the token IS, because the halves mean different
        // things. The first piece of a word is ours to choose, so the model
        // cannot fully anticipate it and a moderate rank there is the price of
        // forcing arbitrary text. A within-word continuation is the opposite —
        // the model is confident how a word it already started will finish, so
        // a high rank THERE is the schedule genuinely fighting it, and that is
        // the number a cadence change has to move.
        if was_speaking_injected {
            if let Some(t) = chosen {
                if !trace.text_logits.is_empty() && (t as usize) < trace.text_logits.len() {
                    let lv = trace.text_logits[t as usize];
                    let rank = trace.text_logits.iter().filter(|&&v| v > lv).count();
                    if t < N_TEXT_SPECIALS {
                        rank_pad.push(rank);
                    } else if spm.piece_bytes(t).starts_with("\u{2581}".as_bytes()) {
                        rank_onset.push(rank);
                    } else {
                        rank_cont.push(rank);
                    }
                }
            }
        }
        if let Some(f) = trace_out.as_mut() {
            use std::io::Write;
            let t = chosen.unwrap_or(-1);
            let class = if t < 0 {
                "none"
            } else if t == TEXT_EPAD {
                "epad"
            } else if t < N_TEXT_SPECIALS {
                "pad"
            } else if spm.piece_bytes(t).starts_with("\u{2581}".as_bytes()) {
                "onset"
            } else {
                "cont"
            };
            let source = if free_timing { "model" } else { "schedule" };
            let _ = writeln!(f, "{frame_index}\t{t}\t{class}\t{source}");
        }

        // Rhythm, measured wherever it came from.
        if was_speaking_injected {
            match chosen {
                Some(t) if t >= N_TEXT_SPECIALS => {
                    word_gaps.push(since_word);
                    since_word = 0;
                }
                _ => since_word += 1,
            }
        }

        if let Some(out) = trace.out.as_ref() {
            mouth.push(agent_codes(out));
            let token = out[0];
            if token >= N_TEXT_SPECIALS {
                let piece = spm.decode_token(token);
                if !piece.is_empty() {
                    spoken.push_str(&piece);
                    spoken_was_injected |= was_speaking_injected;
                    pad_run = 0;
                    speaking_hangover = args.utterance_gap;
                }
            } else {
                pad_run += 1;
                speaking_hangover = speaking_hangover.saturating_sub(1);
            }
        }

        // Hold the pause file across the whole time our voice is IN THE ROOM,
        // which is the generation window plus whatever the decoder and the
        // speaker still have queued. Software only: nothing here touches a
        // device, and `hear` keeps reading throughout.
        if let Some(path) = args.pause_file.as_deref() {
            let sounding = audible_window.observe(speaking_hangover > 0, mouth.lead() as usize);
            if sounding && audible.is_none() {
                audible = Some(crate::turntaking::PauseGuard::hold(path));
            } else if !sounding {
                audible = None;
            }
        }

        // An utterance ends where the text stream goes quiet — but NOT while
        // a line is still being said. A quiet stretch is only an ending when
        // there is nothing left to say; until the queue is drained it is a
        // pause inside the line.
        //
        // This became load-bearing when the model took over the timing. A
        // scheduled cadence bounded every gap at `--pace` frames, so a pause
        // could never reach `--utterance-gap`; the model's own pauses run to
        // 25 frames, well past it, and the rule then cut one sentence into six
        // transcript entries mid-line. The audio and the words were never
        // affected, but the pile recorded fragments and the injected-line
        // bookkeeping advanced once per fragment.
        if queued.is_empty() && pad_run >= args.utterance_gap && !spoken.trim().is_empty() {
            let line = spoken.trim().to_owned();
            let speaker = if spoken_was_injected {
                Speaker::Agent
            } else {
                Speaker::Model
            };
            seq += 1;
            spoken_line(
                session,
                &Line {
                    seq,
                    at_ms: now_millis()?,
                    speaker: speaker.tag().into(),
                    text: line.clone(),
                },
                &ledger,
                out,
            )?;
            let _ = injected_text.pop_front();
            spoken.clear();
            spoken_was_injected = false;
            pad_run = 0;
        }

        frame_index += 1;
        if frame_index % 125 == 0 {
            let skipped = ear.as_ref().map(Ear::skipped).unwrap_or(0);
            // The BODY's frame, not ours: a `hear` on the same microphone is
            // counting the same one, so the two logs can be laid side by side
            // and read as one instant.
            let body = ear
                .as_ref()
                .map(|ear| ear.clock().1.to_string())
                .unwrap_or_else(|| "-".into());
            out.line(format!(
                "  [clock] {:.1}s | body frame {body} | step mean {:.1} ms max {:.1} ms | \
                 {} over budget | {} in skipped | {} out dropped | {} underruns | lead {} | \
                 ear rms mean {:.4} peak {:.4}",
                session_start.elapsed().as_secs_f64(),
                step_total / frame_index as f64,
                step_max,
                over_budget,
                skipped,
                mouth.dropped(),
                mouth.underruns(),
                mouth.lead(),
                heard_total / heard_frames.max(1) as f64,
                heard_peak
            ))?;
        }
    }

    // A turn still in progress at shutdown is still a turn. Without this the
    // far end talking right up to the end leaves no trace at all, which reads
    // as "they said nothing" rather than "we stopped listening".
    if far_talking {
        let seconds = (now_millis()?.saturating_sub(far_started_ms)) as f64 / 1000.0;
        seq += 1;
        let _ = append_line(
            session,
            &Line {
                seq,
                at_ms: far_started_ms,
                speaker: Speaker::Far.tag().into(),
                text: format!(
                    "[speaking for {seconds:.1}s when the session ended — no \
                     transcription available]"
                ),
            },
        );
    }
    if !spoken.trim().is_empty() {
        let line = spoken.trim().to_owned();
        seq += 1;
        let speaker = if spoken_was_injected {
            Speaker::Agent
        } else {
            Speaker::Model
        };
        spoken_line(
            session,
            &Line {
                seq,
                at_ms: now_millis()?,
                speaker: speaker.tag().into(),
                text: line.clone(),
            },
            &ledger,
            out,
        )?;
    }
    let wall = session_start.elapsed().as_secs_f64();
    step_times.sort_by(|a, b| a.partial_cmp(b).expect("finite"));
    let pct = |p: f64| -> f64 {
        if step_times.is_empty() {
            return 0.0;
        }
        let index = ((step_times.len() - 1) as f64 * p).round() as usize;
        step_times[index]
    };
    out.line(format!(
        "duplex: {} frames of audio in {:.1}s wall — {:.2}x realtime\n\
         \x20 step ms: p50 {:.1}  p90 {:.1}  p99 {:.1}  max {:.1}  mean {:.1} \
         (budget 80)\n\
         \x20 {} of {} frames over budget ({:.0}%), {} playback underruns, \
         {} speaker stalls",
        frame_index,
        wall,
        (frame_index as f64 * 0.08) / wall.max(1e-9),
        pct(0.50),
        pct(0.90),
        pct(0.99),
        step_max,
        step_total / frame_index.max(1) as f64,
        over_budget,
        frame_index,
        100.0 * over_budget as f64 / frame_index.max(1) as f64,
        mouth.underruns(),
        stalled
    ))?;
    // Was the model dragged? Report the schedule alongside its cost, so a
    // cadence claim is checkable against the model rather than against ears.
    let quantiles = |v: &mut Vec<usize>| -> (usize, usize, usize) {
        v.sort_unstable();
        let at = |p: f64| v[((v.len() - 1) as f64 * p).round() as usize];
        (at(0.50), at(0.90), at(0.99))
    };
    if rank_onset.is_empty() && rank_cont.is_empty() {
        out.line(format!("  forced-token rank: nothing was forced"))?;
    } else {
        let total = rank_onset.len() + rank_cont.len() + rank_pad.len();
        out.line(format!(
            "  forced-token rank in the model's own logits ({} cadence, gap {}, {} forced frames, {:.0}% PAD):",
            args.cadence.name(),
            args.pace,
            total,
            100.0 * rank_pad.len() as f64 / total.max(1) as f64,
        ))?;
        for (what, v) in [
            ("word continuation", &mut rank_cont),
            ("word onset       ", &mut rank_onset),
            ("pad              ", &mut rank_pad),
        ] {
            if v.is_empty() {
                continue;
            }
            let n = v.len();
            let (a, b, c) = quantiles(v);
            out.line(format!(
                "   {what}  p50 {a:>6}  p90 {b:>6}  p99 {c:>6}  (n={n})"
            ))?;
        }
    }
    if !word_gaps.is_empty() {
        let n = word_gaps.len();
        let spread = {
            let mut u: Vec<usize> = word_gaps.clone();
            u.sort_unstable();
            u.dedup();
            u.len()
        };
        let mean = word_gaps.iter().sum::<usize>() as f64 / n as f64;
        let (g50, g90, g99) = quantiles(&mut word_gaps);
        out.line(format!(
            "  rhythm — frames from one word to the next: p50 {g50}  p90 {g90}  p99 {g99}  mean {mean:.2}  ({spread} distinct values over {n} words)"
        ))?;
    }
    if model_chose + nudged > 0 {
        out.line(format!(
            "  timing: the model chose the moment {model_chose} times, we started it {nudged} times ({:.0}% ours)",
            100.0 * nudged as f64 / (model_chose + nudged) as f64
        ))?;
        if held_back > 0 {
            out.line(format!(
                "          and held it back {held_back} frames for --min-word-gap {min_word_gap}"
            ))?;
        }
    }
    let report = RunReport {
        frames: frame_index,
        wall_seconds: wall,
        step_times_ms: step_times,
        over_budget,
        playback_underruns: mouth.underruns(),
        speaker_stalls: stalled,
        output_dropped: mouth.dropped(),
        input_skipped: ear.as_ref().map(Ear::skipped).unwrap_or(0),
        forced_onset_ranks: rank_onset,
        forced_continuation_ranks: rank_cont,
        forced_padding_ranks: rank_pad,
        word_gaps,
        model_chose,
        nudged,
        held_back,
    };
    // Give the other consumers their ears back before anything slow.
    drop(audible);
    drop(ear);
    mouth.finish()?;
    ledger.finish(out)?;
    Ok(report)
}

// ── odds and ends ──────────────────────────────────────────────────────────

/// Ask the scheduler for an interactive class. Without it the frame loop gets
/// parked on efficiency cores under load and its period swings several-fold.
fn set_interactive_qos() {
    #[cfg(target_os = "macos")]
    unsafe {
        extern "C" {
            fn pthread_set_qos_class_self_np(qos_class: u32, relative_priority: i32) -> i32;
        }
        let _ = pthread_set_qos_class_self_np(0x21, 0);
    }
}

/// Minimal streaming mono WAV receipt.
#[cfg(any(feature = "duplex", test))]
struct WavWriter {
    file: std::fs::File,
    data_bytes: u32,
}

#[cfg(any(feature = "duplex", test))]
impl WavWriter {
    fn create(path: PathBuf) -> Result<Self> {
        let mut file =
            std::fs::File::create(&path).with_context(|| format!("create {}", path.display()))?;
        file.write_all(&wav_header(0))?;
        Ok(Self {
            file,
            data_bytes: 0,
        })
    }

    fn write(&mut self, samples: &[f32]) -> Result<()> {
        let mut bytes = Vec::with_capacity(samples.len() * 2);
        for sample in samples {
            let value = (sample.clamp(-1.0, 1.0) * i16::MAX as f32).round() as i16;
            bytes.extend_from_slice(&value.to_le_bytes());
        }
        self.file.write_all(&bytes)?;
        self.data_bytes = self.data_bytes.saturating_add(bytes.len() as u32);
        Ok(())
    }
}

#[cfg(any(feature = "duplex", test))]
impl Drop for WavWriter {
    fn drop(&mut self) {
        use std::io::{Seek, SeekFrom};
        if self.file.seek(SeekFrom::Start(0)).is_ok() {
            let _ = self.file.write_all(&wav_header(self.data_bytes));
            let _ = self.file.flush();
        }
    }
}

#[cfg(any(feature = "duplex", test))]
fn wav_header(data_bytes: u32) -> Vec<u8> {
    let mut header = Vec::with_capacity(44);
    header.extend_from_slice(b"RIFF");
    header.extend_from_slice(&(36u32.saturating_add(data_bytes)).to_le_bytes());
    header.extend_from_slice(b"WAVEfmt ");
    header.extend_from_slice(&16u32.to_le_bytes());
    header.extend_from_slice(&1u16.to_le_bytes());
    header.extend_from_slice(&1u16.to_le_bytes());
    header.extend_from_slice(&SAMPLE_RATE.to_le_bytes());
    header.extend_from_slice(&(SAMPLE_RATE * 2).to_le_bytes());
    header.extend_from_slice(&2u16.to_le_bytes());
    header.extend_from_slice(&16u16.to_le_bytes());
    header.extend_from_slice(b"data");
    header.extend_from_slice(&data_bytes.to_le_bytes());
    header
}

#[cfg(test)]
#[path = "tests.rs"]
mod tests;