semantic-memory 0.5.9

Local-first hybrid semantic search (SQLite + FTS5 + usearch 2.25) with bitemporal truth and typed receipts
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
use semantic_memory::{
    AssertionDraftV1, AuthorityFaultStage, AuthorityPermit, MemoryConfig, MemoryStore,
    MemoryTransitionCandidateV1, MemoryTransitionOutcomeV1, MockEmbedder, SourceArtifactV1,
    SourceSpanRefV1, SupersessionDraftV1, TransitionDisposition, TransitionOperation,
};
use tempfile::TempDir;

fn test_store() -> (MemoryStore, TempDir) {
    let tmp = TempDir::new().unwrap();
    let store = MemoryStore::open_with_embedder(
        MemoryConfig {
            base_dir: tmp.path().to_path_buf(),
            ..Default::default()
        },
        Box::new(MockEmbedder::new(768)),
    )
    .unwrap();
    (store, tmp)
}

fn permit(capability: &str) -> AuthorityPermit {
    AuthorityPermit::operator_system("principal:test", "caller:test", capability)
}

fn artifact(id: &str, content: &str) -> SourceArtifactV1 {
    SourceArtifactV1::new(id, content).unwrap()
}

fn span(artifact_id: &str, start: usize, end: usize) -> SourceSpanRefV1 {
    SourceSpanRefV1::new(artifact_id, start, end).unwrap()
}

fn append_candidate(
    candidate_id: &str,
    artifact_id: &str,
    evidence: &str,
    assertion: &str,
) -> MemoryTransitionCandidateV1 {
    let source_span = span(artifact_id, 0, evidence.len());
    MemoryTransitionCandidateV1::new(
        candidate_id,
        vec![artifact(artifact_id, evidence)],
        vec![source_span.clone()],
        vec![AssertionDraftV1::new(
            "assertion-1",
            "general",
            assertion,
            vec![source_span],
            vec![],
        )
        .unwrap()],
        TransitionOperation::Append {
            assertion_id: "assertion-1".into(),
        },
        vec![],
    )
    .unwrap()
}

#[test]
fn source_span_and_candidate_contracts_fail_closed() {
    assert!(SourceSpanRefV1::new("", 0, 1).is_err());
    assert!(SourceSpanRefV1::new("artifact", 1, 1).is_err());
    assert!(SourceSpanRefV1::new("artifact", 2, 1).is_err());

    let assertion =
        AssertionDraftV1::new("assertion", "general", "claim", vec![], vec![]).unwrap_err();
    assert!(assertion.contains("source span"));

    let missing = SourceSpanRefV1::new("artifact:missing", 0, 5).unwrap();
    let candidate = MemoryTransitionCandidateV1::new(
        "candidate-missing-source",
        vec![artifact("artifact:present", "claim")],
        vec![missing.clone()],
        vec![
            AssertionDraftV1::new("assertion", "general", "claim", vec![missing], vec![]).unwrap(),
        ],
        TransitionOperation::Append {
            assertion_id: "assertion".into(),
        },
        vec![],
    );
    assert!(candidate.unwrap_err().contains("missing source artifact"));

    let outside = SourceSpanRefV1::new("artifact:present", 0, 6).unwrap();
    let candidate = MemoryTransitionCandidateV1::new(
        "candidate-invalid-range",
        vec![artifact("artifact:present", "claim")],
        vec![outside.clone()],
        vec![
            AssertionDraftV1::new("assertion", "general", "claim", vec![outside], vec![]).unwrap(),
        ],
        TransitionOperation::Append {
            assertion_id: "assertion".into(),
        },
        vec![],
    );
    assert!(candidate.unwrap_err().contains("UTF-8 boundaries"));
}

#[tokio::test]
async fn exact_source_backed_candidate_commits_and_preserves_raw_evidence() {
    let (store, _tmp) = test_store();
    let authority = store.authority();
    let raw = "The verified launch date is 2026-07-10.";
    let candidate = append_candidate("candidate-exact", "artifact:launch", raw, raw);

    let outcome = authority
        .verify_and_commit(
            permit(AuthorityPermit::APPEND_CAPABILITY),
            "transition-exact".into(),
            candidate.clone(),
        )
        .await
        .unwrap();
    let MemoryTransitionOutcomeV1::Committed {
        verification,
        authority_receipt,
        ..
    } = outcome
    else {
        panic!("exact candidate must commit")
    };
    assert_eq!(
        verification.schema_version,
        "memory_transition_verification_v1"
    );
    assert_eq!(verification.disposition, TransitionDisposition::Commit);
    assert_eq!(verification.coverage.satisfied, verification.coverage.total);
    assert_eq!(
        verification.faithfulness.satisfied,
        verification.faithfulness.total
    );
    assert!(verification.omitted_spans.is_empty());
    assert!(verification.unsupported_spans.is_empty());
    assert_eq!(authority_receipt.after_epoch.0, 1);

    let current = store.list_facts("general", 10, 0).await.unwrap();
    assert_eq!(current.len(), 1);
    assert_eq!(current[0].content, raw);
    let record = authority
        .get_transition_by_idempotency_key("transition-exact")
        .await
        .unwrap()
        .unwrap();
    assert_eq!(record.candidate, candidate);
    assert_eq!(record.candidate.source_artifacts[0].content, raw);
    assert_eq!(
        record.authority_receipt_id,
        Some(authority_receipt.receipt_id)
    );
    let overwrite = store
        .raw_execute(
            "UPDATE memory_transition_records SET candidate_json = '{}' WHERE caller_idempotency_key = ?1",
            vec!["transition-exact".into()],
        )
        .await;
    assert!(
        overwrite.is_err(),
        "raw transition evidence must be immutable"
    );
}

