stolas 0.2.0-rc.2

Knowledge and RAG engine - The Prince reveals hidden knowledge
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
//! Retrieval-Augmented Generation pipeline.

use std::collections::HashMap;
use std::sync::Arc;

use infernum_core::Result;

use crate::bm25::{BM25Config, HybridRetriever};
use crate::chunker::Chunker;
use crate::cross_encoder::{CrossEncoder, HeuristicCrossEncoder};
use crate::embedding::Embedder;
use crate::store::{SearchParams, VectorRecord, VectorStore};
use parking_lot::RwLock;

/// Configuration for retrieval.
#[derive(Debug, Clone)]
pub struct RetrievalConfig {
    /// Number of documents to retrieve.
    pub top_k: usize,
    /// Minimum similarity score threshold.
    pub min_score: f32,
    /// Enable cross-encoder reranking.
    pub rerank: bool,
    /// Number of documents to fetch before reranking.
    pub rerank_top_k: usize,
    /// Include metadata in context.
    pub include_metadata: bool,
    /// Maximum total context length in characters.
    pub max_context_length: usize,
    /// Enable hybrid search (BM25 + dense retrieval).
    pub hybrid_search: bool,
    /// Weight for BM25 sparse scores (0.0 - 1.0).
    pub bm25_weight: f32,
    /// Weight for dense embedding scores (0.0 - 1.0).
    pub dense_weight: f32,
}

impl Default for RetrievalConfig {
    fn default() -> Self {
        Self {
            top_k: 5,
            min_score: 0.5,
            rerank: false,
            rerank_top_k: 20,
            include_metadata: true,
            max_context_length: 8000,
            hybrid_search: false,
            bm25_weight: 0.3,
            dense_weight: 0.7,
        }
    }
}

/// Source document for ingestion.
#[derive(Debug, Clone)]
pub struct Document {
    /// Unique document identifier.
    pub id: String,
    /// Document content.
    pub content: String,
    /// Document metadata.
    pub metadata: HashMap<String, serde_json::Value>,
}

impl Document {
    /// Creates a new document.
    #[must_use]
    pub fn new(id: impl Into<String>, content: impl Into<String>) -> Self {
        Self {
            id: id.into(),
            content: content.into(),
            metadata: HashMap::new(),
        }
    }

    /// Adds metadata to the document.
    #[must_use]
    pub fn with_metadata(mut self, key: impl Into<String>, value: serde_json::Value) -> Self {
        self.metadata.insert(key.into(), value);
        self
    }
}

/// A retrieved context item.
#[derive(Debug, Clone)]
pub struct ContextItem {
    /// The content text.
    pub content: String,
    /// Source document ID.
    pub source_id: String,
    /// Chunk index within the document.
    pub chunk_index: usize,
    /// Similarity score.
    pub score: f32,
    /// Associated metadata.
    pub metadata: HashMap<String, serde_json::Value>,
}

/// RAG pipeline for retrieval-augmented generation.
pub struct RagPipeline {
    embedder: Arc<dyn Embedder>,
    store: Arc<dyn VectorStore>,
    chunker: Chunker,
    config: RetrievalConfig,
    /// Optional cross-encoder for improved reranking.
    cross_encoder: Option<Arc<dyn CrossEncoder>>,
    /// Optional hybrid retriever for BM25 + dense search.
    hybrid_retriever: Option<RwLock<HybridRetriever>>,
}

impl RagPipeline {
    /// Creates a new RAG pipeline.
    #[must_use]
    pub fn new(
        embedder: Arc<dyn Embedder>,
        store: Arc<dyn VectorStore>,
        config: RetrievalConfig,
    ) -> Self {
        // Use heuristic cross-encoder by default if reranking is enabled
        let cross_encoder: Option<Arc<dyn CrossEncoder>> = if config.rerank {
            Some(Arc::new(HeuristicCrossEncoder::new()))
        } else {
            None
        };

        // Create hybrid retriever if hybrid search is enabled
        let hybrid_retriever = if config.hybrid_search {
            Some(RwLock::new(HybridRetriever::new(
                BM25Config::default(),
                config.bm25_weight,
                config.dense_weight,
            )))
        } else {
            None
        };

        Self {
            embedder,
            store,
            chunker: Chunker::default(),
            config,
            cross_encoder,
            hybrid_retriever,
        }
    }

    /// Creates a new RAG pipeline with custom chunking.
    #[must_use]
    pub fn with_chunker(
        embedder: Arc<dyn Embedder>,
        store: Arc<dyn VectorStore>,
        chunker: Chunker,
        config: RetrievalConfig,
    ) -> Self {
        let cross_encoder: Option<Arc<dyn CrossEncoder>> = if config.rerank {
            Some(Arc::new(HeuristicCrossEncoder::new()))
        } else {
            None
        };

        let hybrid_retriever = if config.hybrid_search {
            Some(RwLock::new(HybridRetriever::new(
                BM25Config::default(),
                config.bm25_weight,
                config.dense_weight,
            )))
        } else {
            None
        };

        Self {
            embedder,
            store,
            chunker,
            config,
            cross_encoder,
            hybrid_retriever,
        }
    }

    /// Creates a RAG pipeline with a custom cross-encoder.
    #[must_use]
    pub fn with_cross_encoder(
        embedder: Arc<dyn Embedder>,
        store: Arc<dyn VectorStore>,
        config: RetrievalConfig,
        cross_encoder: Arc<dyn CrossEncoder>,
    ) -> Self {
        let hybrid_retriever = if config.hybrid_search {
            Some(RwLock::new(HybridRetriever::new(
                BM25Config::default(),
                config.bm25_weight,
                config.dense_weight,
            )))
        } else {
            None
        };

        Self {
            embedder,
            store,
            chunker: Chunker::default(),
            config,
            cross_encoder: Some(cross_encoder),
            hybrid_retriever,
        }
    }

    /// Ingests a document into the vector store.
    ///
    /// # Errors
    ///
    /// Returns an error if embedding or storage fails.
    pub async fn ingest(&self, document: Document) -> Result<usize> {
        let chunks = self.chunker.chunk(&document.content);

        if chunks.is_empty() {
            return Ok(0);
        }

        // Get embeddings for all chunks
        let texts: Vec<&str> = chunks.iter().map(|c| c.text.as_str()).collect();
        let embeddings = self.embedder.embed(&texts).await?;

        // Create vector records
        let records: Vec<VectorRecord> = chunks
            .iter()
            .zip(embeddings.iter())
            .map(|(chunk, embedding)| {
                let mut metadata = document.metadata.clone();
                metadata.insert("source_id".to_string(), serde_json::json!(document.id));
                metadata.insert("chunk_index".to_string(), serde_json::json!(chunk.index));
                metadata.insert("start_offset".to_string(), serde_json::json!(chunk.start));
                metadata.insert("end_offset".to_string(), serde_json::json!(chunk.end));

                VectorRecord {
                    id: format!("{}_{}", document.id, chunk.index),
                    vector: embedding.clone(),
                    content: chunk.text.clone(),
                    metadata,
                }
            })
            .collect();

        // Add chunks to BM25 index for hybrid search
        if let Some(ref hybrid_retriever) = self.hybrid_retriever {
            let mut retriever = hybrid_retriever.write();
            for record in &records {
                retriever.add_document(&record.id, &record.content);
            }
        }

        let count = self.store.upsert(records).await?;
        Ok(count)
    }

