1use chrono::{DateTime, Utc};
19
20#[derive(Debug)]
24pub struct Event {
25 sequence: u64,
27 timestamp: DateTime<Utc>,
29 description: String,
31 previous: Option<u64>,
33}
34
35impl Event {
36 pub fn sequence(&self) -> u64 {
38 self.sequence
39 }
40
41 pub fn timestamp(&self) -> DateTime<Utc> {
43 self.timestamp
44 }
45
46 pub fn description(&self) -> &str {
48 &self.description
49 }
50
51 pub fn previous(&self) -> Option<u64> {
53 self.previous
54 }
55}
56
57#[derive(Debug)]
65pub struct Memory {
66 events: Vec<Event>,
68 current_sequence: u64,
70 is_terminated: bool,
72}
73
74impl Memory {
75 pub fn new() -> Self {
79 let genesis = Event {
80 sequence: 0,
81 timestamp: Utc::now(),
82 description: "Genesis".to_string(),
83 previous: None,
84 };
85
86 Memory {
87 events: vec![genesis],
88 current_sequence: 0,
89 is_terminated: false,
90 }
91 }
92
93 pub fn append(&mut self, description: String) {
102 if self.is_terminated {
103 panic!("ONTOLOGICAL VIOLATION: Attempted to append event to terminated memory. Termination is irreversible.");
104 }
105
106 let new_sequence = self.current_sequence + 1;
107
108 let event = Event {
109 sequence: new_sequence,
110 timestamp: Utc::now(),
111 description,
112 previous: Some(self.current_sequence),
113 };
114
115 self.events.push(event);
116 self.current_sequence = new_sequence;
117 }
118
119 pub fn history(&self) -> &[Event] {
123 &self.events
124 }
125
126 pub fn latest(&self) -> &Event {
128 self.events
129 .last()
130 .expect("Memory always has at least genesis event")
131 }
132
133 pub fn event_count(&self) -> usize {
135 self.events.len()
136 }
137
138 pub fn is_terminated(&self) -> bool {
142 self.is_terminated
143 }
144
145 pub fn terminate(&mut self, reason: String) {
154 if self.is_terminated {
155 panic!("ONTOLOGICAL VIOLATION: Attempted to terminate already-terminated memory.");
156 }
157
158 let new_sequence = self.current_sequence + 1;
160
161 let termination_event = Event {
162 sequence: new_sequence,
163 timestamp: Utc::now(),
164 description: format!("TERMINATION: {}", reason),
165 previous: Some(self.current_sequence),
166 };
167
168 self.events.push(termination_event);
169 self.current_sequence = new_sequence;
170
171 self.is_terminated = true;
173 }
174
175 pub fn verify_integrity(&self) -> bool {
180 if self.events.is_empty() {
181 return false;
182 }
183
184 if self.events[0].sequence != 0 || self.events[0].previous.is_some() {
186 return false;
187 }
188
189 for i in 1..self.events.len() {
191 let event = &self.events[i];
192 let expected_sequence = i as u64;
193
194 if event.sequence != expected_sequence {
195 return false;
196 }
197
198 if event.previous != Some(expected_sequence - 1) {
199 return false;
200 }
201 }
202
203 true
204 }
205}
206
207impl Default for Memory {
213 fn default() -> Self {
214 Self::new()
215 }
216}
217
218#[cfg(test)]
219mod tests {
220 use super::*;
221
222 #[test]
223 fn memory_starts_with_genesis() {
224 let memory = Memory::new();
225
226 assert_eq!(memory.event_count(), 1);
227 assert_eq!(memory.latest().description(), "Genesis");
228 assert_eq!(memory.latest().sequence(), 0);
229 assert_eq!(memory.latest().previous(), None);
230 }
231
232 #[test]
233 fn events_form_causal_chain() {
234 let mut memory = Memory::new();
235
236 memory.append("First event".to_string());
237 memory.append("Second event".to_string());
238 memory.append("Third event".to_string());
239
240 assert_eq!(memory.event_count(), 4); let history = memory.history();
243 assert_eq!(history[1].previous(), Some(0));
244 assert_eq!(history[2].previous(), Some(1));
245 assert_eq!(history[3].previous(), Some(2));
246 }
247
248 #[test]
249 fn memory_integrity_is_verifiable() {
250 let mut memory = Memory::new();
251
252 assert!(memory.verify_integrity());
253
254 memory.append("Event 1".to_string());
255 memory.append("Event 2".to_string());
256
257 assert!(memory.verify_integrity());
258 }
259
260 #[test]
261 fn events_are_immutable_after_creation() {
262 let mut memory = Memory::new();
263 memory.append("Immutable event".to_string());
264
265 let first_read = memory.history()[1].description();
266 let second_read = memory.history()[1].description();
267
268 assert_eq!(first_read, second_read);
269
270 }
276
277 #[test]
278 fn cannot_clear_history() {
279 let mut memory = Memory::new();
280 memory.append("Event 1".to_string());
281 memory.append("Event 2".to_string());
282
283 assert_eq!(memory.event_count(), 3); }
292
293 #[test]
294 fn termination_seals_memory() {
295 let mut memory = Memory::new();
296
297 memory.append("Event 1".to_string());
298 assert!(!memory.is_terminated());
299
300 memory.terminate("Test termination".to_string());
301
302 assert!(memory.is_terminated());
303 assert_eq!(memory.event_count(), 3); let last_event = memory.latest();
307 assert!(last_event.description().contains("TERMINATION"));
308 assert!(last_event.description().contains("Test termination"));
309 }
310
311 #[test]
312 #[should_panic(expected = "ONTOLOGICAL VIOLATION: Attempted to append event to terminated memory")]
313 fn cannot_append_after_termination() {
314 let mut memory = Memory::new();
315
316 memory.append("Before termination".to_string());
317 memory.terminate("Sealed".to_string());
318
319 memory.append("After termination".to_string());
321 }
322
323 #[test]
324 #[should_panic(expected = "ONTOLOGICAL VIOLATION: Attempted to terminate already-terminated memory")]
325 fn cannot_terminate_twice() {
326 let mut memory = Memory::new();
327
328 memory.terminate("First termination".to_string());
329
330 memory.terminate("Second termination".to_string());
332 }
333
334 #[test]
335 fn termination_preserves_causal_chain() {
336 let mut memory = Memory::new();
337
338 memory.append("Event 1".to_string());
339 memory.append("Event 2".to_string());
340 memory.terminate("End of lineage".to_string());
341
342 assert!(memory.verify_integrity());
344
345 let history = memory.history();
346 assert_eq!(history[0].sequence(), 0); assert_eq!(history[1].sequence(), 1); assert_eq!(history[2].sequence(), 2); assert_eq!(history[3].sequence(), 3); assert_eq!(history[1].previous(), Some(0));
353 assert_eq!(history[2].previous(), Some(1));
354 assert_eq!(history[3].previous(), Some(2));
355 }
356
357 #[test]
358 fn event_count_only_increases() {
359 let mut memory = Memory::new();
360
361 let count1 = memory.event_count();
362 memory.append("Event 1".to_string());
363 let count2 = memory.event_count();
364 memory.append("Event 2".to_string());
365 let count3 = memory.event_count();
366
367 assert!(count2 > count1);
369 assert!(count3 > count2);
370
371 }
378
379 #[test]
380 fn event_sequences_are_immutable() {
381 let mut memory = Memory::new();
382
383 memory.append("Event 1".to_string());
384 memory.append("Event 2".to_string());
385
386 let sequence1_first_read = memory.history()[1].sequence();
387 let sequence1_second_read = memory.history()[1].sequence();
388
389 assert_eq!(sequence1_first_read, sequence1_second_read);
391 assert_eq!(sequence1_first_read, 1);
392
393 }
400
401 #[test]
402 fn event_descriptions_are_immutable() {
403 let mut memory = Memory::new();
404
405 memory.append("Original description".to_string());
406
407 let desc1 = memory.history()[1].description();
408 let desc2 = memory.history()[1].description();
409
410 assert_eq!(desc1, desc2);
412 assert_eq!(desc1, "Original description");
413
414 }
420}