chaotic_semantic_memory 0.3.6

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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
//! WASM extension methods: stats, text encoding, graph traversal, metadata ops.
//!
//! Split from `wasm.rs` to keep each file under the 500-LOC project limit.

// Redundant clones are intentional for WASM ownership semantics

#[cfg(target_arch = "wasm32")]
use js_sys::{Array, Function};
#[cfg(target_arch = "wasm32")]
use tokio::sync::broadcast::error::RecvError;
#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*;
#[cfg(target_arch = "wasm32")]
use wasm_bindgen_futures::spawn_local;

#[cfg(target_arch = "wasm32")]
use crate::wasm::{WasmFramework, to_js_error};

#[cfg(target_arch = "wasm32")]
#[wasm_bindgen]
impl WasmFramework {
    /// Get framework stats
    pub async fn stats(&self) -> Result<JsValue, JsValue> {
        let stats = self.framework.stats().await.map_err(to_js_error)?;

        let obj = js_sys::Object::new();
        js_sys::Reflect::set(
            &obj,
            &"concept_count".into(),
            &(stats.concept_count as u32).into(),
        )
        .map_err(|_| JsValue::from_str("failed to set JS property"))?;
        js_sys::Reflect::set(
            &obj,
            &"db_size_bytes".into(),
            &stats
                .db_size_bytes
                .map_or(JsValue::NULL, |v| (v as f64).into()),
        )
        .map_err(|_| JsValue::from_str("failed to set JS property"))?;

        Ok(obj.into())
    }

    /// Get concept count (convenience method)
    pub async fn concept_count(&self) -> Result<usize, JsValue> {
        let sing = self.framework.singularity.read().await;
        let ns = self.framework.namespace().await;
        Ok(sing.len(&ns))
    }

    /// Update a concept's metadata from a JSON string.
    ///
    /// The `metadata_json` argument must be a valid JSON object string,
    /// e.g. `{"category":"science","score":0.9}`.
    ///
    /// Note: In WASM, persistence is in-memory only. Use `exportToBytes` to
    /// snapshot state to IndexedDB or other storage.
    pub async fn update_concept_metadata(
        &self,
        id: String,
        metadata_json: String,
    ) -> Result<(), JsValue> {
        let metadata: std::collections::HashMap<String, serde_json::Value> =
            serde_json::from_str(&metadata_json)
                .map_err(|e| JsValue::from_str(&format!("invalid metadata JSON: {e}")))?;
        self.framework
            .update_concept_metadata(&id, metadata)
            .await
            .map_err(to_js_error)
    }

    /// Clear all outbound associations for a concept.
    pub async fn clear_associations(&self, id: String) -> Result<(), JsValue> {
        let mut sing = self.framework.singularity.write().await;
        let ns = self.framework.namespace().await;
        sing.clear_associations(&ns, &id).map_err(to_js_error)
    }

    /// Get direct neighbors of a concept with edge strengths.
    ///
    /// Returns an Array of `{to: string, strength: number}` objects.
    pub async fn neighbors(&self, id: String, min_strength: f32) -> Result<Array, JsValue> {
        let sing = self.framework.singularity.read().await;
        let ns = self.framework.namespace().await;
        let neighbors = sing.neighbors(&ns, &id, min_strength);
        let array = Array::new();
        for (to, strength) in neighbors {
            let obj = js_sys::Object::new();
            js_sys::Reflect::set(&obj, &"to".into(), &to.into())
                .map_err(|_| JsValue::from_str("failed to set JS property"))?;
            js_sys::Reflect::set(&obj, &"strength".into(), &strength.into())
                .map_err(|_| JsValue::from_str("failed to set JS property"))?;
            array.push(&obj);
        }
        Ok(array)
    }

    /// Probe for similar concepts with metadata filtering.
    pub async fn probe_filtered(
        &self,
        vector: &[u8],
        top_k: usize,
        filter_json: String,
    ) -> Result<Array, JsValue> {
        let query = crate::hyperdim::HVec10240::from_bytes(vector).map_err(to_js_error)?;
        let filter: crate::metadata_filter::MetadataFilter = serde_json::from_str(&filter_json)
            .map_err(|e| JsValue::from_str(&format!("invalid filter JSON: {e}")))?;

        let results = self
            .framework
            .probe_filtered(&query, top_k, &filter)
            .await
            .map_err(to_js_error)?;

        let array = Array::new();
        for (id, score) in results {
            let obj = js_sys::Object::new();
            js_sys::Reflect::set(&obj, &"id".into(), &id.into())
                .map_err(|_| JsValue::from_str("failed to set JS property"))?;
            js_sys::Reflect::set(&obj, &"score".into(), &score.into())
                .map_err(|_| JsValue::from_str("failed to set JS property"))?;
            array.push(&obj);
        }

        Ok(array)
    }

