sqlite-graphrag 1.0.78

Persistent GraphRAG memory for Claude Code, Codex, Cursor, and 24+ AI agents in a single 6 MB Rust binary. LLM-only and one-shot in v1.0.78: every `remember` / `ingest` spawns a headless claude code or codex subprocess (OAuth, no MCP, no hooks). No daemon. No ONNX runtime. No model download. Graph-native retrieval with FTS5 + cosine + multi-hop traversal. OAuth-only enforcement: API keys ABORT the spawn.
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
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
#![cfg(feature = "slow-tests")]

// Suite 3 — Schema and migrations validation V001-V009
//
// ISOLATION: each test uses `SQLITE_GRAPHRAG_DB_PATH` pointing to a SQLite
// file in an exclusive `TempDir`. Introspection runs through rusqlite directly,
// without depending on any binary output.
//
// NOTE: sqlite-vec uses `sqlite3_auto_extension`, which is process-global.
// To avoid registering the extension multiple times in parallel tests,
// every test that opens a sqlite-vec database does so via `sqlite-graphrag init`
// (external binary), which loads the extension in its own process. Pure
// introspection tests (sqlite_master, triggers, FTS) open the database via
// rusqlite after init for read-only queries — they do not load sqlite-vec
// in the test process.
//
// `#[serial]` is mandatory: although each test uses its own DB, the compiled
// artefact is shared and `TempDir` is only released after the test ends;
// serialising eliminates filesystem races and makes timings predictable.

use assert_cmd::Command;
use rusqlite::Connection;
use serial_test::serial;
use tempfile::TempDir;

/// Builds a fresh `Command` with the mock LLM PATH prepended.
///
/// v1.0.76 spawns `claude` or `codex` on every `remember` / `ingest` /
/// `edit`. The bundled mocks under `tests/mock-llm/` return a fixed
/// 384-dim zero vector so the binary finishes without a real OAuth
/// login. The mock directory is leaked (no TempDir cleanup) so the
/// spawned subprocess always finds the mocks.
fn sgr_cmd() -> Command {
    let mock_dir = common::mock_llm_path();
    let mut c = Command::cargo_bin("sqlite-graphrag").expect("sqlite-graphrag binary not found");
    c.env("PATH", common::prepend_path(&mock_dir));
    c
}

#[path = "common/mod.rs"]
mod common;

// ---------------------------------------------------------------------------
// helpers
// ---------------------------------------------------------------------------

/// Runs `sqlite-graphrag init` on an isolated temporary database and returns
/// the `TempDir` (to keep the database alive) and the SQLite file path.
fn init_isolated_db() -> (TempDir, std::path::PathBuf) {
    let tmp = TempDir::new().expect("TempDir must be created");
    let db_path = tmp.path().join("test.sqlite");

    sgr_cmd()
        .env("SQLITE_GRAPHRAG_DB_PATH", &db_path)
        .env("SQLITE_GRAPHRAG_CACHE_DIR", tmp.path())
        .args(["--skip-memory-guard", "init"])
        .assert()
        .success();

    (tmp, db_path)
}

/// Opens the database read-only after init (without sqlite-vec in the test process).
fn conn_ro(db_path: &std::path::Path) -> Connection {
    Connection::open(db_path).expect("database connection must work")
}

/// Checks whether a table or view exists in `sqlite_master`.
fn table_exists(conn: &Connection, name: &str) -> bool {
    let count: i64 = conn
        .query_row(
            "SELECT COUNT(*) FROM sqlite_master WHERE type IN ('table','view') AND name = ?1",
            rusqlite::params![name],
            |row| row.get(0),
        )
        .unwrap_or(0);
    count > 0
}

/// Checks whether a trigger exists in `sqlite_master`.
fn trigger_exists(conn: &Connection, name: &str) -> bool {
    let count: i64 = conn
        .query_row(
            "SELECT COUNT(*) FROM sqlite_master WHERE type = 'trigger' AND name = ?1",
            rusqlite::params![name],
            |row| row.get(0),
        )
        .unwrap_or(0);
    count > 0
}

