ccswarm 0.4.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
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::VecDeque;
use std::sync::{Arc, Mutex, RwLock};
use std::time::{Duration, Instant};
use tokio::sync::broadcast;

/// Maximum number of output lines to buffer per agent
const MAX_BUFFER_SIZE: usize = 10000;

/// Maximum age of buffered output before automatic cleanup
const MAX_OUTPUT_AGE: Duration = Duration::from_secs(3600); // 1 hour

/// Type of output message
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum OutputType {
    Stdout,
    Stderr,
    Info,
    Warning,
    Error,
    Debug,
    System,
}

/// A single output entry from an agent
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OutputEntry {
    pub timestamp: DateTime<Utc>,
    pub agent_id: String,
    pub agent_type: String,
    pub output_type: OutputType,
    pub content: String,
    pub task_id: Option<String>,
    pub session_id: String,
}

impl OutputEntry {
    /// Create a new output entry
    pub fn new(
        agent_id: String,
        agent_type: String,
        output_type: OutputType,
        content: String,
        task_id: Option<String>,
        session_id: String,
    ) -> Self {
        Self {
            timestamp: Utc::now(),
            agent_id,
            agent_type,
            output_type,
            content,
            task_id,
            session_id,
        }
    }

    /// Check if this entry matches a filter pattern
    pub fn matches_filter(&self, filter: &OutputFilter) -> bool {
        // Check agent filter
        if let Some(ref agent_ids) = filter.agent_ids {
            if !agent_ids.contains(&self.agent_id) {
                return false;
            }
        }

        // Check output type filter
        if let Some(ref output_types) = filter.output_types {
            if !output_types.contains(&self.output_type) {
                return false;
            }
        }

        // Check content filter
        if let Some(ref pattern) = filter.content_pattern {
            if !self
                .content
                .to_lowercase()
                .contains(&pattern.to_lowercase())
            {
                return false;
            }
        }

        // Check task filter
        if let Some(ref task_id) = filter.task_id {
            if self.task_id.as_ref() != Some(task_id) {
                return false;
            }
        }

        true
    }
}

/// Filter for output entries
#[derive(Debug, Clone, Default)]
pub struct OutputFilter {
    pub agent_ids: Option<Vec<String>>,
    pub output_types: Option<Vec<OutputType>>,
    pub content_pattern: Option<String>,
    pub task_id: Option<String>,
}

/// Output stream for a single agent
pub struct AgentOutputStream {
    buffer: Arc<Mutex<VecDeque<OutputEntry>>>,
    last_cleanup: Arc<Mutex<Instant>>,
    #[allow(dead_code)] // Used in matches_filter method via OutputEntry
    agent_id: String,
    broadcast_tx: broadcast::Sender<OutputEntry>,
}

impl AgentOutputStream {
    /// Create a new agent output stream
    pub fn new(agent_id: String) -> Self {
        let (broadcast_tx, _) = broadcast::channel(1024);
        Self {
            buffer: Arc::new(Mutex::new(VecDeque::with_capacity(MAX_BUFFER_SIZE))),
            last_cleanup: Arc::new(Mutex::new(Instant::now())),
            agent_id,
            broadcast_tx,
        }
    }

    /// Add an output entry to the stream
    pub fn add_output(&self, entry: OutputEntry) -> Result<(), String> {
        // Broadcast to live subscribers
        let _ = self.broadcast_tx.send(entry.clone());

        let mut buffer = self
            .buffer
            .lock()
            .map_err(|e| format!("Failed to lock buffer: {}", e))?;

        // Add new entry
        buffer.push_back(entry);

        // Enforce buffer size limit
        while buffer.len() > MAX_BUFFER_SIZE {
            buffer.pop_front();
        }

        // Periodic cleanup of old entries
        let mut last_cleanup = self
            .last_cleanup
            .lock()
            .map_err(|e| format!("Failed to lock cleanup time: {}", e))?;
        if last_cleanup.elapsed() > Duration::from_secs(300) {
            // Check every 5 minutes
            let cutoff_time = Utc::now()
                - chrono::Duration::from_std(MAX_OUTPUT_AGE)
                    .expect("MAX_OUTPUT_AGE should be a valid duration");
            buffer.retain(|entry| entry.timestamp > cutoff_time);
            *last_cleanup = Instant::now();
        }

        Ok(())
    }

