lens-core 1.0.0

High-performance code search engine with LSP integration and benchmarking
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
//! Parallel Pipeline Executor with Async Overlap Processing
//!
//! Implements true stage parallelism and async overlap for maximum performance.
//! Replaces sequential stage execution with overlapping parallel processing.
//! 
//! Target: >40% latency reduction through parallelism per TODO.md

use super::{
    PipelineContext, PipelineData, PipelineConfig, PipelineStage, PipelineStageProcessor,
    PipelineError, memory::ZeroCopyBuffer, stopping::CrossShardStopper, learning::LearningStopModel,
    prefetch::PrefetchManager,
};
use crate::lsp::LspState;
use crate::search::SearchEngine;
use anyhow::Result;
use std::collections::{HashMap, HashSet};
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::sync::{RwLock, Semaphore, mpsc, watch, Barrier};
use tokio::time::timeout;
use tokio::{select, spawn};
use tokio_util::sync::CancellationToken;
use tracing::{debug, error, info, warn};
use serde::{Deserialize, Serialize};

/// Enhanced parallel pipeline executor
pub struct ParallelPipelineExecutor {
    config: PipelineConfig,
    
    // Core engines with enhanced coordination
    search_engine: Arc<SearchEngine>,
    lsp_manager: Arc<LspState>,
    
    // Advanced optimization engines
    cross_shard_stopper: Arc<CrossShardStopper>,
    learning_model: Arc<LearningStopModel>,
    prefetch_manager: Arc<PrefetchManager>,
    
    // Stage coordination
    stage_coordinators: HashMap<PipelineStage, Arc<StageCoordinator>>,
    
    // Parallel execution control
    execution_barrier: Arc<Barrier>,
    stage_semaphores: HashMap<PipelineStage, Arc<Semaphore>>,
    
    // Fusion management
    fusion_controller: Arc<FusionController>,
    
    // Performance metrics
    metrics: Arc<RwLock<ParallelExecutionMetrics>>,
}

/// Stage coordinator for parallel execution
pub struct StageCoordinator {
    stage: PipelineStage,
    processor: Arc<dyn PipelineStageProcessor>,
    dependencies: HashSet<PipelineStage>,
    can_overlap: bool,
    fusion_group: Option<FusionGroupId>,
    concurrency_limit: Arc<Semaphore>,
    metrics: Arc<RwLock<StageMetrics>>,
}

/// Fusion controller for stage combining
pub struct FusionController {
    fusion_groups: HashMap<FusionGroupId, FusionGroup>,
    fusion_opportunities: HashMap<PipelineStage, Vec<PipelineStage>>,
    fusion_benefits: HashMap<FusionGroupId, FusionBenefit>,
    active_fusions: RwLock<HashMap<ExecutionId, Vec<FusionGroupId>>>,
}

/// Fusion group for combined stage execution
#[derive(Debug, Clone)]
pub struct FusionGroup {
    id: FusionGroupId,
    stages: Vec<PipelineStage>,
    fusion_strategy: FusionStrategy,
    expected_speedup: f64,
    memory_sharing: bool,
}

/// Fusion strategy for different stage combinations
#[derive(Debug, Clone, PartialEq)]
pub enum FusionStrategy {
    Pipeline,      // Stages execute in sequence within fusion
    DataParallel,  // Stages operate on different data slices
    TaskParallel,  // Stages execute completely parallel tasks
    Hybrid,        // Mix of pipeline and parallel
}

/// Fusion benefit analysis
#[derive(Debug, Clone)]
pub struct FusionBenefit {
    latency_reduction: f64,
    memory_savings: f64,
    cpu_efficiency: f64,
    cache_locality: f64,
    total_score: f64,
}

/// Parallel execution context with coordination
#[derive(Debug, Clone)]
pub struct ParallelExecutionContext {
    execution_id: ExecutionId,
    base_context: PipelineContext,
    
    // Coordination channels
    stage_completion_tx: mpsc::UnboundedSender<StageCompletion>,
    data_flow_tx: mpsc::UnboundedSender<DataFlow>,
    
    // Cancellation and timeout
    cancellation_token: CancellationToken,
    stage_timeouts: HashMap<PipelineStage, Instant>,
    
    // Resource management
    memory_budget: Arc<RwLock<MemoryBudget>>,
    cpu_budget: Arc<RwLock<CpuBudget>>,
}

/// Stage completion notification
#[derive(Debug, Clone)]
pub struct StageCompletion {
    stage: PipelineStage,
    execution_id: ExecutionId,
    success: bool,
    duration: Duration,
    output_data: Option<PipelineData>,
    error: Option<PipelineError>,
}

/// Data flow between stages
#[derive(Debug, Clone)]
pub struct DataFlow {
    from_stage: PipelineStage,
    to_stage: PipelineStage,
    data: PipelineData,
    flow_type: DataFlowType,
}

/// Type of data flow between stages
#[derive(Debug, Clone, PartialEq)]
pub enum DataFlowType {
    Direct,        // Direct stage-to-stage
    Broadcast,     // One-to-many
    Scatter,       // Split data across stages
    Gather,        // Collect data from stages
}

/// Memory budget tracking
#[derive(Debug, Clone)]
pub struct MemoryBudget {
    total_mb: f64,
    allocated_mb: f64,
    stage_allocations: HashMap<PipelineStage, f64>,
    fusion_savings: f64,
}

/// CPU budget tracking
#[derive(Debug, Clone)]
pub struct CpuBudget {
    total_time_ms: f64,
    allocated_time_ms: f64,
    stage_allocations: HashMap<PipelineStage, f64>,
    overlap_savings: f64,
}

/// Parallel execution metrics
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct ParallelExecutionMetrics {
    pub total_executions: u64,
    pub parallel_executions: u64,
    pub fusion_executions: u64,
    pub overlap_executions: u64,
    
    pub avg_latency_ms: f64,
    pub avg_parallel_latency_ms: f64,
    pub latency_reduction_percent: f64,
    
    pub stage_parallelism: f64,
    pub fusion_effectiveness: f64,
    pub memory_efficiency: f64,
    pub cpu_utilization: f64,
    
    pub early_stopping_rate: f64,
    pub cache_hit_rate: f64,
    pub prefetch_accuracy: f64,
}

