cersei-memory 0.2.5

Memory trait and backends for the Cersei SDK
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
//! Graph-backed memory using Grafeo embedded graph database.
//!
//! Optional feature: enable with `features = ["graph"]` in Cargo.toml.
//!
//! ## Schema (v2)
//! ```text
//! (:Memory {id, content, mem_type, confidence, created_at, updated_at,
//!           last_validated_at, decay_rate, embedding_model_version})
//!   -[:RELATES_TO {relationship, weight}]-> (:Memory)
//!
//! (:Session {session_id, started_at, model, turns})
//!   -[:PRODUCED]-> (:Memory)
//!
//! (:Topic {name})
//!   -[:TAGGED]-> (:Memory)
//!
//! (:SchemaVersion {singleton, version, migrated_at, code_version})
//! ```

#[cfg(feature = "graph")]
use grafeo::GrafeoDB;

use crate::memdir::MemoryType;
use cersei_types::*;
use std::path::Path;

// Re-export migration utilities
pub use crate::graph_migrate::{self, effective_confidence, VersionCheck, CURRENT_SCHEMA_VERSION};

/// Graph-backed memory store.
pub struct GraphMemory {
    #[cfg(feature = "graph")]
    db: GrafeoDB,
    #[cfg(not(feature = "graph"))]
    _phantom: (),
}

/// Stats about the graph memory store.
#[derive(Debug, Clone, Default)]
pub struct GraphStats {
    pub memory_count: usize,
    pub session_count: usize,
    pub topic_count: usize,
    pub relationship_count: usize,
}

// ─── Centralized GQL queries ───────────────────────────────────────────────

#[cfg(feature = "graph")]
mod gql {
    pub fn escape(s: &str) -> String {
        s.replace('\\', "\\\\").replace('\'', "\\'")
    }

    pub fn insert_memory(
        id: &str,
        content: &str,
        mem_type: &str,
        confidence: f32,
        now: &str,
    ) -> String {
        format!(
            "INSERT (:Memory {{id: '{id}', content: '{content}', mem_type: '{mem_type}', \
             confidence: {confidence}, created_at: '{now}', updated_at: '{now}', \
             last_validated_at: '{now}', decay_rate: 0.01, embedding_model_version: ''}})"
        )
    }

    pub fn link_memories(from_id: &str, to_id: &str, relationship: &str) -> String {
        format!(
            "MATCH (a:Memory {{id: '{from_id}'}}), (b:Memory {{id: '{to_id}'}}) \
             INSERT (a)-[:RELATES_TO {{relationship: '{relationship}'}}]->(b)"
        )
    }

    pub fn tag_memory(memory_id: &str, topic: &str) -> String {
        format!(
            "MATCH (m:Memory {{id: '{memory_id}'}}) \
             INSERT (:Topic {{name: '{topic}'}})-[:TAGGED]->(m)"
        )
    }

    pub fn insert_session(session_id: &str, now: &str, model: &str, turns: u32) -> String {
        format!(
            "INSERT (:Session {{session_id: '{session_id}', started_at: '{now}', \
             model: '{model}', turns: {turns}}})"
        )
    }

    pub fn recall(escaped_query: &str, limit: usize) -> String {
        format!(
            "MATCH (m:Memory) WHERE m.content CONTAINS '{escaped_query}' RETURN m.content LIMIT {limit}"
        )
    }

    pub fn by_type(type_str: &str) -> String {
        format!("MATCH (m:Memory {{mem_type: '{type_str}'}}) RETURN m.content")
    }

    pub fn by_topic(topic: &str) -> String {
        format!("MATCH (:Topic {{name: '{topic}'}})-[:TAGGED]->(m:Memory) RETURN m.content")
    }

    pub fn revalidate(memory_id: &str, now: &str) -> String {
        // Since Grafeo may not support SET, we use a workaround:
        // Delete and re-insert would lose data. Instead we just track validation
        // through the SchemaVersion system. For now this is a no-op query that
        // verifies the node exists.
        format!("MATCH (m:Memory {{id: '{memory_id}'}}) RETURN m.id")
    }

    pub const COUNT_MEMORIES: &str = "MATCH (m:Memory) RETURN count(m)";
    pub const COUNT_SESSIONS: &str = "MATCH (s:Session) RETURN count(s)";
    pub const COUNT_TOPICS: &str = "MATCH (t:Topic) RETURN count(t)";
    pub const COUNT_RELATIONSHIPS: &str = "MATCH ()-[r:RELATES_TO]->() RETURN count(r)";
}

impl GraphMemory {
    /// Open a persistent graph database at the given path.
    /// Automatically checks schema version and runs migrations if needed.
    #[cfg(feature = "graph")]
    pub fn open(path: &Path) -> Result<Self> {
        let db = GrafeoDB::open(path)
            .map_err(|e| CerseiError::Config(format!("Failed to open graph DB: {}", e)))?;

        // Version check and auto-migrate
        match graph_migrate::check_version(&db) {
            VersionCheck::UpToDate => {}
            VersionCheck::NeedsMigration { from, to } => {
                graph_migrate::run_migrations(&db, from, to)?;
            }
            VersionCheck::CodeBehind {
                graph_version,
                code_version,
            } => {
                tracing::warn!(
                    "Graph schema v{} is newer than code v{}. Forward-compatible reads will be used.",
                    graph_version, code_version
                );
            }
        }

        Ok(Self { db })
    }