#[tokio::test]
async fn unsupported_and_omitted_spans_are_distinct_and_quarantined() {
    let (store, _tmp) = test_store();
    let authority = store.authority();
    let evidence = "supported statement";
    let unsupported = append_candidate(
        "candidate-unsupported",
        "artifact:one",
        evidence,
        "supported statement plus invention",
    );
    let outcome = authority
        .verify_and_commit(
            permit(AuthorityPermit::APPEND_CAPABILITY),
            "transition-unsupported".into(),
            unsupported,
        )
        .await
        .unwrap();
    let MemoryTransitionOutcomeV1::Quarantined { record } = outcome else {
        panic!("unsupported content must quarantine")
    };
    assert_eq!(
        record.verification.disposition,
        TransitionDisposition::Quarantine
    );
    assert!(!record.verification.unsupported_spans.is_empty());
    assert!(record.verification.omitted_spans.is_empty());
    assert!(store.list_facts("general", 10, 0).await.unwrap().is_empty());

    let mut omitted = append_candidate(
        "candidate-omitted",
        "artifact:two",
        "first required span; second required span",
        "first required span",
    );
    omitted.required_source_spans = vec![span("artifact:two", 0, 19), span("artifact:two", 21, 41)];
    omitted.assertions[0].source_spans = vec![span("artifact:two", 0, 19)];
    let outcome = authority
        .verify_and_commit(
            permit(AuthorityPermit::APPEND_CAPABILITY),
            "transition-omitted".into(),
            omitted,
        )
        .await
        .unwrap();
    let MemoryTransitionOutcomeV1::Quarantined { record } = outcome else {
        panic!("omitted required evidence must quarantine")
    };
    assert_eq!(record.verification.omitted_spans.len(), 1);
    assert!(record.verification.unsupported_spans.is_empty());
    assert!(store.list_facts("general", 10, 0).await.unwrap().is_empty());
}

#[tokio::test]
async fn active_head_preservation_and_dependency_effects_are_simulated_before_commit() {
    let (store, _tmp) = test_store();
    let authority = store.authority();
    let first = authority
        .append(
            permit(AuthorityPermit::APPEND_CAPABILITY),
            "seed-one".into(),
            "general".into(),
            "old service color".into(),
            Some("seed".into()),
        )
        .await
        .unwrap();
    let old_id = first.affected_ids[0].clone();
    let second = authority
        .append(
            permit(AuthorityPermit::APPEND_CAPABILITY),
            "seed-two".into(),
            "general".into(),
            "still valid region".into(),
            Some("seed".into()),
        )
        .await
        .unwrap();
    let preserved_id = second.affected_ids[0].clone();
    let dependency = store
        .add_graph_edge(
            &format!("fact:{preserved_id}"),
            &format!("fact:{old_id}"),
            semantic_memory::GraphEdgeType::Entity {
                relation: "depends_on".into(),
            },
            1.0,
            None,
        )
        .await
        .unwrap();

    let raw = "new service color";
    let source_span = span("artifact:color", 0, raw.len());
    let assertion = AssertionDraftV1::new(
        "replacement",
        "general",
        raw,
        vec![source_span.clone()],
        vec![preserved_id.clone()],
    )
    .unwrap();
    let candidate = MemoryTransitionCandidateV1::new(
        "candidate-replace",
        vec![artifact("artifact:color", raw)],
        vec![source_span],
        vec![assertion],
        TransitionOperation::Supersede {
            draft: SupersessionDraftV1::new(&old_id, "replacement").unwrap(),
        },
        vec![],
    )
    .unwrap();
    let outcome = authority
        .verify_and_commit(
            permit(AuthorityPermit::SUPERSEDE_CAPABILITY),
            "transition-preservation-fail".into(),
            candidate.clone(),
        )
        .await
        .unwrap();
    let MemoryTransitionOutcomeV1::Quarantined { record } = outcome else {
        panic!("unaccounted active assertion must quarantine")
    };
    assert_eq!(
        record.verification.preservation.satisfied + 1,
        record.verification.preservation.total
    );
    assert_eq!(
        record.verification.omitted_active_fact_ids,
        vec![preserved_id.clone()]
    );

    let mut preserved = candidate;
    preserved.candidate_id = "candidate-replace-preserved".into();
    preserved.preserved_active_fact_ids = vec![preserved_id.clone()];
    let outcome = authority
        .verify_and_commit(
            permit(AuthorityPermit::SUPERSEDE_CAPABILITY),
            "transition-preservation-pass".into(),
            preserved,
        )
        .await
        .unwrap();
    let MemoryTransitionOutcomeV1::Committed { verification, .. } = outcome else {
        panic!("explicitly preserved active assertion must commit")
    };
    assert!(verification.active_head_simulation.consistent);
    assert_eq!(
        verification.active_head_simulation.replaced_fact_id,
        Some(old_id)
    );
    assert!(verification
        .dependency_simulation
        .preserved_edge_ids
        .contains(&dependency.id));
}