/// Stage-specific metrics
#[derive(Debug, Default, Clone)]
pub struct StageMetrics {
    pub executions: u64,
    pub parallel_executions: u64,
    pub avg_duration_ms: f64,
    pub overlap_time_saved_ms: f64,
    pub fusion_time_saved_ms: f64,
    pub memory_peak_mb: f64,
    pub throughput_ops_per_sec: f64,
}

// Type aliases
pub type FusionGroupId = u32;
pub type ExecutionId = u64;

impl ParallelPipelineExecutor {
    /// Create a new parallel pipeline executor
    pub async fn new(config: PipelineConfig) -> Result<Self> {
        // Initialize core engines
        let search_engine = Arc::new(SearchEngine::new(&config.max_concurrent.to_string()).await?);
        
        let lsp_state = Arc::new(LspState::new(crate::lsp::LspConfig {
            enabled: true,
            server_timeout_ms: config.max_latency_ms / 3,
            cache_ttl_hours: 24,
            max_concurrent_requests: config.max_concurrent,
            routing_percentage: 0.6, // 60% routing for better coverage
            ..Default::default()
        }));
        lsp_state.initialize().await?;
        
        // Initialize advanced optimization engines
        let stopper_config = crate::pipeline::stopping::StoppingConfig {
            confidence_threshold: 0.85,
            quality_threshold: 0.8,
            max_computation_budget: 0.7,
            num_shards: 4,
            ..Default::default()
        };
        let cross_shard_stopper = Arc::new(CrossShardStopper::new(stopper_config).await?);
        
        let learning_config = crate::pipeline::learning::LearningConfig {
            confidence_threshold: 0.85,
            min_training_samples: 20,
            ..Default::default()
        };
        let learning_model = Arc::new(LearningStopModel::new(learning_config).await?);
        
        let prefetch_config = crate::pipeline::prefetch::PrefetchConfig {
            similarity_threshold: 0.7,
            max_cached_visited_sets: 10000,
            ..Default::default()
        };
        let prefetch_manager = Arc::new(PrefetchManager::new(prefetch_config).await?);
        
        // Initialize stage coordinators
        let mut stage_coordinators = HashMap::new();
        let mut stage_semaphores = HashMap::new();
        
        // Query Analysis - can be parallelized and fused
        let query_coordinator = Arc::new(StageCoordinator::new(
            PipelineStage::QueryAnalysis,
            Arc::new(EnhancedQueryAnalysisStage::new(learning_model.clone())),
            HashSet::new(), // No dependencies
            true, // Can overlap
            Some(1), // Fusion group 1
            4, // Concurrency limit
        ).await);
        stage_coordinators.insert(PipelineStage::QueryAnalysis, query_coordinator);
        stage_semaphores.insert(PipelineStage::QueryAnalysis, Arc::new(Semaphore::new(4)));
        
        // LSP Routing - can overlap with analysis, fusion with search
        let mut lsp_deps = HashSet::new();
        lsp_deps.insert(PipelineStage::QueryAnalysis);
        let lsp_coordinator = Arc::new(StageCoordinator::new(
            PipelineStage::LspRouting,
            Arc::new(EnhancedLspRoutingStage::new(lsp_state.clone(), prefetch_manager.clone())),
            lsp_deps,
            true, // Can overlap
            Some(2), // Fusion group 2
            8, // Higher concurrency for LSP
        ).await);
        stage_coordinators.insert(PipelineStage::LspRouting, lsp_coordinator);
        stage_semaphores.insert(PipelineStage::LspRouting, Arc::new(Semaphore::new(8)));
        
        // Parallel Search - main computational stage
        let mut search_deps = HashSet::new();
        search_deps.insert(PipelineStage::LspRouting);
        let search_coordinator = Arc::new(StageCoordinator::new(
            PipelineStage::ParallelSearch,
            Arc::new(EnhancedParallelSearchStage::new(
                search_engine.clone(),
                lsp_state.clone(),
                cross_shard_stopper.clone(),
            )),
            search_deps,
            true, // Can overlap
            Some(2), // Fusion group 2 with LSP
            16, // High concurrency for search
        ).await);
        stage_coordinators.insert(PipelineStage::ParallelSearch, search_coordinator);
        stage_semaphores.insert(PipelineStage::ParallelSearch, Arc::new(Semaphore::new(16)));
        
        // Result Fusion - combines results with zero-copy
        let mut fusion_deps = HashSet::new();
        fusion_deps.insert(PipelineStage::ParallelSearch);
        let fusion_coordinator = Arc::new(StageCoordinator::new(
            PipelineStage::ResultFusion,
            Arc::new(EnhancedResultFusionStage::new()),
            fusion_deps,
            true, // Can overlap
            Some(3), // Fusion group 3
            4, // Medium concurrency
        ).await);
        stage_coordinators.insert(PipelineStage::ResultFusion, fusion_coordinator);
        stage_semaphores.insert(PipelineStage::ResultFusion, Arc::new(Semaphore::new(4)));
        
        // Post Processing - final cleanup and optimization
        let mut post_deps = HashSet::new();
        post_deps.insert(PipelineStage::ResultFusion);
        let post_coordinator = Arc::new(StageCoordinator::new(
            PipelineStage::PostProcess,
            Arc::new(EnhancedPostProcessStage::new(learning_model.clone())),
            post_deps,
            false, // Sequential for final processing
            Some(3), // Fusion group 3 with result fusion
            2, // Low concurrency
        ).await);
        stage_coordinators.insert(PipelineStage::PostProcess, post_coordinator);
        stage_semaphores.insert(PipelineStage::PostProcess, Arc::new(Semaphore::new(2)));
        
        // Create execution barrier (6 stages total)
        let execution_barrier = Arc::new(Barrier::new(6));
        
        // Initialize fusion controller
        let fusion_controller = Arc::new(FusionController::new());
        fusion_controller.initialize_fusion_groups().await?;
        
        let metrics = Arc::new(RwLock::new(ParallelExecutionMetrics::default()));
        
        info!("Initialized parallel pipeline executor with {} fusion groups", 
              fusion_controller.fusion_groups.len());
        
        Ok(Self {
            config,
            search_engine,
            lsp_manager: lsp_state,
            cross_shard_stopper,
            learning_model,
            prefetch_manager,
            stage_coordinators,
            execution_barrier,
            stage_semaphores,
            fusion_controller,
            metrics,
        })
    }
    
