coding-agent-search 0.5.0

Unified TUI search over local coding agent histories
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
use serde::{Deserialize, Serialize};
use std::cmp::Ordering;
use std::time::{Duration, Instant};

/// Hard eligibility cutoff: models must be released on/after this date.
/// Format: YYYY-MM-DD
pub const ELIGIBILITY_CUTOFF: &str = "2025-11-01";

/// Success criteria from the epic.
pub mod criteria {
    /// Cold start must be under 2 seconds.
    pub const COLD_START_MAX_MS: u64 = 2000;
    /// Warm p99 latency must be under 250ms.
    pub const WARM_P99_MAX_MS: u64 = 250;
    /// Memory usage must be under 300MB per model.
    pub const MEMORY_MAX_MB: u64 = 300;
    /// Quality must be at least 80% of baseline (MiniLM).
    pub const QUALITY_MIN_RATIO: f64 = 0.80;
}

/// Model metadata for eligibility checking.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ModelMetadata {
    /// Model identifier (e.g., "bge-small-en-v1.5").
    pub id: String,
    /// Human-readable name.
    pub name: String,
    /// HuggingFace model ID or source.
    pub source: String,
    /// Release/update date (YYYY-MM-DD format).
    pub release_date: String,
    /// Embedding dimension (for embedders).
    pub dimension: Option<usize>,
    /// Model size in bytes.
    pub size_bytes: Option<u64>,
    /// Whether this is a baseline model (not eligible to win, but used for comparison).
    pub is_baseline: bool,
}

impl ModelMetadata {
    /// Check if the model is eligible based on release date.
    pub fn is_eligible(&self) -> bool {
        if self.is_baseline {
            return false;
        }
        self.release_date.as_str() >= ELIGIBILITY_CUTOFF
    }
}

/// Minimal validation report for bake-off runs.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ValidationReport {
    pub model_id: String,
    pub corpus_hash: String,
    pub ndcg_at_10: f64,
    pub latency_ms_p50: u64,
    pub latency_ms_p95: u64,
    pub latency_ms_p99: u64,
    pub cold_start_ms: u64,
    pub memory_mb: u64,
    pub eligible: bool,
    pub meets_criteria: bool,
    pub warnings: Vec<String>,
}

impl ValidationReport {
    /// Check if this report meets all success criteria.
    pub fn check_criteria(&self) -> bool {
        self.cold_start_ms <= criteria::COLD_START_MAX_MS
            && self.latency_ms_p99 <= criteria::WARM_P99_MAX_MS
            && self.memory_mb <= criteria::MEMORY_MAX_MB
    }

    /// Check quality against a baseline report.
    pub fn meets_quality_threshold(&self, baseline: &ValidationReport) -> bool {
        if baseline.ndcg_at_10 == 0.0 {
            return true;
        }
        self.ndcg_at_10 / baseline.ndcg_at_10 >= criteria::QUALITY_MIN_RATIO
    }
}

/// Latency statistics from a benchmark run.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct LatencyStats {
    pub samples: usize,
    pub min_ms: u64,
    pub max_ms: u64,
    pub mean_ms: f64,
    pub p50_ms: u64,
    pub p95_ms: u64,
    pub p99_ms: u64,
}

impl LatencyStats {
    /// Compute latency statistics from a list of durations.
    pub fn from_durations(durations: &[Duration]) -> Self {
        if durations.is_empty() {
            return Self {
                samples: 0,
                min_ms: 0,
                max_ms: 0,
                mean_ms: 0.0,
                p50_ms: 0,
                p95_ms: 0,
                p99_ms: 0,
            };
        }

        let mut millis: Vec<u64> = durations.iter().map(|d| d.as_millis() as u64).collect();
        millis.sort_unstable();

        let n = millis.len();
        let sum: u64 = millis.iter().sum();

        Self {
            samples: n,
            min_ms: millis[0],
            max_ms: millis[n - 1],
            mean_ms: sum as f64 / n as f64,
            p50_ms: percentile(&millis, 50),
            p95_ms: percentile(&millis, 95),
            p99_ms: percentile(&millis, 99),
        }
    }
}

/// Compute percentile from sorted values.
fn percentile(sorted: &[u64], p: usize) -> u64 {
    if sorted.is_empty() {
        return 0;
    }
    let idx = (p * sorted.len() / 100).min(sorted.len() - 1);
    sorted[idx]
}

/// Timer for measuring operation latency.
pub struct LatencyTimer {
    samples: Vec<Duration>,
}

impl LatencyTimer {
    pub fn new() -> Self {
        Self {
            samples: Vec::new(),
        }
    }