/// Checks if an index exists in `sqlite_master`.
fn index_exists(conn: &Connection, name: &str) -> bool {
    let count: i64 = conn
        .query_row(
            "SELECT COUNT(*) FROM sqlite_master WHERE type = 'index' AND name = ?1",
            rusqlite::params![name],
            |row| row.get(0),
        )
        .unwrap_or(0);
    count > 0
}

// ---------------------------------------------------------------------------
// Test 1 — init applies exactly 13 migrations V001 through V013
// ---------------------------------------------------------------------------
// v1.0.76 added V012 and V013 on top of the historical V001-V011 set.

#[test]
#[serial]
fn init_creates_13_migrations_v001_to_v013() {
    let (_tmp, db_path) = init_isolated_db();
    let conn = conn_ro(&db_path);

    let versions: Vec<i64> = {
        let mut stmt = conn
            .prepare("SELECT version FROM refinery_schema_history ORDER BY version ASC")
            .expect("prepare must work");
        stmt.query_map([], |row| row.get(0))
            .expect("query must work")
            .map(|r| r.expect("row must be readable"))
            .collect()
    };

    assert_eq!(
        versions.len(),
        13,
        "exactly 13 migrations must be applied, found: {versions:?}"
    );
    assert_eq!(
        versions,
        vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13],
        "expected versions V001-V013"
    );
}

// ---------------------------------------------------------------------------
// Test 2 — trigger trg_fts_ai exists after V004
// ---------------------------------------------------------------------------

#[test]
#[serial]
fn trigger_trg_fts_ai_exists() {
    let (_tmp, db_path) = init_isolated_db();
    let conn = conn_ro(&db_path);

    assert!(
        trigger_exists(&conn, "trg_fts_ai"),
        "trigger trg_fts_ai must exist after V004"
    );
}

// ---------------------------------------------------------------------------
// Test 3 — trigger trg_fts_ad exists after V004
// ---------------------------------------------------------------------------

#[test]
#[serial]
fn trigger_trg_fts_ad_exists() {
    let (_tmp, db_path) = init_isolated_db();
    let conn = conn_ro(&db_path);

    assert!(
        trigger_exists(&conn, "trg_fts_ad"),
        "trigger trg_fts_ad must exist after V004"
    );
}

// ---------------------------------------------------------------------------
// Test 4 — trigger trg_fts_au is INTENTIONALLY ABSENT (FTS5 sync handled in Rust)
// ---------------------------------------------------------------------------
// v1.0.76 removed sqlite-vec, but the design choice of handling FTS5 sync
// in Rust (edit.rs, rename.rs, restore.rs) instead of a trigger is kept.
// trg_fts_ai and trg_fts_ad are created by V004; trg_fts_au is NOT,
// because the Rust handlers cover UPDATE-equivalent operations explicitly
// and we avoid the historical sqlite-vec / FTS5 conflict inside the
// trigger body for symmetry with the v1.0.74 design.

#[test]
#[serial]
fn trigger_trg_fts_au_absent_handled_in_rust() {
    let (_tmp, db_path) = init_isolated_db();
    let conn = conn_ro(&db_path);

    assert!(
        !trigger_exists(&conn, "trg_fts_au"),
        "trigger trg_fts_au must NOT exist — FTS5 sync is handled in Rust (edit.rs, rename.rs, restore.rs)"
    );
}

// ---------------------------------------------------------------------------
// Test 5 — memory_embeddings uses BLOB and dim=384 (v1.0.76 replacement for vec_memories)
// ---------------------------------------------------------------------------
// v1.0.76 dropped vec_memories (sqlite-vec virtual table) and replaced it with
// a regular BLOB-backed memory_embeddings table. The embedding dimensionality
// is recorded in the dim column rather than in the DDL. Cosine similarity is
// computed in pure Rust at query time (src/similarity.rs).

