vera2-core 0.12.16-aristoddle.1

Core engine for Vera: parsing, indexing, retrieval, storage, and embedding
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
//! Embedding provider abstraction and OpenAI-compatible implementation.

use std::collections::HashMap;
use std::sync::{Arc, Mutex, OnceLock};
use std::time::{Duration, Instant};

use anyhow::{Context, Result};
use regex::Regex;
use serde::{Deserialize, Serialize};
use tokio_util::sync::CancellationToken;
use tracing::{debug, warn};

use crate::chunk_text;
use crate::local_models::CODERANK_QUERY_PREFIX;
use crate::types::Chunk;

// ── Error types ──────────────────────────────────────────────────────

/// Errors specific to the embedding pipeline.
#[derive(Debug, thiserror::Error)]
pub enum EmbeddingError {
    /// Authentication failure (invalid or missing API key).
    #[error("embedding API authentication failed: {message}")]
    AuthError { message: String },

    /// Cannot reach the embedding endpoint.
    #[error("embedding API connection failed: {message}")]
    ConnectionError { message: String },

    /// The request exceeded its client-side timeout. Retrying could duplicate
    /// work when an upstream proxy continues processing after disconnect.
    #[error("embedding API request timed out: {message}")]
    TimeoutError { message: String },

    /// The API returned a non-auth, non-connection error.
    #[error("embedding API error (status {status}): {message}")]
    ApiError { status: u16, message: String },

    /// Rate limit exceeded.
    #[error("embedding API rate limit exceeded: {message}")]
    RateLimitError { message: String },

    /// Unexpected response format.
    #[error("unexpected embedding API response: {message}")]
    ResponseError { message: String },

    /// Request was cancelled because the client disconnected.
    #[error("embedding cancelled")]
    Cancelled,
}

/// Wrap an internal failure as an `ApiError` with status 500 (local pipeline
/// errors surface through the same variant as remote API errors).
pub(crate) fn api_err(error: impl std::fmt::Display) -> EmbeddingError {
    EmbeddingError::ApiError {
        status: 500,
        message: error.to_string(),
    }
}

// ── Provider trait ───────────────────────────────────────────────────

/// Trait abstracting an embedding provider.
///
/// Implementations must be able to embed a batch of text inputs and return
/// one vector per input. Vectors must have consistent dimensionality.
#[allow(async_fn_in_trait)]
pub trait EmbeddingProvider: Send + Sync {
    /// Embed a batch of text inputs, returning one vector per input.
    ///
    /// The returned vectors must all have the same dimensionality.
    async fn embed_batch(&self, texts: &[String]) -> Result<Vec<Vec<f32>>, EmbeddingError>;

    /// Return the expected vector dimensionality (if known ahead of time).
    fn expected_dim(&self) -> Option<usize>;

    /// Rewrite query text for providers that require asymmetric query prefixes.
    fn prepare_query_text(&self, query: &str) -> String {
        query.to_string()
    }

    /// Rewrite passage text for providers that require asymmetric document
    /// prefixes. Applied on the indexing path only.
    fn prepare_document_text(&self, document: &str) -> String {
        document.to_string()
    }

    /// Identity of the document-side text preparation, stored in the index
    /// metadata so a changed document prefix trips the stale-index guard
    /// instead of silently mixing vector spaces. The empty string means
    /// documents embed unprefixed. Local providers may leave the default:
    /// their stored model identity already covers prefix configuration.
    fn document_prefix_identity(&self) -> String {
        String::new()
    }

    /// Return the maximum number of inputs the provider accepts per request.
    ///
    /// `None` means Vera should use the configured batch size as-is.
    fn max_batch_size(&self) -> Option<usize> {
        None
    }

    /// Like `embed_batch`, but aborts between sub-batches if `cancel` is fired.
    ///
    /// The default implementation ignores the token and delegates to `embed_batch`.
    /// Providers that run blocking inference in sub-batch loops should override this
    /// to check `cancel.is_cancelled()` between iterations so client disconnects
    /// stop GPU work without waiting for the entire request to finish.
    async fn embed_batch_cancellable(
        &self,
        texts: &[String],
        cancel: &CancellationToken,
    ) -> Result<Vec<Vec<f32>>, EmbeddingError> {
        let _ = cancel;
        self.embed_batch(texts).await
    }
}

// ── Configuration ────────────────────────────────────────────────────

/// Configuration for an OpenAI-compatible embedding provider.
#[derive(Clone)]
pub struct EmbeddingProviderConfig {
    /// Base URL for the API (e.g. "https://api.openai.com/v1").
    pub base_url: String,
    /// Model identifier (e.g. "Qwen/Qwen3-Embedding-8B").
    pub model_id: String,
    /// API key (never logged or exposed).
    api_key: String,
    /// Request timeout.
    pub timeout: Duration,
    /// Maximum retries on transient errors.
    pub max_retries: u32,
    /// Optional prefix prepended to query text for asymmetric embedding models.
    /// Read from `EMBEDDING_QUERY_PREFIX` env var.
    pub query_prefix: Option<String>,
    /// Optional prefix prepended to indexed passage text for asymmetric
    /// embedding models. Read from `EMBEDDING_DOCUMENT_PREFIX` env var.
    pub document_prefix: Option<String>,
}

impl std::fmt::Debug for EmbeddingProviderConfig {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("EmbeddingProviderConfig")
            .field("base_url", &self.base_url)
            .field("model_id", &self.model_id)
            .field("api_key", &"[REDACTED]")
            .field("timeout", &self.timeout)
            .field("max_retries", &self.max_retries)
            .finish()
    }
}

impl EmbeddingProviderConfig {
    /// Create a new config. The API key is stored opaquely and never exposed.
    pub fn new(base_url: String, model_id: String, api_key: String) -> Self {
        let query_prefix = default_query_prefix_for_known_model(&model_id);
        let document_prefix = default_document_prefix_for_model(&model_id);
        Self {
            base_url,
            model_id,
            api_key,
            timeout: Duration::from_secs(30),
            max_retries: 3,
            query_prefix,
            document_prefix,
        }
    }

    /// Create config from environment variables.
    ///
    /// Reads:
    /// - `EMBEDDING_MODEL_BASE_URL`
    /// - `EMBEDDING_MODEL_ID`
    /// - `EMBEDDING_MODEL_API_KEY`
    /// - `EMBEDDING_QUERY_PREFIX` (optional override; auto-detected from model ID if unset)
    /// - `EMBEDDING_DOCUMENT_PREFIX` (optional override; auto-detected from model ID if unset)
    pub fn from_env() -> Result<Self> {
        let base_url = std::env::var("EMBEDDING_MODEL_BASE_URL")
            .context("EMBEDDING_MODEL_BASE_URL not set")?;
        let model_id = std::env::var("EMBEDDING_MODEL_ID").context("EMBEDDING_MODEL_ID not set")?;
        let api_key =
            std::env::var("EMBEDDING_MODEL_API_KEY").context("EMBEDDING_MODEL_API_KEY not set")?;
        let query_prefix = std::env::var("EMBEDDING_QUERY_PREFIX")
            .ok()
            .filter(|s| !s.is_empty())
            .or_else(|| default_query_prefix_for_model(&model_id));
        let document_prefix = std::env::var("EMBEDDING_DOCUMENT_PREFIX")
            .ok()
            .filter(|s| !s.is_empty())
            .or_else(|| default_document_prefix_for_model(&model_id));

        let mut config = Self::new(base_url, model_id, api_key);
        config.query_prefix = query_prefix;
        config.document_prefix = document_prefix;
        Ok(config)
    }

    /// Create config from environment variables without blocking an async
    /// runtime worker on the best-effort HuggingFace lookup.
    pub async fn from_env_async() -> Result<Self> {
        let base_url = std::env::var("EMBEDDING_MODEL_BASE_URL")
            .context("EMBEDDING_MODEL_BASE_URL not set")?;
        let model_id = std::env::var("EMBEDDING_MODEL_ID").context("EMBEDDING_MODEL_ID not set")?;
        let api_key =
            std::env::var("EMBEDDING_MODEL_API_KEY").context("EMBEDDING_MODEL_API_KEY not set")?;
        let query_prefix = std::env::var("EMBEDDING_QUERY_PREFIX")
            .ok()
            .filter(|s| !s.is_empty())
            .or_else(|| default_query_prefix_for_known_model(&model_id));
        let query_prefix = match query_prefix {
            Some(prefix) => Some(prefix),
            None => {
                let model_id_for_fetch = model_id.clone();
                tokio::task::spawn_blocking(move || fetch_query_prefix_from_hf(&model_id_for_fetch))
                    .await
                    .ok()
                    .flatten()
            }
        };
        let document_prefix = std::env::var("EMBEDDING_DOCUMENT_PREFIX")
            .ok()
            .filter(|s| !s.is_empty())
            .or_else(|| default_document_prefix_for_model(&model_id));

        let mut config = Self::new(base_url, model_id, api_key);
        config.query_prefix = query_prefix;
        config.document_prefix = document_prefix;
        Ok(config)
    }