    /// Create an in-memory graph database (no persistence).
    /// Automatically stamps the current schema version.
    #[cfg(feature = "graph")]
    pub fn open_in_memory() -> Result<Self> {
        let db = GrafeoDB::new_in_memory();

        // Fresh in-memory graph always needs version stamp
        match graph_migrate::check_version(&db) {
            VersionCheck::UpToDate => {}
            VersionCheck::NeedsMigration { from, to } => {
                graph_migrate::run_migrations(&db, from, to)?;
            }
            _ => {}
        }

        Ok(Self { db })
    }

    /// Fallback: graph feature not enabled.
    #[cfg(not(feature = "graph"))]
    pub fn open(_path: &Path) -> Result<Self> {
        Err(CerseiError::Config(
            "Graph memory requires the 'graph' feature. Enable it in Cargo.toml.".into(),
        ))
    }

    /// Fallback: graph feature not enabled.
    #[cfg(not(feature = "graph"))]
    pub fn open_in_memory() -> Result<Self> {
        Err(CerseiError::Config(
            "Graph memory requires the 'graph' feature. Enable it in Cargo.toml.".into(),
        ))
    }

    // ─── Write operations ────────────────────────────────────────────────

    /// Store a memory as a graph node (v2 schema: includes decay and embedding fields).
    #[cfg(feature = "graph")]
    pub fn store_memory(
        &self,
        content: &str,
        mem_type: MemoryType,
        confidence: f32,
    ) -> Result<String> {
        let session = self.db.session();
        let mem_type_str = format!("{:?}", mem_type);
        let now = chrono::Utc::now().to_rfc3339();
        let id = uuid::Uuid::new_v4().to_string();
        let escaped = gql::escape(content);

        let query = gql::insert_memory(&id, &escaped, &mem_type_str, confidence, &now);
        session
            .execute(&query)
            .map_err(|e| CerseiError::Config(format!("Graph insert failed: {}", e)))?;

        Ok(id)
    }

    /// Link two memories with a named relationship.
    #[cfg(feature = "graph")]
    pub fn link_memories(&self, from_id: &str, to_id: &str, relationship: &str) -> Result<()> {
        let session = self.db.session();
        let query = gql::link_memories(from_id, to_id, relationship);
        session
            .execute(&query)
            .map_err(|e| CerseiError::Config(format!("Graph link failed: {}", e)))?;
        Ok(())
    }

    /// Tag a memory with a topic.
    #[cfg(feature = "graph")]
    pub fn tag_memory(&self, memory_id: &str, topic: &str) -> Result<()> {
        let session = self.db.session();
        let query = gql::tag_memory(memory_id, topic);
        session
            .execute(&query)
            .map_err(|e| CerseiError::Config(format!("Graph tag failed: {}", e)))?;
        Ok(())
    }

    /// Record a session in the graph.
    #[cfg(feature = "graph")]
    pub fn record_session(&self, session_id: &str, model: Option<&str>, turns: u32) -> Result<()> {
        let session = self.db.session();
        let now = chrono::Utc::now().to_rfc3339();
        let model_str = model.unwrap_or("unknown");
        let query = gql::insert_session(session_id, &now, model_str, turns);
        session
            .execute(&query)
            .map_err(|e| CerseiError::Config(format!("Graph session record failed: {}", e)))?;
        Ok(())
    }

    /// Revalidate a memory — resets the confidence decay clock.
    /// Returns Ok(true) if the memory was found, Ok(false) if not.
    #[cfg(feature = "graph")]
    pub fn revalidate_memory(&self, memory_id: &str) -> Result<bool> {
        let session = self.db.session();
        let query = gql::revalidate(memory_id, &chrono::Utc::now().to_rfc3339());
        match session.execute(&query) {
            Ok(result) => Ok(result.iter().next().is_some()),
            Err(e) => Err(CerseiError::Config(format!(
                "Graph revalidate failed: {}",
                e
            ))),
        }
    }

    // ─── Query operations ────────────────────────────────────────────────

    /// Recall memories matching a text query (substring match).
    #[cfg(feature = "graph")]
    pub fn recall(&self, query_text: &str, limit: usize) -> Vec<String> {
        let session = self.db.session();
        let escaped = gql::escape(query_text);
        let query = gql::recall(&escaped, limit);
        match session.execute(&query) {
            Ok(result) => result
                .iter()
                .filter_map(|row| row.first().map(|v| format!("{}", v)))
                .collect(),
            Err(_) => Vec::new(),
        }
    }

