cersei-memory 0.2.4

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
//! Unified Memory Manager: composes all memory layers into a single API.
//!
//! Layers (in query order):
//! 1. Graph (optional) — Grafeo for relationship-aware recall
//! 2. Memdir — flat file scanning for MEMORY.md and topic files
//! 3. CLAUDE.md — hierarchical instruction loading
//! 4. Session storage — JSONL transcript persistence
//!
//! The manager delegates to the appropriate layer for each operation.

use crate::claudemd::{self};
use crate::graph::{GraphMemory, GraphStats};
use crate::memdir::{self, MemoryFile, MemoryFileMeta, MemoryType};
use crate::session_storage;
use cersei_types::*;
use std::path::{Path, PathBuf};

/// Unified memory manager composing all layers.
pub struct MemoryManager {
    /// Project root for resolving paths.
    project_root: PathBuf,
    /// Memory directory path.
    memory_dir: PathBuf,
    /// Session storage directory.
    sessions_dir: PathBuf,
    /// Optional graph memory layer.
    graph: Option<GraphMemory>,
}

impl MemoryManager {
    /// Create a new memory manager for a project.
    pub fn new(project_root: &Path) -> Self {
        let memory_dir = memdir::auto_memory_path(project_root);
        let sanitized = memdir::sanitize_path_component(&project_root.display().to_string());
        let home = dirs::home_dir().unwrap_or_else(|| PathBuf::from("."));
        let sessions_dir = home.join(".claude").join("projects").join(&sanitized);

        Self {
            project_root: project_root.to_path_buf(),
            memory_dir,
            sessions_dir,
            graph: None,
        }
    }

    /// Enable graph memory at a given path.
    /// Requires the `graph` feature.
    pub fn with_graph(mut self, path: &Path) -> Result<Self> {
        self.graph = Some(GraphMemory::open(path)?);
        Ok(self)
    }

    /// Enable in-memory graph (no persistence).
    /// Requires the `graph` feature.
    pub fn with_graph_in_memory(mut self) -> Result<Self> {
        self.graph = Some(GraphMemory::open_in_memory()?);
        Ok(self)
    }

    /// Set a custom memory directory.
    pub fn with_memory_dir(mut self, dir: PathBuf) -> Self {
        self.memory_dir = dir;
        self
    }

    /// Set a custom sessions directory.
    pub fn with_sessions_dir(mut self, dir: PathBuf) -> Self {
        self.sessions_dir = dir;
        self
    }

    // ─── Context building ────────────────────────────────────────────────

    /// Build the complete memory context for the system prompt.
    /// Includes MEMORY.md index + CLAUDE.md hierarchy.
    pub fn build_context(&self) -> String {
        let mut parts = Vec::new();

        // CLAUDE.md hierarchy
        let claude_files = claudemd::load_all_memory_files(&self.project_root);
        let claude_prompt = claudemd::build_memory_prompt(&claude_files);
        if !claude_prompt.is_empty() {
            parts.push(claude_prompt);
        }

        // MEMORY.md index
        let memdir_content = memdir::build_memory_prompt_content(&self.memory_dir);
        if !memdir_content.is_empty() {
            parts.push(memdir_content);
        }

        parts.join("\n\n")
    }

    // ─── Memory operations ───────────────────────────────────────────────

    /// Scan memory directory for all memory file metadata.
    pub fn scan(&self) -> Vec<MemoryFileMeta> {
        memdir::scan_memory_dir(&self.memory_dir)
    }

    /// Load a specific memory file by path.
    pub fn load_file(&self, path: &Path) -> Option<MemoryFile> {
        memdir::load_memory_file(path)
    }

    /// Store a memory (writes to graph if available, always returns success).
    pub fn store_memory(
        &self,
        content: &str,
        mem_type: MemoryType,
        confidence: f32,
    ) -> Option<String> {
        if let Some(graph) = &self.graph {
            graph.store_memory(content, mem_type, confidence).ok()
        } else {
            None
        }
    }

