engram-core 0.19.0

AI Memory Infrastructure - Persistent memory for AI agents with semantic search
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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
//! Integration tests for the Engram watcher modules.
//!
//! Verifies that the file system watcher, app focus watcher, and configuration
//! modules work correctly together including real filesystem interaction via
//! temporary directories.
//!
//! Run with: cargo test --features watcher --test watcher_integration

#![cfg(feature = "watcher")]

use std::collections::VecDeque;
use std::path::PathBuf;
use std::sync::{Arc, Mutex};
use std::time::Duration;

use engram::watcher::{
    app_focus::{AppFocusWatcher, AppNameProvider, FocusEvent},
    config::{AppFocusConfig, BrowserWatcherConfig, FileWatcherConfig, WatcherConfig},
    fs_watcher::{ChangeKind, FileEvent, FsWatcher},
};

// ============================================================================
// Helpers
// ============================================================================

/// A mock [`AppNameProvider`] backed by a queue of names to return in order.
/// `None` entries simulate OS-level failures.
struct MockAppRunner {
    queue: Arc<Mutex<VecDeque<Option<String>>>>,
}

impl MockAppRunner {
    fn from_names(names: &[Option<&str>]) -> Self {
        let deque = names.iter().map(|n| n.map(|s| s.to_string())).collect();
        Self {
            queue: Arc::new(Mutex::new(deque)),
        }
    }
}

impl AppNameProvider for MockAppRunner {
    fn current_app(&self) -> Option<String> {
        self.queue.lock().unwrap().pop_front().flatten()
    }
}

fn app_focus_config_enabled() -> AppFocusConfig {
    AppFocusConfig {
        enabled: true,
        poll_interval_secs: 5,
        min_focus_secs: 0,
        exclude_apps: Vec::new(),
    }
}

// ============================================================================
// Test 1: FsWatcher detects file creation in a temp directory
// ============================================================================

#[test]
fn test_fs_watcher_detects_file_creation() {
    let dir = tempfile::tempdir().expect("failed to create temp dir");
    let watched_path = dir.path().to_path_buf();

    let config = FileWatcherConfig {
        enabled: true,
        paths: vec![watched_path.clone()],
        extensions: Vec::new(), // watch everything
        debounce_ms: 50,
        ignore_patterns: Vec::new(),
    };

    let received: Arc<Mutex<Vec<FileEvent>>> = Arc::new(Mutex::new(Vec::new()));
    let received_clone = Arc::clone(&received);

    let (watcher, stop_tx) = FsWatcher::new(config, move |event| {
        received_clone.lock().unwrap().push(event);
    })
    .expect("failed to create FsWatcher");

    let handle = std::thread::spawn(move || watcher.run());

    // Give the watcher a moment to start watching before creating a file.
    std::thread::sleep(Duration::from_millis(100));

    let new_file = watched_path.join("hello.txt");
    std::fs::write(&new_file, "engram watcher test").expect("failed to write test file");

    // Wait long enough for the debounce interval to fire (50 ms) plus margin.
    std::thread::sleep(Duration::from_millis(300));

    // Signal shutdown.
    stop_tx.send(()).ok();
    handle.join().expect("watcher thread should not panic");

    let events = received.lock().unwrap();
    assert!(
        !events.is_empty(),
        "expected at least one FileEvent for newly created file"
    );

    let found = events.iter().any(|e| {
        e.path.file_name().and_then(|n| n.to_str()) == Some("hello.txt")
            && e.kind == ChangeKind::Created
    });
    assert!(
        found,
        "expected a Created event for hello.txt, got: {:?}",
        events
            .iter()
            .map(|e| (&e.path, &e.kind))
            .collect::<Vec<_>>()
    );
}

// ============================================================================
// Test 2: FsWatcher respects extension filtering
// ============================================================================

