oxirs-vec 0.2.4

Vector index abstractions for semantic similarity and AI-augmented querying
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
//! # Adaptive Recall Tuner for Vector Search
//!
//! Dynamically adjusts vector search parameters (ef_search, num_candidates,
//! re-ranking depth, over-fetch ratio) to achieve target recall@k while
//! minimizing latency.
//!
//! ## Problem
//!
//! Fixed search parameters lead to either:
//! - Under-fetching: Missing relevant results (low recall)
//! - Over-fetching: Wasting compute on unnecessary candidates (high latency)
//!
//! ## Solution
//!
//! The adaptive tuner observes query feedback (user clicks, explicit relevance
//! judgments, or ground-truth evaluations) and uses a PID-like control loop
//! to adjust parameters in real-time.
//!
//! ## Architecture
//!
//! ```text
//! Query --> SearchEngine --> Results --> FeedbackCollector
//!   ^                                         |
//!   |                                         v
//!   +--- ParameterAdjuster <--- RecallEstimator
//! ```

use serde::{Deserialize, Serialize};
use std::collections::VecDeque;
use std::fmt;
use std::time::{Duration, Instant};

// ---------------------------------------------------------------------------
// Search parameters
// ---------------------------------------------------------------------------

/// Tunable search parameters for vector index queries.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct SearchParams {
    /// HNSW ef_search: number of candidates to explore during search.
    pub ef_search: usize,
    /// Number of candidates to pre-fetch before re-ranking.
    pub num_candidates: usize,
    /// Over-fetch ratio: fetch this many times more candidates than k.
    pub over_fetch_ratio: f64,
    /// Re-ranking depth: how many candidates to re-rank with exact distances.
    pub rerank_depth: usize,
    /// Whether to enable approximate early termination.
    pub early_termination: bool,
}

impl Default for SearchParams {
    fn default() -> Self {
        Self {
            ef_search: 64,
            num_candidates: 100,
            over_fetch_ratio: 2.0,
            rerank_depth: 50,
            early_termination: true,
        }
    }
}

impl SearchParams {
    /// Create parameters optimized for high recall.
    pub fn high_recall() -> Self {
        Self {
            ef_search: 256,
            num_candidates: 500,
            over_fetch_ratio: 5.0,
            rerank_depth: 200,
            early_termination: false,
        }
    }

    /// Create parameters optimized for low latency.
    pub fn low_latency() -> Self {
        Self {
            ef_search: 32,
            num_candidates: 50,
            over_fetch_ratio: 1.5,
            rerank_depth: 20,
            early_termination: true,
        }
    }

    /// Clamp all parameters to valid ranges.
    pub fn clamp(&mut self) {
        self.ef_search = self.ef_search.clamp(8, 1024);
        self.num_candidates = self.num_candidates.clamp(10, 5000);
        self.over_fetch_ratio = self.over_fetch_ratio.clamp(1.0, 20.0);
        self.rerank_depth = self.rerank_depth.clamp(0, self.num_candidates);
    }
}

// ---------------------------------------------------------------------------
// Feedback
// ---------------------------------------------------------------------------

/// Feedback from a single query execution.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QueryFeedback {
    /// The parameters used for this query.
    pub params: SearchParams,
    /// Number of results requested (k).
    pub k: usize,
    /// Number of truly relevant results in the top-k (from ground truth or clicks).
    pub relevant_in_top_k: usize,
    /// Total number of known relevant results (if available).
    pub total_relevant: Option<usize>,
    /// Query latency.
    pub latency: Duration,
    /// Timestamp of the feedback.
    #[serde(skip, default = "std::time::Instant::now")]
    pub timestamp: Instant,
}

impl QueryFeedback {
    /// Compute recall@k for this feedback.
    pub fn recall_at_k(&self) -> f64 {
        match self.total_relevant {
            Some(total) if total > 0 => self.relevant_in_top_k as f64 / total as f64,
            _ => {
                // If total_relevant is unknown, estimate from k
                if self.k == 0 {
                    return 0.0;
                }
                self.relevant_in_top_k as f64 / self.k as f64
            }
        }
    }

