leindex 1.9.0

LeIndex MCP and semantic code search engine for AI tools and large codebases
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
// Worker client for delegating ONNX inference to the leindex-embed process
//
// VAL-CPHASE-002: The main crate no longer owns ONNX runtime deps directly.
// This client communicates with the leindex-embed worker over local IPC.
//
// VAL-CPHASE-016: The client writes worker output into destination embedding
// storage via the flat EmbedResponse buffer, avoiding a nested Vec<Vec<f32>>
// heap mirror in the main process.
//
// VAL-CPHASE-017: On worker failure, the client retries once before falling back.
// VAL-CPHASE-018: After retry failure, only the affected batch falls back to TF-IDF.
// VAL-CPHASE-019: Fallback emits an actionable warning naming the batch and error.
// VAL-CPHASE-020: Worker failure does not crash the main daemon.
// VAL-CPHASE-021: A fresh worker can be spawned after a fallback episode.

use std::fmt;
use std::io::{BufRead, BufReader, Read, Write};
use std::path::{Path, PathBuf};
use std::process::{Child, Command, Stdio};
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::mpsc;
use std::sync::{Arc, Mutex, OnceLock};
use std::thread;
use std::time::{Duration, Instant};

#[cfg(unix)]
use std::os::fd::AsRawFd;
#[cfg(unix)]
use std::os::unix::net::UnixStream;
#[cfg(unix)]
use std::os::unix::process::CommandExt;

use leindex_embed::protocol::{
    self, BatchId, EmbedRequest, EmbedResponse, ErrorKind, Frame, HealthResponse, MsgType,
    RerankDocument, RerankRequest, RerankResponse, Response, WorkerError, WorkerState,
};

#[path = "client_config.rs"]
mod client_config;
use client_config::*;
pub use client_config::{
    ClientError, EmbedResult, EmbeddingClient, WorkerAvailability, migraphx_cache_path,
    prune_stale_migraphx_profiles,
};

/// Classify a `std::io::Error` as one that indicates the worker process has
/// died (closed the connection or crashed).
///
/// VAL-DEADWORKER-001: When the worker process dies, `read_exact` on the
/// `UnixStream` returns one of these error kinds. This is a deterministic
/// transport-level signal, not an arbitrary wall-clock timeout.
fn is_worker_dead_io_error(e: &std::io::Error) -> bool {
    matches!(
        e.kind(),
        std::io::ErrorKind::UnexpectedEof
            | std::io::ErrorKind::BrokenPipe
            | std::io::ErrorKind::ConnectionReset
            | std::io::ErrorKind::ConnectionAborted
    )
}

/// Read a response frame from the worker.
///
/// This is the core I/O routine used by the persistent reader thread.
/// It reads the 4-byte length prefix followed by the payload, enforcing
/// the max frame size guard to prevent excessive allocations.
///
/// VAL-DEADWORKER-001: On EOF/EPIPE (worker process died), the error is
/// classified as `ClientError::WorkerDied` so the caller can kill the stale
/// daemon and spawn a replacement, rather than being masked as a generic
/// IPC error.
fn read_frame<R: Read>(reader: &mut R) -> Result<Vec<u8>, ClientError> {
    // Read response length (4 bytes, little-endian)
    let mut len_buf = [0u8; 4];
    match reader.read_exact(&mut len_buf) {
        Ok(()) => {}
        Err(e) if is_worker_dead_io_error(&e) => {
            return Err(ClientError::WorkerDied {
                message: format!("EOF/EPIPE reading frame length: {}", e),
            });
        }
        Err(e) => {
            return Err(ClientError::Ipc(format!(
                "failed to read frame length: {}",
                e
            )));
        }
    }

    let payload_len = u32::from_le_bytes(len_buf);

    // Guard against oversized responses to prevent excessive allocations.
    if payload_len > MAX_RESPONSE_FRAME_SIZE {
        return Err(ClientError::Ipc(format!(
            "response frame too large: {} bytes (max: {} bytes)",
            payload_len, MAX_RESPONSE_FRAME_SIZE
        )));
    }

    let payload_len = payload_len as usize;
    let mut frame_buf = vec![0u8; payload_len];
    match reader.read_exact(&mut frame_buf) {
        Ok(()) => Ok(frame_buf),
        Err(e) if is_worker_dead_io_error(&e) => Err(ClientError::WorkerDied {
            message: format!("EOF/EPIPE reading frame payload: {}", e),
        }),
        Err(e) => Err(ClientError::Ipc(format!(
            "failed to read frame payload: {}",
            e
        ))),
    }
}

/// Monotonic batch ID counter for correlating requests.
static BATCH_COUNTER: AtomicU64 = AtomicU64::new(1);

impl EmbeddingClient {
    /// Observe worker lifecycle without spawning a process or waiting for
    /// model initialization. Socket health is the source of truth for daemon
    /// workers; the pipe startup report remains the compatibility boundary.
    pub fn availability(&self) -> WorkerAvailability {
        let report = self
            .last_startup_report
            .lock()
            .ok()
            .and_then(|report| report.clone());
        if !self.use_daemon {
            return match report {
                Some(report) if report.contains("status=available") => WorkerAvailability::Ready,
                Some(report) if report.contains("status=unavailable") => {
                    WorkerAvailability::Failed(worker_health_snapshot(
                        WorkerState::Failed,
                        parse_startup_report_provider(&report),
                        None,
                        Some(report),
                    ))
                }
                _ => WorkerAvailability::Absent,
            };
        }

        #[cfg(unix)]
        {
            // VAL-DAEMON-003: Use cached config so leindex.toml is not re-read.
            let config = self.cached_config();
            let provider = std::env::var("LEINDEX_WORKER_EXECUTION_PROVIDER")
                .ok()
                .or(config.execution_provider.clone());
            let model = std::env::var("LEINDEX_WORKER_MODEL")
                .ok()
                .or(config.model_name.clone());
            let Some(socket_path) = daemon_socket_path(provider.as_deref(), model.as_deref())
            else {
                return WorkerAvailability::Absent;
            };
            let status_path = daemon_status_path(provider.as_deref(), model.as_deref());
            let state_hint = status_path.as_deref().and_then(status_state);
            let pid_path = daemon_pid_path(provider.as_deref(), model.as_deref());

            if state_hint.is_some()
                && pid_path
                    .as_deref()
                    .is_some_and(|path| !daemon_pid_alive(path))
            {
                cleanup_daemon_paths(&socket_path);
                return WorkerAvailability::Absent;
            }

            if !socket_path.exists() {
                if state_hint.is_some() {
                    let _ = status_path.as_deref().map(std::fs::remove_file);
                    let _ = daemon_pid_path(provider.as_deref(), model.as_deref())
                        .as_deref()
                        .map(std::fs::remove_file);
                }
                return WorkerAvailability::Absent;
            }

            // VAL-DAEMON-005: Fast path. When the status file says "ready"
            // and the PID is alive, return Ready immediately without a
            // socket health probe. This eliminates a Unix socket connect +
            // write + read cycle on every availability() poll.
            if state_hint == Some(WorkerState::Ready)
                && pid_path.as_deref().is_some_and(daemon_pid_alive)
            {
                return WorkerAvailability::Ready;
            }

            match probe_daemon_health(&socket_path) {
                Ok(health) => match health.state {
                    WorkerState::Ready => WorkerAvailability::Ready,
                    WorkerState::Initializing => WorkerAvailability::Initializing(health),
                    WorkerState::Failed => WorkerAvailability::Failed(health),
                },
                Err(ClientError::Worker(error)) if error.kind == ErrorKind::Initializing => {
                    WorkerAvailability::Initializing(worker_health_snapshot(
                        WorkerState::Initializing,
                        provider,
                        model,
                        Some(error.message),
                    ))
                }
                Err(error) => {
                    // A status hint still gives callers an immediate, useful
                    // answer when a health connection is momentarily busy.
                    match state_hint {
                        Some(WorkerState::Initializing) => {
                            WorkerAvailability::Initializing(worker_health_snapshot(
                                WorkerState::Initializing,
                                provider,
                                model,
                                Some(error.to_string()),
                            ))
                        }
                        Some(WorkerState::Failed) => {
                            WorkerAvailability::Failed(worker_health_snapshot(
                                WorkerState::Failed,
                                provider,
                                model,
                                Some(error.to_string()),
                            ))
                        }
                        Some(WorkerState::Ready) => WorkerAvailability::Ready,
                        None => WorkerAvailability::Absent,
                    }
                }
            }
        }
        #[cfg(not(unix))]
        {
            WorkerAvailability::Absent
        }
    }

