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
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
use crate::cli_db_noop::DB_NOOP_HELP;
use crate::config::{self, compute_fingerprint, mask_key, ApiKeyEntry};
use crate::errors::AppError;
use clap::{Args, Subcommand};
use serde_json::json;
use std::io::{self, Read};

/// Config args.
#[derive(Debug, Args)]
pub struct ConfigArgs {
    /// Action.
    #[command(subcommand)]
    pub action: ConfigAction,
}

/// Config action.
#[derive(Debug, Subcommand)]
pub enum ConfigAction {
    /// Add an API key for a provider (reads from stdin to avoid shell history).
    AddKey {
        /// Provider name.
        #[arg(long)]
        provider: String,
        /// From stdin.
        #[arg(long, default_value_t = true)]
        from_stdin: bool,
        /// GAP-SG-34: no-op; JSON is always emitted on stdout.
        #[arg(long, hide = true)]
        json: bool,
        /// GAP-SG-139: accepted as a no-op for agent uniformity (XDG keys; no graph I/O).
        #[arg(long, value_name = "PATH", help = DB_NOOP_HELP)]
        db: Option<String>,
    },
    /// List stored API keys (masked) with fingerprints.
    ListKeys {
        /// GAP-SG-34: no-op; JSON is always emitted on stdout.
        #[arg(long, hide = true)]
        json: bool,
        /// GAP-SG-139: accepted as a no-op for agent uniformity (XDG keys; no graph I/O).
        #[arg(long, value_name = "PATH", help = DB_NOOP_HELP)]
        db: Option<String>,
    },
    /// Remove an API key by its fingerprint.
    RemoveKey {
        /// Fingerprint.
        fingerprint: String,
        /// GAP-SG-34: no-op; JSON is always emitted on stdout.
        #[arg(long, hide = true)]
        json: bool,
        /// GAP-SG-139: accepted as a no-op for agent uniformity (XDG keys; no graph I/O).
        #[arg(long, value_name = "PATH", help = DB_NOOP_HELP)]
        db: Option<String>,
    },
    /// Diagnose key resolution layers (flag/cli and XDG config; product env deprecated).
    Doctor {
        /// GAP-SG-34: no-op; JSON is always emitted on stdout.
        #[arg(long, hide = true)]
        json: bool,
        /// GAP-SG-139: accepted as a no-op for agent uniformity (XDG keys; no graph I/O).
        #[arg(long, value_name = "PATH", help = DB_NOOP_HELP)]
        db: Option<String>,
    },
    /// Print the resolved XDG config file path.
    Path {
        /// GAP-SG-34: no-op; JSON is always emitted on stdout.
        #[arg(long, hide = true)]
        json: bool,
        /// GAP-SG-139: accepted as a no-op for agent uniformity (XDG keys; no graph I/O).
        #[arg(long, value_name = "PATH", help = DB_NOOP_HELP)]
        db: Option<String>,
    },
    /// Set an operational setting in XDG config (G-T-XDG-01).
    ///
    /// Known keys (non-exhaustive):
    /// `enrich.preserve_threshold`, `enrich.entity_description.domain`,
    /// `enrich.entity_description.grounding_threshold`,
    /// `enrich.entity_connect.default_limit`,
    /// `enrich.entity_connect.max_runtime_secs`,
    /// `network.openrouter.chat_url` (alias `network.chat_url`),
    /// `network.openrouter.embeddings_url` (alias `network.embed_url`),
    /// `log.level`, `log.format`, `display.tz`,
    /// `embedding.dim`, `llm.concurrency`.
    Set {
        /// Dotted key name, e.g. `enrich.preserve_threshold`.
        key: String,
        /// Value as string (parsed by consumers).
        value: String,
        /// Emit machine-readable JSON on stdout.
        #[arg(long, hide = true)]
        json: bool,
        /// GAP-SG-139: accepted as a no-op for agent uniformity (XDG settings; no graph I/O).
        #[arg(long, value_name = "PATH", help = DB_NOOP_HELP)]
        db: Option<String>,
    },
    /// Get an operational setting from XDG config.
    Get {
        /// Key.
        key: String,
        /// Emit machine-readable JSON on stdout.
        #[arg(long, hide = true)]
        json: bool,
        /// GAP-SG-139: accepted as a no-op for agent uniformity (XDG settings; no graph I/O).
        #[arg(long, value_name = "PATH", help = DB_NOOP_HELP)]
        db: Option<String>,
    },
    /// List all operational settings (no secrets).
    List {
        /// Include well-known defaults even when not stored in XDG.
        #[arg(long, default_value_t = false)]
        effective: bool,
        /// Emit machine-readable JSON on stdout.
        #[arg(long, hide = true)]
        json: bool,
        /// Emit the JSON Schema for `config list` stdout and exit 0
        /// without reading settings (agent-native R-AN-01).
        #[arg(
            long,
            default_value_t = false,
            help = "Print JSON Schema for config list output and exit"
        )]
        print_schema: bool,
        /// GAP-SG-139: accepted as a no-op for agent uniformity (XDG settings; no graph I/O).
        #[arg(long, value_name = "PATH", help = DB_NOOP_HELP)]
        db: Option<String>,
    },
    /// Unset an operational setting.
    Unset {
        /// Key.
        key: String,
        /// Emit machine-readable JSON on stdout.
        #[arg(long, hide = true)]
        json: bool,
        /// GAP-SG-139: accepted as a no-op for agent uniformity (XDG settings; no graph I/O).
        #[arg(long, value_name = "PATH", help = DB_NOOP_HELP)]
        db: Option<String>,
    },
}

