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
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
//! Approval policies for HITL decisions

use serde::{Deserialize, Serialize};
use std::collections::HashSet;

use super::approval::ApprovalRequest;

/// Risk level of an action
#[derive(
    Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, Hash, Default,
)]
pub enum RiskLevel {
    /// No risk, can be auto-approved
    None,
    /// Low risk, minor changes
    Low,
    /// Medium risk, needs attention
    #[default]
    Medium,
    /// High risk, requires approval
    High,
    /// Critical risk, requires explicit approval
    Critical,
}

impl RiskLevel {
    /// Get display name
    pub fn display_name(&self) -> &'static str {
        match self {
            RiskLevel::None => "None",
            RiskLevel::Low => "Low",
            RiskLevel::Medium => "Medium",
            RiskLevel::High => "High",
            RiskLevel::Critical => "Critical",
        }
    }

    /// Get color for display
    pub fn color(&self) -> &'static str {
        match self {
            RiskLevel::None => "green",
            RiskLevel::Low => "blue",
            RiskLevel::Medium => "yellow",
            RiskLevel::High => "orange",
            RiskLevel::Critical => "red",
        }
    }
}

/// Type of action being performed
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Hash)]
pub enum ActionType {
    /// Reading a file
    FileRead,
    /// Writing to a file
    FileWrite,
    /// Deleting a file
    FileDelete,
    /// Executing a system command
    SystemCommand,
    /// Deploying to an environment
    Deploy,
    /// Modifying database
    DatabaseModify,
    /// Network request
    NetworkRequest,
    /// Git operation
    GitOperation,
    /// Environment variable change
    EnvChange,
    /// Configuration change
    ConfigChange,
    /// API call
    ApiCall,
    /// Custom action type
    Custom,
}

impl ActionType {
    /// Get default risk level for this action type
    pub fn default_risk(&self) -> RiskLevel {
        match self {
            ActionType::FileRead => RiskLevel::None,
            ActionType::FileWrite => RiskLevel::Low,
            ActionType::FileDelete => RiskLevel::High,
            ActionType::SystemCommand => RiskLevel::High,
            ActionType::Deploy => RiskLevel::Critical,
            ActionType::DatabaseModify => RiskLevel::Critical,
            ActionType::NetworkRequest => RiskLevel::Medium,
            ActionType::GitOperation => RiskLevel::Medium,
            ActionType::EnvChange => RiskLevel::High,
            ActionType::ConfigChange => RiskLevel::High,
            ActionType::ApiCall => RiskLevel::Medium,
            ActionType::Custom => RiskLevel::Medium,
        }
    }

    /// Get display name
    pub fn display_name(&self) -> &'static str {
        match self {
            ActionType::FileRead => "File Read",
            ActionType::FileWrite => "File Write",
            ActionType::FileDelete => "File Delete",
            ActionType::SystemCommand => "System Command",
            ActionType::Deploy => "Deploy",
            ActionType::DatabaseModify => "Database Modify",
            ActionType::NetworkRequest => "Network Request",
            ActionType::GitOperation => "Git Operation",
            ActionType::EnvChange => "Environment Change",
            ActionType::ConfigChange => "Config Change",
            ActionType::ApiCall => "API Call",
            ActionType::Custom => "Custom",
        }
    }
}

/// An approval policy that defines when approval is required
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ApprovalPolicy {
    /// Policy name
    pub name: String,
    /// Policy description
    pub description: Option<String>,
    /// Whether this policy is enabled
    pub enabled: bool,
    /// Priority (higher = evaluated first)
    pub priority: i32,
    /// Rules that trigger this policy
    pub rules: Vec<PolicyRule>,
    /// Required approvers
    pub required_approvers: Vec<String>,
    /// Minimum approvals needed
    pub min_approvals: usize,
    /// Allowed approvers (empty = anyone)
    pub allowed_approvers: HashSet<String>,
}

