ccswarm 0.4.2

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
/// Master Agent Task Delegation System
///
/// This module implements intelligent task delegation where a Master agent
/// analyzes tasks and assigns them to the most appropriate specialized agents.
use anyhow::Result;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use tracing::info;

use crate::agent::{Priority, Task, TaskType};
use crate::identity::AgentRole;

/// Task delegation decision made by the Master agent
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DelegationDecision {
    /// The task being delegated
    pub task: Task,
    /// Target agent role for execution
    pub target_agent: AgentRole,
    /// Confidence level (0.0 to 1.0)
    pub confidence: f64,
    /// Reasoning for the delegation
    pub reasoning: String,
    /// Priority adjustment (if any)
    pub priority_adjustment: Option<Priority>,
    /// Estimated completion time in seconds
    pub estimated_duration: Option<u32>,
    /// Dependencies on other tasks
    pub dependencies: Vec<String>,
}

/// Master delegation strategy
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum DelegationStrategy {
    /// Delegate based on task content analysis
    ContentBased,
    /// Delegate based on agent workload balancing
    LoadBalanced,
    /// Delegate based on agent expertise and past performance
    ExpertiseBased,
    /// Delegate based on task dependencies and workflow
    WorkflowBased,
    /// Hybrid approach combining multiple strategies
    Hybrid,
}

/// Agent workload and performance metrics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AgentMetrics {
    pub agent_role: AgentRole,
    pub current_tasks: usize,
    pub completed_tasks: usize,
    pub average_completion_time: std::time::Duration,
    pub success_rate: f64,
    pub specialization_score: f64,
    pub availability: f64,
}

/// Master delegation engine
#[derive(Debug)]
pub struct MasterDelegationEngine {
    pub strategy: DelegationStrategy,
    pub agent_metrics: HashMap<String, AgentMetrics>,
    pub task_history: Vec<DelegationDecision>,
    pub delegation_rules: Vec<DelegationRule>,
}

/// Rules for task delegation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DelegationRule {
    pub name: String,
    pub priority: u8,
    pub condition: DelegationCondition,
    pub target_agent: AgentRole,
    pub confidence_boost: f64,
}

/// Conditions for delegation rules
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum DelegationCondition {
    /// Task description contains keywords
    DescriptionContains(Vec<String>),
    /// Task type matches
    TaskTypeEquals(TaskType),
    /// Task priority is above threshold
    PriorityAbove(Priority),
    /// Agent has low workload
    AgentWorkloadBelow(f64),
    /// Combined conditions
    And(Vec<DelegationCondition>),
    Or(Vec<DelegationCondition>),
}

impl MasterDelegationEngine {
    /// Create a new master delegation engine
    pub fn new(strategy: DelegationStrategy) -> Self {
        Self {
            strategy,
            agent_metrics: HashMap::new(),
            task_history: Vec::new(),
            delegation_rules: Self::default_delegation_rules(),
        }
    }

    /// Create default delegation rules
    fn default_delegation_rules() -> Vec<DelegationRule> {
        vec![
            DelegationRule {
                name: "Frontend UI Tasks".to_string(),
                priority: 10,
                condition: DelegationCondition::Or(vec![
                    DelegationCondition::DescriptionContains(vec![
                        "html".to_string(),
                        "css".to_string(),
                        "javascript".to_string(),
                        "ui".to_string(),
                        "component".to_string(),
                        "frontend".to_string(),
                        "react".to_string(),
                        "vue".to_string(),
                        "angular".to_string(),
                    ]),
                    DelegationCondition::TaskTypeEquals(TaskType::Feature),
                ]),
                target_agent: AgentRole::Frontend {
                    technologies: vec![
                        "HTML".to_string(),
                        "CSS".to_string(),
                        "JavaScript".to_string(),
                    ],
                    responsibilities: vec!["UI Development".to_string()],
                    boundaries: vec!["No backend work".to_string()],
                },
                confidence_boost: 0.8,
            },
            DelegationRule {
                name: "Backend API Tasks".to_string(),
                priority: 11, // Higher priority than Frontend to ensure API tasks go to Backend
                condition: DelegationCondition::DescriptionContains(vec![
                    "api".to_string(),
                    "server".to_string(),
                    "database".to_string(),
                    "backend".to_string(),
                    "endpoint".to_string(),
                    "rest".to_string(),
                    "crud".to_string(),
                    "express".to_string(),
                ]),
                target_agent: AgentRole::Backend {
                    technologies: vec!["Node.js".to_string(), "Express".to_string()],
                    responsibilities: vec!["API Development".to_string()],
                    boundaries: vec!["No frontend work".to_string()],
                },
                confidence_boost: 0.8,
            },
            DelegationRule {
                name: "Testing Tasks".to_string(),
                priority: 9,
                condition: DelegationCondition::Or(vec![
                    DelegationCondition::DescriptionContains(vec![
                        "test".to_string(),
                        "testing".to_string(),
                        "qa".to_string(),
                        "quality".to_string(),
                        "validation".to_string(),
                    ]),
                    DelegationCondition::TaskTypeEquals(TaskType::Testing),
                ]),
                target_agent: AgentRole::QA {
                    technologies: vec![
                        "Jest".to_string(),
                        "Mocha".to_string(),
                        "Puppeteer".to_string(),
                    ],
                    responsibilities: vec!["Testing".to_string(), "Quality Assurance".to_string()],
                    boundaries: vec!["No production code".to_string()],
                },
                confidence_boost: 0.9,
            },
            DelegationRule {
                name: "Infrastructure Tasks".to_string(),
                priority: 9,
                condition: DelegationCondition::Or(vec![
                    DelegationCondition::DescriptionContains(vec![
                        "deploy".to_string(),
                        "ci/cd".to_string(),
                        "docker".to_string(),
                        "infrastructure".to_string(),
                        "pipeline".to_string(),
                    ]),
                    DelegationCondition::TaskTypeEquals(TaskType::Infrastructure),
                ]),
                target_agent: AgentRole::DevOps {
                    technologies: vec!["Docker".to_string(), "GitHub Actions".to_string()],
                    responsibilities: vec!["Deployment".to_string(), "Infrastructure".to_string()],
                    boundaries: vec!["No application code".to_string()],
                },
                confidence_boost: 0.9,
            },
        ]
    }

