Skip to main content

second_brain_api/
watcher.rs

1use std::collections::HashMap;
2use std::path::PathBuf;
3use std::sync::Arc;
4use std::time::{Duration, Instant};
5
6use notify::{EventKind, RecursiveMode, Watcher};
7
8use second_brain_core::embedding::Embedder;
9use second_brain_core::store::StoreAdmin;
10
11use crate::ingest::{ingest_file, ingest_memory_file};
12
13const INITIAL_BACKOFF: Duration = Duration::from_secs(30);
14const MAX_BACKOFF: Duration = Duration::from_secs(600);
15const MISSING_DIR_RECHECK: Duration = Duration::from_secs(300);
16
17pub fn spawn(store: Arc<dyn StoreAdmin>, embedder: Option<Arc<Embedder>>, machine_id: String) {
18    std::thread::spawn(move || {
19        let mut backoff = INITIAL_BACKOFF;
20        loop {
21            match run(store.clone(), embedder.clone(), machine_id.clone()) {
22                // run() only returns Ok when the watch dir is missing; it may be
23                // created later (fresh machine, first session), so keep checking
24                Ok(()) => std::thread::sleep(MISSING_DIR_RECHECK),
25                Err(e) => {
26                    tracing::error!("watcher failed: {e}; restarting in {}s", backoff.as_secs());
27                    std::thread::sleep(backoff);
28                    backoff = next_backoff(backoff);
29                }
30            }
31        }
32    });
33}
34
35fn next_backoff(prev: Duration) -> Duration {
36    (prev * 2).min(MAX_BACKOFF)
37}
38
39fn run(
40    store: Arc<dyn StoreAdmin>,
41    embedder: Option<Arc<Embedder>>,
42    machine_id: String,
43) -> anyhow::Result<()> {
44    let home = std::env::var("HOME").unwrap_or_else(|_| "/tmp".to_string());
45    let watch_dir = PathBuf::from(&home).join(".claude").join("projects");
46
47    if !watch_dir.exists() {
48        tracing::info!(
49            "watcher: {} does not exist, re-checking periodically",
50            watch_dir.display()
51        );
52        return Ok(());
53    }
54
55    let (tx, rx) = std::sync::mpsc::channel();
56
57    let mut watcher = notify::recommended_watcher(move |res: Result<notify::Event, _>| {
58        if let Ok(event) = res
59            && matches!(event.kind, EventKind::Create(_) | EventKind::Modify(_))
60        {
61            for path in event.paths {
62                let is_jsonl = path.extension().is_some_and(|e| e == "jsonl");
63                let is_memory_md = path.extension().is_some_and(|e| e == "md")
64                    && path
65                        .parent()
66                        .is_some_and(|p| p.file_name().is_some_and(|n| n == "memory"))
67                    && !path
68                        .file_name()
69                        .is_some_and(|n| n.eq_ignore_ascii_case("MEMORY.md"));
70                if is_jsonl || is_memory_md {
71                    tx.send(path).ok();
72                }
73            }
74        }
75    })?;
76
77    watcher.watch(&watch_dir, RecursiveMode::Recursive)?;
78    tracing::info!("watcher: monitoring {}", watch_dir.display());
79
80    let mut pending: HashMap<PathBuf, Instant> = HashMap::new();
81    let settle_duration = Duration::from_secs(10);
82
83    loop {
84        while let Ok(path) = rx.try_recv() {
85            pending.insert(path, Instant::now());
86        }
87
88        let now = Instant::now();
89        let ready: Vec<PathBuf> = pending
90            .iter()
91            .filter(|(_, when)| now.duration_since(**when) >= settle_duration)
92            .map(|(p, _)| p.clone())
93            .collect();
94
95        for path in ready {
96            pending.remove(&path);
97            let is_memory_md = path.extension().is_some_and(|e| e == "md");
98            if is_memory_md {
99                ingest_memory_file(&store, embedder.as_deref(), &path, &machine_id);
100            } else {
101                ingest_file(&store, embedder.as_deref(), &path, &machine_id);
102            }
103        }
104
105        std::thread::sleep(Duration::from_secs(2));
106    }
107}
108
109#[cfg(test)]
110mod tests {
111    use super::*;
112
113    #[test]
114    fn backoff_doubles_and_caps() {
115        let mut b = INITIAL_BACKOFF;
116        b = next_backoff(b);
117        assert_eq!(b, Duration::from_secs(60));
118        for _ in 0..10 {
119            b = next_backoff(b);
120        }
121        assert_eq!(b, MAX_BACKOFF);
122    }
123}