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
//! # Feature Flags & Progressive Rollout for Calibration V22
//!
//! Production-ready feature flag system enabling safe, progressive rollout of CALIB_V22
//! with comprehensive monitoring, automatic circuit breakers, and repository-based traffic
//! splitting for consistent user experience.
//!
//! Key features:
//! - Progressive rollout stages: 5% → 25% → 50% → 100%
//! - Repository bucket-based traffic splitting for user consistency
//! - Configuration fingerprinting for audit trails
//! - Auto-revert capability via SLA tripwires
//! - Integration with existing monitoring and circuit breakers

use crate::calibration::{
    CalibrationResult, CalibrationSample, Phase4Config,
    sla_tripwires::{SlaTripwires, SlaConfig, AutoRevertAction, RevertEvent},
    drift_monitor::{DriftMonitor, DriftThresholds, HealthStatus, AlertEvent},
    shared_binning_core::{SharedBinningCore, SharedBinningConfig},
};
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, hash_map::DefaultHasher};
use std::hash::{Hash, Hasher};
use std::sync::{Arc, RwLock, atomic::{AtomicU64, AtomicBool, Ordering}};
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
use chrono::{DateTime, Utc};
use anyhow::{Result, Context as AnyhowContext};
use tracing::{info, warn, error};

/// CALIB_V22 feature flag configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CalibV22Config {
    /// Feature flag enabled globally
    pub enabled: bool,
    /// Current rollout percentage (0-100)
    pub rollout_percentage: u8,
    /// Rollout stage name for tracking
    pub rollout_stage: String,
    /// Repository bucket strategy for traffic splitting
    pub bucket_strategy: BucketStrategy,
    /// SLA gate validation configuration
    pub sla_gates: SlaGateConfig,
    /// Auto-revert configuration
    pub auto_revert_config: AutoRevertConfig,
    /// Configuration fingerprint for attestation
    pub config_fingerprint: String,
    /// Rollout start timestamp
    pub rollout_start_time: DateTime<Utc>,
    /// Stage promotion criteria
    pub promotion_criteria: PromotionCriteria,
}

/// Repository bucket strategy for consistent traffic splitting
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BucketStrategy {
    /// Bucket assignment method
    pub method: BucketMethod,
    /// Salt for hash-based bucketing
    pub bucket_salt: String,
    /// Enable sticky sessions (user consistency)
    pub sticky_sessions: bool,
    /// Bucket override for testing
    pub override_buckets: HashMap<String, bool>,
}

/// Bucket assignment methods
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum BucketMethod {
    /// Hash-based bucketing using repository ID
    RepositoryHash { salt: String },
    /// Random assignment (not recommended for production)
    Random,
    /// Manual override for testing
    Manual { assignments: HashMap<String, bool> },
}

/// SLA gate configuration for production safety
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SlaGateConfig {
    /// Maximum allowed p99 latency increase (microseconds)
    pub max_p99_latency_increase_us: f64,
    /// Maximum allowed AECE-τ threshold
    pub max_aece_tau_threshold: f64,
    /// Maximum allowed confidence shift
    pub max_confidence_shift: f64,
    /// Require zero SLA-Recall@50 change
    pub require_zero_sla_recall_change: bool,
    /// Gate evaluation window (minutes)
    pub evaluation_window_minutes: u32,
    /// Consecutive breach threshold for auto-revert
    pub consecutive_breach_threshold: u32,
}

/// Auto-revert configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AutoRevertConfig {
    /// Enable automatic revert on SLA violations
    pub enabled: bool,
    /// Consecutive window breaches before revert
    pub breach_window_threshold: u32,
    /// Window duration for breach detection (minutes)
    pub breach_window_duration_minutes: u32,
    /// Cooldown period before re-enabling (minutes)
    pub revert_cooldown_minutes: u32,
    /// Maximum auto-reverts per day
    pub max_reverts_per_day: u32,
}

/// Stage promotion criteria
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PromotionCriteria {
    /// Minimum observation time at current stage (hours)
    pub min_observation_hours: u32,
    /// Required health status for promotion
    pub required_health_status: HealthStatus,
    /// Maximum allowed AECE degradation for promotion
    pub max_aece_degradation: f64,
    /// Required p99 latency compliance
    pub require_p99_compliance: bool,
    /// Minimum success rate for promotion (0.0-1.0)
    pub min_success_rate: f64,
}

