sqlite-graphrag 1.0.36

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

// Suite — `ingest` end-to-end behaviour
//
// ISOLATION: every test owns an exclusive `TempDir` and points the binary at it
// through `SQLITE_GRAPHRAG_DB_PATH` and `SQLITE_GRAPHRAG_CACHE_DIR`. The
// `--skip-memory-guard` global flag prevents the daemon autostart path from
// being triggered by parallel test runs.
//
// CHILD PROCESS NOTE: `ingest` spawns `remember` as a child process via
// `current_exe()`. The child inherits the parent's environment, so the
// `SQLITE_GRAPHRAG_DAEMON_DISABLE_AUTOSTART=1` flag set by the parent's
// `--skip-memory-guard` propagates automatically.
//
// `#[serial]` is mandatory: although every test owns its DB, the binary
// artefact is shared and process-global resources (sqlite-vec auto-extension,
// fastembed model cache) are loaded per child. Serialising eliminates races.

use assert_cmd::prelude::*;
use serde_json::Value;
use serial_test::serial;
use std::path::Path;
use std::process::Command;
use tempfile::TempDir;

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

/// Builds an `ingest` command bound to an isolated TempDir.
fn ingest_cmd(temp: &TempDir) -> Command {
    let mut cmd = Command::cargo_bin("sqlite-graphrag").expect("binary not found");
    cmd.env(
        "SQLITE_GRAPHRAG_DB_PATH",
        temp.path().join("graphrag.sqlite"),
    );
    cmd.env("SQLITE_GRAPHRAG_CACHE_DIR", temp.path().join("cache"));
    cmd.env("SQLITE_GRAPHRAG_NAMESPACE", "global");
    cmd.arg("--skip-memory-guard");
    cmd
}

/// Initialises an isolated database with V001..V009 applied.
fn init_db(temp: &TempDir) {
    Command::cargo_bin("sqlite-graphrag")
        .expect("binary not found")
        .env(
            "SQLITE_GRAPHRAG_DB_PATH",
            temp.path().join("graphrag.sqlite"),
        )
        .env("SQLITE_GRAPHRAG_CACHE_DIR", temp.path().join("cache"))
        .args(["--skip-memory-guard", "init"])
        .assert()
        .success();
}

/// Writes a Markdown file with the given basename and a deterministic body.
fn write_md(dir: &Path, basename: &str, body: &str) {
    std::fs::write(dir.join(basename), body).expect("write file must succeed");
}

/// Splits NDJSON stdout into trimmed non-empty lines.
fn ndjson_lines(stdout: &[u8]) -> Vec<String> {
    String::from_utf8_lossy(stdout)
        .lines()
        .map(str::trim)
        .filter(|l| !l.is_empty())
        .map(String::from)
        .collect()
}

/// Parses every line as JSON and panics on the first failure.
fn parse_all_lines(lines: &[String]) -> Vec<Value> {
    lines
        .iter()
        .enumerate()
        .map(|(i, line)| {
            serde_json::from_str::<Value>(line)
                .unwrap_or_else(|e| panic!("line {i} is not valid JSON: {e}\nline: {line}"))
        })
        .collect()
}

/// Returns the summary value (last line) and per-file events (preceding lines).
fn split_events_and_summary(values: Vec<Value>) -> (Vec<Value>, Value) {
    assert!(!values.is_empty(), "expected at least the summary line");
    let summary = values.last().cloned().expect("summary present");
    assert_eq!(
        summary.get("summary"),
        Some(&Value::Bool(true)),
        "last line must be the summary, got {summary}"
    );
    let events = values[..values.len() - 1].to_vec();
    (events, summary)
}

// ---------------------------------------------------------------------------
// Test 1 — every emitted line is valid standalone JSON (NDJSON contract)
// ---------------------------------------------------------------------------

