sqlite-graphrag 1.2.1

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
use super::*;
use std::ffi::OsString;

fn dummy_argv() -> Vec<OsString> {
    vec![
        OsString::from("/usr/bin/claude"),
        OsString::from("-p"),
        OsString::from("hello"),
    ]
}

fn dummy_args<'a>(
    binary: &'a Path,
    argv: &'a [OsString],
    inline_json: Option<&'a str>,
) -> PreFlightArgs<'a> {
    // Use a dedicated empty tempdir for workspace_root so walk-up of
    // `.mcp.json` does not pick up unrelated files in the test's CWD.
    // The tempdir is leaked (kept alive for the test lifetime) via
    // `OnceLock` to keep the API simple.
    use std::sync::OnceLock;
    static WORKSPACE: OnceLock<tempfile::TempDir> = OnceLock::new();
    let workspace = WORKSPACE.get_or_init(|| tempfile::tempdir().expect("tempdir"));
    PreFlightArgs {
        binary_path: binary,
        argv,
        workspace_root: workspace.path(),
        mcp_config_inline_json: inline_json,
        expected_output_bytes: 1024,
        spawner_name: "test",
    }
}

#[test]
#[serial_test::serial(env)]
fn check_binary_exists_passes_when_path_valid() {
    // SAFETY: serial_test::serial(env) ensures no parallel mutation.
    let saved = std::env::var_os("CLAUDE_CONFIG_DIR");
    unsafe {
        std::env::remove_var("CLAUDE_CONFIG_DIR");
    }
    let binary = if cfg!(windows) {
        "C:\\Windows\\System32\\cmd.exe"
    } else {
        "/bin/sh"
    };
    let argv = dummy_argv();
    let args = dummy_args(Path::new(binary), &argv, None);
    let result = preflight_check(&args);
    if let Some(v) = saved {
        unsafe {
            std::env::set_var("CLAUDE_CONFIG_DIR", v);
        }
    }
    assert!(result.is_ok(), "preflight returned: {result:?}");
}

#[test]
fn check_binary_exists_fails_when_missing() {
    let argv = dummy_argv();
    let args = dummy_args(Path::new("/does/not/exist/claude-binary"), &argv, None);
    let err = preflight_check(&args).unwrap_err();
    assert!(
        matches!(err, PreFlightError::BinaryNotFound { .. }),
        "expected BinaryNotFound, got {err:?}"
    );
}

#[test]
#[serial_test::serial(env)]
fn check_argv_size_passes_under_limit() {
    let saved = std::env::var_os("CLAUDE_CONFIG_DIR");
    unsafe {
        std::env::remove_var("CLAUDE_CONFIG_DIR");
    }
    let argv = dummy_argv();
    let args = dummy_args(Path::new("/bin/sh"), &argv, None);
    let result = preflight_check(&args);
    if let Some(v) = saved {
        unsafe {
            std::env::set_var("CLAUDE_CONFIG_DIR", v);
        }
    }
    // dummy_argv() is tiny — well under ARG_MAX.
    assert!(result.is_ok(), "preflight returned: {result:?}");
}

#[test]
#[serial_test::serial(env)]
fn check_argv_size_fails_when_exceeds_arg_max() {
    let saved = std::env::var_os("CLAUDE_CONFIG_DIR");
    unsafe {
        std::env::remove_var("CLAUDE_CONFIG_DIR");
    }
    // Synthesize an argv that exceeds ARG_MAX regardless of the
    // host value. We allocate 64 MiB to leave the 4 KiB safety
    // margin well below `getconf ARG_MAX` on every supported OS.
    let huge = "x".repeat(64 * 1024 * 1024);
    let argv = vec![OsString::from("/bin/sh"), OsString::from(huge)];
    let args = dummy_args(Path::new("/bin/sh"), &argv, None);
    let err = preflight_check(&args).unwrap_err();
    if let Some(v) = saved {
        unsafe {
            std::env::set_var("CLAUDE_CONFIG_DIR", v);
        }
    }
    assert!(
        matches!(err, PreFlightError::ArgvExceedsArgMax { .. }),
        "expected ArgvExceedsArgMax, got {err:?}"
    );
}