#[test]
fn test_fs_watcher_extension_filtering() {
    let dir = tempfile::tempdir().expect("failed to create temp dir");
    let watched_path = dir.path().to_path_buf();

    let config = FileWatcherConfig {
        enabled: true,
        paths: vec![watched_path.clone()],
        extensions: vec!["md".to_string(), "txt".to_string()],
        debounce_ms: 50,
        ignore_patterns: Vec::new(),
    };

    let received: Arc<Mutex<Vec<FileEvent>>> = Arc::new(Mutex::new(Vec::new()));
    let received_clone = Arc::clone(&received);

    let (watcher, stop_tx) = FsWatcher::new(config, move |event| {
        received_clone.lock().unwrap().push(event);
    })
    .expect("failed to create FsWatcher");

    let handle = std::thread::spawn(move || watcher.run());

    std::thread::sleep(Duration::from_millis(100));

    // Create files with different extensions.
    std::fs::write(watched_path.join("notes.md"), "markdown").unwrap();
    std::fs::write(watched_path.join("readme.txt"), "text").unwrap();
    std::fs::write(watched_path.join("data.json"), "{}").unwrap(); // should be ignored
    std::fs::write(watched_path.join("image.png"), "binary").unwrap(); // should be ignored

    std::thread::sleep(Duration::from_millis(300));

    stop_tx.send(()).ok();
    handle.join().expect("watcher thread should not panic");

    let events = received.lock().unwrap();

    // All received events must be for .md or .txt files.
    for event in events.iter() {
        let ext = event
            .path
            .extension()
            .and_then(|e| e.to_str())
            .unwrap_or("");
        assert!(
            ext == "md" || ext == "txt",
            "received event for unexpected extension '{}': {:?}",
            ext,
            event.path
        );
    }

    // We must have received at least one event for a matching extension file.
    let has_md_or_txt = events.iter().any(|e| {
        matches!(
            e.path.extension().and_then(|x| x.to_str()),
            Some("md") | Some("txt")
        )
    });
    assert!(
        has_md_or_txt,
        "expected at least one event for .md or .txt files"
    );
}

// ============================================================================
// Test 3: FsWatcher respects ignore patterns
// ============================================================================

#[test]
fn test_fs_watcher_ignore_patterns() {
    let dir = tempfile::tempdir().expect("failed to create temp dir");
    let watched_path = dir.path().to_path_buf();

    // Create a subdirectory that should be ignored.
    let ignored_dir = watched_path.join(".git");
    std::fs::create_dir_all(&ignored_dir).unwrap();

    let config = FileWatcherConfig {
        enabled: true,
        paths: vec![watched_path.clone()],
        extensions: Vec::new(),
        debounce_ms: 50,
        ignore_patterns: vec![".git".to_string()],
    };

    let received: Arc<Mutex<Vec<FileEvent>>> = Arc::new(Mutex::new(Vec::new()));
    let received_clone = Arc::clone(&received);

    let (watcher, stop_tx) = FsWatcher::new(config, move |event| {
        received_clone.lock().unwrap().push(event);
    })
    .expect("failed to create FsWatcher");

    let handle = std::thread::spawn(move || watcher.run());

    std::thread::sleep(Duration::from_millis(100));

    // This file should be ignored.
    std::fs::write(ignored_dir.join("COMMIT_EDITMSG"), "initial commit").unwrap();

    // This file should be watched.
    std::fs::write(watched_path.join("main.rs"), "fn main() {}").unwrap();

    std::thread::sleep(Duration::from_millis(300));

    stop_tx.send(()).ok();
    handle.join().expect("watcher thread should not panic");

    let events = received.lock().unwrap();

    // No event should be for a path containing ".git".
    for event in events.iter() {
        let path_str = event.path.to_string_lossy();
        assert!(
            !path_str.contains(".git"),
            "received event for ignored path: {}",
            path_str
        );
    }
}

// ============================================================================
// Test 4: AppFocusWatcher with mock runner tracks focus changes
// ============================================================================

