reasonkit-core 0.1.8

The Reasoning Engine — Auditable Reasoning for Production AI | Rust-Native | Turn Prompts into Protocols
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
//! Retrieval-Augmented Generation (RAG) Engine
//!
//! Combines local Tantivy BM25 retrieval with LLM generation for
//! context-aware question answering.
//!
//! ## Features
//!
//! - Local-first: Uses Tantivy for BM25 indexing (no external vector DB)
//! - Multiple LLM providers: Works with any UnifiedLlmClient provider
//! - Configurable retrieval: Adjust top_k, min_score, max_context
//! - Structured output: Includes sources and confidence scores
//!
//! ## Quick Start
//!
//! ```rust,ignore
//! use reasonkit::rag::{RagEngine, RagConfig};
//!
//! #[tokio::main]
//! async fn main() -> anyhow::Result<()> {
//!     let engine = RagEngine::in_memory()?;
//!
//!     // Add documents
//!     engine.add_document(&doc).await?;
//!
//!     // Query with RAG
//!     let response = engine.query("How does chain-of-thought work?").await?;
//!     println!("{}", response.answer);
//!
//!     for source in response.sources {
//!         println!("- {}", source.text);
//!     }
//!
//!     Ok(())
//! }
//! ```

use crate::{
    thinktool::{LlmClient, LlmRequest, UnifiedLlmClient},
    Document, Error, Result, RetrievalConfig,
};

pub mod chunking;
pub mod hyde;
pub mod performance;
use crate::rag::hyde::HyDEExpander;
#[cfg(feature = "memory")]
use reasonkit_mem::{
    indexing::IndexManager,
    retrieval::{HybridResult, HybridRetriever, RetrievalStats},
    storage::Storage,
};
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
use uuid::Uuid;

/// RAG configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RagConfig {
    /// Number of chunks to retrieve
    pub top_k: usize,

    /// Minimum relevance score (0.0-1.0)
    pub min_score: f32,

    /// Maximum context tokens to include
    pub max_context_tokens: usize,

    /// Whether to include source citations
    pub include_sources: bool,

    /// System prompt template
    pub system_prompt: String,

    /// Whether to use sparse-only retrieval (BM25)
    pub sparse_only: bool,

    /// Alpha for hybrid search (0.0 = sparse only, 1.0 = dense only)
    pub hybrid_alpha: f32,

    /// Whether to enable HyDE (Hypothetical Document Embeddings) query expansion
    pub hyde_enabled: bool,
}

impl Default for RagConfig {
    fn default() -> Self {
        Self {
            top_k: 5,
            min_score: 0.1,
            max_context_tokens: 2000,
            include_sources: true,
            system_prompt: DEFAULT_RAG_PROMPT.to_string(),
            sparse_only: true, // Default to BM25-only for local prototype
            hybrid_alpha: 0.3,
            hyde_enabled: false,
        }
    }
}

impl RagConfig {
    /// Create a config optimized for quick responses
    pub fn quick() -> Self {
        Self {
            top_k: 3,
            min_score: 0.2,
            max_context_tokens: 1000,
            include_sources: false,
            sparse_only: true,
            ..Default::default()
        }
    }

    /// Create a config for thorough research
    pub fn thorough() -> Self {
        Self {
            top_k: 10,
            min_score: 0.05,
            max_context_tokens: 4000,
            include_sources: true,
            sparse_only: false,
            hybrid_alpha: 0.5,
            hyde_enabled: true,
            ..Default::default()
        }
    }
}

const DEFAULT_RAG_PROMPT: &str = r#"You are a helpful assistant answering questions based on the provided context.

INSTRUCTIONS:
1. Answer the question using ONLY the provided context
2. If the context doesn't contain the answer, say "I don't have enough information to answer this"
3. Be concise but comprehensive
4. When citing information, reference the source section

CONTEXT:
{context}

Answer the question based on the context above. Be accurate and cite your sources."#;

/// Response from RAG query
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RagResponse {
    /// The generated answer
    pub answer: String,

    /// Sources used to generate the answer
    pub sources: Vec<RagSource>,

    /// Retrieval statistics
    pub retrieval_stats: RagRetrievalStats,

    /// Tokens used in generation
    pub tokens_used: Option<u32>,

    /// Query that was processed
    pub query: String,
}

/// Source document used in RAG response
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RagSource {
    /// Document ID
    pub doc_id: Uuid,

    /// Chunk ID
    pub chunk_id: Uuid,

    /// Text snippet
    pub text: String,

    /// Relevance score
    pub score: f32,

    /// Section or page reference
    pub section: Option<String>,
}

