ccswarm 0.4.5

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
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
//! CLI commands for semantic features
//! 
//! Provides command-line interface for semantic analysis and optimization

use clap::{Parser, Subcommand};
use std::path::PathBuf;
use crate::semantic::{
    SemanticManager, SemanticConfig,
    dynamic_agent_generation::{DynamicAgentGenerator, ProjectComplexityLevel},
    refactoring_system::AutomaticRefactoringSystem,
    sangha_voting::{SanghaSemanticVoting, ConsensusAlgorithm, ProposalType, VoteDecision},
    cross_codebase_optimization::{CrossCodebaseOptimizer, ProgrammingLanguage},
};
use crate::semantic::subagent_integration::AgentRole;
use anyhow::Result;
use colored::*;
use indicatif::{ProgressBar, ProgressStyle, MultiProgress};
use tokio::time::{sleep, Duration};
use std::sync::Arc;

// ============================================================================
// Helper functions for consistent CLI output
// ============================================================================

/// Create a spinner progress bar with standard styling
fn create_spinner(message: &str) -> Result<ProgressBar> {
    let pb = ProgressBar::new_spinner();
    pb.set_style(
        ProgressStyle::default_spinner()
            .template("{spinner:.blue} {msg}")
            .map_err(|e| anyhow::anyhow!("Failed to set progress bar style: {}", e))?
    );
    pb.set_message(message.to_string());
    Ok(pb)
}

/// Create a spinner progress bar for MultiProgress with custom template
fn create_multi_spinner(multi_pb: &MultiProgress, template: &str) -> Result<ProgressBar> {
    let pb = multi_pb.add(ProgressBar::new_spinner());
    pb.set_style(
        ProgressStyle::default_spinner()
            .template(template)
            .map_err(|e| anyhow::anyhow!("Failed to set progress bar style: {}", e))?
    );
    Ok(pb)
}

/// Print a styled header with emoji
fn print_header(emoji: &str, message: &str) {
    println!("{}", format!("{} {}", emoji, message).bright_blue().bold());
}

/// Semantic feature commands
#[derive(Parser, Debug)]
#[command(name = "semantic")]
#[command(about = "Semantic code analysis and optimization")]
pub struct SemanticCommands {
    #[command(subcommand)]
    pub command: SemanticSubcommand,
}

/// Semantic subcommands
#[derive(Subcommand, Debug)]
pub enum SemanticSubcommand {
    /// Analyze codebase for insights
    Analyze {
        /// Path to analyze
        #[arg(short, long, default_value = ".")]
        path: PathBuf,
        
        /// Output format (json, table, markdown)
        #[arg(short, long, default_value = "table")]
        format: String,
        
        /// Include symbol details
        #[arg(short, long)]
        symbols: bool,
    },
    
    /// Scan for refactoring opportunities
    Refactor {
        /// Automatically apply safe refactorings
        #[arg(short, long)]
        auto_apply: bool,
        
        /// Priority threshold (low, medium, high, critical)
        #[arg(short, long, default_value = "medium")]
        priority: String,
        
        /// Maximum proposals to show
        #[arg(short, long, default_value = "10")]
        max: usize,
    },
    
    /// Generate agents based on project needs
    GenerateAgents {
        /// Force regeneration even if agents exist
        #[arg(short, long)]
        force: bool,
        
        /// Deploy generated agents immediately
        #[arg(short, long)]
        deploy: bool,
    },
    
    /// Cross-codebase optimization analysis
    Optimize {
        /// Repositories to analyze (format: name:path:language)
        #[arg(short, long, required = true)]
        repos: Vec<String>,
        
        /// Generate detailed report
        #[arg(short, long)]
        detailed: bool,
        
        /// Output file for report
        #[arg(short, long)]
        output: Option<PathBuf>,
    },
    
    /// Sangha voting on proposals
    Vote {
        /// Create new proposal
        #[arg(long, conflicts_with = "list")]
        create: Option<String>,
        
        /// List active proposals
        #[arg(long, conflicts_with = "create")]
        list: bool,
        
        /// Submit vote (format: proposal_id:decision:reason)
        #[arg(long)]
        submit: Option<String>,
        
        /// Show voting history
        #[arg(long)]
        history: bool,
    },
    
