khive-db 0.10.0

SQLite storage backend: entities, edges, notes, events, FTS5, sqlite-vec vectors.
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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
use std::sync::Arc;

use super::*;
use crate::pool::{ConnectionPool, PoolConfig};

fn make_store() -> (Arc<ConnectionPool>, SqliteVecStore) {
    crate::extension::ensure_extensions_loaded();
    let pool = Arc::new(
        ConnectionPool::new(PoolConfig {
            path: None,
            ..PoolConfig::default()
        })
        .expect("in-memory pool"),
    );
    {
        let writer = pool.try_writer().expect("writer");
        writer
            .conn()
            .execute_batch(
                "CREATE VIRTUAL TABLE vec_provenance_test USING vec0(\
                     subject_id TEXT PRIMARY KEY, namespace TEXT NOT NULL, \
                     kind TEXT NOT NULL, field TEXT NOT NULL, \
                     embedding_model TEXT NOT NULL, embedding float[2] distance_metric=cosine)",
            )
            .expect("create vec0 table");
        writer
            .conn()
            .execute_batch(crate::migrations::ANN_WRITE_LOG_DDL)
            .expect("create ANN log");
        writer
            .conn()
            .execute_batch(crate::migrations::VECTOR_PROVENANCE_DDL)
            .expect("create provenance table");
    }
    let store = SqliteVecStore::new(
        Arc::clone(&pool),
        false,
        "provenance_test".into(),
        "model/a".into(),
        2,
        "ns:test".into(),
    )
    .expect("vector store");
    (pool, store)
}

fn record(subject_id: Uuid, text: &str, updated_at: &DateTime<Utc>) -> VectorRecord {
    VectorRecord {
        subject_id,
        kind: SubstrateKind::Entity,
        namespace: "ns:test".into(),
        field: "entity.body".into(),
        embedding_model: Some("model/a".into()),
        vectors: vec![vec![0.1, 0.2]],
        text_fingerprint: Some(VectorRecord::fingerprint_text(text)),
        updated_at: *updated_at,
    }
}

fn joined_provenance_count(
    conn: &rusqlite::Connection,
    table: &str,
    model_key: &str,
    subject_id: Uuid,
    namespace: &str,
) -> i64 {
    let sql = format!(
        "SELECT COUNT(embedding_digest) FROM ({})",
        provenance_read_sql(table, true)
    );
    conn.query_row(
        &sql,
        rusqlite::params![model_key, subject_id.to_string(), namespace],
        |row| row.get(0),
    )
    .expect("count sidecar matches through production read join")
}

#[tokio::test]
async fn persisted_vector_provenance_tracks_exact_embedded_text() {
    let (pool, store) = make_store();
    let subject_id = Uuid::new_v4();
    let timestamp = DateTime::parse_from_rfc3339("2026-09-25T12:34:56Z")
        .unwrap()
        .with_timezone(&Utc);
    let original_text = "rendered title\nbody";
    store
        .insert_batch(vec![record(subject_id, original_text, &timestamp)])
        .await
        .expect("index original text");
    let expected = ContentRef::from_digest_bytes(blake3::hash(original_text.as_bytes()).as_bytes());
    let stored = store.provenance(subject_id).await.unwrap().unwrap();
    assert_eq!(stored.embedding_model, "model/a");
    assert_eq!(stored.field, "entity.body");
    assert_eq!(stored.text_fingerprint, Some(expected.clone()));
    let raw: String = pool
        .try_writer()
        .unwrap()
        .conn()
        .query_row(
            "SELECT text_fingerprint FROM vector_provenance \
                 WHERE model_key = 'provenance_test' AND subject_id = ?1",
            [subject_id.to_string()],
            |row| row.get(0),
        )
        .unwrap();
    assert_eq!(raw, expected.as_str());
    let (stored_digest, live_embedding): (String, Vec<u8>) = pool
        .try_writer()
        .unwrap()
        .conn()
        .query_row(
            "SELECT p.embedding_digest, v.embedding \
                 FROM vector_provenance AS p JOIN vec_provenance_test AS v \
                   ON v.subject_id = p.subject_id \
                 WHERE p.model_key = 'provenance_test' AND p.subject_id = ?1",
            [subject_id.to_string()],
            |row| Ok((row.get(0)?, row.get(1)?)),
        )
        .unwrap();
    assert_eq!(
        stored_digest,
        blake3::hash(&live_embedding).to_hex().to_string()
    );

    store
        .insert_batch(vec![record(subject_id, original_text, &timestamp)])
        .await
        .expect("re-embed unchanged text");
    assert_eq!(
        store
            .provenance(subject_id)
            .await
            .unwrap()
            .unwrap()
            .text_fingerprint,
        Some(expected.clone())
    );

    store
        .insert_batch(vec![record(
            subject_id,
            "rendered title\nchanged body",
            &timestamp,
        )])
        .await
        .expect("re-embed changed text");
    let changed = store.provenance(subject_id).await.unwrap().unwrap();
    assert_eq!(
        changed.text_fingerprint,
        Some(ContentRef::from_digest_bytes(
            blake3::hash(b"rendered title\nchanged body").as_bytes()
        ))
    );
    assert_ne!(changed.text_fingerprint, Some(expected));
}

