chaotic_semantic_memory 0.3.8

AI memory systems with hyperdimensional vectors and chaotic reservoirs
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
//! Framework persistence operations.
//!
//! Extracted from framework.rs to satisfy the 500 LOC gate.
//! ADR-0093: rows are authoritative; ANN snapshots are revisioned derivatives;
//! state locks are not held across persistence I/O.

use tracing::warn;

use crate::framework::ChaoticSemanticFramework;
use crate::framework_events::MemoryEvent;
use crate::index_envelope::{IndexSnapshotEnvelope, backend_fingerprint};
use crate::singularity::{Concept, ConceptDiff, ConceptVersion};
use csm_core_lib::error::{MemoryError, Result};

impl ChaoticSemanticFramework {
    /// Persist all data to storage (checkpoint + revisioned ANN snapshot).
    #[tracing::instrument(err, skip(self))]
    pub async fn persist(&self) -> Result<()> {
        if let Some(ref persistence) = self.persistence {
            #[cfg(not(target_arch = "wasm32"))]
            let p_start = std::time::Instant::now();

            // Snapshot under short locks; re-check revision after unlock so we never
            // stamp a current revision onto an incomplete index (TOCTOU, ADR-0093).
            let ns = self.namespace.read().await.clone();
            let rev_before = persistence.get_namespace_revision(&ns).await?;
            let (index_data, fingerprint) = {
                let sing = self.singularity.read().await;
                let fingerprint = backend_fingerprint(&sing.config.index_backend);
                let index_data = match sing.get_namespace(&ns) {
                    Some(ns_state) => match ns_state.index.serialize() {
                        Ok(data) if !data.is_empty() => Some(data),
                        Ok(_) => None,
                        Err(e) => {
                            warn!(error = %e, "ANN index serialize failed; skipping envelope");
                            None
                        }
                    },
                    None => None,
                };
                drop(sing);
                (index_data, fingerprint)
            };

            if let Some(index_data) = index_data {
                let rev_after = persistence.get_namespace_revision(&ns).await?;
                if rev_after == rev_before {
                    let envelope = IndexSnapshotEnvelope::new(rev_after, fingerprint, index_data);
                    persistence
                        .save_index_envelope(&ns, "main", &envelope)
                        .await?;
                } else {
                    warn!(
                        rev_before,
                        rev_after,
                        "namespace revision moved during ANN snapshot; skipping envelope write"
                    );
                }
            }

            persistence.checkpoint().await?;
            #[cfg(not(target_arch = "wasm32"))]
            {
                self.metrics.observe_persist_latency_ms(
                    u64::try_from(p_start.elapsed().as_millis()).unwrap_or(u64::MAX),
                    "persist",
                );
            }
        }
        Ok(())
    }

    /// Verify persistence connectivity.
    #[tracing::instrument(err, skip(self))]
    pub async fn persistence_health_check(&self) -> Result<()> {
        if let Some(ref persistence) = self.persistence {
            persistence.health_check().await?;
        }
        Ok(())
    }

    /// Load and replace all in-memory state from persistence.
    ///
    /// Clears existing state, loads persisted state. Use for fresh starts.
    /// See also: [`load_merge`](Self::load_merge) for additive semantics.
    // Lock held for inject + rebuild after all I/O is complete (ADR-0093).
    #[allow(clippy::significant_drop_tightening)]
    #[tracing::instrument(err, skip(self))]
    pub async fn load_replace(&self) -> Result<()> {
        if let Some(ref persistence) = self.persistence {
            #[cfg(not(target_arch = "wasm32"))]
            let p_start = std::time::Instant::now();

            // Namespace string + all durable I/O without holding singularity locks.
            let ns = self.namespace.read().await.clone();
            let concepts = persistence.load_all_concepts(&ns).await?;
            for concept in &concepts {
                self.validate_concept(concept)?;
            }
            let all_associations = persistence.load_all_associations(&ns).await?;
            let revision = persistence.get_namespace_revision(&ns).await?;
            // Corrupt/legacy envelopes degrade to rebuild (rows authoritative).
            let envelope = match persistence.load_index_envelope(&ns, "main").await {
                Ok(env) => env,
                Err(e) => {
                    warn!(
                        error = %e,
                        "ANN envelope unreadable; rebuilding index from concept rows"
                    );
                    None
                }
            };

            let expected_fingerprint = {
                let sing = self.singularity.read().await;
                backend_fingerprint(&sing.config.index_backend)
            };

            {
                let mut sing = self.singularity.write().await;
                sing.clear(&ns);
                for concept in concepts {
                    sing.inject(&ns, concept)?;
                }
                for (from_id, to_id, strength, created_at) in all_associations {
                    let ns_state = sing.ensure_namespace(&ns)?;
                    let neighbors = ns_state.associations.entry(from_id).or_default();
                    neighbors.insert(to_id, (strength, created_at));
                }

                let apply_snapshot = envelope.as_ref().is_some_and(|env| {
                    env.namespace_revision == revision
                        && env.backend_fingerprint == expected_fingerprint
                });

                if apply_snapshot {
                    if let Some(env) = envelope {
                        let ns_state = sing.ensure_namespace(&ns)?;
                        if let Err(e) = ns_state.index.deserialize(&env.index_data) {
                            warn!(error = %e, "ANN snapshot deserialize failed; rebuilding");
                            let concepts_map = ns_state.concepts.clone();
                            ns_state.index.rebuild(&concepts_map)?;
                        }
                    }
                } else {
                    if envelope.is_some() {
                        warn!(
                            revision,
                            expected = %expected_fingerprint,
                            "rejecting stale or incompatible ANN snapshot; rebuilding from rows"
                        );
                    }
                    let ns_state = sing.ensure_namespace(&ns)?;
                    let concepts_map = ns_state.concepts.clone();
                    ns_state.index.rebuild(&concepts_map)?;
                }
            }

            #[cfg(not(target_arch = "wasm32"))]
            {
                self.metrics.observe_persist_latency_ms(
                    u64::try_from(p_start.elapsed().as_millis()).unwrap_or(u64::MAX),
                    "load",
                );
            }
        }
        Ok(())
    }

