yantrikdb 0.23.0

Cognitive memory engine for persistent AI systems
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
use super::*;

#[test]
fn recall_fetch_plan_reports_only_effective_cap_binding() {
    let ordinary = crate::engine::recall::recall_fetch_plan(20, 5_000, false);
    assert_eq!(ordinary.fetch_k, 400);
    assert_eq!(ordinary.requested_candidates, 400);
    assert_eq!(ordinary.candidate_cap, 10_000);
    assert!(!ordinary.cap_bound);

    let filtered = crate::engine::recall::recall_fetch_plan(20, 20_000, true);
    assert_eq!(filtered.fetch_k, 10_000);
    assert_eq!(filtered.requested_candidates, 20_000);
    assert!(filtered.cap_bound);

    let small_index = crate::engine::recall::recall_fetch_plan(1_000, 5_000, false);
    assert_eq!(small_index.fetch_k, 5_000);
    assert!(!small_index.cap_bound);

    let above_static_cap = crate::engine::recall::recall_fetch_plan(10_001, 20_000, false);
    assert_eq!(above_static_cap.fetch_k, 10_001);
    assert_eq!(above_static_cap.candidate_cap, 10_001);
    assert!(above_static_cap.cap_bound);
}

// ── Issue #46: confidence first-class on recall ───────────────────────

/// Seed a small DB with N records, each carrying a different `certainty`.
/// The i-th record has `certainty = (i + 1) as f64 / N as f64` (so 1..=N
/// maps to evenly-spaced certainty in (0, 1.0]). Embeddings are nearly
/// identical (same `vec_seed(1.0, ...)` shape) so that all candidates
/// survive the HNSW pass and downstream filtering / re-sorting is the
/// only thing distinguishing them.
fn seed_for_certainty_test(db: &YantrikDB, n: usize) -> Vec<String> {
    let mut rids = Vec::with_capacity(n);
    for i in 0..n {
        let certainty = (i as f64 + 1.0) / (n as f64);
        let rid = db
            .record(
                &format!("certainty test memory {i}"),
                "semantic",
                0.5,
                0.0,
                604800.0,
                &empty_meta(),
                &vec_seed(1.0 + (i as f32) * 0.001, 8),
                "default",
                certainty,
                "general",
                "user",
                None,
            )
            .unwrap();
        rids.push(rid);
        // Space out created_at so order=recency has a meaningful ranking.
        std::thread::sleep(std::time::Duration::from_millis(2));
    }
    rids
}

#[test]
fn recall_certainty_min_filters_low_certainty() {
    // Issue #46: candidates whose certainty falls below the requested
    // floor must NOT appear in results. Seed 5 memories with certainty
    // 0.2, 0.4, 0.6, 0.8, 1.0; recall with certainty_min=0.5 must
    // return at most 3 (the 0.6/0.8/1.0 ones).
    let db = YantrikDB::new(":memory:", 8).unwrap();
    let _rids = seed_for_certainty_test(&db, 5);

    let results = db
        .recall(
            &vec_seed(1.0, 8),
            10,
            None,
            None,
            false,
            false,
            None,
            true,
            None,
            None,
            None,
            Some(0.5), // certainty_min
            None,
            false,
            None, // event_after (#149)
            None, // event_before (#149)
        )
        .unwrap();

    assert!(
        !results.is_empty(),
        "expected at least one high-certainty result"
    );
    for r in &results {
        assert!(
            r.certainty >= 0.5,
            "certainty_min=0.5 must filter out cert={}: rid={}",
            r.certainty,
            r.rid
        );
    }
}

#[test]
fn recall_order_certainty_returns_results_in_certainty_desc() {
    // Issue #46: order="certainty" must re-sort the final top_k by
    // certainty descending. Seed 5 memories with varying certainty,
    // recall all 5 with order="certainty", assert the sequence is
    // monotonically non-increasing.
    let db = YantrikDB::new(":memory:", 8).unwrap();
    let _rids = seed_for_certainty_test(&db, 5);

    let results = db
        .recall(
            &vec_seed(1.0, 8),
            10,
            None,
            None,
            false,
            false,
            None,
            true,
            None,
            None,
            None,
            None,
            Some("certainty"),
            false,
            None, // event_after (#149)
            None, // event_before (#149)
        )
        .unwrap();

    assert!(
        results.len() >= 2,
        "need at least 2 results to verify ordering, got {}",
        results.len()
    );
    for w in results.windows(2) {
        assert!(
            w[0].certainty >= w[1].certainty,
            "order=certainty must be non-increasing; got [{}, {}]",
            w[0].certainty,
            w[1].certainty,
        );
    }
}