    /// Recall memories matching a query.
    /// Uses graph if available, falls back to memdir scan + text matching.
    pub fn recall(&self, query: &str, limit: usize) -> Vec<String> {
        // Try graph first
        if let Some(graph) = &self.graph {
            let results = graph.recall(query, limit);
            if !results.is_empty() {
                return results;
            }
        }

        // Fallback: scan memdir and text-match
        let query_lower = query.to_lowercase();
        let metas = self.scan();
        let mut results = Vec::new();

        for meta in metas.iter().take(limit * 2) {
            if let Some(file) = memdir::load_memory_file(&meta.path) {
                if file.content.to_lowercase().contains(&query_lower)
                    || meta
                        .name
                        .as_deref()
                        .unwrap_or("")
                        .to_lowercase()
                        .contains(&query_lower)
                    || meta
                        .description
                        .as_deref()
                        .unwrap_or("")
                        .to_lowercase()
                        .contains(&query_lower)
                {
                    results.push(file.content);
                    if results.len() >= limit {
                        break;
                    }
                }
            }
        }

        results
    }

    /// Get memories by type (graph only, returns empty without graph).
    pub fn by_type(&self, mem_type: MemoryType) -> Vec<String> {
        if let Some(graph) = &self.graph {
            graph.by_type(mem_type)
        } else {
            Vec::new()
        }
    }

    /// Get memories by topic (graph only).
    pub fn by_topic(&self, topic: &str) -> Vec<String> {
        if let Some(graph) = &self.graph {
            graph.by_topic(topic)
        } else {
            Vec::new()
        }
    }

    // ─── Session operations ──────────────────────────────────────────────

    /// Get the transcript path for a session.
    pub fn session_path(&self, session_id: &str) -> PathBuf {
        self.sessions_dir.join(format!("{}.jsonl", session_id))
    }

    /// Write a user message to the session transcript.
    pub fn write_user_message(
        &self,
        session_id: &str,
        message: Message,
    ) -> std::io::Result<String> {
        let path = self.session_path(session_id);
        let cwd = self.project_root.display().to_string();
        session_storage::write_user_entry(&path, session_id, message, &cwd)
    }

    /// Write an assistant message to the session transcript.
    pub fn write_assistant_message(
        &self,
        session_id: &str,
        message: Message,
        parent_uuid: Option<&str>,
    ) -> std::io::Result<String> {
        let path = self.session_path(session_id);
        let cwd = self.project_root.display().to_string();
        session_storage::write_assistant_entry(&path, session_id, message, &cwd, parent_uuid)
    }

    /// Load a session's messages.
    pub fn load_session_messages(&self, session_id: &str) -> Result<Vec<Message>> {
        let path = self.session_path(session_id);
        if !path.exists() {
            return Ok(Vec::new());
        }
        let entries = session_storage::load_transcript(&path)?;
        Ok(session_storage::messages_from_transcript(&entries))
    }

    /// List all session files.
    pub fn list_sessions(&self) -> Vec<SessionInfo> {
        let mut sessions = Vec::new();
        let entries = match std::fs::read_dir(&self.sessions_dir) {
            Ok(e) => e,
            Err(_) => return sessions,
        };

        for entry in entries.flatten() {
            let path = entry.path();
            if path.extension().and_then(|e| e.to_str()) != Some("jsonl") {
                continue;
            }
            let id = path
                .file_stem()
                .and_then(|s| s.to_str())
                .unwrap_or("")
                .to_string();
            let created_at = std::fs::metadata(&path)
                .and_then(|m| m.created())
                .ok()
                .and_then(|t| {
                    let d = t.duration_since(std::time::UNIX_EPOCH).ok()?;
                    chrono::DateTime::from_timestamp(d.as_secs() as i64, 0)
                })
                .unwrap_or_else(chrono::Utc::now);

            sessions.push(SessionInfo {
                id,
                created_at,
                message_count: 0, // would need to parse to count
                model: None,
            });
        }

        sessions
    }

    // ─── Graph operations ────────────────────────────────────────────────

    /// Check if graph memory is available.
    pub fn has_graph(&self) -> bool {
        self.graph.is_some()
    }

    /// Get graph statistics (returns default if no graph).
    pub fn graph_stats(&self) -> GraphStats {
        self.graph.as_ref().map(|g| g.stats()).unwrap_or_default()
    }

    /// Tag a memory in the graph (no-op without graph).
    pub fn tag_memory(&self, memory_id: &str, topic: &str) {
        if let Some(graph) = &self.graph {
            let _ = graph.tag_memory(memory_id, topic);
        }
    }

    /// Link two memories in the graph (no-op without graph).
    pub fn link_memories(&self, from_id: &str, to_id: &str, relationship: &str) {
        if let Some(graph) = &self.graph {
            let _ = graph.link_memories(from_id, to_id, relationship);
        }
    }