#[test]
#[serial]
fn memory_embeddings_blob_dim_384() {
    let (_tmp, db_path) = init_isolated_db();
    let conn = conn_ro(&db_path);

    let ddl: String = conn
        .query_row(
            "SELECT sql FROM sqlite_master WHERE name = 'memory_embeddings'",
            [],
            |row| row.get(0),
        )
        .expect("memory_embeddings must exist in sqlite_master");

    assert!(
        ddl.contains("BLOB"),
        "memory_embeddings must declare embedding as BLOB, DDL was: {ddl}"
    );
    assert!(
        ddl.contains("dim"),
        "memory_embeddings must declare a dim column, DDL was: {ddl}"
    );
    assert!(
        ddl.contains("384"),
        "memory_embeddings must default dim to 384, DDL was: {ddl}"
    );

    // Confirm sqlite-vec tables are GONE.
    let vec_present: i64 = conn
        .query_row(
            "SELECT COUNT(*) FROM sqlite_master WHERE name = 'vec_memories'",
            [],
            |row| row.get(0),
        )
        .unwrap_or(1);
    assert_eq!(
        vec_present, 0,
        "vec_memories must NOT exist after V013, but it is still present"
    );
}

// ---------------------------------------------------------------------------
// Test 6 — memory_embeddings has 2 partition-like indexes (namespace, source)
// ---------------------------------------------------------------------------
// vec_memories used sqlite-vec partition keys. memory_embeddings uses regular
// SQLite indexes. The functional requirement is "find embeddings by namespace"
// and "audit embeddings by source".

#[test]
#[serial]
fn memory_embeddings_partition_indexes() {
    let (_tmp, db_path) = init_isolated_db();
    let conn = conn_ro(&db_path);

    let has_ns_index: i64 = conn
        .query_row(
            "SELECT COUNT(*) FROM sqlite_master WHERE name = 'idx_memory_embeddings_ns'",
            [],
            |row| row.get(0),
        )
        .unwrap_or(0);
    assert_eq!(
        has_ns_index, 1,
        "idx_memory_embeddings_ns must exist (namespace partition)"
    );

    let has_source_index: i64 = conn
        .query_row(
            "SELECT COUNT(*) FROM sqlite_master WHERE name = 'idx_memory_embeddings_source'",
            [],
            |row| row.get(0),
        )
        .unwrap_or(0);
    assert_eq!(
        has_source_index, 1,
        "idx_memory_embeddings_source must exist (source partition)"
    );
}

// ---------------------------------------------------------------------------
// Test 7 — fts_memories uses tokenizer unicode61 with remove_diacritics 1
// ---------------------------------------------------------------------------

#[test]
#[serial]
fn fts_memories_tokenizer_unicode61_remove_diacritics() {
    let (_tmp, db_path) = init_isolated_db();
    let conn = conn_ro(&db_path);

    let ddl: String = conn
        .query_row(
            "SELECT sql FROM sqlite_master WHERE name = 'fts_memories'",
            [],
            |row| row.get(0),
        )
        .expect("fts_memories must exist in sqlite_master");

    assert!(
        ddl.contains("unicode61"),
        "fts_memories must use the unicode61 tokenizer, DDL: {ddl}"
    );
    assert!(
        ddl.contains("remove_diacritics"),
        "fts_memories must declare remove_diacritics, DDL: {ddl}"
    );
}

// ---------------------------------------------------------------------------
// Test 8 — FTS5 search 'cafe' matches text containing 'café' (remove_diacritics)
// ---------------------------------------------------------------------------
// Inserts a memory with an accented body via the CLI and verifies that an
// unaccented search succeeds, confirming that remove_diacritics is active.

#[test]
#[serial]
fn fts5_matching_with_accents_cafe_cafe() {
    let tmp = TempDir::new().expect("TempDir must be created");
    let db_path = tmp.path().join("test.sqlite");

    // DB init
    sgr_cmd()
        .env("SQLITE_GRAPHRAG_DB_PATH", &db_path)
        .env("SQLITE_GRAPHRAG_CACHE_DIR", tmp.path())
        .args(["--skip-memory-guard", "init"])
        .assert()
        .success();

    // Insert memory with accented text
    sgr_cmd()
        .env("SQLITE_GRAPHRAG_DB_PATH", &db_path)
        .env("SQLITE_GRAPHRAG_CACHE_DIR", tmp.path())
        .env("SQLITE_GRAPHRAG_NAMESPACE", "global")
        .args([
            "--skip-memory-guard",
            "remember",
            "--name",
            "nota-cafe",
            "--type",
            "user",
            "--description",
            "note about café",
            "--body",
            "Brazilian café is famous worldwide for its quality",
        ])
        .assert()
        .success();

    // Unaccented search must find the accented memory (remove_diacritics=1)
    let conn = conn_ro(&db_path);
    let count: i64 = conn
        .query_row(
            "SELECT COUNT(*) FROM fts_memories WHERE fts_memories MATCH 'cafe'",
            [],
            |row| row.get(0),
        )
        .expect("FTS5 query must work");

    assert!(
        count >= 1,
        "FTS5 with remove_diacritics must match 'café' when searching 'cafe', count={count}"
    );
}

