Skip to main content

sqlite_graphrag/
errors.rs

1//! Library-wide error type.
2//!
3//! `AppError` is the single error type returned by every public API in the
4//! crate. Each variant maps to a deterministic exit code through
5//! `AppError::exit_code`, which the binary propagates to the shell on
6//! failure. See the README for the full exit code contract.
7
8use crate::i18n::{current, Language};
9use crate::spawn::preflight::PreFlightError;
10use thiserror::Error;
11
12/// Unified error type for all CLI and library operations.
13///
14/// Each variant corresponds to a distinct failure category. The
15/// [`AppError::exit_code`] method converts a variant into a stable numeric
16/// code so that shell callers and LLM agents can route on it.
17///
18/// # SemVer Policy
19///
20/// This enum is `#[non_exhaustive]`. New variants may be added in minor
21/// releases without breaking downstream match arms (use a wildcard `_`).
22#[derive(Error, Debug)]
23#[non_exhaustive]
24pub enum AppError {
25    /// Input failed schema, length or format validation. Maps to exit code `1`.
26    ///
27    /// This variant groups multiple validation failure causes. Callers that need
28    /// programmatic retry decisions should use [`AppError::is_retryable`] instead
29    /// of parsing the message string.
30    #[error("validation error: {0}")]
31    Validation(String),
32
33    /// External binary required for operation was not found in PATH. Maps to exit code `1`.
34    #[error("binary not found: {name} — ensure it is installed and in PATH")]
35    BinaryNotFound { name: String },
36
37    /// Remote service signaled rate limiting; caller should retry with backoff. Maps to exit code `1`.
38    #[error("rate limited: {detail}")]
39    RateLimited { detail: String },
40
41    /// Operation exceeded its time budget. Maps to exit code `1`.
42    #[error("timeout after {duration_secs}s: {operation}")]
43    Timeout {
44        operation: String,
45        duration_secs: u64,
46    },
47
48    /// A memory or entity with the same `(namespace, name)` already exists. Maps to exit code `9`.
49    #[error("duplicate detected: {0}")]
50    Duplicate(String),
51
52    /// Optimistic update lost the race because `updated_at` changed. Maps to exit code `3`.
53    #[error("conflict: {0}")]
54    Conflict(String),
55
56    /// The requested record does not exist or was soft-deleted. Maps to exit code `4`.
57    #[error("not found: {0}")]
58    NotFound(String),
59
60    /// Memory lookup by `(namespace, name)` returned no row. Maps to exit code `4`.
61    ///
62    /// G55 S2 (v1.0.80): structural variant that carries the requested identifier
63    /// and namespace, eliminating the "not found: unknown in namespace 'X'" class
64    /// of bugs that masked which lookup target failed. The display format matches
65    /// the legacy string-based `NotFound` so the i18n replace-chain and external
66    /// scripts that pattern-match on `memory not found: name='N' in namespace 'NS'`
67    /// keep working.
68    #[error("memory not found: name='{name}' in namespace '{namespace}'")]
69    MemoryNotFound { name: String, namespace: String },
70
71    /// Memory lookup by integer `id` returned no row. Maps to exit code `4`.
72    #[error("memory not found: id={id}")]
73    MemoryNotFoundById { id: i64 },
74
75    /// Namespace could not be resolved from flag, environment or markers. Maps to exit code `5`.
76    #[error("namespace not resolved: {0}")]
77    NamespaceError(String),
78
79    /// Payload exceeded one of the configured body, name or batch limits. Maps to exit code `6`.
80    #[error("limit exceeded: {0}")]
81    LimitExceeded(String),
82
83    /// Low-level SQLite error propagated from `rusqlite`. Maps to exit code `10`.
84    #[error("database error: {0}")]
85    Database(#[from] rusqlite::Error),
86
87    /// Embedding generation via `fastembed` failed or produced the wrong shape. Maps to exit code `11`.
88    #[error("embedding error: {0}")]
89    Embedding(String),
90
91    /// The `sqlite-vec` extension could not load or register its virtual table. Maps to exit code `12`.
92    #[error("sqlite-vec extension failed: {0}")]
93    VecExtension(String),
94
95    /// SQLite returned `SQLITE_BUSY` after exhausting retries. Maps to exit code `15` (was `13` before v2.0.0; relocated to free `13` for BatchPartialFailure per PRD).
96    #[error("database busy: {0}")]
97    DbBusy(String),
98
99    /// Batch operation failed partially — N of M items failed. Maps to exit code `13` (PRD 1822).
100    ///
101    /// Reserved for use in `import`, `reindex` and batch stdin (BLOCK 3/4). Variant present
102    /// since v2.0.0 even if call-sites do not yet exist — stable exit code mapping.
103    #[error("batch partial failure: {failed} of {total} items failed")]
104    BatchPartialFailure { total: usize, failed: usize },
105
106    /// Filesystem I/O error while reading or writing the database or cache. Maps to exit code `14`.
107    #[error("IO error: {0}")]
108    Io(#[from] std::io::Error),
109
110    /// Unexpected internal error surfaced through `anyhow`. Maps to exit code `20`.
111    #[error(transparent)]
112    Internal(#[from] anyhow::Error),
113
114    /// JSON serialization or deserialization failure. Maps to exit code `20`.
115    #[error("json error: {0}")]
116    Json(#[from] serde_json::Error),
117
118    /// Another instance is already running and holds the advisory lock. Maps to exit code `75`.
119    ///
120    /// Use `--allow-parallel` to skip the lock or `--wait-lock SECONDS` to retry.
121    #[error("lock busy: {0}")]
122    LockBusy(String),
123
124    /// All concurrency slots are occupied after the wait timeout. Maps to exit code `75`.
125    ///
126    /// Occurs when [`crate::constants::MAX_CONCURRENT_CLI_INSTANCES`] instances are already
127    /// active and the wait limit [`crate::constants::CLI_LOCK_DEFAULT_WAIT_SECS`] is exhausted.
128    #[error(
129        "all {max} concurrency slots occupied after waiting {waited_secs}s (exit 75); \
130         use --max-concurrency or wait for other invocations to finish"
131    )]
132    AllSlotsFull { max: usize, waited_secs: u64 },
133
134    /// A heavy long-running job is already running for this job_type/namespace
135    /// pair. Maps to exit code `75` (the same `EX_TEMPFAIL` code used by the
136    /// CLI semaphore).
137    ///
138    /// G28-B (v1.0.68): ensures at most one `enrich`, `ingest --mode
139    /// claude-code`, or `ingest --mode codex` runs at a time per namespace.
140    /// Use `--wait-job-singleton <SECONDS>` (per-command) to poll until the
141    /// other invocation finishes.
142    #[error(
143        "job {job_type} for namespace '{namespace}' is already running (exit 75); \
144         wait for it to finish or pass --wait-job-singleton <SECONDS>"
145    )]
146    JobSingletonLocked { job_type: String, namespace: String },
147
148    /// G45: an LLM embedding operation is already running against the
149    /// same `(namespace, db)` pair in another process. Exit code 75
150    /// (retryable). The caller can pass `--wait-embed-singleton
151    /// <SECONDS>` to poll until the lock drops.
152    #[error(
153        "embedding singleton for namespace '{namespace}' is already held (exit 75); \
154         another CLI is calling the LLM on this database; pass --wait-embed-singleton <SECONDS> to wait"
155    )]
156    EmbeddingSingletonLocked { namespace: String },
157
158    /// Available memory is below the minimum required to load the model. Maps to exit code `77`.
159    ///
160    /// Returned when `sysinfo` reports available memory below
161    /// [`crate::constants::MIN_AVAILABLE_MEMORY_MB`] MiB before starting the ONNX model load.
162    #[error(
163        "available memory ({available_mb}MB) below required minimum ({required_mb}MB) \
164         to load the model; abort other loads or use --skip-memory-guard (exit 77)"
165    )]
166    LowMemory { available_mb: u64, required_mb: u64 },
167
168    /// v1.0.82 (GAP-002 final): shutdown was requested via SIGINT, SIGTERM or
169    /// SIGHUP before the current command completed. Maps to exit code
170    /// [`crate::constants::SHUTDOWN_EXIT_CODE`] (19).
171    ///
172    /// The signal name is preserved in the `signal` field so the JSON
173    /// envelope emitted before exit can route the operator to a
174    /// deterministic branch. Distinct from the legacy `128 + signal`
175    /// Unix convention (130/143/129) so LLM agents can match on a
176    /// single code for "cancelled by user".
177    #[error("shutdown signal received: {signal}")]
178    Shutdown { signal: String },
179
180    /// v1.0.87 (GAP-META-005, ADR-0045): pre-flight validation gate
181    /// rejected the spawn before fork. Maps to exit code `16`.
182    ///
183    /// The `source` field carries the structured [`PreFlightError`]
184    /// variant so callers and operators can route on the specific
185    /// failure class (BinaryNotFound, ArgvExceedsArgMax,
186    /// McpConfigInlineJsonRejected, McpConfigPathMissing,
187    /// McpConfigPathInvalidJson, WalkUpMcpJsonInvalid,
188    /// OutputBufferTooSmall, ClaudeConfigDirNotEmpty) instead of
189    /// parsing the legacy `detail: String` representation.
190    ///
191    /// This variant is **permanent** — retrying the same argv will fail
192    /// identically. Operators must fix the underlying condition (install
193    /// the binary, shorten the body, override `CLAUDE_CONFIG_DIR`,
194    /// substitute the inline `--mcp-config '{}'` for a tempfile path,
195    /// etc.) before retrying.
196    #[error("preflight validation failed: {source}")]
197    PreFlightFailed { source: Box<PreFlightError> },
198}
199
200/// Bridges the structured [`PreFlightError`] produced by the
201/// pre-flight validation gate (v1.0.87, ADR-0045) into the unified
202/// [`AppError`] envelope. Lets spawners use the `?` operator instead
203/// of hand-rolling `AppError::PreFlightFailed { source: ... }` at every
204/// call site, and keeps the variant alive as the canonical exit code 16
205/// path rather than the dead code it was at v1.0.87.
206impl From<PreFlightError> for AppError {
207    fn from(source: PreFlightError) -> Self {
208        AppError::PreFlightFailed {
209            source: Box::new(source),
210        }
211    }
212}
213
214impl AppError {
215    /// Returns the deterministic process exit code for this error variant.
216    ///
217    /// The codes follow the contract documented in the README: `1` for
218    /// validation, `9` for duplicates (moved from `2` in v1.0.52), `3` for conflicts, `4` for missing
219    /// records, `5` for namespace errors, `6` for limit violations, `10`–`14`
220    /// for infrastructure failures, `13` for BatchPartialFailure (PRD 1822),
221    /// `15` for DbBusy (migrated from `13` in v2.0.0), `20` for internal errors,
222    /// `75` (EX_TEMPFAIL) when the advisory CLI lock is held or all concurrency
223    /// slots are exhausted, and `77` when available memory is insufficient to
224    /// load the embedding model.
225    ///
226    /// # Examples
227    ///
228    /// ```
229    /// use sqlite_graphrag::errors::AppError;
230    ///
231    /// assert_eq!(AppError::Validation("invalid field".into()).exit_code(), 1);
232    /// assert_eq!(AppError::Duplicate("ns/mem".into()).exit_code(), 9);
233    /// assert_eq!(AppError::Conflict("ts changed".into()).exit_code(), 3);
234    /// assert_eq!(AppError::NotFound("id 42".into()).exit_code(), 4);
235    /// assert_eq!(AppError::NamespaceError("no marker".into()).exit_code(), 5);
236    /// assert_eq!(AppError::LimitExceeded("body too large".into()).exit_code(), 6);
237    /// assert_eq!(AppError::Embedding("wrong dim".into()).exit_code(), 11);
238    /// assert_eq!(AppError::DbBusy("retries exhausted".into()).exit_code(), 15);
239    /// assert_eq!(AppError::LockBusy("another instance".into()).exit_code(), 75);
240    /// ```
241    #[inline]
242    #[must_use]
243    pub fn exit_code(&self) -> i32 {
244        match self {
245            Self::Validation(_) => 1,
246            Self::BinaryNotFound { .. } => 1,
247            Self::RateLimited { .. } => 1,
248            Self::Timeout { .. } => 1,
249            Self::Duplicate(_) => crate::constants::DUPLICATE_EXIT_CODE,
250            Self::Conflict(_) => 3,
251            Self::NotFound(_) => 4,
252            Self::MemoryNotFound { .. } => 4,
253            Self::MemoryNotFoundById { .. } => 4,
254            Self::NamespaceError(_) => 5,
255            Self::LimitExceeded(_) => 6,
256            Self::Database(_) => 10,
257            Self::Embedding(_) => 11,
258            Self::VecExtension(_) => 12,
259            Self::BatchPartialFailure { .. } => crate::constants::BATCH_PARTIAL_FAILURE_EXIT_CODE,
260            Self::DbBusy(_) => crate::constants::DB_BUSY_EXIT_CODE,
261            Self::Io(_) => 14,
262            Self::Internal(_) => 20,
263            Self::Json(_) => 20,
264            Self::LockBusy(_) => crate::constants::CLI_LOCK_EXIT_CODE,
265            Self::AllSlotsFull { .. } => crate::constants::CLI_LOCK_EXIT_CODE,
266            Self::JobSingletonLocked { .. } => crate::constants::CLI_LOCK_EXIT_CODE,
267            Self::EmbeddingSingletonLocked { .. } => crate::constants::CLI_LOCK_EXIT_CODE,
268            Self::LowMemory { .. } => crate::constants::LOW_MEMORY_EXIT_CODE,
269            Self::Shutdown { .. } => crate::constants::SHUTDOWN_EXIT_CODE,
270            Self::PreFlightFailed { .. } => 16,
271        }
272    }
273
274    /// Returns `true` when the error is transient and the operation may
275    /// succeed on retry with backoff.
276    ///
277    /// # Examples
278    ///
279    /// ```
280    /// use sqlite_graphrag::errors::AppError;
281    ///
282    /// assert!(AppError::DbBusy("busy".into()).is_retryable());
283    /// assert!(AppError::LockBusy("held".into()).is_retryable());
284    /// assert!(!AppError::NotFound("x".into()).is_retryable());
285    /// assert!(!AppError::Validation("bad".into()).is_retryable());
286    /// ```
287    #[inline]
288    #[must_use]
289    pub fn is_retryable(&self) -> bool {
290        matches!(
291            self,
292            Self::DbBusy(_)
293                | Self::LockBusy(_)
294                | Self::AllSlotsFull { .. }
295                | Self::JobSingletonLocked { .. }
296                | Self::EmbeddingSingletonLocked { .. }
297                | Self::LowMemory { .. }
298                | Self::RateLimited { .. }
299                | Self::Timeout { .. }
300        )
301    }
302
303    /// Returns `true` when shutdown was requested by the user via signal.
304    ///
305    /// Distinct from `is_permanent` because shutdown is a USER intent, not
306    /// a state to retry against. The operation should be retried with
307    /// `--resume` (GAP-001) when the persisted staging row still exists.
308    ///
309    /// # Examples
310    ///
311    /// ```
312    /// use sqlite_graphrag::errors::AppError;
313    ///
314    /// assert!(AppError::Shutdown { signal: "SIGINT".into() }.is_shutdown());
315    /// assert!(!AppError::Validation("x".into()).is_shutdown());
316    /// ```
317    #[inline]
318    #[must_use]
319    pub fn is_shutdown(&self) -> bool {
320        matches!(self, Self::Shutdown { .. })
321    }
322
323    /// Returns `true` when the error is permanent and must NOT be retried.
324    ///
325    /// Complement to [`Self::is_retryable`]. Errors not classified by either
326    /// method (e.g. `Database`, `Io`, `Internal`) are ambiguous — the caller
327    /// decides based on context.
328    ///
329    /// # Examples
330    ///
331    /// ```
332    /// use sqlite_graphrag::errors::AppError;
333    ///
334    /// assert!(AppError::Validation("bad".into()).is_permanent());
335    /// assert!(!AppError::DbBusy("busy".into()).is_permanent());
336    /// ```
337    #[inline]
338    #[must_use]
339    pub fn is_permanent(&self) -> bool {
340        matches!(
341            self,
342            Self::Validation(_)
343                | Self::BinaryNotFound { .. }
344                | Self::Duplicate(_)
345                | Self::NotFound(_)
346                | Self::MemoryNotFound { .. }
347                | Self::MemoryNotFoundById { .. }
348                | Self::NamespaceError(_)
349                | Self::LimitExceeded(_)
350                | Self::VecExtension(_)
351                | Self::PreFlightFailed { .. }
352        )
353    }
354
355    /// Returns the localized error message in the active language (`--lang` / `SQLITE_GRAPHRAG_LANG`).
356    ///
357    /// In English the text is identical to the `Display` generated by thiserror.
358    /// In Portuguese the prefixes and messages are translated to PT-BR.
359    pub fn localized_message(&self) -> String {
360        self.localized_message_for(current())
361    }
362
363    /// Returns the localized message for the explicitly provided language.
364    /// Useful in tests that cannot depend on the global `OnceLock`.
365    ///
366    /// # Examples
367    ///
368    /// ```
369    /// use sqlite_graphrag::errors::AppError;
370    /// use sqlite_graphrag::i18n::Language;
371    ///
372    /// let err = AppError::NotFound("mem-xyz".into());
373    ///
374    /// let en = err.localized_message_for(Language::English);
375    /// assert!(en.contains("not found"));
376    ///
377    /// let pt = err.localized_message_for(Language::Portuguese);
378    /// assert!(pt.contains("n\u{e3}o encontrado"));
379    /// ```
380    pub fn localized_message_for(&self, lang: Language) -> String {
381        match lang {
382            Language::English => self.to_string(),
383            Language::Portuguese => self.to_string_pt(),
384        }
385    }
386
387    fn to_string_pt(&self) -> String {
388        use crate::i18n::validation::app_error_pt as pt;
389        match self {
390            Self::Validation(msg) => pt::validation(msg),
391            Self::BinaryNotFound { name } => pt::binary_not_found(name),
392            Self::RateLimited { detail } => pt::rate_limited(detail),
393            Self::Timeout {
394                operation,
395                duration_secs,
396            } => pt::timeout(operation, *duration_secs),
397            Self::Duplicate(msg) => pt::duplicate(msg),
398            Self::Conflict(msg) => pt::conflict(msg),
399            Self::NotFound(msg) => pt::not_found(msg),
400            Self::MemoryNotFound { name, namespace } => pt::memory_not_found(name, namespace),
401            Self::MemoryNotFoundById { id } => pt::memory_not_found_by_id(*id),
402            Self::NamespaceError(msg) => pt::namespace_error(msg),
403            Self::LimitExceeded(msg) => pt::limit_exceeded(msg),
404            Self::Database(e) => pt::database(&e.to_string()),
405            Self::Embedding(msg) => pt::embedding(msg),
406            Self::VecExtension(msg) => pt::vec_extension(msg),
407            Self::DbBusy(msg) => pt::db_busy(msg),
408            Self::BatchPartialFailure { total, failed } => {
409                pt::batch_partial_failure(*total, *failed)
410            }
411            Self::Io(e) => pt::io(&e.to_string()),
412            Self::Internal(e) => pt::internal(&e.to_string()),
413            Self::Json(e) => pt::json(&e.to_string()),
414            Self::LockBusy(msg) => pt::lock_busy(msg),
415            Self::AllSlotsFull { max, waited_secs } => pt::all_slots_full(*max, *waited_secs),
416            Self::JobSingletonLocked {
417                job_type,
418                namespace,
419            } => pt::job_singleton_locked(job_type, namespace),
420            Self::EmbeddingSingletonLocked { namespace } => {
421                pt::embedding_singleton_locked(namespace)
422            }
423            Self::LowMemory {
424                available_mb,
425                required_mb,
426            } => pt::low_memory(*available_mb, *required_mb),
427            Self::Shutdown { signal } => pt::shutdown(signal),
428            Self::PreFlightFailed { source } => pt::preflight_failed(&source.to_string()),
429        }
430    }
431}
432
433#[cfg(test)]
434mod tests {
435    use super::*;
436    use std::io;
437
438    #[test]
439    fn exit_code_validation_returns_1() {
440        assert_eq!(AppError::Validation("invalid field".into()).exit_code(), 1);
441    }
442
443    #[test]
444    fn exit_code_duplicate_returns_9() {
445        assert_eq!(AppError::Duplicate("namespace/name".into()).exit_code(), 9);
446    }
447
448    #[test]
449    fn exit_code_conflict_returns_3() {
450        assert_eq!(
451            AppError::Conflict("updated_at changed".into()).exit_code(),
452            3
453        );
454    }
455
456    #[test]
457    fn exit_code_not_found_returns_4() {
458        assert_eq!(AppError::NotFound("memory missing".into()).exit_code(), 4);
459    }
460
461    #[test]
462    fn exit_code_namespace_error_returns_5() {
463        assert_eq!(
464            AppError::NamespaceError("not resolved".into()).exit_code(),
465            5
466        );
467    }
468
469    #[test]
470    fn exit_code_limit_exceeded_returns_6() {
471        assert_eq!(
472            AppError::LimitExceeded("body too large".into()).exit_code(),
473            6
474        );
475    }
476
477    #[test]
478    fn exit_code_embedding_returns_11() {
479        assert_eq!(AppError::Embedding("model failure".into()).exit_code(), 11);
480    }
481
482    #[test]
483    fn exit_code_vec_extension_returns_12() {
484        assert_eq!(
485            AppError::VecExtension("extension did not load".into()).exit_code(),
486            12
487        );
488    }
489
490    #[test]
491    fn exit_code_db_busy_returns_15() {
492        assert_eq!(AppError::DbBusy("retries exhausted".into()).exit_code(), 15);
493    }
494
495    #[test]
496    fn exit_code_batch_partial_failure_returns_13() {
497        assert_eq!(
498            AppError::BatchPartialFailure {
499                total: 10,
500                failed: 3
501            }
502            .exit_code(),
503            13
504        );
505    }
506
507    #[test]
508    fn display_batch_partial_failure_includes_counts() {
509        let err = AppError::BatchPartialFailure {
510            total: 50,
511            failed: 7,
512        };
513        let msg = err.to_string();
514        assert!(msg.contains("7"));
515        assert!(msg.contains("50"));
516        // to_string() uses the English #[error] attr; PT is in localized_message_for
517        assert!(msg.contains("batch partial failure"));
518    }
519
520    #[test]
521    fn exit_code_io_returns_14() {
522        let io_err = io::Error::new(io::ErrorKind::NotFound, "file missing");
523        assert_eq!(AppError::Io(io_err).exit_code(), 14);
524    }
525
526    #[test]
527    fn exit_code_internal_returns_20() {
528        let anyhow_err = anyhow::anyhow!("unexpected internal error");
529        assert_eq!(AppError::Internal(anyhow_err).exit_code(), 20);
530    }
531
532    #[test]
533    fn exit_code_json_returns_20() {
534        let json_err = serde_json::from_str::<serde_json::Value>("invalid json {{").unwrap_err();
535        assert_eq!(AppError::Json(json_err).exit_code(), 20);
536    }
537
538    #[test]
539    fn exit_code_lock_busy_returns_75() {
540        assert_eq!(
541            AppError::LockBusy("another active instance".into()).exit_code(),
542            75
543        );
544    }
545
546    #[test]
547    fn display_validation_includes_message() {
548        let err = AppError::Validation("invalid id".into());
549        assert!(err.to_string().contains("invalid id"));
550        assert!(err.to_string().contains("validation error"));
551    }
552
553    #[test]
554    fn display_duplicate_includes_message() {
555        let err = AppError::Duplicate("proj/mem".into());
556        assert!(err.to_string().contains("proj/mem"));
557        assert!(err.to_string().contains("duplicate detected"));
558    }
559
560    #[test]
561    fn display_not_found_includes_message() {
562        let err = AppError::NotFound("id 42".into());
563        assert!(err.to_string().contains("id 42"));
564        assert!(err.to_string().contains("not found"));
565    }
566
567    #[test]
568    fn display_embedding_includes_message() {
569        let err = AppError::Embedding("wrong dimension".into());
570        assert!(err.to_string().contains("wrong dimension"));
571        assert!(err.to_string().contains("embedding error"));
572    }
573
574    #[test]
575    fn display_lock_busy_includes_message() {
576        let err = AppError::LockBusy("pid 1234".into());
577        assert!(err.to_string().contains("pid 1234"));
578        assert!(err.to_string().contains("lock busy"));
579    }
580
581    #[test]
582    fn from_io_error_converts_correctly() {
583        let io_err = io::Error::new(io::ErrorKind::PermissionDenied, "permission denied");
584        let app_err: AppError = io_err.into();
585        assert_eq!(app_err.exit_code(), 14);
586        assert!(app_err.to_string().contains("IO error"));
587    }
588
589    #[test]
590    fn from_anyhow_error_converts_correctly() {
591        let anyhow_err = anyhow::anyhow!("internal detail");
592        let app_err: AppError = anyhow_err.into();
593        assert_eq!(app_err.exit_code(), 20);
594        assert!(app_err.to_string().contains("internal detail"));
595    }
596
597    #[test]
598    fn from_serde_json_error_converts_correctly() {
599        let json_err = serde_json::from_str::<serde_json::Value>("{bad_field}").unwrap_err();
600        let app_err: AppError = json_err.into();
601        assert_eq!(app_err.exit_code(), 20);
602        assert!(app_err.to_string().contains("json error"));
603    }
604
605    #[test]
606    fn exit_code_lock_busy_matches_constant() {
607        assert_eq!(
608            AppError::LockBusy("test".into()).exit_code(),
609            crate::constants::CLI_LOCK_EXIT_CODE
610        );
611    }
612
613    #[test]
614    fn localized_message_en_equals_to_string() {
615        let err = AppError::NotFound("mem-x".into());
616        assert_eq!(
617            err.localized_message_for(crate::i18n::Language::English),
618            err.to_string()
619        );
620    }
621
622    // Detailed Portuguese-specific assertions live in `src/i18n.rs`
623    // (the bilingual module). Here we only verify that delegation is wired
624    // correctly, without embedding PT strings in this English-only file.
625
626    #[test]
627    fn localized_message_pt_differs_from_en() {
628        let err = AppError::NotFound("mem-x".into());
629        let en = err.localized_message_for(crate::i18n::Language::English);
630        let pt = err.localized_message_for(crate::i18n::Language::Portuguese);
631        assert_ne!(en, pt, "PT and EN must produce distinct messages");
632        assert!(pt.contains("mem-x"), "PT must include the variant payload");
633    }
634
635    #[test]
636    fn localized_message_pt_delegates_to_app_error_pt_helper() {
637        use crate::i18n::validation::app_error_pt as pt;
638
639        let cases: Vec<(AppError, String)> = vec![
640            (AppError::Validation("x".into()), pt::validation("x")),
641            (AppError::Duplicate("x".into()), pt::duplicate("x")),
642            (AppError::Conflict("x".into()), pt::conflict("x")),
643            (AppError::NotFound("x".into()), pt::not_found("x")),
644            (
645                AppError::NamespaceError("x".into()),
646                pt::namespace_error("x"),
647            ),
648            (AppError::LimitExceeded("x".into()), pt::limit_exceeded("x")),
649            (AppError::Embedding("x".into()), pt::embedding("x")),
650            (AppError::VecExtension("x".into()), pt::vec_extension("x")),
651            (AppError::DbBusy("x".into()), pt::db_busy("x")),
652            (
653                AppError::BatchPartialFailure {
654                    total: 10,
655                    failed: 3,
656                },
657                pt::batch_partial_failure(10, 3),
658            ),
659            (AppError::LockBusy("x".into()), pt::lock_busy("x")),
660            (
661                AppError::AllSlotsFull {
662                    max: 4,
663                    waited_secs: 60,
664                },
665                pt::all_slots_full(4, 60),
666            ),
667            (
668                AppError::LowMemory {
669                    available_mb: 100,
670                    required_mb: 500,
671                },
672                pt::low_memory(100, 500),
673            ),
674            (
675                AppError::BinaryNotFound {
676                    name: "claude".into(),
677                },
678                pt::binary_not_found("claude"),
679            ),
680            (
681                AppError::RateLimited {
682                    detail: "429".into(),
683                },
684                pt::rate_limited("429"),
685            ),
686            (
687                AppError::Timeout {
688                    operation: "op".into(),
689                    duration_secs: 30,
690                },
691                pt::timeout("op", 30),
692            ),
693        ];
694
695        for (err, expected) in cases {
696            let actual = err.localized_message_for(crate::i18n::Language::Portuguese);
697            assert_eq!(actual, expected, "delegation mismatch");
698        }
699    }
700
701    #[test]
702    fn is_retryable_transient_errors() {
703        assert!(AppError::DbBusy("x".into()).is_retryable());
704        assert!(AppError::LockBusy("x".into()).is_retryable());
705        assert!(AppError::AllSlotsFull {
706            max: 4,
707            waited_secs: 60
708        }
709        .is_retryable());
710        assert!(AppError::LowMemory {
711            available_mb: 100,
712            required_mb: 500
713        }
714        .is_retryable());
715        assert!(AppError::RateLimited {
716            detail: "429".into()
717        }
718        .is_retryable());
719        assert!(AppError::Timeout {
720            operation: "op".into(),
721            duration_secs: 30
722        }
723        .is_retryable());
724    }
725
726    #[test]
727    fn is_retryable_permanent_errors() {
728        assert!(!AppError::Validation("x".into()).is_retryable());
729        assert!(!AppError::NotFound("x".into()).is_retryable());
730        assert!(!AppError::Duplicate("x".into()).is_retryable());
731        assert!(!AppError::Conflict("x".into()).is_retryable());
732        assert!(!AppError::BinaryNotFound { name: "x".into() }.is_retryable());
733    }
734
735    #[test]
736    fn exit_code_new_variants() {
737        assert_eq!(AppError::BinaryNotFound { name: "x".into() }.exit_code(), 1);
738        assert_eq!(AppError::RateLimited { detail: "x".into() }.exit_code(), 1);
739        assert_eq!(
740            AppError::Timeout {
741                operation: "x".into(),
742                duration_secs: 5
743            }
744            .exit_code(),
745            1
746        );
747    }
748
749    #[test]
750    fn app_error_size_does_not_exceed_budget() {
751        let size = std::mem::size_of::<AppError>();
752        assert!(
753            size <= 128,
754            "AppError is {size} bytes — exceeds 128-byte budget; \
755             consider boxing large variants to reduce memcpy cost in Result propagation"
756        );
757    }
758}