    /// Return true only after the worker has emitted a successful startup
    /// handshake. This is a non-blocking observation; it never spawns a
    /// process or waits for ORT/model initialization.
    pub fn is_ready(&self) -> bool {
        self.availability().is_ready()
    }

    /// Wait briefly for the stderr mirror to publish the startup report.
    pub fn wait_for_active_execution_provider(&self, timeout: Duration) -> Option<String> {
        let deadline = Instant::now() + timeout;
        loop {
            if let Some(provider) = self.active_execution_provider() {
                return Some(provider);
            }
            if Instant::now() >= deadline {
                return None;
            }
            thread::sleep(Duration::from_millis(5));
        }
    }

    /// Allocate a new unique batch ID.
    fn next_batch_id() -> BatchId {
        BatchId::new(BATCH_COUNTER.fetch_add(1, Ordering::Relaxed))
    }

    /// Ensure the worker is running, spawning it if necessary.
    fn ensure_worker(&self) -> Result<(), ClientError> {
        let mut guard = self
            .worker
            .lock()
            .map_err(|e| ClientError::Ipc(format!("failed to lock worker handle: {}", e)))?;

        if guard.is_some() {
            return Ok(());
        }

        self.spawn_worker(&mut guard)
    }

    fn ensure_worker_ready(&self) -> Result<(), ClientError> {
        let started = Instant::now();

        // If the worker cannot be spawned or connected (daemon timeout, missing
        // binary, IPC failure), treat neural as unavailable. Indexing proceeds
        // with core TF-IDF/PDG results (decision #6: terminal neural failure
        // preserves useful TF-IDF/PDG results).
        if let Err(error) = self.ensure_worker() {
            tracing::warn!(
                error = %error,
                "neural worker unavailable; proceeding with core TF-IDF/PDG results"
            );
            return Ok(());
        }

        loop {
            match self.availability() {
                WorkerAvailability::Ready => return Ok(()),
                WorkerAvailability::Initializing(health) if self.use_daemon => {
                    // The daemon binds its socket before ORT/model loading.
                    // Wait for the explicit lifecycle transition so the
                    // first request uses neural embeddings instead of being
                    // silently downgraded merely because startup is cold.
                    if started.elapsed() >= DAEMON_READY_MAX_WAIT {
                        tracing::warn!(
                            phase = %health.phase,
                            elapsed_secs = started.elapsed().as_secs(),
                            "neural worker did not become ready within {}s; \
                             falling back to core TF-IDF/PDG results",
                            DAEMON_READY_MAX_WAIT.as_secs()
                        );
                        return Ok(());
                    }
                    tracing::debug!(
                        phase = %health.phase,
                        "waiting for neural worker readiness"
                    );
                    thread::sleep(DAEMON_READINESS_POLL);
                }
                WorkerAvailability::Initializing(_) => {
                    // Pipe mode initializes before publishing its handle, so
                    // the stderr startup report can legitimately arrive after
                    // this check. The framed request below blocks until the
                    // worker has finished initialization.
                    return Ok(());
                }
                WorkerAvailability::Failed(health) => {
                    return Err(ClientError::Worker(WorkerError {
                        kind: ErrorKind::OnnxRuntime,
                        message: health
                            .error
                            .unwrap_or_else(|| "worker neural runtime is unavailable".to_string()),
                    }));
                }
                WorkerAvailability::Absent if !self.use_daemon => {
                    // Pipe mode has no external health endpoint. A missing
                    // startup report is normal until the worker writes stderr.
                    return Ok(());
                }
                WorkerAvailability::Absent => {
                    return Err(ClientError::Worker(WorkerError {
                        kind: ErrorKind::OnnxRuntime,
                        message: "worker daemon is absent".to_string(),
                    }));
                }
            }
        }
    }

    /// Spawn a new worker process into the given guard slot.
    fn spawn_worker(
        &self,
        guard: &mut std::sync::MutexGuard<'_, Option<WorkerHandle>>,
    ) -> Result<(), ClientError> {
        let worker_path = resolve_worker_binary().map_err(|e| {
            ClientError::SpawnFailed(format!("failed to resolve worker binary: {}", e))
        })?;
        // VAL-DAEMON-003: Use cached config so leindex.toml is not re-read.
        let config_env = self.cached_config();
        let configured_provider = std::env::var("LEINDEX_WORKER_EXECUTION_PROVIDER")
            .ok()
            .or_else(|| config_env.execution_provider.clone());
        #[cfg(unix)]
        if self.use_daemon {
            let configured_model = std::env::var("LEINDEX_WORKER_MODEL")
                .ok()
                .or_else(|| config_env.model_name.clone());
            if let Some(handle) = self.spawn_or_connect_daemon(
                &worker_path,
                config_env,
                configured_provider.as_deref(),
                configured_model.as_deref(),
            )? {
                **guard = Some(handle);
                return Ok(());
            }
        }

        **guard = Some(self.spawn_pipe_worker(
            &worker_path,
            config_env,
            configured_provider.as_deref(),
        )?);

        Ok(())
    }

    fn configure_worker_command(
        cmd: &mut Command,
        config_env: &WorkerConfigEnv,
        configured_provider: Option<&str>,
    ) {
        // VAL-ONNX-002: Explicitly pass key env vars to the worker process so
        // it can locate model files and select the correct execution provider.
        if let Ok(model_path) = std::env::var("LEINDEX_MODEL_PATH") {
            cmd.env("LEINDEX_MODEL_PATH", &model_path);
        }
        if let Ok(provider) = std::env::var("LEINDEX_WORKER_EXECUTION_PROVIDER") {
            cmd.env("LEINDEX_WORKER_EXECUTION_PROVIDER", &provider);
        } else if let Some(provider) = &config_env.execution_provider {
            cmd.env("LEINDEX_WORKER_EXECUTION_PROVIDER", provider);
        }
        if let Ok(model_name) = std::env::var("LEINDEX_WORKER_MODEL") {
            cmd.env("LEINDEX_WORKER_MODEL", &model_name);
        } else if let Some(model_name) = &config_env.model_name {
            cmd.env("LEINDEX_WORKER_MODEL", model_name);
        }
        if matches!(
            configured_provider,
            Some("migraphx" | "rocm" | "auto") | None
        ) && std::env::var_os("ORT_MIGRAPHX_MODEL_CACHE_PATH").is_none()
        {
            if let Some(cache_path) = migraphx_model_cache_path(config_env.model_name.as_deref()) {
                if let Err(e) = std::fs::create_dir_all(&cache_path) {
                    tracing::warn!(
                        path = %cache_path.display(),
                        error = %e,
                        "failed to create MIGraphX model cache directory"
                    );
                } else {
                    cmd.env("ORT_MIGRAPHX_MODEL_CACHE_PATH", cache_path);
                }
            }
        }
        // VAL-SETUP-020/VAL-ORT-006: When ORT_DYLIB_PATH is not already in the
        // ambient environment, propagate the path recorded in
        // `~/.leindex/config/leindex.toml` so the worker reliably loads the
        // ORT build chosen during `leindex setup`. This keeps the discovery
        // chain consistent across both the interactive setup flow (which
        // installs ORT via pip and remembers the discovered `.so`) and the
        // plain-spawn path used by searches.
        // An empty value is treated as unset so the config fallback fires.
        let ort_dylib_unset = match std::env::var_os("ORT_DYLIB_PATH") {
            None => true,
            Some(v) => v.is_empty(),
        };
        if ort_dylib_unset {
            if let Some(path) = &config_env.ort_dylib_path {
                cmd.env("ORT_DYLIB_PATH", path);
            }
        }
    }

    fn spawn_pipe_worker(
        &self,
        worker_path: &Path,
        config_env: &WorkerConfigEnv,
        configured_provider: Option<&str>,
    ) -> Result<WorkerHandle, ClientError> {
        let mut cmd = Command::new(worker_path);
        Self::configure_worker_command(&mut cmd, config_env, configured_provider);
        let mut child = cmd
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            // Pipe stderr so startup_report can be captured while still being
            // mirrored to the parent stderr for operator visibility.
            .stderr(Stdio::piped())
            .spawn()
            .map_err(|e| ClientError::SpawnFailed(e.to_string()))?;

        let writer = child
            .stdin
            .take()
            .ok_or_else(|| ClientError::SpawnFailed("failed to open worker stdin".to_string()))?;
        let reader = child
            .stdout
            .take()
            .ok_or_else(|| ClientError::SpawnFailed("failed to open worker stdout".to_string()))?;
        let stderr = child
            .stderr
            .take()
            .ok_or_else(|| ClientError::SpawnFailed("failed to open worker stderr".to_string()))?;

        let (read_thread, read_request_tx) = Self::spawn_reader_thread(reader);
        let stderr_thread =
            Self::spawn_stderr_thread(stderr, Arc::clone(&self.last_startup_report));

        Ok(WorkerHandle {
            child: Some(child),
            writer: Some(WorkerWriter::Pipe(writer)),
            read_thread,
            read_request_tx,
            stderr_thread: Some(stderr_thread),
            persistent: false,
            socket_path: None,
        })
    }