    /// Execute pipeline with parallel stages and fusion
    pub async fn execute_parallel(&self, context: PipelineContext) -> Result<PipelineData, PipelineError> {
        let start_time = Instant::now();
        let execution_id = self.generate_execution_id();
        
        debug!("Starting parallel execution {}", execution_id);
        
        // Check if we can use prefetch results
        let prefetch_result = self.prefetch_manager
            .process_query(&context.query, context.file_path.clone())
            .await
            .map_err(|e| PipelineError::FusionError {
                stage: PipelineStage::Input,
                message: format!("Prefetch failed: {}", e),
            })?;
        
        if prefetch_result.cache_hit {
            info!("Using prefetch cache for execution {}", execution_id);
            return self.create_cached_result(prefetch_result, context).await;
        }
        
        // Set up parallel execution context
        let (completion_tx, mut completion_rx) = mpsc::unbounded_channel::<StageCompletion>();
        let (data_flow_tx, mut data_flow_rx) = mpsc::unbounded_channel::<DataFlow>();
        let cancellation_token = CancellationToken::new();
        
        let parallel_context = ParallelExecutionContext {
            execution_id,
            base_context: context.clone(),
            stage_completion_tx: completion_tx,
            data_flow_tx,
            cancellation_token: cancellation_token.clone(),
            stage_timeouts: self.calculate_stage_timeouts(&context),
            memory_budget: Arc::new(RwLock::new(MemoryBudget::new(256.0))), // 256MB budget
            cpu_budget: Arc::new(RwLock::new(CpuBudget::new(context.timeout.as_millis() as f64))),
        };
        
        // Analyze fusion opportunities
        let fusion_plan = self.fusion_controller.create_fusion_plan(execution_id).await?;
        
        // Start parallel stage execution
        let stage_handles = self.spawn_parallel_stages(parallel_context.clone(), fusion_plan).await?;
        
        // Monitor execution progress
        let result = self.monitor_parallel_execution(
            parallel_context,
            stage_handles,
            &mut completion_rx,
            &mut data_flow_rx,
            cancellation_token,
        ).await?;
        
        // Update metrics
        let execution_time = start_time.elapsed();
        self.update_parallel_metrics(execution_time, result.is_ok()).await;
        
        result
    }
    
    /// Spawn parallel stage execution tasks
    async fn spawn_parallel_stages(
        &self,
        context: ParallelExecutionContext,
        fusion_plan: FusionPlan,
    ) -> Result<Vec<tokio::task::JoinHandle<Result<(), PipelineError>>>, PipelineError> {
        let mut handles = Vec::new();
        
        // Execute fused stages first
        for fusion_group in fusion_plan.fusion_groups {
            let handle = self.spawn_fused_stages(
                context.clone(),
                fusion_group,
            ).await?;
            handles.push(handle);
        }
        
        // Execute independent stages
        for stage in fusion_plan.independent_stages {
            let handle = self.spawn_single_stage(
                context.clone(),
                stage,
            ).await?;
            handles.push(handle);
        }
        
        Ok(handles)
    }
    
    /// Spawn fused stage execution
    async fn spawn_fused_stages(
        &self,
        context: ParallelExecutionContext,
        fusion_group: FusionGroup,
    ) -> Result<tokio::task::JoinHandle<Result<(), PipelineError>>, PipelineError> {
        let stages = fusion_group.stages.clone();
        let fusion_strategy = fusion_group.fusion_strategy.clone();
        let stage_coordinators = self.stage_coordinators.clone();
        let barrier = self.execution_barrier.clone();
        
        let handle = spawn(async move {
            debug!("Executing fused stages: {:?} with strategy {:?}", stages, fusion_strategy);
            
            match fusion_strategy {
                FusionStrategy::Pipeline => {
                    Self::execute_pipeline_fusion(context, stages, stage_coordinators, barrier).await
                }
                FusionStrategy::DataParallel => {
                    Self::execute_data_parallel_fusion(context, stages, stage_coordinators, barrier).await
                }
                FusionStrategy::TaskParallel => {
                    Self::execute_task_parallel_fusion(context, stages, stage_coordinators, barrier).await
                }
                FusionStrategy::Hybrid => {
                    Self::execute_hybrid_fusion(context, stages, stage_coordinators, barrier).await
                }
            }
        });
        
        Ok(handle)
    }
    
    /// Execute pipeline fusion (sequential within group)
    async fn execute_pipeline_fusion(
        mut context: ParallelExecutionContext,
        stages: Vec<PipelineStage>,
        coordinators: HashMap<PipelineStage, Arc<StageCoordinator>>,
        _barrier: Arc<Barrier>,
    ) -> Result<(), PipelineError> {
        let mut current_data = PipelineData::new(8192); // 8KB initial buffer
        
        for stage in stages {
            if let Some(coordinator) = coordinators.get(&stage) {
                // Check timeout
                if let Some(&deadline) = context.stage_timeouts.get(&stage) {
                    if Instant::now() > deadline {
                        return Err(PipelineError::StageTimeout {
                            stage,
                            elapsed_ms: context.base_context.elapsed().as_millis() as u64,
                        });
                    }
                }
                
                // Execute stage
                let stage_start = Instant::now();
                match coordinator.execute_stage(&context.base_context, current_data).await {
                    Ok(output_data) => {
                        current_data = output_data;
                        let duration = stage_start.elapsed();
                        
                        // Send completion notification
                        let completion = StageCompletion {
                            stage,
                            execution_id: context.execution_id,
                            success: true,
                            duration,
                            output_data: Some(current_data.clone()),
                            error: None,
                        };
                        
                        if context.stage_completion_tx.send(completion).is_err() {
                            warn!("Failed to send stage completion notification");
                        }
                        
                        debug!("Pipeline fusion stage {:?} completed in {}ms", stage, duration.as_millis());
                    }
                    Err(e) => {
                        let completion = StageCompletion {
                            stage,
                            execution_id: context.execution_id,
                            success: false,
                            duration: stage_start.elapsed(),
                            output_data: None,
                            error: Some(e.clone()),
                        };
                        
                        let _ = context.stage_completion_tx.send(completion);
                        return Err(e);
                    }
                }
            }
        }
        
        Ok(())
    }
    
