khive-runtime 0.2.11

Composable Service API: entity/note CRUD, graph traversal, hybrid search, curation.
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
//! Retrieval Objective implementations for khive-runtime.
//!
//! Domain-specific objectives that operate on pre-computed retrieval signals.
//! Pure math: no IO, no async. The runtime layer materialises the signal data
//! and feeds it in via the candidate struct.

use std::collections::HashMap;

use uuid::Uuid;

use khive_fold::objective::{Objective, ObjectiveContext};
use khive_fold::ordering::HasId;

/// Pre-computed retrieval signals for a single candidate entity.
///
/// All fields are `Option` — a missing signal scores 0.0. The runtime layer
/// is responsible for populating whichever fields are available before handing
/// the slice to an objective.
#[derive(Debug, Clone)]
pub struct RetrievalCandidate {
    /// Stable entity UUID.
    pub id: Uuid,
    /// Cosine similarity to the query vector (0.0–1.0).
    pub vector_score: Option<f64>,
    /// BM25/FTS relevance score (0.0–1.0 normalised, or raw rank score).
    pub text_score: Option<f64>,
    /// Hop distance from the nearest anchor node (0 = anchor itself).
    pub graph_distance: Option<u32>,
    /// Pre-fused RRF score from `FusionStrategy::Rrf`.
    pub rrf_score: Option<f64>,
}

impl HasId for RetrievalCandidate {
    #[inline]
    fn id(&self) -> Uuid {
        self.id
    }
}

// ── VectorSimilarityObjective ────────────────────────────────────────────────

/// Scores a candidate by cosine similarity to the query vector.
///
/// Returns `vector_score` unchanged, or 0.0 when the field is absent.
pub struct VectorSimilarityObjective;

impl Objective<RetrievalCandidate> for VectorSimilarityObjective {
    #[inline]
    fn score(&self, candidate: &RetrievalCandidate, _context: &ObjectiveContext) -> f64 {
        candidate.vector_score.unwrap_or(0.0)
    }

    fn name(&self) -> &str {
        "VectorSimilarityObjective"
    }
}

// ── TextRelevanceObjective ───────────────────────────────────────────────────

/// Scores a candidate by BM25/FTS relevance.
///
/// Returns `text_score` unchanged, or 0.0 when the field is absent.
pub struct TextRelevanceObjective;

impl Objective<RetrievalCandidate> for TextRelevanceObjective {
    #[inline]
    fn score(&self, candidate: &RetrievalCandidate, _context: &ObjectiveContext) -> f64 {
        candidate.text_score.unwrap_or(0.0)
    }

    fn name(&self) -> &str {
        "TextRelevanceObjective"
    }
}

// ── GraphProximityObjective ──────────────────────────────────────────────────

/// Scores a candidate by graph proximity to anchor nodes.
///
/// Score formula (linear decay):
///
/// ```text
/// d ≤ max_distance → score = 1.0 − (d as f64 / max_distance as f64)
/// d > max_distance → score = 0.0
/// missing          → score = 0.0
/// ```
///
/// Direct anchor hits (d = 0) score 1.0. The boundary `d == max_distance`
/// scores 0.0; anything beyond also scores 0.0.
pub struct GraphProximityObjective {
    /// Maximum hop distance to consider. Candidates beyond this score 0.0.
    pub max_distance: u32,
}

impl Objective<RetrievalCandidate> for GraphProximityObjective {
    fn score(&self, candidate: &RetrievalCandidate, _context: &ObjectiveContext) -> f64 {
        let d = match candidate.graph_distance {
            Some(d) => d,
            None => return 0.0,
        };
        if self.max_distance == 0 || d >= self.max_distance {
            return 0.0;
        }
        1.0 - (d as f64 / self.max_distance as f64)
    }

    fn name(&self) -> &str {
        "GraphProximityObjective"
    }
}

// ── RrfFusionObjective ───────────────────────────────────────────────────────

/// Scores a candidate by its pre-computed RRF fusion score.
///
/// Returns `rrf_score` unchanged, or 0.0 when the field is absent.
/// Implements `Objective` for both `RetrievalCandidate` and `NoteCandidate`
/// so the same objective can be used in the general retrieval pipeline
/// and the memory recall pipeline.
pub struct RrfFusionObjective;

