minutes-core 0.18.7

Core library for minutes — audio capture, transcription, and meeting memory
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
#[cfg(all(feature = "parakeet", unix))]
mod imp {
    use crate::config::Config;
    use crate::error::TranscribeError;
    use crate::transcribe::{
        parakeet_transcript_from_segments, write_wav_16k_mono, ParakeetCliSegment,
        ParakeetCliTranscript,
    };
    use serde::{Deserialize, Serialize};
    use std::collections::{BTreeSet, HashMap, VecDeque};
    use std::fmt;
    use std::fs;
    use std::io::{self, BufRead, BufReader, Read, Write};
    use std::os::unix::fs::PermissionsExt;
    use std::os::unix::net::UnixStream;
    use std::panic::{catch_unwind, AssertUnwindSafe};
    use std::path::{Path, PathBuf};
    use std::process::{Child, Command, Stdio};
    use std::sync::{Arc, Mutex, OnceLock};
    use std::thread;
    use std::time::{Duration, Instant};

    const LOG_PREFIX: &str = "parakeet-sidecar:";
    const STDERR_RING_CAPACITY: usize = 200;
    const DEFAULT_STARTUP_TIMEOUT: Duration = Duration::from_secs(30);
    const DEFAULT_CONNECT_TIMEOUT: Duration = Duration::from_secs(3);
    const DEFAULT_SHUTDOWN_TIMEOUT: Duration = Duration::from_secs(5);
    const DEFAULT_FP16_CRASH_WINDOW: Duration = Duration::from_secs(60);
    const DEFAULT_MAX_REQUEST_TIMEOUT: Duration = Duration::from_secs(30 * 60);
    const DEFAULT_MIN_REQUEST_TIMEOUT: Duration = Duration::from_secs(120);
    const HEALTHCHECK_WAV_FILENAME: &str = "parakeet-sidecar-healthcheck.wav";
    const FP16_BLACKLIST_FILENAME: &str = "parakeet-fp16-blacklist.json";
    const SIDECAR_SOCKET_PREFIX: &str = "parakeet-sidecar-";
    const SIDECAR_SOCKET_SUFFIX: &str = ".sock";
    const FP16_CRASH_SIGNATURES: &[&str] = &[
        "MPSGraph",
        "requires the same element type",
        "original module failed verification",
        "'mps.add' op requires the same element type for all operands and results",
    ];

    #[derive(Debug, Clone)]
    pub struct SidecarLaunchSpec {
        pub server_binary: PathBuf,
        pub version_binary: PathBuf,
        pub socket_path: PathBuf,
        pub model_path: PathBuf,
        pub vocab_path: PathBuf,
        pub model_id: String,
        pub use_gpu: bool,
        pub use_fp16: bool,
        pub vad_path: Option<PathBuf>,
    }

    impl SidecarLaunchSpec {
        fn matches(&self, other: &Self) -> bool {
            self.server_binary == other.server_binary
                && self.version_binary == other.version_binary
                && self.model_path == other.model_path
                && self.vocab_path == other.vocab_path
                && self.model_id == other.model_id
                && self.use_gpu == other.use_gpu
                && self.use_fp16 == other.use_fp16
                && self.vad_path == other.vad_path
        }

        fn display_key(&self) -> String {
            format!(
                "{}:{}:gpu={}:fp16={}",
                self.server_binary.display(),
                self.model_id,
                self.use_gpu,
                self.use_fp16
            )
        }
    }

    #[derive(Debug, Clone, Serialize)]
    pub struct SidecarRequest {
        pub request_id: String,
        pub audio_path: String,
        pub decoder: String,
        pub timestamps: bool,
        pub use_vad: bool,
        pub beam_width: i32,
        pub lm_path: String,
        pub lm_weight: f32,
        pub boost_phrases: Vec<String>,
        pub boost_score: f32,
    }

    #[derive(Debug, Clone)]
    pub struct SidecarTranscriptResult {
        pub transcript: ParakeetCliTranscript,
        pub elapsed_ms: u64,
        pub first_request_on_process: bool,
        pub effective_fp16: bool,
    }

    #[derive(Debug, Clone, Deserialize)]
    struct SidecarWordTimestamp {
        word: String,
        start: f64,
        end: f64,
        confidence: Option<f32>,
    }

    #[derive(Debug, Clone, Deserialize)]
    struct SidecarResponse {
        ok: bool,
        request_id: Option<String>,
        text: Option<String>,
        elapsed_ms: Option<u64>,
        word_timestamps: Option<Vec<SidecarWordTimestamp>>,
        error: Option<String>,
    }

    #[derive(Debug, Clone)]
    pub struct SidecarError {
        message: String,
    }

    impl SidecarError {
        fn new(message: impl Into<String>) -> Self {
            Self {
                message: message.into(),
            }
        }
    }

