clawft-core 0.5.1

Core engine for clawft (agent loop, message bus, context management)
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
//! File-system watcher for automatic skill hot-reload.
//!
//! Watches skill directories for changes and triggers [`SkillRegistry`] rebuilds
//! with debouncing to avoid redundant work during rapid file operations.
//!
//! # Architecture
//!
//! The watcher runs as a background `tokio::spawn` task that:
//! 1. Receives events from the [`notify`] crate via an `mpsc` channel.
//! 2. Debounces rapid changes (default: 500ms).
//! 3. Acquires a write lock on [`SharedSkillRegistry`] only during rebuild.
//! 4. Releases the write lock immediately after rebuild completes.
//!
//! # Concurrency
//!
//! - Reads: Every agent loop iteration reads skills (high frequency).
//! - Writes: Only on file-system changes (low frequency).
//! - [`tokio::sync::RwLock`] is appropriate: many concurrent readers, rare
//!   exclusive writers. Write starvation is not a concern given low write
//!   frequency.

use std::path::PathBuf;
use std::time::Duration;

use notify::{Event, EventKind, RecommendedWatcher, RecursiveMode, Watcher};
use tokio::sync::mpsc;
use tracing::{debug, info, warn};

use clawft_types::skill::SkillDefinition;

use super::skills_v2::SharedSkillRegistry;

/// Configuration for the skill file-system watcher.
#[derive(Debug, Clone)]
pub struct SkillWatcherConfig {
    /// Workspace skill directory (highest priority).
    pub workspace_dir: Option<PathBuf>,
    /// User skill directory (medium priority).
    pub user_dir: Option<PathBuf>,
    /// Additional directories to watch (e.g. plugin-shipped skill dirs).
    pub extra_dirs: Vec<PathBuf>,
    /// Debounce duration for rapid file changes.
    pub debounce: Duration,
    /// Built-in skills (lowest priority, compiled into binary).
    pub builtin_skills: Vec<SkillDefinition>,
    /// Whether workspace skills are trusted.
    pub trust_workspace: bool,
}

impl Default for SkillWatcherConfig {
    fn default() -> Self {
        Self {
            workspace_dir: None,
            user_dir: None,
            extra_dirs: Vec::new(),
            debounce: Duration::from_millis(500),
            builtin_skills: Vec::new(),
            trust_workspace: false,
        }
    }
}

impl SkillWatcherConfig {
    /// Collect all directories that should be watched.
    fn watch_dirs(&self) -> Vec<&PathBuf> {
        let mut dirs = Vec::new();
        if let Some(ref d) = self.workspace_dir {
            dirs.push(d);
        }
        if let Some(ref d) = self.user_dir {
            dirs.push(d);
        }
        for d in &self.extra_dirs {
            dirs.push(d);
        }
        dirs
    }
}

/// Handle to a running skill watcher. Drop to stop watching.
pub struct SkillWatcherHandle {
    /// Sends a shutdown signal to the watcher task.
    shutdown_tx: Option<tokio::sync::oneshot::Sender<()>>,
}

impl SkillWatcherHandle {
    /// Stop the watcher gracefully.
    pub fn stop(mut self) {
        if let Some(tx) = self.shutdown_tx.take() {
            let _ = tx.send(());
        }
    }
}

impl Drop for SkillWatcherHandle {
    fn drop(&mut self) {
        if let Some(tx) = self.shutdown_tx.take() {
            let _ = tx.send(());
        }
    }
}

