oxirag 0.1.1

A four-layer RAG engine with SMT-based logic verification and knowledge graph support
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
//! Multi-stage verification pipeline for the Speculator layer.
//!
//! This module provides a flexible pipeline for verifying draft answers
//! through multiple stages, with optional early termination based on
//! confidence thresholds.

use crate::error::SpeculatorError;
use crate::layer2_speculator::calibration::ConfidenceCalibrator;
use crate::types::{Draft, SearchResult, SpeculationDecision, SpeculationResult};
use std::collections::HashSet;

/// Result from a single verification stage.
#[derive(Debug, Clone)]
pub struct StageResult {
    /// Name of the stage that produced this result.
    pub stage_name: String,
    /// Confidence score from this stage (0.0 to 1.0).
    pub confidence: f32,
    /// Whether verification should continue to the next stage.
    pub should_continue: bool,
    /// Optional notes or explanation from this stage.
    pub notes: Option<String>,
}

impl StageResult {
    /// Create a new stage result.
    #[must_use]
    pub fn new(stage_name: impl Into<String>, confidence: f32) -> Self {
        Self {
            stage_name: stage_name.into(),
            confidence: confidence.clamp(0.0, 1.0),
            should_continue: true,
            notes: None,
        }
    }

    /// Set whether verification should continue.
    #[must_use]
    pub fn with_continue(mut self, should_continue: bool) -> Self {
        self.should_continue = should_continue;
        self
    }

    /// Add notes to this stage result.
    #[must_use]
    pub fn with_notes(mut self, notes: impl Into<String>) -> Self {
        self.notes = Some(notes.into());
        self
    }
}

/// Trait for verification stages in the pipeline.
pub trait VerificationStage: Send + Sync {
    /// Get the name of this verification stage.
    fn name(&self) -> &str;

    /// Verify the draft against the context.
    ///
    /// # Arguments
    /// * `draft` - The draft answer to verify
    /// * `context` - The retrieved documents providing context
    ///
    /// # Returns
    /// A stage result with confidence and continuation decision.
    fn verify(&self, draft: &Draft, context: &[SearchResult]) -> StageResult;
}

/// Multi-stage verification pipeline.
pub struct VerificationPipeline {
    stages: Vec<Box<dyn VerificationStage>>,
    calibrator: Option<ConfidenceCalibrator>,
    early_accept_threshold: f32,
    early_reject_threshold: f32,
    aggregation_method: AggregationMethod,
}

/// Method for aggregating confidence scores across stages.
#[derive(Debug, Clone, Copy, Default)]
pub enum AggregationMethod {
    /// Use the average of all stage confidences.
    #[default]
    Average,
    /// Use the minimum confidence (most conservative).
    Minimum,
    /// Use the maximum confidence (most optimistic).
    Maximum,
    /// Use a weighted average based on stage order.
    WeightedAverage,
    /// Use the last stage's confidence.
    LastStage,
}

impl VerificationPipeline {
    /// Create a new empty verification pipeline.
    #[must_use]
    pub fn new() -> Self {
        Self {
            stages: Vec::new(),
            calibrator: None,
            early_accept_threshold: 0.95,
            early_reject_threshold: 0.1,
            aggregation_method: AggregationMethod::Average,
        }
    }

    /// Add a verification stage to the pipeline.
    #[must_use]
    pub fn add_stage(mut self, stage: Box<dyn VerificationStage>) -> Self {
        self.stages.push(stage);
        self
    }

    /// Set a confidence calibrator.
    #[must_use]
    pub fn with_calibrator(mut self, calibrator: ConfidenceCalibrator) -> Self {
        self.calibrator = Some(calibrator);
        self
    }

    /// Set the early accept threshold.
    #[must_use]
    pub fn with_early_accept_threshold(mut self, threshold: f32) -> Self {
        self.early_accept_threshold = threshold.clamp(0.0, 1.0);
        self
    }

    /// Set the early reject threshold.
    #[must_use]
    pub fn with_early_reject_threshold(mut self, threshold: f32) -> Self {
        self.early_reject_threshold = threshold.clamp(0.0, 1.0);
        self
    }

    /// Set the aggregation method.
    #[must_use]
    pub fn with_aggregation_method(mut self, method: AggregationMethod) -> Self {
        self.aggregation_method = method;
        self
    }

