Skip to main content

omega_cognitive_kernel/l2/
monitor.rs

1#[allow(unused_imports)]
2use crate::prelude::*;
3
4use crate::error::Result;
5use crate::types::{MetaMetrics, SystemState};
6
7/// Data structure representing meta monitor.
8pub struct MetaMonitor;
9
10impl MetaMonitor {
11    /// Performs evaluate for this type.
12    pub fn evaluate(&self, state: &SystemState) -> Result<MetaMetrics> {
13        let complexity =
14            (state.active_code.len() as f64 * 0.12) + (state.total_steps as f64 * 0.01);
15
16        // Data-driven consistency: lower when prediction error is high or volatile.
17        // Use a soft inverse of avg_prediction_error + presence of generative base.
18        let err = state.avg_prediction_error.clamp(0.0, 1.0);
19        let mut consistency = (1.0 - err * 0.85).clamp(0.25, 0.95);
20        if state.generative_base.is_some() {
21            consistency = (consistency + 0.08).min(0.95);
22        }
23        if state.active_code.contains_key("cognitive_rules") {
24            consistency = (consistency + 0.05).min(0.95);
25        }
26
27        let predictive_power = (1.0 - state.avg_prediction_error).clamp(0.2, 0.95);
28
29        let self_ref_stability = if state.generative_base.is_some() {
30            0.58 + (state.active_code.len() as f64 * 0.03).min(0.3)
31        } else {
32            0.32
33        };
34
35        Ok(MetaMetrics {
36            complexity,
37            consistency,
38            predictive_power,
39            self_ref_stability,
40            timestamp: crate::now_utc(),
41        })
42    }
43}
44
45#[cfg(test)]
46mod tests {
47    use super::*;
48    use alloc::collections::BTreeMap;
49
50    fn state(err: f64, has_base: bool, has_rules: bool) -> SystemState {
51        let mut active = BTreeMap::new();
52        if has_rules {
53            active.insert("cognitive_rules".into(), "fn x() {}".into());
54        }
55        SystemState {
56            active_code: active,
57            highest_attractor: "test".into(),
58            generative_base: if has_base { Some("dayan".into()) } else { None },
59            last_trajectory_summary: None,
60            avg_prediction_error: err,
61            total_steps: 5,
62        }
63    }
64
65    #[test]
66    fn higher_error_lowers_consistency() {
67        let m = MetaMonitor;
68        let high = m.evaluate(&state(0.8, false, false)).unwrap();
69        let low = m.evaluate(&state(0.1, false, false)).unwrap();
70        assert!(low.consistency > high.consistency);
71    }
72
73    #[test]
74    fn generative_base_boosts_consistency() {
75        let m = MetaMonitor;
76        let with = m.evaluate(&state(0.3, true, true)).unwrap();
77        let without = m.evaluate(&state(0.3, false, false)).unwrap();
78        assert!(with.consistency > without.consistency);
79    }
80}