// ---------------------------------------------------------------------------
// Test 9 — main tables exist after init
// ---------------------------------------------------------------------------
// Verifies all 7 regular tables plus virtual vec/fts tables created by migrations.

#[test]
#[serial]
fn all_main_tables_exist_after_init() {
    let (_tmp, db_path) = init_isolated_db();
    let conn = conn_ro(&db_path);

    let tables = [
        "schema_meta",
        "memories",
        "memory_versions",
        "memory_chunks",
        "entities",
        "relationships",
        "memory_entities",
        "memory_relationships",
        "fts_memories",
    ];

    for name in tables {
        assert!(
            table_exists(&conn, name),
            "table '{name}' must exist after init"
        );
    }
}

// ---------------------------------------------------------------------------
// Test 10 — main indexes from V001 and V005 exist
// ---------------------------------------------------------------------------

#[test]
#[serial]
fn main_indexes_exist_after_init() {
    let (_tmp, db_path) = init_isolated_db();
    let conn = conn_ro(&db_path);

    let indexes = [
        "idx_memories_ns_type",
        "idx_memories_ns_live",
        "idx_memories_body_hash",
        "idx_entities_ns",
        "idx_me_entity",
        "idx_relationships_source",
        "idx_relationships_target",
        "idx_relationships_ns",
        "idx_relationships_ns_relation",
        "idx_entities_namespace_degree",
        "idx_memory_chunks_memory_id",
        "idx_memory_relationships_relationship_id",
    ];

    for name in indexes {
        assert!(
            index_exists(&conn, name),
            "index '{name}' must exist after init"
        );
    }
}

// ---------------------------------------------------------------------------
// Test 11 — schema_meta contains required keys after init
// ---------------------------------------------------------------------------

#[test]
#[serial]
fn schema_meta_required_keys_exist() {
    let (_tmp, db_path) = init_isolated_db();
    let conn = conn_ro(&db_path);

    let expected_keys = [
        "schema_version",
        "model",
        "dim",
        "created_at",
        "namespace_initial",
    ];

    for key in expected_keys {
        let count: i64 = conn
            .query_row(
                "SELECT COUNT(*) FROM schema_meta WHERE key = ?1",
                rusqlite::params![key],
                |row| row.get(0),
            )
            .expect("schema_meta query must work");

        assert!(count > 0, "schema_meta must contain key '{key}' after init");
    }
}

// ---------------------------------------------------------------------------
// Test 12 — schema_version in schema_meta matches V009 (9)
// ---------------------------------------------------------------------------

#[test]
#[serial]
fn schema_version_meta_equals_13() {
    let (_tmp, db_path) = init_isolated_db();
    let conn = conn_ro(&db_path);

    let version: String = conn
        .query_row(
            "SELECT value FROM schema_meta WHERE key = 'schema_version'",
            [],
            |row| row.get(0),
        )
        .expect("schema_version must exist in schema_meta");

    assert_eq!(
        version, "13",
        "schema_version in schema_meta must be '13' after V013"
    );
}

// ---------------------------------------------------------------------------
// Test 13 — V009 e2e: full lifecycle for the new `document` memory type
// ---------------------------------------------------------------------------
// V009 expanded the `memories.type` CHECK constraint to accept `document`
// and `note` in addition to the seven pre-existing types. This test validates
// the full path: remember -> list (filtered by type) -> recall.

