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
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
use anyhow::Result;
use chrono::{DateTime, Utc};
use dashmap::DashMap;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::{RwLock, mpsc};
use tracing::{debug, error, info, warn};
use uuid::Uuid;

use crate::agent::search_agent::{SearchRequest, SearchResponse};
use crate::agent::{AgentStatus, ClaudeCodeAgent, Priority, Task, TaskResult, TaskType};
use crate::coordination::{AgentMessage, CoordinationBus, CoordinationType};
use crate::subagent::{ParallelConfig, ParallelExecutor, SpawnTask};

/// Proactive Master Claude intelligence system
pub struct ProactiveMaster {
    /// Unique identifier for this master instance
    pub id: String,

    /// List of agents (compatibility field)
    pub agents: Vec<String>,

    /// Project context and goals
    project_context: Arc<RwLock<ProjectContext>>,

    /// Task dependency graph
    dependency_graph: Arc<RwLock<DependencyGraph>>,

    /// Progress analyzer
    progress_analyzer: Arc<RwLock<ProgressAnalyzer>>,

    /// Task predictor
    task_predictor: Arc<RwLock<TaskPredictor>>,

    /// Goal tracker
    goal_tracker: Arc<RwLock<GoalTracker>>,

    /// Parallel executor for task execution
    parallel_executor: ParallelExecutor,

    /// Working directory for task execution
    working_dir: PathBuf,

    /// Shutdown signal sender
    shutdown_tx: Option<mpsc::Sender<()>>,

    /// Coordination bus for agent communication
    coordination_bus: Option<Arc<CoordinationBus>>,

    /// Active agents registry
    active_agents: Arc<DashMap<String, ClaudeCodeAgent>>,