impl Objective<RetrievalCandidate> for RrfFusionObjective {
    #[inline]
    fn score(&self, candidate: &RetrievalCandidate, _context: &ObjectiveContext) -> f64 {
        candidate.rrf_score.unwrap_or(0.0)
    }

    fn name(&self) -> &str {
        "RrfFusionObjective"
    }
}

impl Objective<NoteCandidate> for RrfFusionObjective {
    #[inline]
    fn score(&self, candidate: &NoteCandidate, _context: &ObjectiveContext) -> f64 {
        candidate.rrf_score.unwrap_or(0.0)
    }

    fn name(&self) -> &str {
        "RrfFusionObjective"
    }
}

// ── Memory-Recall Objectives ──────────────────────────────────────────────────

/// Pre-computed signals for a single memory note candidate.
///
/// Used by the recall pipeline's `ComposePipeline` to score and rank candidates
/// via `DecayAwareSalienceObjective`, `TemporalRecencyObjective`, and
/// `RerankerObjective` without any IO. The runtime layer populates this struct
/// from stored notes before handing the slice to the pipeline.
#[derive(Debug, Clone)]
pub struct NoteCandidate {
    /// Stable note UUID.
    pub id: Uuid,
    /// Pre-fused RRF score from the retrieval stage (0.0–1.0).
    pub rrf_score: Option<f64>,
    /// Raw salience stored on the note (0.0–1.0).
    pub salience: f64,
    /// Per-note exponential decay rate (>= 0.0).
    pub decay_factor: f64,
    /// Age of the note in days at query time.
    pub age_days: f64,
    /// Salience after applying the configured `DecayModel` (pre-computed by the caller).
    ///
    /// The caller must set this to `DecayModel::apply(salience, age_days, decay_factor, half_life)`
    /// so that objectives respect the configured decay model variant rather than
    /// always applying exponential decay. When not set, defaults to 0.0.
    pub effective_salience: f64,
    /// Per-reranker scores populated by the rerank stage.
    /// Keyed by reranker name (e.g. "cross_encoder", "salience", "graph_proximity").
    pub rerank_scores: HashMap<String, f64>,
}

impl HasId for NoteCandidate {
    #[inline]
    fn id(&self) -> Uuid {
        self.id
    }
}

// ── DecayAwareSalienceObjective ──────────────────────────────────────────────

/// Scores a `NoteCandidate` by salience with configurable temporal decay.
///
/// The decay formula is determined by the configured `DecayModel` (injected at
/// construction time). The default `DecayModel::Exponential` uses the note's own
/// `decay_factor`: `salience * exp(-decay_factor * age_days)`.
///
/// This objective participates in `WeightedObjective` composition alongside
/// `RrfFusionObjective` and `TemporalRecencyObjective` to form the full recall
/// scoring pipeline.
pub struct DecayAwareSalienceObjective {
    /// Exponential decay rate k (>= 0.0). Score = `salience * exp(-k * age_days)`.
    /// Corresponds to the per-note `decay_factor` parameter stored on memory notes.
    pub decay_rate: f64,
}

impl DecayAwareSalienceObjective {
    /// Create a new objective with the given exponential decay rate.
    ///
    /// `decay_rate = 0.01` gives a ~69-day half-life (default for memory notes).
    pub fn new(decay_rate: f64) -> Self {
        Self { decay_rate }
    }

    /// Default memory decay rate: 0.01 (~69-day half-life).
    pub fn default_memory() -> Self {
        Self::new(0.01)
    }
}

impl Objective<NoteCandidate> for DecayAwareSalienceObjective {
    #[inline]
    fn score(&self, candidate: &NoteCandidate, _context: &ObjectiveContext) -> f64 {
        // effective_salience = salience * exp(-decay_factor * age_days)
        candidate.salience * (-candidate.decay_factor * candidate.age_days).exp()
    }

    fn name(&self) -> &str {
        "DecayAwareSalienceObjective"
    }
}

// ── AmplifiedDecayAwareSalienceObjective ─────────────────────────────────────