#[test]
fn recall_order_recency_returns_results_in_created_at_desc() {
    // Issue #46: order="recency" must re-sort the final top_k by
    // created_at descending (newest first). The seed helper spaces out
    // writes by 2ms each so created_at has a strict ordering.
    let db = YantrikDB::new(":memory:", 8).unwrap();
    let _rids = seed_for_certainty_test(&db, 5);

    let results = db
        .recall(
            &vec_seed(1.0, 8),
            10,
            None,
            None,
            false,
            false,
            None,
            true,
            None,
            None,
            None,
            None,
            Some("recency"),
            false,
            None, // event_after (#149)
            None, // event_before (#149)
        )
        .unwrap();

    assert!(
        results.len() >= 2,
        "need at least 2 results to verify ordering, got {}",
        results.len()
    );
    for w in results.windows(2) {
        assert!(
            w[0].created_at >= w[1].created_at,
            "order=recency must be non-increasing on created_at; got [{}, {}]",
            w[0].created_at,
            w[1].created_at,
        );
    }
}

#[test]
fn recall_order_first_mention_uses_metadata_then_created_at() {
    let db = YantrikDB::new(":memory:", 8).unwrap();
    let inputs = [
        ("first concern", 300.0, Some(100.0)),
        ("second concern", 200.0, None),
        ("third concern", 100.0, Some(250.0)),
    ];
    for (text, created_at, first_mention_at) in inputs {
        let metadata = first_mention_at
            .map(|value| serde_json::json!({"first_mention_at": value}))
            .unwrap_or_else(empty_meta);
        db.record_with_idempotency(
            text,
            "semantic",
            0.5,
            0.0,
            604800.0,
            &metadata,
            &vec_seed(1.0, 8),
            "default",
            0.8,
            "general",
            "inference",
            None,
            None,
            Some(created_at),
        )
        .unwrap();
    }

    for order in ["first_mention", "chronological"] {
        let results = db
            .recall(
                &vec_seed(1.0, 8),
                3,
                None,
                None,
                false,
                false,
                None,
                true,
                None,
                None,
                None,
                None,
                Some(order),
                false,
                None, // event_after (#149)
                None, // event_before (#149)
            )
            .unwrap();
        assert_eq!(
            results
                .iter()
                .map(|result| result.text.as_str())
                .collect::<Vec<_>>(),
            vec!["first concern", "second concern", "third concern"],
            "order={order} must use first_mention_at and fall back to created_at"
        );
    }
}

#[test]
fn recall_order_invalid_string_returns_invalid_input_error() {
    // Issue #46: unknown `order` values must surface as a typed
    // InvalidInput error rather than silently falling back to relevance.
    let db = YantrikDB::new(":memory:", 8).unwrap();
    let _rids = seed_for_certainty_test(&db, 3);

    let err = db
        .recall(
            &vec_seed(1.0, 8),
            10,
            None,
            None,
            false,
            false,
            None,
            true,
            None,
            None,
            None,
            None,
            Some("most_relevant"), // typo / not a valid order
            false,
            None, // event_after (#149)
            None, // event_before (#149)
        )
        .expect_err("invalid order string must return error");

    match err {
        crate::error::YantrikDbError::InvalidInput(msg) => {
            assert!(
                msg.contains("order") && msg.contains("most_relevant"),
                "error message should name the bad order value, got: {msg}"
            );
        }
        other => panic!("expected InvalidInput, got {other:?}"),
    }
}

#[test]
fn recall_default_order_is_relevance_unchanged() {
    // Issue #46: passing None for `order` must NOT change the existing
    // relevance-based behaviour. The first result's score must be >= the
    // last result's score (monotone non-increasing on score).
    let db = YantrikDB::new(":memory:", 8).unwrap();
    let _rids = seed_for_certainty_test(&db, 5);

    let results = db
        .recall(
            &vec_seed(1.0, 8),
            10,
            None,
            None,
            false,
            false,
            None,
            true,
            None,
            None,
            None,
            None,
            None, // default order = relevance
            false,
            None, // event_after (#149)
            None, // event_before (#149)
        )
        .unwrap();

    assert!(
        results.len() >= 2,
        "need at least 2 results to verify default order, got {}",
        results.len()
    );
    for w in results.windows(2) {
        assert!(
            w[0].score >= w[1].score,
            "default order must be score-desc (relevance); got [{}, {}]",
            w[0].score,
            w[1].score,
        );
    }
}