    #[cfg(unix)]
    fn spawn_or_connect_daemon(
        &self,
        worker_path: &Path,
        config_env: &WorkerConfigEnv,
        configured_provider: Option<&str>,
        configured_model: Option<&str>,
    ) -> Result<Option<WorkerHandle>, ClientError> {
        let Some(socket_path) = daemon_socket_path(configured_provider, configured_model) else {
            return Ok(None);
        };

        if let Some(handle) = self.connect_daemon_when_ready(&socket_path, None)? {
            return Ok(Some(handle));
        }

        if let Some(parent) = socket_path.parent() {
            std::fs::create_dir_all(parent).map_err(|e| {
                ClientError::SpawnFailed(format!(
                    "failed to create worker socket dir {}: {}",
                    parent.display(),
                    e
                ))
            })?;
        }

        let lock_path = socket_path.with_extension("lock");
        let _spawn_lock =
            DaemonSpawnLock::acquire(&lock_path, Duration::from_secs(DAEMON_LOCK_WAIT_SECS))?;
        if let Some(handle) = self.connect_daemon_when_ready(&socket_path, None)? {
            return Ok(Some(handle));
        }

        Self::spawn_locked_daemon(worker_path, config_env, configured_provider, socket_path)
            .map(Some)
    }

    #[cfg(unix)]
    fn spawn_locked_daemon(
        worker_path: &Path,
        config_env: &WorkerConfigEnv,
        configured_provider: Option<&str>,
        socket_path: PathBuf,
    ) -> Result<WorkerHandle, ClientError> {
        cleanup_daemon_paths(&socket_path);

        let mut cmd = Command::new(worker_path);
        Self::configure_worker_command(&mut cmd, config_env, configured_provider);
        unsafe {
            cmd.pre_exec(|| {
                if libc::setsid() < 0 {
                    return Err(std::io::Error::last_os_error());
                }
                Ok(())
            });
        }
        let stderr = Self::daemon_stderr();
        let mut child = cmd
            .arg("--socket")
            .arg(&socket_path)
            .stdin(Stdio::null())
            .stdout(Stdio::null())
            .stderr(stderr)
            .spawn()
            .map_err(|e| ClientError::SpawnFailed(e.to_string()))?;

        let deadline = Instant::now() + Duration::from_secs(DAEMON_BIND_WAIT_SECS);
        loop {
            match UnixStream::connect(&socket_path) {
                Ok(stream) => {
                    // The daemon accepts connections before model loading so
                    // health probes can observe `initializing`. Do not keep
                    // this bootstrap connection: its server thread may have
                    // entered the not-ready path and would reject the first
                    // inference frame even after the lifecycle becomes ready.
                    drop(stream);
                    match Self::wait_for_daemon_ready(&socket_path) {
                        Ok(()) => {
                            Self::spawn_daemon_reaper(child, socket_path.clone());
                            let stream = UnixStream::connect(&socket_path).map_err(|e| {
                                ClientError::Ipc(format!(
                                    "failed to reconnect ready worker daemon {}: {}",
                                    socket_path.display(),
                                    e
                                ))
                            })?;
                            return Self::socket_worker_handle(stream, None, Some(socket_path));
                        }
                        Err(error) => {
                            let _ = child.kill();
                            let _ = child.wait();
                            cleanup_daemon_paths(&socket_path);
                            Self::print_daemon_log_tail();
                            return Err(error);
                        }
                    }
                }
                Err(e)
                    if e.kind() == std::io::ErrorKind::NotFound
                        || e.kind() == std::io::ErrorKind::ConnectionRefused => {}
                Err(e) => {
                    let _ = child.kill();
                    let _ = child.wait();
                    cleanup_daemon_paths(&socket_path);
                    Self::print_daemon_log_tail();
                    return Err(ClientError::Ipc(format!(
                        "failed to connect worker daemon {}: {}",
                        socket_path.display(),
                        e
                    )));
                }
            }

            match child.try_wait() {
                Ok(Some(status)) => {
                    cleanup_daemon_paths(&socket_path);
                    Self::print_daemon_log_tail();
                    return Err(ClientError::SpawnFailed(format!(
                        "worker daemon exited before accepting connections: {}",
                        status
                    )));
                }
                Ok(None) => {}
                Err(e) => {
                    cleanup_daemon_paths(&socket_path);
                    Self::print_daemon_log_tail();
                    return Err(ClientError::SpawnFailed(format!(
                        "failed to poll worker daemon startup: {}",
                        e
                    )));
                }
            }

            if Instant::now() >= deadline {
                let _ = child.kill();
                let _ = child.wait();
                cleanup_daemon_paths(&socket_path);
                Self::print_daemon_log_tail();
                return Err(ClientError::Timeout);
            }
            thread::sleep(Duration::from_millis(100));
        }
    }

    #[cfg(unix)]
    fn connect_daemon_when_ready(
        &self,
        socket_path: &Path,
        child: Option<Child>,
    ) -> Result<Option<WorkerHandle>, ClientError> {
        match UnixStream::connect(socket_path) {
            Ok(stream) => {
                // A connection accepted during model initialization is bound
                // to the server's not-ready handler. Close it, wait for the
                // explicit Ready state, then create the inference connection.
                drop(stream);
                Self::wait_for_daemon_ready(socket_path)?;
                let stream = UnixStream::connect(socket_path).map_err(|e| {
                    ClientError::Ipc(format!(
                        "failed to reconnect ready worker daemon {}: {}",
                        socket_path.display(),
                        e
                    ))
                })?;
                Self::socket_worker_handle(stream, child, Some(socket_path.to_path_buf())).map(Some)
            }
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(None),
            Err(e) if e.kind() == std::io::ErrorKind::ConnectionRefused => {
                cleanup_daemon_paths(socket_path);
                Ok(None)
            }
            Err(e) => Err(ClientError::Ipc(format!(
                "failed to connect worker daemon {}: {}",
                socket_path.display(),
                e
            ))),
        }
    }

    /// Wait for the daemon's explicit lifecycle transition before retaining a
    /// socket for inference. A readiness deadline prevents an infinite hang
    /// when the daemon is stuck in Initializing state. When the deadline fires,
    /// the error propagates so callers can treat neural as unavailable.
    ///
    /// VAL-DAEMON-004: Uses one persistent Unix socket connection held in
    /// `stream: Option<UnixStream>` for the entire polling duration. Each
    /// poll writes + reads on the same socket; the connection is only
    /// dropped on error (triggering a reconnect on the next iteration).
    #[cfg(unix)]
    fn wait_for_daemon_ready(socket_path: &Path) -> Result<(), ClientError> {
        let started = Instant::now();
        let mut stream: Option<UnixStream> = None;

        loop {
            if started.elapsed() >= DAEMON_READY_MAX_WAIT {
                return Err(ClientError::Worker(WorkerError {
                    kind: ErrorKind::OnnxRuntime,
                    message: format!(
                        "daemon did not reach Ready state within {}s;\
                         falling back to core TF-IDF/PDG results",
                        DAEMON_READY_MAX_WAIT.as_secs()
                    ),
                }));
            }

            // Establish connection once, reuse for all subsequent polls.
            // On error, drop and reconnect on the next iteration.
            if stream.is_none() {
                match UnixStream::connect(socket_path) {
                    Ok(s) => {
                        // No timeout: once connected to a live daemon, let
                        // the health response arrive without a deadline.
                        let _ = s.set_read_timeout(None);
                        let _ = s.set_write_timeout(None);
                        stream = Some(s);
                    }
                    Err(e)
                        if e.kind() == std::io::ErrorKind::NotFound
                            || e.kind() == std::io::ErrorKind::ConnectionRefused =>
                    {
                        // Daemon hasn't bound yet; wait and retry.
                        thread::sleep(DAEMON_READINESS_POLL);
                        continue;
                    }
                    Err(e) => {
                        return Err(ClientError::Ipc(format!(
                            "failed to connect worker health socket {}: {}",
                            socket_path.display(),
                            e
                        )));
                    }
                }
            }

            let s = stream.as_mut().unwrap();

            let batch_id = BatchId::new(BATCH_COUNTER.fetch_add(1, Ordering::Relaxed));
            let wire = protocol::health_request_frame(batch_id)
                .map_err(|e| ClientError::Ipc(e.to_string()))?
                .encode_wire()
                .map_err(|e| ClientError::Ipc(e.to_string()))?;

            if let Err(e) = s.write_all(&wire).and_then(|_| s.flush()) {
                tracing::debug!(error = %e, "health probe write failed; reconnecting");
                stream.take();
                thread::sleep(DAEMON_READINESS_POLL);
                continue;
            }

            let Some(frame) = Self::read_daemon_health_frame(s, batch_id) else {
                stream.take();
                thread::sleep(DAEMON_READINESS_POLL);
                continue;
            };
            match Self::interpret_daemon_health_frame(frame)? {
                DaemonHealthState::Ready => return Ok(()),
                DaemonHealthState::Initializing => thread::sleep(DAEMON_READINESS_POLL),
                DaemonHealthState::Reconnect => {
                    stream.take();
                    thread::sleep(DAEMON_READINESS_POLL);
                }
            }
        }
    }