/// Scores a `NoteCandidate` by salience with exponential decay and a non-linear
/// amplification exponent applied after decay.
///
/// Formula: `(salience * exp(-decay_factor * age_days)) ^ alpha`
///
/// With `alpha > 1.0`, high-salience memories rank more clearly above low-salience
/// ones when relevance is similar. At `alpha = 1.5` (the memory-recall default),
/// salience 0.9 → 0.854 and salience 0.3 → 0.164 — a ~5.2× spread vs the ~3× linear
/// spread. Keep `alpha ≤ 2.0`; values above 2 compress near-zero salience toward 0.
///
/// Used by the memory recall pipeline to make salience a meaningful tiebreaker
/// without dominating relevance at the default weight of 0.20.
pub struct AmplifiedDecayAwareSalienceObjective {
    /// Power applied to the decayed salience value. Must be > 0.
    pub alpha: f64,
}

impl AmplifiedDecayAwareSalienceObjective {
    /// Create with the given amplification exponent.
    pub fn new(alpha: f64) -> Self {
        Self { alpha }
    }

    /// Default memory alpha from the memory recall handler: 1.5.
    pub fn default_memory() -> Self {
        Self::new(1.5)
    }
}

impl Objective<NoteCandidate> for AmplifiedDecayAwareSalienceObjective {
    #[inline]
    fn score(&self, candidate: &NoteCandidate, _context: &ObjectiveContext) -> f64 {
        // Use the pre-computed effective_salience which was produced by the caller
        // via DecayModel::apply(). This respects all four DecayModel variants
        // (Exponential, Hyperbolic, PowerLaw, None) instead of hardcoding exponential.
        candidate.effective_salience.powf(self.alpha)
    }

    fn name(&self) -> &str {
        "AmplifiedDecayAwareSalienceObjective"
    }
}

// ── TemporalRecencyObjective ─────────────────────────────────────────────────

/// Scores a `NoteCandidate` by pure temporal recency with a configurable half-life.
///
/// Formula: `exp(-ln(2) / half_life_days * age_days)`
///
/// At `age_days = 0` → score 1.0 (brand new note).
/// At `age_days = half_life_days` → score 0.5.
///
/// Complements `DecayAwareSalienceObjective`: this signal rewards freshness
/// independently of the note's own decay rate.
pub struct TemporalRecencyObjective {
    /// Number of days for the recency score to halve. Must be > 0.
    pub half_life_days: f64,
}

impl TemporalRecencyObjective {
    /// Create with the default temporal half-life of 30 days.
    pub fn default_memory() -> Self {
        Self {
            half_life_days: 30.0,
        }
    }
}

impl Objective<NoteCandidate> for TemporalRecencyObjective {
    #[inline]
    fn score(&self, candidate: &NoteCandidate, _context: &ObjectiveContext) -> f64 {
        let k = std::f64::consts::LN_2 / self.half_life_days.max(f64::EPSILON);
        (-k * candidate.age_days).exp()
    }

    fn name(&self) -> &str {
        "TemporalRecencyObjective"
    }
}

// ── RerankerObjective ────────────────────────────────────────────────────────

/// Scores a `NoteCandidate` using a named reranker's pre-computed score.
///
/// Looks up `candidate.rerank_scores[reranker_name]`. Returns 0.0 when the
/// reranker was not run (key absent) — callers should gate on
/// `RecallConfig.reranker_weights[name] > 0.0` before including this objective
/// in a `WeightedObjective` composition.
///
pub struct RerankerObjective {
    /// Name of the reranker to look up in `candidate.rerank_scores`.
    pub reranker_name: String,
}

impl RerankerObjective {
    /// Create a new objective for the named reranker.
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            reranker_name: name.into(),
        }
    }
}

impl Objective<NoteCandidate> for RerankerObjective {
    #[inline]
    fn score(&self, candidate: &NoteCandidate, _context: &ObjectiveContext) -> f64 {
        candidate
            .rerank_scores
            .get(&self.reranker_name)
            .copied()
            .unwrap_or(0.0)
    }

    fn name(&self) -> &str {
        "RerankerObjective"
    }
}

