ccswarm 0.5.0

AI-powered multi-agent orchestration system with proactive intelligence, security monitoring, and session management
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
//! Watch mode for Piece/Movement workflows.
//!
//! Monitors file system changes and automatically triggers workflow execution.
//! Inspired by takt's watch mode feature.

use anyhow::Result;
use serde::{Deserialize, Serialize};
use std::collections::HashSet;
use std::path::{Path, PathBuf};
use std::time::{Duration, Instant};
use tracing::{debug, info, warn};

/// Configuration for watch mode
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WatchConfig {
    /// Directories to watch
    pub watch_paths: Vec<PathBuf>,
    /// File patterns to include (glob patterns)
    #[serde(default)]
    pub include_patterns: Vec<String>,
    /// File patterns to exclude (glob patterns)
    #[serde(default)]
    pub exclude_patterns: Vec<String>,
    /// Debounce duration in milliseconds
    #[serde(default = "default_debounce")]
    pub debounce_ms: u64,
    /// Piece to execute when changes are detected
    pub piece_name: String,
    /// Whether to run the full piece or just validate
    #[serde(default = "default_true")]
    pub full_execution: bool,
    /// Whether to clear the terminal before each run
    #[serde(default)]
    pub clear_screen: bool,
    /// Maximum consecutive runs without user intervention
    #[serde(default = "default_max_consecutive")]
    pub max_consecutive_runs: u32,
}

fn default_debounce() -> u64 {
    500
}

fn default_true() -> bool {
    true
}

fn default_max_consecutive() -> u32 {
    10
}

impl Default for WatchConfig {
    fn default() -> Self {
        Self {
            watch_paths: vec![PathBuf::from(".")],
            include_patterns: vec!["**/*.rs".to_string(), "**/*.ts".to_string()],
            exclude_patterns: vec![
                "**/target/**".to_string(),
                "**/node_modules/**".to_string(),
                "**/.git/**".to_string(),
            ],
            debounce_ms: default_debounce(),
            piece_name: "default".to_string(),
            full_execution: true,
            clear_screen: false,
            max_consecutive_runs: default_max_consecutive(),
        }
    }
}

/// A detected file change
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FileChange {
    /// Path of the changed file
    pub path: PathBuf,
    /// Type of change
    pub change_type: ChangeType,
    /// Timestamp of detection
    pub detected_at: std::time::SystemTime,
}

/// Type of file change
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ChangeType {
    /// File was created
    Created,
    /// File was modified
    Modified,
    /// File was deleted
    Deleted,
}

/// State of the watch mode
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum WatchState {
    /// Watching for changes
    Idle,
    /// Debouncing (waiting for changes to settle)
    Debouncing,
    /// Executing the workflow
    Executing,
    /// Paused (max runs reached or user paused)
    Paused,
    /// Stopped
    Stopped,
}

/// Watch mode controller (non-blocking, poll-based)
pub struct WatchController {
    config: WatchConfig,
    state: WatchState,
    /// Accumulated changes during debounce period
    pending_changes: Vec<FileChange>,
    /// Last execution time
    last_execution: Option<Instant>,
    /// Consecutive run count
    consecutive_runs: u32,
    /// Debounce start time
    debounce_start: Option<Instant>,
    /// Known file modification times for change detection
    known_files: std::collections::HashMap<PathBuf, std::time::SystemTime>,
}

impl WatchController {
    pub fn new(config: WatchConfig) -> Self {
        Self {
            config,
            state: WatchState::Idle,
            pending_changes: Vec::new(),
            last_execution: None,
            consecutive_runs: 0,
            debounce_start: None,
            known_files: std::collections::HashMap::new(),
        }
    }

    /// Get the current watch state
    pub fn state(&self) -> &WatchState {
        &self.state
    }

