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
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
pub mod agent_extension;
pub mod meta_learning;
pub mod sangha;

use crate::error::{CCSwarmError, Result};
use crate::extension::{Extension, ExtensionState};
use crate::traits::{Identifiable, Stateful, Validatable};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Stub implementation for extensions with improved error handling
///
/// This module provides a lightweight implementation of the extension system
/// for testing and development purposes. It manages extension registration,
/// validation, and basic lifecycle operations.
#[derive(Debug, Clone)]
pub struct ExtensionStub {
    extensions: HashMap<String, Extension>,
    registry_path: Option<std::path::PathBuf>,
}

impl ExtensionStub {
    /// Create a new extension stub
    pub fn new() -> Self {
        Self {
            extensions: HashMap::new(),
            registry_path: None,
        }
    }

    /// Create extension stub with registry file path
    pub fn with_registry<P: Into<std::path::PathBuf>>(path: P) -> Self {
        Self {
            extensions: HashMap::new(),
            registry_path: Some(path.into()),
        }
    }

    /// Register a new extension
    pub fn register(&mut self, mut extension: Extension) -> Result<()> {
        // Validate extension before registration
        let validation_issues = extension.validate()?;
        if !validation_issues.is_empty() {
            // Try to auto-fix issues
            let fixes = extension.auto_fix()?;
            tracing::info!(
                "Auto-fixed {} issues for extension {}: {:?}",
                fixes.len(),
                extension.id(),
                fixes
            );

            // Re-validate after fixes
            let remaining_issues = extension.validate()?;
            if !remaining_issues.is_empty() {
                return Err(CCSwarmError::extension(
                    extension.id(),
                    format!("Extension validation failed: {:?}", remaining_issues),
                ));
            }
        }

        // Check for duplicate registration
        if self.extensions.contains_key(extension.id()) {
            return Err(CCSwarmError::extension(
                extension.id(),
                "Extension with this ID is already registered",
            ));
        }

        // Check dependencies
        let available_extensions: Vec<Extension> = self.extensions.values().cloned().collect();
        let missing_deps = extension.check_dependencies(&available_extensions)?;
        if !missing_deps.is_empty() {
            return Err(CCSwarmError::extension(
                extension.id(),
                format!("Missing dependencies: {:?}", missing_deps),
            ));
        }

        let extension_id = extension.id().to_string();
        self.extensions.insert(extension_id.clone(), extension);

        tracing::info!("Successfully registered extension: {}", extension_id);
        Ok(())
    }

    /// Unregister an extension
    pub fn unregister(&mut self, extension_id: &str) -> Result<Extension> {
        // Check if other extensions depend on this one
        let dependents: Vec<String> = self
            .extensions
            .values()
            .filter(|ext| {
                ext.dependencies
                    .iter()
                    .any(|dep| dep.name == extension_id && !dep.optional)
            })
            .map(|ext| ext.id().to_string())
            .collect();

        if !dependents.is_empty() {
            return Err(CCSwarmError::extension(
                extension_id,
                format!("Cannot unregister extension: required by {:?}", dependents),
            ));
        }

        self.extensions
            .remove(extension_id)
            .ok_or_else(|| CCSwarmError::extension(extension_id, "Extension not found"))
    }

    /// Get an extension by ID
    pub fn get(&self, extension_id: &str) -> Option<&Extension> {
        self.extensions.get(extension_id)
    }

    /// Get a mutable reference to an extension
    pub fn get_mut(&mut self, extension_id: &str) -> Option<&mut Extension> {
        self.extensions.get_mut(extension_id)
    }

    /// List all extensions
    pub fn list(&self) -> Vec<&Extension> {
        self.extensions.values().collect()
    }

    /// List extensions by state
    pub fn list_by_state(&self, state: &ExtensionState) -> Vec<&Extension> {
        self.extensions
            .values()
            .filter(|ext| ext.state() == state)
            .collect()
    }

    /// Get extension count
    pub fn count(&self) -> usize {
        self.extensions.len()
    }

    /// Check if an extension exists
    pub fn contains(&self, extension_id: &str) -> bool {
        self.extensions.contains_key(extension_id)
    }

