cognis-core 0.2.1

Core traits and types for the Cognis LLM framework
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
//! Cross-encoder interface for reranking.
//!
//! Cross-encoders score (query, document) pairs for semantic similarity,
//! enabling reranking of search results. This module provides:
//!
//! - [`CrossEncoder`] -- base trait for scoring text pairs
//! - [`CrossEncoderResult`] -- scored pair with index and metadata
//! - [`FakeCrossEncoder`] -- deterministic scorer for testing
//! - [`ThresholdCrossEncoder`] -- filters pairs below a score threshold
//! - [`BatchCrossEncoder`] -- batched scoring with configurable batch size
//! - [`CrossEncoderReranker`] -- reranks documents by cross-encoder score
//! - [`CachedCrossEncoder`] -- LRU-cached scoring wrapper
//! - [`NormalizedCrossEncoder`] -- normalizes scores to \[0, 1\]

use std::collections::HashMap;
use std::sync::Mutex;

use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::documents::Document;
use crate::error::Result;

// ---------------------------------------------------------------------------
// Core trait
// ---------------------------------------------------------------------------

/// Base trait for cross-encoder models that score text pairs for similarity.
#[async_trait]
pub trait CrossEncoder: Send + Sync {
    /// Score a list of (text_a, text_b) pairs.
    ///
    /// Returns one `f64` score per pair, where higher values indicate greater
    /// similarity.
    async fn score_pairs(&self, pairs: &[(String, String)]) -> Result<Vec<f64>>;
}

// ---------------------------------------------------------------------------
// Result type
// ---------------------------------------------------------------------------

/// The result of scoring a single text pair through a cross-encoder.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct CrossEncoderResult {
    /// Index of the pair in the original input list.
    pub index: usize,
    /// The similarity score produced by the cross-encoder.
    pub score: f64,
    /// Optional metadata attached to this result.
    #[serde(default, skip_serializing_if = "HashMap::is_empty")]
    pub metadata: HashMap<String, Value>,
}

impl CrossEncoderResult {
    /// Create a new result with the given index and score.
    pub fn new(index: usize, score: f64) -> Self {
        Self {
            index,
            score,
            metadata: HashMap::new(),
        }
    }

    /// Attach metadata to this result.
    pub fn with_metadata(mut self, metadata: HashMap<String, Value>) -> Self {
        self.metadata = metadata;
        self
    }
}

// ---------------------------------------------------------------------------
// FakeCrossEncoder
// ---------------------------------------------------------------------------

/// A deterministic cross-encoder for testing.
///
/// Scores each pair based on the fraction of characters in the second string
/// that also appear in the first string (case-insensitive character overlap).
#[derive(Debug, Clone, Default)]
pub struct FakeCrossEncoder;

impl FakeCrossEncoder {
    pub fn new() -> Self {
        Self
    }

    fn overlap_score(a: &str, b: &str) -> f64 {
        if b.is_empty() {
            return 0.0;
        }
        let a_lower: Vec<char> = a.to_lowercase().chars().collect();
        let matching = b
            .to_lowercase()
            .chars()
            .filter(|c| a_lower.contains(c))
            .count();
        matching as f64 / b.len() as f64
    }
}

#[async_trait]
impl CrossEncoder for FakeCrossEncoder {
    async fn score_pairs(&self, pairs: &[(String, String)]) -> Result<Vec<f64>> {
        Ok(pairs
            .iter()
            .map(|(a, b)| Self::overlap_score(a, b))
            .collect())
    }
}

// ---------------------------------------------------------------------------
// ThresholdCrossEncoder
// ---------------------------------------------------------------------------

/// Wraps a cross-encoder and zeroes out scores below a threshold.
///
/// Pairs with a score below `threshold` receive a score of `0.0`.
#[derive(Debug)]
pub struct ThresholdCrossEncoder<E: CrossEncoder> {
    inner: E,
    threshold: f64,
}

impl<E: CrossEncoder> ThresholdCrossEncoder<E> {
    /// Create a new threshold wrapper.
    pub fn new(inner: E, threshold: f64) -> Self {
        Self { inner, threshold }
    }

    /// Return the current threshold.
    pub fn threshold(&self) -> f64 {
        self.threshold
    }
}

#[async_trait]
impl<E: CrossEncoder> CrossEncoder for ThresholdCrossEncoder<E> {
    async fn score_pairs(&self, pairs: &[(String, String)]) -> Result<Vec<f64>> {
        let scores = self.inner.score_pairs(pairs).await?;
        Ok(scores
            .into_iter()
            .map(|s| if s >= self.threshold { s } else { 0.0 })
            .collect())
    }
}

