khive-score 0.2.9

Deterministic fixed-point scoring: cross-platform ordering, aggregation (sum/avg/max/min/RRF), ranking with ID tiebreak.
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
//! Aggregation and fusion operations for deterministic scores.

use crate::DeterministicScore;
use std::fmt;
use std::num::NonZeroUsize;

/// Errors produced by score aggregation and distance conversion operations.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ScoreError {
    /// The two input slices have different lengths.
    LengthMismatch {
        /// Human-readable description of which two slices were compared.
        expected_desc: &'static str,
        /// Length of the first slice.
        first_len: usize,
        /// Length of the second slice.
        second_len: usize,
    },
    /// A weight at the given index is NaN or infinite.
    NonFiniteWeight {
        /// Zero-based index of the offending weight.
        index: usize,
    },
    /// The distance value is NaN, `+Inf`, or `-Inf`.
    NonFiniteDistance,
    /// The distance is finite but outside the valid range for the metric.
    InvalidDistanceRange {
        /// The metric whose range was violated.
        metric_name: &'static str,
        /// Bit-representation of the out-of-range distance for diagnostics.
        dist_bits: u32,
    },
    /// The distance metric is not one of the three currently supported variants.
    UnsupportedMetric,
}

impl fmt::Display for ScoreError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ScoreError::LengthMismatch {
                expected_desc,
                first_len,
                second_len,
            } => write!(
                f,
                "{expected_desc}: first has {first_len} elements, second has {second_len}"
            ),
            ScoreError::NonFiniteWeight { index } => {
                write!(f, "weight at index {index} must be finite")
            }
            ScoreError::NonFiniteDistance => {
                write!(f, "distance must be finite (not NaN or infinity)")
            }
            ScoreError::InvalidDistanceRange {
                metric_name,
                dist_bits,
            } => write!(
                f,
                "distance value (bits=0x{dist_bits:08x}) is out of valid range for metric {metric_name}"
            ),
            ScoreError::UnsupportedMetric => {
                write!(f, "unsupported distance metric")
            }
        }
    }
}

impl std::error::Error for ScoreError {}

/// Return the saturating sum of `scores`, clamped to `[NEG_INF, MAX]`.
#[inline]
pub fn sum_scores(scores: &[DeterministicScore]) -> DeterministicScore {
    if scores.is_empty() {
        return DeterministicScore::ZERO;
    }
    let sum: i128 = scores.iter().map(|s| s.to_raw() as i128).sum();
    DeterministicScore::from_raw(sum.clamp(
        DeterministicScore::NEG_INF.to_raw() as i128,
        i64::MAX as i128,
    ) as i64)
}

/// Return the arithmetic mean of `scores`, clamped to `[NEG_INF, MAX]`.
#[inline]
pub fn avg_scores(scores: &[DeterministicScore]) -> DeterministicScore {
    if scores.is_empty() {
        return DeterministicScore::ZERO;
    }
    let sum: i128 = scores.iter().map(|s| s.to_raw() as i128).sum();
    let mean = sum / scores.len() as i128;
    DeterministicScore::from_raw(mean.clamp(
        DeterministicScore::NEG_INF.to_raw() as i128,
        i64::MAX as i128,
    ) as i64)
}

/// Return the mean of `scores` and a boolean saturation flag.
#[inline]
pub fn avg_scores_checked(scores: &[DeterministicScore]) -> (DeterministicScore, bool) {
    if scores.is_empty() {
        return (DeterministicScore::ZERO, false);
    }
    const SATURATION_THRESHOLD: i128 = (i64::MAX as i128) * 9 / 10;
    let sum: i128 = scores.iter().map(|s| s.to_raw() as i128).sum();
    let mean = sum / scores.len() as i128;
    // Use order-independent measures: check the absolute sum of all input
    // magnitudes (independent of sign cancellation order) and the final mean.
    let abs_mass: i128 = scores
        .iter()
        .map(|s| (s.to_raw() as i128).unsigned_abs() as i128)
        .sum();
    let near_saturation =
        abs_mass > SATURATION_THRESHOLD || mean.unsigned_abs() as i128 > SATURATION_THRESHOLD;
    let result = DeterministicScore::from_raw(mean.clamp(
        DeterministicScore::NEG_INF.to_raw() as i128,
        i64::MAX as i128,
    ) as i64);
    (result, near_saturation)
}

/// Return the maximum score, or [`DeterministicScore::NEG_INF`] for an empty slice.
#[inline]
pub fn max_score(scores: &[DeterministicScore]) -> DeterministicScore {
    scores
        .iter()
        .copied()
        .max()
        .unwrap_or(DeterministicScore::NEG_INF)
}

/// Return the minimum score, or [`DeterministicScore::MAX`] for an empty slice.
#[inline]
pub fn min_score(scores: &[DeterministicScore]) -> DeterministicScore {
    scores
        .iter()
        .copied()
        .min()
        .unwrap_or(DeterministicScore::MAX)
}