impl ApprovalPolicy {
    /// Create a new policy
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            description: None,
            enabled: true,
            priority: 0,
            rules: Vec::new(),
            required_approvers: Vec::new(),
            min_approvals: 1,
            allowed_approvers: HashSet::new(),
        }
    }

    /// Set description
    pub fn with_description(mut self, desc: impl Into<String>) -> Self {
        self.description = Some(desc.into());
        self
    }

    /// Set priority
    pub fn with_priority(mut self, priority: i32) -> Self {
        self.priority = priority;
        self
    }

    /// Add a rule
    pub fn with_rule(mut self, rule: PolicyRule) -> Self {
        self.rules.push(rule);
        self
    }

    /// Set minimum approvals
    pub fn with_min_approvals(mut self, min: usize) -> Self {
        self.min_approvals = min;
        self
    }

    /// Add required approver
    pub fn with_required_approver(mut self, approver: impl Into<String>) -> Self {
        self.required_approvers.push(approver.into());
        self
    }

    /// Add allowed approver
    pub fn with_allowed_approver(mut self, approver: impl Into<String>) -> Self {
        self.allowed_approvers.insert(approver.into());
        self
    }

    /// Check if this policy matches a request
    pub fn matches(&self, request: &ApprovalRequest) -> bool {
        if !self.enabled {
            return false;
        }

        // Must match at least one rule
        self.rules.iter().any(|rule| rule.matches(request))
    }

    /// Check if an approver is allowed
    pub fn can_approve(&self, approver: &str) -> bool {
        // If no restrictions, anyone can approve
        if self.allowed_approvers.is_empty() {
            return true;
        }

        self.allowed_approvers.contains(approver)
    }

    /// Check if required approvers have all approved
    pub fn all_required_approved(&self, approvers: &[String]) -> bool {
        self.required_approvers
            .iter()
            .all(|req| approvers.contains(req))
    }
}

/// A rule within a policy
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum PolicyRule {
    /// Require approval for specific action types
    RequireApproval {
        action_types: Vec<ActionType>,
        environments: Option<Vec<String>>,
    },
    /// Require approval above a risk level
    RiskThreshold { min_level: RiskLevel },
    /// Require approval for specific file patterns
    FilePattern { patterns: Vec<String> },
    /// Require approval for specific commands
    CommandPattern { patterns: Vec<String> },
    /// Require approval for specific agents
    AgentRestriction { agent_ids: Vec<String> },
    /// Time-based restriction
    TimeRestriction {
        /// Hours during which approval is required (0-23)
        require_during_hours: Vec<u32>,
        /// Days during which approval is required (0=Sun, 6=Sat)
        require_during_days: Vec<u32>,
    },
    /// Always require approval
    AlwaysRequire,
    /// Never require approval
    NeverRequire,
    /// Custom condition
    Custom { condition: String },
}

impl PolicyRule {
    /// Check if this rule matches a request
    pub fn matches(&self, request: &ApprovalRequest) -> bool {
        match self {
            PolicyRule::RequireApproval {
                action_types,
                environments,
            } => {
                let type_matches = action_types.contains(&request.action_type);
                let env_matches = environments.as_ref().is_none_or(|envs| {
                    request
                        .environment
                        .as_ref()
                        .is_some_and(|e| envs.contains(e))
                });
                type_matches && env_matches
            }
            PolicyRule::RiskThreshold { min_level } => request.risk_level >= *min_level,
            PolicyRule::FilePattern { patterns } => request.affected_files.iter().any(|file| {
                patterns
                    .iter()
                    .any(|pattern| file_matches_pattern(file, pattern))
            }),
            PolicyRule::CommandPattern { patterns } => request.commands.iter().any(|cmd| {
                patterns
                    .iter()
                    .any(|pattern| command_matches_pattern(cmd, pattern))
            }),
            PolicyRule::AgentRestriction { agent_ids } => request
                .agent_id
                .as_ref()
                .is_some_and(|id| agent_ids.contains(id)),
            PolicyRule::TimeRestriction {
                require_during_hours,
                require_during_days,
            } => {
                let now = chrono::Utc::now();
                let hour = now.format("%H").to_string().parse::<u32>().unwrap_or(0);
                let day = now.format("%w").to_string().parse::<u32>().unwrap_or(0);

                let hour_match =
                    require_during_hours.is_empty() || require_during_hours.contains(&hour);
                let day_match =
                    require_during_days.is_empty() || require_during_days.contains(&day);

                hour_match && day_match
            }
            PolicyRule::AlwaysRequire => true,
            PolicyRule::NeverRequire => false,
            PolicyRule::Custom { condition: _ } => {
                // Custom conditions would need a scripting engine
                false
            }
        }
    }
}