#[test]
fn check_mcp_inline_json_detects_literal_braces() {
    // argv references /bin/sh (exists) so the binary check passes.
    let argv = dummy_argv();
    let args = dummy_args(Path::new("/bin/sh"), &argv, Some("{}"));
    let err = preflight_check(&args).unwrap_err();
    assert!(
        matches!(err, PreFlightError::McpConfigInlineJsonRejected(_)),
        "expected McpConfigInlineJsonRejected, got {err:?}"
    );
}

#[test]
fn check_mcp_inline_json_writes_valid_tempfile() {
    // Round-trip: write_empty_mcp_config_tempfile produces a file
    // parseable as JSON containing `mcpServers: {}`.
    let path = write_empty_mcp_config_tempfile().expect("tempfile write");
    let contents = std::fs::read_to_string(&path).expect("tempfile read");
    let parsed: serde_json::Value = serde_json::from_str(&contents).expect("tempfile valid JSON");
    assert!(parsed.get("mcpServers").is_some());
    assert!(parsed["mcpServers"].as_object().unwrap().is_empty());
    // Cleanup
    let _ = std::fs::remove_file(&path);
}

#[test]
fn check_mcp_path_missing_returns_error() {
    // Build an argv with --mcp-config pointing at a nonexistent path.
    let argv = vec![
        OsString::from("/bin/sh"),
        OsString::from("--mcp-config"),
        OsString::from("/nonexistent/path/mcp.json"),
    ];
    let args = dummy_args(Path::new("/bin/sh"), &argv, None);
    let err = preflight_check(&args).unwrap_err();
    assert!(
        matches!(err, PreFlightError::McpConfigPathMissing { .. }),
        "expected McpConfigPathMissing, got {err:?}"
    );
}

#[test]
fn check_mcp_path_invalid_json_returns_error() {
    // Write an invalid JSON tempfile then reference it.
    let tmp = tempfile::NamedTempFile::new().expect("tempfile");
    std::fs::write(tmp.path(), b"this is not json").expect("write");
    let argv = vec![
        OsString::from("/bin/sh"),
        OsString::from("--mcp-config"),
        OsString::from(tmp.path().to_string_lossy().into_owned()),
    ];
    let args = dummy_args(Path::new("/bin/sh"), &argv, None);
    let err = preflight_check(&args).unwrap_err();
    assert!(
        matches!(err, PreFlightError::McpConfigPathInvalidJson { .. }),
        "expected McpConfigPathInvalidJson, got {err:?}"
    );
}

#[test]
fn check_walkup_mcp_json_passes_when_clean() {
    // Use a dedicated tempdir created for the test (guaranteed empty).
    let dir = tempfile::tempdir().expect("tempdir");
    let argv = dummy_argv();
    let args = PreFlightArgs {
        workspace_root: dir.path(),
        ..dummy_args(Path::new("/bin/sh"), &argv, None)
    };
    let result = preflight_check(&args);
    // We only assert we did NOT return WalkUpMcpJsonInvalid for a
    // clean workspace.
    if let Err(PreFlightError::WalkUpMcpJsonInvalid { .. }) = &result {
        panic!("walk-up incorrectly flagged on clean workspace");
    }
}

#[test]
fn check_walkup_mcp_json_fails_on_zod_invalid() {
    // Create a temp workspace dir with an invalid .mcp.json inside.
    let dir = tempfile::tempdir().expect("tempdir");
    let bad = dir.path().join(".mcp.json");
    std::fs::write(&bad, b"{not json").expect("write bad mcp.json");
    let argv = dummy_argv();
    let args = PreFlightArgs {
        workspace_root: dir.path(),
        ..dummy_args(Path::new("/bin/sh"), &argv, None)
    };
    let err = preflight_check(&args).unwrap_err();
    assert!(
        matches!(err, PreFlightError::WalkUpMcpJsonInvalid { .. }),
        "expected WalkUpMcpJsonInvalid, got {err:?}"
    );
}