    /// Interactive semantic dashboard
    Dashboard {
        /// Port for web UI
        #[arg(short, long, default_value = "3000")]
        port: u16,
        
        /// Enable real-time updates
        #[arg(short, long)]
        realtime: bool,
    },
    
    /// Monitor semantic operations in real-time
    Monitor {
        /// Refresh interval in seconds
        #[arg(short, long, default_value = "5")]
        interval: u64,
        
        /// Show only specific metrics
        #[arg(short, long)]
        filter: Option<String>,
    },
}

/// Execute semantic commands
pub async fn execute(cmd: SemanticCommands) -> Result<()> {
    let config = SemanticConfig::default();
    let manager = Arc::new(SemanticManager::new(config).await?);
    manager.initialize().await?;
    
    match cmd.command {
        SemanticSubcommand::Analyze { path, format, symbols } => {
            analyze_codebase(manager, path, format, symbols).await?;
        }
        SemanticSubcommand::Refactor { auto_apply, priority, max } => {
            refactor_codebase(manager, auto_apply, priority, max).await?;
        }
        SemanticSubcommand::GenerateAgents { force, deploy } => {
            generate_agents(manager, force, deploy).await?;
        }
        SemanticSubcommand::Optimize { repos, detailed, output } => {
            optimize_cross_codebase(manager, repos, detailed, output).await?;
        }
        SemanticSubcommand::Vote { create, list, submit, history } => {
            handle_voting(manager, create, list, submit, history).await?;
        }
        SemanticSubcommand::Dashboard { port, realtime } => {
            launch_dashboard(port, realtime).await?;
        }
        SemanticSubcommand::Monitor { interval, filter } => {
            monitor_operations(manager, interval, filter).await?;
        }
    }
    
    Ok(())
}

/// Analyze codebase
async fn analyze_codebase(
    manager: Arc<SemanticManager>,
    _path: PathBuf,
    format: String,
    show_symbols: bool,
) -> Result<()> {
    print_header("🔍", "Analyzing codebase...");

    let pb = create_spinner("Indexing symbols...")?;

    // Index the codebase
    manager.symbol_index().index_codebase().await?;

    pb.set_message("Analyzing code structure...".to_string());
    let all_symbols = manager.symbol_index().get_all_symbols().await?;

    pb.finish_with_message("✓ Analysis complete");
    
    // Display results based on format
    match format.as_str() {
        "json" => {
            let json = serde_json::to_string_pretty(&all_symbols)?;
            println!("{}", json);
        }
        "markdown" => {
            print_markdown_analysis(&all_symbols, show_symbols);
        }
        _ => {
            print_table_analysis(&all_symbols, show_symbols);
        }
    }
    
    // Show summary statistics
    println!("\n{}", "📊 Summary Statistics".bright_green().bold());
    println!("  Total symbols: {}", all_symbols.len());
    
    let functions = all_symbols.iter()
        .filter(|s| matches!(s.kind, crate::semantic::analyzer::SymbolKind::Function))
        .count();
    let structs = all_symbols.iter()
        .filter(|s| matches!(s.kind, crate::semantic::analyzer::SymbolKind::Struct))
        .count();
    
    println!("  Functions: {}", functions);
    println!("  Structs: {}", structs);
    
    Ok(())
}