/// Statistics about the retrieval phase
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct RagRetrievalStats {
    /// Number of chunks retrieved
    pub chunks_retrieved: usize,

    /// Number of chunks used after filtering
    pub chunks_used: usize,

    /// Total context tokens
    pub context_tokens: usize,

    /// Retrieval time in milliseconds
    pub retrieval_time_ms: u64,
}

/// RAG Engine combining retrieval with generation
pub struct RagEngine {
    retriever: HybridRetriever,
    llm_client: Option<UnifiedLlmClient>,
    config: RagConfig,
}

impl RagEngine {
    /// Create a new in-memory RAG engine
    pub fn in_memory() -> Result<Self> {
        Ok(Self {
            retriever: HybridRetriever::in_memory()?,
            llm_client: None,
            config: RagConfig::default(),
        })
    }

    /// Create a RAG engine with persistent storage
    pub async fn persistent(base_path: PathBuf) -> Result<Self> {
        let storage_path = base_path.join("storage");
        let index_path = base_path.join("index");

        std::fs::create_dir_all(&storage_path)
            .map_err(|e| Error::io(format!("Failed to create storage dir: {}", e)))?;
        std::fs::create_dir_all(&index_path)
            .map_err(|e| Error::io(format!("Failed to create index dir: {}", e)))?;

        let storage = Storage::file(storage_path).await?;
        let index = IndexManager::open(index_path)?;

        Ok(Self {
            retriever: HybridRetriever::new(storage, index),
            llm_client: None,
            config: RagConfig::default(),
        })
    }

    /// Set the LLM client for generation
    pub fn with_llm(mut self, client: UnifiedLlmClient) -> Self {
        self.llm_client = Some(client);
        self
    }

    /// Set the RAG configuration
    pub fn with_config(mut self, config: RagConfig) -> Self {
        self.config = config;
        self
    }

    /// Add a document to the knowledge base
    pub async fn add_document(&self, doc: &Document) -> Result<()> {
        let mem_doc: reasonkit_mem::Document = doc.clone().into();
        self.retriever.add_document(&mem_doc).await?;
        Ok(())
    }

    /// Add multiple documents
    pub async fn add_documents(&self, docs: &[Document]) -> Result<usize> {
        let mut count = 0;
        for doc in docs {
            let mem_doc: reasonkit_mem::Document = doc.clone().into();
            self.retriever.add_document(&mem_doc).await?;
            count += 1;
        }
        Ok(count)
    }

    /// Query the knowledge base with RAG
    pub async fn query(&self, query: &str) -> Result<RagResponse> {
        // HyDE query expansion
        let effective_query = if self.config.hyde_enabled {
            if let Some(ref client) = self.llm_client {
                let expander = HyDEExpander::new(client.clone());
                expander.expand_query(query).await?
            } else {
                // No LLM client available, fall back to original query
                query.to_string()
            }
        } else {
            query.to_string()
        };

        let start = std::time::Instant::now();

        // Retrieve relevant chunks
        let results = if self.config.sparse_only {
            self.retriever
                .search_sparse(&effective_query, self.config.top_k)
                .await?
        } else {
            let retrieval_config = RetrievalConfig {
                top_k: self.config.top_k,
                alpha: self.config.hybrid_alpha,
                ..Default::default()
            };
            self.retriever
                .search_hybrid(&effective_query, None, &retrieval_config)
                .await?
        };

        let retrieval_time_ms = start.elapsed().as_millis() as u64;

        // Filter by minimum score
        let filtered_results: Vec<_> = results
            .into_iter()
            .filter(|r| r.score >= self.config.min_score)
            .collect();

        // Build context from results
        let (context, context_tokens) = self.build_context(&filtered_results);

        // Build sources list with section info from retrieval
        let sources: Vec<RagSource> = filtered_results
            .iter()
            .map(|r| RagSource {
                doc_id: r.doc_id,
                chunk_id: r.chunk_id,
                text: truncate_text(&r.text, 200),
                score: r.score,
                section: None, // Section field removed in reasonkit-mem HybridResult
            })
            .collect();

        let retrieval_stats = RagRetrievalStats {
            chunks_retrieved: self.config.top_k,
            chunks_used: filtered_results.len(),
            context_tokens,
            retrieval_time_ms,
        };

        // Generate answer using LLM
        let (answer, tokens_used) = if let Some(ref client) = self.llm_client {
            let system_prompt = self.config.system_prompt.replace("{context}", &context);

            let request = LlmRequest::new(query)
                .with_system(&system_prompt)
                .with_max_tokens(1000);

            let response = client
                .complete(request)
                .await
                .map_err(|e| Error::network(format!("LLM generation failed: {}", e)))?;

            let tokens = Some(response.usage.total_tokens);
            (response.content, tokens)
        } else {
            // No LLM client - return retrieval-only response
            let answer = format!(
                "Retrieved {} relevant chunks for query: \"{}\"\n\nTop results:\n{}",
                filtered_results.len(),
                query,
                filtered_results
                    .iter()
                    .take(3)
                    .enumerate()
                    .map(|(i, r)| format!(
                        "{}. [score: {:.3}] {}",
                        i + 1,
                        r.score,
                        truncate_text(&r.text, 150)
                    ))
                    .collect::<Vec<_>>()
                    .join("\n")
            );
            (answer, None)
        };

        Ok(RagResponse {
            answer,
            sources,
            retrieval_stats,
            tokens_used,
            query: query.to_string(),
        })
    }