    /// Load and merge persisted state into in-memory state.
    ///
    /// Preserves existing state, adds persisted state on top.
    /// Never applies a persisted-only ANN snapshot over the merged union (ADR-0093).
    // Lock held for inject + rebuild after I/O (ADR-0093).
    #[allow(clippy::significant_drop_tightening)]
    #[tracing::instrument(err, skip(self))]
    pub async fn load_merge(&self) -> Result<()> {
        if let Some(ref persistence) = self.persistence {
            let ns = self.namespace.read().await.clone();
            let concepts = persistence.load_all_concepts(&ns).await?;
            for concept in &concepts {
                self.validate_concept(concept)?;
            }
            let all_associations = persistence.load_all_associations(&ns).await?;

            {
                let mut sing = self.singularity.write().await;
                for concept in &concepts {
                    if sing.get(&ns, &concept.id).is_some() {
                        warn!(
                            concept_id = %concept.id,
                            "skipping persisted concept during load_merge because id already exists in memory"
                        );
                        continue;
                    }
                    sing.inject(&ns, (*concept).clone())?;
                }
                for (from_id, to_id, strength, created_at) in all_associations {
                    let ns_state = sing.ensure_namespace(&ns)?;
                    let neighbors = ns_state.associations.entry(from_id).or_default();
                    neighbors.insert(to_id, (strength, created_at));
                }
                // Rebuild from final union; never replace with a persisted-only snapshot.
                let ns_state = sing.ensure_namespace(&ns)?;
                let concepts_map = ns_state.concepts.clone();
                ns_state.index.rebuild(&concepts_map)?;
            }
        }
        Ok(())
    }

    /// Batch durable inject (ADR-0093): commit all rows, then apply memory.
    #[allow(clippy::significant_drop_tightening)] // sequential inject under one write lock
    pub(crate) async fn durable_inject_concepts(&self, concepts: &[Concept]) -> Result<()> {
        if concepts.is_empty() {
            return Ok(());
        }
        if let Some(ref persistence) = self.persistence {
            let ns = self.namespace().await;
            persistence.save_concepts(&ns, concepts).await?;
            let mut sing = self.singularity.write().await;
            for concept in concepts {
                if let Err(e) = sing.inject(&ns, concept.clone()) {
                    drop(sing);
                    warn!(
                        error = %e,
                        "post-commit batch inject failed; reloading namespace"
                    );
                    self.reload_namespace_from_rows(&ns).await?;
                    return Ok(());
                }
            }
        } else {
            let mut sing = self.singularity.write().await;
            let ns = self.namespace.read().await;
            for concept in concepts {
                sing.inject(&ns, concept.clone())?;
            }
        }
        Ok(())
    }

    /// Commit a concept to durable storage first, then apply to memory (ADR-0093).
    ///
    /// Persistence failures leave in-memory state unchanged. A rare post-commit
    /// memory apply failure reloads the namespace from authoritative rows.
    pub(crate) async fn durable_inject_concept(&self, concept: Concept) -> Result<()> {
        if let Some(ref persistence) = self.persistence {
            let ns = self.namespace().await;
            persistence.save_concept(&ns, &concept).await?;
            let mut sing = self.singularity.write().await;
            if let Err(e) = sing.inject(&ns, concept.clone()) {
                drop(sing);
                warn!(
                    error = %e,
                    concept_id = %concept.id,
                    "post-commit memory inject failed; reloading namespace from durable rows"
                );
                // Durable commit succeeded; reconcile memory and return Ok so
                // callers do not treat a successful write as failure.
                self.reload_namespace_from_rows(&ns).await?;
            }
        } else {
            let mut sing = self.singularity.write().await;
            let ns = self.namespace.read().await;
            sing.inject(&ns, concept)?;
        }
        Ok(())
    }

