agentic_reality/engine/write/
coherence.rs1use crate::engine::write::WriteEngine;
4use crate::types::coherence::*;
5use crate::types::error::{RealityError, RealityResult};
6use crate::types::ids::{ContextId, TransitionId};
7
8impl<'a> WriteEngine<'a> {
9 pub fn run_coherence_check(&mut self, check: CoherenceCheck) -> RealityResult<()> {
11 let state = self
12 .engine
13 .coherence_store
14 .state
15 .get_or_insert_with(|| CoherenceState {
16 level: CoherenceLevel::Full { confidence: 1.0 },
17 checks: vec![],
18 violations: vec![],
19 strategies: vec![],
20 history: vec![],
21 });
22 state.checks.push(check);
23 self.engine.mark_dirty();
24 Ok(())
25 }
26
27 pub fn record_violation(&mut self, violation: CoherenceViolation) -> RealityResult<()> {
29 let state = self
30 .engine
31 .coherence_store
32 .state
33 .get_or_insert_with(|| CoherenceState {
34 level: CoherenceLevel::Full { confidence: 1.0 },
35 checks: vec![],
36 violations: vec![],
37 strategies: vec![],
38 history: vec![],
39 });
40 state.violations.push(violation);
41 let unresolved = state
43 .violations
44 .iter()
45 .filter(|v| v.resolution.is_none())
46 .count();
47 if unresolved > 2 {
48 state.level = CoherenceLevel::Significant {
49 issues: state
50 .violations
51 .iter()
52 .filter(|v| v.resolution.is_none())
53 .map(|v| v.description.clone())
54 .collect(),
55 impact: "multiple unresolved violations".into(),
56 };
57 } else if unresolved > 0 {
58 state.level = CoherenceLevel::Minor {
59 issues: state
60 .violations
61 .iter()
62 .filter(|v| v.resolution.is_none())
63 .map(|v| v.description.clone())
64 .collect(),
65 };
66 }
67 self.engine.mark_dirty();
68 Ok(())
69 }
70
71 pub fn resolve_violation(&mut self, idx: usize, resolution: String) -> RealityResult<()> {
73 let state = self
74 .engine
75 .coherence_store
76 .state
77 .as_mut()
78 .ok_or_else(|| RealityError::NotInitialized("coherence state".into()))?;
79 let violation = state
80 .violations
81 .get_mut(idx)
82 .ok_or_else(|| RealityError::NotFound(format!("violation index {}", idx)))?;
83 violation.resolution = Some(resolution);
84 let unresolved = state
86 .violations
87 .iter()
88 .filter(|v| v.resolution.is_none())
89 .count();
90 if unresolved == 0 {
91 state.level = CoherenceLevel::Full { confidence: 0.9 };
92 }
93 self.engine.mark_dirty();
94 Ok(())
95 }
96
97 pub fn begin_transition(
99 &mut self,
100 transition: PendingTransition,
101 ) -> RealityResult<TransitionId> {
102 let id = transition.id;
103 let trans = self
104 .engine
105 .coherence_store
106 .transitions
107 .get_or_insert_with(|| TransitionState {
108 current: ContextId::new(),
109 pending: vec![],
110 history: vec![],
111 rules: vec![],
112 });
113 trans.pending.push(transition);
114 self.engine.mark_dirty();
115 Ok(id)
116 }
117
118 pub fn advance_transition(
120 &mut self,
121 id: &TransitionId,
122 phase: TransitionPhase,
123 ) -> RealityResult<()> {
124 let trans = self
125 .engine
126 .coherence_store
127 .transitions
128 .as_mut()
129 .ok_or_else(|| RealityError::NotInitialized("transition state".into()))?;
130 let pending = trans
131 .pending
132 .iter_mut()
133 .find(|t| t.id == *id)
134 .ok_or_else(|| RealityError::NotFound(format!("transition {}", id)))?;
135 pending.phase = phase;
136 self.engine.mark_dirty();
137 Ok(())
138 }
139
140 pub fn complete_transition(&mut self, id: &TransitionId) -> RealityResult<()> {
142 let trans = self
143 .engine
144 .coherence_store
145 .transitions
146 .as_mut()
147 .ok_or_else(|| RealityError::NotInitialized("transition state".into()))?;
148 let idx = trans
149 .pending
150 .iter()
151 .position(|t| t.id == *id)
152 .ok_or_else(|| RealityError::NotFound(format!("transition {}", id)))?;
153 let pending = trans.pending.remove(idx);
154 trans.history.push(CompletedTransition {
155 id: pending.id,
156 transition_type: pending.transition_type,
157 started: pending.started,
158 completed: crate::types::now_micros() as i64,
159 success: true,
160 summary: format!("Completed {} transition", pending.transition_type),
161 });
162 self.engine.mark_dirty();
163 Ok(())
164 }
165
166 pub fn abort_transition(&mut self, id: &TransitionId) -> RealityResult<()> {
168 let trans = self
169 .engine
170 .coherence_store
171 .transitions
172 .as_mut()
173 .ok_or_else(|| RealityError::NotInitialized("transition state".into()))?;
174 let idx = trans
175 .pending
176 .iter()
177 .position(|t| t.id == *id)
178 .ok_or_else(|| RealityError::NotFound(format!("transition {}", id)))?;
179 let pending = trans.pending.remove(idx);
180 trans.history.push(CompletedTransition {
181 id: pending.id,
182 transition_type: pending.transition_type,
183 started: pending.started,
184 completed: crate::types::now_micros() as i64,
185 success: false,
186 summary: format!("Aborted {} transition", pending.transition_type),
187 });
188 self.engine.mark_dirty();
189 Ok(())
190 }
191
192 pub fn rollback_transition(&mut self, id: &TransitionId) -> RealityResult<()> {
194 self.advance_transition(id, TransitionPhase::RollingBack)?;
195 self.abort_transition(id)
196 }
197}