oris-evolution 0.3.4

Append-only evolution memory and pipeline for Oris EvoKernel.
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
//! Confidence Lifecycle Scheduler
//!
//! This module implements automatic confidence decay and lifecycle management
//! for genes and capsules within the evolution crate.

use std::collections::HashMap;

use serde::{Deserialize, Serialize};
use thiserror::Error;

use crate::core::{
    AssetState, Capsule, GeneId, MIN_REPLAY_CONFIDENCE, REPLAY_CONFIDENCE_DECAY_RATE_PER_HOUR,
};

/// Confidence scheduler configuration
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ConfidenceSchedulerConfig {
    /// How often to run the decay check (in seconds)
    pub check_interval_secs: u64,
    /// Maximum confidence boost per reuse success
    pub confidence_boost_per_success: f32,
    /// Maximum confidence (capped at 1.0)
    pub max_confidence: f32,
    /// Enable/disable the scheduler
    pub enabled: bool,
}

impl Default for ConfidenceSchedulerConfig {
    fn default() -> Self {
        Self {
            check_interval_secs: 3600, // 1 hour
            confidence_boost_per_success: 0.1,
            max_confidence: 1.0,
            enabled: true,
        }
    }
}

/// Confidence update action
#[derive(Clone, Debug)]
pub enum ConfidenceAction {
    /// Apply decay to a capsule
    DecayCapsule {
        capsule_id: String,
        gene_id: GeneId,
        old_confidence: f32,
        new_confidence: f32,
    },
    /// Demote asset to quarantined due to low confidence
    DemoteToQuarantined { asset_id: String, confidence: f32 },
    /// Boost confidence on successful reuse
    BoostConfidence {
        asset_id: String,
        old_confidence: f32,
        new_confidence: f32,
    },
}

/// Scheduler errors
#[derive(Error, Debug)]
pub enum SchedulerError {
    #[error("Scheduler not running")]
    NotRunning,

    #[error("IO error: {0}")]
    IoError(String),

    #[error("Store error: {0}")]
    StoreError(String),
}

/// Trait for confidence lifecycle management
pub trait ConfidenceScheduler: Send + Sync {
    /// Apply confidence decay to a single capsule
    fn apply_decay_to_capsule(&self, capsule_confidence: f32, age_hours: f32) -> f32;

    /// Boost confidence on successful reuse
    fn boost_confidence(&self, current: f32) -> f32;

    /// Check if confidence is below minimum threshold
    fn should_quarantine(&self, confidence: f32) -> bool;
}

/// Standard implementation of confidence scheduler
pub struct StandardConfidenceScheduler {
    config: ConfidenceSchedulerConfig,
}

impl StandardConfidenceScheduler {
    pub fn new(config: ConfidenceSchedulerConfig) -> Self {
        Self { config }
    }

    pub fn with_default_config() -> Self {
        Self::new(ConfidenceSchedulerConfig::default())
    }

    /// Calculate decayed confidence
    pub fn calculate_decay(confidence: f32, hours: f32) -> f32 {
        if confidence <= 0.0 {
            return 0.0;
        }
        let decay = (-REPLAY_CONFIDENCE_DECAY_RATE_PER_HOUR * hours).exp();
        (confidence * decay).clamp(0.0, 1.0)
    }

    /// Calculate age in hours from a timestamp
    pub fn calculate_age_hours(created_at_ms: i64, now_ms: i64) -> f32 {
        let diff_ms = now_ms - created_at_ms;
        let diff_secs = diff_ms / 1000;
        diff_secs as f32 / 3600.0
    }
}

impl ConfidenceScheduler for StandardConfidenceScheduler {
    fn apply_decay_to_capsule(&self, capsule_confidence: f32, age_hours: f32) -> f32 {
        Self::calculate_decay(capsule_confidence, age_hours)
    }

    fn boost_confidence(&self, current: f32) -> f32 {
        let new_confidence = current + self.config.confidence_boost_per_success;
        new_confidence.min(self.config.max_confidence)
    }

    fn should_quarantine(&self, confidence: f32) -> bool {
        confidence < MIN_REPLAY_CONFIDENCE
    }
}

/// Confidence metrics
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct ConfidenceMetrics {
    pub decay_checks_total: u64,
    pub capsules_decayed_total: u64,
    pub capsules_quarantined_total: u64,
    pub confidence_boosts_total: u64,
}