#[tokio::test]
async fn bypass_vector_replacement_makes_stale_sidecar_unknown() {
    let (pool, store) = make_store();
    let subject_id = Uuid::new_v4();
    let timestamp = DateTime::parse_from_rfc3339("2026-09-25T12:34:56Z")
        .unwrap()
        .with_timezone(&Utc);
    store
        .insert_batch(vec![record(subject_id, "original text", &timestamp)])
        .await
        .expect("index original text");

    {
        let writer = pool.try_writer().unwrap();
        writer
            .conn()
            .execute(
                "DELETE FROM vec_provenance_test WHERE subject_id = ?1",
                [subject_id.to_string()],
            )
            .unwrap();
        writer
            .conn()
            .execute(
                "INSERT INTO vec_provenance_test \
                     (subject_id, namespace, kind, field, embedding_model, embedding) \
                     VALUES (?1, 'ns:test', 'entity', 'entity.body', 'model/a', ?2)",
                rusqlite::params![subject_id.to_string(), f32_slice_as_bytes(&[0.7_f32, 0.8])],
            )
            .unwrap();
    }
    let present = store.provenance(subject_id).await.unwrap().unwrap();
    assert_eq!(present.embedding_model, "model/a");
    assert_eq!(present.field, "entity.body");
    assert_eq!(present.text_fingerprint, None);
    assert_eq!(present.updated_at, None);
}

#[tokio::test]
async fn model_scoped_identical_blob_provenance() {
    let (pool, first_store) = make_store();
    pool.try_writer()
        .unwrap()
        .conn()
        .execute_batch(
            "CREATE VIRTUAL TABLE vec_provenance_other USING vec0(\
                 subject_id TEXT PRIMARY KEY, namespace TEXT NOT NULL, \
                 kind TEXT NOT NULL, field TEXT NOT NULL, \
                 embedding_model TEXT NOT NULL, embedding float[2] distance_metric=cosine)",
        )
        .unwrap();
    let second_store = SqliteVecStore::new(
        Arc::clone(&pool),
        false,
        "provenance_other".into(),
        "model/b".into(),
        2,
        "ns:test".into(),
    )
    .unwrap();
    let subject_id = Uuid::new_v4();
    let first_timestamp = DateTime::parse_from_rfc3339("2026-09-25T12:34:56.123456789Z")
        .unwrap()
        .with_timezone(&Utc);
    let second_timestamp = DateTime::parse_from_rfc3339("2026-09-25T12:34:57.987654321Z")
        .unwrap()
        .with_timezone(&Utc);
    first_store
        .insert_batch(vec![record(subject_id, "model a source", &first_timestamp)])
        .await
        .unwrap();
    let mut other_record = record(subject_id, "model b source", &second_timestamp);
    other_record.embedding_model = Some("model/b".into());
    second_store.insert_batch(vec![other_record]).await.unwrap();

    let first = first_store.provenance(subject_id).await.unwrap().unwrap();
    let second = second_store.provenance(subject_id).await.unwrap().unwrap();
    assert_eq!(
        first.text_fingerprint,
        Some(VectorRecord::fingerprint_text("model a source"))
    );
    assert_eq!(first.updated_at, Some(first_timestamp));
    assert_eq!(
        second.text_fingerprint,
        Some(VectorRecord::fingerprint_text("model b source"))
    );
    assert_eq!(second.updated_at, Some(second_timestamp));

    let writer = pool.try_writer().unwrap();
    let first_blob: Vec<u8> = writer
        .conn()
        .query_row(
            "SELECT embedding FROM vec_provenance_test WHERE subject_id = ?1",
            [subject_id.to_string()],
            |row| row.get(0),
        )
        .unwrap();
    let second_blob: Vec<u8> = writer
        .conn()
        .query_row(
            "SELECT embedding FROM vec_provenance_other WHERE subject_id = ?1",
            [subject_id.to_string()],
            |row| row.get(0),
        )
        .unwrap();
    assert_eq!(
        first_blob, second_blob,
        "the digests alone cannot separate models"
    );
    assert_eq!(
        joined_provenance_count(
            writer.conn(),
            "vec_provenance_test",
            "provenance_test",
            subject_id,
            "ns:test"
        ),
        1
    );
    assert_eq!(
        joined_provenance_count(
            writer.conn(),
            "vec_provenance_other",
            "provenance_other",
            subject_id,
            "ns:test"
        ),
        1
    );
}

