vipune 0.19.0

A minimal memory layer for AI agents
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
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
//! Tests for `vipune reindex` handler.

#![cfg(test)]

use crate::commands::reindex::*;
use crate::errors::Error;
use crate::memory::crud::{mock_embedding_for_content, test_fake_embedder};
use crate::memory::store::MemoryStore;
use crate::output::ReindexFailure;
use crate::sqlite::Database;
use crate::sqlite::embedding::{EmbeddingClass, classify_embedding};
use rusqlite::Connection;

fn reindex_project_with_fake_embedder(
    db: &Database,
    project_id: &str,
) -> Result<(usize, usize, Vec<ReindexFailure>), Error> {
    let mut embed_callback = |content: &str| test_fake_embedder(content);
    reindex_project(db, &mut embed_callback, project_id, false)
}

fn create_test_db() -> (tempfile::TempDir, std::path::PathBuf) {
    let dir = tempfile::TempDir::new().unwrap();
    let path = dir.path().join("test.db");
    Database::open(&path).unwrap();
    (dir, path)
}

fn get_embedding(db: &Database, id: &str) -> Vec<f32> {
    db.list_all_rows_for_project("proj")
        .unwrap()
        .iter()
        .find(|(i, _, _)| i == id)
        .map(|(_, _, e)| e.clone())
        .unwrap()
}

#[test]
fn test_mock_rows_are_repaired() {
    let (_dir, db_path) = create_test_db();
    let db = Database::open(&db_path).unwrap();
    let content = "mock memory";
    let mock_vec = mock_embedding_for_content(content);
    let id = db
        .insert("proj", content, &mock_vec, None, "fact", "active")
        .unwrap();

    assert_eq!(
        classify_embedding(&get_embedding(&db, &id)),
        EmbeddingClass::Mock
    );

    let (reindexed, skipped, failed) = reindex_project_with_fake_embedder(&db, "proj").unwrap();
    assert_eq!(reindexed, 1);
    assert_eq!(skipped, 0);
    assert_eq!(failed.len(), 0);

    let emb_after = get_embedding(&db, &id);
    assert_ne!(mock_vec, emb_after);
    assert_eq!(classify_embedding(&emb_after), EmbeddingClass::Real);
}

#[test]
fn test_real_rows_untouched() {
    let (_dir, db_path) = create_test_db();
    let db = Database::open(&db_path).unwrap();
    let real_vec = test_fake_embedder("real memory").unwrap();
    let id = db
        .insert("proj", "real memory", &real_vec, None, "fact", "active")
        .unwrap();

    let (reindexed, _, _) = reindex_project_with_fake_embedder(&db, "proj").unwrap();
    assert_eq!(reindexed, 0);
    assert_eq!(get_embedding(&db, &id), real_vec);
}

#[test]
fn test_unknown_rows_skipped() {
    let (_dir, db_path) = create_test_db();
    let db = Database::open(&db_path).unwrap();
    db.insert(
        "proj",
        "real",
        &test_fake_embedder("real").unwrap(),
        None,
        "fact",
        "active",
    )
    .unwrap();
    let zero_vec = vec![0.0; 384];
    let id = db
        .insert("proj", "unknown", &zero_vec, None, "fact", "active")
        .unwrap();

    assert_eq!(
        classify_embedding(&get_embedding(&db, &id)),
        EmbeddingClass::Unknown
    );

    let (_, skipped, _) = reindex_project_with_fake_embedder(&db, "proj").unwrap();
    assert_eq!(skipped, 1);
    assert_eq!(get_embedding(&db, &id), zero_vec);
}

#[test]
fn test_idempotency_second_run_zero_changes() {
    let (_dir, db_path) = create_test_db();
    let db = Database::open(&db_path).unwrap();
    let id1 = db
        .insert(
            "proj",
            "A",
            &mock_embedding_for_content("A"),
            None,
            "fact",
            "active",
        )
        .unwrap();
    let id2 = db
        .insert(
            "proj",
            "B",
            &mock_embedding_for_content("B"),
            None,
            "fact",
            "active",
        )
        .unwrap();

    let (r1, _, _) = reindex_project_with_fake_embedder(&db, "proj").unwrap();
    assert_eq!(r1, 2);

    let emb1_first = get_embedding(&db, &id1);
    let emb2_first = get_embedding(&db, &id2);

    let (r2, _, _) = reindex_project_with_fake_embedder(&db, "proj").unwrap();
    assert_eq!(r2, 0);
    assert_eq!(get_embedding(&db, &id1), emb1_first);
    assert_eq!(get_embedding(&db, &id2), emb2_first);
}

