vtcode-config 0.141.12

Config loader components shared across VT Code and downstream adopters
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
use anyhow::{Context, Result, anyhow};
use hashbrown::HashMap;
use notify::{RecommendedWatcher, RecursiveMode, Watcher};
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant, SystemTime};

use super::{ConfigManager, VTCodeConfig};
use crate::defaults;

/// Configuration watcher that monitors config files for changes
/// and automatically reloads them when modifications are detected.
pub struct ConfigWatcher {
    workspace_path: PathBuf,
    last_load_time: Arc<Mutex<Instant>>,
    current_config: Arc<Mutex<Option<VTCodeConfig>>>,
    watcher: Option<RecommendedWatcher>,
    debounce_duration: Duration,
    last_event_time: Arc<Mutex<Instant>>,
}

impl ConfigWatcher {
    /// Create a new ConfigWatcher for the given workspace.
    #[must_use]
    pub fn new(workspace_path: PathBuf) -> Self {
        Self {
            workspace_path,
            last_load_time: Arc::new(Mutex::new(Instant::now())),
            current_config: Arc::new(Mutex::new(None)),
            watcher: None,
            debounce_duration: Duration::from_millis(500),
            last_event_time: Arc::new(Mutex::new(Instant::now())),
        }
    }

    /// Initialize the file watcher and load initial configuration.
    ///
    /// # Errors
    ///
    /// Returns an error when the initial config load fails or when the watcher
    /// cannot subscribe to config parent directories.
    pub async fn initialize(&mut self) -> Result<()> {
        self.load_config().await?;

        let last_event_time = Arc::clone(&self.last_event_time);
        let debounce_duration = self.debounce_duration;

        let mut watcher = RecommendedWatcher::new(
            move |res: Result<notify::Event, notify::Error>| {
                if let Ok(event) = res {
                    let now = Instant::now();
                    if let Ok(mut last_time) = last_event_time.lock()
                        && now.duration_since(*last_time) >= debounce_duration
                    {
                        *last_time = now;
                        if is_relevant_config_event(&event) {
                            tracing::debug!("Config file changed: {:?}", event);
                        }
                    }
                }
            },
            notify::Config::default(),
        )?;

        for path in get_config_file_paths(&self.workspace_path) {
            if let Some(parent) = path.parent() {
                watcher
                    .watch(parent, RecursiveMode::NonRecursive)
                    .with_context(|| format!("Failed to watch config directory: {parent:?}"))?;
            }
        }

        self.watcher = Some(watcher);
        Ok(())
    }

    /// Load or reload configuration.
    ///
    /// # Errors
    ///
    /// Returns an error when internal watcher state cannot be updated.
    pub async fn load_config(&mut self) -> Result<()> {
        ConfigManager::invalidate_workspace_cache(&self.workspace_path);
        let config = ConfigManager::load_from_workspace(&self.workspace_path)
            .ok()
            .map(|manager| manager.config().clone());

        let mut current = self
            .current_config
            .lock()
            .map_err(|e| anyhow!("config watcher state lock poisoned: {e}"))?;
        *current = config;
        drop(current);

        let mut last_load = self
            .last_load_time
            .lock()
            .map_err(|e| anyhow!("config watcher timestamp lock poisoned: {e}"))?;
        *last_load = Instant::now();

        Ok(())
    }

    /// Get the current configuration, reloading if the watcher detected changes.
    pub async fn get_config(&mut self) -> Option<VTCodeConfig> {
        if self.should_reload().await
            && let Err(err) = self.load_config().await
        {
            tracing::warn!("Failed to reload config: {}", err);
        }

        self.current_config.lock().unwrap_or_else(|e| e.into_inner()).clone()
    }

    async fn should_reload(&self) -> bool {
        let Ok(last_event) = self.last_event_time.lock() else {
            return false;
        };
        let Ok(last_load) = self.last_load_time.lock() else {
            return false;
        };

        *last_event > *last_load
    }

    /// Get the last load time for debugging.
    #[must_use]
    pub async fn last_load_time(&self) -> Instant {
        self.last_load_time
            .lock()
            .map(|instant| *instant)
            .unwrap_or_else(|_| Instant::now())
    }
}

/// Simple config watcher that polls file mtimes instead of using filesystem events.
pub struct SimpleConfigWatcher {
    workspace_path: PathBuf,
    additional_paths: Vec<PathBuf>,
    last_load_time: Instant,
    last_check_time: Instant,
    check_interval: Duration,
    last_modified_times: HashMap<PathBuf, Option<SystemTime>>,
    debounce_duration: Duration,
    last_reload_attempt: Option<Instant>,
}