    /// Time a single operation and record the duration.
    pub fn time<F, T>(&mut self, f: F) -> T
    where
        F: FnOnce() -> T,
    {
        let start = Instant::now();
        let result = f();
        self.samples.push(start.elapsed());
        result
    }

    /// Get statistics from recorded samples.
    pub fn stats(&self) -> LatencyStats {
        LatencyStats::from_durations(&self.samples)
    }

    /// Clear recorded samples.
    pub fn clear(&mut self) {
        self.samples.clear();
    }
}

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

/// Bake-off comparison result.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BakeoffComparison {
    /// Corpus hash for reproducibility.
    pub corpus_hash: String,
    /// Baseline model report.
    pub baseline: ValidationReport,
    /// All candidate reports.
    pub candidates: Vec<ValidationReport>,
    /// Recommended model ID (best eligible candidate meeting criteria).
    pub recommendation: Option<String>,
    /// Reason for recommendation.
    pub recommendation_reason: String,
}

impl BakeoffComparison {
    /// Find the best eligible candidate that meets all criteria.
    pub fn find_winner(&self) -> Option<&ValidationReport> {
        self.candidates
            .iter()
            .filter(|r| r.eligible && r.meets_criteria && r.meets_quality_threshold(&self.baseline))
            .max_by(|a, b| {
                // Prefer higher quality, then lower latency
                a.ndcg_at_10
                    .partial_cmp(&b.ndcg_at_10)
                    .unwrap_or(Ordering::Equal)
                    .then_with(|| b.latency_ms_p99.cmp(&a.latency_ms_p99))
            })
    }
}

/// Compute NDCG@k for a list of relevances in rank order.
/// Non-finite or <= 0 relevances are treated as non-relevant.
///
/// `all_ground_truth` should contain ALL ground-truth relevances for the query
/// (not just those that appeared in the results). The IDCG is computed from
/// the ideal ranking of these values. Passing only the returned-doc relevances
/// would inflate NDCG scores for poor retrievers.
pub fn ndcg_at_k(relevances: &[f64], k: usize, all_ground_truth: &[f64]) -> f64 {
    if k == 0 || relevances.is_empty() {
        return 0.0;
    }
    let dcg = dcg_at_k(relevances, k);
    if dcg == 0.0 {
        return 0.0;
    }
    let mut ideal: Vec<f64> = all_ground_truth
        .iter()
        .map(|rel| if rel.is_finite() { rel.max(0.0) } else { 0.0 })
        .collect();
    ideal.sort_by(|a, b| b.partial_cmp(a).unwrap_or(Ordering::Equal));
    let idcg = dcg_at_k(&ideal, k);
    if idcg == 0.0 { 0.0 } else { dcg / idcg }
}

fn dcg_at_k(relevances: &[f64], k: usize) -> f64 {
    relevances
        .iter()
        .take(k)
        .enumerate()
        .map(|(idx, rel)| {
            let rel = if rel.is_finite() { *rel } else { 0.0 };
            let rel = rel.max(0.0);
            let denom = (idx as f64 + 2.0).log2();
            (2.0_f64.powf(rel) - 1.0) / denom
        })
        .sum()
}

// ==================== Evaluation Harness ====================

/// A document in the evaluation corpus.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Document {
    /// Unique document identifier.
    pub id: String,
    /// Document content (text to embed).
    pub content: String,
}

/// Ground truth relevance judgment for a query-document pair.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct RelevanceJudgment {
    /// Document ID.
    pub doc_id: String,
    /// Relevance score (0=not relevant, 1=somewhat, 2=highly, 3=perfect).
    pub relevance: f64,
}

/// A query with ground truth relevance judgments.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct QueryWithJudgments {
    /// Query text.
    pub query: String,
    /// Ground truth relevance judgments for this query.
    pub judgments: Vec<RelevanceJudgment>,
}

/// Evaluation corpus containing documents and queries with ground truth.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct EvaluationCorpus {
    /// Corpus name/identifier.
    pub name: String,
    /// Documents in the corpus.
    pub documents: Vec<Document>,
    /// Queries with ground truth judgments.
    pub queries: Vec<QueryWithJudgments>,
}

impl EvaluationCorpus {
    /// Create a new empty corpus.
    pub fn new(name: &str) -> Self {
        Self {
            name: name.to_string(),
            documents: Vec::new(),
            queries: Vec::new(),
        }
    }

    /// Add a document to the corpus.
    pub fn add_document(&mut self, id: &str, content: &str) {
        self.documents.push(Document {
            id: id.to_string(),
            content: content.to_string(),
        });
    }

