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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]

use crate::bridge_retrieval::BridgeRetrieval;
use crate::semantic_bridge::{BridgeConfig, CanonicalConcept, ConceptGraph, ScoreBreakdown};
use crate::singularity::{ConceptBuilder, Singularity, SingularityConfig};
use csm_core_lib::encoder::TextEncoder;
use csm_core_lib::hyperdim::HVec10240;

#[test]
fn test_bridge_retrieval_empty_singularity() {
    let encoder = TextEncoder::new();
    let graph = ConceptGraph::new();
    let bridge = BridgeRetrieval::with_defaults(encoder, graph);
    let singularity = Singularity::<HVec10240>::new(SingularityConfig::default());

    let results = bridge
        .query("_default", &singularity, "test query", 10, None)
        .unwrap();
    assert!(results.is_empty());
}

#[test]
fn test_bridge_retrieval_empty_graph() {
    let encoder = TextEncoder::new();
    let graph = ConceptGraph::new();
    let bridge = BridgeRetrieval::with_defaults(encoder.clone(), graph);

    let mut singularity = Singularity::<HVec10240>::new(SingularityConfig::default());
    let concept = ConceptBuilder::new("test-concept")
        .with_vector(encoder.encode("test content"))
        .build()
        .unwrap();
    singularity.inject("_default", concept).unwrap();

    let results = bridge
        .query("_default", &singularity, "test query", 10, None)
        .unwrap();
    assert!(!results.is_empty());
    assert!(results[0].scores.deterministic > 0.0);
    assert!((results[0].scores.concept).abs() < f32::EPSILON);
}

#[test]
fn test_bridge_retrieval_with_expansion() {
    let encoder = TextEncoder::new();
    let mut graph = ConceptGraph::new();

    graph.add_concept(
        CanonicalConcept::new("c1")
            .with_label("agent-memory")
            .with_label("session-context"),
    );

    let bridge = BridgeRetrieval::with_defaults(encoder.clone(), graph);

    let mut singularity = Singularity::<HVec10240>::new(SingularityConfig::default());
    let concept = ConceptBuilder::new("mem-1")
        .with_vector(encoder.encode("session context for AI agent"))
        .build()
        .unwrap();
    singularity.inject("_default", concept).unwrap();

    let results = bridge
        .query("_default", &singularity, "agent memory session", 10, None)
        .unwrap();

    assert!(!results.is_empty());
    assert!(
        results[0]
            .scores
            .evidence
            .contains(&"deterministic_recall".to_string())
    );
}

#[test]
fn test_memory_packet_empty_hits() {
    let encoder = TextEncoder::new();
    let graph = ConceptGraph::new();
    let bridge = BridgeRetrieval::with_defaults(encoder, graph);
    let singularity = Singularity::<HVec10240>::new(SingularityConfig::default());

    let packet = bridge
        .memory_packet("_default", &singularity, "test query", 10, None)
        .unwrap();
    assert!(packet.facts.is_empty());
    assert!(packet.sources.is_empty());
    assert!((packet.confidence).abs() < f32::EPSILON);
}

#[test]
fn test_final_score_weights() {
    let config = BridgeConfig {
        deterministic_weight: 0.6,
        concept_weight: 0.3,
        semantic_weight: 0.1,
        ..Default::default()
    };

    let encoder = TextEncoder::new();
    let graph = ConceptGraph::new();
    let bridge = BridgeRetrieval::new(encoder, graph, config);

    let scores = ScoreBreakdown {
        deterministic: 1.0,
        concept: 1.0,
        semantic: 1.0,
        final_score: 0.0,
        evidence: vec!["test".to_string()],
    };

    let final_score = bridge.compute_final_score(&scores);
    assert!((final_score - 1.0).abs() < 1e-6);
}