    /// Verify a draft through the pipeline.
    ///
    /// # Errors
    ///
    /// This function does not currently return errors, but the signature
    /// allows for future extensions where verification stages may fail.
    #[allow(clippy::unused_async)]
    pub async fn verify(
        &self,
        draft: &Draft,
        context: &[SearchResult],
    ) -> Result<SpeculationResult, SpeculatorError> {
        if self.stages.is_empty() {
            // No stages, return neutral result
            return Ok(SpeculationResult::new(SpeculationDecision::Revise, 0.5)
                .with_explanation("No verification stages configured"));
        }

        let mut stage_results = Vec::new();
        let mut all_notes = Vec::new();

        for stage in &self.stages {
            let result = stage.verify(draft, context);

            // Collect notes
            if let Some(ref notes) = result.notes {
                all_notes.push(format!("[{}] {}", result.stage_name, notes));
            }

            stage_results.push(result.clone());

            // Check for early termination
            if !result.should_continue {
                break;
            }

            // Early accept/reject based on thresholds
            if result.confidence >= self.early_accept_threshold {
                break;
            }
            if result.confidence <= self.early_reject_threshold {
                break;
            }
        }

        // Aggregate confidence scores
        let raw_confidence = self.aggregate_confidence(&stage_results);

        // Apply calibration if available
        let confidence = self
            .calibrator
            .as_ref()
            .map_or(raw_confidence, |c| c.calibrate(raw_confidence));

        // Determine decision
        let decision = if confidence >= self.early_accept_threshold {
            SpeculationDecision::Accept
        } else if confidence <= self.early_reject_threshold {
            SpeculationDecision::Reject
        } else {
            SpeculationDecision::Revise
        };

        // Build explanation
        let explanation = if all_notes.is_empty() {
            format!(
                "Verification completed through {} stage(s) with confidence {:.2}",
                stage_results.len(),
                confidence
            )
        } else {
            all_notes.join("; ")
        };

        Ok(SpeculationResult::new(decision, confidence).with_explanation(explanation))
    }

    /// Aggregate confidence scores based on the configured method.
    #[allow(clippy::cast_precision_loss)]
    fn aggregate_confidence(&self, results: &[StageResult]) -> f32 {
        if results.is_empty() {
            return 0.5;
        }

        match self.aggregation_method {
            AggregationMethod::Average => {
                let sum: f32 = results.iter().map(|r| r.confidence).sum();
                sum / results.len() as f32
            }
            AggregationMethod::Minimum => results
                .iter()
                .map(|r| r.confidence)
                .fold(f32::MAX, f32::min),
            AggregationMethod::Maximum => results
                .iter()
                .map(|r| r.confidence)
                .fold(f32::MIN, f32::max),
            AggregationMethod::WeightedAverage => {
                let weighted_sum: f32 = results
                    .iter()
                    .enumerate()
                    .map(|(i, r)| r.confidence * (i + 1) as f32)
                    .sum();
                let weight_total: f32 = (1..=results.len()).map(|i| i as f32).sum();
                weighted_sum / weight_total
            }
            AggregationMethod::LastStage => results.last().map_or(0.5, |r| r.confidence),
        }
    }

    /// Get the number of stages in the pipeline.
    #[must_use]
    pub fn num_stages(&self) -> usize {
        self.stages.len()
    }

    /// Get the early accept threshold.
    #[must_use]
    pub fn early_accept_threshold(&self) -> f32 {
        self.early_accept_threshold
    }

    /// Get the early reject threshold.
    #[must_use]
    pub fn early_reject_threshold(&self) -> f32 {
        self.early_reject_threshold
    }
}

impl Default for VerificationPipeline {
    fn default() -> Self {
        Self::new()
    }
}

// Built-in verification stages

/// Verification stage that checks for keyword overlap.
pub struct KeywordMatchStage {
    name: String,
    min_overlap_ratio: f32,
}

impl KeywordMatchStage {
    /// Create a new keyword match stage.
    #[must_use]
    pub fn new() -> Self {
        Self {
            name: "keyword_match".to_string(),
            min_overlap_ratio: 0.1,
        }
    }

    /// Set the minimum overlap ratio for passing.
    #[must_use]
    pub fn with_min_overlap(mut self, ratio: f32) -> Self {
        self.min_overlap_ratio = ratio.clamp(0.0, 1.0);
        self
    }
}

impl Default for KeywordMatchStage {
    fn default() -> Self {
        Self::new()
    }
}

