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
//! Database-path resolution, `init`, `health`, `stats` and removed-surface guards.
//!
//! Split out of the 2 485-line `tests/integration.rs` in v1.2.5. Each theme is
//! its own binary, so a compile error in one no longer takes the other two down
//! with it, and the shared helpers moved to `tests/common/` instead of being
//! copied three times.

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

#[allow(unused_imports)]
use assert_cmd::Command;
#[allow(unused_imports)]
use common::{
    cmd, home_isolated_cmd, init_db, isolated_cmd_in, seed_memory_with_entities, sgr_cmd,
};
#[allow(unused_imports)]
use tempfile::TempDir;

#[test]
fn cli_home_env_creates_db_in_target_dir() {
    // GAP-SG-101 / G-T-XDG-04 (v1.2.0): with no `--db` and no XDG `db.path`,
    // `init` resolves through the XDG DATA directory, which `directories`
    // derives from HOME as `$HOME/.local/share/sqlite-graphrag`. The pre-v1.2.0
    // contract put the file at the HOME ROOT; that expectation had been failing
    // since the release. The question under test is unchanged — "does HOME, and
    // not the working directory, decide where the default database lands?" —
    // only the expected location moved one directory deeper.
    let home_dir = TempDir::new().expect("home tempdir");
    let cwd_dir = TempDir::new().expect("cwd tempdir");
    let db_in_home = home_dir
        .path()
        .join(".local")
        .join("share")
        .join("sqlite-graphrag")
        .join("graphrag.sqlite");
    let db_at_home_root = home_dir.path().join("graphrag.sqlite");
    let db_in_cwd = cwd_dir.path().join("graphrag.sqlite");

    home_isolated_cmd(cwd_dir.path())
        .env("HOME", home_dir.path())
        .arg("init")
        .assert()
        .success();

    assert!(
        db_in_home.exists(),
        "init com HOME deve criar o banco no diretório de dados XDG derivado de HOME"
    );
    assert!(
        !db_at_home_root.exists(),
        "init NÃO deve criar banco na raiz do HOME (G-T-XDG-04)"
    );
    assert!(
        !db_in_cwd.exists(),
        "init com HOME NÃO deve criar banco no current_dir"
    );
}

#[test]
fn cli_home_traversal_rejected() {
    let cwd_dir = TempDir::new().expect("cwd tempdir");

    home_isolated_cmd(cwd_dir.path())
        .env("HOME", "/tmp/../etc")
        .arg("init")
        .assert()
        .failure();
}

#[test]
fn cli_product_env_db_path_is_ignored_flag_wins() {
    // GAP-SG-101 / G-T-XDG-04: SQLITE_GRAPHRAG_DB_PATH is not read.
    // --db after the subcommand is the only override; product env must not
    // create a database at the env path.
    let home_dir = TempDir::new().expect("home tempdir");
    let env_dir = TempDir::new().expect("env tempdir");
    let flag_dir = TempDir::new().expect("flag tempdir");
    let cwd_dir = TempDir::new().expect("cwd tempdir");
    let db_from_env = env_dir.path().join("from-env.sqlite");
    let db_flag = flag_dir.path().join("via-flag.sqlite");
    let db_in_home = home_dir.path().join("graphrag.sqlite");

    home_isolated_cmd(cwd_dir.path())
        .env("HOME", home_dir.path())
        // Intentionally set: must be ignored (negative assertion).
        .env("SQLITE_GRAPHRAG_DB_PATH", &db_from_env)
        .args(["init", "--db"])
        .arg(&db_flag)
        .assert()
        .success();

    assert!(db_flag.exists(), "--db must create the explicit database");
    assert!(
        !db_from_env.exists(),
        "SQLITE_GRAPHRAG_DB_PATH must be ignored (G-T-XDG-04)"
    );
    assert!(
        !db_in_home.exists(),
        "HOME must not be used when --db is present"
    );
}

#[test]
fn cli_flag_db_overrides_home_env() {
    let home_dir = TempDir::new().expect("home tempdir");
    let flag_dir = TempDir::new().expect("flag tempdir");
    let cwd_dir = TempDir::new().expect("cwd tempdir");
    let db_flag = flag_dir.path().join("via-flag.sqlite");
    let db_in_home = home_dir.path().join("graphrag.sqlite");

    home_isolated_cmd(cwd_dir.path())
        .env("HOME", home_dir.path())
        .args(["init", "--db", db_flag.to_str().unwrap()])
        .assert()
        .success();

    assert!(db_flag.exists(), "flag --db deve vencer HOME");
    assert!(
        !db_in_home.exists(),
        "HOME não deve ser usado quando --db está presente"
    );
}