    /// Set the request timeout.
    pub fn with_timeout(mut self, timeout: Duration) -> Self {
        self.timeout = timeout;
        self
    }

    /// Set the maximum retry count.
    pub fn with_max_retries(mut self, max_retries: u32) -> Self {
        self.max_retries = max_retries;
        self
    }
}

// ── Query prefix auto-detection ──────────────────────────────────────

/// Auto-detect the query prefix for known asymmetric embedding model families.
///
/// Returns `None` for symmetric models or unrecognized model IDs.
/// Users can always override via `EMBEDDING_QUERY_PREFIX` env var.
fn default_query_prefix_for_model(model_id: &str) -> Option<String> {
    default_query_prefix_for_known_model(model_id)
        .or_else(|| fetch_query_prefix_off_runtime(model_id))
}

fn fetch_query_prefix_off_runtime(model_id: &str) -> Option<String> {
    if let Ok(handle) = tokio::runtime::Handle::try_current() {
        let model_id = model_id.to_string();
        return match handle.runtime_flavor() {
            tokio::runtime::RuntimeFlavor::MultiThread => tokio::task::block_in_place(|| {
                handle
                    .block_on(tokio::task::spawn_blocking(move || {
                        fetch_query_prefix_from_hf(&model_id)
                    }))
                    .ok()
                    .flatten()
            }),
            tokio::runtime::RuntimeFlavor::CurrentThread => {
                std::thread::spawn(move || fetch_query_prefix_from_hf(&model_id))
                    .join()
                    .ok()
                    .flatten()
            }
            _ => std::thread::spawn(move || fetch_query_prefix_from_hf(&model_id))
                .join()
                .ok()
                .flatten(),
        };
    }

    fetch_query_prefix_from_hf(model_id)
}

fn default_query_prefix_for_known_model(model_id: &str) -> Option<String> {
    let id = model_id.to_lowercase();
    if id.contains("qwen3-embedding") || id.contains("qwen3_embedding") {
        Some("Instruct: Given a code search query, retrieve relevant code snippets that match the query\nQuery: ".into())
    } else if id.contains("coderankembed") {
        // `prepare_query_text` concatenates without a separator, so the
        // trailing space belongs here rather than in the shared constant.
        Some(format!("{CODERANK_QUERY_PREFIX} "))
    } else if id.contains("e5-") || id.contains("e5_") {
        Some("query: ".into())
    } else if id.contains("bge-") || id.contains("bge_") {
        Some("Represent this sentence for searching relevant passages: ".into())
    } else {
        None
    }
}

/// Auto-detect a documented passage prefix for known asymmetric models.
fn default_document_prefix_for_model(model_id: &str) -> Option<String> {
    let id = model_id.to_lowercase();
    if id.contains("e5-") || id.contains("e5_") {
        Some("passage: ".into())
    } else {
        // BGE and the other known API presets document a query instruction but
        // no passage prefix, so leave indexed text unchanged.
        None
    }
}

/// Try to fetch the default query prompt from a model's HuggingFace `tokenizer_config.json`.
///
/// Many HuggingFace models store a default retrieval prompt under
/// `prompts.retrieval` or `default_prompt` in their tokenizer config.
/// This is a best-effort fallback; returns `None` on any failure.
fn fetch_query_prefix_from_hf(model_id: &str) -> Option<String> {
    // Only attempt if model_id looks like a HuggingFace repo (contains '/').
    if !model_id.contains('/') {
        return None;
    }
    let cache = QUERY_PREFIX_CACHE.get_or_init(|| Mutex::new(HashMap::new()));
    cached_lookup_or_fetch(cache, model_id, || {
        fetch_query_prefix_from_hf_uncached(model_id)
    })
}

fn fetch_query_prefix_from_hf_uncached(model_id: &str) -> Option<String> {
    let url = format!(
        "https://huggingface.co/{}/resolve/main/tokenizer_config.json",
        model_id
    );
    debug!(model_id, url = %url, "fetching query prefix from HuggingFace");

    crate::init_tls();
    let body = reqwest::blocking::Client::builder()
        .timeout(Duration::from_secs(5))
        .build()
        .ok()?
        .get(&url)
        .send()
        .ok()?
        .text()
        .ok()?;

    let config: serde_json::Value = serde_json::from_str(&body).ok()?;

    // Check common locations for a retrieval/query prompt.
    let prompt = config
        .get("prompts")
        .and_then(|p| p.get("retrieval").or_else(|| p.get("query")))
        .and_then(|v| v.as_str())
        .or_else(|| config.get("default_prompt").and_then(|v| v.as_str()));

    prompt.map(|p| {
        debug!(model_id, prefix = %p, "auto-detected query prefix from HuggingFace");
        format!("{p} ")
    })
}

type QueryPrefixCacheEntry = Arc<OnceLock<Option<String>>>;
type QueryPrefixCache = Mutex<HashMap<String, QueryPrefixCacheEntry>>;

static QUERY_PREFIX_CACHE: OnceLock<QueryPrefixCache> = OnceLock::new();

fn cached_lookup_or_fetch<F>(cache: &QueryPrefixCache, model_id: &str, fetch: F) -> Option<String>
where
    F: FnOnce() -> Option<String>,
{
    let entry = {
        let mut entries = cache.lock().unwrap();
        entries
            .entry(model_id.to_string())
            .or_insert_with(|| Arc::new(OnceLock::new()))
            .clone()
    };

    // Each model gets its own single-flight cell. The map lock is released
    // before the request starts, so a slow lookup cannot block other models.
    entry.get_or_init(fetch).clone()
}

// ── OpenAI-compatible provider ───────────────────────────────────────

/// OpenAI-compatible embedding provider.
///
/// Works with any API that implements the OpenAI `/v1/embeddings` endpoint,
/// including Nebius, Together, Fireworks, vLLM, etc.
pub struct OpenAiProvider {
    client: reqwest::Client,
    config: EmbeddingProviderConfig,
}

impl OpenAiProvider {
    /// Create a new provider from configuration.
    pub fn new(config: EmbeddingProviderConfig) -> Result<Self> {
        crate::init_tls();
        let client = reqwest::Client::builder()
            .timeout(config.timeout)
            .build()
            .context("failed to create HTTP client")?;

        Ok(Self { client, config })
    }

    /// Build the embeddings endpoint URL.
    fn endpoint_url(&self) -> String {
        let base = self.config.base_url.trim_end_matches('/');
        format!("{base}/embeddings")
    }

    /// Execute a single embedding API call with retry logic.
    ///
    /// Rate limit errors (429) get extra retries with longer backoffs
    /// to respect API quotas while still completing the operation.
    async fn call_api(&self, texts: &[String]) -> Result<Vec<Vec<f32>>, EmbeddingError> {
        let url = self.endpoint_url();
        let body = EmbeddingRequest {
            model: &self.config.model_id,
            input: texts,
        };

        let mut last_err = None;
        let mut retries = 0;
        loop {
            if retries > 0 {
                let is_rate_limit = matches!(last_err, Some(EmbeddingError::RateLimitError { .. }));
                let delay = if is_rate_limit {
                    // Rate limit: wait 2-4s with exponential backoff.
                    Duration::from_secs(2 + u64::from(retries.min(2)))
                } else {
                    Duration::from_millis(500 * 2u64.pow(retries.min(5) - 1))
                };
                debug!(
                    attempt = retries + 1,
                    delay_ms = delay.as_millis(),
                    rate_limited = is_rate_limit,
                    "retrying embedding API"
                );
                tokio::time::sleep(delay).await;
            }

            match self.send_request(&url, &body).await {
                Ok(vectors) => return Ok(vectors),
                Err(e) => {
                    // A timed-out request may still be running behind a proxy,
                    // so replaying it can create an abandoned-work queue.
                    // Context-size errors are handled by embed_batch_resilient.
                    if !is_retryable_error(&e) {
                        return Err(e);
                    }

                    let retry_limit = retry_limit_for_error(&e, self.config.max_retries);
                    warn!(
                        attempt = retries + 1,
                        max = retry_limit + 1,
                        error = %e,
                        "embedding API call failed"
                    );
                    if retries >= retry_limit {
                        return Err(e);
                    }
                    last_err = Some(e);
                    retries += 1;
                }
            }
        }
    }