    /// Load extensions from registry file
    pub async fn load_from_registry(&mut self) -> Result<usize> {
        let registry_path = self
            .registry_path
            .as_ref()
            .ok_or_else(|| CCSwarmError::config("No registry path configured"))?;

        if !registry_path.exists() {
            return Ok(0);
        }

        let content = tokio::fs::read_to_string(registry_path)
            .await
            .map_err(|e| CCSwarmError::config(format!("Failed to read registry: {}", e)))?;

        let extensions: Vec<Extension> = serde_json::from_str(&content)
            .map_err(|e| CCSwarmError::config(format!("Failed to parse registry: {}", e)))?;

        let mut loaded_count = 0;
        for extension in extensions {
            if let Err(e) = self.register(extension) {
                tracing::warn!("Failed to load extension from registry: {}", e);
            } else {
                loaded_count += 1;
            }
        }

        Ok(loaded_count)
    }

    /// Save extensions to registry file
    pub async fn save_to_registry(&self) -> Result<()> {
        let registry_path = self
            .registry_path
            .as_ref()
            .ok_or_else(|| CCSwarmError::config("No registry path configured"))?;

        // Create parent directory if it doesn't exist
        if let Some(parent) = registry_path.parent() {
            tokio::fs::create_dir_all(parent).await.map_err(|e| {
                CCSwarmError::config(format!("Failed to create registry directory: {}", e))
            })?;
        }

        let extensions: Vec<Extension> = self.extensions.values().cloned().collect();
        let content = serde_json::to_string_pretty(&extensions)
            .map_err(|e| CCSwarmError::config(format!("Failed to serialize extensions: {}", e)))?;

        tokio::fs::write(registry_path, content)
            .await
            .map_err(|e| CCSwarmError::config(format!("Failed to write registry: {}", e)))?;

        Ok(())
    }

    /// Validate all registered extensions
    pub fn validate_all(&self) -> Result<HashMap<String, Vec<String>>> {
        let mut results = HashMap::new();

        for extension in self.extensions.values() {
            let issues = extension.validate()?;
            if !issues.is_empty() {
                results.insert(extension.id().to_string(), issues);
            }
        }

        Ok(results)
    }

    /// Get extension statistics
    pub fn get_stats(&self) -> ExtensionStats {
        let total = self.extensions.len();
        let active = self.list_by_state(&ExtensionState::Active).len();
        let loaded = self.list_by_state(&ExtensionState::Loaded).len();
        let error_count = self
            .extensions
            .values()
            .filter(|ext| matches!(ext.state(), ExtensionState::Error(_)))
            .count();

        ExtensionStats {
            total_extensions: total,
            active_extensions: active,
            loaded_extensions: loaded,
            error_extensions: error_count,
            pending_proposals: 0, // Managed by ExtensionManager
            successful_extensions: active + loaded,
            failed_extensions: error_count,
        }
    }

    /// Find extensions by capability
    pub fn find_by_capability(
        &self,
        capability: &crate::extension::ExtensionCapability,
    ) -> Vec<&Extension> {
        self.extensions
            .values()
            .filter(|ext| ext.has_capability(capability))
            .collect()
    }

    /// Clear all extensions
    pub fn clear(&mut self) {
        self.extensions.clear();
    }
}

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