#[test]
fn test_timestamps_preserved() {
    let (_dir, db_path) = create_test_db();
    let db = Database::open(&db_path).unwrap();
    let mock_vec = mock_embedding_for_content("mock");
    let id = db
        .insert("proj", "mock", &mock_vec, None, "fact", "active")
        .unwrap();

    let conn = db.conn();
    conn.execute(
        "UPDATE memories SET retrieval_count = 7, last_retrieved_at = '2024-03-20T14:30:00Z' WHERE id = ?",
        [&id],
    )
    .unwrap();

    let (u_before, rc_before, lr_before) = conn
        .query_row(
            "SELECT updated_at, retrieval_count, last_retrieved_at FROM memories WHERE id = ?",
            [&id],
            |row| {
                Ok((
                    row.get::<_, String>(0)?,
                    row.get::<_, i64>(1)?,
                    row.get::<_, Option<String>>(2)?,
                ))
            },
        )
        .unwrap();

    reindex_project_with_fake_embedder(&db, "proj").unwrap();

    let (u_after, rc_after, lr_after) = conn
        .query_row(
            "SELECT updated_at, retrieval_count, last_retrieved_at FROM memories WHERE id = ?",
            [&id],
            |row| {
                Ok((
                    row.get::<_, String>(0)?,
                    row.get::<_, i64>(1)?,
                    row.get::<_, Option<String>>(2)?,
                ))
            },
        )
        .unwrap();

    assert_eq!(u_before, u_after);
    assert_eq!(rc_before, rc_after);
    assert_eq!(lr_before, lr_after);
}

#[test]
fn test_counters_match_seeded_data() {
    let (_dir, db_path) = create_test_db();
    let db = Database::open(&db_path).unwrap();
    db.insert(
        "proj",
        "m1",
        &mock_embedding_for_content("m1"),
        None,
        "fact",
        "active",
    )
    .unwrap();
    db.insert(
        "proj",
        "m2",
        &mock_embedding_for_content("m2"),
        None,
        "fact",
        "active",
    )
    .unwrap();
    db.insert(
        "proj",
        "m3",
        &mock_embedding_for_content("m3"),
        None,
        "fact",
        "active",
    )
    .unwrap();
    db.insert(
        "proj",
        "r1",
        &test_fake_embedder("r1").unwrap(),
        None,
        "fact",
        "active",
    )
    .unwrap();
    db.insert(
        "proj",
        "r2",
        &test_fake_embedder("r2").unwrap(),
        None,
        "fact",
        "active",
    )
    .unwrap();
    db.insert("proj", "u", &vec![0.0; 384], None, "fact", "active")
        .unwrap();

    let (reindexed, skipped, failed) = reindex_project_with_fake_embedder(&db, "proj").unwrap();
    assert_eq!(reindexed, 3);
    assert_eq!(skipped, 1);
    assert_eq!(failed.len(), 0);
}

#[test]
fn test_database_update_embedding_does_not_touch_updated_at() {
    let (_dir, db) = MemoryStore::test_store();
    let emb = test_fake_embedder("orig").unwrap();
    let id = db
        .db
        .insert("proj", "orig", &emb, None, "fact", "active")
        .unwrap();

    let t1: String = db
        .db
        .conn()
        .query_row("SELECT updated_at FROM memories WHERE id = ?", [&id], |r| {
            r.get(0)
        })
        .unwrap();

    std::thread::sleep(std::time::Duration::from_millis(10));
    let emb2 = test_fake_embedder("up").unwrap();
    db.db.update_embedding(&id, &emb2).unwrap();

    let t2: String = db
        .db
        .conn()
        .query_row("SELECT updated_at FROM memories WHERE id = ?", [&id], |r| {
            r.get(0)
        })
        .unwrap();
    assert_eq!(t1, t2);
}

