sqlite-graphrag 1.0.39

Local GraphRAG memory for LLMs in a single SQLite file
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
#![cfg(feature = "slow-tests")]

// Suite 4 — Hardened lock and concurrency tests
//
// ISOLATION: each test uses `SQLITE_GRAPHRAG_CACHE_DIR` pointing to a
// `TempDir` exclusive per test. `#[serial]` is required in all tests to avoid
// filesystem races between tests that share the same binary.
//
// `--skip-memory-guard` is used so that the RAM check does not abort before
// the semaphore is exercised in CI environments with limited memory.
//
// Flaky timing tests are marked with `#[ignore]` and document how to run them
// manually.

use assert_cmd::Command;
use serial_test::serial;
use std::sync::{Arc, Barrier};
use tempfile::TempDir;

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

/// Returns the lock file path for the given slot (1-based) inside the `TempDir`.
fn slot_path(tmp: &TempDir, slot: usize) -> std::path::PathBuf {
    tmp.path().join(format!("cli-slot-{slot}.lock"))
}

/// Ocupa `n_slots` arquivos de lock diretamente via fs4, retornando os handles.
fn ocupar_slots(tmp: &TempDir, n_slots: usize) -> Vec<std::fs::File> {
    use fs4::fs_std::FileExt;
    use std::fs::OpenOptions;

    (1..=n_slots)
        .map(|slot| {
            let path = slot_path(tmp, slot);
            let file = OpenOptions::new()
                .read(true)
                .write(true)
                .create(true)
                .truncate(false)
                .open(&path)
                .unwrap_or_else(|_| panic!("criação do lock file slot {slot} deve funcionar"));
            file.try_lock_exclusive()
                .unwrap_or_else(|_| panic!("slot {slot} deve estar livre antes do teste"));
            file
        })
        .collect()
}

// ---------------------------------------------------------------------------
// Test 1 — 5 simultaneous instances: the 5th receives exit 75
// ---------------------------------------------------------------------------
// Occupies the 4 default slots via fs4, then triggers a 5th invocation with
// --wait-lock 0 e confirma que ela retorna exit 75 (AllSlotsFull).

#[test]
#[serial]
fn cinco_instancias_quinta_exit_75() {
    let tmp = TempDir::new().expect("TempDir deve ser criado");

    // Occupy all 4 default slots
    let handles = ocupar_slots(&tmp, 4);

    // 5th invocation with --wait-lock 0 must fail with exit 75
    Command::cargo_bin("sqlite-graphrag")
        .expect("binário sqlite-graphrag não encontrado")
        .env("SQLITE_GRAPHRAG_CACHE_DIR", tmp.path())
        .args([
            "--skip-memory-guard",
            "--max-concurrency",
            "4",
            "--wait-lock",
            "0",
            "namespace-detect",
        ])
        .assert()
        .failure()
        .code(75);

    drop(handles);
}

// ---------------------------------------------------------------------------
// Test 2 — --wait-lock 3 waits up to 3 seconds for a slot
// ---------------------------------------------------------------------------
// Occupies all slots, releases after 1s in a separate thread, confirms that
// --wait-lock 3 aguarda e conclui com sucesso.
//
// MARCADO #[ignore] — adiciona ~1-2s ao CI e depende de timing de threads.
// Para rodar manualmente:
//   cargo test -- --ignored wait_lock_3s_respeitado

#[test]
#[serial]
#[ignore = "flaky — depende de timing de threads — rodar manualmente com: cargo test -- --ignored"]
fn wait_lock_3s_respeitado() {
    let tmp = TempDir::new().expect("TempDir deve ser criado");
    let tmp_path = tmp.path().to_path_buf();

    // Ocupa todos os 4 slots
    let handles = ocupar_slots(&tmp, 4);

    // Release all after 1 second in a separate thread
    std::thread::spawn(move || {
        std::thread::sleep(std::time::Duration::from_millis(800));
        drop(handles);
        // Keep tmp_path alive until here
        let _ = &tmp_path;
    });

    // --wait-lock 3 must wait for release (within 3s) and complete
    Command::cargo_bin("sqlite-graphrag")
        .expect("binário sqlite-graphrag não encontrado")
        .env("SQLITE_GRAPHRAG_CACHE_DIR", tmp.path())
        .args([
            "--skip-memory-guard",
            "--max-concurrency",
            "4",
            "--wait-lock",
            "3",
            "namespace-detect",
        ])
        .assert()
        .success();
}

// ---------------------------------------------------------------------------
// Teste 3 — remember duplicado seguido de edit com --updated-at stale → exit 3
// ---------------------------------------------------------------------------
// Simulates optimistic locking: insert a memory, get updated_at, modify
// via CLI, then try editing again with the stale updated_at (before the
// modification) and confirm exit 3 (Conflict).