#[test]
#[serial]
fn test_ingest_emits_valid_ndjson() {
    let tmp = TempDir::new().expect("TempDir");
    init_db(&tmp);

    let docs = tmp.path().join("docs");
    std::fs::create_dir(&docs).expect("create docs dir");
    write_md(&docs, "alpha.md", "alpha body content");
    write_md(&docs, "beta.md", "beta body content");
    write_md(&docs, "gamma.md", "gamma body content");

    let output = ingest_cmd(&tmp)
        .args([
            "ingest",
            docs.to_str().expect("path utf-8"),
            "--type",
            "document",
            "--skip-extraction",
        ])
        .output()
        .expect("ingest must run");

    assert!(
        output.status.success(),
        "ingest failed: status={:?}\nstderr={}",
        output.status,
        String::from_utf8_lossy(&output.stderr)
    );

    let lines = ndjson_lines(&output.stdout);
    assert!(lines.len() >= 4, "expected at least 4 lines, got {lines:?}");

    let values = parse_all_lines(&lines);
    let (events, summary) = split_events_and_summary(values);

    assert_eq!(events.len(), 3, "expected 3 file events, got {events:?}");
    for event in &events {
        assert_eq!(event.get("status"), Some(&Value::String("indexed".into())));
    }

    assert_eq!(summary["files_total"], 3);
    assert_eq!(summary["files_succeeded"], 3);
    assert_eq!(summary["files_failed"], 0);
    assert_eq!(summary["files_skipped"], 0);
}

// ---------------------------------------------------------------------------
// Test 2 — when no --db / SQLITE_GRAPHRAG_DB_PATH is provided, `graphrag.sqlite`
// is created relative to the current working directory.
// ---------------------------------------------------------------------------

#[test]
#[serial]
fn test_ingest_creates_db_in_cwd() {
    let tmp = TempDir::new().expect("TempDir");
    let cwd = tmp.path().join("workspace");
    std::fs::create_dir(&cwd).expect("create cwd");
    let cache = tmp.path().join("cache");

    // init must run inside the same CWD so the implicit DB path resolves there.
    Command::cargo_bin("sqlite-graphrag")
        .expect("binary not found")
        .current_dir(&cwd)
        .env("SQLITE_GRAPHRAG_CACHE_DIR", &cache)
        .env_remove("SQLITE_GRAPHRAG_DB_PATH")
        .args(["--skip-memory-guard", "init"])
        .assert()
        .success();

    let docs = cwd.join("docs");
    std::fs::create_dir(&docs).expect("create docs dir");
    write_md(&docs, "only.md", "only body");

    let output = Command::cargo_bin("sqlite-graphrag")
        .expect("binary not found")
        .current_dir(&cwd)
        .env("SQLITE_GRAPHRAG_CACHE_DIR", &cache)
        .env("SQLITE_GRAPHRAG_NAMESPACE", "global")
        .env_remove("SQLITE_GRAPHRAG_DB_PATH")
        .args([
            "--skip-memory-guard",
            "ingest",
            "docs",
            "--type",
            "document",
            "--skip-extraction",
        ])
        .output()
        .expect("ingest must run");

    assert!(
        output.status.success(),
        "ingest failed: stderr={}",
        String::from_utf8_lossy(&output.stderr)
    );

    let db = cwd.join("graphrag.sqlite");
    assert!(
        db.exists(),
        "graphrag.sqlite must exist in CWD after ingest, looked at {}",
        db.display()
    );
}

// ---------------------------------------------------------------------------
// Test 3 — `--skip-extraction` propagates to the child `remember` invocation
// without breaking the indexed status.
// ---------------------------------------------------------------------------