    /// Register a callback for memory events.
    pub fn on_event(&self, callback: Function) {
        let mut receiver = self.framework.subscribe();
        spawn_local(async move {
            loop {
                match receiver.recv().await {
                    Ok(event) => {
                        let js_event = memory_event_to_js_value(&event);
                        let _ = callback.call1(&JsValue::NULL, &js_event);
                    }
                    Err(RecvError::Lagged(_)) => continue,
                    Err(RecvError::Closed) => break,
                }
            }
        });
    }

    /// Inject a concept from text
    pub async fn inject_text(&self, id: String, text: String) -> Result<(), JsValue> {
        self.framework
            .inject_text(&id, &text)
            .await
            .map_err(to_js_error)
    }

    /// Probe for similar concepts using text
    pub async fn probe_text(&self, query: String, top_k: usize) -> Result<Array, JsValue> {
        let results = self
            .framework
            .probe_text(&query, top_k)
            .await
            .map_err(to_js_error)?;

        let array = Array::new();
        for (id, similarity) in results {
            let obj = js_sys::Object::new();
            js_sys::Reflect::set(&obj, &"id".into(), &id.into())
                .map_err(|_| JsValue::from_str("failed to set JS property"))?;
            js_sys::Reflect::set(&obj, &"similarity".into(), &similarity.into())
                .map_err(|_| JsValue::from_str("failed to set JS property"))?;
            array.push(&obj);
        }

        Ok(array)
    }
}

#[cfg(target_arch = "wasm32")]
fn memory_event_to_js_value(event: &crate::framework_events::MemoryEvent) -> JsValue {
    let obj = js_sys::Object::new();
    match event {
        crate::framework_events::MemoryEvent::ConceptInjected { id, timestamp } => {
            let _ = js_sys::Reflect::set(&obj, &"type".into(), &"ConceptInjected".into());
            let _ = js_sys::Reflect::set(&obj, &"id".into(), &id.clone().into());
            let _ = js_sys::Reflect::set(&obj, &"timestamp".into(), &(*timestamp as f64).into());
        }
        crate::framework_events::MemoryEvent::ConceptUpdated { id, timestamp } => {
            let _ = js_sys::Reflect::set(&obj, &"type".into(), &"ConceptUpdated".into());
            let _ = js_sys::Reflect::set(&obj, &"id".into(), &id.clone().into());
            let _ = js_sys::Reflect::set(&obj, &"timestamp".into(), &(*timestamp as f64).into());
        }
        crate::framework_events::MemoryEvent::ConceptDeleted { id, timestamp } => {
            let _ = js_sys::Reflect::set(&obj, &"type".into(), &"ConceptDeleted".into());
            let _ = js_sys::Reflect::set(&obj, &"id".into(), &id.clone().into());
            let _ = js_sys::Reflect::set(&obj, &"timestamp".into(), &(*timestamp as f64).into());
        }
        crate::framework_events::MemoryEvent::Associated { from, to, strength } => {
            let _ = js_sys::Reflect::set(&obj, &"type".into(), &"Associated".into());
            let _ = js_sys::Reflect::set(&obj, &"from".into(), &from.clone().into());
            let _ = js_sys::Reflect::set(&obj, &"to".into(), &to.clone().into());
            let _ = js_sys::Reflect::set(&obj, &"strength".into(), &(*strength as f64).into());
        }
        crate::framework_events::MemoryEvent::Disassociated { from, to } => {
            let _ = js_sys::Reflect::set(&obj, &"type".into(), &"Disassociated".into());
            let _ = js_sys::Reflect::set(&obj, &"from".into(), &from.clone().into());
            let _ = js_sys::Reflect::set(&obj, &"to".into(), &to.clone().into());
        }
    }
    obj.into()
}

// TESTS - verify WASM binding data patterns on native targets

#[cfg(test)]
mod tests {
    // Exact float comparisons for test assertions

    use crate::framework_builder::FrameworkBuilder;
    use crate::framework_events::MemoryEvent;
    use crate::graph_traversal::TraversalConfig;
    use crate::hyperdim::HVec10240;
    use crate::metadata_filter::MetadataFilter;
    use serde_json::json;
    use std::collections::HashMap;