impl VerificationStage for KeywordMatchStage {
    fn name(&self) -> &str {
        &self.name
    }

    fn verify(&self, draft: &Draft, context: &[SearchResult]) -> StageResult {
        if context.is_empty() {
            return StageResult::new(&self.name, 0.5)
                .with_notes("No context provided for keyword matching");
        }

        // Extract keywords from context
        let context_text: String = context
            .iter()
            .map(|r| r.document.content.as_str())
            .collect::<Vec<_>>()
            .join(" ");

        let context_words: HashSet<String> = context_text
            .to_lowercase()
            .split_whitespace()
            .filter(|w| w.len() > 3)
            .map(String::from)
            .collect();

        let draft_words: HashSet<String> = draft
            .content
            .to_lowercase()
            .split_whitespace()
            .filter(|w| w.len() > 3)
            .map(String::from)
            .collect();

        if draft_words.is_empty() {
            return StageResult::new(&self.name, 0.0)
                .with_notes("Draft contains no significant keywords")
                .with_continue(true);
        }

        #[allow(clippy::cast_precision_loss)]
        let overlap_count = draft_words.intersection(&context_words).count();
        #[allow(clippy::cast_precision_loss)]
        let overlap_ratio = overlap_count as f32 / draft_words.len() as f32;

        let confidence = overlap_ratio.clamp(0.0, 1.0);

        let notes = format!(
            "Found {}/{} draft keywords in context ({:.1}%)",
            overlap_count,
            draft_words.len(),
            overlap_ratio * 100.0
        );

        StageResult::new(&self.name, confidence)
            .with_notes(notes)
            .with_continue(confidence >= self.min_overlap_ratio)
    }
}

/// Verification stage that checks for semantic similarity.
pub struct SemanticSimilarityStage {
    name: String,
    min_similarity: f32,
}

impl SemanticSimilarityStage {
    /// Create a new semantic similarity stage.
    #[must_use]
    pub fn new() -> Self {
        Self {
            name: "semantic_similarity".to_string(),
            min_similarity: 0.5,
        }
    }

    /// Set the minimum similarity threshold.
    #[must_use]
    pub fn with_min_similarity(mut self, similarity: f32) -> Self {
        self.min_similarity = similarity.clamp(0.0, 1.0);
        self
    }

    /// Compute simple character n-gram based similarity.
    fn ngram_similarity(text1: &str, text2: &str, n: usize) -> f32 {
        let ngrams1: HashSet<String> = Self::extract_ngrams(text1, n);
        let ngrams2: HashSet<String> = Self::extract_ngrams(text2, n);

        if ngrams1.is_empty() || ngrams2.is_empty() {
            return 0.0;
        }

        #[allow(clippy::cast_precision_loss)]
        let intersection = ngrams1.intersection(&ngrams2).count() as f32;
        #[allow(clippy::cast_precision_loss)]
        let union = ngrams1.union(&ngrams2).count() as f32;

        intersection / union
    }

    /// Extract character n-grams from text.
    fn extract_ngrams(text: &str, n: usize) -> HashSet<String> {
        let chars: Vec<char> = text.to_lowercase().chars().collect();
        if chars.len() < n {
            return HashSet::new();
        }

        chars.windows(n).map(|w| w.iter().collect()).collect()
    }
}

impl Default for SemanticSimilarityStage {
    fn default() -> Self {
        Self::new()
    }
}

impl VerificationStage for SemanticSimilarityStage {
    fn name(&self) -> &str {
        &self.name
    }

    fn verify(&self, draft: &Draft, context: &[SearchResult]) -> StageResult {
        if context.is_empty() {
            return StageResult::new(&self.name, 0.5).with_notes("No context for similarity check");
        }

        // Combine context texts
        let context_text: String = context
            .iter()
            .map(|r| r.document.content.as_str())
            .collect::<Vec<_>>()
            .join(" ");

        // Compute n-gram similarities at different levels
        let sim_3gram = Self::ngram_similarity(&draft.content, &context_text, 3);
        let sim_4gram = Self::ngram_similarity(&draft.content, &context_text, 4);
        let sim_5gram = Self::ngram_similarity(&draft.content, &context_text, 5);

        // Weight higher n-grams more heavily (they capture phrases better)
        let similarity = sim_3gram * 0.2 + sim_4gram * 0.3 + sim_5gram * 0.5;
        let confidence = similarity.clamp(0.0, 1.0);

        let notes = format!(
            "N-gram similarity: 3-gram={sim_3gram:.2}, 4-gram={sim_4gram:.2}, 5-gram={sim_5gram:.2}"
        );

        StageResult::new(&self.name, confidence)
            .with_notes(notes)
            .with_continue(confidence >= self.min_similarity)
    }
}

