agent-diva-core 0.5.0

Core types and traits for agent-diva
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
//! Memory manager for handling long-term memory

use super::provider::{
    MemoryProvider, PrefetchRequest, PrefetchResponse, PrefetchStatus, SessionEndRequest,
    SessionEndResponse, SessionEndStatus, StartupInjectionShape, SyncTurnRequest,
    SyncTurnResponse, SyncTurnStatus, SystemPromptBlock, SystemPromptRequest,
    SystemPromptResponse,
};
use super::storage::{DailyNote, Memory};
use parking_lot::Mutex;
use std::collections::HashSet;
use std::path::{Path, PathBuf};

/// Manages long-term memory storage
#[derive(Debug)]
pub struct MemoryManager {
    /// Workspace directory
    _workspace: PathBuf,
    /// Memory file path
    memory_path: PathBuf,
    /// Daily notes directory
    notes_dir: PathBuf,
    /// History file path
    history_path: PathBuf,
    /// Session IDs whose shutdown hook has already been handled.
    handled_session_end_ids: Mutex<HashSet<String>>,
}

impl MemoryManager {
    /// Create a new memory manager
    pub fn new<P: AsRef<Path>>(workspace: P) -> Self {
        let workspace = workspace.as_ref().to_path_buf();
        let memory_path = workspace.join("memory").join("MEMORY.md");
        let history_path = workspace.join("memory").join("HISTORY.md");
        let notes_dir = workspace.join("memory");

        Self {
            _workspace: workspace,
            memory_path,
            notes_dir,
            history_path,
            handled_session_end_ids: Mutex::new(HashSet::new()),
        }
    }

    /// Load the long-term memory
    pub fn load_memory(&self) -> Memory {
        if self.memory_path.exists() {
            match std::fs::read_to_string(&self.memory_path) {
                Ok(content) => Memory::with_content(content),
                Err(_) => Memory::new(),
            }
        } else {
            Memory::new()
        }
    }

    /// Save the long-term memory
    pub fn save_memory(&self, memory: &Memory) -> crate::Result<()> {
        if let Some(parent) = self.memory_path.parent() {
            std::fs::create_dir_all(parent)?;
        }
        std::fs::write(&self.memory_path, &memory.content)?;
        Ok(())
    }

    /// Load history entries from `HISTORY.md`
    pub fn load_history(&self) -> String {
        if self.history_path.exists() {
            std::fs::read_to_string(&self.history_path).unwrap_or_default()
        } else {
            String::new()
        }
    }

    /// Append an entry to `HISTORY.md`
    pub fn append_history(&self, entry: &str) -> crate::Result<()> {
        if entry.trim().is_empty() {
            return Ok(());
        }
        if let Some(parent) = self.history_path.parent() {
            std::fs::create_dir_all(parent)?;
        }
        let mut content = self.load_history();
        if !content.is_empty() && !content.ends_with('\n') {
            content.push('\n');
        }
        content.push_str(entry.trim_end());
        content.push_str("\n\n");
        std::fs::write(&self.history_path, content)?;
        Ok(())
    }

    /// Load a daily note
    pub fn load_daily_note(&self, date: impl AsRef<str>) -> DailyNote {
        let date = date.as_ref();
        let path = self.notes_dir.join(format!("{}.md", date));

        if path.exists() {
            match std::fs::read_to_string(&path) {
                Ok(content) => {
                    let mut note = DailyNote::for_date(date);
                    note.content = content;
                    note
                }
                Err(_) => DailyNote::for_date(date),
            }
        } else {
            DailyNote::for_date(date)
        }
    }

    /// Load today's note
    pub fn load_today_note(&self) -> DailyNote {
        let today = chrono::Local::now().format("%Y-%m-%d").to_string();
        self.load_daily_note(&today)
    }

    /// Save a daily note
    pub fn save_daily_note(&self, note: &DailyNote) -> crate::Result<()> {
        std::fs::create_dir_all(&self.notes_dir)?;
        let path = self.notes_dir.join(note.filename());
        std::fs::write(&path, &note.content)?;
        Ok(())
    }

