sqlite-graphrag 1.2.8

Persistent GraphRAG memory for Claude Code, Codex, Cursor, and 27 AI agents — one self-contained ~19 MiB Rust binary, zero daemon. Never re-explain your codebase again. Hybrid retrieval (FTS5 BM25 + cosine similarity + multi-hop graph traversal) surfaces the right memory in milliseconds. Embedding and entity enrichment run as parallel REST calls against your cloud LLM — no fragile headless subprocesses, no ONNX runtime, no model downloads. Soft-delete with full version history, transactional atomic writes, BLAKE3-tracked mutations. OAuth-only: raw 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
//! PRD compliance: namespace isolation, soft-delete, FTS integrity, merge, limits and optimistic locking (clauses 1-12).
//!
//! Part of the PRD-compliance suite split by GAP-SG-208. Covers the `MUST`/`DEVE`
//! clauses of the sqlite-graphrag PRD. The shared harness lives in
//! `tests/prd_support/`.

#[path = "prd_support/mod.rs"]
mod support;

use rusqlite::Connection;
use serial_test::serial;
use support::{cmd_base, db_path, init_db, remember_ok, sgr_cmd};
use tempfile::TempDir;
// ---------------------------------------------------------------------------
// 1 — namespace with __ prefix rejected with exit 1
//     (the check is done in remember.rs at the name level; there is no __ guard at namespace level)
// ---------------------------------------------------------------------------

#[test]
fn prd_name_double_underscore_rejected() {
    let tmp = TempDir::new().unwrap();
    init_db(&tmp);

    cmd_base(&tmp)
        .args([
            "remember",
            "--name",
            "___",
            "--type",
            "user",
            "--description",
            "must fail because name is empty after normalization",
            "--body",
            "body content",
        ])
        .assert()
        .failure()
        .code(1);
}

// ---------------------------------------------------------------------------
// 2 — cross-namespace link rejected (exit 4: entity does not exist in namespace)
// ---------------------------------------------------------------------------

#[test]
fn prd_cross_namespace_link_rejected() {
    let tmp = TempDir::new().unwrap();
    init_db(&tmp);

    // Create an entity in ns-alpha
    remember_ok(&tmp, "entidade-alpha", "corpo alpha");

    // Try to link between entities from distinct namespaces (to: ns-beta does not exist)
    cmd_base(&tmp)
        .args([
            "link",
            "--from",
            "entidade-alpha",
            "--to",
            "entidade-inexistente-beta",
            "--relation",
            "related",
            "--namespace",
            "global",
        ])
        .assert()
        .failure()
        .code(4);
}

// ---------------------------------------------------------------------------
// 3 — soft-delete: forgotten memories do not appear in recall
// ---------------------------------------------------------------------------

#[test]
fn prd_soft_delete_recall_does_not_return_forgotten() {
    let tmp = TempDir::new().unwrap();
    init_db(&tmp);

    remember_ok(&tmp, "memoria-apagavel", "conteudo apagavel importante");

    // Delete (soft-delete)
    cmd_base(&tmp)
        .args([
            "forget",
            "--name",
            "memoria-apagavel",
            "--namespace",
            "global",
        ])
        .assert()
        .success();

    // Verify that deleted_at was filled (does not return in SELECT ... WHERE deleted_at IS NULL)
    let conn = Connection::open(db_path(&tmp)).unwrap();
    let count: i64 = conn
        .query_row(
            "SELECT COUNT(*) FROM memories WHERE name='memoria-apagavel' AND deleted_at IS NULL",
            [],
            |r| r.get(0),
        )
        .unwrap();
    assert_eq!(
        count, 0,
        "memória esquecida não deve aparecer sem deleted_at"
    );

    let deleted_count: i64 = conn
        .query_row(
            "SELECT COUNT(*) FROM memories WHERE name='memoria-apagavel' AND deleted_at IS NOT NULL",
            [],
            |r| r.get(0),
        )
        .unwrap();
    assert_eq!(deleted_count, 1, "soft-delete deve preencher deleted_at");
}

// ---------------------------------------------------------------------------
// 4 — trg_fts_ad idempotent: double-delete does not corrupt fts_memories
// ---------------------------------------------------------------------------

#[test]
fn prd_trg_fts_ad_idempotent_double_delete() {
    let tmp = TempDir::new().unwrap();
    init_db(&tmp);

    remember_ok(
        &tmp,
        "memoria-dupla",
        "conteudo para double delete fts test",
    );

    let conn = Connection::open(db_path(&tmp)).unwrap();

    // Obtain the memory id
    let memory_id: i64 = conn
        .query_row(
            "SELECT id FROM memories WHERE name='memoria-dupla'",
            [],
            |r| r.get(0),
        )
        .unwrap();

    // First deletion via UPDATE (manual soft-delete directly in the database)
    conn.execute(
        "UPDATE memories SET deleted_at=strftime('%s','now') WHERE id=?1",
        [memory_id],
    )
    .unwrap();

    // Second "deletion" — the trg_fts_ad trigger already removed it from FTS; should not error
    conn.execute("DELETE FROM fts_memories WHERE rowid=?1", [memory_id])
        .unwrap_or(0); // idempotent: if it does not exist, ignore

    // Verify FTS integrity after the double operation
    let result =
        conn.execute_batch("INSERT INTO fts_memories(fts_memories) VALUES('integrity-check')");
    assert!(
        result.is_ok(),
        "fts_memories deve passar integrity-check após double-delete"
    );
}