    /// Execute data parallel fusion (split data across stages)
    async fn execute_data_parallel_fusion(
        context: ParallelExecutionContext,
        stages: Vec<PipelineStage>,
        coordinators: HashMap<PipelineStage, Arc<StageCoordinator>>,
        barrier: Arc<Barrier>,
    ) -> Result<(), PipelineError> {
        let initial_data = PipelineData::new(8192);
        let data_slices = Self::split_data_for_parallel_processing(&initial_data, stages.len());
        
        let mut handles = Vec::new();
        
        for (stage, data_slice) in stages.into_iter().zip(data_slices.into_iter()) {
            if let Some(coordinator) = coordinators.get(&stage).cloned() {
                let ctx = context.clone();
                let barrier_ref = barrier.clone();
                
                let handle = spawn(async move {
                    barrier_ref.wait().await;
                    
                    let stage_start = Instant::now();
                    let result = coordinator.execute_stage(&ctx.base_context, data_slice).await;
                    let duration = stage_start.elapsed();
                    
                    let completion = match result {
                        Ok(output_data) => StageCompletion {
                            stage,
                            execution_id: ctx.execution_id,
                            success: true,
                            duration,
                            output_data: Some(output_data),
                            error: None,
                        },
                        Err(e) => StageCompletion {
                            stage,
                            execution_id: ctx.execution_id,
                            success: false,
                            duration,
                            output_data: None,
                            error: Some(e.clone()),
                        },
                    };
                    
                    let _ = ctx.stage_completion_tx.send(completion);
                    result.map(|_| ())
                });
                
                handles.push(handle);
            }
        }
        
        // Wait for all parallel stages to complete
        for handle in handles {
            handle.await.map_err(|e| PipelineError::FusionError {
                stage: PipelineStage::Input,
                message: format!("Parallel stage execution failed: {}", e),
            })??;
        }
        
        Ok(())
    }
    
    /// Execute task parallel fusion (completely parallel)
    async fn execute_task_parallel_fusion(
        context: ParallelExecutionContext,
        stages: Vec<PipelineStage>,
        coordinators: HashMap<PipelineStage, Arc<StageCoordinator>>,
        barrier: Arc<Barrier>,
    ) -> Result<(), PipelineError> {
        let mut handles = Vec::new();
        let initial_data = PipelineData::new(8192);
        
        for stage in stages {
            if let Some(coordinator) = coordinators.get(&stage).cloned() {
                let ctx = context.clone();
                let data = initial_data.clone();
                let barrier_ref = barrier.clone();
                
                let handle = spawn(async move {
                    barrier_ref.wait().await;
                    
                    let stage_start = Instant::now();
                    let result = coordinator.execute_stage(&ctx.base_context, data).await;
                    let duration = stage_start.elapsed();
                    
                    let completion = match result {
                        Ok(output_data) => StageCompletion {
                            stage,
                            execution_id: ctx.execution_id,
                            success: true,
                            duration,
                            output_data: Some(output_data),
                            error: None,
                        },
                        Err(e) => StageCompletion {
                            stage,
                            execution_id: ctx.execution_id,
                            success: false,
                            duration,
                            output_data: None,
                            error: Some(e.clone()),
                        },
                    };
                    
                    let _ = ctx.stage_completion_tx.send(completion);
                    result.map(|_| ())
                });
                
                handles.push(handle);
            }
        }
        
        // Wait for all tasks to complete
        for handle in handles {
            handle.await.map_err(|e| PipelineError::FusionError {
                stage: PipelineStage::Input,
                message: format!("Task parallel execution failed: {}", e),
            })??;
        }
        
        Ok(())
    }
    
    /// Execute hybrid fusion (mix of strategies)
    async fn execute_hybrid_fusion(
        context: ParallelExecutionContext,
        stages: Vec<PipelineStage>,
        coordinators: HashMap<PipelineStage, Arc<StageCoordinator>>,
        barrier: Arc<Barrier>,
    ) -> Result<(), PipelineError> {
        // For hybrid fusion, we can analyze stages and apply the best strategy for each subset
        // This is a simplified implementation - real logic would be more complex
        
        let (pipeline_stages, parallel_stages): (Vec<_>, Vec<_>) = stages.into_iter()
            .partition(|stage| {
                coordinators.get(stage)
                    .map(|c| !c.can_overlap)
                    .unwrap_or(false)
            });
        
        // Execute pipeline stages first
        if !pipeline_stages.is_empty() {
            Self::execute_pipeline_fusion(
                context.clone(),
                pipeline_stages,
                coordinators.clone(),
                barrier.clone(),
            ).await?;
        }
        
        // Then execute parallel stages
        if !parallel_stages.is_empty() {
            Self::execute_task_parallel_fusion(
                context,
                parallel_stages,
                coordinators,
                barrier,
            ).await?;
        }
        
        Ok(())
    }
    
    /// Spawn single stage execution
    async fn spawn_single_stage(
        &self,
        context: ParallelExecutionContext,
        stage: PipelineStage,
    ) -> Result<tokio::task::JoinHandle<Result<(), PipelineError>>, PipelineError> {
        let coordinator = self.stage_coordinators.get(&stage)
            .ok_or_else(|| PipelineError::FusionError {
                stage,
                message: "Stage coordinator not found".to_string(),
            })?
            .clone();
        
        let handle = spawn(async move {
            let stage_start = Instant::now();
            let initial_data = PipelineData::new(8192);
            
            let result = coordinator.execute_stage(&context.base_context, initial_data).await;
            let duration = stage_start.elapsed();
            
            let completion = match result {
                Ok(output_data) => StageCompletion {
                    stage,
                    execution_id: context.execution_id,
                    success: true,
                    duration,
                    output_data: Some(output_data),
                    error: None,
                },
                Err(e) => StageCompletion {
                    stage,
                    execution_id: context.execution_id,
                    success: false,
                    duration,
                    output_data: None,
                    error: Some(e.clone()),
                },
            };
            
            let _ = context.stage_completion_tx.send(completion);
            result.map(|_| ())
        });
        
        Ok(handle)
    }
    
