khive-score
Deterministic fixed-point scoring: f64 relevance scores are converted to a
2^32-scaled i64 (DeterministicScore) so ranking, aggregation, and RRF
fusion give bit-identical results across platforms. Also provides sum/avg/min/max
aggregation, weighted sum, distance-to-score conversion, and ID-tiebreak comparators.
Usage
use ;
use NonZeroUsize;
let a = from_f64;
let b = from_f64;
assert!;
// Reciprocal Rank Fusion: 1-based rank, k=60 smoothing constant.
let fused = rrf_score_one_based;
assert!;
// Sort candidates by score descending, lower ID wins ties.
let mut hits = vec!;
hits.sort_by;
Semantics
DeterministicScoresaturates atMAX(i64::MAX) andNEG_INF(i64::MIN + 1) rather than overflowing.MIN(i64::MIN) is a reserved sentinel never produced by arithmetic,from_f64, orto_f64— onlyfrom_rawcan construct it, and theserdeDeserializeimpl rejects it.NaNinputs map toZERO;±Infinitymap toMAX/NEG_INF.sum_scores,avg_scores, andweighted_sumclamp their result to[NEG_INF, MAX]and are order-independent regardless of input ordering.try_score_from_distanceconverts a raw distance plus akhive_types::DistanceMetric(Cosine,Dot,L2) into a score, erroring on non-finite or out-of-range input;score_from_distance_lossyis the infallible variant (NEG_INFon error). The un-prefixedscore_from_distanceis deprecated since 0.2.3 — it silently mapsNaNto a perfect score.Ranked<T: Ord>is aBinaryHeap-ready max-heap adapter (higher score pops first, lower ID breaks ties). ItsOrdimpl is not a plain ascending sort order — usecmp_desc_then_id/cmp_asc_then_idwhen callingsort()directly on aVec.
Where this sits
Built on khive-types (for DistanceMetric). Consumed by every crate that
needs cross-platform-identical ranking:
khive-bm25,
khive-hnsw,
khive-fusion,
khive-fold,
khive-storage,
khive-retrieval,
khive-db, and
khive-runtime.
Deterministic scoring is required so that two nodes evaluating the same query
produce the same ranked order — see
ADR-006.
License
Apache-2.0.