macrame-db 0.7.0

A Bitemporal Graph Ledger on libSQL · Embedded knowledge database
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
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
#[path = "common/harness.rs"]
mod harness;

use harness::TestHarness;
use macrame::error::DbError;
use macrame::graph::vector_filter::{CandidateCount, CostEstimator, VectorFilterStrategy};
use macrame::schema::migrations;
use macrame::vector::{
    declared_dimension, reciprocal_rank_fusion, register_model, registered_models, search_vector,
    upsert_embedding, EmbeddingCodec, ModelName,
};

const TS: &str = "2026-01-01T00:00:00.000000Z";

/// A migrated database with two concepts, and the writer's PRAGMAs applied.
async fn seeded(harness: &TestHarness) -> (libsql::Database, libsql::Connection) {
    let db = libsql::Builder::new_local(&harness.db_path)
        .build()
        .await
        .unwrap();
    let conn = db.connect().unwrap();
    conn.execute("PRAGMA foreign_keys = ON", ()).await.unwrap();
    migrations::run(&conn).await.unwrap();
    for id in ["c0", "c1", "c2"] {
        conn.execute(
            "INSERT INTO concepts (id, title, valid_from, recorded_at) VALUES (?1, 'N', ?2, ?2)",
            libsql::params![id, TS],
        )
        .await
        .unwrap();
    }
    (db, conn)
}

fn model() -> ModelName {
    ModelName::new("probe_v1").unwrap()
}

/// Little-endian F32 bytes, the wire form of an F32_BLOB.
fn le(v: &[f32]) -> Vec<u8> {
    v.iter().flat_map(|f| f.to_le_bytes()).collect()
}

async fn count(conn: &libsql::Connection, sql: &str) -> i64 {
    conn.query(sql, ())
        .await
        .unwrap()
        .next()
        .await
        .unwrap()
        .unwrap()
        .get(0)
        .unwrap()
}

/// Registration creates the table *and* its DiskANN index, and the dimension
/// is readable back out of the schema rather than remembered by the crate.
#[tokio::test]
async fn registering_a_model_creates_its_table_and_index() {
    let harness = TestHarness::new();
    let (_db, conn) = seeded(&harness).await;
    let m = model();

    register_model(&conn, &m, 4).await.unwrap();

    assert_eq!(
        count(
            &conn,
            &format!(
                "SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='{}'",
                m.table()
            )
        )
        .await,
        1
    );
    assert_eq!(
        count(
            &conn,
            &format!(
                "SELECT COUNT(*) FROM sqlite_master WHERE type='index' AND name='{}'",
                m.index()
            )
        )
        .await,
        1,
        "the index is not optional: without it the engine accepts any dimension"
    );

    assert_eq!(declared_dimension(&conn, &m).await.unwrap(), 4);
    assert_eq!(registered_models(&conn).await.unwrap(), vec![m.clone()]);

    // Idempotent, as an application calling it on every start requires.
    register_model(&conn, &m, 4).await.unwrap();
    assert_eq!(registered_models(&conn).await.unwrap().len(), 1);
}

/// **The regression that this phase would otherwise have introduced.**
///
/// `verify()` used to require exactly four tables in `sqlite_master`. Registering
/// a model adds `embeddings_*`, and libSQL's vector index adds shadow tables and
/// a shadow index of its own — so on the next open, migration verification
/// failed and a perfectly healthy database refused to open. Nothing about the
/// ledger had changed; the check was counting instead of looking.
#[tokio::test]
async fn a_database_with_a_registered_model_still_opens() {
    let harness = TestHarness::new();
    {
        let (db, conn) = seeded(&harness).await;
        register_model(&conn, &model(), 4).await.unwrap();
        upsert_embedding(&conn, &model(), "c0", &[1.0, 0.0, 0.0, 0.0])
            .await
            .unwrap();
        drop(conn);
        drop(db);
    }

    let db = libsql::Builder::new_local(&harness.db_path)
        .build()
        .await
        .unwrap();
    let conn = db.connect().unwrap();
    migrations::run(&conn)
        .await
        .expect("a database carrying a registered model must reopen");

    assert_eq!(declared_dimension(&conn, &model()).await.unwrap(), 4);
}