/// Check if a file path matches a glob-like pattern
fn file_matches_pattern(file: &str, pattern: &str) -> bool {
    // Simple pattern matching (supports * wildcard)
    if pattern.contains('*') {
        let parts: Vec<&str> = pattern.split('*').collect();
        if parts.len() == 2 {
            let prefix = parts[0];
            let suffix = parts[1];
            return file.starts_with(prefix) && file.ends_with(suffix);
        }
    }
    file == pattern || file.ends_with(pattern)
}

/// Check if a command matches a pattern
fn command_matches_pattern(cmd: &str, pattern: &str) -> bool {
    // Simple pattern matching
    if pattern.contains('*') {
        let parts: Vec<&str> = pattern.split('*').collect();
        if parts.len() == 2 {
            let prefix = parts[0];
            let suffix = parts[1];
            return cmd.starts_with(prefix) && cmd.ends_with(suffix);
        }
    }
    cmd.contains(pattern)
}

/// Predefined policies for common scenarios
#[allow(dead_code)]
pub struct PredefinedPolicies;

#[allow(dead_code)]
impl PredefinedPolicies {
    /// Block production deployments without approval
    pub fn production_deploy() -> ApprovalPolicy {
        ApprovalPolicy::new("production_deploy")
            .with_description("Require approval for production deployments")
            .with_priority(100)
            .with_rule(PolicyRule::RequireApproval {
                action_types: vec![ActionType::Deploy],
                environments: Some(vec!["production".to_string(), "prod".to_string()]),
            })
    }

    /// Require approval for all critical operations
    pub fn critical_operations() -> ApprovalPolicy {
        ApprovalPolicy::new("critical_operations")
            .with_description("Require approval for critical risk operations")
            .with_priority(90)
            .with_rule(PolicyRule::RiskThreshold {
                min_level: RiskLevel::Critical,
            })
    }

    /// Protect sensitive files
    pub fn sensitive_files() -> ApprovalPolicy {
        ApprovalPolicy::new("sensitive_files")
            .with_description("Require approval for sensitive file modifications")
            .with_priority(80)
            .with_rule(PolicyRule::FilePattern {
                patterns: vec![
                    ".env".to_string(),
                    "*.key".to_string(),
                    "*.pem".to_string(),
                    "*secret*".to_string(),
                    "*password*".to_string(),
                    "credentials*".to_string(),
                ],
            })
    }

    /// Block dangerous commands
    pub fn dangerous_commands() -> ApprovalPolicy {
        ApprovalPolicy::new("dangerous_commands")
            .with_description("Require approval for dangerous commands")
            .with_priority(70)
            .with_rule(PolicyRule::CommandPattern {
                patterns: vec![
                    "rm -rf".to_string(),
                    "drop database".to_string(),
                    "DELETE FROM".to_string(),
                    "TRUNCATE".to_string(),
                    "git push --force".to_string(),
                    "git reset --hard".to_string(),
                ],
            })
    }

    /// After-hours restrictions
    pub fn after_hours() -> ApprovalPolicy {
        ApprovalPolicy::new("after_hours")
            .with_description("Require approval outside business hours")
            .with_priority(50)
            .with_rule(PolicyRule::TimeRestriction {
                // Require approval between 6 PM and 8 AM
                require_during_hours: (0..8).chain(18..24).collect(),
                // And on weekends
                require_during_days: vec![0, 6], // Sunday, Saturday
            })
    }