/// Extension manager for handling extension proposals and lifecycle
///
/// This manages the more complex aspects of extension management including
/// proposals, voting, and deployment of extensions in the system.
#[derive(Debug, Clone)]
pub struct ExtensionManager {
    pub proposals: HashMap<String, ExtensionProposal>,
    stats: ExtensionStats,
    auto_approve: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExtensionProposal {
    pub id: String,
    pub agent_id: String,
    pub extension_type: ExtensionType,
    pub status: ExtensionStatus,
    pub specification: String,
    pub proposer: String,
    pub title: String,
    pub description: String,
    pub current_state: CurrentState,
    pub proposed_state: ProposedState,
    pub implementation_plan: ImplementationPlan,
    pub risk_assessment: RiskAssessment,
    pub success_criteria: Vec<SuccessCriterion>,
    pub created_at: chrono::DateTime<chrono::Utc>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ExtensionType {
    Capability,
    Performance,
    Knowledge,
    Integration,
    System,
    Cognitive,
    Collaborative,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum ExtensionStatus {
    Proposed,
    UnderReview,
    Approved,
    Rejected,
    Implemented,
}

/// Statistics about the extension system
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExtensionStats {
    pub total_extensions: usize,
    pub active_extensions: usize,
    pub loaded_extensions: usize,
    pub error_extensions: usize,
    pub pending_proposals: usize,
    pub successful_extensions: usize,
    pub failed_extensions: usize,
}

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

impl ExtensionManager {
    /// Create a new extension manager
    pub fn new() -> Self {
        Self {
            proposals: HashMap::new(),
            stats: ExtensionStats {
                total_extensions: 0,
                active_extensions: 0,
                loaded_extensions: 0,
                error_extensions: 0,
                pending_proposals: 0,
                successful_extensions: 0,
                failed_extensions: 0,
            },
            auto_approve: false,
        }
    }

    /// Create extension manager with auto-approval enabled
    pub fn with_auto_approve() -> Self {
        Self {
            auto_approve: true,
            ..Self::new()
        }
    }

    /// Submit a new extension proposal
    pub async fn propose_extension(&mut self, mut proposal: ExtensionProposal) -> Result<String> {
        // Validate proposal
        self.validate_proposal(&proposal)?;

        // Auto-approve if enabled
        if self.auto_approve {
            proposal.status = ExtensionStatus::Approved;
        }

        let id = proposal.id.clone();
        self.proposals.insert(id.clone(), proposal);
        self.update_stats();

        tracing::info!("Extension proposal submitted: {}", id);
        Ok(id)
    }

    /// Get a proposal by ID
    pub fn get_proposal(&self, proposal_id: &str) -> Option<&ExtensionProposal> {
        self.proposals.get(proposal_id)
    }

    /// Get mutable proposal by ID
    pub fn get_proposal_mut(&mut self, proposal_id: &str) -> Option<&mut ExtensionProposal> {
        self.proposals.get_mut(proposal_id)
    }

    /// List all proposals
    pub fn list_proposals(&self) -> Vec<&ExtensionProposal> {
        self.proposals.values().collect()
    }

    /// List proposals by status
    pub fn list_proposals_by_status(&self, status: &ExtensionStatus) -> Vec<&ExtensionProposal> {
        self.proposals
            .values()
            .filter(|p| &p.status == status)
            .collect()
    }

    /// Approve a proposal
    pub async fn approve_proposal(&mut self, proposal_id: &str) -> Result<()> {
        let proposal = self
            .proposals
            .get_mut(proposal_id)
            .ok_or_else(|| CCSwarmError::extension(proposal_id, "Proposal not found"))?;

        if proposal.status != ExtensionStatus::UnderReview {
            return Err(CCSwarmError::extension(
                proposal_id,
                format!("Cannot approve proposal in status: {:?}", proposal.status),
            ));
        }

        proposal.status = ExtensionStatus::Approved;
        self.update_stats();

        tracing::info!("Extension proposal approved: {}", proposal_id);
        Ok(())
    }

    /// Reject a proposal
    pub async fn reject_proposal(&mut self, proposal_id: &str, reason: String) -> Result<()> {
        let proposal = self
            .proposals
            .get_mut(proposal_id)
            .ok_or_else(|| CCSwarmError::extension(proposal_id, "Proposal not found"))?;

        proposal.status = ExtensionStatus::Rejected;
        self.update_stats();

        tracing::info!("Extension proposal rejected: {} - {}", proposal_id, reason);
        Ok(())
    }

    /// Mark proposal as implemented
    pub async fn mark_implemented(&mut self, proposal_id: &str) -> Result<()> {
        let proposal = self
            .proposals
            .get_mut(proposal_id)
            .ok_or_else(|| CCSwarmError::extension(proposal_id, "Proposal not found"))?;

        if proposal.status != ExtensionStatus::Approved {
            return Err(CCSwarmError::extension(
                proposal_id,
                format!("Cannot implement proposal in status: {:?}", proposal.status),
            ));
        }

        proposal.status = ExtensionStatus::Implemented;
        self.update_stats();

        tracing::info!("Extension proposal implemented: {}", proposal_id);
        Ok(())
    }

    /// Get current statistics
    pub async fn get_stats(&self) -> ExtensionStats {
        self.stats.clone()
    }

    /// Update internal statistics
    fn update_stats(&mut self) {
        let total = self.proposals.len();
        let pending = self
            .list_proposals_by_status(&ExtensionStatus::Proposed)
            .len()
            + self
                .list_proposals_by_status(&ExtensionStatus::UnderReview)
                .len();
        let successful = self
            .list_proposals_by_status(&ExtensionStatus::Implemented)
            .len();
        let failed = self
            .list_proposals_by_status(&ExtensionStatus::Rejected)
            .len();

        self.stats.total_extensions = total;
        self.stats.pending_proposals = pending;
        self.stats.successful_extensions = successful;
        self.stats.failed_extensions = failed;
    }

    /// Validate a proposal before submission
    fn validate_proposal(&self, proposal: &ExtensionProposal) -> Result<()> {
        if proposal.title.trim().is_empty() {
            return Err(CCSwarmError::extension(
                &proposal.id,
                "Proposal title cannot be empty",
            ));
        }

        if proposal.description.trim().is_empty() {
            return Err(CCSwarmError::extension(
                &proposal.id,
                "Proposal description cannot be empty",
            ));
        }

        if proposal.proposer.trim().is_empty() {
            return Err(CCSwarmError::extension(
                &proposal.id,
                "Proposer cannot be empty",
            ));
        }

        // Validate risk assessment
        if proposal.risk_assessment.overall_risk_score < 0.0
            || proposal.risk_assessment.overall_risk_score > 1.0
        {
            return Err(CCSwarmError::extension(
                &proposal.id,
                "Risk score must be between 0.0 and 1.0",
            ));
        }

        Ok(())
    }

    /// Clean up old rejected proposals
    pub async fn cleanup_old_proposals(&mut self, max_age: chrono::Duration) -> Result<usize> {
        let cutoff = chrono::Utc::now() - max_age;
        let initial_count = self.proposals.len();

        self.proposals.retain(|_, proposal| {
            proposal.status != ExtensionStatus::Rejected || proposal.created_at > cutoff
        });

        let removed = initial_count - self.proposals.len();
        if removed > 0 {
            self.update_stats();
            tracing::info!("Cleaned up {} old proposals", removed);
        }

        Ok(removed)
    }
}

/// Current state
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CurrentState {
    pub capabilities: Vec<String>,
    pub limitations: Vec<String>,
    pub performance_metrics: HashMap<String, f64>,
}

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

impl CurrentState {
    pub fn new() -> Self {
        Self {
            capabilities: Vec::new(),
            limitations: Vec::new(),
            performance_metrics: HashMap::new(),
        }
    }
}

/// Proposed state
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProposedState {
    pub new_capabilities: Vec<String>,
    pub removed_limitations: Vec<String>,
    pub expected_improvements: HashMap<String, f64>,
    pub performance_targets: HashMap<String, f64>,
}

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

impl ProposedState {
    pub fn new() -> Self {
        Self {
            new_capabilities: Vec::new(),
            removed_limitations: Vec::new(),
            expected_improvements: HashMap::new(),
            performance_targets: HashMap::new(),
        }
    }
}

/// Implementation plan
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ImplementationPlan {
    pub phases: Vec<ImplementationPhase>,
    pub total_duration: std::time::Duration,
    pub resources_required: Vec<String>,
    pub timeline: String,
    pub dependencies: Vec<String>,
}

/// Implementation phase with reduced redundancy
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ImplementationPhase {
    pub name: String,
    pub description: String,
    pub estimated_duration: std::time::Duration,
    pub dependencies: Vec<String>,
    pub tasks: Vec<String>,
    pub validation_method: String,
    pub complexity: ComplexityLevel,
}

/// Complexity level enumeration
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum ComplexityLevel {
    Low,
    Medium,
    High,
    Critical,
}

impl std::fmt::Display for ComplexityLevel {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Low => write!(f, "Low"),
            Self::Medium => write!(f, "Medium"),
            Self::High => write!(f, "High"),
            Self::Critical => write!(f, "Critical"),
        }
    }
}

/// Risk assessment with improved structure
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RiskAssessment {
    pub risk_level: RiskLevel,
    pub potential_issues: Vec<String>,
    pub mitigation_strategies: Vec<String>,
    pub rollback_plan: String,
    pub overall_risk_score: f32,
    pub categories: Vec<RiskCategory>,
}

/// Risk categories
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum RiskCategory {
    Security,
    Performance,
    Compatibility,
    Stability,
    Maintenance,
    Compliance,
    UserExperience,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum RiskLevel {
    Low,
    Medium,
    High,
    Critical,
}

/// Success criterion with reduced redundancy
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SuccessCriterion {
    pub name: String,
    pub description: String,
    pub target_value: f64,
    pub measurement_method: String,
    pub measurable: bool,
}