omega-cognitive-kernel 0.15.2

Online strategy-patch runtime: training-free RuleEffect adaptation with audit and rollback
Documentation
#[allow(unused_imports)]
use crate::prelude::*;

use crate::l2::fixed_point::FixedPointResult;
use crate::types::RuleEffect;
use alloc::collections::BTreeMap;

/// Data structure representing meta modulator.
pub struct MetaModulator;

impl MetaModulator {
    /// Performs apply for this type.
    pub fn apply(
        &self,
        fp: &FixedPointResult,
        mut biases: BTreeMap<String, f64>,
        mut constraints: BTreeMap<String, f64>,
    ) -> (BTreeMap<String, f64>, BTreeMap<String, f64>) {
        for (k, v) in &fp.modulation.bias_delta {
            *biases.entry(k.clone()).or_insert(0.0) += *v;
        }
        for (k, v) in &fp.modulation.constraint_delta {
            let entry = constraints.entry(k.clone()).or_insert(0.65);
            *entry = (*entry + *v).clamp(0.1, 0.95);
        }
        (biases, constraints)
    }

    /// Performs apply scaled for this type.
    pub fn apply_scaled(
        &self,
        fp: &FixedPointResult,
        gain: f64,
        mut biases: BTreeMap<String, f64>,
        mut constraints: BTreeMap<String, f64>,
    ) -> (BTreeMap<String, f64>, BTreeMap<String, f64>) {
        let gain = gain.clamp(0.0, 1.0);
        for (k, v) in &fp.modulation.bias_delta {
            *biases.entry(k.clone()).or_insert(0.0) += *v * gain;
        }
        for (k, v) in &fp.modulation.constraint_delta {
            let entry = constraints.entry(k.clone()).or_insert(0.65);
            *entry = (*entry + *v * gain).clamp(0.1, 0.95);
        }
        (biases, constraints)
    }

    /// Soft-merge fixed-point deltas into a RuleEffect, then sanitize.
    /// Deltas are intentionally small so L1 patches remain the primary policy writer.
    pub fn apply_to_effect(&self, fp: &FixedPointResult, effect: &RuleEffect) -> RuleEffect {
        let mut e = effect.clone();
        e.explore_bonus += fp.modulation.explore_bonus_delta;
        e.risk_tolerance += fp.modulation.risk_tolerance_delta;
        e.sanitize()
    }

    /// Performs apply to effect scaled for this type.
    pub fn apply_to_effect_scaled(
        &self,
        fp: &FixedPointResult,
        gain: f64,
        effect: &RuleEffect,
    ) -> RuleEffect {
        let mut e = effect.clone();
        let gain = gain.clamp(0.0, 1.0);
        e.explore_bonus += fp.modulation.explore_bonus_delta * gain;
        e.risk_tolerance += fp.modulation.risk_tolerance_delta * gain;
        e.sanitize()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::l2::fixed_point::Modulation;

    #[test]
    fn soft_merge_clamps_via_sanitize() {
        let modu = MetaModulator;
        let fp = FixedPointResult {
            mode: "t".into(),
            aligned: true,
            alignment_score: 0.9,
            score: 0.9,
            breakdown: BTreeMap::new(),
            modulation: Modulation {
                bias_delta: BTreeMap::new(),
                constraint_delta: BTreeMap::new(),
                explore_bonus_delta: 5.0, // way out of bounds
                risk_tolerance_delta: -2.0,
            },
        };
        let base = RuleEffect::default();
        let merged = modu.apply_to_effect(&fp, &base);
        assert!(merged.explore_bonus <= 1.5);
        assert!(merged.risk_tolerance >= 0.15);
    }
}