    /// Add a query with judgments.
    pub fn add_query(&mut self, query: &str, judgments: Vec<(&str, f64)>) {
        self.queries.push(QueryWithJudgments {
            query: query.to_string(),
            judgments: judgments
                .into_iter()
                .map(|(doc_id, relevance)| RelevanceJudgment {
                    doc_id: doc_id.to_string(),
                    relevance,
                })
                .collect(),
        });
    }

    /// Compute a hash of the corpus for reproducibility.
    pub fn compute_hash(&self) -> String {
        use std::collections::hash_map::DefaultHasher;
        use std::hash::{Hash, Hasher};

        let mut hasher = DefaultHasher::new();
        self.name.hash(&mut hasher);
        for doc in &self.documents {
            doc.id.hash(&mut hasher);
            doc.content.hash(&mut hasher);
        }
        for query in &self.queries {
            query.query.hash(&mut hasher);
            for j in &query.judgments {
                j.doc_id.hash(&mut hasher);
                // Hash relevance as bits to avoid float issues
                j.relevance.to_bits().hash(&mut hasher);
            }
        }
        format!("{:016x}", hasher.finish())
    }

    /// Create a sample corpus for testing embedders on code search scenarios.
    pub fn code_search_sample() -> Self {
        let mut corpus = Self::new("code-search-sample");

        // Add sample documents representing code snippets and discussions
        corpus.add_document("d1", "implementing authentication with jwt tokens in rust using jsonwebtoken crate for secure api access");
        corpus.add_document("d2", "database connection pool configuration using sqlx with postgres for high performance queries");
        corpus.add_document(
            "d3",
            "error handling patterns in rust using thiserror and anyhow for better error messages",
        );
        corpus.add_document(
            "d4",
            "async runtime setup with asupersync for concurrent task processing and io operations",
        );
        corpus.add_document(
            "d5",
            "parsing json data with serde for serialization and deserialization of structs",
        );
        corpus.add_document(
            "d6",
            "logging configuration using tracing crate for structured observability and debugging",
        );
        corpus.add_document(
            "d7",
            "cli argument parsing with clap for building command line applications",
        );
        corpus.add_document(
            "d8",
            "http client requests using asupersync http primitives for external service calls",
        );
        corpus.add_document(
            "d9",
            "unit testing patterns with cargo test and mock objects for reliable tests",
        );
        corpus.add_document(
            "d10",
            "file system operations reading and writing files with std fs module",
        );

        // Add queries with ground truth relevance judgments
        // Relevance: 0=not relevant, 1=somewhat, 2=highly, 3=perfect match
        corpus.add_query(
            "how to authenticate users with jwt",
            vec![
                ("d1", 3.0), // Perfect match
                ("d2", 0.0), // Not relevant
                ("d8", 1.0), // Somewhat (might involve API auth)
            ],
        );

        corpus.add_query(
            "database connection setup",
            vec![
                ("d2", 3.0),  // Perfect match
                ("d4", 1.0),  // Async might be related
                ("d10", 0.0), // Not relevant
            ],
        );

        corpus.add_query(
            "error handling best practices",
            vec![
                ("d3", 3.0), // Perfect match
                ("d6", 1.0), // Logging errors
                ("d9", 1.0), // Testing error cases
            ],
        );

        corpus.add_query(
            "async programming asupersync",
            vec![
                ("d4", 3.0), // Perfect match
                ("d2", 1.0), // Async DB queries
                ("d8", 2.0), // Async HTTP
            ],
        );

        corpus.add_query(
            "json serialization",
            vec![
                ("d5", 3.0), // Perfect match
                ("d8", 1.0), // API often uses JSON
                ("d1", 1.0), // JWT is JSON-based
            ],
        );

        corpus
    }
}

/// Result of evaluating a single query.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QueryEvalResult {
    /// The query text.
    pub query: String,
    /// NDCG@10 for this query.
    pub ndcg_at_10: f64,
    /// Ranked document IDs returned by the model.
    pub ranked_docs: Vec<String>,
    /// Latency for this query in milliseconds.
    pub latency_ms: u64,
}

/// Configuration for the evaluation harness.
#[derive(Debug, Clone)]
pub struct EvaluationConfig {
    /// Number of warmup queries before timing.
    pub warmup_queries: usize,
    /// Number of timing iterations per query.
    pub timing_iterations: usize,
    /// Top-k for NDCG calculation.
    pub ndcg_k: usize,
}

impl Default for EvaluationConfig {
    fn default() -> Self {
        Self {
            warmup_queries: 3,
            timing_iterations: 5,
            ndcg_k: 10,
        }
    }
}

