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
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
//! Entity graph — `--graph-stdin` ingestion, export formats and orphan cleanup.
//!
//! Split out of `tests/integration_graph.rs` by GAP-SG-210: that file held 922
//! lines, past the 800-line ceiling this project sets for itself. It was itself
//! carved out of the 2 485-line `tests/integration.rs` in v1.2.5, so this is
//! the second cut of the same tree; the shared helpers stayed in
//! `tests/common/` and were never copied.

#[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;

// ---------------------------------------------------------------------------
// graph (export)
// ---------------------------------------------------------------------------

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

    seed_memory_with_entities(
        &tmp,
        "graph-seed-json",
        r#"[
            {"name":"graph-ent-a","entity_type":"project","description":null},
            {"name":"graph-ent-b","entity_type":"tool","description":null}
        ]"#,
    );
    cmd(&tmp)
        .args([
            "link",
            "--from",
            "graph-ent-a",
            "--to",
            "graph-ent-b",
            "--relation",
            "uses",
        ])
        .assert()
        .success();

    let output = cmd(&tmp)
        .args(["graph", "--format", "json"])
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();

    let json: serde_json::Value = serde_json::from_slice(&output).unwrap();
    assert!(json["nodes"].is_array());
    assert!(json["edges"].is_array());
    assert!(json["nodes"].as_array().unwrap().len() >= 2);
    assert!(!json["edges"].as_array().unwrap().is_empty());
}

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

    let payload = r#"{
        "entities": [
            {"name": "tipo-tool", "entity_type": "tool"},
            {"name": "tipo-file", "entity_type": "file"}
        ],
        "relationships": [
            {"source": "tipo-tool", "target": "tipo-file", "relation": "uses", "strength": 0.9}
        ]
    }"#;

    cmd(&tmp)
        .args([
            "remember",
            "--name",
            "grafo-tipado",
            "--type",
            "project",
            "--description",
            "grafo tipado via stdin",
            "--graph-stdin",
        ])
        .write_stdin(payload)
        .assert()
        .success();

    let output = cmd(&tmp)
        .args(["graph", "--format", "json"])
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();

    let json: serde_json::Value = serde_json::from_slice(&output).unwrap();
    let nodes = json["nodes"].as_array().unwrap();
    let tipo_tool = nodes
        .iter()
        .find(|node| node["name"] == "tipo-tool")
        .expect("tipo-tool deve existir");
    let tipo_file = nodes
        .iter()
        .find(|node| node["name"] == "tipo-file")
        .expect("tipo-file deve existir");

    assert_eq!(tipo_tool["type"], "tool");
    assert_eq!(tipo_file["type"], "file");
}

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

    let payload = r#"{
        "entities": [
            {"name": "alias-tool", "entity_type": "tool"},
            {"name": "alias-file", "entity_type": "file"}
        ],
        "relationships": [
            {"from": "alias-tool", "to": "alias-file", "relation": "depends-on", "strength": 0.7}
        ]
    }"#;

    cmd(&tmp)
        .args([
            "remember",
            "--name",
            "grafo-aliases",
            "--type",
            "project",
            "--description",
            "grafo com aliases de relacionamento",
            "--graph-stdin",
        ])
        .write_stdin(payload)
        .assert()
        .success();

    let output = cmd(&tmp)
        .args(["graph", "--format", "json"])
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();

    let json: serde_json::Value = serde_json::from_slice(&output).unwrap();
    let edges = json["edges"].as_array().unwrap();
    // v1.2.8: the hyphenated input is stored hyphenated. This assertion used to
    // expect `depends_on`, and that expectation was the defect in miniature —
    // the crate accepted `depends-on`, converted it to `depends_on`, and every
    // read filter then normalised the query the same way, which is why the
    // conversion looked harmless right up until a bulk write path skipped it.
    assert!(edges.iter().any(|edge| {
        edge["from"] == "alias-tool"
            && edge["to"] == "alias-file"
            && edge["relation"] == "depends-on"
    }));
}

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

    let payload = r#"{
        "entities": [
            {"name": "skip-tool", "entity_type": "tool"},
            {"name": "skip-file", "entity_type": "file"}
        ],
        "relationships": [
            {"source": "skip-tool", "target": "skip-file", "relation": "uses", "strength": 0.8}
        ]
    }"#;

    let remember_output = cmd(&tmp)
        .args([
            "remember",
            "--name",
            "grafo-skip",
            "--type",
            "project",
            "--description",
            "grafo explicito com skip",
            "--skip-extraction",
            "--graph-stdin",
            "--json",
        ])
        .write_stdin(payload)
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();
    let remember_json: serde_json::Value = serde_json::from_slice(&remember_output).unwrap();
    assert_eq!(remember_json["entities_persisted"], 2);
    assert_eq!(remember_json["relationships_persisted"], 1);

    let output = cmd(&tmp)
        .args(["graph", "--json"])
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();

    let json: serde_json::Value = serde_json::from_slice(&output).unwrap();
    assert!(json["nodes"]
        .as_array()
        .unwrap()
        .iter()
        .any(|node| node["name"] == "skip-tool" && node["type"] == "tool"));
    assert_eq!(json["edges"].as_array().unwrap().len(), 1);
}

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

    let payload = r#"{
        "body": "corpo textual enviado junto com grafo explicito",
        "entities": [
            {"name": "payload-tool", "entity_type": "tool"},
            {"name": "payload-file", "entity_type": "file"}
        ],
        "relationships": [
            {"source": "payload-tool", "target": "payload-file", "relation": "uses", "strength": 0.8}
        ]
    }"#;

    let remember_output = cmd(&tmp)
        .args([
            "remember",
            "--name",
            "grafo-com-body",
            "--type",
            "project",
            "--description",
            "grafo com body via stdin",
            "--skip-extraction",
            "--graph-stdin",
            "--json",
        ])
        .write_stdin(payload)
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();
    let remember_json: serde_json::Value = serde_json::from_slice(&remember_output).unwrap();
    assert_eq!(remember_json["entities_persisted"], 2);
    assert_eq!(remember_json["relationships_persisted"], 1);

    let read_output = cmd(&tmp)
        .args(["read", "--name", "grafo-com-body", "--json"])
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();
    let read_json: serde_json::Value = serde_json::from_slice(&read_output).unwrap();
    assert_eq!(
        read_json["body"],
        "corpo textual enviado junto com grafo explicito"
    );
}

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

    let output = cmd(&tmp)
        .args(["graph", "--json", "--format", "dot"])
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();

    let json: serde_json::Value = serde_json::from_slice(&output).unwrap();
    assert!(json["nodes"].is_array());
    assert!(json["edges"].is_array());
}

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

    let output = cmd(&tmp)
        .args(["graph", "--json", "--format", "mermaid"])
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();

    let json: serde_json::Value = serde_json::from_slice(&output).unwrap();
    assert!(json["nodes"].is_array());
    assert!(json["edges"].is_array());
}

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

    let output_path = tmp.path().join("graph.dot");
    let output_path_str = output_path.to_str().unwrap();

    let output = cmd(&tmp)
        .args([
            "graph",
            "--json",
            "--format",
            "dot",
            "--output",
            output_path_str,
        ])
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();

    let json: serde_json::Value = serde_json::from_slice(&output).unwrap();
    assert!(json["nodes"].is_array());
    assert!(
        !output_path.exists(),
        "--json deve manter o contrato stdout em vez de gravar DOT"
    );
}

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

    let output = cmd(&tmp)
        .args(["graph", "stats", "--json", "--format", "text"])
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();

    let json: serde_json::Value = serde_json::from_slice(&output).unwrap();
    assert!(json["node_count"].is_number());
    assert!(json["edge_count"].is_number());
}

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

    seed_memory_with_entities(
        &tmp,
        "graph-seed-dot",
        r#"[
            {"name":"dot-a","entity_type":"project","description":null},
            {"name":"dot-b","entity_type":"tool","description":null}
        ]"#,
    );
    cmd(&tmp)
        .args([
            "link",
            "--from",
            "dot-a",
            "--to",
            "dot-b",
            "--relation",
            "uses",
        ])
        .assert()
        .success();

    let out = cmd(&tmp)
        .args(["graph", "--format", "dot"])
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();
    let rendered = String::from_utf8(out).unwrap();
    assert!(rendered.contains("digraph sqlite_graphrag"));
    assert!(rendered.contains("dot-a"));
    assert!(rendered.contains("dot-b"));
    assert!(rendered.contains("uses"));
}

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

    seed_memory_with_entities(
        &tmp,
        "graph-seed-mermaid",
        r#"[{"name":"mer-a","entity_type":"project","description":null}]"#,
    );

    let out = cmd(&tmp)
        .args(["graph", "--format", "mermaid"])
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();
    let rendered = String::from_utf8(out).unwrap();
    assert!(rendered.contains("graph LR"));
    assert!(rendered.contains("mer_a"));
}