    /// Coordination interval in seconds
    coordination_interval_secs: u64,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProjectContext {
    pub project_type: String,
    pub tech_stack: Vec<String>,
    pub features: Vec<String>,
    pub current_phase: DevelopmentPhase,
    pub milestones: Vec<Milestone>,
    pub constraints: Vec<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum DevelopmentPhase {
    Planning,
    Setup,
    Development,
    Testing,
    Deployment,
    Maintenance,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Milestone {
    pub id: String,
    pub name: String,
    pub description: String,
    pub deadline: Option<DateTime<Utc>>,
    pub completion_percentage: f64,
    pub dependencies: Vec<String>,
    pub critical_path: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DependencyGraph {
    pub nodes: HashMap<String, TaskNode>,
    pub edges: HashMap<String, Vec<String>>, // task_id -> dependent_task_ids
    pub reverse_edges: HashMap<String, Vec<String>>, // task_id -> prerequisite_task_ids
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TaskNode {
    pub task_id: String,
    pub task_type: TaskType,
    pub status: TaskNodeStatus,
    pub estimated_duration: u64, // minutes
    pub actual_duration: Option<u64>,
    pub agent_id: Option<String>,
    pub priority: Priority,
    pub blocking_others: Vec<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum TaskNodeStatus {
    NotStarted,
    InProgress,
    Completed,
    Failed,
    Blocked,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProgressAnalyzer {
    pub velocity_history: Vec<VelocityPoint>,
    pub bottlenecks: Vec<Bottleneck>,
    pub efficiency_metrics: HashMap<String, f64>, // agent_id -> efficiency
    pub prediction_accuracy: f64,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VelocityPoint {
    pub timestamp: DateTime<Utc>,
    pub tasks_completed: u32,
    pub story_points: u32,
    pub team_size: u32,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Bottleneck {
    pub agent_id: String,
    pub task_type: TaskType,
    pub queue_length: u32,
    pub average_wait_time: u64, // minutes
    pub severity: BottleneckSeverity,
}

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

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TaskPredictor {
    pub pattern_library: HashMap<String, TaskPattern>,
    pub completion_patterns: Vec<CompletionPattern>,
    pub feature_templates: HashMap<String, FeatureTemplate>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TaskPattern {
    pub pattern_id: String,
    pub trigger_conditions: Vec<String>,
    pub generated_tasks: Vec<TaskTemplate>,
    pub confidence: f64,
    pub usage_count: u32,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CompletionPattern {
    pub completed_task_type: TaskType,
    pub follow_up_tasks: Vec<TaskTemplate>,
    pub probability: f64,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TaskTemplate {
    pub description_template: String,
    pub task_type: TaskType,
    pub priority: Priority,
    pub estimated_duration: u64,
    pub required_agent_type: String,
    pub variables: HashMap<String, String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FeatureTemplate {
    pub feature_name: String,
    pub required_tasks: Vec<TaskTemplate>,
    pub optional_tasks: Vec<TaskTemplate>,
    pub typical_duration: u64, // hours
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GoalTracker {
    pub objectives: Vec<Objective>,
    pub key_results: Vec<KeyResult>,
    pub current_sprint: Option<Sprint>,
    pub backlog: Vec<BacklogItem>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Objective {
    pub id: String,
    pub title: String,
    pub description: String,
    pub deadline: Option<DateTime<Utc>>,
    pub progress: f64,            // 0.0 to 1.0
    pub key_results: Vec<String>, // IDs
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct KeyResult {
    pub id: String,
    pub objective_id: String,
    pub description: String,
    pub target_value: f64,
    pub current_value: f64,
    pub metric_type: MetricType,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum MetricType {
    Percentage,
    Count,
    Duration,
    Quality,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Sprint {
    pub id: String,
    pub start_date: DateTime<Utc>,
    pub end_date: DateTime<Utc>,
    pub tasks: Vec<String>, // task IDs
    pub velocity_target: u32,
    pub current_velocity: u32,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BacklogItem {
    pub id: String,
    pub title: String,
    pub description: String,
    pub priority: Priority,
    pub story_points: u32,
    pub estimated_tasks: Vec<TaskTemplate>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProactiveDecision {
    pub decision_type: DecisionType,
    pub reasoning: String,
    pub confidence: f64,
    pub suggested_actions: Vec<SuggestedAction>,
    pub risk_assessment: RiskLevel,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum DecisionType {
    GenerateTask,
    ReassignTask,
    ScaleTeam,
    ChangeStrategy,
    RequestIntervention,
    RequestSearch,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SuggestedAction {
    pub action_type: String,
    pub description: String,
    pub parameters: HashMap<String, String>,
    pub expected_impact: String,
}

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

impl ProactiveMaster {
    /// Create a new ProactiveMaster instance
    pub async fn new() -> Result<Self> {
        Self::new_with_config(Default::default(), std::path::PathBuf::from(".")).await
    }

    /// Create a new ProactiveMaster with config and repo path (for compatibility)
    pub async fn new_with_config(
        _config: crate::config::CcswarmConfig,
        repo_path: std::path::PathBuf,
    ) -> Result<Self> {
        let project_context = Arc::new(RwLock::new(ProjectContext {
            project_type: "web_application".to_string(),
            tech_stack: vec![
                "React".to_string(),
                "Node.js".to_string(),
                "PostgreSQL".to_string(),
            ],
            features: vec![],
            current_phase: DevelopmentPhase::Planning,
            milestones: vec![],
            constraints: vec![],
        }));

        let dependency_graph = Arc::new(RwLock::new(DependencyGraph {
            nodes: HashMap::new(),
            edges: HashMap::new(),
            reverse_edges: HashMap::new(),
        }));

        let progress_analyzer = Arc::new(RwLock::new(ProgressAnalyzer {
            velocity_history: vec![],
            bottlenecks: vec![],
            efficiency_metrics: HashMap::new(),
            prediction_accuracy: 0.8,
        }));

        let task_predictor = Arc::new(RwLock::new(TaskPredictor {
            pattern_library: Self::initialize_pattern_library(),
            completion_patterns: Self::initialize_completion_patterns(),
            feature_templates: Self::initialize_feature_templates(),
        }));

        let goal_tracker = Arc::new(RwLock::new(GoalTracker {
            objectives: vec![],
            key_results: vec![],
            current_sprint: None,
            backlog: vec![],
        }));

        // Initialize parallel executor with sensible defaults
        let parallel_config = ParallelConfig {
            max_concurrent: 5,
            default_timeout_ms: 300_000, // 5 minutes
            fail_fast: false,
            retry_failed: true,
            max_retries: 2,
            retry_delay_ms: 1000,
            collect_partial_on_timeout: true,
        };
        let parallel_executor = ParallelExecutor::new(parallel_config);

        Ok(Self {
            id: uuid::Uuid::new_v4().to_string(),
            agents: vec![],
            project_context,
            dependency_graph,
            progress_analyzer,
            task_predictor,
            goal_tracker,
            parallel_executor,
            working_dir: repo_path,
            shutdown_tx: None,
            coordination_bus: None,
            active_agents: Arc::new(DashMap::new()),
            coordination_interval_secs: 5,
        })
    }

    /// Initialize predefined task patterns
    fn initialize_pattern_library() -> HashMap<String, TaskPattern> {
        let mut patterns = HashMap::new();

        // Frontend component pattern
        patterns.insert(
            "frontend_component".to_string(),
            TaskPattern {
                pattern_id: "frontend_component".to_string(),
                trigger_conditions: vec!["component created".to_string()],
                generated_tasks: vec![
                    TaskTemplate {
                        description_template: "Write unit tests for {component_name} component"
                            .to_string(),
                        task_type: TaskType::Testing,
                        priority: Priority::High,
                        estimated_duration: 30,
                        required_agent_type: "QA".to_string(),
                        variables: HashMap::new(),
                    },
                    TaskTemplate {
                        description_template: "Add {component_name} to component library docs"
                            .to_string(),
                        task_type: TaskType::Documentation,
                        priority: Priority::Medium,
                        estimated_duration: 15,
                        required_agent_type: "Frontend".to_string(),
                        variables: HashMap::new(),
                    },
                ],
                confidence: 0.95,
                usage_count: 0,
            },
        );

        // API endpoint pattern
        patterns.insert(
            "api_endpoint".to_string(),
            TaskPattern {
                pattern_id: "api_endpoint".to_string(),
                trigger_conditions: vec!["API endpoint created".to_string()],
                generated_tasks: vec![
                    TaskTemplate {
                        description_template: "Write integration tests for {endpoint_name} API"
                            .to_string(),
                        task_type: TaskType::Testing,
                        priority: Priority::High,
                        estimated_duration: 45,
                        required_agent_type: "QA".to_string(),
                        variables: HashMap::new(),
                    },
                    TaskTemplate {
                        description_template: "Update API documentation for {endpoint_name}"
                            .to_string(),
                        task_type: TaskType::Documentation,
                        priority: Priority::Medium,
                        estimated_duration: 20,
                        required_agent_type: "Backend".to_string(),
                        variables: HashMap::new(),
                    },
                    TaskTemplate {
                        description_template: "Add rate limiting to {endpoint_name} endpoint"
                            .to_string(),
                        task_type: TaskType::Development,
                        priority: Priority::Medium,
                        estimated_duration: 25,
                        required_agent_type: "Backend".to_string(),
                        variables: HashMap::new(),
                    },
                ],
                confidence: 0.9,
                usage_count: 0,
            },
        );

        patterns
    }

    /// Initialize completion patterns
    fn initialize_completion_patterns() -> Vec<CompletionPattern> {
        vec![
            CompletionPattern {
                completed_task_type: TaskType::Development,
                follow_up_tasks: vec![TaskTemplate {
                    description_template: "Test the implemented functionality".to_string(),
                    task_type: TaskType::Testing,
                    priority: Priority::High,
                    estimated_duration: 30,
                    required_agent_type: "QA".to_string(),
                    variables: HashMap::new(),
                }],
                probability: 0.85,
            },
            CompletionPattern {
                completed_task_type: TaskType::Testing,
                follow_up_tasks: vec![TaskTemplate {
                    description_template: "Update documentation with test results".to_string(),
                    task_type: TaskType::Documentation,
                    priority: Priority::Low,
                    estimated_duration: 15,
                    required_agent_type: "QA".to_string(),
                    variables: HashMap::new(),
                }],
                probability: 0.6,
            },
        ]
    }

    /// Initialize feature templates
    fn initialize_feature_templates() -> HashMap<String, FeatureTemplate> {
        let mut templates = HashMap::new();

        templates.insert(
            "user_authentication".to_string(),
            FeatureTemplate {
                feature_name: "User Authentication".to_string(),
                required_tasks: vec![
                    TaskTemplate {
                        description_template: "Create user registration API endpoint".to_string(),
                        task_type: TaskType::Development,
                        priority: Priority::High,
                        estimated_duration: 120,
                        required_agent_type: "Backend".to_string(),
                        variables: HashMap::new(),
                    },
                    TaskTemplate {
                        description_template: "Create user login API endpoint".to_string(),
                        task_type: TaskType::Development,
                        priority: Priority::High,
                        estimated_duration: 90,
                        required_agent_type: "Backend".to_string(),
                        variables: HashMap::new(),
                    },
                    TaskTemplate {
                        description_template: "Create registration form component".to_string(),
                        task_type: TaskType::Development,
                        priority: Priority::High,
                        estimated_duration: 60,
                        required_agent_type: "Frontend".to_string(),
                        variables: HashMap::new(),
                    },
                    TaskTemplate {
                        description_template: "Create login form component".to_string(),
                        task_type: TaskType::Development,
                        priority: Priority::High,
                        estimated_duration: 45,
                        required_agent_type: "Frontend".to_string(),
                        variables: HashMap::new(),
                    },
                ],
                optional_tasks: vec![
                    TaskTemplate {
                        description_template: "Add social login integration".to_string(),
                        task_type: TaskType::Development,
                        priority: Priority::Medium,
                        estimated_duration: 180,
                        required_agent_type: "Backend".to_string(),
                        variables: HashMap::new(),
                    },
                    TaskTemplate {
                        description_template: "Add password reset functionality".to_string(),
                        task_type: TaskType::Development,
                        priority: Priority::Medium,
                        estimated_duration: 90,
                        required_agent_type: "Backend".to_string(),
                        variables: HashMap::new(),
                    },
                ],
                typical_duration: 8, // hours
            },
        );

        templates
    }

    /// Analyze current progress and make proactive decisions
    pub async fn analyze_and_decide(
        &self,
        agents: &DashMap<String, ClaudeCodeAgent>,
        coordination_bus: &CoordinationBus,
    ) -> Result<Vec<ProactiveDecision>> {
        debug!("Starting proactive analysis");

        let mut decisions = Vec::new();

        // 1. Analyze agent progress
        let progress_decisions = self.analyze_agent_progress(agents).await?;
        decisions.extend(progress_decisions);

        // 2. Check for blocked dependencies
        let dependency_decisions = self.resolve_dependencies().await?;
        decisions.extend(dependency_decisions);

        // 3. Predict next tasks based on completion patterns
        let prediction_decisions = self.predict_next_tasks(agents).await?;
        decisions.extend(prediction_decisions);

        // 4. Monitor goal progress
        let goal_decisions = self.monitor_goals().await?;
        decisions.extend(goal_decisions);

        // 5. Analyze for search needs
        let search_decisions = self.analyze_search_needs(agents).await?;
        decisions.extend(search_decisions);

        // Execute high-confidence decisions automatically
        for decision in &decisions {
            if decision.confidence > 0.8 && decision.risk_assessment == RiskLevel::Low {
                self.execute_decision(decision, coordination_bus).await?;
            }
        }

        info!("Generated {} proactive decisions", decisions.len());
        Ok(decisions)
    }

    /// Analyze individual agent progress
    async fn analyze_agent_progress(
        &self,
        agents: &DashMap<String, ClaudeCodeAgent>,
    ) -> Result<Vec<ProactiveDecision>> {
        let mut decisions = Vec::new();
        let mut analyzer = self.progress_analyzer.write().await;

        for entry in agents.iter() {
            let agent = entry.value();
            let agent_id = &agent.identity.agent_id;

            // Check if agent is stuck
            if matches!(agent.status, AgentStatus::Working) {
                let time_since_activity = Utc::now() - agent.last_activity;
                if time_since_activity.num_minutes() > 15 {
                    decisions.push(ProactiveDecision {
                        decision_type: DecisionType::RequestIntervention,
                        reasoning: format!(
                            "Agent {} has been working on the same task for {} minutes without progress",
                            agent_id, time_since_activity.num_minutes()
                        ),
                        confidence: 0.9,
                        suggested_actions: vec![
                            SuggestedAction {
                                action_type: "check_agent_status".to_string(),
                                description: "Check if agent needs assistance".to_string(),
                                parameters: HashMap::from([
                                    ("agent_id".to_string(), agent_id.clone()),
                                ]),
                                expected_impact: "Unblock agent or reassign task".to_string(),
                            },
                        ],
                        risk_assessment: RiskLevel::Low,
                    });
                }
            }

            // Analyze agent efficiency
            let recent_tasks: Vec<_> = agent.task_history.iter().rev().take(5).collect();
            if recent_tasks.len() >= 3 {
                let avg_completion_time: f64 = recent_tasks
                    .iter()
                    .filter_map(|(_task, result)| {
                        if result.success {
                            Some(result.duration.as_secs() as f64 / 60.0)
                        } else {
                            None
                        }
                    })
                    .sum::<f64>()
                    / recent_tasks.len() as f64;

                analyzer
                    .efficiency_metrics
                    .insert(agent_id.clone(), avg_completion_time);
            }
        }

        Ok(decisions)
    }

    /// Resolve dependency conflicts
    async fn resolve_dependencies(&self) -> Result<Vec<ProactiveDecision>> {
        let mut decisions = Vec::new();
        let graph = self.dependency_graph.read().await;

        // Find blocked tasks
        for (task_id, node) in &graph.nodes {
            if node.status == TaskNodeStatus::Blocked {
                // Check if blocking dependencies are resolved
                if let Some(prerequisites) = graph.reverse_edges.get(task_id) {
                    let unresolved: Vec<_> = prerequisites
                        .iter()
                        .filter(|&prereq_id| {
                            graph
                                .nodes
                                .get(prereq_id)
                                .map(|n| n.status != TaskNodeStatus::Completed)
                                .unwrap_or(true)
                        })
                        .collect();

                    if unresolved.is_empty() {
                        // All dependencies resolved, task can be unblocked
                        decisions.push(ProactiveDecision {
                            decision_type: DecisionType::GenerateTask,
                            reasoning: format!(
                                "Task {} can be unblocked - all dependencies completed",
                                task_id
                            ),
                            confidence: 0.95,
                            suggested_actions: vec![SuggestedAction {
                                action_type: "unblock_task".to_string(),
                                description: "Move task to ready queue".to_string(),
                                parameters: HashMap::from([(
                                    "task_id".to_string(),
                                    task_id.clone(),
                                )]),
                                expected_impact: "Enable task execution".to_string(),
                            }],
                            risk_assessment: RiskLevel::Low,
                        });
                    }
                }
            }
        }

        Ok(decisions)
    }

    /// Predict next tasks based on patterns
    async fn predict_next_tasks(
        &self,
        agents: &DashMap<String, ClaudeCodeAgent>,
    ) -> Result<Vec<ProactiveDecision>> {
        let mut decisions = Vec::new();
        let predictor = self.task_predictor.read().await;

        // Analyze recent completions
        for entry in agents.iter() {
            let agent = entry.value();

            // Check last completed task
            if let Some((last_task, last_result)) = agent.task_history.last() {
                if last_result.success {
                    // Find matching completion patterns
                    for pattern in &predictor.completion_patterns {
                        if pattern.completed_task_type == last_task.task_type
                            && pattern.probability > 0.7
                        {
                            for task_template in &pattern.follow_up_tasks {
                                decisions.push(ProactiveDecision {
                                    decision_type: DecisionType::GenerateTask,
                                    reasoning: format!(
                                        "Pattern match: {:?} completion typically requires {}",
                                        last_task.task_type, task_template.description_template
                                    ),
                                    confidence: pattern.probability,
                                    suggested_actions: vec![SuggestedAction {
                                        action_type: "create_task".to_string(),
                                        description: format!(
                                            "Create follow-up task: {}",
                                            task_template.description_template
                                        ),
                                        parameters: HashMap::from([
                                            (
                                                "template".to_string(),
                                                serde_json::to_string(task_template)?,
                                            ),
                                            ("parent_task".to_string(), last_task.id.clone()),
                                        ]),
                                        expected_impact: "Maintain development momentum"
                                            .to_string(),
                                    }],
                                    risk_assessment: RiskLevel::Low,
                                });
                            }
                        }
                    }

                    // Check for pattern triggers in task description
                    let description_lower = last_task.description.to_lowercase();
                    for (pattern_id, pattern) in &predictor.pattern_library {
                        for trigger in &pattern.trigger_conditions {
                            if description_lower.contains(&trigger.to_lowercase()) {
                                for task_template in &pattern.generated_tasks {
                                    decisions.push(ProactiveDecision {
                                        decision_type: DecisionType::GenerateTask,
                                        reasoning: format!(
                                            "Pattern '{}' triggered by: {}",
                                            pattern_id, trigger
                                        ),
                                        confidence: pattern.confidence,
                                        suggested_actions: vec![
                                            SuggestedAction {
                                                action_type: "create_task".to_string(),
                                                description: format!(
                                                    "Auto-generate: {}",
                                                    task_template.description_template
                                                ),
                                                parameters: HashMap::from([
                                                    ("template".to_string(), serde_json::to_string(task_template)?),
                                                    ("trigger_task".to_string(), last_task.id.clone()),
                                                ]),
                                                expected_impact: "Ensure complete feature implementation".to_string(),
                                            },
                                        ],
                                        risk_assessment: RiskLevel::Low,
                                    });
                                }
                            }
                        }
                    }
                }
            }
        }

        Ok(decisions)
    }

    /// Monitor goal progress
    async fn monitor_goals(&self) -> Result<Vec<ProactiveDecision>> {
        let mut decisions = Vec::new();
        let goals = self.goal_tracker.read().await;

        // Check objective progress
        for objective in &goals.objectives {
            if objective.progress < 0.5 {
                if let Some(deadline) = objective.deadline {
                    let time_remaining = deadline - Utc::now();
                    let days_remaining = time_remaining.num_days();

                    if days_remaining <= 7 && objective.progress < 0.8 {
                        decisions.push(ProactiveDecision {
                            decision_type: DecisionType::ChangeStrategy,
                            reasoning: format!(
                                "Objective '{}' is behind schedule: {}% complete with {} days remaining",
                                objective.title, (objective.progress * 100.0) as u32, days_remaining
                            ),
                            confidence: 0.85,
                            suggested_actions: vec![
                                SuggestedAction {
                                    action_type: "reprioritize_tasks".to_string(),
                                    description: "Focus resources on critical objective".to_string(),
                                    parameters: HashMap::from([
                                        ("objective_id".to_string(), objective.id.clone()),
                                    ]),
                                    expected_impact: "Improve deadline adherence".to_string(),
                                },
                            ],
                            risk_assessment: RiskLevel::Medium,
                        });
                    }
                }
            }
        }

        // Check sprint velocity
        if let Some(sprint) = &goals.current_sprint {
            let sprint_progress = (Utc::now() - sprint.start_date).num_days() as f64
                / (sprint.end_date - sprint.start_date).num_days() as f64;

            let velocity_progress = sprint.current_velocity as f64 / sprint.velocity_target as f64;

            if sprint_progress > 0.5 && velocity_progress < 0.3 {
                decisions.push(ProactiveDecision {
                    decision_type: DecisionType::ScaleTeam,
                    reasoning: format!(
                        "Sprint velocity is significantly behind: {}% of target with {}% time remaining",
                        (velocity_progress * 100.0) as u32,
                        ((1.0 - sprint_progress) * 100.0) as u32
                    ),
                    confidence: 0.7,
                    suggested_actions: vec![
                        SuggestedAction {
                            action_type: "add_agent".to_string(),
                            description: "Consider adding additional agents".to_string(),
                            parameters: HashMap::new(),
                            expected_impact: "Increase development velocity".to_string(),
                        },
                    ],
                    risk_assessment: RiskLevel::Medium,
                });
            }
        }

        Ok(decisions)
    }

    /// Execute a proactive decision
    async fn execute_decision(
        &self,
        decision: &ProactiveDecision,
        coordination_bus: &CoordinationBus,
    ) -> Result<()> {
        info!("Executing proactive decision: {:?}", decision.decision_type);

        for action in &decision.suggested_actions {
            match action.action_type.as_str() {
                "create_task" => {
                    if let Some(template_json) = action.parameters.get("template") {
                        let template: TaskTemplate = serde_json::from_str(template_json)?;
                        let task = self.create_task_from_template(&template).await?;

                        // Send task creation message
                        coordination_bus
                            .send_message(AgentMessage::TaskGenerated {
                                task_id: task.id.clone(),
                                description: task.description.clone(),
                                reasoning: decision.reasoning.clone(),
                            })
                            .await?;

                        info!("Auto-generated task: {}", task.description);
                    }
                }
                "unblock_task" => {
                    if let Some(task_id) = action.parameters.get("task_id") {
                        self.unblock_task(task_id).await?;
                    }
                }
                "request_search" => {
                    if let Some(query) = action.parameters.get("query") {
                        if let Some(context) = action.parameters.get("context") {
                            self.request_search(query, context, coordination_bus)
                                .await?;
                        }
                    }
                }
                _ => {
                    debug!("Skipping execution of action: {}", action.action_type);
                }
            }
        }

        Ok(())
    }

    /// Create a task from a template
    async fn create_task_from_template(&self, template: &TaskTemplate) -> Result<Task> {
        let task_id = format!("auto-{}", Uuid::new_v4());
        let description = template.description_template.clone();

        Ok(
            Task::new(task_id, description, template.priority, template.task_type)
                .with_duration((template.estimated_duration * 60) as u32),
        ) // convert minutes to seconds
    }

    /// Unblock a task in the dependency graph
    async fn unblock_task(&self, task_id: &str) -> Result<()> {
        let mut graph = self.dependency_graph.write().await;

        if let Some(node) = graph.nodes.get_mut(task_id) {
            node.status = TaskNodeStatus::NotStarted;
            info!("Unblocked task: {}", task_id);
        }

        Ok(())
    }

    /// Update project context based on completed work
    pub async fn update_context_from_completion(
        &self,
        task: &Task,
        result: &TaskResult,
    ) -> Result<()> {
        if result.success {
            let mut context = self.project_context.write().await;

            // Analyze task to understand what was built
            if task.description.to_lowercase().contains("component") {
                let component_name = self.extract_component_name(&task.description);
                if !context.features.contains(&component_name) {
                    context.features.push(component_name);
                }
            }

            // Update dependency graph
            let mut graph = self.dependency_graph.write().await;
            if let Some(node) = graph.nodes.get_mut(&task.id) {
                node.status = TaskNodeStatus::Completed;
                node.actual_duration = Some(result.duration.as_secs() / 60); // convert to minutes
            }
        }

        Ok(())
    }

    /// Extract component name from task description
    fn extract_component_name(&self, description: &str) -> String {
        // Simple heuristic to extract component names
        let words: Vec<&str> = description.split_whitespace().collect();

        for (i, word) in words.iter().enumerate() {
            if word.to_lowercase() == "component" && i > 0 {
                return words[i - 1].to_string();
            }
        }

        "Unknown Component".to_string()
    }

    /// Add a milestone to track
    pub async fn add_milestone(&self, milestone: Milestone) -> Result<()> {
        let mut context = self.project_context.write().await;
        context.milestones.push(milestone);
        Ok(())
    }

    /// Set project goal
    pub async fn set_objective(&self, objective: Objective) -> Result<()> {
        let mut goals = self.goal_tracker.write().await;
        goals.objectives.push(objective);
        Ok(())
    }

    /// Analyze if search is needed for current tasks
    async fn analyze_search_needs(
        &self,
        agents: &DashMap<String, ClaudeCodeAgent>,
    ) -> Result<Vec<ProactiveDecision>> {
        let mut decisions = Vec::new();

        // Patterns that indicate search might be helpful
        let search_indicators = vec![
            ("research", "Researching information about"),
            ("find information", "Finding information about"),
            ("look up", "Looking up"),
            ("best practices", "Discovering best practices for"),
            ("documentation", "Finding documentation for"),
            ("examples", "Finding examples of"),
            ("how to", "Understanding how to"),
            ("comparison", "Comparing technologies"),
            ("alternatives", "Finding alternatives to"),
            ("error", "Investigating error"),
            ("unknown", "Clarifying unknown concept"),
            ("investigate", "Investigating"),
        ];

        // Check recent agent activities and errors
        for entry in agents.iter() {
            let agent = entry.value();

            // Check if agent is stuck and might need information
            if matches!(agent.status, AgentStatus::Working) {
                let time_since_activity = Utc::now() - agent.last_activity;

                // If stuck for more than 10 minutes, suggest search
                if time_since_activity.num_minutes() > 10 {
                    // Look at current task context
                    if let Some((current_task, _)) = agent.task_history.last() {
                        let task_desc_lower = current_task.description.to_lowercase();

                        // Check if task involves research or information gathering
                        for (indicator, search_prefix) in &search_indicators {
                            if task_desc_lower.contains(indicator) {
                                decisions.push(ProactiveDecision {
                                    decision_type: DecisionType::RequestSearch,
                                    reasoning: format!(
                                        "Agent {} appears stuck on task requiring information: '{}'",
                                        agent.identity.agent_id, current_task.description
                                    ),
                                    confidence: 0.85,
                                    suggested_actions: vec![SuggestedAction {
                                        action_type: "request_search".to_string(),
                                        description: format!("{} {}", search_prefix, current_task.description),
                                        parameters: HashMap::from([
                                            ("query".to_string(), current_task.description.clone()),
                                            ("context".to_string(), format!("Agent {} stuck on task", agent.identity.agent_id)),
                                            ("requesting_agent".to_string(), agent.identity.agent_id.clone()),
                                        ]),
                                        expected_impact: "Provide information to unblock agent".to_string(),
                                    }],
                                    risk_assessment: RiskLevel::Low,
                                });
                                break;
                            }
                        }
                    }
                }
            }

            // Check recent failed tasks for missing information
            let recent_failures: Vec<_> = agent
                .task_history
                .iter()
                .rev()
                .take(3)
                .filter(|(_, result)| !result.success)
                .collect();

            for (failed_task, result) in recent_failures {
                if let Some(error) = &result.error {
                    let error_lower = error.to_lowercase();

                    // Common error patterns that might benefit from search
                    if error_lower.contains("not found")
                        || error_lower.contains("unknown")
                        || error_lower.contains("missing documentation")
                        || error_lower.contains("unclear")
                        || error_lower.contains("deprecat")
                        || error_lower.contains("no examples")
                    {
                        decisions.push(ProactiveDecision {
                            decision_type: DecisionType::RequestSearch,
                            reasoning: format!(
                                "Task {} failed with error suggesting missing information: {}",
                                failed_task.id, error
                            ),
                            confidence: 0.9,
                            suggested_actions: vec![SuggestedAction {
                                action_type: "request_search".to_string(),
                                description: format!("Search for solution to: {}", error),
                                parameters: HashMap::from([
                                    (
                                        "query".to_string(),
                                        format!("{} {}", failed_task.description, error),
                                    ),
                                    (
                                        "context".to_string(),
                                        format!("Error resolution for task {}", failed_task.id),
                                    ),
                                    (
                                        "requesting_agent".to_string(),
                                        agent.identity.agent_id.clone(),
                                    ),
                                ]),
                                expected_impact: "Find solution to resolve error".to_string(),
                            }],
                            risk_assessment: RiskLevel::Low,
                        });
                    }
                }
            }
        }

        // Check project context for technology research needs
        let context = self.project_context.read().await;

        // If in planning or setup phase, suggest research for tech stack
        if matches!(
            context.current_phase,
            DevelopmentPhase::Planning | DevelopmentPhase::Setup
        ) {
            for tech in &context.tech_stack {
                decisions.push(ProactiveDecision {
                    decision_type: DecisionType::RequestSearch,
                    reasoning: format!(
                        "Project in {} phase - gathering best practices for {}",
                        match context.current_phase {
                            DevelopmentPhase::Planning => "planning",
                            DevelopmentPhase::Setup => "setup",
                            _ => "early",
                        },
                        tech
                    ),
                    confidence: 0.75,
                    suggested_actions: vec![SuggestedAction {
                        action_type: "request_search".to_string(),
                        description: format!("{} best practices and setup guide", tech),
                        parameters: HashMap::from([
                            (
                                "query".to_string(),
                                format!("{} best practices setup guide tutorial", tech),
                            ),
                            (
                                "context".to_string(),
                                "Project setup and architecture planning".to_string(),
                            ),
                            ("requesting_agent".to_string(), "master-claude".to_string()),
                        ]),
                        expected_impact: "Ensure proper setup and architecture".to_string(),
                    }],
                    risk_assessment: RiskLevel::Low,
                });
            }
        }

        Ok(decisions)
    }

    /// Request a search from the search agent
    async fn request_search(
        &self,
        query: &str,
        context: &str,
        coordination_bus: &CoordinationBus,
    ) -> Result<()> {
        info!("Requesting search for: {}", query);

        let search_request = SearchRequest {
            requesting_agent: "master-claude".to_string(),
            query: query.to_string(),
            scope: crate::agent::search_agent::SearchScope::All,
            max_results: Some(10),
            filters: None,
            context: Some(context.to_string()),
        };

        let message = AgentMessage::Coordination {
            from_agent: "master-claude".to_string(),
            to_agent: "search".to_string(),
            message_type: CoordinationType::Custom("search_request".to_string()),
            payload: serde_json::to_value(search_request)?,
        };

        coordination_bus.send_message(message).await?;

        Ok(())
    }

    /// Handle search response from search agent
    pub async fn handle_search_response(
        &self,
        response: SearchResponse,
        coordination_bus: &CoordinationBus,
    ) -> Result<()> {
        info!(
            "Received search response with {} results",
            response.results.len()
        );

        // Analyze search results and create appropriate tasks or insights
        if !response.results.is_empty() {
            let mut insights = Vec::new();

            for (i, result) in response.results.iter().take(5).enumerate() {
                insights.push(format!(
                    "{}. {} - {} (relevance: {:?})",
                    i + 1,
                    result.title,
                    result.snippet,
                    result.relevance_score
                ));
            }

            // Create a task to review and apply the findings
            let review_task = Task::new(
                format!("review-search-{}", Uuid::new_v4()),
                format!("Review search findings for: {}", response.query_used),
                Priority::Medium,
                TaskType::Research,
            )
            .with_details(format!(
                "Search query: {}\nTop findings:\n{}\n\nPlease review these findings and apply relevant insights to the current work.",
                response.query_used,
                insights.join("\n")
            ))
            .with_duration(1200); // 20 minutes

            // Send task generation message
            coordination_bus
                .send_message(AgentMessage::TaskGenerated {
                    task_id: review_task.id.clone(),
                    description: review_task.description.clone(),
                    reasoning: format!(
                        "Search completed with {} relevant results",
                        response.results.len()
                    ),
                })
                .await?;
        }

        Ok(())
    }

    /// Set isolation mode for agents (compatibility method)
    pub fn set_isolation_mode(&mut self, _mode: crate::agent::IsolationMode) {
        // This is a compatibility method, isolation is handled differently in ProactiveMaster
        tracing::debug!("Isolation mode set (handled internally)");
    }

    /// Enable or disable delegate mode (lead orchestrates only, no direct code execution)
    pub fn set_delegate_mode(&mut self, enabled: bool) {
        if enabled {
            tracing::info!(
                "Delegate mode enabled: lead will only orchestrate, not execute code directly"
            );
        }
    }

    /// Initialize the ProactiveMaster (compatibility method)
    pub async fn initialize(&mut self) -> Result<()> {
        // ProactiveMaster initializes itself in new(), this is for compatibility
        tracing::info!("ProactiveMaster initialized");
        Ok(())
    }

    /// Start the continuous coordination loop
    ///
    /// This is the main entry point for running the ProactiveMaster. It:
    /// 1. Creates a coordination bus for agent communication
    /// 2. Sets up a shutdown channel for graceful termination
    /// 3. Runs a continuous loop that:
    ///    - Analyzes agent progress and makes decisions
    ///    - Executes high-confidence decisions in parallel
    ///    - Processes incoming messages from agents
    ///    - Handles shutdown signals (Ctrl+C or explicit shutdown)
    pub async fn start_coordination(&mut self) -> Result<()> {
        info!("ProactiveMaster {} starting coordination loop", self.id);

        // Create coordination bus
        let bus = CoordinationBus::new().await?;
        let bus = Arc::new(bus);
        self.coordination_bus = Some(Arc::clone(&bus));

        // Create shutdown channel
        let (shutdown_tx, mut shutdown_rx) = mpsc::channel::<()>(1);
        self.shutdown_tx = Some(shutdown_tx);

        // Create interval for periodic coordination
        let mut interval =
            tokio::time::interval(Duration::from_secs(self.coordination_interval_secs));

        info!(
            "Coordination loop started with {}s interval",
            self.coordination_interval_secs
        );

        loop {
            tokio::select! {
                // Periodic coordination tick
                _ = interval.tick() => {
                    if let Err(e) = self.run_coordination_cycle(&bus).await {
                        error!("Coordination cycle error: {}", e);
                    }
                }

                // Process incoming messages from coordination bus
                msg = async {
                    if let Some(msg) = bus.try_receive_message() {
                        Some(msg)
                    } else {
                        // Small delay to prevent busy-waiting
                        tokio::time::sleep(Duration::from_millis(100)).await;
                        None
                    }
                } => {
                    if let Some(message) = msg {
                        if let Err(e) = self.handle_agent_message(message, &bus).await {
                            warn!("Failed to handle agent message: {}", e);
                        }
                    }
                }

                // Shutdown signal
                _ = shutdown_rx.recv() => {
                    info!("Received shutdown signal, stopping coordination loop");
                    break;
                }

                // Handle Ctrl+C
                _ = tokio::signal::ctrl_c() => {
                    info!("Received Ctrl+C, initiating graceful shutdown");
                    break;
                }
            }
        }

        // Cleanup
        info!("Coordination loop stopped, cleaning up...");
        bus.close().await?;
        self.coordination_bus = None;

        Ok(())
    }

    /// Run a single coordination cycle
    async fn run_coordination_cycle(&self, bus: &Arc<CoordinationBus>) -> Result<()> {
        debug!("Running coordination cycle");

        // Analyze and make decisions
        let decisions = self.analyze_and_decide(&self.active_agents, bus).await?;

        // Filter decisions that should be executed in parallel
        let parallel_decisions: Vec<_> = decisions
            .iter()
            .filter(|d| {
                d.confidence > 0.8
                    && d.risk_assessment == RiskLevel::Low
                    && matches!(d.decision_type, DecisionType::GenerateTask)
            })
            .collect();

        // Execute parallel decisions if any
        if !parallel_decisions.is_empty() {
            info!(
                "Executing {} decisions in parallel",
                parallel_decisions.len()
            );
            if let Err(e) = self.execute_parallel_decisions(&parallel_decisions).await {
                warn!("Parallel execution error: {}", e);
            }
        }

        Ok(())
    }

    /// Execute decisions in parallel using the ParallelExecutor
    async fn execute_parallel_decisions(&self, decisions: &[&ProactiveDecision]) -> Result<()> {
        // Convert decisions to SpawnTasks
        let tasks: Vec<SpawnTask> = decisions
            .iter()
            .flat_map(|decision| {
                decision
                    .suggested_actions
                    .iter()
                    .filter(|action| action.action_type == "create_task")
                    .filter_map(|action| {
                        action.parameters.get("template").map(|template_json| {
                            // Create a SpawnTask from the action
                            SpawnTask::new(&action.description)
                                .with_priority(match decision.risk_assessment {
                                    RiskLevel::Low => 50,
                                    RiskLevel::Medium => 75,
                                    RiskLevel::High => 100,
                                })
                                .with_context("template", serde_json::json!(template_json))
                                .with_context("reasoning", serde_json::json!(&decision.reasoning))
                        })
                    })
            })
            .collect();

        if tasks.is_empty() {
            return Ok(());
        }

        info!("Spawning {} tasks for parallel execution", tasks.len());

        // Execute using PTY-based Claude sessions
        let result = self
            .parallel_executor
            .execute_with_claude_pty(tasks, Some(self.working_dir.clone()), Some(3))
            .await;

        match result {
            Ok(execution_result) => {
                info!(
                    "Parallel execution completed: {} successful, {} failed",
                    execution_result.successful_count, execution_result.failed_count
                );
            }
            Err(e) => {
                error!("Parallel execution failed: {}", e);
            }
        }

        Ok(())
    }

    /// Handle incoming agent messages
    async fn handle_agent_message(
        &self,
        message: AgentMessage,
        bus: &Arc<CoordinationBus>,
    ) -> Result<()> {
        match message {
            AgentMessage::Registration {
                agent_id,
                capabilities,
                metadata,
            } => {
                info!(
                    "Agent {} registered with capabilities: {:?}",
                    agent_id, capabilities
                );
                // Store agent info (would need to create ClaudeCodeAgent here in real impl)
                debug!("Agent metadata: {:?}", metadata);
            }

            AgentMessage::TaskCompleted {
                agent_id,
                task_id,
                result,
            } => {
                info!(
                    "Task {} completed by agent {} (success: {})",
                    task_id, agent_id, result.success
                );
                // Update dependency graph
                let mut graph = self.dependency_graph.write().await;
                if let Some(node) = graph.nodes.get_mut(&task_id) {
                    node.status = if result.success {
                        TaskNodeStatus::Completed
                    } else {
                        TaskNodeStatus::Failed
                    };
                }
            }

            AgentMessage::HelpRequest {
                agent_id,
                context,
                priority,
            } => {
                warn!(
                    "Agent {} requesting help (priority: {:?}): {}",
                    agent_id, priority, context
                );
                // Could trigger search agent or escalate
            }

            AgentMessage::StatusUpdate {
                agent_id,
                status,
                metrics,
            } => {
                debug!("Agent {} status update: {:?}", agent_id, status);
                // Update agent status in active_agents
                if let Some(mut agent) = self.active_agents.get_mut(&agent_id) {
                    agent.status = status;
                    agent.last_activity = Utc::now();
                }
                debug!("Metrics: {:?}", metrics);
            }

            AgentMessage::Coordination {
                from_agent,
                to_agent,
                message_type,
                payload,
            } => {
                debug!(
                    "Coordination message from {} to {}: {:?}",
                    from_agent, to_agent, message_type
                );
                // Handle search responses specially
                if matches!(message_type, CoordinationType::Custom(ref s) if s == "search_response")
                {
                    if let Ok(response) = serde_json::from_value::<SearchResponse>(payload) {
                        self.handle_search_response(response, bus).await?;
                    }
                }
            }

            _ => {
                debug!("Received other message type: {:?}", message);
            }
        }

        Ok(())
    }

    /// Request shutdown of the coordination loop
    pub async fn request_shutdown(&self) -> Result<()> {
        if let Some(tx) = &self.shutdown_tx {
            tx.send(())
                .await
                .map_err(|e| anyhow::anyhow!("Failed to send shutdown signal: {}", e))?;
            info!("Shutdown signal sent");
        } else {
            warn!("No shutdown channel available");
        }
        Ok(())
    }

    /// Get the coordination bus (if running)
    pub fn get_coordination_bus(&self) -> Option<Arc<CoordinationBus>> {
        self.coordination_bus.clone()
    }

    /// Set coordination interval
    pub fn set_coordination_interval(&mut self, seconds: u64) {
        self.coordination_interval_secs = seconds;
    }

    /// Get active agents registry
    pub fn get_active_agents(&self) -> Arc<DashMap<String, ClaudeCodeAgent>> {
        Arc::clone(&self.active_agents)
    }
}

// Extend AgentMessage to include task generation
impl AgentMessage {
    pub fn task_generated(task_id: String, description: String, reasoning: String) -> Self {
        AgentMessage::TaskGenerated {
            task_id,
            description,
            reasoning,
        }
    }
}

/// Custom agent message for task generation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ProactiveAgentMessage {
    TaskGenerated {
        task_id: String,
        description: String,
        reasoning: String,
    },
}