    /// Record a file change event
    pub fn record_change(&mut self, change: FileChange) {
        if self.state == WatchState::Stopped {
            return;
        }

        // Filter by include/exclude patterns
        let path_str = change.path.to_string_lossy().to_string();

        if !self.config.exclude_patterns.is_empty()
            && self
                .config
                .exclude_patterns
                .iter()
                .any(|p| simple_glob_match(p, &path_str))
        {
            debug!("Excluded change: {}", path_str);
            return;
        }

        if !self.config.include_patterns.is_empty()
            && !self
                .config
                .include_patterns
                .iter()
                .any(|p| simple_glob_match(p, &path_str))
        {
            debug!("Not included: {}", path_str);
            return;
        }

        info!(
            "Change detected: {:?} {:?}",
            change.change_type, change.path
        );
        self.pending_changes.push(change);

        // Start or reset debounce timer
        self.debounce_start = Some(Instant::now());
        self.state = WatchState::Debouncing;
    }

    /// Poll the controller to check if a workflow should be triggered.
    /// Returns Some(changes) if debounce period has elapsed and execution should start.
    pub fn poll(&mut self) -> Option<Vec<FileChange>> {
        if self.state != WatchState::Debouncing {
            return None;
        }

        if let Some(start) = self.debounce_start {
            let debounce = Duration::from_millis(self.config.debounce_ms);
            if start.elapsed() >= debounce {
                // Check max consecutive runs
                if self.consecutive_runs >= self.config.max_consecutive_runs {
                    warn!(
                        "Max consecutive runs ({}) reached, pausing watch",
                        self.config.max_consecutive_runs
                    );
                    self.state = WatchState::Paused;
                    return None;
                }

                // Drain pending changes and trigger
                let changes: Vec<FileChange> = self.pending_changes.drain(..).collect();
                self.state = WatchState::Executing;
                self.last_execution = Some(Instant::now());
                self.consecutive_runs += 1;
                self.debounce_start = None;

                return Some(changes);
            }
        }

        None
    }

    /// Mark execution as complete, return to idle
    pub fn execution_complete(&mut self) {
        self.state = WatchState::Idle;
    }

    /// Resume from paused state, reset consecutive counter
    pub fn resume(&mut self) {
        self.consecutive_runs = 0;
        self.state = WatchState::Idle;
        info!("Watch mode resumed");
    }

    /// Pause watching
    pub fn pause(&mut self) {
        self.state = WatchState::Paused;
    }

    /// Stop watching
    pub fn stop(&mut self) {
        self.state = WatchState::Stopped;
        self.pending_changes.clear();
    }

    /// Get summary of pending changes
    pub fn pending_summary(&self) -> WatchSummary {
        let unique_files: HashSet<&PathBuf> =
            self.pending_changes.iter().map(|c| &c.path).collect();

        WatchSummary {
            pending_count: self.pending_changes.len(),
            unique_files: unique_files.len(),
            consecutive_runs: self.consecutive_runs,
            state: self.state.clone(),
        }
    }

    /// Scan directories for changes (compare with known state)
    pub fn scan_for_changes(&mut self) -> Result<Vec<FileChange>> {
        let mut changes = Vec::new();

        let watch_paths = self.config.watch_paths.clone();
        for watch_path in &watch_paths {
            if !watch_path.exists() {
                continue;
            }
            self.scan_directory(watch_path, &mut changes)?;
        }

        // Record all detected changes
        for change in &changes {
            self.record_change(change.clone());
        }

        Ok(changes)
    }

    fn scan_directory(&mut self, dir: &Path, changes: &mut Vec<FileChange>) -> Result<()> {
        let entries = match std::fs::read_dir(dir) {
            Ok(entries) => entries,
            Err(e) => {
                warn!("Cannot read directory {}: {}", dir.display(), e);
                return Ok(());
            }
        };

        for entry in entries.flatten() {
            let path = entry.path();

            if path.is_dir() {
                // Skip excluded directories
                let path_str = path.to_string_lossy().to_string();
                if self
                    .config
                    .exclude_patterns
                    .iter()
                    .any(|p| simple_glob_match(p, &path_str))
                {
                    continue;
                }
                self.scan_directory(&path, changes)?;
            } else if path.is_file()
                && let Ok(metadata) = std::fs::metadata(&path)
                && let Ok(modified) = metadata.modified()
            {
                match self.known_files.get(&path) {
                    Some(known_time) if *known_time != modified => {
                        changes.push(FileChange {
                            path: path.clone(),
                            change_type: ChangeType::Modified,
                            detected_at: std::time::SystemTime::now(),
                        });
                    }
                    None => {
                        changes.push(FileChange {
                            path: path.clone(),
                            change_type: ChangeType::Created,
                            detected_at: std::time::SystemTime::now(),
                        });
                    }
                    _ => {} // No change
                }
                self.known_files.insert(path, modified);
            }
        }

        Ok(())
    }
}