/// Compute cosine similarity between two vectors.
pub fn cosine_similarity(a: &[f32], b: &[f32]) -> f32 {
    if a.len() != b.len() {
        return 0.0;
    }
    let dot: f32 = a.iter().zip(b.iter()).map(|(x, y)| x * y).sum();
    let norm_a: f32 = a.iter().map(|x| x * x).sum::<f32>().sqrt();
    let norm_b: f32 = b.iter().map(|x| x * x).sum::<f32>().sqrt();
    if norm_a == 0.0 || norm_b == 0.0 {
        return 0.0;
    }
    dot / (norm_a * norm_b)
}

/// Evaluation harness for running bake-off evaluations.
pub struct EvaluationHarness {
    config: EvaluationConfig,
}

impl EvaluationHarness {
    /// Create a new evaluation harness with default config.
    pub fn new() -> Self {
        Self {
            config: EvaluationConfig::default(),
        }
    }

    /// Create with custom config.
    pub fn with_config(config: EvaluationConfig) -> Self {
        Self { config }
    }

    /// Evaluate an embedder against a corpus.
    ///
    /// Returns a ValidationReport with NDCG, latency, and memory metrics.
    pub fn evaluate<E: crate::search::embedder::Embedder>(
        &self,
        embedder: &E,
        corpus: &EvaluationCorpus,
        metadata: &ModelMetadata,
    ) -> Result<ValidationReport, String> {
        let corpus_hash = corpus.compute_hash();
        let first_doc = corpus.documents.first().ok_or("Empty corpus")?;
        if corpus.queries.is_empty() {
            return Err("Empty query set".to_string());
        }

        // Measure cold start (first embedding)
        let cold_start = Instant::now();
        embedder
            .embed_sync(&first_doc.content)
            .map_err(|e| e.to_string())?;
        let cold_start_ms = cold_start.elapsed().as_millis() as u64;

        // Embed all documents
        let doc_embeddings: Vec<Vec<f32>> = corpus
            .documents
            .iter()
            .map(|d| embedder.embed_sync(&d.content))
            .collect::<Result<Vec<_>, _>>()
            .map_err(|e| e.to_string())?;

        // Warmup queries
        for i in 0..self.config.warmup_queries.min(corpus.queries.len()) {
            let _ = embedder.embed_sync(&corpus.queries[i].query);
        }

        // Evaluate each query
        let mut query_results = Vec::new();
        let mut latencies = Vec::new();

        for query_with_judgments in &corpus.queries {
            // Build relevance map
            let relevance_map: std::collections::HashMap<&str, f64> = query_with_judgments
                .judgments
                .iter()
                .map(|j| (j.doc_id.as_str(), j.relevance))
                .collect();

            // Time the query embedding (average over iterations, minimum 1)
            let iterations = self.config.timing_iterations.max(1);
            let mut query_latencies = Vec::with_capacity(iterations);
            let mut query_embedding = Vec::new();
            for _ in 0..iterations {
                let start = Instant::now();
                query_embedding = embedder
                    .embed_sync(&query_with_judgments.query)
                    .map_err(|e| e.to_string())?;
                query_latencies.push(start.elapsed());
            }
            let avg_latency = query_latencies
                .iter()
                .map(|d| d.as_millis() as u64)
                .sum::<u64>()
                / query_latencies.len() as u64;
            latencies.push(Duration::from_millis(avg_latency));

            // Rank documents by similarity
            let mut scored_docs: Vec<(usize, f32)> = doc_embeddings
                .iter()
                .enumerate()
                .map(|(idx, emb)| (idx, cosine_similarity(&query_embedding, emb)))
                .collect();
            scored_docs.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));

            // Get ranked doc IDs
            let ranked_docs: Vec<String> = scored_docs
                .iter()
                .take(self.config.ndcg_k)
                .map(|(idx, _)| corpus.documents[*idx].id.clone())
                .collect();

            // Compute relevances in ranked order
            let relevances: Vec<f64> = ranked_docs
                .iter()
                .map(|id| *relevance_map.get(id.as_str()).unwrap_or(&0.0))
                .collect();

            // All ground-truth relevances (for ideal DCG computation)
            let all_gt: Vec<f64> = relevance_map.values().copied().collect();
            let ndcg = ndcg_at_k(&relevances, self.config.ndcg_k, &all_gt);