    #[cfg(unix)]
    fn read_daemon_health_frame(stream: &mut UnixStream, batch_id: BatchId) -> Option<Frame> {
        let payload = match read_frame(stream) {
            Ok(payload) => payload,
            Err(e) => {
                tracing::debug!(error = %e, "health probe read failed; reconnecting");
                return None;
            }
        };
        let frame = match Frame::from_wire_bytes(&payload) {
            Ok(frame) => frame,
            Err(e) => {
                tracing::debug!(error = %e, "health frame decode failed");
                return None;
            }
        };
        if frame.header.batch_id != batch_id {
            tracing::debug!("health response batch_id mismatch; reconnecting");
            return None;
        }
        Some(frame)
    }

    #[cfg(unix)]
    fn interpret_daemon_health_frame(frame: Frame) -> Result<DaemonHealthState, ClientError> {
        match frame.header.msg_type {
            MsgType::HealthResponse => match frame.decode_payload::<Response>() {
                Ok(Response::Health(health)) => match health.state {
                    WorkerState::Ready => Ok(DaemonHealthState::Ready),
                    WorkerState::Initializing => Ok(DaemonHealthState::Initializing),
                    WorkerState::Failed => Err(ClientError::Worker(WorkerError {
                        kind: ErrorKind::OnnxRuntime,
                        message: health
                            .error
                            .unwrap_or_else(|| "worker daemon initialization failed".to_string()),
                    })),
                },
                _ => Ok(DaemonHealthState::Reconnect),
            },
            MsgType::Error => match frame.decode_payload::<Response>() {
                Ok(Response::Error(error)) if error.kind == ErrorKind::Initializing => {
                    Ok(DaemonHealthState::Initializing)
                }
                Ok(Response::Error(error)) => Err(ClientError::Worker(WorkerError {
                    kind: ErrorKind::OnnxRuntime,
                    message: error.message,
                })),
                _ => Ok(DaemonHealthState::Reconnect),
            },
            _ => Ok(DaemonHealthState::Reconnect),
        }
    }

    #[cfg(unix)]
    fn spawn_daemon_reaper(mut child: Child, socket_path: PathBuf) {
        let pid = child.id();
        thread::spawn(move || {
            let _ = child.wait();
            let pid_path = socket_path.with_extension("pid");
            let owns_state = std::fs::read_to_string(&pid_path)
                .ok()
                .and_then(|value| value.trim().parse::<u32>().ok())
                .is_some_and(|recorded| recorded == pid);
            if owns_state {
                cleanup_daemon_paths(&socket_path);
            }
        });
    }

    #[cfg(unix)]
    fn socket_worker_handle(
        stream: UnixStream,
        child: Option<Child>,
        socket_path: Option<PathBuf>,
    ) -> Result<WorkerHandle, ClientError> {
        stream.set_nonblocking(false).map_err(|e| {
            ClientError::Ipc(format!("failed to make worker socket blocking: {}", e))
        })?;
        let writer = stream
            .try_clone()
            .map_err(|e| ClientError::Ipc(format!("failed to clone worker socket: {}", e)))?;
        let (read_thread, read_request_tx) = Self::spawn_reader_thread(stream);
        Ok(WorkerHandle {
            child,
            writer: Some(WorkerWriter::Unix(writer)),
            read_thread,
            read_request_tx,
            stderr_thread: None,
            persistent: true,
            socket_path,
        })
    }

    fn daemon_log_path() -> Option<PathBuf> {
        leindex_home_dir().map(|home| home.join("logs").join("leindex-embed-daemon.log"))
    }

    fn daemon_stderr() -> Stdio {
        Self::daemon_log_path()
            .and_then(|path| {
                if let Some(parent) = path.parent() {
                    std::fs::create_dir_all(parent).ok()?;
                }
                std::fs::OpenOptions::new()
                    .create(true)
                    .append(true)
                    .open(path)
                    .ok()
            })
            .map(Stdio::from)
            .unwrap_or_else(Stdio::null)
    }

    fn print_daemon_log_tail() {
        let Some(path) = Self::daemon_log_path() else {
            return;
        };
        let Ok(contents) = std::fs::read_to_string(&path) else {
            return;
        };
        eprintln!("leindex-embed daemon stderr tail ({}):", path.display());
        for line in contents
            .lines()
            .rev()
            .take(40)
            .collect::<Vec<_>>()
            .into_iter()
            .rev()
        {
            eprintln!("{}", line);
        }
    }

    fn spawn_reader_thread<R>(reader: R) -> (thread::JoinHandle<()>, mpsc::Sender<ReadRequest>)
    where
        R: Read + Send + 'static,
    {
        let (read_request_tx, read_request_rx) = mpsc::channel::<ReadRequest>();
        let read_thread = thread::spawn(move || {
            // VAL-DAEMON-006: Wrap reader in BufReader with 128KB capacity to
            // reduce syscall count for large embedding responses.
            let mut buf_reader = BufReader::with_capacity(READ_BUF_CAPACITY, reader);
            while let Ok(request) = read_request_rx.recv() {
                match request {
                    ReadRequest::Read { tx } => {
                        let _ = tx.send(read_frame(&mut buf_reader));
                    }
                    ReadRequest::Shutdown => break,
                }
            }
        });
        (read_thread, read_request_tx)
    }

    fn spawn_stderr_thread<R>(
        stderr: R,
        last_startup_report: Arc<Mutex<Option<String>>>,
    ) -> thread::JoinHandle<()>
    where
        R: Read + Send + 'static,
    {
        thread::spawn(move || {
            let mut reader = BufReader::new(stderr);
            let mut line = String::new();
            loop {
                line.clear();
                match reader.read_line(&mut line) {
                    Ok(0) => break,
                    Ok(_) => {
                        let line = line.trim_end_matches(['\r', '\n']);
                        eprintln!("{}", line);
                        if line.contains("startup_report") {
                            if let Ok(mut report) = last_startup_report.lock() {
                                *report = Some(line.to_string());
                            }
                        }
                    }
                    Err(e) => {
                        eprintln!("leindex-embed stderr mirror failed: {}", e);
                        break;
                    }
                }
            }
        })
    }

    /// Clear the current worker connection so a fresh request can reconnect.
    ///
    /// VAL-CPHASE-021: After calling this, the next embed request will
    /// transparently spawn a new worker process.
    ///
    /// Resident daemon ownership stays with its reaper; a client never
    /// deletes a persistent daemon socket or kills a process it did not own.
    pub fn kill_worker(&self) {
        if let Ok(mut guard) = self.worker.lock() {
            if let Some(mut handle) = guard.take() {
                Self::shutdown_worker_handle(&mut handle, true);
            }
        }
    }

    /// Force-shutdown the persistent ONNX daemon, killing it even if the
    /// client does not own the child process.
    ///
    /// This is the CLI-mode cleanup: after a one-shot command (e.g. `leindex
    /// index` or `leindex search`) completes, the persistent daemon it spawned
    /// has no reason to outlive the process. The default `Drop` impl passes
    /// `kill_persistent=false`, which leaves daemon workers running for reuse
    /// by the MCP server. This method forces `kill_persistent=true` so the
    /// daemon is properly terminated before the CLI process exits.
    ///
    /// This is a no-op if no worker was ever spawned or the worker has already
    /// been shut down.
    pub fn force_shutdown_daemon(&self) {
        if let Ok(mut guard) = self.worker.lock() {
            if let Some(mut handle) = guard.take() {
                Self::shutdown_worker_handle(&mut handle, true);
            }
        }
    }