#[test]
fn test_app_focus_watcher_tracks_focus_changes() {
    let config = app_focus_config_enabled();
    let runner = MockAppRunner::from_names(&[
        Some("Safari"),
        Some("Safari"),
        Some("Terminal"),
        Some("Terminal"),
        Some("VSCode"),
    ]);

    let mut watcher = AppFocusWatcher::with_runner(config, Box::new(runner));

    // Tick 1: Safari starts.
    let e1 = watcher.tick();
    assert!(!e1, "first tick should not emit an event");
    assert_eq!(watcher.current_app(), Some("Safari"));

    // Tick 2: Still Safari.
    let e2 = watcher.tick();
    assert!(!e2, "same app should not emit an event");

    // Tick 3: Switch to Terminal — Safari session completes.
    let e3 = watcher.tick();
    assert!(e3, "app switch should emit a completed event");
    assert_eq!(watcher.current_app(), Some("Terminal"));

    let events = watcher.drain_completed_events();
    assert_eq!(events.len(), 1, "expected exactly one completed event");
    assert_eq!(events[0].app_name, "Safari");

    // Tick 4: Still Terminal.
    let e4 = watcher.tick();
    assert!(!e4);

    // Tick 5: Switch to VSCode — Terminal session completes.
    let e5 = watcher.tick();
    assert!(e5);

    let events2 = watcher.drain_completed_events();
    assert_eq!(events2.len(), 1);
    assert_eq!(events2[0].app_name, "Terminal");
    assert_eq!(watcher.current_app(), Some("VSCode"));
}

// ============================================================================
// Test 5: WatcherConfig round-trips through TOML serialization
// ============================================================================

#[test]
fn test_watcher_config_toml_round_trip() {
    let original = WatcherConfig {
        watched_directories: vec![PathBuf::from("/home/user/documents")],
        browser_history_enabled: false,
        app_focus_enabled: true,
        poll_interval_secs: 120,
        engram_url: "http://engram.example.com:4000".to_string(),
        api_key: Some("sk_test_integration".to_string()),
        workspace: "integration-tests".to_string(),
        ignore_patterns: vec!["*.tmp".to_string(), "node_modules".to_string()],
        file_watcher: FileWatcherConfig {
            enabled: true,
            paths: vec![PathBuf::from("/tmp/watch")],
            extensions: vec!["rs".to_string(), "toml".to_string()],
            debounce_ms: 250,
            ignore_patterns: vec!["target".to_string()],
        },
        browser: BrowserWatcherConfig {
            enabled: true,
            browsers: vec!["chrome".to_string()],
            poll_interval_secs: 30,
            exclude_patterns: vec!["localhost".to_string()],
        },
        app_focus: AppFocusConfig {
            enabled: true,
            poll_interval_secs: 10,
            min_focus_secs: 2,
            exclude_apps: vec!["Finder".to_string()],
        },
    };

    let serialized = toml::to_string(&original).expect("serialization to TOML should succeed");
    let deserialized: WatcherConfig =
        toml::from_str(&serialized).expect("deserialization from TOML should succeed");

    assert_eq!(
        deserialized.watched_directories,
        original.watched_directories
    );
    assert_eq!(
        deserialized.browser_history_enabled,
        original.browser_history_enabled
    );
    assert_eq!(deserialized.app_focus_enabled, original.app_focus_enabled);
    assert_eq!(deserialized.poll_interval_secs, original.poll_interval_secs);
    assert_eq!(deserialized.engram_url, original.engram_url);
    assert_eq!(deserialized.api_key, original.api_key);
    assert_eq!(deserialized.workspace, original.workspace);
    assert_eq!(deserialized.ignore_patterns, original.ignore_patterns);

    // file_watcher sub-section
    assert_eq!(
        deserialized.file_watcher.enabled,
        original.file_watcher.enabled
    );
    assert_eq!(deserialized.file_watcher.paths, original.file_watcher.paths);
    assert_eq!(
        deserialized.file_watcher.extensions,
        original.file_watcher.extensions
    );
    assert_eq!(
        deserialized.file_watcher.debounce_ms,
        original.file_watcher.debounce_ms
    );

    // browser sub-section
    assert_eq!(deserialized.browser.enabled, original.browser.enabled);
    assert_eq!(deserialized.browser.browsers, original.browser.browsers);
    assert_eq!(
        deserialized.browser.poll_interval_secs,
        original.browser.poll_interval_secs
    );

    // app_focus sub-section
    assert_eq!(deserialized.app_focus.enabled, original.app_focus.enabled);
    assert_eq!(
        deserialized.app_focus.poll_interval_secs,
        original.app_focus.poll_interval_secs
    );
    assert_eq!(
        deserialized.app_focus.min_focus_secs,
        original.app_focus.min_focus_secs
    );
    assert_eq!(
        deserialized.app_focus.exclude_apps,
        original.app_focus.exclude_apps
    );
}