    /// List all daily notes
    pub fn list_notes(&self) -> Vec<String> {
        let mut notes = Vec::new();

        if let Ok(entries) = std::fs::read_dir(&self.notes_dir) {
            for entry in entries.flatten() {
                if let Some(name) = entry.file_name().to_str() {
                    if name.ends_with(".md") && name != "MEMORY.md" {
                        let date = name.trim_end_matches(".md").to_string();
                        notes.push(date);
                    }
                }
            }
        }

        notes.sort_by(|a, b| b.cmp(a)); // Newest first
        notes
    }

    /// Get the memory directory path
    pub fn memory_dir(&self) -> &Path {
        &self.notes_dir
    }

    /// Append content to today's daily note
    pub fn append_today(&self, content: &str) -> crate::Result<()> {
        let mut note = self.load_today_note();

        if note.content.is_empty() {
            // Add header for new day
            let today = chrono::Local::now().format("%Y-%m-%d").to_string();
            note.content = format!("# {}\n\n{}", today, content);
        } else {
            // Append to existing content
            note.content.push('\n');
            note.content.push_str(content);
        }

        self.save_daily_note(&note)
    }

    /// Get memories from the last N days
    pub fn get_recent_memories(&self, days: usize) -> String {
        use chrono::Duration;

        let mut memories = Vec::new();
        let today = chrono::Local::now().date_naive();

        for i in 0..days {
            let date = today - Duration::days(i as i64);
            let date_str = date.format("%Y-%m-%d").to_string();
            let note = self.load_daily_note(&date_str);

            if !note.content.is_empty() {
                memories.push(note.content);
            }
        }

        memories.join("\n\n---\n\n")
    }

    /// List all memory files sorted by date (newest first)
    pub fn list_memory_files(&self) -> Vec<PathBuf> {
        let mut files = Vec::new();

        if let Ok(entries) = std::fs::read_dir(&self.notes_dir) {
            for entry in entries.flatten() {
                let path = entry.path();
                if let Some(name) = path.file_name().and_then(|n| n.to_str()) {
                    // Match pattern YYYY-MM-DD.md
                    if name.len() == 13 && name.ends_with(".md") && name != "MEMORY.md" {
                        let date_part = &name[..10];
                        // Basic validation: check if it looks like a date
                        if date_part.chars().filter(|c| *c == '-').count() == 2 {
                            files.push(path);
                        }
                    }
                }
            }
        }

        // Sort by filename (which is the date) in reverse order
        files.sort_by(|a, b| b.cmp(a));
        files
    }

    /// Get memory context for the agent.
    /// The redesigned memory model injects only long-term memory into prompts.
    pub fn get_memory_context(&self) -> String {
        let memory = self.load_memory();
        if memory.content.is_empty() {
            String::new()
        } else {
            format!("## Long-term Memory\n{}", memory.content)
        }
    }

}

#[async_trait::async_trait]
impl MemoryProvider for MemoryManager {
    fn system_prompt_block(
        &self,
        _request: &SystemPromptRequest,
    ) -> crate::Result<SystemPromptResponse> {
        let context = self.get_memory_context();
        if context.is_empty() {
            Ok(SystemPromptResponse::degraded(
                "startup continuity unavailable; no long-term memory available",
            ))
        } else {
            Ok(SystemPromptResponse::ready(SystemPromptBlock {
                shape: StartupInjectionShape::CompactRenderedMarkdown,
                markdown: context,
            }))
        }
    }

    async fn prefetch(&self, request: PrefetchRequest) -> crate::Result<PrefetchResponse> {
        if request.intent.trim().is_empty() {
            return Ok(PrefetchResponse {
                status: PrefetchStatus::SkippedNoIntent,
                prompt_block: None,
            });
        }

        Ok(PrefetchResponse {
            status: PrefetchStatus::Failed {
                reason: format!(
                    "prefetch recall is unavailable in the default MemoryManager for intent '{}'",
                    request.intent.trim()
                ),
            },
            prompt_block: None,
        })
    }