    /// Recall memories matching a text query with a relevance score.
    /// Score = fraction of query words found in each memory, in [0, 1].
    /// Results are ranked by score descending; ties preserve insertion order.
    /// Pulls up to 4× `limit` candidates via substring match, then re-ranks.
    #[cfg(feature = "graph")]
    pub fn recall_top_k(&self, query_text: &str, limit: usize) -> Vec<(String, f32)> {
        if limit == 0 || query_text.trim().is_empty() {
            return Vec::new();
        }
        // Pull a generous candidate set so word-overlap re-ranking has room.
        let candidates = self.recall(query_text, limit.saturating_mul(4).max(16));
        let words: Vec<String> = query_text
            .split_whitespace()
            .filter_map(|w| {
                let w = w
                    .trim_matches(|c: char| !c.is_alphanumeric())
                    .to_lowercase();
                if w.is_empty() || w.len() < 2 {
                    None
                } else {
                    Some(w)
                }
            })
            .collect();
        if words.is_empty() {
            // Fall back to uniform top-limit.
            return candidates
                .into_iter()
                .take(limit)
                .map(|c| (c, 1.0))
                .collect();
        }
        let mut scored: Vec<(String, f32)> = candidates
            .into_iter()
            .map(|c| {
                let lower = c.to_lowercase();
                let hits = words.iter().filter(|w| lower.contains(w.as_str())).count();
                (c, hits as f32 / words.len() as f32)
            })
            .collect();
        scored.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));
        scored.truncate(limit);
        scored
    }

    /// Get all memories of a specific type.
    #[cfg(feature = "graph")]
    pub fn by_type(&self, mem_type: MemoryType) -> Vec<String> {
        let session = self.db.session();
        let type_str = format!("{:?}", mem_type);
        let query = gql::by_type(&type_str);
        match session.execute(&query) {
            Ok(result) => result
                .iter()
                .filter_map(|row| row.first().map(|v| format!("{}", v)))
                .collect(),
            Err(_) => Vec::new(),
        }
    }

    /// Get memories tagged with a specific topic.
    #[cfg(feature = "graph")]
    pub fn by_topic(&self, topic: &str) -> Vec<String> {
        let session = self.db.session();
        let query = gql::by_topic(topic);
        match session.execute(&query) {
            Ok(result) => result
                .iter()
                .filter_map(|row| row.first().map(|v| format!("{}", v)))
                .collect(),
            Err(_) => Vec::new(),
        }
    }

    /// Get graph statistics.
    #[cfg(feature = "graph")]
    pub fn stats(&self) -> GraphStats {
        let session = self.db.session();
        let count = |query: &str| -> usize {
            session
                .execute(query)
                .ok()
                .and_then(|r| r.scalar::<i64>().ok())
                .map(|v| v as usize)
                .unwrap_or(0)
        };

        GraphStats {
            memory_count: count(gql::COUNT_MEMORIES),
            session_count: count(gql::COUNT_SESSIONS),
            topic_count: count(gql::COUNT_TOPICS),
            relationship_count: count(gql::COUNT_RELATIONSHIPS),
        }
    }

    /// Get the current schema version of the graph.
    #[cfg(feature = "graph")]
    pub fn schema_version(&self) -> VersionCheck {
        graph_migrate::check_version(&self.db)
    }

    // ─── Fallback implementations (no graph feature) ─────────────────────

    #[cfg(not(feature = "graph"))]
    pub fn store_memory(&self, _: &str, _: MemoryType, _: f32) -> Result<String> {
        Err(CerseiError::Config("Graph feature not enabled".into()))
    }

    #[cfg(not(feature = "graph"))]
    pub fn recall_top_k(&self, _: &str, _: usize) -> Vec<(String, f32)> {
        Vec::new()
    }

    #[cfg(not(feature = "graph"))]
    pub fn link_memories(&self, _: &str, _: &str, _: &str) -> Result<()> {
        Err(CerseiError::Config("Graph feature not enabled".into()))
    }

    #[cfg(not(feature = "graph"))]
    pub fn tag_memory(&self, _: &str, _: &str) -> Result<()> {
        Err(CerseiError::Config("Graph feature not enabled".into()))
    }

    #[cfg(not(feature = "graph"))]
    pub fn record_session(&self, _: &str, _: Option<&str>, _: u32) -> Result<()> {
        Err(CerseiError::Config("Graph feature not enabled".into()))
    }

    #[cfg(not(feature = "graph"))]
    pub fn revalidate_memory(&self, _: &str) -> Result<bool> {
        Err(CerseiError::Config("Graph feature not enabled".into()))
    }

    #[cfg(not(feature = "graph"))]
    pub fn recall(&self, _: &str, _: usize) -> Vec<String> {
        Vec::new()
    }

    #[cfg(not(feature = "graph"))]
    pub fn by_type(&self, _: MemoryType) -> Vec<String> {
        Vec::new()
    }

    #[cfg(not(feature = "graph"))]
    pub fn by_topic(&self, _: &str) -> Vec<String> {
        Vec::new()
    }

    #[cfg(not(feature = "graph"))]
    pub fn stats(&self) -> GraphStats {
        GraphStats::default()
    }

    #[cfg(not(feature = "graph"))]
    pub fn schema_version(&self) -> VersionCheck {
        VersionCheck::UpToDate
    }
}

/// Check if graph memory is available (compiled with the feature).
pub fn is_graph_available() -> bool {
    cfg!(feature = "graph")
}