bhava 2.0.0

Emotion and personality engine — trait spectrums, mood vectors, archetypes, behavioral mapping
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
//! Salience classification — somatic marker urgency/importance scoring.
//!
//! Based on Damasio's somatic marker hypothesis (1994): emotionally significant
//! events leave body-state markers that bias future decision-making. Salience
//! is scored on two orthogonal dimensions:
//!
//! - **Urgency** — how time-critical is this? Driven by desirability, likelihood,
//!   and current arousal/deviation.
//! - **Importance** — how much does this matter long-term? Driven by moral
//!   weight (praiseworthiness), prior memory intensity, and causal attribution.
//!
//! Combined salience uses the geometric mean (both must be nonzero for high
//! salience), classified into Background / Notable / Significant / Critical.

use serde::{Deserialize, Serialize};

use crate::appraisal::Appraisal;
use crate::mood::{EmotionalMemory, MoodVector};
use crate::types::{Normalized01, ThresholdClassifier};

/// Urgency × importance score for an event or memory.
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct SalienceScore {
    /// How time-critical is this? 0.0 = no urgency, 1.0 = act now.
    pub urgency: Normalized01,
    /// How much does this matter long-term? 0.0 = trivial, 1.0 = life-defining.
    pub importance: Normalized01,
}

impl SalienceScore {
    /// Create a score (values clamped to 0.0–1.0).
    #[must_use]
    pub fn new(urgency: f32, importance: f32) -> Self {
        Self {
            urgency: Normalized01::new(urgency),
            importance: Normalized01::new(importance),
        }
    }

    /// Zero salience — completely unremarkable.
    #[must_use]
    pub fn zero() -> Self {
        Self {
            urgency: Normalized01::ZERO,
            importance: Normalized01::ZERO,
        }
    }

    /// Combined salience magnitude: sqrt(urgency × importance).
    ///
    /// Geometric mean ensures both dimensions must contribute for high salience.
    /// An urgent but unimportant event scores lower than one that is both.
    #[must_use]
    #[inline]
    pub fn magnitude(&self) -> f32 {
        (self.urgency.get() * self.importance.get()).sqrt()
    }

    /// Classify into a discrete salience level.
    #[must_use]
    pub fn level(&self) -> SalienceLevel {
        const CLASSIFIER: ThresholdClassifier<SalienceLevel> = ThresholdClassifier::new(
            &[
                (0.75, SalienceLevel::Critical),
                (0.45, SalienceLevel::Significant),
                (0.2, SalienceLevel::Notable),
            ],
            SalienceLevel::Background,
        );
        CLASSIFIER.classify(self.magnitude())
    }
}

/// Discrete salience classification.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[non_exhaustive]
pub enum SalienceLevel {
    /// Below awareness threshold (magnitude < 0.2).
    Background,
    /// Noticed but not prioritized (0.2–0.45).
    Notable,
    /// Demands attention (0.45–0.75).
    Significant,
    /// Immediate action required (>= 0.75).
    Critical,
}

impl_display!(SalienceLevel {
    Background => "background",
    Notable => "notable",
    Significant => "significant",
    Critical => "critical",
});

/// Score salience of an appraisal given current mood state.
///
/// - **Urgency** = |desirability| × likelihood × (1 + mood_deviation).
///   Strong desirability + high likelihood + already-aroused agent = urgent.
/// - **Importance** = max(|desirability|, |praiseworthiness|) × (1 + memory_intensity).
///   Morally weighted events matter more; reinforced by prior memories.
///
/// Both dimensions clamped to 0.0–1.0.
#[cfg_attr(feature = "tracing", tracing::instrument(skip_all))]
#[must_use]
pub fn classify_salience(
    appraisal: &Appraisal,
    mood_deviation: f32,
    memory_intensity: f32,
) -> SalienceScore {
    let urgency = (appraisal.desirability.abs()
        * appraisal.likelihood
        * (1.0 + mood_deviation.clamp(0.0, 1.0)))
    .clamp(0.0, 1.0);

    let importance = (appraisal
        .desirability
        .abs()
        .max(appraisal.praiseworthiness.abs())
        * (1.0 + memory_intensity.clamp(0.0, 1.0)))
    .clamp(0.0, 1.0);

    SalienceScore::new(urgency, importance)
}

