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
#![cfg(feature = "slow-tests")]

//! Suite 11 — tests for the recipes documented in `docs/COOKBOOK.md`: recipes 10 through 15, maintenance and throughput.
//!
//! Each test checks that actual CLI behaviour matches what the cookbook
//! promises, so drift between documentation and implementation shows up here
//! rather than in an operator's terminal.
//!
//! Split by GAP-SG-210: the single file held 907 lines, past the 800-line
//! ceiling this project sets for itself.
//!
//! Recipes skipped by design, in either half:
//!   - Recipe 6: AGENTS.md discovery — documentation only, no executable commands
//!   - Recipe 12: Git LFS — requires git lfs installed and a git repository

use assert_cmd::Command;
use serial_test::serial;
#[allow(unused_imports)]
use std::fs;
use tempfile::TempDir;

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

fn bin() -> std::path::PathBuf {
    std::path::PathBuf::from(env!("CARGO_BIN_EXE_sqlite-graphrag"))
}

fn cmd(dir: &TempDir) -> Command {
    // GAP-SG-101: product env is not read (G-T-XDG-04).
    let mock_dir = common::mock_llm_path();
    let mut c = Command::new(bin());
    c.env_clear()
        .env("PATH", common::prepend_path(&mock_dir))
        .arg("--skip-memory-guard");
    common::wire_assert_cmd(dir, &mut c, "ng.sqlite");
    c
}

#[allow(dead_code)]
fn init(dir: &TempDir) {
    cmd(dir).arg("init").assert().success();
}

// Recipe 10 — Purge + vacuum + optimize: full pipeline returns JSON with status ok
#[test]
#[serial]
fn recipe_10_purge_vacuum_optimize() {
    let dir = TempDir::new().unwrap();
    init(&dir);

    // Seed and soft-delete so that purge has data to work on
    cmd(&dir)
        .args([
            "remember",
            "--name",
            "mem-a-purgar",
            "--type",
            "user",
            "--description",
            "será deletada",
            "--body",
            "conteúdo temporário para teste de purge",
            "--namespace",
            "global",
        ])
        .assert()
        .success();

    cmd(&dir)
        .args(["forget", "--name", "mem-a-purgar", "--namespace", "global"])
        .assert()
        .success();

    // Purge
    let purge_out = cmd(&dir)
        .args([
            "purge",
            "--retention-days",
            "0",
            "--yes",
            "--namespace",
            "global",
        ])
        .output()
        .unwrap();

    assert!(
        purge_out.status.success(),
        "recipe 10: purge deve ter exit 0"
    );
    let purge_json: serde_json::Value = serde_json::from_slice(&purge_out.stdout)
        .expect("recipe 10: purge deve retornar JSON válido");
    assert!(
        purge_json["elapsed_ms"].is_number(),
        "recipe 10: purge deve ter elapsed_ms"
    );

    // Vacuum
    let vacuum_out = cmd(&dir).arg("vacuum").output().unwrap();
    assert!(
        vacuum_out.status.success(),
        "recipe 10: vacuum deve ter exit 0"
    );
    let vacuum_json: serde_json::Value = serde_json::from_slice(&vacuum_out.stdout)
        .expect("recipe 10: vacuum deve retornar JSON válido");
    assert_eq!(
        vacuum_json["status"], "ok",
        "recipe 10: vacuum.status deve ser ok"
    );

    // Optimize
    let optimize_out = cmd(&dir).arg("optimize").output().unwrap();
    assert!(
        optimize_out.status.success(),
        "recipe 10: optimize deve ter exit 0"
    );
    let optimize_json: serde_json::Value = serde_json::from_slice(&optimize_out.stdout)
        .expect("recipe 10: optimize deve retornar JSON válido");
    assert_eq!(
        optimize_json["status"], "ok",
        "recipe 10: optimize.status deve ser ok"
    );
}