            query_results.push(QueryEvalResult {
                query: query_with_judgments.query.clone(),
                ndcg_at_10: ndcg,
                ranked_docs,
                latency_ms: avg_latency,
            });
        }

        // Compute aggregate metrics
        let avg_ndcg = if query_results.is_empty() {
            0.0
        } else {
            query_results.iter().map(|r| r.ndcg_at_10).sum::<f64>() / query_results.len() as f64
        };

        let latency_stats = LatencyStats::from_durations(&latencies);

        // Estimate memory (model size as proxy - real measurement would need system APIs)
        let memory_mb = metadata.size_bytes.unwrap_or(0) / (1024 * 1024);

        let eligible = metadata.is_eligible();
        let mut report = ValidationReport {
            model_id: metadata.id.clone(),
            corpus_hash,
            ndcg_at_10: avg_ndcg,
            latency_ms_p50: latency_stats.p50_ms,
            latency_ms_p95: latency_stats.p95_ms,
            latency_ms_p99: latency_stats.p99_ms,
            cold_start_ms,
            memory_mb,
            eligible,
            meets_criteria: false,
            warnings: Vec::new(),
        };

        report.meets_criteria = report.check_criteria();

        // Add warnings
        if cold_start_ms > criteria::COLD_START_MAX_MS {
            report.warnings.push(format!(
                "Cold start {}ms exceeds {}ms limit",
                cold_start_ms,
                criteria::COLD_START_MAX_MS
            ));
        }
        if latency_stats.p99_ms > criteria::WARM_P99_MAX_MS {
            report.warnings.push(format!(
                "P99 latency {}ms exceeds {}ms limit",
                latency_stats.p99_ms,
                criteria::WARM_P99_MAX_MS
            ));
        }
        if memory_mb > criteria::MEMORY_MAX_MB {
            report.warnings.push(format!(
                "Memory {}MB exceeds {}MB limit",
                memory_mb,
                criteria::MEMORY_MAX_MB
            ));
        }

        Ok(report)
    }

    /// Run a full bake-off comparison with baseline and candidates.
    pub fn run_comparison<E: crate::search::embedder::Embedder>(
        &self,
        baseline: (&E, &ModelMetadata),
        candidates: Vec<(&E, &ModelMetadata)>,
        corpus: &EvaluationCorpus,
    ) -> Result<BakeoffComparison, String> {
        let corpus_hash = corpus.compute_hash();

        // Evaluate baseline
        let baseline_report = self.evaluate(baseline.0, corpus, baseline.1)?;

        // Evaluate all candidates
        let mut candidate_reports = Vec::new();
        for (embedder, metadata) in candidates {
            let report = self.evaluate(embedder, corpus, metadata)?;
            candidate_reports.push(report);
        }

        // Build initial comparison
        let mut comparison = BakeoffComparison {
            corpus_hash,
            baseline: baseline_report.clone(),
            candidates: candidate_reports,
            recommendation: None,
            recommendation_reason: String::new(),
        };

        // Find the winner and extract data before mutating
        let winner_data = comparison.find_winner().map(|w| {
            (
                w.model_id.clone(),
                w.ndcg_at_10,
                w.latency_ms_p99,
                w.memory_mb,
            )
        });

        if let Some((model_id, ndcg, p99, memory)) = winner_data {
            comparison.recommendation = Some(model_id.clone());
            let pct_of_baseline = if baseline_report.ndcg_at_10 > 0.0 {
                format!("{}%", (ndcg / baseline_report.ndcg_at_10 * 100.0) as u32)
            } else {
                "N/A".to_string()
            };
            comparison.recommendation_reason = format!(
                "Best eligible candidate with NDCG@10={:.3} ({} of baseline), p99={}ms, memory={}MB",
                ndcg, pct_of_baseline, p99, memory
            );
        } else {
            comparison.recommendation_reason =
                "No eligible candidate meets all criteria".to_string();
        }

        Ok(comparison)
    }
}

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