/// Start watching skill directories for changes.
///
/// Returns a [`SkillWatcherHandle`] that can be used to stop watching.
/// The watcher runs in a background tokio task and will automatically
/// rebuild the registry when files change.
///
/// # Errors
///
/// Returns an error if the file-system watcher cannot be created.
pub fn start_watching(
    config: SkillWatcherConfig,
    registry: SharedSkillRegistry,
) -> Result<SkillWatcherHandle, notify::Error> {
    let (shutdown_tx, mut shutdown_rx) = tokio::sync::oneshot::channel();
    let (event_tx, mut event_rx) = mpsc::channel::<Event>(100);

    // Create OS file watcher.
    let mut watcher = RecommendedWatcher::new(
        move |res: notify::Result<Event>| {
            if let Ok(event) = res {
                let _ = event_tx.blocking_send(event);
            }
        },
        notify::Config::default(),
    )?;

    // Watch all configured directories that exist.
    for dir in config.watch_dirs() {
        if dir.exists() {
            if let Err(e) = watcher.watch(dir, RecursiveMode::Recursive) {
                warn!(
                    path = %dir.display(),
                    error = %e,
                    "failed to watch skill directory"
                );
            } else {
                debug!(path = %dir.display(), "watching skill directory");
            }
        } else {
            debug!(path = %dir.display(), "skill directory does not exist, skipping watch");
        }
    }

    let debounce = config.debounce;
    let ws_dir = config.workspace_dir.clone();
    let user_dir = config.user_dir.clone();
    let builtins = config.builtin_skills.clone();
    let trust = config.trust_workspace;

    // Spawn the event processing loop.
    tokio::spawn(async move {
        // Keep the watcher alive for the duration of the task.
        let _watcher = watcher;

        let mut pending_rebuild = false;
        let mut debounce_deadline: Option<tokio::time::Instant> = None;

        loop {
            tokio::select! {
                // Receive file-system events.
                event = event_rx.recv() => {
                    match event {
                        Some(ev) => {
                            match ev.kind {
                                EventKind::Create(_)
                                | EventKind::Modify(_)
                                | EventKind::Remove(_) => {
                                    pending_rebuild = true;
                                    debounce_deadline = Some(
                                        tokio::time::Instant::now() + debounce,
                                    );
                                }
                                _ => {}
                            }
                        }
                        None => {
                            // Channel closed, exit.
                            break;
                        }
                    }
                }
                // Debounce timer fires.
                _ = async {
                    match debounce_deadline {
                        Some(deadline) => tokio::time::sleep_until(deadline).await,
                        None => std::future::pending::<()>().await,
                    }
                }, if pending_rebuild => {
                    pending_rebuild = false;
                    debounce_deadline = None;

                    let mut reg = registry.write().await;
                    match reg.rebuild(
                        ws_dir.as_deref(),
                        user_dir.as_deref(),
                        builtins.clone(),
                        trust,
                    ).await {
                        Ok(()) => {
                            info!(
                                count = reg.len(),
                                "skill registry reloaded after file change"
                            );
                        }
                        Err(e) => {
                            warn!(error = %e, "skill registry rebuild failed");
                        }
                    }
                }
                // Shutdown signal.
                _ = &mut shutdown_rx => {
                    info!("skill watcher shutting down");
                    break;
                }
            }
        }
    });

    Ok(SkillWatcherHandle {
        shutdown_tx: Some(shutdown_tx),
    })
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::atomic::{AtomicU64, Ordering};
    use std::sync::Arc;

    use tokio::sync::RwLock;

    use super::super::skills_v2::SkillRegistry;
    use clawft_types::skill::SkillDefinition;

    static TEST_COUNTER: AtomicU64 = AtomicU64::new(0);

    fn temp_dir(prefix: &str) -> PathBuf {
        let id = TEST_COUNTER.fetch_add(1, Ordering::Relaxed);
        let pid = std::process::id();
        std::env::temp_dir().join(format!("clawft_watcher_{prefix}_{pid}_{id}"))
    }

    fn create_skill_md(dir: &std::path::Path, name: &str, desc: &str) {
        let skill_dir = dir.join(name);
        std::fs::create_dir_all(&skill_dir).unwrap();
        let content =
            format!("---\nname: {name}\ndescription: {desc}\n---\n\nInstructions for {name}.");
        std::fs::write(skill_dir.join("SKILL.md"), content).unwrap();
    }

    #[tokio::test]
    async fn test_watcher_config_defaults() {
        let config = SkillWatcherConfig::default();
        assert_eq!(config.debounce, Duration::from_millis(500));
        assert!(config.workspace_dir.is_none());
        assert!(config.user_dir.is_none());
        assert!(config.extra_dirs.is_empty());
        assert!(config.builtin_skills.is_empty());
        assert!(!config.trust_workspace);
    }

    #[tokio::test]
    async fn test_watcher_config_watch_dirs() {
        let config = SkillWatcherConfig {
            workspace_dir: Some(PathBuf::from("/ws/skills")),
            user_dir: Some(PathBuf::from("/user/skills")),
            extra_dirs: vec![PathBuf::from("/extra/skills")],
            ..Default::default()
        };
        assert_eq!(config.watch_dirs().len(), 3);
    }

    #[tokio::test]
    async fn test_watcher_handle_drop_sends_shutdown() {
        let dir = temp_dir("handle_drop");
        std::fs::create_dir_all(&dir).unwrap();
        create_skill_md(&dir, "drop_test", "Drop test skill");

        let registry = Arc::new(RwLock::new(
            SkillRegistry::discover(Some(&dir), None, vec![])
                .await
                .unwrap(),
        ));

        let config = SkillWatcherConfig {
            workspace_dir: Some(dir.clone()),
            trust_workspace: true,
            debounce: Duration::from_millis(50),
            ..Default::default()
        };

        let handle = start_watching(config, registry).unwrap();
        // Dropping the handle should send shutdown
        drop(handle);

        // Give the task a moment to receive the signal
        tokio::time::sleep(Duration::from_millis(100)).await;

        let _ = std::fs::remove_dir_all(&dir);
    }

    #[tokio::test]
    async fn test_watcher_detects_new_skill() {
        let dir = temp_dir("detect_new");
        std::fs::create_dir_all(&dir).unwrap();
        create_skill_md(&dir, "original", "Original skill");

        let registry = Arc::new(RwLock::new(
            SkillRegistry::discover(Some(&dir), None, vec![])
                .await
                .unwrap(),
        ));

        {
            let reg = registry.read().await;
            assert_eq!(reg.len(), 1);
            assert!(reg.get("original").is_some());
        }

        let config = SkillWatcherConfig {
            workspace_dir: Some(dir.clone()),
            trust_workspace: true,
            debounce: Duration::from_millis(100),
            ..Default::default()
        };

        let handle = start_watching(config, registry.clone()).unwrap();

        // Add a new skill
        create_skill_md(&dir, "added", "Added skill");

        // Wait for debounce + rebuild
        tokio::time::sleep(Duration::from_millis(500)).await;

        {
            let reg = registry.read().await;
            assert_eq!(reg.len(), 2);
            assert!(reg.get("original").is_some());
            assert!(reg.get("added").is_some());
        }

        handle.stop();
        let _ = std::fs::remove_dir_all(&dir);
    }

    #[tokio::test]
    async fn test_watcher_detects_skill_removal() {
        let dir = temp_dir("detect_remove");
        std::fs::create_dir_all(&dir).unwrap();
        create_skill_md(&dir, "alpha", "Alpha");
        create_skill_md(&dir, "beta", "Beta");

        let registry = Arc::new(RwLock::new(
            SkillRegistry::discover(Some(&dir), None, vec![])
                .await
                .unwrap(),
        ));

        {
            let reg = registry.read().await;
            assert_eq!(reg.len(), 2);
        }

        let config = SkillWatcherConfig {
            workspace_dir: Some(dir.clone()),
            trust_workspace: true,
            debounce: Duration::from_millis(100),
            ..Default::default()
        };

        let handle = start_watching(config, registry.clone()).unwrap();

        // Remove one skill
        std::fs::remove_dir_all(dir.join("beta")).unwrap();

        // Wait for debounce + rebuild
        tokio::time::sleep(Duration::from_millis(500)).await;

        {
            let reg = registry.read().await;
            assert_eq!(reg.len(), 1);
            assert!(reg.get("alpha").is_some());
            assert!(reg.get("beta").is_none());
        }

        handle.stop();
        let _ = std::fs::remove_dir_all(&dir);
    }

    #[tokio::test]
    async fn test_watcher_detects_skill_modification() {
        let dir = temp_dir("detect_modify");
        std::fs::create_dir_all(&dir).unwrap();
        create_skill_md(&dir, "mutable", "Original description");

        let registry = Arc::new(RwLock::new(
            SkillRegistry::discover(Some(&dir), None, vec![])
                .await
                .unwrap(),
        ));

        {
            let reg = registry.read().await;
            assert_eq!(reg.get("mutable").unwrap().description, "Original description");
        }

        let config = SkillWatcherConfig {
            workspace_dir: Some(dir.clone()),
            trust_workspace: true,
            debounce: Duration::from_millis(100),
            ..Default::default()
        };

        let handle = start_watching(config, registry.clone()).unwrap();

        // Modify the skill
        let modified =
            "---\nname: mutable\ndescription: Updated description\n---\n\nUpdated instructions.";
        std::fs::write(dir.join("mutable").join("SKILL.md"), modified).unwrap();

        // Wait for debounce + rebuild
        tokio::time::sleep(Duration::from_millis(500)).await;

        {
            let reg = registry.read().await;
            assert_eq!(reg.get("mutable").unwrap().description, "Updated description");
        }

        handle.stop();
        let _ = std::fs::remove_dir_all(&dir);
    }

    #[tokio::test]
    async fn test_watcher_missing_dir_does_not_error() {
        let registry = Arc::new(RwLock::new(
            SkillRegistry::discover(None, None, vec![]).await.unwrap(),
        ));

        let config = SkillWatcherConfig {
            workspace_dir: Some(PathBuf::from("/definitely/does/not/exist")),
            debounce: Duration::from_millis(50),
            ..Default::default()
        };

        // Should not panic or error
        let handle = start_watching(config, registry);
        assert!(handle.is_ok());
        handle.unwrap().stop();
    }

    #[tokio::test]
    async fn test_registry_upsert_and_remove() {
        let mut registry = SkillRegistry::discover(None, None, vec![]).await.unwrap();
        assert!(registry.is_empty());

        // Upsert a skill
        let skill = SkillDefinition::new("dynamic", "A dynamically added skill");
        let prev = registry.upsert(skill);
        assert!(prev.is_none());
        assert_eq!(registry.len(), 1);
        assert!(registry.get("dynamic").is_some());

        // Upsert again (replace)
        let updated = SkillDefinition::new("dynamic", "Updated description");
        let prev = registry.upsert(updated);
        assert!(prev.is_some());
        assert_eq!(prev.unwrap().description, "A dynamically added skill");
        assert_eq!(registry.get("dynamic").unwrap().description, "Updated description");

        // Remove
        let removed = registry.remove("dynamic");
        assert!(removed.is_some());
        assert!(registry.is_empty());

        // Remove nonexistent
        let removed = registry.remove("nonexistent");
        assert!(removed.is_none());
    }

    #[tokio::test]
    async fn test_registry_rebuild() {
        let dir = temp_dir("rebuild");
        std::fs::create_dir_all(&dir).unwrap();
        create_skill_md(&dir, "first", "First skill");

        let mut registry = SkillRegistry::discover(Some(&dir), None, vec![]).await.unwrap();
        assert_eq!(registry.len(), 1);

        // Add another skill to disk
        create_skill_md(&dir, "second", "Second skill");

        // Rebuild
        registry.rebuild(Some(&dir), None, vec![], true).await.unwrap();
        assert_eq!(registry.len(), 2);
        assert!(registry.get("first").is_some());
        assert!(registry.get("second").is_some());

        let _ = std::fs::remove_dir_all(&dir);
    }

    #[tokio::test]
    async fn test_precedence_on_reload() {
        let ws_dir = temp_dir("prec_ws");
        let user_dir = temp_dir("prec_user");

        std::fs::create_dir_all(&ws_dir).unwrap();
        std::fs::create_dir_all(&user_dir).unwrap();

        // Both have a skill with the same name
        create_skill_md(&ws_dir, "shared", "Workspace version");
        create_skill_md(&user_dir, "shared", "User version");

        let registry = Arc::new(RwLock::new(
            SkillRegistry::discover(Some(&ws_dir), Some(&user_dir), vec![])
                .await
                .unwrap(),
        ));

        {
            let reg = registry.read().await;
            // Workspace takes priority
            assert_eq!(reg.get("shared").unwrap().description, "Workspace version");
        }

        let config = SkillWatcherConfig {
            workspace_dir: Some(ws_dir.clone()),
            user_dir: Some(user_dir.clone()),
            trust_workspace: true,
            debounce: Duration::from_millis(100),
            ..Default::default()
        };

        let handle = start_watching(config, registry.clone()).unwrap();

        // Remove the workspace skill -- user version should be revealed
        std::fs::remove_dir_all(ws_dir.join("shared")).unwrap();

        tokio::time::sleep(Duration::from_millis(500)).await;

        {
            let reg = registry.read().await;
            assert_eq!(reg.get("shared").unwrap().description, "User version");
        }

        handle.stop();
        let _ = std::fs::remove_dir_all(&ws_dir);
        let _ = std::fs::remove_dir_all(&user_dir);
    }
}