Skip to main content

sqlite_graphrag/commands/enrich/
args.rs

1//! CLI argument types for the `enrich` subcommand.
2//! Extracted from mod.rs (Wave C1).
3
4use serde::{Deserialize, Serialize};
5use std::path::PathBuf;
6
7use super::DEFAULT_BODY_ENRICH_MAX_CHARS;
8use super::DEFAULT_BODY_ENRICH_MIN_CHARS;
9use super::DEFAULT_ENRICH_CIRCUIT_BREAKER_THRESHOLD;
10use super::DEFAULT_ENRICH_GROUNDING_THRESHOLD;
11use super::DEFAULT_ENRICH_MAX_ATTEMPTS;
12use super::DEFAULT_ENRICH_PRESERVE_THRESHOLD;
13use super::DEFAULT_ENRICH_RATE_LIMIT_BUFFER_SECS;
14use super::DEFAULT_ENRICH_STALE_CLAIM_SECS;
15use crate::constants::{DEFAULT_ENRICH_REST_CONCURRENCY, DEFAULT_OPENROUTER_CHAT_TIMEOUT_SECS};
16
17// ---------------------------------------------------------------------------
18// CLI args
19// ---------------------------------------------------------------------------
20
21/// Operation to perform in the `enrich` command.
22#[derive(Debug, Clone, PartialEq, Eq, clap::ValueEnum, Serialize, Deserialize)]
23#[serde(rename_all = "kebab-case")]
24pub enum EnrichOperation {
25    /// Add missing entity/relationship bindings to memories (fully implemented).
26    /// memory-bindings LINKS each memory to the EXISTING entities extracted from
27    /// its body — it does not invent a new graph, it only connects what is missing. Scans
28    /// only UNBOUND memories (those with zero `memory_entities`).
29    MemoryBindings,
30    /// GAP-SG-24/26: additive augmentation — re-run binding extraction over
31    /// memories that are ALREADY bound, filtered by `--names`/`--names-file`, to
32    /// merge newly-discovered entities/relationships WITHOUT removing existing
33    /// links. Requires a name filter (refuses to re-scan the whole namespace).
34    AugmentBindings,
35    /// Fill NULL/empty entity descriptions with LLM-generated summaries (fully implemented).
36    EntityDescriptions,
37    /// Expand short memory bodies into richer content (fully implemented, GAP-18).
38    BodyEnrich,
39    /// Rebuild missing memory embeddings without rewriting the memory body.
40    ReEmbed,
41    /// Calibrate relationship weights using LLM analysis (fully implemented; persists weight).
42    WeightCalibrate,
43    /// Reclassify relationship types using LLM judgment (fully implemented; persists relation).
44    RelationReclassify,
45    /// Connect isolated entities by suggesting and persisting new relationships (fully implemented).
46    EntityConnect,
47    /// Validate entity type assignments using LLM judgment (fully implemented; persists type).
48    EntityTypeValidate,
49    /// Enrich memory descriptions that are generic/auto-generated (fully implemented; persists description).
50    DescriptionEnrich,
51    /// Identify cross-domain bridges between disconnected subgraphs.
52    /// Shares the O(k) pair scan + `entity_connect_seen` drain path with
53    /// `entity-connect` (v1.1.04+ / v1.1.06); status backlog proxy remains 0.
54    CrossDomainBridges,
55    /// Classify memories into domain categories (fully implemented; persists metadata).
56    DomainClassify,
57    /// Audit the graph for quality issues (scan/report; does not mutate graph structure).
58    GraphAudit,
59    /// Synthesize deep-research findings into graph memories (fully implemented when bindings persist).
60    DeepResearchSynth,
61    /// Extract structured body from unstructured text (fully implemented; persists body).
62    BodyExtract,
63}
64
65/// v1.1.1 (P2): which embedding table the `re-embed` operation backfills.
66///
67/// `memories` is the historical behaviour (and the default, so existing
68/// invocations are unchanged); `entities` and `chunks` close the retroactive
69/// coverage gap for `entity_embeddings` / `chunk_embeddings`; `all` runs the
70/// three scans in one invocation.
71#[derive(Debug, Clone, Copy, PartialEq, Eq, clap::ValueEnum, Serialize, Deserialize)]
72#[serde(rename_all = "kebab-case")]
73pub enum ReEmbedTarget {
74    /// Memories without a live vector in `memory_embeddings` (default).
75    Memories,
76    /// Entities without a live vector in `entity_embeddings`.
77    Entities,
78    /// Chunks without a live vector in `chunk_embeddings`.
79    Chunks,
80    /// All three targets in a single run.
81    All,
82}
83
84/// LLM provider for enrichment.
85#[derive(Debug, Clone, PartialEq, Eq, clap::ValueEnum)]
86pub enum EnrichMode {
87    /// Use the OpenRouter chat-completions REST API (no local CLI; v1.0.95).
88    #[value(name = "openrouter")]
89    OpenRouter,
90}
91
92impl std::fmt::Display for EnrichMode {
93    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
94        match self {
95            EnrichMode::OpenRouter => write!(f, "openrouter"),
96        }
97    }
98}
99
100/// Arguments for the `enrich` subcommand.
101#[derive(clap::Args)]
102#[command(
103    about = "Enrich graph memories and entities using an LLM provider",
104    after_long_help = "EXAMPLES:\n  \
105    # Add missing entity bindings to all unbound memories\n  \
106    sqlite-graphrag enrich --operation memory-bindings --mode openrouter \\\n    \
107      --openrouter-model deepseek/deepseek-v4-flash:nitro\n\n  \
108    # Fill entity descriptions (dry-run preview, no tokens spent)\n  \
109    sqlite-graphrag enrich --operation entity-descriptions --dry-run --json\n\n  \
110    # Expand short memory bodies (GAP-18)\n  \
111    sqlite-graphrag enrich --operation body-enrich --min-output-chars 600\n\n  \
112    # Rebuild only missing memory embeddings without rewriting bodies\n  \
113    sqlite-graphrag enrich --operation re-embed --limit 100\n\n  \
114    # Resume an interrupted body-enrich run\n  \
115    sqlite-graphrag enrich --operation body-enrich --resume --json\n\n  \
116    # Retry only failed items from a previous run\n  \
117    sqlite-graphrag enrich --operation memory-bindings --retry-failed --json\n\n  \
118    # Converge the whole backlog (internal scan+drain loop, no bash wrapper)\n  \
119    sqlite-graphrag enrich --operation memory-bindings --mode openrouter \\\n    \
120      --openrouter-model deepseek/deepseek-v4-flash:nitro --until-empty --max-runtime 600\n\n  \
121    # Inspect / resurrect dead-letter items\n  \
122    sqlite-graphrag enrich --operation memory-bindings --list-dead\n  \
123    sqlite-graphrag enrich --operation memory-bindings --requeue-dead\n\n  \
124    # Read-only status (no LLM, no singleton)\n  \
125    sqlite-graphrag enrich --operation memory-bindings --status\n\n\
126    OPERATIONS NOTE:\n  \
127    memory-bindings LINKS each memory to the EXISTING entities extracted from its\n  \
128    body — it does not invent a new graph, it connects what is missing. It scans\n  \
129    only UNBOUND memories. To re-run extraction over ALREADY-bound memories and\n  \
130    MERGE newly-found entities/relationships additively (without removing links),\n  \
131    use --operation augment-bindings with --names/--names-file.\n\n\
132    DEAD-LETTER SIDECAR (.enrich-queue.sqlite):\n  \
133    A SQLite sidecar tracks each work item across runs. Schema (table `queue`):\n  \
134    item_key (UNIQUE name/id), item_type (memory|entity), operation, memory_id,\n  \
135    status (pending|processing|done|skipped|dead), attempt, error, error_class,\n  \
136    next_retry_at (backoff cooldown). --until-empty loops scan→drain internally\n  \
137    until eligible items are exhausted; transient failures (incl. malformed/non-\n  \
138    JSON LLM output, GAP-SG-09) reschedule with backoff until --max-attempts, then\n  \
139    land in status='dead'. Use --status to see the queue, --list-dead to inspect\n  \
140    the sink, --requeue-dead to retry it, and --ignore-backoff to skip cooldowns.\n  \
141    --names/--names-file also remedy a cooldown by targeting a specific subset.\n\n\
142    EXIT CODES:\n  \
143    0  success\n  \
144    1  validation error (bad args, binary not found)\n  \
145    14 I/O error"
146)]
147#[derive(Clone)]
148pub struct EnrichArgs {
149    /// Enrichment operation to run. Required for write operations; optional for
150    /// the read-only queue inspectors (`--status` / `--list-dead` /
151    /// `--requeue-dead`), where it defaults to `memory-bindings` when omitted
152    /// (GAP-SG-31).
153    #[arg(
154        long,
155        short = 'o',
156        value_enum,
157        value_name = "OPERATION",
158        // GAP-CLI-DRY-01: dry-run is read-only (no LLM); still needs --operation.
159        // R-AN-01: --print-schema exits before any work.
160        required_unless_present_any = ["status", "list_dead", "requeue_dead", "list_skipped", "requeue_skipped", "prune_dead_orphans", "prune_dead_entity_orphans", "print_schema"]
161    )]
162    pub operation: Option<EnrichOperation>,
163
164    /// LLM provider to use. Required for write operations; not needed for the
165    /// read-only queue inspectors (`--status` / `--list-dead` /
166    /// `--requeue-dead` / `--list-skipped` / `--requeue-skipped`), which never
167    /// call the LLM (GAP-SG-31).
168    ///
169    /// GAP-CLI-DRY-01 (v1.1.8): also optional for `--dry-run` (preview only).
170    ///
171    /// When omitted on a path that allows it, the resolved provider is
172    /// `openrouter`. That default is deliberate: it is a REST call and never
173    /// spawns a headless CLI (GAP-HEADLESS-DEFAULT).
174    #[arg(
175        long,
176        value_enum,
177        required_unless_present_any = ["status", "list_dead", "requeue_dead", "list_skipped", "requeue_skipped", "prune_dead_orphans", "prune_dead_entity_orphans", "dry_run", "print_schema"]
178    )]
179    pub mode: Option<EnrichMode>,
180
181    /// Maximum number of items to process in this run. Omit for all.
182    #[arg(long, value_name = "N")]
183    pub limit: Option<usize>,
184
185    /// GAP-SG-185 / v1.2.4: keyset page size for the enrich SCAN phase.
186    ///
187    /// SQL row buffers and in-process candidate pages are this wide; production
188    /// enqueue walks pages without retaining the full eligible key list. The
189    /// sidecar queue still stores one row per eligible item. Precedence: this
190    /// flag > XDG `enrich.scan_page_size` > 512. Accepted range: 1..=4096
191    /// (clamped).
192    #[arg(long, value_name = "N")]
193    pub scan_page_size: Option<usize>,
194
195    /// v1.1.1 (P2): embedding table backfilled by `--operation re-embed`.
196    /// `memories` (default) preserves the historical behaviour; `entities`
197    /// and `chunks` rebuild `entity_embeddings` / `chunk_embeddings`; `all`
198    /// covers the three tables in one run. The scan also selects rows whose
199    /// stored `dim` diverges from the configured `--embedding-dim` (P10),
200    /// so legacy-dimension vectors are regenerated, not only missing ones.
201    /// Ignored (rejected) for every other operation.
202    #[arg(long, value_enum, value_name = "TARGET", default_value_t = ReEmbedTarget::Memories)]
203    pub target: ReEmbedTarget,
204
205    /// Preview items without calling the LLM (zero tokens consumed).
206    #[arg(long)]
207    pub dry_run: bool,
208
209    /// Namespace to operate on. Default: global.
210    #[arg(long)]
211    pub namespace: Option<String>,
212
213    // -- Provider flags (OpenRouter, v1.0.95) --
214    /// OpenRouter text model to use (REQUIRED with --mode openrouter; no default).
215    #[arg(long, value_name = "MODEL")]
216    pub openrouter_model: Option<String>,
217
218    /// OpenRouter API key. Prefer `config add-key --provider openrouter --from-stdin`,
219    /// which stores it at rest under XDG; this flag overrides that store for a
220    /// single invocation and is visible in the process table.
221    #[arg(long, value_name = "KEY")]
222    pub openrouter_api_key: Option<String>,
223
224    /// Timeout per item in seconds when using OpenRouter. Default: 600.
225    ///
226    /// GAP-SG-17: raised from 300 to 600 because dense bodies (close to the
227    /// ~32K-token context ceiling of the configured model) routinely take
228    /// longer than five minutes to generate via `deepseek-v4-flash:nitro`.
229    /// Raise it further for very large corpora; lower it for short snippets.
230    ///
231    // `--openrouter-timeout` USED to be declared here. It is now a GLOBAL
232    // argument on `Cli` (v1.2.3), so redeclaring it on this struct would give
233    // clap two arguments with the same id and abort at startup. The flag still
234    // accepts being written at the subcommand position — `enrich
235    // --openrouter-timeout 600` is unchanged — and the value is read through
236    // `openrouter_chat_timeout_secs()` below.
237    /// Optional OpenRouter base URL override (reserved; defaults to the public API).
238    #[arg(long, value_name = "URL")]
239    pub openrouter_base_url: Option<String>,
240
241    // -- Cost controls --
242    /// Abort when cumulative cost exceeds this USD budget (API key only; ignored for OAuth).
243    #[arg(long, value_name = "USD")]
244    pub max_cost_usd: Option<f64>,
245
246    // -- Queue controls --
247    /// Resume a previously interrupted run (skip already-done items).
248    #[arg(long)]
249    pub resume: bool,
250
251    /// Retry only items that failed in a previous run.
252    #[arg(long)]
253    pub retry_failed: bool,
254
255    /// v1.1.2 (Bug 4): reset `processing` rows whose `claimed_at` is older than
256    /// `--stale-claim-secs` (default 1800s = 30 min) back to `pending`, then exit.
257    /// Recover rows orphaned by a kill -9 mid-enrich without a full re-scan.
258    #[arg(long)]
259    pub reset_stale_claims: bool,
260
261    /// v1.1.2 (Bug 4): age threshold (seconds) for `--reset-stale-claims` and
262    /// the run-startup stale sweep. Default 1800 (30 min).
263    #[arg(long, value_name = "SECONDS", default_value_t = DEFAULT_ENRICH_STALE_CLAIM_SECS)]
264    pub stale_claim_secs: u64,
265
266    /// GAP-ENRICH-BACKLOG-CONVERGE: loop scan→drain internally until the queue
267    /// empties of eligible items or --max-runtime elapses; removes the need for
268    /// an external bash retry loop.
269    #[arg(long)]
270    pub until_empty: bool,
271
272    /// GAP-ENRICH-BACKLOG-CONVERGE: wall-clock ceiling in seconds for
273    /// --until-empty. Defaults to 3600 when omitted.
274    #[arg(long, value_name = "SECONDS")]
275    pub max_runtime: Option<u64>,
276
277    /// GAP-ENRICH-BACKLOG-CONVERGE: attempts per item before it becomes a
278    /// dead-letter (status='dead'). Range 1..=20. Default 8.
279    ///
280    /// GAP-SG-21: the default was raised from 5 to 8 because GAP-SG-09 now
281    /// reclassifies malformed / non-JSON LLM output as TRANSIENT (retryable)
282    /// rather than a permanent HardFailure. A flaky structured-output model
283    /// (e.g. deepseek-v4-flash:nitro) may emit several bad generations in a row
284    /// even after JSON repair (GAP-SG-10) recovers most of them; the extra
285    /// attempts give the backlog room to converge before an item is parked in
286    /// the dead-letter sink. Permanent faults (ProviderError, NotFound) still
287    /// dead-letter on the first attempt regardless of this value.
288    #[arg(long, value_name = "N", default_value_t = DEFAULT_ENRICH_MAX_ATTEMPTS, value_parser = clap::value_parser!(u32).range(1..=20))]
289    pub max_attempts: u32,
290
291    /// GAP-ENRICH-BACKLOG-CONVERGE: read-only mode — report queue and backlog
292    /// counts without calling the LLM or acquiring the singleton.
293    ///
294    /// Field semantics (v1.1.03 clarification):
295    /// - `scan_backlog` = candidates a fresh scan WOULD select from the database
296    ///   (REAL pending work, same WHERE predicate as the scanners).
297    /// - `queue_pending` = a COMPUTED COUNT over the sidecar queue, NOT a physical
298    ///   queue of rows to process — it stays non-zero even after a clean drain.
299    /// - `eligible_now == 0` WITH `queue_pending > 0` means COOLDOWN (rate-limit
300    ///   backoff), NOT a deadlock: items are parked on `next_retry_at`.
301    /// - `eligible_now > 0` stuck against `state: "draining"` IS a deadlock —
302    ///   stale `processing` claims hold the state. Run `--reset-stale-claims`
303    ///   to clear processing claims older than the threshold.
304    #[arg(long)]
305    pub status: bool,
306
307    /// GAP-SG-23: list every dead-letter item (status='dead') for the current
308    /// operation with its error_class, attempt count and last error message.
309    /// Read-only — no LLM, no singleton. Use it to inspect what `--requeue-dead`
310    /// would resurrect before running it.
311    #[arg(long)]
312    pub list_dead: bool,
313
314    /// GAP-SG-11/14: resurrect dead-letter items — move every `status='dead'`
315    /// row back to `pending`, zeroing `attempt`, `next_retry_at`, `error` and
316    /// `error_class`. Distinct from `--retry-failed`, which only resets the
317    /// legacy `status='failed'` rows; dead-letter rows are the terminal sink of
318    /// the v1.0.96 converge loop and are never re-selected without this flag.
319    /// No LLM call or singleton is taken — it only rewrites queue statuses.
320    #[arg(long)]
321    pub requeue_dead: bool,
322
323    /// GAP-SG-96 / G-PR-4: list every `status='skipped'` row for the operation
324    /// (preservation_failed / veto sink). Read-only — no LLM, no singleton.
325    #[arg(long)]
326    pub list_skipped: bool,
327
328    /// GAP-SG-96 / G-PR-3/4: resurrect skipped items — move every
329    /// `status='skipped'` row back to `pending`, zeroing attempt/backoff so a
330    /// lower grounding threshold or corpus fix can re-process them without SQL.
331    #[arg(long)]
332    pub requeue_skipped: bool,
333
334    /// GAP-SG-66: prune ORPHAN dead-letter rows — remove every `status='dead'`
335    /// memory row whose `item_key` (the memory name) no longer exists in the
336    /// main DB for this namespace. These are terminal "not found" failures that
337    /// `--requeue-dead` can never recover (re-processing re-fails the same way),
338    /// so they inflate `queue_dead` forever. Read-only on the main DB; deletes
339    /// only confirmed-orphan rows from the queue sidecar. Entity-keyed dead rows
340    /// are left untouched. No LLM, no singleton — like `--list-dead`.
341    #[arg(long)]
342    pub prune_dead_orphans: bool,
343
344    /// v1.1.2: prune dead ENTITY orphan rows — remove every `status='dead'`
345    /// `item_type='entity'` row from the queue sidecar. Distinct from
346    /// `--prune-dead-orphans` (memory-keyed, consults the main DB): entity dead
347    /// rows are terminal artifacts of re-extraction and have no recovery path,
348    /// so no main-DB check is needed. Required because the v1.1.1 re-embed bug
349    /// left 14680 entity-keyed dead-letter rows that the memory-scoped pruner
350    /// cannot reach. No LLM, no singleton — like `--list-dead`.
351    #[arg(long, conflicts_with = "prune_dead_orphans")]
352    pub prune_dead_entity_orphans: bool,
353
354    /// GAP-SG-16: ignore the per-item backoff cooldown (`next_retry_at`) when
355    /// selecting candidates, so items waiting on exponential backoff are
356    /// processed immediately. Use to drain a backlog whose cooldown windows are
357    /// long but the provider has recovered. Without it, `--status` reports such
358    /// items under `waiting` and they are skipped until their `next_retry_at`.
359    #[arg(long)]
360    pub ignore_backoff: bool,
361
362    /// GAP-SG-28: read-only `body-extract` — extract entities/relationships into
363    /// the graph WITHOUT rewriting (or truncating) the memory body. The default
364    /// `body-extract` restructures the stored body in place; with this flag the
365    /// body is left untouched and only graph bindings are persisted (additive,
366    /// via the same upsert path as `memory-bindings`). Ignored for every other
367    /// operation.
368    #[arg(long)]
369    pub body_extract_graph_only: bool,
370
371    /// GAP-ENRICH-BACKLOG-CONVERGE: REST concurrency for `--mode openrouter`
372    /// (clamp 1..=16). This is the ONLY flag that controls drain fan-out in
373    /// OpenRouter mode; `--llm-parallelism` is inert there.
374    #[arg(
375        long,
376        value_name = "N",
377        default_value_t = DEFAULT_ENRICH_REST_CONCURRENCY,
378        value_parser = clap::value_parser!(u32).range(1..=16)
379    )]
380    pub rest_concurrency: u32,
381
382    // -- body-enrich specific flags (GAP-18) --
383    /// Minimum output character count for body-enrich. Default: 500.
384    #[arg(long, value_name = "CHARS", default_value_t = DEFAULT_BODY_ENRICH_MIN_CHARS)]
385    pub min_output_chars: usize,
386
387    /// Maximum output character count for body-enrich. Default: 2000.
388    #[arg(long, value_name = "CHARS", default_value_t = DEFAULT_BODY_ENRICH_MAX_CHARS)]
389    pub max_output_chars: usize,
390
391    /// Check that enriched body preserves all facts from the original (LLM judge). Default: true.
392    #[arg(long, default_value_t = true)]
393    pub preserve_check: bool,
394
395    /// Path to a custom prompt template file for body-enrich.
396    #[arg(long, value_name = "PATH")]
397    pub prompt_template: Option<PathBuf>,
398
399    // GAP-SG-204. The help text used to end with "It applies only to the
400    // subprocess modes", which reads as a live capability sitting behind a mode
401    // the operator merely has to select. v1.2.0 deleted those backends and
402    // `EnrichMode` has had a single variant ever since, so no such selection
403    // exists. A flag that does nothing is a smaller problem than a flag that
404    // documents a way to make it work.
405    //
406    // The rationale lives in `//` and not in `///` on purpose: everything above
407    // the `#[arg]` is rendered into `--help`, and an operator reading the flag
408    // needs the rule, not the history. Release archaeology belongs in
409    // `gaps.md` and the CHANGELOG.
410    /// ALWAYS INERT; accepted only so existing scripts keep parsing.
411    ///
412    /// Use `--rest-concurrency` to size the drain fan-out. Passing this emits a
413    /// warning and changes nothing.
414    #[arg(long, value_name = "N", value_parser = clap::value_parser!(u32).range(1..=32))]
415    pub llm_parallelism: Option<u32>,
416
417    // -- Output / infra --
418    /// Emit NDJSON output. Always true; flag accepted for compatibility.
419    #[arg(long)]
420    pub json: bool,
421
422    /// Database path override.
423    #[arg(long)]
424    pub db: Option<String>,
425
426    /// G30: poll for the job singleton every second for up to N seconds
427    /// when another invocation holds the lock. Default: 0 (fail fast).
428    #[arg(long, value_name = "SECONDS")]
429    pub wait_job_singleton: Option<u64>,
430
431    /// G30: force acquisition of the singleton lock by removing a stale
432    /// lock file from a previously crashed invocation. Use only when you
433    /// are certain no other `enrich`/`ingest` is running.
434    #[arg(long, default_value_t = false)]
435    pub force_job_singleton: bool,
436
437    /// Select a subset of names to enrich instead of the full candidate set.
438    /// Comma-separated, e.g. `--names a,b,c`. Semantics depend on the
439    /// operation (GAP-CLI-NAMES-01): entity-keyed ops (`entity-descriptions`,
440    /// `entity-connect`, …) treat values as **entity** names; memory-keyed
441    /// ops (`memory-bindings`, `body-enrich`, …) treat values as **memory**
442    /// names. Prefer `--entity-names` / `--memory-names` for an explicit
443    /// namespace. Empty when omitted (processes all candidates).
444    ///
445    /// GAP-SG-18: also a cooldown remedy — when `--status` shows items under
446    /// `waiting` (parked on `next_retry_at` backoff), pass the exact names here
447    /// to re-enqueue and process just that subset on the next run instead of
448    /// waiting for every cooldown to elapse. REQUIRED for `--operation
449    /// augment-bindings`, which refuses to re-scan the whole namespace.
450    #[arg(long, value_name = "NAMES", value_delimiter = ',')]
451    pub names: Vec<String>,
452
453    /// G37: read the subset of memory names from a file (one per line).
454    /// Lines starting with `#` and empty lines are ignored. Combined with
455    /// `--names` (union) when both are set.
456    #[arg(long, value_name = "PATH")]
457    pub names_file: Option<PathBuf>,
458
459    /// G35: probe the LLM provider with a 1-turn ping before processing
460    /// the batch. Aborts with a clear error if the rate-limit window is
461    /// closed (avoids burning N turns only to fail on item 1).
462    #[arg(long, default_value_t = false)]
463    pub preflight_check: bool,
464
465    /// G35: number of seconds before the OAuth rate-limit reset at which
466    /// the preflight probe should refuse to start. Default 300 (5 min).
467    #[arg(long, value_name = "SECONDS", default_value_t = DEFAULT_ENRICH_RATE_LIMIT_BUFFER_SECS)]
468    pub rate_limit_buffer: u64,
469
470    /// G28-D: refuse to start when the 1-minute load average exceeds
471    /// `2 × ncpus` (or XDG `system.max_load_per_ncpu` if set).
472    /// Set to false to skip the check on contended runners.
473    ///
474    /// The key is `system.` and not `enrich.`: this help named a key that the
475    /// registry never declared, so the documented `config set` answered exit 1
476    /// with a did-you-mean pointing at the real one.
477    #[arg(long, default_value_t = true)]
478    pub max_load_check: bool,
479
480    /// G28-D: when the system is saturated, abort the job after this
481    /// many consecutive HardFailure outcomes. Default 5.
482    #[arg(long, value_name = "N", default_value_t = DEFAULT_ENRICH_CIRCUIT_BREAKER_THRESHOLD)]
483    pub circuit_breaker_threshold: u32,
484
485    /// G29 Step 4: minimum trigram-Jaccard similarity between the
486    /// original body and the LLM-rewritten body for the rewrite to be
487    /// accepted. Scores below the threshold are rejected and emitted as
488    /// `EnrichItemResult::PreservationFailed`. Default 0.7 (per the G29
489    /// gap specification). Ignored when `--operation` is not
490    /// `body-enrich`.
491    #[arg(long, value_name = "FLOAT", default_value_t = DEFAULT_ENRICH_PRESERVE_THRESHOLD)]
492    pub preserve_threshold: f64,
493
494    /// GAP-CLI-ED-03: minimum grounding coverage of an entity description
495    /// against linked memory bodies. Uses trigram coverage (`|A∩B|/|A|`),
496    /// not symmetric Jaccard, because descriptions are short. Default 0.12.
497    /// Set to 0 to use the compiled default. Only applies to
498    /// `entity-descriptions`.
499    #[arg(long, value_name = "FLOAT", default_value_t = DEFAULT_ENRICH_GROUNDING_THRESHOLD)]
500    pub entity_description_grounding_threshold: f64,
501
502    /// GAP-CLI-ED-06 / CAPA-B: re-scan entities whose description is empty OR
503    /// matches high-precision low-quality compound markers (e.g. "is a software
504    /// component", "is a configuration file" — not bare domain phrases).
505    /// Once per invocation, reopens matching `skipped`/`done` queue rows for
506    /// those scan keys to `pending` (does not reopen `dead`; use
507    /// `--requeue-dead`). Default false (write-once for non-empty descriptions).
508    #[arg(long, default_value_t = false)]
509    pub force_redescribe: bool,
510
511    /// Wave 2 / GAP-CLI-OBS-04: sample N entities with descriptions and report
512    /// `quality_pct` / `scan_backlog_low_grounding_est` on `--status`.
513    /// `scan_backlog_low_grounding_est` is a **sample-based estimate only** —
514    /// it is not a drain backlog and is not processed by `--force-redescribe`.
515    /// Precedence: this flag > XDG `enrich.entity_description.quality_sample`
516    /// > default 50. Set 0 to disable sampling.
517    #[arg(long, value_name = "N")]
518    pub quality_sample: Option<usize>,
519
520    /// GAP-CLI-NAMES-02: explicit entity name filter (entity-descriptions,
521    /// entity-connect, …). Alias of `--names` for entity-keyed ops.
522    #[arg(long, value_name = "NAMES", value_delimiter = ',')]
523    pub entity_names: Vec<String>,
524
525    /// GAP-CLI-NAMES-02: explicit memory name filter (memory-bindings,
526    /// body-enrich, …). Alias of `--names` for memory-keyed ops.
527    #[arg(long, value_name = "NAMES", value_delimiter = ',')]
528    pub memory_names: Vec<String>,
529
530    /// GAP-CLI-EC-03: limit entity-connect pair scan to the subgraph of
531    /// entities linked to this memory name.
532    #[arg(long, value_name = "NAME")]
533    pub anchor_memory: Option<String>,
534
535    /// GAP-CLI-ED-05: optional domain label for entity-descriptions
536    /// (`auto` = no hint, `none` = force neutral, or free-form e.g. `fiscal`).
537    /// Precedence: this flag > XDG `enrich.entity_description.domain` > `auto`.
538    #[arg(long, value_name = "LABEL", default_value = "auto")]
539    pub entity_description_domain: String,
540
541    /// GAP-CLI-PRIO-05: cooperative yield every N processed items so hot-set
542    /// entity-descriptions can preempt long entity-connect drains. 0 disables.
543    /// XDG key: `enrich.yield_every_n_items` (default 10).
544    #[arg(long, value_name = "N")]
545    pub yield_every_n_items: Option<usize>,
546
547    /// GAP-CLI-OBS-02 / PRIO-04: run gate ops in order
548    /// memory-bindings → entity-descriptions (before entity-connect).
549    #[arg(long, default_value_t = false)]
550    pub ops_gate: bool,
551
552    /// Emit the JSON Schema for `enrich --status` stdout and exit 0 without
553    /// opening the database or calling the LLM (agent-native R-AN-01).
554    #[arg(
555        long,
556        default_value_t = false,
557        help = "Print JSON Schema for enrich --status output and exit"
558    )]
559    pub print_schema: bool,
560}
561
562impl EnrichArgs {
563    /// GAP-SG-31: resolved enrichment operation.
564    ///
565    /// `operation` is `Option` so the read-only queue inspectors
566    /// (`--status` / `--list-dead` / `--requeue-dead`) can run without it.
567    /// Write paths always carry a value (enforced by
568    /// `required_unless_present_any` at parse time); the read-only paths fall
569    /// back to `memory-bindings`, the most common queue, when it is omitted.
570    pub(crate) fn operation(&self) -> EnrichOperation {
571        self.operation
572            .clone()
573            .unwrap_or(EnrichOperation::MemoryBindings)
574    }
575
576    /// GAP-SG-31: resolved LLM provider.
577    ///
578    /// `mode` is `Option` because clap does not require it for the read-only
579    /// inspectors, nor for `--dry-run` since GAP-CLI-DRY-01 (v1.1.8). Every
580    /// WRITE path still carries a value, enforced by `required_unless_present_any`
581    /// at parse time — omitting `--mode` on a write run exits 2.
582    ///
583    /// The fallback is [`EnrichMode::OpenRouter`], and that choice is load-bearing
584    /// rather than arbitrary: GAP-HEADLESS-DEFAULT requires that an omitted
585    /// `--mode` never reach a provider that spawns a headless CLI, and OpenRouter
586    /// is a REST call that spawns nothing. Do not change it to a subprocess mode.
587    ///
588    /// Note this doc previously claimed the fallback was "only ever observed by
589    /// read-only code". That stopped being true in v1.1.8 when `--dry-run` joined
590    /// the exemption list; dry-run observes it too, and is safe for a different
591    /// reason — binary resolution is skipped before the mode is inspected.
592    pub(crate) fn mode(&self) -> EnrichMode {
593        self.mode.clone().unwrap_or(EnrichMode::OpenRouter)
594    }
595
596    /// Effective chat-completion budget, in the documented precedence:
597    /// `--openrouter-timeout` > XDG `llm.openrouter_timeout_secs` >
598    /// [`DEFAULT_OPENROUTER_CHAT_TIMEOUT_SECS`].
599    ///
600    /// Reads through `runtime_config` rather than off this struct because the
601    /// flag is GLOBAL since v1.2.3 and therefore no longer lives here. The
602    /// middle layer is the part that used to be unreachable: with the value
603    /// read straight off the enum variant, the XDG key could never win, so an
604    /// operator who set it saw no effect and no diagnostic.
605    pub(crate) fn openrouter_chat_timeout_secs(&self) -> u64 {
606        crate::runtime_config::openrouter_chat_timeout_secs(DEFAULT_OPENROUTER_CHAT_TIMEOUT_SECS)
607    }
608
609    /// GAP-SG-185: resolved keyset page size for the SCAN phase.
610    ///
611    /// Precedence: `--scan-page-size` > XDG `enrich.scan_page_size` > default 512.
612    pub(crate) fn scan_page_size(&self) -> usize {
613        crate::runtime_config::enrich_scan_page_size(self.scan_page_size)
614    }
615}