    /// Kill the stale daemon and clear local worker state after detecting a
    /// dead worker.
    ///
    /// VAL-DEADWORKER-003: On `ClientError::WorkerDied`, the stale daemon PID
    /// is read from the `.pid` sidecar file and killed (SIGTERM then SIGKILL
    /// if needed), daemon filesystem paths are cleaned up, and the local
    /// `WorkerHandle` is cleared so the next `embed_attempt` triggers a fresh
    /// `spawn_or_connect_daemon()`.
    fn handle_worker_died(&self) {
        #[cfg(unix)]
        {
            // Kill the stale daemon process by PID from the sidecar file.
            // The socket_path on the WorkerHandle pinpoints which daemon to
            // reap. For persistent daemons the client does not own the child
            // (the reaper thread does), so we kill it directly by PID.
            if let Ok(guard) = self.worker.lock() {
                if let Some(handle) = guard.as_ref() {
                    if let Some(socket_path) = handle.socket_path.as_ref() {
                        kill_stale_daemon_by_pid(socket_path);
                        cleanup_daemon_paths(socket_path);
                    }
                }
            }
        }

        // Clear the local worker handle so the next call spawns fresh.
        if let Ok(mut guard) = self.worker.lock() {
            if let Some(mut handle) = guard.take() {
                Self::shutdown_worker_handle(&mut handle, true);
            }
        }
    }

    // ── Test helpers (not part of the public API) ────────────────────────
    //
    // These `#[doc(hidden)]` methods exist solely so integration tests in
    // `tests/onnx_worker_fallback.rs` can inject mock connections and exercise
    // the `send_and_receive` I/O path without spawning a real daemon.

    /// **Test-only:** Create a pipe-mode client whose worker handle is backed
    /// by the supplied `UnixStream`. This bypasses the daemon spawn/connect
    /// flow, allowing deterministic dead-worker detection tests.
    #[doc(hidden)]
    #[cfg(all(unix, feature = "onnx"))]
    pub fn test_from_unix_stream(stream: UnixStream) -> Self {
        let client = Self::new_pipe();
        let handle = Self::socket_worker_handle(stream, None, None)
            .expect("socket_worker_handle must succeed for test stream");
        *client.worker.lock().unwrap() = Some(handle);
        client
    }

    /// **Test-only:** Create a daemon-mode client whose worker handle is
    /// backed by the supplied `UnixStream` and `socket_path`. This simulates
    /// a connected daemon so tests can verify stale-daemon cleanup on
    /// `WorkerDied`.
    #[doc(hidden)]
    #[cfg(all(unix, feature = "onnx"))]
    pub fn test_from_daemon_stream(stream: UnixStream, socket_path: PathBuf) -> Self {
        let client = Self::new();
        let handle = Self::socket_worker_handle(stream, None, Some(socket_path))
            .expect("socket_worker_handle must succeed for test stream");
        *client.worker.lock().unwrap() = Some(handle);
        client
    }

    /// **Test-only:** Send a raw frame and receive the response, bypassing the
    /// `ensure_worker_ready()` readiness gate. This exposes the private
    /// `send_and_receive` to integration tests so dead-worker I/O behavior can
    /// be verified without spawning a real worker process.
    #[doc(hidden)]
    #[cfg(feature = "onnx")]
    pub fn test_send_and_receive(&self, frame: Frame) -> Result<Frame, ClientError> {
        self.send_and_receive(frame)
    }

    /// **Test-only:** Check whether the local worker handle has been cleared
    /// (i.e., `handle_worker_died` ran successfully).
    #[doc(hidden)]
    #[cfg(feature = "onnx")]
    pub fn test_worker_handle_cleared(&self) -> bool {
        self.worker
            .lock()
            .map(|guard| guard.is_none())
            .unwrap_or(true)
    }

    fn shutdown_worker_handle(handle: &mut WorkerHandle, kill_persistent: bool) {
        // Signal the reader thread to stop. If the reader is currently
        // blocked inside read_frame (waiting for worker response), the
        // Shutdown message stays queued until read_frame returns.
        let _ = handle.read_request_tx.send(ReadRequest::Shutdown);
        if let Some(writer) = handle.writer.as_ref() {
            writer.shutdown();
        }
        drop(handle.writer.take());

        let owns_child = handle.child.is_some();
        let should_kill_child = !handle.persistent || (kill_persistent && owns_child);
        if should_kill_child {
            if handle.persistent && owns_child {
                #[cfg(unix)]
                if let Some(socket_path) = handle.socket_path.take() {
                    cleanup_daemon_paths(&socket_path);
                }
            }
            if let Some(child) = handle.child.as_mut() {
                #[cfg(unix)]
                {
                    let pid = child.id() as libc::pid_t;
                    if pid > 0 {
                        unsafe {
                            libc::kill(pid, libc::SIGTERM);
                        }
                    }
                }

                let deadline = Instant::now() + Duration::from_secs(2);
                loop {
                    match child.try_wait() {
                        Ok(Some(_)) => break,
                        Ok(None) if Instant::now() < deadline => {
                            thread::sleep(Duration::from_millis(50));
                        }
                        _ => break,
                    }
                }

                let _ = child.kill();
                let _ = child.wait();
            }
        }

        Self::join_shutdown_threads(handle);
    }

    fn join_shutdown_threads(handle: &mut WorkerHandle) {
        let (replacement_tx, _replacement_rx) = mpsc::channel::<ReadRequest>();
        let old_tx = std::mem::replace(&mut handle.read_request_tx, replacement_tx);
        drop(old_tx);

        let replacement_thread = thread::spawn(|| {});
        let read_thread = std::mem::replace(&mut handle.read_thread, replacement_thread);
        // Bounded join: if the reader thread is stuck inside read_frame on
        // a socket that the worker hasn't closed, join would block forever.
        // Give it 2 seconds; if it doesn't exit, the OS reaps the thread at
        // process exit. This prevents the index command from hanging after
        // all work is complete.
        let join_deadline = Instant::now() + Duration::from_secs(2);
        while !read_thread.is_finished() {
            if Instant::now() >= join_deadline {
                #[cfg(unix)]
                {
                    tracing::warn!(
                        "reader thread did not exit within 2s during worker shutdown; \
                         detaching (process exit will clean up)"
                    );
                }
                break;
            }
            thread::sleep(Duration::from_millis(10));
        }
        if read_thread.is_finished() {
            let _ = read_thread.join();
        }

        if let Some(stderr_thread) = handle.stderr_thread.take() {
            let stderr_deadline = Instant::now() + Duration::from_secs(2);
            while !stderr_thread.is_finished() {
                if Instant::now() >= stderr_deadline {
                    break;
                }
                thread::sleep(Duration::from_millis(10));
            }
            if stderr_thread.is_finished() {
                let _ = stderr_thread.join();
            }
        }
    }

    /// Send an embed request to the worker with retry-once fallback semantics.
    ///
    /// VAL-CPHASE-017: On worker failure, retries once before falling back.
    /// VAL-CPHASE-018: After retry failure, only this batch falls back to TF-IDF.
    /// VAL-CPHASE-019: Fallback emits an actionable warning.
    /// VAL-CPHASE-020: Worker failure does not crash the main daemon.
    /// VAL-CPHASE-021: After fallback, the worker is cleared so a fresh one
    /// can be spawned for later requests.
    ///
    /// VAL-CPHASE-016: The returned `EmbedResult::Success` contains a flat
    /// row-major `EmbedResponse` that can be written directly into destination
    /// storage without creating a nested `Vec<Vec<f32>>` heap mirror.
    pub fn embed_with_fallback(&self, texts: &[String], expected_dim: usize) -> EmbedResult {
        let batch_id = Self::next_batch_id();

        // Attempt 1: initial try
        match self.embed_attempt(batch_id, texts, expected_dim) {
            Ok(response) => EmbedResult::Success(response),
            Err(first_error) => {
                // Readiness is a state decision, not an elapsed-time retry.
                // `ensure_worker_ready` waits for an initializing daemon, so
                // this branch is reserved for a worker that explicitly
                // disappeared or failed while the request was in flight.
                if (self.use_daemon && self.availability().is_unavailable())
                    || matches!(
                        &first_error,
                        ClientError::Worker(WorkerError {
                            kind: ErrorKind::Initializing,
                            ..
                        })
                    )
                {
                    return EmbedResult::Fallback {
                        batch_id,
                        error: first_error,
                    };
                }

                // VAL-CPHASE-017: Retry once after killing the failed worker
                tracing::warn!(
                    batch_id = %batch_id,
                    error = %first_error,
                    "ONNX worker failed on first attempt, retrying once"
                );

                // Kill the failed worker so we can spawn a fresh one
                self.kill_worker();

                // Attempt 2: retry with a fresh worker
                let retry_batch_id = Self::next_batch_id();
                match self.embed_attempt(retry_batch_id, texts, expected_dim) {
                    Ok(response) => {
                        tracing::info!(
                            original_batch = %batch_id,
                            retry_batch = %retry_batch_id,
                            "ONNX worker retry succeeded"
                        );
                        EmbedResult::Success(response)
                    }
                    Err(retry_error) => {
                        // VAL-CPHASE-018: Second failure -> TF-IDF fallback for this batch only
                        // VAL-CPHASE-019: Emit actionable warning
                        tracing::warn!(
                            batch_id = %batch_id,
                            retry_batch_id = %retry_batch_id,
                            first_error = %first_error,
                            retry_error = %retry_error,
                            "ONNX worker fallback for batch {}: {} (retry exhausted, degrading to TF-IDF)",
                            batch_id,
                            retry_error
                        );

                        // VAL-CPHASE-021: Kill the worker so a fresh one can be
                        // spawned for later requests
                        self.kill_worker();

                        EmbedResult::Fallback {
                            batch_id,
                            error: retry_error,
                        }
                    }
                }
            }
        }
    }