// ---------------------------------------------------------------------------
// BatchCrossEncoder
// ---------------------------------------------------------------------------

/// Wraps a cross-encoder and processes pairs in fixed-size batches.
#[derive(Debug)]
pub struct BatchCrossEncoder<E: CrossEncoder> {
    inner: E,
    batch_size: usize,
}

impl<E: CrossEncoder> BatchCrossEncoder<E> {
    /// Create a batch wrapper with the given batch size.
    ///
    /// # Panics
    /// Panics if `batch_size` is zero.
    pub fn new(inner: E, batch_size: usize) -> Self {
        assert!(batch_size > 0, "batch_size must be > 0");
        Self { inner, batch_size }
    }

    /// Return the configured batch size.
    pub fn batch_size(&self) -> usize {
        self.batch_size
    }
}

#[async_trait]
impl<E: CrossEncoder> CrossEncoder for BatchCrossEncoder<E> {
    async fn score_pairs(&self, pairs: &[(String, String)]) -> Result<Vec<f64>> {
        let mut all_scores = Vec::with_capacity(pairs.len());
        for chunk in pairs.chunks(self.batch_size) {
            let scores = self.inner.score_pairs(chunk).await?;
            all_scores.extend(scores);
        }
        Ok(all_scores)
    }
}

// ---------------------------------------------------------------------------
// CrossEncoderReranker
// ---------------------------------------------------------------------------

/// Reranks documents by scoring each against a query via a cross-encoder.
#[derive(Debug)]
pub struct CrossEncoderReranker<E: CrossEncoder> {
    encoder: E,
    top_k: Option<usize>,
}

impl<E: CrossEncoder> CrossEncoderReranker<E> {
    /// Create a new reranker.
    pub fn new(encoder: E) -> Self {
        Self {
            encoder,
            top_k: None,
        }
    }

    /// Limit the number of returned documents.
    pub fn with_top_k(mut self, k: usize) -> Self {
        self.top_k = Some(k);
        self
    }