/// The crate-level check reports the *declared* dimension, which is the number
/// the caller needs and the one the engine will enforce.
///
/// Before this phase, `search_vector` called `encode(query_vec,
/// query_vec.len(), …)` — comparing the length against itself — so `DimMismatch`
/// could not be produced through the search path at all.
#[tokio::test]
async fn a_wrong_dimension_is_refused_with_the_declared_dimension() {
    let harness = TestHarness::new();
    let (_db, conn) = seeded(&harness).await;
    let m = model();
    register_model(&conn, &m, 4).await.unwrap();

    let err = upsert_embedding(&conn, &m, "c0", &[1.0, 2.0])
        .await
        .unwrap_err();
    match err {
        DbError::DimMismatch {
            got,
            expected,
            model,
        } => {
            assert_eq!((got, expected, model.as_str()), (2, 4, "probe_v1"));
        }
        other => panic!("expected DimMismatch, got {other:?}"),
    }

    let err = search_vector(&conn, &[1.0, 2.0, 3.0], &m, 5)
        .await
        .unwrap_err();
    assert!(
        matches!(
            err,
            DbError::DimMismatch {
                got: 3,
                expected: 4,
                ..
            }
        ),
        "search must check the query vector too, got {err:?}"
    );

    assert_eq!(
        count(&conn, &format!("SELECT COUNT(*) FROM {}", m.table())).await,
        0
    );
}

/// **The storage layer enforces the dimension only because the index exists.**
///
/// §4.1 claimed `F32_BLOB(n)` rejects a wrong-length vector at insert time and
/// that the crate-level check was there "for error quality" while the engine's
/// was there "for correctness". Measured against libSQL 0.9.30 that is false as
/// stated: with no vector index the engine accepts the row. The index is what
/// enforces, so this test pins both halves — the guarantee we do have, and the
/// one we do not — because `register_model` creating the index is the only
/// reason the first half holds.
#[tokio::test]
async fn the_engine_enforces_dimension_only_where_the_index_exists() {
    let harness = TestHarness::new();
    let (_db, conn) = seeded(&harness).await;
    let m = model();
    register_model(&conn, &m, 4).await.unwrap();

    // Bypass the crate check entirely: hand the engine a raw two-float blob.
    let rejected = conn
        .execute(
            &format!(
                "INSERT INTO {} (concept_id, embedding) VALUES ('c0', ?1)",
                m.table()
            ),
            libsql::params![le(&[1.0, 2.0])],
        )
        .await;
    assert!(
        rejected.is_err(),
        "the indexed table must reject a short vector"
    );
    assert_eq!(
        count(&conn, &format!("SELECT COUNT(*) FROM {}", m.table())).await,
        0,
        "a rejected vector must not leave a row behind"
    );

    // And the converse, which is why the index is created with the table: an
    // unindexed F32_BLOB column enforces nothing.
    conn.execute(
        "CREATE TABLE unindexed (concept_id TEXT PRIMARY KEY, embedding F32_BLOB(4) NOT NULL)",
        (),
    )
    .await
    .unwrap();
    let accepted = conn
        .execute(
            "INSERT INTO unindexed VALUES ('c0', ?1)",
            libsql::params![le(&[1.0, 2.0])],
        )
        .await;
    assert!(
        accepted.is_ok(),
        "if this ever starts failing, libSQL has begun enforcing the column type \
         itself and the §4.1 correction (D-037) should be revisited"
    );
}

/// Nearest first, through the DiskANN index rather than a table scan.
#[tokio::test]
async fn search_returns_neighbours_nearest_first() {
    let harness = TestHarness::new();
    let (_db, conn) = seeded(&harness).await;
    let m = model();
    register_model(&conn, &m, 4).await.unwrap();

    upsert_embedding(&conn, &m, "c0", &[1.0, 0.0, 0.0, 0.0])
        .await
        .unwrap();
    upsert_embedding(&conn, &m, "c1", &[0.9, 0.1, 0.0, 0.0])
        .await
        .unwrap();
    upsert_embedding(&conn, &m, "c2", &[0.0, 0.0, 0.0, 1.0])
        .await
        .unwrap();

    let hits = search_vector(&conn, &[1.0, 0.0, 0.0, 0.0], &m, 3)
        .await
        .unwrap();
    assert_eq!(hits.len(), 3);
    assert_eq!(hits[0].concept_id, "c0");
    assert_eq!(hits[1].concept_id, "c1");
    assert_eq!(hits[2].concept_id, "c2");
    assert!(hits[0].score <= hits[1].score && hits[1].score <= hits[2].score);

    let top1 = search_vector(&conn, &[0.0, 0.0, 0.0, 1.0], &m, 1)
        .await
        .unwrap();
    assert_eq!(top1.len(), 1);
    assert_eq!(top1[0].concept_id, "c2");

    assert!(search_vector(&conn, &[1.0, 0.0, 0.0, 0.0], &m, 0)
        .await
        .unwrap()
        .is_empty());
}

