magellan 4.12.3

Deterministic codebase mapping tool for local development
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
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
//! Filesystem watcher with debounced batch events.
//!
//! Provides deterministic event coalescing: all events within a debounce window
//! are collected, de-duplicated, sorted lexicographically, and emitted as a single
//! batch. This ensures the same final DB state regardless of event arrival order.
//!
//! # Threading Design
//!
//! This watcher uses thread-safe synchronization for concurrent access.
//! The legacy pending state fields use `Arc<parking_lot::Mutex<T>>` to allow safe access
//! from multiple threads during concurrent operations and shutdown.
//!
//! **Thread safety:** `Arc<parking_lot::Mutex<T>>` provides runtime mutual exclusion
//! and is safe to share across threads. parking_lot Mutex does not poison on panic.
//!
//! # Global Lock Ordering
//!
//! This module participates in the global lock ordering hierarchy:
//!
//! 1. **watcher state locks** (legacy_pending_batch, legacy_pending_index)—acquired first
//! 2. **indexer shared state locks** (dirty_paths)—acquired second
//! 3. **wakeup channel send** (highest priority)—acquired last
//!
//! **Rule:** Never send to wakeup channel while holding other locks.
//!
//! See `src/indexer.rs::PipelineSharedState` for full lock ordering documentation.
//!
//! See MANUAL.md for architecture details.

pub mod async_watcher;

use anyhow::Result;
use notify::{EventKind, RecommendedWatcher, RecursiveMode, Watcher};
use parking_lot::Mutex;
use serde::{Deserialize, Serialize};
use std::collections::{BTreeSet, HashMap};
use std::mem::ManuallyDrop;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::mpsc::{self, Receiver, Sender};
use std::sync::Arc;
use std::thread;
use std::time::{Duration, Instant};

use crate::diagnostics::SkipReason;
use crate::graph::filter::FileFilter;

/// Deterministic batch of dirty file paths.
///
/// Contains ONLY paths (no timestamps, no event types) to ensure deterministic
/// behavior. Paths are sorted lexicographically before emission.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct WatcherBatch {
    /// Dirty file paths to reconcile, in lexicographic order
    pub paths: Vec<PathBuf>,
}

impl WatcherBatch {
    /// Create a new batch from a set of paths, sorting them deterministically.
    pub fn from_set(paths: BTreeSet<PathBuf>) -> Self {
        Self {
            paths: paths.into_iter().collect(),
        }
    }

    /// Empty batch for when no dirty paths exist after filtering.
    pub fn empty() -> Self {
        Self { paths: Vec::new() }
    }

    /// Whether this batch contains any paths.
    pub fn is_empty(&self) -> bool {
        self.paths.is_empty()
    }
}

/// Filesystem watcher configuration
#[derive(Debug, Clone)]
pub struct WatcherConfig {
    /// Root directory for path validation
    pub root_path: PathBuf,
    /// Debounce delay in milliseconds
    pub debounce_ms: u64,
    /// Enable .gitignore filtering (default: true)
    pub gitignore_aware: bool,
}

impl Default for WatcherConfig {
    fn default() -> Self {
        Self {
            root_path: PathBuf::from("."),
            debounce_ms: 500,
            gitignore_aware: true,
        }
    }
}

/// Filesystem watcher that emits debounced batches of dirty paths.
///
/// Uses `notify::RecommendedWatcher` directly with custom debouncing that
/// filters out read-only events (ACCESS/OPEN/CLOSE_NOWRITE) at the source.
/// All paths within the debounce window are collected, de-duplicated, sorted,
/// and emitted as a single `WatcherBatch`.
pub struct FileSystemWatcher {
    /// Watcher thread handle (wrapped in ManuallyDrop for custom Drop/shutdown logic)
    _watcher_thread: ManuallyDrop<thread::JoinHandle<()>>,
    batch_receiver: Receiver<WatcherBatch>,
    /// Legacy compatibility: pending batch to emit one path at a time
    /// Thread-safe: wrapped in `Arc<parking_lot::Mutex<T>>` for concurrent access
    legacy_pending_batch: Arc<Mutex<Option<WatcherBatch>>>,
    /// Legacy compatibility: current index into pending batch
    /// Thread-safe: wrapped in `Arc<parking_lot::Mutex<T>>` for concurrent access
    legacy_pending_index: Arc<Mutex<usize>>,
}

