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
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
682
/// Session coordination integration for ccswarm multi-agent system
///
/// This module integrates the Session-Persistent Agent Architecture with
/// the existing coordination system, providing seamless interoperability
/// while maximizing token efficiency gains.
use anyhow::{Context, Result};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use tokio::sync::{Mutex, RwLock};
use uuid::Uuid;

use crate::agent::{Task, TaskResult};
use crate::config::ClaudeConfig;
// Temporary coordination types until full coordination system is implemented
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum AgentMessage {
    Coordination {
        from_agent: String,
        to_agent: String,
        message_type: CoordinationType,
        payload: serde_json::Value,
    },
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum CoordinationType {
    StatusUpdate,
    TaskRequest,
    SessionManagement,
}
use crate::identity::AgentRole;
use crate::session::session_pool::{PoolStatistics, SessionPool, SessionPoolConfig};
use crate::session::worktree_session::WorktreeSessionConfig;

/// Enhanced coordination message types for session management
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum SessionCoordinationMessage {
    /// Request task execution with session preferences
    TaskRequest {
        task: Task,
        preferred_session: Option<String>,
        batch_compatible: bool,
        priority_boost: f64,
    },

    /// Batch task request for efficiency
    BatchTaskRequest {
        tasks: Vec<Task>,
        role: AgentRole,
        deadline: Option<DateTime<Utc>>,
    },

    /// Session status update
    SessionStatusUpdate {
        session_id: String,
        agent_id: String,
        load: f64,
        tasks_completed: usize,
        efficiency_metrics: EfficiencyMetrics,
    },

    /// Request session creation/scaling
    SessionScalingRequest {
        role: AgentRole,
        min_sessions: usize,
        reason: ScalingReason,
    },

    /// Efficiency report
    EfficiencyReport {
        timestamp: DateTime<Utc>,
        token_savings: TokenSavingsReport,
        performance_metrics: PerformanceReport,
    },
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ScalingReason {
    HighLoad,
    TaskBacklog,
    PerformanceOptimization,
    UserRequest,
    PredictiveScaling,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EfficiencyMetrics {
    pub sessions_reused: usize,
    pub tokens_saved: usize,
    pub context_continuity_score: f64,
    pub average_response_time: f64,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TokenSavingsReport {
    pub total_tasks_processed: usize,
    pub sessions_created: usize,
    pub estimated_traditional_tokens: usize,
    pub actual_tokens_used: usize,
    pub savings_percentage: f64,
    pub efficiency_score: f64,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PerformanceReport {
    pub total_execution_time: std::time::Duration,
    pub average_task_time: std::time::Duration,
    pub throughput_tasks_per_minute: f64,
    pub error_rate_percentage: f64,
    pub session_utilization: f64,
}

/// Session coordinator that bridges coordination system and session pool
#[derive(Debug)]
pub struct SessionCoordinator {
    /// Session pool for efficient agent management
    session_pool: Arc<Mutex<SessionPool>>,

    /// Coordination bus integration
    #[allow(dead_code)] // Will be used for inter-agent coordination
    coordination_bus: Arc<RwLock<CoordinationBus>>,

    /// Active task tracking
    active_tasks: Arc<RwLock<HashMap<String, TaskExecution>>>,

    /// Performance tracking
    performance_tracker: Arc<Mutex<PerformanceTracker>>,

    /// Configuration
    config: SessionCoordinatorConfig,
}

#[derive(Debug)]
struct CoordinationBus {
    /// Outbound message queue
    #[allow(dead_code)] // Will be used for message queuing
    outbound_queue: Vec<AgentMessage>,

    /// Coordination directory for file-based communication
    #[allow(dead_code)] // Will be used for file-based coordination
    coordination_dir: PathBuf,
}

// Simplified message handler for now - avoiding async trait in dyn context
#[allow(dead_code)] // Will be implemented for message handling
trait MessageHandler: Send + Sync {
    fn handle_message_sync(&self, message: &AgentMessage) -> Result<Option<AgentMessage>>;
}

#[derive(Debug, Clone)]
struct TaskExecution {
    #[allow(dead_code)] // Will be used for task tracking
    pub task: Task,
    #[allow(dead_code)] // Will be used for session assignment tracking
    pub assigned_session: String,
    #[allow(dead_code)] // Will be used for timing metrics
    pub started_at: DateTime<Utc>,
    #[allow(dead_code)] // Will be used for completion estimation
    pub estimated_completion: Option<DateTime<Utc>>,
    pub status: TaskExecutionStatus,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
enum TaskExecutionStatus {
    Queued,
    Executing,
    Completed,
    Failed(String),
    Cancelled,
}

#[derive(Debug)]
struct PerformanceTracker {
    #[allow(dead_code)] // Will be used for performance metrics
    start_time: DateTime<Utc>,
    total_tasks: usize,
    total_sessions_created: usize,
    total_tokens_saved: usize,
    total_execution_time: std::time::Duration,
    error_count: usize,
}

#[derive(Debug, Clone)]
pub struct SessionCoordinatorConfig {
    /// Coordination directory path
    pub coordination_dir: PathBuf,

    /// Performance reporting interval
    pub reporting_interval: std::time::Duration,

    /// Enable efficiency optimization
    pub efficiency_optimization: bool,

    /// Efficiency calculation baseline
    pub efficiency_baseline: f64,

    /// Batch optimization settings
    pub batch_optimization: BatchOptimizationConfig,
}

#[derive(Debug, Clone)]
pub struct BatchOptimizationConfig {
    pub enabled: bool,
    pub max_batch_size: usize,
    pub batch_timeout: std::time::Duration,
    pub compatibility_threshold: f64,
}

impl Default for SessionCoordinatorConfig {
    fn default() -> Self {
        Self {
            coordination_dir: PathBuf::from("coordination"),
            reporting_interval: std::time::Duration::from_secs(60),
            efficiency_optimization: true,
            efficiency_baseline: 1.0, // Baseline efficiency metric
            batch_optimization: BatchOptimizationConfig {
                enabled: true,
                max_batch_size: 10,
                batch_timeout: std::time::Duration::from_secs(30),
                compatibility_threshold: 0.8,
            },
        }
    }
}

impl SessionCoordinator {
    /// Create a new session coordinator
    pub async fn new(
        worktree_config: WorktreeSessionConfig,
        pool_config: SessionPoolConfig,
        coordinator_config: SessionCoordinatorConfig,
    ) -> Result<Self> {
        // Create session pool
        let mut session_pool = SessionPool::new(worktree_config, pool_config).await?;
        session_pool.start().await?;

        // Initialize coordination bus
        let coordination_bus =
            CoordinationBus::new(coordinator_config.coordination_dir.clone()).await?;

        // Initialize performance tracker
        let performance_tracker = PerformanceTracker {
            start_time: Utc::now(),
            total_tasks: 0,
            total_sessions_created: 0,
            total_tokens_saved: 0,
            total_execution_time: std::time::Duration::ZERO,
            error_count: 0,
        };

        Ok(Self {
            session_pool: Arc::new(Mutex::new(session_pool)),
            coordination_bus: Arc::new(RwLock::new(coordination_bus)),
            active_tasks: Arc::new(RwLock::new(HashMap::new())),
            performance_tracker: Arc::new(Mutex::new(performance_tracker)),
            config: coordinator_config,
        })
    }

    /// Start the session coordinator
    pub async fn start(&mut self) -> Result<()> {
        tracing::info!("Starting session coordinator");

        // Start performance reporting task
        if self.config.efficiency_optimization {
            self.start_efficiency_monitoring().await;
        }

        // Start coordination message processing
        self.start_coordination_processing().await;

        tracing::info!("Session coordinator started successfully");
        Ok(())
    }

    /// Execute a task through the coordinated session system
    pub async fn execute_coordinated_task(
        &self,
        role: AgentRole,
        task: Task,
        claude_config: ClaudeConfig,
    ) -> Result<TaskResult> {
        let task_id = task.id.clone();
        let start_time = std::time::Instant::now();

        // Check for batch optimization opportunities
        if self.config.batch_optimization.enabled {
            if let Some(batch_result) = self.try_batch_optimization(&role, &task).await? {
                return Ok(batch_result);
            }
        }

        // Track task execution
        let execution = TaskExecution {
            task: task.clone(),
            assigned_session: "pending".to_string(),
            started_at: Utc::now(),
            estimated_completion: None,
            status: TaskExecutionStatus::Queued,
        };

        {
            let mut active_tasks = self.active_tasks.write().await;
            active_tasks.insert(task_id.clone(), execution);
        }

        // Execute through session pool
        let result = {
            let session_pool = self.session_pool.lock().await;
            session_pool
                .execute_task(role.clone(), task, claude_config)
                .await?
        };

        // Update performance tracking
        {
            let mut tracker = self.performance_tracker.lock().await;
            tracker.total_tasks += 1;
            tracker.total_execution_time += start_time.elapsed();

            if !result.success {
                tracker.error_count += 1;
            }

            // Estimate token savings (simplified calculation)
            let estimated_savings = self.calculate_token_savings(&result).await;
            tracker.total_tokens_saved += estimated_savings;
        }

        // Update task status
        {
            let mut active_tasks = self.active_tasks.write().await;
            if let Some(mut execution) = active_tasks.remove(&task_id) {
                execution.status = if result.success {
                    TaskExecutionStatus::Completed
                } else {
                    TaskExecutionStatus::Failed(
                        result
                            .error
                            .as_deref()
                            .unwrap_or("Unknown error")
                            .to_string(),
                    )
                };
            }
        }

        // Send coordination update
        self.send_coordination_update(&role, &result).await?;

        Ok(result)
    }

    /// Execute multiple tasks in batch for maximum efficiency
    pub async fn execute_coordinated_batch(
        &self,
        role: AgentRole,
        tasks: Vec<Task>,
        claude_config: ClaudeConfig,
    ) -> Result<Vec<TaskResult>> {
        if tasks.is_empty() {
            return Ok(Vec::new());
        }

        tracing::info!(
            "Executing coordinated batch of {} tasks for role: {}",
            tasks.len(),
            role.name()
        );
        let start_time = std::time::Instant::now();

        // Track all tasks
        for task in &tasks {
            let execution = TaskExecution {
                task: task.clone(),
                assigned_session: "batch".to_string(),
                started_at: Utc::now(),
                estimated_completion: None,
                status: TaskExecutionStatus::Executing,
            };

            let mut active_tasks = self.active_tasks.write().await;
            active_tasks.insert(task.id.clone(), execution);
        }

        // Execute batch through session pool
        let results = {
            let session_pool = self.session_pool.lock().await;
            session_pool
                .execute_task_batch(role.clone(), tasks.clone(), claude_config)
                .await?
        };

        // Calculate massive token savings from batch execution
        let estimated_savings = (tasks.len().saturating_sub(1)) * 3400; // ~3400 tokens saved per additional task

        // Update performance tracking
        {
            let mut tracker = self.performance_tracker.lock().await;
            tracker.total_tasks += results.len();
            tracker.total_execution_time += start_time.elapsed();
            tracker.total_tokens_saved += estimated_savings;

            let error_count = results.iter().filter(|r| !r.success).count();
            tracker.error_count += error_count;
        }

        // Send efficiency report
        self.send_efficiency_report(estimated_savings, results.len())
            .await?;

        tracing::info!(
            "Batch execution completed - estimated {} tokens saved",
            estimated_savings
        );
        Ok(results)
    }

    /// Try to optimize task execution through batching
    async fn try_batch_optimization(
        &self,
        _role: &AgentRole,
        _task: &Task,
    ) -> Result<Option<TaskResult>> {
        // In a real implementation, this would check for pending compatible tasks
        // and potentially delay execution to form efficient batches

        // For now, return None to indicate no batch optimization applied
        Ok(None)
    }

    /// Calculate token savings from session reuse
    async fn calculate_token_savings(&self, result: &TaskResult) -> usize {
        // Simplified calculation - in practice would be more sophisticated
        // Based on whether this was a new session or reused session

        // If session was reused (indicated by session_id in output), significant savings
        if let Some(output) = result.output.as_object() {
            if output.contains_key("session_id") {
                return 3400; // Estimated savings from session reuse
            }
        }

        0 // No savings if new session was created
    }

    /// Send coordination update
    async fn send_coordination_update(&self, _role: &AgentRole, result: &TaskResult) -> Result<()> {
        let message = SessionCoordinationMessage::SessionStatusUpdate {
            session_id: "session_id".to_string(), // Would extract from result
            agent_id: "agent_id".to_string(),     // Would extract from result
            load: 0.5,                            // Would calculate actual load
            tasks_completed: 1,
            efficiency_metrics: EfficiencyMetrics {
                sessions_reused: if result.success { 1 } else { 0 },
                tokens_saved: 3400,
                context_continuity_score: 0.95,
                average_response_time: result.duration.as_secs_f64(),
            },
        };

        // Send to coordination system
        self.send_coordination_message(message).await?;
        Ok(())
    }

    /// Send efficiency report
    async fn send_efficiency_report(
        &self,
        _tokens_saved: usize,
        _tasks_processed: usize,
    ) -> Result<()> {
        let tracker = self.performance_tracker.lock().await;

        let report = SessionCoordinationMessage::EfficiencyReport {
            timestamp: Utc::now(),
            token_savings: TokenSavingsReport {
                total_tasks_processed: tracker.total_tasks,
                sessions_created: tracker.total_sessions_created,
                estimated_traditional_tokens: tracker.total_tasks * 3600, // Traditional approach
                actual_tokens_used: (tracker.total_tasks * 3600)
                    .saturating_sub(tracker.total_tokens_saved),
                savings_percentage: if tracker.total_tasks > 0 {
                    (tracker.total_tokens_saved as f64 / (tracker.total_tasks * 3600) as f64)
                        * 100.0
                } else {
                    0.0
                },
                efficiency_score: if tracker.total_tasks > 0 {
                    tracker.total_tokens_saved as f64 / tracker.total_tasks as f64
                } else {
                    0.0
                },
            },
            performance_metrics: PerformanceReport {
                total_execution_time: tracker.total_execution_time,
                average_task_time: if tracker.total_tasks > 0 {
                    tracker.total_execution_time / tracker.total_tasks as u32
                } else {
                    std::time::Duration::ZERO
                },
                throughput_tasks_per_minute: if tracker.total_execution_time.as_secs() > 0 {
                    (tracker.total_tasks as f64 * 60.0) / tracker.total_execution_time.as_secs_f64()
                } else {
                    0.0
                },
                error_rate_percentage: if tracker.total_tasks > 0 {
                    (tracker.error_count as f64 / tracker.total_tasks as f64) * 100.0
                } else {
                    0.0
                },
                session_utilization: 0.75, // Would calculate from pool statistics
            },
        };

        self.send_coordination_message(report).await?;
        Ok(())
    }

    /// Send message to coordination system
    async fn send_coordination_message(&self, message: SessionCoordinationMessage) -> Result<()> {
        let coordination_message = AgentMessage::Coordination {
            from_agent: "session-coordinator".to_string(),
            to_agent: "system".to_string(),
            message_type: CoordinationType::StatusUpdate, // Assuming this exists
            payload: serde_json::to_value(message)?,
        };

        // Write to coordination directory
        let message_file = self
            .config
            .coordination_dir
            .join("messages")
            .join(format!("session-{}.json", Uuid::new_v4()));

        if let Some(parent) = message_file.parent() {
            tokio::fs::create_dir_all(parent).await?;
        }

        tokio::fs::write(
            &message_file,
            serde_json::to_string_pretty(&coordination_message)?,
        )
        .await
        .context("Failed to write coordination message")?;

        Ok(())
    }

    /// Start efficiency monitoring task
    async fn start_efficiency_monitoring(&self) {
        let performance_tracker = Arc::clone(&self.performance_tracker);
        let config = self.config.clone();

        tokio::spawn(async move {
            let mut interval = tokio::time::interval(config.reporting_interval);

            loop {
                interval.tick().await;

                let tracker = performance_tracker.lock().await;

                if tracker.total_tasks > 0 {
                    let efficiency_percentage = (tracker.total_tokens_saved as f64
                        / (tracker.total_tasks * 3600) as f64)
                        * 100.0;
                    let efficiency_score = if tracker.total_tasks > 0 {
                        tracker.total_tokens_saved as f64 / tracker.total_tasks as f64
                    } else {
                        0.0
                    };

                    tracing::info!(
                        "Session Efficiency Report - Tasks: {}, Context Optimized: {}, Efficiency: {:.1}%, Score: {:.2}",
                        tracker.total_tasks,
                        tracker.total_tokens_saved,
                        efficiency_percentage,
                        efficiency_score
                    );
                }
            }
        });
    }

    /// Start coordination message processing
    async fn start_coordination_processing(&self) {
        let coordination_dir = self.config.coordination_dir.clone();

        tokio::spawn(async move {
            let mut interval = tokio::time::interval(std::time::Duration::from_secs(5));

            loop {
                interval.tick().await;

                // Process incoming coordination messages
                if let Err(e) = Self::process_coordination_messages(&coordination_dir).await {
                    tracing::error!("Failed to process coordination messages: {}", e);
                }
            }
        });
    }

    /// Process coordination messages from file system
    async fn process_coordination_messages(coordination_dir: &Path) -> Result<()> {
        let messages_dir = coordination_dir.join("messages");

        if !messages_dir.exists() {
            return Ok(());
        }

        // Read and process coordination messages
        // This would integrate with the existing coordination system
        Ok(())
    }

    /// Get comprehensive coordination statistics
    pub async fn get_coordination_statistics(&self) -> CoordinationStatistics {
        let pool_stats = {
            let session_pool = self.session_pool.lock().await;
            session_pool.get_pool_statistics().await
        };

        let performance_tracker = self.performance_tracker.lock().await;
        let active_tasks = self.active_tasks.read().await;

        CoordinationStatistics {
            pool_statistics: pool_stats,
            efficiency_metrics: EfficiencyMetrics {
                sessions_reused: performance_tracker
                    .total_tasks
                    .saturating_sub(performance_tracker.total_sessions_created),
                tokens_saved: performance_tracker.total_tokens_saved,
                context_continuity_score: 0.95, // Would calculate from session data
                average_response_time: if performance_tracker.total_tasks > 0 {
                    performance_tracker.total_execution_time.as_secs_f64()
                        / performance_tracker.total_tasks as f64
                } else {
                    0.0
                },
            },
            active_task_count: active_tasks.len(),
            total_coordination_messages: 0, // Would track from coordination bus
        }
    }

    /// Shutdown the session coordinator
    pub async fn shutdown(&mut self) -> Result<()> {
        tracing::info!("Shutting down session coordinator");

        // Shutdown session pool
        {
            let mut session_pool = self.session_pool.lock().await;
            session_pool.shutdown().await?;
        }

        tracing::info!("Session coordinator shutdown complete");
        Ok(())
    }
}

/// Coordination statistics combining pool and efficiency metrics
#[derive(Debug, Serialize, Deserialize)]
pub struct CoordinationStatistics {
    pub pool_statistics: PoolStatistics,
    pub efficiency_metrics: EfficiencyMetrics,
    pub active_task_count: usize,
    pub total_coordination_messages: usize,
}

impl CoordinationBus {
    async fn new(coordination_dir: PathBuf) -> Result<Self> {
        tokio::fs::create_dir_all(&coordination_dir).await?;

        Ok(Self {
            outbound_queue: Vec::new(),
            coordination_dir,
        })
    }
}