engram-core 0.16.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
//! File system watcher module
//!
//! Monitors configured directories for file changes and invokes a callback with
//! [`FileEvent`] values that callers use to create Engram memories.
//!
//! # Usage
//!
//! ```rust,ignore
//! use engram::watcher::{FileWatcherConfig, FsWatcher};
//! use std::path::PathBuf;
//!
//! let config = FileWatcherConfig {
//!     enabled: true,
//!     paths: vec![PathBuf::from("/tmp/notes")],
//!     extensions: vec!["md".to_string(), "txt".to_string()],
//!     debounce_ms: 500,
//!     ignore_patterns: vec![".git".to_string()],
//! };
//!
//! let (watcher, stop_tx) = FsWatcher::new(config, |event| {
//!     println!("File event: {:?}", event);
//! }).expect("failed to create watcher");
//!
//! // Run in a dedicated thread
//! let handle = std::thread::spawn(move || watcher.run());
//!
//! // Signal shutdown
//! stop_tx.send(()).ok();
//! handle.join().ok();
//! ```

use std::{
    collections::HashMap,
    path::{Path, PathBuf},
    sync::mpsc,
    time::{Duration, Instant},
};

use notify::{Event, EventKind, RecursiveMode, Watcher};
use tracing::{debug, error, warn};

use super::config::FileWatcherConfig;
use crate::error::{EngramError, Result};

// ---------------------------------------------------------------------------
// Public types
// ---------------------------------------------------------------------------

/// The kind of change that was detected on a file.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ChangeKind {
    /// A file was created.
    Created,
    /// A file was modified.
    Modified,
    /// A file was deleted.
    Deleted,
}

impl std::fmt::Display for ChangeKind {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ChangeKind::Created => write!(f, "created"),
            ChangeKind::Modified => write!(f, "modified"),
            ChangeKind::Deleted => write!(f, "deleted"),
        }
    }
}

/// A filesystem change event delivered to the callback.
#[derive(Debug, Clone)]
pub struct FileEvent {
    /// Absolute path to the changed file.
    pub path: PathBuf,
    /// Nature of the change.
    pub kind: ChangeKind,
    /// RFC3339 UTC timestamp when the event was detected.
    pub timestamp: String,
}

impl FileEvent {
    /// Build the memory content string for this event.
    ///
    /// Format: `"File {kind}: {path} at {timestamp}"`
    pub fn to_memory_content(&self) -> String {
        format!(
            "File {}: {} at {}",
            self.kind,
            self.path.display(),
            self.timestamp
        )
    }
}

// ---------------------------------------------------------------------------
// Internal types
// ---------------------------------------------------------------------------

/// A pending (debounced) event waiting to fire.
#[derive(Debug)]
struct PendingEvent {
    kind: ChangeKind,
    earliest_fire_at: Instant,
}

// ---------------------------------------------------------------------------
// FsWatcher
// ---------------------------------------------------------------------------

/// File system watcher that monitors directories for changes.
///
/// Create with [`FsWatcher::new`], then call [`FsWatcher::run`] in a dedicated
/// thread.  Send `()` on the stop channel to shut down gracefully.
pub struct FsWatcher<F>
where
    F: Fn(FileEvent) + Send + 'static,
{
    config: FileWatcherConfig,
    callback: F,
    stop_rx: mpsc::Receiver<()>,
    event_rx: mpsc::Receiver<notify::Result<Event>>,
    /// Keep the underlying watcher alive for its full lifetime.
    _watcher: Box<dyn Watcher + Send>,
}