impl FileSystemWatcher {
    /// Create a new watcher for the given directory.
    ///
    /// # Arguments
    /// * `path` - Directory to watch recursively (also used as root_path for validation)
    /// * `config` - Watcher configuration
    /// * `shutdown` - AtomicBool for graceful shutdown
    ///
    /// # Returns
    /// A watcher that can be polled for batch events
    pub fn new(path: PathBuf, config: WatcherConfig, shutdown: Arc<AtomicBool>) -> Result<Self> {
        let (batch_tx, batch_rx) = mpsc::channel();

        let config = WatcherConfig {
            root_path: path.clone(),
            ..config
        };

        let thread = thread::spawn(move || {
            if let Err(e) = run_watcher(path, batch_tx, config, shutdown) {
                eprintln!("Watcher error: {:?}", e);
            }
        });

        Ok(Self {
            _watcher_thread: ManuallyDrop::new(thread),
            batch_receiver: batch_rx,
            legacy_pending_batch: Arc::new(Mutex::new(None)),
            legacy_pending_index: Arc::new(Mutex::new(0)),
        })
    }

    /// Receive the next batch, blocking until available.
    ///
    /// # Returns
    /// `None` if the watcher thread has terminated
    pub fn recv_batch(&self) -> Option<WatcherBatch> {
        self.batch_receiver.recv().ok()
    }

    /// Try to receive a batch without blocking.
    ///
    /// # Returns
    /// - `Some(batch)` if a batch is available
    /// - `None` if no batch is available or watcher terminated
    pub fn try_recv_batch(&self) -> Option<WatcherBatch> {
        self.batch_receiver.try_recv().ok()
    }

    /// Receive the next batch with a timeout.
    ///
    /// # Returns
    /// - `Ok(Some(batch))` if a batch is available
    /// - `Ok(None)` if the watcher thread has terminated
    /// - `Err` if timeout elapsed
    #[allow(
        clippy::result_unit_err,
        reason = "simple timeout/empty signaling; no error details needed"
    )]
    pub fn recv_batch_timeout(&self, timeout: Duration) -> Result<Option<WatcherBatch>, ()> {
        match self.batch_receiver.recv_timeout(timeout) {
            Ok(batch) => Ok(Some(batch)),
            Err(std::sync::mpsc::RecvTimeoutError::Timeout) => Err(()),
            Err(std::sync::mpsc::RecvTimeoutError::Disconnected) => Ok(None),
        }
    }

    // ========================================================================
    // LEGACY: Old single-event API for backward compatibility during migration
    // ========================================================================

    /// Legacy: Try to receive a single event without blocking (DEPRECATED).
    ///
    /// This method converts batch events to single events for backward
    /// compatibility. Paths from each batch are returned one at a time
    /// in sorted order.
    ///
    /// # Deprecated
    /// Use `try_recv_batch()` instead for deterministic batch processing.
    ///
    /// # Errors
    pub fn try_recv_event(&self) -> Result<Option<FileEvent>> {
        {
            let mut pending_batch = self.legacy_pending_batch.lock();
            let mut pending_index = self.legacy_pending_index.lock();

            if let Some(ref batch) = *pending_batch {
                if *pending_index < batch.paths.len() {
                    let path = batch.paths[*pending_index].clone();
                    *pending_index += 1;

                    if *pending_index >= batch.paths.len() {
                        *pending_batch = None;
                        *pending_index = 0;
                    }

                    return Ok(Some(FileEvent {
                        path,
                        event_type: EventType::Modify,
                    }));
                }
            }
        }

        if let Ok(batch) = self.batch_receiver.try_recv() {
            if batch.paths.is_empty() {
                return Ok(None);
            }

            if batch.paths.len() > 1 {
                let path = batch.paths[0].clone();
                let mut pending_batch = self.legacy_pending_batch.lock();
                let mut pending_index = self.legacy_pending_index.lock();
                *pending_batch = Some(batch);
                *pending_index = 1;
                drop(pending_batch);
                drop(pending_index);
                return Ok(Some(FileEvent {
                    path,
                    event_type: EventType::Modify,
                }));
            }

            Ok(Some(FileEvent {
                path: batch.paths[0].clone(),
                event_type: EventType::Modify,
            }))
        } else {
            Ok(None)
        }
    }

    /// Legacy: Receive the next event, blocking until available (DEPRECATED).
    ///
    /// This method converts batch events to single events for backward
    /// compatibility. Paths from each batch are returned one at a time
    /// in sorted order.
    ///
    /// # Deprecated
    /// Use `recv_batch()` instead for deterministic batch processing.
    ///
    /// # Errors
    pub fn recv_event(&self) -> Result<Option<FileEvent>> {
        {
            let mut pending_batch = self.legacy_pending_batch.lock();
            let mut pending_index = self.legacy_pending_index.lock();

            if let Some(ref batch) = *pending_batch {
                if *pending_index < batch.paths.len() {
                    let path = batch.paths[*pending_index].clone();
                    *pending_index += 1;

                    if *pending_index >= batch.paths.len() {
                        *pending_batch = None;
                        *pending_index = 0;
                    }

                    return Ok(Some(FileEvent {
                        path,
                        event_type: EventType::Modify,
                    }));
                }
            }
        }

        if let Ok(batch) = self.batch_receiver.recv() {
            if batch.paths.is_empty() {
                return Ok(None);
            }

            if batch.paths.len() > 1 {
                let path = batch.paths[0].clone();
                let mut pending_batch = self.legacy_pending_batch.lock();
                let mut pending_index = self.legacy_pending_index.lock();
                *pending_batch = Some(batch);
                *pending_index = 1;
                drop(pending_batch);
                drop(pending_index);
                return Ok(Some(FileEvent {
                    path,
                    event_type: EventType::Modify,
                }));
            }

            Ok(Some(FileEvent {
                path: batch.paths[0].clone(),
                event_type: EventType::Modify,
            }))
        } else {
            Ok(None)
        }
    }

    /// Explicitly shut down the watcher and join all background threads.
    ///
    /// This method consumes the watcher, ensuring that:
    /// 1. The pub/sub receiver is shut down cleanly (if present)
    /// 2. The watcher thread is joined (waits for clean termination)
    ///
    /// # Note
    ///
    /// This method should be called during graceful shutdown to ensure
    /// all threads have terminated before the program exits.
    pub fn shutdown(mut self) {
        let thread = unsafe { ManuallyDrop::take(&mut self._watcher_thread) };
        let _ = thread.join();
    }
}