    /// Durable delete: commit row removal first, then memory (ADR-0093).
    pub(crate) async fn durable_delete_concept(&self, id: &str) -> Result<()> {
        if let Some(ref persistence) = self.persistence {
            let ns = self.namespace().await;
            persistence.delete_concept(&ns, id).await?;
            let mut sing = self.singularity.write().await;
            if let Err(e) = sing.delete(&ns, id) {
                drop(sing);
                warn!(
                    error = %e,
                    concept_id = %id,
                    "post-commit memory delete failed; reloading namespace from durable rows"
                );
                self.reload_namespace_from_rows(&ns).await?;
                return Err(MemoryError::database(format!(
                    "in-memory delete failed after durable commit for concept {id}; namespace reloaded: {e}"
                )));
            }
        } else {
            let mut sing = self.singularity.write().await;
            let ns = self.namespace.read().await;
            sing.delete(&ns, id)?;
        }
        Ok(())
    }

    /// Reload one namespace from authoritative concept/association rows.
    #[allow(clippy::significant_drop_tightening)] // inject + rebuild under one write lock
    pub(crate) async fn reload_namespace_from_rows(&self, ns: &str) -> Result<()> {
        let Some(ref persistence) = self.persistence else {
            return Ok(());
        };
        let concepts = persistence.load_all_concepts(ns).await?;
        let associations = persistence.load_all_associations(ns).await?;
        let mut sing = self.singularity.write().await;
        sing.clear(ns);
        for concept in concepts {
            sing.inject(ns, concept)?;
        }
        for (from_id, to_id, strength, created_at) in associations {
            let ns_state = sing.ensure_namespace(ns)?;
            let neighbors = ns_state.associations.entry(from_id).or_default();
            neighbors.insert(to_id, (strength, created_at));
        }
        let ns_state = sing.ensure_namespace(ns)?;
        let concepts_map = ns_state.concepts.clone();
        ns_state.index.rebuild(&concepts_map)?;
        Ok(())
    }

    /// List all historical versions of a concept.
    #[tracing::instrument(err, skip(self))]
    pub async fn list_versions(&self, id: &str) -> Result<Vec<ConceptVersion>> {
        if let Some(ref persistence) = self.persistence {
            let ns = self.namespace.read().await;
            persistence.list_versions_scoped(&ns, id).await
        } else {
            Err(MemoryError::UnsupportedOperation(
                "Persistence is required for version history".to_string(),
            ))
        }
    }

    /// Load a specific concept version.
    #[tracing::instrument(err, skip(self))]
    pub async fn get_version(&self, id: &str, version: u64) -> Result<Option<Concept>> {
        if let Some(ref persistence) = self.persistence {
            let ns = self.namespace.read().await;
            persistence.get_version_scoped(&ns, id, version).await
        } else {
            Err(MemoryError::UnsupportedOperation(
                "Persistence is required to retrieve a concept version".to_string(),
            ))
        }
    }

    /// Calculate differences between two versions of a concept.
    #[tracing::instrument(err, skip(self))]
    pub async fn diff_versions(
        &self,
        id: &str,
        from_version: u64,
        to_version: u64,
    ) -> Result<ConceptDiff> {
        let from_concept =
            self.get_version(id, from_version)
                .await?
                .ok_or_else(|| MemoryError::NotFound {
                    entity: "ConceptVersion".to_string(),
                    id: format!("{id}@{from_version}"),
                })?;
        let to_concept =
            self.get_version(id, to_version)
                .await?
                .ok_or_else(|| MemoryError::NotFound {
                    entity: "ConceptVersion".to_string(),
                    id: format!("{id}@{to_version}"),
                })?;
        Ok(ConceptDiff::calculate(&from_concept, &to_concept))
    }

    /// Roll back a concept to a historical version.
    /// Rollbacks must not delete history; they must inject the old concept state as a new head version.
    #[tracing::instrument(err, skip(self))]
    pub async fn rollback_to_version(&self, id: &str, version: u64) -> Result<Concept> {
        let mut target_concept =
            self.get_version(id, version)
                .await?
                .ok_or_else(|| MemoryError::NotFound {
                    entity: "ConceptVersion".to_string(),
                    id: format!("{id}@{version}"),
                })?;

        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map_err(|e| MemoryError::database(format!("System clock error: {e}")))?
            .as_secs();
        target_concept.modified_at = now;

        #[cfg(not(target_arch = "wasm32"))]
        let p_start = std::time::Instant::now();
        self.durable_inject_concept(target_concept.clone()).await?;
        #[cfg(not(target_arch = "wasm32"))]
        if self.persistence.is_some() {
            self.metrics.observe_persist_latency_ms(
                u64::try_from(p_start.elapsed().as_millis()).unwrap_or(u64::MAX),
                "save",
            );
        }

        self.emit_event(MemoryEvent::ConceptInjected {
            id: target_concept.id.clone(),
            timestamp: target_concept.modified_at,
        })
        .await;

        Ok(target_concept)
    }
}