/// Verification stage that checks for factual consistency.
pub struct FactualConsistencyStage {
    name: String,
    negation_patterns: Vec<String>,
    contradiction_penalty: f32,
}

impl FactualConsistencyStage {
    /// Create a new factual consistency stage.
    #[must_use]
    pub fn new() -> Self {
        Self {
            name: "factual_consistency".to_string(),
            negation_patterns: vec![
                "not".to_string(),
                "never".to_string(),
                "no".to_string(),
                "none".to_string(),
                "neither".to_string(),
                "without".to_string(),
                "isn't".to_string(),
                "aren't".to_string(),
                "wasn't".to_string(),
                "weren't".to_string(),
                "don't".to_string(),
                "doesn't".to_string(),
                "didn't".to_string(),
                "won't".to_string(),
                "wouldn't".to_string(),
                "can't".to_string(),
                "cannot".to_string(),
                "couldn't".to_string(),
            ],
            contradiction_penalty: 0.2,
        }
    }

    /// Set the penalty for contradictions.
    #[must_use]
    pub fn with_contradiction_penalty(mut self, penalty: f32) -> Self {
        self.contradiction_penalty = penalty.clamp(0.0, 1.0);
        self
    }

    /// Check if a sentence contains negation.
    fn contains_negation(&self, text: &str) -> bool {
        let lower = text.to_lowercase();
        self.negation_patterns.iter().any(|p| lower.contains(p))
    }

    /// Extract simple assertions from text.
    fn extract_assertions(text: &str) -> Vec<(String, String)> {
        // Very simple subject-predicate extraction
        let mut assertions = Vec::new();

        for sentence in text.split(['.', '!', '?']) {
            let sentence = sentence.trim().to_lowercase();
            if sentence.len() < 5 {
                continue;
            }

            // Split on "is", "are", "was", "were" as simple predicate markers
            for marker in ["is", "are", "was", "were"] {
                if let Some(pos) = sentence.find(&format!(" {marker} ")) {
                    let subject = sentence[..pos].trim().to_string();
                    let predicate = sentence[pos + marker.len() + 2..].trim().to_string();
                    if !subject.is_empty() && !predicate.is_empty() {
                        assertions.push((subject, predicate));
                    }
                }
            }
        }

        assertions
    }
}

impl Default for FactualConsistencyStage {
    fn default() -> Self {
        Self::new()
    }
}

impl VerificationStage for FactualConsistencyStage {
    fn name(&self) -> &str {
        &self.name
    }

    fn verify(&self, draft: &Draft, context: &[SearchResult]) -> StageResult {
        if context.is_empty() {
            return StageResult::new(&self.name, 0.5)
                .with_notes("No context for consistency check");
        }

        let context_text: String = context
            .iter()
            .map(|r| r.document.content.as_str())
            .collect::<Vec<_>>()
            .join(" ");

        let draft_assertions = Self::extract_assertions(&draft.content);
        let context_assertions = Self::extract_assertions(&context_text);

        if draft_assertions.is_empty() {
            return StageResult::new(&self.name, 0.7)
                .with_notes("No clear assertions found in draft");
        }

        let mut contradictions = 0;
        let mut supported = 0;

        for (draft_subj, draft_pred) in &draft_assertions {
            let draft_negated = self.contains_negation(draft_pred);

            for (ctx_subj, ctx_pred) in &context_assertions {
                // Check if subjects overlap
                if draft_subj.contains(ctx_subj) || ctx_subj.contains(draft_subj) {
                    let ctx_negated = self.contains_negation(ctx_pred);

                    // Check for predicate overlap with opposite negation
                    if draft_negated != ctx_negated {
                        // Potential contradiction
                        if draft_pred.split_whitespace().any(|w| ctx_pred.contains(w)) {
                            contradictions += 1;
                        }
                    } else if draft_pred.split_whitespace().any(|w| ctx_pred.contains(w)) {
                        // Same polarity with predicate overlap = support
                        supported += 1;
                    }
                }
            }
        }

        #[allow(clippy::cast_precision_loss)]
        let base_confidence = if supported > 0 || contradictions > 0 {
            supported as f32 / (supported + contradictions) as f32
        } else {
            0.6 // Neutral when no matches found
        };

        #[allow(clippy::cast_precision_loss)]
        let penalty = contradictions as f32 * self.contradiction_penalty;
        let confidence = (base_confidence - penalty).clamp(0.0, 1.0);

        let notes = format!(
            "Found {} supported and {} potential contradictions in {} assertions",
            supported,
            contradictions,
            draft_assertions.len()
        );

        StageResult::new(&self.name, confidence).with_notes(notes)
    }
}