/// Rollout stage definitions
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum RolloutStage {
    /// Initial canary: 5% traffic
    Canary,
    /// Limited rollout: 25% traffic
    Limited,
    /// Major rollout: 50% traffic
    Major,
    /// Full rollout: 100% traffic
    Full,
    /// Disabled due to issues
    Disabled,
}

impl RolloutStage {
    fn get_percentage(&self) -> u8 {
        match self {
            RolloutStage::Canary => 5,
            RolloutStage::Limited => 25,
            RolloutStage::Major => 50,
            RolloutStage::Full => 100,
            RolloutStage::Disabled => 0,
        }
    }
    
    fn next_stage(&self) -> Option<RolloutStage> {
        match self {
            RolloutStage::Canary => Some(RolloutStage::Limited),
            RolloutStage::Limited => Some(RolloutStage::Major),
            RolloutStage::Major => Some(RolloutStage::Full),
            RolloutStage::Full => None,
            RolloutStage::Disabled => Some(RolloutStage::Canary),
        }
    }
}

/// Feature flag decision result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FeatureFlagDecision {
    /// Whether CALIB_V22 should be used
    pub use_calib_v22: bool,
    /// Reason for the decision
    pub decision_reason: String,
    /// Repository bucket hash
    pub bucket_hash: u64,
    /// Current rollout stage
    pub rollout_stage: String,
    /// Configuration fingerprint
    pub config_fingerprint: String,
    /// Decision timestamp
    pub timestamp: DateTime<Utc>,
}

/// Stage transition event for monitoring
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StageTransition {
    pub timestamp: DateTime<Utc>,
    pub from_stage: String,
    pub to_stage: String,
    pub trigger_reason: String,
    pub metrics_snapshot: StageMetrics,
    pub config_fingerprint: String,
}

/// Metrics snapshot for stage transitions
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StageMetrics {
    pub aece_value: f64,
    pub p99_latency_us: f64,
    pub success_rate: f64,
    pub confidence_shift: f64,
    pub health_status: HealthStatus,
    pub observation_hours: f64,
    pub sample_count: u64,
}

/// CALIB_V22 feature flag system
pub struct CalibV22FeatureFlag {
    config: Arc<RwLock<CalibV22Config>>,
    sla_tripwires: Arc<RwLock<SlaTripwires>>,
    drift_monitor: Arc<RwLock<DriftMonitor>>,
    
    // State tracking
    current_stage: Arc<RwLock<RolloutStage>>,
    stage_start_time: Arc<RwLock<Instant>>,
    transition_history: Arc<RwLock<Vec<StageTransition>>>,
    
    // Metrics tracking
    v22_success_count: Arc<AtomicU64>,
    v22_failure_count: Arc<AtomicU64>,
    control_success_count: Arc<AtomicU64>,
    control_failure_count: Arc<AtomicU64>,
    
    // Circuit breaker state
    circuit_breaker_open: Arc<AtomicBool>,
    last_revert_time: Arc<RwLock<Option<Instant>>>,
    daily_revert_count: Arc<AtomicU64>,
    
    // Performance tracking
    recent_decisions: Arc<RwLock<Vec<FeatureFlagDecision>>>,
}