    /// Ingests multiple documents.
    ///
    /// # Errors
    ///
    /// Returns an error if any ingestion fails.
    pub async fn ingest_batch(&self, documents: Vec<Document>) -> Result<usize> {
        let mut total = 0;
        for doc in documents {
            total += self.ingest(doc).await?;
        }
        Ok(total)
    }

    /// Deletes all chunks for a document by source ID.
    ///
    /// This removes all chunks from the vector store and BM25 index
    /// that belong to the specified document.
    ///
    /// # Errors
    ///
    /// Returns an error if deletion fails.
    pub async fn delete_document(&self, source_id: &str) -> Result<usize> {
        // Find all chunk IDs that belong to this document
        // Chunks have IDs like "{source_id}_{chunk_index}"
        let prefix = format!("{}_", source_id);

        // Get all records to find matching IDs
        // Note: This is inefficient for large stores but works for the current VectorStore trait
        // A production implementation would add a query-by-prefix method
        let mut ids_to_delete = Vec::new();

        // We need to search with a broad query to find all documents
        // Then filter by source_id in metadata
        let params = crate::store::SearchParams {
            top_k: 10000, // Large limit to get all
            min_score: None,
            ..Default::default()
        };

        // Use a zero vector search to get all records (hacky but works)
        let dim = self.embedder.dimension();
        let zero_query = vec![0.0; dim];
        let all_results = self.store.search(&zero_query, params).await?;

        for result in all_results {
            // Check if this chunk belongs to the document
            if result.record.id.starts_with(&prefix) {
                ids_to_delete.push(result.record.id.clone());
            }
        }

        if ids_to_delete.is_empty() {
            return Ok(0);
        }

        // Remove from BM25 index if hybrid search is enabled
        if let Some(ref hybrid_retriever) = self.hybrid_retriever {
            let mut retriever = hybrid_retriever.write();
            for id in &ids_to_delete {
                retriever.bm25_index_mut().remove_document(id);
            }
        }

        // Delete from vector store
        let deleted = self.store.delete(ids_to_delete).await?;
        Ok(deleted)
    }

    /// Updates a document by deleting old chunks and ingesting new content.
    ///
    /// This is equivalent to calling `delete_document()` followed by `ingest()`,
    /// ensuring that old chunks are properly removed even if the document
    /// gets shorter.
    ///
    /// # Errors
    ///
    /// Returns an error if deletion or ingestion fails.
    pub async fn update_document(&self, document: Document) -> Result<usize> {
        // First delete the old document
        self.delete_document(&document.id).await?;

        // Then ingest the new version
        self.ingest(document).await
    }

    /// Retrieves relevant documents for a query.
    ///
    /// # Errors
    ///
    /// Returns an error if embedding or search fails.
    pub async fn retrieve(&self, query: &str) -> Result<Vec<ContextItem>> {
        // Generate query embedding
        let embeddings = self.embedder.embed(&[query]).await?;
        let query_embedding = &embeddings[0];

        // Search vector store - fetch more if reranking or hybrid search
        let fetch_k = if self.config.rerank || self.config.hybrid_search {
            self.config.rerank_top_k.max(self.config.top_k * 2)
        } else {
            self.config.top_k
        };

        let params = SearchParams {
            top_k: fetch_k,
            min_score: Some(self.config.min_score),
            ..Default::default()
        };

        let results = self.store.search(query_embedding, params).await?;

        // Apply hybrid search if enabled
        let results = if self.config.hybrid_search {
            if let Some(ref hybrid_retriever) = self.hybrid_retriever {
                // Prepare dense results for hybrid scoring
                let dense_results: Vec<(String, f32)> = results
                    .iter()
                    .map(|r| (r.record.id.clone(), r.score))
                    .collect();

                // Get hybrid-ranked results
                let retriever = hybrid_retriever.read();
                let hybrid_results = retriever.hybrid_search(query, &dense_results, fetch_k);

                // Build a map of record IDs to records for lookup
                let record_map: HashMap<String, _> = results
                    .into_iter()
                    .map(|r| (r.record.id.clone(), r))
                    .collect();

                // Reorder based on hybrid scores
                hybrid_results
                    .into_iter()
                    .filter_map(|hr| {
                        record_map.get(&hr.id).map(|r| crate::store::SearchResult {
                            record: r.record.clone(),
                            score: hr.hybrid_score,
                        })
                    })
                    .collect()
            } else {
                results
            }
        } else {
            results
        };

        // Convert to context items
        let mut items: Vec<ContextItem> = results
            .into_iter()
            .map(|r| {
                let source_id = r
                    .record
                    .metadata
                    .get("source_id")
                    .and_then(|v| v.as_str())
                    .unwrap_or("unknown")
                    .to_string();

                let chunk_index = r
                    .record
                    .metadata
                    .get("chunk_index")
                    .and_then(|v| v.as_u64())
                    .unwrap_or(0) as usize;

                ContextItem {
                    content: r.record.content,
                    source_id,
                    chunk_index,
                    score: r.score,
                    metadata: r.record.metadata,
                }
            })
            .collect();

        // Apply reranking if enabled
        if self.config.rerank && items.len() > self.config.top_k {
            items = self.rerank(query, items).await?;
            items.truncate(self.config.top_k);
        }

        // Truncate to top_k if not reranking
        if !self.config.rerank {
            items.truncate(self.config.top_k);
        }

        Ok(items)
    }