/// Summary of watch state
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WatchSummary {
    /// Number of pending changes
    pub pending_count: usize,
    /// Number of unique files with changes
    pub unique_files: usize,
    /// Consecutive runs since last pause
    pub consecutive_runs: u32,
    /// Current state
    pub state: WatchState,
}

/// Simple glob-style match (supports ** and *)
fn simple_glob_match(pattern: &str, path: &str) -> bool {
    if pattern.contains("**") {
        // Split by ** and check that all literal segments appear in order
        let segments: Vec<&str> = pattern.split("**").collect();

        // Trim path separators from each segment
        let segments: Vec<&str> = segments.iter().map(|s| s.trim_matches('/')).collect();

        // For patterns like **/foo/** or **/*.rs
        // Check each non-empty segment appears in the path
        let mut remaining = path;
        for seg in &segments {
            if seg.is_empty() {
                continue;
            }
            // If segment contains *, match it as a simple glob against the filename
            if seg.contains('*') {
                let filename = path.rsplit('/').next().unwrap_or(path);
                if !simple_star_match(seg, filename) {
                    return false;
                }
            } else if let Some(pos) = remaining.find(seg) {
                remaining = &remaining[pos + seg.len()..];
            } else {
                return false;
            }
        }
        return true;
    }

    if pattern.contains('*') {
        return simple_star_match(pattern, path);
    }

    path == pattern || path.ends_with(pattern)
}