// ---------------------------------------------------------------------------
// 5 — duplicate remember with --force-merge returns merged_into_memory_id
// ---------------------------------------------------------------------------

#[test]
fn prd_remember_duplicate_returns_merged_into_memory_id() {
    let tmp = TempDir::new().unwrap();
    init_db(&tmp);

    remember_ok(&tmp, "mem-merge-alvo", "corpo original da memoria merge");

    // Second call with the same name + --force-merge
    let output = cmd_base(&tmp)
        .args([
            "remember",
            "--name",
            "mem-merge-alvo",
            "--type",
            "user",
            "--description",
            "desc atualizada",
            "--body",
            "corpo novo do merge",
            "--namespace",
            "global",
            "--force-merge",
            "--skip-extraction",
        ])
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();

    let json: serde_json::Value = serde_json::from_slice(&output).unwrap();
    // merged_into_memory_id must be present (may be null or an integer)
    assert!(
        json.get("merged_into_memory_id").is_some(),
        "remember com --force-merge deve incluir campo merged_into_memory_id"
    );
}

// ---------------------------------------------------------------------------
// 6 — remember JSON contains entities_persisted and relationships_persisted
// ---------------------------------------------------------------------------

#[test]
fn prd_remember_json_contains_entities_and_relationships_persisted() {
    let tmp = TempDir::new().unwrap();
    init_db(&tmp);

    let output = cmd_base(&tmp)
        .args([
            "remember",
            "--name",
            "mem-fields-check",
            "--type",
            "user",
            "--description",
            "verificar campos de saida",
            "--body",
            "corpo para checar campos json",
            "--namespace",
            "global",
            "--skip-extraction",
        ])
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();

    let json: serde_json::Value = serde_json::from_slice(&output).unwrap();
    assert!(
        json.get("entities_persisted").is_some(),
        "remember deve emitir entities_persisted"
    );
    assert!(
        json.get("relationships_persisted").is_some(),
        "remember deve emitir relationships_persisted"
    );
}

// ---------------------------------------------------------------------------
// 7 — FTS5 unicode61 remove_diacritics: searching "nao" matches "não"
// ---------------------------------------------------------------------------

#[test]
fn prd_fts5_unicode61_remove_diacritics() {
    let tmp = TempDir::new().unwrap();
    init_db(&tmp);

    let conn = Connection::open(db_path(&tmp)).unwrap();

    // Verify that fts_memories uses tokenize with unicode61 remove_diacritics
    let tokenize: String = conn
        .query_row(
            "SELECT tokenize FROM pragma_table_info('fts_memories') LIMIT 1",
            [],
            |r| r.get(0),
        )
        .unwrap_or_else(|_| {
            // Fallback: look it up via sqlite_master
            conn.query_row(
                "SELECT sql FROM sqlite_master WHERE name='fts_memories'",
                [],
                |r| r.get::<_, String>(0),
            )
            .unwrap_or_default()
        });

    assert!(
        tokenize.contains("unicode61") || tokenize.contains("remove_diacritics"),
        "fts_memories deve usar tokenize='unicode61 remove_diacritics 1', encontrado: {tokenize}"
    );
}

// ---------------------------------------------------------------------------
// 8 — pure-Rust cosine similarity produces the expected distance range
// ---------------------------------------------------------------------------
// v1.0.76 dropped vec_memories and the distance_metric=cosine DDL. The cosine
// invariant is now guaranteed by src/similarity.rs::cosine_similarity plus
// the BLOB-backed memory_embeddings table. This test pins the contract:
//   - orthogonal unit vectors yield distance > 0.5
//   - identical unit vectors yield distance ~ 0.0
//   - the result lies in (0.5, 2.0] for near-orthogonal vectors
// The test does NOT shell out to the binary; it runs a tiny pure-Rust snippet
// against the live library, so it stays fast and hermetic.

#[test]
fn prd_cosine_similarity_distance_invariant() {
    use sqlite_graphrag::similarity::{cosine_similarity, similarity_to_distance};

    let a: Vec<f32> = (0..384).map(|i| (i as f32).sin()).collect();
    let b: Vec<f32> = (0..384).map(|i| (i as f32).cos()).collect();
    let c: Vec<f32> = a.clone();

    let sim_ab = cosine_similarity(&a, &b);
    let sim_ac = cosine_similarity(&a, &c);
    let d_ab = similarity_to_distance(sim_ab);
    let d_ac = similarity_to_distance(sim_ac);

    assert!(
        d_ab > 0.5 && d_ab <= 2.0,
        "distance must lie in (0.5, 2.0] for near-orthogonal vectors, got {d_ab}"
    );
    assert!(
        d_ac.abs() < 1e-6,
        "identical vectors must yield distance ~ 0.0, got {d_ac}"
    );
}