/// Apply decay to a capsule and return actions
pub fn process_capsule_confidence(
    scheduler: &dyn ConfidenceScheduler,
    capsule_id: &str,
    gene_id: &GeneId,
    confidence: f32,
    created_at_ms: i64,
    current_time_ms: i64,
    state: AssetState,
) -> Vec<ConfidenceAction> {
    let mut actions = Vec::new();

    // Only process promoted capsules
    if state != AssetState::Promoted {
        return actions;
    }

    let age_hours =
        StandardConfidenceScheduler::calculate_age_hours(created_at_ms, current_time_ms);

    if age_hours > 0.0 {
        let old_conf = confidence;
        let new_conf = scheduler.apply_decay_to_capsule(old_conf, age_hours);

        if (new_conf - old_conf).abs() > 0.001 {
            actions.push(ConfidenceAction::DecayCapsule {
                capsule_id: capsule_id.to_string(),
                gene_id: gene_id.clone(),
                old_confidence: old_conf,
                new_confidence: new_conf,
            });
        }

        // Check quarantine threshold
        if scheduler.should_quarantine(new_conf) {
            actions.push(ConfidenceAction::DemoteToQuarantined {
                asset_id: capsule_id.to_string(),
                confidence: new_conf,
            });
        }
    }

    actions
}

// ---------------------------------------------------------------------------
// ConfidenceController — continuous failure-rate-based confidence tracking
// ---------------------------------------------------------------------------

/// A single outcome record associated with an asset.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct OutcomeRecord {
    pub asset_id: String,
    pub success: bool,
    pub recorded_at_ms: i64,
}

/// Configuration for [`ConfidenceController`].
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ControllerConfig {
    /// Rolling time window in milliseconds used to compute failure rate
    /// (default: 3 600 000 ms = 1 hour).
    pub window_ms: i64,
    /// Failure-rate threshold in [0.0, 1.0] that triggers a downgrade step
    /// (default: 0.5).
    pub failure_rate_threshold: f32,
    /// Minimum number of outcomes inside the window before any downgrade
    /// decision is made (default: 3).
    pub min_samples: usize,
    /// Confidence amount subtracted per downgrade step, also used as the
    /// recovery boost per success (default: 0.15).
    pub downgrade_penalty: f32,
    /// Assets with confidence strictly below this value are considered
    /// non-selectable and require re-validation (default:
    /// `MIN_REPLAY_CONFIDENCE`).
    pub min_selectable_confidence: f32,
    /// Confidence assigned to assets that are not yet tracked (default: 1.0).
    pub initial_confidence: f32,
}

impl Default for ControllerConfig {
    fn default() -> Self {
        Self {
            window_ms: 3_600_000,
            failure_rate_threshold: 0.5,
            min_samples: 3,
            downgrade_penalty: 0.15,
            min_selectable_confidence: MIN_REPLAY_CONFIDENCE,
            initial_confidence: 1.0,
        }
    }
}

/// An observability event emitted whenever an asset is automatically
/// downgraded by the [`ConfidenceController`].
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct DowngradeEvent {
    pub asset_id: String,
    pub old_confidence: f32,
    pub new_confidence: f32,
    /// Observed failure rate inside the rolling window.
    pub failure_rate: f32,
    /// Number of outcomes that were inside the window.
    pub window_samples: usize,
    pub event_at_ms: i64,
    /// `true` when `new_confidence` fell below `min_selectable_confidence`,
    /// indicating that re-validation should be triggered.
    pub revalidation_required: bool,
}

/// Continuous confidence controller for genes and capsules.
///
/// Tracks per-asset success / failure outcomes within a rolling time window.
/// When the failure rate exceeds the configured threshold and the minimum
/// sample count is met, the asset's confidence score is automatically
/// downgraded and a [`DowngradeEvent`] is appended to an internal
/// observability log.  Successive successes can recover confidence.
///
/// # Selector integration
///
/// Call [`is_selectable`](ConfidenceController::is_selectable) before
/// choosing a gene/capsule for reuse.  Assets below
/// [`ControllerConfig::min_selectable_confidence`] return `false` and
/// should be skipped until they have been re-validated.
pub struct ConfidenceController {
    config: ControllerConfig,
    scores: HashMap<String, f32>,
    history: HashMap<String, Vec<OutcomeRecord>>,
    downgrade_log: Vec<DowngradeEvent>,
}

impl ConfidenceController {
    /// Create a new controller with the given configuration.
    pub fn new(config: ControllerConfig) -> Self {
        Self {
            config,
            scores: HashMap::new(),
            history: HashMap::new(),
            downgrade_log: Vec::new(),
        }
    }

    /// Create a controller using [`ControllerConfig::default`].
    pub fn with_default_config() -> Self {
        Self::new(ControllerConfig::default())
    }