    /// Retrieve without generation (for inspection)
    pub async fn retrieve(&self, query: &str, top_k: usize) -> Result<Vec<HybridResult>> {
        self.retriever
            .search_sparse(query, top_k)
            .await
            .map_err(Error::from)
    }

    /// Get knowledge base statistics
    pub async fn stats(&self) -> Result<RetrievalStats> {
        self.retriever.stats().await.map_err(Error::from)
    }

    /// Delete a document from the knowledge base
    pub async fn delete_document(&self, doc_id: &Uuid) -> Result<()> {
        self.retriever
            .delete_document(doc_id)
            .await
            .map_err(Error::from)
    }

    /// Build context string from retrieved results
    fn build_context(&self, results: &[HybridResult]) -> (String, usize) {
        let mut context_parts = Vec::new();
        let mut total_tokens = 0;

        for (i, result) in results.iter().enumerate() {
            // Rough token estimate (4 chars per token)
            let chunk_tokens = result.text.len() / 4;

            if total_tokens + chunk_tokens > self.config.max_context_tokens {
                break;
            }

            context_parts.push(format!(
                "[Source {}] (relevance: {:.2})\n{}",
                i + 1,
                result.score,
                result.text
            ));

            total_tokens += chunk_tokens;
        }

        (context_parts.join("\n\n---\n\n"), total_tokens)
    }
}

/// Truncate text to specified length
fn truncate_text(text: &str, max_len: usize) -> String {
    if text.len() <= max_len {
        text.to_string()
    } else {
        format!("{}...", &text[..max_len.saturating_sub(3)])
    }
}