#[test]
#[serial]
fn optimistic_locking_conflito_exit_3() {
    let tmp = TempDir::new().expect("TempDir deve ser criado");
    let db_path = tmp.path().join("test.sqlite");

    // Init
    Command::cargo_bin("sqlite-graphrag")
        .expect("binário não encontrado")
        .env("SQLITE_GRAPHRAG_DB_PATH", &db_path)
        .env("SQLITE_GRAPHRAG_CACHE_DIR", tmp.path())
        .args(["--skip-memory-guard", "init"])
        .assert()
        .success();

    // Insert memory
    Command::cargo_bin("sqlite-graphrag")
        .expect("binário não encontrado")
        .env("SQLITE_GRAPHRAG_DB_PATH", &db_path)
        .env("SQLITE_GRAPHRAG_CACHE_DIR", tmp.path())
        .args([
            "--skip-memory-guard",
            "remember",
            "--name",
            "mem-conflito",
            "--type",
            "user",
            "--namespace",
            "global",
            "--description",
            "desc original",
            "--body",
            "corpo original",
        ])
        .assert()
        .success();

    // Obter updated_at via read para capturar o timestamp antes de modificar
    let output_leitura = Command::cargo_bin("sqlite-graphrag")
        .expect("binário não encontrado")
        .env("SQLITE_GRAPHRAG_DB_PATH", &db_path)
        .env("SQLITE_GRAPHRAG_CACHE_DIR", tmp.path())
        .args([
            "--skip-memory-guard",
            "read",
            "--name",
            "mem-conflito",
            "--namespace",
            "global",
        ])
        .output()
        .expect("output deve funcionar");

    let json_leitura: serde_json::Value =
        serde_json::from_slice(&output_leitura.stdout).expect("output deve ser JSON");

    let _updated_at_real = json_leitura
        .get("updated_at")
        .and_then(|v| v.as_i64())
        .expect("updated_at deve existir e ser i64");

    // Impossible value: Unix epoch 1970-01-01 will never be updated_at for a freshly created memory.
    // Ensures the conflict regardless of how many operations happen in the same second.
    let updated_at_stale: i64 = 1;

    // Edit with stale --expected-updated-at must fail with exit 3 (Conflict)
    Command::cargo_bin("sqlite-graphrag")
        .expect("binário não encontrado")
        .env("SQLITE_GRAPHRAG_DB_PATH", &db_path)
        .env("SQLITE_GRAPHRAG_CACHE_DIR", tmp.path())
        .args([
            "--skip-memory-guard",
            "edit",
            "--name",
            "mem-conflito",
            "--namespace",
            "global",
            "--description",
            "desc conflitante",
            "--expected-updated-at",
            &updated_at_stale.to_string(),
        ])
        .assert()
        .failure()
        .code(3);
}

// ---------------------------------------------------------------------------
// Test 4 — purge during recall does not corrupt the database
// ---------------------------------------------------------------------------
// Dispara recall e purge em paralelo via threads e confirma que o banco
// remains intact (no SQLITE_CORRUPT errors or panic) after both finish.
// Uses std::sync::Barrier to synchronize the start.

#[test]
#[serial]
fn purge_during_recall_does_not_corrupt() {
    let tmp = TempDir::new().expect("TempDir deve ser criado");
    let db_path = tmp.path().join("test.sqlite");

    // Init
    Command::cargo_bin("sqlite-graphrag")
        .expect("binário não encontrado")
        .env("SQLITE_GRAPHRAG_DB_PATH", &db_path)
        .env("SQLITE_GRAPHRAG_CACHE_DIR", tmp.path())
        .args(["--skip-memory-guard", "init"])
        .assert()
        .success();

    // Insert some old memories so that purge has something to do
    for i in 0..3 {
        Command::cargo_bin("sqlite-graphrag")
            .expect("binário não encontrado")
            .env("SQLITE_GRAPHRAG_DB_PATH", &db_path)
            .env("SQLITE_GRAPHRAG_CACHE_DIR", tmp.path())
            .args([
                "--skip-memory-guard",
                "remember",
                "--name",
                &format!("mem-purge-{i}"),
                "--type",
                "user",
                "--namespace",
                "global",
                "--description",
                &format!("memória antiga {i}"),
                "--body",
                &format!("corpo da memória para purge teste {i}"),
            ])
            .assert()
            .success();
    }

    let db_path_recall = db_path.clone();
    let db_path_purge = db_path.clone();
    let cache_path_recall = tmp.path().to_path_buf();
    let cache_path_purge = tmp.path().to_path_buf();

    let barrier = Arc::new(Barrier::new(2));
    let barrier_recall = Arc::clone(&barrier);
    let barrier_purge = Arc::clone(&barrier);

    let bin_path = std::path::PathBuf::from(env!("CARGO_BIN_EXE_sqlite-graphrag"));

    let bin_recall = bin_path.clone();
    let bin_purge = bin_path.clone();

    // Thread recall — busca concorrente
    let handle_recall = std::thread::spawn(move || {
        barrier_recall.wait();
        std::process::Command::new(&bin_recall)
            .env("SQLITE_GRAPHRAG_DB_PATH", &db_path_recall)
            .env("SQLITE_GRAPHRAG_CACHE_DIR", &cache_path_recall)
            .args([
                "--skip-memory-guard",
                "recall",
                "memória antiga",
                "--namespace",
                "global",
                "--k",
                "5",
            ])
            .output()
            .expect("recall deve executar sem panic")
    });

    // Purge thread — concurrent purge with --dry-run so nothing is deleted
    let handle_purge = std::thread::spawn(move || {
        barrier_purge.wait();
        std::process::Command::new(&bin_purge)
            .env("SQLITE_GRAPHRAG_DB_PATH", &db_path_purge)
            .env("SQLITE_GRAPHRAG_CACHE_DIR", &cache_path_purge)
            .args([
                "--skip-memory-guard",
                "purge",
                "--namespace",
                "global",
                "--dry-run",
            ])
            .output()
            .expect("purge deve executar sem panic")
    });

    let resultado_recall = handle_recall
        .join()
        .expect("thread recall não deve entrar em panic");
    let resultado_purge = handle_purge
        .join()
        .expect("thread purge não deve entrar em panic");

    // Neither must have exited with a corruption error code
    // Exit code 10 = Database error (SQLite), 20 = Internal
    let codigo_recall = resultado_recall.status.code().unwrap_or(-1);
    let codigo_purge = resultado_purge.status.code().unwrap_or(-1);

    assert_ne!(
        codigo_recall, 20,
        "recall não deve retornar erro interno (exit 20)"
    );
    assert_ne!(
        codigo_purge, 20,
        "purge não deve retornar erro interno (exit 20)"
    );

    // Verify database integrity after concurrent operations
    let conn = rusqlite::Connection::open(&db_path).expect("banco deve abrir após concorrência");
    let integrity: String = conn
        .query_row("PRAGMA integrity_check", [], |row| row.get(0))
        .expect("PRAGMA integrity_check deve funcionar");
    assert_eq!(
        integrity, "ok",
        "banco deve estar íntegro após recall+purge concorrentes"
    );
}