/// Builder for creating verification pipelines with common configurations.
pub struct PipelineBuilder {
    pipeline: VerificationPipeline,
}

impl PipelineBuilder {
    /// Create a new pipeline builder.
    #[must_use]
    pub fn new() -> Self {
        Self {
            pipeline: VerificationPipeline::new(),
        }
    }

    /// Add the keyword match stage.
    #[must_use]
    pub fn with_keyword_match(mut self) -> Self {
        self.pipeline = self.pipeline.add_stage(Box::new(KeywordMatchStage::new()));
        self
    }

    /// Add the semantic similarity stage.
    #[must_use]
    pub fn with_semantic_similarity(mut self) -> Self {
        self.pipeline = self
            .pipeline
            .add_stage(Box::new(SemanticSimilarityStage::new()));
        self
    }

    /// Add the factual consistency stage.
    #[must_use]
    pub fn with_factual_consistency(mut self) -> Self {
        self.pipeline = self
            .pipeline
            .add_stage(Box::new(FactualConsistencyStage::new()));
        self
    }

    /// Add all standard stages.
    #[must_use]
    pub fn with_standard_stages(self) -> Self {
        self.with_keyword_match()
            .with_semantic_similarity()
            .with_factual_consistency()
    }

    /// Set the early accept threshold.
    #[must_use]
    pub fn early_accept(mut self, threshold: f32) -> Self {
        self.pipeline = self.pipeline.with_early_accept_threshold(threshold);
        self
    }

    /// Set the early reject threshold.
    #[must_use]
    pub fn early_reject(mut self, threshold: f32) -> Self {
        self.pipeline = self.pipeline.with_early_reject_threshold(threshold);
        self
    }

    /// Set the aggregation method.
    #[must_use]
    pub fn aggregation(mut self, method: AggregationMethod) -> Self {
        self.pipeline = self.pipeline.with_aggregation_method(method);
        self
    }

    /// Set a calibrator.
    #[must_use]
    pub fn calibrator(mut self, calibrator: ConfidenceCalibrator) -> Self {
        self.pipeline = self.pipeline.with_calibrator(calibrator);
        self
    }

    /// Build the pipeline.
    #[must_use]
    pub fn build(self) -> VerificationPipeline {
        self.pipeline
    }
}

impl Default for PipelineBuilder {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
#[allow(clippy::float_cmp, clippy::unnecessary_to_owned)]
mod tests {
    use super::*;
    use crate::types::Document;

    fn create_context() -> Vec<SearchResult> {
        vec![
            SearchResult::new(
                Document::new("Paris is the capital of France. It is known for the Eiffel Tower."),
                0.9,
                0,
            ),
            SearchResult::new(
                Document::new(
                    "France is a country in Western Europe with a population of about 67 million.",
                ),
                0.85,
                1,
            ),
        ]
    }

    #[test]
    fn test_stage_result_creation() {
        let result = StageResult::new("test_stage", 0.8)
            .with_continue(true)
            .with_notes("Test notes");

        assert_eq!(result.stage_name, "test_stage");
        assert_eq!(result.confidence, 0.8);
        assert!(result.should_continue);
        assert_eq!(result.notes, Some("Test notes".to_string()));
    }

    #[test]
    fn test_stage_result_clamping() {
        let result = StageResult::new("test", 1.5);
        assert_eq!(result.confidence, 1.0);

        let result2 = StageResult::new("test", -0.5);
        assert_eq!(result2.confidence, 0.0);
    }

    #[test]
    fn test_pipeline_creation() {
        let pipeline = VerificationPipeline::new()
            .with_early_accept_threshold(0.9)
            .with_early_reject_threshold(0.2);

        assert_eq!(pipeline.num_stages(), 0);
        assert_eq!(pipeline.early_accept_threshold(), 0.9);
        assert_eq!(pipeline.early_reject_threshold(), 0.2);
    }

