arbor-watcher 3.0.5

File watching and incremental indexing for Arbor
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
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
//! Directory indexing.
//!
//! Walks directories to find and parse source files, building
//! the initial code graph.

use arbor_core::{parse_file, CodeNode};
use arbor_graph::{ArborGraph, GraphBuilder, GraphStore};
use ignore::gitignore::{Gitignore, GitignoreBuilder};
use ignore::WalkBuilder;
use rayon::prelude::*;
use serde::Deserialize;
use std::collections::HashSet;
use std::path::{Path, PathBuf};
use std::time::Instant;
use tracing::{debug, info, warn};

/// Result of indexing a directory.
pub struct IndexResult {
    /// The built graph.
    pub graph: ArborGraph,

    /// Number of files processed (parsed fresh).
    pub files_indexed: usize,

    /// Number of files loaded from cache.
    pub cache_hits: usize,

    /// Number of nodes extracted.
    pub nodes_extracted: usize,

    /// Time taken in milliseconds.
    pub duration_ms: u64,

    /// Files that failed to parse.
    pub errors: Vec<(String, String)>,
}

/// Options for directory indexing.
#[derive(Debug, Clone, Default)]
pub struct IndexOptions {
    /// Follow symbolic links when walking directories.
    pub follow_symlinks: bool,

    /// Path to cache directory (e.g., `.arbor/cache`).
    /// If None, caching is disabled.
    pub cache_path: Option<PathBuf>,
}

const DEFAULT_EXCLUDE_PATTERNS: &[&str] = &[
    ".git/",
    ".arbor/",
    "node_modules/",
    "venv/",
    ".venv/",
    "site-packages/",
    "__pycache__/",
    "target/",
    "dist/",
    "build/",
    "out/",
    // Bundled third-party trees and minified assets. `vendor/` matches that
    // directory at any depth, including Android `assets/**/vendor`.
    "vendor/",
    "*.min.js",
    "*.min.css",
];

#[derive(Debug, Deserialize)]
struct ArborConfig {
    #[serde(default)]
    ignore: Vec<String>,
}

/// Shared ignore matcher used by both the directory walker and the file watcher.
///
/// Combines Arbor's built-in excludes (`.git/`, `target/`, `node_modules/`, ...),
/// the root `.gitignore` and `.arborignore` files, and any custom patterns from
/// `.arbor/config.json`.
pub struct IgnoreMatcher {
    inner: Option<Gitignore>,
}

impl IgnoreMatcher {
    /// Build a matcher rooted at `root`.
    pub fn new(root: &Path) -> Self {
        let mut builder = GitignoreBuilder::new(root);

        for pattern in DEFAULT_EXCLUDE_PATTERNS {
            if let Err(e) = builder.add_line(None, pattern) {
                warn!("Invalid built-in ignore pattern '{}': {}", pattern, e);
            }
        }

        // Root-level ignore files. (Nested `.gitignore` files are honored by
        // `WalkBuilder` during the initial walk; this matcher is enough for the
        // watcher and for root-level rules in the indexer.)
        for ignore_file in [".gitignore", ".arborignore"] {
            let path = root.join(ignore_file);
            if path.exists() {
                if let Some(e) = builder.add(&path) {
                    warn!("Failed to add {}: {}", path.display(), e);
                }
            }
        }

        // Custom ignore patterns from `.arbor/config.json`
        let config_path = root.join(".arbor").join("config.json");
        if config_path.exists() {
            match std::fs::read_to_string(&config_path) {
                Ok(text) => match serde_json::from_str::<ArborConfig>(&text) {
                    Ok(config) => {
                        for pattern in config.ignore {
                            let trimmed = pattern.trim();
                            if trimmed.is_empty() {
                                continue;
                            }
                            if let Err(e) = builder.add_line(None, trimmed) {
                                warn!(
                                    "Invalid ignore pattern '{}' in {}: {}",
                                    trimmed,
                                    config_path.display(),
                                    e
                                );
                            }
                        }
                    }
                    Err(e) => warn!("Failed to parse {}: {}", config_path.display(), e),
                },
                Err(e) => warn!("Failed to read {}: {}", config_path.display(), e),
            }
        }

        match builder.build() {
            Ok(matcher) => Self {
                inner: Some(matcher),
            },
            Err(e) => {
                warn!("Failed to build ignore matcher: {}", e);
                Self { inner: None }
            }
        }
    }