    /// Update agent metrics
    pub fn update_agent_metrics(&mut self, agent_role: AgentRole, metrics: AgentMetrics) {
        self.agent_metrics
            .insert(agent_role.name().to_string(), metrics);
    }

    /// Delegate a task to the most appropriate agent
    pub fn delegate_task(&mut self, task: Task) -> Result<DelegationDecision> {
        info!(
            "🎯 Master analyzing task for delegation: {}",
            task.description
        );

        let decision = match self.strategy {
            DelegationStrategy::ContentBased => self.delegate_content_based(&task)?,
            DelegationStrategy::LoadBalanced => self.delegate_load_balanced(&task)?,
            DelegationStrategy::ExpertiseBased => self.delegate_expertise_based(&task)?,
            DelegationStrategy::WorkflowBased => self.delegate_workflow_based(&task)?,
            DelegationStrategy::Hybrid => self.delegate_hybrid(&task)?,
        };

        // Record the delegation decision
        self.task_history.push(decision.clone());

        info!(
            "✅ Master delegated task '{}' to {} with {:.1}% confidence",
            task.description,
            decision.target_agent.name(),
            decision.confidence * 100.0
        );

        Ok(decision)
    }

    /// Content-based delegation using rules
    fn delegate_content_based(&self, task: &Task) -> Result<DelegationDecision> {
        let mut best_match: Option<(DelegationRule, f64)> = None;
        let task_lower = task.description.to_lowercase();

        for rule in &self.delegation_rules {
            if let Some(confidence) = Self::evaluate_condition(&rule.condition, task, &task_lower) {
                let total_confidence = confidence + rule.confidence_boost;

                if best_match
                    .as_ref()
                    .is_none_or(|(_, prev_confidence)| total_confidence > *prev_confidence)
                {
                    best_match = Some((rule.clone(), total_confidence.min(1.0)));
                }
            }
        }

        if let Some((rule, confidence)) = best_match {
            Ok(DelegationDecision {
                task: task.clone(),
                target_agent: rule.target_agent,
                confidence,
                reasoning: format!(
                    "Matched rule: {} with {:.1}% confidence",
                    rule.name,
                    confidence * 100.0
                ),
                priority_adjustment: None,
                estimated_duration: task.estimated_duration,
                dependencies: vec![],
            })
        } else {
            // Default to backend agent as most versatile
            Ok(DelegationDecision {
                task: task.clone(),
                target_agent: AgentRole::Backend {
                    technologies: vec!["Node.js".to_string()],
                    responsibilities: vec!["General Development".to_string()],
                    boundaries: vec![],
                },
                confidence: 0.3,
                reasoning: "No specific rule matched, defaulting to backend agent".to_string(),
                priority_adjustment: None,
                estimated_duration: task.estimated_duration,
                dependencies: vec![],
            })
        }
    }

    /// Load-balanced delegation
    fn delegate_load_balanced(&self, task: &Task) -> Result<DelegationDecision> {
        // Find agent with lowest current workload
        let mut best_agent: Option<(AgentRole, f64)> = None;

        for metrics in self.agent_metrics.values() {
            let workload = metrics.current_tasks as f64 / 10.0; // Normalize to 0-1
            let availability_score = metrics.availability * (1.0 - workload);

            if best_agent
                .as_ref()
                .is_none_or(|(_, prev_score)| availability_score > *prev_score)
            {
                best_agent = Some((metrics.agent_role.clone(), availability_score));
            }
        }

        if let Some((agent, score)) = best_agent {
            Ok(DelegationDecision {
                task: task.clone(),
                target_agent: agent,
                confidence: score,
                reasoning: format!(
                    "Load-balanced assignment with availability score {:.1}%",
                    score * 100.0
                ),
                priority_adjustment: None,
                estimated_duration: task.estimated_duration,
                dependencies: vec![],
            })
        } else {
            // Fallback to content-based
            self.delegate_content_based(task)
        }
    }