/// RRF score `1 / (k + rank)`. Rank is 1-based; prefer `rrf_score_one_based` or `rrf_score_zero_based`.
#[inline]
pub fn rrf_score(rank: usize, k: usize) -> DeterministicScore {
    let Some(denominator) = k.checked_add(rank) else {
        return DeterministicScore::ZERO;
    };
    if denominator == 0 {
        return DeterministicScore::ZERO;
    }
    DeterministicScore::from_f64(1.0 / (denominator as f64))
}

/// RRF score with 1-based rank (first result = rank 1). `k` is the smoothing constant.
#[inline]
pub fn rrf_score_one_based(rank: NonZeroUsize, k: usize) -> DeterministicScore {
    let Some(denominator) = k.checked_add(rank.get()) else {
        return DeterministicScore::ZERO;
    };
    DeterministicScore::from_f64(1.0 / denominator as f64)
}

/// RRF score with 0-based index (index 0 → rank 1 internally).
#[inline]
pub fn rrf_score_zero_based(index: usize, k: usize) -> DeterministicScore {
    let Some(rank) = index.checked_add(1).and_then(NonZeroUsize::new) else {
        return DeterministicScore::ZERO;
    };
    rrf_score_one_based(rank, k)
}

const SCALE_RAW: i128 = 4_294_967_296; // 2^32 — matches DeterministicScore::SCALE