    /// Send a single HTTP request and parse the response.
    async fn send_request(
        &self,
        url: &str,
        body: &EmbeddingRequest<'_>,
    ) -> Result<Vec<Vec<f32>>, EmbeddingError> {
        let response = self
            .client
            .post(url)
            .header("Authorization", format!("Bearer {}", self.config.api_key))
            .header("Content-Type", "application/json")
            .json(body)
            .send()
            .await
            .map_err(|e| {
                if e.is_timeout() {
                    EmbeddingError::TimeoutError {
                        message: format!("request to embedding API timed out: {e}"),
                    }
                } else if e.is_connect() {
                    EmbeddingError::ConnectionError {
                        message: format!("failed to connect to embedding API: {e}"),
                    }
                } else {
                    EmbeddingError::ConnectionError {
                        message: format!("request failed: {e}"),
                    }
                }
            })?;

        let status = response.status().as_u16();

        if status == 401 || status == 403 {
            let text =
                read_error_response_text(response, "failed to read authentication error response")
                    .await?;
            return Err(EmbeddingError::AuthError {
                message: sanitize_error_message(&text),
            });
        }

        if status == 429 {
            let text =
                read_error_response_text(response, "failed to read rate limit response").await?;
            return Err(EmbeddingError::RateLimitError {
                message: sanitize_error_message(&text),
            });
        }

        if !response.status().is_success() {
            let text =
                read_error_response_text(response, "failed to read embedding error response")
                    .await?;
            // Some providers return 400 with "Unable to process" for transient
            // overload conditions. Treat these as rate limits so they get retried.
            if status == 400 && text.contains("Unable to process") {
                return Err(EmbeddingError::RateLimitError {
                    message: sanitize_error_message(&text),
                });
            }
            return Err(EmbeddingError::ApiError {
                status,
                message: sanitize_error_message(&text),
            });
        }

        let resp: EmbeddingResponse = response
            .json()
            .await
            .map_err(|error| response_read_error(error, "failed to parse embedding response"))?;

        // Sort by index to ensure correct ordering.
        let mut data = resp.data;
        data.sort_by_key(|d| d.index);

        let vectors: Vec<Vec<f32>> = data.into_iter().map(|d| d.embedding).collect();

        if vectors.len() != body.input.len() {
            return Err(EmbeddingError::ResponseError {
                message: format!(
                    "expected {} embeddings, got {}",
                    body.input.len(),
                    vectors.len()
                ),
            });
        }

        Ok(vectors)
    }
}

fn response_read_error(error: reqwest::Error, context: &str) -> EmbeddingError {
    if error.is_timeout() {
        EmbeddingError::TimeoutError {
            message: format!("{context}: {error}"),
        }
    } else {
        EmbeddingError::ResponseError {
            message: format!("{context}: {error}"),
        }
    }
}

async fn read_error_response_text(
    response: reqwest::Response,
    context: &str,
) -> Result<String, EmbeddingError> {
    match response.text().await {
        Ok(text) => Ok(text),
        Err(error) if error.is_timeout() => Err(response_read_error(error, context)),
        Err(error) => Ok(format!("{context}: {error}")),
    }
}

impl EmbeddingProvider for OpenAiProvider {
    async fn embed_batch(&self, texts: &[String]) -> Result<Vec<Vec<f32>>, EmbeddingError> {
        if texts.is_empty() {
            return Ok(Vec::new());
        }
        self.call_api(texts).await
    }

    fn expected_dim(&self) -> Option<usize> {
        None
    }

    fn prepare_query_text(&self, query: &str) -> String {
        match &self.config.query_prefix {
            Some(prefix) => format!("{prefix}{query}"),
            None => query.to_string(),
        }
    }

    fn prepare_document_text(&self, document: &str) -> String {
        match &self.config.document_prefix {
            Some(prefix) => format!("{prefix}{document}"),
            None => document.to_string(),
        }
    }

    fn document_prefix_identity(&self) -> String {
        self.config
            .document_prefix
            .as_deref()
            .unwrap_or("")
            .trim()
            .to_string()
    }

    fn max_batch_size(&self) -> Option<usize> {
        provider_batch_limit(&self.config)
    }
}

// ── Cached embedding provider ────────────────────────────────────────

/// An in-memory LRU-style cache wrapper around any `EmbeddingProvider`.
///
/// Caches `query_text → embedding_vector` so that repeated identical queries
/// (common during interactive search sessions) skip the API call entirely.
/// The first query pays full API cost; subsequent identical queries resolve
/// in microseconds from cache.
///
/// The cache uses a bounded `HashMap` with capacity-based eviction: when the
/// cache exceeds `max_entries`, the oldest entry (by insertion time) is removed.
pub struct CachedEmbeddingProvider<P> {
    inner: P,
    cache: Mutex<LruCache>,
    /// Namespace prefix for cache keys (typically the model id or provider URL).
    /// Prevents cross-model cache poisoning when the same query text is embedded
    /// by different models producing incompatible vectors.
    namespace: String,
}

/// Simple bounded cache with insertion-order eviction.
struct LruCache {
    entries: HashMap<String, CacheEntry>,
    max_entries: usize,
}

struct CacheEntry {
    vector: Vec<f32>,
    inserted_at: Instant,
}

impl LruCache {
    fn new(max_entries: usize) -> Self {
        Self {
            entries: HashMap::with_capacity(max_entries),
            max_entries: max_entries.max(1),
        }
    }

    fn get(&self, key: &str) -> Option<&Vec<f32>> {
        self.entries.get(key).map(|e| &e.vector)
    }

    fn insert(&mut self, key: String, vector: Vec<f32>) {
        // Evict oldest entry if at capacity and this is a new key.
        if self.entries.len() >= self.max_entries
            && !self.entries.contains_key(&key)
            && let Some(oldest_key) = self
                .entries
                .iter()
                .min_by_key(|(_, e)| e.inserted_at)
                .map(|(k, _)| k.clone())
        {
            self.entries.remove(&oldest_key);
        }
        self.entries.insert(
            key,
            CacheEntry {
                vector,
                inserted_at: Instant::now(),
            },
        );
    }

    #[cfg(test)]
    fn len(&self) -> usize {
        self.entries.len()
    }
}

impl<P: EmbeddingProvider> CachedEmbeddingProvider<P> {
    /// Create a new cached provider wrapping the given inner provider.
    ///
    /// `max_entries` controls the maximum number of cached embeddings.
    /// A reasonable default for interactive use is 256–1024.
    ///
    /// `namespace` is a stable identifier for the embedding model (e.g.,
    /// model name, provider URL, or config key). It prefixes every cache key
    /// so that switching models invalidates the cache instead of silently
    /// returning vectors from the wrong model.
    pub fn new(inner: P, max_entries: usize) -> Self {
        Self {
            inner,
            cache: Mutex::new(LruCache::new(max_entries)),
            namespace: String::new(),
        }
    }

    /// Create a cached provider with an explicit namespace for cache key isolation.
    pub fn with_namespace(inner: P, max_entries: usize, namespace: impl Into<String>) -> Self {
        Self {
            inner,
            cache: Mutex::new(LruCache::new(max_entries)),
            namespace: namespace.into(),
        }
    }

    /// Return the number of currently cached entries.
    #[cfg(test)]
    pub fn cache_size(&self) -> usize {
        self.cache.lock().unwrap().len()
    }

    /// Build a cache key that includes the namespace (model identity) so that
    /// switching embedding models invalidates the cache rather than silently
    /// returning vectors from a different model.
    fn make_cache_key(&self, query: &str) -> String {
        if self.namespace.is_empty() {
            query.to_string()
        } else {
            format!("{}\0{}", self.namespace, query)
        }
    }
}

impl<P: EmbeddingProvider> EmbeddingProvider for CachedEmbeddingProvider<P> {
    async fn embed_batch(&self, texts: &[String]) -> Result<Vec<Vec<f32>>, EmbeddingError> {
        if texts.is_empty() {
            return Ok(Vec::new());
        }

        // For single-text batches (query embedding), check cache first.
        if texts.len() == 1 {
            let key = self.make_cache_key(&texts[0]);
            if let Some(cached) = self.cache.lock().unwrap().get(&key) {
                debug!("embedding cache hit for query");
                return Ok(vec![cached.clone()]);
            }
        }

        // Cache miss — delegate to inner provider.
        let vectors = self.inner.embed_batch(texts).await?;

        // Cache single-text results (query embeddings).
        if texts.len() == 1 && vectors.len() == 1 {
            let key = self.make_cache_key(&texts[0]);
            let vector = vectors[0].clone();
            self.cache.lock().unwrap().insert(key, vector);
            debug!("embedding cached for query");
        }

        Ok(vectors)
    }