/// Re-embedding replaces the vector: one row per concept per model, because an
/// embedding is derived and carries no history of its own (Doctrine VII).
#[tokio::test]
async fn re_embedding_replaces_rather_than_accumulates() {
    let harness = TestHarness::new();
    let (_db, conn) = seeded(&harness).await;
    let m = model();
    register_model(&conn, &m, 4).await.unwrap();

    upsert_embedding(&conn, &m, "c0", &[1.0, 0.0, 0.0, 0.0])
        .await
        .unwrap();
    upsert_embedding(&conn, &m, "c0", &[0.0, 0.0, 0.0, 1.0])
        .await
        .unwrap();

    assert_eq!(
        count(&conn, &format!("SELECT COUNT(*) FROM {}", m.table())).await,
        1
    );
    let hits = search_vector(&conn, &[0.0, 0.0, 0.0, 1.0], &m, 1)
        .await
        .unwrap();
    assert_eq!(hits[0].concept_id, "c0");
    assert!(
        hits[0].score < 0.001,
        "the newest vector must be the one stored"
    );
}

/// **Doctrine VII, the half that needed Phase 3 to become testable.**
///
/// A vector is a derived artifact and never enters the ledger. The static test
/// in `doctrine_static_tests.rs` proves no trigger *payload* mentions one; this
/// proves the stronger thing now that the tables exist — writing embeddings adds
/// nothing to `transaction_log`, and no trigger is attached to the table that
/// could.
#[tokio::test]
async fn embeddings_never_enter_the_ledger() {
    let harness = TestHarness::new();
    let (_db, conn) = seeded(&harness).await;
    let m = model();
    register_model(&conn, &m, 4).await.unwrap();

    let before = count(&conn, "SELECT COUNT(*) FROM transaction_log").await;
    for (id, v) in [
        ("c0", [1.0f32, 0.0, 0.0, 0.0]),
        ("c1", [0.0, 1.0, 0.0, 0.0]),
        ("c2", [0.0, 0.0, 1.0, 0.0]),
    ] {
        upsert_embedding(&conn, &m, id, &v).await.unwrap();
    }

    assert_eq!(
        count(&conn, "SELECT COUNT(*) FROM transaction_log").await,
        before,
        "an embedding write reached transaction_log; Doctrine VII excludes vectors"
    );
    assert_eq!(
        count(
            &conn,
            &format!(
                "SELECT COUNT(*) FROM sqlite_master WHERE type='trigger' AND tbl_name='{}'",
                m.table()
            )
        )
        .await,
        0,
        "a trigger on the embeddings table could carry a vector into the ledger"
    );
}

/// An embedding references a concept, so it cannot outlive one that never
/// existed. The FK is what keeps the per-model tables from drifting into a
/// second, unreferenced population of ids.
#[tokio::test]
async fn an_embedding_cannot_reference_a_concept_that_does_not_exist() {
    let harness = TestHarness::new();
    let (_db, conn) = seeded(&harness).await;
    let m = model();
    register_model(&conn, &m, 4).await.unwrap();

    let err = upsert_embedding(&conn, &m, "no_such_concept", &[1.0, 0.0, 0.0, 0.0]).await;
    assert!(err.is_err(), "an orphan embedding must be refused");
}