    /// Compute precision@k.
    pub fn precision_at_k(&self) -> f64 {
        if self.k == 0 {
            return 0.0;
        }
        self.relevant_in_top_k as f64 / self.k as f64
    }
}

// ---------------------------------------------------------------------------
// Tuner configuration
// ---------------------------------------------------------------------------

/// Configuration for the adaptive recall tuner.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TunerConfig {
    /// Target recall@k (e.g., 0.95 for 95%).
    pub target_recall: f64,
    /// Maximum acceptable latency.
    pub max_latency: Duration,
    /// Number of recent feedback samples to consider.
    pub window_size: usize,
    /// Proportional gain for the control loop.
    pub kp: f64,
    /// Integral gain for the control loop.
    pub ki: f64,
    /// Derivative gain for the control loop.
    pub kd: f64,
    /// Minimum number of samples before adjusting.
    pub min_samples: usize,
    /// How often to recalculate (in number of queries).
    pub adjust_interval: usize,
}

impl Default for TunerConfig {
    fn default() -> Self {
        Self {
            target_recall: 0.95,
            max_latency: Duration::from_millis(100),
            window_size: 100,
            kp: 0.5,
            ki: 0.1,
            kd: 0.05,
            min_samples: 10,
            adjust_interval: 5,
        }
    }
}

// ---------------------------------------------------------------------------
// Tuner statistics
// ---------------------------------------------------------------------------

/// Statistics from the adaptive tuner.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct TunerStats {
    /// Total number of feedback samples received.
    pub total_feedbacks: u64,
    /// Number of parameter adjustments made.
    pub adjustments_made: u64,
    /// Current estimated recall.
    pub current_recall: f64,
    /// Current average latency in milliseconds.
    pub current_avg_latency_ms: f64,
    /// Whether the tuner is currently meeting its target.
    pub target_met: bool,
    /// Running average precision.
    pub avg_precision: f64,
    /// Historical recall values (last N).
    pub recall_history: Vec<f64>,
}

impl TunerStats {
    /// Check if recall is within tolerance of target.
    pub fn is_near_target(&self, target: f64, tolerance: f64) -> bool {
        (self.current_recall - target).abs() < tolerance
    }
}

// ---------------------------------------------------------------------------
// Adaptive Recall Tuner
// ---------------------------------------------------------------------------

/// The main adaptive recall tuner.
///
/// Collects query feedback and adjusts search parameters to converge on
/// the target recall while respecting latency constraints.
pub struct AdaptiveRecallTuner {
    config: TunerConfig,
    current_params: SearchParams,
    feedback_window: VecDeque<QueryFeedback>,
    stats: TunerStats,
    /// PID controller state
    integral_error: f64,
    prev_error: f64,
    query_count: u64,
}

impl AdaptiveRecallTuner {
    /// Create a new tuner with default parameters and configuration.
    pub fn new(config: TunerConfig) -> Self {
        Self {
            config,
            current_params: SearchParams::default(),
            feedback_window: VecDeque::new(),
            stats: TunerStats::default(),
            integral_error: 0.0,
            prev_error: 0.0,
            query_count: 0,
        }
    }

    /// Create with specific initial parameters.
    pub fn with_initial_params(config: TunerConfig, initial: SearchParams) -> Self {
        Self {
            config,
            current_params: initial,
            feedback_window: VecDeque::new(),
            stats: TunerStats::default(),
            integral_error: 0.0,
            prev_error: 0.0,
            query_count: 0,
        }
    }

    /// Get the current recommended search parameters.
    pub fn current_params(&self) -> &SearchParams {
        &self.current_params
    }

    /// Get tuner statistics.
    pub fn stats(&self) -> &TunerStats {
        &self.stats
    }