// ── MemoryRecallPipeline ──────────────────────────────────────────────────────

/// Composable scoring pipeline for memory recall candidates.
///
/// Wraps a `WeightedObjective<NoteCandidate>` with the three standard memory
/// scoring components (RRF relevance, amplified salience, temporal recency)
/// weighted by the recall config parameters. Pack code uses this type to avoid
/// a direct dependency on `khive-fold`.
pub struct MemoryRecallPipeline {
    pipeline: khive_fold::WeightedObjective<NoteCandidate>,
}

impl MemoryRecallPipeline {
    /// Build a pipeline from explicit component weights and temporal half-life.
    ///
    /// `relevance_weight`, `salience_weight`, `temporal_weight` correspond to
    /// `RecallConfig`'s three weight fields. `half_life_days` drives
    /// `TemporalRecencyObjective`. `salience_alpha` is the amplification exponent
    /// for `AmplifiedDecayAwareSalienceObjective` (default 1.5).
    pub fn new(
        relevance_weight: f64,
        salience_weight: f64,
        temporal_weight: f64,
        half_life_days: f64,
        salience_alpha: f64,
    ) -> Self {
        use khive_fold::WeightedObjective;
        let pipeline = WeightedObjective::<NoteCandidate>::new()
            .add(Box::new(RrfFusionObjective), relevance_weight)
            .add(
                Box::new(AmplifiedDecayAwareSalienceObjective::new(salience_alpha)),
                salience_weight,
            )
            .add(
                Box::new(TemporalRecencyObjective { half_life_days }),
                temporal_weight,
            );
        Self { pipeline }
    }

    /// Build a pipeline using the standard memory recall defaults.
    ///
    /// Weights: relevance=0.70, salience=0.20, temporal=0.10; half_life=30 days; alpha=1.5.
    pub fn default_memory() -> Self {
        Self::new(0.70, 0.20, 0.10, 30.0, 1.5)
    }

    /// Score a `NoteCandidate` through the pipeline.
    ///
    /// The result is in [0.0, 1.0]. The `NoteCandidate.rrf_score` field should
    /// carry the pre-normalized relevance (output of `normalize_relevance` / `RrfFusionObjective`).
    pub fn score(&self, candidate: &NoteCandidate) -> f64 {
        let ctx = ObjectiveContext::new();
        use khive_fold::objective::Objective;
        self.pipeline.score(candidate, &ctx).clamp(0.0, 1.0)
    }
}

// ────────────────────────────────────────────────────────────────────────────

// INLINE TEST JUSTIFICATION: tests here exercise the scoring math on internal
// NoteCandidate fields (raw_score, salience, recency) that are not exported.
// Moving them to tests/ would require pub-exporting those fields.
#[cfg(test)]
mod tests {
    use super::*;
    use khive_fold::objective::{Objective, ObjectiveContext};
    use khive_fold::WeightedObjective;
    use uuid::Uuid;

    fn ctx() -> ObjectiveContext {
        ObjectiveContext::new()
    }

    fn candidate(
        vector: Option<f64>,
        text: Option<f64>,
        dist: Option<u32>,
        rrf: Option<f64>,
    ) -> RetrievalCandidate {
        RetrievalCandidate {
            id: Uuid::new_v4(),
            vector_score: vector,
            text_score: text,
            graph_distance: dist,
            rrf_score: rrf,
        }
    }

    fn note_candidate(
        rrf: Option<f64>,
        salience: f64,
        decay_factor: f64,
        age_days: f64,
    ) -> NoteCandidate {
        // For tests, compute effective_salience using the default Exponential formula.
        let effective_salience = salience * (-decay_factor * age_days).exp();
        NoteCandidate {
            id: Uuid::new_v4(),
            rrf_score: rrf,
            salience,
            decay_factor,
            age_days,
            effective_salience,
            rerank_scores: HashMap::new(),
        }
    }

    // ── VectorSimilarityObjective ────────────────────────────────────────

    #[test]
    fn vector_present_returns_signal() {
        let c = candidate(Some(0.85), None, None, None);
        let score = VectorSimilarityObjective.score(&c, &ctx());
        assert!((score - 0.85).abs() < 1e-12);
    }