    /// Expertise-based delegation
    fn delegate_expertise_based(&self, task: &Task) -> Result<DelegationDecision> {
        let mut best_match: Option<(AgentRole, f64)> = None;

        for metrics in self.agent_metrics.values() {
            let expertise_score = metrics.specialization_score * metrics.success_rate;

            if best_match
                .as_ref()
                .is_none_or(|(_, prev_score)| expertise_score > *prev_score)
            {
                best_match = Some((metrics.agent_role.clone(), expertise_score));
            }
        }

        if let Some((agent, score)) = best_match {
            Ok(DelegationDecision {
                task: task.clone(),
                target_agent: agent,
                confidence: score,
                reasoning: format!(
                    "Expertise-based assignment with score {:.1}%",
                    score * 100.0
                ),
                priority_adjustment: None,
                estimated_duration: task.estimated_duration,
                dependencies: vec![],
            })
        } else {
            // Fallback to content-based
            self.delegate_content_based(task)
        }
    }

    /// Workflow-based delegation (considering dependencies)
    fn delegate_workflow_based(&self, task: &Task) -> Result<DelegationDecision> {
        // Analyze task dependencies and workflow
        // For now, fall back to content-based with workflow awareness
        let mut decision = self.delegate_content_based(task)?;

        // Add workflow analysis
        decision.reasoning = format!("{} (workflow-aware)", decision.reasoning);

        Ok(decision)
    }

    /// Hybrid delegation strategy
    fn delegate_hybrid(&self, task: &Task) -> Result<DelegationDecision> {
        // Combine multiple strategies
        let content_decision = self.delegate_content_based(task)?;
        let load_decision = self.delegate_load_balanced(task)?;

        // Choose the decision with higher confidence
        if content_decision.confidence >= load_decision.confidence {
            Ok(DelegationDecision {
                reasoning: format!("Hybrid: Content-based ({})", content_decision.reasoning),
                ..content_decision
            })
        } else {
            Ok(DelegationDecision {
                reasoning: format!("Hybrid: Load-based ({})", load_decision.reasoning),
                ..load_decision
            })
        }
    }

    /// Evaluate a delegation condition
    fn evaluate_condition(
        condition: &DelegationCondition,
        task: &Task,
        task_lower: &str,
    ) -> Option<f64> {
        match condition {
            DelegationCondition::DescriptionContains(keywords) => {
                let matches = keywords
                    .iter()
                    .filter(|keyword| task_lower.contains(&keyword.to_lowercase()))
                    .count();
                if matches > 0 {
                    Some(matches as f64 / keywords.len() as f64)
                } else {
                    None
                }
            }
            DelegationCondition::TaskTypeEquals(task_type) => {
                if task.task_type == *task_type {
                    Some(1.0)
                } else {
                    None
                }
            }
            DelegationCondition::PriorityAbove(priority) => {
                if task.priority as u8 >= *priority as u8 {
                    Some(0.5 + (task.priority as u8 as f64 / 10.0))
                } else {
                    None
                }
            }
            DelegationCondition::AgentWorkloadBelow(_threshold) => {
                // This would require current agent workload info
                Some(0.5) // Placeholder
            }
            DelegationCondition::And(conditions) => {
                let scores: Vec<f64> = conditions
                    .iter()
                    .filter_map(|c| Self::evaluate_condition(c, task, task_lower))
                    .collect();

                if scores.len() == conditions.len() {
                    Some(scores.iter().sum::<f64>() / scores.len() as f64)
                } else {
                    None
                }
            }
            DelegationCondition::Or(conditions) => conditions
                .iter()
                .filter_map(|c| Self::evaluate_condition(c, task, task_lower))
                .max_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal)),
        }
    }

    /// Get delegation statistics
    pub fn get_delegation_stats(&self) -> DelegationStats {
        let total_delegations = self.task_history.len();
        let mut agent_counts = HashMap::new();
        let mut total_confidence = 0.0;

        for decision in &self.task_history {
            *agent_counts
                .entry(decision.target_agent.name().to_string())
                .or_insert(0) += 1;
            total_confidence += decision.confidence;
        }

        DelegationStats {
            total_delegations,
            average_confidence: if total_delegations > 0 {
                total_confidence / total_delegations as f64
            } else {
                0.0
            },
            agent_distribution: agent_counts,
            strategy: self.strategy.clone(),
        }
    }
}

/// Delegation statistics
#[derive(Debug, Serialize, Deserialize)]
pub struct DelegationStats {
    pub total_delegations: usize,
    pub average_confidence: f64,
    pub agent_distribution: HashMap<String, usize>,
    pub strategy: DelegationStrategy,
}