use skrills_snapshot::{Hint, ScoredHint};
pub const FREQUENCY_WEIGHT: f64 = 2.0;
pub const IMPACT_WEIGHT: f64 = 1.5;
pub const HALF_LIFE_DAYS: f64 = 14.0;
#[derive(Debug, Clone, Copy)]
pub struct MultiSignalScorer {
pub(crate) frequency_weight: f64,
pub(crate) impact_weight: f64,
pub(crate) half_life_days: f64,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ScorerError {
NaNValue,
Negative,
ZeroHalfLife,
}
impl core::fmt::Display for ScorerError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::NaNValue => write!(f, "MultiSignalScorer: weight may not be NaN"),
Self::Negative => write!(f, "MultiSignalScorer: weight may not be negative"),
Self::ZeroHalfLife => write!(f, "MultiSignalScorer: half_life_days must be > 0"),
}
}
}
impl std::error::Error for ScorerError {}
fn validate_weight(value: f64) -> Result<f64, ScorerError> {
if value.is_nan() {
return Err(ScorerError::NaNValue);
}
if value < 0.0 {
return Err(ScorerError::Negative);
}
Ok(value)
}
fn validate_half_life(value: f64) -> Result<f64, ScorerError> {
let v = validate_weight(value)?;
if v == 0.0 {
return Err(ScorerError::ZeroHalfLife);
}
Ok(v)
}
impl MultiSignalScorer {
pub fn new() -> Self {
Self {
frequency_weight: FREQUENCY_WEIGHT,
impact_weight: IMPACT_WEIGHT,
half_life_days: HALF_LIFE_DAYS,
}
}
#[must_use]
pub fn frequency_weight(&self) -> f64 {
self.frequency_weight
}
#[must_use]
pub fn impact_weight(&self) -> f64 {
self.impact_weight
}
#[must_use]
pub fn half_life_days(&self) -> f64 {
self.half_life_days
}
pub fn try_with_frequency_weight(mut self, w: f64) -> Result<Self, ScorerError> {
self.frequency_weight = validate_weight(w)?;
Ok(self)
}
pub fn try_with_impact_weight(mut self, w: f64) -> Result<Self, ScorerError> {
self.impact_weight = validate_weight(w)?;
Ok(self)
}
pub fn try_with_half_life_days(mut self, d: f64) -> Result<Self, ScorerError> {
self.half_life_days = validate_half_life(d)?;
Ok(self)
}
pub fn with_frequency_weight(self, w: f64) -> Self {
self.try_with_frequency_weight(w)
.expect("with_frequency_weight: invalid weight; use try_* in non-test code")
}
pub fn with_impact_weight(self, w: f64) -> Self {
self.try_with_impact_weight(w)
.expect("with_impact_weight: invalid weight; use try_* in non-test code")
}
pub fn with_half_life_days(self, d: f64) -> Self {
self.try_with_half_life_days(d)
.expect("with_half_life_days: invalid value; use try_* in non-test code")
}
pub fn score_one(&self, hint: &Hint) -> f64 {
let numerator =
(hint.frequency as f64) * self.frequency_weight + hint.impact * self.impact_weight;
let denominator = hint.ease_score + 1.0;
let recency = (-hint.age_days / self.half_life_days.max(f64::MIN_POSITIVE)).exp();
(numerator / denominator) * recency
}
pub fn rank(&self, hints: Vec<Hint>) -> Vec<ScoredHint> {
self.rank_with_pins(hints, |_| false)
}
pub fn rank_with_pins<F>(&self, hints: Vec<Hint>, is_pinned: F) -> Vec<ScoredHint>
where
F: Fn(&Hint) -> bool,
{
let mut scored: Vec<ScoredHint> = hints
.into_iter()
.map(|hint| {
let pinned = is_pinned(&hint);
let score = self.score_one(&hint);
ScoredHint {
hint,
score,
pinned,
}
})
.collect();
scored.sort_by(|a, b| {
b.pinned.cmp(&a.pinned).then_with(|| {
b.score
.partial_cmp(&a.score)
.unwrap_or(std::cmp::Ordering::Equal)
})
});
scored
}
}
impl Default for MultiSignalScorer {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
use skrills_snapshot::HintCategory;
fn hint(uri: &str, frequency: u32, impact: f64, ease: f64, age: f64) -> Hint {
Hint {
uri: uri.to_string(),
category: HintCategory::Token,
message: "test".into(),
frequency,
impact,
ease_score: ease,
age_days: age,
}
}
#[test]
fn defaults_match_spec() {
let s = MultiSignalScorer::new();
assert_eq!(s.frequency_weight(), 2.0);
assert_eq!(s.impact_weight(), 1.5);
assert_eq!(s.half_life_days(), 14.0);
}
#[test]
fn try_with_frequency_weight_rejects_nan() {
assert_eq!(
MultiSignalScorer::new()
.try_with_frequency_weight(f64::NAN)
.unwrap_err(),
ScorerError::NaNValue
);
}
#[test]
fn try_with_impact_weight_rejects_negative() {
assert_eq!(
MultiSignalScorer::new()
.try_with_impact_weight(-1.0)
.unwrap_err(),
ScorerError::Negative
);
}
#[test]
fn try_with_half_life_days_rejects_zero() {
assert_eq!(
MultiSignalScorer::new()
.try_with_half_life_days(0.0)
.unwrap_err(),
ScorerError::ZeroHalfLife
);
}
#[test]
fn try_with_half_life_days_rejects_nan_and_negative() {
assert_eq!(
MultiSignalScorer::new()
.try_with_half_life_days(f64::NAN)
.unwrap_err(),
ScorerError::NaNValue
);
assert_eq!(
MultiSignalScorer::new()
.try_with_half_life_days(-7.0)
.unwrap_err(),
ScorerError::Negative
);
}
#[test]
fn try_with_frequency_weight_accepts_zero_and_positive() {
let z = MultiSignalScorer::new()
.try_with_frequency_weight(0.0)
.unwrap();
assert_eq!(z.frequency_weight(), 0.0);
let p = MultiSignalScorer::new()
.try_with_frequency_weight(7.5)
.unwrap();
assert_eq!(p.frequency_weight(), 7.5);
}
#[test]
fn higher_frequency_outranks_lower_when_others_equal() {
let s = MultiSignalScorer::new();
let h_low = hint("low", 1, 5.0, 5.0, 0.0);
let h_hi = hint("hi", 10, 5.0, 5.0, 0.0);
assert!(s.score_one(&h_hi) > s.score_one(&h_low));
}
#[test]
fn higher_impact_outranks_lower_when_others_equal() {
let s = MultiSignalScorer::new();
let h_low = hint("low", 5, 1.0, 5.0, 0.0);
let h_hi = hint("hi", 5, 9.0, 5.0, 0.0);
assert!(s.score_one(&h_hi) > s.score_one(&h_low));
}
#[test]
fn higher_ease_lowers_priority() {
let s = MultiSignalScorer::new();
let h_hard = hint("hard", 5, 5.0, 1.0, 0.0);
let h_easy = hint("easy", 5, 5.0, 9.0, 0.0);
assert!(s.score_one(&h_easy) < s.score_one(&h_hard));
}
#[test]
fn newer_hint_outranks_older_when_others_equal() {
let s = MultiSignalScorer::new();
let h_old = hint("old", 5, 5.0, 5.0, 30.0);
let h_new = hint("new", 5, 5.0, 5.0, 0.0);
assert!(s.score_one(&h_new) > s.score_one(&h_old));
}
#[test]
fn pinned_hint_outranks_any_unpinned_in_ranking() {
let s = MultiSignalScorer::new();
let weak = hint("weak", 1, 0.1, 9.0, 60.0);
let strong = hint("strong", 100, 10.0, 1.0, 0.0);
let ranked = s.rank_with_pins(vec![strong, weak], |h| h.uri == "weak");
assert_eq!(ranked[0].hint.uri, "weak");
assert!(ranked[0].pinned);
assert!(!ranked[1].pinned);
}
#[test]
fn rank_sorts_descending_by_score() {
let s = MultiSignalScorer::new();
let hints = vec![
hint("a", 1, 1.0, 5.0, 0.0),
hint("b", 10, 9.0, 1.0, 0.0),
hint("c", 5, 5.0, 5.0, 0.0),
];
let ranked = s.rank(hints);
assert!(ranked[0].score >= ranked[1].score);
assert!(ranked[1].score >= ranked[2].score);
assert_eq!(ranked[0].hint.uri, "b");
}
#[test]
fn rank_with_pins_uses_predicate() {
let s = MultiSignalScorer::new();
let hints = vec![
hint("plain", 100, 10.0, 1.0, 0.0),
hint("pinned", 1, 0.1, 9.0, 60.0),
];
let ranked = s.rank_with_pins(hints, |h| h.uri == "pinned");
assert_eq!(ranked[0].hint.uri, "pinned");
assert!(ranked[0].pinned);
assert!(!ranked[1].pinned);
}
#[test]
fn empty_hint_list_yields_empty_ranking() {
let s = MultiSignalScorer::new();
let ranked = s.rank(vec![]);
assert!(ranked.is_empty());
}
#[test]
fn custom_weights_change_ranking_predictably() {
let s = MultiSignalScorer::new()
.with_frequency_weight(0.1)
.with_impact_weight(10.0);
let h_freq = hint("freq", 100, 1.0, 5.0, 0.0);
let h_imp = hint("imp", 1, 9.0, 5.0, 0.0);
let ranked = s.rank(vec![h_freq, h_imp]);
assert_eq!(ranked[0].hint.uri, "imp");
}
#[test]
fn with_frequency_weight_doubles_frequency_contribution() {
let base = MultiSignalScorer::new().with_impact_weight(0.0);
let doubled = MultiSignalScorer::new()
.with_impact_weight(0.0)
.with_frequency_weight(FREQUENCY_WEIGHT * 2.0);
let h = hint("h", 5, 0.0, 5.0, 0.0);
let s_base = base.score_one(&h);
let s_doubled = doubled.score_one(&h);
assert!((s_doubled - 2.0 * s_base).abs() < 1e-9);
}
#[test]
fn with_impact_weight_doubles_impact_contribution() {
let base = MultiSignalScorer::new().with_frequency_weight(0.0);
let doubled = MultiSignalScorer::new()
.with_frequency_weight(0.0)
.with_impact_weight(IMPACT_WEIGHT * 2.0);
let h = hint("h", 0, 4.0, 5.0, 0.0);
let s_base = base.score_one(&h);
let s_doubled = doubled.score_one(&h);
assert!((s_doubled - 2.0 * s_base).abs() < 1e-9);
}
#[test]
fn recency_decay_is_exponential() {
let s = MultiSignalScorer::new();
let h_now = hint("now", 5, 5.0, 5.0, 0.0);
let h_one_halflife = hint("h1", 5, 5.0, 5.0, 14.0);
let s_now = s.score_one(&h_now);
let s_decayed = s.score_one(&h_one_halflife);
let ratio = s_decayed / s_now;
assert!((ratio - (-1.0_f64).exp()).abs() < 1e-9);
}
#[test]
fn ranking_ties_broken_stably_by_score_within_pin_group() {
let s = MultiSignalScorer::new();
let pinned_strong = hint("ps", 50, 8.0, 2.0, 0.0);
let pinned_weak = hint("pw", 5, 1.0, 9.0, 0.0);
let unpinned_strong = hint("us", 100, 10.0, 1.0, 0.0);
let unpinned_weak = hint("uw", 1, 0.5, 9.0, 60.0);
let ranked = s.rank_with_pins(
vec![unpinned_weak, pinned_weak, unpinned_strong, pinned_strong],
|h| h.uri.starts_with('p'),
);
assert_eq!(ranked[0].hint.uri, "ps");
assert_eq!(ranked[1].hint.uri, "pw");
assert_eq!(ranked[2].hint.uri, "us");
assert_eq!(ranked[3].hint.uri, "uw");
}
}