    /// Get all predefined policies
    pub fn all() -> Vec<ApprovalPolicy> {
        vec![
            Self::production_deploy(),
            Self::critical_operations(),
            Self::sensitive_files(),
            Self::dangerous_commands(),
        ]
    }
}

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

    #[test]
    fn test_risk_level_ordering() {
        assert!(RiskLevel::Critical > RiskLevel::High);
        assert!(RiskLevel::High > RiskLevel::Medium);
        assert!(RiskLevel::Medium > RiskLevel::Low);
        assert!(RiskLevel::Low > RiskLevel::None);
    }

    #[test]
    fn test_action_type_default_risk() {
        assert_eq!(ActionType::FileRead.default_risk(), RiskLevel::None);
        assert_eq!(ActionType::Deploy.default_risk(), RiskLevel::Critical);
        assert_eq!(ActionType::FileDelete.default_risk(), RiskLevel::High);
    }

    #[test]
    fn test_policy_creation() {
        let policy = ApprovalPolicy::new("test_policy")
            .with_description("Test description")
            .with_priority(10)
            .with_rule(PolicyRule::RiskThreshold {
                min_level: RiskLevel::High,
            });

        assert_eq!(policy.name, "test_policy");
        assert_eq!(policy.priority, 10);
        assert_eq!(policy.rules.len(), 1);
    }

    #[test]
    fn test_policy_matching() {
        let policy = ApprovalPolicy::new("production").with_rule(PolicyRule::RequireApproval {
            action_types: vec![ActionType::Deploy],
            environments: Some(vec!["production".to_string()]),
        });

        let request_prod = ApprovalRequest::new("Deploy", ActionType::Deploy, RiskLevel::High)
            .with_environment("production");

        let request_dev = ApprovalRequest::new("Deploy", ActionType::Deploy, RiskLevel::Medium)
            .with_environment("development");

        assert!(policy.matches(&request_prod));
        assert!(!policy.matches(&request_dev));
    }

    #[test]
    fn test_risk_threshold_rule() {
        let rule = PolicyRule::RiskThreshold {
            min_level: RiskLevel::High,
        };

        let high_risk = ApprovalRequest::new("Delete", ActionType::FileDelete, RiskLevel::High);
        let low_risk = ApprovalRequest::new("Read", ActionType::FileRead, RiskLevel::Low);

        assert!(rule.matches(&high_risk));
        assert!(!rule.matches(&low_risk));
    }

    #[test]
    fn test_file_pattern_rule() {
        let rule = PolicyRule::FilePattern {
            patterns: vec!["*.env".to_string(), ".secret*".to_string()],
        };

        let matches = ApprovalRequest::new("Edit", ActionType::FileWrite, RiskLevel::Medium)
            .with_files(vec!["production.env".to_string()]);

        let no_match = ApprovalRequest::new("Edit", ActionType::FileWrite, RiskLevel::Medium)
            .with_files(vec!["README.md".to_string()]);

        assert!(rule.matches(&matches));
        assert!(!rule.matches(&no_match));
    }

    #[test]
    fn test_command_pattern_rule() {
        let rule = PolicyRule::CommandPattern {
            patterns: vec!["rm -rf".to_string(), "drop database".to_string()],
        };

        let dangerous = ApprovalRequest::new("Delete", ActionType::SystemCommand, RiskLevel::High)
            .with_commands(vec!["rm -rf /tmp/test".to_string()]);

        let safe = ApprovalRequest::new("List", ActionType::SystemCommand, RiskLevel::Low)
            .with_commands(vec!["ls -la".to_string()]);

        assert!(rule.matches(&dangerous));
        assert!(!rule.matches(&safe));
    }

    #[test]
    fn test_file_matches_pattern() {
        assert!(file_matches_pattern("config.env", "*.env"));
        assert!(file_matches_pattern(".env", ".env"));
        assert!(file_matches_pattern("src/secret.key", "*.key"));
        assert!(!file_matches_pattern("readme.md", "*.env"));
    }

    #[test]
    fn test_predefined_policies() {
        let policies = PredefinedPolicies::all();
        assert!(!policies.is_empty());

        let production_policy = PredefinedPolicies::production_deploy();
        assert_eq!(production_policy.name, "production_deploy");
    }

    #[test]
    fn test_approver_restrictions() {
        let policy = ApprovalPolicy::new("restricted")
            .with_allowed_approver("admin")
            .with_allowed_approver("lead");

        assert!(policy.can_approve("admin"));
        assert!(policy.can_approve("lead"));
        assert!(!policy.can_approve("dev"));
    }

    #[test]
    fn test_required_approvers() {
        let policy = ApprovalPolicy::new("multi_approval")
            .with_required_approver("security")
            .with_required_approver("lead");

        let partial = vec!["security".to_string()];
        let complete = vec!["security".to_string(), "lead".to_string()];

        assert!(!policy.all_required_approved(&partial));
        assert!(policy.all_required_approved(&complete));
    }
}