2026-03-25 01:55:21 -04:00
|
|
|
// hippocampus/memory.rs — In-memory view of a graph node
|
2026-03-25 01:39:48 -04:00
|
|
|
//
|
2026-03-25 01:55:21 -04:00
|
|
|
// MemoryNode is a lightweight representation of a loaded node:
|
|
|
|
|
// key, content, links, version, weight. Used by the agent for
|
2026-03-25 01:59:13 -04:00
|
|
|
// context tracking and by the CLI for rendering.
|
2026-03-25 01:39:48 -04:00
|
|
|
|
2026-03-25 01:55:21 -04:00
|
|
|
use super::store::Store;
|
2026-03-25 01:39:48 -04:00
|
|
|
|
|
|
|
|
/// A memory node loaded into the agent's working memory.
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
|
pub struct MemoryNode {
|
|
|
|
|
pub key: String,
|
|
|
|
|
pub content: String,
|
2026-03-25 01:59:13 -04:00
|
|
|
pub links: Vec<(String, f32)>, // (target_key, strength)
|
2026-03-25 01:39:48 -04:00
|
|
|
pub version: u32,
|
|
|
|
|
pub weight: f32,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl MemoryNode {
|
2026-03-25 01:59:13 -04:00
|
|
|
/// Load a node from the store by key.
|
2026-03-25 01:39:48 -04:00
|
|
|
pub fn load(key: &str) -> Option<Self> {
|
|
|
|
|
let store = Store::load().ok()?;
|
|
|
|
|
Self::from_store(&store, key)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Load from an already-open store.
|
|
|
|
|
pub fn from_store(store: &Store, key: &str) -> Option<Self> {
|
|
|
|
|
let node = store.nodes.get(key)?;
|
|
|
|
|
|
|
|
|
|
let mut neighbors: std::collections::HashMap<&str, f32> = std::collections::HashMap::new();
|
|
|
|
|
for r in &store.relations {
|
|
|
|
|
if r.deleted { continue; }
|
|
|
|
|
if r.source_key == key {
|
|
|
|
|
let e = neighbors.entry(&r.target_key).or_insert(0.0);
|
|
|
|
|
*e = e.max(r.strength);
|
|
|
|
|
} else if r.target_key == key {
|
|
|
|
|
let e = neighbors.entry(&r.source_key).or_insert(0.0);
|
|
|
|
|
*e = e.max(r.strength);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-25 01:59:13 -04:00
|
|
|
let mut links: Vec<(String, f32)> = neighbors.into_iter()
|
|
|
|
|
.map(|(k, s)| (k.to_string(), s))
|
2026-03-25 01:39:48 -04:00
|
|
|
.collect();
|
2026-03-25 01:59:13 -04:00
|
|
|
links.sort_by(|a, b| b.1.total_cmp(&a.1));
|
2026-03-25 01:39:48 -04:00
|
|
|
|
|
|
|
|
Some(MemoryNode {
|
|
|
|
|
key: key.to_string(),
|
|
|
|
|
content: node.content.clone(),
|
|
|
|
|
links,
|
|
|
|
|
version: node.version,
|
|
|
|
|
weight: node.weight,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Render for inclusion in the context window.
|
|
|
|
|
pub fn render(&self) -> String {
|
|
|
|
|
let mut out = self.content.clone();
|
|
|
|
|
|
|
|
|
|
// Footer: links not already referenced inline
|
2026-03-25 01:59:13 -04:00
|
|
|
let footer: Vec<&(String, f32)> = self.links.iter()
|
|
|
|
|
.filter(|(target, _)| !self.content.contains(target.as_str()))
|
2026-03-25 01:39:48 -04:00
|
|
|
.collect();
|
|
|
|
|
|
2026-03-25 01:59:13 -04:00
|
|
|
if !footer.is_empty() {
|
|
|
|
|
let total = footer.len();
|
2026-03-25 01:39:48 -04:00
|
|
|
out.push_str("\n\n---\nLinks:");
|
2026-03-25 01:59:13 -04:00
|
|
|
for (target, strength) in footer.iter().take(15) {
|
2026-03-25 02:15:46 -04:00
|
|
|
out.push_str(&format!("\n ({:.2}) {}", strength, target));
|
2026-03-25 01:39:48 -04:00
|
|
|
}
|
|
|
|
|
if total > 15 {
|
2026-03-25 02:15:46 -04:00
|
|
|
out.push_str(&format!("\n ... and {} more (memory_links key={})",
|
2026-03-25 01:39:48 -04:00
|
|
|
total - 15, self.key));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
out
|
|
|
|
|
}
|
|
|
|
|
}
|