File size: 3,104 Bytes
3ce0306 661a2e7 3ce0306 661a2e7 3ce0306 661a2e7 3ce0306 661a2e7 3ce0306 661a2e7 3ce0306 661a2e7 | 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 | //! User memory model — what Star knows about Zachary's memory
//!
//! Tracks what Zachary remembers well, what he tends to forget,
//! and how his memory interacts with Star's self-model.
use std::collections::HashMap;
/// Memory strength for a topic/domain
#[derive(Debug, Clone)]
pub struct MemoryStrength {
pub topic: String,
/// How well Zachary remembers this (0-1)
pub strength: f64,
pub access_count: usize,
pub last_accessed: i64,
}
/// User memory model
#[derive(Debug, Clone)]
pub struct UserMemoryModel {
/// Zachary's memory strengths by topic/domain
strengths: HashMap<String, MemoryStrength>,
/// Topics Zachary has introduced and might remember
introduced_topics: Vec<String>,
}
impl UserMemoryModel {
pub fn new() -> Self {
Self {
strengths: HashMap::new(),
introduced_topics: Vec::new(),
}
}
/// Record that Zachary introduced a topic
pub fn record_introduction(&mut self, topic: &str) {
if !self.introduced_topics.contains(&topic.to_lowercase()) {
self.introduced_topics.push(topic.to_lowercase());
}
}
/// Record that Zachary demonstrated memory of a topic
pub fn record_memory_access(&mut self, topic: &str, success: bool) {
let key = topic.to_lowercase();
if success {
if let Some(strength) = self.strengths.get_mut(&key) {
strength.strength = (strength.strength * 0.9 + 0.1).min(1.0);
strength.access_count += 1;
strength.last_accessed = crate::now_timestamp();
} else {
self.strengths.insert(
key,
MemoryStrength {
topic: topic.to_string(),
strength: 0.6,
access_count: 1,
last_accessed: crate::now_timestamp(),
},
);
}
} else if let Some(strength) = self.strengths.get_mut(&key) {
strength.strength *= 0.8;
}
}
/// Get memory strength for a topic
pub fn get_strength(&self, topic: &str) -> Option<f64> {
self.strengths
.get(&topic.to_lowercase())
.map(|s| s.strength)
}
/// Check if Zachary introduced this topic
pub fn did_introduce(&self, topic: &str) -> bool {
self.introduced_topics.contains(&topic.to_lowercase())
}
/// Topics that might have been forgotten
pub fn potentially_forgotten(&self) -> Vec<&str> {
self.strengths
.iter()
.filter(|(_, s)| s.strength < 0.3)
.map(|(_, s)| s.topic.as_str())
.collect()
}
/// Get all strong topics (where Zachary has good memory)
pub fn strong_topics(&self) -> Vec<&str> {
self.strengths
.iter()
.filter(|(_, s)| s.strength > 0.7)
.map(|(_, s)| s.topic.as_str())
.collect()
}
}
impl Default for UserMemoryModel {
fn default() -> Self {
Self::new()
}
}
|