    /// Record a query feedback observation.
    ///
    /// Returns `true` if parameters were adjusted as a result.
    pub fn record_feedback(&mut self, feedback: QueryFeedback) -> bool {
        // Add to window
        self.feedback_window.push_back(feedback);
        while self.feedback_window.len() > self.config.window_size {
            self.feedback_window.pop_front();
        }

        self.stats.total_feedbacks += 1;
        self.query_count += 1;

        // Update running statistics
        self.update_stats();

        // Check if we should adjust
        if self.feedback_window.len() >= self.config.min_samples
            && self.query_count % self.config.adjust_interval as u64 == 0
        {
            self.adjust_parameters();
            return true;
        }

        false
    }

    /// Force a parameter adjustment regardless of interval.
    pub fn force_adjust(&mut self) {
        if self.feedback_window.len() >= self.config.min_samples {
            self.adjust_parameters();
        }
    }

    /// Reset the tuner state (keeps configuration).
    pub fn reset(&mut self) {
        self.current_params = SearchParams::default();
        self.feedback_window.clear();
        self.stats = TunerStats::default();
        self.integral_error = 0.0;
        self.prev_error = 0.0;
        self.query_count = 0;
    }

    // ── Internal methods ──────────────────────────────────────────────────

    fn update_stats(&mut self) {
        if self.feedback_window.is_empty() {
            return;
        }

        let recalls: Vec<f64> = self
            .feedback_window
            .iter()
            .map(|f| f.recall_at_k())
            .collect();
        let precisions: Vec<f64> = self
            .feedback_window
            .iter()
            .map(|f| f.precision_at_k())
            .collect();
        let latencies: Vec<f64> = self
            .feedback_window
            .iter()
            .map(|f| f.latency.as_millis() as f64)
            .collect();

        let n = recalls.len() as f64;
        self.stats.current_recall = recalls.iter().sum::<f64>() / n;
        self.stats.avg_precision = precisions.iter().sum::<f64>() / n;
        self.stats.current_avg_latency_ms = latencies.iter().sum::<f64>() / n;
        self.stats.target_met = self.stats.current_recall >= self.config.target_recall;

        // Record recall history (keep last 50)
        self.stats.recall_history.push(self.stats.current_recall);
        if self.stats.recall_history.len() > 50 {
            self.stats.recall_history.remove(0);
        }
    }

    fn adjust_parameters(&mut self) {
        let error = self.config.target_recall - self.stats.current_recall;

        // PID control
        self.integral_error += error;
        // Clamp integral to prevent windup
        self.integral_error = self.integral_error.clamp(-10.0, 10.0);

        let derivative = error - self.prev_error;
        self.prev_error = error;

        let adjustment = self.config.kp * error
            + self.config.ki * self.integral_error
            + self.config.kd * derivative;

        // Apply adjustment to parameters
        // Positive adjustment means recall is too low -> increase search effort
        // Negative adjustment means recall is high enough -> can reduce effort

        let scale = 1.0 + adjustment;

        self.current_params.ef_search =
            ((self.current_params.ef_search as f64 * scale) as usize).max(8);
        self.current_params.num_candidates =
            ((self.current_params.num_candidates as f64 * scale) as usize).max(10);
        self.current_params.over_fetch_ratio =
            (self.current_params.over_fetch_ratio * scale).max(1.0);
        self.current_params.rerank_depth =
            ((self.current_params.rerank_depth as f64 * scale) as usize).max(1);

        // If latency is too high, pull back
        if self.stats.current_avg_latency_ms > self.config.max_latency.as_millis() as f64 {
            let latency_ratio =
                self.config.max_latency.as_millis() as f64 / self.stats.current_avg_latency_ms;
            self.current_params.ef_search =
                ((self.current_params.ef_search as f64 * latency_ratio) as usize).max(8);
            self.current_params.num_candidates =
                ((self.current_params.num_candidates as f64 * latency_ratio) as usize).max(10);
        }

        self.current_params.clamp();
        self.stats.adjustments_made += 1;
    }
}