// ── recall_as_of: bitemporal recall over the revision + link ledgers ──

#[test]
fn recall_as_of_returns_pre_correction_state() {
    let db = YantrikDB::new(":memory:", 8).unwrap();
    let rid = db
        .record(
            "The deadline is March 1st",
            "semantic",
            0.9,
            0.0,
            604800.0,
            &serde_json::json!({"rev": "v1"}),
            &vec_seed(1.0, 8),
            "default",
            0.8,
            "work",
            "user",
            None,
        )
        .unwrap();
    std::thread::sleep(std::time::Duration::from_millis(25));
    let t_mid = super::super::now();
    std::thread::sleep(std::time::Duration::from_millis(25));
    let generation = db.search_generation();
    db.correct_with_embedding(
        &rid,
        Some("The deadline is March 15th"),
        &vec_seed(1.0, 8),
        generation,
        None,
        None,
        None,
        "slip",
    )
    .unwrap();

    // Present-day recall sees the corrected text.
    let today = db
        .recall_as_of(&vec_seed(1.0, 8), 5, super::super::now(), None, None)
        .unwrap();
    assert_eq!(today.len(), 1);
    assert_eq!(today[0].text, "The deadline is March 15th");
    assert!(
        !today[0]
            .why_retrieved
            .iter()
            .any(|w| w.starts_with("as_of:")),
        "no rollback tag when nothing was rolled back"
    );

    // As of t_mid, the belief was the ORIGINAL text — the correction
    // archived exactly that state in record_revisions.
    let then = db
        .recall_as_of(&vec_seed(1.0, 8), 5, t_mid, None, None)
        .unwrap();
    assert_eq!(then.len(), 1);
    assert_eq!(then[0].text, "The deadline is March 1st");
    assert!((then[0].importance - 0.9).abs() < 1e-9);
    assert_eq!(
        then[0].metadata.get("rev").and_then(|v| v.as_str()),
        Some("v1")
    );
    assert!(
        then[0]
            .why_retrieved
            .iter()
            .any(|w| w.starts_with("as_of:")),
        "rolled-back hit is labeled"
    );

    // Before the record existed there was nothing to believe.
    let before = db
        .recall_as_of(&vec_seed(1.0, 8), 5, then[0].created_at - 10.0, None, None)
        .unwrap();
    assert!(before.is_empty());
}

#[test]
fn recall_as_of_respects_supersede_edge_time() {
    use crate::types::{LinkType, RecordLink};

    let db = YantrikDB::new(":memory:", 8).unwrap();
    let old = db
        .record(
            "Use the v1 endpoint",
            "semantic",
            0.8,
            0.0,
            604800.0,
            &serde_json::json!({}),
            &vec_seed(1.0, 8),
            "default",
            0.8,
            "work",
            "user",
            None,
        )
        .unwrap();
    std::thread::sleep(std::time::Duration::from_millis(25));
    let t_mid = super::super::now();
    std::thread::sleep(std::time::Duration::from_millis(25));
    let new = db
        .record(
            "Use the v2 endpoint",
            "semantic",
            0.8,
            0.0,
            604800.0,
            &serde_json::json!({}),
            &vec_seed(1.0, 8),
            "default",
            0.8,
            "work",
            "user",
            None,
        )
        .unwrap();
    db.link(
        &new,
        &RecordLink {
            target_rid: old.clone(),
            link_type: LinkType::Supersedes,
        },
    )
    .unwrap();

    // As of t_mid: the old record was still current (the supersede edge
    // did not exist yet) and the successor did not exist at all.
    let then = db
        .recall_as_of(&vec_seed(1.0, 8), 5, t_mid, None, None)
        .unwrap();
    let then_rids: Vec<&str> = then.iter().map(|r| r.rid.as_str()).collect();
    assert!(
        then_rids.contains(&old.as_str()),
        "old was current at t_mid"
    );
    assert!(
        !then_rids.contains(&new.as_str()),
        "successor did not exist at t_mid"
    );

    // As of now: the edge exists, so the old record is out and the
    // successor is in.
    let today = db
        .recall_as_of(&vec_seed(1.0, 8), 5, super::super::now(), None, None)
        .unwrap();
    let today_rids: Vec<&str> = today.iter().map(|r| r.rid.as_str()).collect();
    assert!(today_rids.contains(&new.as_str()));
    assert!(!today_rids.contains(&old.as_str()));
}