/// Refactor codebase
async fn refactor_codebase(
    manager: Arc<SemanticManager>,
    auto_apply: bool,
    priority: String,
    max: usize,
) -> Result<()> {
    print_header("🔧", "Scanning for refactoring opportunities...");

    let mut refactoring_system = AutomaticRefactoringSystem::new(
        manager.analyzer(),
        manager.symbol_index(),
        manager.memory(),
    );

    let pb = create_spinner("Analyzing code quality...")?;

    let proposals = refactoring_system.scan_codebase().await?;
    pb.finish_with_message("✓ Scan complete");
    
    // Filter by priority
    let priority_filter = parse_priority(&priority);
    let filtered: Vec<_> = proposals.iter()
        .filter(|p| p.priority >= priority_filter)
        .take(max)
        .collect();
    
    if filtered.is_empty() {
        println!("{}", "No refactoring opportunities found at this priority level.".yellow());
        return Ok(());
    }
    
    println!("\n{}", format!("Found {} refactoring opportunities:", filtered.len()).bright_green().bold());
    
    for (i, proposal) in filtered.iter().enumerate() {
        println!("\n{}. {} [{}]", 
            i + 1, 
            proposal.title.bright_white().bold(),
            format!("{:?}", proposal.priority).color(priority_color(&proposal.priority))
        );
        println!("   {}", proposal.description.dimmed());
        println!("   Effort: {:?} | Automated: {}", 
            proposal.estimated_effort,
            if proposal.automated { "".green() } else { "".red() }
        );
        
        if !proposal.benefits.is_empty() {
            println!("   Benefits:");
            for benefit in &proposal.benefits {
                println!("{}", benefit.green());
            }
        }
        
        if auto_apply && proposal.automated {
            print!("   Applying automatically... ");
            refactoring_system.apply_proposal(&proposal.id).await?;
            println!("{}", "".bright_green().bold());
        }
    }
    
    if auto_apply {
        let stats = refactoring_system.get_stats();
        println!("\n{}", "📈 Refactoring Statistics".bright_green().bold());
        println!("  Applied: {} proposals", stats.applied_proposals);
        println!("  Time saved: {:.1} hours", stats.time_saved_hours);
        println!("  Lines refactored: {}", stats.lines_refactored);
    }
    
    Ok(())
}

/// Generate agents based on project needs
async fn generate_agents(
    manager: Arc<SemanticManager>,
    force: bool,
    deploy: bool,
) -> Result<()> {
    print_header("🤖", "Analyzing project for agent generation...");

    let generator = DynamicAgentGenerator::new(
        manager.analyzer(),
        manager.symbol_index(),
        manager.memory(),
    );

    let pb = create_spinner("Analyzing project characteristics...")?;

    let needs = generator.analyze_agent_needs().await?;
    pb.finish_with_message("✓ Analysis complete");
    
    if needs.is_empty() && !force {
        println!("{}", "All necessary agents already exist.".green());
        return Ok(());
    }
    
    println!("\n{}", format!("Need to generate {} agents:", needs.len()).bright_green().bold());
    
    for request in needs {
        println!("\n  Generating agent for: {}", 
            request.task_context.task.title.bright_white().bold());
        
        let generated = generator.generate_agent(&request).await?;
        
        println!("  ✓ Generated: {}", generated.template.name.green());
        println!("    Role: {:?}", generated.template.role);
        println!("    Capabilities: {}", generated.template.capabilities.len());
        
        if deploy {
            print!("  Deploying agent... ");
            generator.deploy_agent(&generated).await?;
            println!("{}", "".bright_green().bold());
        }
    }
    
    Ok(())
}

/// Optimize across multiple codebases
async fn optimize_cross_codebase(
    manager: Arc<SemanticManager>,
    repos: Vec<String>,
    detailed: bool,
    output: Option<PathBuf>,
) -> Result<()> {
    print_header("🚀", "Cross-codebase optimization analysis...");

    let mut optimizer = CrossCodebaseOptimizer::new(manager.memory());

    let multi_pb = MultiProgress::new();

    // Parse and add repositories
    for repo_spec in repos {
        let parts: Vec<&str> = repo_spec.split(':').collect();
        if parts.len() != 3 {
            eprintln!("Invalid repo format: {} (expected name:path:language)", repo_spec);
            continue;
        }

        let name = parts[0].to_string();
        let path = PathBuf::from(parts[1]);
        let language = parse_language(parts[2]);

        let pb = create_multi_spinner(&multi_pb, "{spinner:.blue} Adding repository: {msg}")?;
        pb.set_message(name.clone());

        optimizer.add_repository(name, path, language).await?;
        pb.finish_with_message("");
    }

    // Perform analysis
    let pb = create_multi_spinner(&multi_pb, "{spinner:.blue} {msg}")?;
    pb.set_message("Performing comprehensive analysis...".to_string());

    let analysis = optimizer.analyze_all().await?;
    pb.finish_with_message("✓ Analysis complete");
    
    // Generate report
    let report = optimizer.generate_report().await?;
    
    // Save or display report
    if let Some(output_path) = output {
        tokio::fs::write(&output_path, &report).await?;
        println!("{}", format!("Report saved to: {}", output_path.display()).green());
    } else {
        println!("\n{}", report);
    }
    
    if detailed {
        // Show detailed findings
        println!("\n{}", "🔍 Detailed Findings".bright_green().bold());
        
        if !analysis.security_findings.is_empty() {
            println!("\n  Security Issues:");
            for finding in &analysis.security_findings {
                println!("    • [{:?}] {}", finding.severity, finding.description);
            }
        }
        
        if !analysis.performance_bottlenecks.is_empty() {
            println!("\n  Performance Bottlenecks:");
            for bottleneck in &analysis.performance_bottlenecks {
                println!("{:?} in {}", 
                    bottleneck.bottleneck_type,
                    bottleneck.location.function_name
                );
            }
        }
        
        println!("\n  Technical Debt:");
        println!("    Total: {:.0} hours", analysis.technical_debt_map.total_debt_hours);
        println!("    Trend: {:?}", analysis.technical_debt_map.debt_trends.trend_direction);
    }
    
    Ok(())
}