impl Drop for FileSystemWatcher {
    fn drop(&mut self) {
        let _thread = unsafe { ManuallyDrop::take(&mut self._watcher_thread) };
        drop(_thread);
    }
}

/// Whether a notify event kind represents a write-side mutation worth indexing.
///
/// Read-only events (ACCESS, OPEN, CLOSE_NOWRITE) are excluded because
/// `reconcile_file_path` calls `fs::read`, which triggers those inotify
/// events and would otherwise create an infinite feedback loop.
fn is_mutation_event(kind: &EventKind) -> bool {
    matches!(
        kind,
        EventKind::Create(_)
            | EventKind::Modify(_)
            | EventKind::Remove(_)
            | EventKind::Any
            | EventKind::Other
    )
}

/// Run the debounced watcher in a dedicated thread.
///
/// Uses `notify::RecommendedWatcher` directly with custom debouncing that
/// filters out read-only events (ACCESS/OPEN/CLOSE_NOWRITE) at the source.
/// This prevents the infinite feedback loop where `reconcile_file_path`
/// reads a file → triggers ACCESS inotify event → re-indexes → reads again.
fn run_watcher(
    path: PathBuf,
    tx: Sender<WatcherBatch>,
    config: WatcherConfig,
    shutdown: Arc<AtomicBool>,
) -> Result<()> {
    let debounce_duration = Duration::from_millis(config.debounce_ms);
    let root_path = config.root_path.clone();

    let filter = if config.gitignore_aware {
        match FileFilter::new(&root_path, &[], &[]) {
            Ok(f) => Some(f),
            Err(e) => {
                eprintln!("Warning: Failed to create gitignore filter: {}", e);
                None
            }
        }
    } else {
        None
    };

    let (raw_tx, raw_rx): (Sender<Vec<PathBuf>>, Receiver<Vec<PathBuf>>) = mpsc::channel();

    let mut watcher = RecommendedWatcher::new(
        move |result: notify::Result<notify::Event>| match result {
            Ok(event) => {
                if !is_mutation_event(&event.kind) {
                    return;
                }
                let paths: Vec<PathBuf> = event.paths;
                if !paths.is_empty() {
                    let _ = raw_tx.send(paths);
                }
            }
            Err(error) => {
                eprintln!("Watcher error: {:?}", error);
            }
        },
        notify::Config::default(),
    )?;

    watcher.watch(&path, RecursiveMode::Recursive)?;

    let mut pending: HashMap<PathBuf, Instant> = HashMap::new();
    let idle_sleep = Duration::from_millis(50);

    while !shutdown.load(Ordering::SeqCst) {
        let mut new_paths: Vec<PathBuf> = Vec::new();

        match raw_rx.recv_timeout(debounce_duration) {
            Ok(paths) => {
                new_paths.extend(paths);
                let drain_until = Instant::now() + Duration::from_millis(10);
                while Instant::now() < drain_until {
                    match raw_rx.try_recv() {
                        Ok(paths) => new_paths.extend(paths),
                        Err(_) => break,
                    }
                }
            }
            Err(mpsc::RecvTimeoutError::Timeout) => {}
            Err(mpsc::RecvTimeoutError::Disconnected) => break,
        }

        let now = Instant::now();
        for p in new_paths {
            pending.insert(p, now);
        }

        let mut expired: BTreeSet<PathBuf> = BTreeSet::new();
        pending.retain(|p, ts| {
            if now.duration_since(*ts) >= debounce_duration {
                expired.insert(p.clone());
                false
            } else {
                true
            }
        });

        if !expired.is_empty() {
            let dirty_paths = filter_dirty_paths(expired, &root_path, filter.as_ref());
            if !dirty_paths.is_empty() {
                let batch = WatcherBatch::from_set(dirty_paths);
                let _ = tx.send(batch);
            }
        }

        if pending.is_empty() {
            thread::sleep(idle_sleep);
        }
    }

    Ok(())
}