    /// Score and rerank `documents` against `query`.
    ///
    /// Returns [`CrossEncoderResult`]s sorted by descending score. The `index`
    /// field refers to the position in the original `documents` slice.
    pub async fn rerank(
        &self,
        query: &str,
        documents: &[Document],
    ) -> Result<Vec<CrossEncoderResult>> {
        if documents.is_empty() {
            return Ok(vec![]);
        }

        let pairs: Vec<(String, String)> = documents
            .iter()
            .map(|d| (query.to_string(), d.page_content.clone()))
            .collect();

        let scores = self.encoder.score_pairs(&pairs).await?;

        let mut results: Vec<CrossEncoderResult> = scores
            .into_iter()
            .enumerate()
            .map(|(i, score)| {
                let mut meta = documents[i].metadata.clone();
                if let Some(id) = &documents[i].id {
                    meta.insert("document_id".to_string(), Value::String(id.clone()));
                }
                CrossEncoderResult {
                    index: i,
                    score,
                    metadata: meta,
                }
            })
            .collect();

        results.sort_by(|a, b| {
            b.score
                .partial_cmp(&a.score)
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        if let Some(k) = self.top_k {
            results.truncate(k);
        }

        Ok(results)
    }
}

// ---------------------------------------------------------------------------
// CachedCrossEncoder
// ---------------------------------------------------------------------------

/// An LRU-cached wrapper around a cross-encoder.
///
/// Caches individual pair scores so repeated queries are not re-scored.
/// The cache key is the pair `(text_a, text_b)`.
#[derive(Debug)]
pub struct CachedCrossEncoder<E: CrossEncoder> {
    inner: E,
    cache: Mutex<LruCache>,
}

/// A simple bounded LRU cache backed by a `Vec`.
#[derive(Debug)]
struct LruCache {
    entries: Vec<((String, String), f64)>,
    capacity: usize,
}

impl LruCache {
    fn new(capacity: usize) -> Self {
        Self {
            entries: Vec::with_capacity(capacity),
            capacity,
        }
    }

    fn get(&mut self, key: &(String, String)) -> Option<f64> {
        if let Some(pos) = self.entries.iter().position(|(k, _)| k == key) {
            let entry = self.entries.remove(pos);
            let score = entry.1;
            self.entries.push(entry);
            Some(score)
        } else {
            None
        }
    }

    fn insert(&mut self, key: (String, String), value: f64) {
        // Remove existing entry if present.
        if let Some(pos) = self.entries.iter().position(|(k, _)| k == &key) {
            self.entries.remove(pos);
        }
        if self.entries.len() >= self.capacity {
            self.entries.remove(0); // evict oldest
        }
        self.entries.push((key, value));
    }

    fn len(&self) -> usize {
        self.entries.len()
    }
}

impl<E: CrossEncoder> CachedCrossEncoder<E> {
    /// Create a cached wrapper with the given LRU capacity.
    pub fn new(inner: E, capacity: usize) -> Self {
        Self {
            inner,
            cache: Mutex::new(LruCache::new(capacity)),
        }
    }

    /// Return the number of entries currently in the cache.
    pub fn cache_len(&self) -> usize {
        self.cache.lock().unwrap().len()
    }
}

#[async_trait]
impl<E: CrossEncoder> CrossEncoder for CachedCrossEncoder<E> {
    async fn score_pairs(&self, pairs: &[(String, String)]) -> Result<Vec<f64>> {
        let mut results = vec![0.0_f64; pairs.len()];
        let mut misses: Vec<(usize, (String, String))> = Vec::new();

        {
            let mut cache = self.cache.lock().unwrap();
            for (i, pair) in pairs.iter().enumerate() {
                if let Some(score) = cache.get(pair) {
                    results[i] = score;
                } else {
                    misses.push((i, pair.clone()));
                }
            }
        }

        if !misses.is_empty() {
            let miss_pairs: Vec<(String, String)> = misses.iter().map(|(_, p)| p.clone()).collect();
            let scores = self.inner.score_pairs(&miss_pairs).await?;

            let mut cache = self.cache.lock().unwrap();
            for ((idx, pair), score) in misses.into_iter().zip(scores) {
                cache.insert(pair, score);
                results[idx] = score;
            }
        }

        Ok(results)
    }
}

// ---------------------------------------------------------------------------
// NormalizedCrossEncoder
// ---------------------------------------------------------------------------

/// Wraps a cross-encoder and normalizes all scores to the `[0, 1]` range
/// using min-max normalization across the batch.
///
/// If all scores in a batch are equal the output is `0.5` for every pair.
#[derive(Debug)]
pub struct NormalizedCrossEncoder<E: CrossEncoder> {
    inner: E,
}

impl<E: CrossEncoder> NormalizedCrossEncoder<E> {
    /// Create a normalized wrapper.
    pub fn new(inner: E) -> Self {
        Self { inner }
    }
}

#[async_trait]
impl<E: CrossEncoder> CrossEncoder for NormalizedCrossEncoder<E> {
    async fn score_pairs(&self, pairs: &[(String, String)]) -> Result<Vec<f64>> {
        let scores = self.inner.score_pairs(pairs).await?;
        if scores.is_empty() {
            return Ok(scores);
        }

        let min = scores.iter().cloned().fold(f64::INFINITY, f64::min);
        let max = scores.iter().cloned().fold(f64::NEG_INFINITY, f64::max);
        let range = max - min;

        if range == 0.0 {
            return Ok(vec![0.5; scores.len()]);
        }

        Ok(scores.into_iter().map(|s| (s - min) / range).collect())
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::Arc;

    // ---- CrossEncoderResult ----

    #[test]
    fn result_new() {
        let r = CrossEncoderResult::new(3, 0.75);
        assert_eq!(r.index, 3);
        assert!((r.score - 0.75).abs() < f64::EPSILON);
        assert!(r.metadata.is_empty());
    }

    #[test]
    fn result_with_metadata() {
        let mut meta = HashMap::new();
        meta.insert("source".into(), Value::String("test".into()));
        let r = CrossEncoderResult::new(0, 0.5).with_metadata(meta.clone());
        assert_eq!(r.metadata, meta);
    }

    #[test]
    fn result_serialization_roundtrip() {
        let r = CrossEncoderResult::new(1, 0.9);
        let json = serde_json::to_string(&r).unwrap();
        let r2: CrossEncoderResult = serde_json::from_str(&json).unwrap();
        assert_eq!(r, r2);
    }

    #[test]
    fn result_serialization_with_metadata() {
        let mut meta = HashMap::new();
        meta.insert("k".into(), Value::Number(42.into()));
        let r = CrossEncoderResult::new(0, 1.0).with_metadata(meta);
        let json = serde_json::to_string(&r).unwrap();
        assert!(json.contains("\"k\":42"));
    }

    #[test]
    fn result_empty_metadata_not_serialized() {
        let r = CrossEncoderResult::new(0, 0.0);
        let json = serde_json::to_string(&r).unwrap();
        assert!(!json.contains("metadata"));
    }

    // ---- FakeCrossEncoder ----

    #[tokio::test]
    async fn fake_identical_strings() {
        let enc = FakeCrossEncoder::new();
        let pairs = vec![("hello".into(), "hello".into())];
        let scores = enc.score_pairs(&pairs).await.unwrap();
        assert!((scores[0] - 1.0).abs() < f64::EPSILON);
    }

    #[tokio::test]
    async fn fake_no_overlap() {
        let enc = FakeCrossEncoder::new();
        let pairs = vec![("abc".into(), "xyz".into())];
        let scores = enc.score_pairs(&pairs).await.unwrap();
        assert!((scores[0]).abs() < f64::EPSILON);
    }

    #[tokio::test]
    async fn fake_partial_overlap() {
        let enc = FakeCrossEncoder::new();
        let pairs = vec![("abcd".into(), "abef".into())];
        let scores = enc.score_pairs(&pairs).await.unwrap();
        // 'a' and 'b' from "abef" match in "abcd" => 2/4 = 0.5
        assert!((scores[0] - 0.5).abs() < f64::EPSILON);
    }

    #[tokio::test]
    async fn fake_case_insensitive() {
        let enc = FakeCrossEncoder::new();
        let pairs = vec![("HELLO".into(), "hello".into())];
        let scores = enc.score_pairs(&pairs).await.unwrap();
        assert!((scores[0] - 1.0).abs() < f64::EPSILON);
    }

    #[tokio::test]
    async fn fake_empty_second_string() {
        let enc = FakeCrossEncoder::new();
        let pairs = vec![("hello".into(), "".into())];
        let scores = enc.score_pairs(&pairs).await.unwrap();
        assert!((scores[0]).abs() < f64::EPSILON);
    }

    #[tokio::test]
    async fn fake_empty_first_string() {
        let enc = FakeCrossEncoder::new();
        let pairs = vec![("".into(), "hello".into())];
        let scores = enc.score_pairs(&pairs).await.unwrap();
        assert!((scores[0]).abs() < f64::EPSILON);
    }

    #[tokio::test]
    async fn fake_both_empty() {
        let enc = FakeCrossEncoder::new();
        let pairs = vec![("".into(), "".into())];
        let scores = enc.score_pairs(&pairs).await.unwrap();
        assert!((scores[0]).abs() < f64::EPSILON);
    }

    #[tokio::test]
    async fn fake_multiple_pairs() {
        let enc = FakeCrossEncoder::new();
        let pairs = vec![
            ("abc".into(), "abc".into()),
            ("abc".into(), "xyz".into()),
            ("abc".into(), "abx".into()),
        ];
        let scores = enc.score_pairs(&pairs).await.unwrap();
        assert_eq!(scores.len(), 3);
        assert!((scores[0] - 1.0).abs() < f64::EPSILON);
        assert!((scores[1]).abs() < f64::EPSILON);
        // 'a','b' match out of 3 => 2/3
        assert!((scores[2] - 2.0 / 3.0).abs() < 1e-10);
    }

    #[tokio::test]
    async fn fake_empty_pairs_list() {
        let enc = FakeCrossEncoder::new();
        let pairs: Vec<(String, String)> = vec![];
        let scores = enc.score_pairs(&pairs).await.unwrap();
        assert!(scores.is_empty());
    }

    #[tokio::test]
    async fn fake_deterministic() {
        let enc = FakeCrossEncoder::new();
        let pairs = vec![("rust is great".into(), "great rust".into())];
        let s1 = enc.score_pairs(&pairs).await.unwrap();
        let s2 = enc.score_pairs(&pairs).await.unwrap();
        assert_eq!(s1, s2);
    }

    // ---- ThresholdCrossEncoder ----

    #[tokio::test]
    async fn threshold_filters_below() {
        let enc = ThresholdCrossEncoder::new(FakeCrossEncoder::new(), 0.6);
        let pairs = vec![
            ("abc".into(), "abc".into()), // 1.0 -> keep
            ("abc".into(), "xyz".into()), // 0.0 -> zero
            ("abc".into(), "abx".into()), // 0.666 -> keep
        ];
        let scores = enc.score_pairs(&pairs).await.unwrap();
        assert!((scores[0] - 1.0).abs() < f64::EPSILON);
        assert!((scores[1]).abs() < f64::EPSILON);
        assert!(scores[2] > 0.0);
    }

    #[tokio::test]
    async fn threshold_exact_boundary() {
        let enc = ThresholdCrossEncoder::new(FakeCrossEncoder::new(), 0.5);
        // "abcd" vs "abef" => 0.5, should be kept (>=)
        let pairs = vec![("abcd".into(), "abef".into())];
        let scores = enc.score_pairs(&pairs).await.unwrap();
        assert!((scores[0] - 0.5).abs() < f64::EPSILON);
    }

    #[tokio::test]
    async fn threshold_accessor() {
        let enc = ThresholdCrossEncoder::new(FakeCrossEncoder::new(), 0.42);
        assert!((enc.threshold() - 0.42).abs() < f64::EPSILON);
    }

    #[tokio::test]
    async fn threshold_zero_keeps_all() {
        let enc = ThresholdCrossEncoder::new(FakeCrossEncoder::new(), 0.0);
        let pairs = vec![("abc".into(), "xyz".into())]; // score 0.0
        let scores = enc.score_pairs(&pairs).await.unwrap();
        // 0.0 >= 0.0 is true, so it stays
        assert!((scores[0]).abs() < f64::EPSILON);
    }

    #[tokio::test]
    async fn threshold_one_filters_imperfect() {
        let enc = ThresholdCrossEncoder::new(FakeCrossEncoder::new(), 1.0);
        let pairs = vec![
            ("abc".into(), "abc".into()), // 1.0 -> keep
            ("abc".into(), "abx".into()), // 0.666 -> zero
        ];
        let scores = enc.score_pairs(&pairs).await.unwrap();
        assert!((scores[0] - 1.0).abs() < f64::EPSILON);
        assert!((scores[1]).abs() < f64::EPSILON);
    }

    // ---- BatchCrossEncoder ----

    #[tokio::test]
    async fn batch_produces_same_results() {
        let enc = BatchCrossEncoder::new(FakeCrossEncoder::new(), 2);
        let pairs = vec![
            ("abc".into(), "abc".into()),
            ("abc".into(), "xyz".into()),
            ("abc".into(), "abx".into()),
        ];
        let batch_scores = enc.score_pairs(&pairs).await.unwrap();
        let direct_scores = FakeCrossEncoder::new().score_pairs(&pairs).await.unwrap();
        assert_eq!(batch_scores, direct_scores);
    }

    #[tokio::test]
    async fn batch_size_accessor() {
        let enc = BatchCrossEncoder::new(FakeCrossEncoder::new(), 10);
        assert_eq!(enc.batch_size(), 10);
    }

    #[tokio::test]
    async fn batch_single_item_batches() {
        let enc = BatchCrossEncoder::new(FakeCrossEncoder::new(), 1);
        let pairs = vec![("a".into(), "a".into()), ("b".into(), "b".into())];
        let scores = enc.score_pairs(&pairs).await.unwrap();
        assert_eq!(scores.len(), 2);
        assert!((scores[0] - 1.0).abs() < f64::EPSILON);
        assert!((scores[1] - 1.0).abs() < f64::EPSILON);
    }

    #[tokio::test]
    async fn batch_larger_than_input() {
        let enc = BatchCrossEncoder::new(FakeCrossEncoder::new(), 100);
        let pairs = vec![("abc".into(), "abc".into())];
        let scores = enc.score_pairs(&pairs).await.unwrap();
        assert_eq!(scores.len(), 1);
    }

    #[tokio::test]
    async fn batch_empty_input() {
        let enc = BatchCrossEncoder::new(FakeCrossEncoder::new(), 5);
        let pairs: Vec<(String, String)> = vec![];
        let scores = enc.score_pairs(&pairs).await.unwrap();
        assert!(scores.is_empty());
    }

    #[test]
    #[should_panic(expected = "batch_size must be > 0")]
    fn batch_zero_panics() {
        let _ = BatchCrossEncoder::new(FakeCrossEncoder::new(), 0);
    }

    // ---- CrossEncoderReranker ----

    #[tokio::test]
    async fn reranker_basic_order() {
        let reranker = CrossEncoderReranker::new(FakeCrossEncoder::new());
        let docs = vec![
            Document::new("xyz"), // low overlap with "abc"
            Document::new("abc"), // perfect overlap
            Document::new("abx"), // partial overlap
        ];
        let results = reranker.rerank("abc", &docs).await.unwrap();
        assert_eq!(results.len(), 3);
        assert_eq!(results[0].index, 1); // "abc" ranked first
    }

    #[tokio::test]
    async fn reranker_top_k() {
        let reranker = CrossEncoderReranker::new(FakeCrossEncoder::new()).with_top_k(2);
        let docs = vec![
            Document::new("xyz"),
            Document::new("abc"),
            Document::new("abx"),
        ];
        let results = reranker.rerank("abc", &docs).await.unwrap();
        assert_eq!(results.len(), 2);
    }

    #[tokio::test]
    async fn reranker_empty_documents() {
        let reranker = CrossEncoderReranker::new(FakeCrossEncoder::new());
        let results = reranker.rerank("query", &[]).await.unwrap();
        assert!(results.is_empty());
    }

    #[tokio::test]
    async fn reranker_preserves_document_id() {
        let reranker = CrossEncoderReranker::new(FakeCrossEncoder::new());
        let docs = vec![Document::new("abc").with_id("doc-1")];
        let results = reranker.rerank("abc", &docs).await.unwrap();
        assert_eq!(
            results[0].metadata.get("document_id"),
            Some(&Value::String("doc-1".into()))
        );
    }

    #[tokio::test]
    async fn reranker_preserves_document_metadata() {
        let reranker = CrossEncoderReranker::new(FakeCrossEncoder::new());
        let mut meta = HashMap::new();
        meta.insert("source".into(), Value::String("web".into()));
        let docs = vec![Document::new("abc").with_metadata(meta)];
        let results = reranker.rerank("abc", &docs).await.unwrap();
        assert_eq!(
            results[0].metadata.get("source"),
            Some(&Value::String("web".into()))
        );
    }

    #[tokio::test]
    async fn reranker_top_k_larger_than_docs() {
        let reranker = CrossEncoderReranker::new(FakeCrossEncoder::new()).with_top_k(10);
        let docs = vec![Document::new("abc")];
        let results = reranker.rerank("abc", &docs).await.unwrap();
        assert_eq!(results.len(), 1);
    }

    #[tokio::test]
    async fn reranker_scores_descending() {
        let reranker = CrossEncoderReranker::new(FakeCrossEncoder::new());
        let docs = vec![
            Document::new("z"),
            Document::new("ab"),
            Document::new("abc"),
        ];
        let results = reranker.rerank("abc", &docs).await.unwrap();
        for w in results.windows(2) {
            assert!(w[0].score >= w[1].score);
        }
    }

    // ---- CachedCrossEncoder ----

    #[tokio::test]
    async fn cached_returns_correct_scores() {
        let enc = CachedCrossEncoder::new(FakeCrossEncoder::new(), 10);
        let pairs = vec![("abc".into(), "abc".into())];
        let scores = enc.score_pairs(&pairs).await.unwrap();
        assert!((scores[0] - 1.0).abs() < f64::EPSILON);
    }

    #[tokio::test]
    async fn cached_populates_cache() {
        let enc = CachedCrossEncoder::new(FakeCrossEncoder::new(), 10);
        assert_eq!(enc.cache_len(), 0);
        let pairs = vec![("abc".into(), "abc".into())];
        enc.score_pairs(&pairs).await.unwrap();
        assert_eq!(enc.cache_len(), 1);
    }

    #[tokio::test]
    async fn cached_serves_from_cache() {
        let enc = CachedCrossEncoder::new(FakeCrossEncoder::new(), 10);
        let pairs = vec![("abc".into(), "abc".into())];
        let s1 = enc.score_pairs(&pairs).await.unwrap();
        let s2 = enc.score_pairs(&pairs).await.unwrap();
        assert_eq!(s1, s2);
        assert_eq!(enc.cache_len(), 1); // still 1, served from cache
    }

    #[tokio::test]
    async fn cached_mixed_hits_and_misses() {
        let enc = CachedCrossEncoder::new(FakeCrossEncoder::new(), 10);
        let pairs1 = vec![("abc".into(), "abc".into())];
        enc.score_pairs(&pairs1).await.unwrap();

        let pairs2 = vec![
            ("abc".into(), "abc".into()), // hit
            ("xyz".into(), "xyz".into()), // miss
        ];
        let scores = enc.score_pairs(&pairs2).await.unwrap();
        assert_eq!(scores.len(), 2);
        assert!((scores[0] - 1.0).abs() < f64::EPSILON);
        assert!((scores[1] - 1.0).abs() < f64::EPSILON);
        assert_eq!(enc.cache_len(), 2);
    }

    #[tokio::test]
    async fn cached_evicts_lru() {
        let enc = CachedCrossEncoder::new(FakeCrossEncoder::new(), 2);
        // Fill cache with 2 entries
        enc.score_pairs(&[("a".into(), "a".into())]).await.unwrap();
        enc.score_pairs(&[("b".into(), "b".into())]).await.unwrap();
        assert_eq!(enc.cache_len(), 2);

        // Adding a third should evict the oldest ("a","a")
        enc.score_pairs(&[("c".into(), "c".into())]).await.unwrap();
        assert_eq!(enc.cache_len(), 2);
    }

    #[tokio::test]
    async fn cached_empty_input() {
        let enc = CachedCrossEncoder::new(FakeCrossEncoder::new(), 10);
        let scores = enc.score_pairs(&[]).await.unwrap();
        assert!(scores.is_empty());
        assert_eq!(enc.cache_len(), 0);
    }

    // ---- NormalizedCrossEncoder ----

    #[tokio::test]
    async fn normalized_range() {
        let enc = NormalizedCrossEncoder::new(FakeCrossEncoder::new());
        let pairs = vec![
            ("abc".into(), "abc".into()), // highest
            ("abc".into(), "xyz".into()), // lowest
            ("abc".into(), "abx".into()), // middle
        ];
        let scores = enc.score_pairs(&pairs).await.unwrap();
        assert!((scores[0] - 1.0).abs() < f64::EPSILON); // max -> 1.0
        assert!((scores[1]).abs() < f64::EPSILON); // min -> 0.0
        assert!(scores[2] > 0.0 && scores[2] < 1.0); // between
    }

    #[tokio::test]
    async fn normalized_all_equal() {
        let enc = NormalizedCrossEncoder::new(FakeCrossEncoder::new());
        let pairs = vec![("abc".into(), "abc".into()), ("xyz".into(), "xyz".into())];
        // Both are 1.0, so range is 0 -> all become 0.5
        let scores = enc.score_pairs(&pairs).await.unwrap();
        assert!((scores[0] - 0.5).abs() < f64::EPSILON);
        assert!((scores[1] - 0.5).abs() < f64::EPSILON);
    }

    #[tokio::test]
    async fn normalized_single_pair() {
        let enc = NormalizedCrossEncoder::new(FakeCrossEncoder::new());
        let pairs = vec![("abc".into(), "abc".into())];
        let scores = enc.score_pairs(&pairs).await.unwrap();
        // single item -> all equal -> 0.5
        assert!((scores[0] - 0.5).abs() < f64::EPSILON);
    }

    #[tokio::test]
    async fn normalized_empty() {
        let enc = NormalizedCrossEncoder::new(FakeCrossEncoder::new());
        let scores = enc.score_pairs(&[]).await.unwrap();
        assert!(scores.is_empty());
    }

    #[tokio::test]
    async fn normalized_two_distinct_values() {
        let enc = NormalizedCrossEncoder::new(FakeCrossEncoder::new());
        // "abc" vs "abc" => 1.0, "abc" vs "xyz" => 0.0
        let pairs = vec![("abc".into(), "abc".into()), ("abc".into(), "xyz".into())];
        let scores = enc.score_pairs(&pairs).await.unwrap();
        assert!((scores[0] - 1.0).abs() < f64::EPSILON);
        assert!((scores[1]).abs() < f64::EPSILON);
    }

    // ---- Composition tests ----

    #[tokio::test]
    async fn normalized_threshold_composition() {
        let inner = ThresholdCrossEncoder::new(FakeCrossEncoder::new(), 0.5);
        let enc = NormalizedCrossEncoder::new(inner);
        let pairs = vec![
            ("abc".into(), "abc".into()), // 1.0 (above threshold)
            ("abc".into(), "xyz".into()), // 0.0 (below -> zeroed by threshold)
        ];
        let scores = enc.score_pairs(&pairs).await.unwrap();
        // After threshold: [1.0, 0.0]; after normalize: [1.0, 0.0]
        assert!((scores[0] - 1.0).abs() < f64::EPSILON);
        assert!((scores[1]).abs() < f64::EPSILON);
    }

    #[tokio::test]
    async fn batch_cached_composition() {
        let inner = CachedCrossEncoder::new(FakeCrossEncoder::new(), 10);
        let enc = BatchCrossEncoder::new(inner, 2);
        let pairs = vec![
            ("abc".into(), "abc".into()),
            ("abc".into(), "xyz".into()),
            ("abc".into(), "abx".into()),
        ];
        let scores = enc.score_pairs(&pairs).await.unwrap();
        assert_eq!(scores.len(), 3);
    }

    // ---- Trait object safety ----

    #[tokio::test]
    async fn trait_object_works() {
        let enc: Box<dyn CrossEncoder> = Box::new(FakeCrossEncoder::new());
        let pairs = vec![("abc".into(), "abc".into())];
        let scores = enc.score_pairs(&pairs).await.unwrap();
        assert!((scores[0] - 1.0).abs() < f64::EPSILON);
    }

    #[tokio::test]
    async fn arc_trait_object_works() {
        let enc: Arc<dyn CrossEncoder> = Arc::new(FakeCrossEncoder::new());
        let pairs = vec![("abc".into(), "abc".into())];
        let scores = enc.score_pairs(&pairs).await.unwrap();
        assert!((scores[0] - 1.0).abs() < f64::EPSILON);
    }

    // ---- LRU cache internals ----

    #[test]
    fn lru_cache_insert_and_get() {
        let mut cache = LruCache::new(3);
        cache.insert(("a".into(), "b".into()), 0.5);
        assert_eq!(cache.get(&("a".into(), "b".into())), Some(0.5));
    }

    #[test]
    fn lru_cache_miss() {
        let mut cache = LruCache::new(3);
        assert_eq!(cache.get(&("a".into(), "b".into())), None);
    }

    #[test]
    fn lru_cache_eviction_order() {
        let mut cache = LruCache::new(2);
        cache.insert(("a".into(), "1".into()), 1.0);
        cache.insert(("b".into(), "2".into()), 2.0);
        cache.insert(("c".into(), "3".into()), 3.0); // evicts ("a","1")
        assert_eq!(cache.get(&("a".into(), "1".into())), None);
        assert_eq!(cache.get(&("b".into(), "2".into())), Some(2.0));
        assert_eq!(cache.get(&("c".into(), "3".into())), Some(3.0));
    }

    #[test]
    fn lru_cache_access_refreshes() {
        let mut cache = LruCache::new(2);
        cache.insert(("a".into(), "1".into()), 1.0);
        cache.insert(("b".into(), "2".into()), 2.0);
        // Access "a" to make it recently used
        cache.get(&("a".into(), "1".into()));
        // Insert "c", which should evict "b" (now oldest)
        cache.insert(("c".into(), "3".into()), 3.0);
        assert_eq!(cache.get(&("a".into(), "1".into())), Some(1.0));
        assert_eq!(cache.get(&("b".into(), "2".into())), None);
    }

    #[test]
    fn lru_cache_update_existing() {
        let mut cache = LruCache::new(2);
        cache.insert(("a".into(), "1".into()), 1.0);
        cache.insert(("a".into(), "1".into()), 9.0);
        assert_eq!(cache.len(), 1);
        assert_eq!(cache.get(&("a".into(), "1".into())), Some(9.0));
    }

    // ---- Additional FakeCrossEncoder tests ----

    #[tokio::test]
    async fn fake_whitespace_overlap() {
        let enc = FakeCrossEncoder::new();
        let pairs = vec![("hello world".into(), "hello world".into())];
        let scores = enc.score_pairs(&pairs).await.unwrap();
        assert!((scores[0] - 1.0).abs() < f64::EPSILON);
    }

    #[tokio::test]
    async fn fake_scores_between_zero_and_one() {
        let enc = FakeCrossEncoder::new();
        let pairs = vec![
            ("rust lang".into(), "python lang".into()),
            ("quick brown fox".into(), "lazy dog jumps".into()),
        ];
        let scores = enc.score_pairs(&pairs).await.unwrap();
        for s in &scores {
            assert!(*s >= 0.0 && *s <= 1.0);
        }
    }

    // ---- Additional reranker tests ----

    #[tokio::test]
    async fn reranker_single_document() {
        let reranker = CrossEncoderReranker::new(FakeCrossEncoder::new());
        let docs = vec![Document::new("hello")];
        let results = reranker.rerank("hello", &docs).await.unwrap();
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].index, 0);
    }

    #[tokio::test]
    async fn reranker_top_k_zero() {
        let reranker = CrossEncoderReranker::new(FakeCrossEncoder::new()).with_top_k(0);
        let docs = vec![Document::new("abc")];
        let results = reranker.rerank("abc", &docs).await.unwrap();
        assert!(results.is_empty());
    }

    // ---- Additional threshold tests ----

    #[tokio::test]
    async fn threshold_empty_input() {
        let enc = ThresholdCrossEncoder::new(FakeCrossEncoder::new(), 0.5);
        let scores = enc.score_pairs(&[]).await.unwrap();
        assert!(scores.is_empty());
    }

    // ---- Additional cached tests ----

    #[tokio::test]
    async fn cached_multiple_pairs_at_once() {
        let enc = CachedCrossEncoder::new(FakeCrossEncoder::new(), 10);
        let pairs = vec![
            ("a".into(), "a".into()),
            ("b".into(), "b".into()),
            ("c".into(), "c".into()),
        ];
        enc.score_pairs(&pairs).await.unwrap();
        assert_eq!(enc.cache_len(), 3);
        // Second call should all be cache hits
        let scores = enc.score_pairs(&pairs).await.unwrap();
        assert_eq!(scores.len(), 3);
        assert_eq!(enc.cache_len(), 3);
    }

    // ---- Additional normalized tests ----

    #[tokio::test]
    async fn normalized_preserves_order() {
        let enc = NormalizedCrossEncoder::new(FakeCrossEncoder::new());
        let pairs = vec![
            ("abc".into(), "abc".into()),
            ("abc".into(), "abx".into()),
            ("abc".into(), "xyz".into()),
        ];
        let raw = FakeCrossEncoder::new().score_pairs(&pairs).await.unwrap();
        let norm = enc.score_pairs(&pairs).await.unwrap();
        // Order should be preserved
        assert!(raw[0] > raw[1] && norm[0] > norm[1]);
        assert!(raw[1] > raw[2] && norm[1] > norm[2]);
    }
}