#[allow(unused_imports)]
use crate::prelude::*;
use crate::types::{CognitivePatch, RuleEffect, Trajectory};
use hashbrown::HashMap;
#[derive(Debug, Clone, Default)]
pub struct PatchContext {
pub recent_mean_error: f64,
pub strategy_weights: HashMap<String, f64>,
pub high_error_streak: u32,
}
pub struct PatchGenerator;
impl PatchGenerator {
pub fn generate(traj: &Trajectory, abstraction_summary: &str) -> CognitivePatch {
Self::generate_with_context(traj, abstraction_summary, &PatchContext::default())
}
pub fn generate_with_context(
traj: &Trajectory,
abstraction_summary: &str,
ctx: &PatchContext,
) -> CognitivePatch {
let mean_err = if traj.prediction_errors.is_empty() {
ctx.recent_mean_error.max(0.5)
} else {
traj.prediction_errors.iter().sum::<f64>() / traj.prediction_errors.len() as f64
};
let high_error = mean_err > 0.42 || ctx.high_error_streak >= 2;
let error_damping = if high_error {
(0.32 + mean_err * 0.12).clamp(0.28, 0.55)
} else {
0.78
};
let mut action_preference = HashMap::new();
let (explore_bonus, risk_tolerance) = if high_error {
action_preference.insert("stabilize".into(), 1.6);
action_preference.insert("observe".into(), 0.6);
action_preference.insert("explore".into(), -0.4);
(-0.35, 0.38) } else {
action_preference.insert("exploit".into(), 0.9);
action_preference.insert("observe".into(), 0.25);
let best_strat = ctx
.strategy_weights
.values()
.cloned()
.fold(1.0_f64, f64::max);
if best_strat < 1.8 {
action_preference.insert("explore".into(), 0.4);
(0.25, 0.52)
} else {
(-0.1, 0.48)
}
};
if let Some((name, w)) = ctx.strategy_weights.iter().max_by(|a, b| {
a.1.partial_cmp(b.1)
.unwrap_or(core::cmp::Ordering::Equal)
.then_with(|| a.0.cmp(b.0))
}) {
if *w > 2.0 {
let boost = action_preference.entry(name.clone()).or_insert(0.0);
*boost += 0.35;
}
}
let error_damping = if ctx.high_error_streak >= 3 {
(error_damping * 0.92).clamp(0.25, 0.55)
} else {
error_damping
};
let risk = if high_error { 0.48 } else { 0.20 };
let code = format!(
r#"
// auto-generated cognitive rule (audit trail only — runtime uses RuleEffect)
// summary: {summary}
// error_damping: {damping:.3} explore_bonus: {eb:.2} risk_tolerance: {rt:.2}
// high_error_streak: {streak} recent_mean_err: {rme:.3}
// generated_at: {ts}
fn adjust_bias(err: f64) -> f64 {{
if err > 0.5 {{ {damping:.3} }} else {{ 1.0 }}
}}
"#,
summary = abstraction_summary,
damping = error_damping,
eb = explore_bonus,
rt = risk_tolerance,
streak = ctx.high_error_streak,
rme = ctx.recent_mean_error,
ts = crate::now_utc_string()
);
CognitivePatch {
patch_id: crate::new_uuid(),
description: abstraction_summary.to_string(),
code,
target_module: "cognitive_rules".into(),
expected_effect: format!(
"damp={:.2} explore_bonus={:.2} risk_tol={:.2} streak={} prefs={:?}",
error_damping,
explore_bonus,
risk_tolerance,
ctx.high_error_streak,
action_preference.keys().collect::<Vec<_>>()
),
risk_level: risk,
created_at: crate::now_utc(),
effect: RuleEffect {
error_damping,
action_preference,
min_uncertainty: 0.08,
explore_bonus,
risk_tolerance,
},
}
}
}