    /// Single attempt to send an embed request to the worker.
    fn embed_attempt(
        &self,
        batch_id: BatchId,
        texts: &[String],
        expected_dim: usize,
    ) -> Result<EmbedResponse, ClientError> {
        crate::cli::mcp::request_meta::NEURAL_REQUESTS
            .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
        let neural_started = Instant::now();
        let result = (|| {
            self.ensure_worker_ready()?;

            let request = EmbedRequest {
                texts: texts.to_vec(),
                expected_dim,
            };

            let frame = protocol::embed_request_frame(batch_id, request)
                .map_err(|e| ClientError::Ipc(e.to_string()))?;

            let response_frame = self.send_and_receive(frame)?;

            match response_frame.header.msg_type {
                MsgType::EmbedResponse => {
                    let response: Response = response_frame
                        .decode_payload()
                        .map_err(|e| ClientError::Ipc(e.to_string()))?;
                    match response {
                        Response::Embed(embed_resp) => Ok(embed_resp),
                        _ => Err(ClientError::Protocol("expected Embed response".to_string())),
                    }
                }
                MsgType::Error => {
                    let response: Response = response_frame
                        .decode_payload()
                        .map_err(|e| ClientError::Ipc(e.to_string()))?;
                    match response {
                        Response::Error(err) => Err(ClientError::Worker(err)),
                        _ => Err(ClientError::Protocol("expected Error response".to_string())),
                    }
                }
                other => Err(ClientError::Protocol(format!(
                    "unexpected response type: {:?}",
                    other
                ))),
            }
        })();
        let neural_ms = neural_started.elapsed().as_millis().min(u64::MAX as u128) as u64;
        tracing::debug!(
            batch_id = %batch_id,
            neural_ms,
            "ONNX embedding attempt complete"
        );
        crate::cli::mcp::request_meta::record_neural_ms(neural_ms);
        result
    }

    /// Send an embed request to the worker and return the response.
    ///
    /// This is the simple API that returns an error on failure rather than
    /// falling back. For retry-once fallback semantics, use `embed_with_fallback`.
    pub fn embed(
        &self,
        texts: &[String],
        expected_dim: usize,
    ) -> Result<EmbedResponse, ClientError> {
        self.ensure_worker_ready()?;

        let batch_id = Self::next_batch_id();
        let request = EmbedRequest {
            texts: texts.to_vec(),
            expected_dim,
        };

        let frame = protocol::embed_request_frame(batch_id, request)
            .map_err(|e| ClientError::Ipc(e.to_string()))?;

        let response_frame = self.send_and_receive(frame)?;

        match response_frame.header.msg_type {
            MsgType::EmbedResponse => {
                let response: Response = response_frame
                    .decode_payload()
                    .map_err(|e| ClientError::Ipc(e.to_string()))?;
                match response {
                    Response::Embed(embed_resp) => Ok(embed_resp),
                    _ => Err(ClientError::Protocol("expected Embed response".to_string())),
                }
            }
            MsgType::Error => {
                let response: Response = response_frame
                    .decode_payload()
                    .map_err(|e| ClientError::Ipc(e.to_string()))?;
                match response {
                    Response::Error(err) => Err(ClientError::Worker(err)),
                    _ => Err(ClientError::Protocol("expected Error response".to_string())),
                }
            }
            other => Err(ClientError::Protocol(format!(
                "unexpected response type: {:?}",
                other
            ))),
        }
    }

    /// Send a rerank request to the worker and return the response.
    pub fn rerank(
        &self,
        query: &str,
        documents: Vec<RerankDocument>,
    ) -> Result<RerankResponse, ClientError> {
        self.ensure_worker_ready()?;

        let batch_id = Self::next_batch_id();
        let request = RerankRequest {
            query: query.to_string(),
            documents,
        };

        let frame = protocol::rerank_request_frame(batch_id, request)
            .map_err(|e| ClientError::Ipc(e.to_string()))?;

        let response_frame = self.send_and_receive(frame)?;

        match response_frame.header.msg_type {
            MsgType::RerankResponse => {
                let response: Response = response_frame
                    .decode_payload()
                    .map_err(|e| ClientError::Ipc(e.to_string()))?;
                match response {
                    Response::Rerank(rerank_resp) => Ok(rerank_resp),
                    _ => Err(ClientError::Protocol(
                        "expected Rerank response".to_string(),
                    )),
                }
            }
            MsgType::Error => {
                let response: Response = response_frame
                    .decode_payload()
                    .map_err(|e| ClientError::Ipc(e.to_string()))?;
                match response {
                    Response::Error(err) => Err(ClientError::Worker(err)),
                    _ => Err(ClientError::Protocol("expected Error response".to_string())),
                }
            }
            other => Err(ClientError::Protocol(format!(
                "unexpected response type: {:?}",
                other
            ))),
        }
    }

    /// Send a frame and receive the response frame.
    ///
    /// A persistent reader thread keeps pipe/socket I/O off the caller's
    /// stack, while the response channel waits for either a complete frame or
    /// a real transport disconnect. Model compilation and inference are not
    /// cancelled by elapsed-time policy.
    ///
    /// VAL-DEADWORKER-001/002: When the reader thread detects EOF/EPIPE (the
    /// worker process died), or when the reader thread itself disconnects, the
    /// error is mapped to `ClientError::WorkerDied` and `handle_worker_died`
    /// is called to kill the stale daemon and clear local state.
    fn send_and_receive(&self, frame: Frame) -> Result<Frame, ClientError> {
        let mut guard = self
            .worker
            .lock()
            .map_err(|e| ClientError::Ipc(format!("failed to lock worker handle: {}", e)))?;

        let handle = guard
            .as_mut()
            .ok_or_else(|| ClientError::Ipc("worker not running".to_string()))?;

        // Send the frame
        let wire = frame
            .encode_wire()
            .map_err(|e| ClientError::Ipc(e.to_string()))?;
        let request_batch_id = frame.header.batch_id;

        let writer = handle
            .writer
            .as_mut()
            .ok_or_else(|| ClientError::Ipc("worker transport not available".into()))?;

        if let Err(e) = writer.write_all(&wire) {
            drop(guard);
            // VAL-DEADWORKER-001: EPIPE on write means the worker died
            // (reading end closed). Classify as WorkerDied so the stale
            // daemon is killed and replaced.
            if is_worker_dead_io_error(&e) {
                self.handle_worker_died();
                return Err(ClientError::WorkerDied {
                    message: format!("EPIPE writing request frame: {}", e),
                });
            }
            self.kill_worker();
            return Err(ClientError::Ipc(format!(
                "failed to write to worker: {}",
                e
            )));
        }
        if let Err(e) = writer.flush() {
            drop(guard);
            if is_worker_dead_io_error(&e) {
                self.handle_worker_died();
                return Err(ClientError::WorkerDied {
                    message: format!("EPIPE flushing request frame: {}", e),
                });
            }
            self.kill_worker();
            return Err(ClientError::Ipc(format!(
                "failed to flush worker transport: {}",
                e
            )));
        }

        // Use the persistent reader thread to read the response.
        let (tx, rx) = mpsc::channel();
        handle
            .read_request_tx
            .send(ReadRequest::Read { tx })
            .map_err(|_e| ClientError::Ipc("reader thread channel closed".to_string()))?;

        self.receive_worker_response(rx, request_batch_id, guard)
    }