#[test]
fn test_locked_database_fast_fails() {
    let (_dir, db_path) = create_test_db();

    // Open a raw SQLite connection and begin an exclusive transaction to lock the database
    let lock_conn = Connection::open(&db_path).unwrap();
    lock_conn.execute("BEGIN EXCLUSIVE", []).unwrap();

    // Now try to open the database with handle_reindex - it should fail fast
    let result = handle_reindex(&db_path, "BAAI/bge-small-en-v1.5", None, false, false);
    // Verify the error is the actionable Config error, not a timeout or hang
    match result {
        Err(Error::Config(msg)) => {
            assert!(
                msg.contains("locked"),
                "Expected 'locked' in error message, got: {}",
                msg
            );
            assert!(
                msg.contains("MCP server"),
                "Expected 'MCP server' in error message, got: {}",
                msg
            );
        }
        Err(e) => panic!(
            "Expected Error::Config with locked database message, got: {:?}",
            e
        ),
        Ok(_) => panic!("Expected error when database is locked, got Ok"),
    }

    // Release lock
    lock_conn.execute("ROLLBACK", []).unwrap();
}

#[test]
fn locked_storage_error_surfaces_as_the_structured_public_variant() {
    // The CLI assertion in `test_locked_database_fast_fails` proves the
    // user-visible text; this proves the classification underneath it without
    // depending on a second `Database::open` (which also runs schema creation
    // and migrations) happening to fail while the lock is held.
    let busy = rusqlite::Error::SqliteFailure(
        rusqlite::ffi::Error::new(rusqlite::ffi::SQLITE_BUSY),
        Some("database is locked".to_string()),
    );
    let public: Error = crate::sqlite::Error::from(busy).into();
    assert!(
        matches!(public, Error::DatabaseLocked) && public.is_locked(),
        "a locked database must surface as Error::DatabaseLocked, got {public:?}"
    );
}

// ── Hint text: pure function tests ──
//
// The hint is emitted by `handle_reindex` when scoped to a single project and
// other projects exist in the database. We test the format string here via a
// helper that mirrors the logic in `handle_reindex`, so the test stays fast
// and doesn't require stdout capture.

fn hint_text_for(other_count: usize) -> Option<String> {
    // Mirrors the logic in handle_reindex: emit the hint only when other_count > 0.
    if other_count > 0 {
        Some(format!(
            "Note: {} other project(s) in this database not reindexed. Run with --all-projects to reindex them.",
            other_count
        ))
    } else {
        None
    }
}

#[test]
fn test_hint_text_scoped_multiple_projects() {
    let hint = hint_text_for(4).unwrap();
    assert_eq!(
        hint,
        "Note: 4 other project(s) in this database not reindexed. Run with --all-projects to reindex them."
    );
}

#[test]
fn test_hint_text_scoped_single_project() {
    let hint = hint_text_for(1).unwrap();
    assert_eq!(
        hint,
        "Note: 1 other project(s) in this database not reindexed. Run with --all-projects to reindex them."
    );
}

#[test]
fn test_hint_text_no_other_projects() {
    assert_eq!(hint_text_for(0), None);
}

// ── Footer text: pure function tests ──

fn footer_scope_text(projects: &[String]) -> String {
    if projects.len() == 1 {
        format!("project {}", projects[0])
    } else {
        "all projects".to_string()
    }
}

#[test]
fn test_footer_scope_single_project() {
    let projects = vec!["my-proj".to_string()];
    assert_eq!(footer_scope_text(&projects), "project my-proj");
}

#[test]
fn test_footer_scope_multiple_projects() {
    let projects = vec!["a".to_string(), "b".to_string(), "c".to_string()];
    assert_eq!(footer_scope_text(&projects), "all projects");
}

#[test]
fn test_force_refused_without_all_projects() {
    // `reindex --force` is a per-database migration (issue #217): a project
    // filter would leave a silently mixed store, so the handler refuses to
    // start (exit 1) rather than partially migrate. No marker is written.
    let (_dir, db_path) = create_test_db();
    let db = Database::open(&db_path).unwrap();
    db.insert(
        "proj",
        "content",
        &test_fake_embedder("c").unwrap(),
        None,
        "fact",
        "active",
    )
    .unwrap();

    let result = handle_reindex(
        &db_path,
        "BAAI/bge-small-en-v1.5",
        Some("proj"),
        true,
        false,
    );
    let exit = result.expect("handler returns Ok(ExitCode::from(1)) on refusal");
    assert_ne!(
        exit,
        std::process::ExitCode::SUCCESS,
        "force without --all-projects must not exit 0"
    );

    // No marker was written, no identity recorded.
    let db2 = Database::open(&db_path).unwrap();
    assert!(!identity::is_migrating(db2.conn()).unwrap());
    assert!(identity::read_identity(db2.conn()).unwrap().is_none());
}