#[test]
#[serial]
fn test_ingest_skip_extraction_flag() {
    let tmp = TempDir::new().expect("TempDir");
    init_db(&tmp);

    let docs = tmp.path().join("docs");
    std::fs::create_dir(&docs).expect("create docs dir");
    write_md(&docs, "first.md", "first body");
    write_md(&docs, "second.md", "second body");

    let output = ingest_cmd(&tmp)
        .args([
            "ingest",
            docs.to_str().expect("utf-8"),
            "--type",
            "document",
            "--skip-extraction",
        ])
        .output()
        .expect("ingest must run");

    assert!(
        output.status.success(),
        "ingest failed: stderr={}",
        String::from_utf8_lossy(&output.stderr)
    );

    let values = parse_all_lines(&ndjson_lines(&output.stdout));
    let (events, summary) = split_events_and_summary(values);

    assert_eq!(events.len(), 2);
    for event in &events {
        assert_eq!(event["status"], Value::String("indexed".into()));
        assert!(
            event.get("memory_id").and_then(Value::as_i64).is_some(),
            "memory_id must be present on indexed events: {event}"
        );
    }
    assert_eq!(summary["files_succeeded"], 2);
    assert_eq!(summary["files_failed"], 0);
}

// ---------------------------------------------------------------------------
// Test 4 — `--pattern` filters by basename suffix (`*.md`-style globs only).
// ---------------------------------------------------------------------------

#[test]
#[serial]
fn test_ingest_pattern_filter() {
    let tmp = TempDir::new().expect("TempDir");
    init_db(&tmp);

    let docs = tmp.path().join("docs");
    std::fs::create_dir(&docs).expect("create docs dir");
    write_md(&docs, "keep.md", "keep");
    write_md(&docs, "drop.txt", "drop");
    write_md(&docs, "drop.log", "drop");

    let output = ingest_cmd(&tmp)
        .args([
            "ingest",
            docs.to_str().expect("utf-8"),
            "--type",
            "document",
            "--pattern",
            "*.md",
            "--skip-extraction",
        ])
        .output()
        .expect("ingest must run");

    assert!(
        output.status.success(),
        "ingest failed: stderr={}",
        String::from_utf8_lossy(&output.stderr)
    );

    let values = parse_all_lines(&ndjson_lines(&output.stdout));
    let (events, summary) = split_events_and_summary(values);

    assert_eq!(summary["files_total"], 1);
    assert_eq!(events.len(), 1);
    assert!(events[0]["file"]
        .as_str()
        .expect("file string")
        .ends_with("keep.md"));
}

// ---------------------------------------------------------------------------
// Test 5 — exceeding `--max-files` is a validation error (no partial ingest).
// ---------------------------------------------------------------------------
// The current contract treats `max_files` as a safety cap: if the discovered
// file set exceeds it, the run aborts with a validation error before any file
// is processed. The test pins this contract so a future "process up to N"
// behaviour change is a deliberate decision.

#[test]
#[serial]
fn test_ingest_max_files_cap() {
    let tmp = TempDir::new().expect("TempDir");
    init_db(&tmp);

    let docs = tmp.path().join("docs");
    std::fs::create_dir(&docs).expect("create docs dir");
    for i in 0..12 {
        write_md(&docs, &format!("file-{i:02}.md"), "body");
    }

    let output = ingest_cmd(&tmp)
        .args([
            "ingest",
            docs.to_str().expect("utf-8"),
            "--type",
            "document",
            "--max-files",
            "5",
            "--skip-extraction",
        ])
        .output()
        .expect("ingest must run");

    assert!(
        !output.status.success(),
        "ingest must fail when files exceed --max-files cap"
    );
    let stderr = String::from_utf8_lossy(&output.stderr).to_lowercase();
    assert!(
        stderr.contains("max-files") || stderr.contains("cap") || stderr.contains("exceeds"),
        "stderr should mention the cap, got: {stderr}"
    );
}

// ---------------------------------------------------------------------------
// Test 6 — `--fail-fast` aborts after the first failure; the default keeps
// going. Failures are forced by pointing `--db` at an unwritable path so that
// every child `remember` call fails to open the database.
// ---------------------------------------------------------------------------