/// Run.
pub fn run(args: ConfigArgs) -> Result<(), AppError> {
    match args.action {
        ConfigAction::AddKey {
            provider,
            from_stdin,
            json: _,
            db: _,
        } => {
            let key = if from_stdin {
                let mut buf = String::new();
                io::stdin().read_to_string(&mut buf).map_err(AppError::Io)?;
                buf.trim().to_string()
            } else {
                return Err(AppError::Validation(
                    "--from-stdin is required to avoid shell history exposure".into(),
                ));
            };
            if key.is_empty() {
                return Err(AppError::Validation(
                    crate::i18n::validation::api_key_cannot_be_empty(),
                ));
            }
            let fingerprint = compute_fingerprint(&key);
            let entry = ApiKeyEntry {
                provider: provider.clone(),
                value: key,
                added_at: chrono::Utc::now().to_rfc3339(),
                fingerprint: fingerprint.clone(),
            };
            let mut cfg = config::load_config()?;
            cfg.keys.retain(|k| k.provider != provider);
            cfg.keys.push(entry);
            config::save_config(&cfg)?;
            let output = json!({
                "action": "key_added",
                "provider": provider,
                "fingerprint": fingerprint,
            });
            println!("{}", serde_json::to_string(&output)?);
            Ok(())
        }
        ConfigAction::ListKeys { json: _, db: _ } => {
            let cfg = config::load_config()?;
            let keys: Vec<_> = cfg
                .keys
                .iter()
                .map(|k| {
                    json!({
                        "provider": k.provider,
                        "fingerprint": k.fingerprint,
                        "masked_value": mask_key(&k.value),
                        "added_at": k.added_at,
                    })
                })
                .collect();
            let output = json!({ "keys": keys });
            println!("{}", serde_json::to_string_pretty(&output)?);
            Ok(())
        }
        ConfigAction::RemoveKey {
            fingerprint,
            json: _,
            db: _,
        } => {
            let mut cfg = config::load_config()?;
            let before = cfg.keys.len();
            cfg.keys.retain(|k| k.fingerprint != fingerprint);
            if cfg.keys.len() == before {
                return Err(AppError::NotFound(format!(
                    "no key with fingerprint {fingerprint}"
                )));
            }
            config::save_config(&cfg)?;
            let output = json!({
                "action": "key_removed",
                "fingerprint": fingerprint,
            });
            println!("{}", serde_json::to_string(&output)?);
            Ok(())
        }
        ConfigAction::Doctor { json: _, db: _ } => {
            let config_path = config::config_file_path()
                .map(|p| p.display().to_string())
                .unwrap_or_else(|_| "unavailable".to_string());
            let config_exists = std::path::Path::new(&config_path).exists();
            let providers = ["openrouter"];
            let mut results = vec![];
            for provider in &providers {
                let resolved = config::resolve_api_key(provider, None);
                results.push(json!({
                    "provider": provider,
                    "resolved": resolved.is_some(),
                    "source": resolved.as_ref().map(|r| r.source),
                    "masked_value": resolved.as_ref().map(|r| {
                        use secrecy::ExposeSecret;
                        mask_key(r.value.expose_secret())
                    }),
                }));
            }
            // Operational knobs with source layer (flag|xdg|default|derived).
            // Product env is never a source.
            //
            // GAP-SG-85: this listing used to be a hand-written table of 14
            // entries next to a 44-key registry, and `db.path` — the key that
            // redirects the whole database — was one of the missing ones. The
            // list is now DERIVED from `config::SETTING_KEYS`, so a key cannot
            // exist without being discoverable here.
            let rt = crate::runtime_config::get();

            // Only these keys can be overridden by a CLI flag today. Mapping
            // them explicitly keeps the `flag` source honest instead of
            // claiming a flag layer that does not exist for the other keys.
            let flag_for = |key: &str| -> Option<&str> {
                match key {
                    "display.tz" => rt.display_tz.as_deref(),
                    "i18n.lang" => rt.lang.as_deref(),
                    "log.level" => rt.log_level.as_deref(),
                    "log.format" => rt.log_format.as_deref(),
                    "llm.claude_binary" => rt.claude_binary.as_deref(),
                    "llm.codex_binary" => rt.codex_binary.as_deref(),
                    "llm.opencode_binary" => rt.opencode_binary.as_deref(),
                    "llm.model" => rt.llm_model.as_deref(),
                    "llm.fallback" => rt.llm_fallback.as_deref(),
                    "db.path" => rt.db_path.as_deref(),
                    _ => None,
                }
            };

            let knobs: Vec<_> = config::SETTING_KEYS
                .iter()
                .map(|entry| {
                    let runtime_flag = flag_for(entry.key).filter(|v| !v.is_empty());
                    let xdg_value = config::get_setting(entry.key)
                        .ok()
                        .flatten()
                        .filter(|v| !v.is_empty());
                    let (source, value) = match (runtime_flag, xdg_value) {
                        (Some(v), _) => ("flag", Some(v.to_string())),
                        (None, Some(v)) => ("xdg", Some(v)),
                        // A key whose default is derived from the host has no
                        // literal to report; `derived` says so instead of
                        // printing an empty string that reads like "unset".
                        (None, None) => match entry.default {
                            Some(d) => ("default", Some(d.to_string())),
                            None => ("derived", None),
                        },
                    };
                    json!({ "key": entry.key, "value": value, "source": source })
                })
                .collect();
            let output = json!({
                "config_path": config_path,
                "config_exists": config_exists,
                "providers": results,
                "knobs": knobs,
                "product_env_reads": false,
                "note": "Precedence: CLI flag > XDG config set > named default. No SQLITE_GRAPHRAG_* product env.",
            });
            println!("{}", serde_json::to_string_pretty(&output)?);
            Ok(())
        }
        ConfigAction::Path { json: _, db: _ } => {
            let path = config::config_file_path()?;
            let output = json!({
                "config_path": path.display().to_string(),
                "exists": path.exists(),
            });
            println!("{}", serde_json::to_string(&output)?);
            Ok(())
        }
        ConfigAction::Set {
            key,
            value,
            json: _,
            db: _,
        } => {
            config::set_setting(&key, &value)?;
            let output = json!({
                "action": "setting_set",
                "key": key,
                "value": value,
            });
            println!("{}", serde_json::to_string(&output)?);
            Ok(())
        }
        ConfigAction::Get {
            key,
            json: _,
            db: _,
        } => {
            let value = config::get_setting(&key)?;
            let output = json!({
                "key": key,
                "value": value,
                "found": value.is_some(),
            });
            println!("{}", serde_json::to_string(&output)?);
            Ok(())
        }
        ConfigAction::List {
            effective,
            json: _,
            print_schema,
            db: _,
        } => {
            if print_schema {
                return crate::print_schema::emit(crate::print_schema::SchemaId::ConfigList);
            }
            let mut settings = config::list_settings()?;
            if effective {
                // GAP-SG-93: defaults MUST come from constants / named defaults,
                // never hard-coded literals that can drift from DEFAULT_EMBEDDING_DIM.
                let dim_default = crate::constants::DEFAULT_EMBEDDING_DIM.to_string();
                let probe_default = crate::constants::DEFAULT_LLM_PROBE_TIMEOUT_MS.to_string();
                let defaults: &[(&str, &str)] = &[
                    (
                        "network.openrouter.chat_url",
                        crate::constants::DEFAULT_OPENROUTER_CHAT_URL,
                    ),
                    (
                        "network.openrouter.embeddings_url",
                        crate::constants::DEFAULT_OPENROUTER_EMBEDDINGS_URL,
                    ),
                    ("llm.probe_timeout_ms", probe_default.as_str()),
                    ("llm.fallback", "codex,claude,none"),
                    ("embedding.dim", dim_default.as_str()),
                    ("log.level", crate::constants::DEFAULT_LOG_LEVEL),
                    ("display.tz", "UTC"),
                ];
                for (k, v) in defaults {
                    settings
                        .entry(k.to_string())
                        .or_insert_with(|| v.to_string());
                }
            }
            let output = json!({
                "settings": settings,
                "effective": effective,
            });
            println!("{}", serde_json::to_string_pretty(&output)?);
            Ok(())
        }
        ConfigAction::Unset {
            key,
            json: _,
            db: _,
        } => {
            let removed = config::unset_setting(&key)?;
            let output = json!({
                "action": "setting_unset",
                "key": key,
                "removed": removed,
            });
            println!("{}", serde_json::to_string(&output)?);
            Ok(())
        }
    }
}