    /// Reranks results using cross-encoder scoring.
    async fn rerank(&self, query: &str, items: Vec<ContextItem>) -> Result<Vec<ContextItem>> {
        // Use cross-encoder if available
        if let Some(ref cross_encoder) = self.cross_encoder {
            let texts: Vec<&str> = items.iter().map(|i| i.content.as_str()).collect();
            let scores = cross_encoder.score_batch(query, &texts).await?;

            let mut scored: Vec<(f32, ContextItem)> = items
                .into_iter()
                .zip(scores.into_iter())
                .map(|(item, score)| (score, item))
                .collect();

            // Sort by score descending
            scored.sort_by(|a, b| b.0.partial_cmp(&a.0).unwrap_or(std::cmp::Ordering::Equal));

            return Ok(scored
                .into_iter()
                .map(|(score, mut item)| {
                    item.score = score;
                    item
                })
                .collect());
        }

        // Fallback: use embedding-based reranking
        let texts: Vec<&str> = items.iter().map(|i| i.content.as_str()).collect();
        let doc_embeddings = self.embedder.embed(&texts).await?;
        let query_embedding = &self.embedder.embed(&[query]).await?[0];

        // Compute cosine similarities
        let mut scored: Vec<(f32, ContextItem)> = items
            .into_iter()
            .zip(doc_embeddings.iter())
            .map(|(item, doc_emb)| {
                let score = cosine_similarity(query_embedding, doc_emb);
                (score, item)
            })
            .collect();

        // Sort by score descending
        scored.sort_by(|a, b| b.0.partial_cmp(&a.0).unwrap_or(std::cmp::Ordering::Equal));

        // Update scores and return
        Ok(scored
            .into_iter()
            .map(|(score, mut item)| {
                item.score = score;
                item
            })
            .collect())
    }

    /// Sets the cross-encoder for reranking.
    pub fn set_cross_encoder(&mut self, cross_encoder: Arc<dyn CrossEncoder>) {
        self.cross_encoder = Some(cross_encoder);
    }

    /// Returns the cross-encoder if configured.
    #[must_use]
    pub fn cross_encoder(&self) -> Option<&Arc<dyn CrossEncoder>> {
        self.cross_encoder.as_ref()
    }

    /// Builds context from retrieved items.
    #[must_use]
    pub fn build_context(&self, items: &[ContextItem]) -> String {
        let mut context = String::new();
        let mut total_len = 0;

        for (i, item) in items.iter().enumerate() {
            let entry = if self.config.include_metadata {
                format!(
                    "[{}] (source: {}, score: {:.2})\n{}\n\n",
                    i + 1,
                    item.source_id,
                    item.score,
                    item.content
                )
            } else {
                format!("[{}] {}\n\n", i + 1, item.content)
            };

            if total_len + entry.len() > self.config.max_context_length {
                break;
            }

            context.push_str(&entry);
            total_len += entry.len();
        }

        context.trim().to_string()
    }

    /// Augments a prompt with retrieved context.
    ///
    /// # Errors
    ///
    /// Returns an error if retrieval fails.
    pub async fn augment(&self, query: &str, system_prompt: Option<&str>) -> Result<String> {
        let items = self.retrieve(query).await?;
        let context = self.build_context(&items);

        let system = system_prompt.unwrap_or(
            "You are a helpful assistant. Answer questions based on the provided context. \
             If the context doesn't contain relevant information, say so.",
        );

        Ok(format!(
            "{}\n\n---\nRelevant Context:\n{}\n---\n\nQuestion: {}",
            system, context, query
        ))
    }

    /// Creates a formatted prompt with context for chat models.
    ///
    /// # Errors
    ///
    /// Returns an error if retrieval fails.
    pub async fn augment_messages(
        &self,
        query: &str,
        system_prompt: Option<&str>,
    ) -> Result<Vec<infernum_core::Message>> {
        let items = self.retrieve(query).await?;
        let context = self.build_context(&items);

        let system = system_prompt.unwrap_or(
            "You are a helpful assistant. Answer questions based on the provided context.",
        );

        let context_msg = format!("Here is relevant context for the question:\n\n{}", context);

        Ok(vec![
            infernum_core::Message::system(system),
            infernum_core::Message::system(&context_msg),
            infernum_core::Message::user(query),
        ])
    }

    /// Returns the embedder.
    #[must_use]
    pub fn embedder(&self) -> &Arc<dyn Embedder> {
        &self.embedder
    }

    /// Returns the vector store.
    #[must_use]
    pub fn store(&self) -> &Arc<dyn VectorStore> {
        &self.store
    }

    /// Returns the configuration.
    #[must_use]
    pub fn config(&self) -> &RetrievalConfig {
        &self.config
    }

    /// Returns the hybrid retriever if configured.
    #[must_use]
    pub fn hybrid_retriever(&self) -> Option<&RwLock<HybridRetriever>> {
        self.hybrid_retriever.as_ref()
    }
}