    /// Monitor parallel execution progress
    async fn monitor_parallel_execution(
        &self,
        context: ParallelExecutionContext,
        stage_handles: Vec<tokio::task::JoinHandle<Result<(), PipelineError>>>,
        completion_rx: &mut mpsc::UnboundedReceiver<StageCompletion>,
        data_flow_rx: &mut mpsc::UnboundedReceiver<DataFlow>,
        cancellation_token: CancellationToken,
    ) -> Result<PipelineData, PipelineError> {
        let mut completed_stages = HashSet::new();
        let mut stage_outputs = HashMap::new();
        let total_stages = self.stage_coordinators.len();
        
        // Set up timeout
        let timeout_future = timeout(context.base_context.timeout, async {
            loop {
                select! {
                    completion = completion_rx.recv() => {
                        if let Some(completion) = completion {
                            debug!("Stage {:?} completed: success={}", completion.stage, completion.success);
                            
                            if !completion.success {
                                if let Some(error) = completion.error {
                                    return Err(error);
                                }
                                return Err(PipelineError::FusionError {
                                    stage: completion.stage,
                                    message: "Stage execution failed".to_string(),
                                });
                            }
                            
                            completed_stages.insert(completion.stage);
                            if let Some(output) = completion.output_data {
                                stage_outputs.insert(completion.stage, output);
                            }
                            
                            // Check if all stages completed
                            if completed_stages.len() == total_stages {
                                return Ok(self.merge_stage_outputs(stage_outputs).await?);
                            }
                        }
                    }
                    
                    data_flow = data_flow_rx.recv() => {
                        if let Some(flow) = data_flow {
                            debug!("Data flow: {:?} -> {:?}", flow.from_stage, flow.to_stage);
                            // Handle data flow between stages
                        }
                    }
                    
                    _ = cancellation_token.cancelled() => {
                        warn!("Parallel execution cancelled");
                        return Err(PipelineError::DeadlineExceeded {
                            elapsed_ms: context.base_context.elapsed().as_millis() as u64,
                            deadline_ms: context.base_context.timeout.as_millis() as u64,
                        });
                    }
                }
            }
        });
        
        let result = timeout_future.await.map_err(|_| {
            cancellation_token.cancel();
            PipelineError::DeadlineExceeded {
                elapsed_ms: context.base_context.elapsed().as_millis() as u64,
                deadline_ms: context.base_context.timeout.as_millis() as u64,
            }
        })??;
        
        // Wait for all stage handles to complete
        for handle in stage_handles {
            if let Err(e) = handle.await {
                warn!("Stage handle completion error: {:?}", e);
            }
        }
        
        Ok(result)
    }
    
    /// Merge outputs from parallel stages
    async fn merge_stage_outputs(&self, outputs: HashMap<PipelineStage, PipelineData>) -> Result<PipelineData, PipelineError> {
        // Use the result fusion stage output if available
        if let Some(fusion_output) = outputs.get(&PipelineStage::ResultFusion) {
            return Ok(fusion_output.clone());
        }
        
        // Otherwise, merge available outputs (simplified)
        let mut merged_data = PipelineData::new(16384); // 16KB buffer
        
        for (stage, data) in outputs {
            debug!("Merging output from stage {:?} with {} segments", stage, data.segments.len());
            for segment in data.segments {
                merged_data.add_segment(segment);
            }
            
            // Merge metadata
            merged_data.metadata.stage_transitions += data.metadata.stage_transitions;
            merged_data.metadata.bytes_processed += data.metadata.bytes_processed;
            merged_data.metadata.cache_hits += data.metadata.cache_hits;
            merged_data.metadata.lsp_queries += data.metadata.lsp_queries;
            merged_data.metadata.search_results_count += data.metadata.search_results_count;
        }
        
        Ok(merged_data)
    }
    
    /// Helper methods
    fn generate_execution_id(&self) -> ExecutionId {
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_nanos() as u64
    }
    
    fn calculate_stage_timeouts(&self, context: &PipelineContext) -> HashMap<PipelineStage, Instant> {
        let mut timeouts = HashMap::new();
        let base_deadline = context.sla_deadline;
        
        for stage in [
            PipelineStage::QueryAnalysis,
            PipelineStage::LspRouting,
            PipelineStage::ParallelSearch,
            PipelineStage::ResultFusion,
            PipelineStage::PostProcess,
        ] {
            let timeout_duration = self.config.stage_timeouts.calculate_timeout(
                self.config.max_latency_ms,
                stage,
            );
            timeouts.insert(stage, base_deadline - timeout_duration);
        }
        
        timeouts
    }
    
    fn split_data_for_parallel_processing(data: &PipelineData, num_splits: usize) -> Vec<PipelineData> {
        // Simplified data splitting - real implementation would be more sophisticated
        let mut splits = Vec::new();
        
        for i in 0..num_splits {
            let mut split_data = PipelineData::new(data.buffer.len() / num_splits);
            split_data.metadata.stage_transitions = data.metadata.stage_transitions;
            
            // Create a view of the original data
            if let Ok(segment) = data.create_view(
                i * (data.total_size() / num_splits),
                data.total_size() / num_splits,
            ) {
                split_data.add_segment(segment);
            }
            
            splits.push(split_data);
        }
        
        splits
    }
    
    async fn create_cached_result(
        &self,
        prefetch_result: crate::pipeline::prefetch::PrefetchResult,
        context: PipelineContext,
    ) -> Result<PipelineData, PipelineError> {
        let mut cached_data = PipelineData::new(4096);
        
        // Use prefetch hints to populate result
        cached_data.metadata.search_results_count = prefetch_result.result_hints.len();
        cached_data.metadata.cache_hits = 1;
        cached_data.stage = PipelineStage::Output;
        
        debug!("Created cached result with {} hints", prefetch_result.result_hints.len());
        Ok(cached_data)
    }
    