/// An unregistered model is a typed error naming the missing table, not an
/// opaque `no such table` from the engine.
#[tokio::test]
async fn an_unregistered_model_is_a_typed_error() {
    let harness = TestHarness::new();
    let (_db, conn) = seeded(&harness).await;
    let m = ModelName::new("never_registered").unwrap();

    match search_vector(&conn, &[1.0, 0.0], &m, 3).await.unwrap_err() {
        DbError::ModelNotRegistered { model, table } => {
            assert_eq!(model, "never_registered");
            assert_eq!(table, "embeddings_never_registered");
        }
        other => panic!("expected ModelNotRegistered, got {other:?}"),
    }
    assert!(declared_dimension(&conn, &m).await.is_err());
    assert!(registered_models(&conn).await.unwrap().is_empty());
}

/// Registering a name that already exists at another dimension is refused
/// rather than silently no-opped by `IF NOT EXISTS`, which would leave the
/// caller believing a dimension that is not the one in force.
#[tokio::test]
async fn re_registering_at_a_different_dimension_is_refused() {
    let harness = TestHarness::new();
    let (_db, conn) = seeded(&harness).await;
    let m = model();
    register_model(&conn, &m, 4).await.unwrap();

    match register_model(&conn, &m, 8).await.unwrap_err() {
        DbError::DimMismatch { got, expected, .. } => assert_eq!((got, expected), (8, 4)),
        other => panic!("expected DimMismatch, got {other:?}"),
    }
    assert_eq!(declared_dimension(&conn, &m).await.unwrap(), 4);
}

/// A model name reaches SQL as a table identifier, which cannot be bound as a
/// parameter. `ModelName` is the boundary that makes the splice safe, so the
/// ledger must survive a name built to escape it — and the name must be
/// rejected before any statement is constructed.
#[tokio::test]
async fn a_hostile_model_name_never_reaches_sql() {
    let harness = TestHarness::new();
    let (_db, conn) = seeded(&harness).await;

    for hostile in [
        "x; DROP TABLE links; --",
        "x\"; DROP TABLE concepts; --",
        "x) --",
        "x' OR '1'='1",
    ] {
        assert!(
            matches!(ModelName::new(hostile), Err(DbError::InvalidModelName(_))),
            "{hostile:?} was accepted as a model name and would be spliced into SQL"
        );
    }

    // A name that *is* a valid identifier but collides with a ledger table is
    // harmless, because the table name is always prefixed. Worth pinning: the
    // prefix is the only thing standing between `ModelName::new("links")` and a
    // vector column being created on the ledger.
    let collide = ModelName::new("links").unwrap();
    assert_eq!(collide.table(), "embeddings_links");
    register_model(&conn, &collide, 4).await.unwrap();

    assert_eq!(count(&conn, "SELECT COUNT(*) FROM links").await, 0);
    assert_eq!(count(&conn, "SELECT COUNT(*) FROM concepts").await, 3);
    assert_eq!(
        count(
            &conn,
            "SELECT COUNT(*) FROM pragma_table_info('links') WHERE name = 'embedding'"
        )
        .await,
        0,
        "registering a model must never touch a ledger table"
    );
}

#[test]
fn test_embedding_codec_roundtrip() {
    let vec = vec![1.0f32, -2.5f32, 3.15f32, 0.0f32];
    let bytes = EmbeddingCodec::encode(&vec, 4, "nomic-v1").expect("encode failed");
    let decoded = EmbeddingCodec::decode(&bytes).expect("decode failed");
    assert_eq!(vec, decoded);
}

#[test]
fn test_dim_mismatch_rejection() {
    let vec = vec![1.0f32, 2.0f32, 3.0f32];
    let res = EmbeddingCodec::encode(&vec, 768, "nomic-v1");
    assert!(res.is_err());
    if let Err(DbError::DimMismatch {
        got,
        expected,
        model,
    }) = res
    {
        assert_eq!(got, 3);
        assert_eq!(expected, 768);
        assert_eq!(model, "nomic-v1");
    } else {
        panic!("Expected DimMismatch error");
    }
}

#[test]
fn test_reciprocal_rank_fusion_scoring() {
    let vector_ranks = vec![
        "doc_a".to_string(),
        "doc_b".to_string(),
        "doc_c".to_string(),
    ];
    let keyword_ranks = vec![
        "doc_b".to_string(),
        "doc_a".to_string(),
        "doc_d".to_string(),
    ];

    let fused = reciprocal_rank_fusion(&vector_ranks, &keyword_ranks, 60);
    assert!(!fused.is_empty());

    // Both doc_a and doc_b appear in top positions, doc_a (1st vec, 2nd kw) & doc_b (2nd vec, 1st kw) share top scores
    let top_doc = &fused[0].0;
    assert!(top_doc == "doc_a" || top_doc == "doc_b");
}

