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
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
//! Memory lifecycle: forget, purge, rename, edit, history, restore, and the FTS index integrity check across a forget/purge cycle.
//!
//! Split out of `integration_memory_crud.rs` by GAP-SG-208: that file reached
//! 1018 lines, past the 800-line ceiling this project sets for itself. It was
//! itself carved out of a 2485-line `integration.rs` in v1.2.5, so this is the
//! second pass of the same decomposition. Helpers stay in `tests/common/`.

#[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 test_forget_soft_delete() {
    let tmp = TempDir::new().unwrap();
    init_db(&tmp);

    cmd(&tmp)
        .args([
            "remember",
            "--name",
            "esquecivel",
            "--type",
            "user",
            "--description",
            "sera deletada",
            "--body",
            "corpo",
        ])
        .assert()
        .success();

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

    let json: serde_json::Value = serde_json::from_slice(&output).unwrap();
    assert_eq!(json["forgotten"], true);

    cmd(&tmp)
        .args(["read", "--name", "esquecivel"])
        .assert()
        .failure()
        .code(4);
}

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

    cmd(&tmp)
        .args(["forget", "--name", "nao-existe"])
        .assert()
        .failure()
        .code(4);
}

// ---------------------------------------------------------------------------
// purge
// ---------------------------------------------------------------------------

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

    cmd(&tmp)
        .args([
            "remember",
            "--name",
            "purge-target",
            "--type",
            "user",
            "--description",
            "soft delete target",
            "--body",
            "body to purge later",
        ])
        .assert()
        .success();

    cmd(&tmp)
        .args(["forget", "--name", "purge-target"])
        .assert()
        .success();

    let output = cmd(&tmp)
        .args([
            "purge",
            "--name",
            "purge-target",
            "--retention-days",
            "0",
            "--yes",
        ])
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();

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

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

    cmd(&tmp)
        .args([
            "remember",
            "--name",
            "purge-yes-target",
            "--type",
            "user",
            "--description",
            "alvo para teste --yes",
            "--body",
            "corpo yes noop",
        ])
        .assert()
        .success();

    cmd(&tmp)
        .args(["forget", "--name", "purge-yes-target"])
        .assert()
        .success();

    let output = cmd(&tmp)
        .args([
            "purge",
            "--name",
            "purge-yes-target",
            "--retention-days",
            "0",
            "--yes",
        ])
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();

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

// ---------------------------------------------------------------------------
// rename
// ---------------------------------------------------------------------------

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

    cmd(&tmp)
        .args([
            "remember",
            "--name",
            "memoria-antiga",
            "--type",
            "user",
            "--description",
            "desc original",
            "--body",
            "corpo original",
        ])
        .assert()
        .success();

    let output = cmd(&tmp)
        .args([
            "rename",
            "--name",
            "memoria-antiga",
            "--new-name",
            "memory-renamed",
        ])
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();

    let json: serde_json::Value = serde_json::from_slice(&output).unwrap();
    assert_eq!(json["name"], "memory-renamed");
    assert!(json["memory_id"].as_i64().unwrap() > 0);
}

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

    cmd(&tmp)
        .args(["rename", "--name", "nao-existe", "--new-name", "novo-nome"])
        .assert()
        .failure()
        .code(4);
}

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

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

    let output = cmd(&tmp)
        .args([
            "rename",
            "--name",
            "memoria-valida",
            "--new-name",
            "Nome Com Espaco",
        ])
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();

    let json: serde_json::Value = serde_json::from_slice(&output).unwrap();
    assert_eq!(json["name"], "nome-com-espaco");
    assert_eq!(json["action"], "renamed");
}