#[tokio::test]
async fn namespace_scoped_identical_blob_provenance() {
    let (pool, source_store) = make_store();
    let subject_id = Uuid::new_v4();
    let timestamp = DateTime::parse_from_rfc3339("2026-09-25T12:34:56.123456789Z")
        .unwrap()
        .with_timezone(&Utc);
    source_store
        .insert_batch(vec![record(subject_id, "source namespace", &timestamp)])
        .await
        .unwrap();

    {
        let writer = pool.try_writer().unwrap();
        let live_blob: Vec<u8> = writer
            .conn()
            .query_row(
                "SELECT embedding FROM vec_provenance_test WHERE subject_id = ?1",
                [subject_id.to_string()],
                |row| row.get(0),
            )
            .unwrap();
        writer.conn().execute_batch("BEGIN IMMEDIATE").unwrap();
        writer
            .conn()
            .execute(
                "DELETE FROM vec_provenance_test WHERE subject_id = ?1",
                [subject_id.to_string()],
            )
            .unwrap();
        writer
            .conn()
            .execute(
                "INSERT INTO vec_provenance_test \
                     (subject_id, namespace, kind, field, embedding_model, embedding) \
                     VALUES (?1, 'ns:other', 'entity', 'entity.body', 'model/a', ?2)",
                rusqlite::params![subject_id.to_string(), live_blob],
            )
            .unwrap();
        writer.conn().execute_batch("COMMIT").unwrap();
        let sidecar_namespace: String = writer
            .conn()
            .query_row(
                "SELECT namespace FROM vector_provenance \
                     WHERE model_key = 'provenance_test' AND subject_id = ?1",
                [subject_id.to_string()],
                |row| row.get(0),
            )
            .unwrap();
        assert_eq!(sidecar_namespace, "ns:test");
        assert_eq!(
            joined_provenance_count(
                writer.conn(),
                "vec_provenance_test",
                "provenance_test",
                subject_id,
                "ns:other"
            ),
            0
        );
    }

    let destination_store = SqliteVecStore::new(
        Arc::clone(&pool),
        false,
        "provenance_test".into(),
        "model/a".into(),
        2,
        "ns:other".into(),
    )
    .unwrap();
    assert!(source_store.provenance(subject_id).await.unwrap().is_none());
    let moved = destination_store
        .provenance(subject_id)
        .await
        .unwrap()
        .unwrap();
    assert_eq!(moved.embedding_model, "model/a");
    assert_eq!(moved.field, "entity.body");
    assert_eq!(moved.text_fingerprint, None);
    assert_eq!(moved.updated_at, None);
}

#[tokio::test]
async fn vector_provenance_timestamp_round_trips_and_unattributed_update_clears_it() {
    let (_, store) = make_store();
    let subject_id = Uuid::new_v4();
    let timestamp = DateTime::parse_from_rfc3339("2026-09-25T12:34:56.123456789Z")
        .unwrap()
        .with_timezone(&Utc);
    store
        .insert_batch(vec![record(subject_id, "indexed body", &timestamp)])
        .await
        .expect("index record");
    let persisted = store.provenance(subject_id).await.unwrap().unwrap();
    assert_eq!(persisted.updated_at, Some(timestamp));

    store
        .update(
            subject_id,
            SubstrateKind::Entity,
            "ns:test",
            "entity.body",
            vec![vec![0.3, 0.4]],
        )
        .await
        .expect("replace without source text");
    let replaced = store.provenance(subject_id).await.unwrap().unwrap();
    assert_eq!(replaced.text_fingerprint, None);
    assert_eq!(replaced.updated_at, None);
}

