Skip to main content

sqlite_graphrag/
cli.rs

1//! CLI argument structs and command surface (clap-based).
2//!
3//! Defines `Cli` and all subcommand enums; contains no business logic.
4
5use crate::commands::*;
6use crate::i18n::{current, Language};
7use clap::{Parser, Subcommand};
8
9/// Returns the maximum simultaneous invocations allowed by the CPU heuristic.
10fn max_concurrency_ceiling() -> usize {
11    std::thread::available_parallelism()
12        .map(|n| n.get() * 2)
13        .unwrap_or(8)
14}
15
16#[derive(Copy, Clone, Debug, PartialEq, Eq, clap::ValueEnum)]
17pub enum GraphExportFormat {
18    Json,
19    Dot,
20    Mermaid,
21    /// Stream one JSON object per entity, then one per edge, then a summary line.
22    Ndjson,
23}
24
25/// v1.0.82 (GAP-003): LLM backend for embedding. Accepts `auto` (default —
26/// detects `codex` or `claude` on the PATH), `codex` (forces codex exec), `claude`
27/// (forces claude -p), or `none` (skips embedding; useful for tests).
28#[derive(Copy, Clone, Debug, PartialEq, Eq, clap::ValueEnum)]
29pub enum LlmBackendChoice {
30    Auto,
31    Claude,
32    Codex,
33    Opencode,
34    OpenRouter,
35    None,
36}
37
38/// v1.0.93: embedding backend selector. Separate from `--llm-backend` which
39/// controls enrichment (entity extraction, body enrichment) via subprocess.
40/// `auto` tries OpenRouter if API key is available, falls back to LLM subprocess.
41/// `openrouter` requires API key (exit 78 if absent).
42/// `llm` forces subprocess (codex/claude/opencode) — legacy behaviour.
43#[derive(Copy, Clone, Debug, PartialEq, Eq, clap::ValueEnum)]
44pub enum EmbeddingBackendChoice {
45    Auto,
46    Openrouter,
47    Llm,
48}
49
50impl EmbeddingBackendChoice {
51    /// v1.0.93: produces a fallback chain that prepends OpenRouter when
52    /// the client is initialised. Falls back to the LLM subprocess chain.
53    pub fn to_chain(self, llm_choice: LlmBackendChoice) -> Vec<crate::embedder::LlmBackendKind> {
54        use crate::embedder::LlmBackendKind;
55        match self {
56            EmbeddingBackendChoice::Openrouter => vec![LlmBackendKind::OpenRouter],
57            EmbeddingBackendChoice::Llm => llm_choice.to_chain(),
58            EmbeddingBackendChoice::Auto => {
59                if crate::embedder::is_openrouter_initialized() {
60                    let mut chain = vec![LlmBackendKind::OpenRouter];
61                    chain.extend(llm_choice.to_chain());
62                    chain
63                } else {
64                    llm_choice.to_chain()
65                }
66            }
67        }
68    }
69}
70
71impl LlmBackendChoice {
72    /// v1.0.82 (GAP-003): converts the CLI choice into an ordered chain
73    /// of backends that `embedder::embed_with_fallback` iterates. The first
74    /// element of the chain is the preferred backend; subsequent elements
75    /// are fallbacks used when the preferred one fails with `LlmBackendError`.
76    ///
77    /// `Auto` produces `[Codex, Claude, None]` — codex is the default since v1.0.76+,
78    /// claude is the fallback if codex fails (OAuth contention, quota), and
79    /// `None` lets `embed_with_fallback` return an empty vector when
80    /// `skip_on_failure` is active.
81    pub fn to_chain(self) -> Vec<crate::embedder::LlmBackendKind> {
82        use crate::embedder::LlmBackendKind;
83        match self {
84            LlmBackendChoice::Codex => vec![LlmBackendKind::Codex, LlmBackendKind::None],
85            LlmBackendChoice::Claude => vec![LlmBackendKind::Claude, LlmBackendKind::None],
86            LlmBackendChoice::Opencode => vec![
87                LlmBackendKind::Opencode,
88                LlmBackendKind::Codex,
89                LlmBackendKind::Claude,
90                LlmBackendKind::None,
91            ],
92            LlmBackendChoice::OpenRouter => vec![
93                LlmBackendKind::OpenRouter,
94                LlmBackendKind::Codex,
95                LlmBackendKind::None,
96            ],
97            LlmBackendChoice::None => vec![LlmBackendKind::None],
98            LlmBackendChoice::Auto => parse_fallback_chain(
99                &std::env::var("SQLITE_GRAPHRAG_LLM_FALLBACK")
100                    .unwrap_or_else(|_| "codex,claude,none".to_string()),
101            ),
102        }
103    }
104}
105
106fn parse_fallback_chain(s: &str) -> Vec<crate::embedder::LlmBackendKind> {
107    use crate::embedder::LlmBackendKind;
108    let mut chain: Vec<LlmBackendKind> = s
109        .split(',')
110        .filter_map(|tok| match tok.trim().to_ascii_lowercase().as_str() {
111            "codex" => Some(LlmBackendKind::Codex),
112            "claude" | "claude-code" => Some(LlmBackendKind::Claude),
113            "opencode" => Some(LlmBackendKind::Opencode),
114            "openrouter" => Some(LlmBackendKind::OpenRouter),
115            "none" => Some(LlmBackendKind::None),
116            _ => {
117                tracing::warn!(
118                    token = tok.trim(),
119                    "unknown backend in --llm-fallback, skipping"
120                );
121                Option::None
122            }
123        })
124        .collect();
125    if chain.is_empty() {
126        chain = vec![
127            LlmBackendKind::Codex,
128            LlmBackendKind::Claude,
129            LlmBackendKind::None,
130        ];
131    }
132    chain
133}
134
135#[derive(Parser)]
136#[command(name = "sqlite-graphrag")]
137#[command(version)]
138#[command(about = "Local GraphRAG memory for LLMs in a single SQLite file")]
139#[command(arg_required_else_help = true)]
140#[command(after_help = "DATABASE PATH (GAP-SG-32):\n  \
141    `--db` is a PER-SUBCOMMAND flag, so it must come AFTER the subcommand:\n    \
142    sqlite-graphrag remember --db ./graphrag.sqlite --name mem --type note ...\n  \
143    Placing it before the subcommand (e.g. `sqlite-graphrag --db x.sqlite remember`) is rejected.\n  \
144    For a position-independent path, set the canonical env var instead:\n    \
145    SQLITE_GRAPHRAG_DB_PATH=./graphrag.sqlite sqlite-graphrag remember --name mem ...")]
146pub struct Cli {
147    /// Maximum number of simultaneous CLI invocations allowed (default: 4).
148    ///
149    /// Caps the counting semaphore used for CLI concurrency slots. The value must
150    /// stay within [1, 2×nCPUs]. Values above the ceiling are rejected with exit 2.
151    #[arg(long, global = true, value_name = "N")]
152    pub max_concurrency: Option<usize>,
153
154    /// Wait up to SECONDS for a free concurrency slot before giving up (exit 75).
155    ///
156    /// Useful in retrying agent pipelines: the process polls every 500 ms until a
157    /// slot opens or the timeout expires. Default: 300s (5 minutes).
158    #[arg(long, global = true, value_name = "SECONDS")]
159    pub wait_lock: Option<u64>,
160
161    /// Skip the available-memory check before loading the model.
162    ///
163    /// Exclusive use in automated tests where real allocation does not occur.
164    #[arg(long, global = true, hide = true, default_value_t = false)]
165    pub skip_memory_guard: bool,
166
167    /// v1.0.83 (ADR-0041): strict env-clear mode for compliance environments.
168    ///
169    /// When enabled, the LLM subprocess receives ONLY `PATH` — no
170    /// `ANTHROPIC_AUTH_TOKEN`, `ANTHROPIC_BASE_URL`, `OPENAI_BASE_URL`
171    /// or other custom-provider credentials are forwarded. Defaults to
172    /// the standard v1.0.83 whitelist that preserves custom-provider
173    /// credentials (ADR-0041). Honors env var
174    /// `SQLITE_GRAPHRAG_STRICT_ENV_CLEAR=1` when set.
175    #[arg(
176        long,
177        global = true,
178        hide = true,
179        default_value_t = false,
180        value_parser = clap::builder::BoolishValueParser::new(),
181        env = "SQLITE_GRAPHRAG_STRICT_ENV_CLEAR"
182    )]
183    pub strict_env_clear: bool,
184
185    /// v1.0.84 (ADR-0042 / GAP-002): resolve and print the LLM backend that
186    /// WOULD be invoked for embedding (binary path + model + flavour),
187    /// then exit 0 without executing the subprocess. Useful for CI
188    /// audit and sanity-check of `--llm-backend` before long sessions.
189    ///
190    /// Honors env var `SQLITE_GRAPHRAG_DRY_RUN_BACKEND=1` when set.
191    #[arg(
192        long,
193        global = true,
194        hide = true,
195        default_value_t = false,
196        value_parser = clap::builder::BoolishValueParser::new(),
197        env = "SQLITE_GRAPHRAG_DRY_RUN_BACKEND"
198    )]
199    pub dry_run_backend: bool,
200
201    /// Language for human-facing stderr messages. Accepts `en` or `pt`.
202    ///
203    /// Without the flag, detection falls back to `SQLITE_GRAPHRAG_LANG` and then
204    /// `LC_ALL`/`LANG`. JSON stdout stays deterministic and identical across
205    /// languages; only human-facing strings are affected.
206    #[arg(long, global = true, value_enum, value_name = "LANG")]
207    pub lang: Option<crate::i18n::Language>,
208
209    /// Time zone for `*_iso` fields in JSON output (for example `America/Sao_Paulo`).
210    ///
211    /// Accepts any IANA time zone name. Without the flag, it falls back to
212    /// `SQLITE_GRAPHRAG_DISPLAY_TZ`; if unset, UTC is used. Integer epoch fields
213    /// are not affected.
214    #[arg(long, global = true, value_name = "IANA")]
215    pub tz: Option<chrono_tz::Tz>,
216
217    /// Increase logging verbosity (-v=info, -vv=debug, -vvv=trace).
218    ///
219    /// Overrides `SQLITE_GRAPHRAG_LOG_LEVEL` env var when present. Logs are emitted
220    /// to stderr; JSON stdout is unaffected.
221    #[arg(short = 'v', long, global = true, action = clap::ArgAction::Count)]
222    pub verbose: u8,
223
224    /// v1.0.75 (G21 solution): extraction backend selector. Accepts
225    /// `llm` (default), `embedding` (legacy), `none`, or `both` (composite).
226    /// The `llm` backend invokes claude code / codex CLI headless to extract
227    /// entities and relationships; `embedding` is a permanent stub since
228    /// v1.0.79 (legacy fastembed pipeline removed) that returns a clear
229    /// migration error.
230    #[arg(long, global = true, value_name = "KIND", default_value = "llm")]
231    pub extraction_backend: Option<String>,
232
233    /// v1.0.79 (G42/S1): embedding dimensionality override (default 64).
234    ///
235    /// Precedence: this flag > `SQLITE_GRAPHRAG_EMBEDDING_DIM` env var >
236    /// the `dim` recorded in the database `schema_meta` > 64. Existing
237    /// databases keep their recorded dimensionality automatically; use
238    /// this flag only to migrate a corpus to a new dimensionality
239    /// (followed by `enrich --operation re-embed`). Range: [8, 4096].
240    #[arg(long, global = true, value_name = "N", value_parser = clap::value_parser!(u64).range(8..=4096))]
241    pub embedding_dim: Option<u64>,
242
243    /// v1.0.82 (GAP-003) / v1.0.84 (ADR-0042): LLM backend for embedding.
244    /// Accepts `auto` (detects via PATH, codex-first), `codex` (forces
245    /// `codex exec`), `claude` (forces `claude -p`; since v1.0.84 does NOT fall back to
246    /// codex — emits `AppError::Validation` if `claude` is absent),
247    /// `opencode` (forces `opencode run`), or `none`
248    /// (skips embedding; useful for tests). Honors the env var
249    /// `SQLITE_GRAPHRAG_LLM_BACKEND`.
250    #[arg(long, global = true, value_enum, default_value_t = LlmBackendChoice::Auto, env = "SQLITE_GRAPHRAG_LLM_BACKEND")]
251    pub llm_backend: LlmBackendChoice,
252
253    /// v1.0.82 (GAP-003): model to invoke on the chosen backend.
254    /// Honors the env var `SQLITE_GRAPHRAG_LLM_MODEL`. The default depends
255    /// on the backend (codex: `gpt-5.5`; claude: `claude-sonnet-4-6`).
256    #[arg(
257        long,
258        global = true,
259        value_name = "MODEL",
260        env = "SQLITE_GRAPHRAG_LLM_MODEL"
261    )]
262    pub llm_model: Option<String>,
263
264    /// v1.0.82 (GAP-003): path to the `claude` binary (overrides
265    /// PATH detection). Honors the env var `SQLITE_GRAPHRAG_CLAUDE_BINARY`.
266    #[arg(
267        long,
268        global = true,
269        value_name = "PATH",
270        env = "SQLITE_GRAPHRAG_CLAUDE_BINARY"
271    )]
272    pub claude_binary: Option<std::path::PathBuf>,
273
274    /// v1.0.89 (GAP-1): path to the `codex` binary (overrides
275    /// PATH detection). Honors the env var `SQLITE_GRAPHRAG_CODEX_BINARY`.
276    #[arg(
277        long,
278        global = true,
279        value_name = "PATH",
280        env = "SQLITE_GRAPHRAG_CODEX_BINARY"
281    )]
282    pub codex_binary: Option<std::path::PathBuf>,
283
284    /// v1.0.90 (GAP-OPENCODE-001): path to the `opencode` binary (overrides
285    /// PATH detection). Honors the env var `SQLITE_GRAPHRAG_OPENCODE_BINARY`.
286    #[arg(
287        long,
288        global = true,
289        value_name = "PATH",
290        env = "SQLITE_GRAPHRAG_OPENCODE_BINARY"
291    )]
292    pub opencode_binary: Option<std::path::PathBuf>,
293
294    /// v1.0.82 (GAP-005): chain of LLM backends tried in order
295    /// when the primary fails. Default `codex,claude,none`. Honors
296    /// the env var `SQLITE_GRAPHRAG_LLM_FALLBACK`.
297    #[arg(
298        long,
299        global = true,
300        default_value = "codex,claude,none",
301        env = "SQLITE_GRAPHRAG_LLM_FALLBACK"
302    )]
303    pub llm_fallback: String,
304
305    /// v1.0.82 (GAP-005): persists with a NULL embedding when all
306    /// backends in the chain fail. The memory stays in `pending_embeddings`
307    /// for reprocessing via `embedding retry`. Honors the env var
308    /// `SQLITE_GRAPHRAG_SKIP_EMBEDDING_ON_FAILURE`.
309    #[arg(
310        long,
311        global = true,
312        default_value_t = false,
313        value_parser = clap::builder::BoolishValueParser::new(),
314        env = "SQLITE_GRAPHRAG_SKIP_EMBEDDING_ON_FAILURE"
315    )]
316    pub skip_embedding_on_failure: bool,
317
318    /// v1.0.82 (GAP-004): host-wide limit of concurrent LLM
319    /// subprocesses. Default derived from `ncpus`. Honors the env var
320    /// `SQLITE_GRAPHRAG_LLM_MAX_HOST_CONCURRENCY`.
321    #[arg(
322        long,
323        global = true,
324        value_name = "N",
325        env = "SQLITE_GRAPHRAG_LLM_MAX_HOST_CONCURRENCY"
326    )]
327    pub llm_max_host_concurrency: Option<u32>,
328
329    /// v1.0.82 (GAP-004): seconds to wait for a free LLM slot
330    /// before failing with exit 75. Default 30s. Honors the env var
331    /// `SQLITE_GRAPHRAG_LLM_SLOT_WAIT_SECS`.
332    #[arg(
333        long,
334        global = true,
335        value_name = "SECONDS",
336        env = "SQLITE_GRAPHRAG_LLM_SLOT_WAIT_SECS"
337    )]
338    pub llm_slot_wait_secs: Option<u64>,
339
340    /// v1.0.82 (GAP-004): if set, fails immediately (exit 75)
341    /// when no LLM slot is free. Honors the env var
342    /// `SQLITE_GRAPHRAG_LLM_SLOT_NO_WAIT`.
343    #[arg(
344        long,
345        global = true,
346        default_value_t = false,
347        value_parser = clap::builder::BoolishValueParser::new(),
348        env = "SQLITE_GRAPHRAG_LLM_SLOT_NO_WAIT"
349    )]
350    pub llm_slot_no_wait: bool,
351
352    /// v1.0.93: embedding backend selector. `auto` tries OpenRouter API if key
353    /// available, falls back to LLM subprocess. `openrouter` requires API key.
354    /// `llm` forces subprocess. Honra env var `SQLITE_GRAPHRAG_EMBEDDING_BACKEND`.
355    #[arg(long, global = true, value_enum, default_value_t = EmbeddingBackendChoice::Auto, env = "SQLITE_GRAPHRAG_EMBEDDING_BACKEND")]
356    pub embedding_backend: EmbeddingBackendChoice,
357
358    /// v1.0.93: embedding model for the OpenRouter API. Required when
359    /// `--embedding-backend openrouter`. Honors env var `SQLITE_GRAPHRAG_EMBEDDING_MODEL`.
360    #[arg(
361        long,
362        global = true,
363        value_name = "MODEL",
364        env = "SQLITE_GRAPHRAG_EMBEDDING_MODEL"
365    )]
366    pub embedding_model: Option<String>,
367
368    /// v1.0.93: OpenRouter API key (prefer env var or config.toml over CLI flag
369    /// to avoid shell history exposure). Honra env var `OPENROUTER_API_KEY`.
370    #[arg(
371        long,
372        global = true,
373        value_name = "KEY",
374        hide = true,
375        env = "OPENROUTER_API_KEY",
376        hide_env_values = true
377    )]
378    pub openrouter_api_key: Option<String>,
379
380    #[command(subcommand)]
381    pub command: Option<Commands>,
382}
383
384#[cfg(test)]
385mod json_only_format_tests {
386    use super::Cli;
387    use clap::Parser;
388
389    #[test]
390    fn restore_accepts_only_format_json() {
391        assert!(Cli::try_parse_from([
392            "sqlite-graphrag",
393            "restore",
394            "--name",
395            "mem",
396            "--version",
397            "1",
398            "--format",
399            "json",
400        ])
401        .is_ok());
402
403        assert!(Cli::try_parse_from([
404            "sqlite-graphrag",
405            "restore",
406            "--name",
407            "mem",
408            "--version",
409            "1",
410            "--format",
411            "text",
412        ])
413        .is_err());
414    }
415
416    #[test]
417    fn hybrid_search_accepts_only_format_json() {
418        assert!(Cli::try_parse_from([
419            "sqlite-graphrag",
420            "hybrid-search",
421            "query",
422            "--format",
423            "json",
424        ])
425        .is_ok());
426
427        assert!(Cli::try_parse_from([
428            "sqlite-graphrag",
429            "hybrid-search",
430            "query",
431            "--format",
432            "markdown",
433        ])
434        .is_err());
435    }
436
437    #[test]
438    fn remember_recall_rename_vacuum_json_only() {
439        assert!(Cli::try_parse_from([
440            "sqlite-graphrag",
441            "remember",
442            "--name",
443            "mem",
444            "--type",
445            "project",
446            "--description",
447            "desc",
448            "--format",
449            "json",
450        ])
451        .is_ok());
452        assert!(Cli::try_parse_from([
453            "sqlite-graphrag",
454            "remember",
455            "--name",
456            "mem",
457            "--type",
458            "project",
459            "--description",
460            "desc",
461            "--format",
462            "text",
463        ])
464        .is_err());
465
466        assert!(
467            Cli::try_parse_from(["sqlite-graphrag", "recall", "query", "--format", "json",])
468                .is_ok()
469        );
470        assert!(
471            Cli::try_parse_from(["sqlite-graphrag", "recall", "query", "--format", "text",])
472                .is_err()
473        );
474
475        assert!(Cli::try_parse_from([
476            "sqlite-graphrag",
477            "rename",
478            "--name",
479            "old",
480            "--new-name",
481            "new",
482            "--format",
483            "json",
484        ])
485        .is_ok());
486        assert!(Cli::try_parse_from([
487            "sqlite-graphrag",
488            "rename",
489            "--name",
490            "old",
491            "--new-name",
492            "new",
493            "--format",
494            "markdown",
495        ])
496        .is_err());
497
498        assert!(Cli::try_parse_from(["sqlite-graphrag", "vacuum", "--format", "json",]).is_ok());
499        assert!(Cli::try_parse_from(["sqlite-graphrag", "vacuum", "--format", "text",]).is_err());
500    }
501}
502
503impl Cli {
504    /// Validates concurrency flags and returns a localised descriptive error if invalid.
505    ///
506    /// Requires that `crate::i18n::init()` has already been called (happens before this
507    /// function in the `main` flow). In English it emits EN messages; in Portuguese it emits PT.
508    pub fn validate_flags(&self) -> Result<(), String> {
509        if let Some(n) = self.max_concurrency {
510            if n == 0 {
511                return Err(match current() {
512                    Language::English => "--max-concurrency must be >= 1".to_string(),
513                    Language::Portuguese => "--max-concurrency deve ser >= 1".to_string(),
514                });
515            }
516            let teto = max_concurrency_ceiling();
517            if n > teto {
518                return Err(match current() {
519                    Language::English => format!(
520                        "--max-concurrency {n} exceeds the ceiling of {teto} (2×nCPUs) on this system"
521                    ),
522                    Language::Portuguese => format!(
523                        "--max-concurrency {n} excede o teto de {teto} (2×nCPUs) neste sistema"
524                    ),
525                });
526            }
527        }
528        Ok(())
529    }
530}
531
532impl Commands {
533    /// Returns true for subcommands that load the ONNX model locally.
534    pub fn is_embedding_heavy(&self) -> bool {
535        matches!(
536            self,
537            Self::Init(_)
538                | Self::Remember(_)
539                | Self::RememberBatch(_)
540                | Self::Recall(_)
541                | Self::HybridSearch(_)
542                | Self::DeepResearch(_)
543        )
544    }
545
546    pub fn uses_cli_slot(&self) -> bool {
547        true
548    }
549
550    /// Read-only / no-embedding subcommands that MUST run without an embedding
551    /// API key. `init` warms a best-effort smoke test internally and degrades to
552    /// `ok_no_embedding` when the backend is unreachable; the `enrich` queue
553    /// inspectors (`--status` / `--list-dead` / `--requeue-dead` /
554    /// `--prune-dead-orphans`) never embed and never call the LLM. The eager
555    /// OpenRouter key preflight in `main` must skip its hard-fail for these.
556    pub fn tolerates_missing_embedding_key(&self) -> bool {
557        match self {
558            Self::Init(_) => true,
559            Self::Enrich(args) => {
560                args.status
561                    || args.list_dead
562                    || args.requeue_dead
563                    || args.prune_dead_orphans
564                    || args.prune_dead_entity_orphans
565            }
566            _ => false,
567        }
568    }
569}
570
571/// GAP-E2E-010 (v1.0.89): `codex-models` accepts `--json` as a no-op so
572/// agents that append `--json` to every subcommand never see clap errors.
573/// The handler in `main.rs` always emits JSON on stdout; this flag is
574/// accepted and ignored for parity with the rest of the CLI surface.
575#[derive(Debug, clap::Args)]
576pub struct CodexModelsArgs {
577    /// No-op; JSON is always emitted on stdout by `codex-models`.
578    #[arg(long, hide = true, help = "No-op; JSON is always emitted on stdout")]
579    pub json: bool,
580}
581
582#[derive(Subcommand)]
583pub enum Commands {
584    /// Initialize database and download embedding model
585    #[command(after_long_help = "EXAMPLES:\n  \
586        # Initialize in current directory (default behavior)\n  \
587        sqlite-graphrag init\n\n  \
588        # Initialize at a specific path\n  \
589        sqlite-graphrag init --db /path/to/graphrag.sqlite\n\n  \
590        # Initialize using SQLITE_GRAPHRAG_HOME env var\n  \
591        SQLITE_GRAPHRAG_HOME=/data sqlite-graphrag init\n\n\
592        NOTES:\n  \
593        - `init` is OPTIONAL: any subsequent CRUD command auto-initializes graphrag.sqlite if missing.\n  \
594        - As a side effect, `init` warms a smoke-test embedding via the LLM-only one-shot pipeline.")]
595    Init(init::InitArgs),
596    /// Save a memory with optional entity graph
597    #[command(after_long_help = "EXAMPLES:\n  \
598        # Inline body\n  \
599        sqlite-graphrag remember --name onboarding --type user --description \"intro\" --body \"hello\"\n\n  \
600        # Body from file\n  \
601        sqlite-graphrag remember --name doc1 --type document --description \"...\" --body-file ./README.md\n\n  \
602        # Body from stdin (pipe)\n  \
603        cat README.md | sqlite-graphrag remember --name doc1 --type document --description \"...\" --body-stdin\n\n  \
604        # Enable automatic URL extraction (URL-regex only since v1.0.79)\n  \
605        sqlite-graphrag remember --name rich --type note --description \"...\" --body \"...\" --enable-ner")]
606    Remember(remember::RememberArgs),
607    /// Batch-create memories from NDJSON stdin (one invocation, one slot)
608    #[command(after_long_help = "EXAMPLES:\n  \
609        # Batch create from NDJSON\n  \
610        cat memories.ndjson | sqlite-graphrag remember-batch --force-merge --json\n\n  \
611        # Atomic batch\n  \
612        cat memories.ndjson | sqlite-graphrag remember-batch --transaction --json")]
613    RememberBatch(remember_batch::RememberBatchArgs),
614    /// Bulk-ingest every file under a directory as separate memories (NDJSON output)
615    Ingest(ingest::IngestArgs),
616    /// Search memories semantically
617    #[command(after_long_help = "EXAMPLES:\n  \
618        # Top 10 semantic matches (default)\n  \
619        sqlite-graphrag recall \"agent memory\"\n\n  \
620        # Top 3 only\n  \
621        sqlite-graphrag recall \"agent memory\" -k 3\n\n  \
622        # Search across all namespaces\n  \
623        sqlite-graphrag recall \"agent memory\" --all-namespaces\n\n  \
624        # Disable graph traversal (vector-only)\n  \
625        sqlite-graphrag recall \"agent memory\" --no-graph")]
626    Recall(recall::RecallArgs),
627    /// Read a memory by exact name
628    Read(read::ReadArgs),
629    /// List memories with filters
630    List(list::ListArgs),
631    /// Soft-delete a memory
632    Forget(forget::ForgetArgs),
633    /// Permanently delete soft-deleted memories
634    Purge(purge::PurgeArgs),
635    /// Rename a memory preserving history
636    Rename(rename::RenameArgs),
637    /// Split an oversized memory body into N child memories (v1.1.03, GAP-V8)
638    SplitBody(split_body::SplitBodyArgs),
639    /// Edit a memory's body or description
640    Edit(edit::EditArgs),
641    /// List all versions of a memory
642    History(history::HistoryArgs),
643    /// Restore a memory to a previous version
644    Restore(restore::RestoreArgs),
645    /// Search using hybrid vector + full-text search
646    #[command(after_long_help = "EXAMPLES:\n  \
647        # Hybrid search combining KNN + FTS5 BM25 with RRF\n  \
648        sqlite-graphrag hybrid-search \"agent memory architecture\"\n\n  \
649        # Custom weights for vector vs full-text components\n  \
650        sqlite-graphrag hybrid-search \"agent\" --weight-vec 0.7 --weight-fts 0.3")]
651    HybridSearch(hybrid_search::HybridSearchArgs),
652    /// Show database health
653    Health(health::HealthArgs),
654    /// Apply pending schema migrations
655    Migrate(migrate::MigrateArgs),
656    /// Resolve namespace precedence for the current invocation
657    NamespaceDetect(namespace_detect::NamespaceDetectArgs),
658    /// Run PRAGMA optimize on the database
659    Optimize(optimize::OptimizeArgs),
660    /// Show database statistics
661    Stats(stats::StatsArgs),
662    /// Create a checkpointed copy safe for file sync
663    SyncSafeCopy(sync_safe_copy::SyncSafeCopyArgs),
664    /// Back up the database using the SQLite Online Backup API
665    Backup(backup::BackupArgs),
666    /// Run VACUUM after checkpointing the WAL
667    Vacuum(vacuum::VacuumArgs),
668    /// Create an explicit relationship between two entities
669    Link(link::LinkArgs),
670    /// Remove a specific relationship between two entities
671    Unlink(unlink::UnlinkArgs),
672    /// Deep parallel multi-hop GraphRAG research
673    #[command(name = "deep-research")]
674    DeepResearch(deep_research::DeepResearchArgs),
675    /// List memories connected via the entity graph
676    Related(related::RelatedArgs),
677    /// Export a graph snapshot in json, dot or mermaid
678    Graph(graph_export::GraphArgs),
679    /// Export memories as NDJSON (one JSON line per memory, plus a summary line)
680    Export(export::ExportArgs),
681    /// FTS5 full-text search index management (rebuild or check)
682    Fts(fts::FtsArgs),
683    /// Vector index maintenance (orphan detection, purge, stats) — G39
684    Vec(vec::VecArgs),
685    /// List codex OAuth models accepted by ChatGPT Pro (G33).
686    ///
687    /// GAP-E2E-010 (v1.0.89): accepts `--json` as a no-op (JSON is always
688    /// emitted on stdout) so the flag never breaks agent pipelines that
689    /// append `--json` to every invocation.
690    #[command(name = "codex-models")]
691    CodexModels(CodexModelsArgs),
692    /// Bulk-delete all relationships of a given type (e.g. mentions)
693    PruneRelations(prune_relations::PruneRelationsArgs),
694    /// Remove NER bindings (memory_entities rows) for an entity or all entities
695    #[command(name = "prune-ner")]
696    PruneNer(prune_ner::PruneNerArgs),
697    /// Inspect and manage cross-process LLM slot semaphore (GAP-004, v1.0.82)
698    Slots(slots::SlotsArgs),
699    /// Inspect and manage the `remember` checkpoint queue (GAP-001, v1.0.82)
700    Pending(pending::PendingArgs),
701    /// Health and per-entry inspection of the pending-embeddings queue (GAP-005, v1.0.82)
702    Embedding(embedding::EmbeddingArgs),
703    /// Batch operations over the pending-embeddings queue (GAP-005, v1.0.82)
704    #[command(name = "pending-embeddings")]
705    PendingEmbeddings(pending_embeddings::PendingEmbeddingsArgs),
706    /// Remove entities that have no memories and no relationships
707    CleanupOrphans(cleanup_orphans::CleanupOrphansArgs),
708    /// List entities linked to a specific memory
709    MemoryEntities(memory_entities::MemoryEntitiesArgs),
710    /// Manage cached resources (embedding models, etc.)
711    Cache(cache::CacheArgs),
712    /// Delete an entity and all its relationships from the graph
713    #[command(name = "delete-entity")]
714    DeleteEntity(delete_entity::DeleteEntityArgs),
715    /// Reclassify one entity or a batch of entities to a new type
716    Reclassify(reclassify::ReclassifyArgs),
717    /// Rename an entity preserving all relationships and memory bindings
718    #[command(name = "rename-entity")]
719    RenameEntity(rename_entity::RenameEntityArgs),
720    /// Merge multiple source entities into a single target entity
721    #[command(name = "merge-entities")]
722    MergeEntities(merge_entities::MergeEntitiesArgs),
723    /// Enrich graph memories and entities using an LLM provider
724    Enrich(enrich::EnrichArgs),
725    /// Reclassify relationship types across the graph using rules or LLM judgment
726    #[command(name = "reclassify-relation")]
727    ReclassifyRelation(reclassify_relation::ReclassifyRelationArgs),
728    /// Normalize entity names (deduplicate, kebab-case, merge near-duplicates)
729    #[command(name = "normalize-entities")]
730    NormalizeEntities(normalize_entities::NormalizeEntitiesArgs),
731    /// Generate shell completions for Bash, Zsh, Fish, PowerShell, or Elvish
732    Completions(completions::CompletionsArgs),
733    #[command(name = "debug-schema", hide = true)]
734    DebugSchema(debug_schema::DebugSchemaArgs),
735    /// Manage API keys and diagnose provider configuration (v1.0.93)
736    Config(config_cmd::ConfigArgs),
737}
738// FIX-1 (v1.0.89): manual `Debug` impl so test panic messages that print
739// `{:?}` on a captured `Commands` variant compile without requiring every
740// contained subcommand arg struct to derive `Debug`. The Debug output is
741// only used in test assertions for diagnostic messages; we emit the variant
742// name only — arg payload is intentionally omitted.
743impl std::fmt::Debug for Commands {
744    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
745        let name = match self {
746            Self::Init(_) => "Init",
747            Self::Health(_) => "Health",
748            Self::Stats(_) => "Stats",
749            Self::List(_) => "List",
750            Self::Read(_) => "Read",
751            Self::Edit(_) => "Edit",
752            Self::Rename(_) => "Rename",
753            Self::SplitBody(_) => "SplitBody",
754            Self::Restore(_) => "Restore",
755            Self::History(_) => "History",
756            Self::Forget(_) => "Forget",
757            Self::Purge(_) => "Purge",
758            Self::Remember(_) => "Remember",
759            Self::RememberBatch(_) => "RememberBatch",
760            Self::Recall(_) => "Recall",
761            Self::HybridSearch(_) => "HybridSearch",
762            Self::Enrich(_) => "Enrich",
763            Self::Ingest(_) => "Ingest",
764            Self::Optimize(_) => "Optimize",
765            Self::Migrate(_) => "Migrate",
766            Self::SyncSafeCopy(_) => "SyncSafeCopy",
767            Self::Backup(_) => "Backup",
768            Self::Vacuum(_) => "Vacuum",
769            Self::Link(_) => "Link",
770            Self::Unlink(_) => "Unlink",
771            Self::DeepResearch(_) => "DeepResearch",
772            Self::Related(_) => "Related",
773            Self::Graph(_) => "Graph",
774            Self::Export(_) => "Export",
775            Self::Fts(_) => "Fts",
776            Self::Vec(_) => "Vec",
777            Self::CodexModels(_) => "CodexModels",
778            Self::PruneRelations(_) => "PruneRelations",
779            Self::PruneNer(_) => "PruneNer",
780            Self::Slots(_) => "Slots",
781            Self::Pending(_) => "Pending",
782            Self::Embedding(_) => "Embedding",
783            Self::PendingEmbeddings(_) => "PendingEmbeddings",
784            Self::CleanupOrphans(_) => "CleanupOrphans",
785            Self::MemoryEntities(_) => "MemoryEntities",
786            Self::Cache(_) => "Cache",
787            Self::DeleteEntity(_) => "DeleteEntity",
788            Self::Reclassify(_) => "Reclassify",
789            Self::RenameEntity(_) => "RenameEntity",
790            Self::ReclassifyRelation(_) => "ReclassifyRelation",
791            Self::NormalizeEntities(_) => "NormalizeEntities",
792            Self::MergeEntities(_) => "MergeEntities",
793            Self::NamespaceDetect(_) => "NamespaceDetect",
794            Self::Completions(_) => "Completions",
795            Self::DebugSchema(_) => "DebugSchema",
796            Self::Config(_) => "Config",
797        };
798        f.write_str(name)
799    }
800}
801
802#[derive(Copy, Clone, Debug, Default, clap::ValueEnum)]
803pub enum MemoryType {
804    User,
805    Feedback,
806    Project,
807    Reference,
808    Decision,
809    Incident,
810    Skill,
811    #[default]
812    Document,
813    Note,
814}
815
816#[cfg(test)]
817mod heavy_concurrency_tests {
818    use super::*;
819
820    #[test]
821    fn command_heavy_detects_init_and_embeddings() {
822        let init = Cli::try_parse_from(["sqlite-graphrag", "init"]).expect("parse init");
823        assert!(init
824            .command
825            .as_ref()
826            .is_some_and(|c| c.is_embedding_heavy()));
827
828        let remember = Cli::try_parse_from([
829            "sqlite-graphrag",
830            "remember",
831            "--name",
832            "test-memory",
833            "--type",
834            "project",
835            "--description",
836            "desc",
837        ])
838        .expect("parse remember");
839        assert!(remember
840            .command
841            .as_ref()
842            .is_some_and(|c| c.is_embedding_heavy()));
843
844        let recall =
845            Cli::try_parse_from(["sqlite-graphrag", "recall", "query"]).expect("parse recall");
846        assert!(recall
847            .command
848            .as_ref()
849            .is_some_and(|c| c.is_embedding_heavy()));
850
851        let hybrid = Cli::try_parse_from(["sqlite-graphrag", "hybrid-search", "query"])
852            .expect("parse hybrid");
853        assert!(hybrid
854            .command
855            .as_ref()
856            .is_some_and(|c| c.is_embedding_heavy()));
857    }
858
859    #[test]
860    fn command_light_does_not_mark_stats() {
861        let stats = Cli::try_parse_from(["sqlite-graphrag", "stats"]).expect("parse stats");
862        assert!(!stats
863            .command
864            .as_ref()
865            .is_some_and(|c| c.is_embedding_heavy()));
866    }
867}
868
869impl MemoryType {
870    pub fn as_str(&self) -> &'static str {
871        match self {
872            Self::User => "user",
873            Self::Feedback => "feedback",
874            Self::Project => "project",
875            Self::Reference => "reference",
876            Self::Decision => "decision",
877            Self::Incident => "incident",
878            Self::Skill => "skill",
879            Self::Document => "document",
880            Self::Note => "note",
881        }
882    }
883}
884
885/// GAP-SG-31/33/34/35/30: parse-time contracts for the Fase G clap fixes.
886#[cfg(test)]
887mod fase_g_parsing_tests {
888    use super::Cli;
889    use clap::Parser;
890
891    /// GAP-SG-31(b): `enrich --status` parses without --operation/--mode.
892    #[test]
893    fn enrich_status_optional_operation_and_mode() {
894        assert!(
895            Cli::try_parse_from(["sqlite-graphrag", "enrich", "--status"]).is_ok(),
896            "--status alone must not require --operation/--mode"
897        );
898        assert!(
899            Cli::try_parse_from(["sqlite-graphrag", "enrich", "--list-dead"]).is_ok(),
900            "--list-dead is read-only and must not require --operation/--mode"
901        );
902        // Write path still requires both: bare `enrich` is rejected.
903        assert!(
904            Cli::try_parse_from(["sqlite-graphrag", "enrich"]).is_err(),
905            "bare enrich (no status/list-dead/requeue-dead) must require --operation/--mode"
906        );
907        // Full write invocation still parses.
908        assert!(Cli::try_parse_from([
909            "sqlite-graphrag",
910            "enrich",
911            "--operation",
912            "memory-bindings",
913            "--mode",
914            "openrouter",
915        ])
916        .is_ok());
917    }
918
919    /// GAP-SG-34(c): `config doctor --json` parses (no-op flag accepted).
920    #[test]
921    fn config_doctor_accepts_json() {
922        assert!(Cli::try_parse_from(["sqlite-graphrag", "config", "doctor", "--json"]).is_ok());
923        assert!(Cli::try_parse_from(["sqlite-graphrag", "config", "list-keys", "--json"]).is_ok());
924    }
925
926    /// GAP-SG-33(d): a hyphen-led --description value is accepted, not parsed
927    /// as a flag.
928    #[test]
929    fn remember_description_allows_leading_hyphen() {
930        assert!(Cli::try_parse_from([
931            "sqlite-graphrag",
932            "remember",
933            "--name",
934            "mem",
935            "--type",
936            "note",
937            "--description",
938            "- bullet description",
939        ])
940        .is_ok());
941    }
942
943    /// GAP-SG-35(e): `remember-batch --llm-parallelism N` parses.
944    #[test]
945    fn remember_batch_accepts_llm_parallelism() {
946        assert!(Cli::try_parse_from([
947            "sqlite-graphrag",
948            "remember-batch",
949            "--llm-parallelism",
950            "4"
951        ])
952        .is_ok());
953    }
954
955    /// GAP-SG-30: --graph-file combines with a body source but conflicts with
956    /// the other graph-input flags.
957    #[test]
958    fn remember_graph_file_combines_with_body_but_conflicts_with_graph_stdin() {
959        assert!(
960            Cli::try_parse_from([
961                "sqlite-graphrag",
962                "remember",
963                "--name",
964                "mem",
965                "--type",
966                "note",
967                "--body",
968                "inline body",
969                "--graph-file",
970                "/tmp/graph.json",
971            ])
972            .is_ok(),
973            "--body + --graph-file must coexist"
974        );
975        assert!(
976            Cli::try_parse_from([
977                "sqlite-graphrag",
978                "remember",
979                "--name",
980                "mem",
981                "--type",
982                "note",
983                "--graph-file",
984                "/tmp/graph.json",
985                "--graph-stdin",
986            ])
987            .is_err(),
988            "--graph-file conflicts with --graph-stdin"
989        );
990    }
991}