/// The cost model in isolation: a loose filter prices `PostFilter` cheaper, a
/// tight one prices the exact scan cheaper.
///
/// This replaces a test that asserted `select_strategy(100/1000/10000)` returned
/// the three variants in order — a test of two hard-coded thresholds and of a
/// `TwoPhaseTempTable` whose mechanisms libSQL does not offer. The strategies it
/// named are down to two, and the selector now reads the `byte_budget` it used
/// to carry unused. See `vector_filter_tests.rs` for the executable half.
#[test]
fn test_vector_filter_cost_estimator() {
    // 100K vectors of 768 dimensions.
    let estimator = CostEstimator::new(10_000_000, 100_000, 768 * 4);

    let loose = estimator
        .estimate(10, CandidateCount::Exact(90_000))
        .unwrap();
    assert_eq!(loose.strategy, VectorFilterStrategy::PostFilter);

    let tight = estimator.estimate(10, CandidateCount::Exact(50)).unwrap();
    assert_eq!(tight.strategy, VectorFilterStrategy::PreFilterCTE);

    // The budget is a ceiling on the candidate set, not decoration.
    let over = estimator.estimate(10, CandidateCount::Exact(10_000_000));
    assert!(
        matches!(over, Err(DbError::SubgraphTooLarge { .. })),
        "got {over:?}"
    );
}

// -- D-048: the vector write path reaches the actor -------------------------

/// **The regression test for "green and unreachable at the same time".**
///
/// Every other test in this file opens its own `libsql::Connection` and reaches
/// around the Write Actor, which is why the vector suite passed for a whole
/// release while an application had no way to store a vector at all:
/// `upsert_embedding` and `register_model` take a raw connection, `read_conn`
/// is `query_only`, and the write connection lives inside the actor.
///
/// So the constraint here is not what is asserted, it is what is *used*. This
/// test touches `Database` and nothing else on the write side. If the only path
/// to a stored vector is a connection the caller had to build, this does not
/// compile.
#[tokio::test]
async fn an_application_can_register_and_embed_through_the_handle_alone() {
    use macrame::prelude::*;

    let harness = TestHarness::new();
    let db = Database::open(&harness.db_path).await.unwrap();
    let m = ModelName::new("handle_v1").unwrap();

    db.upsert_concept(ConceptUpsert::new("c0", "First").valid_from(TS))
        .await
        .unwrap();
    db.upsert_concept(ConceptUpsert::new("c1", "Second").valid_from(TS))
        .await
        .unwrap();

    db.register_model(&m, 4).await.unwrap();
    let written = db
        .upsert_embeddings(
            &m,
            vec![
                ("c0".to_string(), vec![1.0, 0.0, 0.0, 0.0]),
                ("c1".to_string(), vec![0.0, 1.0, 0.0, 0.0]),
            ],
        )
        .await
        .unwrap();
    assert_eq!(written, 2);

    // Reads still go direct, which is the point: they never traverse the actor.
    let hits = search_vector(db.read_conn(), &[1.0, 0.0, 0.0, 0.0], &m, 2)
        .await
        .unwrap();
    assert_eq!(
        hits[0].concept_id, "c0",
        "nearest neighbour is the identical vector"
    );

    db.close().await.unwrap();
}