#[test]
#[serial]
fn v009_document_type_lifecycle_e2e() {
    let tmp = TempDir::new().expect("TempDir must be created");
    let db_path = tmp.path().join("test.sqlite");

    // Init applies V001..V009 in a fresh DB.
    sgr_cmd()
        .env("SQLITE_GRAPHRAG_DB_PATH", &db_path)
        .env("SQLITE_GRAPHRAG_CACHE_DIR", tmp.path())
        .args(["--skip-memory-guard", "init"])
        .assert()
        .success();

    // Insert a memory with the new type=document accepted by V009.
    let output = sgr_cmd()
        .env("SQLITE_GRAPHRAG_DB_PATH", &db_path)
        .env("SQLITE_GRAPHRAG_CACHE_DIR", tmp.path())
        .env("SQLITE_GRAPHRAG_NAMESPACE", "global")
        .args([
            "--skip-memory-guard",
            "remember",
            "--name",
            "doc-test",
            "--type",
            "document",
            "--description",
            "test doc",
            "--body",
            "Sample document body for e2e test",
            "--skip-extraction",
        ])
        .output()
        .expect("remember must run");
    assert!(
        output.status.success(),
        "remember failed: status={:?} stderr={}",
        output.status,
        String::from_utf8_lossy(&output.stderr)
    );

    // List filtered by type=document must return the inserted record.
    let output = sgr_cmd()
        .env("SQLITE_GRAPHRAG_DB_PATH", &db_path)
        .env("SQLITE_GRAPHRAG_CACHE_DIR", tmp.path())
        .args([
            "--skip-memory-guard",
            "list",
            "--type",
            "document",
            "--json",
        ])
        .output()
        .expect("list must run");
    assert!(
        output.status.success(),
        "list failed: stderr={}",
        String::from_utf8_lossy(&output.stderr)
    );
    let json: serde_json::Value =
        serde_json::from_slice(&output.stdout).expect("list output must be valid JSON");
    let items = json["items"]
        .as_array()
        .expect("list response must contain `items` array");
    assert_eq!(items.len(), 1, "expected exactly 1 document, got {items:?}");
    assert_eq!(items[0]["type"], "document");

    // Recall via FTS5/vector must surface the freshly inserted document.
    let output = sgr_cmd()
        .env("SQLITE_GRAPHRAG_DB_PATH", &db_path)
        .env("SQLITE_GRAPHRAG_CACHE_DIR", tmp.path())
        .args(["--skip-memory-guard", "recall", "Sample", "--json"])
        .output()
        .expect("recall must run");
    assert!(
        output.status.success(),
        "recall failed: stderr={}",
        String::from_utf8_lossy(&output.stderr)
    );
    let json: serde_json::Value =
        serde_json::from_slice(&output.stdout).expect("recall output must be valid JSON");
    let results = json["results"]
        .as_array()
        .expect("recall response must contain `results` array");
    assert!(
        !results.is_empty(),
        "recall must return at least one match for 'Sample', got: {results:?}"
    );
}

// ---------------------------------------------------------------------------
// Test 14 — V009 e2e: full lifecycle for the new `note` memory type
// ---------------------------------------------------------------------------