    async fn update_parallel_metrics(&self, execution_time: Duration, success: bool) {
        let mut metrics = self.metrics.write().await;
        metrics.total_executions += 1;
        
        if success {
            metrics.parallel_executions += 1;
        }
        
        let execution_time_ms = execution_time.as_millis() as f64;
        let total = metrics.total_executions as f64;
        
        metrics.avg_latency_ms = (metrics.avg_latency_ms * (total - 1.0) + execution_time_ms) / total;
        
        if success {
            let parallel_total = metrics.parallel_executions as f64;
            metrics.avg_parallel_latency_ms = 
                (metrics.avg_parallel_latency_ms * (parallel_total - 1.0) + execution_time_ms) / parallel_total;
        }
        
        // Update latency reduction percentage
        if metrics.avg_latency_ms > 0.0 {
            metrics.latency_reduction_percent = 
                (1.0 - metrics.avg_parallel_latency_ms / metrics.avg_latency_ms) * 100.0;
        }
    }
    
    /// Get parallel execution metrics
    pub async fn get_metrics(&self) -> ParallelExecutionMetrics {
        self.metrics.read().await.clone()
    }
}

/// Fusion plan for execution
#[derive(Debug, Clone)]
pub struct FusionPlan {
    pub fusion_groups: Vec<FusionGroup>,
    pub independent_stages: Vec<PipelineStage>,
    pub expected_speedup: f64,
}

// Enhanced stage implementations would be in separate files
// These are simplified placeholders showing the interface

pub struct EnhancedQueryAnalysisStage {
    learning_model: Arc<LearningStopModel>,
}

impl EnhancedQueryAnalysisStage {
    pub fn new(learning_model: Arc<LearningStopModel>) -> Self {
        Self { learning_model }
    }
}

pub struct EnhancedLspRoutingStage {
    lsp_manager: Arc<LspState>,
    prefetch_manager: Arc<PrefetchManager>,
}

impl EnhancedLspRoutingStage {
    pub fn new(lsp_manager: Arc<LspState>, prefetch_manager: Arc<PrefetchManager>) -> Self {
        Self { lsp_manager, prefetch_manager }
    }
}

pub struct EnhancedParallelSearchStage {
    search_engine: Arc<SearchEngine>,
    lsp_manager: Arc<LspState>,
    cross_shard_stopper: Arc<CrossShardStopper>,
}

impl EnhancedParallelSearchStage {
    pub fn new(
        search_engine: Arc<SearchEngine>,
        lsp_manager: Arc<LspState>,
        cross_shard_stopper: Arc<CrossShardStopper>,
    ) -> Self {
        Self {
            search_engine,
            lsp_manager,
            cross_shard_stopper,
        }
    }
}

pub struct EnhancedResultFusionStage;

impl EnhancedResultFusionStage {
    pub fn new() -> Self {
        Self
    }
}

pub struct EnhancedPostProcessStage {
    learning_model: Arc<LearningStopModel>,
}

impl EnhancedPostProcessStage {
    pub fn new(learning_model: Arc<LearningStopModel>) -> Self {
        Self { learning_model }
    }
}

// Implementations of stage coordination
impl StageCoordinator {
    pub async fn new(
        stage: PipelineStage,
        processor: Arc<dyn PipelineStageProcessor>,
        dependencies: HashSet<PipelineStage>,
        can_overlap: bool,
        fusion_group: Option<FusionGroupId>,
        concurrency_limit: usize,
    ) -> Self {
        Self {
            stage,
            processor,
            dependencies,
            can_overlap,
            fusion_group,
            concurrency_limit: Arc::new(Semaphore::new(concurrency_limit)),
            metrics: Arc::new(RwLock::new(StageMetrics::default())),
        }
    }
    
    pub async fn execute_stage(
        &self,
        context: &PipelineContext,
        data: PipelineData,
    ) -> Result<PipelineData, PipelineError> {
        let _permit = self.concurrency_limit.acquire().await.map_err(|_| {
            PipelineError::FusionError {
                stage: self.stage,
                message: "Failed to acquire concurrency permit".to_string(),
            }
        })?;
        
        let start_time = Instant::now();
        let result = self.processor.process(context, data).await;
        let duration = start_time.elapsed();
        
        // Update metrics
        {
            let mut metrics = self.metrics.write().await;
            metrics.executions += 1;
            let total = metrics.executions as f64;
            metrics.avg_duration_ms = 
                (metrics.avg_duration_ms * (total - 1.0) + duration.as_millis() as f64) / total;
        }
        
        result
    }
}

impl FusionController {
    pub fn new() -> Self {
        Self {
            fusion_groups: HashMap::new(),
            fusion_opportunities: HashMap::new(),
            fusion_benefits: HashMap::new(),
            active_fusions: RwLock::new(HashMap::new()),
        }
    }
    
    pub async fn initialize_fusion_groups(&self) -> Result<()> {
        // This would analyze stage dependencies and create fusion opportunities
        // Simplified implementation
        info!("Initialized fusion controller with stage analysis");
        Ok(())
    }
    
    pub async fn create_fusion_plan(&self, execution_id: ExecutionId) -> Result<FusionPlan> {
        // Simplified fusion planning
        Ok(FusionPlan {
            fusion_groups: vec![
                FusionGroup {
                    id: 1,
                    stages: vec![PipelineStage::QueryAnalysis],
                    fusion_strategy: FusionStrategy::Pipeline,
                    expected_speedup: 1.2,
                    memory_sharing: true,
                },
                FusionGroup {
                    id: 2,
                    stages: vec![PipelineStage::LspRouting, PipelineStage::ParallelSearch],
                    fusion_strategy: FusionStrategy::TaskParallel,
                    expected_speedup: 1.8,
                    memory_sharing: false,
                },
                FusionGroup {
                    id: 3,
                    stages: vec![PipelineStage::ResultFusion, PipelineStage::PostProcess],
                    fusion_strategy: FusionStrategy::Pipeline,
                    expected_speedup: 1.1,
                    memory_sharing: true,
                },
            ],
            independent_stages: vec![],
            expected_speedup: 1.5,
        })
    }
}

impl MemoryBudget {
    pub fn new(total_mb: f64) -> Self {
        Self {
            total_mb,
            allocated_mb: 0.0,
            stage_allocations: HashMap::new(),
            fusion_savings: 0.0,
        }
    }
}

impl CpuBudget {
    pub fn new(total_time_ms: f64) -> Self {
        Self {
            total_time_ms,
            allocated_time_ms: 0.0,
            stage_allocations: HashMap::new(),
            overlap_savings: 0.0,
        }
    }
}