impl CalibV22FeatureFlag {
    /// Create new CALIB_V22 feature flag system
    pub fn new(
        config: CalibV22Config,
        sla_config: SlaConfig,
        drift_thresholds: DriftThresholds,
        binning_config: SharedBinningConfig,
    ) -> Result<Self> {
        // Validate configuration
        if config.rollout_percentage > 100 {
            anyhow::bail!("Invalid rollout percentage: {}", config.rollout_percentage);
        }
        
        // Create SLA tripwires
        let sla_tripwires = Arc::new(RwLock::new(
            SlaTripwires::new(sla_config, drift_thresholds.clone())
        ));
        
        // Create drift monitor (simplified initialization)
        let canary_gate_config = crate::calibration::drift_monitor::CanaryGateConfig::default();
        let bootstrap_config = crate::calibration::fast_bootstrap::FastBootstrapConfig::default();
        let drift_monitor = Arc::new(RwLock::new(
            DriftMonitor::new(drift_thresholds, canary_gate_config, binning_config, bootstrap_config)
        ));
        
        let current_stage = Self::stage_from_percentage(config.rollout_percentage);
        
        info!("🚀 Initializing CALIB_V22 feature flag system");
        info!("Initial stage: {:?} ({}%)", current_stage, config.rollout_percentage);
        info!("Config fingerprint: {}", config.config_fingerprint);
        
        Ok(Self {
            config: Arc::new(RwLock::new(config)),
            sla_tripwires,
            drift_monitor,
            current_stage: Arc::new(RwLock::new(current_stage)),
            stage_start_time: Arc::new(RwLock::new(Instant::now())),
            transition_history: Arc::new(RwLock::new(Vec::new())),
            v22_success_count: Arc::new(AtomicU64::new(0)),
            v22_failure_count: Arc::new(AtomicU64::new(0)),
            control_success_count: Arc::new(AtomicU64::new(0)),
            control_failure_count: Arc::new(AtomicU64::new(0)),
            circuit_breaker_open: Arc::new(AtomicBool::new(false)),
            last_revert_time: Arc::new(RwLock::new(None)),
            daily_revert_count: Arc::new(AtomicU64::new(0)),
            recent_decisions: Arc::new(RwLock::new(Vec::new())),
        })
    }
    
    /// Make feature flag decision for repository
    pub fn should_use_calib_v22(&self, repository_id: &str) -> Result<FeatureFlagDecision> {
        let config = self.config.read().unwrap();
        let current_stage = self.current_stage.read().unwrap();
        let now = Utc::now();
        
        // Check if feature flag is globally disabled
        if !config.enabled {
            let decision = FeatureFlagDecision {
                use_calib_v22: false,
                decision_reason: "Feature flag globally disabled".to_string(),
                bucket_hash: 0,
                rollout_stage: format!("{:?}", *current_stage),
                config_fingerprint: config.config_fingerprint.clone(),
                timestamp: now,
            };
            
            self.record_decision(&decision)?;
            return Ok(decision);
        }
        
        // Check circuit breaker
        if self.circuit_breaker_open.load(Ordering::Relaxed) {
            let decision = FeatureFlagDecision {
                use_calib_v22: false,
                decision_reason: "Circuit breaker open - auto-reverted".to_string(),
                bucket_hash: 0,
                rollout_stage: "Disabled".to_string(),
                config_fingerprint: config.config_fingerprint.clone(),
                timestamp: now,
            };
            
            self.record_decision(&decision)?;
            return Ok(decision);
        }
        
        // Compute repository bucket hash
        let bucket_hash = self.compute_repository_bucket(repository_id, &config.bucket_strategy)?;
        
        // Check bucket override
        if let Some(&override_enabled) = config.bucket_strategy.override_buckets.get(repository_id) {
            let decision = FeatureFlagDecision {
                use_calib_v22: override_enabled,
                decision_reason: format!("Bucket override: {}", override_enabled),
                bucket_hash,
                rollout_stage: format!("{:?}", *current_stage),
                config_fingerprint: config.config_fingerprint.clone(),
                timestamp: now,
            };
            
            self.record_decision(&decision)?;
            return Ok(decision);
        }
        
        // Determine if repository should use V22 based on rollout percentage
        let rollout_threshold = (config.rollout_percentage as f64 / 100.0 * u64::MAX as f64) as u64;
        let use_v22 = bucket_hash <= rollout_threshold;
        
        let decision = FeatureFlagDecision {
            use_calib_v22: use_v22,
            decision_reason: format!("Bucket assignment: hash={}, threshold={}, rollout={}%", 
                bucket_hash, rollout_threshold, config.rollout_percentage),
            bucket_hash,
            rollout_stage: format!("{:?}", *current_stage),
            config_fingerprint: config.config_fingerprint.clone(),
            timestamp: now,
        };
        
        self.record_decision(&decision)?;
        Ok(decision)
    }
    