    fn receive_worker_response(
        &self,
        rx: mpsc::Receiver<Result<Vec<u8>, ClientError>>,
        request_batch_id: BatchId,
        guard: std::sync::MutexGuard<'_, Option<WorkerHandle>>,
    ) -> Result<Frame, ClientError> {
        match rx.recv() {
            Ok(Ok(frame_buf)) => {
                let response = match Frame::from_wire_bytes(&frame_buf) {
                    Ok(response) => response,
                    Err(e) => {
                        drop(guard);
                        self.kill_worker();
                        return Err(ClientError::Ipc(e.to_string()));
                    }
                };
                if response.header.batch_id != request_batch_id {
                    drop(guard);
                    self.kill_worker();
                    return Err(ClientError::Ipc(format!(
                        "response batch_id mismatch: expected {}, got {}",
                        request_batch_id, response.header.batch_id
                    )));
                }
                Ok(response)
            }
            Ok(Err(e)) => {
                drop(guard);
                // VAL-DEADWORKER-001/002: The reader thread got EOF/EPIPE (the
                // worker process died). Map this to WorkerDied and reap the
                // stale daemon so the next call spawns a replacement.
                if matches!(e, ClientError::WorkerDied { .. }) {
                    self.handle_worker_died();
                    return Err(e);
                }
                self.kill_worker();
                // Frame too large or other I/O error
                if e.to_string().contains("too large") {
                    Err(ClientError::Ipc(e.to_string()))
                } else {
                    Err(ClientError::Ipc(format!(
                        "failed to read from worker: {}",
                        e
                    )))
                }
            }
            Err(mpsc::RecvError) => {
                drop(guard);
                // VAL-DEADWORKER-001/002: The reader thread itself
                // disconnected, which means it either received EOF/EPIPE or
                // panicked. Either way the worker is dead.
                self.handle_worker_died();
                Err(ClientError::WorkerDied {
                    message: "reader thread disconnected".to_string(),
                })
            }
        }
    }
}