// Placeholder implementations for enhanced stages
#[async_trait::async_trait]
impl PipelineStageProcessor for EnhancedQueryAnalysisStage {
    async fn process(&self, context: &PipelineContext, data: PipelineData) -> Result<PipelineData, PipelineError> {
        // Enhanced query analysis with learning model integration
        debug!("Enhanced query analysis for: {}", context.query);
        Ok(data)
    }
    
    fn stage_id(&self) -> PipelineStage {
        PipelineStage::QueryAnalysis
    }
    
    fn supports_fusion(&self) -> bool {
        true
    }
}

#[async_trait::async_trait]
impl PipelineStageProcessor for EnhancedLspRoutingStage {
    async fn process(&self, context: &PipelineContext, data: PipelineData) -> Result<PipelineData, PipelineError> {
        // Enhanced LSP routing with prefetch integration
        debug!("Enhanced LSP routing for: {}", context.query);
        Ok(data)
    }
    
    fn stage_id(&self) -> PipelineStage {
        PipelineStage::LspRouting
    }
    
    fn supports_fusion(&self) -> bool {
        true
    }
}

#[async_trait::async_trait]
impl PipelineStageProcessor for EnhancedParallelSearchStage {
    async fn process(&self, context: &PipelineContext, data: PipelineData) -> Result<PipelineData, PipelineError> {
        // Enhanced parallel search with cross-shard stopping
        debug!("Enhanced parallel search for: {}", context.query);
        Ok(data)
    }
    
    fn stage_id(&self) -> PipelineStage {
        PipelineStage::ParallelSearch
    }
    
    fn supports_fusion(&self) -> bool {
        false // Complex search should not be fused
    }
}

#[async_trait::async_trait]
impl PipelineStageProcessor for EnhancedResultFusionStage {
    async fn process(&self, _context: &PipelineContext, data: PipelineData) -> Result<PipelineData, PipelineError> {
        // Enhanced result fusion with zero-copy operations
        debug!("Enhanced result fusion");
        Ok(data)
    }
    
    fn stage_id(&self) -> PipelineStage {
        PipelineStage::ResultFusion
    }
    
    fn supports_fusion(&self) -> bool {
        true
    }
}

#[async_trait::async_trait]
impl PipelineStageProcessor for EnhancedPostProcessStage {
    async fn process(&self, _context: &PipelineContext, data: PipelineData) -> Result<PipelineData, PipelineError> {
        // Enhanced post-processing with learning feedback
        debug!("Enhanced post-processing");
        Ok(data)
    }
    
    fn stage_id(&self) -> PipelineStage {
        PipelineStage::PostProcess
    }
    