/// Handle voting operations
async fn handle_voting(
    manager: Arc<SemanticManager>,
    create: Option<String>,
    list: bool,
    submit: Option<String>,
    history: bool,
) -> Result<()> {
    let sangha = SanghaSemanticVoting::new(
        manager.analyzer(),
        manager.memory(),
        ConsensusAlgorithm::SimpleMajority,
    );
    
    if let Some(proposal_title) = create {
        println!("{}", "📝 Creating new proposal...".bright_blue().bold());
        
        // For demo, create a simple proposal
        let proposal = sangha.create_proposal(
            proposal_title.clone(),
            "Proposal created via CLI".to_string(),
            ProposalType::ArchitectureChange,
            vec![],
        ).await?;
        
        println!("{}", format!("✓ Proposal created: {}", proposal.id).green());
        println!("  Title: {}", proposal.title);
        println!("  Deadline: {}", proposal.voting_deadline);
        println!("  Quorum: {}", proposal.quorum_required);
    }
    
    if list {
        println!("{}", "📋 Active Proposals".bright_blue().bold());
        let proposals = sangha.get_active_proposals().await?;
        
        if proposals.is_empty() {
            println!("  No active proposals");
        } else {
            for proposal in proposals {
                println!("\n  ID: {}", proposal.id.bright_white().bold());
                println!("  Title: {}", proposal.title);
                println!("  Status: {:?}", proposal.status);
                println!("  Votes: {}", proposal.votes.len());
            }
        }
    }
    
    if let Some(vote_spec) = submit {
        let parts: Vec<&str> = vote_spec.split(':').collect();
        if parts.len() != 3 {
            eprintln!("Invalid vote format (expected proposal_id:decision:reason)");
            return Ok(());
        }
        
        let decision = match parts[1] {
            "approve" => VoteDecision::Approve,
            "reject" => VoteDecision::Reject,
            "abstain" => VoteDecision::Abstain,
            _ => VoteDecision::RequestChanges,
        };
        
        sangha.submit_vote(
            parts[0],
            "cli-user".to_string(),
            AgentRole::Custom("Human".to_string()),
            decision,
            parts[2].to_string(),
        ).await?;
        
        println!("{}", "✓ Vote submitted successfully".green());
    }
    
    if history {
        println!("{}", "📜 Voting History".bright_blue().bold());
        let history = sangha.get_voting_history().await?;
        
        for result in history {
            println!("\n  Proposal: {}", result.proposal_id);
            println!("  Result: {:?} ({}% approval)", 
                result.final_decision,
                result.approval_percentage
            );
            println!("  Consensus: {}", 
                if result.consensus_achieved { "".green() } else { "".red() }
            );
        }
    }
    
    Ok(())
}

/// Launch interactive dashboard
async fn launch_dashboard(port: u16, realtime: bool) -> Result<()> {
    println!("{}", format!("🌐 Launching semantic dashboard on port {}...", port).bright_blue().bold());
    
    // This would launch a web server with the dashboard
    // For now, we'll simulate it
    println!("  Dashboard URL: http://localhost:{}", port);
    
    if realtime {
        println!("  Real-time updates: {}", "enabled".green());
    }
    
    println!("\n  Press Ctrl+C to stop the dashboard");
    
    // Keep running until interrupted
    loop {
        sleep(Duration::from_secs(1)).await;
    }
}