/// Computes cosine similarity between two vectors.
fn cosine_similarity(a: &[f32], b: &[f32]) -> f32 {
    if a.len() != b.len() {
        return 0.0;
    }

    let dot: f32 = a.iter().zip(b.iter()).map(|(x, y)| x * y).sum();
    let norm_a: f32 = a.iter().map(|x| x * x).sum::<f32>().sqrt();
    let norm_b: f32 = b.iter().map(|x| x * x).sum::<f32>().sqrt();

    if norm_a == 0.0 || norm_b == 0.0 {
        return 0.0;
    }

    dot / (norm_a * norm_b)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::chunker::{Chunker, ChunkingStrategy};
    use crate::cross_encoder::MockCrossEncoder;
    use crate::embedding::MockEmbedder;
    use crate::store::InMemoryStore;

    // ==========================================================================
    // RetrievalConfig Tests
    // ==========================================================================

    #[test]
    fn test_retrieval_config_default() {
        let config = RetrievalConfig::default();
        assert_eq!(config.top_k, 5);
        assert!((config.min_score - 0.5).abs() < 0.001);
        assert!(!config.rerank);
        assert_eq!(config.rerank_top_k, 20);
        assert!(config.include_metadata);
        assert_eq!(config.max_context_length, 8000);
    }

    #[test]
    fn test_retrieval_config_custom() {
        let config = RetrievalConfig {
            top_k: 10,
            min_score: 0.7,
            rerank: true,
            rerank_top_k: 50,
            include_metadata: false,
            max_context_length: 4000,
            ..Default::default()
        };
        assert_eq!(config.top_k, 10);
        assert!((config.min_score - 0.7).abs() < 0.001);
        assert!(config.rerank);
        assert_eq!(config.rerank_top_k, 50);
        assert!(!config.include_metadata);
        assert_eq!(config.max_context_length, 4000);
    }

    #[test]
    fn test_retrieval_config_clone() {
        let config1 = RetrievalConfig::default();
        let config2 = config1.clone();
        assert_eq!(config1.top_k, config2.top_k);
        assert_eq!(config1.min_score, config2.min_score);
    }

    // ==========================================================================
    // Document Tests
    // ==========================================================================

    #[test]
    fn test_document_new() {
        let doc = Document::new("doc123", "Hello, world!");
        assert_eq!(doc.id, "doc123");
        assert_eq!(doc.content, "Hello, world!");
        assert!(doc.metadata.is_empty());
    }

    #[test]
    fn test_document_new_with_into() {
        let doc = Document::new(String::from("id"), String::from("content"));
        assert_eq!(doc.id, "id");
        assert_eq!(doc.content, "content");
    }

    #[test]
    fn test_document_with_metadata() {
        let doc = Document::new("doc1", "content")
            .with_metadata("author", serde_json::json!("Alice"))
            .with_metadata("version", serde_json::json!(1));

        assert_eq!(doc.metadata.len(), 2);
        assert_eq!(
            doc.metadata.get("author").unwrap(),
            &serde_json::json!("Alice")
        );
        assert_eq!(doc.metadata.get("version").unwrap(), &serde_json::json!(1));
    }

    #[test]
    fn test_document_with_complex_metadata() {
        let doc = Document::new("doc1", "content")
            .with_metadata("tags", serde_json::json!(["rust", "programming"]))
            .with_metadata("nested", serde_json::json!({"key": "value"}));

        assert_eq!(doc.metadata.len(), 2);
        assert!(doc.metadata.get("tags").unwrap().is_array());
        assert!(doc.metadata.get("nested").unwrap().is_object());
    }

    #[test]
    fn test_document_clone() {
        let doc1 =
            Document::new("doc1", "content").with_metadata("key", serde_json::json!("value"));
        let doc2 = doc1.clone();
        assert_eq!(doc1.id, doc2.id);
        assert_eq!(doc1.content, doc2.content);
        assert_eq!(doc1.metadata, doc2.metadata);
    }

    // ==========================================================================
    // ContextItem Tests
    // ==========================================================================

    #[test]
    fn test_context_item_structure() {
        let item = ContextItem {
            content: "Test content".to_string(),
            source_id: "source123".to_string(),
            chunk_index: 5,
            score: 0.85,
            metadata: HashMap::new(),
        };

        assert_eq!(item.content, "Test content");
        assert_eq!(item.source_id, "source123");
        assert_eq!(item.chunk_index, 5);
        assert!((item.score - 0.85).abs() < 0.001);
        assert!(item.metadata.is_empty());
    }

    #[test]
    fn test_context_item_with_metadata() {
        let mut metadata = HashMap::new();
        metadata.insert("key".to_string(), serde_json::json!("value"));

        let item = ContextItem {
            content: "content".to_string(),
            source_id: "source".to_string(),
            chunk_index: 0,
            score: 0.9,
            metadata,
        };

        assert_eq!(item.metadata.len(), 1);
    }

    #[test]
    fn test_context_item_clone() {
        let item1 = ContextItem {
            content: "content".to_string(),
            source_id: "source".to_string(),
            chunk_index: 1,
            score: 0.75,
            metadata: HashMap::new(),
        };
        let item2 = item1.clone();
        assert_eq!(item1.content, item2.content);
        assert_eq!(item1.source_id, item2.source_id);
        assert_eq!(item1.chunk_index, item2.chunk_index);
        assert_eq!(item1.score, item2.score);
    }

    // ==========================================================================
    // RagPipeline Creation Tests
    // ==========================================================================

    #[test]
    fn test_rag_pipeline_new() {
        let embedder = Arc::new(MockEmbedder::new(384));
        let store = Arc::new(InMemoryStore::new());
        let config = RetrievalConfig::default();
        let pipeline = RagPipeline::new(embedder, store, config);

        assert_eq!(pipeline.embedder().dimension(), 384);
        assert!(pipeline.cross_encoder().is_none()); // rerank is false by default
    }

    #[test]
    fn test_rag_pipeline_new_with_rerank() {
        let embedder = Arc::new(MockEmbedder::new(384));
        let store = Arc::new(InMemoryStore::new());
        let config = RetrievalConfig {
            rerank: true,
            ..Default::default()
        };
        let pipeline = RagPipeline::new(embedder, store, config);

        // With rerank enabled, cross_encoder should be set
        assert!(pipeline.cross_encoder().is_some());
    }

    #[test]
    fn test_rag_pipeline_with_chunker() {
        let embedder = Arc::new(MockEmbedder::new(384));
        let store = Arc::new(InMemoryStore::new());
        let chunker = Chunker::new(ChunkingStrategy::Sentence {
            min_size: 50,
            max_size: 200,
        });
        let config = RetrievalConfig::default();

        let pipeline = RagPipeline::with_chunker(embedder, store, chunker, config);
        assert_eq!(pipeline.embedder().dimension(), 384);
    }

    #[test]
    fn test_rag_pipeline_with_cross_encoder() {
        let embedder = Arc::new(MockEmbedder::new(384));
        let store = Arc::new(InMemoryStore::new());
        let config = RetrievalConfig::default();
        let cross_encoder = Arc::new(MockCrossEncoder::new());

        let pipeline = RagPipeline::with_cross_encoder(embedder, store, config, cross_encoder);
        assert!(pipeline.cross_encoder().is_some());
    }

    // ==========================================================================
    // RagPipeline Accessors Tests
    // ==========================================================================

    #[test]
    fn test_rag_pipeline_embedder() {
        let embedder = Arc::new(MockEmbedder::new(768));
        let store = Arc::new(InMemoryStore::new());
        let config = RetrievalConfig::default();
        let pipeline = RagPipeline::new(embedder, store, config);

        assert_eq!(pipeline.embedder().dimension(), 768);
        assert_eq!(pipeline.embedder().model_name(), "mock-embedder");
    }

    #[tokio::test]
    async fn test_rag_pipeline_store() {
        let embedder = Arc::new(MockEmbedder::new(384));
        let store = Arc::new(InMemoryStore::new());
        let config = RetrievalConfig::default();
        let pipeline = RagPipeline::new(embedder, store, config);

        // Store should be accessible and functional
        let count = pipeline.store().count().await.unwrap();
        assert_eq!(count, 0);
    }

    #[test]
    fn test_rag_pipeline_config() {
        let embedder = Arc::new(MockEmbedder::new(384));
        let store = Arc::new(InMemoryStore::new());
        let config = RetrievalConfig {
            top_k: 20,
            ..Default::default()
        };
        let pipeline = RagPipeline::new(embedder, store, config);

        assert_eq!(pipeline.config().top_k, 20);
    }

    // ==========================================================================
    // Ingestion Tests
    // ==========================================================================

    #[tokio::test]
    async fn test_document_ingestion() {
        let embedder = Arc::new(MockEmbedder::new(384));
        let store = Arc::new(InMemoryStore::new());
        let config = RetrievalConfig::default();
        let pipeline = RagPipeline::new(embedder, store.clone(), config);

        let doc = Document::new("doc1", "This is a test document with some content.")
            .with_metadata("author", serde_json::json!("test"));

        let count = pipeline.ingest(doc).await.unwrap();
        assert!(count > 0);
        assert!(store.count().await.unwrap() > 0);
    }

    #[tokio::test]
    async fn test_ingest_empty_document() {
        let embedder = Arc::new(MockEmbedder::new(384));
        let store = Arc::new(InMemoryStore::new());
        let config = RetrievalConfig::default();
        let pipeline = RagPipeline::new(embedder, store.clone(), config);

        let doc = Document::new("empty", "");
        let count = pipeline.ingest(doc).await.unwrap();

        // Empty document should produce 0 chunks
        assert_eq!(count, 0);
        assert_eq!(store.count().await.unwrap(), 0);
    }

    #[tokio::test]
    async fn test_ingest_with_custom_chunker() {
        let embedder = Arc::new(MockEmbedder::new(384));
        let store = Arc::new(InMemoryStore::new());
        let chunker = Chunker::new(ChunkingStrategy::FixedTokens {
            size: 20,
            overlap: 5,
        });
        let config = RetrievalConfig::default();
        let pipeline = RagPipeline::with_chunker(embedder, store.clone(), chunker, config);

        let doc = Document::new("doc1", "This is a longer test document that should be split into multiple chunks by the chunker.");
        let count = pipeline.ingest(doc).await.unwrap();

        assert!(count > 1); // Should produce multiple chunks with size 20
    }

    #[tokio::test]
    async fn test_ingest_batch() {
        let embedder = Arc::new(MockEmbedder::new(384));
        let store = Arc::new(InMemoryStore::new());
        let config = RetrievalConfig::default();
        let pipeline = RagPipeline::new(embedder, store.clone(), config);

        let docs = vec![
            Document::new("doc1", "First document content here."),
            Document::new("doc2", "Second document with different content."),
            Document::new("doc3", "Third document, also with unique content."),
        ];

        let count = pipeline.ingest_batch(docs).await.unwrap();
        assert!(count >= 3); // At least 3 chunks (one per doc minimum)
    }

    #[tokio::test]
    async fn test_ingest_preserves_metadata() {
        let embedder = Arc::new(MockEmbedder::new(384));
        let store = Arc::new(InMemoryStore::new());
        let config = RetrievalConfig {
            min_score: -1.0, // Accept all scores for testing (MockEmbedder may produce low similarity)
            top_k: 10,
            ..Default::default()
        };
        let pipeline = RagPipeline::new(embedder, store.clone(), config);

        let doc = Document::new("doc1", "Test document content.")
            .with_metadata("author", serde_json::json!("Alice"))
            .with_metadata("category", serde_json::json!("testing"));

        let count = pipeline.ingest(doc).await.unwrap();
        assert!(count > 0, "Document should be ingested");

        // Verify the store has records
        let store_count = store.count().await.unwrap();
        assert!(store_count > 0, "Store should have records after ingestion");

        // Get records directly from store to verify metadata
        let results = pipeline.retrieve("Test document content").await.unwrap();
        assert!(!results.is_empty(), "Should retrieve at least one result");

        // Metadata should be preserved in the result
        assert!(results[0].metadata.contains_key("author"));
        assert!(results[0].metadata.contains_key("source_id"));
    }

    // ==========================================================================
    // Retrieval Tests
    // ==========================================================================

    #[tokio::test]
    async fn test_retrieval() {
        let embedder = Arc::new(MockEmbedder::new(384));
        let store = Arc::new(InMemoryStore::new());
        let config = RetrievalConfig {
            min_score: 0.0, // Accept all for testing
            ..Default::default()
        };
        let pipeline = RagPipeline::new(embedder, store, config);

        let doc = Document::new("doc1", "The quick brown fox jumps over the lazy dog.");
        pipeline.ingest(doc).await.unwrap();

        let results = pipeline.retrieve("fox").await.unwrap();
        assert!(!results.is_empty());
    }

    #[tokio::test]
    async fn test_retrieve_respects_top_k() {
        let embedder = Arc::new(MockEmbedder::new(384));
        let store = Arc::new(InMemoryStore::new());
        let config = RetrievalConfig {
            top_k: 2,
            min_score: 0.0,
            ..Default::default()
        };
        let pipeline = RagPipeline::new(embedder, store, config);

        // Ingest multiple documents
        for i in 0..5 {
            let doc = Document::new(
                format!("doc{}", i),
                format!("Document number {} content.", i),
            );
            pipeline.ingest(doc).await.unwrap();
        }

        let results = pipeline.retrieve("document").await.unwrap();
        assert!(results.len() <= 2);
    }

    #[tokio::test]
    async fn test_retrieve_respects_min_score() {
        let embedder = Arc::new(MockEmbedder::new(384));
        let store = Arc::new(InMemoryStore::new());
        let config = RetrievalConfig {
            min_score: 0.99, // Very high threshold
            ..Default::default()
        };
        let pipeline = RagPipeline::new(embedder, store, config);

        let doc = Document::new("doc1", "Some content here.");
        pipeline.ingest(doc).await.unwrap();

        let results = pipeline.retrieve("random query").await.unwrap();
        // With a very high min_score threshold, may get no results
        for result in &results {
            assert!(result.score >= 0.99);
        }
    }

    #[tokio::test]
    async fn test_retrieve_with_reranking() {
        let embedder = Arc::new(MockEmbedder::new(384));
        let store = Arc::new(InMemoryStore::new());
        let config = RetrievalConfig {
            top_k: 2,
            min_score: 0.0,
            rerank: true,
            rerank_top_k: 10,
            ..Default::default()
        };
        let pipeline = RagPipeline::new(embedder, store, config);

        for i in 0..5 {
            let doc = Document::new(
                format!("doc{}", i),
                format!("Document {} about programming.", i),
            );
            pipeline.ingest(doc).await.unwrap();
        }

        let results = pipeline.retrieve("programming").await.unwrap();
        // Should return at most top_k results after reranking
        assert!(results.len() <= 2);
    }

    // ==========================================================================
    // Build Context Tests
    // ==========================================================================

    #[test]
    fn test_build_context_with_metadata() {
        let embedder = Arc::new(MockEmbedder::new(384));
        let store = Arc::new(InMemoryStore::new());
        let config = RetrievalConfig {
            include_metadata: true,
            ..Default::default()
        };
        let pipeline = RagPipeline::new(embedder, store, config);

        let items = vec![
            ContextItem {
                content: "First item content".to_string(),
                source_id: "source1".to_string(),
                chunk_index: 0,
                score: 0.95,
                metadata: HashMap::new(),
            },
            ContextItem {
                content: "Second item content".to_string(),
                source_id: "source2".to_string(),
                chunk_index: 0,
                score: 0.85,
                metadata: HashMap::new(),
            },
        ];

        let context = pipeline.build_context(&items);
        assert!(context.contains("[1]"));
        assert!(context.contains("source1"));
        assert!(context.contains("0.95"));
        assert!(context.contains("First item content"));
        assert!(context.contains("[2]"));
        assert!(context.contains("source2"));
    }

    #[test]
    fn test_build_context_without_metadata() {
        let embedder = Arc::new(MockEmbedder::new(384));
        let store = Arc::new(InMemoryStore::new());
        let config = RetrievalConfig {
            include_metadata: false,
            ..Default::default()
        };
        let pipeline = RagPipeline::new(embedder, store, config);

        let items = vec![ContextItem {
            content: "Item content here".to_string(),
            source_id: "source1".to_string(),
            chunk_index: 0,
            score: 0.9,
            metadata: HashMap::new(),
        }];

        let context = pipeline.build_context(&items);
        assert!(context.contains("[1]"));
        assert!(context.contains("Item content here"));
        // Should NOT contain source info when metadata is disabled
        assert!(!context.contains("source:"));
    }

    #[test]
    fn test_build_context_respects_max_length() {
        let embedder = Arc::new(MockEmbedder::new(384));
        let store = Arc::new(InMemoryStore::new());
        let config = RetrievalConfig {
            max_context_length: 100,
            include_metadata: false,
            ..Default::default()
        };
        let pipeline = RagPipeline::new(embedder, store, config);

        let items = vec![
            ContextItem {
                content: "A".repeat(50),
                source_id: "s1".to_string(),
                chunk_index: 0,
                score: 0.9,
                metadata: HashMap::new(),
            },
            ContextItem {
                content: "B".repeat(50),
                source_id: "s2".to_string(),
                chunk_index: 0,
                score: 0.8,
                metadata: HashMap::new(),
            },
            ContextItem {
                content: "C".repeat(50),
                source_id: "s3".to_string(),
                chunk_index: 0,
                score: 0.7,
                metadata: HashMap::new(),
            },
        ];

        let context = pipeline.build_context(&items);
        // Context should be truncated to respect max_context_length
        assert!(context.len() <= 150); // Some overhead for formatting
    }

    #[test]
    fn test_build_context_empty_items() {
        let embedder = Arc::new(MockEmbedder::new(384));
        let store = Arc::new(InMemoryStore::new());
        let config = RetrievalConfig::default();
        let pipeline = RagPipeline::new(embedder, store, config);

        let context = pipeline.build_context(&[]);
        assert!(context.is_empty());
    }

    // ==========================================================================
    // Augment Tests
    // ==========================================================================

    #[tokio::test]
    async fn test_augment_basic() {
        let embedder = Arc::new(MockEmbedder::new(384));
        let store = Arc::new(InMemoryStore::new());
        let config = RetrievalConfig {
            min_score: 0.0,
            ..Default::default()
        };
        let pipeline = RagPipeline::new(embedder, store, config);

        let doc = Document::new("doc1", "Rust is a systems programming language.");
        pipeline.ingest(doc).await.unwrap();

        let prompt = pipeline.augment("What is Rust?", None).await.unwrap();

        assert!(prompt.contains("Relevant Context:"));
        assert!(prompt.contains("Question: What is Rust?"));
    }

    #[tokio::test]
    async fn test_augment_with_custom_system_prompt() {
        let embedder = Arc::new(MockEmbedder::new(384));
        let store = Arc::new(InMemoryStore::new());
        let config = RetrievalConfig {
            min_score: 0.0,
            ..Default::default()
        };
        let pipeline = RagPipeline::new(embedder, store, config);

        let doc = Document::new("doc1", "Python is a high-level language.");
        pipeline.ingest(doc).await.unwrap();

        let custom_prompt = "You are a programming expert.";
        let prompt = pipeline
            .augment("Tell me about Python", Some(custom_prompt))
            .await
            .unwrap();

        assert!(prompt.contains("You are a programming expert"));
        assert!(prompt.contains("Question: Tell me about Python"));
    }

    #[tokio::test]
    async fn test_augment_messages() {
        let embedder = Arc::new(MockEmbedder::new(384));
        let store = Arc::new(InMemoryStore::new());
        let config = RetrievalConfig {
            min_score: 0.0,
            ..Default::default()
        };
        let pipeline = RagPipeline::new(embedder, store, config);

        let doc = Document::new("doc1", "Machine learning uses data to train models.");
        pipeline.ingest(doc).await.unwrap();

        let messages = pipeline
            .augment_messages("What is machine learning?", None)
            .await
            .unwrap();

        assert_eq!(messages.len(), 3); // System, context, user
    }

    #[tokio::test]
    async fn test_augment_messages_with_custom_prompt() {
        let embedder = Arc::new(MockEmbedder::new(384));
        let store = Arc::new(InMemoryStore::new());
        let config = RetrievalConfig {
            min_score: 0.0,
            ..Default::default()
        };
        let pipeline = RagPipeline::new(embedder, store, config);

        let doc = Document::new("doc1", "AI is transforming industries.");
        pipeline.ingest(doc).await.unwrap();

        let custom = "You are an AI expert.";
        let messages = pipeline
            .augment_messages("Explain AI", Some(custom))
            .await
            .unwrap();

        assert_eq!(messages.len(), 3);
    }

    // ==========================================================================
    // Set Cross-Encoder Tests
    // ==========================================================================

    #[test]
    fn test_set_cross_encoder() {
        let embedder = Arc::new(MockEmbedder::new(384));
        let store = Arc::new(InMemoryStore::new());
        let config = RetrievalConfig::default();
        let mut pipeline = RagPipeline::new(embedder, store, config);

        assert!(pipeline.cross_encoder().is_none());

        let cross_encoder = Arc::new(MockCrossEncoder::new());
        pipeline.set_cross_encoder(cross_encoder);

        assert!(pipeline.cross_encoder().is_some());
    }

    // ==========================================================================
    // Cosine Similarity Tests
    // ==========================================================================

    #[test]
    fn test_cosine_similarity_identical_vectors() {
        let v = vec![1.0, 2.0, 3.0];
        let sim = cosine_similarity(&v, &v);
        assert!((sim - 1.0).abs() < 0.0001);
    }

    #[test]
    fn test_cosine_similarity_orthogonal_vectors() {
        let a = vec![1.0, 0.0, 0.0];
        let b = vec![0.0, 1.0, 0.0];
        let sim = cosine_similarity(&a, &b);
        assert!(sim.abs() < 0.0001);
    }

    #[test]
    fn test_cosine_similarity_opposite_vectors() {
        let a = vec![1.0, 0.0, 0.0];
        let b = vec![-1.0, 0.0, 0.0];
        let sim = cosine_similarity(&a, &b);
        assert!((sim + 1.0).abs() < 0.0001);
    }

    #[test]
    fn test_cosine_similarity_different_lengths() {
        let a = vec![1.0, 2.0];
        let b = vec![1.0, 2.0, 3.0];
        let sim = cosine_similarity(&a, &b);
        assert_eq!(sim, 0.0);
    }

    #[test]
    fn test_cosine_similarity_zero_vector() {
        let a = vec![1.0, 2.0, 3.0];
        let b = vec![0.0, 0.0, 0.0];
        let sim = cosine_similarity(&a, &b);
        assert_eq!(sim, 0.0);
    }

    #[test]
    fn test_cosine_similarity_both_zero_vectors() {
        let a = vec![0.0, 0.0, 0.0];
        let b = vec![0.0, 0.0, 0.0];
        let sim = cosine_similarity(&a, &b);
        assert_eq!(sim, 0.0);
    }

    #[test]
    fn test_cosine_similarity_parallel_vectors() {
        let a = vec![1.0, 2.0, 3.0];
        let b = vec![2.0, 4.0, 6.0]; // Same direction, different magnitude
        let sim = cosine_similarity(&a, &b);
        assert!((sim - 1.0).abs() < 0.0001);
    }

    #[test]
    fn test_cosine_similarity_negative_values() {
        let a = vec![-1.0, -2.0, -3.0];
        let b = vec![-1.0, -2.0, -3.0];
        let sim = cosine_similarity(&a, &b);
        assert!((sim - 1.0).abs() < 0.0001);
    }

    // ==========================================================================
    // Hybrid Search Tests (BM25 + Dense)
    // ==========================================================================

    #[test]
    fn test_retrieval_config_hybrid_search_default() {
        let config = RetrievalConfig::default();
        assert!(
            !config.hybrid_search,
            "Hybrid search should be disabled by default"
        );
        assert!(
            (config.bm25_weight - 0.3).abs() < 0.001,
            "Default BM25 weight should be 0.3"
        );
        assert!(
            (config.dense_weight - 0.7).abs() < 0.001,
            "Default dense weight should be 0.7"
        );
    }

    #[test]
    fn test_retrieval_config_hybrid_search_enabled() {
        let config = RetrievalConfig {
            hybrid_search: true,
            bm25_weight: 0.5,
            dense_weight: 0.5,
            ..Default::default()
        };
        assert!(config.hybrid_search);
        assert!((config.bm25_weight - 0.5).abs() < 0.001);
        assert!((config.dense_weight - 0.5).abs() < 0.001);
    }

    #[test]
    fn test_rag_pipeline_hybrid_retriever() {
        let embedder = Arc::new(MockEmbedder::new(384));
        let store = Arc::new(InMemoryStore::new());
        let config = RetrievalConfig {
            hybrid_search: true,
            ..Default::default()
        };
        let pipeline = RagPipeline::new(embedder, store, config);

        // Pipeline should have hybrid retriever when hybrid_search is enabled
        assert!(pipeline.hybrid_retriever().is_some());
    }

    #[test]
    fn test_rag_pipeline_no_hybrid_retriever_by_default() {
        let embedder = Arc::new(MockEmbedder::new(384));
        let store = Arc::new(InMemoryStore::new());
        let config = RetrievalConfig::default();
        let pipeline = RagPipeline::new(embedder, store, config);

        // Pipeline should NOT have hybrid retriever by default
        assert!(pipeline.hybrid_retriever().is_none());
    }

    #[tokio::test]
    async fn test_hybrid_search_indexes_documents() {
        let embedder = Arc::new(MockEmbedder::new(384));
        let store = Arc::new(InMemoryStore::new());
        let config = RetrievalConfig {
            hybrid_search: true,
            ..Default::default()
        };
        let pipeline = RagPipeline::new(embedder, store, config);

        // Ingest a document
        let doc = Document::new("doc1", "The quick brown fox jumps over the lazy dog");
        pipeline.ingest(doc).await.unwrap();

        // BM25 index should have the document
        let retriever = pipeline.hybrid_retriever().unwrap();
        assert_eq!(retriever.read().document_count(), 1);
    }

    #[tokio::test]
    async fn test_hybrid_search_retrieval() {
        let embedder = Arc::new(MockEmbedder::new(384));
        let store = Arc::new(InMemoryStore::new());
        let config = RetrievalConfig {
            top_k: 3,
            min_score: 0.0,
            hybrid_search: true,
            bm25_weight: 0.5,
            dense_weight: 0.5,
            ..Default::default()
        };
        let pipeline = RagPipeline::new(embedder, store, config);

        // Ingest documents with different content
        let docs = vec![
            Document::new("doc1", "The quick brown fox jumps over the lazy dog"),
            Document::new("doc2", "A lazy cat sleeps on the couch all day"),
            Document::new("doc3", "Machine learning models process data efficiently"),
        ];
        for doc in docs {
            pipeline.ingest(doc).await.unwrap();
        }

        // Query that matches "lazy" - should boost docs with that keyword
        let results = pipeline.retrieve("lazy animals").await.unwrap();

        // Results should include documents with "lazy" keyword
        assert!(!results.is_empty());
        // First result should be either doc1 or doc2 (both contain "lazy")
        let first_source = &results[0].source_id;
        assert!(
            first_source.starts_with("doc1") || first_source.starts_with("doc2"),
            "First result should be a doc containing 'lazy', got: {}",
            first_source
        );
    }

    #[tokio::test]
    async fn test_hybrid_search_with_reranking() {
        let embedder = Arc::new(MockEmbedder::new(384));
        let store = Arc::new(InMemoryStore::new());
        let config = RetrievalConfig {
            top_k: 2,
            min_score: 0.0,
            hybrid_search: true,
            rerank: true,
            rerank_top_k: 10,
            ..Default::default()
        };
        let pipeline = RagPipeline::new(embedder, store, config);

        // Ingest documents
        let docs = vec![
            Document::new("doc1", "Python programming language for data science"),
            Document::new("doc2", "JavaScript frameworks for web development"),
            Document::new("doc3", "Rust systems programming with memory safety"),
        ];
        for doc in docs {
            pipeline.ingest(doc).await.unwrap();
        }

        // Both hybrid search and reranking should work together
        let results = pipeline.retrieve("programming language").await.unwrap();
        assert!(results.len() <= 2);
    }

    // ==========================================================================
    // Incremental Document Update Tests
    // ==========================================================================

    #[tokio::test]
    async fn test_delete_document() {
        let embedder = Arc::new(MockEmbedder::new(384));
        let store = Arc::new(InMemoryStore::new());
        let config = RetrievalConfig {
            min_score: 0.0,
            ..Default::default()
        };
        let pipeline = RagPipeline::new(embedder, store.clone(), config);

        // Ingest a document
        let doc = Document::new("doc1", "The quick brown fox jumps over the lazy dog");
        pipeline.ingest(doc).await.unwrap();

        // Verify it's there
        let count_before = store.count().await.unwrap();
        assert!(count_before > 0);

        // Delete the document
        let deleted = pipeline.delete_document("doc1").await.unwrap();
        assert!(deleted > 0);

        // Verify it's gone
        let count_after = store.count().await.unwrap();
        assert_eq!(count_after, 0);
    }

    #[tokio::test]
    async fn test_delete_document_not_found() {
        let embedder = Arc::new(MockEmbedder::new(384));
        let store = Arc::new(InMemoryStore::new());
        let config = RetrievalConfig::default();
        let pipeline = RagPipeline::new(embedder, store, config);

        // Try to delete non-existent document
        let deleted = pipeline.delete_document("nonexistent").await.unwrap();
        assert_eq!(deleted, 0);
    }

    #[tokio::test]
    async fn test_update_document_content() {
        let embedder = Arc::new(MockEmbedder::new(384));
        let store = Arc::new(InMemoryStore::new());
        let config = RetrievalConfig {
            min_score: -1.0, // Accept all scores for testing
            ..Default::default()
        };
        let pipeline = RagPipeline::new(embedder, store.clone(), config);

        // Ingest original document
        let doc = Document::new("doc1", "Original content about rust programming.");
        pipeline.ingest(doc).await.unwrap();

        // Verify document is in store
        let count_before = store.count().await.unwrap();
        assert_eq!(count_before, 1);

        // Update document with new content
        let updated_doc = Document::new("doc1", "Updated content about python scripting.");
        pipeline.update_document(updated_doc).await.unwrap();

        // Should still have 1 document (replaced)
        let count_after = store.count().await.unwrap();
        assert_eq!(count_after, 1);

        // Get all results and verify content changed
        let results = pipeline.retrieve("any query").await.unwrap();
        assert!(!results.is_empty());
        assert!(
            results[0].content.contains("Updated"),
            "Should contain updated content"
        );
        assert!(
            !results[0].content.contains("Original"),
            "Should not contain original content"
        );
    }

    #[tokio::test]
    async fn test_update_document_shorter() {
        let embedder = Arc::new(MockEmbedder::new(384));
        let store = Arc::new(InMemoryStore::new());
        let chunker = Chunker::new(ChunkingStrategy::FixedTokens {
            size: 10,
            overlap: 0,
        });
        let config = RetrievalConfig {
            min_score: 0.0,
            ..Default::default()
        };
        let pipeline = RagPipeline::with_chunker(embedder, store.clone(), chunker, config);

        // Ingest a long document (will create multiple chunks)
        let long_content =
            "This is chunk one. This is chunk two. This is chunk three. This is chunk four.";
        let doc = Document::new("doc1", long_content);
        let original_chunks = pipeline.ingest(doc).await.unwrap();
        assert!(original_chunks > 1, "Should create multiple chunks");

        let count_before = store.count().await.unwrap();

        // Update with shorter content (fewer chunks)
        let short_content = "Short updated content.";
        let updated_doc = Document::new("doc1", short_content);
        pipeline.update_document(updated_doc).await.unwrap();

        let count_after = store.count().await.unwrap();

        // Should have fewer chunks now
        assert!(
            count_after < count_before,
            "Shorter document should have fewer chunks"
        );
    }

    #[tokio::test]
    async fn test_update_document_preserves_others() {
        let embedder = Arc::new(MockEmbedder::new(384));
        let store = Arc::new(InMemoryStore::new());
        let config = RetrievalConfig {
            min_score: -1.0, // Accept all scores for testing
            ..Default::default()
        };
        let pipeline = RagPipeline::new(embedder, store.clone(), config);

        // Ingest two documents
        let doc1 = Document::new("doc1", "First document about cats.");
        let doc2 = Document::new("doc2", "Second document about dogs.");
        pipeline.ingest(doc1).await.unwrap();
        pipeline.ingest(doc2).await.unwrap();

        let count_before = store.count().await.unwrap();
        assert_eq!(count_before, 2);

        // Update only doc1
        let updated_doc1 = Document::new("doc1", "Updated first document about birds.");
        pipeline.update_document(updated_doc1).await.unwrap();

        // Should still have 2 documents
        let count_after = store.count().await.unwrap();
        assert_eq!(count_after, 2);

        // Get all results and verify both documents exist
        let results = pipeline.retrieve("any").await.unwrap();
        assert_eq!(results.len(), 2);

        // Verify doc1 was updated (contains "birds", not "cats")
        let doc1_result = results.iter().find(|r| r.source_id == "doc1").unwrap();
        assert!(doc1_result.content.contains("birds"));
        assert!(!doc1_result.content.contains("cats"));

        // Verify doc2 is unchanged
        let doc2_result = results.iter().find(|r| r.source_id == "doc2").unwrap();
        assert!(doc2_result.content.contains("dogs"));
    }

    #[tokio::test]
    async fn test_update_removes_from_bm25_index() {
        let embedder = Arc::new(MockEmbedder::new(384));
        let store = Arc::new(InMemoryStore::new());
        let config = RetrievalConfig {
            hybrid_search: true,
            min_score: 0.0,
            ..Default::default()
        };
        let pipeline = RagPipeline::new(embedder, store, config);

        // Ingest document with unique keyword
        let doc = Document::new("doc1", "UniqueKeyword123 in original content");
        pipeline.ingest(doc).await.unwrap();

        // BM25 should have the document
        let retriever = pipeline.hybrid_retriever().unwrap();
        assert_eq!(retriever.read().document_count(), 1);

        // Update document without the unique keyword
        let updated_doc = Document::new("doc1", "Completely different content now");
        pipeline.update_document(updated_doc).await.unwrap();

        // BM25 should still have 1 document (the updated one)
        assert_eq!(retriever.read().document_count(), 1);

        // Old keyword should not match
        let results = pipeline.retrieve("UniqueKeyword123").await.unwrap();
        let has_old = results
            .iter()
            .any(|r| r.content.contains("UniqueKeyword123"));
        assert!(!has_old, "Old content should be removed from BM25 index");
    }
}