impl<F> FsWatcher<F>
where
    F: Fn(FileEvent) + Send + 'static,
{
    /// Create a new `FsWatcher`.
    ///
    /// Returns `(watcher, stop_tx)`.  Call [`FsWatcher::run`] (usually in a
    /// dedicated thread) and drop `stop_tx` or send `()` to initiate graceful
    /// shutdown.
    ///
    /// # Errors
    ///
    /// Returns an error if the underlying `notify` watcher cannot be created or
    /// if any of the configured paths cannot be watched.
    pub fn new(config: FileWatcherConfig, callback: F) -> Result<(Self, mpsc::SyncSender<()>)> {
        let (event_tx, event_rx) = mpsc::channel::<notify::Result<Event>>();

        let mut watcher = notify::recommended_watcher(move |res| {
            // Ignore send errors — they only occur after the receiver has been
            // dropped (i.e. after FsWatcher is shut down).
            let _ = event_tx.send(res);
        })
        .map_err(|e| EngramError::Config(format!("Cannot create filesystem watcher: {e}")))?;

        for path in &config.paths {
            if !path.exists() {
                warn!(path = ?path, "Watched path does not exist; skipping");
                continue;
            }
            watcher
                .watch(path, RecursiveMode::Recursive)
                .map_err(|e| EngramError::Config(format!("Cannot watch path {:?}: {e}", path)))?;

            debug!(path = ?path, "Watching path");
        }

        let (stop_tx, stop_rx) = mpsc::sync_channel::<()>(1);

        let fs_watcher = Self {
            config,
            callback,
            stop_rx,
            event_rx,
            _watcher: Box::new(watcher),
        };

        Ok((fs_watcher, stop_tx))
    }

    /// Run the watcher event loop until a stop signal is received.
    ///
    /// This method blocks the calling thread.  Run it in a dedicated
    /// `std::thread::spawn` call.
    pub fn run(self) {
        if !self.config.enabled {
            debug!("File watcher is disabled; exiting immediately");
            return;
        }

        let debounce = Duration::from_millis(self.config.debounce_ms);
        // path → pending change
        let mut pending: HashMap<PathBuf, PendingEvent> = HashMap::new();

        loop {
            // Wait for the next event (or until the next pending event is due).
            let recv_timeout = Self::next_fire_delay(&pending, debounce)
                .unwrap_or_else(|| Duration::from_millis(50));

            match self.event_rx.recv_timeout(recv_timeout) {
                Ok(Ok(event)) => {
                    self.handle_raw_event(event, debounce, &mut pending);
                }
                Ok(Err(e)) => {
                    error!(error = %e, "Notify watcher error");
                }
                Err(mpsc::RecvTimeoutError::Timeout) => {
                    // Fall through to flush pending events.
                }
                Err(mpsc::RecvTimeoutError::Disconnected) => {
                    debug!("Event channel disconnected; shutting down");
                    break;
                }
            }

            // Deliver any debounced events whose deadline has passed.
            self.flush_pending(&mut pending);

            // Check for stop signal (non-blocking).
            match self.stop_rx.try_recv() {
                Ok(()) | Err(mpsc::TryRecvError::Disconnected) => {
                    debug!("Stop signal received; shutting down file watcher");
                    break;
                }
                Err(mpsc::TryRecvError::Empty) => {}
            }
        }
    }

    // -----------------------------------------------------------------------
    // Private helpers
    // -----------------------------------------------------------------------

    /// Convert a raw `notify` event into a pending debounced entry.
    fn handle_raw_event(
        &self,
        event: Event,
        debounce: Duration,
        pending: &mut HashMap<PathBuf, PendingEvent>,
    ) {
        let kind = match classify_event_kind(&event.kind) {
            Some(k) => k,
            None => return,
        };

        for path in &event.paths {
            if !self.should_watch(path) {
                continue;
            }

            let fire_at = Instant::now() + debounce;

            pending
                .entry(path.clone())
                .and_modify(|p| {
                    // Keep the highest-priority change kind.
                    if kind_priority(&kind) > kind_priority(&p.kind) {
                        p.kind = kind.clone();
                    }
                    // Always push the deadline forward so rapid events stay debounced.
                    p.earliest_fire_at = fire_at;
                })
                .or_insert(PendingEvent {
                    kind: kind.clone(),
                    earliest_fire_at: fire_at,
                });
        }
    }

    /// Fire all pending events whose deadline has passed.
    fn flush_pending(&self, pending: &mut HashMap<PathBuf, PendingEvent>) {
        let now = Instant::now();
        let ready: Vec<PathBuf> = pending
            .iter()
            .filter(|(_, p)| now >= p.earliest_fire_at)
            .map(|(path, _)| path.clone())
            .collect();

        for path in ready {
            if let Some(p) = pending.remove(&path) {
                let event = FileEvent {
                    path,
                    kind: p.kind,
                    timestamp: chrono::Utc::now().to_rfc3339(),
                };
                debug!(path = ?event.path, kind = ?event.kind, "Firing debounced file event");
                (self.callback)(event);
            }
        }
    }

    /// Duration until the next pending event fires, capped at one debounce interval.
    fn next_fire_delay(
        pending: &HashMap<PathBuf, PendingEvent>,
        debounce: Duration,
    ) -> Option<Duration> {
        pending
            .values()
            .map(|p| p.earliest_fire_at)
            .min()
            .map(|earliest| {
                let now = Instant::now();
                if earliest > now {
                    (earliest - now).min(debounce)
                } else {
                    Duration::ZERO
                }
            })
    }

    /// Returns `true` if this path should generate an event.
    ///
    /// Checks the extension filter (empty = watch all) and then the ignore
    /// patterns (simple substring match against the full path string).
    pub(crate) fn should_watch(&self, path: &Path) -> bool {
        // Extension filter
        if !self.config.extensions.is_empty() {
            let ext = path.extension().and_then(|e| e.to_str()).unwrap_or("");
            if !self.config.extensions.iter().any(|e| e == ext) {
                return false;
            }
        }

        // Ignore patterns
        let path_str = path.to_string_lossy();
        for pattern in &self.config.ignore_patterns {
            if path_str.contains(pattern.as_str()) {
                return false;
            }
        }

        true
    }
}