/// Score salience of a stored emotional memory.
///
/// Synthesizes urgency from arousal intensity and importance from
/// the emotional magnitude of the stored mood.
#[cfg_attr(feature = "tracing", tracing::instrument(skip_all))]
#[must_use]
pub fn memory_salience(memory: &EmotionalMemory) -> SalienceScore {
    let urgency = (memory.mood.arousal.abs() * memory.intensity).clamp(0.0, 1.0);
    let importance =
        (memory.mood.joy.abs().max(memory.mood.dominance.abs()) * memory.intensity).clamp(0.0, 1.0);

    SalienceScore::new(urgency, importance)
}

/// Filter a slice of memories by salience threshold.
///
/// Returns matching memories paired with their salience scores,
/// sorted by magnitude descending. Use with `recall_congruent` or
/// any other source of `EmotionalMemory` references.
#[cfg_attr(feature = "tracing", tracing::instrument(skip_all))]
#[must_use]
pub fn filter_salient<'a>(
    memories: &[&'a EmotionalMemory],
    threshold: f32,
) -> Vec<(&'a EmotionalMemory, SalienceScore)> {
    let mut results: Vec<_> = memories
        .iter()
        .filter_map(|&mem| {
            let score = memory_salience(mem);
            if score.magnitude() >= threshold {
                Some((mem, score))
            } else {
                None
            }
        })
        .collect();

    results.sort_by(|a, b| {
        b.1.magnitude()
            .partial_cmp(&a.1.magnitude())
            .unwrap_or(std::cmp::Ordering::Equal)
    });
    results
}