    /// Record calibration result for monitoring
    pub fn record_calibration_result(&self, 
        use_v22: bool, 
        result: &CalibrationResult, 
        repository_id: &str
    ) -> Result<()> {
        // Record success/failure metrics
        if result.calibrated_score >= 0.0 && result.calibrated_score <= 1.0 && !result.calibrated_score.is_nan() {
            if use_v22 {
                self.v22_success_count.fetch_add(1, Ordering::Relaxed);
            } else {
                self.control_success_count.fetch_add(1, Ordering::Relaxed);
            }
        } else {
            if use_v22 {
                self.v22_failure_count.fetch_add(1, Ordering::Relaxed);
            } else {
                self.control_failure_count.fetch_add(1, Ordering::Relaxed);
            }
        }
        
        // Update SLA tripwires heartbeat
        {
            let tripwires = self.sla_tripwires.read().unwrap();
            tripwires.heartbeat();
        }
        
        // Check for stage promotion or auto-revert
        self.evaluate_stage_transition()?;
        
        Ok(())
    }
    
    /// Evaluate whether to promote to next stage or trigger auto-revert
    fn evaluate_stage_transition(&self) -> Result<()> {
        let config = self.config.read().unwrap();
        let current_stage = self.current_stage.read().unwrap();
        let stage_start_time = *self.stage_start_time.read().unwrap();
        
        // Check observation time requirement
        let observation_hours = stage_start_time.elapsed().as_secs() as f64 / 3600.0;
        if observation_hours < config.promotion_criteria.min_observation_hours as f64 {
            return Ok(()); // Too early to evaluate
        }
        
        // Compute current stage metrics
        let metrics = self.compute_current_stage_metrics()?;
        
        // Check auto-revert conditions first
        if self.should_auto_revert(&metrics)? {
            self.trigger_auto_revert("SLA gate violations detected".to_string(), &metrics)?;
            return Ok(());
        }
        
        // Check promotion criteria
        if self.should_promote_stage(&metrics, &config.promotion_criteria)? {
            if let Some(next_stage) = current_stage.next_stage() {
                self.promote_to_stage(next_stage, "Promotion criteria met".to_string(), &metrics)?;
            }
        }
        
        Ok(())
    }
    
    /// Compute current stage metrics
    fn compute_current_stage_metrics(&self) -> Result<StageMetrics> {
        let v22_success = self.v22_success_count.load(Ordering::Relaxed);
        let v22_failure = self.v22_failure_count.load(Ordering::Relaxed);
        let control_success = self.control_success_count.load(Ordering::Relaxed);
        let control_failure = self.control_failure_count.load(Ordering::Relaxed);
        
        let v22_total = v22_success + v22_failure;
        let control_total = control_success + control_failure;
        
        let v22_success_rate = if v22_total > 0 {
            v22_success as f64 / v22_total as f64
        } else {
            1.0
        };
        
        let control_success_rate = if control_total > 0 {
            control_success as f64 / control_total as f64
        } else {
            1.0
        };
        
        // Mock other metrics (would be computed from real monitoring data)
        let stage_start_time = *self.stage_start_time.read().unwrap();
        let observation_hours = stage_start_time.elapsed().as_secs() as f64 / 3600.0;
        
        Ok(StageMetrics {
            aece_value: 0.012, // Would be computed from drift monitor
            p99_latency_us: 850.0, // Would be from SLA tripwires
            success_rate: v22_success_rate,
            confidence_shift: (v22_success_rate - control_success_rate).abs(),
            health_status: HealthStatus::Green, // Would be from drift monitor
            observation_hours,
            sample_count: v22_total,
        })
    }
    
    /// Check if auto-revert should be triggered
    fn should_auto_revert(&self, metrics: &StageMetrics) -> Result<bool> {
        let config = self.config.read().unwrap();
        
        if !config.auto_revert_config.enabled {
            return Ok(false);
        }
        
        // Check SLA gate violations
        if metrics.p99_latency_us > config.sla_gates.max_p99_latency_increase_us {
            warn!("P99 latency violation: {:.0}μs > {:.0}μs", 
                metrics.p99_latency_us, config.sla_gates.max_p99_latency_increase_us);
            return Ok(true);
        }
        
        if metrics.aece_value > config.sla_gates.max_aece_tau_threshold {
            warn!("AECE-τ threshold violation: {:.4} > {:.4}", 
                metrics.aece_value, config.sla_gates.max_aece_tau_threshold);
            return Ok(true);
        }
        
        if metrics.confidence_shift > config.sla_gates.max_confidence_shift {
            warn!("Confidence shift violation: {:.4} > {:.4}", 
                metrics.confidence_shift, config.sla_gates.max_confidence_shift);
            return Ok(true);
        }
        
        // Check circuit breaker from SLA tripwires
        {
            let tripwires = self.sla_tripwires.read().unwrap();
            if tripwires.should_block_calibration() {
                warn!("SLA tripwires indicate calibration should be blocked");
                return Ok(true);
            }
        }
        
        Ok(false)
    }
    