/// Filter a set of expired paths through gitignore, database, and validation checks.
fn filter_dirty_paths(
    candidates: BTreeSet<PathBuf>,
    root: &Path,
    filter: Option<&FileFilter>,
) -> BTreeSet<PathBuf> {
    let mut dirty_paths = BTreeSet::new();

    for path in candidates {
        if path.is_dir() {
            continue;
        }

        let path_str = path.to_string_lossy();
        if is_database_file(&path_str) {
            continue;
        }

        if let Some(f) = filter {
            match f.should_skip(&path) {
                None => {}
                Some(SkipReason::NotAFile) => {}
                Some(_) => continue,
            }
        }

        match crate::validation::validate_path_within_root(&path, root) {
            Ok(_) => {
                let normalized = crate::validation::normalize_path(&path)
                    .unwrap_or_else(|_| path.to_string_lossy().to_string());
                dirty_paths.insert(PathBuf::from(normalized));
            }
            Err(crate::validation::PathValidationError::OutsideRoot(p, _)) => {
                eprintln!("WARNING: Watcher rejected path outside project root: {}", p);
            }
            Err(crate::validation::PathValidationError::SuspiciousTraversal(p)) => {
                eprintln!(
                    "WARNING: Watcher rejected suspicious traversal pattern: {}",
                    p
                );
            }
            Err(crate::validation::PathValidationError::SymlinkEscape(from, to)) => {
                eprintln!(
                    "WARNING: Watcher rejected symlink escaping root: {} -> {}",
                    from, to
                );
            }
            Err(crate::validation::PathValidationError::CannotCanonicalize(_)) => {
                let normalized = crate::validation::normalize_path(&path)
                    .unwrap_or_else(|_| path.to_string_lossy().to_string());
                dirty_paths.insert(PathBuf::from(normalized));
            }
        }
    }

    dirty_paths
}

/// Check if a path is a database file that should be excluded from watching.
///
/// Database files are excluded because the indexer writes to them, which
/// would create a feedback loop (write event -> indexer writes again -> ...).
fn is_database_file(path: &str) -> bool {
    let path_lower = path.to_lowercase();
    path_lower.ends_with(".db")
        || path_lower.ends_with(".db-journal")
        || path_lower.ends_with(".db-wal")
        || path_lower.ends_with(".db-shm")
        || path_lower.ends_with(".sqlite")
        || path_lower.ends_with(".sqlite3")
}

// ============================================================================
// LEGACY: Old single-event types for backward compatibility during migration
// ============================================================================

/// Legacy: File event emitted by the watcher (DEPRECATED).
///
/// This type is kept for backward compatibility during the migration to
/// batch-based processing. New code should use `WatcherBatch` instead.
#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct FileEvent {
    /// Path of the affected file
    pub path: PathBuf,
    /// Type of event (DEPRECATED - not used in batch processing)
    pub event_type: EventType,
}