#[tokio::test]
async fn low_level_same_blob_reinsert_clears_provenance() {
    let (pool, store) = make_store();
    let timestamp = DateTime::parse_from_rfc3339("2026-09-25T12:34:56.123456789Z")
        .unwrap()
        .with_timezone(&Utc);
    for operation in ["insert", "update", "insert_exact_only"] {
        let subject_id = Uuid::new_v4();
        store
            .insert_batch(vec![record(subject_id, "attributed source", &timestamp)])
            .await
            .unwrap();
        let before: Vec<u8> = pool
            .try_writer()
            .unwrap()
            .conn()
            .query_row(
                "SELECT embedding FROM vec_provenance_test WHERE subject_id = ?1",
                [subject_id.to_string()],
                |row| row.get(0),
            )
            .unwrap();
        match operation {
            "insert" => {
                store
                    .insert(
                        subject_id,
                        SubstrateKind::Entity,
                        "ns:test",
                        "entity.body",
                        vec![vec![0.1, 0.2]],
                    )
                    .await
                    .unwrap();
            }
            "update" => {
                store
                    .update(
                        subject_id,
                        SubstrateKind::Entity,
                        "ns:test",
                        "entity.body",
                        vec![vec![0.1, 0.2]],
                    )
                    .await
                    .unwrap();
            }
            "insert_exact_only" => {
                store
                    .insert_exact_only(
                        subject_id,
                        SubstrateKind::Entity,
                        "ns:test",
                        "entity.body",
                        vec![vec![0.1, 0.2]],
                    )
                    .await
                    .unwrap();
            }
            _ => unreachable!(),
        }

        let writer = pool.try_writer().unwrap();
        let after: Vec<u8> = writer
            .conn()
            .query_row(
                "SELECT embedding FROM vec_provenance_test WHERE subject_id = ?1",
                [subject_id.to_string()],
                |row| row.get(0),
            )
            .unwrap();
        assert_eq!(after, before, "{operation} must replace with the same BLOB");
        let fields: (Option<String>, Option<String>) = writer
            .conn()
            .query_row(
                "SELECT text_fingerprint, updated_at FROM vector_provenance \
                     WHERE model_key = 'provenance_test' AND subject_id = ?1",
                [subject_id.to_string()],
                |row| Ok((row.get(0)?, row.get(1)?)),
            )
            .unwrap();
        assert_eq!(
            fields,
            (None, None),
            "{operation} must clear sidecar fields"
        );
        drop(writer);
        let observed = store.provenance(subject_id).await.unwrap().unwrap();
        assert_eq!(observed.text_fingerprint, None, "{operation}");
        assert_eq!(observed.updated_at, None, "{operation}");
    }
}

#[tokio::test]
async fn failed_replacement_rolls_back_vector_and_bound_provenance() {
    let (_, store) = make_store();
    let subject_id = Uuid::new_v4();
    let timestamp = DateTime::parse_from_rfc3339("2026-09-25T12:34:56Z")
        .unwrap()
        .with_timezone(&Utc);
    store
        .insert_batch(vec![record(subject_id, "original text", &timestamp)])
        .await
        .unwrap();
    let original = store.provenance(subject_id).await.unwrap().unwrap();
    let mut replacement = record(subject_id, "replacement text", &timestamp);
    replacement.vectors = vec![vec![0.7, 0.8]];

    let _guard = failpoint::FailpointGuard::new();
    let summary = store.insert_batch(vec![replacement]).await.unwrap();
    assert_eq!(summary.failed, 1);
    assert_eq!(summary.affected, 0);
    assert_eq!(
        store.provenance(subject_id).await.unwrap().unwrap(),
        original
    );
}

#[tokio::test]
async fn legacy_and_deleted_vectors_have_no_current_provenance() {
    let (pool, store) = make_store();
    let subject_id = Uuid::new_v4();
    {
        let writer = pool.try_writer().unwrap();
        writer
            .conn()
            .execute_batch("DROP TABLE vector_provenance")
            .expect("simulate pre-migration database");
        writer
            .conn()
            .execute(
                "INSERT INTO vec_provenance_test \
                     (subject_id, namespace, kind, field, embedding_model, embedding) \
                     VALUES (?1, 'ns:test', 'entity', 'entity.body', 'model/a', ?2)",
                rusqlite::params![subject_id.to_string(), f32_slice_as_bytes(&[0.1_f32, 0.2])],
            )
            .expect("write legacy vector without provenance");
        writer
            .conn()
            .execute_batch(crate::migrations::VECTOR_PROVENANCE_DDL)
            .expect("migrate provenance sidecar");
    }
    let legacy = store.provenance(subject_id).await.unwrap().unwrap();
    assert_eq!(legacy.text_fingerprint, None);
    assert_eq!(legacy.updated_at, None);

    let timestamp = DateTime::parse_from_rfc3339("2026-09-25T12:34:56Z")
        .unwrap()
        .with_timezone(&Utc);
    store
        .insert_batch(vec![record(subject_id, "new text", &timestamp)])
        .await
        .expect("replace legacy vector");
    let other_namespace = SqliteVecStore::new(
        Arc::clone(&pool),
        false,
        "provenance_test".into(),
        "model/a".into(),
        2,
        "ns:other".into(),
    )
    .unwrap();
    assert!(!other_namespace.delete(subject_id).await.unwrap());
    assert!(store
        .provenance(subject_id)
        .await
        .unwrap()
        .unwrap()
        .text_fingerprint
        .is_some());
    assert!(store.delete(subject_id).await.expect("delete vector"));
    assert!(store.provenance(subject_id).await.unwrap().is_none());
    let sidecar_count: i64 = pool
        .try_writer()
        .unwrap()
        .conn()
        .query_row(
            "SELECT count(*) FROM vector_provenance \
                 WHERE model_key = 'provenance_test' AND subject_id = ?1",
            [subject_id.to_string()],
            |row| row.get(0),
        )
        .unwrap();
    assert_eq!(sidecar_count, 0);
}