// ---------------------------------------------------------------------------
// Free helpers
// ---------------------------------------------------------------------------

/// Map `notify::EventKind` to our simpler [`ChangeKind`].
pub(crate) fn classify_event_kind(kind: &EventKind) -> Option<ChangeKind> {
    match kind {
        EventKind::Create(_) => Some(ChangeKind::Created),
        EventKind::Modify(_) => Some(ChangeKind::Modified),
        EventKind::Remove(_) => Some(ChangeKind::Deleted),
        _ => None,
    }
}

/// Higher number = higher priority (kept in the pending map).
fn kind_priority(kind: &ChangeKind) -> u8 {
    match kind {
        ChangeKind::Deleted => 3,
        ChangeKind::Created => 2,
        ChangeKind::Modified => 1,
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use notify::{
        event::{CreateKind, ModifyKind, RemoveKind},
        Config as NotifyConfig, NullWatcher,
    };

    // ---- Helpers -----------------------------------------------------------

    /// Build an FsWatcher with a NullWatcher backend (no real FS watching).
    ///
    /// The stop channel is dropped immediately so `run()` exits on the first
    /// check.  The event channel is also closed so the loop exits fast.
    fn make_test_watcher(
        config: FileWatcherConfig,
    ) -> FsWatcher<impl Fn(FileEvent) + Send + 'static> {
        let (_event_tx, event_rx) = mpsc::channel::<notify::Result<Event>>();
        let (_stop_tx, stop_rx) = mpsc::sync_channel::<()>(1);

        let null_watcher = NullWatcher::new(|_: notify::Result<Event>| {}, NotifyConfig::default())
            .expect("NullWatcher should always succeed");

        FsWatcher {
            config,
            callback: |_: FileEvent| {},
            stop_rx,
            event_rx,
            _watcher: Box::new(null_watcher),
        }
    }

    fn config_with(extensions: Vec<&str>, ignore: Vec<&str>) -> FileWatcherConfig {
        FileWatcherConfig {
            enabled: true,
            paths: Vec::new(),
            extensions: extensions.into_iter().map(String::from).collect(),
            debounce_ms: 50,
            ignore_patterns: ignore.into_iter().map(String::from).collect(),
        }
    }

    // ---- classify_event_kind -----------------------------------------------

    #[test]
    fn test_classify_create_event() {
        let kind = EventKind::Create(CreateKind::File);
        assert_eq!(classify_event_kind(&kind), Some(ChangeKind::Created));
    }

    #[test]
    fn test_classify_modify_event() {
        let kind = EventKind::Modify(ModifyKind::Any);
        assert_eq!(classify_event_kind(&kind), Some(ChangeKind::Modified));
    }

    #[test]
    fn test_classify_remove_event() {
        let kind = EventKind::Remove(RemoveKind::File);
        assert_eq!(classify_event_kind(&kind), Some(ChangeKind::Deleted));
    }

    #[test]
    fn test_classify_access_event_returns_none() {
        let kind = EventKind::Access(notify::event::AccessKind::Any);
        assert!(classify_event_kind(&kind).is_none());
    }

    #[test]
    fn test_classify_other_event_returns_none() {
        assert!(classify_event_kind(&EventKind::Other).is_none());
    }

    // ---- Extension filter --------------------------------------------------

    #[test]
    fn test_extension_filter_passes_matching_extension() {
        let w = make_test_watcher(config_with(vec!["rs", "md"], vec![]));
        assert!(w.should_watch(Path::new("/home/user/notes/README.md")));
        assert!(w.should_watch(Path::new("/project/src/main.rs")));
    }

    #[test]
    fn test_extension_filter_rejects_non_matching_extension() {
        let w = make_test_watcher(config_with(vec!["rs", "md"], vec![]));
        assert!(!w.should_watch(Path::new("/project/image.png")));
        assert!(!w.should_watch(Path::new("/project/data.json")));
    }

    #[test]
    fn test_empty_extension_list_passes_all() {
        let w = make_test_watcher(config_with(vec![], vec![]));
        assert!(w.should_watch(Path::new("/anything/file.xyz")));
        assert!(w.should_watch(Path::new("/no-extension")));
    }

    // ---- Ignore patterns ---------------------------------------------------

    #[test]
    fn test_ignore_pattern_rejects_matching_path() {
        let w = make_test_watcher(config_with(vec![], vec![".git", "node_modules"]));
        assert!(!w.should_watch(Path::new("/project/.git/config")));
        assert!(!w.should_watch(Path::new("/project/node_modules/lodash/index.js")));
    }

    #[test]
    fn test_ignore_pattern_passes_non_matching_path() {
        let w = make_test_watcher(config_with(vec![], vec![".git"]));
        assert!(w.should_watch(Path::new("/project/src/main.rs")));
    }

    #[test]
    fn test_extension_and_ignore_combined() {
        let w = make_test_watcher(config_with(vec!["rs"], vec!["target"]));
        // Good: right extension, not in ignored dir
        assert!(w.should_watch(Path::new("/project/src/lib.rs")));
        // Bad: right extension but under ignored dir
        assert!(!w.should_watch(Path::new("/project/target/debug/build/foo.rs")));
        // Bad: wrong extension
        assert!(!w.should_watch(Path::new("/project/src/style.css")));
    }

    // ---- FileEvent --------------------------------------------------------

    #[test]
    fn test_file_event_to_memory_content() {
        let event = FileEvent {
            path: PathBuf::from("/home/user/notes/README.md"),
            kind: ChangeKind::Modified,
            timestamp: "2026-03-09T00:00:00Z".to_string(),
        };
        let content = event.to_memory_content();
        assert!(content.contains("modified"), "content: {content}");
        assert!(content.contains("README.md"), "content: {content}");
        assert!(
            content.contains("2026-03-09T00:00:00Z"),
            "content: {content}"
        );
    }

    #[test]
    fn test_change_kind_display() {
        assert_eq!(ChangeKind::Created.to_string(), "created");
        assert_eq!(ChangeKind::Modified.to_string(), "modified");
        assert_eq!(ChangeKind::Deleted.to_string(), "deleted");
    }

    // ---- Debounce helpers -------------------------------------------------

    #[test]
    fn test_next_fire_delay_empty_returns_none() {
        let pending: HashMap<PathBuf, PendingEvent> = HashMap::new();
        assert!(
            FsWatcher::<fn(FileEvent)>::next_fire_delay(&pending, Duration::from_millis(500))
                .is_none()
        );
    }

    #[test]
    fn test_next_fire_delay_with_entry_returns_some_bounded_by_debounce() {
        let debounce = Duration::from_millis(500);
        let mut pending: HashMap<PathBuf, PendingEvent> = HashMap::new();
        pending.insert(
            PathBuf::from("/tmp/file.txt"),
            PendingEvent {
                kind: ChangeKind::Modified,
                earliest_fire_at: Instant::now() + Duration::from_millis(200),
            },
        );
        let delay = FsWatcher::<fn(FileEvent)>::next_fire_delay(&pending, debounce)
            .expect("should be Some");
        assert!(
            delay <= debounce,
            "delay {delay:?} should be <= debounce {debounce:?}"
        );
    }

    // ---- Disabled watcher -------------------------------------------------

    #[test]
    fn test_disabled_watcher_run_returns_immediately() {
        let (_event_tx, event_rx) = mpsc::channel::<notify::Result<Event>>();
        let (_stop_tx, stop_rx) = mpsc::sync_channel::<()>(1);

        let null_watcher = NullWatcher::new(|_: notify::Result<Event>| {}, NotifyConfig::default())
            .expect("NullWatcher should always succeed");

        let watcher = FsWatcher {
            config: FileWatcherConfig {
                enabled: false,
                ..FileWatcherConfig::default()
            },
            callback: |_: FileEvent| {},
            stop_rx,
            event_rx,
            _watcher: Box::new(null_watcher),
        };

        let handle = std::thread::spawn(move || watcher.run());
        handle
            .join()
            .expect("disabled watcher thread should not panic");
    }
}