impl Drop for EmbeddingClient {
    fn drop(&mut self) {
        // Only the last Arc owner should kill the worker.
        let worker = match Arc::try_unwrap(std::mem::take(&mut self.worker)) {
            Ok(worker) => worker,
            Err(_) => return,
        };

        let mut guard = worker.into_inner().unwrap_or_else(|e| e.into_inner());
        if let Some(mut handle) = guard.take() {
            Self::shutdown_worker_handle(&mut handle, false);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use leindex_embed::protocol::ErrorKind;

    /// TEMP verification: spawn the REAL leindex-embed worker (pipe mode) and run
    /// one embed through the production EmbeddingClient. On a cold MIGraphX cache
    /// this JIT-compiles (~300 s) and the native ORT cache writes a `.mxr`; on a
    /// warm cache it loads the `.mxr` (~10 s). Run: `cargo test -p leindex
    /// --features onnx real_pipe_embed -- --ignored --nocapture`.
    #[test]
    #[ignore = "spawns real leindex-embed worker + ~300s MIGraphX JIT compile"]
    fn real_pipe_embed_seeds_migraphx_cache() {
        let cache = migraphx_cache_path("qwen3-embed-0.6b");
        eprintln!("cache dir: {}", cache.display());
        let start = std::time::Instant::now();
        let client = EmbeddingClient::new_pipe();
        let response = client
            .embed(&["hello world".to_string()], 1024)
            .expect("pipe embed failed");
        eprintln!(
            "embed count={} elapsed={:?}",
            response.count,
            start.elapsed()
        );
        assert!(response.count > 0, "embed returned no vectors");
    }

    #[test]
    fn test_client_creation() {
        let _client = EmbeddingClient::new();
    }

    #[test]
    fn availability_uses_pipe_startup_report_without_spawning() {
        let client = EmbeddingClient::new_pipe();
        assert!(matches!(client.availability(), WorkerAvailability::Absent));
        *client.last_startup_report.lock().unwrap() =
            Some("startup_report provider=cpu status=available".to_string());
        assert!(matches!(client.availability(), WorkerAvailability::Ready));
    }

    #[test]
    fn test_client_debug_impl() {
        let client = EmbeddingClient::new();
        let debug_str = format!("{:?}", client);
        assert!(debug_str.contains("EmbeddingClient"));
    }

    #[test]
    fn test_client_clone_shares_worker() {
        let client = EmbeddingClient::new();
        let cloned = client.clone();
        // Clone shares the worker handle via Arc, not a new empty client
        let _ = format!("{:?}", cloned);
    }

    #[test]
    fn test_parse_startup_report_provider_from_plain_line() {
        let line = "startup_report provider=migraphx status=available model=qwen3-embed-0.6b";
        assert_eq!(
            parse_startup_report_provider(line).as_deref(),
            Some("migraphx")
        );
    }

    #[test]
    fn test_parse_startup_report_provider_from_tracing_line() {
        let line = "2026-06-30T01:02:03Z INFO startup_report provider=cpu status=unavailable (fallback: no GPU)";
        assert_eq!(parse_startup_report_provider(line).as_deref(), Some("cpu"));
    }

    #[test]
    fn test_client_reports_last_startup_provider() {
        let client = EmbeddingClient::new();
        *client.last_startup_report.lock().unwrap() =
            Some("startup_report provider=cuda status=available".to_string());

        assert_eq!(client.active_execution_provider().as_deref(), Some("cuda"));
    }

    #[test]
    fn test_wait_for_active_provider_observes_stderr_update() {
        let client = EmbeddingClient::new_pipe();
        let report = Arc::clone(&client.last_startup_report);
        let updater = thread::spawn(move || {
            thread::sleep(Duration::from_millis(20));
            *report.lock().unwrap() =
                Some("startup_report provider=migraphx status=available".to_string());
        });

        assert_eq!(
            client
                .wait_for_active_execution_provider(Duration::from_secs(1))
                .as_deref(),
            Some("migraphx")
        );
        updater.join().unwrap();
    }

    #[cfg(unix)]
    #[test]
    fn daemon_spawn_lock_serializes_contenders() {
        let temp = tempfile::tempdir().unwrap();
        let path = temp.path().join("worker.lock");
        let first = DaemonSpawnLock::acquire(&path, Duration::from_secs(1)).unwrap();
        assert!(matches!(
            DaemonSpawnLock::acquire(&path, Duration::from_millis(20)),
            Err(ClientError::Timeout)
        ));
        drop(first);
        DaemonSpawnLock::acquire(&path, Duration::from_secs(1)).unwrap();
    }

    #[cfg(unix)]
    #[test]
    fn persistent_shutdown_unblocks_and_joins_reader() {
        let (client_stream, _server_stream) = UnixStream::pair().unwrap();
        let mut handle = EmbeddingClient::socket_worker_handle(client_stream, None, None).unwrap();
        let (tx, _rx) = mpsc::channel();
        handle
            .read_request_tx
            .send(ReadRequest::Read { tx })
            .unwrap();
        thread::sleep(Duration::from_millis(10));

        EmbeddingClient::shutdown_worker_handle(&mut handle, false);
    }

    #[cfg(unix)]
    #[test]
    fn persistent_client_does_not_delete_daemon_socket() {
        let temp = tempfile::tempdir().unwrap();
        let socket_path = temp.path().join("daemon.sock");
        std::fs::write(&socket_path, b"owned by daemon").unwrap();
        let (client_stream, _server_stream) = UnixStream::pair().unwrap();
        let mut handle =
            EmbeddingClient::socket_worker_handle(client_stream, None, Some(socket_path.clone()))
                .unwrap();

        EmbeddingClient::shutdown_worker_handle(&mut handle, true);
        assert!(socket_path.exists());
    }

    #[test]
    fn stderr_mirror_exits_after_reported_io_error() {
        struct FailingReader;
        impl Read for FailingReader {
            fn read(&mut self, _buf: &mut [u8]) -> std::io::Result<usize> {
                Err(std::io::Error::other("synthetic stderr failure"))
            }
        }

        let report = Arc::new(Mutex::new(None));
        EmbeddingClient::spawn_stderr_thread(FailingReader, report)
            .join()
            .unwrap();
    }

    #[test]
    fn test_client_error_display() {
        let err = ClientError::SpawnFailed("not found".to_string());
        assert!(err.to_string().contains("not found"));

        let worker_err = WorkerError {
            kind: ErrorKind::ModelNotFound,
            message: "missing model".to_string(),
        };
        let err = ClientError::Worker(worker_err);
        assert!(err.to_string().contains("missing model"));
    }

    #[test]
    fn test_embed_result_success() {
        let response = EmbedResponse::new(vec![1.0, 2.0, 3.0, 4.0], 1, 4);
        let result = EmbedResult::Success(response);
        assert!(result.is_success());
        assert!(!result.is_fallback());
        assert!(result.into_success().is_some());
    }

    #[test]
    fn test_embed_result_fallback() {
        let error = ClientError::Worker(WorkerError {
            kind: ErrorKind::Inference,
            message: "worker crashed".to_string(),
        });
        let result = EmbedResult::Fallback {
            batch_id: BatchId::new(42),
            error,
        };
        assert!(!result.is_success());
        assert!(result.is_fallback());
        assert!(result.into_success().is_none());
    }

    #[test]
    fn test_batch_id_monotonic() {
        let id1 = EmbeddingClient::next_batch_id();
        let id2 = EmbeddingClient::next_batch_id();
        assert!(
            id2.0 > id1.0,
            "batch IDs should be monotonically increasing"
        );
    }

    // ── VAL-SETUP-020/VAL-ORT-006: config-driven ORT_DYLIB_PATH injection ──

    // Use a process-shared lock so env-mutating tests serialize within the module.
    use std::sync::Mutex;
    static TEST_ENV_LOCK: Mutex<()> = Mutex::new(());

    #[test]
    fn test_read_ort_dylib_path_from_config_returns_value() {
        let _g = TEST_ENV_LOCK.lock().unwrap();
        let tmp = tempfile::tempdir().unwrap();
        unsafe { std::env::set_var("LEINDEX_HOME", tmp.path()) };
        unsafe { std::env::remove_var("LEINDEX_ONNX_INFERENCE_BATCH_SIZE") };
        unsafe { std::env::remove_var("LEINDEX_ONNX_SEQUENCE_LEN") };

        let cfg_dir = tmp.path().join("config");
        std::fs::create_dir_all(&cfg_dir).unwrap();
        std::fs::write(
            cfg_dir.join("leindex.toml"),
            "[neural]\nenabled = true\nexecution_provider = \"cpu\"\nort_dylib_path = \"/opt/onnxruntime/libonnxruntime.so\"\nort_version = \"1.25.0\"\nmodel_dir = \"/models\"\n",
        )
        .unwrap();

        let parsed = read_ort_dylib_path_from_config();
        assert_eq!(
            parsed.as_deref(),
            Some("/opt/onnxruntime/libonnxruntime.so")
        );
        assert_eq!(
            read_execution_provider_from_config().as_deref(),
            Some("cpu")
        );

        unsafe { std::env::remove_var("LEINDEX_HOME") };
    }

    #[test]
    fn test_read_execution_provider_from_config_skips_auto() {
        let _g = TEST_ENV_LOCK.lock().unwrap();
        let tmp = tempfile::tempdir().unwrap();
        unsafe { std::env::set_var("LEINDEX_HOME", tmp.path()) };

        let cfg_dir = tmp.path().join("config");
        std::fs::create_dir_all(&cfg_dir).unwrap();
        std::fs::write(
            cfg_dir.join("leindex.toml"),
            "[neural]\nenabled = true\nexecution_provider = \"auto\"\n",
        )
        .unwrap();
        assert_eq!(read_execution_provider_from_config(), None);

        std::fs::write(
            cfg_dir.join("leindex.toml"),
            "[neural]\nenabled = true\nexecution_provider = \"migraphx\"\n",
        )
        .unwrap();
        assert_eq!(
            read_execution_provider_from_config().as_deref(),
            Some("migraphx")
        );

        unsafe { std::env::remove_var("LEINDEX_HOME") };
    }

    #[test]
    fn test_read_worker_model_name_from_config_returns_value() {
        let _g = TEST_ENV_LOCK.lock().unwrap();
        let tmp = tempfile::tempdir().unwrap();
        unsafe { std::env::set_var("LEINDEX_HOME", tmp.path()) };

        let cfg_dir = tmp.path().join("config");
        std::fs::create_dir_all(&cfg_dir).unwrap();
        std::fs::write(
            cfg_dir.join("leindex.toml"),
            "[neural]\nenabled = true\nmodel_name = \"qwen3-embed-0.6b-dynamic\"\n",
        )
        .unwrap();

        assert_eq!(
            read_worker_model_name_from_config().as_deref(),
            Some("qwen3-embed-0.6b-dynamic")
        );

        unsafe { std::env::remove_var("LEINDEX_HOME") };
    }

    #[test]
    fn test_migraphx_model_cache_path_uses_leindex_home() {
        let _g = TEST_ENV_LOCK.lock().unwrap();
        let tmp = tempfile::tempdir().unwrap();
        unsafe { std::env::set_var("LEINDEX_HOME", tmp.path()) };
        unsafe { std::env::remove_var("LEINDEX_ONNX_INFERENCE_BATCH_SIZE") };
        unsafe { std::env::remove_var("LEINDEX_ONNX_SEQUENCE_LEN") };
        let expected = tmp
            .path()
            .join("cache")
            .join("migraphx")
            .join("qwen3-embed-0_6b-dynamic")
            .join("b8-s128");

        assert_eq!(
            migraphx_model_cache_path(Some("qwen3-embed-0.6b-dynamic")).as_deref(),
            Some(expected.as_path())
        );

        unsafe { std::env::remove_var("LEINDEX_HOME") };
    }

    #[test]
    #[cfg(unix)]
    fn test_daemon_socket_path_includes_inference_shape() {
        let _g = TEST_ENV_LOCK.lock().unwrap();
        let tmp = tempfile::tempdir().unwrap();
        unsafe { std::env::set_var("LEINDEX_HOME", tmp.path()) };
        unsafe { std::env::remove_var("LEINDEX_ONNX_INFERENCE_BATCH_SIZE") };
        unsafe { std::env::remove_var("LEINDEX_ONNX_SEQUENCE_LEN") };

        let socket =
            daemon_socket_path(Some("migraphx"), Some("qwen3-embed-0.6b-dynamic")).unwrap();
        let filename = socket.file_name().and_then(|name| name.to_str()).unwrap();
        assert!(filename.starts_with("leindex-embed-"));
        assert!(filename.ends_with(".sock"));
        assert!(socket.to_string_lossy().len() <= 100);

        unsafe { std::env::remove_var("LEINDEX_HOME") };
    }

    #[test]
    fn test_read_ort_dylib_path_from_config_returns_none_when_absent() {
        let _g = TEST_ENV_LOCK.lock().unwrap();
        let tmp = tempfile::tempdir().unwrap();
        unsafe { std::env::set_var("LEINDEX_HOME", tmp.path()) };

        // No config file at all.
        assert_eq!(read_ort_dylib_path_from_config(), None);

        // Config exists but lacks ort_dylib_path.
        let cfg_dir = tmp.path().join("config");
        std::fs::create_dir_all(&cfg_dir).unwrap();
        std::fs::write(
            cfg_dir.join("leindex.toml"),
            "[neural]\nenabled = true\nmodel_dir = \"/models\"\n",
        )
        .unwrap();
        assert_eq!(read_ort_dylib_path_from_config(), None);

        unsafe { std::env::remove_var("LEINDEX_HOME") };
    }

    #[test]
    fn test_read_ort_dylib_path_from_config_handles_single_quotes() {
        let _g = TEST_ENV_LOCK.lock().unwrap();
        let tmp = tempfile::tempdir().unwrap();
        unsafe { std::env::set_var("LEINDEX_HOME", tmp.path()) };

        let cfg_dir = tmp.path().join("config");
        std::fs::create_dir_all(&cfg_dir).unwrap();
        std::fs::write(
            cfg_dir.join("leindex.toml"),
            "[neural]\nort_dylib_path = '/quote/ort.so'\n",
        )
        .unwrap();

        assert_eq!(
            read_ort_dylib_path_from_config().as_deref(),
            Some("/quote/ort.so")
        );

        unsafe { std::env::remove_var("LEINDEX_HOME") };
    }

    #[test]
    fn test_leindex_home_dir_prefers_env_override() {
        let _g = TEST_ENV_LOCK.lock().unwrap();
        unsafe { std::env::set_var("LEINDEX_HOME", "/custom/leindex/home") };
        assert_eq!(
            leindex_home_dir(),
            Some(std::path::PathBuf::from("/custom/leindex/home"))
        );
        unsafe { std::env::remove_var("LEINDEX_HOME") };
    }

    #[test]
    fn test_leindex_home_dir_falls_back_to_home() {
        let _g = TEST_ENV_LOCK.lock().unwrap();
        unsafe { std::env::remove_var("LEINDEX_HOME") };
        unsafe { std::env::set_var("HOME", "/home/testuser") };
        let home = leindex_home_dir();
        assert_eq!(
            home,
            Some(std::path::PathBuf::from("/home/testuser/.leindex"))
        );
        unsafe { std::env::remove_var("HOME") };
    }

    #[test]
    fn test_leindex_home_dir_relative_env_ignored() {
        let _g = TEST_ENV_LOCK.lock().unwrap();
        unsafe { std::env::set_var("LEINDEX_HOME", "relative/path") };
        unsafe { std::env::set_var("HOME", "/home/fallback") };
        // Should fall back to HOME-based path, not use relative.
        let home = leindex_home_dir();
        assert_eq!(
            home,
            Some(std::path::PathBuf::from("/home/fallback/.leindex"))
        );
        unsafe { std::env::remove_var("LEINDEX_HOME") };
        unsafe { std::env::remove_var("HOME") };
    }
}