impl SimpleConfigWatcher {
    #[must_use]
    pub fn new(workspace_path: PathBuf) -> Self {
        Self {
            workspace_path,
            additional_paths: Vec::new(),
            last_load_time: Instant::now(),
            last_check_time: Instant::now(),
            check_interval: Duration::from_secs(10),
            last_modified_times: HashMap::new(),
            debounce_duration: Duration::from_millis(1000),
            last_reload_attempt: None,
        }
    }

    /// Create a polling watcher that tracks all workspace and user-level
    /// configuration locations supported by the current defaults provider.
    ///
    /// Loading the manager here is best-effort. If a config file is malformed,
    /// the watcher still tracks the provider's default paths so a subsequent
    /// correction or newly-created user config can be observed.
    #[must_use]
    pub fn new_with_user_config_paths(workspace_path: PathBuf) -> Self {
        let mut watcher = Self::new(workspace_path.clone());
        if let Ok(manager) = ConfigManager::load_from_workspace(&workspace_path) {
            for path in manager.user_config_paths() {
                watcher.add_watch_path(path);
            }
        } else {
            let defaults = defaults::current_config_defaults();
            for path in defaults.home_config_paths(defaults.config_file_name()) {
                watcher.add_watch_path(path);
            }
        }
        watcher.seed_current_mtimes();
        watcher
    }

    /// Watch an additional config file (for example the user-level `vtcode.toml`)
    /// in addition to the workspace-local files.
    pub fn add_watch_path(&mut self, path: PathBuf) {
        if !self.additional_paths.contains(&path) {
            self.additional_paths.push(path);
        }
    }

    fn watch_paths(&self) -> Vec<PathBuf> {
        let mut paths = get_config_file_paths(&self.workspace_path);
        for path in &self.additional_paths {
            if !paths.iter().any(|existing| existing == path) {
                paths.push(path.clone());
            }
        }
        paths
    }

    fn seed_current_mtimes(&mut self) {
        for target in self.watch_paths() {
            let current_modified = latest_modified(&target);
            self.last_modified_times.insert(target, current_modified);
        }
    }

    pub fn should_reload(&mut self) -> bool {
        let now = Instant::now();

        if now.duration_since(self.last_check_time) < self.check_interval {
            return false;
        }
        self.last_check_time = now;

        let mut changed_paths = Vec::new();
        for target in self.watch_paths() {
            let current_modified = latest_modified(&target);
            match self.last_modified_times.get(&target).copied() {
                Some(previous) if previous != current_modified => {
                    // Keep the old baseline until the debounce window has
                    // elapsed. Otherwise a rapid edit can be consumed by a
                    // suppressed poll and never trigger a reload.
                    changed_paths.push((target, current_modified));
                }
                Some(_) => {}
                None => {
                    // The initial observation establishes a baseline. A
                    // later `None -> Some(mtime)` transition is a change.
                    self.last_modified_times.insert(target, current_modified);
                }
            }
        }

        if changed_paths.is_empty() {
            return false;
        }

        if let Some(last_attempt) = self.last_reload_attempt
            && now.duration_since(last_attempt) < self.debounce_duration
        {
            return false;
        }

        for (target, modified) in changed_paths {
            self.last_modified_times.insert(target, modified);
        }
        self.last_reload_attempt = Some(now);
        true
    }

    pub fn load_config(&mut self) -> Option<VTCodeConfig> {
        ConfigManager::invalidate_workspace_cache(&self.workspace_path);
        let config = ConfigManager::load_from_workspace(&self.workspace_path)
            .ok()
            .map(|manager| manager.config().clone());

        self.last_load_time = Instant::now();
        self.last_modified_times.clear();
        for target in self.watch_paths() {
            self.last_modified_times.insert(target.clone(), latest_modified(&target));
        }

        config
    }

    pub fn set_check_interval(&mut self, seconds: u64) {
        self.check_interval = Duration::from_secs(seconds);
    }

    pub fn set_debounce_duration(&mut self, millis: u64) {
        self.debounce_duration = Duration::from_millis(millis);
    }
}

fn is_relevant_config_event(event: &notify::Event) -> bool {
    let relevant_files = ["vtcode.toml", "theme.toml"];

    match &event.kind {
        notify::EventKind::Create(_) | notify::EventKind::Modify(_) | notify::EventKind::Remove(_) => {
            event.paths.iter().any(|path| {
                path.file_name()
                    .and_then(|file_name| file_name.to_str())
                    .is_some_and(|file_name| relevant_files.contains(&file_name))
            })
        }
        _ => false,
    }
}

fn get_config_file_paths(workspace_path: &Path) -> Vec<PathBuf> {
    vec![
        workspace_path.join("vtcode.toml"),
        workspace_path.join(".vtcode").join("theme.toml"),
    ]
}

fn latest_modified(path: &Path) -> Option<SystemTime> {
    std::fs::metadata(path).ok()?.modified().ok()
}

#[cfg(test)]
mod tests {
    use std::path::PathBuf;
    use std::time::{Duration, Instant};