#[tokio::test]
async fn source_backed_retraction_simulates_and_commits_through_existing_redaction_lane() {
    let (store, _tmp) = test_store();
    let authority = store.authority();
    let seeded = authority
        .append(
            permit(AuthorityPermit::APPEND_CAPABILITY),
            "seed-retract".into(),
            "general".into(),
            "sensitive assertion".into(),
            Some("seed".into()),
        )
        .await
        .unwrap();
    let target_id = seeded.affected_ids[0].clone();
    let evidence = "operator-approved retraction";
    let evidence_span = span("artifact:retraction", 0, evidence.len());
    let candidate = MemoryTransitionCandidateV1::new(
        "candidate-retraction",
        vec![artifact("artifact:retraction", evidence)],
        vec![evidence_span.clone()],
        vec![],
        TransitionOperation::Retract {
            target_fact_id: target_id.clone(),
            reason: evidence.into(),
            source_spans: vec![evidence_span],
        },
        vec![],
    )
    .unwrap();

    let outcome = authority
        .verify_and_commit(
            permit(AuthorityPermit::REDACT_CAPABILITY),
            "transition-retraction".into(),
            candidate,
        )
        .await
        .unwrap();
    let MemoryTransitionOutcomeV1::Committed { verification, .. } = outcome else {
        panic!("source-backed retraction must commit")
    };
    assert_eq!(
        verification.active_head_simulation.replaced_fact_id,
        Some(target_id)
    );
    assert!(verification.active_head_simulation.consistent);
    assert_eq!(
        store.list_facts("general", 10, 0).await.unwrap()[0].content,
        "[REDACTED]"
    );
}

#[tokio::test]
async fn quarantine_replay_is_stable_and_conflicting_retry_fails_closed() {
    let (store, _tmp) = test_store();
    let authority = store.authority();
    let candidate = append_candidate(
        "candidate-replay",
        "artifact:replay",
        "source",
        "unsupported",
    );
    let first = authority
        .verify_and_commit(
            permit(AuthorityPermit::APPEND_CAPABILITY),
            "transition-replay".into(),
            candidate.clone(),
        )
        .await
        .unwrap();
    let replay = authority
        .verify_and_commit(
            permit(AuthorityPermit::APPEND_CAPABILITY),
            "transition-replay".into(),
            candidate,
        )
        .await
        .unwrap();
    assert_eq!(first, replay);

    let conflict = authority
        .verify_and_commit(
            permit(AuthorityPermit::APPEND_CAPABILITY),
            "transition-replay".into(),
            append_candidate("different", "artifact:other", "other", "other"),
        )
        .await
        .unwrap_err();
    assert!(matches!(
        conflict,
        semantic_memory::MemoryError::AuthorityIdempotencyConflict { .. }
    ));
}

#[tokio::test]
async fn fault_after_canonical_append_rolls_back_transition_and_evidence_record() {
    let (store, _tmp) = test_store();
    let authority = store.authority();
    authority.set_fault(Some(AuthorityFaultStage::AfterAppend));
    let result = authority
        .verify_and_commit(
            permit(AuthorityPermit::APPEND_CAPABILITY),
            "transition-fault".into(),
            append_candidate("candidate-fault", "artifact:fault", "fact", "fact"),
        )
        .await;
    assert!(matches!(
        result,
        Err(semantic_memory::MemoryError::AuthorityFaultInjected {
            stage: AuthorityFaultStage::AfterAppend
        })
    ));
    assert!(store.list_facts("general", 10, 0).await.unwrap().is_empty());
    assert!(authority
        .get_transition_by_idempotency_key("transition-fault")
        .await
        .unwrap()
        .is_none());
}