#[test]
#[serial]
fn v009_note_type_lifecycle_e2e() {
    let tmp = TempDir::new().expect("TempDir must be created");
    let db_path = tmp.path().join("test.sqlite");

    sgr_cmd()
        .env("SQLITE_GRAPHRAG_DB_PATH", &db_path)
        .env("SQLITE_GRAPHRAG_CACHE_DIR", tmp.path())
        .args(["--skip-memory-guard", "init"])
        .assert()
        .success();

    let output = sgr_cmd()
        .env("SQLITE_GRAPHRAG_DB_PATH", &db_path)
        .env("SQLITE_GRAPHRAG_CACHE_DIR", tmp.path())
        .env("SQLITE_GRAPHRAG_NAMESPACE", "global")
        .args([
            "--skip-memory-guard",
            "remember",
            "--name",
            "note-test",
            "--type",
            "note",
            "--description",
            "test note",
            "--body",
            "Quick scratch note for e2e validation",
            "--skip-extraction",
        ])
        .output()
        .expect("remember must run");
    assert!(
        output.status.success(),
        "remember failed: status={:?} stderr={}",
        output.status,
        String::from_utf8_lossy(&output.stderr)
    );

    let output = sgr_cmd()
        .env("SQLITE_GRAPHRAG_DB_PATH", &db_path)
        .env("SQLITE_GRAPHRAG_CACHE_DIR", tmp.path())
        .args(["--skip-memory-guard", "list", "--type", "note", "--json"])
        .output()
        .expect("list must run");
    assert!(
        output.status.success(),
        "list failed: stderr={}",
        String::from_utf8_lossy(&output.stderr)
    );
    let json: serde_json::Value =
        serde_json::from_slice(&output.stdout).expect("list output must be valid JSON");
    let items = json["items"]
        .as_array()
        .expect("list response must contain `items` array");
    assert_eq!(items.len(), 1, "expected exactly 1 note, got {items:?}");
    assert_eq!(items[0]["type"], "note");

    let output = sgr_cmd()
        .env("SQLITE_GRAPHRAG_DB_PATH", &db_path)
        .env("SQLITE_GRAPHRAG_CACHE_DIR", tmp.path())
        .args(["--skip-memory-guard", "recall", "scratch", "--json"])
        .output()
        .expect("recall must run");
    assert!(
        output.status.success(),
        "recall failed: stderr={}",
        String::from_utf8_lossy(&output.stderr)
    );
    let json: serde_json::Value =
        serde_json::from_slice(&output.stdout).expect("recall output must be valid JSON");
    let results = json["results"]
        .as_array()
        .expect("recall response must contain `results` array");
    assert!(
        !results.is_empty(),
        "recall must return at least one match for 'scratch', got: {results:?}"
    );
}

// ---------------------------------------------------------------------------
// Test 15 — V009: invalid memory type must be rejected by clap value_enum
// ---------------------------------------------------------------------------
// `--type` is bound to `MemoryType` via `value_enum`, so clap rejects unknown
// variants before reaching the SQLite CHECK constraint. This guards against
// a future regression where the enum drifts away from the migration's CHECK.

#[test]
#[serial]
fn v009_invalid_type_rejected() {
    let tmp = TempDir::new().expect("TempDir must be created");
    let db_path = tmp.path().join("test.sqlite");

    sgr_cmd()
        .env("SQLITE_GRAPHRAG_DB_PATH", &db_path)
        .env("SQLITE_GRAPHRAG_CACHE_DIR", tmp.path())
        .args(["--skip-memory-guard", "init"])
        .assert()
        .success();

    let output = sgr_cmd()
        .env("SQLITE_GRAPHRAG_DB_PATH", &db_path)
        .env("SQLITE_GRAPHRAG_CACHE_DIR", tmp.path())
        .env("SQLITE_GRAPHRAG_NAMESPACE", "global")
        .args([
            "--skip-memory-guard",
            "remember",
            "--name",
            "x",
            "--type",
            "invalid_type_xyz",
            "--description",
            "t",
            "--body",
            "t",
        ])
        .output()
        .expect("remember must run");

    assert!(
        !output.status.success(),
        "remember must reject invalid type 'invalid_type_xyz'"
    );
    let stderr = String::from_utf8_lossy(&output.stderr).to_lowercase();
    assert!(
        stderr.contains("invalid") || stderr.contains("type") || stderr.contains("possible values"),
        "stderr should mention type rejection, got: {stderr}"
    );
}

// ---------------------------------------------------------------------------
// v1.0.76 — migrate --rehash and --to-llm-only integration tests
// ---------------------------------------------------------------------------
// These tests exercise the CLI subcommands end-to-end through `assert_cmd`.
// They cover three real-world flows:
//   1. --rehash on a healthy fresh DB is a no-op (status = ok_no_changes).
//   2. --rehash rewrites a corrupted V001 checksum and the next `migrate`
//      run no longer fails with "applied migration V1 is different than
//      filesystem one V1".
//   3. --to-llm-only on a fresh v1.0.76 DB reports no vec tables and a
//      successful schema_version 13 (V013 applied).
//   4. --to-llm-only refuses to run without the explicit --drop-vec-tables
//      safety guard (exit code 1, validation error).