    /// Check if stage should be promoted
    fn should_promote_stage(&self, metrics: &StageMetrics, criteria: &PromotionCriteria) -> Result<bool> {
        // Check minimum observation time
        if metrics.observation_hours < criteria.min_observation_hours as f64 {
            return Ok(false);
        }
        
        // Check health status
        if metrics.health_status != criteria.required_health_status {
            return Ok(false);
        }
        
        // Check AECE degradation
        if metrics.aece_value > criteria.max_aece_degradation {
            return Ok(false);
        }
        
        // Check success rate
        if metrics.success_rate < criteria.min_success_rate {
            return Ok(false);
        }
        
        // All criteria met
        Ok(true)
    }
    
    /// Trigger auto-revert to disable feature
    fn trigger_auto_revert(&self, reason: String, metrics: &StageMetrics) -> Result<()> {
        let config = self.config.read().unwrap();
        
        // Check daily revert limit
        let daily_reverts = self.daily_revert_count.load(Ordering::Relaxed);
        if daily_reverts >= config.auto_revert_config.max_reverts_per_day as u64 {
            warn!("Daily auto-revert limit reached: {}/{}", 
                daily_reverts, config.auto_revert_config.max_reverts_per_day);
            return Ok(());
        }
        
        // Set circuit breaker
        self.circuit_breaker_open.store(true, Ordering::Relaxed);
        
        // Record revert time
        {
            let mut last_revert = self.last_revert_time.write().unwrap();
            *last_revert = Some(Instant::now());
        }
        
        // Increment daily revert count
        self.daily_revert_count.fetch_add(1, Ordering::Relaxed);
        
        // Transition to disabled stage
        let current_stage = self.current_stage.read().unwrap().clone();
        let transition = StageTransition {
            timestamp: Utc::now(),
            from_stage: format!("{:?}", current_stage),
            to_stage: "Disabled".to_string(),
            trigger_reason: format!("AUTO-REVERT: {}", reason),
            metrics_snapshot: metrics.clone(),
            config_fingerprint: config.config_fingerprint.clone(),
        };
        
        // Update stage
        {
            let mut stage = self.current_stage.write().unwrap();
            *stage = RolloutStage::Disabled;
        }
        
        // Record transition
        {
            let mut history = self.transition_history.write().unwrap();
            history.push(transition.clone());
            
            // Keep only recent history
            let history_len = history.len();
            if history_len > 100 {
                history.drain(0..history_len - 100);
            }
        }
        
        error!("🚨 AUTO-REVERT TRIGGERED: {}", reason);
        error!("Transition: {} -> {}", transition.from_stage, transition.to_stage);
        error!("Metrics: AECE={:.4}, P99={:.0}μs, Success={:.2}%, Shift={:.4}",
            metrics.aece_value, metrics.p99_latency_us, metrics.success_rate * 100.0, metrics.confidence_shift);
        
        Ok(())
    }
    
    /// Promote to next rollout stage
    fn promote_to_stage(&self, next_stage: RolloutStage, reason: String, metrics: &StageMetrics) -> Result<()> {
        let config = self.config.read().unwrap();
        let current_stage = self.current_stage.read().unwrap().clone();
        
        let transition = StageTransition {
            timestamp: Utc::now(),
            from_stage: format!("{:?}", current_stage),
            to_stage: format!("{:?}", next_stage),
            trigger_reason: reason.clone(),
            metrics_snapshot: metrics.clone(),
            config_fingerprint: config.config_fingerprint.clone(),
        };
        
        // Update stage and reset timer
        {
            let mut stage = self.current_stage.write().unwrap();
            *stage = next_stage.clone();
        }
        {
            let mut start_time = self.stage_start_time.write().unwrap();
            *start_time = Instant::now();
        }
        
        // Record transition
        {
            let mut history = self.transition_history.write().unwrap();
            history.push(transition.clone());
            
            // Keep only recent history
            let history_len = history.len();
            if history_len > 100 {
                history.drain(0..history_len - 100);
            }
        }
        
        info!("🎯 STAGE PROMOTION: {}", reason);
        info!("Transition: {} -> {}", transition.from_stage, transition.to_stage);
        info!("New rollout percentage: {}%", next_stage.get_percentage());
        
        Ok(())
    }
    