    #[test]
    fn vector_absent_returns_zero() {
        let c = candidate(None, None, None, None);
        assert_eq!(VectorSimilarityObjective.score(&c, &ctx()), 0.0);
    }

    #[test]
    fn vector_zero_score_returns_zero() {
        let c = candidate(Some(0.0), None, None, None);
        assert_eq!(VectorSimilarityObjective.score(&c, &ctx()), 0.0);
    }

    // ── TextRelevanceObjective ───────────────────────────────────────────

    #[test]
    fn text_present_returns_signal() {
        let c = candidate(None, Some(0.6), None, None);
        let score = TextRelevanceObjective.score(&c, &ctx());
        assert!((score - 0.6).abs() < 1e-12);
    }

    #[test]
    fn text_absent_returns_zero() {
        let c = candidate(None, None, None, None);
        assert_eq!(TextRelevanceObjective.score(&c, &ctx()), 0.0);
    }

    // ── GraphProximityObjective ──────────────────────────────────────────

    #[test]
    fn graph_anchor_hit_scores_one() {
        // d=0 → score = 1.0 − 0/max = 1.0
        let c = candidate(None, None, Some(0), None);
        let obj = GraphProximityObjective { max_distance: 3 };
        assert!((obj.score(&c, &ctx()) - 1.0).abs() < 1e-12);
    }

    #[test]
    fn graph_midpoint_scores_half() {
        // d=1, max=2 → score = 1.0 − 1/2 = 0.5
        let c = candidate(None, None, Some(1), None);
        let obj = GraphProximityObjective { max_distance: 2 };
        assert!((obj.score(&c, &ctx()) - 0.5).abs() < 1e-12);
    }

    #[test]
    fn graph_at_boundary_scores_zero() {
        // d == max_distance → score = 0.0 (boundary excluded)
        let c = candidate(None, None, Some(3), None);
        let obj = GraphProximityObjective { max_distance: 3 };
        assert_eq!(obj.score(&c, &ctx()), 0.0);
    }

    #[test]
    fn graph_beyond_boundary_scores_zero() {
        let c = candidate(None, None, Some(10), None);
        let obj = GraphProximityObjective { max_distance: 3 };
        assert_eq!(obj.score(&c, &ctx()), 0.0);
    }

    #[test]
    fn graph_absent_scores_zero() {
        let c = candidate(None, None, None, None);
        let obj = GraphProximityObjective { max_distance: 3 };
        assert_eq!(obj.score(&c, &ctx()), 0.0);
    }

    #[test]
    fn graph_max_distance_zero_always_scores_zero() {
        // max_distance=0 is degenerate; guard against divide-by-zero.
        let c = candidate(None, None, Some(0), None);
        let obj = GraphProximityObjective { max_distance: 0 };
        assert_eq!(obj.score(&c, &ctx()), 0.0);
    }

    // ── RrfFusionObjective ───────────────────────────────────────────────

    #[test]
    fn rrf_present_returns_signal() {
        let c = candidate(None, None, None, Some(0.0327));
        let score = RrfFusionObjective.score(&c, &ctx());
        assert!((score - 0.0327).abs() < 1e-12);
    }

    #[test]
    fn rrf_absent_returns_zero() {
        let c = candidate(None, None, None, None);
        assert_eq!(RrfFusionObjective.score(&c, &ctx()), 0.0);
    }

    // ── WeightedObjective composition ───────────────────────────────────

    #[test]
    fn weighted_composition_vector_and_text() {
        // Candidate with vector=0.8, text=0.6
        // Weighted(0.5*vector + 0.5*text) = 0.5*0.8 + 0.5*0.6 = 0.7
        let c = candidate(Some(0.8), Some(0.6), None, None);

        let obj = WeightedObjective::<RetrievalCandidate>::new()
            .add(Box::new(VectorSimilarityObjective), 0.5)
            .add(Box::new(TextRelevanceObjective), 0.5);

        let score = obj.score(&c, &ctx());
        // WeightedObjective divides by total weight (1.0), so result is 0.7
        assert!((score - 0.7).abs() < 1e-12);
    }