    /// Current confidence score for `asset_id`.
    /// Returns [`ControllerConfig::initial_confidence`] for unknown assets.
    pub fn confidence(&self, asset_id: &str) -> f32 {
        self.scores
            .get(asset_id)
            .copied()
            .unwrap_or(self.config.initial_confidence)
    }

    /// Returns `true` when the asset's confidence is at or above
    /// [`ControllerConfig::min_selectable_confidence`].
    pub fn is_selectable(&self, asset_id: &str) -> bool {
        self.confidence(asset_id) >= self.config.min_selectable_confidence
    }

    /// Record a **successful** outcome for `asset_id` at `now_ms`.
    ///
    /// Applies a recovery boost (capped at `initial_confidence`).
    pub fn record_success(&mut self, asset_id: &str, now_ms: i64) {
        self.history
            .entry(asset_id.to_string())
            .or_default()
            .push(OutcomeRecord {
                asset_id: asset_id.to_string(),
                success: true,
                recorded_at_ms: now_ms,
            });
        let initial = self.config.initial_confidence;
        let penalty = self.config.downgrade_penalty;
        let entry = self.scores.entry(asset_id.to_string()).or_insert(initial);
        *entry = (*entry + penalty).min(initial);
    }

    /// Record a **failure** outcome for `asset_id` at `now_ms`.
    ///
    /// Immediately evaluates the rolling-window failure rate and downgrades
    /// confidence if the threshold is exceeded.
    pub fn record_failure(&mut self, asset_id: &str, now_ms: i64) {
        self.history
            .entry(asset_id.to_string())
            .or_default()
            .push(OutcomeRecord {
                asset_id: asset_id.to_string(),
                success: false,
                recorded_at_ms: now_ms,
            });
        if let Some(evt) =
            Self::compute_downgrade(&self.history, &self.scores, asset_id, now_ms, &self.config)
        {
            *self
                .scores
                .entry(asset_id.to_string())
                .or_insert(evt.old_confidence) = evt.new_confidence;
            self.downgrade_log.push(evt);
        }
    }

    /// Sweep all tracked assets and apply downgrade logic at `now_ms`.
    ///
    /// Returns every [`DowngradeEvent`] generated in this sweep (also
    /// appended to the internal log).
    pub fn run_downgrade_check(&mut self, now_ms: i64) -> Vec<DowngradeEvent> {
        let asset_ids: Vec<String> = self.history.keys().cloned().collect();
        let mut events = Vec::new();
        for id in &asset_ids {
            if let Some(evt) =
                Self::compute_downgrade(&self.history, &self.scores, id, now_ms, &self.config)
            {
                *self.scores.entry(id.clone()).or_insert(evt.old_confidence) = evt.new_confidence;
                self.downgrade_log.push(evt.clone());
                events.push(evt);
            }
        }
        events
    }

    /// Full observability log of every downgrade event since construction.
    pub fn downgrade_log(&self) -> &[DowngradeEvent] {
        &self.downgrade_log
    }

    /// Asset IDs whose confidence has fallen below
    /// [`ControllerConfig::min_selectable_confidence`] and therefore require
    /// re-validation before they can be reused.
    pub fn assets_requiring_revalidation(&self) -> Vec<String> {
        self.scores
            .iter()
            .filter(|(_, &v)| v < self.config.min_selectable_confidence)
            .map(|(k, _)| k.clone())
            .collect()
    }

    // --- private helpers ---