// ---------------------------------------------------------------------------
// cleanup-orphans
// ---------------------------------------------------------------------------

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

    // Create a memory with linked entities
    seed_memory_with_entities(
        &tmp,
        "co-mem-ligada",
        r#"[{"name":"co-ent-ligada","entity_type":"project","description":null}]"#,
    );

    // Create a memory with additional entities and remove it, leaving orphan entities
    seed_memory_with_entities(
        &tmp,
        "co-mem-descartada",
        r#"[{"name":"co-ent-orfa","entity_type":"project","description":null}]"#,
    );
    cmd(&tmp)
        .args(["forget", "--name", "co-mem-descartada"])
        .assert()
        .success();
    cmd(&tmp)
        .args([
            "purge",
            "--name",
            "co-mem-descartada",
            "--retention-days",
            "0",
            "--yes",
        ])
        .assert()
        .success();

    // Dry-run counts orphans without removing
    let output = cmd(&tmp)
        .args(["cleanup-orphans", "--dry-run"])
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();
    let dry: serde_json::Value = serde_json::from_slice(&output).unwrap();
    assert_eq!(dry["dry_run"], true);
    assert!(dry["orphan_count"].as_u64().unwrap() >= 1);
    assert_eq!(dry["deleted"].as_u64().unwrap(), 0);

    // Real execution removes the orphans
    let output = cmd(&tmp)
        .args(["cleanup-orphans", "--yes"])
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();
    let done: serde_json::Value = serde_json::from_slice(&output).unwrap();
    assert_eq!(done["dry_run"], false);
    assert!(done["deleted"].as_u64().unwrap() >= 1);
}

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

    seed_memory_with_entities(
        &tmp,
        "co-limpo",
        r#"[{"name":"co-ent-limpa","entity_type":"project","description":null}]"#,
    );

    let output = cmd(&tmp)
        .args(["cleanup-orphans"])
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();
    let json: serde_json::Value = serde_json::from_slice(&output).unwrap();
    assert_eq!(json["orphan_count"], 0);
    assert_eq!(json["deleted"], 0);
}