1#[allow(unused_imports)]
2use crate::prelude::*;
3
4use chrono::{DateTime, Utc};
5use hashbrown::HashMap;
6use serde::{Deserialize, Serialize};
7use uuid::Uuid;
8
9#[derive(Debug, Clone, Serialize, Deserialize)]
10pub enum Modality {
11 Text,
12 Image,
13 Audio,
14 Structured,
15 Internal,
16}
17
18#[derive(Debug, Clone, Serialize, Deserialize)]
19pub struct Observation {
20 pub timestamp: DateTime<Utc>,
21 pub modality: Modality,
22 pub content: serde_json::Value,
23 pub confidence: f64,
24 pub source: String,
25}
26
27#[derive(Debug, Clone, Serialize, Deserialize)]
28pub struct Prediction {
29 pub expected: serde_json::Value,
30 pub uncertainty: f64,
31 pub horizon_secs: f64,
32}
33
34#[derive(Debug, Clone, Serialize, Deserialize)]
35pub struct Intent {
36 pub goal: String,
37 pub priority: f64,
38 pub bias_from_memory: HashMap<String, f64>,
39}
40
41#[derive(Debug, Clone, Serialize, Deserialize)]
42pub struct Action {
43 pub name: String,
44 pub params: HashMap<String, serde_json::Value>,
45 pub constraints_checked: bool,
46}
47
48#[derive(Debug, Clone, Serialize, Deserialize)]
49pub struct Trajectory {
50 pub observations: Vec<Observation>,
51 pub predictions: Vec<Prediction>,
52 pub actions: Vec<Action>,
53 pub outcomes: Vec<serde_json::Value>,
54 pub prediction_errors: Vec<f64>,
55 pub meta: HashMap<String, serde_json::Value>,
56}
57
58#[derive(Debug, Clone, Serialize, Deserialize)]
61pub struct RuleEffect {
62 pub error_damping: f64,
64 pub action_preference: HashMap<String, f64>,
66 pub min_uncertainty: f64,
68 #[serde(default)]
70 pub explore_bonus: f64,
71 #[serde(default = "default_risk_tolerance")]
73 pub risk_tolerance: f64,
74}
75
76fn default_risk_tolerance() -> f64 {
77 0.45
78}
79
80impl Default for RuleEffect {
81 fn default() -> Self {
82 Self {
83 error_damping: 1.0,
84 action_preference: HashMap::new(),
85 min_uncertainty: 0.05,
86 explore_bonus: 0.0,
87 risk_tolerance: 0.45,
88 }
89 }
90}
91
92impl RuleEffect {
93 pub const ALLOWED_ACTIONS: &'static [&'static str] =
94 &["noop", "observe", "explore", "exploit", "stabilize"];
95
96 pub fn sanitize(mut self) -> Self {
97 self.error_damping = self.error_damping.clamp(0.2, 1.5);
98 self.min_uncertainty = self.min_uncertainty.clamp(0.01, 0.5);
99 self.explore_bonus = self.explore_bonus.clamp(-1.5, 1.5);
100 self.risk_tolerance = self.risk_tolerance.clamp(0.15, 0.85);
101 self.action_preference.retain(|k, v| {
102 if !Self::ALLOWED_ACTIONS.contains(&k.as_str()) {
103 return false;
104 }
105 *v = v.clamp(-5.0, 5.0);
106 true
107 });
108 self
109 }
110
111 pub fn validate_bounds(&self) -> Option<String> {
112 if !(0.15..=2.0).contains(&self.error_damping) {
113 return Some(format!(
114 "error_damping {:.3} outside [0.15, 2.0]",
115 self.error_damping
116 ));
117 }
118 if !(0.0..=1.0).contains(&self.min_uncertainty) {
119 return Some(format!(
120 "min_uncertainty {:.3} outside [0.0, 1.0]",
121 self.min_uncertainty
122 ));
123 }
124 if !(-2.0..=2.0).contains(&self.explore_bonus) {
125 return Some(format!(
126 "explore_bonus {:.3} outside [-2.0, 2.0]",
127 self.explore_bonus
128 ));
129 }
130 if !(0.1..=0.9).contains(&self.risk_tolerance) {
131 return Some(format!(
132 "risk_tolerance {:.3} outside [0.1, 0.9]",
133 self.risk_tolerance
134 ));
135 }
136 for (k, v) in &self.action_preference {
137 if !Self::ALLOWED_ACTIONS.contains(&k.as_str()) {
138 return Some(format!("unknown action preference key: {k}"));
139 }
140 if !(-10.0..=10.0).contains(v) {
141 return Some(format!("action preference {k}={v} outside [-10, 10]"));
142 }
143 }
144 None
145 }
146}
147
148#[derive(Debug, Clone, Serialize, Deserialize)]
149pub struct CognitivePatch {
150 pub patch_id: Uuid,
151 pub description: String,
152 pub code: String,
153 pub target_module: String,
154 pub expected_effect: String,
155 pub risk_level: f64,
156 pub created_at: DateTime<Utc>,
157 pub effect: RuleEffect,
158}
159
160#[derive(Debug, Clone, Serialize, Deserialize)]
161pub struct MetaMetrics {
162 pub complexity: f64,
163 pub consistency: f64,
164 pub predictive_power: f64,
165 pub self_ref_stability: f64,
166 pub timestamp: DateTime<Utc>,
167}
168
169#[derive(Debug, Clone, Default)]
170pub struct SystemState {
171 pub active_code: HashMap<String, String>,
172 pub highest_attractor: String,
173 pub generative_base: Option<String>,
174 pub last_trajectory_summary: Option<String>,
175 pub avg_prediction_error: f64,
176 pub total_steps: u64,
177}