#[test]
#[serial]
fn test_ingest_fail_fast_aborts_on_first_error() {
    let tmp = TempDir::new().expect("TempDir");
    init_db(&tmp);

    let docs = tmp.path().join("docs");
    std::fs::create_dir(&docs).expect("create docs dir");
    write_md(&docs, "a.md", "a");
    write_md(&docs, "b.md", "b");
    write_md(&docs, "c.md", "c");

    // An unwritable absolute path — `/proc` is read-only on Linux, so any DB
    // file requested under it cannot be created. Each child `remember` will
    // fail with an I/O error.
    let bad_db = "/proc/sqlite-graphrag-must-not-create.sqlite";

    // Without --fail-fast: every file fails but the run reaches the summary.
    let output = ingest_cmd(&tmp)
        .args([
            "ingest",
            docs.to_str().expect("utf-8"),
            "--type",
            "document",
            "--db",
            bad_db,
            "--skip-extraction",
        ])
        .output()
        .expect("ingest must run");

    let values = parse_all_lines(&ndjson_lines(&output.stdout));
    let (events, summary) = split_events_and_summary(values);
    assert_eq!(events.len(), 3, "all 3 files should have been attempted");
    assert_eq!(summary["files_failed"], 3);
    assert_eq!(summary["files_succeeded"], 0);

    // With --fail-fast: stops after the first failure.
    let output = ingest_cmd(&tmp)
        .args([
            "ingest",
            docs.to_str().expect("utf-8"),
            "--type",
            "document",
            "--db",
            bad_db,
            "--fail-fast",
            "--skip-extraction",
        ])
        .output()
        .expect("ingest must run");

    assert!(
        !output.status.success(),
        "fail-fast must surface a non-zero exit code"
    );
    let values = parse_all_lines(&ndjson_lines(&output.stdout));
    let (events, summary) = split_events_and_summary(values);
    assert_eq!(events.len(), 1, "only the first file should be attempted");
    assert_eq!(summary["files_failed"], 1);
    assert_eq!(summary["files_succeeded"], 0);
}

// ---------------------------------------------------------------------------
// Test 7 — `--recursive` walks subdirectories; the default does not.
// ---------------------------------------------------------------------------

#[test]
#[serial]
fn test_ingest_recursive_walks_subdirs() {
    let tmp = TempDir::new().expect("TempDir");
    init_db(&tmp);

    let root = tmp.path().join("docs");
    let nested = root.join("a").join("b").join("c");
    std::fs::create_dir_all(&nested).expect("create nested dir");
    write_md(&root, "top.md", "top");
    write_md(&nested, "deep.md", "deep");

    // Without --recursive: only the top-level file is found.
    let output = ingest_cmd(&tmp)
        .args([
            "ingest",
            root.to_str().expect("utf-8"),
            "--type",
            "document",
            "--skip-extraction",
        ])
        .output()
        .expect("ingest must run");
    assert!(output.status.success());
    let values = parse_all_lines(&ndjson_lines(&output.stdout));
    let (_, summary) = split_events_and_summary(values);
    assert_eq!(summary["files_total"], 1);

    // With --recursive: both files are picked up.
    let output = ingest_cmd(&tmp)
        .args([
            "ingest",
            root.to_str().expect("utf-8"),
            "--type",
            "document",
            "--recursive",
            "--skip-extraction",
        ])
        .output()
        .expect("ingest must run");
    assert!(output.status.success());
    let values = parse_all_lines(&ndjson_lines(&output.stdout));
    let (events, summary) = split_events_and_summary(values);
    assert_eq!(summary["files_total"], 2);
    let names: Vec<&str> = events
        .iter()
        .map(|e| e["name"].as_str().unwrap_or(""))
        .collect();
    assert!(names.contains(&"top"));
    assert!(names.contains(&"deep"));
}

// ---------------------------------------------------------------------------
// Test 8 — derived names are truncated to a maximum of 60 characters.
// ---------------------------------------------------------------------------