#[test]
fn check_walkup_mcp_json_fails_on_active_mcp_servers() {
    // BUG-9 regression: a syntactically valid `.mcp.json` that
    // declares MCP servers under `mcpServers` must be rejected.
    let dir = tempfile::tempdir().expect("tempdir");
    let bad = dir.path().join(".mcp.json");
    std::fs::write(
        &bad,
        r#"{"mcpServers":{"github":{"command":"gh","args":["mcp"]}}}"#,
    )
    .expect("write bad mcp.json");
    let argv = dummy_argv();
    let args = PreFlightArgs {
        workspace_root: dir.path(),
        ..dummy_args(Path::new("/bin/sh"), &argv, None)
    };
    let err = preflight_check(&args).unwrap_err();
    assert!(
        matches!(err, PreFlightError::WalkUpMcpJsonInvalid { .. }),
        "expected WalkUpMcpJsonInvalid, got {err:?}"
    );
}

#[test]
fn check_walkup_mcp_json_passes_with_empty_mcp_servers() {
    let dir = tempfile::tempdir().expect("tempdir");
    let ok = dir.path().join(".mcp.json");
    std::fs::write(&ok, r#"{"mcpServers":{}}"#).expect("write");
    let argv = dummy_argv();
    let args = PreFlightArgs {
        workspace_root: dir.path(),
        ..dummy_args(Path::new("/bin/sh"), &argv, None)
    };
    let result = preflight_check(&args);
    if let Err(PreFlightError::WalkUpMcpJsonInvalid { .. }) = &result {
        panic!("empty mcpServers must pass walk-up: {result:?}");
    }
}

#[test]
fn check_mcp_path_equals_form_detects_missing_file() {
    // BUG-5 regression: --mcp-config=PATH single-slot form must be
    // caught the same as the GNU --mcp-config <PATH> form.
    let argv = vec![
        OsString::from("/bin/sh"),
        OsString::from("--mcp-config=/nonexistent/path/mcp.json"),
    ];
    let args = dummy_args(Path::new("/bin/sh"), &argv, None);
    let err = preflight_check(&args).unwrap_err();
    assert!(
        matches!(err, PreFlightError::McpConfigPathMissing { .. }),
        "expected McpConfigPathMissing, got {err:?}"
    );
}

#[test]
fn check_output_buffer_warns_when_oversized() {
    let argv = dummy_argv();
    let args = PreFlightArgs {
        expected_output_bytes: 100_000, // > 65536 cap
        ..dummy_args(Path::new("/bin/sh"), &argv, None)
    };
    let err = preflight_check(&args).unwrap_err();
    assert!(
        matches!(err, PreFlightError::OutputBufferTooSmall { .. }),
        "expected OutputBufferTooSmall, got {err:?}"
    );
}

#[test]
#[serial_test::serial(env)]
fn check_claude_config_dir_fails_when_settings_has_active_mcps() {
    // SAFETY: serial_test::serial(env) ensures no parallel mutation.
    let dir = tempfile::tempdir().expect("tempdir");
    let settings = dir.path().join("settings.json");
    std::fs::write(
        &settings,
        r#"{"mcpServers":{"github":{"command":"gh","args":["mcp"]}}}"#,
    )
    .expect("write settings.json");
    unsafe {
        std::env::set_var("CLAUDE_CONFIG_DIR", dir.path());
    }
    let argv = dummy_argv();
    let args = dummy_args(Path::new("/bin/sh"), &argv, None);
    let err = preflight_check(&args);
    unsafe {
        std::env::remove_var("CLAUDE_CONFIG_DIR");
    }
    if let Err(PreFlightError::ClaudeConfigDirNotEmpty { reason, .. }) = err {
        assert_eq!(reason, "mcpServers");
    } else {
        panic!("expected ClaudeConfigDirNotEmpty mcpServers, got {err:?}");
    }
}

#[test]
#[serial_test::serial(env)]
fn check_claude_config_dir_passes_when_settings_empty() {
    // SAFETY: serial_test::serial(env) ensures no parallel mutation.
    let dir = tempfile::tempdir().expect("tempdir");
    let settings = dir.path().join("settings.json");
    std::fs::write(&settings, r#"{"mcpServers":{},"hooks":{}}"#).expect("write");
    unsafe {
        std::env::set_var("CLAUDE_CONFIG_DIR", dir.path());
    }
    let argv = dummy_argv();
    let args = dummy_args(Path::new("/bin/sh"), &argv, None);
    let result = preflight_check(&args);
    unsafe {
        std::env::remove_var("CLAUDE_CONFIG_DIR");
    }
    assert!(result.is_ok(), "empty MCPs and hooks must pass: {result:?}");
}

#[test]
#[serial_test::serial(env)]
fn check_claude_config_dir_passes_when_no_settings_json() {
    // SAFETY: serial_test::serial(env) ensures no parallel mutation.
    let dir = tempfile::tempdir().expect("tempdir");
    // Populate with non-MCP files only (CLAUDE.md, commands/, etc).
    std::fs::write(dir.path().join("CLAUDE.md"), "# project notes").expect("write");
    unsafe {
        std::env::set_var("CLAUDE_CONFIG_DIR", dir.path());
    }
    let argv = dummy_argv();
    let args = dummy_args(Path::new("/bin/sh"), &argv, None);
    let result = preflight_check(&args);
    unsafe {
        std::env::remove_var("CLAUDE_CONFIG_DIR");
    }
    assert!(
        result.is_ok(),
        "populated dir without settings.json must pass: {result:?}"
    );
}

#[test]
#[serial_test::serial(env)]
fn check_claude_config_dir_passes_when_settings_has_only_hooks() {
    // Hooks are tolerated because the spawners override
    // `--settings '{"hooks":{}}'` at the CLI boundary; only MCP
    // servers are flagged as a hard error.
    let dir = tempfile::tempdir().expect("tempdir");
    let settings = dir.path().join("settings.json");
    std::fs::write(&settings, r#"{"hooks":{"PreToolUse":[]}}"#).expect("write");
    unsafe {
        std::env::set_var("CLAUDE_CONFIG_DIR", dir.path());
    }
    let argv = dummy_argv();
    let args = dummy_args(Path::new("/bin/sh"), &argv, None);
    let result = preflight_check(&args);
    unsafe {
        std::env::remove_var("CLAUDE_CONFIG_DIR");
    }
    assert!(result.is_ok(), "hooks must be tolerated: {result:?}");
}

#[test]
fn preflight_check_runs_all_guards_in_order() {
    // Valid path + clean argv + clean workspace + no inline JSON.
    let dir = tempfile::tempdir().expect("tempdir");
    let argv = dummy_argv();
    let args = PreFlightArgs {
        workspace_root: dir.path(),
        ..dummy_args(Path::new("/bin/sh"), &argv, None)
    };
    assert!(preflight_check(&args).is_ok());
}

#[test]
fn preflight_check_short_circuits_on_first_failure() {
    // Invalid binary + bad inline JSON — should report BinaryNotFound
    // first (cheap in-memory check) NOT the McpConfigInlineJsonRejected
    // (also cheap, but binary is checked earlier in the order).
    let argv = dummy_argv();
    let args = dummy_args(Path::new("/does/not/exist/at/all"), &argv, Some("{}"));
    let err = preflight_check(&args).unwrap_err();
    assert!(
        matches!(err, PreFlightError::BinaryNotFound { .. }),
        "expected BinaryNotFound (short-circuit), got {err:?}"
    );
}

#[test]
#[serial_test::serial(env)]
fn app_error_preflight_failed_has_exit_code_16() {
    // Cross-check the integration: AppError::PreFlightFailed maps to
    // exit code 16 (validated by this test, not by preflight itself).
    use crate::errors::AppError;
    let err: AppError = crate::spawn::preflight::PreFlightError::BinaryNotFound {
        path: "/bin/test".into(),
    }
    .into();
    assert_eq!(err.exit_code(), 16);
    assert!(err.is_permanent());
    assert!(!err.is_retryable());
}