Skip to main content

symforge/
watcher_state.rs

1//! Watcher state snapshot types — pure data shared by the engine (health stats)
2//! and the server-only notify-based watcher runtime. No server dependencies, so
3//! these compile in `--no-default-features --features embed` builds.
4
5use std::time::SystemTime;
6
7/// Watcher operational state.
8#[derive(Clone, Debug, PartialEq, Eq)]
9pub enum WatcherState {
10    /// File watcher is registered and looping (receiving/ready for events).
11    Active,
12    /// Watcher (re)start has been initiated but the recursive filesystem watch
13    /// has not finished registering yet. On large trees, registering the
14    /// `notify` recursive watch can take seconds; this state distinguishes
15    /// in-progress startup from a watcher that is genuinely not running (`Off`).
16    Starting,
17    /// File watcher encountered errors but partial operation continues.
18    Degraded,
19    /// File watcher is not running.
20    Off,
21}
22
23/// Snapshot of file watcher status for health reporting.
24#[derive(Clone, Debug)]
25pub struct WatcherInfo {
26    pub state: WatcherState,
27    pub events_processed: u64,
28    pub last_event_at: Option<SystemTime>,
29    pub debounce_window_ms: u64,
30    /// Number of watcher buffer overflow events detected.
31    pub overflow_count: u64,
32    /// Wall-clock time of the most recent overflow event.
33    pub last_overflow_at: Option<SystemTime>,
34    /// Cumulative count of stale files found and re-indexed by reconciliation sweeps.
35    pub stale_files_found: u64,
36    /// Wall-clock time of the most recent reconciliation sweep.
37    pub last_reconcile_at: Option<SystemTime>,
38}
39
40impl Default for WatcherInfo {
41    fn default() -> Self {
42        WatcherInfo {
43            state: WatcherState::Off,
44            events_processed: 0,
45            last_event_at: None,
46            debounce_window_ms: 200,
47            overflow_count: 0,
48            last_overflow_at: None,
49            stale_files_found: 0,
50            last_reconcile_at: None,
51        }
52    }
53}
54
55impl WatcherInfo {
56    pub fn detached_local_fallback() -> Self {
57        WatcherInfo {
58            debounce_window_ms: 0,
59            ..WatcherInfo::default()
60        }
61    }
62
63    pub fn is_local_fallback(&self) -> bool {
64        matches!(self.state, WatcherState::Off)
65            && self.events_processed == 0
66            && self.last_event_at.is_none()
67            && self.debounce_window_ms == 0
68            && self.overflow_count == 0
69            && self.last_overflow_at.is_none()
70            && self.stale_files_found == 0
71            && self.last_reconcile_at.is_none()
72    }
73}