// ---------------------------------------------------------------------------
// init
// ---------------------------------------------------------------------------

#[test]
fn test_init_creates_sqlite_file() {
    let tmp = TempDir::new().unwrap();
    let db_path = tmp.path().join("test.sqlite");
    assert!(!db_path.exists(), "banco nao deve existir antes do init");

    cmd(&tmp).arg("init").assert().success();

    assert!(db_path.exists(), "banco deve existir apos o init");
}

#[test]
fn test_init_creates_local_db_in_invocation_directory() {
    let dir_a = TempDir::new().unwrap();
    let dir_b = TempDir::new().unwrap();
    let db_a = dir_a.path().join("graphrag.sqlite");
    let db_b = dir_b.path().join("graphrag.sqlite");

    assert!(
        !db_a.exists(),
        "banco local nao deve existir antes do init em a"
    );
    assert!(
        !db_b.exists(),
        "banco local nao deve existir antes do init em b"
    );

    isolated_cmd_in(dir_a.path()).arg("init").assert().success();
    isolated_cmd_in(dir_b.path()).arg("init").assert().success();

    assert!(db_a.exists(), "init deve criar graphrag.sqlite em a");
    assert!(db_b.exists(), "init deve criar graphrag.sqlite em b");
}

#[test]
fn test_crud_uses_graphrag_sqlite_in_invocation_directory() {
    let dir = TempDir::new().unwrap();
    let db = dir.path().join("graphrag.sqlite");

    assert!(
        !db.exists(),
        "banco local nao deve existir antes do init no diretorio da invocacao"
    );

    isolated_cmd_in(dir.path()).arg("init").assert().success();

    assert!(
        db.exists(),
        "init deve criar graphrag.sqlite no diretorio da invocacao"
    );

    isolated_cmd_in(dir.path())
        .args([
            "remember",
            "--name",
            "memory-cwd",
            "--type",
            "user",
            "--description",
            "crud cwd",
            "--body",
            "conteudo salvo no banco local da pasta atual",
        ])
        .assert()
        .success();

    let read_output = isolated_cmd_in(dir.path())
        .args(["read", "--name", "memory-cwd"])
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();
    let read_json: serde_json::Value = serde_json::from_slice(&read_output).unwrap();
    assert_eq!(read_json["name"], "memory-cwd");
    assert_eq!(read_json["description"], "crud cwd");

    let list_output = isolated_cmd_in(dir.path())
        .arg("list")
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();
    let list_json: serde_json::Value = serde_json::from_slice(&list_output).unwrap();
    let items = list_json["items"].as_array().unwrap();
    assert!(
        items.iter().any(|item| item["name"] == "memory-cwd"),
        "list deve ler a memoria persistida em ./graphrag.sqlite"
    );

    isolated_cmd_in(dir.path())
        .args(["forget", "--name", "memory-cwd"])
        .assert()
        .success();

    let purge_output = isolated_cmd_in(dir.path())
        .args(["purge", "--retention-days", "0", "--yes"])
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();
    let purge_json: serde_json::Value = serde_json::from_slice(&purge_output).unwrap();
    assert_eq!(purge_json["purged_count"], 1);
}

#[test]
fn test_remember_without_init_creates_migrated_local_db() {
    let dir = TempDir::new().unwrap();
    let db = dir.path().join("graphrag.sqlite");

    assert!(
        !db.exists(),
        "banco local nao deve existir antes do remember"
    );

    isolated_cmd_in(dir.path())
        .args([
            "remember",
            "--name",
            "memory-without-init",
            "--type",
            "user",
            "--description",
            "create sem init",
            "--body",
            "conteudo salvo sem init explicito",
            "--skip-extraction",
            "--json",
        ])
        .assert()
        .success();

    assert!(
        db.exists(),
        "remember deve criar graphrag.sqlite migrado no cwd"
    );

    let read_output = isolated_cmd_in(dir.path())
        .args(["read", "--name", "memory-without-init", "--json"])
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();
    let read_json: serde_json::Value = serde_json::from_slice(&read_output).unwrap();
    assert_eq!(read_json["name"], "memory-without-init");
    assert_eq!(read_json["body"], "conteudo salvo sem init explicito");
}

#[test]
fn test_init_returns_json_with_status_ok() {
    let tmp = TempDir::new().unwrap();
    let output = cmd(&tmp)
        .arg("init")
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();

    let json: serde_json::Value = serde_json::from_slice(&output).unwrap();
    assert_eq!(json["status"], "ok");
    // Until v1.2.4 this asserted the CRATE VERSION, freezing the divergence
    // `init.schema.json` documented against: the schema described `model` as
    // the embedding model name while the handler filled it with
    // `SQLITE_GRAPHRAG_VERSION`. The version still reaches the database via
    // `schema_meta.sqlite-graphrag_version`; the envelope field now answers
    // what it is named after — the model this invocation would embed with,
    // resolved as `--embedding-model` > XDG `embedding.model` > `"none"`.
    assert_eq!(json["model"], common::openrouter_mock::STUB_MODEL);
    assert_ne!(json["model"], env!("CARGO_PKG_VERSION"));
    assert!(json["dim"].as_u64().unwrap() > 0);
}