/// Format a comparison as a markdown table for reporting.
pub fn format_comparison_table(comparison: &BakeoffComparison) -> String {
    let mut output = String::new();

    output.push_str("# Bake-off Results\n\n");
    output.push_str(&format!("Corpus hash: `{}`\n\n", comparison.corpus_hash));

    output.push_str("| Model | NDCG@10 | P50 (ms) | P95 (ms) | P99 (ms) | Cold (ms) | Memory (MB) | Eligible | Meets Criteria |\n");
    output.push_str("|-------|---------|----------|----------|----------|-----------|-------------|----------|----------------|\n");

    // Baseline first
    let b = &comparison.baseline;
    output.push_str(&format!(
        "| {} (baseline) | {:.3} | {} | {} | {} | {} | {} | {} | {} |\n",
        b.model_id,
        b.ndcg_at_10,
        b.latency_ms_p50,
        b.latency_ms_p95,
        b.latency_ms_p99,
        b.cold_start_ms,
        b.memory_mb,
        if b.eligible { "" } else { "" },
        if b.meets_criteria { "" } else { "" }
    ));

    // Candidates
    for c in &comparison.candidates {
        let marker = if Some(&c.model_id) == comparison.recommendation.as_ref() {
            ""
        } else {
            ""
        };
        output.push_str(&format!(
            "| {}{} | {:.3} | {} | {} | {} | {} | {} | {} | {} |\n",
            c.model_id,
            marker,
            c.ndcg_at_10,
            c.latency_ms_p50,
            c.latency_ms_p95,
            c.latency_ms_p99,
            c.cold_start_ms,
            c.memory_mb,
            if c.eligible { "" } else { "" },
            if c.meets_criteria { "" } else { "" }
        ));
    }

    output.push_str("\n## Recommendation\n\n");
    if let Some(ref winner) = comparison.recommendation {
        output.push_str(&format!("**Winner:** {}\n\n", winner));
    }
    output.push_str(&format!("{}\n", comparison.recommendation_reason));

    output
}

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

    #[test]
    fn ndcg_perfect_is_one() {
        let relevances = vec![3.0, 2.0, 1.0];
        let ndcg = ndcg_at_k(&relevances, 3, &relevances);
        assert!((ndcg - 1.0).abs() < 1e-9);
    }

    #[test]
    fn ndcg_zero_when_no_relevance() {
        let relevances = vec![0.0, 0.0, 0.0];
        let ndcg = ndcg_at_k(&relevances, 3, &relevances);
        assert_eq!(ndcg, 0.0);
    }

    #[test]
    fn ndcg_handles_partial_relevance() {
        let all_gt = vec![2.0, 1.0, 0.0];
        let returned = vec![1.0, 0.0, 2.0]; // out of ideal order
        let ndcg = ndcg_at_k(&returned, 3, &all_gt);
        assert!(ndcg > 0.0 && ndcg < 1.0);
    }

    #[test]
    fn report_roundtrip() {
        let report = ValidationReport {
            model_id: "hash".to_string(),
            corpus_hash: "deadbeef".to_string(),
            ndcg_at_10: 0.42,
            latency_ms_p50: 12,
            latency_ms_p95: 30,
            latency_ms_p99: 45,
            cold_start_ms: 500,
            memory_mb: 150,
            eligible: true,
            meets_criteria: true,
            warnings: vec!["example warning".to_string()],
        };
        let encoded = serde_json::to_string(&report).expect("serialize");
        let decoded: ValidationReport = serde_json::from_str(&encoded).expect("deserialize");
        assert_eq!(report, decoded);
    }

    #[test]
    fn model_eligibility_by_date() {
        let eligible_model = ModelMetadata {
            id: "new-model".to_string(),
            name: "New Model".to_string(),
            source: "huggingface".to_string(),
            release_date: "2025-12-01".to_string(),
            dimension: Some(384),
            size_bytes: Some(100_000_000),
            is_baseline: false,
        };
        assert!(eligible_model.is_eligible());

        let old_model = ModelMetadata {
            id: "old-model".to_string(),
            name: "Old Model".to_string(),
            source: "huggingface".to_string(),
            release_date: "2025-06-01".to_string(),
            dimension: Some(384),
            size_bytes: Some(100_000_000),
            is_baseline: false,
        };
        assert!(!old_model.is_eligible());

        let baseline_model = ModelMetadata {
            id: "baseline".to_string(),
            name: "Baseline".to_string(),
            source: "huggingface".to_string(),
            release_date: "2025-12-01".to_string(),
            dimension: Some(384),
            size_bytes: Some(100_000_000),
            is_baseline: true,
        };
        assert!(!baseline_model.is_eligible());
    }

    #[test]
    fn latency_stats_from_durations() {
        let durations = vec![
            Duration::from_millis(10),
            Duration::from_millis(20),
            Duration::from_millis(30),
            Duration::from_millis(40),
            Duration::from_millis(100),
        ];
        let stats = LatencyStats::from_durations(&durations);

        assert_eq!(stats.samples, 5);
        assert_eq!(stats.min_ms, 10);
        assert_eq!(stats.max_ms, 100);
        assert!((stats.mean_ms - 40.0).abs() < 0.1);
        assert_eq!(stats.p50_ms, 30);
    }

    #[test]
    fn latency_stats_empty() {
        let stats = LatencyStats::from_durations(&[]);
        assert_eq!(stats.samples, 0);
        assert_eq!(stats.p50_ms, 0);
    }

    #[test]
    fn latency_timer_records_samples() {
        let mut timer = LatencyTimer::new();

        // Time a simple operation
        let result = timer.time(|| 42);
        assert_eq!(result, 42);

        let stats = timer.stats();
        assert_eq!(stats.samples, 1);
    }

    #[test]
    fn report_meets_criteria() {
        let good_report = ValidationReport {
            model_id: "good".to_string(),
            corpus_hash: "test".to_string(),
            ndcg_at_10: 0.85,
            latency_ms_p50: 50,
            latency_ms_p95: 100,
            latency_ms_p99: 200, // Under 250ms
            cold_start_ms: 1500, // Under 2s
            memory_mb: 200,      // Under 300MB
            eligible: true,
            meets_criteria: true,
            warnings: vec![],
        };
        assert!(good_report.check_criteria());

        let bad_latency = ValidationReport {
            latency_ms_p99: 300, // Over 250ms
            ..good_report.clone()
        };
        assert!(!bad_latency.check_criteria());

        let bad_cold_start = ValidationReport {
            cold_start_ms: 3000, // Over 2s
            ..good_report.clone()
        };
        assert!(!bad_cold_start.check_criteria());

        let bad_memory = ValidationReport {
            memory_mb: 400, // Over 300MB
            ..good_report
        };
        assert!(!bad_memory.check_criteria());
    }

    #[test]
    fn report_quality_threshold() {
        let baseline = ValidationReport {
            model_id: "baseline".to_string(),
            corpus_hash: "test".to_string(),
            ndcg_at_10: 0.80,
            latency_ms_p50: 50,
            latency_ms_p95: 100,
            latency_ms_p99: 150,
            cold_start_ms: 1000,
            memory_mb: 200,
            eligible: false,
            meets_criteria: true,
            warnings: vec![],
        };

        let good_candidate = ValidationReport {
            model_id: "good".to_string(),
            ndcg_at_10: 0.70, // 87.5% of baseline, above 80%
            ..baseline.clone()
        };
        assert!(good_candidate.meets_quality_threshold(&baseline));

        let bad_candidate = ValidationReport {
            model_id: "bad".to_string(),
            ndcg_at_10: 0.60, // 75% of baseline, below 80%
            ..baseline.clone()
        };
        assert!(!bad_candidate.meets_quality_threshold(&baseline));
    }

    #[test]
    fn bakeoff_comparison_finds_winner() {
        let baseline = ValidationReport {
            model_id: "baseline".to_string(),
            corpus_hash: "test".to_string(),
            ndcg_at_10: 0.80,
            latency_ms_p50: 50,
            latency_ms_p95: 100,
            latency_ms_p99: 150,
            cold_start_ms: 1000,
            memory_mb: 200,
            eligible: false,
            meets_criteria: true,
            warnings: vec![],
        };

        let candidate1 = ValidationReport {
            model_id: "candidate1".to_string(),
            ndcg_at_10: 0.75, // Good quality
            eligible: true,
            meets_criteria: true,
            ..baseline.clone()
        };

        let candidate2 = ValidationReport {
            model_id: "candidate2".to_string(),
            ndcg_at_10: 0.85, // Better quality
            eligible: true,
            meets_criteria: true,
            ..baseline.clone()
        };

        let ineligible = ValidationReport {
            model_id: "ineligible".to_string(),
            ndcg_at_10: 0.90, // Best quality but not eligible
            eligible: false,
            meets_criteria: true,
            ..baseline.clone()
        };

        let comparison = BakeoffComparison {
            corpus_hash: "test".to_string(),
            baseline: baseline.clone(),
            candidates: vec![candidate1, candidate2.clone(), ineligible],
            recommendation: None,
            recommendation_reason: String::new(),
        };

        let winner = comparison.find_winner();
        assert!(winner.is_some());
        assert_eq!(winner.unwrap().model_id, "candidate2");
    }

    // ==================== Harness Tests ====================

    #[test]
    fn corpus_creation_and_hash() {
        let mut corpus = EvaluationCorpus::new("test-corpus");
        corpus.add_document("d1", "hello world");
        corpus.add_document("d2", "goodbye world");
        corpus.add_query("hello", vec![("d1", 3.0), ("d2", 0.0)]);

        assert_eq!(corpus.name, "test-corpus");
        assert_eq!(corpus.documents.len(), 2);
        assert_eq!(corpus.queries.len(), 1);

        let hash1 = corpus.compute_hash();
        assert_eq!(hash1.len(), 16); // 16 hex chars

        // Same corpus should produce same hash
        let hash2 = corpus.compute_hash();
        assert_eq!(hash1, hash2);

        // Different corpus should produce different hash
        corpus.add_document("d3", "new document");
        let hash3 = corpus.compute_hash();
        assert_ne!(hash1, hash3);
    }

    #[test]
    fn evaluation_rejects_empty_query_set() {
        let harness = EvaluationHarness::new();
        let mut corpus = EvaluationCorpus::new("no-queries");
        corpus.add_document("d1", "hello world");
        let embedder = crate::search::hash_embedder::HashEmbedder::new(16);
        let metadata = ModelMetadata {
            id: "hash".to_string(),
            name: "Hash".to_string(),
            source: "test".to_string(),
            release_date: "2025-12-01".to_string(),
            dimension: Some(16),
            size_bytes: Some(0),
            is_baseline: false,
        };

        let err = harness
            .evaluate(&embedder, &corpus, &metadata)
            .expect_err("empty query set must not produce a successful bakeoff report");
        assert!(err.contains("Empty query set"));
    }

    #[test]
    fn sample_corpus_is_valid() {
        let corpus = EvaluationCorpus::code_search_sample();
        assert!(!corpus.documents.is_empty());
        assert!(!corpus.queries.is_empty());

        // Each query should have at least one judgment
        for query in &corpus.queries {
            assert!(!query.judgments.is_empty());
        }

        // Hash should be stable
        let hash = corpus.compute_hash();
        assert!(!hash.is_empty());
    }

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

    #[test]
    fn cosine_similarity_orthogonal_vectors() {
        let a = vec![1.0, 0.0, 0.0];
        let b = vec![0.0, 1.0, 0.0];
        let sim = cosine_similarity(&a, &b);
        assert!(sim.abs() < 1e-6);
    }

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

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

    #[test]
    fn evaluation_config_defaults() {
        let config = EvaluationConfig::default();
        assert_eq!(config.warmup_queries, 3);
        assert_eq!(config.timing_iterations, 5);
        assert_eq!(config.ndcg_k, 10);
    }

    #[test]
    fn harness_creation() {
        let harness = EvaluationHarness::new();
        assert_eq!(harness.config.ndcg_k, 10);

        let custom_config = EvaluationConfig {
            warmup_queries: 5,
            timing_iterations: 10,
            ndcg_k: 5,
        };
        let harness = EvaluationHarness::with_config(custom_config);
        assert_eq!(harness.config.ndcg_k, 5);
    }

    #[test]
    fn corpus_roundtrip() {
        let corpus = EvaluationCorpus::code_search_sample();
        let json = serde_json::to_string(&corpus).expect("serialize");
        let decoded: EvaluationCorpus = serde_json::from_str(&json).expect("deserialize");
        assert_eq!(corpus, decoded);
    }

    #[test]
    fn query_eval_result_roundtrip() {
        let result = QueryEvalResult {
            query: "test query".to_string(),
            ndcg_at_10: 0.85,
            ranked_docs: vec!["d1".to_string(), "d2".to_string()],
            latency_ms: 15,
        };
        let json = serde_json::to_string(&result).expect("serialize");
        let decoded: QueryEvalResult = serde_json::from_str(&json).expect("deserialize");
        assert_eq!(result.query, decoded.query);
        assert_eq!(result.ndcg_at_10, decoded.ndcg_at_10);
    }

    #[test]
    fn format_comparison_table_output() {
        let baseline = ValidationReport {
            model_id: "baseline".to_string(),
            corpus_hash: "test123".to_string(),
            ndcg_at_10: 0.80,
            latency_ms_p50: 50,
            latency_ms_p95: 100,
            latency_ms_p99: 150,
            cold_start_ms: 1000,
            memory_mb: 200,
            eligible: false,
            meets_criteria: true,
            warnings: vec![],
        };

        let candidate = ValidationReport {
            model_id: "winner".to_string(),
            ndcg_at_10: 0.85,
            eligible: true,
            meets_criteria: true,
            ..baseline.clone()
        };

        let comparison = BakeoffComparison {
            corpus_hash: "test123".to_string(),
            baseline,
            candidates: vec![candidate],
            recommendation: Some("winner".to_string()),
            recommendation_reason: "Best candidate".to_string(),
        };

        let table = format_comparison_table(&comparison);
        assert!(table.contains("Bake-off Results"));
        assert!(table.contains("baseline"));
        assert!(table.contains("winner"));
        assert!(table.contains("")); // Winner marker
        assert!(table.contains("Recommendation"));
    }
}