    async fn embed_batch_cancellable(
        &self,
        texts: &[String],
        cancel: &CancellationToken,
    ) -> Result<Vec<Vec<f32>>, EmbeddingError> {
        if texts.is_empty() {
            return Ok(Vec::new());
        }

        // Query cache hits do not perform provider work, so they can return
        // immediately even when cancellation has already fired.
        if texts.len() == 1 {
            let key = self.make_cache_key(&texts[0]);
            if let Some(cached) = self.cache.lock().unwrap().get(&key) {
                debug!("embedding cache hit for query");
                return Ok(vec![cached.clone()]);
            }
        }

        // Preserve cancellation on cache misses so wrapped local providers
        // can stop between their own inference sub-batches.
        let vectors = self.inner.embed_batch_cancellable(texts, cancel).await?;

        if texts.len() == 1 && vectors.len() == 1 {
            let key = self.make_cache_key(&texts[0]);
            let vector = vectors[0].clone();
            self.cache.lock().unwrap().insert(key, vector);
            debug!("embedding cached for query");
        }

        Ok(vectors)
    }

    fn expected_dim(&self) -> Option<usize> {
        self.inner.expected_dim()
    }

    fn prepare_query_text(&self, query: &str) -> String {
        self.inner.prepare_query_text(query)
    }

    fn prepare_document_text(&self, document: &str) -> String {
        self.inner.prepare_document_text(document)
    }

    fn document_prefix_identity(&self) -> String {
        self.inner.document_prefix_identity()
    }

    fn max_batch_size(&self) -> Option<usize> {
        self.inner.max_batch_size()
    }
}

// ── Batch embedding orchestrator ─────────────────────────────────────

const TRUNCATED_EMBEDDING_MARKER: &str = "\n\n[truncated for embedding]";

fn provider_batch_limit(config: &EmbeddingProviderConfig) -> Option<usize> {
    let base_url = config.base_url.to_ascii_lowercase();
    let model_id = config.model_id.to_ascii_lowercase();

    if base_url.contains("generativelanguage.googleapis.com")
        || base_url.contains("ai.google.dev")
        || (base_url.contains("googleapis.com") && model_id.contains("gemini"))
        || model_id.contains("gemini")
    {
        return Some(100);
    }

    // Voyage AI enforces both a per-batch input count cap (128) and a
    // per-batch token cap (120k for voyage-code-3). The token cap is handled
    // adaptively via context_size_info; this caps input count as a safety net.
    if base_url.contains("api.voyageai.com") || model_id.starts_with("voyage-") {
        return Some(128);
    }

    None
}

fn effective_batch_size<P: EmbeddingProvider>(provider: &P, configured_batch_size: usize) -> usize {
    let configured_batch_size = configured_batch_size.max(1);
    match provider.max_batch_size().filter(|limit| *limit > 0) {
        Some(provider_limit) if provider_limit < configured_batch_size => {
            debug!(
                configured_batch_size,
                provider_limit,
                effective_batch_size = provider_limit,
                "clamped embedding batch size to provider limit"
            );
            provider_limit
        }
        _ => configured_batch_size,
    }
}

fn retry_limit_for_error(error: &EmbeddingError, configured_retries: u32) -> u32 {
    if matches!(error, EmbeddingError::RateLimitError { .. }) {
        configured_retries.saturating_add(4)
    } else {
        configured_retries
    }
}

fn is_retryable_error(error: &EmbeddingError) -> bool {
    !matches!(
        error,
        EmbeddingError::AuthError { .. } | EmbeddingError::TimeoutError { .. }
    ) && !is_context_size_error(error)
}

#[derive(Clone)]
struct EmbeddingBatchItem {
    original_index: usize,
    chunk_id: String,
    text: String,
}

fn embedding_error_message(error: &EmbeddingError) -> &str {
    match error {
        EmbeddingError::AuthError { message }
        | EmbeddingError::ConnectionError { message }
        | EmbeddingError::TimeoutError { message }
        | EmbeddingError::ApiError { message, .. }
        | EmbeddingError::RateLimitError { message }
        | EmbeddingError::ResponseError { message } => message,
        EmbeddingError::Cancelled => "embedding cancelled",
    }
}

struct ContextSizeInfo {
    max_tokens: usize,
    input_tokens: Option<usize>,
}

