Skip to main content

agentic_reality/types/
coherence.rs

1//! Coherence maintenance types — "How do I stay grounded?"
2
3use serde::{Deserialize, Serialize};
4
5use super::ids::{ContextId, TransitionId};
6
7/// Overall coherence state across all reality domains.
8#[derive(Debug, Clone, Serialize, Deserialize)]
9pub struct CoherenceState {
10    pub level: CoherenceLevel,
11    pub checks: Vec<CoherenceCheck>,
12    pub violations: Vec<CoherenceViolation>,
13    pub strategies: Vec<CoherenceStrategy>,
14    pub history: Vec<CoherenceEvent>,
15}
16
17/// Level of coherence.
18#[derive(Debug, Clone, Serialize, Deserialize)]
19pub enum CoherenceLevel {
20    Full { confidence: f64 },
21    Minor { issues: Vec<String> },
22    Significant { issues: Vec<String>, impact: String },
23    Incoherent { reason: String, severity: f64 },
24}
25
26impl std::fmt::Display for CoherenceLevel {
27    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
28        match self {
29            Self::Full { .. } => write!(f, "full"),
30            Self::Minor { .. } => write!(f, "minor"),
31            Self::Significant { .. } => write!(f, "significant"),
32            Self::Incoherent { .. } => write!(f, "incoherent"),
33        }
34    }
35}
36
37/// A coherence check performed.
38#[derive(Debug, Clone, Serialize, Deserialize)]
39pub struct CoherenceCheck {
40    pub check_type: CoherenceCheckType,
41    pub passed: bool,
42    pub details: String,
43    pub timestamp: i64,
44    pub duration_ms: u64,
45}
46
47#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
48pub enum CoherenceCheckType {
49    TimeConsistency,
50    StateConsistency,
51    LayerConsistency,
52    AnchorConsistency,
53    MemoryConsistency,
54    CausalConsistency,
55}
56
57impl std::fmt::Display for CoherenceCheckType {
58    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
59        match self {
60            Self::TimeConsistency => write!(f, "time"),
61            Self::StateConsistency => write!(f, "state"),
62            Self::LayerConsistency => write!(f, "layer"),
63            Self::AnchorConsistency => write!(f, "anchor"),
64            Self::MemoryConsistency => write!(f, "memory"),
65            Self::CausalConsistency => write!(f, "causal"),
66        }
67    }
68}
69
70/// A detected coherence violation.
71#[derive(Debug, Clone, Serialize, Deserialize)]
72pub struct CoherenceViolation {
73    pub violation_type: CoherenceCheckType,
74    pub detected: i64,
75    pub severity: f64,
76    pub evidence: Vec<String>,
77    pub possible_causes: Vec<String>,
78    pub resolution: Option<String>,
79    pub description: String,
80}
81
82/// Strategy for maintaining coherence.
83#[derive(Debug, Clone, Serialize, Deserialize)]
84pub struct CoherenceStrategy {
85    pub name: String,
86    pub description: String,
87    pub active: bool,
88    pub effectiveness: f64,
89}
90
91/// Historical coherence event.
92#[derive(Debug, Clone, Serialize, Deserialize)]
93pub struct CoherenceEvent {
94    pub event_type: CoherenceEventType,
95    pub timestamp: i64,
96    pub details: String,
97}
98
99#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
100pub enum CoherenceEventType {
101    CheckPassed,
102    CheckFailed,
103    ViolationDetected,
104    ViolationResolved,
105    TransitionStarted,
106    TransitionCompleted,
107}
108
109/// Context transition state.
110#[derive(Debug, Clone, Serialize, Deserialize)]
111pub struct TransitionState {
112    pub current: ContextId,
113    pub pending: Vec<PendingTransition>,
114    pub history: Vec<CompletedTransition>,
115    pub rules: Vec<TransitionRule>,
116}
117
118/// A pending context transition.
119#[derive(Debug, Clone, Serialize, Deserialize)]
120pub struct PendingTransition {
121    pub id: TransitionId,
122    pub from: ContextId,
123    pub to_context: String,
124    pub transition_type: TransitionType,
125    pub phase: TransitionPhase,
126    pub carry_over: Vec<String>,
127    pub started: i64,
128}
129
130#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
131pub enum TransitionType {
132    EnvironmentChange,
133    LayerChange,
134    ScaleEvent,
135    Migration,
136    Failover,
137    Upgrade,
138    Restoration,
139}
140
141impl std::fmt::Display for TransitionType {
142    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
143        match self {
144            Self::EnvironmentChange => write!(f, "environment_change"),
145            Self::LayerChange => write!(f, "layer_change"),
146            Self::ScaleEvent => write!(f, "scale_event"),
147            Self::Migration => write!(f, "migration"),
148            Self::Failover => write!(f, "failover"),
149            Self::Upgrade => write!(f, "upgrade"),
150            Self::Restoration => write!(f, "restoration"),
151        }
152    }
153}
154
155#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
156pub enum TransitionPhase {
157    Planning,
158    Preparing,
159    Validating,
160    Executing,
161    Verifying,
162    Complete,
163    Failed,
164    RollingBack,
165}
166
167impl std::fmt::Display for TransitionPhase {
168    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
169        match self {
170            Self::Planning => write!(f, "planning"),
171            Self::Preparing => write!(f, "preparing"),
172            Self::Validating => write!(f, "validating"),
173            Self::Executing => write!(f, "executing"),
174            Self::Verifying => write!(f, "verifying"),
175            Self::Complete => write!(f, "complete"),
176            Self::Failed => write!(f, "failed"),
177            Self::RollingBack => write!(f, "rolling_back"),
178        }
179    }
180}
181
182/// A completed transition.
183#[derive(Debug, Clone, Serialize, Deserialize)]
184pub struct CompletedTransition {
185    pub id: TransitionId,
186    pub transition_type: TransitionType,
187    pub started: i64,
188    pub completed: i64,
189    pub success: bool,
190    pub summary: String,
191}
192
193/// Rules governing transitions.
194#[derive(Debug, Clone, Serialize, Deserialize)]
195pub struct TransitionRule {
196    pub name: String,
197    pub condition: String,
198    pub action: String,
199    pub priority: u32,
200}