// ── reindex --force: model-switch migration path (issue #217) ──
//
// The --force path is exercised through the library's migration core in
// `crate::migration` (per-project re-embed pass, marker write, record-and-
// clear) plus the read side in `crate::sqlite::identity`.
use crate::migration;
use crate::sqlite::identity;

fn force_reindex_project_with_fake_embedder(
    db: &mut Database,
    project_id: &str,
) -> Result<
    (
        usize,
        usize,
        Vec<crate::sqlite::migration_types::MigrationRowFailure>,
    ),
    crate::sqlite::Error,
> {
    migration::reembed_project(db, project_id, |content| {
        test_fake_embedder(content).map_err(|e| crate::sqlite::Error::Sqlite(e.to_string()))
    })
}

#[test]
fn test_force_reembeds_all_rows_including_real() {
    let (_dir, db_path) = create_test_db();
    let mut db = Database::open(&db_path).unwrap();
    let real_vec = test_fake_embedder("real memory").unwrap();
    let id = db
        .insert("proj", "real memory", &real_vec, None, "fact", "active")
        .unwrap();
    let mock_vec = mock_embedding_for_content("mock memory");
    let id2 = db
        .insert("proj", "mock memory", &mock_vec, None, "fact", "active")
        .unwrap();
    let unknown_vec = vec![0.0; 384];
    let id3 = db
        .insert(
            "proj",
            "unknown memory",
            &unknown_vec,
            None,
            "fact",
            "active",
        )
        .unwrap();

    let (reindexed, skipped, failed) =
        force_reindex_project_with_fake_embedder(&mut db, "proj").unwrap();

    // Force re-embeds every Mock and Real row (including Real-classified
    // vectors that plain reindex would skip); only Unknown (corrupted) rows skip.
    assert_eq!(reindexed, 2);
    assert_eq!(skipped, 1);
    assert_eq!(failed.len(), 0);

    // All re-embedded rows now hold fresh embeddings from the fake embedder.
    let emb_after = get_embedding(&db, &id);
    let emb_after2 = get_embedding(&db, &id2);
    // The fake embedder is deterministic, so re-embedding "real memory"
    // reproduces the same vector. Classify as Real to confirm the new vector
    // is in the valid norm band.
    assert_eq!(emb_after, real_vec);
    assert_eq!(classify_embedding(&emb_after), EmbeddingClass::Real);
    assert_ne!(emb_after2, mock_vec);
    assert_eq!(classify_embedding(&emb_after2), EmbeddingClass::Real);
    // The unknown (zero-vector) row was skipped — left untouched.
    assert_eq!(get_embedding(&db, &id3), unknown_vec);
}

#[test]
fn test_force_idempotent_rerun() {
    let (_dir, db_path) = create_test_db();
    let mut db = Database::open(&db_path).unwrap();
    let mock_vec = mock_embedding_for_content("A");
    let id = db
        .insert("proj", "A", &mock_vec, None, "fact", "active")
        .unwrap();

    let (r1, _, _) = force_reindex_project_with_fake_embedder(&mut db, "proj").unwrap();
    assert_eq!(r1, 1);
    let emb1 = get_embedding(&db, &id);

    // Simulate an interrupted run: marker written, some rows re-embedded,
    // then re-run. Force path re-embeds every row from the start.
    let (r2, _, _) = force_reindex_project_with_fake_embedder(&mut db, "proj").unwrap();
    assert_eq!(
        r2, 1,
        "force must re-embed every row on re-run, not skip Real"
    );
    // The fake embedder is deterministic, so the second pass yields the
    // same vector (idempotent at the vector level, not a no-op at the row level).
    assert_eq!(get_embedding(&db, &id), emb1);
}