// ---------------------------------------------------------------------------
// 9 — edit with a stale --expected-updated-at returns exit 3 (Conflict)
// ---------------------------------------------------------------------------

#[test]
fn prd_edit_expected_updated_at_stale_returns_exit_3() {
    let tmp = TempDir::new().unwrap();
    init_db(&tmp);

    remember_ok(&tmp, "mem-edit-lock", "corpo para edit lock test");

    // Use stale timestamp (0) to force a conflict
    cmd_base(&tmp)
        .args([
            "edit",
            "--name",
            "mem-edit-lock",
            "--namespace",
            "global",
            "--body",
            "novo corpo conflito",
            "--expected-updated-at",
            "0",
        ])
        .assert()
        .failure()
        .code(3);
}

// ---------------------------------------------------------------------------
// 10 — 5 simultaneous instances: the 5th returns exit 75
// ---------------------------------------------------------------------------

#[test]
#[serial]
fn prd_five_instances_fifth_returns_exit_75() {
    use fs4::fs_std::FileExt;
    use std::fs::OpenOptions;

    let tmp = TempDir::new().unwrap();

    // GAP-SG-94 / G-T-XDG-04: `lock::slot_path` resolves through
    // `paths::cache_dir()`, whose precedence is `--cache-dir` > XDG `cache.dir`
    // > the OS cache directory. Setting only `XDG_CACHE_HOME` sends the binary
    // to `<XDG_CACHE_HOME>/sqlite-graphrag/`, one level BELOW where this test
    // planted its lock files, so every slot looked free and the 5th invocation
    // exited 0. Passing `--cache-dir` pins the resolver to the exact directory
    // the locks live in. The question under test — "does the 5th concurrent
    // instance get exit 75 when all 4 slots are taken?" — is unchanged.
    let cache_dir = tmp.path().join("cache");
    std::fs::create_dir_all(&cache_dir).unwrap();

    // Occupy the 4 default slots directly via fs4
    let handles: Vec<std::fs::File> = (1..=4)
        .map(|slot| {
            let path = cache_dir.join(format!("cli-slot-{slot}.lock"));
            let file = OpenOptions::new()
                .read(true)
                .write(true)
                .create(true)
                .truncate(false)
                .open(&path)
                .unwrap();
            file.try_lock_exclusive().unwrap();
            file
        })
        .collect();

    // 5th invocation with --wait-lock 0 must return exit 75
    sgr_cmd()
        .env("XDG_CACHE_HOME", tmp.path().join("xdg_cache"))
        .arg("--cache-dir")
        .arg(&cache_dir)
        .args([
            "--skip-memory-guard",
            "--max-concurrency",
            "4",
            "--wait-lock",
            "0",
            "namespace-detect",
        ])
        .assert()
        .failure()
        .code(75);

    drop(handles);
}

// ---------------------------------------------------------------------------
// 11 — MAX_MEMORY_BODY_LEN=512000: a body above the limit returns exit 6
// ---------------------------------------------------------------------------

#[test]
fn prd_max_body_len_exceeded_returns_exit_6() {
    let tmp = TempDir::new().unwrap();
    init_db(&tmp);

    let corpo_gigante = "x".repeat(512_001);
    let body_path = tmp.path().join("body-grande.txt");
    std::fs::write(&body_path, corpo_gigante).unwrap();

    cmd_base(&tmp)
        .args([
            "remember",
            "--name",
            "mem-body-limit",
            "--type",
            "user",
            "--description",
            "limite de corpo",
            "--body-file",
            body_path.to_str().unwrap(),
        ])
        .assert()
        .failure()
        .code(6);
}

// ---------------------------------------------------------------------------
// 12 — --namespace flag sets the memory namespace (product env is not a channel)
// ---------------------------------------------------------------------------

#[test]
#[serial]
fn prd_namespace_flag_sets_memory_namespace() {
    // GAP-SG-101 / G-T-XDG-04: SQLITE_GRAPHRAG_NAMESPACE is not read.
    // The real channel is --namespace (or config set namespace.default).
    let tmp = TempDir::new().unwrap();
    init_db(&tmp);

    cmd_base(&tmp)
        .args([
            "remember",
            "--name",
            "mem-via-flag-ns",
            "--type",
            "user",
            "--description",
            "namespace via flag",
            "--namespace",
            "ns-from-flag",
            "--body",
            "corpo namespace flag",
            "--skip-extraction",
        ])
        .assert()
        .success();

    // The literal was unquoted, so SQLite parsed `name=mem-via-flag-ns` as a
    // comparison between COLUMNS and failed with "no such column: mem", which
    // `.unwrap()` turned into a panic before the namespace was ever read. The
    // question under test — "does --namespace decide the persisted namespace?"
    // — is unchanged; the row is now selected through a bound parameter.
    let conn = Connection::open(db_path(&tmp)).unwrap();
    let ns: String = conn
        .query_row(
            "SELECT namespace FROM memories WHERE name = ?1",
            rusqlite::params!["mem-via-flag-ns"],
            |r| r.get(0),
        )
        .unwrap();
    assert_eq!(ns, "ns-from-flag", "namespace must match --namespace flag");
}