// ---------------------------------------------------------------------------
// Test 5 — 10 remembers in different namespaces do not collide
// ---------------------------------------------------------------------------
// Confirms that inserts into 10 distinct namespaces via concurrent threads
// all succeed and that each namespace contains exactly 1 memory.

#[test]
#[serial]
fn dez_remembers_namespaces_diferentes() {
    let tmp = TempDir::new().expect("TempDir deve ser criado");
    let db_path = tmp.path().join("test.sqlite");

    // Init
    Command::cargo_bin("sqlite-graphrag")
        .expect("binário não encontrado")
        .env("SQLITE_GRAPHRAG_DB_PATH", &db_path)
        .env("SQLITE_GRAPHRAG_CACHE_DIR", tmp.path())
        .args(["--skip-memory-guard", "init"])
        .assert()
        .success();

    let n_threads = 10;
    let barrier = Arc::new(Barrier::new(n_threads));
    let bin_path = std::path::PathBuf::from(env!("CARGO_BIN_EXE_sqlite-graphrag"));

    let handles: Vec<_> = (0..n_threads)
        .map(|i| {
            let db_path_clone = db_path.clone();
            let cache_path_clone = tmp.path().to_path_buf();
            let barrier_clone = Arc::clone(&barrier);
            let namespace = format!("ns-thread-{i}");
            let bin_clone = bin_path.clone();

            std::thread::spawn(move || {
                // Sincroniza todos os threads antes de disparar
                barrier_clone.wait();

                std::process::Command::new(&bin_clone)
                    .env("SQLITE_GRAPHRAG_DB_PATH", &db_path_clone)
                    .env("SQLITE_GRAPHRAG_CACHE_DIR", &cache_path_clone)
                    .args([
                        "--skip-memory-guard",
                        "remember",
                        "--name",
                        &format!("mem-thread-{i}"),
                        "--type",
                        "user",
                        "--namespace",
                        &namespace,
                        "--description",
                        &format!("memória do thread {i}"),
                        "--body",
                        &format!("corpo da memória isolada para o namespace {namespace}"),
                    ])
                    .output()
                    .expect("remember deve executar sem panic")
            })
        })
        .collect();

    // Coleta resultados de todas as threads
    let resultados: Vec<_> = handles
        .into_iter()
        .map(|h| h.join().expect("thread não deve entrar em panic"))
        .collect();

    let sucessos = resultados.iter().filter(|r| r.status.success()).count();
    let falhas = resultados.len() - sucessos;

    assert_eq!(
        sucessos, n_threads,
        "todos os {n_threads} remembers em namespaces distintos devem ter sucesso, \
         obtivemos {sucessos} sucessos e {falhas} falhas"
    );

    // Verify that each namespace has exactly 1 memory in the database
    let conn = rusqlite::Connection::open(&db_path).expect("banco deve abrir");
    for i in 0..n_threads {
        let namespace = format!("ns-thread-{i}");
        let count: i64 = conn
            .query_row(
                "SELECT COUNT(*) FROM memories WHERE namespace = ?1 AND deleted_at IS NULL",
                rusqlite::params![namespace],
                |row| row.get(0),
            )
            .expect("query deve funcionar");

        assert_eq!(
            count, 1,
            "namespace '{namespace}' deve ter exatamente 1 memória, encontrou {count}"
        );
    }
}