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
// Legacy Path Cleanup and CI Enforcement System
// Removes technical debt and enforces "shared binning core only" architecture

use std::collections::HashMap;
use std::path::Path;
use serde::{Deserialize, Serialize};

/// Legacy components detected in the codebase
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LegacyComponent {
    pub file_path: String,
    pub component_type: LegacyComponentType,
    pub lines: Vec<u32>,
    pub risk_level: RiskLevel,
    pub migration_status: MigrationStatus,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
pub enum LegacyComponentType {
    SimulatorHook,
    AlternateEceEvaluator,
    DuplicatedBinning,
    ObsoleteCalibratorInterface,
    HardcodedTestData,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum RiskLevel {
    Critical,  // Blocks CI immediately
    High,      // Must be fixed before next release
    Medium,    // Should be fixed in current sprint
    Low,       // Technical debt to address
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum MigrationStatus {
    NotStarted,
    InProgress,
    Completed,
    Verified,
}

/// Comprehensive cleanup report
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CleanupReport {
    pub total_legacy_components: usize,
    pub critical_issues: usize,
    pub high_priority_issues: usize,
    pub cleanup_completed: usize,
    pub migration_progress: f64,
    pub components: Vec<LegacyComponent>,
    pub blocked_patterns: Vec<String>,
    pub ci_enforcement_result: CiEnforcementResult,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CiEnforcementResult {
    pub passed: bool,
    pub blocking_issues: Vec<String>,
    pub warnings: Vec<String>,
    pub cleanup_recommendations: Vec<String>,
}

/// Legacy cleanup and enforcement system
pub struct LegacyCleanupSystem {
    legacy_patterns: HashMap<LegacyComponentType, Vec<String>>,
    blocked_imports: Vec<String>,
    required_migrations: Vec<MigrationRule>,
}

impl LegacyCleanupSystem {
    pub fn new() -> Self {
        let mut legacy_patterns = HashMap::new();
        
        // Define legacy patterns to detect and remove
        legacy_patterns.insert(LegacyComponentType::SimulatorHook, vec![
            "legacy_simulator".to_string(),
            "old_calibration_hook".to_string(),
            "deprecated_simulator_interface".to_string(),
            "SimulatorCompat".to_string(),
            "legacy_calibration_adapter".to_string(),
        ]);

        legacy_patterns.insert(LegacyComponentType::AlternateEceEvaluator, vec![
            "AlternateECE".to_string(),
            "custom_ece_impl".to_string(),
            "non_standard_ece".to_string(),
            "legacy_ece_calculator".to_string(),
            "experimental_ece".to_string(),
        ]);

        legacy_patterns.insert(LegacyComponentType::DuplicatedBinning, vec![
            "duplicate_binning".to_string(),
            "custom_bin_logic".to_string(),
            "alternative_binning".to_string(),
            "binning_override".to_string(),
            "local_bin_implementation".to_string(),
        ]);

        legacy_patterns.insert(LegacyComponentType::ObsoleteCalibratorInterface, vec![
            "OldCalibrator".to_string(),
            "LegacyCalibrationInterface".to_string(),
            "deprecated_calibrator".to_string(),
            "compat_calibrator".to_string(),
        ]);

        legacy_patterns.insert(LegacyComponentType::HardcodedTestData, vec![
            "hardcoded_test_data".to_string(),
            "static_calibration_data".to_string(),
            "embedded_test_cases".to_string(),
            "inline_test_predictions".to_string(),
        ]);

        let blocked_imports = vec![
            "legacy_calibration::*".to_string(),
            "deprecated_ece::*".to_string(),
            "old_binning::*".to_string(),
            "simulator_compat::*".to_string(),
            "experimental::calibration::*".to_string(),
        ];

        let required_migrations = vec![
            MigrationRule {
                from_pattern: "legacy_simulator_hook".to_string(),
                to_pattern: "shared_binning_core".to_string(),
                migration_type: MigrationType::Replace,
                verification_test: "test_shared_binning_core".to_string(),
            },
            MigrationRule {
                from_pattern: "custom_ece_impl".to_string(),
                to_pattern: "standard_ece_calculation".to_string(),
                migration_type: MigrationType::Replace,
                verification_test: "test_standard_ece_consistency".to_string(),
            },
            MigrationRule {
                from_pattern: "duplicate_binning_logic".to_string(),
                to_pattern: "centralized_binning_core".to_string(),
                migration_type: MigrationType::Consolidate,
                verification_test: "test_binning_consistency".to_string(),
            },
        ];

        Self {
            legacy_patterns,
            blocked_imports,
            required_migrations,
        }
    }

    /// Comprehensive scan for legacy components
    pub fn scan_codebase<P: AsRef<Path>>(&self, root_path: P) -> Result<CleanupReport, std::io::Error> {
        let mut components = Vec::new();
        let mut critical_issues = 0;
        let mut high_priority_issues = 0;
        let mut cleanup_completed = 0;

        // Scan Rust source files
        self.scan_directory(&root_path, &mut components)?;

        // Analyze risk levels and migration status
        for component in &mut components {
            self.assess_component_risk(component);
            self.check_migration_status(component);
            
            match component.risk_level {
                RiskLevel::Critical => critical_issues += 1,
                RiskLevel::High => high_priority_issues += 1,
                _ => {}
            }

            if component.migration_status == MigrationStatus::Completed {
                cleanup_completed += 1;
            }
        }

        let migration_progress = if !components.is_empty() {
            cleanup_completed as f64 / components.len() as f64 * 100.0
        } else {
            100.0
        };

        // Generate CI enforcement result
        let ci_enforcement_result = self.enforce_ci_requirements(&components);

        Ok(CleanupReport {
            total_legacy_components: components.len(),
            critical_issues,
            high_priority_issues,
            cleanup_completed,
            migration_progress,
            components,
            blocked_patterns: self.get_blocked_patterns(),
            ci_enforcement_result,
        })
    }

    /// Enforce CI requirements for clean architecture
    fn enforce_ci_requirements(&self, components: &[LegacyComponent]) -> CiEnforcementResult {
        let mut blocking_issues = Vec::new();
        let mut warnings = Vec::new();
        let mut cleanup_recommendations = Vec::new();

        // Check for critical blocking issues
        let critical_components: Vec<_> = components.iter()
            .filter(|c| matches!(c.risk_level, RiskLevel::Critical))
            .collect();

        if !critical_components.is_empty() {
            blocking_issues.push(format!(
                "CRITICAL: {} legacy components must be removed before release",
                critical_components.len()
            ));

            for component in &critical_components {
                blocking_issues.push(format!(
                    "  - {:?} in {}: lines {:?}",
                    component.component_type,
                    component.file_path,
                    component.lines
                ));
            }
        }

        // Check for high priority issues
        let high_priority_components: Vec<_> = components.iter()
            .filter(|c| matches!(c.risk_level, RiskLevel::High))
            .collect();

        if !high_priority_components.is_empty() {
            warnings.push(format!(
                "HIGH PRIORITY: {} legacy components should be addressed",
                high_priority_components.len()
            ));
        }

        // Generate cleanup recommendations
        let not_started_components: Vec<_> = components.iter()
            .filter(|c| matches!(c.migration_status, MigrationStatus::NotStarted))
            .collect();

        if !not_started_components.is_empty() {
            cleanup_recommendations.push("Start migration of unaddressed legacy components".to_string());
            cleanup_recommendations.push("Implement shared binning core pattern consistently".to_string());
            cleanup_recommendations.push("Remove all alternate ECE evaluators".to_string());
            cleanup_recommendations.push("Consolidate duplicated calibration logic".to_string());
        }

        // Enforce "shared binning core only" architecture
        let shared_core_violations = self.detect_shared_core_violations(components);
        if !shared_core_violations.is_empty() {
            blocking_issues.extend(shared_core_violations);
        }

        let passed = blocking_issues.is_empty();

        CiEnforcementResult {
            passed,
            blocking_issues,
            warnings,
            cleanup_recommendations,
        }
    }

    /// Detect violations of "shared binning core only" architecture
    fn detect_shared_core_violations(&self, components: &[LegacyComponent]) -> Vec<String> {
        let mut violations = Vec::new();

        // Check for duplicate binning implementations
        let duplicate_binning = components.iter()
            .filter(|c| matches!(c.component_type, LegacyComponentType::DuplicatedBinning))
            .count();

        if duplicate_binning > 0 {
            violations.push(format!(
                "ARCHITECTURE VIOLATION: {} duplicate binning implementations found (shared core only allowed)",
                duplicate_binning
            ));
        }

        // Check for alternate ECE evaluators
        let alternate_ece = components.iter()
            .filter(|c| matches!(c.component_type, LegacyComponentType::AlternateEceEvaluator))
            .count();

        if alternate_ece > 0 {
            violations.push(format!(
                "ARCHITECTURE VIOLATION: {} alternate ECE evaluators found (standard implementation only)",
                alternate_ece
            ));
        }

        violations
    }

    /// Remove legacy components with safety validation
    pub fn remove_legacy_components(&self, components: &[LegacyComponent]) -> Result<RemovalReport, String> {
        let mut removal_report = RemovalReport {
            attempted_removals: 0,
            successful_removals: 0,
            failed_removals: Vec::new(),
            safety_validations: Vec::new(),
        };

        for component in components {
            if matches!(component.migration_status, MigrationStatus::Completed) {
                continue; // Already handled
            }

            removal_report.attempted_removals += 1;

            // Safety validation before removal
            if let Err(safety_issue) = self.validate_removal_safety(component) {
                removal_report.failed_removals.push(RemovalFailure {
                    component: component.clone(),
                    reason: safety_issue,
                });
                continue;
            }

            // Attempt removal
            match self.execute_component_removal(component) {
                Ok(validation) => {
                    removal_report.successful_removals += 1;
                    removal_report.safety_validations.push(validation);
                }
                Err(error) => {
                    removal_report.failed_removals.push(RemovalFailure {
                        component: component.clone(),
                        reason: error,
                    });
                }
            }
        }

        Ok(removal_report)
    }

    /// Validate migration path completeness
    pub fn validate_migration_paths(&self) -> MigrationPathReport {
        let mut report = MigrationPathReport {
            total_migration_rules: self.required_migrations.len(),
            completed_migrations: 0,
            pending_migrations: Vec::new(),
            failed_validations: Vec::new(),
        };

        for migration_rule in &self.required_migrations {
            match self.validate_migration_rule(migration_rule) {
                Ok(true) => {
                    report.completed_migrations += 1;
                }
                Ok(false) => {
                    report.pending_migrations.push(migration_rule.clone());
                }
                Err(error) => {
                    report.failed_validations.push(MigrationValidationFailure {
                        rule: migration_rule.clone(),
                        error,
                    });
                }
            }
        }

        report
    }

    /// Generate comprehensive CI report
    pub fn generate_ci_report(&self, cleanup_report: &CleanupReport) -> String {
        let status = if cleanup_report.ci_enforcement_result.passed {
            "PASSED"
        } else {
            "FAILED"
        };

        format!(
            r#"
# Legacy Cleanup and CI Enforcement Report

## Status: {}

### Legacy Component Summary
- Total Legacy Components: {}
- Critical Issues: {} 
- High Priority Issues: {}
- Migration Progress: {:.1}%

### CI Enforcement Results
{}

### Blocking Issues
{}

### Warnings
{}

### Cleanup Recommendations
{}

### Architecture Compliance
- Shared Binning Core Only: {}
- Standard ECE Implementation: {}
- No Duplicate Logic: {}

### Next Steps
{}
"#,
            status,
            cleanup_report.total_legacy_components,
            cleanup_report.critical_issues,
            cleanup_report.high_priority_issues,
            cleanup_report.migration_progress,
            if cleanup_report.ci_enforcement_result.passed {
                "✅ All requirements satisfied"
            } else {
                "❌ Requirements violated - blocking release"
            },
            format_string_list(&cleanup_report.ci_enforcement_result.blocking_issues),
            format_string_list(&cleanup_report.ci_enforcement_result.warnings),
            format_string_list(&cleanup_report.ci_enforcement_result.cleanup_recommendations),
            if cleanup_report.ci_enforcement_result.blocking_issues.iter()
                .any(|issue| issue.contains("duplicate binning")) { "" } else { "" },
            if cleanup_report.ci_enforcement_result.blocking_issues.iter()
                .any(|issue| issue.contains("alternate ECE")) { "" } else { "" },
            if cleanup_report.ci_enforcement_result.blocking_issues.iter()
                .any(|issue| issue.contains("duplicate")) { "" } else { "" },
            if cleanup_report.ci_enforcement_result.passed {
                "Ready for release - no blocking legacy issues"
            } else {
                "Address all blocking issues before proceeding with release"
            }
        )
    }

    // Private helper methods

    fn scan_directory<P: AsRef<Path>>(&self, path: P, components: &mut Vec<LegacyComponent>) -> Result<(), std::io::Error> {
        use std::fs;
        use std::io;
        
        let path = path.as_ref();
        if !path.exists() || !path.is_dir() {
            return Ok(()); // Silent return for non-existent directories
        }
        
        fn scan_recursive(
            dir: &Path,
            patterns: &HashMap<LegacyComponentType, Vec<String>>,
            components: &mut Vec<LegacyComponent>
        ) -> io::Result<()> {
            for entry in fs::read_dir(dir)? {
                let entry = entry?;
                let path = entry.path();
                
                if path.is_dir() {
                    scan_recursive(&path, patterns, components)?;
                } else if path.extension().and_then(|s| s.to_str()) == Some("rs") {
                    // Read file content for pattern matching
                    if let Ok(content) = fs::read_to_string(&path) {
                        scan_file_content(&path, &content, patterns, components);
                    }
                }
            }
            Ok(())
        }
        
        fn scan_file_content(
            file_path: &Path,
            content: &str,
            patterns: &HashMap<LegacyComponentType, Vec<String>>,
            components: &mut Vec<LegacyComponent>
        ) {
            for (component_type, pattern_list) in patterns {
                for pattern in pattern_list {
                    let mut lines = Vec::new();
                    for (line_num, line) in content.lines().enumerate() {
                        if line.contains(pattern) {
                            lines.push((line_num + 1) as u32);
                        }
                    }
                    
                    if !lines.is_empty() {
                        components.push(LegacyComponent {
                            file_path: file_path.to_string_lossy().to_string(),
                            component_type: component_type.clone(),
                            lines,
                            risk_level: RiskLevel::Low, // Will be assessed later
                            migration_status: MigrationStatus::NotStarted,
                        });
                    }
                }
            }
        }
        
        scan_recursive(path, &self.legacy_patterns, components)?;
        Ok(())
    }

    fn assess_component_risk(&self, component: &mut LegacyComponent) {
        component.risk_level = match component.component_type {
            LegacyComponentType::SimulatorHook => RiskLevel::Critical,
            LegacyComponentType::AlternateEceEvaluator => RiskLevel::Critical,
            LegacyComponentType::DuplicatedBinning => RiskLevel::High,
            LegacyComponentType::ObsoleteCalibratorInterface => RiskLevel::Medium,
            LegacyComponentType::HardcodedTestData => RiskLevel::Low,
        };
    }

    fn check_migration_status(&self, component: &mut LegacyComponent) {
        // Mock implementation - would check actual migration status
        component.migration_status = MigrationStatus::NotStarted;
    }

    fn get_blocked_patterns(&self) -> Vec<String> {
        self.blocked_imports.clone()
    }

    fn validate_removal_safety(&self, _component: &LegacyComponent) -> Result<(), String> {
        // Mock implementation - would validate dependencies
        Ok(())
    }

    fn execute_component_removal(&self, _component: &LegacyComponent) -> Result<SafetyValidation, String> {
        // Mock implementation - would perform actual removal
        Ok(SafetyValidation {
            component_path: "mock_path".to_string(),
            tests_passed: true,
            dependencies_resolved: true,
        })
    }

    fn validate_migration_rule(&self, _rule: &MigrationRule) -> Result<bool, String> {
        // Mock implementation - would validate migration
        Ok(false)
    }
}

// Supporting types

#[derive(Debug, Clone, Serialize, Deserialize)]
struct MigrationRule {
    from_pattern: String,
    to_pattern: String,
    migration_type: MigrationType,
    verification_test: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
enum MigrationType {
    Replace,
    Consolidate,
    Remove,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RemovalReport {
    pub attempted_removals: usize,
    pub successful_removals: usize,
    pub failed_removals: Vec<RemovalFailure>,
    pub safety_validations: Vec<SafetyValidation>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RemovalFailure {
    pub component: LegacyComponent,
    pub reason: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SafetyValidation {
    pub component_path: String,
    pub tests_passed: bool,
    pub dependencies_resolved: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MigrationPathReport {
    pub total_migration_rules: usize,
    pub completed_migrations: usize,
    pub pending_migrations: Vec<MigrationRule>,
    pub failed_validations: Vec<MigrationValidationFailure>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MigrationValidationFailure {
    pub rule: MigrationRule,
    pub error: String,
}

fn format_string_list(items: &[String]) -> String {
    if items.is_empty() {
        "None".to_string()
    } else {
        items.iter()
            .enumerate()
            .map(|(i, item)| format!("{}. {}", i + 1, item))
            .collect::<Vec<_>>()
            .join("\n")
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_legacy_cleanup_system_creation() {
        let system = LegacyCleanupSystem::new();
        assert!(!system.legacy_patterns.is_empty());
        assert!(!system.blocked_imports.is_empty());
        assert!(!system.required_migrations.is_empty());
    }

    #[test]
    fn test_ci_enforcement_with_critical_issues() {
        let system = LegacyCleanupSystem::new();
        let critical_component = LegacyComponent {
            file_path: "test.rs".to_string(),
            component_type: LegacyComponentType::SimulatorHook,
            lines: vec![10, 15, 20],
            risk_level: RiskLevel::Critical,
            migration_status: MigrationStatus::NotStarted,
        };

        let result = system.enforce_ci_requirements(&[critical_component]);
        assert!(!result.passed);
        assert!(!result.blocking_issues.is_empty());
    }

    #[test]
    fn test_shared_core_violation_detection() {
        let system = LegacyCleanupSystem::new();
        let duplicate_binning = LegacyComponent {
            file_path: "duplicated.rs".to_string(),
            component_type: LegacyComponentType::DuplicatedBinning,
            lines: vec![5],
            risk_level: RiskLevel::High,
            migration_status: MigrationStatus::NotStarted,
        };

        let violations = system.detect_shared_core_violations(&[duplicate_binning]);
        assert!(!violations.is_empty());
        assert!(violations[0].contains("ARCHITECTURE VIOLATION"));
    }
}