/// Write a migration marker (marker-first; identity left untouched).
fn marker_only(conn: &rusqlite::Connection, target: &identity::ModelIdentity) {
    conn.execute(
        "INSERT INTO model_identity (id, model_id, model_revision, migration_marker)
         VALUES (1, NULL, NULL, ?1)
         ON CONFLICT(id) DO UPDATE SET migration_marker = excluded.migration_marker",
        [migration::migration_marker_for(target)],
    )
    .unwrap();
}

/// Record the new identity and clear the marker in one transaction (the only
/// sanctioned exit from the "migrating" state).
fn record_identity(conn: &rusqlite::Connection, identity: &identity::ModelIdentity) {
    conn.execute(
        "INSERT INTO model_identity (id, model_id, model_revision, migration_marker)
         VALUES (1, ?1, ?2, NULL)
         ON CONFLICT(id) DO UPDATE SET model_id = excluded.model_id,
                                      model_revision = excluded.model_revision,
                                      migration_marker = NULL",
        (&identity.model_id, &identity.revision),
    )
    .unwrap();
}

#[test]
fn test_force_with_all_projects_migrates_entire_database() {
    // Per-database migration lifecycle through the same helpers the handler
    // uses: marker written once before the pass, then identity recorded +
    // marker cleared only after every project is re-embedded. (The handler
    // itself constructs a real EmbeddingEngine, which would download the
    // model, so the wiring is verified here with a fake embedder; the
    // handler's refusal path is covered by
    // test_force_refused_without_all_projects above.)
    let (_dir, db_path) = create_test_db();
    let mut db = Database::open(&db_path).unwrap();
    db.insert(
        "a",
        "alpha",
        &test_fake_embedder("a").unwrap(),
        None,
        "fact",
        "active",
    )
    .unwrap();
    db.insert(
        "b",
        "beta",
        &test_fake_embedder("b").unwrap(),
        None,
        "fact",
        "active",
    )
    .unwrap();

    let target = identity::ModelIdentity {
        model_id: "BAAI/bge-small-en-v1.5".to_string(),
        revision: crate::embedding::EMBED_MODEL_REVISION.to_string(),
    };
    marker_only(db.conn(), &target);
    let _ = migration::reembed_project(&mut db, "a", |_| Ok(vec![0.1; 384])).unwrap();
    let _ = migration::reembed_project(&mut db, "b", |_| Ok(vec![0.1; 384])).unwrap();
    record_identity(db.conn(), &target);

    assert_eq!(identity::current_identity(db.conn()).unwrap(), target);
    assert!(!identity::is_migrating(db.conn()).unwrap());
}

#[test]
fn test_force_marker_written_first_then_cleared() {
    let (_dir, db_path) = create_test_db();
    let mut db = Database::open(&db_path).unwrap();
    db.insert(
        "proj",
        "content",
        &test_fake_embedder("c").unwrap(),
        None,
        "fact",
        "active",
    )
    .unwrap();

    let target = identity::ModelIdentity {
        model_id: "intfloat/multilingual-e5-small".to_string(),
        revision: "614241f622f53c4eeff9890bdc4f31cfecc418b3".to_string(),
    };

    // Step 1: marker-first write (before any re-embedding) — touches only
    // the marker column; the recorded identity (none here) is untouched.
    marker_only(db.conn(), &target);
    assert_eq!(
        identity::read_marker(db.conn()).unwrap().as_deref(),
        Some(
            "migrating to intfloat/multilingual-e5-small@614241f622f53c4eeff9890bdc4f31cfecc418b3"
        )
    );

    // Step 2: re-embed all rows (simulated via the force loop).
    force_reindex_project_with_fake_embedder(&mut db, "proj").unwrap();
    // Marker is still present while the re-embed pass runs.
    assert!(identity::read_marker(db.conn()).unwrap().is_some());

    // Step 3: record identity + clear marker in one transaction.
    record_identity(db.conn(), &target);
    assert_eq!(identity::read_identity(db.conn()).unwrap(), Some(target));
    assert_eq!(identity::read_marker(db.conn()).unwrap(), None);
}