    async fn sync_turn(&self, request: SyncTurnRequest) -> crate::Result<SyncTurnResponse> {
        let mut persisted = false;

        if let Some(memory_update) = request.memory_update_markdown.as_deref() {
            if !memory_update.trim().is_empty() {
                let memory = Memory::with_content(memory_update);
                if let Err(err) = self.save_memory(&memory) {
                    return Ok(SyncTurnResponse {
                        status: SyncTurnStatus::Failed {
                            reason: format!("failed to persist MEMORY.md: {err}"),
                        },
                    });
                }
                persisted = true;
            }
        }

        if let Some(history_entry) = request.history_entry.as_deref() {
            if !history_entry.trim().is_empty() {
                if let Err(err) = self.append_history(history_entry) {
                    return Ok(SyncTurnResponse {
                        status: SyncTurnStatus::Failed {
                            reason: format!("failed to append HISTORY.md: {err}"),
                        },
                    });
                }
                persisted = true;
            }
        }

        Ok(SyncTurnResponse {
            status: if persisted {
                SyncTurnStatus::Persisted
            } else {
                SyncTurnStatus::Noop
            },
        })
    }

    async fn on_session_end(
        &self,
        request: SessionEndRequest,
    ) -> crate::Result<SessionEndResponse> {
        if let Some(session_id) = request.session_id {
            let mut handled = self.handled_session_end_ids.lock();
            if !handled.insert(session_id) {
                return Ok(SessionEndResponse {
                    status: SessionEndStatus::AlreadyHandled,
                });
            }
        }

        Ok(SessionEndResponse {
            status: SessionEndStatus::Noop,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::memory::{
        MemoryProvider, PrefetchRequest, PrefetchStatus, SessionEndRequest, SessionEndStatus,
        StartupStatus, SyncTurnRequest, SyncTurnStatus, SystemPromptRequest,
    };
    use tempfile::TempDir;

    #[test]
    fn test_append_history() {
        let temp_dir = TempDir::new().unwrap();
        let manager = MemoryManager::new(temp_dir.path());
        manager
            .append_history("[2026-02-12 09:00] Added memory event")
            .unwrap();

        let history = manager.load_history();
        assert!(history.contains("Added memory event"));
    }

    #[tokio::test]
    async fn test_memory_provider_system_prompt_block_reads_memory() {
        let temp_dir = TempDir::new().unwrap();
        let manager = MemoryManager::new(temp_dir.path());
        manager
            .save_memory(&Memory::with_content("Long term provider context"))
            .unwrap();

        let response = manager
            .system_prompt_block(&SystemPromptRequest {
                workspace_root: temp_dir.path().to_path_buf(),
            })
            .unwrap();

        assert_eq!(response.status, StartupStatus::Ready);

        let block = response
            .prompt_block
            .expect("memory-backed provider should emit a prompt block");

        assert!(block.markdown.contains("## Long-term Memory"));
        assert!(block.markdown.contains("Long term provider context"));
        assert_eq!(
            block.shape,
            crate::memory::StartupInjectionShape::CompactRenderedMarkdown
        );
    }

    #[tokio::test]
    async fn test_memory_provider_sync_turn_persists_memory_and_history() {
        let temp_dir = TempDir::new().unwrap();
        let manager = MemoryManager::new(temp_dir.path());

        let result = manager
            .sync_turn(SyncTurnRequest {
                workspace_root: temp_dir.path().to_path_buf(),
                memory_update_markdown: Some("Updated memory from sync_turn".to_string()),
                history_entry: Some("[2026-05-08 10:00 UTC] synchronized turn".to_string()),
            })
            .await
            .unwrap();

        assert_eq!(result.status, SyncTurnStatus::Persisted);
        assert_eq!(manager.load_memory().content, "Updated memory from sync_turn");
        assert!(manager.load_history().contains("synchronized turn"));
    }

    #[tokio::test]
    async fn test_memory_provider_session_end_is_noop_by_default() {
        let temp_dir = TempDir::new().unwrap();
        let manager = MemoryManager::new(temp_dir.path());

        let response = manager
            .on_session_end(SessionEndRequest {
                workspace_root: temp_dir.path().to_path_buf(),
                session_id: Some("session-1".to_string()),
            })
            .await
            .unwrap();

        assert_eq!(response.status, SessionEndStatus::Noop);
    }

    #[tokio::test]
    async fn test_memory_provider_returns_degraded_startup_when_no_context_is_available() {
        let temp_dir = TempDir::new().unwrap();
        let manager = MemoryManager::new(temp_dir.path());

        let response = manager
            .system_prompt_block(&SystemPromptRequest {
                workspace_root: temp_dir.path().to_path_buf(),
            })
            .unwrap();

        match response.status {
            StartupStatus::Degraded {
                reason,
                last_usable_wakeup,
            } => {
                assert!(reason.contains("startup continuity unavailable"));
                assert!(last_usable_wakeup.is_none());
            }
            other => panic!("expected degraded startup, got {other:?}"),
        }

        let block = response
            .prompt_block
            .expect("degraded startup should still provide explicit startup text");
        assert!(block.markdown.contains("status: degraded"));
        assert!(block.markdown.contains("last_usable_wakeup: omitted"));
    }

    #[tokio::test]
    async fn test_memory_provider_prefetch_distinguishes_no_intent_from_failed_recall() {
        let temp_dir = TempDir::new().unwrap();
        let manager = MemoryManager::new(temp_dir.path());

        let skipped = manager
            .prefetch(PrefetchRequest {
                workspace_root: temp_dir.path().to_path_buf(),
                intent: "   ".to_string(),
                current_room: None,
                user_message: Some("help".to_string()),
            })
            .await
            .unwrap();
        assert_eq!(skipped.status, PrefetchStatus::SkippedNoIntent);
        assert!(skipped.prompt_block.is_none());

        let failed = manager
            .prefetch(PrefetchRequest {
                workspace_root: temp_dir.path().to_path_buf(),
                intent: "recall-project-status".to_string(),
                current_room: Some("roadmap".to_string()),
                user_message: Some("what changed?".to_string()),
            })
            .await
            .unwrap();
        match failed.status {
            PrefetchStatus::Failed { reason } => {
                assert!(reason.contains("prefetch recall is unavailable"));
            }
            other => panic!("expected failed recall, got {other:?}"),
        }
        assert!(failed.prompt_block.is_none());
    }

    #[tokio::test]
    async fn test_memory_provider_sync_turn_failure_is_explicit() {
        let temp_dir = TempDir::new().unwrap();
        let workspace = temp_dir.path().to_path_buf();
        std::fs::create_dir_all(workspace.join("memory")).unwrap();
        std::fs::write(workspace.join("memory").join("MEMORY.md"), "locked").unwrap();
        std::fs::remove_file(workspace.join("memory").join("MEMORY.md")).unwrap();
        std::fs::create_dir_all(workspace.join("memory").join("MEMORY.md")).unwrap();

        let manager = MemoryManager::new(&workspace);
        let result = manager
            .sync_turn(SyncTurnRequest {
                workspace_root: workspace,
                memory_update_markdown: Some("cannot persist".to_string()),
                history_entry: None,
            })
            .await
            .unwrap();

        match result.status {
            SyncTurnStatus::Failed { reason } => {
                assert!(reason.contains("failed to persist MEMORY.md"));
            }
            other => panic!("expected sync failure, got {other:?}"),
        }
    }

    #[tokio::test]
    async fn test_memory_provider_session_end_is_idempotent_for_duplicates() {
        let temp_dir = TempDir::new().unwrap();
        let manager = MemoryManager::new(temp_dir.path());

        let first = manager
            .on_session_end(SessionEndRequest {
                workspace_root: temp_dir.path().to_path_buf(),
                session_id: Some("session-dup".to_string()),
            })
            .await
            .unwrap();
        assert_eq!(first.status, SessionEndStatus::Noop);

        let duplicate = manager
            .on_session_end(SessionEndRequest {
                workspace_root: temp_dir.path().to_path_buf(),
                session_id: Some("session-dup".to_string()),
            })
            .await
            .unwrap();
        assert_eq!(duplicate.status, SessionEndStatus::AlreadyHandled);
    }
}