impl fmt::Debug for AdaptiveRecallTuner {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("AdaptiveRecallTuner")
            .field("config", &self.config)
            .field("current_params", &self.current_params)
            .field("stats", &self.stats)
            .finish()
    }
}

// ---------------------------------------------------------------------------
// Recall evaluator (ground-truth based)
// ---------------------------------------------------------------------------

/// Evaluates recall by comparing search results against ground-truth.
pub struct RecallEvaluator;

/// A single recall evaluation result.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RecallEvaluation {
    /// Recall at the specified k.
    pub recall_at_k: f64,
    /// Precision at the specified k.
    pub precision_at_k: f64,
    /// F1 score.
    pub f1_score: f64,
    /// Average precision (AP).
    pub average_precision: f64,
    /// Normalized discounted cumulative gain (nDCG).
    pub ndcg: f64,
    /// Number of queries evaluated.
    pub num_queries: usize,
}

impl RecallEvaluator {
    /// Evaluate recall for a set of query results against ground truth.
    ///
    /// - `results`: For each query, the IDs returned by the search engine.
    /// - `ground_truth`: For each query, the set of truly relevant IDs.
    /// - `k`: The cutoff for evaluation.
    pub fn evaluate(
        results: &[Vec<String>],
        ground_truth: &[Vec<String>],
        k: usize,
    ) -> RecallEvaluation {
        if results.is_empty() || ground_truth.is_empty() {
            return RecallEvaluation {
                recall_at_k: 0.0,
                precision_at_k: 0.0,
                f1_score: 0.0,
                average_precision: 0.0,
                ndcg: 0.0,
                num_queries: 0,
            };
        }

        let n = results.len().min(ground_truth.len());
        let mut total_recall = 0.0;
        let mut total_precision = 0.0;
        let mut total_ap = 0.0;
        let mut total_ndcg = 0.0;

        for i in 0..n {
            let result_k: Vec<_> = results[i].iter().take(k).cloned().collect();
            let truth: std::collections::HashSet<_> = ground_truth[i].iter().cloned().collect();

            if truth.is_empty() {
                continue;
            }

            // Recall@k
            let relevant_found = result_k.iter().filter(|r| truth.contains(*r)).count();
            let recall = relevant_found as f64 / truth.len() as f64;
            total_recall += recall;

            // Precision@k
            let precision = if result_k.is_empty() {
                0.0
            } else {
                relevant_found as f64 / result_k.len() as f64
            };
            total_precision += precision;

            // Average Precision (AP)
            let mut running_relevant = 0.0;
            let mut ap_sum = 0.0;
            for (pos, item) in result_k.iter().enumerate() {
                if truth.contains(item) {
                    running_relevant += 1.0;
                    ap_sum += running_relevant / (pos + 1) as f64;
                }
            }
            total_ap += if truth.is_empty() {
                0.0
            } else {
                ap_sum / truth.len() as f64
            };

            // nDCG
            let dcg: f64 = result_k
                .iter()
                .enumerate()
                .map(|(pos, item)| {
                    let rel = if truth.contains(item) { 1.0 } else { 0.0 };
                    rel / ((pos + 2) as f64).ln()
                })
                .sum();
            let ideal_k = truth.len().min(k);
            let ideal_dcg: f64 = (0..ideal_k).map(|pos| 1.0 / ((pos + 2) as f64).ln()).sum();
            let ndcg = if ideal_dcg > 0.0 {
                dcg / ideal_dcg
            } else {
                0.0
            };
            total_ndcg += ndcg;
        }

        let n_f = n as f64;
        let avg_recall = total_recall / n_f;
        let avg_precision = total_precision / n_f;
        let f1 = if (avg_recall + avg_precision) > 0.0 {
            2.0 * avg_recall * avg_precision / (avg_recall + avg_precision)
        } else {
            0.0
        };

        RecallEvaluation {
            recall_at_k: avg_recall,
            precision_at_k: avg_precision,
            f1_score: f1,
            average_precision: total_ap / n_f,
            ndcg: total_ndcg / n_f,
            num_queries: n,
        }
    }
}

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

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

    // ── SearchParams tests ────────────────────────────────────────────────

    #[test]
    fn test_search_params_default() {
        let p = SearchParams::default();
        assert_eq!(p.ef_search, 64);
        assert_eq!(p.num_candidates, 100);
        assert!((p.over_fetch_ratio - 2.0).abs() < 0.01);
        assert_eq!(p.rerank_depth, 50);
        assert!(p.early_termination);
    }

    #[test]
    fn test_search_params_high_recall() {
        let p = SearchParams::high_recall();
        assert!(p.ef_search >= 256);
        assert!(!p.early_termination);
    }

    #[test]
    fn test_search_params_low_latency() {
        let p = SearchParams::low_latency();
        assert!(p.ef_search <= 32);
        assert!(p.early_termination);
    }

    #[test]
    fn test_search_params_clamp() {
        let mut p = SearchParams {
            ef_search: 0,
            num_candidates: 0,
            over_fetch_ratio: 0.1,
            rerank_depth: 99999,
            early_termination: false,
        };
        p.clamp();
        assert_eq!(p.ef_search, 8);
        assert_eq!(p.num_candidates, 10);
        assert!((p.over_fetch_ratio - 1.0).abs() < 0.01);
        assert_eq!(p.rerank_depth, 10); // clamped to num_candidates
    }

    // ── QueryFeedback tests ───────────────────────────────────────────────

    #[test]
    fn test_feedback_recall_with_ground_truth() {
        let fb = QueryFeedback {
            params: SearchParams::default(),
            k: 10,
            relevant_in_top_k: 8,
            total_relevant: Some(10),
            latency: Duration::from_millis(50),
            timestamp: Instant::now(),
        };
        assert!((fb.recall_at_k() - 0.8).abs() < 0.01);
    }

    #[test]
    fn test_feedback_recall_without_ground_truth() {
        let fb = QueryFeedback {
            params: SearchParams::default(),
            k: 10,
            relevant_in_top_k: 7,
            total_relevant: None,
            latency: Duration::from_millis(50),
            timestamp: Instant::now(),
        };
        assert!((fb.recall_at_k() - 0.7).abs() < 0.01);
    }

    #[test]
    fn test_feedback_precision() {
        let fb = QueryFeedback {
            params: SearchParams::default(),
            k: 10,
            relevant_in_top_k: 5,
            total_relevant: Some(20),
            latency: Duration::from_millis(50),
            timestamp: Instant::now(),
        };
        assert!((fb.precision_at_k() - 0.5).abs() < 0.01);
    }

    #[test]
    fn test_feedback_k_zero() {
        let fb = QueryFeedback {
            params: SearchParams::default(),
            k: 0,
            relevant_in_top_k: 0,
            total_relevant: None,
            latency: Duration::from_millis(1),
            timestamp: Instant::now(),
        };
        assert_eq!(fb.recall_at_k(), 0.0);
        assert_eq!(fb.precision_at_k(), 0.0);
    }

    // ── TunerConfig tests ─────────────────────────────────────────────────

    #[test]
    fn test_tuner_config_default() {
        let c = TunerConfig::default();
        assert!((c.target_recall - 0.95).abs() < 0.01);
        assert_eq!(c.window_size, 100);
        assert_eq!(c.min_samples, 10);
    }

    // ── Tuner core tests ──────────────────────────────────────────────────

    fn make_feedback(recall_ratio: f64, k: usize, latency_ms: u64) -> QueryFeedback {
        let relevant = (k as f64 * recall_ratio) as usize;
        QueryFeedback {
            params: SearchParams::default(),
            k,
            relevant_in_top_k: relevant,
            total_relevant: Some(k),
            latency: Duration::from_millis(latency_ms),
            timestamp: Instant::now(),
        }
    }

    #[test]
    fn test_tuner_initial_params() {
        let tuner = AdaptiveRecallTuner::new(TunerConfig::default());
        assert_eq!(tuner.current_params().ef_search, 64);
    }

    #[test]
    fn test_tuner_with_initial_params() {
        let initial = SearchParams::high_recall();
        let tuner =
            AdaptiveRecallTuner::with_initial_params(TunerConfig::default(), initial.clone());
        assert_eq!(tuner.current_params().ef_search, initial.ef_search);
    }

    #[test]
    fn test_tuner_no_adjust_before_min_samples() {
        let config = TunerConfig {
            min_samples: 10,
            adjust_interval: 1,
            ..Default::default()
        };
        let mut tuner = AdaptiveRecallTuner::new(config);

        for _ in 0..5 {
            let adjusted = tuner.record_feedback(make_feedback(0.5, 10, 50));
            assert!(!adjusted);
        }
        assert_eq!(tuner.stats().adjustments_made, 0);
    }

    #[test]
    fn test_tuner_adjusts_after_min_samples() {
        let config = TunerConfig {
            min_samples: 5,
            adjust_interval: 5,
            ..Default::default()
        };
        let mut tuner = AdaptiveRecallTuner::new(config);

        for i in 0..10 {
            tuner.record_feedback(make_feedback(0.5, 10, 50));
            if i >= 4 && (i + 1) % 5 == 0 {
                // Should have adjusted
            }
        }
        assert!(tuner.stats().adjustments_made > 0);
    }

    #[test]
    fn test_tuner_increases_params_for_low_recall() {
        let config = TunerConfig {
            min_samples: 5,
            adjust_interval: 1,
            target_recall: 0.95,
            kp: 0.5,
            ki: 0.0,
            kd: 0.0,
            ..Default::default()
        };
        let mut tuner = AdaptiveRecallTuner::new(config);
        let initial_ef = tuner.current_params().ef_search;

        // Feed low recall data
        for _ in 0..10 {
            tuner.record_feedback(make_feedback(0.3, 10, 30));
        }

        // Parameters should have increased
        assert!(tuner.current_params().ef_search > initial_ef);
    }

    #[test]
    fn test_tuner_decreases_params_for_high_recall() {
        let config = TunerConfig {
            min_samples: 5,
            adjust_interval: 1,
            target_recall: 0.5,
            kp: 0.5,
            ki: 0.0,
            kd: 0.0,
            ..Default::default()
        };
        let initial = SearchParams::high_recall();
        let mut tuner = AdaptiveRecallTuner::with_initial_params(config, initial.clone());

        // Feed high recall data (above target)
        for _ in 0..10 {
            tuner.record_feedback(make_feedback(0.99, 10, 30));
        }

        // Parameters should have decreased (or stayed) since recall exceeds target
        assert!(tuner.current_params().ef_search <= initial.ef_search);
    }

    #[test]
    fn test_tuner_respects_latency_constraint() {
        let config = TunerConfig {
            min_samples: 5,
            adjust_interval: 1,
            max_latency: Duration::from_millis(50),
            target_recall: 0.99,
            kp: 1.0,
            ki: 0.0,
            kd: 0.0,
            ..Default::default()
        };
        let mut tuner = AdaptiveRecallTuner::new(config);

        // Feed data with high latency -> tuner should pull back despite low recall
        for _ in 0..20 {
            tuner.record_feedback(make_feedback(0.3, 10, 200));
        }

        // ef_search should not grow unboundedly
        assert!(tuner.current_params().ef_search < 1024);
    }

    #[test]
    fn test_tuner_stats_tracking() {
        let config = TunerConfig {
            min_samples: 3,
            adjust_interval: 1,
            ..Default::default()
        };
        let mut tuner = AdaptiveRecallTuner::new(config);

        for _ in 0..5 {
            tuner.record_feedback(make_feedback(0.8, 10, 40));
        }

        assert_eq!(tuner.stats().total_feedbacks, 5);
        assert!(tuner.stats().current_recall > 0.0);
        assert!(tuner.stats().current_avg_latency_ms > 0.0);
    }

    #[test]
    fn test_tuner_reset() {
        let config = TunerConfig {
            min_samples: 3,
            adjust_interval: 1,
            ..Default::default()
        };
        let mut tuner = AdaptiveRecallTuner::new(config);

        for _ in 0..5 {
            tuner.record_feedback(make_feedback(0.8, 10, 40));
        }
        tuner.reset();

        assert_eq!(tuner.stats().total_feedbacks, 0);
        assert_eq!(tuner.stats().adjustments_made, 0);
        assert_eq!(tuner.current_params().ef_search, 64);
    }

    #[test]
    fn test_tuner_force_adjust() {
        let config = TunerConfig {
            min_samples: 3,
            adjust_interval: 100, // Very high interval
            ..Default::default()
        };
        let mut tuner = AdaptiveRecallTuner::new(config);

        for _ in 0..5 {
            tuner.record_feedback(make_feedback(0.5, 10, 40));
        }
        assert_eq!(tuner.stats().adjustments_made, 0);

        tuner.force_adjust();
        assert_eq!(tuner.stats().adjustments_made, 1);
    }

    #[test]
    fn test_stats_near_target() {
        let stats = TunerStats {
            current_recall: 0.94,
            ..Default::default()
        };
        assert!(stats.is_near_target(0.95, 0.02));
        assert!(!stats.is_near_target(0.95, 0.005));
    }

    #[test]
    fn test_recall_history() {
        let config = TunerConfig {
            min_samples: 3,
            adjust_interval: 1,
            ..Default::default()
        };
        let mut tuner = AdaptiveRecallTuner::new(config);

        for _ in 0..5 {
            tuner.record_feedback(make_feedback(0.8, 10, 40));
        }

        assert!(!tuner.stats().recall_history.is_empty());
    }

    // ── RecallEvaluator tests ─────────────────────────────────────────────

    #[test]
    fn test_evaluator_perfect_recall() {
        let results = vec![vec!["a".to_string(), "b".to_string(), "c".to_string()]];
        let truth = vec![vec!["a".to_string(), "b".to_string(), "c".to_string()]];
        let eval = RecallEvaluator::evaluate(&results, &truth, 3);
        assert!((eval.recall_at_k - 1.0).abs() < 0.01);
        assert!((eval.precision_at_k - 1.0).abs() < 0.01);
    }

    #[test]
    fn test_evaluator_partial_recall() {
        let results = vec![vec!["a".to_string(), "b".to_string(), "x".to_string()]];
        let truth = vec![vec![
            "a".to_string(),
            "b".to_string(),
            "c".to_string(),
            "d".to_string(),
        ]];
        let eval = RecallEvaluator::evaluate(&results, &truth, 3);
        assert!((eval.recall_at_k - 0.5).abs() < 0.01); // 2/4
        assert!((eval.precision_at_k - 2.0 / 3.0).abs() < 0.01); // 2/3
    }

    #[test]
    fn test_evaluator_zero_recall() {
        let results = vec![vec!["x".to_string(), "y".to_string(), "z".to_string()]];
        let truth = vec![vec!["a".to_string(), "b".to_string(), "c".to_string()]];
        let eval = RecallEvaluator::evaluate(&results, &truth, 3);
        assert_eq!(eval.recall_at_k, 0.0);
        assert_eq!(eval.precision_at_k, 0.0);
    }

    #[test]
    fn test_evaluator_empty() {
        let eval = RecallEvaluator::evaluate(&[], &[], 10);
        assert_eq!(eval.num_queries, 0);
        assert_eq!(eval.recall_at_k, 0.0);
    }

    #[test]
    fn test_evaluator_multiple_queries() {
        let results = vec![
            vec!["a".to_string(), "b".to_string()],
            vec!["c".to_string(), "d".to_string()],
        ];
        let truth = vec![
            vec!["a".to_string(), "b".to_string()],
            vec!["c".to_string(), "x".to_string()],
        ];
        let eval = RecallEvaluator::evaluate(&results, &truth, 2);
        assert_eq!(eval.num_queries, 2);
        // Query 1: recall=1.0, Query 2: recall=0.5 -> avg=0.75
        assert!((eval.recall_at_k - 0.75).abs() < 0.01);
    }

    #[test]
    fn test_evaluator_k_less_than_results() {
        let results = vec![vec![
            "a".to_string(),
            "b".to_string(),
            "c".to_string(),
            "d".to_string(),
        ]];
        let truth = vec![vec!["a".to_string(), "b".to_string()]];
        let eval = RecallEvaluator::evaluate(&results, &truth, 2);
        // Only top-2 considered: a, b — both relevant
        assert!((eval.recall_at_k - 1.0).abs() < 0.01);
    }

    #[test]
    fn test_evaluator_ndcg() {
        let results = vec![vec!["a".to_string(), "b".to_string(), "c".to_string()]];
        let truth = vec![vec!["a".to_string(), "b".to_string(), "c".to_string()]];
        let eval = RecallEvaluator::evaluate(&results, &truth, 3);
        // Perfect ranking -> nDCG should be 1.0
        assert!((eval.ndcg - 1.0).abs() < 0.01);
    }

    #[test]
    fn test_evaluator_f1_score() {
        let results = vec![vec!["a".to_string(), "b".to_string(), "c".to_string()]];
        let truth = vec![vec!["a".to_string(), "b".to_string(), "c".to_string()]];
        let eval = RecallEvaluator::evaluate(&results, &truth, 3);
        // P=1.0, R=1.0 -> F1=1.0
        assert!((eval.f1_score - 1.0).abs() < 0.01);
    }

    #[test]
    fn test_evaluator_average_precision() {
        let results = vec![vec!["a".to_string(), "x".to_string(), "b".to_string()]];
        let truth = vec![vec!["a".to_string(), "b".to_string()]];
        let eval = RecallEvaluator::evaluate(&results, &truth, 3);
        // AP: (1/1 + 2/3) / 2 = (1 + 0.667) / 2 = 0.833
        assert!(eval.average_precision > 0.0);
    }

    // ── Integration test ──────────────────────────────────────────────────

    #[test]
    fn test_tuner_convergence_simulation() {
        let config = TunerConfig {
            min_samples: 5,
            adjust_interval: 1,
            target_recall: 0.9,
            kp: 0.3,
            ki: 0.05,
            kd: 0.02,
            ..Default::default()
        };
        let mut tuner = AdaptiveRecallTuner::new(config);

        // Simulate recall improving as parameters are tuned
        for i in 0..50 {
            let recall = 0.5 + (i as f64 * 0.01).min(0.45);
            tuner.record_feedback(make_feedback(recall, 10, 30));
        }

        // After 50 iterations, should have made adjustments
        assert!(tuner.stats().adjustments_made > 0);
        assert!(tuner.stats().total_feedbacks == 50);
    }

    #[test]
    fn test_integral_windup_prevention() {
        let config = TunerConfig {
            min_samples: 3,
            adjust_interval: 1,
            target_recall: 0.99,
            kp: 0.1,
            ki: 1.0, // Very high integral gain
            kd: 0.0,
            ..Default::default()
        };
        let mut tuner = AdaptiveRecallTuner::new(config);

        // Feed consistently low recall
        for _ in 0..100 {
            tuner.record_feedback(make_feedback(0.1, 10, 20));
        }

        // Despite high integral error, parameters should be clamped
        assert!(tuner.current_params().ef_search <= 1024);
        assert!(tuner.current_params().num_candidates <= 5000);
    }
}