    impl fmt::Display for SidecarError {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            write!(f, "{}", self.message)
        }
    }

    impl std::error::Error for SidecarError {}

    #[derive(Debug, Clone, Copy)]
    struct SidecarTimeouts {
        startup: Duration,
        connect: Duration,
        shutdown: Duration,
        fp16_crash_window: Duration,
        max_request: Duration,
        min_request: Duration,
    }

    impl Default for SidecarTimeouts {
        fn default() -> Self {
            Self {
                startup: DEFAULT_STARTUP_TIMEOUT,
                connect: DEFAULT_CONNECT_TIMEOUT,
                shutdown: DEFAULT_SHUTDOWN_TIMEOUT,
                fp16_crash_window: DEFAULT_FP16_CRASH_WINDOW,
                max_request: DEFAULT_MAX_REQUEST_TIMEOUT,
                min_request: DEFAULT_MIN_REQUEST_TIMEOUT,
            }
        }
    }

    #[derive(Debug)]
    struct RunningSidecar {
        spec: SidecarLaunchSpec,
        child: Child,
        started_at: Instant,
        stderr_lines: Arc<Mutex<VecDeque<String>>>,
        stderr_thread: Option<thread::JoinHandle<()>>,
        requests_served: u64,
    }

    #[derive(Debug)]
    enum SidecarState {
        Disabled,
        Cold,
        Starting {
            spec: SidecarLaunchSpec,
            started_at: Instant,
        },
        Healthy(RunningSidecar),
        SubprocessOnly {
            spec: Option<SidecarLaunchSpec>,
            reason: String,
            since: Instant,
        },
        Stopping,
    }

    #[derive(Debug)]
    enum LaunchFailureAction {
        DowngradeToFp32,
        Fallback,
    }

    #[derive(Debug)]
    struct StartFailure {
        message: String,
        stderr_lines: Vec<String>,
        uptime: Duration,
    }

    #[derive(Debug, Clone)]
    struct SidecarManagerPaths {
        minutes_dir: PathBuf,
    }

    impl Default for SidecarManagerPaths {
        fn default() -> Self {
            Self {
                minutes_dir: Config::minutes_dir(),
            }
        }
    }

    impl SidecarManagerPaths {
        fn fp16_blacklist_path(&self) -> PathBuf {
            self.minutes_dir.join(FP16_BLACKLIST_FILENAME)
        }

        fn tmp_dir(&self) -> PathBuf {
            self.minutes_dir.join("tmp")
        }
    }

    #[derive(Debug, Default, Serialize, Deserialize)]
    struct PersistedFp16Blacklist {
        #[serde(default)]
        fingerprints: BTreeSet<String>,
    }

    #[derive(Debug)]
    pub struct ParakeetSidecarManager {
        state: SidecarState,
        request_counter: u64,
        timeouts: SidecarTimeouts,
        fp16_blacklist_fingerprints: BTreeSet<String>,
        version_cache: HashMap<PathBuf, String>,
        startup_housekeeping_done: bool,
        paths: SidecarManagerPaths,
    }

    impl Default for ParakeetSidecarManager {
        fn default() -> Self {
            Self {
                state: SidecarState::Cold,
                request_counter: 0,
                timeouts: SidecarTimeouts::default(),
                fp16_blacklist_fingerprints: BTreeSet::new(),
                version_cache: HashMap::new(),
                startup_housekeeping_done: false,
                paths: SidecarManagerPaths::default(),
            }
        }
    }

    impl Drop for ParakeetSidecarManager {
        fn drop(&mut self) {
            let _ = catch_unwind(AssertUnwindSafe(|| self.stop_running_sidecar()));
        }
    }

    impl ParakeetSidecarManager {
        #[cfg(test)]
        fn with_minutes_dir(minutes_dir: PathBuf) -> Self {
            let mut manager = Self::default();
            manager.paths = SidecarManagerPaths { minutes_dir };
            manager
        }

        pub fn transcribe(
            &mut self,
            spec: SidecarLaunchSpec,
            request: SidecarRequest,
            config: &Config,
            audio_duration_secs: f64,
        ) -> Result<SidecarTranscriptResult, SidecarError> {
            if let SidecarState::Disabled = self.state {
                return Err(SidecarError::new(format!(
                    "{LOG_PREFIX} disabled for this process"
                )));
            }

            self.ensure_started(spec.clone(), config)?;

            match self.transcribe_once(&request, config, audio_duration_secs) {
                Ok(result) => Ok(result),
                Err(first_error) => {
                    if is_non_restartable_request_error(&first_error) {
                        return Err(first_error);
                    }
                    let retry_spec = self.retry_spec_after_live_failure(&spec, &first_error);
                    tracing::warn!(
                        "{} live request failed — restarting once before fallback: {}",
                        LOG_PREFIX,
                        first_error
                    );
                    self.stop_running_sidecar();
                    self.state = SidecarState::Cold;
                    self.ensure_started(retry_spec.clone(), config)?;
                    match self.transcribe_once(&request, config, audio_duration_secs) {
                        Ok(result) => Ok(result),
                        Err(second_error) => {
                            let reason = format!(
                                "{LOG_PREFIX} request failed after one restart: {}",
                                second_error
                            );
                            self.state = SidecarState::SubprocessOnly {
                                spec: Some(spec),
                                reason: reason.clone(),
                                since: Instant::now(),
                            };
                            Err(SidecarError::new(reason))
                        }
                    }
                }
            }
        }

        pub fn ensure_started(
            &mut self,
            mut spec: SidecarLaunchSpec,
            config: &Config,
        ) -> Result<(), SidecarError> {
            self.run_startup_housekeeping(config);
            self.apply_sticky_fp16_downgrade(&mut spec);

            if let SidecarState::Healthy(running) = &mut self.state {
                if running.spec.matches(&spec) && process_is_alive(&mut running.child) {
                    return Ok(());
                }
            }
            if let SidecarState::SubprocessOnly {
                spec: Some(failed_spec),
                reason,
                since,
            } = &self.state
            {
                if failed_spec.matches(&spec) {
                    return Err(SidecarError::new(format!(
                        "{LOG_PREFIX} subprocess fallback remains active since {:?}: {}",
                        since.elapsed(),
                        reason
                    )));
                }
            }

            if matches!(self.state, SidecarState::Healthy(_)) {
                self.stop_running_sidecar();
            }

            let original_spec = spec.clone();
            let mut attempt_spec = spec;

            loop {
                self.state = SidecarState::Starting {
                    spec: attempt_spec.clone(),
                    started_at: Instant::now(),
                };
                match self.start_once(attempt_spec.clone(), config) {
                    Ok(running) => {
                        tracing::info!(
                            "{} healthy socket={} spec={}",
                            LOG_PREFIX,
                            running.spec.socket_path.display(),
                            running.spec.display_key()
                        );
                        self.state = SidecarState::Healthy(running);
                        return Ok(());
                    }
                    Err(failure) => {
                        let action = classify_start_failure(
                            &attempt_spec,
                            &failure,
                            self.timeouts.fp16_crash_window,
                        );
                        match action {
                            LaunchFailureAction::DowngradeToFp32 => {
                                self.remember_fp16_downgrade(&attempt_spec);
                                tracing::warn!(
                                    "{} detected fp16 startup crash signature — retrying in fp32",
                                    LOG_PREFIX
                                );
                                attempt_spec.use_fp16 = false;
                            }
                            LaunchFailureAction::Fallback => {
                                let reason = format!("{} {}", LOG_PREFIX, failure.message);
                                self.state = SidecarState::SubprocessOnly {
                                    spec: Some(original_spec),
                                    reason: reason.clone(),
                                    since: Instant::now(),
                                };
                                return Err(SidecarError::new(reason));
                            }
                        }
                    }
                }
            }
        }

        fn transcribe_once(
            &mut self,
            request: &SidecarRequest,
            config: &Config,
            audio_duration_secs: f64,
        ) -> Result<SidecarTranscriptResult, SidecarError> {
            let request_timeout = request_timeout(audio_duration_secs, self.timeouts);
            let (spec, first_request_on_process) = match &mut self.state {
                SidecarState::Healthy(running) => {
                    if !process_is_alive(&mut running.child) {
                        return Err(SidecarError::new(format!(
                            "{LOG_PREFIX} child exited before request dispatch"
                        )));
                    }
                    let first = running.requests_served == 0;
                    running.requests_served += 1;
                    (running.spec.clone(), first)
                }
                _ => {
                    return Err(SidecarError::new(format!(
                        "{LOG_PREFIX} requested transcription while not healthy"
                    )));
                }
            };

            let response_line = request_sidecar(
                &spec.socket_path,
                request,
                self.timeouts.connect,
                request_timeout,
            )
            .map_err(|error| SidecarError::new(format!("{LOG_PREFIX} {}", error)))?;

            let response: SidecarResponse =
                serde_json::from_str(&response_line).map_err(|error| {
                    SidecarError::new(format!("{LOG_PREFIX} malformed JSON response: {}", error))
                })?;
            let elapsed_ms = response.elapsed_ms.unwrap_or_default();

            if response.request_id.as_deref() != Some(request.request_id.as_str()) {
                return Err(SidecarError::new(format!(
                    "{LOG_PREFIX} mismatched request_id in sidecar response"
                )));
            }

            if !response.ok {
                return Err(SidecarError::new(format!(
                    "{LOG_PREFIX} server error: {}",
                    response.error.unwrap_or_else(|| "unknown error".into())
                )));
            }

            let transcript = response_to_parakeet_transcript(&response_line, response, config)
                .map_err(|error| SidecarError::new(format!("{LOG_PREFIX} {}", error)))?;

            Ok(SidecarTranscriptResult {
                transcript,
                elapsed_ms,
                first_request_on_process,
                effective_fp16: spec.use_fp16,
            })
        }

        fn start_once(
            &mut self,
            spec: SidecarLaunchSpec,
            _config: &Config,
        ) -> Result<RunningSidecar, StartFailure> {
            if let Some(parent) = spec.socket_path.parent() {
                fs::create_dir_all(parent).map_err(|error| StartFailure {
                    message: format!(
                        "could not create sidecar socket directory {}: {}",
                        parent.display(),
                        error
                    ),
                    stderr_lines: Vec::new(),
                    uptime: Duration::ZERO,
                })?;
            }
            let _ = fs::remove_file(&spec.socket_path);

            let mut command = Command::new(&spec.server_binary);
            command
                .arg(&spec.socket_path)
                .arg(&spec.model_path)
                .arg(&spec.vocab_path)
                .args(["--model", spec.model_id.as_str()])
                .stderr(Stdio::piped())
                .stdout(Stdio::null())
                .stdin(Stdio::null());
            if spec.use_gpu {
                command.arg("--gpu");
            }
            if spec.use_fp16 {
                command.arg("--fp16");
            }
            if let Some(vad_path) = &spec.vad_path {
                command.arg("--vad").arg(vad_path);
            }

            tracing::info!(
                "{} starting binary={} socket={} model={} gpu={} fp16={} vad={}",
                LOG_PREFIX,
                spec.server_binary.display(),
                spec.socket_path.display(),
                spec.model_id,
                spec.use_gpu,
                spec.use_fp16,
                spec.vad_path.is_some()
            );

            let started_at = Instant::now();
            let mut child = command.spawn().map_err(|error| StartFailure {
                message: format!(
                    "failed to spawn sidecar binary {}: {}",
                    spec.server_binary.display(),
                    error
                ),
                stderr_lines: Vec::new(),
                uptime: Duration::ZERO,
            })?;

            let stderr_lines = Arc::new(Mutex::new(VecDeque::with_capacity(STDERR_RING_CAPACITY)));
            let stderr_thread = child
                .stderr
                .take()
                .map(|stderr| spawn_stderr_drain(stderr, stderr_lines.clone()));

            let mut running = RunningSidecar {
                spec,
                child,
                started_at,
                stderr_lines,
                stderr_thread,
                requests_served: 0,
            };

            match self.health_check(&mut running) {
                Ok(()) => Ok(running),
                Err(message) => {
                    let stderr = running.recent_stderr_lines();
                    let uptime = running.started_at.elapsed();
                    self.terminate_running_sidecar(&mut running);
                    Err(StartFailure {
                        message,
                        stderr_lines: stderr,
                        uptime,
                    })
                }
            }
        }

        fn retry_spec_after_live_failure(
            &mut self,
            spec: &SidecarLaunchSpec,
            error: &SidecarError,
        ) -> SidecarLaunchSpec {
            let failure = match &self.state {
                SidecarState::Healthy(running) => StartFailure {
                    message: error.to_string(),
                    stderr_lines: running.recent_stderr_lines(),
                    uptime: running.started_at.elapsed(),
                },
                _ => {
                    return spec.clone();
                }
            };

            if matches!(
                classify_start_failure(spec, &failure, self.timeouts.fp16_crash_window),
                LaunchFailureAction::DowngradeToFp32
            ) {
                let mut downgraded = spec.clone();
                self.remember_fp16_downgrade(spec);
                downgraded.use_fp16 = false;
                tracing::warn!(
                    "{} live request crash matched fp16 MPSGraph signature — retrying in fp32",
                    LOG_PREFIX
                );
                downgraded
            } else {
                spec.clone()
            }
        }

        fn remember_fp16_downgrade(&mut self, spec: &SidecarLaunchSpec) {
            let fingerprint = self.machine_fp16_fingerprint(spec);
            if self.fp16_blacklist_fingerprints.insert(fingerprint.clone()) {
                let path = self.paths.fp16_blacklist_path();
                if let Err(error) =
                    write_persisted_fp16_blacklist(&path, &self.fp16_blacklist_fingerprints)
                {
                    tracing::warn!(
                        "{} failed to persist fp16 blacklist at {}: {}",
                        LOG_PREFIX,
                        path.display(),
                        error
                    );
                } else {
                    tracing::warn!(
                        "{} persisted fp16 blacklist fingerprint={}",
                        LOG_PREFIX,
                        fingerprint
                    );
                }
            }
        }

        fn apply_sticky_fp16_downgrade(&mut self, spec: &mut SidecarLaunchSpec) {
            let fingerprint = self.machine_fp16_fingerprint(spec);
            if spec.use_fp16 && self.fp16_blacklist_fingerprints.contains(&fingerprint) {
                tracing::warn!(
                    "{} reusing prior fp16 downgrade decision fingerprint={}",
                    LOG_PREFIX,
                    fingerprint
                );
                spec.use_fp16 = false;
            }
        }

        fn run_startup_housekeeping(&mut self, config: &Config) {
            if self.startup_housekeeping_done {
                return;
            }

            let blacklist_path = self.paths.fp16_blacklist_path();
            if config.transcription.parakeet_fp16_blacklist_reset {
                self.fp16_blacklist_fingerprints.clear();
                match fs::remove_file(&blacklist_path) {
                    Ok(()) => tracing::info!(
                        "{} cleared fp16 blacklist at {}",
                        LOG_PREFIX,
                        blacklist_path.display()
                    ),
                    Err(error) if error.kind() == io::ErrorKind::NotFound => {}
                    Err(error) => tracing::warn!(
                        "{} failed to clear fp16 blacklist at {}: {}",
                        LOG_PREFIX,
                        blacklist_path.display(),
                        error
                    ),
                }
            } else {
                match load_persisted_fp16_blacklist(&blacklist_path) {
                    Ok(fingerprints) => {
                        self.fp16_blacklist_fingerprints = fingerprints;
                    }
                    Err(error) => tracing::warn!(
                        "{} failed to load fp16 blacklist at {}: {}",
                        LOG_PREFIX,
                        blacklist_path.display(),
                        error
                    ),
                }
            }

            let tmp_dir = self.paths.tmp_dir();
            match sweep_stale_sidecar_sockets_in_dir(&tmp_dir) {
                Ok(removed) if removed > 0 => tracing::info!(
                    "{} removed {} stale sidecar sockets from {}",
                    LOG_PREFIX,
                    removed,
                    tmp_dir.display()
                ),
                Ok(_) => {}
                Err(error) => tracing::warn!(
                    "{} failed to sweep stale sidecar sockets in {}: {}",
                    LOG_PREFIX,
                    tmp_dir.display(),
                    error
                ),
            }

            self.startup_housekeeping_done = true;
        }

        fn machine_fp16_fingerprint(&mut self, spec: &SidecarLaunchSpec) -> String {
            let version = self
                .version_cache
                .entry(spec.version_binary.clone())
                .or_insert_with(|| query_binary_version(&spec.version_binary))
                .clone();
            format!(
                "{}-{}-{}",
                std::env::consts::OS,
                std::env::consts::ARCH,
                version
            )
        }

        fn health_check(&mut self, running: &mut RunningSidecar) -> Result<(), String> {
            let deadline = Instant::now() + self.timeouts.startup;
            while !running.spec.socket_path.exists() {
                if !process_is_alive(&mut running.child) {
                    let status = running.child.try_wait().ok().flatten();
                    return Err(format!(
                        "sidecar exited before socket became ready{}",
                        format_exit_status(status)
                    ));
                }
                if Instant::now() >= deadline {
                    return Err(format!(
                        "health check timed out waiting for socket {}",
                        running.spec.socket_path.display()
                    ));
                }
                thread::sleep(Duration::from_millis(50));
            }

            let request = SidecarRequest {
                request_id: "__minutes_healthcheck__".into(),
                audio_path: healthcheck_wav_path()
                    .map_err(|error| error.to_string())?
                    .display()
                    .to_string(),
                decoder: "tdt".into(),
                timestamps: false,
                use_vad: false,
                beam_width: 8,
                lm_path: String::new(),
                lm_weight: 0.5,
                boost_phrases: Vec::new(),
                boost_score: 0.0,
            };
            let response_line = request_sidecar(
                &running.spec.socket_path,
                &request,
                self.timeouts.connect,
                self.timeouts.startup,
            )
            .map_err(|error| format!("health check failed: {}", error))?;
            let response: SidecarResponse = serde_json::from_str(&response_line)
                .map_err(|error| format!("health check returned malformed JSON: {}", error))?;
            if !response.ok {
                return Err(format!(
                    "health check returned server error: {}",
                    response.error.unwrap_or_else(|| "unknown error".into())
                ));
            }
            Ok(())
        }

        pub fn shutdown(&mut self) {
            self.stop_running_sidecar();
            self.state = SidecarState::Disabled;
        }

        fn stop_running_sidecar(&mut self) {
            let previous = std::mem::replace(&mut self.state, SidecarState::Stopping);
            match previous {
                SidecarState::Healthy(mut running) => {
                    tracing::info!(
                        "{} stopping socket={} spec={}",
                        LOG_PREFIX,
                        running.spec.socket_path.display(),
                        running.spec.display_key()
                    );
                    self.terminate_running_sidecar(&mut running);
                    self.state = SidecarState::Cold;
                }
                SidecarState::Starting { spec, started_at } => {
                    tracing::debug!(
                        "{} abandoning in-flight start spec={} after {:?}",
                        LOG_PREFIX,
                        spec.display_key(),
                        started_at.elapsed()
                    );
                    self.state = SidecarState::Cold;
                }
                other => {
                    self.state = other;
                }
            }
        }

        fn terminate_running_sidecar(&self, running: &mut RunningSidecar) {
            let pid = running.child.id();
            #[allow(clippy::cast_possible_wrap)]
            unsafe {
                libc::kill(pid as i32, libc::SIGTERM);
            }

            let deadline = Instant::now() + self.timeouts.shutdown;
            loop {
                match running.child.try_wait() {
                    Ok(Some(_)) => break,
                    Ok(None) => {
                        if Instant::now() >= deadline {
                            break;
                        }
                        thread::sleep(Duration::from_millis(50));
                    }
                    Err(error) => {
                        tracing::warn!(
                            "{} failed to poll child {} during shutdown: {}",
                            LOG_PREFIX,
                            pid,
                            error
                        );
                        break;
                    }
                }
            }

            match running.child.try_wait() {
                Ok(Some(_)) => {}
                Ok(None) => {
                    if let Err(error) = running.child.kill() {
                        tracing::warn!("{} failed to SIGKILL child {}: {}", LOG_PREFIX, pid, error);
                    }
                    let _ = running.child.wait();
                }
                Err(error) => {
                    tracing::warn!(
                        "{} failed to poll child {} before forced kill: {}",
                        LOG_PREFIX,
                        pid,
                        error
                    );
                }
            }

            if let Some(handle) = running.stderr_thread.take() {
                let _ = handle.join();
            }
            let _ = fs::remove_file(&running.spec.socket_path);
        }
    }

    impl RunningSidecar {
        fn recent_stderr_lines(&self) -> Vec<String> {
            let guard = self
                .stderr_lines
                .lock()
                .unwrap_or_else(|poisoned| poisoned.into_inner());
            guard.iter().cloned().collect()
        }
    }

    fn spawn_stderr_drain(
        stderr: impl Read + Send + 'static,
        lines: Arc<Mutex<VecDeque<String>>>,
    ) -> thread::JoinHandle<()> {
        thread::Builder::new()
            .name("parakeet-sidecar-stderr".into())
            .spawn(move || {
                let reader = BufReader::new(stderr);
                for line in reader.lines() {
                    match line {
                        Ok(line) => {
                            tracing::warn!("{} {}", LOG_PREFIX, line);
                            let mut guard = lines
                                .lock()
                                .unwrap_or_else(|poisoned| poisoned.into_inner());
                            if guard.len() >= STDERR_RING_CAPACITY {
                                guard.pop_front();
                            }
                            guard.push_back(line);
                        }
                        Err(error) => {
                            tracing::warn!("{} failed reading child stderr: {}", LOG_PREFIX, error);
                            break;
                        }
                    }
                }
            })
            .unwrap_or_else(|error| {
                tracing::warn!("{} failed to spawn stderr reader: {}", LOG_PREFIX, error);
                thread::spawn(|| {})
            })
    }

    fn process_is_alive(child: &mut Child) -> bool {
        match child.try_wait() {
            Ok(Some(_)) => false,
            Ok(None) => true,
            Err(_) => false,
        }
    }

    fn query_binary_version(binary: &Path) -> String {
        let output = Command::new(binary)
            .arg("--version")
            .stderr(Stdio::piped())
            .stdout(Stdio::piped())
            .output();
        match output {
            Ok(output) if output.status.success() => {
                let stdout = String::from_utf8_lossy(&output.stdout);
                let stderr = String::from_utf8_lossy(&output.stderr);
                first_non_empty_line(stdout.lines())
                    .or_else(|| first_non_empty_line(stderr.lines()))
                    .unwrap_or("unknown")
                    .to_string()
            }
            Ok(_) | Err(_) => "unknown".to_string(),
        }
    }

    fn first_non_empty_line<'a>(lines: impl Iterator<Item = &'a str>) -> Option<&'a str> {
        lines.map(str::trim).find(|line| !line.is_empty())
    }

    fn format_exit_status(status: Option<std::process::ExitStatus>) -> String {
        match status {
            Some(status) => format!(" (status: {})", status),
            None => String::new(),
        }
    }

    fn load_persisted_fp16_blacklist(path: &Path) -> io::Result<BTreeSet<String>> {
        let raw = match fs::read_to_string(path) {
            Ok(raw) => raw,
            Err(error) if error.kind() == io::ErrorKind::NotFound => return Ok(BTreeSet::new()),
            Err(error) => return Err(error),
        };
        let persisted: PersistedFp16Blacklist =
            serde_json::from_str(&raw).map_err(io::Error::other)?;
        Ok(persisted.fingerprints)
    }

    fn write_persisted_fp16_blacklist(
        path: &Path,
        fingerprints: &BTreeSet<String>,
    ) -> io::Result<()> {
        if let Some(parent) = path.parent() {
            fs::create_dir_all(parent)?;
        }
        let raw = serde_json::to_string_pretty(&PersistedFp16Blacklist {
            fingerprints: fingerprints.clone(),
        })
        .map_err(io::Error::other)?;
        fs::write(path, raw)
    }

    fn sweep_stale_sidecar_sockets_in_dir(tmp_dir: &Path) -> io::Result<usize> {
        let entries = match fs::read_dir(tmp_dir) {
            Ok(entries) => entries,
            Err(error) if error.kind() == io::ErrorKind::NotFound => return Ok(0),
            Err(error) => return Err(error),
        };

        let mut removed = 0usize;
        for entry in entries {
            let entry = entry?;
            let path = entry.path();
            let Some(name) = path.file_name().and_then(|value| value.to_str()) else {
                continue;
            };
            let Some(pid) = parse_sidecar_socket_pid(name) else {
                continue;
            };

            match pid_is_alive(pid) {
                Ok(true) => continue,
                Ok(false) => match fs::remove_file(&path) {
                    Ok(()) => removed += 1,
                    Err(error) if error.kind() == io::ErrorKind::NotFound => {}
                    Err(error) => tracing::debug!(
                        "{} failed to remove stale socket {}: {}",
                        LOG_PREFIX,
                        path.display(),
                        error
                    ),
                },
                Err(error) => tracing::debug!(
                    "{} skipping socket {} because PID {} could not be checked: {}",
                    LOG_PREFIX,
                    path.display(),
                    pid,
                    error
                ),
            }
        }

        Ok(removed)
    }

    fn parse_sidecar_socket_pid(name: &str) -> Option<u32> {
        let trimmed = name
            .strip_prefix(SIDECAR_SOCKET_PREFIX)?
            .strip_suffix(SIDECAR_SOCKET_SUFFIX)?;
        let (pid, _) = trimmed.split_once('-')?;
        pid.parse().ok()
    }

    fn pid_is_alive(pid: u32) -> io::Result<bool> {
        #[allow(clippy::cast_possible_wrap)]
        let pid = pid as i32;
        let result = unsafe { libc::kill(pid, 0) };
        if result == 0 {
            return Ok(true);
        }

        let error = io::Error::last_os_error();
        match error.raw_os_error() {
            Some(libc::ESRCH) => Ok(false),
            Some(libc::EPERM) => Ok(true),
            _ => Err(error),
        }
    }

    fn request_timeout(audio_duration_secs: f64, timeouts: SidecarTimeouts) -> Duration {
        let scaled = Duration::from_secs_f64((audio_duration_secs * 2.0).max(0.0));
        scaled.clamp(timeouts.min_request, timeouts.max_request)
    }

    fn request_sidecar(
        socket_path: &Path,
        request: &SidecarRequest,
        connect_timeout: Duration,
        read_timeout: Duration,
    ) -> io::Result<String> {
        let mut stream = UnixStream::connect(socket_path)?;
        stream.set_read_timeout(Some(read_timeout))?;
        stream.set_write_timeout(Some(connect_timeout))?;

        let line = serde_json::to_string(request)
            .map_err(|error| io::Error::new(io::ErrorKind::InvalidInput, error.to_string()))?;
        write_complete_line(&mut stream, &line)?;
        read_line_response(&mut stream)
    }

    fn write_complete_line(stream: &mut UnixStream, line: &str) -> io::Result<()> {
        let mut bytes = line.as_bytes().to_vec();
        bytes.push(b'\n');
        let mut written = 0usize;
        while written < bytes.len() {
            let count = stream.write(&bytes[written..])?;
            if count == 0 {
                return Err(io::Error::new(
                    io::ErrorKind::WriteZero,
                    "sidecar socket closed during write",
                ));
            }
            written += count;
        }
        Ok(())
    }

    fn read_line_response(stream: &mut UnixStream) -> io::Result<String> {
        let mut buffer = Vec::new();
        let mut chunk = [0u8; 4096];
        loop {
            match stream.read(&mut chunk) {
                Ok(0) => {
                    return Err(io::Error::new(
                        io::ErrorKind::UnexpectedEof,
                        "sidecar socket closed before newline-delimited response",
                    ));
                }
                Ok(count) => {
                    buffer.extend_from_slice(&chunk[..count]);
                    if let Some(position) = buffer.iter().position(|byte| *byte == b'\n') {
                        let line = String::from_utf8_lossy(&buffer[..position]).to_string();
                        return Ok(line);
                    }
                }
                Err(error)
                    if error.kind() == io::ErrorKind::Interrupted
                        || error.kind() == io::ErrorKind::WouldBlock =>
                {
                    continue;
                }
                Err(error) => return Err(error),
            }
        }
    }

    fn response_to_parakeet_transcript(
        raw_line: &str,
        response: SidecarResponse,
        config: &Config,
    ) -> Result<ParakeetCliTranscript, TranscribeError> {
        let server_text = response.text.ok_or_else(|| {
            TranscribeError::ParakeetFailed("sidecar success response missing text".into())
        })?;
        let words = response.word_timestamps.ok_or_else(|| {
            TranscribeError::ParakeetFailed(
                "sidecar success response missing word_timestamps".into(),
            )
        })?;
        let segments: Vec<ParakeetCliSegment> = words
            .into_iter()
            .map(|word| ParakeetCliSegment {
                start_secs: word.start,
                end_secs: word.end,
                confidence: word.confidence,
                text: word.word,
            })
            .collect();

        let parsed = parakeet_transcript_from_segments(raw_line, segments, config)?;
        if parsed.transcript.trim().is_empty() && !server_text.trim().is_empty() {
            return Err(TranscribeError::EmptyTranscript(
                config.transcription.min_words,
            ));
        }
        Ok(parsed)
    }

    fn is_non_restartable_request_error(error: &SidecarError) -> bool {
        error.to_string().contains("transcription produced no text")
    }

    fn classify_start_failure(
        spec: &SidecarLaunchSpec,
        failure: &StartFailure,
        fp16_crash_window: Duration,
    ) -> LaunchFailureAction {
        if spec.use_fp16
            && failure.uptime <= fp16_crash_window
            && failure
                .stderr_lines
                .iter()
                .any(|line| FP16_CRASH_SIGNATURES.iter().any(|sig| line.contains(sig)))
        {
            LaunchFailureAction::DowngradeToFp32
        } else {
            LaunchFailureAction::Fallback
        }
    }

    fn healthcheck_wav_path() -> io::Result<PathBuf> {
        static HEALTHCHECK_PATH: OnceLock<PathBuf> = OnceLock::new();
        if let Some(path) = HEALTHCHECK_PATH.get() {
            if path.exists() {
                return Ok(path.clone());
            }
        }

        let tmp_dir = Config::minutes_dir().join("tmp");
        fs::create_dir_all(&tmp_dir)?;
        let path = tmp_dir.join(HEALTHCHECK_WAV_FILENAME);
        if !path.exists() {
            let silence = vec![0.0f32; 16000 / 4];
            write_wav_16k_mono(&path, &silence)
                .map_err(|error| io::Error::other(error.to_string()))?;
            let mut permissions = fs::metadata(&path)?.permissions();
            permissions.set_mode(0o600);
            let _ = fs::set_permissions(&path, permissions);
        }
        let _ = HEALTHCHECK_PATH.set(path.clone());
        Ok(path)
    }

    fn next_request_id(manager: &mut ParakeetSidecarManager) -> String {
        manager.request_counter += 1;
        format!("minutes-{}", manager.request_counter)
    }

    pub fn build_launch_spec(
        config: &Config,
        model_path: &Path,
        vocab_path: &Path,
        vad_path: Option<&Path>,
    ) -> Result<SidecarLaunchSpec, SidecarError> {
        let server_binary = resolve_server_binary(&config.transcription.parakeet_binary)
            .ok_or_else(|| {
                SidecarError::new(format!(
                    "{LOG_PREFIX} could not resolve example-server binary. \
                     Set MINUTES_PARAKEET_SERVER_BINARY or put example-server on PATH"
                ))
            })?;
        let version_binary = crate::parakeet::resolve_parakeet_binary(
            &config.transcription.parakeet_binary,
            crate::parakeet::ResolveParakeetBinaryMode::WarnAndFallback,
        )
        .unwrap_or_else(|_| server_binary.clone());

        let socket_path = Config::minutes_dir().join("tmp").join(format!(
            "parakeet-sidecar-{}-{}.sock",
            std::process::id(),
            config.transcription.parakeet_model
        ));

        Ok(SidecarLaunchSpec {
            server_binary,
            version_binary,
            socket_path,
            model_path: model_path.to_path_buf(),
            vocab_path: vocab_path.to_path_buf(),
            model_id: config.transcription.parakeet_model.clone(),
            use_gpu: cfg!(all(target_os = "macos", target_arch = "aarch64")),
            use_fp16: cfg!(all(target_os = "macos", target_arch = "aarch64"))
                && config.transcription.parakeet_fp16,
            vad_path: vad_path.map(Path::to_path_buf),
        })
    }

    pub fn build_request(
        manager: &mut ParakeetSidecarManager,
        config: &Config,
        wav_path: &Path,
        use_vad: bool,
        hints: &crate::transcribe::DecodeHints,
    ) -> Result<SidecarRequest, SidecarError> {
        let audio_path = wav_path.to_str().ok_or_else(|| {
            SidecarError::new(format!("{LOG_PREFIX} temp WAV path is not valid UTF-8"))
        })?;

        Ok(SidecarRequest {
            request_id: next_request_id(manager),
            audio_path: audio_path.to_string(),
            decoder: "tdt".into(),
            timestamps: true,
            use_vad,
            beam_width: 8,
            lm_path: String::new(),
            lm_weight: 0.5,
            boost_phrases: load_boost_phrases(config, hints),
            boost_score: config.transcription.parakeet_boost_score,
        })
    }

    fn load_boost_phrases(config: &Config, hints: &crate::transcribe::DecodeHints) -> Vec<String> {
        crate::transcribe::combined_parakeet_boost_phrases(config, hints)
    }

    pub fn resolve_server_binary(parakeet_binary: &str) -> Option<PathBuf> {
        if let Ok(explicit) = std::env::var("MINUTES_PARAKEET_SERVER_BINARY") {
            let explicit = PathBuf::from(explicit);
            if explicit.exists() {
                return Some(explicit);
            }
        }

        if let Ok(path_binary) = which::which("example-server") {
            return Some(path_binary);
        }

        if let Ok(resolved_parakeet) = crate::parakeet::resolve_parakeet_binary(
            parakeet_binary,
            crate::parakeet::ResolveParakeetBinaryMode::WarnAndFallback,
        ) {
            if let Some(parent) = resolved_parakeet.parent() {
                let sibling = parent.join("example-server");
                if sibling.exists() {
                    return Some(sibling);
                }
            }
        }

        None
    }

    fn global_manager() -> &'static Mutex<ParakeetSidecarManager> {
        static MANAGER: OnceLock<Mutex<ParakeetSidecarManager>> = OnceLock::new();
        MANAGER.get_or_init(|| Mutex::new(ParakeetSidecarManager::default()))
    }

    pub fn transcribe_via_global_sidecar(
        config: &Config,
        model_path: &Path,
        vocab_path: &Path,
        vad_path: Option<&Path>,
        wav_path: &Path,
        audio_duration_secs: f64,
        hints: &crate::transcribe::DecodeHints,
    ) -> Result<SidecarTranscriptResult, SidecarError> {
        let spec = build_launch_spec(config, model_path, vocab_path, vad_path)?;
        let manager = global_manager();
        let mut guard = manager
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner());
        let request = build_request(&mut guard, config, wav_path, vad_path.is_some(), hints)?;
        guard.transcribe(spec, request, config, audio_duration_secs)
    }

    pub fn warmup_global_sidecar(
        config: &Config,
        model_path: &Path,
        vocab_path: &Path,
        vad_path: Option<&Path>,
    ) -> Result<bool, SidecarError> {
        let spec = build_launch_spec(config, model_path, vocab_path, vad_path)?;
        let manager = global_manager();
        let mut guard = manager
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner());
        let already_healthy =
            matches!(&guard.state, SidecarState::Healthy(running) if running.spec.matches(&spec));
        guard.ensure_started(spec, config)?;
        Ok(!already_healthy)
    }

    pub fn shutdown_global_parakeet_sidecar() {
        let manager = global_manager();
        let mut guard = manager
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner());
        guard.shutdown();
    }

    #[cfg(test)]
    mod tests {
        use super::*;
        use tempfile::TempDir;

        fn make_temp_script(contents: &str) -> PathBuf {
            let base = std::env::temp_dir().join(format!(
                "minutes-sidecar-test-{}-{}",
                std::process::id(),
                std::time::SystemTime::now()
                    .duration_since(std::time::UNIX_EPOCH)
                    .unwrap_or_default()
                    .as_nanos()
            ));
            fs::create_dir_all(&base).unwrap();
            let path = base.join("fake-server.sh");
            fs::write(&path, contents).unwrap();
            let mut perms = fs::metadata(&path).unwrap().permissions();
            perms.set_mode(0o755);
            fs::set_permissions(&path, perms).unwrap();
            path
        }

        fn make_spec(binary: PathBuf, use_fp16: bool) -> SidecarLaunchSpec {
            SidecarLaunchSpec {
                server_binary: binary,
                version_binary: PathBuf::from("/tmp/parakeet"),
                socket_path: Config::minutes_dir().join("tmp").join(format!(
                    "test-{}-{}.sock",
                    std::process::id(),
                    use_fp16
                )),
                model_path: PathBuf::from("/tmp/model.safetensors"),
                vocab_path: PathBuf::from("/tmp/vocab.txt"),
                model_id: "tdt-600m".into(),
                use_gpu: true,
                use_fp16,
                vad_path: None,
            }
        }

        fn make_running_sidecar(spec: SidecarLaunchSpec, child: Child) -> RunningSidecar {
            RunningSidecar {
                spec,
                child,
                started_at: Instant::now(),
                stderr_lines: Arc::new(Mutex::new(VecDeque::with_capacity(STDERR_RING_CAPACITY))),
                stderr_thread: None,
                requests_served: 0,
            }
        }

        #[test]
        fn fp16_crash_signature_requests_downgrade() {
            let spec = make_spec(PathBuf::from("/tmp/example-server"), true);
            let failure = StartFailure {
                message: "startup failed".into(),
                stderr_lines: FP16_CRASH_SIGNATURES
                    .iter()
                    .map(|value| value.to_string())
                    .collect(),
                uptime: Duration::from_secs(5),
            };
            assert!(matches!(
                classify_start_failure(&spec, &failure, Duration::from_secs(60)),
                LaunchFailureAction::DowngradeToFp32
            ));
        }

        #[test]
        fn fp16_crash_path_retries_without_fp16() {
            let mut manager = ParakeetSidecarManager::default();
            manager.timeouts.startup = Duration::from_millis(100);
            let config = Config::default();
            let script = make_temp_script(
                "#!/bin/sh\n\
                 case \"$*\" in\n\
                   *--fp16*)\n\
                     echo \"MPSGraph\" >&2\n\
                     echo \"requires the same element type\" >&2\n\
                     echo \"original module failed verification\" >&2\n\
                     exit 1\n\
                     ;;\n\
                   *)\n\
                     sleep 2\n\
                     ;;\n\
                 esac\n",
            );
            let spec = make_spec(script, true);

            let result = manager.ensure_started(spec, &config);
            assert!(result.is_err());
            let error_text = result.unwrap_err().to_string();
            assert!(
                error_text.contains("health check timed out"),
                "expected downgrade retry to reach the second launch timeout, got: {}",
                error_text
            );
            assert!(matches!(manager.state, SidecarState::SubprocessOnly { .. }));
        }

        #[test]
        fn spawn_failure_transitions_to_subprocess_only() {
            let mut manager = ParakeetSidecarManager::default();
            manager.timeouts.startup = Duration::from_millis(50);
            let config = Config::default();
            let spec = make_spec(PathBuf::from("/definitely/missing/example-server"), false);

            let result = manager.ensure_started(spec.clone(), &config);
            assert!(result.is_err());
            assert!(matches!(manager.state, SidecarState::SubprocessOnly { .. }));
        }

        #[test]
        fn health_check_timeout_transitions_to_subprocess_only() {
            let mut manager = ParakeetSidecarManager::default();
            manager.timeouts.startup = Duration::from_millis(100);
            let config = Config::default();
            let script = make_temp_script("#!/bin/sh\nsleep 2\n");
            let spec = make_spec(script, false);

            let result = manager.ensure_started(spec, &config);
            assert!(result.is_err());
            assert!(matches!(manager.state, SidecarState::SubprocessOnly { .. }));
        }

        #[test]
        fn line_reader_handles_partial_json_frames() {
            let dir = TempDir::new().unwrap();
            let socket = dir.path().join("partial.sock");
            let listener = std::os::unix::net::UnixListener::bind(&socket).unwrap();
            let handle = thread::spawn(move || {
                let (mut stream, _) = listener.accept().unwrap();
                let mut buf = [0u8; 512];
                let _ = stream.read(&mut buf).unwrap();
                stream.write_all(br#"{"ok":true,"request_id":"x""#).unwrap();
                thread::sleep(Duration::from_millis(10));
                stream
                    .write_all(br#","text":"ok","elapsed_ms":1,"word_timestamps":[{"word":"ok","start":0.0,"end":0.1,"confidence":1.0}]}"#)
                    .unwrap();
                stream.write_all(b"\n").unwrap();
            });

            let request = SidecarRequest {
                request_id: "x".into(),
                audio_path: "/tmp/test.wav".into(),
                decoder: "tdt".into(),
                timestamps: true,
                use_vad: false,
                beam_width: 8,
                lm_path: String::new(),
                lm_weight: 0.5,
                boost_phrases: Vec::new(),
                boost_score: 0.0,
            };
            let line = request_sidecar(
                &socket,
                &request,
                Duration::from_secs(1),
                Duration::from_secs(1),
            )
            .unwrap();
            handle.join().unwrap();
            assert!(line.contains("\"request_id\":\"x\""));
            assert!(line.contains("\"word_timestamps\""));
        }

        #[test]
        fn manager_drop_reaps_running_child_after_panic() {
            let minutes_dir = TempDir::new().unwrap();
            let script = make_temp_script("#!/bin/sh\nsleep 30\n");
            let child = Command::new(&script).spawn().unwrap();
            let pid = child.id();

            let result = std::panic::catch_unwind(AssertUnwindSafe(|| {
                let mut manager =
                    ParakeetSidecarManager::with_minutes_dir(minutes_dir.path().join(".minutes"));
                manager.state = SidecarState::Healthy(make_running_sidecar(
                    make_spec(script.clone(), false),
                    child,
                ));
                panic!("boom");
            }));

            assert!(result.is_err());
            thread::sleep(Duration::from_millis(100));
            assert!(!pid_is_alive(pid).unwrap());
        }

        #[test]
        fn fp16_blacklist_persists_across_managers() {
            let minutes_dir = TempDir::new().unwrap();
            let version_script = make_temp_script("#!/bin/sh\necho parakeet 9.9.9\n");
            let blacklist_path = minutes_dir
                .path()
                .join(".minutes")
                .join(FP16_BLACKLIST_FILENAME);

            let mut manager =
                ParakeetSidecarManager::with_minutes_dir(minutes_dir.path().join(".minutes"));
            let mut spec = make_spec(PathBuf::from("/tmp/example-server"), true);
            spec.version_binary = version_script.clone();

            manager.remember_fp16_downgrade(&spec);
            assert!(blacklist_path.exists());

            let fingerprints = load_persisted_fp16_blacklist(&blacklist_path).unwrap();
            assert_eq!(fingerprints.len(), 1);

            let mut next_manager =
                ParakeetSidecarManager::with_minutes_dir(minutes_dir.path().join(".minutes"));
            next_manager.fp16_blacklist_fingerprints =
                load_persisted_fp16_blacklist(&blacklist_path).unwrap();
            let mut next_spec = spec.clone();
            next_manager.apply_sticky_fp16_downgrade(&mut next_spec);
            assert!(!next_spec.use_fp16);
        }

        #[test]
        fn stale_socket_sweep_removes_dead_pids_but_keeps_live_ones() {
            let tmp_root = TempDir::new().unwrap();
            let tmp_dir = tmp_root.path().join("tmp");
            fs::create_dir_all(&tmp_dir).unwrap();

            let dead_path = tmp_dir.join("parakeet-sidecar-999999-dead.sock");
            let live_path =
                tmp_dir.join(format!("parakeet-sidecar-{}-live.sock", std::process::id()));
            fs::write(&dead_path, "").unwrap();
            fs::write(&live_path, "").unwrap();

            let removed = sweep_stale_sidecar_sockets_in_dir(&tmp_dir).unwrap();
            assert_eq!(removed, 1);
            assert!(!dead_path.exists());
            assert!(live_path.exists());
        }
    }
}

#[cfg(all(feature = "parakeet", unix))]
pub use imp::*;

#[cfg(not(all(feature = "parakeet", unix)))]
mod imp_stub {
    use crate::config::Config;
    use std::path::Path;

    #[derive(Debug, Clone)]
    pub struct SidecarLaunchSpec;

    #[derive(Debug, Clone)]
    pub struct SidecarRequest;

    // Stub shape mirrors the real `SidecarTranscriptResult` from the unix
    // imp module so non-unix builds (e.g. Windows with `--features parakeet`)
    // type-check the match arm at the call site. The stub
    // `transcribe_via_global_sidecar` always returns Err, so this branch is
    // unreachable at runtime on these platforms.
    #[cfg(feature = "parakeet")]
    #[derive(Debug, Clone)]
    pub struct SidecarTranscriptResult {
        pub transcript: crate::transcribe::ParakeetCliTranscript,
        pub elapsed_ms: u64,
        pub first_request_on_process: bool,
        pub effective_fp16: bool,
    }

    #[cfg(not(feature = "parakeet"))]
    #[derive(Debug, Clone)]
    pub struct SidecarTranscriptResult;

    #[derive(Debug, Clone)]
    pub struct SidecarError {
        message: String,
    }

    impl std::fmt::Display for SidecarError {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            write!(f, "{}", self.message)
        }
    }

    impl std::error::Error for SidecarError {}

    pub fn build_launch_spec(
        _config: &Config,
        _model_path: &Path,
        _vocab_path: &Path,
        _vad_path: Option<&Path>,
    ) -> Result<SidecarLaunchSpec, SidecarError> {
        Err(SidecarError {
            message: "parakeet-sidecar: unavailable on this build".into(),
        })
    }

    pub fn resolve_server_binary(_parakeet_binary: &str) -> Option<std::path::PathBuf> {
        None
    }

    pub fn transcribe_via_global_sidecar(
        _config: &Config,
        _model_path: &Path,
        _vocab_path: &Path,
        _vad_path: Option<&Path>,
        _wav_path: &Path,
        _audio_duration_secs: f64,
        _hints: &crate::transcribe::DecodeHints,
    ) -> Result<SidecarTranscriptResult, SidecarError> {
        Err(SidecarError {
            message: "parakeet-sidecar: unavailable on this build".into(),
        })
    }

    pub fn warmup_global_sidecar(
        _config: &Config,
        _model_path: &Path,
        _vocab_path: &Path,
        _vad_path: Option<&Path>,
    ) -> Result<bool, SidecarError> {
        Err(SidecarError {
            message: "parakeet-sidecar: unavailable on this build".into(),
        })
    }

    pub fn shutdown_global_parakeet_sidecar() {}
}

#[cfg(not(all(feature = "parakeet", unix)))]
pub use imp_stub::*;