    /// Access paths.
    pub fn memory_dir(&self) -> &Path {
        &self.memory_dir
    }
    pub fn sessions_dir(&self) -> &Path {
        &self.sessions_dir
    }
    pub fn project_root(&self) -> &Path {
        &self.project_root
    }
}

// ─── Tests ───────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_manager_basic() {
        let tmp = tempfile::tempdir().unwrap();
        let manager = MemoryManager::new(tmp.path())
            .with_memory_dir(tmp.path().join("memory"))
            .with_sessions_dir(tmp.path().join("sessions"));

        assert!(!manager.has_graph());
        assert_eq!(manager.graph_stats().memory_count, 0);
    }

    #[test]
    fn test_manager_context_with_claude_md() {
        let tmp = tempfile::tempdir().unwrap();
        std::fs::write(
            tmp.path().join("CLAUDE.md"),
            "# Project Rules\nUse Rust only.",
        )
        .unwrap();

        let mem_dir = tmp.path().join("memory");
        std::fs::create_dir_all(&mem_dir).unwrap();
        std::fs::write(mem_dir.join("MEMORY.md"), "- [pref](pref.md) — user prefs").unwrap();

        let manager = MemoryManager::new(tmp.path()).with_memory_dir(mem_dir);

        let context = manager.build_context();
        assert!(context.contains("Use Rust only"));
        assert!(context.contains("user prefs"));
    }

    #[test]
    fn test_manager_session_write_load() {
        let tmp = tempfile::tempdir().unwrap();
        let manager = MemoryManager::new(tmp.path()).with_sessions_dir(tmp.path().join("sessions"));

        let uuid = manager
            .write_user_message("s1", Message::user("Hello"))
            .unwrap();
        manager
            .write_assistant_message("s1", Message::assistant("Hi!"), Some(&uuid))
            .unwrap();

        let messages = manager.load_session_messages("s1").unwrap();
        assert_eq!(messages.len(), 2);
        assert_eq!(messages[0].get_text().unwrap(), "Hello");
        assert_eq!(messages[1].get_text().unwrap(), "Hi!");
    }

    #[test]
    fn test_manager_recall_fallback() {
        let tmp = tempfile::tempdir().unwrap();
        let mem_dir = tmp.path().join("memory");
        std::fs::create_dir_all(&mem_dir).unwrap();
        std::fs::write(
            mem_dir.join("rust_tips.md"),
            "---\nname: Rust Tips\n---\n\nAlways use clippy for linting.",
        )
        .unwrap();
        std::fs::write(
            mem_dir.join("python_tips.md"),
            "---\nname: Python Tips\n---\n\nUse ruff for linting.",
        )
        .unwrap();

        let manager = MemoryManager::new(tmp.path()).with_memory_dir(mem_dir);

        let results = manager.recall("clippy", 10);
        assert_eq!(results.len(), 1);
        assert!(results[0].contains("clippy"));

        let results = manager.recall("linting", 10);
        assert_eq!(results.len(), 2);
    }

    #[test]
    fn test_manager_scan() {
        let tmp = tempfile::tempdir().unwrap();
        let mem_dir = tmp.path().join("memory");
        std::fs::create_dir_all(&mem_dir).unwrap();
        std::fs::write(mem_dir.join("a.md"), "content a").unwrap();
        std::fs::write(mem_dir.join("b.md"), "content b").unwrap();
        std::fs::write(mem_dir.join("MEMORY.md"), "index").unwrap();

        let manager = MemoryManager::new(tmp.path()).with_memory_dir(mem_dir);

        let metas = manager.scan();
        assert_eq!(metas.len(), 2); // excludes MEMORY.md
    }

    #[test]
    fn test_manager_list_sessions() {
        let tmp = tempfile::tempdir().unwrap();
        let sessions_dir = tmp.path().join("sessions");
        std::fs::create_dir_all(&sessions_dir).unwrap();
        std::fs::write(sessions_dir.join("s1.jsonl"), "{}").unwrap();
        std::fs::write(sessions_dir.join("s2.jsonl"), "{}").unwrap();
        std::fs::write(sessions_dir.join("not-a-session.txt"), "x").unwrap();

        let manager = MemoryManager::new(tmp.path()).with_sessions_dir(sessions_dir);

        let sessions = manager.list_sessions();
        assert_eq!(sessions.len(), 2);
    }
}