/// Kills compute_final_score -> 1.0 mutant: partial scores must NOT yield 1.0.
#[test]
fn test_final_score_partial_deterministic_only() {
    let config = BridgeConfig {
        deterministic_weight: 0.6,
        concept_weight: 0.3,
        semantic_weight: 0.1,
        ..Default::default()
    };
    let bridge = BridgeRetrieval::new(TextEncoder::new(), ConceptGraph::new(), config);
    let scores = ScoreBreakdown {
        deterministic: 0.5,
        concept: 0.0,
        semantic: 0.0,
        final_score: 0.0,
        evidence: vec![],
    };
    let final_score = bridge.compute_final_score(&scores);
    assert!(
        (final_score - 0.3).abs() < 1e-5,
        "expected 0.3, got {final_score}"
    );
}

/// Kills query `||` -> `&&` mutant: top_k == 0 with non-empty singularity must return empty.
#[test]
fn test_query_top_k_zero_returns_empty() {
    let encoder = TextEncoder::new();
    let bridge = BridgeRetrieval::with_defaults(encoder.clone(), ConceptGraph::new());
    let mut singularity = Singularity::<HVec10240>::new(SingularityConfig::default());
    let concept = ConceptBuilder::new("c1")
        .with_vector(encoder.encode("hello world"))
        .build()
        .unwrap();
    singularity.inject("_default", concept).unwrap();
    let results = bridge
        .query("_default", &singularity, "hello", 0, None)
        .unwrap();
    assert!(results.is_empty(), "top_k=0 must yield empty results");
}

/// Kills query `||` -> `&&`: empty namespace with top_k > 0 must also short-circuit.
#[test]
fn test_query_empty_namespace_returns_empty() {
    let encoder = TextEncoder::new();
    let bridge = BridgeRetrieval::with_defaults(encoder, ConceptGraph::new());
    let singularity = Singularity::<HVec10240>::new(SingularityConfig::default());
    let results = bridge
        .query("_default", &singularity, "anything", 5, None)
        .unwrap();
    assert!(
        results.is_empty(),
        "empty namespace must yield empty results"
    );
}

/// Kills compile_packet `delete !` in dedup: duplicate facts must appear only once.
#[test]
fn test_compile_packet_deduplicates_facts() {
    let encoder = TextEncoder::new();
    let bridge = BridgeRetrieval::with_defaults(encoder.clone(), ConceptGraph::new());
    let mut singularity = Singularity::<HVec10240>::new(SingularityConfig::default());

    for id in &["c1", "c2"] {
        let concept = ConceptBuilder::new(*id)
            .with_vector(encoder.encode("shared text"))
            .with_metadata(
                "_text",
                serde_json::Value::String("duplicate fact".to_string()),
            )
            .build()
            .unwrap();
        singularity.inject("_default", concept).unwrap();
    }

    let hits = bridge
        .query("_default", &singularity, "shared text", 10, None)
        .unwrap();
    let packet = bridge
        .memory_packet("_default", &singularity, "shared text", 10, None)
        .unwrap();
    assert!(!hits.is_empty(), "expected hits for matching concepts");
    let dup_count = packet
        .facts
        .iter()
        .filter(|f| *f == "duplicate fact")
        .count();
    assert_eq!(dup_count, 1, "duplicate facts must be deduplicated");
}

/// Kills `/->*` at estimated=ceil(count/0.75): a 3-word fact costs ceil(3/0.75)=4 tokens,
/// which exceeds budget=3; with `*` it costs ceil(3*0.75)=3 which would fit.
#[test]
fn test_compile_packet_token_budget_estimation_division() {
    // budget=3; "one two three" => ceil(3/0.75)=4 > 3 => excluded
    // with `/->*`: ceil(3*0.75)=3 <= 3 => included (mutant survives if test weak)
    let config = BridgeConfig {
        token_budget: 3,
        max_packet_facts: 20,
        ..Default::default()
    };
    let encoder = TextEncoder::new();
    let bridge = BridgeRetrieval::new(encoder.clone(), ConceptGraph::new(), config);
    let mut singularity = Singularity::<HVec10240>::new(SingularityConfig::default());

    let text3 = "one two three";
    let c = ConceptBuilder::new("c1")
        .with_vector(encoder.encode(text3))
        .with_metadata("_text", serde_json::Value::String(text3.to_string()))
        .build()
        .unwrap();
    singularity.inject("_default", c).unwrap();

    let packet = bridge
        .memory_packet("_default", &singularity, "one two three", 10, None)
        .unwrap();
    assert!(
        !packet.facts.contains(&text3.to_string()),
        "3-word fact must be excluded from budget=3 (estimated=4 tokens)"
    );
}