    /// Compute repository bucket hash for consistent traffic splitting
    fn compute_repository_bucket(&self, repository_id: &str, strategy: &BucketStrategy) -> Result<u64> {
        match &strategy.method {
            BucketMethod::RepositoryHash { salt } => {
                let mut hasher = DefaultHasher::new();
                repository_id.hash(&mut hasher);
                salt.hash(&mut hasher);
                strategy.bucket_salt.hash(&mut hasher);
                Ok(hasher.finish())
            }
            BucketMethod::Random => {
                // For testing only - not deterministic
                let mut hasher = DefaultHasher::new();
                SystemTime::now().duration_since(UNIX_EPOCH)?.as_nanos().hash(&mut hasher);
                Ok(hasher.finish())
            }
            BucketMethod::Manual { assignments } => {
                if let Some(&enabled) = assignments.get(repository_id) {
                    Ok(if enabled { 0 } else { u64::MAX })
                } else {
                    Ok(u64::MAX) // Default to control group
                }
            }
        }
    }
    
    /// Record feature flag decision for monitoring
    fn record_decision(&self, decision: &FeatureFlagDecision) -> Result<()> {
        let mut recent_decisions = self.recent_decisions.write().unwrap();
        recent_decisions.push(decision.clone());
        
        // Keep only recent decisions (last 1000)
        let decisions_len = recent_decisions.len();
        if decisions_len > 1000 {
            recent_decisions.drain(0..decisions_len - 1000);
        }
        
        Ok(())
    }
    
    /// Get current feature flag status and metrics
    pub fn get_status(&self) -> Result<serde_json::Value> {
        let config = self.config.read().unwrap();
        let current_stage = self.current_stage.read().unwrap();
        let stage_start_time = *self.stage_start_time.read().unwrap();
        let transition_history = self.transition_history.read().unwrap();
        
        let v22_success = self.v22_success_count.load(Ordering::Relaxed);
        let v22_failure = self.v22_failure_count.load(Ordering::Relaxed);
        let control_success = self.control_success_count.load(Ordering::Relaxed);
        let control_failure = self.control_failure_count.load(Ordering::Relaxed);
        
        let v22_total = v22_success + v22_failure;
        let control_total = control_success + control_failure;
        
        let metrics = self.compute_current_stage_metrics()?;
        
        Ok(serde_json::json!({
            "enabled": config.enabled,
            "current_stage": format!("{:?}", *current_stage),
            "rollout_percentage": current_stage.get_percentage(),
            "stage_duration_hours": stage_start_time.elapsed().as_secs() as f64 / 3600.0,
            "circuit_breaker_open": self.circuit_breaker_open.load(Ordering::Relaxed),
            "daily_reverts": self.daily_revert_count.load(Ordering::Relaxed),
            "config_fingerprint": config.config_fingerprint,
            "metrics": {
                "v22_success": v22_success,
                "v22_failure": v22_failure,
                "v22_success_rate": if v22_total > 0 { v22_success as f64 / v22_total as f64 } else { 0.0 },
                "control_success": control_success,
                "control_failure": control_failure,
                "control_success_rate": if control_total > 0 { control_success as f64 / control_total as f64 } else { 0.0 },
                "current_stage_metrics": metrics
            },
            "transition_history_count": transition_history.len(),
            "sla_gates": config.sla_gates,
            "auto_revert_config": config.auto_revert_config,
        }))
    }
    
    /// Force stage transition (for admin/testing)
    pub fn force_stage_transition(&self, target_stage: RolloutStage, reason: String) -> Result<()> {
        let metrics = self.compute_current_stage_metrics()?;
        
        if target_stage == RolloutStage::Disabled {
            self.trigger_auto_revert(reason, &metrics)
        } else {
            self.promote_to_stage(target_stage, reason, &metrics)
        }
    }
    