// ---------------------------------------------------------------------------
// edit
// ---------------------------------------------------------------------------

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

    cmd(&tmp)
        .args([
            "remember",
            "--name",
            "memoria-editavel",
            "--type",
            "user",
            "--description",
            "desc original",
            "--body",
            "corpo original",
        ])
        .assert()
        .success();

    let output = cmd(&tmp)
        .args([
            "edit",
            "--name",
            "memoria-editavel",
            "--body",
            "corpo atualizado",
        ])
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();

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

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

    cmd(&tmp)
        .args([
            "remember",
            "--name",
            "memoria-edit-ambigua",
            "--type",
            "user",
            "--description",
            "desc",
            "--body",
            "corpo original",
        ])
        .assert()
        .success();

    cmd(&tmp)
        .args([
            "edit",
            "--name",
            "memoria-edit-ambigua",
            "--body",
            "corpo explicito",
            "--body-stdin",
        ])
        .write_stdin("corpo stdin")
        .assert()
        .failure()
        .code(2);
}

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

    cmd(&tmp)
        .args(["edit", "--name", "nao-existe", "--body", "novo corpo"])
        .assert()
        .failure()
        .code(4);
}

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

    let output = cmd(&tmp)
        .args([
            "remember",
            "--name",
            "memoria-conflito",
            "--type",
            "user",
            "--description",
            "desc original",
            "--body",
            "corpo original",
        ])
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();

    let json: serde_json::Value = serde_json::from_slice(&output).unwrap();
    let wrong_updated_at = json["version"].as_i64().unwrap() + 999;

    cmd(&tmp)
        .args([
            "edit",
            "--name",
            "memoria-conflito",
            "--body",
            "novo corpo",
            "--expected-updated-at",
            &wrong_updated_at.to_string(),
        ])
        .assert()
        .failure()
        .code(3);
}

// ---------------------------------------------------------------------------
// history
// ---------------------------------------------------------------------------

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

    cmd(&tmp)
        .args([
            "remember",
            "--name",
            "memoria-historico",
            "--type",
            "user",
            "--description",
            "v1",
            "--body",
            "corpo v1",
        ])
        .assert()
        .success();

    cmd(&tmp)
        .args([
            "remember",
            "--name",
            "memoria-historico",
            "--type",
            "user",
            "--description",
            "v2",
            "--body",
            "corpo v2",
            "--force-merge",
        ])
        .assert()
        .success();

    let output = cmd(&tmp)
        .args(["history", "--name", "memoria-historico"])
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();

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

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

    cmd(&tmp)
        .args(["history", "--name", "nao-existe"])
        .assert()
        .failure()
        .code(4);
}

// ---------------------------------------------------------------------------
// restore
// ---------------------------------------------------------------------------

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

    cmd(&tmp)
        .args([
            "remember",
            "--name",
            "memoria-restore",
            "--type",
            "user",
            "--description",
            "v1",
            "--body",
            "corpo v1",
        ])
        .assert()
        .success();

    cmd(&tmp)
        .args([
            "remember",
            "--name",
            "memoria-restore",
            "--type",
            "user",
            "--description",
            "v2",
            "--body",
            "corpo v2",
            "--force-merge",
        ])
        .assert()
        .success();

    let output = cmd(&tmp)
        .args(["restore", "--name", "memoria-restore", "--version", "1"])
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();

    let json: serde_json::Value = serde_json::from_slice(&output).unwrap();
    assert_eq!(json["restored_from"], 1);
    assert!(json["version"].as_i64().unwrap() >= 3);
}

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

    cmd(&tmp)
        .args([
            "remember",
            "--name",
            "memoria-sem-versao",
            "--type",
            "user",
            "--description",
            "desc",
            "--body",
            "corpo",
        ])
        .assert()
        .success();

    cmd(&tmp)
        .args(["restore", "--name", "memoria-sem-versao", "--version", "99"])
        .assert()
        .failure()
        .code(4);
}

// ---------------------------------------------------------------------------
// forget+purge regression (FTS5 external-content corruption)
// ---------------------------------------------------------------------------

#[test]
fn test_forget_purge_does_not_corrupt_fts_index() {
    // Regression: forget.rs previously executed `DELETE FROM fts_memories WHERE rowid=?`
    // directly, corrupting the FTS5 external-content index. The corruption only appeared
    // when purge ran a physical DELETE on memories triggering trg_fts_ad.
    let tmp = TempDir::new().unwrap();
    init_db(&tmp);

    for i in 0..3 {
        let name = format!("fts-reg-{i}");
        cmd(&tmp)
            .args([
                "remember",
                "--name",
                &name,
                "--type",
                "user",
                "--description",
                "regression",
                "--body",
                &format!("corpo fts regression {i}"),
            ])
            .assert()
            .success();

        cmd(&tmp)
            .args(["forget", "--name", &name])
            .assert()
            .success();

        cmd(&tmp)
            .args(["purge", "--name", &name, "--retention-days", "0", "--yes"])
            .assert()
            .success();
    }

    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["integrity"], "ok",
        "PRAGMA integrity_check DEVE permanecer ok após ciclos forget+purge"
    );
}