/// Kills `+->*` in `token_count + estimated <= budget`: with `*`, first iteration is
/// `0 * estimated = 0` which always satisfies any budget, so everything is included.
#[test]
fn test_compile_packet_token_budget_accumulation() {
    // budget=3; "one two three" => estimated=4; 0+4=4>3 => excluded
    // with `+->*`: 0*4=0<=3 => included (mutant would include it)
    let config = BridgeConfig {
        token_budget: 3,
        max_packet_facts: 20,
        ..Default::default()
    };
    let encoder = TextEncoder::new();
    let bridge = BridgeRetrieval::new(encoder.clone(), ConceptGraph::new(), config);
    let mut singularity = Singularity::<HVec10240>::new(SingularityConfig::default());

    let text = "alpha beta gamma";
    let c = ConceptBuilder::new("c2")
        .with_vector(encoder.encode(text))
        .with_metadata("_text", serde_json::Value::String(text.to_string()))
        .build()
        .unwrap();
    singularity.inject("_default", c).unwrap();

    let packet = bridge
        .memory_packet("_default", &singularity, "alpha beta gamma", 10, None)
        .unwrap();
    assert!(
        !packet.facts.contains(&text.to_string()),
        "3-word fact must be excluded (estimated=4 > budget=3)"
    );
}

/// Kills `+=->*=` in `token_count += estimated`: with `*=`, token_count stays at 0
/// (0 * anything = 0), so all subsequent facts appear to fit within any budget.
#[test]
fn test_compile_packet_token_accumulator_grows() {
    // Two 2-word facts, budget=3. estimated("a b") = ceil(2/0.75) = 3.
    // First: 0+3=3<=3 => included, token_count becomes 3 (with +=) or 0 (with *=).
    // Second: with +=: 3+3=6>3 => excluded. With *=: 0+3=3<=3 => included.
    let config = BridgeConfig {
        token_budget: 3,
        max_packet_facts: 20,
        ..Default::default()
    };
    let encoder = TextEncoder::new();
    let bridge = BridgeRetrieval::new(encoder.clone(), ConceptGraph::new(), config);
    let mut singularity = Singularity::<HVec10240>::new(SingularityConfig::default());

    let t1 = "foo bar";
    let t2 = "baz qux";
    for (id, text) in [("c3", t1), ("c4", t2)] {
        let c = ConceptBuilder::new(id)
            .with_vector(encoder.encode(text))
            .with_metadata("_text", serde_json::Value::String(text.to_string()))
            .build()
            .unwrap();
        singularity.inject("_default", c).unwrap();
    }

    let packet = bridge
        .memory_packet("_default", &singularity, "foo bar baz qux", 10, None)
        .unwrap();
    // Both facts are 2 words each (estimated=3 tokens). Budget=3 only fits one.
    let count = packet.facts.iter().filter(|f| *f == t1 || *f == t2).count();
    assert!(
        count <= 1,
        "only one 2-word fact must fit in budget=3; got {count}"
    );
}