    // to_js_error is a string conversion that works on native targets too
    fn to_js_error_test(msg: &str) -> bool {
        !msg.is_empty()
    }

    #[test]
    fn metadata_filter_eq_json_roundtrip() {
        let filter = MetadataFilter::eq("type", "document");
        let json = serde_json::to_string(&filter).unwrap();
        let parsed: MetadataFilter = serde_json::from_str(&json).unwrap();
        assert_eq!(filter, parsed);
    }

    #[test]
    fn metadata_filter_in_json_roundtrip() {
        let filter = MetadataFilter::in_("tag", vec![json!("rust"), json!("python")]);
        let json = serde_json::to_string(&filter).unwrap();
        let parsed: MetadataFilter = serde_json::from_str(&json).unwrap();
        assert_eq!(filter, parsed);
    }

    #[test]
    fn metadata_filter_exists_json_roundtrip() {
        let filter = MetadataFilter::exists("title");
        let json = serde_json::to_string(&filter).unwrap();
        let parsed: MetadataFilter = serde_json::from_str(&json).unwrap();
        assert_eq!(filter, parsed);
    }

    #[test]
    fn metadata_filter_and_json_roundtrip() {
        let filter = MetadataFilter::and(vec![
            MetadataFilter::eq("type", "document"),
            MetadataFilter::exists("author"),
        ]);
        let json = serde_json::to_string(&filter).unwrap();
        let parsed: MetadataFilter = serde_json::from_str(&json).unwrap();
        assert_eq!(filter, parsed);
    }

    #[test]
    fn metadata_filter_or_json_roundtrip() {
        let filter = MetadataFilter::or(vec![
            MetadataFilter::eq("status", "active"),
            MetadataFilter::eq("status", "pending"),
        ]);
        let json = serde_json::to_string(&filter).unwrap();
        let parsed: MetadataFilter = serde_json::from_str(&json).unwrap();
        assert_eq!(filter, parsed);
    }

    #[test]
    fn metadata_filter_not_json_roundtrip() {
        let filter = MetadataFilter::Not(Box::new(MetadataFilter::eq("private", true)));
        let json = serde_json::to_string(&filter).unwrap();
        let parsed: MetadataFilter = serde_json::from_str(&json).unwrap();
        assert_eq!(filter, parsed);
    }

    #[test]
    fn metadata_filter_nested_complex_json_roundtrip() {
        let filter = MetadataFilter::and(vec![
            MetadataFilter::eq("type", "document"),
            MetadataFilter::in_("tag", vec![json!("rust"), json!("python")]),
            MetadataFilter::Not(Box::new(MetadataFilter::eq("private", true))),
        ]);
        let json = serde_json::to_string(&filter).unwrap();
        let parsed: MetadataFilter = serde_json::from_str(&json).unwrap();
        assert_eq!(filter, parsed);
    }

    #[test]
    fn metadata_filter_json_string_format() {
        let filter = MetadataFilter::eq("category", "science");
        let json = serde_json::to_string(&filter).unwrap();
        assert!(json.contains("Eq") && json.contains("category") && json.contains("science"));
    }

    #[test]
    fn traversal_config_defaults() {
        let config = TraversalConfig::default();
        assert_eq!(config.max_depth, 3);
        assert!((config.min_strength - (0.0)).abs() < 1e-6);
        assert_eq!(config.max_results, 100);
    }

    #[test]
    fn traversal_config_custom_values() {
        let config = TraversalConfig {
            max_depth: 5,
            min_strength: 0.7,
            ..Default::default()
        };
        assert_eq!(config.max_depth, 5);
        assert!((config.min_strength - (0.7)).abs() < 1e-6);
    }

    #[test]
    fn hvec_bytes_roundtrip() {
        let original = HVec10240::random();
        let bytes = original.to_bytes();
        let restored = HVec10240::from_bytes(&bytes).unwrap();
        assert_eq!(original, restored);
    }

    #[test]
    fn hvec_bytes_length() {
        let hvec = HVec10240::random();
        let bytes = hvec.to_bytes();
        assert_eq!(bytes.len(), 1280); // 10240 bits / 8 = 1280 bytes
    }

    #[test]
    fn hvec_from_bytes_invalid_length() {
        let short_bytes = vec![0u8; 100];
        assert!(HVec10240::from_bytes(&short_bytes).is_err());
    }