/// Type of file event (DEPRECATED - not used in batch processing).
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum EventType {
    /// File was created
    Create,
    /// File was modified
    Modify,
    /// File was deleted
    Delete,
}

impl std::fmt::Display for EventType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            EventType::Create => write!(f, "CREATE"),
            EventType::Modify => write!(f, "MODIFY"),
            EventType::Delete => write!(f, "DELETE"),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_batch_is_empty() {
        let batch = WatcherBatch::empty();
        assert!(batch.is_empty());
    }

    #[test]
    fn test_batch_from_set_sorts_deterministically() {
        let mut set = BTreeSet::new();
        set.insert(PathBuf::from("/zebra.rs"));
        set.insert(PathBuf::from("/alpha.rs"));
        set.insert(PathBuf::from("/beta.rs"));

        let batch = WatcherBatch::from_set(set);

        assert_eq!(batch.paths[0], PathBuf::from("/alpha.rs"));
        assert_eq!(batch.paths[1], PathBuf::from("/beta.rs"));
        assert_eq!(batch.paths[2], PathBuf::from("/zebra.rs"));
    }

    #[test]
    fn test_database_file_detection() {
        assert!(is_database_file("test.db"));
        assert!(is_database_file("test.sqlite"));
        assert!(is_database_file("test.db-journal"));
        assert!(is_database_file("test.DB"));
        assert!(is_database_file("test.SQLITE"));

        assert!(!is_database_file("test.rs"));
        assert!(!is_database_file("test.py"));
        assert!(!is_database_file("database.rs"));
    }

    #[test]
    fn test_batch_serialization() {
        let batch = WatcherBatch {
            paths: vec![PathBuf::from("/alpha.rs"), PathBuf::from("/beta.rs")],
        };

        let json = serde_json::to_string(&batch).unwrap();
        let deserialized: WatcherBatch = serde_json::from_str(&json).unwrap();

        assert_eq!(batch.paths, deserialized.paths);
    }

    #[test]
    fn test_watcher_config_has_root() {
        let config = WatcherConfig {
            root_path: PathBuf::from("/test/root"),
            debounce_ms: 100,
            gitignore_aware: true,
        };

        assert_eq!(config.root_path, PathBuf::from("/test/root"));
        assert_eq!(config.debounce_ms, 100);
        assert!(config.gitignore_aware);
    }

    #[test]
    fn test_watcher_config_default() {
        let config = WatcherConfig::default();

        assert_eq!(config.root_path, PathBuf::from("."));
        assert_eq!(config.debounce_ms, 500);
        assert!(config.gitignore_aware);
    }

    #[test]
    fn test_extract_dirty_paths_filters_traversal() {
        use std::fs;
        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();
        let root = temp_dir.path();

        let valid_file = root.join("valid.rs");
        fs::write(&valid_file, b"fn valid() {}").unwrap();

        let result = crate::validation::validate_path_within_root(&valid_file, root);
        assert!(result.is_ok());

        let outside = root.join("../../../etc/passwd");
        let result_outside = crate::validation::validate_path_within_root(&outside, root);
        assert!(result_outside.is_err());
    }

    #[test]
    fn test_is_mutation_event_accepts_create_modify_remove() {
        assert!(is_mutation_event(&EventKind::Create(
            notify::event::CreateKind::File
        )));
        assert!(is_mutation_event(&EventKind::Modify(
            notify::event::ModifyKind::Data(notify::event::DataChange::Content)
        )));
        assert!(is_mutation_event(&EventKind::Remove(
            notify::event::RemoveKind::File
        )));
        assert!(is_mutation_event(&EventKind::Any));
    }

    #[test]
    fn test_is_mutation_event_rejects_access() {
        assert!(!is_mutation_event(&EventKind::Access(
            notify::event::AccessKind::Read
        )));
        assert!(!is_mutation_event(&EventKind::Access(
            notify::event::AccessKind::Close(notify::event::AccessMode::Read)
        )));
    }

    #[test]
    fn test_filter_dirty_paths_excludes_db_files() {
        let temp_dir = tempfile::TempDir::new().unwrap();
        let root = temp_dir.path();
        let db_file = root.join("index.db");
        std::fs::write(&db_file, b"fake db").unwrap();

        let mut candidates = BTreeSet::new();
        candidates.insert(db_file);

        let result = filter_dirty_paths(candidates, root, None);
        assert!(result.is_empty());
    }
}