/// Monitor semantic operations
async fn monitor_operations(
    manager: Arc<SemanticManager>,
    interval: u64,
    filter: Option<String>,
) -> Result<()> {
    println!("{}", "📊 Monitoring semantic operations...".bright_blue().bold());
    println!("  Refresh interval: {} seconds", interval);
    
    if let Some(f) = &filter {
        println!("  Filter: {}", f);
    }
    
    loop {
        // Clear screen (in real implementation)
        print!("\x1B[2J\x1B[1;1H");
        
        println!("{}", "═══════════════════════════════════════════".bright_blue());
        println!("{}", " SEMANTIC OPERATIONS MONITOR ".bright_white().bold());
        println!("{}", "═══════════════════════════════════════════".bright_blue());
        
        // Get current metrics
        let symbols = manager.symbol_index().get_all_symbols().await?;
        let memories = manager.memory().list_memories().await?;
        
        println!("\n📊 Metrics:");
        println!("  Indexed Symbols: {}", symbols.len());
        println!("  Project Memories: {}", memories.len());
        
        // Show recent operations (simulated)
        println!("\n🔄 Recent Operations:");
        println!("  [{}] Symbol analysis completed", chrono::Local::now().format("%H:%M:%S"));
        println!("  [{}] Memory stored: architecture_decision", chrono::Local::now().format("%H:%M:%S"));
        
        println!("\n  Press Ctrl+C to exit");
        
        sleep(Duration::from_secs(interval)).await;
    }
}

// Helper functions

fn print_table_analysis(symbols: &[crate::semantic::analyzer::Symbol], show_symbols: bool) {
    use prettytable::{Table, row, cell};
    
    let mut table = Table::new();
    table.add_row(row!["Type", "Name", "File", "Line"]);
    
    for symbol in symbols.iter().take(20) {
        table.add_row(row![
            format!("{:?}", symbol.kind),
            symbol.name,
            symbol.file_path,
            symbol.line
        ]);
    }
    
    table.printstd();
    
    if symbols.len() > 20 {
        println!("... and {} more", symbols.len() - 20);
    }
}

fn print_markdown_analysis(symbols: &[crate::semantic::analyzer::Symbol], show_symbols: bool) {
    println!("# Codebase Analysis\n");
    
    println!("## Symbol Summary\n");
    println!("| Type | Count |");
    println!("|------|-------|");
    
    use std::collections::HashMap;
    let mut type_counts = HashMap::new();
    
    for symbol in symbols {
        *type_counts.entry(format!("{:?}", symbol.kind)).or_insert(0) += 1;
    }
    
    for (kind, count) in type_counts {
        println!("| {} | {} |", kind, count);
    }
    
    if show_symbols {
        println!("\n## Symbols\n");
        for symbol in symbols.iter().take(50) {
            println!("- **{}** (`{:?}`) - {}:{}", 
                symbol.name, 
                symbol.kind,
                symbol.file_path,
                symbol.line
            );
        }
    }
}

fn parse_priority(priority: &str) -> crate::semantic::refactoring_system::RefactoringPriority {
    use crate::semantic::refactoring_system::RefactoringPriority;
    
    match priority.to_lowercase().as_str() {
        "low" => RefactoringPriority::Low,
        "medium" => RefactoringPriority::Medium,
        "high" => RefactoringPriority::High,
        "critical" => RefactoringPriority::Critical,
        _ => RefactoringPriority::Medium,
    }
}

fn priority_color(priority: &crate::semantic::refactoring_system::RefactoringPriority) -> colored::Color {
    use crate::semantic::refactoring_system::RefactoringPriority;
    
    match priority {
        RefactoringPriority::Low => colored::Color::Blue,
        RefactoringPriority::Medium => colored::Color::Yellow,
        RefactoringPriority::High => colored::Color::BrightRed,
        RefactoringPriority::Critical => colored::Color::Red,
    }
}

fn parse_language(lang: &str) -> ProgrammingLanguage {
    match lang.to_lowercase().as_str() {
        "rust" => ProgrammingLanguage::Rust,
        "typescript" | "ts" => ProgrammingLanguage::TypeScript,
        "javascript" | "js" => ProgrammingLanguage::JavaScript,
        "python" | "py" => ProgrammingLanguage::Python,
        "go" => ProgrammingLanguage::Go,
        "java" => ProgrammingLanguage::Java,
        _ => ProgrammingLanguage::Other(lang.to_string()),
    }
}