// ============================================================================
// Test 6: Complete config file with all sections loads correctly
// ============================================================================

#[test]
fn test_full_config_file_loads_correctly() {
    let toml_content = r#"
watched_directories = ["/home/user/docs", "/home/user/code"]
browser_history_enabled = true
app_focus_enabled = true
poll_interval_secs = 60
engram_url = "http://localhost:4000"
api_key = "sk_live_abc123"
workspace = "my-workspace"
ignore_patterns = ["*.log", ".DS_Store"]

[file_watcher]
enabled = true
paths = ["/home/user/docs", "/home/user/code"]
extensions = ["md", "rs", "toml"]
debounce_ms = 200
ignore_patterns = [".git", "target", "node_modules"]

[browser]
enabled = true
browsers = ["chrome", "firefox", "safari"]
poll_interval_secs = 30
exclude_patterns = ["localhost", "127.0.0.1", "about:"]

[app_focus]
enabled = true
poll_interval_secs = 3
min_focus_secs = 2
exclude_apps = ["Finder", "loginwindow", "SystemUIServer"]
"#;

    // Write to a temp file and load it.
    let temp_file = tempfile::NamedTempFile::new().expect("failed to create temp file");
    std::fs::write(temp_file.path(), toml_content).expect("failed to write config file");

    let config = WatcherConfig::load(temp_file.path()).expect("config should load without error");

    // Top-level fields.
    assert_eq!(config.watched_directories.len(), 2);
    assert!(config.browser_history_enabled);
    assert!(config.app_focus_enabled);
    assert_eq!(config.poll_interval_secs, 60);
    assert_eq!(config.engram_url, "http://localhost:4000");
    assert_eq!(config.api_key.as_deref(), Some("sk_live_abc123"));
    assert_eq!(config.workspace, "my-workspace");
    assert_eq!(config.ignore_patterns, vec!["*.log", ".DS_Store"]);

    // [file_watcher] section.
    assert!(config.file_watcher.enabled);
    assert_eq!(config.file_watcher.paths.len(), 2);
    assert_eq!(config.file_watcher.extensions, vec!["md", "rs", "toml"]);
    assert_eq!(config.file_watcher.debounce_ms, 200);
    assert_eq!(config.file_watcher.ignore_patterns.len(), 3);

    // [browser] section.
    assert!(config.browser.enabled);
    assert_eq!(config.browser.browsers, vec!["chrome", "firefox", "safari"]);
    assert_eq!(config.browser.poll_interval_secs, 30);
    assert!(config
        .browser
        .exclude_patterns
        .contains(&"localhost".to_string()));

    // [app_focus] section.
    assert!(config.app_focus.enabled);
    assert_eq!(config.app_focus.poll_interval_secs, 3);
    assert_eq!(config.app_focus.min_focus_secs, 2);
    assert_eq!(
        config.app_focus.exclude_apps,
        vec!["Finder", "loginwindow", "SystemUIServer"]
    );
}

// ============================================================================
// Test 7: FileEvent.to_memory_content() produces well-formed content strings
// ============================================================================