// Recipe 11 — NDJSON export via list: list returns object with items key (not a root array)
// The public COOKBOOK already documents `jaq -c '.items[]'` and this test guards that contract.
#[test]
#[serial]
fn recipe_11_ndjson_list() {
    let dir = TempDir::new().unwrap();
    init(&dir);

    for i in 1..=3u32 {
        cmd(&dir)
            .args([
                "remember",
                "--name",
                &format!("mem-export-{i}"),
                "--type",
                "reference",
                "--description",
                &format!("memória {i} para export"),
                "--body",
                &format!("conteúdo da memória número {i}"),
                "--namespace",
                "global",
            ])
            .assert()
            .success();
    }

    let output = cmd(&dir)
        .args([
            "list",
            "--limit",
            "10000",
            "--format",
            "json",
            "--namespace",
            "global",
        ])
        .output()
        .unwrap();

    assert!(output.status.success(), "recipe 11: list deve ter exit 0");

    let json: serde_json::Value =
        serde_json::from_slice(&output.stdout).expect("recipe 11: list deve retornar JSON válido");

    // Actual behaviour: an object with an "items" key
    assert!(
        json["items"].is_array(),
        "recipe 11: list retorna objeto com chave 'items' (não array root — drift detectado se mudou)"
    );
    assert!(
        json["elapsed_ms"].is_number(),
        "recipe 11: list deve ter elapsed_ms"
    );

    let items = json["items"].as_array().unwrap();
    assert_eq!(
        items.len(),
        3,
        "recipe 11: deve listar 3 memórias inseridas"
    );

    // Every item must carry the expected fields for NDJSON
    let first = &items[0];
    assert!(first["id"].is_number(), "recipe 11: item.id deve existir");
    assert!(
        first["name"].is_string(),
        "recipe 11: item.name deve existir"
    );
    assert!(
        first["namespace"].is_string(),
        "recipe 11: item.namespace deve existir"
    );
}

// Recipe 13 — GNU parallel simulated with threads: parallel recall across 4 namespaces
#[test]
#[serial]
fn recipe_13_parallel_namespaces() {
    let dir = TempDir::new().unwrap();
    init(&dir);

    let namespaces = ["project-a", "project-b", "project-c", "project-d"];

    // Seed one memory in each namespace
    for ns in &namespaces {
        cmd(&dir)
            .args([
                "remember",
                "--name",
                &format!("mem-{ns}"),
                "--type",
                "project",
                "--description",
                &format!("memória do {ns}"),
                "--body",
                &format!("taxa de erro elevada em {ns} detectada"),
                "--namespace",
                ns,
            ])
            .assert()
            .success();
    }

    let db_path = dir.path().join("ng.sqlite").to_owned();
    let root = dir.path().to_owned();
    let bin_path = bin();
    let mock_path = common::prepend_path(&common::mock_llm_path());

    // Simulate `parallel -j 4` with 4 simultaneous threads
    let handles: Vec<_> = namespaces
        .iter()
        .map(|ns| {
            let ns = ns.to_string();
            let db = db_path.clone();
            let root = root.clone();
            let bin = bin_path.clone();
            let path = mock_path.clone();
            std::thread::spawn(move || {
                let mut c = std::process::Command::new(&bin);
                c.env_clear().env("PATH", &path).arg("--skip-memory-guard");
                common::wire_std_cmd(&root, &mut c, &db);
                c.args(["recall", "--db"])
                    .arg(&db)
                    .args(["error rate", "--k", "5", "--namespace", &ns])
                    .output()
                    .expect("recall em thread deve executar sem panic")
            })
        })
        .collect();

    let outputs: Vec<_> = handles.into_iter().map(|h| h.join().unwrap()).collect();

    for (i, output) in outputs.iter().enumerate() {
        assert!(
            output.status.success(),
            "recipe 13: recall no namespace {} deve ter exit 0",
            namespaces[i]
        );
        let json: serde_json::Value = serde_json::from_slice(&output.stdout)
            .expect("recipe 13: recall deve retornar JSON válido");
        assert!(
            json["results"].is_array(),
            "recipe 13: recall.results deve ser array no namespace {}",
            namespaces[i]
        );
        let results = json["results"].as_array().unwrap();
        assert!(
            !results.is_empty(),
            "recipe 13: recall deve encontrar memória no namespace {} com query 'error rate'",
            namespaces[i]
        );
    }
}