    #[test]
    fn test_pipeline_add_stage() {
        let pipeline = VerificationPipeline::new().add_stage(Box::new(KeywordMatchStage::new()));

        assert_eq!(pipeline.num_stages(), 1);
    }

    #[tokio::test]
    async fn test_pipeline_empty() {
        let pipeline = VerificationPipeline::new();
        let draft = Draft::new("Test answer", "Test question");
        let context = create_context();

        let result = pipeline.verify(&draft, &context).await.unwrap();

        assert!(matches!(result.decision, SpeculationDecision::Revise));
        assert_eq!(result.confidence, 0.5);
    }

    #[tokio::test]
    async fn test_pipeline_with_stages() {
        let pipeline = VerificationPipeline::new()
            .add_stage(Box::new(KeywordMatchStage::new()))
            .add_stage(Box::new(SemanticSimilarityStage::new()));

        let draft = Draft::new(
            "Paris is the capital of France and is known for the Eiffel Tower.",
            "What is the capital of France?",
        );
        let context = create_context();

        let result = pipeline.verify(&draft, &context).await.unwrap();

        assert!(result.confidence > 0.0);
    }

    #[test]
    fn test_keyword_match_stage() {
        let stage = KeywordMatchStage::new();
        let draft = Draft::new(
            "Paris is the capital of France with the Eiffel Tower.",
            "test",
        );
        let context = create_context();

        let result = stage.verify(&draft, &context);

        assert_eq!(result.stage_name, "keyword_match");
        assert!(result.confidence > 0.0);
        assert!(result.notes.is_some());
    }

    #[test]
    fn test_keyword_match_no_context() {
        let stage = KeywordMatchStage::new();
        let draft = Draft::new("Some text", "test");

        let result = stage.verify(&draft, &[]);

        assert_eq!(result.confidence, 0.5);
    }

    #[test]
    fn test_keyword_match_empty_draft() {
        let stage = KeywordMatchStage::new();
        let draft = Draft::new("a", "test"); // No words > 3 chars
        let context = create_context();

        let result = stage.verify(&draft, &context);

        assert_eq!(result.confidence, 0.0);
    }

    #[test]
    fn test_semantic_similarity_stage() {
        let stage = SemanticSimilarityStage::new();
        let draft = Draft::new(
            "Paris is the capital of France known for the Eiffel Tower.",
            "test",
        );
        let context = create_context();

        let result = stage.verify(&draft, &context);

        assert_eq!(result.stage_name, "semantic_similarity");
        assert!(result.confidence >= 0.0);
    }

    #[test]
    fn test_semantic_similarity_no_context() {
        let stage = SemanticSimilarityStage::new();
        let draft = Draft::new("Some text", "test");

        let result = stage.verify(&draft, &[]);

        assert_eq!(result.confidence, 0.5);
    }

    #[test]
    fn test_factual_consistency_stage() {
        let stage = FactualConsistencyStage::new();
        let draft = Draft::new("Paris is the capital of France.", "test");
        let context = create_context();

        let result = stage.verify(&draft, &context);

        assert_eq!(result.stage_name, "factual_consistency");
    }

    #[test]
    fn test_factual_consistency_contradiction() {
        let stage = FactualConsistencyStage::new();
        let draft = Draft::new("Paris is not the capital of France.", "test");
        let context = vec![SearchResult::new(
            Document::new("Paris is the capital of France."),
            0.9,
            0,
        )];

        let result = stage.verify(&draft, &context);

        // Should detect potential contradiction
        assert!(result.notes.is_some());
    }

    #[test]
    fn test_factual_consistency_no_context() {
        let stage = FactualConsistencyStage::new();
        let draft = Draft::new("Some statement", "test");

        let result = stage.verify(&draft, &[]);

        assert_eq!(result.confidence, 0.5);
    }

    #[test]
    fn test_aggregation_average() {
        let pipeline =
            VerificationPipeline::new().with_aggregation_method(AggregationMethod::Average);

        let results = vec![
            StageResult::new("s1", 0.6),
            StageResult::new("s2", 0.8),
            StageResult::new("s3", 0.4),
        ];

        let confidence = pipeline.aggregate_confidence(&results);
        assert!((confidence - 0.6).abs() < 0.01);
    }