    fn supports_fusion(&self) -> bool {
        true
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::pipeline::{PipelineContext, PipelineData, PipelineStage, PipelineConfig};
    use std::sync::atomic::{AtomicU64, Ordering};
    use tokio::time::{sleep, Duration};

    // Helper to create test pipeline config
    fn create_test_config() -> PipelineConfig {
        PipelineConfig {
            max_concurrent: 4,
            max_latency_ms: 5000,
            enable_parallelism: true,
            enable_fusion: true,
            enable_prefetch: true,
            enable_learning: true,
            fusion_threshold: 0.1,
            memory_limit_mb: 1024,
            cpu_budget_ms: 1000,
        }
    }

    // Helper to create test pipeline context
    fn create_test_context() -> PipelineContext {
        PipelineContext {
            query: "test query".to_string(),
            execution_id: 12345,
            timeout: Duration::from_millis(5000),
            enable_caching: true,
            debug_mode: false,
            max_results: 100,
            metadata: std::collections::HashMap::new(),
        }
    }

    // Helper to create test pipeline data
    fn create_test_data() -> PipelineData {
        PipelineData {
            query_analysis: None,
            lsp_results: Vec::new(),
            search_results: Vec::new(),
            fused_results: Vec::new(),
            final_results: Vec::new(),
            metadata: std::collections::HashMap::new(),
            stage_timings: std::collections::HashMap::new(),
            memory_usage: 0,
        }
    }

    #[tokio::test]
    async fn test_fusion_controller_initialization() {
        let controller = FusionController::new();
        assert!(controller.fusion_groups.is_empty());
        assert!(controller.fusion_opportunities.is_empty());
        assert!(controller.fusion_benefits.is_empty());
    }

    #[tokio::test]
    async fn test_fusion_group_creation() {
        let group = FusionGroup {
            id: 1,
            stages: vec![PipelineStage::QueryAnalysis, PipelineStage::LspRouting],
            fusion_strategy: FusionStrategy::Pipeline,
            expected_speedup: 1.2,
            memory_sharing: true,
        };

        assert_eq!(group.id, 1);
        assert_eq!(group.stages.len(), 2);
        assert_eq!(group.fusion_strategy, FusionStrategy::Pipeline);
        assert!(group.memory_sharing);
    }

    #[tokio::test]
    async fn test_fusion_strategy_types() {
        let strategies = vec![
            FusionStrategy::Pipeline,
            FusionStrategy::DataParallel,
            FusionStrategy::TaskParallel,
            FusionStrategy::Hybrid,
        ];

        for strategy in strategies {
            match strategy {
                FusionStrategy::Pipeline => assert!(true),
                FusionStrategy::DataParallel => assert!(true),
                FusionStrategy::TaskParallel => assert!(true),
                FusionStrategy::Hybrid => assert!(true),
            }
        }
    }

    #[tokio::test]
    async fn test_fusion_benefit_calculation() {
        let benefit = FusionBenefit {
            latency_reduction: 0.3,
            memory_savings: 0.2,
            cpu_efficiency: 0.4,
            cache_locality: 0.1,
            total_score: 1.0,
        };

        assert_eq!(benefit.latency_reduction, 0.3);
        assert_eq!(benefit.memory_savings, 0.2);
        assert_eq!(benefit.cpu_efficiency, 0.4);
        assert_eq!(benefit.cache_locality, 0.1);
        assert_eq!(benefit.total_score, 1.0);
    }

    #[tokio::test]
    async fn test_resource_budget_memory() {
        let budget = MemoryBudget::new(1024.0);
        
        assert_eq!(budget.total_mb, 1024.0);
        assert_eq!(budget.allocated_mb, 0.0);
        assert_eq!(budget.fusion_savings, 0.0);
        assert!(budget.stage_allocations.is_empty());

        // Test that we can create multiple budgets
        let budget2 = MemoryBudget::new(512.0);
        assert_eq!(budget2.total_mb, 512.0);
    }

    #[tokio::test]
    async fn test_resource_budget_cpu() {
        let budget = CpuBudget::new(1000.0);
        
        assert_eq!(budget.total_time_ms, 1000.0);
        assert_eq!(budget.allocated_time_ms, 0.0);
        assert_eq!(budget.overlap_savings, 0.0);
        assert!(budget.stage_allocations.is_empty());

        // Test that we can create multiple budgets
        let budget2 = CpuBudget::new(500.0);
        assert_eq!(budget2.total_time_ms, 500.0);
    }

    #[tokio::test]
    async fn test_enhanced_stages_creation() {
        // Test EnhancedResultFusionStage
        let fusion_stage = EnhancedResultFusionStage::new();
        assert_eq!(fusion_stage.stage_id(), PipelineStage::ResultFusion);
        assert!(fusion_stage.supports_fusion());

        // Test EnhancedPostProcessStage
        let post_stage = EnhancedPostProcessStage::new();
        assert_eq!(post_stage.stage_id(), PipelineStage::PostProcess);
        assert!(post_stage.supports_fusion());
    }

    #[tokio::test]
    async fn test_stage_processing() {
        let context = create_test_context();
        let data = create_test_data();

        // Test result fusion stage processing
        let fusion_stage = EnhancedResultFusionStage::new();
        let result = fusion_stage.process(&context, data.clone()).await;
        assert!(result.is_ok());

        // Test post process stage processing
        let post_stage = EnhancedPostProcessStage::new();
        let result = post_stage.process(&context, data.clone()).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_stage_dependencies() {
        // Test that stages have correct dependencies
        let mut dependencies = HashSet::new();
        dependencies.insert(PipelineStage::QueryAnalysis);

        // LSP should depend on QueryAnalysis
        assert!(dependencies.contains(&PipelineStage::QueryAnalysis));

        // Search should depend on LSP
        let mut search_deps = HashSet::new();
        search_deps.insert(PipelineStage::LspRouting);
        assert!(search_deps.contains(&PipelineStage::LspRouting));
    }

    #[tokio::test]
    async fn test_concurrent_stage_execution() {
        // Test concurrent execution patterns
        let counter = Arc::new(AtomicU64::new(0));
        let tasks: Vec<_> = (0..4).map(|i| {
            let counter_clone = counter.clone();
            tokio::spawn(async move {
                // Simulate stage processing
                sleep(Duration::from_millis(10)).await;
                counter_clone.fetch_add(1, Ordering::SeqCst);
                i
            })
        }).collect();

        let results: Vec<_> = futures::future::join_all(tasks).await;
        
        assert_eq!(results.len(), 4);
        assert_eq!(counter.load(Ordering::SeqCst), 4);
        
        for (i, result) in results.into_iter().enumerate() {
            assert_eq!(result.unwrap(), i);
        }
    }

    #[tokio::test]
    async fn test_pipeline_error_handling() {
        // Test error propagation in pipeline stages
        let context = create_test_context();
        let data = create_test_data();

        // Create a stage that might fail (test the error handling path)
        let fusion_stage = EnhancedResultFusionStage::new();
        
        // Normal processing should work
        let result = fusion_stage.process(&context, data).await;
        assert!(result.is_ok());

        // Test the stage identification
        assert_eq!(fusion_stage.stage_id(), PipelineStage::ResultFusion);
    }

    #[tokio::test]
    async fn test_execution_id_generation() {
        // Test that execution IDs are properly typed
        let id1: ExecutionId = 123456;
        let id2: ExecutionId = 789012;
        
        assert_ne!(id1, id2);
        assert!(id1 > 0);
        assert!(id2 > 0);
    }

    #[tokio::test]
    async fn test_fusion_group_id_generation() {
        // Test that fusion group IDs are properly typed
        let group1: FusionGroupId = 1;
        let group2: FusionGroupId = 2;
        
        assert_ne!(group1, group2);
        assert!(group1 > 0);
        assert!(group2 > 0);
    }

    // Performance-focused tests
    #[tokio::test]
    async fn test_stage_performance_metrics() {
        let start = std::time::Instant::now();
        
        // Simulate stage execution
        let context = create_test_context();
        let data = create_test_data();
        let post_stage = EnhancedPostProcessStage::new();
        let _result = post_stage.process(&context, data).await;
        
        let duration = start.elapsed();
        
        // Ensure processing is reasonably fast (should be much less than 1 second for this simple test)
        assert!(duration < Duration::from_secs(1));
    }

    #[tokio::test]
    async fn test_parallel_execution_metrics() {
        let metrics = ParallelExecutionMetrics {
            total_executions: 10,
            successful_executions: 9,
            failed_executions: 1,
            average_latency_ms: 150.5,
            p95_latency_ms: 200.0,
            p99_latency_ms: 250.0,
            average_parallelism: 3.2,
            fusion_utilization: 0.8,
            memory_efficiency: 0.9,
            cpu_utilization: 0.75,
            stage_metrics: std::collections::HashMap::new(),
        };

        assert_eq!(metrics.total_executions, 10);
        assert_eq!(metrics.successful_executions, 9);
        assert_eq!(metrics.failed_executions, 1);
        assert_eq!(metrics.average_latency_ms, 150.5);
        assert_eq!(metrics.fusion_utilization, 0.8);
    }

    #[tokio::test]
    async fn test_stage_metrics() {
        let stage_metrics = StageMetrics {
            executions: 5,
            average_duration_ms: 100.0,
            p95_duration_ms: 150.0,
            success_rate: 0.9,
            parallelism_factor: 2.5,
            memory_peak_mb: 256.0,
            cpu_utilization: 0.8,
            fusion_participations: 3,
        };

        assert_eq!(stage_metrics.executions, 5);
        assert_eq!(stage_metrics.average_duration_ms, 100.0);
        assert_eq!(stage_metrics.success_rate, 0.9);
        assert_eq!(stage_metrics.fusion_participations, 3);
    }
}