feedsignal/crates/core/src/affinity.rs

138 lines
5 KiB
Rust
Raw Normal View History

use serde::{Deserialize, Serialize};
use std::collections::HashMap;
/// Per-topic affinity scores, in `[-1.0, 1.0]`, updated from observed
/// engagement vs. predicted relevance. Persisted as a single row per user
/// (JSON blob) in `feedsignal-db`; the event log remains the source of
/// truth and this can always be rebuilt by replaying it.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct TopicAffinities {
scores: HashMap<String, f64>,
}
/// How much a single feedback event moves an affinity score. Kept small so
/// no single article dominates a topic's long-run trend.
const LEARNING_RATE: f64 = 0.15;
/// Fraction of every affinity pulled back toward zero on each nightly decay
/// pass, so stale interests fade instead of anchoring the model forever.
const DAILY_DECAY: f64 = 0.02;
impl TopicAffinities {
pub fn score(&self, topic: &str) -> f64 {
self.scores.get(topic).copied().unwrap_or(0.0)
}
/// Mean affinity across an article's topics; 0.0 for an untagged
/// article (neutral, defers entirely to the embedding/LLM stages).
pub fn score_topics(&self, topics: &[String]) -> f64 {
if topics.is_empty() {
return 0.0;
}
topics.iter().map(|t| self.score(t)).sum::<f64>() / topics.len() as f64
}
/// Update affinities for an article's topics from an observed
/// `surprise`: `engagement_score - predicted_relevance_score`, both in
/// `[0.0, 1.0]` (see `scoring::engagement_score`). Positive surprise
/// (the user engaged more than the pipeline predicted) nudges those
/// topics up; negative surprise nudges them down. This is what lets
/// "the model thought this was irrelevant but I read the whole thing"
/// actually change future behavior.
pub fn apply_feedback(&mut self, topics: &[String], surprise: f64) {
for topic in topics {
let current = self.score(topic);
let updated = (current + LEARNING_RATE * surprise).clamp(-1.0, 1.0);
self.scores.insert(topic.clone(), updated);
}
}
/// Run once per day (see the scheduler in `feedsignal-web`) to let
/// affinities the user hasn't reinforced recently drift back toward
/// neutral rather than staying permanently pinned from a few old
/// signals.
pub fn decay(&mut self) {
self.scores.retain(|_, v| v.abs() > 1e-4);
for v in self.scores.values_mut() {
*v -= v.signum() * DAILY_DECAY;
}
}
pub fn top_n(&self, n: usize) -> Vec<(&str, f64)> {
let mut items: Vec<_> = self.scores.iter().map(|(k, v)| (k.as_str(), *v)).collect();
items.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap());
items.truncate(n);
items
}
}
/// Converts a raw reading interaction into an engagement score in
/// `[0.0, 1.0]`, and folds in the explicit star/dismiss signal.
///
/// - Never opened: 0.0
/// - Opened: `dwell_seconds / estimated_read_seconds`, capped at 1.0, so
/// skimming half an article scores lower than reading it fully.
/// - Starred: +0.3 on top (capped at 1.0) — an explicit "yes" beyond dwell
/// time alone.
/// - Dismissed without opening: -0.3, floored at 0.0's negative counterpart
/// handled by the caller via `surprise` (engagement itself never goes
/// negative; the *deviation* from a predicted score can).
pub fn engagement_score(
opened: bool,
dwell_seconds: Option<u32>,
estimated_read_seconds: Option<u32>,
starred: bool,
dismissed: bool,
) -> f64 {
if dismissed && !opened {
return 0.0;
}
let mut score = if !opened {
0.0
} else {
match (dwell_seconds, estimated_read_seconds) {
(Some(dwell), Some(est)) if est > 0 => (dwell as f64 / est as f64).min(1.0),
// Opened but we don't yet know dwell time / read-time estimate:
// credit partial engagement rather than 0 or 1.
_ => 0.5,
}
};
if starred {
score = (score + 0.3).min(1.0);
}
score
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn under_predicted_relevance_boosts_topic() {
let mut aff = TopicAffinities::default();
let topics = vec!["rust".to_string()];
// Model predicted 0.2 relevance, user fully read it: surprise = 0.8.
aff.apply_feedback(&topics, 0.8);
assert!(aff.score("rust") > 0.0);
}
#[test]
fn over_predicted_relevance_lowers_topic() {
let mut aff = TopicAffinities::default();
let topics = vec!["crypto".to_string()];
// Model predicted 0.9, user dismissed unread: engagement 0, surprise = -0.9.
aff.apply_feedback(&topics, -0.9);
assert!(aff.score("crypto") < 0.0);
}
#[test]
fn decay_pulls_toward_zero() {
let mut aff = TopicAffinities::default();
aff.apply_feedback(&["rust".to_string()], 1.0);
let before = aff.score("rust");
aff.decay();
assert!(aff.score("rust") < before);
assert!(aff.score("rust") > 0.0);
}
}