#[cfg(test)]
mod tests {
    use clap::Parser;

    #[test]
    fn config_doctor_accepts_db_as_noop() {
        let cli = crate::cli::Cli::try_parse_from([
            "sqlite-graphrag",
            "config",
            "doctor",
            "--db",
            "/tmp/gap-sg-139-sentinel.sqlite",
        ])
        .expect("config doctor must accept --db as a no-op (GAP-SG-139)");

        match cli.command {
            Some(crate::cli::Commands::Config(args)) => match args.action {
                super::ConfigAction::Doctor { db, .. } => {
                    assert_eq!(db.as_deref(), Some("/tmp/gap-sg-139-sentinel.sqlite"));
                }
                other => panic!("expected Doctor, got {other:?}"),
            },
            other => panic!("expected Config, got {other:?}"),
        }
    }

    #[test]
    fn config_list_accepts_db_as_noop() {
        let cli = crate::cli::Cli::try_parse_from([
            "sqlite-graphrag",
            "config",
            "list",
            "--db",
            "/tmp/gap-sg-139-sentinel.sqlite",
        ])
        .expect("config list must accept --db as a no-op (GAP-SG-139)");

        match cli.command {
            Some(crate::cli::Commands::Config(args)) => match args.action {
                super::ConfigAction::List { db, .. } => {
                    assert_eq!(db.as_deref(), Some("/tmp/gap-sg-139-sentinel.sqlite"));
                }
                other => panic!("expected List, got {other:?}"),
            },
            other => panic!("expected Config, got {other:?}"),
        }
    }

    #[test]
    fn config_add_key_accepts_db_as_noop() {
        let cli = crate::cli::Cli::try_parse_from([
            "sqlite-graphrag",
            "config",
            "add-key",
            "--provider",
            "openrouter",
            "--db",
            "/tmp/gap-sg-139-sentinel.sqlite",
        ])
        .expect("config add-key must accept --db as a no-op (GAP-SG-139)");

        match cli.command {
            Some(crate::cli::Commands::Config(args)) => match args.action {
                super::ConfigAction::AddKey { db, .. } => {
                    assert_eq!(db.as_deref(), Some("/tmp/gap-sg-139-sentinel.sqlite"));
                }
                other => panic!("expected AddKey, got {other:?}"),
            },
            other => panic!("expected Config, got {other:?}"),
        }
    }
}