#[test]
#[serial]
fn migrate_rehash_is_noop_on_healthy_db() {
    let (_tmp, db_path) = init_isolated_db();

    let output = sgr_cmd()
        .env("SQLITE_GRAPHRAG_DB_PATH", &db_path)
        .args(["--skip-memory-guard", "migrate", "--rehash"])
        .output()
        .expect("migrate --rehash must run");

    assert!(
        output.status.success(),
        "migrate --rehash must succeed on a healthy DB. stderr={}",
        String::from_utf8_lossy(&output.stderr)
    );
    let stdout = String::from_utf8_lossy(&output.stdout);
    let json: serde_json::Value = serde_json::from_str(&stdout).expect("stdout must be valid JSON");
    assert_eq!(
        json["status"], "ok_no_changes",
        "healthy DB must report ok_no_changes, got: {stdout}"
    );
    assert_eq!(json["rewritten"].as_array().unwrap().len(), 0);
    assert_eq!(json["inspected"], 13);
    assert_eq!(json["schema_version"], 13);
}

#[test]
#[serial]
fn migrate_rehash_fixes_corrupted_checksum() {
    let (_tmp, db_path) = init_isolated_db();

    // Corrupt the V001 checksum so the next `migrate` would fail.
    let conn = conn_ro(&db_path);
    conn.execute_batch(
        "UPDATE refinery_schema_history SET checksum = '999999999999' WHERE version = 1",
    )
    .expect("corrupt V001 checksum");
    drop(conn);

    // Sanity: a regular `migrate` should now fail.
    let bad = sgr_cmd()
        .env("SQLITE_GRAPHRAG_DB_PATH", &db_path)
        .args(["--skip-memory-guard", "migrate"])
        .output()
        .expect("migrate must run");
    assert!(
        !bad.status.success(),
        "migrate must fail on a corrupted checksum, got: {:?}",
        bad.status
    );

    // `migrate --rehash` should detect the mismatch, rewrite the row,
    // and exit 0 with status=ok_rewritten.
    let good = sgr_cmd()
        .env("SQLITE_GRAPHRAG_DB_PATH", &db_path)
        .args(["--skip-memory-guard", "migrate", "--rehash"])
        .output()
        .expect("migrate --rehash must run");
    assert!(
        good.status.success(),
        "migrate --rehash must succeed. stderr={}",
        String::from_utf8_lossy(&good.stderr)
    );
    let json: serde_json::Value = serde_json::from_slice(&good.stdout).expect("JSON");
    assert_eq!(json["status"], "ok_rewritten");
    assert_eq!(json["rewritten"].as_array().unwrap().len(), 1);
    assert_eq!(json["rewritten"][0]["version"], 1);
    assert_eq!(json["rewritten"][0]["name"], "init");
    assert_eq!(json["rewritten"][0]["old_checksum"], "999999999999");

    // And a subsequent plain `migrate` should now succeed.
    let after = sgr_cmd()
        .env("SQLITE_GRAPHRAG_DB_PATH", &db_path)
        .args(["--skip-memory-guard", "migrate"])
        .output()
        .expect("migrate must run");
    assert!(
        after.status.success(),
        "migrate must succeed after rehash. stderr={}",
        String::from_utf8_lossy(&after.stderr)
    );
}

#[test]
#[serial]
fn migrate_to_llm_only_reports_no_vec_tables_on_fresh_db() {
    let (_tmp, db_path) = init_isolated_db();

    let output = sgr_cmd()
        .env("SQLITE_GRAPHRAG_DB_PATH", &db_path)
        .args([
            "--skip-memory-guard",
            "migrate",
            "--to-llm-only",
            "--drop-vec-tables",
        ])
        .output()
        .expect("migrate --to-llm-only must run");

    assert!(
        output.status.success(),
        "migrate --to-llm-only must succeed on a fresh v1.0.76 DB. stderr={}",
        String::from_utf8_lossy(&output.stderr)
    );
    let json: serde_json::Value = serde_json::from_slice(&output.stdout).expect("JSON");
    assert_eq!(json["status"], "ok");
    assert_eq!(json["schema_version"], 13);
    assert_eq!(json["v013_applied"], true);
    assert_eq!(
        json["vec_tables_were_present"], false,
        "fresh v1.0.76 DBs must not have vec0 virtual tables"
    );
    assert_eq!(json["rehashed"].as_array().unwrap().len(), 0);
}