// ---------------------------------------------------------------------------
// health
// ---------------------------------------------------------------------------

#[test]
fn test_health_does_not_auto_init_when_missing() {
    let tmp = TempDir::new().unwrap();
    // v1.0.x contract: `health` no longer auto-creates the database; with a
    // missing DB it guards with exit 4 and tells the operator to run `init`
    // first (it must never silently create the file).
    let assert = cmd(&tmp).arg("health").assert().failure().code(4);
    let stderr = String::from_utf8_lossy(&assert.get_output().stderr).to_string();
    assert!(
        stderr.contains("does not auto-create"),
        "expected the no-auto-create guard, got stderr: {stderr}"
    );
}

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

    let output = cmd(&tmp)
        .arg("health")
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();

    let json: serde_json::Value = serde_json::from_slice(&output).unwrap();
    assert_eq!(json["status"], "ok");
    assert_eq!(json["integrity"], "ok");
}

// ---------------------------------------------------------------------------
// daemon — REMOVED in v1.0.79 (the CLI is 100% one-shot)
// ---------------------------------------------------------------------------

/// v1.0.79 regression guard: the `daemon` subcommand was fully removed
/// (ADR-0021; code deleted in v1.0.79). Invoking it must fail with the
/// clap unknown-subcommand error (exit 2), never start any process.
#[test]
fn test_daemon_subcommand_is_removed() {
    let tmp = TempDir::new().unwrap();

    sgr_cmd()
        .current_dir(tmp.path())
        .env("XDG_CACHE_HOME", tmp.path().join("cache"))
        .args(["daemon", "--ping", "--json"])
        .assert()
        .failure()
        .code(2);
}

/// v1.0.79 regression guard: the top-level help must not advertise the
/// removed `daemon` subcommand.
#[test]
fn test_help_does_not_list_daemon() {
    let tmp = TempDir::new().unwrap();

    let output = sgr_cmd()
        .current_dir(tmp.path())
        .env("XDG_CACHE_HOME", tmp.path().join("cache"))
        .args(["--help"])
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();

    let help = String::from_utf8(output).unwrap();
    assert!(
        !help.contains("daemon"),
        "top-level help must not list the removed daemon subcommand"
    );
}

// ---------------------------------------------------------------------------
// namespace-detect
// ---------------------------------------------------------------------------

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

    let output = isolated_cmd_in(tmp.path())
        .arg("namespace-detect")
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();

    let json: serde_json::Value = serde_json::from_slice(&output).unwrap();
    assert_eq!(json["namespace"], "global");
    assert_eq!(json["source"], "default");
}

// ---------------------------------------------------------------------------
// sync-safe-copy
// ---------------------------------------------------------------------------

#[test]
fn test_sync_safe_copy_creates_consistent_snapshot() {
    let tmp = TempDir::new().unwrap();
    init_db(&tmp);
    let dest = tmp.path().join("snapshot.sqlite");

    let output = cmd(&tmp)
        .args(["sync-safe-copy", "--dest", dest.to_str().unwrap()])
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();

    let json: serde_json::Value = serde_json::from_slice(&output).unwrap();
    assert_eq!(json["status"], "ok");
    assert!(dest.exists());
    assert!(std::fs::metadata(dest).unwrap().len() > 0);
}

// ---------------------------------------------------------------------------
// stats
// ---------------------------------------------------------------------------

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

    cmd(&tmp)
        .args([
            "remember",
            "--name",
            "stat-mem",
            "--type",
            "user",
            "--description",
            "desc",
            "--body",
            "corpo da stat",
        ])
        .assert()
        .success();

    let output = cmd(&tmp)
        .arg("stats")
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();

    let json: serde_json::Value = serde_json::from_slice(&output).unwrap();
    assert!(json["memories"].as_i64().unwrap() >= 1);
    assert!(json["db_size_bytes"].as_u64().unwrap() > 0);
    // 17 since v1.2.8: V017 opened the entity_type vocabulary by dropping the
    // CHECK that V008 put on `entities.type`.
    assert_eq!(json["schema_version"], 17);
}

#[test]
fn test_stats_auto_inits_when_missing() {
    let tmp = TempDir::new().unwrap();
    cmd(&tmp).arg("stats").assert().success();
}