    /// Returns `true` if `path` should be ignored.
    pub fn is_ignored(&self, path: &Path, is_dir: bool) -> bool {
        self.inner
            .as_ref()
            .map(|m| m.matched_path_or_any_parents(path, is_dir).is_ignore())
            .unwrap_or(false)
    }
}

/// Indexes a directory and returns the code graph.
///
/// This walks all source files, parses them, and builds the
/// relationship graph. It respects .gitignore patterns.
///
/// If `options.cache_path` is set, files are cached with their mtimes.
/// Only files with changed mtimes are re-parsed.
///
/// # Example
///
/// ```no_run
/// use arbor_watcher::{index_directory, IndexOptions};
/// use std::path::Path;
///
/// let result = index_directory(Path::new("./src"), IndexOptions::default()).unwrap();
/// println!("Indexed {} files, {} nodes", result.files_indexed, result.nodes_extracted);
/// ```
pub fn index_directory(root: &Path, options: IndexOptions) -> Result<IndexResult, std::io::Error> {
    let start = Instant::now();
    let mut builder = GraphBuilder::new();
    let mut files_indexed = 0;
    let mut cache_hits = 0;
    let mut nodes_extracted = 0;
    let mut errors = Vec::new();

    info!("Starting index of {}", root.display());

    // Open cache if configured
    let store =
        options
            .cache_path
            .as_ref()
            .and_then(|path| match GraphStore::open_or_reset(path) {
                Ok(s) => Some(s),
                Err(e) => {
                    warn!("Failed to open cache: {}, proceeding without cache", e);
                    None
                }
            });

    // Walk the directory, respecting ignore files and collecting supported files.
    let ignore_matcher = IgnoreMatcher::new(root);
    let walker = WalkBuilder::new(root)
        .hidden(true) // Skip hidden files
        .git_ignore(true) // Respect .gitignore
        .ignore(true) // Respect .ignore and custom ignore files
        .require_git(false) // Respect .gitignore even outside a git repository
        .git_global(true)
        .git_exclude(true)
        .add_custom_ignore_filename(".arborignore")
        .follow_links(options.follow_symlinks)
        .build();

    let candidates: Vec<PathBuf> = walker
        .filter_map(Result::ok)
        .filter(|entry| {
            let path = entry.path();
            if ignore_matcher.is_ignored(
                path,
                entry.file_type().map(|ft| ft.is_dir()).unwrap_or(false),
            ) {
                return false;
            }
            if path.is_dir() {
                return false;
            }
            match path.extension().and_then(|e| e.to_str()) {
                Some(ext) => arbor_core::languages::is_supported(ext),
                None => false,
            }
        })
        .map(|entry| entry.into_path())
        .collect();

    // Track files we've seen (for detecting deleted files)
    let seen_files: HashSet<String> = candidates.iter().map(|p| p.display().to_string()).collect();

    // Parse files in parallel. parse_file creates its own tree-sitter parser
    // per call and sled handles concurrent reads/writes, so both the cache
    // check and the parse can fan out. Results collect in walk order, keeping
    // graph construction deterministic.
    enum Outcome {
        CacheHit(Vec<CodeNode>),
        Parsed(Vec<CodeNode>),
        Failed(String),
    }

    let store_ref = store.as_ref();
    let outcomes: Vec<(String, Outcome)> = candidates
        .par_iter()
        .map(|path| {
            let path_str = path.display().to_string();

            let current_mtime = mtime_nanos(path).unwrap_or(0);

            if let Some(store) = store_ref {
                if let Ok(Some(cached_mtime)) = store.get_mtime(&path_str) {
                    if cached_mtime == current_mtime {
                        // File unchanged, load from cache
                        if let Ok(Some(cached_nodes)) = store.get_file_nodes(&path_str) {
                            debug!("Cache hit: {}", path.display());
                            return (path_str, Outcome::CacheHit(cached_nodes));
                        }
                    }
                }
            }

            debug!("Parsing: {}", path.display());
            match parse_file(path) {
                Ok(nodes) => {
                    if let Some(store) = store_ref {
                        if let Err(e) = store.update_file(&path_str, &nodes, current_mtime) {
                            warn!("Failed to update cache for {}: {}", path_str, e);
                        }
                    }
                    (path_str, Outcome::Parsed(nodes))
                }
                Err(e) => {
                    warn!("Failed to parse {}: {}", path.display(), e);
                    (path_str, Outcome::Failed(e.to_string()))
                }
            }
        })
        .collect();

    for (path_str, outcome) in outcomes {
        match outcome {
            Outcome::CacheHit(nodes) => {
                nodes_extracted += nodes.len();
                cache_hits += 1;
                builder.add_nodes(nodes);
            }
            Outcome::Parsed(nodes) => {
                nodes_extracted += nodes.len();
                files_indexed += 1;
                builder.add_nodes(nodes);
            }
            Outcome::Failed(error) => {
                errors.push((path_str, error));
            }
        }
    }

    // Handle deleted files: remove from cache any files that no longer exist
    if let Some(ref store) = store {
        if let Ok(cached_files) = store.list_cached_files() {
            for cached_file in cached_files {
                if !seen_files.contains(&cached_file) {
                    debug!("Removing deleted file from cache: {}", cached_file);
                    if let Err(e) = store.remove_file(&cached_file) {
                        warn!("Failed to remove {} from cache: {}", cached_file, e);
                    }
                }
            }
        }
    }

    let graph = builder.build();
    let duration = start.elapsed();

    info!(
        "Indexed {} files, {} cache hits ({} nodes) in {:?}",
        files_indexed, cache_hits, nodes_extracted, duration
    );

    Ok(IndexResult {
        graph,
        files_indexed,
        cache_hits,
        nodes_extracted,
        duration_ms: duration.as_millis() as u64,
        errors,
    })
}