    #[test]
    fn weighted_composition_with_graph() {
        // vector=1.0, text=0.0, graph d=1/max=4 → proximity = 1 - 1/4 = 0.75
        // weights: vector=0.4, text=0.3, graph=0.3
        // weighted sum = (0.4*1.0 + 0.3*0.0 + 0.3*0.75) / 1.0 = 0.4 + 0.0 + 0.225 = 0.625
        let c = candidate(Some(1.0), Some(0.0), Some(1), None);

        let obj = WeightedObjective::<RetrievalCandidate>::new()
            .add(Box::new(VectorSimilarityObjective), 0.4)
            .add(Box::new(TextRelevanceObjective), 0.3)
            .add(Box::new(GraphProximityObjective { max_distance: 4 }), 0.3);

        let score = obj.score(&c, &ctx());
        assert!((score - 0.625).abs() < 1e-12);
    }

    #[test]
    fn weighted_all_absent_returns_zero() {
        let c = candidate(None, None, None, None);

        let obj = WeightedObjective::<RetrievalCandidate>::new()
            .add(Box::new(VectorSimilarityObjective), 0.5)
            .add(Box::new(TextRelevanceObjective), 0.5);

        // 0.0 * 0.5 + 0.0 * 0.5 = 0.0
        assert_eq!(obj.score(&c, &ctx()), 0.0);
    }

    // ── HasId ────────────────────────────────────────────────────────────

    #[test]
    fn has_id_returns_candidate_uuid() {
        let id = Uuid::new_v4();
        let c = RetrievalCandidate {
            id,
            vector_score: None,
            text_score: None,
            graph_distance: None,
            rrf_score: None,
        };
        assert_eq!(c.id(), id);
    }

    // ── select_top via DeterministicObjective ────────────────────────────

    #[test]
    fn select_top_orders_by_vector_score() {
        use khive_fold::DeterministicObjective;

        let candidates = vec![
            candidate(Some(0.3), None, None, None),
            candidate(Some(0.9), None, None, None),
            candidate(Some(0.6), None, None, None),
        ];

        let top = VectorSimilarityObjective.select_top_deterministic(&candidates, 2, &ctx());

        assert_eq!(top.len(), 2);
        assert!((top[0].score - 0.9).abs() < 1e-12);
        assert!((top[1].score - 0.6).abs() < 1e-12);
    }

    // ── NoteCandidate: HasId ─────────────────────────────────────────────

    #[test]
    fn note_candidate_has_id_returns_uuid() {
        let id = Uuid::new_v4();
        let c = NoteCandidate {
            id,
            rrf_score: None,
            salience: 0.5,
            decay_factor: 0.01,
            age_days: 0.0,
            effective_salience: 0.5,
            rerank_scores: HashMap::new(),
        };
        assert_eq!(c.id(), id);
    }

    // ── DecayAwareSalienceObjective ──────────────────────────────────────

    #[test]
    fn decay_aware_zero_age_returns_full_salience() {
        let obj = DecayAwareSalienceObjective::new(0.01);
        let c = note_candidate(None, 0.8, 0.01, 0.0);
        let score = obj.score(&c, &ctx());
        assert!((score - 0.8).abs() < 1e-12, "got {score}");
    }

    #[test]
    fn decay_aware_uses_note_decay_factor_not_field() {
        // uses the note's own decay_factor, not the objective's field
        let obj = DecayAwareSalienceObjective::new(0.99); // obj.decay_rate ignored
                                                          // Note's decay_factor = 0.01, age=100 days → exp(-0.01*100) ≈ 0.368
        let c = note_candidate(None, 1.0, 0.01, 100.0);
        let score = obj.score(&c, &ctx());
        let expected = (-0.01_f64 * 100.0).exp();
        assert!(
            (score - expected).abs() < 1e-12,
            "got {score}, expected {expected}"
        );
    }

    #[test]
    fn decay_aware_high_decay_reduces_score_faster() {
        // High decay note should score lower at same age
        let obj = DecayAwareSalienceObjective::new(0.0);
        let slow = note_candidate(None, 1.0, 0.001, 100.0);
        let fast = note_candidate(None, 1.0, 0.1, 100.0);
        let score_slow = obj.score(&slow, &ctx());
        let score_fast = obj.score(&fast, &ctx());
        assert!(
            score_slow > score_fast,
            "slow decay should score higher: {score_slow} vs {score_fast}"
        );
    }

