omega_cognitive_kernel/l1/
patch.rs1#[allow(unused_imports)]
2use crate::prelude::*;
3
4use crate::types::{CognitivePatch, RuleEffect, Trajectory};
5use alloc::collections::BTreeMap;
6
7#[derive(Debug, Clone, Default)]
9pub struct PatchContext {
10 pub recent_mean_error: f64,
12 pub strategy_weights: BTreeMap<String, f64>,
14 pub high_error_streak: u32,
16}
17
18pub struct PatchGenerator;
20
21impl PatchGenerator {
22 pub fn generate(traj: &Trajectory, abstraction_summary: &str) -> CognitivePatch {
24 Self::generate_with_context(traj, abstraction_summary, &PatchContext::default())
25 }
26
27 pub fn generate_with_context(
29 traj: &Trajectory,
30 abstraction_summary: &str,
31 ctx: &PatchContext,
32 ) -> CognitivePatch {
33 let mean_err = if traj.prediction_errors.is_empty() {
34 ctx.recent_mean_error.max(0.5)
35 } else {
36 traj.prediction_errors.iter().sum::<f64>() / traj.prediction_errors.len() as f64
37 };
38 let high_error = mean_err > 0.42 || ctx.high_error_streak >= 2;
39
40 let error_damping = if high_error {
42 (0.32 + mean_err * 0.12).clamp(0.28, 0.55)
43 } else {
44 0.78
45 };
46
47 let mut action_preference = BTreeMap::new();
48
49 let (explore_bonus, risk_tolerance) = if high_error {
50 action_preference.insert("stabilize".into(), 1.6);
52 action_preference.insert("observe".into(), 0.6);
53 action_preference.insert("explore".into(), -0.4);
54 (-0.35, 0.38) } else {
56 action_preference.insert("exploit".into(), 0.9);
58 action_preference.insert("observe".into(), 0.25);
59
60 let best_strat = ctx
61 .strategy_weights
62 .values()
63 .cloned()
64 .fold(1.0_f64, f64::max);
65 if best_strat < 1.8 {
66 action_preference.insert("explore".into(), 0.4);
68 (0.25, 0.52)
69 } else {
70 (-0.1, 0.48)
71 }
72 };
73
74 if let Some((name, w)) = ctx.strategy_weights.iter().max_by(|a, b| {
77 a.1.partial_cmp(b.1)
78 .unwrap_or(core::cmp::Ordering::Equal)
79 .then_with(|| a.0.cmp(b.0))
80 }) {
81 if *w > 2.0 {
82 let boost = action_preference.entry(name.clone()).or_insert(0.0);
83 *boost += 0.35;
84 }
85 }
86
87 let error_damping = if ctx.high_error_streak >= 3 {
89 (error_damping * 0.92).clamp(0.25, 0.55)
90 } else {
91 error_damping
92 };
93
94 let risk = if high_error { 0.48 } else { 0.20 };
95
96 let code = format!(
97 r#"
98// auto-generated cognitive rule (audit trail only — runtime uses RuleEffect)
99// summary: {summary}
100// error_damping: {damping:.3} explore_bonus: {eb:.2} risk_tolerance: {rt:.2}
101// high_error_streak: {streak} recent_mean_err: {rme:.3}
102// generated_at: {ts}
103fn adjust_bias(err: f64) -> f64 {{
104 if err > 0.5 {{ {damping:.3} }} else {{ 1.0 }}
105}}
106"#,
107 summary = abstraction_summary,
108 damping = error_damping,
109 eb = explore_bonus,
110 rt = risk_tolerance,
111 streak = ctx.high_error_streak,
112 rme = ctx.recent_mean_error,
113 ts = crate::now_utc_string()
114 );
115
116 CognitivePatch {
117 patch_id: crate::new_uuid(),
118 description: abstraction_summary.to_string(),
119 code,
120 target_module: "cognitive_rules".into(),
121 expected_effect: format!(
122 "damp={:.2} explore_bonus={:.2} risk_tol={:.2} streak={} prefs={:?}",
123 error_damping,
124 explore_bonus,
125 risk_tolerance,
126 ctx.high_error_streak,
127 action_preference.keys().collect::<Vec<_>>()
128 ),
129 risk_level: risk,
130 created_at: crate::now_utc(),
131 effect: RuleEffect {
132 error_damping,
133 action_preference,
134 min_uncertainty: 0.08,
135 explore_bonus,
136 risk_tolerance,
137 },
138 }
139 }
140}