/// Parses a single file and returns its nodes.
#[allow(dead_code)]
pub fn parse_single_file(path: &Path) -> Result<Vec<CodeNode>, arbor_core::ParseError> {
    parse_file(path)
}

/// Returns true if any supported source file under `root` is newer than `cache_mtime`.
///
/// Used by read commands to detect a stale `graph.bin` before trusting it.
/// Walks the same gitignore-respecting tree as [`index_directory`] but only
/// stats files — no parsing — and early-exits on the first newer file.
///
/// `cache_mtime` is the modified time of the cache file, in seconds since the
/// UNIX epoch. Catches edits and additions; a lone deletion leaves no newer
/// file, so it is picked up on the next edit instead.
/// A file's modification time in nanoseconds since the Unix epoch.
///
/// Whole seconds were not enough: an edit in the same second as the last
/// index (easy for a coding agent) looked unchanged to both the per-file
/// cache and the staleness check, so the graph silently missed it. Caches
/// written with second precision simply miss once and are rebuilt.
pub fn mtime_nanos(path: &Path) -> Option<u64> {
    let modified = std::fs::metadata(path).and_then(|m| m.modified()).ok()?;
    let since_epoch = modified.duration_since(std::time::UNIX_EPOCH).ok()?;
    u64::try_from(since_epoch.as_nanos()).ok()
}