    #[test]
    fn memory_event_variants_construct() {
        let injected = MemoryEvent::ConceptInjected {
            id: "test-id".to_string(),
            timestamp: 12345,
        };
        let updated = MemoryEvent::ConceptUpdated {
            id: "test-id".to_string(),
            timestamp: 12346,
        };
        let deleted = MemoryEvent::ConceptDeleted {
            id: "test-id".to_string(),
            timestamp: 12347,
        };
        let associated = MemoryEvent::Associated {
            from: "a".to_string(),
            to: "b".to_string(),
            strength: 0.8,
        };
        let disassociated = MemoryEvent::Disassociated {
            from: "a".to_string(),
            to: "b".to_string(),
        };

        assert!(matches!(injected, MemoryEvent::ConceptInjected { .. }));
        assert!(format!("{updated:?}").contains("ConceptUpdated"));
        assert!(format!("{deleted:?}").contains("ConceptDeleted"));
        assert!(format!("{associated:?}").contains("Associated"));
        assert!(format!("{disassociated:?}").contains("Disassociated"));
    }

    #[test]
    fn memory_event_clone_preserves_data() {
        let event = MemoryEvent::Associated {
            from: "source".to_string(),
            to: "target".to_string(),
            strength: 0.95,
        };
        let cloned = event;
        match cloned {
            MemoryEvent::Associated { from, to, strength } => {
                assert_eq!(from, "source");
                assert_eq!(to, "target");
                assert!((strength - 0.95).abs() < 0.001);
            }
            _ => panic!("Expected Associated variant"),
        }
    }

    #[test]
    fn metadata_filter_matches_empty_metadata() {
        let filter = MetadataFilter::eq("type", "document");
        let empty_metadata = HashMap::new();
        assert!(!filter.matches(&empty_metadata));
    }

    #[test]
    fn metadata_filter_exists_on_empty_metadata() {
        let filter = MetadataFilter::exists("field");
        let empty_metadata = HashMap::new();
        assert!(!filter.matches(&empty_metadata));
    }

    #[test]
    fn wasm_hvec_bytes_roundtrip() {
        let v = HVec10240::random();
        let bytes = v.to_bytes();
        let v2 = HVec10240::from_bytes(&bytes).unwrap();
        assert_eq!(v, v2);
    }

    #[test]
    fn wasm_hvec_bytes_invalid_len() {
        assert!(HVec10240::from_bytes(&[0u8; 100]).is_err());
    }

    #[test]
    fn wasm_to_js_error_msg() {
        assert!(to_js_error_test("test error"));
    }

    #[tokio::test]
    async fn wasm_namespace_switching_isolates_concepts() {
        let framework = FrameworkBuilder::new()
            .without_persistence()
            .build()
            .await
            .unwrap();

        // 1. Inject into default namespace
        framework
            .inject_concept("default-concept", HVec10240::random())
            .await
            .unwrap();

        // 2. Switch namespace
        framework.set_namespace("tenant-a").await;
        assert_eq!(framework.namespace().await, "tenant-a");

        // 3. Verify default concept is not visible in tenant-a
        let results = framework.probe(HVec10240::random(), 10).await.unwrap();
        assert!(results.is_empty());

        // 4. Inject into tenant-a
        framework
            .inject_concept("tenant-concept", HVec10240::random())
            .await
            .unwrap();
        let results = framework.probe(HVec10240::random(), 10).await.unwrap();
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].0, "tenant-concept");

        // 5. Switch back to default
        framework.set_namespace("_default").await;
        assert_eq!(framework.namespace().await, "_default");

        // 6. Verify only default concept is visible
        let results = framework.probe(HVec10240::random(), 10).await.unwrap();
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].0, "default-concept");
    }

    fn native_enc(t: &str) -> Box<[u8]> {
        crate::encoder::TextEncoder::new()
            .encode(t)
            .to_bytes()
            .into_boxed_slice()
    }

    #[test]
    fn wasm_encode_text_consistency() {
        assert_eq!(native_enc("Test"), native_enc("Test"));
    }

    #[test]
    fn wasm_encode_text_length() {
        assert_eq!(native_enc("Len").len(), 1280);
    }

    #[test]
    fn wasm_encode_text_difference() {
        assert_ne!(native_enc("A"), native_enc("B"));
    }

    #[test]
    fn wasm_encode_text_empty() {
        assert_eq!(native_enc("").len(), 1280);
    }
}