omega_cognitive_kernel/l2/
fixed_point.rs1#[allow(unused_imports)]
2use crate::prelude::*;
3
4use crate::error::Result;
5use crate::types::SystemState;
6use alloc::collections::BTreeMap;
7use serde::{Deserialize, Serialize};
8
9#[derive(Debug, Clone, Serialize, Deserialize)]
10pub struct FixedPointResult {
12 pub mode: String,
14 pub aligned: bool,
16 pub alignment_score: f64,
18 pub score: f64,
20 pub breakdown: BTreeMap<String, f64>,
22 pub modulation: Modulation,
24}
25
26#[derive(Debug, Clone, Default, Serialize, Deserialize)]
27pub struct Modulation {
29 pub bias_delta: BTreeMap<String, f64>,
31 pub constraint_delta: BTreeMap<String, f64>,
33 pub explore_bonus_delta: f64,
35 pub risk_tolerance_delta: f64,
37}
38
39#[derive(Debug, Clone, Copy, Default)]
40pub struct ScoreSignals {
42 pub reward_change_score: f64,
44 pub error_change_score: f64,
46 pub stability_score: f64,
48}
49
50pub struct OmegaDayanFixedPoint;
52
53impl OmegaDayanFixedPoint {
54 pub fn enter(&self, state: &SystemState) -> Result<FixedPointResult> {
56 self.enter_with_signals(state, ScoreSignals::default())
57 }
58
59 pub fn enter_with_signals(
61 &self,
62 state: &SystemState,
63 signals: ScoreSignals,
64 ) -> Result<FixedPointResult> {
65 let (alignment_score, score, breakdown) = self.align(
66 &state.highest_attractor,
67 state.generative_base.as_deref(),
68 &state.active_code,
69 state.avg_prediction_error,
70 state.total_steps,
71 signals,
72 );
73
74 let mut modulation = Modulation::default();
75 if alignment_score > 0.82 {
76 modulation.bias_delta.insert("self_ref".into(), 0.18);
77 modulation.bias_delta.insert("coherence".into(), 0.12);
78 modulation.constraint_delta.insert("max_risk".into(), -0.04);
79 modulation
80 .bias_delta
81 .insert("explore_preference".into(), -0.08);
82 modulation.explore_bonus_delta = -0.06;
83 modulation.risk_tolerance_delta = -0.03;
84 } else {
85 modulation
86 .bias_delta
87 .insert("observation_depth".into(), 0.14);
88 modulation.constraint_delta.insert("max_risk".into(), 0.03);
89 modulation
90 .bias_delta
91 .insert("explore_preference".into(), 0.06);
92 modulation.explore_bonus_delta = 0.05;
93 modulation.risk_tolerance_delta = 0.02;
94 }
95
96 Ok(FixedPointResult {
97 mode: "observation_equals_observed".into(),
98 aligned: alignment_score > 0.82,
99 alignment_score,
100 score,
101 breakdown,
102 modulation,
103 })
104 }
105
106 fn align(
107 &self,
108 omega: &str,
109 dayan: Option<&str>,
110 active_code: &BTreeMap<String, String>,
111 avg_error: f64,
112 total_steps: u64,
113 signals: ScoreSignals,
114 ) -> (f64, f64, BTreeMap<String, f64>) {
115 let mut alignment = 0.30;
116
117 let omega_l = omega.to_lowercase();
118 if omega_l.contains("self")
119 || omega_l.contains("coherent")
120 || omega_l.contains("omega")
121 || omega_l.contains("dayan")
122 {
123 alignment += 0.18;
124 }
125
126 if let Some(base) = dayan {
127 let base_l = base.to_lowercase();
128 if base_l.contains("generative")
129 || base_l.contains("cognitive_rules")
130 || base_l.contains("dayan")
131 {
132 alignment += 0.14;
133 }
134
135 let omega_chars: hashbrown::HashSet<char> = omega.chars().collect();
136 let overlap = base.chars().filter(|c| omega_chars.contains(c)).count() as f64;
137 let denom = omega.len().max(base.len()).max(1) as f64;
138 alignment += (overlap / denom) * 0.18;
139 }
140
141 if active_code.contains_key("cognitive_rules") {
142 alignment += 0.14;
143 if let Some(code) = active_code.get("cognitive_rules") {
144 let cl = code.to_lowercase();
145 if cl.contains("self") || cl.contains("bias") || cl.contains("damp") {
146 alignment += 0.06;
147 }
148 }
149 }
150
151 if !active_code.is_empty() {
152 alignment += 0.05;
153 }
154
155 if total_steps > 8 && avg_error < 0.35 {
156 alignment += 0.08;
157 } else if avg_error > 0.6 {
158 alignment -= 0.06;
159 }
160
161 let alignment = alignment.clamp(0.0, 1.0);
162 let reward = signals.reward_change_score.clamp(0.0, 1.0);
163 let error = signals.error_change_score.clamp(0.0, 1.0);
164 let stability = signals.stability_score.clamp(0.0, 1.0);
165 let score =
166 (alignment * 0.45 + reward * 0.30 + error * 0.15 + stability * 0.10).clamp(0.0, 1.0);
167 let mut breakdown = BTreeMap::new();
168 breakdown.insert("alignment".into(), alignment * 0.45);
169 breakdown.insert("reward_change".into(), reward * 0.30);
170 breakdown.insert("error_change".into(), error * 0.15);
171 breakdown.insert("stability".into(), stability * 0.10);
172 breakdown.insert("total".into(), score);
173 (alignment, score, breakdown)
174 }
175}
176
177#[cfg(test)]
178mod tests {
179 use super::*;
180 use crate::types::SystemState;
181 use alloc::collections::BTreeMap;
182
183 fn state(
184 omega: &str,
185 dayan: Option<&str>,
186 has_rules: bool,
187 err: f64,
188 steps: u64,
189 ) -> SystemState {
190 let mut active = BTreeMap::new();
191 if has_rules {
192 active.insert(
193 "cognitive_rules".into(),
194 "fn adjust_bias(err: f64) -> f64 { err * 0.8 } // self damp".into(),
195 );
196 }
197 SystemState {
198 active_code: active,
199 highest_attractor: omega.into(),
200 generative_base: dayan.map(|s| s.into()),
201 last_trajectory_summary: None,
202 avg_prediction_error: err,
203 total_steps: steps,
204 }
205 }
206
207 #[test]
208 fn higher_score_when_aligned_keywords_present() {
209 let fp = OmegaDayanFixedPoint;
210 let s1 = state(
211 "maintain coherent self-model omega",
212 Some("dayan_generative_base"),
213 true,
214 0.2,
215 20,
216 );
217 let s2 = state("random goal", None, false, 0.7, 2);
218 let r1 = fp.enter(&s1).unwrap();
219 let r2 = fp.enter(&s2).unwrap();
220 assert!(r1.score > r2.score);
221 }
222
223 #[test]
224 fn aligned_produces_negative_explore_nudge() {
225 let fp = OmegaDayanFixedPoint;
226 let s = state(
227 "coherent self omega",
228 Some("dayan generative"),
229 true,
230 0.2,
231 20,
232 );
233 let r = fp.enter(&s).unwrap();
234 if r.aligned {
235 assert!(r.modulation.explore_bonus_delta <= 0.0);
236 }
237 }
238
239 #[test]
240 fn produces_modulation() {
241 let fp = OmegaDayanFixedPoint;
242 let s = state("coherent self", Some("cognitive_rules"), true, 0.25, 15);
243 let r = fp.enter(&s).unwrap();
244 assert!(!r.modulation.bias_delta.is_empty() || !r.modulation.constraint_delta.is_empty());
245 }
246
247 #[test]
248 fn score_is_clamped() {
249 let fp = OmegaDayanFixedPoint;
250 let s = state(
251 "self coherent generative omega dayan",
252 Some("cognitive_rules generative dayan"),
253 true,
254 0.1,
255 50,
256 );
257 let r = fp.enter(&s).unwrap();
258 assert!(r.score >= 0.0 && r.score <= 1.0);
259 }
260
261 #[test]
262 fn external_change_signals_raise_candidate_score() {
263 let fp = OmegaDayanFixedPoint;
264 let s = state(
265 "coherent self omega",
266 Some("dayan generative"),
267 true,
268 0.2,
269 20,
270 );
271 let calm = fp.enter(&s).unwrap();
272 let alarm = fp
273 .enter_with_signals(
274 &s,
275 ScoreSignals {
276 reward_change_score: 1.0,
277 error_change_score: 1.0,
278 stability_score: 0.6,
279 },
280 )
281 .unwrap();
282 assert!(alarm.score > calm.score);
283 assert!(alarm.breakdown.contains_key("reward_change"));
284 }
285}