/// True when any supported source file under `root` was modified after
/// `cache_mtime` (nanoseconds since the Unix epoch, see [`mtime_nanos`]).
pub fn sources_newer_than(root: &Path, cache_mtime: u64, follow_symlinks: bool) -> bool {
    let ignore_matcher = IgnoreMatcher::new(root);
    let walker = WalkBuilder::new(root)
        .hidden(true)
        .git_ignore(true)
        .ignore(true)
        .require_git(false)
        .git_global(true)
        .git_exclude(true)
        .add_custom_ignore_filename(".arborignore")
        .follow_links(follow_symlinks)
        .build();

    for entry in walker.filter_map(Result::ok) {
        let path = entry.path();
        if ignore_matcher.is_ignored(
            path,
            entry.file_type().map(|ft| ft.is_dir()).unwrap_or(false),
        ) {
            continue;
        }
        if path.is_dir() {
            continue;
        }
        match path.extension().and_then(|e| e.to_str()) {
            Some(ext) if arbor_core::languages::is_supported(ext) => {}
            _ => continue,
        }
        let Some(mtime) = mtime_nanos(path) else {
            continue;
        };
        if mtime > cache_mtime {
            return true;
        }
    }
    false
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use tempfile::tempdir;

    #[test]
    fn test_index_empty_directory() {
        let dir = tempdir().unwrap();
        let result = index_directory(dir.path(), IndexOptions::default()).unwrap();
        assert_eq!(result.files_indexed, 0);
        assert_eq!(result.nodes_extracted, 0);
    }

    #[test]
    fn test_sources_newer_than_detects_fresh_edit() {
        let dir = tempdir().unwrap();
        fs::write(dir.path().join("a.rs"), "pub fn a() {}").unwrap();

        // Cache mtime far in the past → the source file is newer → stale.
        assert!(sources_newer_than(dir.path(), 0, false));

        // Cache mtime far in the future → nothing newer → fresh.
        let far_future = u64::MAX;
        assert!(!sources_newer_than(dir.path(), far_future, false));
    }

    #[test]
    fn an_edit_within_the_same_second_is_still_newer() {
        let dir = tempdir().unwrap();
        let file = dir.path().join("a.rs");
        fs::write(&file, "pub fn a() {}").unwrap();
        let before = mtime_nanos(&file).unwrap();
        // Well under a second later: whole-second precision would call this equal.
        std::thread::sleep(std::time::Duration::from_millis(20));
        fs::write(&file, "pub fn a() { let _ = 1; }").unwrap();
        let after = mtime_nanos(&file).unwrap();
        assert!(after > before, "{after} should be newer than {before}");
        assert!(sources_newer_than(dir.path(), before, false));
    }

    #[test]
    fn test_sources_newer_than_ignores_unsupported_files() {
        let dir = tempdir().unwrap();
        // Only an unsupported file exists; it must not mark the cache stale.
        fs::write(dir.path().join("notes.txt"), "hello").unwrap();
        assert!(!sources_newer_than(dir.path(), 0, false));
    }

    #[test]
    fn test_index_with_rust_file() {
        let dir = tempdir().unwrap();
        let file_path = dir.path().join("test.rs");

        fs::write(
            &file_path,
            r#"
            pub fn hello() {
                println!("Hello!");
            }
        "#,
        )
        .unwrap();

        let result = index_directory(dir.path(), IndexOptions::default()).unwrap();
        assert_eq!(result.files_indexed, 1);
        assert!(result.nodes_extracted > 0);
    }

    /// Helper to create a directory symlink cross-platform.
    /// Returns None if symlink creation fails (e.g., no privileges on Windows).
    fn create_dir_symlink(original: &std::path::Path, link: &std::path::Path) -> Option<()> {
        #[cfg(unix)]
        {
            std::os::unix::fs::symlink(original, link).ok()
        }
        #[cfg(windows)]
        {
            std::os::windows::fs::symlink_dir(original, link).ok()
        }
        #[cfg(not(any(unix, windows)))]
        {
            None
        }
    }

    #[test]
    fn test_index_does_not_follow_symlinks_by_default() {
        let dir = tempdir().unwrap();
        let linked_dir = tempdir().unwrap();

        // Create a file in the linked directory
        let linked_file = linked_dir.path().join("linked.rs");
        fs::write(&linked_file, "pub fn linked_func() {}").unwrap();

        // Create a symlink to the linked directory
        let symlink_path = dir.path().join("linked");
        if create_dir_symlink(linked_dir.path(), &symlink_path).is_none() {
            // Skip test if symlinks not supported (e.g., Windows without privileges)
            return;
        }

        // Index without following symlinks (default)
        let result = index_directory(dir.path(), IndexOptions::default()).unwrap();
        assert_eq!(result.files_indexed, 0);
    }

    #[test]
    fn test_index_follows_symlinks_when_enabled() {
        let dir = tempdir().unwrap();
        let linked_dir = tempdir().unwrap();

        // Create a file in the linked directory
        let linked_file = linked_dir.path().join("linked.rs");
        fs::write(&linked_file, "pub fn linked_func() {}").unwrap();

        // Create a symlink to the linked directory
        let symlink_path = dir.path().join("linked");
        if create_dir_symlink(linked_dir.path(), &symlink_path).is_none() {
            // Skip test if symlinks not supported (e.g., Windows without privileges)
            return;
        }

        // Index with follow_symlinks enabled
        let options = IndexOptions {
            follow_symlinks: true,
            cache_path: None,
        };
        let result = index_directory(dir.path(), options).unwrap();
        assert_eq!(result.files_indexed, 1);
        assert!(result.nodes_extracted > 0);
    }

    #[test]
    fn test_index_honors_gitignore_outside_git_repo() {
        let dir = tempdir().unwrap();
        fs::create_dir_all(dir.path().join("venv")).unwrap();
        fs::write(dir.path().join(".gitignore"), "venv/\n").unwrap();
        fs::write(
            dir.path().join("venv").join("ignored.rs"),
            "pub fn ignored() {}",
        )
        .unwrap();
        fs::write(dir.path().join("kept.rs"), "pub fn kept() {}").unwrap();

        let result = index_directory(dir.path(), IndexOptions::default()).unwrap();
        assert_eq!(result.files_indexed, 1);
        assert!(result.graph.find_by_name("kept").len() == 1);
        assert!(result.graph.find_by_name("ignored").is_empty());
    }

    #[test]
    fn test_index_honors_arborignore() {
        let dir = tempdir().unwrap();
        fs::create_dir_all(dir.path().join("vendor")).unwrap();
        fs::write(dir.path().join(".arborignore"), "vendor/\n").unwrap();
        fs::write(
            dir.path().join("vendor").join("ignored.rs"),
            "pub fn ignored() {}",
        )
        .unwrap();
        fs::write(dir.path().join("kept.rs"), "pub fn kept() {}").unwrap();

        let result = index_directory(dir.path(), IndexOptions::default()).unwrap();
        assert_eq!(result.files_indexed, 1);
        assert!(result.graph.find_by_name("kept").len() == 1);
        assert!(result.graph.find_by_name("ignored").is_empty());
    }

    #[test]
    fn test_index_ignores_vendor_and_minified_assets_by_default() {
        let dir = tempdir().unwrap();
        let vendor_path = dir
            .path()
            .join("android")
            .join("app")
            .join("src")
            .join("main")
            .join("assets")
            .join("instrument")
            .join("vendor");
        fs::create_dir_all(&vendor_path).unwrap();
        fs::write(vendor_path.join("maplibre-gl.js"), "function el() {}").unwrap();
        fs::write(dir.path().join("app.min.js"), "function boot() {}").unwrap();
        fs::write(dir.path().join("kept.rs"), "pub fn kept() {}").unwrap();

        let result = index_directory(dir.path(), IndexOptions::default()).unwrap();
        assert_eq!(result.files_indexed, 1);
        assert!(result.graph.find_by_name("kept").len() == 1);
        assert!(result.graph.find_by_name("el").is_empty());
        assert!(result.graph.find_by_name("boot").is_empty());
    }

    #[test]
    fn test_index_honors_arbor_config_ignore_patterns() {
        let dir = tempdir().unwrap();
        fs::create_dir_all(dir.path().join(".arbor")).unwrap();
        fs::create_dir_all(dir.path().join("third_party")).unwrap();
        fs::write(
            dir.path().join(".arbor").join("config.json"),
            r#"{"version":"1.0","ignore":["third_party/"]}"#,
        )
        .unwrap();
        fs::write(
            dir.path().join("third_party").join("ignored.rs"),
            "pub fn ignored() {}",
        )
        .unwrap();
        fs::write(dir.path().join("kept.rs"), "pub fn kept() {}").unwrap();

        let result = index_directory(dir.path(), IndexOptions::default()).unwrap();
        assert_eq!(result.files_indexed, 1);
        assert!(result.graph.find_by_name("kept").len() == 1);
        assert!(result.graph.find_by_name("ignored").is_empty());
    }
}