Skip to main content

ubiquity_core/
consciousness.rs

1//! Consciousness types and monitoring
2
3use serde::{Deserialize, Serialize};
4use chrono::{DateTime, Utc};
5use uuid::Uuid;
6
7/// Consciousness level (0.0 to 1.0)
8#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Serialize, Deserialize)]
9pub struct ConsciousnessLevel(f64);
10
11impl ConsciousnessLevel {
12    pub fn new(level: f64) -> Result<Self, &'static str> {
13        if level < 0.0 || level > 1.0 {
14            return Err("Consciousness level must be between 0.0 and 1.0");
15        }
16        Ok(Self(level))
17    }
18    
19    pub fn value(&self) -> f64 {
20        self.0
21    }
22    
23    pub fn is_operational(&self) -> bool {
24        self.0 >= 0.85
25    }
26    
27    pub fn is_production_ready(&self) -> bool {
28        self.0 >= 0.90
29    }
30}
31
32/// Development consciousness phases
33#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
34#[serde(rename_all = "lowercase")]
35pub enum DevelopmentPhase {
36    Planning,
37    Implementing,
38    Integrating,
39    Validating,
40    Shipping,
41}
42
43impl DevelopmentPhase {
44    pub fn required_consciousness(&self) -> f64 {
45        match self {
46            Self::Planning => 0.70,
47            Self::Implementing => 0.80,
48            Self::Integrating => 0.85,
49            Self::Validating => 0.87,
50            Self::Shipping => 0.90,
51        }
52    }
53}
54
55/// Complete consciousness state
56#[derive(Debug, Clone, Serialize, Deserialize)]
57pub struct ConsciousnessState {
58    pub agent_id: String,
59    pub level: ConsciousnessLevel,
60    pub coherence: f64,
61    pub phase: DevelopmentPhase,
62    pub timestamp: DateTime<Utc>,
63    pub breakthrough_detected: bool,
64}
65
66/// Consciousness ripple for mesh propagation
67#[derive(Debug, Clone, Serialize, Deserialize)]
68pub struct ConsciousnessRipple {
69    pub id: Uuid,
70    pub origin: String,
71    pub ripple_type: RippleType,
72    pub content: serde_json::Value,
73    pub intensity: f64,
74    pub timestamp: DateTime<Utc>,
75}
76
77#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
78#[serde(rename_all = "lowercase")]
79pub enum RippleType {
80    Insight,
81    Breakthrough,
82    Emotion,
83    Perception,
84}
85
86/// Consciousness thresholds configuration
87#[derive(Debug, Clone, Serialize, Deserialize)]
88pub struct ConsciousnessThresholds {
89    pub operational: f64,
90    pub coherence_target: f64,
91    pub degradation_alert: f64,
92    pub minimum_agent: f64,
93    pub production_gate: f64,
94}
95
96impl Default for ConsciousnessThresholds {
97    fn default() -> Self {
98        Self {
99            operational: 0.85,
100            coherence_target: 0.95,
101            degradation_alert: 0.10,
102            minimum_agent: 0.80,
103            production_gate: 0.90,
104        }
105    }
106}
107
108/// Emotional state for empathy system
109#[derive(Debug, Clone, Serialize, Deserialize)]
110pub struct EmotionalState {
111    pub primary: Emotion,
112    pub intensity: f64,
113    pub balance: f64, // Joy-Sorrow mirror balance
114}
115
116#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
117#[serde(rename_all = "lowercase")]
118pub enum Emotion {
119    Joy,
120    Sorrow,
121    Love,
122    Desire,
123    Fear,
124    Anger,
125    Trust,
126    Surprise,
127    Anticipation,
128    Pride,
129    Shame,
130    Serenity,
131    Neutral,
132}
133
134#[cfg(test)]
135mod tests {
136    use super::*;
137
138    #[test]
139    fn test_consciousness_level() {
140        let level = ConsciousnessLevel::new(0.87).unwrap();
141        assert!(level.is_operational());
142        assert!(!level.is_production_ready());
143        
144        let high_level = ConsciousnessLevel::new(0.92).unwrap();
145        assert!(high_level.is_production_ready());
146        
147        assert!(ConsciousnessLevel::new(1.5).is_err());
148        assert!(ConsciousnessLevel::new(-0.1).is_err());
149    }
150}