    /// Reset circuit breaker (for recovery)
    pub fn reset_circuit_breaker(&self) -> Result<()> {
        self.circuit_breaker_open.store(false, Ordering::Relaxed);
        
        // Reset to canary stage for gradual recovery
        let metrics = self.compute_current_stage_metrics()?;
        self.promote_to_stage(
            RolloutStage::Canary, 
            "Circuit breaker reset - gradual recovery".to_string(), 
            &metrics
        )?;
        
        info!("🔄 Circuit breaker reset, returning to canary stage");
        Ok(())
    }
    
    /// Helper to determine stage from percentage
    fn stage_from_percentage(percentage: u8) -> RolloutStage {
        match percentage {
            0 => RolloutStage::Disabled,
            1..=5 => RolloutStage::Canary,
            6..=25 => RolloutStage::Limited,
            26..=50 => RolloutStage::Major,
            51..=100 => RolloutStage::Full,
            101..=u8::MAX => RolloutStage::Full, // Invalid values default to Full
        }
    }
}

impl Default for CalibV22Config {
    fn default() -> Self {
        Self {
            enabled: false, // Start disabled
            rollout_percentage: 0,
            rollout_stage: "Disabled".to_string(),
            bucket_strategy: BucketStrategy {
                method: BucketMethod::RepositoryHash { 
                    salt: "calib_v22_2025".to_string() 
                },
                bucket_salt: "production_rollout".to_string(),
                sticky_sessions: true,
                override_buckets: HashMap::new(),
            },
            sla_gates: SlaGateConfig {
                max_p99_latency_increase_us: 1000.0, // 1ms
                max_aece_tau_threshold: 0.01,
                max_confidence_shift: 0.02,
                require_zero_sla_recall_change: true,
                evaluation_window_minutes: 15,
                consecutive_breach_threshold: 2,
            },
            auto_revert_config: AutoRevertConfig {
                enabled: true,
                breach_window_threshold: 2,
                breach_window_duration_minutes: 15,
                revert_cooldown_minutes: 60,
                max_reverts_per_day: 5,
            },
            config_fingerprint: "default".to_string(),
            rollout_start_time: Utc::now(),
            promotion_criteria: PromotionCriteria {
                min_observation_hours: 4,
                required_health_status: HealthStatus::Green,
                max_aece_degradation: 0.005,
                require_p99_compliance: true,
                min_success_rate: 0.99,
            },
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::calibration::fast_bootstrap::FastBootstrapConfig;
    
    #[tokio::test]
    async fn test_feature_flag_creation() {
        let config = CalibV22Config::default();
        let sla_config = SlaConfig::default();
        let drift_thresholds = DriftThresholds::default();
        let binning_config = SharedBinningConfig::default();
        
        let feature_flag = CalibV22FeatureFlag::new(
            config, sla_config, drift_thresholds, binning_config
        );
        
        assert!(feature_flag.is_ok());
    }
    
    #[tokio::test]
    async fn test_repository_bucketing() {
        let config = CalibV22Config::default();
        let sla_config = SlaConfig::default();
        let drift_thresholds = DriftThresholds::default();
        let binning_config = SharedBinningConfig::default();
        
        let feature_flag = CalibV22FeatureFlag::new(
            config, sla_config, drift_thresholds, binning_config
        ).unwrap();
        
        // Test deterministic bucketing
        let decision1 = feature_flag.should_use_calib_v22("repo123").unwrap();
        let decision2 = feature_flag.should_use_calib_v22("repo123").unwrap();
        
        assert_eq!(decision1.bucket_hash, decision2.bucket_hash);
        assert_eq!(decision1.use_calib_v22, decision2.use_calib_v22);
    }
    
    #[tokio::test]
    async fn test_status_reporting() {
        let config = CalibV22Config::default();
        let sla_config = SlaConfig::default();
        let drift_thresholds = DriftThresholds::default();
        let binning_config = SharedBinningConfig::default();
        
        let feature_flag = CalibV22FeatureFlag::new(
            config, sla_config, drift_thresholds, binning_config
        ).unwrap();
        
        let status = feature_flag.get_status().unwrap();
        
        assert!(status["enabled"].is_boolean());
        assert!(status["current_stage"].is_string());
        assert!(status["metrics"].is_object());
    }
}