khive-runtime 0.13.0

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
//! 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;
use khive_score::Decay;

/// 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 to score and rank candidates via
/// `AmplifiedDecayAwareSalienceObjective`, `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 ──────────────────────────────────────────────

#[inline]
fn exponential_decay(value: f64, rate: f64, age_days: f64) -> f64 {
    // Preserve unchecked public candidates outside the shared decay domain.
    match Decay::exponential(rate).and_then(|decay| decay.apply(value, age_days)) {
        Ok(score) if age_days >= 0.0 => score,
        _ => value * (-rate * age_days).exp(),
    }
}

/// Scores a `NoteCandidate` by salience with configurable temporal decay.
///
/// Uses the fixed exponential rate supplied at construction:
/// `salience * exp(-decay_rate * age_days)`. It does not read the candidate's
/// per-note `decay_factor`. The memory recall pipeline applies its configured
/// `DecayModel` before scoring and uses `AmplifiedDecayAwareSalienceObjective`
/// over the resulting `effective_salience` instead.
///
/// This objective remains useful for callers that want one decay policy across
/// every candidate in a `WeightedObjective` composition.
pub struct DecayAwareSalienceObjective {
    decay_rate: f64,
}

impl DecayAwareSalienceObjective {
    /// Create a new objective with the given exponential decay rate. Panics if
    /// `decay_rate` is negative or non-finite.
    ///
    /// `decay_rate = 0.01` gives a ~69-day half-life (default for memory notes).
    pub fn new(decay_rate: f64) -> Self {
        assert!(
            decay_rate.is_finite() && decay_rate >= 0.0,
            "decay_rate must be finite and non-negative, got {decay_rate}"
        );
        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 {
        exponential_decay(candidate.salience, self.decay_rate, candidate.age_days)
    }

    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 {
        // effective_salience is pre-computed via the caller's DecayModel, so this
        // works for all decay model variants, not just 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);
        exponential_decay(1.0, k, candidate.age_days)
    }

    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)
    }
}

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

// Kept inline: these tests exercise internal NoteCandidate fields that would
// otherwise need to be made pub just to reach them from tests/.
#[cfg(test)]
#[path = "objectives_tests.rs"]
mod tests;