/// Compute a salience-weighted mood from a set of memories.
///
/// More salient memories contribute more to the resulting mood vector.
/// Returns neutral if no memories exceed the threshold.
#[cfg_attr(feature = "tracing", tracing::instrument(skip_all))]
#[must_use]
pub fn salience_weighted_mood(memories: &[&EmotionalMemory], threshold: f32) -> MoodVector {
    let mut weighted_sum = MoodVector::neutral();
    let mut total_weight = 0.0f32;

    for &mem in memories {
        let score = memory_salience(mem);
        let weight = score.magnitude();
        if weight < threshold {
            continue;
        }
        for &e in crate::mood::Emotion::ALL {
            let current = weighted_sum.get(e);
            weighted_sum.set(e, current + mem.mood.get(e) * weight * mem.intensity);
        }
        total_weight += weight;
    }

    if total_weight > f32::EPSILON {
        for &e in crate::mood::Emotion::ALL {
            let v = weighted_sum.get(e) / total_weight;
            weighted_sum.set(e, v);
        }
    }

    weighted_sum
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::appraisal::Appraisal;
    use crate::mood::{Emotion, EmotionalMemory, MoodVector};

    #[test]
    fn test_neutral_appraisal_is_background() {
        let a = Appraisal::event("nothing", 0.0);
        let score = classify_salience(&a, 0.0, 0.0);
        assert_eq!(score.level(), SalienceLevel::Background);
    }

    #[test]
    fn test_extreme_appraisal_is_critical() {
        let a = Appraisal::event("crisis", 1.0).with_praise(0.9);
        let score = classify_salience(&a, 0.8, 0.5);
        assert_eq!(
            score.level(),
            SalienceLevel::Critical,
            "magnitude={}",
            score.magnitude()
        );
    }

    #[test]
    fn test_magnitude_geometric_mean() {
        let s = SalienceScore::new(0.64, 1.0);
        assert!((s.magnitude() - 0.8).abs() < 0.001, "mag={}", s.magnitude());
    }

    #[test]
    fn test_magnitude_zero_if_either_zero() {
        let s = SalienceScore::new(0.0, 1.0);
        assert!(s.magnitude().abs() < f32::EPSILON);
        let s2 = SalienceScore::new(1.0, 0.0);
        assert!(s2.magnitude().abs() < f32::EPSILON);
    }

    #[test]
    fn test_importance_from_praiseworthiness() {
        let a = Appraisal::event("moral", 0.1).with_praise(0.9);
        let score = classify_salience(&a, 0.0, 0.0);
        // praiseworthiness (0.9) > desirability (0.1), so importance driven by praise
        assert!(
            score.importance.get() > 0.5,
            "importance={}",
            score.importance
        );
    }

    #[test]
    fn test_urgency_from_deviation() {
        let a = Appraisal::event("urgent", 0.8);
        let calm = classify_salience(&a, 0.0, 0.0);
        let aroused = classify_salience(&a, 0.8, 0.0);
        assert!(
            aroused.urgency.get() > calm.urgency.get(),
            "aroused={} calm={}",
            aroused.urgency,
            calm.urgency
        );
    }

    #[test]
    fn test_salience_level_thresholds() {
        assert_eq!(
            SalienceScore::new(0.01, 0.01).level(),
            SalienceLevel::Background
        );
        assert_eq!(SalienceScore::new(0.3, 0.3).level(), SalienceLevel::Notable);
        assert_eq!(
            SalienceScore::new(0.6, 0.6).level(),
            SalienceLevel::Significant
        );
        assert_eq!(
            SalienceScore::new(0.9, 0.9).level(),
            SalienceLevel::Critical
        );
    }

    #[test]
    fn test_salience_score_clamps() {
        let s = SalienceScore::new(2.0, -1.0);
        assert!((s.urgency.get() - 1.0).abs() < f32::EPSILON);
        assert!(s.importance.get().abs() < f32::EPSILON);
    }

    #[test]
    fn test_memory_salience() {
        let mut mood = MoodVector::neutral();
        mood.set(Emotion::Arousal, 0.8);
        mood.set(Emotion::Joy, 0.7);
        let mem = EmotionalMemory {
            tag: "intense".into(),
            mood,
            intensity: 0.9,
        };
        let score = memory_salience(&mem);
        assert!(score.urgency.get() > 0.5, "urgency={}", score.urgency);
        assert!(
            score.importance.get() > 0.5,
            "importance={}",
            score.importance
        );
    }

    #[test]
    fn test_filter_salient() {
        let mut strong_mood = MoodVector::neutral();
        strong_mood.set(Emotion::Arousal, 0.9);
        strong_mood.set(Emotion::Joy, 0.8);
        let strong = EmotionalMemory {
            tag: "strong".into(),
            mood: strong_mood,
            intensity: 0.9,
        };
        let weak = EmotionalMemory {
            tag: "weak".into(),
            mood: MoodVector::neutral(),
            intensity: 0.1,
        };
        let memories: Vec<&EmotionalMemory> = vec![&strong, &weak];
        let results = filter_salient(&memories, 0.3);
        assert_eq!(results.len(), 1, "only strong should pass threshold");
        assert_eq!(results[0].0.tag, "strong");
    }

    #[test]
    fn test_filter_salient_sorted() {
        let mut m1 = MoodVector::neutral();
        m1.set(Emotion::Arousal, 0.5);
        m1.set(Emotion::Joy, 0.5);
        let medium = EmotionalMemory {
            tag: "medium".into(),
            mood: m1,
            intensity: 0.6,
        };
        let mut m2 = MoodVector::neutral();
        m2.set(Emotion::Arousal, 0.9);
        m2.set(Emotion::Joy, 0.9);
        let high = EmotionalMemory {
            tag: "high".into(),
            mood: m2,
            intensity: 0.9,
        };
        let memories: Vec<&EmotionalMemory> = vec![&medium, &high];
        let results = filter_salient(&memories, 0.1);
        assert!(results.len() >= 2);
        assert!(results[0].1.magnitude() >= results[1].1.magnitude());
    }

    #[test]
    fn test_salience_weighted_mood_empty() {
        let memories: Vec<&EmotionalMemory> = vec![];
        let mood = salience_weighted_mood(&memories, 0.0);
        assert!(mood.intensity() < f32::EPSILON);
    }

    #[test]
    fn test_salience_weighted_mood_biased() {
        let mut m = MoodVector::neutral();
        m.set(Emotion::Joy, 0.8);
        m.set(Emotion::Arousal, 0.7);
        let mem = EmotionalMemory {
            tag: "happy".into(),
            mood: m,
            intensity: 0.9,
        };
        let memories: Vec<&EmotionalMemory> = vec![&mem];
        let recalled = salience_weighted_mood(&memories, 0.0);
        assert!(recalled.joy > 0.0, "should recall positive joy");
    }

    #[test]
    fn test_level_display() {
        assert_eq!(SalienceLevel::Background.to_string(), "background");
        assert_eq!(SalienceLevel::Critical.to_string(), "critical");
    }

    #[test]
    fn test_zero_score() {
        let s = SalienceScore::zero();
        assert!(s.magnitude().abs() < f32::EPSILON);
        assert_eq!(s.level(), SalienceLevel::Background);
    }

    #[test]
    fn test_serde() {
        let s = SalienceScore::new(0.7, 0.8);
        let json = serde_json::to_string(&s).unwrap();
        let s2: SalienceScore = serde_json::from_str(&json).unwrap();
        assert!((s2.urgency.get() - s.urgency.get()).abs() < f32::EPSILON);
    }

    #[test]
    fn test_serde_level() {
        let l = SalienceLevel::Significant;
        let json = serde_json::to_string(&l).unwrap();
        let l2: SalienceLevel = serde_json::from_str(&json).unwrap();
        assert_eq!(l2, l);
    }
}