    // ── TemporalRecencyObjective ─────────────────────────────────────────

    #[test]
    fn temporal_score_one_at_zero_age() {
        let obj = TemporalRecencyObjective {
            half_life_days: 30.0,
        };
        let c = note_candidate(None, 0.5, 0.01, 0.0);
        let score = obj.score(&c, &ctx());
        assert!((score - 1.0).abs() < 1e-12, "got {score}");
    }

    #[test]
    fn temporal_score_half_at_half_life() {
        let half_life = 30.0;
        let obj = TemporalRecencyObjective {
            half_life_days: half_life,
        };
        let c = note_candidate(None, 0.5, 0.01, half_life);
        let score = obj.score(&c, &ctx());
        assert!(
            (score - 0.5).abs() < 1e-10,
            "expected 0.5 at half_life, got {score}"
        );
    }

    #[test]
    fn temporal_score_decreases_with_age() {
        let obj = TemporalRecencyObjective {
            half_life_days: 30.0,
        };
        let young = note_candidate(None, 1.0, 0.01, 10.0);
        let old = note_candidate(None, 1.0, 0.01, 100.0);
        let score_young = obj.score(&young, &ctx());
        let score_old = obj.score(&old, &ctx());
        assert!(
            score_young > score_old,
            "younger note should score higher: {score_young} vs {score_old}"
        );
    }

    // ── RerankerObjective ────────────────────────────────────────────────

    #[test]
    fn reranker_returns_named_score() {
        let mut c = note_candidate(None, 0.5, 0.01, 0.0);
        c.rerank_scores.insert("cross_encoder".to_string(), 0.9);
        let obj = RerankerObjective::new("cross_encoder");
        let score = obj.score(&c, &ctx());
        assert!((score - 0.9).abs() < 1e-12, "got {score}");
    }

    #[test]
    fn reranker_absent_key_returns_zero() {
        let c = note_candidate(None, 0.5, 0.01, 0.0);
        let obj = RerankerObjective::new("cross_encoder");
        let score = obj.score(&c, &ctx());
        assert_eq!(score, 0.0);
    }

    #[test]
    fn reranker_different_keys_independent() {
        let mut c = note_candidate(None, 0.5, 0.01, 0.0);
        c.rerank_scores.insert("salience".to_string(), 0.7);
        let obj_ce = RerankerObjective::new("cross_encoder");
        let obj_sal = RerankerObjective::new("salience");
        assert_eq!(obj_ce.score(&c, &ctx()), 0.0);
        assert!((obj_sal.score(&c, &ctx()) - 0.7).abs() < 1e-12);
    }

    // ── Weighted composition of memory objectives ────────────────────────

    #[test]
    fn memory_pipeline_weighted_composition() {
        // Reproduce decay formula via WeightedObjective:
        // score = rrf * 0.70 + salience_decayed * 0.20 + temporal * 0.10
        // At age=0: salience_decayed = salience, temporal = 1.0
        let c = NoteCandidate {
            id: Uuid::new_v4(),
            rrf_score: Some(0.5),
            salience: 0.8,
            decay_factor: 0.01,
            age_days: 0.0,
            effective_salience: 0.8, // age=0, so effective_salience == salience
            rerank_scores: HashMap::new(),
        };
        let pipeline = WeightedObjective::<NoteCandidate>::new()
            .add(Box::new(RrfFusionObjective), 0.70)
            .add(Box::new(DecayAwareSalienceObjective::new(0.0)), 0.20)
            .add(
                Box::new(TemporalRecencyObjective {
                    half_life_days: 30.0,
                }),
                0.10,
            );
        let score = pipeline.score(&c, &ctx());
        // (0.7*0.5 + 0.2*0.8 + 0.1*1.0) / 1.0 = 0.35 + 0.16 + 0.10 = 0.61
        assert!((score - 0.61).abs() < 1e-10, "got {score}");
    }
}