// Recipe 14 — Debug slow queries: health + stats + --json return the documented fields
#[test]
#[serial]
fn recipe_14_debug_health_stats() {
    let dir = TempDir::new().unwrap();
    init(&dir);

    // Health: fields documented in the COOKBOOK
    let health_out = cmd(&dir).args(["health", "--json"]).output().unwrap();

    assert!(
        health_out.status.success(),
        "recipe 14: health deve ter exit 0"
    );

    let health: serde_json::Value = serde_json::from_slice(&health_out.stdout)
        .expect("recipe 14: health deve retornar JSON válido");

    // Validates the documented fields: `integrity, wal_size_mb, journal_mode`
    assert!(
        health.get("integrity").is_some(),
        "recipe 14: health deve ter campo 'integrity' como documentado"
    );
    assert!(
        health.get("wal_size_mb").is_some(),
        "recipe 14: health deve ter campo 'wal_size_mb' como documentado"
    );
    assert!(
        health.get("journal_mode").is_some(),
        "recipe 14: health deve ter campo 'journal_mode' como documentado"
    );

    // Stats: fields documented in the COOKBOOK
    let stats_out = cmd(&dir).args(["stats", "--json"]).output().unwrap();

    assert!(
        stats_out.status.success(),
        "recipe 14: stats deve ter exit 0"
    );

    let stats: serde_json::Value = serde_json::from_slice(&stats_out.stdout)
        .expect("recipe 14: stats deve retornar JSON válido");

    // Validates the documented fields: `memories, memories_total, entities, entities_total,
    // relationships, relationships_total, edges, chunks_total, avg_body_len,
    // db_size_bytes, db_bytes`
    let expected_fields = [
        "memories",
        "memories_total",
        "entities",
        "entities_total",
        "relationships",
        "relationships_total",
        "edges",
        "chunks_total",
        "avg_body_len",
        "db_size_bytes",
        "db_bytes",
    ];

    for field in &expected_fields {
        assert!(
            stats.get(field).is_some(),
            "recipe 14: stats deve ter campo '{field}' como documentado no COOKBOOK"
        );
    }
}

// Recipe 15 — Simulated benchmark: recall and hybrid-search execute in reasonable time
// Simulates `hyperfine` by checking that both commands complete without a timeout
#[test]
#[serial]
fn recipe_15_hyperfine_timing() {
    let dir = TempDir::new().unwrap();
    init(&dir);

    // Seed with memory for non-trivial search
    cmd(&dir)
        .args([
            "remember",
            "--name",
            "pg-migration",
            "--type",
            "incident",
            "--description",
            "postgres migration benchmark",
            "--body",
            "migração postgres com deadlock em ambiente de produção durante janela de manutenção",
            "--namespace",
            "global",
        ])
        .assert()
        .success();

    // Measure recall
    let t0 = std::time::Instant::now();
    let recall_out = cmd(&dir)
        .args([
            "recall",
            "postgres migration",
            "--k",
            "10",
            "--namespace",
            "global",
        ])
        .output()
        .unwrap();
    let recall_elapsed = t0.elapsed();

    assert!(
        recall_out.status.success(),
        "recipe 15: recall deve ter exit 0"
    );
    assert!(
        recall_elapsed.as_secs() < 30,
        "recipe 15: recall deve completar em menos de 30s, levou {recall_elapsed:?}"
    );

    // Measure hybrid-search
    let t1 = std::time::Instant::now();
    let hybrid_out = cmd(&dir)
        .args([
            "hybrid-search",
            "postgres migration",
            "--k",
            "10",
            "--namespace",
            "global",
        ])
        .output()
        .unwrap();
    let hybrid_elapsed = t1.elapsed();

    assert!(
        hybrid_out.status.success(),
        "recipe 15: hybrid-search deve ter exit 0"
    );
    assert!(
        hybrid_elapsed.as_secs() < 30,
        "recipe 15: hybrid-search deve completar em menos de 30s, levou {hybrid_elapsed:?}"
    );

    // Both return valid JSON results with elapsed_ms
    let recall_json: serde_json::Value = serde_json::from_slice(&recall_out.stdout).unwrap();
    let hybrid_json: serde_json::Value = serde_json::from_slice(&hybrid_out.stdout).unwrap();

    assert!(
        recall_json["elapsed_ms"].is_number(),
        "recipe 15: recall deve reportar elapsed_ms no JSON"
    );
    assert!(
        hybrid_json["elapsed_ms"].is_number(),
        "recipe 15: hybrid-search deve reportar elapsed_ms no JSON"
    );
}