    #[test]
    fn test_aggregation_minimum() {
        let pipeline =
            VerificationPipeline::new().with_aggregation_method(AggregationMethod::Minimum);

        let results = vec![
            StageResult::new("s1", 0.6),
            StageResult::new("s2", 0.8),
            StageResult::new("s3", 0.4),
        ];

        let confidence = pipeline.aggregate_confidence(&results);
        assert!((confidence - 0.4).abs() < 0.01);
    }

    #[test]
    fn test_aggregation_maximum() {
        let pipeline =
            VerificationPipeline::new().with_aggregation_method(AggregationMethod::Maximum);

        let results = vec![
            StageResult::new("s1", 0.6),
            StageResult::new("s2", 0.8),
            StageResult::new("s3", 0.4),
        ];

        let confidence = pipeline.aggregate_confidence(&results);
        assert!((confidence - 0.8).abs() < 0.01);
    }

    #[test]
    fn test_aggregation_last_stage() {
        let pipeline =
            VerificationPipeline::new().with_aggregation_method(AggregationMethod::LastStage);

        let results = vec![
            StageResult::new("s1", 0.6),
            StageResult::new("s2", 0.8),
            StageResult::new("s3", 0.4),
        ];

        let confidence = pipeline.aggregate_confidence(&results);
        assert!((confidence - 0.4).abs() < 0.01);
    }

    #[test]
    fn test_aggregation_empty() {
        let pipeline = VerificationPipeline::new();
        let confidence = pipeline.aggregate_confidence(&[]);
        assert_eq!(confidence, 0.5);
    }

    #[test]
    fn test_pipeline_builder() {
        let pipeline = PipelineBuilder::new()
            .with_standard_stages()
            .early_accept(0.9)
            .early_reject(0.1)
            .build();

        assert_eq!(pipeline.num_stages(), 3);
        assert_eq!(pipeline.early_accept_threshold(), 0.9);
        assert_eq!(pipeline.early_reject_threshold(), 0.1);
    }

    #[test]
    fn test_pipeline_builder_individual_stages() {
        let pipeline = PipelineBuilder::new()
            .with_keyword_match()
            .with_semantic_similarity()
            .build();

        assert_eq!(pipeline.num_stages(), 2);
    }

    #[tokio::test]
    async fn test_pipeline_early_accept() {
        let pipeline = VerificationPipeline::new()
            .with_early_accept_threshold(0.5)
            .add_stage(Box::new(KeywordMatchStage::new()));

        let draft = Draft::new(
            "Paris France capital Eiffel Tower Western Europe population million country",
            "test",
        );
        let context = create_context();

        let result = pipeline.verify(&draft, &context).await.unwrap();

        // High keyword overlap should lead to acceptance
        assert!(result.confidence >= 0.5);
    }

    #[test]
    fn test_ngram_extraction() {
        let ngrams = SemanticSimilarityStage::extract_ngrams("hello", 3);
        assert!(ngrams.contains(&"hel".to_string()));
        assert!(ngrams.contains(&"ell".to_string()));
        assert!(ngrams.contains(&"llo".to_string()));
    }

    #[test]
    fn test_ngram_similarity() {
        let sim = SemanticSimilarityStage::ngram_similarity("hello world", "hello world", 3);
        assert_eq!(sim, 1.0);

        let sim2 = SemanticSimilarityStage::ngram_similarity("abc", "xyz", 3);
        assert_eq!(sim2, 0.0);
    }

    #[test]
    fn test_contains_negation() {
        let stage = FactualConsistencyStage::new();
        assert!(stage.contains_negation("This is not correct"));
        assert!(stage.contains_negation("There was never any doubt"));
        assert!(!stage.contains_negation("This is correct"));
    }

    #[test]
    fn test_extract_assertions() {
        let assertions = FactualConsistencyStage::extract_assertions("Paris is the capital.");
        assert!(!assertions.is_empty());
    }

    #[test]
    fn test_pipeline_default() {
        let pipeline = VerificationPipeline::default();
        assert_eq!(pipeline.num_stages(), 0);
    }

    #[test]
    fn test_pipeline_builder_default() {
        let builder = PipelineBuilder::default();
        let pipeline = builder.build();
        assert_eq!(pipeline.num_stages(), 0);
    }

    #[test]
    fn test_aggregation_method_default() {
        let method = AggregationMethod::default();
        assert!(matches!(method, AggregationMethod::Average));
    }
}