fn context_size_info(error: &EmbeddingError) -> Option<ContextSizeInfo> {
    let message = embedding_error_message(error);
    let lower = message.to_ascii_lowercase();
    if !lower.contains("context size")
        && !lower.contains("exceed_context_size_error")
        && !lower.contains("\"n_ctx\"")
        && !lower.contains("too large to process")
        && !lower.contains("max allowed tokens per submitted batch")
        && !lower.contains("maximum input length")
        && !lower.contains("maximum context length")
    {
        return None;
    }

    static N_CTX_RE: OnceLock<Regex> = OnceLock::new();
    static MAX_CONTEXT_RE: OnceLock<Regex> = OnceLock::new();
    static BATCH_SIZE_RE: OnceLock<Regex> = OnceLock::new();
    static INPUT_TOKENS_RE: OnceLock<Regex> = OnceLock::new();
    static N_PROMPT_RE: OnceLock<Regex> = OnceLock::new();
    static MAX_BATCH_TOKENS_RE: OnceLock<Regex> = OnceLock::new();
    static BATCH_TOKENS_RE: OnceLock<Regex> = OnceLock::new();
    static MAX_INPUT_LENGTH_RE: OnceLock<Regex> = OnceLock::new();
    let n_ctx_re = N_CTX_RE.get_or_init(|| Regex::new(r#""n_ctx"\s*:\s*(\d+)"#).unwrap());
    let max_context_re =
        MAX_CONTEXT_RE.get_or_init(|| Regex::new(r"max context size \((\d+)").unwrap());
    let batch_size_re = BATCH_SIZE_RE.get_or_init(|| Regex::new(r"batch size:\s*(\d+)").unwrap());
    let input_tokens_re =
        INPUT_TOKENS_RE.get_or_init(|| Regex::new(r"input \((\d+) tokens?\)").unwrap());
    let n_prompt_re =
        N_PROMPT_RE.get_or_init(|| Regex::new(r#""n_prompt_tokens"\s*:\s*(\d+)"#).unwrap());
    let max_batch_tokens_re = MAX_BATCH_TOKENS_RE
        .get_or_init(|| Regex::new(r"max allowed tokens per submitted batch is (\d+)").unwrap());
    let batch_tokens_re =
        BATCH_TOKENS_RE.get_or_init(|| Regex::new(r"your batch has (\d+) tokens?").unwrap());
    // OpenAI emits both phrasings depending on endpoint:
    //   `Invalid 'input[3]': maximum input length is 8192 tokens.`   (embeddings)
    //   `This model's maximum context length is 8192 tokens`         (completions-shaped)
    let max_input_length_re = MAX_INPUT_LENGTH_RE
        .get_or_init(|| Regex::new(r"maximum (?:input|context) length is (\d+)").unwrap());

    let max_tokens = n_ctx_re
        .captures(&lower)
        .and_then(|caps| caps.get(1))
        .or_else(|| max_context_re.captures(&lower).and_then(|caps| caps.get(1)))
        .or_else(|| batch_size_re.captures(&lower).and_then(|caps| caps.get(1)))
        .or_else(|| {
            max_batch_tokens_re
                .captures(&lower)
                .and_then(|caps| caps.get(1))
        })
        .or_else(|| {
            max_input_length_re
                .captures(&lower)
                .and_then(|caps| caps.get(1))
        })
        .and_then(|capture| capture.as_str().parse::<usize>().ok())
        .or(Some(8192))?;

    let input_tokens = n_prompt_re
        .captures(&lower)
        .and_then(|caps| caps.get(1))
        .or_else(|| {
            input_tokens_re
                .captures(&lower)
                .and_then(|caps| caps.get(1))
        })
        .or_else(|| {
            batch_tokens_re
                .captures(&lower)
                .and_then(|caps| caps.get(1))
        })
        .and_then(|capture| capture.as_str().parse::<usize>().ok());

    Some(ContextSizeInfo {
        max_tokens,
        input_tokens,
    })
}

fn is_context_size_error(error: &EmbeddingError) -> bool {
    context_size_info(error).is_some()
}

fn truncate_to_char_boundary(text: &str, max_chars: usize) -> &str {
    if text.chars().count() <= max_chars {
        return text;
    }

    text.char_indices()
        .nth(max_chars)
        .map(|(idx, _)| &text[..idx])
        .unwrap_or(text)
}

fn shrink_text_for_context_limit(
    text: &str,
    max_tokens: usize,
    input_tokens: Option<usize>,
) -> String {
    let current_chars = text.chars().count();
    if current_chars <= 1 {
        return text.to_string();
    }

    let marker_chars = TRUNCATED_EMBEDDING_MARKER.chars().count();

    let target_chars = if let Some(actual_tokens) = input_tokens.filter(|&t| t > 0) {
        // We know exactly how many tokens the current text produced.
        // Compute the real chars-per-token ratio and truncate precisely,
        // targeting 85% of the context limit as a safety margin.
        let ratio = current_chars as f64 / actual_tokens as f64;
        let safe_limit = (max_tokens as f64 * 0.85) as usize;
        (safe_limit as f64 * ratio) as usize
    } else {
        // No actual token count available. Use 75% of current length as
        // a conservative fallback (always makes progress).
        current_chars.saturating_mul(3) / 4
    }
    .max(1)
    .saturating_sub(marker_chars);

    if target_chars >= current_chars {
        return text.to_string();
    }

    let truncated = truncate_to_char_boundary(text, target_chars).trim_end();
    if truncated.is_empty() {
        return text.to_string();
    }

    let mut shrunk = truncated.to_string();
    shrunk.push_str(TRUNCATED_EMBEDDING_MARKER);
    shrunk
}

async fn embed_batch_resilient<P: EmbeddingProvider>(
    provider: &P,
    items: Vec<EmbeddingBatchItem>,
    cancel: &CancellationToken,
) -> Result<Vec<(usize, String, Vec<f32>)>, EmbeddingError> {
    let mut pending = vec![items];
    let mut completed = Vec::new();

    while let Some(batch) = pending.pop() {
        if cancel.is_cancelled() {
            return Err(EmbeddingError::Cancelled);
        }
        let texts: Vec<String> = batch.iter().map(|item| item.text.clone()).collect();

        let result = tokio::select! {
            biased;
            result = provider.embed_batch_cancellable(&texts, cancel) => result,
            _ = cancel.cancelled() => Err(EmbeddingError::Cancelled),
        };

        match result {
            Ok(vectors) => {
                completed.extend(
                    batch
                        .into_iter()
                        .zip(vectors)
                        .map(|(item, vector)| (item.original_index, item.chunk_id, vector)),
                );
            }
            Err(error) => {
                let Some(info) = context_size_info(&error) else {
                    return Err(error);
                };

                if batch.len() > 1 {
                    let mid = batch.len() / 2;
                    debug!(
                        batch_size = batch.len(),
                        token_limit = info.max_tokens,
                        "embedding batch exceeded provider context limit, retrying in smaller batches"
                    );
                    pending.push(batch[mid..].to_vec());
                    pending.push(batch[..mid].to_vec());
                    continue;
                }

                let item = batch.into_iter().next().expect("single-item batch");
                let shrunk_text =
                    shrink_text_for_context_limit(&item.text, info.max_tokens, info.input_tokens);
                if shrunk_text == item.text {
                    return Err(error);
                }

                warn!(
                    chunk_id = %item.chunk_id,
                    token_limit = info.max_tokens,
                    input_tokens = ?info.input_tokens,
                    original_chars = item.text.chars().count(),
                    shrunk_chars = shrunk_text.chars().count(),
                    "embedding input exceeded provider context limit; retrying with a truncated text"
                );
                pending.push(vec![EmbeddingBatchItem {
                    text: shrunk_text,
                    ..item
                }]);
            }
        }
    }

    completed.sort_by_key(|(original_index, _, _)| *original_index);
    Ok(completed)
}

/// Embed all chunks using the given provider with concurrent batch processing.
///
/// Splits chunks into batches and sends up to `max_concurrent` batches
/// simultaneously. This significantly reduces wall-clock time for large
/// repositories where the embedding API is the bottleneck.
///
/// Returns a vector of `(chunk_id, embedding)` pairs in the same order
/// as the input chunks.
pub async fn embed_chunks_concurrent<P: EmbeddingProvider>(
    provider: &P,
    chunks: &[Chunk],
    batch_size: usize,
    max_concurrent: usize,
    max_chunk_bytes: usize,
) -> Result<Vec<(String, Vec<f32>)>, EmbeddingError> {
    embed_chunks_concurrent_with_progress_and_cancellation(
        provider,
        chunks,
        batch_size,
        max_concurrent,
        max_chunk_bytes,
        &CancellationToken::new(),
        |_, _| {},
    )
    .await
}

/// Embed chunks concurrently while allowing the caller to cancel active batches.
///
/// The token remains owned by the indexing operation. Cancellation drops every
/// active provider future and returns [`EmbeddingError::Cancelled`] without
/// reporting further progress.
pub(crate) async fn embed_chunks_concurrent_with_progress_and_cancellation<P, F>(
    provider: &P,
    chunks: &[Chunk],
    batch_size: usize,
    max_concurrent: usize,
    max_chunk_bytes: usize,
    cancel: &CancellationToken,
    on_progress: F,
) -> Result<Vec<(String, Vec<f32>)>, EmbeddingError>
where
    P: EmbeddingProvider,
    F: Fn(usize, usize),
{
    if chunks.is_empty() {
        return Ok(Vec::new());
    }

    let batch_size = effective_batch_size(provider, batch_size);
    let max_concurrent = max_concurrent.max(1);
    let total = chunks.len();
    let total_batches = total.div_ceil(batch_size);

    debug!(
        total_chunks = total,
        batch_size, total_batches, max_concurrent, "starting concurrent embedding"
    );

    let mut indexed_chunks: Vec<(usize, &Chunk)> = chunks.iter().enumerate().collect();
    indexed_chunks.sort_by_key(|(_, c)| c.content.len());

    let body_budget = budget_after_prefix(max_chunk_bytes, document_prefix_overhead(provider));

    let batch_inputs: Vec<Vec<EmbeddingBatchItem>> = indexed_chunks
        .chunks(batch_size)
        .map(|batch| {
            batch
                .iter()
                .map(|(orig_idx, chunk)| EmbeddingBatchItem {
                    original_index: *orig_idx,
                    chunk_id: chunk.id.clone(),
                    text: provider
                        .prepare_document_text(&chunk_to_embedding_text(chunk, body_budget)),
                })
                .collect()
        })
        .collect();

    let mut all_results: Vec<(usize, String, Vec<f32>)> = Vec::with_capacity(total);
    let mut done_count: usize = 0;

    for group_start in (0..batch_inputs.len()).step_by(max_concurrent) {
        let group_end = (group_start + max_concurrent).min(batch_inputs.len());
        let group = &batch_inputs[group_start..group_end];

        let futures: Vec<_> = group
            .iter()
            .enumerate()
            .map(|(i, items)| {
                let batch_idx = group_start + i;
                async move {
                    debug!(batch = batch_idx + 1, total_batches, "embedding batch");
                    embed_batch_resilient(provider, items.clone(), cancel).await
                }
            })
            .collect();

        let results = futures::future::join_all(futures).await;
        for result in results {
            // Batch errors (real provider failures or Cancelled) win over a
            // pending cancellation so the caller sees the actual failure.
            let batch_results = result?;
            if cancel.is_cancelled() {
                return Err(EmbeddingError::Cancelled);
            }
            done_count += batch_results.len();
            all_results.extend(batch_results);
            on_progress(done_count, total);
        }
    }

    all_results.sort_by_key(|(orig_idx, _, _)| *orig_idx);

    let results: Vec<(String, Vec<f32>)> = all_results
        .into_iter()
        .map(|(_, id, vec)| (id, vec))
        .collect();

    Ok(results)
}

/// Format a chunk's content for embedding.
///
/// Prepends metadata context (language, symbol info) to help the model
/// produce more code-aware embeddings. When `max_bytes > 0`, the code
/// content is truncated so the total text fits within the budget.
fn chunk_to_embedding_text(chunk: &Chunk, max_bytes: usize) -> String {
    if max_bytes > 0 {
        chunk_text::build_embedding_text_bounded(chunk, max_bytes)
    } else {
        chunk_text::build_embedding_text(chunk)
    }
}

/// Measure how many bytes `prepare_document_text` adds to a passage.
///
/// Read off the provider rather than off the model config, so it stays correct
/// for any implementation of the hook, including the identity default.
fn document_prefix_overhead<P: EmbeddingProvider>(provider: &P) -> usize {
    const PROBE: &str = "x";
    provider
        .prepare_document_text(PROBE)
        .len()
        .saturating_sub(PROBE.len())
}

/// Shrink the chunk byte budget so the document prefix fits inside it.
///
/// The prefix spends the same context window the budget exists to protect, so
/// it has to be reserved before truncation rather than added after it.
///
/// `0` means "unbounded" to [`chunk_to_embedding_text`], so a prefix at least
/// as large as the budget floors at 1 instead of saturating to 0, which would
/// silently switch truncation off entirely.
fn budget_after_prefix(max_chunk_bytes: usize, prefix_overhead: usize) -> usize {
    if max_chunk_bytes == 0 {
        return 0;
    }
    max_chunk_bytes.saturating_sub(prefix_overhead).max(1)
}

// ── Sanitization ─────────────────────────────────────────────────────

/// Remove any potential API key fragments from error messages.
///
/// API error bodies sometimes echo back parts of the request. This
/// ensures we never propagate credential material in error messages.
fn sanitize_error_message(msg: &str) -> String {
    // Truncate at a safe char boundary to avoid panicking on multi-byte UTF-8.
    let truncated = if msg.len() > 500 {
        let end = msg
            .char_indices()
            .take_while(|(i, _)| *i < 500)
            .last()
            .map(|(i, c)| i + c.len_utf8())
            .unwrap_or(0);
        &msg[..end]
    } else {
        msg
    };
    // Strip anything that looks like a bearer token or key.
    let sanitized = truncated
        .replace(|c: char| !c.is_ascii_graphic() && c != ' ', " ")
        .trim()
        .to_string();
    if sanitized.is_empty() {
        "no details available".to_string()
    } else {
        sanitized
    }
}

// ── API request/response types ───────────────────────────────────────

#[derive(Serialize)]
struct EmbeddingRequest<'a> {
    model: &'a str,
    input: &'a [String],
}

#[derive(Deserialize)]
struct EmbeddingResponse {
    data: Vec<EmbeddingData>,
}

#[derive(Deserialize)]
struct EmbeddingData {
    embedding: Vec<f32>,
    index: usize,
}

#[cfg(test)]
pub(crate) mod test_helpers {
    use super::*;

    /// A mock embedding provider for unit testing.
    ///
    /// Returns deterministic vectors based on the input text length.
    pub struct MockProvider {
        pub dim: usize,
        pub fail_with: Option<EmbeddingError>,
    }

    impl MockProvider {
        pub fn new(dim: usize) -> Self {
            Self {
                dim,
                fail_with: None,
            }
        }

        pub fn failing(error: EmbeddingError) -> Self {
            Self {
                dim: 4,
                fail_with: Some(error),
            }
        }
    }

    impl EmbeddingProvider for MockProvider {
        async fn embed_batch(&self, texts: &[String]) -> Result<Vec<Vec<f32>>, EmbeddingError> {
            if let Some(ref err) = self.fail_with {
                // Re-create the error since EmbeddingError is not Clone.
                return Err(match err {
                    EmbeddingError::AuthError { message } => EmbeddingError::AuthError {
                        message: message.clone(),
                    },
                    EmbeddingError::ConnectionError { message } => {
                        EmbeddingError::ConnectionError {
                            message: message.clone(),
                        }
                    }
                    EmbeddingError::TimeoutError { message } => EmbeddingError::TimeoutError {
                        message: message.clone(),
                    },
                    EmbeddingError::ApiError { status, message } => EmbeddingError::ApiError {
                        status: *status,
                        message: message.clone(),
                    },
                    EmbeddingError::RateLimitError { message } => EmbeddingError::RateLimitError {
                        message: message.clone(),
                    },
                    EmbeddingError::ResponseError { message } => EmbeddingError::ResponseError {
                        message: message.clone(),
                    },
                    EmbeddingError::Cancelled => EmbeddingError::Cancelled,
                });
            }

            Ok(texts
                .iter()
                .map(|text| {
                    // Deterministic hash-based seed from text content.
                    let mut hash: u64 = 5381;
                    for byte in text.bytes() {
                        hash = hash.wrapping_mul(33).wrapping_add(byte as u64);
                    }
                    let seed = hash as f32;
                    (0..self.dim)
                        .map(|i| {
                            let x = seed * (i as f32 + 1.0) * 0.001;
                            (x.sin() + 1.0) / 2.0
                        })
                        .collect()
                })
                .collect())
        }

        fn expected_dim(&self) -> Option<usize> {
            Some(self.dim)
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::Arc;
    use std::sync::atomic::{AtomicUsize, Ordering};
    use tokio::io::{AsyncReadExt, AsyncWriteExt};

    async fn provider_with_truncated_error_body(
        status: &'static str,
    ) -> (OpenAiProvider, tokio::task::JoinHandle<()>) {
        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
        let address = listener.local_addr().unwrap();
        let server = tokio::spawn(async move {
            let (mut stream, _) = listener.accept().await.unwrap();
            let mut request = [0_u8; 2048];
            let _ = stream.read(&mut request).await;
            let response = format!(
                "HTTP/1.1 {status}\r\nContent-Length: 32\r\nConnection: close\r\n\r\nshort"
            );
            stream.write_all(response.as_bytes()).await.unwrap();
            stream.shutdown().await.unwrap();
        });
        let config = EmbeddingProviderConfig::new(
            format!("http://{address}"),
            "test-model".into(),
            "test-key".into(),
        )
        .with_max_retries(0);
        (OpenAiProvider::new(config).unwrap(), server)
    }

    /// Cancels the token mid-request but still returns vectors, modelling an
    /// embedding response that completes at the same moment cancellation fires.
    struct CancelThenSucceedProvider;

    impl EmbeddingProvider for CancelThenSucceedProvider {
        async fn embed_batch(&self, texts: &[String]) -> Result<Vec<Vec<f32>>, EmbeddingError> {
            Ok(vec![vec![0.0; 8]; texts.len()])
        }

        async fn embed_batch_cancellable(
            &self,
            texts: &[String],
            cancel: &CancellationToken,
        ) -> Result<Vec<Vec<f32>>, EmbeddingError> {
            cancel.cancel();
            self.embed_batch(texts).await
        }

        fn expected_dim(&self) -> Option<usize> {
            Some(8)
        }
    }

    struct CancellationAwareProvider;

    impl EmbeddingProvider for CancellationAwareProvider {
        async fn embed_batch(&self, _texts: &[String]) -> Result<Vec<Vec<f32>>, EmbeddingError> {
            Err(EmbeddingError::ResponseError {
                message: "non-cancellable path used".to_string(),
            })
        }

        async fn embed_batch_cancellable(
            &self,
            _texts: &[String],
            cancel: &CancellationToken,
        ) -> Result<Vec<Vec<f32>>, EmbeddingError> {
            if cancel.is_cancelled() {
                return Err(EmbeddingError::Cancelled);
            }
            Ok(vec![vec![1.0]])
        }

        fn expected_dim(&self) -> Option<usize> {
            Some(1)
        }
    }

    #[test]
    fn prepare_query_text_with_prefix() {
        let mut config = EmbeddingProviderConfig::new("http://x".into(), "m".into(), "k".into());
        config.query_prefix = Some("Instruct: search\nQuery: ".into());
        let provider = OpenAiProvider::new(config).unwrap();
        assert_eq!(
            provider.prepare_query_text("find foo"),
            "Instruct: search\nQuery: find foo"
        );
    }

    #[test]
    fn prepare_query_text_without_prefix() {
        let config = EmbeddingProviderConfig::new("http://x".into(), "m".into(), "k".into());
        let provider = OpenAiProvider::new(config).unwrap();
        assert_eq!(provider.prepare_query_text("find foo"), "find foo");
    }

    #[test]
    fn document_prefix_identity_normalizes_and_defaults_to_empty() {
        let mut config =
            EmbeddingProviderConfig::new("http://x".into(), "vendor/model".into(), "k".into());
        assert_eq!(
            OpenAiProvider::new(config.clone())
                .unwrap()
                .document_prefix_identity(),
            ""
        );

        config.document_prefix = Some("  passage: ".into());
        assert_eq!(
            OpenAiProvider::new(config.clone())
                .unwrap()
                .document_prefix_identity(),
            "passage:"
        );

        // An empty prefix embeds documents unprefixed, so it must share the
        // bare identity instead of demanding its own re-index.
        config.document_prefix = Some("   ".into());
        assert_eq!(
            OpenAiProvider::new(config)
                .unwrap()
                .document_prefix_identity(),
            ""
        );
    }

    #[test]
    fn api_document_prefix_matches_known_model_conventions() {
        let mut e5_config = EmbeddingProviderConfig::new(
            "http://x".into(),
            "intfloat/e5-large-v2".into(),
            "k".into(),
        );
        e5_config.document_prefix = default_document_prefix_for_model(&e5_config.model_id);
        let e5 = OpenAiProvider::new(e5_config).unwrap();
        assert_eq!(
            e5.prepare_document_text("fn main() {}"),
            "passage: fn main() {}"
        );

        for model_id in [
            "jinaai/jina-embeddings-v5-text-nano-retrieval",
            "minishlab/potion-code-16M-v2",
        ] {
            let mut config =
                EmbeddingProviderConfig::new("http://x".into(), model_id.into(), "k".into());
            config.document_prefix = default_document_prefix_for_model(&config.model_id);
            let provider = OpenAiProvider::new(config).unwrap();
            assert_eq!(
                provider.prepare_document_text("fn main() {}"),
                "fn main() {}"
            );
        }
    }

    #[tokio::test]
    async fn cached_provider_forwards_cancellation_on_cache_miss() {
        let cached = CachedEmbeddingProvider::new(CancellationAwareProvider, 8);
        let cancel = CancellationToken::new();
        cancel.cancel();

        let result = cached
            .embed_batch_cancellable(&["uncached query".to_string()], &cancel)
            .await;

        assert!(matches!(result, Err(EmbeddingError::Cancelled)));
        assert_eq!(cached.cache_size(), 0);
    }

    #[test]
    fn auto_detect_qwen3_prefix() {
        let prefix = default_query_prefix_for_model("Qwen/Qwen3-Embedding-8B");
        assert!(prefix.is_some());
        assert!(prefix.unwrap().contains("Query: "));
    }

    #[test]
    fn auto_detect_coderankembed_prefix() {
        let prefix = default_query_prefix_for_model("krlvi/CodeRankEmbed");
        // Exact: the published prompt in the model's
        // `config_sentence_transformers.json`, trailing space included.
        assert_eq!(
            prefix.as_deref(),
            Some("Represent this query for searching relevant code: ")
        );
    }

    /// The local ONNX path and the API path must embed the same query
    /// identically. They apply the prefix differently (`query_text` trims the
    /// *prefix* and rejoins it with one space, `prepare_query_text`
    /// concatenates a prefix that already carries its own trailing space), so
    /// this pins the resulting text rather than the constant.
    ///
    /// Neither path touches the query itself, so an un-normalized query has to
    /// survive verbatim and identically on both sides; that case is covered
    /// here so a one-sided `trim()` cannot be added without failing.
    #[test]
    fn coderankembed_query_text_matches_across_local_and_api_paths() {
        let cases = [
            (
                "find router code",
                "Represent this query for searching relevant code: find router code",
            ),
            (
                "  find router code  ",
                "Represent this query for searching relevant code:   find router code  ",
            ),
        ];

        for (query, expected) in cases {
            let local =
                crate::local_models::LocalEmbeddingModelConfig::coderankembed().query_text(query);

            let mut config = EmbeddingProviderConfig::new(
                "http://x".into(),
                "krlvi/CodeRankEmbed".into(),
                "k".into(),
            );
            config.query_prefix = default_query_prefix_for_model(&config.model_id);
            let api = OpenAiProvider::new(config)
                .unwrap()
                .prepare_query_text(query);

            assert_eq!(local, api, "paths diverged on {query:?}");
            assert_eq!(local, expected, "unexpected prefixed text for {query:?}");
        }
    }

    #[test]
    fn auto_detect_e5_prefix() {
        let prefix = default_query_prefix_for_model("intfloat/e5-large-v2");
        assert!(prefix.is_some());
        assert_eq!(prefix.unwrap(), "query: ");
    }

    #[test]
    fn auto_detect_bge_prefix() {
        let prefix = default_query_prefix_for_model("BAAI/bge-large-en-v1.5");
        assert!(prefix.is_some());
        assert!(prefix.unwrap().contains("Represent this sentence"));
    }

    #[test]
    fn auto_detect_unknown_model_no_prefix() {
        // Unknown model without '/' won't attempt HF fetch.
        let prefix = default_query_prefix_for_model("some-unknown-model");
        assert!(prefix.is_none());
    }

    #[test]
    fn query_prefix_cache_caches_negative_results() {
        let cache: QueryPrefixCache = Mutex::new(HashMap::new());
        let fetches = AtomicUsize::new(0);

        assert_eq!(
            cached_lookup_or_fetch(&cache, "acme/unknown", || {
                fetches.fetch_add(1, Ordering::SeqCst);
                None
            }),
            None
        );
        assert_eq!(
            cached_lookup_or_fetch(&cache, "acme/unknown", || {
                fetches.fetch_add(1, Ordering::SeqCst);
                Some("should not be used".to_string())
            }),
            None
        );
        assert_eq!(fetches.load(Ordering::SeqCst), 1);
    }

    #[test]
    fn query_prefix_cache_single_flights_each_model_without_serializing_models() {
        let cache = Arc::new(Mutex::new(HashMap::new()));
        let first_started = std::sync::Arc::new(std::sync::Barrier::new(2));
        let first_release = std::sync::Arc::new(std::sync::Barrier::new(2));
        let cache_for_thread = cache.clone();
        let first_started_for_thread = first_started.clone();
        let first_release_for_thread = first_release.clone();

        let first = std::thread::spawn(move || {
            cached_lookup_or_fetch(&cache_for_thread, "acme/slow", || {
                first_started_for_thread.wait();
                first_release_for_thread.wait();
                Some("slow".to_string())
            })
        });

        first_started.wait();
        assert_eq!(
            cached_lookup_or_fetch(&cache, "acme/fast", || Some("fast".to_string())),
            Some("fast".to_string())
        );
        first_release.wait();
        assert_eq!(first.join().unwrap(), Some("slow".to_string()));
    }

    #[test]
    fn detect_gemini_batch_limit_from_base_url() {
        let config = EmbeddingProviderConfig::new(
            "https://generativelanguage.googleapis.com/v1beta/openai".into(),
            "text-embedding-004".into(),
            "k".into(),
        );
        let provider = OpenAiProvider::new(config).unwrap();
        assert_eq!(provider.max_batch_size(), Some(100));
    }

    #[test]
    fn detect_gemini_batch_limit_from_model_id() {
        let config = EmbeddingProviderConfig::new(
            "http://localhost:4000/v1".into(),
            "gemini-embedding-001".into(),
            "k".into(),
        );
        let provider = OpenAiProvider::new(config).unwrap();
        assert_eq!(provider.max_batch_size(), Some(100));
    }

    #[test]
    fn non_gemini_provider_has_no_batch_limit_override() {
        let config = EmbeddingProviderConfig::new(
            "https://api.openai.com/v1".into(),
            "text-embedding-3-small".into(),
            "k".into(),
        );
        let provider = OpenAiProvider::new(config).unwrap();
        assert_eq!(provider.max_batch_size(), None);
    }

    #[test]
    fn detect_voyage_batch_limit_from_base_url() {
        let config = EmbeddingProviderConfig::new(
            "https://api.voyageai.com/v1".into(),
            "voyage-code-3".into(),
            "k".into(),
        );
        let provider = OpenAiProvider::new(config).unwrap();
        assert_eq!(provider.max_batch_size(), Some(128));
    }

    #[test]
    fn detect_voyage_batch_limit_from_model_id() {
        let config = EmbeddingProviderConfig::new(
            "http://localhost:4000/v1".into(),
            "voyage-code-3".into(),
            "k".into(),
        );
        let provider = OpenAiProvider::new(config).unwrap();
        assert_eq!(provider.max_batch_size(), Some(128));
    }

    #[test]
    fn context_size_info_parses_voyage_batch_token_error() {
        let err = EmbeddingError::ApiError {
            status: 400,
            message: "{\"detail\":\"Request to model 'voyage-code-3' failed. The max allowed tokens per submitted batch is 120000. Your batch has 124417 tokens after truncation. Please lower the number of tokens in the batch.\"}".to_string(),
        };
        let info = context_size_info(&err).expect("should recognize voyage batch token error");
        assert_eq!(info.max_tokens, 120000);
        assert_eq!(info.input_tokens, Some(124417));
        assert!(is_context_size_error(&err));
    }

    #[test]
    fn context_size_info_parses_openai_max_input_length_error() {
        // OpenAI reports only the limit, not the input token count. See issue #21.
        let err = EmbeddingError::ApiError {
            status: 400,
            message: "{\"error\":{\"message\":\"Invalid 'input[3]': maximum input length is 8192 tokens.\",\"type\":\"invalid_request_error\",\"param\":null,\"code\":null}}".to_string(),
        };
        let info = context_size_info(&err).expect("should recognize openai max input length error");
        assert_eq!(info.max_tokens, 8192);
        assert_eq!(info.input_tokens, None);
        assert!(is_context_size_error(&err));
    }

    #[test]
    fn context_size_info_parses_maximum_context_length_phrasing() {
        // Some OpenAI-compatible endpoints phrase the same limit as "maximum
        // context length". Without this the gate rejected the message, so
        // truncate-and-retry never engaged and an index build aborted on the
        // first oversized chunk. 8191 (not 8192) so the assertion cannot pass
        // on the function's fallback default.
        let err = EmbeddingError::ApiError {
            status: 400,
            message: "This model's maximum context length is 8191 tokens".to_string(),
        };
        assert!(is_context_size_error(&err));
        let info = context_size_info(&err).expect("should recognize max context length error");
        assert_eq!(info.max_tokens, 8191);
    }

    #[test]
    fn context_size_info_ignores_unrelated_errors() {
        // Negative control: an ordinary 400 must not be classified as a
        // context-size error, or it would be retried with truncated text.
        let err = EmbeddingError::ApiError {
            status: 400,
            message: "Invalid API key provided".to_string(),
        };
        assert!(context_size_info(&err).is_none());
        assert!(!is_context_size_error(&err));
    }

    #[test]
    fn rate_limits_receive_only_the_documented_extra_retries() {
        let error = EmbeddingError::RateLimitError {
            message: "busy".into(),
        };
        assert_eq!(retry_limit_for_error(&error, 3), 7);
    }

    #[test]
    fn ordinary_failures_do_not_receive_rate_limit_retries() {
        let error = EmbeddingError::ConnectionError {
            message: "refused".into(),
        };
        assert_eq!(retry_limit_for_error(&error, 3), 3);
    }

    #[test]
    fn timeouts_are_not_retried() {
        let timeout = EmbeddingError::TimeoutError {
            message: "upstream may still be working".into(),
        };
        let connection = EmbeddingError::ConnectionError {
            message: "connection refused".into(),
        };

        assert!(!is_retryable_error(&timeout));
        assert!(is_retryable_error(&connection));
    }

    #[tokio::test]
    async fn delayed_rate_limit_body_is_a_non_retryable_timeout() {
        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
        let address = listener.local_addr().unwrap();
        let request_count = Arc::new(AtomicUsize::new(0));
        let server_request_count = request_count.clone();

        let server = tokio::spawn(async move {
            loop {
                let (mut stream, _) = listener.accept().await.unwrap();
                server_request_count.fetch_add(1, Ordering::SeqCst);
                tokio::spawn(async move {
                    let mut request = [0_u8; 2048];
                    let _ = stream.read(&mut request).await;
                    stream
                        .write_all(
                            b"HTTP/1.1 429 Too Many Requests\r\nContent-Length: 4\r\nConnection: close\r\n\r\n",
                        )
                        .await
                        .unwrap();
                    tokio::time::sleep(Duration::from_millis(200)).await;
                    let _ = stream.write_all(b"slow").await;
                });
            }
        });

        let config = EmbeddingProviderConfig::new(
            format!("http://{address}"),
            "test-model".into(),
            "test-key".into(),
        )
        .with_timeout(Duration::from_millis(50))
        .with_max_retries(3);
        let provider = OpenAiProvider::new(config).unwrap();

        let error = tokio::time::timeout(
            Duration::from_secs(1),
            provider.embed_batch(&["input".to_string()]),
        )
        .await
        .expect("a body timeout must not enter rate-limit backoff")
        .unwrap_err();

        assert!(matches!(error, EmbeddingError::TimeoutError { .. }));
        assert_eq!(request_count.load(Ordering::SeqCst), 1);
        server.abort();
    }

    #[tokio::test]
    async fn truncated_auth_body_preserves_authentication_classification() {
        let (provider, server) = provider_with_truncated_error_body("401 Unauthorized").await;
        let texts = ["input".to_string()];
        let body = EmbeddingRequest {
            model: &provider.config.model_id,
            input: &texts,
        };

        let error = provider
            .send_request(&provider.endpoint_url(), &body)
            .await
            .unwrap_err();

        assert!(matches!(error, EmbeddingError::AuthError { .. }));
        server.await.unwrap();
    }

    #[tokio::test]
    async fn truncated_rate_limit_body_preserves_rate_limit_classification() {
        let (provider, server) = provider_with_truncated_error_body("429 Too Many Requests").await;
        let texts = ["input".to_string()];
        let body = EmbeddingRequest {
            model: &provider.config.model_id,
            input: &texts,
        };

        let error = provider
            .send_request(&provider.endpoint_url(), &body)
            .await
            .unwrap_err();

        assert!(matches!(error, EmbeddingError::RateLimitError { .. }));
        server.await.unwrap();
    }

    #[tokio::test]
    async fn cancellation_after_completed_batch_suppresses_further_progress() {
        let chunks = vec![crate::types::Chunk {
            id: "chunk-0".to_string(),
            file_path: "src/main.rs".to_string(),
            line_start: 1,
            line_end: 1,
            content: "fn main() {}".to_string(),
            language: crate::types::Language::Rust,
            symbol_type: None,
            symbol_name: None,
        }];
        let progress_events = AtomicUsize::new(0);

        let result = embed_chunks_concurrent_with_progress_and_cancellation(
            &CancelThenSucceedProvider,
            &chunks,
            1,
            1,
            0,
            &CancellationToken::new(),
            |_, _| {
                progress_events.fetch_add(1, Ordering::SeqCst);
            },
        )
        .await;

        assert!(matches!(result, Err(EmbeddingError::Cancelled)));
        assert_eq!(progress_events.load(Ordering::SeqCst), 0);
    }

    /// Records the texts it is asked to embed and prefixes passages, so the
    /// test can prove the indexing funnel actually calls the document hook
    /// rather than merely defining it.
    struct RecordingProvider {
        seen: Mutex<Vec<String>>,
    }

    impl EmbeddingProvider for RecordingProvider {
        async fn embed_batch(&self, texts: &[String]) -> Result<Vec<Vec<f32>>, EmbeddingError> {
            self.seen.lock().unwrap().extend(texts.iter().cloned());
            Ok(texts.iter().map(|_| vec![0.0]).collect())
        }

        fn expected_dim(&self) -> Option<usize> {
            Some(1)
        }

        fn prepare_document_text(&self, document: &str) -> String {
            format!("Document: {document}")
        }

        fn prepare_query_text(&self, query: &str) -> String {
            format!("Query: {query}")
        }
    }

    #[tokio::test]
    async fn indexing_applies_the_document_prefix_and_not_the_query_prefix() {
        let chunks = vec![crate::types::Chunk {
            id: "chunk-0".to_string(),
            file_path: "src/main.rs".to_string(),
            line_start: 1,
            line_end: 1,
            content: "fn main() {}".to_string(),
            language: crate::types::Language::Rust,
            symbol_type: None,
            symbol_name: None,
        }];
        let provider = RecordingProvider {
            seen: Mutex::new(Vec::new()),
        };

        embed_chunks_concurrent_with_progress_and_cancellation(
            &provider,
            &chunks,
            1,
            1,
            0,
            &CancellationToken::new(),
            |_, _| {},
        )
        .await
        .expect("embedding should succeed");

        let seen = provider.seen.lock().unwrap();
        assert_eq!(seen.len(), 1, "expected exactly one embedded passage");
        assert!(
            seen[0].starts_with("Document: "),
            "indexing path did not apply the document prefix: {:?}",
            seen[0]
        );
        assert!(
            !seen[0].contains("Query: "),
            "indexed passage was given the query prefix: {:?}",
            seen[0]
        );
        assert!(
            seen[0].contains("fn main() {}"),
            "prefix replaced the chunk body instead of prefixing it: {:?}",
            seen[0]
        );
    }

    #[tokio::test]
    async fn the_document_prefix_fits_inside_the_chunk_byte_budget() {
        const BUDGET: usize = 200;
        let chunks = vec![crate::types::Chunk {
            id: "chunk-0".to_string(),
            file_path: "src/main.rs".to_string(),
            line_start: 1,
            line_end: 200,
            // Far past the budget, so the bounded builder is the thing that
            // decides the length. Short lines keep the line-boundary cut from
            // landing well inside the budget and hiding the overshoot.
            content: "ab\n".repeat(400),
            language: crate::types::Language::Rust,
            symbol_type: None,
            symbol_name: None,
        }];
        let provider = RecordingProvider {
            seen: Mutex::new(Vec::new()),
        };

        embed_chunks_concurrent_with_progress_and_cancellation(
            &provider,
            &chunks,
            1,
            1,
            BUDGET,
            &CancellationToken::new(),
            |_, _| {},
        )
        .await
        .expect("embedding should succeed");

        let seen = provider.seen.lock().unwrap();
        assert_eq!(seen.len(), 1, "expected exactly one embedded passage");
        assert!(
            seen[0].starts_with("Document: "),
            "the passage lost its document prefix: {:?}",
            seen[0]
        );
        assert!(
            seen[0].contains("Code:\nab\nab"),
            "the reservation ate the chunk body: {:?}",
            seen[0]
        );
        assert!(
            seen[0].len() <= BUDGET,
            "prefixed passage is {} bytes against a {BUDGET}-byte budget",
            seen[0].len()
        );
    }

    #[test]
    fn a_prefix_larger_than_the_budget_still_leaves_a_budget() {
        // 0 means "unbounded" to chunk_to_embedding_text, so saturating to it
        // would turn an over-long prefix into no truncation at all.
        assert_eq!(budget_after_prefix(8, 10), 1);
        assert_eq!(budget_after_prefix(8, 8), 1);
        assert_eq!(budget_after_prefix(200, 10), 190);
        // An unbounded budget stays unbounded.
        assert_eq!(budget_after_prefix(0, 10), 0);
    }

    #[test]
    fn the_prefix_overhead_is_measured_from_the_provider() {
        let provider = RecordingProvider {
            seen: Mutex::new(Vec::new()),
        };
        assert_eq!(document_prefix_overhead(&provider), "Document: ".len());
        // A provider that takes the identity default reserves nothing.
        assert_eq!(document_prefix_overhead(&CancelThenSucceedProvider), 0);
    }
}