omega-cognitive-kernel 0.17.1

Online strategy-patch runtime: training-free RuleEffect adaptation with audit and rollback
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
//! Resident L2 safety controller, implemented with `alloc` and `libm` only.
use crate::l2::meta_cache;
#[allow(unused_imports)]
use crate::prelude::*;
use alloc::collections::BTreeMap;
use serde::{Deserialize, Serialize};

/// Constant defining expert names.
pub const EXPERT_NAMES: [&str; 4] = ["KEEP_L0", "STABILIZE", "EXPLORE", "OBSERVE"];
/// Constant defining keep L0.
pub const KEEP_L0: usize = 0;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "UPPERCASE")]
/// Identifies a supported L2 gate state.
pub enum L2GateState {
    #[default]
    /// Variant representing dormant.
    Dormant,
    /// Variant representing active.
    Active,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
/// Data structure representing trust region.
pub struct TrustRegion {
    /// Alpha for this value.
    pub alpha: f64,
    /// Alpha maximum for this value.
    pub alpha_max: f64,
    /// Rise rate for this value.
    pub rise_rate: f64,
    /// Number of cooldown.
    pub cooldown_steps: u32,
    /// Cooldown remaining for this value.
    pub cooldown_remaining: u32,
}
impl Default for TrustRegion {
    fn default() -> Self {
        Self {
            alpha: 0.0,
            alpha_max: 0.35,
            rise_rate: 0.08,
            cooldown_steps: 0,
            cooldown_remaining: 0,
        }
    }
}
impl TrustRegion {
    /// Performs update for this type.
    pub fn update(&mut self, active: bool, rollback: bool, reward_delta: f64, consequence: f64) {
        self.cooldown_remaining = self.cooldown_remaining.saturating_sub(1);
        if rollback || reward_delta < -0.15 {
            self.alpha = 0.0;
            self.cooldown_remaining = self.cooldown_steps.max(2);
        } else if !active {
            self.alpha = 0.0;
        } else if self.cooldown_remaining == 0 && consequence >= 0.0 {
            self.alpha = (self.alpha + self.rise_rate).min(self.alpha_max);
        } else if consequence < -0.05 {
            self.alpha = 0.0;
            self.cooldown_remaining = self.cooldown_steps;
        }
    }
}

#[derive(Debug, Clone, Copy)]
/// Data structure representing control feedback.
pub struct ControlFeedback {
    /// Error for this value.
    pub error: f64,
    /// Reward for this value.
    pub reward: f64,
    /// Whether rollback is enabled or active.
    pub rollback: bool,
    /// Consequence for this value.
    pub consequence: f64,
    /// Kl policy for this value.
    pub kl_policy: [f64; 2],
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
/// Data structure representing expert aggregator.
pub struct ExpertAggregator {
    /// Weights for this value.
    pub weights: [f64; 4],
    /// Cumulative reward for this value.
    pub cumulative_reward: [f64; 4],
    /// Most recent probabilities.
    pub last_probabilities: [f64; 4],
    /// Forgetting for this value.
    pub forgetting: f64,
    /// Learning rate for this value.
    pub learning_rate: f64,
    /// Mixing for this value.
    pub mixing: f64,
    /// Rounds for this value.
    pub rounds: u64,
}
impl Default for ExpertAggregator {
    fn default() -> Self {
        Self {
            weights: [1.15, 1.0, 1.0, 1.0],
            cumulative_reward: [0.0; 4],
            last_probabilities: [0.25; 4],
            forgetting: 0.97,
            learning_rate: 0.35,
            mixing: 0.05,
            rounds: 0,
        }
    }
}
impl ExpertAggregator {
    /// Performs probabilities for this type.
    pub fn probabilities(&self) -> [f64; 4] {
        let total: f64 = self.weights.iter().sum();
        if !total.is_finite() || total <= 0.0 {
            return [0.25; 4];
        }
        let mut p = [0.0; 4];
        for (i, item) in p.iter_mut().enumerate() {
            *item = 0.95 * self.weights[i] / total + self.mixing / 4.0;
        }
        p
    }
    /// Performs select for this type.
    pub fn select(&self, surprise: f64) -> usize {
        let p = self.probabilities();
        // Deterministic pseudo-random draw: the complete decision is replayable.
        let draw = libm::fabs(libm::sin(
            (self.rounds + 1) as f64 * 12.9898 + surprise * 78.233,
        ));
        let mut cumulative = 0.0;
        for (i, probability) in p.iter().enumerate() {
            cumulative += probability;
            if draw <= cumulative {
                return i;
            }
        }
        KEEP_L0
    }
    /// Inverse-propensity scoring (IPS) for the selected expert. Unselected
    /// counterfactual rewards are not observed and therefore remain zero in
    /// this sparse estimator: E[1{I=i} r/p_i | r_i] = r_i. Nonzero mixing gives
    /// every expert support, while forgetting bounds the effective horizon.
    /// Calling this standard IPS is not a claim of counterfactual observation.
    pub fn update_ips(&mut self, expert: usize, probabilities: [f64; 4], reward: f64) {
        if expert >= 4
            || !probabilities.iter().all(|p| p.is_finite() && *p > 0.0)
            || libm::fabs(probabilities.iter().sum::<f64>() - 1.0) > 1.0e-6
        {
            return;
        }
        let reward = reward.clamp(0.0, 1.0);
        for i in 0..4 {
            self.cumulative_reward[i] *= self.forgetting;
            self.weights[i] = (self.weights[i] * self.forgetting + 1.0e-12).max(1.0e-12);
        }
        let estimate = reward / probabilities[expert].max(1.0e-6);
        self.cumulative_reward[expert] += estimate;
        self.weights[expert] *= libm::exp(self.learning_rate * estimate / 4.0);
        let maximum = self.weights.iter().copied().fold(1.0e-12, f64::max);
        for weight in &mut self.weights {
            *weight = (*weight / maximum).max(1.0e-12);
        }
        self.last_probabilities = probabilities;
        self.rounds = self.rounds.saturating_add(1);
    }
    /// Performs warm start for this type.
    pub fn warm_start(&mut self, mut weights: [f64; 4]) {
        // Never turn an untrusted or zero-valued cached weight into a zero
        // probability: EXP3 support is required by the IPS estimator.
        for weight in &mut weights {
            if !weight.is_finite() {
                *weight = 1.0e-12;
            }
            *weight = weight.max(1.0e-12);
        }
        self.weights = weights;
        self.last_probabilities = self.probabilities();
    }
    /// Diagnostic discounted-Hedge envelope, in reward units, under bounded
    /// rewards and a comparator with `switches` changes. It is not an
    /// unconditional dynamic-regret proof for the closed-loop environment:
    /// IPS variance, model error, and environmental feedback are additional
    /// assumptions that cannot be inferred from a benchmark score.
    pub fn tracking_regret_bound(&self, switches: u64) -> f64 {
        let horizon = self.rounds.max(1) as f64;
        let eta = self.learning_rate.max(1.0e-6);
        let effective_horizon = 1.0 / (1.0 - self.forgetting.clamp(0.0, 0.999999));
        libm::log(4.0) / eta
            + effective_horizon * eta / 8.0
            + (switches as f64 + 1.0) * (1.0 - self.forgetting) * horizon
    }
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
#[serde(default)]
/// Data structure representing stability certificate.
pub struct StabilityCertificate {
    /// Potential for this value.
    pub potential: f64,
    /// Predicted potential for this value.
    pub predicted_potential: f64,
    /// Gamma for this value.
    pub gamma: f64,
    /// Epsilon for this value.
    pub epsilon: f64,
    /// Whether lyapunov veto is enabled or active.
    pub lyapunov_veto: bool,
}
impl Default for StabilityCertificate {
    fn default() -> Self {
        Self {
            potential: 1.0,
            predicted_potential: 1.0,
            gamma: 0.05,
            epsilon: 0.01,
            lyapunov_veto: false,
        }
    }
}
impl StabilityCertificate {
    /// Validates this value and reports any violated constraints.
    pub fn check(&self, residual: f64, predicted_residual: f64) -> Self {
        let potential = residual * residual;
        let predicted_potential = predicted_residual * predicted_residual;
        let veto = !potential.is_finite()
            || !predicted_potential.is_finite()
            || predicted_potential > (1.0 - self.gamma) * potential + self.epsilon;
        Self {
            potential,
            predicted_potential,
            lyapunov_veto: veto,
            ..*self
        }
    }
}

/// Exact frozen solution of `E[R] - beta KL(pi || prior)` for two actions.
pub fn kl_regularized_policy(rewards: [f64; 2], prior: [f64; 2], beta: f64) -> [f64; 2] {
    if beta <= 0.0 {
        return if rewards[1] > rewards[0] {
            [0.0, 1.0]
        } else {
            [1.0, 0.0]
        };
    }
    let prior = [prior[0].max(1.0e-8), prior[1].max(1.0e-8)];
    let total = prior[0] + prior[1];
    let scaled = [
        rewards[0] / beta + libm::log(prior[0] / total),
        rewards[1] / beta + libm::log(prior[1] / total),
    ];
    let max = scaled[0].max(scaled[1]);
    let exp = [libm::exp(scaled[0] - max), libm::exp(scaled[1] - max)];
    [exp[0] / (exp[0] + exp[1]), exp[1] / (exp[0] + exp[1])]
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
/// Data structure representing resident controller.
pub struct ResidentController {
    /// Gate state for this value.
    pub gate_state: L2GateState,
    /// Normal recovery is finalized at consequence settlement. Until then
    /// the commit snapshot remains valid for execution and feedback.
    pub retire_requested: bool,
    /// Whether feedback settled is enabled or active.
    pub feedback_settled: bool,
    /// Trust for this value.
    pub trust: TrustRegion,
    /// Experts for this value.
    pub experts: ExpertAggregator,
    /// Certificate for this value.
    pub certificate: StabilityCertificate,
    /// Cache for this value.
    pub cache: meta_cache::MetaCache,
    /// Baseline error for this value.
    pub baseline_error: f64,
    /// Ema error for this value.
    pub ema_error: f64,
    /// Ema reward for this value.
    pub ema_reward: f64,
    /// Baseline reward for this value.
    pub baseline_reward: f64,
    /// Observations for this value.
    pub observations: u64,
    /// Error margin for this value.
    pub error_margin: f64,
    /// Reward drop margin for this value.
    pub reward_drop_margin: f64,
    /// Rollback wakeup for this value.
    pub rollback_wakeup: u32,
    /// Rollback streak for this value.
    pub rollback_streak: u32,
    /// Calm streak for this value.
    pub calm_streak: u32,
    /// Calm required for this value.
    pub calm_required: u32,
    /// Maximum error history.
    pub max_error_history: usize,
    /// Collection of errors.
    pub errors: Vec<f64>,
    /// Collection of rewards.
    pub rewards: Vec<f64>,
    /// Window rollbacks for this value.
    pub window_rollbacks: u32,
    /// Window total for this value.
    pub window_total: u32,
    /// Beta maximum for this value.
    pub beta_max: f64,
    /// Whether meta cache hit is enabled or active.
    pub meta_cache_hit: bool,
    /// True only on the transition into ACTIVE, including a rollback wakeup.
    pub surprise_started: bool,
    /// Whether cache prepared is enabled or active.
    pub cache_prepared: bool,
    /// Optional cache prepared fingerprint.
    pub cache_prepared_fingerprint: Option<meta_cache::ContextFingerprint>,
    /// Optional surprise fingerprint.
    pub surprise_fingerprint: Option<meta_cache::ContextFingerprint>,
    /// Episode start residual for this value.
    pub episode_start_residual: f64,
    /// Whether cached this episode is enabled or active.
    pub cached_this_episode: bool,
    /// Entropy sum for this value.
    pub entropy_sum: f64,
    /// Most recent selected expert.
    pub last_selected_expert: usize,
    /// Most recent reward.
    pub last_reward: f64,
    /// Residual for this value.
    pub residual: f64,
    /// Residual relaxation state and its most recent frozen target. These
    /// fields retain their v4 names for snapshot compatibility.
    pub dayan_state: f64,
    /// Dayan relaxation for this value.
    pub dayan_relaxation: f64,
    /// Residual ema for this value.
    pub residual_ema: f64,
    /// Previous error for this value.
    pub previous_error: f64,
    /// Residual slope for this value.
    pub residual_slope: f64,
    /// Residual control gain for this value.
    pub residual_control_gain: f64,
    /// Model confidence for this value.
    pub model_confidence: f64,
    /// Number of veto.
    pub veto_count: u64,
    /// Cache hits for this value.
    pub cache_hits: u64,
    /// Best residual for this value.
    pub best_residual: f64,
    /// Initial residual for this value.
    pub initial_residual: f64,
}
impl Default for ResidentController {
    fn default() -> Self {
        Self {
            gate_state: L2GateState::Dormant,
            retire_requested: false,
            feedback_settled: false,
            trust: TrustRegion {
                cooldown_steps: 3,
                ..TrustRegion::default()
            },
            experts: ExpertAggregator::default(),
            certificate: StabilityCertificate::default(),
            cache: meta_cache::MetaCache::default(),
            baseline_error: 0.0,
            ema_error: 0.0,
            ema_reward: 0.5,
            baseline_reward: 0.5,
            observations: 0,
            error_margin: 0.04,
            reward_drop_margin: 0.18,
            rollback_wakeup: 2,
            rollback_streak: 0,
            calm_streak: 0,
            calm_required: 3,
            max_error_history: 16,
            errors: Vec::new(),
            rewards: Vec::new(),
            window_rollbacks: 0,
            window_total: 0,
            beta_max: 2.0,
            meta_cache_hit: false,
            surprise_started: false,
            cache_prepared: false,
            cache_prepared_fingerprint: None,
            surprise_fingerprint: None,
            episode_start_residual: 1.0,
            cached_this_episode: false,
            entropy_sum: 0.0,
            last_selected_expert: KEEP_L0,
            last_reward: 0.5,
            residual: 1.0,
            dayan_state: 0.0,
            dayan_relaxation: 0.35,
            residual_ema: 1.0,
            previous_error: 0.0,
            residual_slope: 0.0,
            residual_control_gain: 0.25,
            model_confidence: 0.0,
            veto_count: 0,
            cache_hits: 0,
            best_residual: 1.0,
            initial_residual: 1.0,
        }
    }
}
impl ResidentController {
    /// Consume exactly one outcome for the policy that acted before this call.
    /// `executed_expert = KEEP_L0` means no intervention was applied and the
    /// reward update uses L0's own selection propensity (IPS still works).
    /// The policy remains noncommitted; its probabilities describe the actual
    /// selection rather than granting a new intervention.
    pub fn observe(&mut self, feedback: ControlFeedback, policy: crate::types::L2Policy) {
        if self.feedback_settled {
            return;
        }
        self.feedback_settled = true;
        self.meta_cache_hit = false;
        if !(feedback.error.is_finite()
            && feedback.reward.is_finite()
            && feedback.consequence.is_finite()
            && feedback
                .kl_policy
                .iter()
                .all(|p| p.is_finite() && *p >= 0.0))
        {
            self.retire_requested = true;
            self.trust.alpha = 0.0;
            self.trust.cooldown_remaining = self.trust.cooldown_steps.max(2);
            return;
        }
        let error = feedback.error.clamp(0.0, 1.0);
        if self.observations > 0 {
            let error_delta = error - self.previous_error;
            let target_delta = self.residual_ema - self.previous_error;
            self.residual_slope =
                (0.8 * self.residual_slope + 0.2 * (target_delta - error_delta)).clamp(-0.5, 0.5);
            self.model_confidence =
                (1.0 - libm::fabs(self.residual_slope - target_delta)).clamp(0.0, 1.0);
        } else {
            self.model_confidence = 1.0;
        }
        self.previous_error = error;
        if self.observations == 0 {
            self.baseline_error = error;
            self.baseline_reward = feedback.reward;
            self.ema_error = error;
            self.ema_reward = feedback.reward;
        } else {
            self.ema_error = 0.75 * self.ema_error + 0.25 * error;
            self.ema_reward = 0.80 * self.ema_reward + 0.20 * feedback.reward;
            self.baseline_error = 0.98 * self.baseline_error + 0.02 * error;
            self.baseline_reward = 0.98 * self.baseline_reward + 0.02 * feedback.reward;
        }
        self.observations = self.observations.saturating_add(1);
        self.errors.push(error);
        self.rewards.push(feedback.reward.clamp(0.0, 1.0));
        let bounded = self.max_error_history.max(2);
        if self.errors.len() > bounded {
            self.errors.remove(0);
        }
        if self.rewards.len() > bounded {
            self.rewards.remove(0);
        }
        self.window_total = self.window_total.saturating_add(1);
        self.window_rollbacks = self
            .window_rollbacks
            .saturating_add(u32::from(feedback.rollback));
        self.rollback_streak = if feedback.rollback {
            self.rollback_streak.saturating_add(1)
        } else {
            0
        };
        let entropy = feedback
            .kl_policy
            .iter()
            .filter(|p| **p > 0.0)
            .map(|p| -p * libm::log(*p))
            .sum::<f64>()
            / libm::log(2.0);
        self.entropy_sum += entropy;
        let probabilities = policy.expert_probabilities;
        let policy = policy.effective();
        let expert = policy.expert;
        self.experts
            .update_ips(expert, probabilities, feedback.reward);

        let rollback_surprise = self.rollback_streak >= self.rollback_wakeup;
        let surprise = self.ema_error > self.baseline_error + self.error_margin
            || self.ema_reward < self.baseline_reward - self.reward_drop_margin
            || rollback_surprise;
        self.surprise_started = false;
        if surprise && self.gate_state == L2GateState::Dormant {
            self.gate_state = L2GateState::Active;
            self.surprise_started = true;
            self.calm_streak = 0;
            self.cache_prepared = false;
            self.surprise_fingerprint = Some(self.fingerprint());
            self.episode_start_residual = self.residual;
            self.cached_this_episode = false;
        }
        self.trust.update(
            self.gate_state == L2GateState::Active,
            feedback.rollback,
            feedback.reward - self.last_reward,
            feedback.consequence,
        );
        self.last_reward = feedback.reward;
        if self.gate_state == L2GateState::Active && self.observations >= 4 {
            // A changed regime may have a new normal reward/error level. Once
            // the actual fixed-point residual is low and stable, raw baseline
            // offsets must not keep the controller awake forever.
            let recovered_residual =
                self.residual <= 0.18 && self.residual < self.episode_start_residual - 0.01;
            // Recovery can mean either convergence from an actual perturbation
            // or confirmation that the original alarm has subsided. Requiring
            // a further decrease from an already tiny residual would keep a
            // healthy controller permanently ACTIVE.
            let calm_observation =
                !surprise && self.residual <= 0.18 && feedback.consequence >= -0.05;
            let recovery = !rollback_surprise && (recovered_residual || calm_observation);
            self.calm_streak = if recovery {
                self.calm_streak.saturating_add(1)
            } else {
                0
            };
            if recovery && !self.cached_this_episode && self.cache_prepared {
                let fingerprint = self
                    .surprise_fingerprint
                    .unwrap_or_else(|| self.fingerprint());
                self.cache.store(
                    fingerprint,
                    self.experts.weights,
                    self.trust.alpha,
                    1.0 + (self.initial_residual - self.residual).max(0.0),
                );
                self.cached_this_episode = true;
            }
            if self.calm_streak >= self.calm_required {
                self.retire_requested = true;
            }
        }
    }
    /// Complete a normal retirement only after the acting policy's feedback
    /// has been consumed. An unused pending commit keeps the gate snapshot
    /// valid until the next action and its consequence have both settled.
    pub fn finish_cycle(&mut self) {
        if !self.feedback_settled {
            return;
        }
        if self.retire_requested {
            self.gate_state = L2GateState::Dormant;
            self.calm_streak = 0;
            self.trust.alpha = 0.0;
            self.retire_requested = false;
            self.cached_this_episode = false;
        }
        self.feedback_settled = false;
        self.window_rollbacks = 0;
        self.window_total = 0;
        self.entropy_sum = 0.0;
        self.meta_cache_hit = false;
    }
    /// Performs fingerprint for this type.
    pub fn fingerprint(&self) -> meta_cache::ContextFingerprint {
        meta_cache::ContextFingerprint::from_window(
            &self.errors,
            self.window_rollbacks,
            self.window_total,
            self.entropy_sum / self.window_total.max(1) as f64,
        )
    }
    /// Performs prior for this type.
    pub fn prior(&self) -> [f64; 2] {
        match self.last_selected_expert {
            1 => [0.90, 0.10],
            2 => [0.10, 0.90],
            3 => [0.65, 0.35],
            _ => [0.5, 0.5],
        }
    }
    /// Performs beta for this type.
    pub fn beta(&self) -> f64 {
        self.beta_for(false)
    }

    /// Return a regularization budget. Legacy controllers apply trust/confidence
    /// here; the spectral experiment applies them once at the KL boundary.
    pub fn beta_for(&self, spectral_weighting: bool) -> f64 {
        if self.gate_state == L2GateState::Dormant
            || self.last_selected_expert == KEEP_L0
            || self.retire_requested
        {
            0.0
        } else if spectral_weighting {
            self.beta_max.max(0.0)
        } else {
            self.beta_max * self.trust.alpha / self.trust.alpha_max.max(1.0e-9)
                * self.model_confidence
        }
    }
    /// Predict one relaxation step from the current frozen target, then add
    /// the declared expert's worst-case bounded disturbance. `error` is the
    /// observation associated with the latest target; alpha only bounds
    /// uncertainty, it is not treated as a guaranteed error reduction.
    ///
    /// This is a model-based one-step check, not a claim that an expert can
    /// control the environment. For a frozen target, STABILIZE (which makes no
    /// additional perturbation in this model) recovers exactly the contraction
    /// `r+ = (1 - lambda) r`; EXPLORE/OBSERVE pay a transparent risk budget.
    pub fn predicted_residual(&self, error: f64, alpha: f64, expert: usize) -> f64 {
        let disturbance = match expert {
            1 => 0.0,
            2 => libm::fabs(self.residual_control_gain),
            3 => 0.35 * libm::fabs(self.residual_control_gain),
            _ => return f64::INFINITY,
        };
        if !error.is_finite() {
            return f64::INFINITY;
        }
        let lambda = self.dayan_relaxation.clamp(0.01, 1.0);
        let innovation =
            (libm::fabs(self.residual_slope) + libm::fabs(self.ema_error - error)).clamp(0.0, 1.0);
        let disturbance_term = 0.2 * lambda * innovation + alpha.clamp(0.0, 1.0) * disturbance;
        let predicted = (1.0 - lambda) * self.residual + disturbance_term;
        if predicted.is_finite() {
            predicted
        } else {
            f64::INFINITY
        }
    }
    /// One explicit fixed-point iteration on the residual:
    /// `r+ = (1-lambda) r + lambda |target - previous_target|`.
    /// The latest observation supplies the omega=dayan target. For a frozen
    /// target and 0 < lambda <= 1 this is a contraction, hence V=r^2 decreases
    /// by (1-lambda)^2. On a regime change the target may move, which is why a
    /// separate single-step Lyapunov certificate guards every commit.
    pub fn update_dayan(&mut self, target_alignment: f64) -> f64 {
        let target = target_alignment.clamp(0.0, 1.0);
        if self.observations == 0 {
            self.residual_ema = target;
            self.initial_residual = 1.0;
            self.best_residual = 1.0;
        }
        let lambda = self.dayan_relaxation.clamp(0.01, 1.0);
        self.dayan_state =
            (1.0 - lambda) * self.dayan_state + lambda * libm::fabs(target - self.residual_ema);
        self.dayan_state = self.dayan_state.clamp(0.0, 1.0);
        self.residual = self.dayan_state;
        self.residual_ema = target;
        self.best_residual = self.best_residual.min(self.residual);
        self.residual
    }
    /// Performs expert weights for this type.
    pub fn expert_weights(&self) -> BTreeMap<String, f64> {
        let p = self.experts.probabilities();
        let mut result = BTreeMap::new();
        for (name, probability) in EXPERT_NAMES.iter().zip(p) {
            result.insert((*name).into(), probability);
        }
        result
    }
}

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

    #[test]
    fn trust_is_zero_outside_active_and_resets_on_rollback() {
        let mut trust = TrustRegion::default();
        trust.update(false, false, 0.0, 1.0);
        assert_eq!(trust.alpha, 0.0);
        trust.update(true, false, 0.0, 1.0);
        assert!(trust.alpha > 0.0);
        trust.update(true, true, 0.0, 1.0);
        assert_eq!(trust.alpha, 0.0);
        assert!(trust.cooldown_remaining > 0);
    }

    #[test]
    fn ips_uses_logged_propensity_and_supports_every_expert() {
        let mut experts = ExpertAggregator::default();
        let probabilities = experts.probabilities();
        let before = experts.cumulative_reward[2];
        experts.update_ips(2, probabilities, 0.5);
        assert!((experts.cumulative_reward[2] - 0.5 / probabilities[2]).abs() < 1e-10);
        assert!(experts.probabilities().iter().all(|p| *p > 0.0));
        assert!(experts.tracking_regret_bound(2).is_finite());
        assert_eq!(before, 0.0);
    }

    #[test]
    fn lyapunov_vetoes_an_increasing_prediction() {
        let certificate = StabilityCertificate::default();
        assert!(certificate.check(0.5, 0.7).lyapunov_veto);
        assert!(!certificate.check(0.5, 0.4).lyapunov_veto);
    }

    #[test]
    fn kl_policy_matches_closed_form_and_zero_beta_is_greedy() {
        let policy = kl_regularized_policy([0.2, 0.8], [0.8, 0.2], 0.5);
        let total = 0.8 * libm::exp(0.2 / 0.5) + 0.2 * libm::exp(0.8 / 0.5);
        assert!((policy[0] - 0.8 * libm::exp(0.2 / 0.5) / total).abs() < 1e-12);
        assert!((policy.iter().sum::<f64>() - 1.0).abs() < 1e-12);
        assert_eq!(
            kl_regularized_policy([0.2, 0.8], [1.0, 0.0], 0.0),
            [0.0, 1.0]
        );
    }

    #[test]
    fn malformed_feedback_fails_closed_without_learning() {
        let mut controller = ResidentController {
            gate_state: L2GateState::Active,
            trust: TrustRegion {
                alpha: 0.2,
                ..TrustRegion::default()
            },
            ..ResidentController::default()
        };
        controller.observe(
            ControlFeedback {
                error: f64::NAN,
                reward: 1.0,
                rollback: false,
                consequence: 0.0,
                kl_policy: [0.5, 0.5],
            },
            L2Policy::default(),
        );
        assert_eq!(controller.observations, 0);
        assert_eq!(controller.experts.rounds, 0);
        assert_eq!(controller.trust.alpha, 0.0);
        assert!(controller.retire_requested);
        controller.finish_cycle();
        assert_eq!(controller.gate_state, L2GateState::Dormant);
    }

    #[test]
    fn prediction_contracts_without_disturbance_and_charges_expert_risk() {
        let controller = ResidentController {
            residual: 0.4,
            dayan_state: 0.4,
            residual_ema: 0.6,
            ema_error: 0.2,
            ..ResidentController::default()
        };
        let predicted = controller.predicted_residual(0.2, 0.2, 1);
        assert!((predicted - 0.65 * 0.4).abs() < 1e-12);
        assert!(!controller.certificate.check(0.4, predicted).lyapunov_veto);
        let risky = controller.predicted_residual(0.2, 1.0, 2);
        assert!(risky > predicted);
        assert!(controller.certificate.check(0.4, risky).lyapunov_veto);
        assert!(controller
            .predicted_residual(0.2, 0.2, KEEP_L0)
            .is_infinite());
    }

    #[test]
    fn retirement_waits_for_feedback_settlement() {
        let mut controller = ResidentController {
            gate_state: L2GateState::Active,
            retire_requested: true,
            trust: TrustRegion {
                alpha: 0.2,
                ..TrustRegion::default()
            },
            ..ResidentController::default()
        };
        controller.finish_cycle();
        assert_eq!(controller.gate_state, L2GateState::Active);
        assert!(controller.retire_requested);
        controller.feedback_settled = true;
        controller.finish_cycle();
        assert_eq!(controller.gate_state, L2GateState::Dormant);
        assert!(!controller.retire_requested);
        assert_eq!(controller.trust.alpha, 0.0);
    }

    #[test]
    fn keep_l0_feedback_uses_actual_selection_propensity() {
        let mut controller = ResidentController::default();
        let probabilities = [0.4, 0.3, 0.2, 0.1];
        controller.experts.last_probabilities = probabilities;
        let before = controller.experts.cumulative_reward;
        controller.observe(
            ControlFeedback {
                error: 0.2,
                reward: 0.5,
                rollback: false,
                consequence: 0.0,
                kl_policy: [1.0, 0.0],
            },
            L2Policy {
                expert_probabilities: probabilities,
                ..L2Policy::default()
            },
        );
        assert!((controller.experts.cumulative_reward[0] - before[0] - 0.5 / 0.4).abs() < 1e-10);
        assert_eq!(controller.experts.last_probabilities, probabilities);
    }

    #[test]
    fn repeated_feedback_is_idempotent() {
        let mut controller = ResidentController::default();
        let feedback = ControlFeedback {
            error: 0.4,
            reward: 0.8,
            rollback: false,
            consequence: 0.1,
            kl_policy: [0.8, 0.2],
        };
        controller.observe(feedback, L2Policy::default());
        let observations = controller.observations;
        let rounds = controller.experts.rounds;
        let ema = controller.ema_error;
        controller.observe(feedback, L2Policy::default());
        assert_eq!(controller.observations, observations);
        assert_eq!(controller.experts.rounds, rounds);
        assert_eq!(controller.ema_error, ema);
    }

    #[test]
    fn fixed_point_residual_contracts_for_frozen_target() {
        let mut controller = ResidentController {
            residual: 0.8,
            dayan_state: 0.8,
            residual_ema: 0.8,
            ..ResidentController::default()
        };
        let mut previous = f64::INFINITY;
        for step in 0..8 {
            let residual = controller.update_dayan(0.8);
            assert!(
                residual < previous,
                "step={step} residual={residual} previous={previous} state={} target={} lambda={}",
                controller.dayan_state,
                controller.residual_ema,
                controller.dayan_relaxation,
            );
            controller.observe(
                ControlFeedback {
                    error: 0.1,
                    reward: 1.0,
                    rollback: false,
                    consequence: 0.1,
                    kl_policy: [1.0, 0.0],
                },
                L2Policy::default(),
            );
            controller.finish_cycle();
            previous = residual;
        }
        assert!(previous < 0.03);
    }

    #[test]
    fn gate_wakes_on_error_and_returns_to_dormant_after_recovery() {
        fn feedback(error: f64, reward: f64) -> ControlFeedback {
            ControlFeedback {
                error,
                reward,
                rollback: false,
                consequence: 0.0,
                kl_policy: [1.0, 0.0],
            }
        }
        let mut controller = ResidentController::default();
        let policy = L2Policy::default();
        for _ in 0..5 {
            controller.observe(feedback(0.1, 1.0), policy);
            controller.finish_cycle();
            controller.residual = 0.3;
            controller.episode_start_residual = 0.6;
        }
        controller.observe(feedback(0.9, 0.0), policy);
        assert_eq!(controller.gate_state, L2GateState::Active);
        assert!(controller.surprise_started);
        controller.residual = 0.6;
        controller.episode_start_residual = 0.6;
        for _ in 0..=controller.calm_required {
            controller.residual = 0.1;
            controller.observe(feedback(0.1, 1.0), policy);
            controller.finish_cycle();
        }
        // A new request must not be finalized before the acting policy's own
        // feedback has settled. In this loop the previous request was already
        // consumed; the next recovery observation starts a fresh cycle.
        assert!(controller.retire_requested || controller.gate_state == L2GateState::Dormant);
        controller.finish_cycle();
        assert_eq!(
            controller.gate_state,
            L2GateState::Dormant,
            "ema={} baseline={} reward={} reward_base={} residual={} calm={}",
            controller.ema_error,
            controller.baseline_error,
            controller.ema_reward,
            controller.baseline_reward,
            controller.residual,
            controller.calm_streak
        );
        assert_eq!(controller.trust.alpha, 0.0);
    }
}