#[test]
fn test_force_marker_survives_interruption() {
    let (_dir, db_path) = create_test_db();
    let mut db = Database::open(&db_path).unwrap();
    db.insert(
        "proj",
        "content",
        &test_fake_embedder("c").unwrap(),
        None,
        "fact",
        "active",
    )
    .unwrap();

    let target = identity::ModelIdentity {
        model_id: "intfloat/multilingual-e5-small".to_string(),
        revision: "614241f622f53c4eeff9890bdc4f31cfecc418b3".to_string(),
    };

    // Marker written, then the process dies before the re-embed loop runs.
    marker_only(db.conn(), &target);
    // Simulate a crash: no record_identity call. The marker must still be
    // present, so subsequent operations can refuse.
    assert!(identity::read_marker(db.conn()).unwrap().is_some());

    // Re-run: force re-embed + record identity + clear marker.
    force_reindex_project_with_fake_embedder(&mut db, "proj").unwrap();
    record_identity(db.conn(), &target);
    assert_eq!(identity::read_marker(db.conn()).unwrap(), None);
    assert_eq!(identity::read_identity(db.conn()).unwrap(), Some(target));
}

/// Private scan helper mirroring the library's pre-flight scan with an
/// injectable token counter — no real `EmbeddingEngine` needed (no model download).
fn over_limit_ids_with_counter(
    db: &Database,
    projects: &[String],
    passage_prefix: &str,
    count: impl Fn(&str) -> Result<usize, crate::sqlite::Error>,
) -> Result<Vec<String>, crate::sqlite::Error> {
    let mut offending: Vec<String> = Vec::new();
    for project_id in projects {
        let rows = db.list_all_rows_for_project(project_id)?;
        for (id, content, _embedding) in rows {
            let prefixed = format!("{passage_prefix}{content}");
            let count = count(&prefixed)?;
            if count > crate::embedding::MAX_EMBEDDING_TOKENS {
                offending.push(id);
            }
        }
    }
    Ok(offending)
}

#[test]
fn test_force_preflight_flags_overlength_rows_and_refuses_to_start() {
    // Decision 1: reindex --force token-counts every row with the target
    // profile's passage prefix BEFORE writing the marker. A row whose
    // prefixed content exceeds 512 tokens must be reported by the pre-flight
    // scan, and the caller must refuse to start (write nothing).
    //
    // The token-count boundary itself (512 tokens) is covered by the
    // existing embed() boundary tests in src/embedding.rs. Here we verify
    // the scan logic with a stub counter so no model download is needed:
    // the stub counts words, so any row with >512 words is flagged.
    let (_dir, db_path) = create_test_db();
    let db = Database::open(&db_path).unwrap();

    // A row well over 512 words (hence well over 512 tokens).
    let long_content: String = (0..600)
        .map(|i| format!("word{i}"))
        .collect::<Vec<_>>()
        .join(" ");
    let long_id = db
        .insert(
            "proj",
            &long_content,
            &vec![0.5; 384],
            None,
            "fact",
            "active",
        )
        .unwrap();
    // A short row that fits comfortably.
    let short_id = db
        .insert(
            "proj",
            "short content",
            &vec![0.5; 384],
            None,
            "fact",
            "active",
        )
        .unwrap();

    // Stub counter: counts whitespace-separated tokens (a lower bound on the
    // real token count, so the >512 threshold is conservative).
    let count_words =
        |text: &str| -> Result<usize, crate::sqlite::Error> { Ok(text.split_whitespace().count()) };

    let projects = vec!["proj".to_string()];
    let offending = over_limit_ids_with_counter(&db, &projects, "passage: ", count_words).unwrap();

    assert_eq!(offending.len(), 1, "only the long row should be reported");
    assert_eq!(offending[0], long_id);
    assert!(
        !offending.iter().any(|id| id == &short_id),
        "short row must not be reported"
    );
}

#[test]
fn test_force_no_marker_when_identity_matches() {
    // If the database already has the target identity recorded and no marker,
    // a force re-embed pass leaves the identity unchanged (idempotent).
    let (_dir, db_path) = create_test_db();
    let mut db = Database::open(&db_path).unwrap();
    db.insert(
        "proj",
        "content",
        &test_fake_embedder("c").unwrap(),
        None,
        "fact",
        "active",
    )
    .unwrap();

    let target = identity::ModelIdentity::default_identity();
    record_identity(db.conn(), &target);
    force_reindex_project_with_fake_embedder(&mut db, "proj").unwrap();
    record_identity(db.conn(), &target);
    assert_eq!(identity::read_identity(db.conn()).unwrap(), Some(target));
    assert_eq!(identity::read_marker(db.conn()).unwrap(), None);
}