/// Re-embedding replaces rather than versioning (Doctrine VII), and the write
/// leaves the ledger alone — there are no triggers on an embeddings table.
#[tokio::test]
async fn re_embedding_replaces_and_never_touches_the_ledger() {
    use macrame::prelude::*;

    let harness = TestHarness::new();
    let db = Database::open(&harness.db_path).await.unwrap();
    let m = ModelName::new("handle_v1").unwrap();
    db.upsert_concept(ConceptUpsert::new("c0", "First").valid_from(TS))
        .await
        .unwrap();
    db.register_model(&m, 4).await.unwrap();

    async fn log_len(db: &Database) -> i64 {
        let mut rows = db
            .read_conn()
            .query("SELECT COUNT(*) FROM transaction_log", ())
            .await
            .unwrap();
        rows.next().await.unwrap().unwrap().get(0).unwrap()
    }
    let before = log_len(&db).await;

    for v in [vec![1.0f32, 0.0, 0.0, 0.0], vec![0.0, 0.0, 0.0, 1.0]] {
        db.upsert_embeddings(&m, vec![("c0".to_string(), v)])
            .await
            .unwrap();
    }

    let mut rows = db
        .read_conn()
        .query(&format!("SELECT COUNT(*) FROM {}", m.table()), ())
        .await
        .unwrap();
    let n: i64 = rows.next().await.unwrap().unwrap().get(0).unwrap();
    assert_eq!(n, 1, "a second embedding must replace the first");
    assert_eq!(
        log_len(&db).await,
        before,
        "an embedding reached the ledger"
    );
}

/// A chunk is one transaction, so a bad vector in the middle takes the whole
/// chunk with it rather than leaving a prefix behind.
#[tokio::test]
async fn a_dimension_mismatch_rolls_back_its_whole_chunk() {
    use macrame::prelude::*;

    let harness = TestHarness::new();
    let db = Database::open(&harness.db_path).await.unwrap();
    let m = ModelName::new("handle_v1").unwrap();
    for id in ["c0", "c1"] {
        db.upsert_concept(ConceptUpsert::new(id, "N").valid_from(TS))
            .await
            .unwrap();
    }
    db.register_model(&m, 4).await.unwrap();

    let err = db
        .upsert_embeddings(
            &m,
            vec![
                ("c0".to_string(), vec![1.0, 0.0, 0.0, 0.0]),
                ("c1".to_string(), vec![1.0, 0.0]), // wrong width
            ],
        )
        .await
        .unwrap_err();
    assert!(matches!(err, DbError::DimMismatch { .. }), "got {err:?}");

    let mut rows = db
        .read_conn()
        .query(&format!("SELECT COUNT(*) FROM {}", m.table()), ())
        .await
        .unwrap();
    let n: i64 = rows.next().await.unwrap().unwrap().get(0).unwrap();
    assert_eq!(n, 0, "the good row before the bad one must not survive");
}

/// Embedding a model nobody registered is a typed refusal, not an engine error
/// about a missing table.
#[tokio::test]
async fn embedding_an_unregistered_model_is_refused_by_name() {
    use macrame::prelude::*;

    let harness = TestHarness::new();
    let db = Database::open(&harness.db_path).await.unwrap();
    let m = ModelName::new("never_registered").unwrap();

    let err = db
        .upsert_embeddings(&m, vec![("c0".to_string(), vec![1.0])])
        .await
        .unwrap_err();
    assert!(
        matches!(err, DbError::ModelNotRegistered { .. }),
        "got {err:?}"
    );
}

/// More rows than `chunk_rows::EMBEDDINGS`, so the chunking loop runs more than
/// once and every chunk's rows land.
#[tokio::test]
async fn a_backfill_larger_than_one_chunk_lands_completely() {
    use macrame::prelude::*;

    let harness = TestHarness::new();
    let db = Database::open(&harness.db_path).await.unwrap();
    let m = ModelName::new("handle_v1").unwrap();

    let n = chunk_rows::EMBEDDINGS + 7;
    let mut concepts = Vec::with_capacity(n);
    for i in 0..n {
        concepts.push(ConceptUpsert::new(format!("c{i:06}"), "N").valid_from(TS));
    }
    db.write_concepts(concepts).await.unwrap();
    db.register_model(&m, 2).await.unwrap();

    let rows: Vec<(String, Vec<f32>)> = (0..n)
        .map(|i| (format!("c{i:06}"), vec![i as f32, 1.0]))
        .collect();
    assert_eq!(db.upsert_embeddings(&m, rows).await.unwrap(), n);

    let mut got = db
        .read_conn()
        .query(&format!("SELECT COUNT(*) FROM {}", m.table()), ())
        .await
        .unwrap();
    let stored: i64 = got.next().await.unwrap().unwrap().get(0).unwrap();
    assert_eq!(stored as usize, n);
}