/// Match a pattern with single `*` wildcards (no `**`)
fn simple_star_match(pattern: &str, text: &str) -> bool {
    let parts: Vec<&str> = pattern.split('*').collect();
    if parts.len() == 2 {
        return text.starts_with(parts[0]) && text.ends_with(parts[1]);
    }
    // Multiple single stars: check segments appear in order
    let mut remaining = text;
    for (i, part) in parts.iter().enumerate() {
        if part.is_empty() {
            continue;
        }
        if i == 0 {
            if !remaining.starts_with(part) {
                return false;
            }
            remaining = &remaining[part.len()..];
        } else if i == parts.len() - 1 {
            if !remaining.ends_with(part) {
                return false;
            }
        } else if let Some(pos) = remaining.find(part) {
            remaining = &remaining[pos + part.len()..];
        } else {
            return false;
        }
    }
    true
}

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

    #[test]
    fn test_watch_config_default() {
        let config = WatchConfig::default();
        assert_eq!(config.debounce_ms, 500);
        assert!(config.full_execution);
        assert_eq!(config.max_consecutive_runs, 10);
    }

    #[test]
    fn test_record_and_poll() {
        let config = WatchConfig {
            debounce_ms: 0, // Immediate
            ..WatchConfig::default()
        };
        let mut controller = WatchController::new(config);

        controller.record_change(FileChange {
            path: PathBuf::from("src/main.rs"),
            change_type: ChangeType::Modified,
            detected_at: std::time::SystemTime::now(),
        });

        assert_eq!(*controller.state(), WatchState::Debouncing);

        // Poll should return changes since debounce is 0
        std::thread::sleep(Duration::from_millis(1));
        let changes = controller.poll();
        assert!(changes.is_some());
        assert_eq!(changes.unwrap().len(), 1);
        assert_eq!(*controller.state(), WatchState::Executing);
    }

    #[test]
    fn test_max_consecutive_runs() {
        let config = WatchConfig {
            debounce_ms: 0,
            max_consecutive_runs: 2,
            ..WatchConfig::default()
        };
        let mut controller = WatchController::new(config);

        // Run 1
        controller.record_change(FileChange {
            path: PathBuf::from("a.rs"),
            change_type: ChangeType::Modified,
            detected_at: std::time::SystemTime::now(),
        });
        std::thread::sleep(Duration::from_millis(1));
        assert!(controller.poll().is_some());
        controller.execution_complete();

        // Run 2
        controller.record_change(FileChange {
            path: PathBuf::from("b.rs"),
            change_type: ChangeType::Modified,
            detected_at: std::time::SystemTime::now(),
        });
        std::thread::sleep(Duration::from_millis(1));
        assert!(controller.poll().is_some());
        controller.execution_complete();

        // Run 3 should be paused
        controller.record_change(FileChange {
            path: PathBuf::from("c.rs"),
            change_type: ChangeType::Modified,
            detected_at: std::time::SystemTime::now(),
        });
        std::thread::sleep(Duration::from_millis(1));
        assert!(controller.poll().is_none());
        assert_eq!(*controller.state(), WatchState::Paused);
    }

    #[test]
    fn test_resume_after_pause() {
        let config = WatchConfig {
            debounce_ms: 0,
            max_consecutive_runs: 1,
            ..WatchConfig::default()
        };
        let mut controller = WatchController::new(config);

        controller.record_change(FileChange {
            path: PathBuf::from("a.rs"),
            change_type: ChangeType::Modified,
            detected_at: std::time::SystemTime::now(),
        });
        std::thread::sleep(Duration::from_millis(1));
        controller.poll();
        controller.execution_complete();

        controller.record_change(FileChange {
            path: PathBuf::from("b.rs"),
            change_type: ChangeType::Modified,
            detected_at: std::time::SystemTime::now(),
        });
        std::thread::sleep(Duration::from_millis(1));
        controller.poll(); // Paused

        controller.resume();
        assert_eq!(*controller.state(), WatchState::Idle);
        assert_eq!(controller.consecutive_runs, 0);
    }

    #[test]
    fn test_stop() {
        let mut controller = WatchController::new(WatchConfig::default());
        controller.stop();
        assert_eq!(*controller.state(), WatchState::Stopped);

        // Changes after stop should be ignored
        controller.record_change(FileChange {
            path: PathBuf::from("a.rs"),
            change_type: ChangeType::Modified,
            detected_at: std::time::SystemTime::now(),
        });
        assert_eq!(*controller.state(), WatchState::Stopped);
    }

    #[test]
    fn test_exclude_patterns() {
        let config = WatchConfig {
            debounce_ms: 0,
            exclude_patterns: vec!["**/target/**".to_string()],
            include_patterns: vec![], // Allow all
            ..WatchConfig::default()
        };
        let mut controller = WatchController::new(config);

        controller.record_change(FileChange {
            path: PathBuf::from("target/debug/main"),
            change_type: ChangeType::Modified,
            detected_at: std::time::SystemTime::now(),
        });

        // Should not be recorded (excluded)
        assert_eq!(controller.pending_changes.len(), 0);
    }

    #[test]
    fn test_glob_match() {
        assert!(simple_glob_match("**/*.rs", "src/main.rs"));
        assert!(simple_glob_match("**/*.rs", "deep/nested/file.rs"));
        assert!(!simple_glob_match("**/*.rs", "src/main.ts"));
        assert!(simple_glob_match("**/target/**", "target/debug/build"));
        assert!(simple_glob_match("*.txt", "readme.txt"));
    }

    #[test]
    fn test_pending_summary() {
        let config = WatchConfig {
            debounce_ms: 10000, // Long debounce so changes stay pending
            include_patterns: vec![],
            exclude_patterns: vec![],
            ..WatchConfig::default()
        };
        let mut controller = WatchController::new(config);

        controller.record_change(FileChange {
            path: PathBuf::from("a.rs"),
            change_type: ChangeType::Modified,
            detected_at: std::time::SystemTime::now(),
        });
        controller.record_change(FileChange {
            path: PathBuf::from("a.rs"),
            change_type: ChangeType::Modified,
            detected_at: std::time::SystemTime::now(),
        });
        controller.record_change(FileChange {
            path: PathBuf::from("b.rs"),
            change_type: ChangeType::Created,
            detected_at: std::time::SystemTime::now(),
        });

        let summary = controller.pending_summary();
        assert_eq!(summary.pending_count, 3);
        assert_eq!(summary.unique_files, 2);
    }
}