/// Weighted sum of `scores`. Errors on length mismatch or non-finite weights.
#[inline]
pub fn weighted_sum(
    scores: &[DeterministicScore],
    weights: &[f64],
) -> Result<DeterministicScore, ScoreError> {
    if scores.len() != weights.len() {
        return Err(ScoreError::LengthMismatch {
            expected_desc: "scores and weights must have same length",
            first_len: scores.len(),
            second_len: weights.len(),
        });
    }
    let mut acc = 0i128;
    for (index, (&score, &weight)) in scores.iter().zip(weights.iter()).enumerate() {
        if !weight.is_finite() {
            return Err(ScoreError::NonFiniteWeight { index });
        }
        let w = DeterministicScore::from_f64(weight);
        acc += (score.to_raw() as i128 * w.to_raw() as i128) / SCALE_RAW;
    }
    Ok(DeterministicScore::from_raw(acc.clamp(
        DeterministicScore::NEG_INF.to_raw() as i128,
        DeterministicScore::MAX.to_raw() as i128,
    ) as i64))
}

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

    fn s(v: f64) -> DeterministicScore {
        DeterministicScore::from_f64(v)
    }

    #[test]
    fn sum_basic() {
        let scores = [s(0.1), s(0.2), s(0.3)];
        let result = sum_scores(&scores);
        assert!((result.to_f64() - 0.6).abs() < 1e-9);
    }

    #[test]
    fn sum_empty() {
        let result = sum_scores(&[]);
        assert_eq!(result, DeterministicScore::ZERO);
    }

    #[test]
    fn avg_basic() {
        let scores = [s(0.1), s(0.2), s(0.3)];
        let result = avg_scores(&scores);
        assert!((result.to_f64() - 0.2).abs() < 1e-9);
    }

    #[test]
    fn rrf_basic() {
        let r1 = rrf_score(1, 60);
        let r2 = rrf_score(2, 60);
        assert!(r1 > r2);
        assert!((r1.to_f64() - 1.0 / 61.0).abs() < 1e-9);
    }

    #[test]
    fn weighted_sum_basic() {
        let scores = [s(0.5), s(1.0)];
        let weights = [0.4, 0.6];
        let result = weighted_sum(&scores, &weights).unwrap();
        assert!((result.to_f64() - 0.8).abs() < 1e-6);
    }

    #[test]
    fn weighted_sum_length_mismatch() {
        let err = weighted_sum(&[s(0.1)], &[0.5, 0.5]).unwrap_err();
        assert!(matches!(err, ScoreError::LengthMismatch { .. }));
    }

    #[test]
    fn weighted_sum_rejects_nan() {
        let err = weighted_sum(&[s(0.1)], &[f64::NAN]).unwrap_err();
        assert!(matches!(err, ScoreError::NonFiniteWeight { index: 0 }));
    }

    #[test]
    fn sum_negative_saturation_clamps_to_neg_inf() {
        let big_neg = DeterministicScore::NEG_INF;
        let result = sum_scores(&[big_neg, big_neg, big_neg]);
        assert_eq!(result, DeterministicScore::NEG_INF);
        assert!(result.is_infinite());
        assert_eq!(result.to_f64(), f64::NEG_INFINITY);
    }

    #[test]
    fn avg_negative_saturation_clamps_to_neg_inf() {
        let big_neg = DeterministicScore::NEG_INF;
        let result = avg_scores(&[big_neg, big_neg]);
        assert_eq!(result, DeterministicScore::NEG_INF);
    }

    #[test]
    fn sum_order_independent() {
        let a = DeterministicScore::from_f64(1e9);
        let b = DeterministicScore::from_f64(-1e9);
        let c = DeterministicScore::from_f64(0.5);
        let r1 = sum_scores(&[a, b, c]);
        let r2 = sum_scores(&[c, a, b]);
        let r3 = sum_scores(&[b, c, a]);
        assert_eq!(r1, r2);
        assert_eq!(r2, r3);
    }

    #[test]
    fn avg_scores_checked_empty_returns_zero_no_flag() {
        let (mean, flag) = avg_scores_checked(&[]);
        assert_eq!(mean, DeterministicScore::ZERO);
        assert!(!flag);
    }

    #[test]
    fn avg_scores_checked_near_saturation_sets_flag() {
        let (_, flag) = avg_scores_checked(&[DeterministicScore::MAX, DeterministicScore::MAX]);
        assert!(flag);
    }

    #[test]
    fn max_score_empty_returns_neg_inf() {
        assert_eq!(max_score(&[]), DeterministicScore::NEG_INF);
    }

    #[test]
    fn min_score_empty_returns_max() {
        assert_eq!(min_score(&[]), DeterministicScore::MAX);
    }

    #[test]
    fn rrf_score_zero_denominator_returns_zero() {
        assert_eq!(rrf_score(0, 0), DeterministicScore::ZERO);
    }

    #[test]
    fn rrf_score_overflow_returns_zero() {
        assert_eq!(rrf_score(usize::MAX, 1), DeterministicScore::ZERO);
    }

    #[test]
    fn weighted_sum_empty_returns_zero() {
        assert_eq!(weighted_sum(&[], &[]).unwrap(), DeterministicScore::ZERO);
    }

    #[test]
    fn weighted_sum_rejects_infinite_weight() {
        let err = weighted_sum(&[s(1.0)], &[f64::INFINITY]).unwrap_err();
        assert_eq!(err, ScoreError::NonFiniteWeight { index: 0 });
    }

    // ── rrf_score_one_based / rrf_score_zero_based ────────────────────────────

    #[test]
    fn rrf_one_based_rank_1_equals_legacy_rank_1() {
        use std::num::NonZeroUsize;
        let one_based = rrf_score_one_based(NonZeroUsize::new(1).unwrap(), 60);
        let legacy = rrf_score(1, 60);
        assert_eq!(
            one_based, legacy,
            "rrf_score_one_based(1,60) must match rrf_score(1,60)"
        );
    }

    #[test]
    fn rrf_zero_based_index_0_equals_one_based_rank_1() {
        use std::num::NonZeroUsize;
        let zero_based = rrf_score_zero_based(0, 60);
        let one_based = rrf_score_one_based(NonZeroUsize::new(1).unwrap(), 60);
        assert_eq!(zero_based, one_based);
    }

    #[test]
    fn rrf_one_based_monotone_decreasing() {
        use std::num::NonZeroUsize;
        let r1 = rrf_score_one_based(NonZeroUsize::new(1).unwrap(), 60);
        let r2 = rrf_score_one_based(NonZeroUsize::new(2).unwrap(), 60);
        let r10 = rrf_score_one_based(NonZeroUsize::new(10).unwrap(), 60);
        assert!(r1 > r2);
        assert!(r2 > r10);
    }

    #[test]
    fn rrf_one_based_value_matches_formula() {
        use std::num::NonZeroUsize;
        let score = rrf_score_one_based(NonZeroUsize::new(1).unwrap(), 60);
        assert!((score.to_f64() - 1.0 / 61.0).abs() < 1e-9);
    }

    #[test]
    fn rrf_one_based_overflow_returns_zero() {
        use std::num::NonZeroUsize;
        let score = rrf_score_one_based(NonZeroUsize::new(usize::MAX).unwrap(), 1);
        assert_eq!(score, DeterministicScore::ZERO);
    }

    #[test]
    fn rrf_zero_based_overflow_returns_zero() {
        let score = rrf_score_zero_based(usize::MAX, 1);
        assert_eq!(score, DeterministicScore::ZERO);
    }

    // ── avg_scores_checked order-independence ─────────────────────────────────

    #[test]
    fn avg_scores_checked_saturation_flag_is_order_independent() {
        // Build two orderings of the same multiset.
        // The near_saturation flag must be the same regardless of order.
        let big = DeterministicScore::from_raw(i64::MAX / 2);
        let neg = DeterministicScore::from_raw(i64::MIN / 2 + 1);
        let scores_order_a = [big, neg, big, neg];
        let scores_order_b = [big, big, neg, neg];
        let (_, flag_a) = avg_scores_checked(&scores_order_a);
        let (_, flag_b) = avg_scores_checked(&scores_order_b);
        assert_eq!(
            flag_a, flag_b,
            "near_saturation flag must be order-independent: order_a={flag_a}, order_b={flag_b}"
        );
    }

    #[test]
    fn avg_scores_checked_normal_values_no_saturation_flag() {
        let scores = [s(0.1), s(0.2), s(-0.1), s(0.3)];
        let (_, flag) = avg_scores_checked(&scores);
        assert!(!flag, "small scores should not trigger near_saturation");
    }
}