Skip to main content

oxibrain_core/
lifecycle.rs

1//! Lifecycle types: salience decay, compaction config (DESIGN ยง10).
2
3use oxibrain_ports::Timestamp;
4use serde::{Deserialize, Serialize};
5
6#[derive(Debug, Clone, Serialize, Deserialize)]
7pub struct DecayConfig {
8    pub base: f64,
9    pub lambda: f64,
10    pub floor: f64,
11}
12
13impl Default for DecayConfig {
14    fn default() -> Self {
15        Self {
16            base: 1.0,
17            lambda: 0.01,
18            floor: 0.05,
19        }
20    }
21}
22
23/// Pure salience computation. Deterministic from the ledger.
24pub fn salience(last_activity: Timestamp, now: Timestamp, config: &DecayConfig) -> f64 {
25    let age_millis = (now.millis() - last_activity.millis()).max(0) as f64;
26    let age_days = age_millis / 86_400_000.0;
27    let decayed = config.base * (-config.lambda * age_days).exp();
28    decayed.max(config.floor)
29}
30
31#[derive(Debug, Clone, Serialize, Deserialize)]
32pub struct CompactionConfig {
33    pub salience_threshold: f64,
34    pub min_age_days: u32,
35}
36
37impl Default for CompactionConfig {
38    fn default() -> Self {
39        Self {
40            salience_threshold: 0.1,
41            min_age_days: 90,
42        }
43    }
44}
45
46#[derive(Debug, Clone, Serialize, Deserialize)]
47pub struct SalienceEntry {
48    pub entity_id: String,
49    pub salience: f64,
50    pub last_activity: Timestamp,
51}