#[test]
#[serial]
fn migrate_to_llm_only_requires_drop_vec_tables_safety_guard() {
    let (_tmp, db_path) = init_isolated_db();

    let output = sgr_cmd()
        .env("SQLITE_GRAPHRAG_DB_PATH", &db_path)
        .args(["--skip-memory-guard", "migrate", "--to-llm-only"])
        .output()
        .expect("migrate --to-llm-only must run");

    assert!(
        !output.status.success(),
        "migrate --to-llm-only without --drop-vec-tables must refuse to run"
    );
    let stdout = String::from_utf8_lossy(&output.stdout);
    let json: serde_json::Value = serde_json::from_str(&stdout).expect("JSON");
    assert_eq!(json["code"], 1, "validation error code 1 expected");
    let msg = json["message"].as_str().unwrap_or("").to_string();
    assert!(
        msg.contains("--drop-vec-tables"),
        "error message must mention --drop-vec-tables, got: {msg}"
    );
}

#[test]
#[serial]
fn migrate_rehash_fixes_null_applied_on() {
    let (_tmp, db_path) = init_isolated_db();

    // NULL out applied_on for all rows to simulate the G40 bug.
    let conn = conn_ro(&db_path);
    conn.execute_batch("UPDATE refinery_schema_history SET applied_on = NULL")
        .expect("nullify applied_on");
    drop(conn);

    // migrate --rehash must succeed and fix the NULL rows.
    let output = sgr_cmd()
        .env("SQLITE_GRAPHRAG_DB_PATH", &db_path)
        .args(["--skip-memory-guard", "migrate", "--rehash"])
        .output()
        .expect("migrate --rehash must run");

    assert!(
        output.status.success(),
        "migrate --rehash must succeed on DB with NULL applied_on. stderr={}",
        String::from_utf8_lossy(&output.stderr)
    );
    let json: serde_json::Value =
        serde_json::from_slice(&output.stdout).expect("stdout must be valid JSON");
    assert!(
        json["null_rows_fixed"].as_u64().unwrap_or(0) > 0,
        "must report null_rows_fixed > 0, got: {}",
        json["null_rows_fixed"]
    );

    // A subsequent plain migrate must also succeed (runner reads applied_on).
    let after = sgr_cmd()
        .env("SQLITE_GRAPHRAG_DB_PATH", &db_path)
        .args(["--skip-memory-guard", "migrate"])
        .output()
        .expect("migrate must run");
    assert!(
        after.status.success(),
        "migrate must succeed after rehash fixed NULLs. stderr={}",
        String::from_utf8_lossy(&after.stderr)
    );

    // Verify zero NULL rows remain via rusqlite.
    let conn = conn_ro(&db_path);
    let null_count: i64 = conn
        .query_row(
            "SELECT COUNT(*) FROM refinery_schema_history WHERE applied_on IS NULL",
            [],
            |r| r.get(0),
        )
        .unwrap();
    assert_eq!(null_count, 0, "no NULL applied_on rows must remain");
}

#[test]
#[serial]
fn migrate_to_llm_only_fixes_null_applied_on() {
    let (_tmp, db_path) = init_isolated_db();

    let conn = conn_ro(&db_path);
    conn.execute_batch("UPDATE refinery_schema_history SET applied_on = NULL")
        .expect("nullify applied_on");
    drop(conn);

    let output = sgr_cmd()
        .env("SQLITE_GRAPHRAG_DB_PATH", &db_path)
        .args([
            "--skip-memory-guard",
            "migrate",
            "--to-llm-only",
            "--drop-vec-tables",
        ])
        .output()
        .expect("migrate --to-llm-only must run");

    assert!(
        output.status.success(),
        "migrate --to-llm-only must succeed with NULL applied_on. stderr={}",
        String::from_utf8_lossy(&output.stderr)
    );
    let json: serde_json::Value =
        serde_json::from_slice(&output.stdout).expect("stdout must be valid JSON");
    assert!(
        json["null_rows_fixed"].as_u64().unwrap_or(0) > 0,
        "must report null_rows_fixed > 0, got: {}",
        json["null_rows_fixed"]
    );
    assert_eq!(json["status"], "ok");
}