    /// Get recent output entries with optional filter
    pub fn get_recent(&self, count: usize, filter: Option<&OutputFilter>) -> Vec<OutputEntry> {
        let buffer = match self.buffer.lock() {
            Ok(b) => b,
            Err(_) => return vec![],
        };

        buffer
            .iter()
            .rev()
            .filter(|entry| filter.is_none_or(|f| entry.matches_filter(f)))
            .take(count)
            .cloned()
            .collect::<Vec<_>>()
            .into_iter()
            .rev()
            .collect()
    }

    /// Subscribe to live output updates
    pub fn subscribe(&self) -> broadcast::Receiver<OutputEntry> {
        self.broadcast_tx.subscribe()
    }

    /// Clear all buffered output
    pub fn clear(&self) {
        if let Ok(mut buffer) = self.buffer.lock() {
            buffer.clear();
        }
    }

    /// Get the total number of buffered entries
    pub fn len(&self) -> usize {
        self.buffer.lock().map(|b| b.len()).unwrap_or(0)
    }

    /// Check if the buffer is empty
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }
}

/// Trait for output subscribers
pub trait OutputSubscriber: Send + Sync {
    /// Called when a new output entry is available
    fn on_output(&self, entry: &OutputEntry);

    /// Get the subscriber ID
    fn id(&self) -> &str;

    /// Check if this subscriber is interested in the given entry
    fn accepts(&self, _entry: &OutputEntry) -> bool {
        true // Default implementation accepts all entries
    }
}

/// Statistics for monitoring system
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MonitoringStats {
    pub total_entries: usize,
    pub entries_per_agent: std::collections::HashMap<String, usize>,
    pub entries_per_type: std::collections::HashMap<String, usize>,
    pub active_streams: usize,
    pub total_subscribers: usize,
}

/// Central monitoring system for all agents
pub struct MonitoringSystem {
    streams: Arc<RwLock<std::collections::HashMap<String, Arc<AgentOutputStream>>>>,
    subscribers: Arc<RwLock<Vec<Arc<dyn OutputSubscriber>>>>,
    global_broadcast: broadcast::Sender<OutputEntry>,
}

impl MonitoringSystem {
    /// Create a new monitoring system
    pub fn new() -> Self {
        let (global_broadcast, _) = broadcast::channel(2048);
        Self {
            streams: Arc::new(RwLock::new(std::collections::HashMap::new())),
            subscribers: Arc::new(RwLock::new(Vec::new())),
            global_broadcast,
        }
    }

    /// Register a new agent stream
    pub fn register_agent(&self, agent_id: String) -> Result<Arc<AgentOutputStream>, String> {
        let mut streams = self
            .streams
            .write()
            .map_err(|e| format!("Failed to lock streams: {}", e))?;

        let stream = Arc::new(AgentOutputStream::new(agent_id.clone()));
        streams.insert(agent_id, stream.clone());

        Ok(stream)
    }

    /// Unregister an agent stream
    pub fn unregister_agent(&self, agent_id: &str) -> Result<(), String> {
        let mut streams = self
            .streams
            .write()
            .map_err(|e| format!("Failed to lock streams: {}", e))?;
        streams.remove(agent_id);
        Ok(())
    }

    /// Get an agent's output stream
    pub fn get_agent_stream(&self, agent_id: &str) -> Option<Arc<AgentOutputStream>> {
        self.streams.read().ok()?.get(agent_id).cloned()
    }