#[test]
fn test_file_event_to_memory_content_format() {
    // Created event
    let created = FileEvent {
        path: PathBuf::from("/home/user/docs/notes.md"),
        kind: ChangeKind::Created,
        timestamp: "2026-03-09T12:00:00Z".to_string(),
    };
    let content = created.to_memory_content();
    assert!(
        content.starts_with("File created:"),
        "created event content should start with 'File created:': {content}"
    );
    assert!(
        content.contains("notes.md"),
        "content should include the filename: {content}"
    );
    assert!(
        content.contains("2026-03-09T12:00:00Z"),
        "content should include the timestamp: {content}"
    );

    // Modified event
    let modified = FileEvent {
        path: PathBuf::from("/project/src/main.rs"),
        kind: ChangeKind::Modified,
        timestamp: "2026-03-09T13:30:45Z".to_string(),
    };
    let content = modified.to_memory_content();
    assert!(
        content.starts_with("File modified:"),
        "modified event content should start with 'File modified:': {content}"
    );
    assert!(content.contains("main.rs"));
    assert!(content.contains("2026-03-09T13:30:45Z"));

    // Deleted event
    let deleted = FileEvent {
        path: PathBuf::from("/tmp/old_file.txt"),
        kind: ChangeKind::Deleted,
        timestamp: "2026-03-09T14:00:00Z".to_string(),
    };
    let content = deleted.to_memory_content();
    assert!(
        content.starts_with("File deleted:"),
        "deleted event content should start with 'File deleted:': {content}"
    );
    assert!(content.contains("old_file.txt"));
    assert!(content.contains("2026-03-09T14:00:00Z"));

    // Verify the format template: "File {kind}: {path} at {timestamp}"
    let expected_format = format!(
        "File deleted: {} at 2026-03-09T14:00:00Z",
        PathBuf::from("/tmp/old_file.txt").display()
    );
    assert_eq!(
        deleted.to_memory_content(),
        expected_format,
        "to_memory_content should follow the format 'File {{kind}}: {{path}} at {{timestamp}}'"
    );
}

// ============================================================================
// Additional integration: FocusEvent.to_memory_content() format
// ============================================================================

#[test]
fn test_focus_event_to_memory_content_format() {
    use chrono::DateTime;

    let started: chrono::DateTime<chrono::Utc> = "2026-03-09T09:00:00Z"
        .parse::<DateTime<chrono::Utc>>()
        .unwrap();
    let ended: chrono::DateTime<chrono::Utc> = "2026-03-09T09:02:30Z"
        .parse::<DateTime<chrono::Utc>>()
        .unwrap();

    let event = FocusEvent {
        app_name: "Cursor".to_string(),
        window_title: Some("engram — watcher_integration.rs".to_string()),
        started_at: started,
        ended_at: ended,
        duration_secs: 150,
    };

    let content = event.to_memory_content();

    assert!(
        content.contains("Cursor"),
        "content should include the app name: {content}"
    );
    assert!(
        content.contains("engram"),
        "content should include the window title: {content}"
    );
    assert!(
        content.contains("150"),
        "content should include the duration in seconds: {content}"
    );
    assert!(
        content.contains("2026-03-09T09:00:00Z"),
        "content should include start timestamp: {content}"
    );
    assert!(
        content.contains("2026-03-09T09:02:30Z"),
        "content should include end timestamp: {content}"
    );

    // Without window title
    let event_no_title = FocusEvent {
        app_name: "iTerm2".to_string(),
        window_title: None,
        started_at: started,
        ended_at: ended,
        duration_secs: 150,
    };
    let content_no_title = event_no_title.to_memory_content();
    assert!(
        content_no_title.contains("iTerm2"),
        "content should include the app name even without window title: {content_no_title}"
    );
    assert!(
        !content_no_title.contains("None"),
        "content should not contain the string 'None' when window_title is None: {content_no_title}"
    );
}