use chrono::{DateTime, Utc};
use crate::types::MemoryRecord;
pub fn retrievability(record: &MemoryRecord, now: DateTime<Utc>) -> f64 {
let last_access = record.access_times.last().unwrap_or(&record.created_at);
let t_days = (now - *last_access).num_seconds() as f64 / 86400.0;
if t_days <= 0.0 {
return 1.0;
}
let s = compute_stability(record);
(-t_days / s).exp()
}
pub fn compute_stability(record: &MemoryRecord) -> f64 {
let base_decay = record.memory_type.default_decay_rate();
let base_s = 1.0 / base_decay;
let n_accesses = record.access_times.len() as f64;
let spacing_factor = 1.0 + 0.5 * (n_accesses + 1.0).ln();
let importance_factor = 0.5 + record.importance;
let consolidation_factor = 1.0 + 0.2 * record.consolidation_count as f64;
base_s * spacing_factor * importance_factor * consolidation_factor
}
pub fn effective_strength(record: &MemoryRecord, now: DateTime<Utc>) -> f64 {
let r = retrievability(record, now);
let trace_strength = record.working_strength + record.core_strength;
trace_strength * r
}
pub fn should_forget(record: &MemoryRecord, threshold: f64, now: DateTime<Utc>) -> bool {
if record.pinned {
return false;
}
effective_strength(record, now) < threshold
}