#[test]
#[serial]
fn test_ingest_name_truncation_60_chars() {
    let tmp = TempDir::new().expect("TempDir");
    init_db(&tmp);

    let docs = tmp.path().join("docs");
    std::fs::create_dir(&docs).expect("create docs dir");

    // 80 ASCII lowercase characters — well under the Linux 255-byte filename
    // limit and large enough to force truncation.
    let long_stem = "a".repeat(80);
    write_md(&docs, &format!("{long_stem}.md"), "body");

    let output = ingest_cmd(&tmp)
        .args([
            "ingest",
            docs.to_str().expect("utf-8"),
            "--type",
            "document",
            "--skip-extraction",
        ])
        .output()
        .expect("ingest must run");
    assert!(
        output.status.success(),
        "ingest failed: stderr={}",
        String::from_utf8_lossy(&output.stderr)
    );

    let values = parse_all_lines(&ndjson_lines(&output.stdout));
    let (events, _) = split_events_and_summary(values);
    assert_eq!(events.len(), 1);
    let name = events[0]["name"].as_str().expect("name string");
    assert!(
        name.len() <= 60,
        "derived name must be truncated to <= 60 chars, got len={} ({name})",
        name.len()
    );
}

// ---------------------------------------------------------------------------
// Test 9 — the default pattern is `*.md`; `.txt` files are ignored unless an
// explicit pattern asks for them.
// ---------------------------------------------------------------------------

#[test]
#[serial]
fn test_ingest_default_pattern_is_md() {
    let tmp = TempDir::new().expect("TempDir");
    init_db(&tmp);

    let docs = tmp.path().join("docs");
    std::fs::create_dir(&docs).expect("create docs dir");
    write_md(&docs, "doc.md", "md body");
    write_md(&docs, "doc.txt", "txt body");

    let output = ingest_cmd(&tmp)
        .args([
            "ingest",
            docs.to_str().expect("utf-8"),
            "--type",
            "document",
            "--skip-extraction",
        ])
        .output()
        .expect("ingest must run");
    assert!(output.status.success());

    let values = parse_all_lines(&ndjson_lines(&output.stdout));
    let (events, summary) = split_events_and_summary(values);
    assert_eq!(summary["files_total"], 1);
    assert_eq!(events.len(), 1);
    assert!(events[0]["file"]
        .as_str()
        .expect("file string")
        .ends_with("doc.md"));
}

// ---------------------------------------------------------------------------
// Test 10 — the last NDJSON line is the summary and exposes the documented
// counter fields.
// ---------------------------------------------------------------------------

#[test]
#[serial]
fn test_ingest_summary_is_last_line() {
    let tmp = TempDir::new().expect("TempDir");
    init_db(&tmp);

    let docs = tmp.path().join("docs");
    std::fs::create_dir(&docs).expect("create docs dir");
    write_md(&docs, "one.md", "one body");
    write_md(&docs, "two.md", "two body");

    let output = ingest_cmd(&tmp)
        .args([
            "ingest",
            docs.to_str().expect("utf-8"),
            "--type",
            "document",
            "--skip-extraction",
        ])
        .output()
        .expect("ingest must run");
    assert!(output.status.success());

    let lines = ndjson_lines(&output.stdout);
    let last = lines.last().expect("at least one line");
    let summary: Value = serde_json::from_str(last).expect("summary must be JSON");

    assert_eq!(summary["summary"], Value::Bool(true));
    for key in [
        "files_total",
        "files_succeeded",
        "files_failed",
        "files_skipped",
        "elapsed_ms",
        "dir",
        "pattern",
        "recursive",
    ] {
        assert!(
            summary.get(key).is_some(),
            "summary must expose `{key}`, got: {summary}"
        );
    }
    // Earlier lines must NOT carry `summary: true`.
    for line in &lines[..lines.len() - 1] {
        let v: Value = serde_json::from_str(line).expect("event JSON");
        assert_ne!(
            v.get("summary"),
            Some(&Value::Bool(true)),
            "only the final line should be flagged as summary, got: {line}"
        );
    }
}