// ============================================================================
// COMPREHENSIVE TEST SUITE
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{Chunk, DocumentType, EmbeddingIds, Source, SourceType};
    use chrono::Utc;

    // ========================================================================
    // TEST HELPERS
    // ========================================================================

    /// Create a test document with a single chunk
    fn create_test_document(content: &str, title: &str) -> Document {
        let source = Source {
            source_type: SourceType::Local,
            url: None,
            path: Some(format!("/test/{}.md", title)),
            arxiv_id: None,
            github_repo: None,
            retrieved_at: Utc::now(),
            version: None,
        };

        let mut doc = Document::new(DocumentType::Note, source).with_content(content.to_string());

        doc.chunks = vec![Chunk {
            id: Uuid::new_v4(),
            text: content.to_string(),
            index: 0,
            start_char: 0,
            end_char: content.len(),
            token_count: Some(content.len() / 4),
            section: Some(title.to_string()),
            page: None,
            embedding_ids: EmbeddingIds::default(),
        }];

        doc
    }

    /// Create a test document with multiple chunks for chunking tests
    fn create_multi_chunk_document(chunks: &[&str], title: &str) -> Document {
        let source = Source {
            source_type: SourceType::Local,
            url: None,
            path: Some(format!("/test/{}.md", title)),
            arxiv_id: None,
            github_repo: None,
            retrieved_at: Utc::now(),
            version: None,
        };

        let full_content = chunks.join("\n\n");
        let mut doc = Document::new(DocumentType::Note, source).with_content(full_content.clone());

        let mut char_offset = 0;
        doc.chunks = chunks
            .iter()
            .enumerate()
            .map(|(i, text)| {
                let chunk = Chunk {
                    id: Uuid::new_v4(),
                    text: text.to_string(),
                    index: i,
                    start_char: char_offset,
                    end_char: char_offset + text.len(),
                    token_count: Some(text.len() / 4),
                    section: Some(format!("Section {}", i + 1)),
                    page: Some(i / 2 + 1),
                    embedding_ids: EmbeddingIds::default(),
                };
                char_offset += text.len() + 2; // +2 for \n\n separator
                chunk
            })
            .collect();

        doc
    }

    // ========================================================================
    // RAG CONFIG TESTS
    // ========================================================================

    #[test]
    fn test_rag_config_default() {
        let config = RagConfig::default();

        assert_eq!(config.top_k, 5);
        assert_eq!(config.min_score, 0.1);
        assert_eq!(config.max_context_tokens, 2000);
        assert!(config.include_sources);
        assert!(config.sparse_only);
        assert_eq!(config.hybrid_alpha, 0.3);
        assert!(config.system_prompt.contains("CONTEXT"));
    }

    #[test]
    fn test_rag_config_quick() {
        let config = RagConfig::quick();

        assert_eq!(config.top_k, 3);
        assert_eq!(config.min_score, 0.2);
        assert_eq!(config.max_context_tokens, 1000);
        assert!(!config.include_sources);
        assert!(config.sparse_only);
    }

    #[test]
    fn test_rag_config_thorough() {
        let config = RagConfig::thorough();

        assert_eq!(config.top_k, 10);
        assert_eq!(config.min_score, 0.05);
        assert_eq!(config.max_context_tokens, 4000);
        assert!(config.include_sources);
        assert!(!config.sparse_only);
        assert_eq!(config.hybrid_alpha, 0.5);
    }

    #[test]
    fn test_rag_config_serialization() {
        let config = RagConfig::default();
        let json = serde_json::to_string(&config).expect("Serialization failed");
        let deserialized: RagConfig = serde_json::from_str(&json).expect("Deserialization failed");

        assert_eq!(config.top_k, deserialized.top_k);
        assert_eq!(config.min_score, deserialized.min_score);
        assert_eq!(config.max_context_tokens, deserialized.max_context_tokens);
    }

    // ========================================================================
    // TEXT TRUNCATION TESTS
    // ========================================================================

    #[test]
    fn test_truncate_text_short() {
        let text = "Short text";
        let result = truncate_text(text, 50);
        assert_eq!(result, "Short text");
    }

    #[test]
    fn test_truncate_text_exact_length() {
        let text = "Exactly ten";
        let result = truncate_text(text, 11);
        assert_eq!(result, "Exactly ten");
    }

    #[test]
    fn test_truncate_text_long() {
        let text = "This is a very long text that needs to be truncated";
        let result = truncate_text(text, 20);
        assert_eq!(result.len(), 20);
        assert!(result.ends_with("..."));
        assert_eq!(result, "This is a very lo...");
    }

    #[test]
    fn test_truncate_text_empty() {
        let text = "";
        let result = truncate_text(text, 10);
        assert_eq!(result, "");
    }

    #[test]
    fn test_truncate_text_zero_max() {
        let text = "Some text";
        let result = truncate_text(text, 0);
        // Should handle gracefully with saturating_sub
        assert_eq!(result, "...");
    }

    #[test]
    fn test_truncate_text_very_small_max() {
        let text = "Hello world";
        let result = truncate_text(text, 3);
        assert_eq!(result, "...");
    }

    // ========================================================================
    // DOCUMENT CHUNKING TESTS
    // ========================================================================

    #[test]
    fn test_single_chunk_document() {
        let doc = create_test_document("Simple content", "simple");

        assert_eq!(doc.chunks.len(), 1);
        assert_eq!(doc.chunks[0].text, "Simple content");
        assert_eq!(doc.chunks[0].index, 0);
        assert_eq!(doc.chunks[0].start_char, 0);
        assert_eq!(doc.chunks[0].end_char, 14);
    }

    #[test]
    fn test_multi_chunk_document() {
        let chunks = [
            "First paragraph about machine learning.",
            "Second paragraph about neural networks.",
            "Third paragraph about deep learning.",
        ];
        let doc = create_multi_chunk_document(&chunks, "ml-doc");

        assert_eq!(doc.chunks.len(), 3);

        // Verify chunk indices
        for (i, chunk) in doc.chunks.iter().enumerate() {
            assert_eq!(chunk.index, i);
            assert!(chunk
                .section
                .as_ref()
                .unwrap()
                .contains(&format!("{}", i + 1)));
        }

        // Verify chunk offsets are sequential
        let mut prev_end = 0;
        for chunk in &doc.chunks {
            assert!(chunk.start_char >= prev_end);
            assert!(chunk.end_char > chunk.start_char);
            prev_end = chunk.end_char;
        }
    }

    #[test]
    fn test_chunk_token_count_estimation() {
        let content = "This is exactly twenty characters."; // 34 chars
        let doc = create_test_document(content, "token-test");

        // Token count should be approximately chars / 4
        let expected_tokens = content.len() / 4;
        assert_eq!(doc.chunks[0].token_count, Some(expected_tokens));
    }

    #[test]
    fn test_chunk_page_assignment() {
        let chunks = [
            "Page 1 content A",
            "Page 1 content B",
            "Page 2 content A",
            "Page 2 content B",
        ];
        let doc = create_multi_chunk_document(&chunks, "paged-doc");

        // First two chunks should be page 1, next two page 2
        assert_eq!(doc.chunks[0].page, Some(1));
        assert_eq!(doc.chunks[1].page, Some(1));
        assert_eq!(doc.chunks[2].page, Some(2));
        assert_eq!(doc.chunks[3].page, Some(2));
    }

    // ========================================================================
    // BM25 SEARCH TESTS
    // ========================================================================

    #[tokio::test]
    async fn test_bm25_search_basic() {
        let engine = RagEngine::in_memory().expect("Failed to create engine");

        let doc = create_test_document(
            "Machine learning algorithms process data to make predictions.",
            "ml-basics",
        );
        engine.add_document(&doc).await.expect("Failed to add doc");

        let results = engine
            .retrieve("machine learning predictions", 5)
            .await
            .expect("Retrieval failed");

        assert!(!results.is_empty());
        assert!(results[0].text.contains("Machine learning"));
    }

    #[tokio::test]
    async fn test_bm25_search_multiple_documents() {
        let engine = RagEngine::in_memory().expect("Failed to create engine");

        let docs = vec![
            create_test_document(
                "Python is a popular programming language for data science.",
                "python",
            ),
            create_test_document(
                "Rust provides memory safety without garbage collection.",
                "rust",
            ),
            create_test_document("JavaScript runs in web browsers and Node.js.", "javascript"),
        ];

        for doc in &docs {
            engine.add_document(doc).await.expect("Failed to add doc");
        }

        // Search for Rust
        let results = engine
            .retrieve("memory safety rust", 5)
            .await
            .expect("Retrieval failed");

        assert!(!results.is_empty());
        assert!(results[0].text.contains("Rust"));
    }

    #[tokio::test]
    async fn test_bm25_search_no_match() {
        let engine = RagEngine::in_memory().expect("Failed to create engine");

        let doc = create_test_document("Cats and dogs are common pets.", "pets");
        engine.add_document(&doc).await.expect("Failed to add doc");

        // Search for something completely unrelated
        let results = engine
            .retrieve("quantum physics relativity", 5)
            .await
            .expect("Retrieval failed");

        // Should return empty or very low score results
        // BM25 might still return the doc if top_k requested, but score should be low
        if !results.is_empty() {
            // Score should be relatively low for unrelated terms
            assert!(results[0].score < 5.0);
        }
    }

    #[tokio::test]
    async fn test_bm25_search_ranking() {
        let engine = RagEngine::in_memory().expect("Failed to create engine");

        // Doc with many term matches should rank higher
        let doc1 = create_test_document(
            "Neural networks and deep learning are subsets of machine learning.",
            "high-relevance",
        );
        let doc2 = create_test_document(
            "The weather today is sunny with clear skies.",
            "low-relevance",
        );
        let doc3 = create_test_document(
            "Machine learning uses algorithms to learn from data.",
            "medium-relevance",
        );

        engine.add_document(&doc1).await.unwrap();
        engine.add_document(&doc2).await.unwrap();
        engine.add_document(&doc3).await.unwrap();

        let results = engine
            .retrieve("machine learning neural networks", 5)
            .await
            .expect("Retrieval failed");

        assert!(results.len() >= 2);
        // First result should be more relevant than last
        assert!(results[0].score >= results[results.len() - 1].score);
    }

    // ========================================================================
    // RAG ENGINE INTEGRATION TESTS
    // ========================================================================

    #[tokio::test]
    async fn test_rag_engine_basic() {
        let engine = RagEngine::in_memory().expect("Failed to create in-memory engine");

        let doc1 = create_test_document(
            "Chain-of-thought prompting enables complex reasoning by breaking problems into steps.",
            "cot-basics",
        );
        let doc2 = create_test_document(
            "Self-consistency improves reasoning by sampling multiple paths and selecting the most common answer.",
            "self-consistency",
        );

        engine
            .add_document(&doc1)
            .await
            .expect("Failed to add doc1");
        engine
            .add_document(&doc2)
            .await
            .expect("Failed to add doc2");

        let response = engine
            .query("How does chain of thought work?")
            .await
            .expect("Query failed");

        assert!(!response.sources.is_empty());
        assert!(response.answer.contains("Retrieved"));
        assert!(response.retrieval_stats.chunks_used > 0);
    }

    #[tokio::test]
    async fn test_rag_engine_with_custom_config() {
        let config = RagConfig {
            top_k: 2,
            min_score: 0.0,
            max_context_tokens: 500,
            include_sources: true,
            sparse_only: true,
            ..Default::default()
        };

        let engine = RagEngine::in_memory()
            .expect("Failed to create engine")
            .with_config(config);

        let doc = create_test_document("Test content for RAG engine.", "test");
        engine.add_document(&doc).await.expect("Failed to add doc");

        let response = engine.query("test content").await.expect("Query failed");

        // Should respect top_k limit
        assert!(response.retrieval_stats.chunks_retrieved <= 2);
    }

    #[tokio::test]
    async fn test_rag_engine_add_multiple_documents() {
        let engine = RagEngine::in_memory().expect("Failed to create engine");

        let docs = vec![
            create_test_document("Document one content.", "doc1"),
            create_test_document("Document two content.", "doc2"),
            create_test_document("Document three content.", "doc3"),
        ];

        let count = engine
            .add_documents(&docs)
            .await
            .expect("Failed to add docs");
        assert_eq!(count, 3);

        let stats = engine.stats().await.expect("Failed to get stats");
        assert_eq!(stats.document_count, 3);
    }

    #[tokio::test]
    async fn test_rag_engine_stats() {
        let engine = RagEngine::in_memory().expect("Failed to create engine");

        // Initially empty
        let stats = engine.stats().await.expect("Failed to get stats");
        assert_eq!(stats.document_count, 0);

        // Add documents
        let doc = create_multi_chunk_document(&["Chunk 1", "Chunk 2", "Chunk 3"], "multi-chunk");
        engine.add_document(&doc).await.expect("Failed to add doc");

        let stats = engine.stats().await.expect("Failed to get stats");
        assert_eq!(stats.document_count, 1);
        assert_eq!(stats.chunk_count, 3);
    }

    #[tokio::test]
    async fn test_rag_engine_delete_document() {
        let engine = RagEngine::in_memory().expect("Failed to create engine");

        let doc = create_test_document("Content to delete.", "delete-me");
        let doc_id = doc.id;

        engine.add_document(&doc).await.expect("Failed to add doc");

        // Verify it exists
        let stats = engine.stats().await.expect("Failed to get stats");
        assert_eq!(stats.document_count, 1);

        // Delete it
        engine
            .delete_document(&doc_id)
            .await
            .expect("Failed to delete doc");

        // Verify it's gone
        let stats = engine.stats().await.expect("Failed to get stats");
        assert_eq!(stats.document_count, 0);
    }

    // ========================================================================
    // MIN SCORE FILTERING TESTS
    // ========================================================================

    #[tokio::test]
    async fn test_min_score_filtering() {
        let config = RagConfig {
            min_score: 5.0, // High threshold to filter out low scores
            ..Default::default()
        };

        let engine = RagEngine::in_memory()
            .expect("Failed to create engine")
            .with_config(config);

        let doc = create_test_document("Some content about cats and dogs.", "pets");
        engine.add_document(&doc).await.expect("Failed to add doc");

        // Query for unrelated topic - should have low BM25 score
        let response = engine
            .query("quantum computing algorithms")
            .await
            .expect("Query failed");

        // Results below min_score should be filtered
        // All sources should have score >= min_score
        for source in &response.sources {
            assert!(source.score >= 5.0);
        }
    }

    #[tokio::test]
    async fn test_min_score_zero() {
        let config = RagConfig {
            min_score: 0.0,
            ..Default::default()
        };

        let engine = RagEngine::in_memory()
            .expect("Failed to create engine")
            .with_config(config);

        let doc = create_test_document("Any content here.", "test");
        engine.add_document(&doc).await.expect("Failed to add doc");

        let response = engine.query("test query").await.expect("Query failed");

        // With min_score 0.0, we should get results
        assert!(response.retrieval_stats.chunks_used >= 0);
    }

    // ========================================================================
    // CONTEXT ASSEMBLY TESTS
    // ========================================================================

    #[tokio::test]
    async fn test_context_token_limit() {
        // Create config with small context limit
        let config = RagConfig {
            max_context_tokens: 10, // Very small limit
            min_score: 0.0,
            ..Default::default()
        };

        let engine = RagEngine::in_memory()
            .expect("Failed to create engine")
            .with_config(config);

        // Add a document with substantial content
        let doc = create_test_document(
            "This is a very long document that contains many words and should exceed the token limit when assembled into context.",
            "long-doc",
        );
        engine.add_document(&doc).await.expect("Failed to add doc");

        let response = engine.query("document").await.expect("Query failed");

        // Context tokens should be limited
        assert!(response.retrieval_stats.context_tokens <= 10);
    }

    #[tokio::test]
    async fn test_context_assembly_format() {
        let engine = RagEngine::in_memory().expect("Failed to create engine");

        let doc = create_test_document("Test content for context assembly.", "test");
        engine.add_document(&doc).await.expect("Failed to add doc");

        let response = engine.query("test content").await.expect("Query failed");

        // Answer should contain formatted output
        assert!(response.answer.contains("Retrieved"));
        assert!(response.answer.contains("score:"));
    }

    // ========================================================================
    // EDGE CASE TESTS
    // ========================================================================

    #[tokio::test]
    async fn test_empty_query() {
        let engine = RagEngine::in_memory().expect("Failed to create engine");

        let doc = create_test_document("Some content here.", "test");
        engine.add_document(&doc).await.expect("Failed to add doc");

        // Empty query should still work
        let response = engine.query("").await.expect("Query failed");

        // Response should be valid even with empty query
        assert!(!response.query.is_empty() || response.query.is_empty()); // Query is stored
    }

    #[tokio::test]
    async fn test_query_with_special_characters() {
        let engine = RagEngine::in_memory().expect("Failed to create engine");

        let doc = create_test_document("C++ and C# are programming languages.", "langs");
        engine.add_document(&doc).await.expect("Failed to add doc");

        // Query with special characters
        let response = engine.query("C++ programming").await.expect("Query failed");

        assert!(!response.answer.is_empty());
    }

    #[tokio::test]
    async fn test_query_with_unicode() {
        let engine = RagEngine::in_memory().expect("Failed to create engine");

        let doc = create_test_document(
            "Machine learning is used in Tokyo for traffic optimization.",
            "japan",
        );
        engine.add_document(&doc).await.expect("Failed to add doc");

        let response = engine.query("Tokyo traffic").await.expect("Query failed");

        assert!(!response.answer.is_empty());
    }

    #[tokio::test]
    async fn test_very_long_query() {
        let engine = RagEngine::in_memory().expect("Failed to create engine");

        let doc = create_test_document("Short content.", "short");
        engine.add_document(&doc).await.expect("Failed to add doc");

        // Very long query
        let long_query = "word ".repeat(1000);
        let response = engine.query(&long_query).await.expect("Query failed");

        assert!(!response.answer.is_empty());
    }

    #[tokio::test]
    async fn test_no_documents_query() {
        let engine = RagEngine::in_memory().expect("Failed to create engine");

        // Query with no documents indexed
        let response = engine.query("any query").await.expect("Query failed");

        // Should handle gracefully with empty results
        assert_eq!(response.sources.len(), 0);
        assert_eq!(response.retrieval_stats.chunks_used, 0);
    }

    // ========================================================================
    // RAG RESPONSE STRUCTURE TESTS
    // ========================================================================

    #[tokio::test]
    async fn test_rag_response_structure() {
        let engine = RagEngine::in_memory().expect("Failed to create engine");

        let doc = create_test_document("Complete content for response test.", "response");
        engine.add_document(&doc).await.expect("Failed to add doc");

        let response = engine.query("response test").await.expect("Query failed");

        // Verify all response fields
        assert!(!response.answer.is_empty());
        assert_eq!(response.query, "response test");
        assert!(response.tokens_used.is_none()); // No LLM client
        assert!(response.retrieval_stats.retrieval_time_ms >= 0);
    }

    #[tokio::test]
    async fn test_rag_source_structure() {
        let engine = RagEngine::in_memory().expect("Failed to create engine");

        let doc = create_test_document("Source structure test content.", "source");
        engine.add_document(&doc).await.expect("Failed to add doc");

        let response = engine
            .query("source structure")
            .await
            .expect("Query failed");

        for source in &response.sources {
            // Verify source fields
            assert!(!source.chunk_id.is_nil());
            assert!(!source.text.is_empty());
            assert!(source.score >= 0.0);
            // Source text should be truncated to 200 chars max
            assert!(source.text.len() <= 200 + 3); // +3 for "..."
        }
    }

    #[tokio::test]
    async fn test_rag_stats_structure() {
        let engine = RagEngine::in_memory().expect("Failed to create engine");

        let doc = create_test_document("Stats test content.", "stats");
        engine.add_document(&doc).await.expect("Failed to add doc");

        let response = engine.query("stats").await.expect("Query failed");

        let stats = &response.retrieval_stats;
        assert!(stats.chunks_retrieved > 0 || stats.chunks_used == 0);
        assert!(stats.retrieval_time_ms < 10000); // Should be fast
    }

    // ========================================================================
    // SERIALIZATION TESTS
    // ========================================================================

    #[tokio::test]
    async fn test_rag_response_serialization() {
        let engine = RagEngine::in_memory().expect("Failed to create engine");

        let doc = create_test_document("Serialization test.", "serial");
        engine.add_document(&doc).await.expect("Failed to add doc");

        let response = engine.query("serialization").await.expect("Query failed");

        // Should serialize to JSON
        let json = serde_json::to_string(&response).expect("Serialization failed");
        assert!(json.contains("answer"));
        assert!(json.contains("sources"));
        assert!(json.contains("retrieval_stats"));

        // Should deserialize back
        let deserialized: RagResponse =
            serde_json::from_str(&json).expect("Deserialization failed");
        assert_eq!(response.query, deserialized.query);
    }

    #[test]
    fn test_rag_source_serialization() {
        let source = RagSource {
            doc_id: Uuid::new_v4(),
            chunk_id: Uuid::new_v4(),
            text: "Test text".to_string(),
            score: 0.95,
            section: Some("Introduction".to_string()),
        };

        let json = serde_json::to_string(&source).expect("Serialization failed");
        let deserialized: RagSource = serde_json::from_str(&json).expect("Deserialization failed");

        assert_eq!(source.text, deserialized.text);
        assert_eq!(source.score, deserialized.score);
        assert_eq!(source.section, deserialized.section);
    }

    #[test]
    fn test_rag_retrieval_stats_serialization() {
        let stats = RagRetrievalStats {
            chunks_retrieved: 5,
            chunks_used: 3,
            context_tokens: 150,
            retrieval_time_ms: 42,
        };

        let json = serde_json::to_string(&stats).expect("Serialization failed");
        let deserialized: RagRetrievalStats =
            serde_json::from_str(&json).expect("Deserialization failed");

        assert_eq!(stats.chunks_retrieved, deserialized.chunks_retrieved);
        assert_eq!(stats.chunks_used, deserialized.chunks_used);
        assert_eq!(stats.context_tokens, deserialized.context_tokens);
        assert_eq!(stats.retrieval_time_ms, deserialized.retrieval_time_ms);
    }

    // ========================================================================
    // CONCURRENT ACCESS TESTS
    // ========================================================================

    #[tokio::test]
    async fn test_concurrent_queries() {
        let engine = std::sync::Arc::new(RagEngine::in_memory().expect("Failed to create engine"));

        let doc = create_test_document("Concurrent access test document.", "concurrent");
        engine.add_document(&doc).await.expect("Failed to add doc");

        // Spawn multiple concurrent queries
        let mut handles = vec![];
        for i in 0..5 {
            let engine_clone = engine.clone();
            let handle = tokio::spawn(async move {
                let query = format!("query {}", i);
                engine_clone.query(&query).await
            });
            handles.push(handle);
        }

        // All queries should complete successfully
        for handle in handles {
            let result = handle.await.expect("Task panicked");
            assert!(result.is_ok());
        }
    }

    // ========================================================================
    // RETRIEVAL ONLY MODE TESTS
    // ========================================================================

    #[tokio::test]
    async fn test_retrieve_only() {
        let engine = RagEngine::in_memory().expect("Failed to create engine");

        let doc = create_test_document(
            "Vector databases store embeddings for semantic search.",
            "vector-db",
        );
        engine.add_document(&doc).await.expect("Failed to add doc");

        let results = engine
            .retrieve("semantic search embeddings", 5)
            .await
            .expect("Retrieval failed");

        assert!(!results.is_empty());
        assert!(results[0].text.contains("embeddings"));

        // Verify HybridResult structure
        for result in &results {
            assert!(!result.chunk_id.is_nil());
            assert!(!result.text.is_empty());
        }
    }

    #[tokio::test]
    async fn test_retrieve_top_k_limit() {
        let engine = RagEngine::in_memory().expect("Failed to create engine");

        // Add many documents
        for i in 0..10 {
            let doc = create_test_document(
                &format!("Document {} about testing retrieval limits.", i),
                &format!("doc-{}", i),
            );
            engine.add_document(&doc).await.expect("Failed to add doc");
        }

        // Request only 3
        let results = engine
            .retrieve("testing retrieval", 3)
            .await
            .expect("Retrieval failed");

        assert!(results.len() <= 3);
    }

    // ========================================================================
    // BUILDER PATTERN TESTS
    // ========================================================================

    #[test]
    fn test_engine_builder_pattern() {
        let config = RagConfig::quick();

        // This should compile and work
        let _engine = RagEngine::in_memory()
            .expect("Failed to create engine")
            .with_config(config);
    }

    #[test]
    fn test_config_builder_pattern() {
        // Test that configs can be modified
        let mut config = RagConfig::default();
        config.top_k = 20;
        config.min_score = 0.5;

        assert_eq!(config.top_k, 20);
        assert_eq!(config.min_score, 0.5);
    }
}