/// Kills compile_packet `/ with %` and `/ with *` on confidence computation.
/// With two hits: sum/2 is strictly less than sum (kills `* which gives sum*2`)
/// and less than sum (kills `% which gives sum%2 = sum-2*floor(sum/2)`).
/// We assert confidence < 1.0 to rule out `*` (would give >1) and check it's
/// the mean (not the sum or modulo).
#[test]
fn test_compile_packet_confidence_is_strict_mean() {
    let encoder = TextEncoder::new();
    let bridge = BridgeRetrieval::with_defaults(encoder.clone(), ConceptGraph::new());
    let mut singularity = Singularity::<HVec10240>::new(SingularityConfig::default());

    // Two concepts — query returns 2 hits; confidence = mean of their final scores.
    for (id, text) in [("ca", "semantic memory"), ("cb", "semantic recall")] {
        let c = ConceptBuilder::new(id)
            .with_vector(encoder.encode(text))
            .build()
            .unwrap();
        singularity.inject("_default", c).unwrap();
    }

    let packet = bridge
        .memory_packet("_default", &singularity, "semantic memory recall", 10, None)
        .unwrap();

    // Both hits have scores in (0, 1]; sum of 2 scores ≤ 2.0.
    // mean = sum/2 is strictly < sum (kills /->*) and in (0, 1] (kills /->% which
    // for sum < 2.0 gives a different value).
    assert!(
        packet.confidence > 0.0 && packet.confidence <= 1.0,
        "confidence mean must be in (0, 1], got {}",
        packet.confidence
    );
}

#[test]
fn test_bridge_retrieval_query_v2() {
    let mut singularity = Singularity::<HVec10240>::new(SingularityConfig::default());
    let concept = ConceptBuilder::new("c1")
        .with_vector(HVec10240::random())
        .build()
        .unwrap();
    singularity.inject("_default", concept).unwrap();

    let bridge = BridgeRetrieval::new(
        TextEncoder::new(),
        ConceptGraph::new(),
        BridgeConfig::default(),
    );
    let results = bridge.query("_default", &singularity, "test", 10, None);
    assert!(results.is_ok());
}

// --- query guard mutation coverage (|| vs &&) ---

#[test]
fn query_returns_empty_when_top_k_is_zero_and_not_empty() {
    // top_k == 0 alone must trigger early return even with a non-empty namespace.
    // We inject a dummy entry so namespace is NOT empty.
    let encoder = TextEncoder::new();
    let bridge = BridgeRetrieval::with_defaults(encoder.clone(), ConceptGraph::new());
    let mut singularity = Singularity::<HVec10240>::new(SingularityConfig::default());

    let concept = ConceptBuilder::new("c1")
        .with_vector(encoder.encode("hello world"))
        .build()
        .unwrap();
    singularity.inject("ns", concept).unwrap();

    // Verify singularity is NOT empty
    assert!(
        !singularity.is_empty("ns"),
        "singularity namespace 'ns' must not be empty"
    );

    let result = bridge.query("ns", &singularity, "hello", 0, None);
    assert!(result.is_ok());
    assert!(
        result.unwrap().is_empty(),
        "top_k=0 must yield empty results regardless of namespace"
    );
}

#[test]
fn query_returns_empty_when_namespace_is_empty_and_top_k_positive() {
    // singularity.is_empty(ns) == true alone must trigger early return even for top_k > 0.
    // With `&&`, this arm would NOT early-return if top_k > 0, causing a traversal
    // over an empty namespace instead of returning immediately.
    let encoder = TextEncoder::new();
    let bridge = BridgeRetrieval::with_defaults(encoder, ConceptGraph::new());
    let singularity = Singularity::<HVec10240>::new(SingularityConfig::default()); // namespace "ns" is empty by default

    assert!(
        singularity.is_empty("ns"),
        "singularity namespace 'ns' must be empty"
    );

    let result = bridge.query("ns", &singularity, "hello", 5, None);
    assert!(result.is_ok());
    assert!(
        result.unwrap().is_empty(),
        "empty namespace must yield empty results regardless of top_k"
    );
}

#[test]
fn query_both_conditions_zero_and_empty() {
    // Belt-and-suspenders: top_k=0 AND empty namespace
    let encoder = TextEncoder::new();
    let bridge = BridgeRetrieval::with_defaults(encoder, ConceptGraph::new());
    let singularity = Singularity::<HVec10240>::new(SingularityConfig::default());

    assert!(
        singularity.is_empty("ns"),
        "singularity namespace 'ns' must be empty"
    );

    let result = bridge.query("ns", &singularity, "hello", 0, None);
    assert!(result.is_ok());
    assert!(
        result.unwrap().is_empty(),
        "both conditions being met must return empty"
    );
}