    /// Pure function: decide whether `asset_id` should be downgraded given
    /// current `history` and `scores`.  Returns `None` when no action is
    /// needed.
    fn compute_downgrade(
        history: &HashMap<String, Vec<OutcomeRecord>>,
        scores: &HashMap<String, f32>,
        asset_id: &str,
        now_ms: i64,
        config: &ControllerConfig,
    ) -> Option<DowngradeEvent> {
        let window_start = now_ms - config.window_ms;
        let records = history.get(asset_id)?;
        let window: Vec<&OutcomeRecord> = records
            .iter()
            .filter(|r| r.recorded_at_ms >= window_start)
            .collect();
        let total = window.len();
        if total < config.min_samples {
            return None;
        }
        let failures = window.iter().filter(|r| !r.success).count();
        let rate = failures as f32 / total as f32;
        if rate < config.failure_rate_threshold {
            return None;
        }
        let old = scores
            .get(asset_id)
            .copied()
            .unwrap_or(config.initial_confidence);
        let new_val = (old - config.downgrade_penalty).max(0.0);
        Some(DowngradeEvent {
            asset_id: asset_id.to_string(),
            old_confidence: old,
            new_confidence: new_val,
            failure_rate: rate,
            window_samples: total,
            event_at_ms: now_ms,
            revalidation_required: new_val < config.min_selectable_confidence,
        })
    }
}

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

    #[test]
    fn test_calculate_decay() {
        // Initial confidence 1.0, after 0 hours should be 1.0
        let conf = StandardConfidenceScheduler::calculate_decay(1.0, 0.0);
        assert!((conf - 1.0).abs() < 0.001);

        // After ~13.86 hours (ln(2)/0.05), confidence should be ~0.5
        let conf = StandardConfidenceScheduler::calculate_decay(1.0, 13.86);
        assert!((conf - 0.5).abs() < 0.01);

        // After 24 hours: e^(-0.05*24) ≈ 0.30
        let conf = StandardConfidenceScheduler::calculate_decay(1.0, 24.0);
        assert!((conf - 0.30).abs() < 0.02);

        // Zero confidence stays zero
        let conf = StandardConfidenceScheduler::calculate_decay(0.0, 100.0);
        assert!(conf.abs() < 0.001);
    }

    #[test]
    fn test_should_quarantine() {
        let scheduler = StandardConfidenceScheduler::with_default_config();

        // Above threshold - should not quarantine
        assert!(!scheduler.should_quarantine(0.5));
        assert!(!scheduler.should_quarantine(0.35));
        assert!(!scheduler.should_quarantine(0.36));

        // Below threshold - should quarantine
        assert!(scheduler.should_quarantine(0.34));
        assert!(scheduler.should_quarantine(0.0));
    }

    #[test]
    fn test_boost_confidence() {
        let scheduler = StandardConfidenceScheduler::with_default_config();

        // Boost from 0.5 should be 0.6
        let conf = scheduler.boost_confidence(0.5);
        assert!((conf - 0.6).abs() < 0.001);

        // Boost should cap at 1.0
        let conf = scheduler.boost_confidence(1.0);
        assert!((conf - 1.0).abs() < 0.001);

        // Boost from 0.95 should cap at 1.0
        let conf = scheduler.boost_confidence(0.95);
        assert!((conf - 1.0).abs() < 0.001);
    }

    #[test]
    fn test_default_config() {
        let config = ConfidenceSchedulerConfig::default();
        assert!(config.enabled);
        assert_eq!(config.check_interval_secs, 3600);
        assert!((config.confidence_boost_per_success - 0.1).abs() < 0.001);
    }

    #[test]
    fn test_calculate_age_hours() {
        // 1 hour = 3600 seconds = 3600000 ms
        let age = StandardConfidenceScheduler::calculate_age_hours(0, 3600000);
        assert!((age - 1.0).abs() < 0.001);

        // 24 hours
        let age = StandardConfidenceScheduler::calculate_age_hours(0, 86400000);
        assert!((age - 24.0).abs() < 0.001);

        // Less than an hour
        let age = StandardConfidenceScheduler::calculate_age_hours(0, 1800000);
        assert!((age - 0.5).abs() < 0.001);
    }

    // -----------------------------------------------------------------------
    // ConfidenceController tests
    // -----------------------------------------------------------------------

    const NOW: i64 = 1_000_000_000_000; // arbitrary fixed "now" in ms
    const WINDOW: i64 = 3_600_000; // 1 hour window

    fn controller_with_3_samples() -> ConfidenceController {
        ConfidenceController::new(ControllerConfig {
            window_ms: WINDOW,
            failure_rate_threshold: 0.5,
            min_samples: 3,
            downgrade_penalty: 0.15,
            min_selectable_confidence: MIN_REPLAY_CONFIDENCE,
            initial_confidence: 1.0,
        })
    }

    #[test]
    fn test_controller_initial_confidence_is_one() {
        let ctrl = controller_with_3_samples();
        // Unknown asset → initial confidence
        assert!((ctrl.confidence("gene-1") - 1.0).abs() < 0.001);
        assert!(ctrl.is_selectable("gene-1"));
    }

    #[test]
    fn test_controller_successive_failures_downgrade() {
        let mut ctrl = controller_with_3_samples();
        // 3 failures in the window → failure rate 1.0 ≥ 0.5 → first downgrade
        ctrl.record_failure("gene-x", NOW);
        ctrl.record_failure("gene-x", NOW + 1);
        ctrl.record_failure("gene-x", NOW + 2);
        let c = ctrl.confidence("gene-x");
        // Expected: 1.0 - 0.15 = 0.85
        assert!((c - 0.85).abs() < 0.01, "expected ~0.85, got {c}");
        assert_eq!(ctrl.downgrade_log().len(), 1);
    }

    #[test]
    fn test_controller_below_min_not_selectable() {
        let mut ctrl = ConfidenceController::new(ControllerConfig {
            window_ms: WINDOW,
            failure_rate_threshold: 0.5,
            min_samples: 2,
            downgrade_penalty: 0.35,
            min_selectable_confidence: MIN_REPLAY_CONFIDENCE,
            initial_confidence: 0.5, // start near the edge
        });
        // Two failures → rate 1.0 ≥ 0.5, 2 ≥ min_samples=2 → downgrade
        ctrl.record_failure("gene-low", NOW);
        ctrl.record_failure("gene-low", NOW + 1);
        // 0.5 - 0.35 = 0.15 < MIN_REPLAY_CONFIDENCE (0.35)
        assert!(!ctrl.is_selectable("gene-low"));
        assert_eq!(ctrl.downgrade_log()[0].revalidation_required, true);
        let rv = ctrl.assets_requiring_revalidation();
        assert!(rv.contains(&"gene-low".to_string()));
    }

    #[test]
    fn test_controller_recovery_via_successes() {
        let mut ctrl = controller_with_3_samples();
        // Drive confidence down first
        ctrl.record_failure("gene-r", NOW);
        ctrl.record_failure("gene-r", NOW + 1);
        ctrl.record_failure("gene-r", NOW + 2);
        let after_failures = ctrl.confidence("gene-r");
        // Now record two successes to partially recover
        ctrl.record_success("gene-r", NOW + 3);
        ctrl.record_success("gene-r", NOW + 4);
        let after_recovery = ctrl.confidence("gene-r");
        assert!(
            after_recovery > after_failures,
            "recovery expected: {after_recovery} > {after_failures}"
        );
    }

    #[test]
    fn test_controller_no_downgrade_below_min_samples() {
        let mut ctrl = controller_with_3_samples();
        // Only 2 failures — below min_samples=3 → no downgrade
        ctrl.record_failure("gene-few", NOW);
        ctrl.record_failure("gene-few", NOW + 1);
        assert!((ctrl.confidence("gene-few") - 1.0).abs() < 0.001);
        assert!(ctrl.downgrade_log().is_empty());
    }

    #[test]
    fn test_controller_failures_outside_window_ignored() {
        let mut ctrl = controller_with_3_samples();
        // 2 failures far outside the 1-hour window (below min_samples=3 so no
        // downgrade fires when they are recorded).
        let old = NOW - WINDOW - 1;
        ctrl.record_failure("gene-old", old);
        ctrl.record_failure("gene-old", old + 1);
        // run_downgrade_check at NOW: these 2 records are outside the window,
        // count = 0 → below min_samples → no new downgrade event.
        let events = ctrl.run_downgrade_check(NOW);
        assert!(events.is_empty(), "expected no downgrade, got {events:?}");
        assert!((ctrl.confidence("gene-old") - 1.0).abs() < 0.001);
        assert!(ctrl.downgrade_log().is_empty());
    }

    #[test]
    fn test_controller_run_downgrade_check_batch() {
        let mut ctrl = controller_with_3_samples();
        // Seed failures for two assets
        for i in 0..3 {
            ctrl.history
                .entry("asset-a".to_string())
                .or_default()
                .push(OutcomeRecord {
                    asset_id: "asset-a".to_string(),
                    success: false,
                    recorded_at_ms: NOW + i,
                });
            ctrl.history
                .entry("asset-b".to_string())
                .or_default()
                .push(OutcomeRecord {
                    asset_id: "asset-b".to_string(),
                    success: false,
                    recorded_at_ms: NOW + i,
                });
        }
        let events = ctrl.run_downgrade_check(NOW + 10);
        assert_eq!(events.len(), 2);
        assert_eq!(ctrl.downgrade_log().len(), 2);
    }

    #[test]
    fn test_controller_downgrade_event_fields() {
        let mut ctrl = controller_with_3_samples();
        ctrl.record_failure("gene-fields", NOW);
        ctrl.record_failure("gene-fields", NOW + 1);
        ctrl.record_failure("gene-fields", NOW + 2);
        let log = ctrl.downgrade_log();
        assert_eq!(log.len(), 1);
        let evt = &log[0];
        assert_eq!(evt.asset_id, "gene-fields");
        assert!((evt.old_confidence - 1.0).abs() < 0.001);
        assert!((evt.new_confidence - 0.85).abs() < 0.01);
        assert!((evt.failure_rate - 1.0).abs() < 0.001);
        assert_eq!(evt.window_samples, 3);
        assert_eq!(evt.event_at_ms, NOW + 2);
    }
}