    use super::SimpleConfigWatcher;

    fn open_check_window(watcher: &mut SimpleConfigWatcher) {
        // Advance the internal poll clock past the check interval so a change is
        // observed immediately instead of waiting out the default 10s interval.
        watcher.last_check_time = Instant::now().checked_sub(Duration::from_secs(11)).unwrap_or_else(Instant::now);
    }

    #[test]
    fn detects_workspace_config_change() {
        let dir = tempfile::tempdir().expect("tempdir");
        let config_path = dir.path().join("vtcode.toml");
        std::fs::write(&config_path, "mode = \"auto\"\n").expect("write config");

        let mut watcher = SimpleConfigWatcher::new(dir.path().to_path_buf());
        watcher.set_debounce_duration(0);
        open_check_window(&mut watcher);

        assert!(!watcher.should_reload(), "baseline poll records mtimes, sees no change");

        std::thread::sleep(Duration::from_millis(30));
        std::fs::write(&config_path, "mode = \"hidden\"\n").expect("rewrite config");
        open_check_window(&mut watcher);

        assert!(watcher.should_reload(), "modified workspace config must trigger reload");
    }

    #[test]
    fn tracks_additional_watch_paths() {
        let dir = tempfile::tempdir().expect("tempdir");
        let workspace_config = dir.path().join("vtcode.toml");
        let extra_config: PathBuf = dir.path().join("user-config.toml");
        std::fs::write(&workspace_config, "mode = \"auto\"\n").expect("write config");
        std::fs::write(&extra_config, "key = 1\n").expect("write extra config");

        let mut watcher = SimpleConfigWatcher::new(dir.path().to_path_buf());
        watcher.add_watch_path(extra_config.clone());
        watcher.set_debounce_duration(0);
        open_check_window(&mut watcher);

        assert!(!watcher.should_reload(), "baseline poll records mtimes, sees no change");

        std::thread::sleep(Duration::from_millis(30));
        std::fs::write(&extra_config, "key = 2\n").expect("rewrite extra config");
        open_check_window(&mut watcher);

        assert!(watcher.should_reload(), "modified additional config must trigger reload");
    }

    #[test]
    fn detects_creation_of_missing_additional_watch_path() {
        let dir = tempfile::tempdir().expect("tempdir");
        let extra_config = dir.path().join("created-user-config.toml");

        let mut watcher = SimpleConfigWatcher::new(dir.path().to_path_buf());
        watcher.add_watch_path(extra_config.clone());
        watcher.set_debounce_duration(0);
        open_check_window(&mut watcher);

        assert!(!watcher.should_reload(), "missing path establishes a baseline");

        std::fs::write(&extra_config, "key = 1\n").expect("create extra config");
        open_check_window(&mut watcher);

        assert!(watcher.should_reload(), "creating a watched config must trigger reload");
    }

    #[test]
    fn detects_change_before_first_poll_after_baseline_seed() {
        let dir = tempfile::tempdir().expect("tempdir");
        let extra_config = dir.path().join("user-vtcode.toml");
        std::fs::write(&extra_config, "mode = \"auto\"\n").expect("write config");

        let mut watcher = SimpleConfigWatcher::new(dir.path().to_path_buf());
        watcher.add_watch_path(extra_config.clone());
        watcher.seed_current_mtimes();
        watcher.set_debounce_duration(0);
        std::fs::write(&extra_config, "mode = \"command\"\n").expect("modify config");
        open_check_window(&mut watcher);

        assert!(watcher.should_reload(), "a change after baseline seeding must trigger reload");
    }

    #[test]
    fn does_not_consume_a_change_during_debounce() {
        let dir = tempfile::tempdir().expect("tempdir");
        let config_path = dir.path().join("vtcode.toml");
        std::fs::write(&config_path, "mode = \"auto\"\n").expect("write config");

        let mut watcher = SimpleConfigWatcher::new(dir.path().to_path_buf());
        watcher.set_debounce_duration(0);
        open_check_window(&mut watcher);
        assert!(!watcher.should_reload(), "baseline poll records mtimes");

        std::thread::sleep(Duration::from_millis(30));
        std::fs::write(&config_path, "mode = \"hidden\"\n").expect("rewrite config");
        open_check_window(&mut watcher);
        assert!(watcher.should_reload(), "first change is outside the debounce window");

        watcher.set_debounce_duration(60_000);
        std::thread::sleep(Duration::from_millis(30));
        std::fs::write(&config_path, "mode = \"command\"\n").expect("rewrite config again");
        open_check_window(&mut watcher);
        assert!(!watcher.should_reload(), "second change is debounced");

        watcher.set_debounce_duration(0);
        open_check_window(&mut watcher);
        assert!(watcher.should_reload(), "debounced change remains observable");
    }
}