    /// Add an output entry for an agent
    pub fn add_output(
        &self,
        agent_id: String,
        agent_type: String,
        output_type: OutputType,
        content: String,
        task_id: Option<String>,
        session_id: String,
    ) -> Result<(), String> {
        let entry = OutputEntry::new(
            agent_id.clone(),
            agent_type,
            output_type,
            content,
            task_id,
            session_id,
        );

        // Send to global broadcast
        let _ = self.global_broadcast.send(entry.clone());

        // Send to agent-specific stream
        if let Some(stream) = self.get_agent_stream(&agent_id) {
            stream.add_output(entry.clone())?;
        } else {
            // Auto-register agent if not exists
            let stream = self.register_agent(agent_id)?;
            stream.add_output(entry.clone())?;
        }

        // Notify subscribers
        if let Ok(subscribers) = self.subscribers.read() {
            for subscriber in subscribers.iter() {
                if subscriber.accepts(&entry) {
                    subscriber.on_output(&entry);
                }
            }
        }

        Ok(())
    }

    /// Subscribe to global output updates
    pub fn subscribe_global(&self) -> broadcast::Receiver<OutputEntry> {
        self.global_broadcast.subscribe()
    }

    /// Add an output subscriber
    pub fn add_subscriber(&self, subscriber: Arc<dyn OutputSubscriber>) -> Result<(), String> {
        let mut subscribers = self
            .subscribers
            .write()
            .map_err(|e| format!("Failed to lock subscribers: {}", e))?;
        subscribers.push(subscriber);
        Ok(())
    }

    /// Remove an output subscriber
    pub fn remove_subscriber(&self, subscriber_id: &str) -> Result<(), String> {
        let mut subscribers = self
            .subscribers
            .write()
            .map_err(|e| format!("Failed to lock subscribers: {}", e))?;
        subscribers.retain(|s| s.id() != subscriber_id);
        Ok(())
    }

    /// Get recent output from all agents
    pub fn get_all_recent(&self, count: usize, filter: Option<&OutputFilter>) -> Vec<OutputEntry> {
        let streams = match self.streams.read() {
            Ok(s) => s,
            Err(_) => return vec![],
        };

        let mut all_entries: Vec<OutputEntry> = streams
            .values()
            .flat_map(|stream| stream.get_recent(count, filter))
            .collect();

        // Sort by timestamp
        all_entries.sort_by(|a, b| a.timestamp.cmp(&b.timestamp));

        // Take the most recent entries
        all_entries.into_iter().rev().take(count).rev().collect()
    }

    /// Clear output for a specific agent
    pub fn clear_agent_output(&self, agent_id: &str) -> Result<(), String> {
        if let Some(stream) = self.get_agent_stream(agent_id) {
            stream.clear();
            Ok(())
        } else {
            Err(format!("Agent stream not found: {}", agent_id))
        }
    }

    /// Clear all output
    pub fn clear_all_output(&self) -> Result<(), String> {
        let streams = self
            .streams
            .read()
            .map_err(|e| format!("Failed to lock streams: {}", e))?;
        for stream in streams.values() {
            stream.clear();
        }
        Ok(())
    }

    /// Get monitoring statistics
    pub fn get_stats(&self) -> MonitoringStats {
        let streams = match self.streams.read() {
            Ok(s) => s,
            Err(_) => {
                return MonitoringStats {
                    total_entries: 0,
                    entries_per_agent: std::collections::HashMap::new(),
                    entries_per_type: std::collections::HashMap::new(),
                    active_streams: 0,
                    total_subscribers: 0,
                };
            }
        };

        let mut total_entries = 0;
        let mut entries_per_agent = std::collections::HashMap::new();
        let mut entries_per_type: std::collections::HashMap<String, usize> =
            std::collections::HashMap::new();

        for (agent_id, stream) in streams.iter() {
            let count = stream.len();
            total_entries += count;
            entries_per_agent.insert(agent_id.clone(), count);

            // Count by type (sampling last 100 entries)
            for entry in stream.get_recent(100, None) {
                let type_name = format!("{:?}", entry.output_type);
                *entries_per_type.entry(type_name).or_insert(0) += 1;
            }
        }

        let total_subscribers = self.subscribers.read().map(|s| s.len()).unwrap_or(0);

        MonitoringStats {
            total_entries,
            entries_per_agent,
            entries_per_type,
            active_streams: streams.len(),
            total_subscribers,
        }
    }
}

impl Default for MonitoringSystem {
    fn default() -> Self {
        Self::new()
    }
}