ragrig 1.1.2

RAG framework for research and prototyping. Zero dependencies, hot-swap any agent at runtime, hybrid BM25+vector retrieval. Default build compiles with cargo build --release and nothing else.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
//! Typed errors for the ragrig library.
//!
//! Consumers can downcast [`anyhow::Error`] to [`RagrigError`] to
//! handle specific failure modes programmatically.
//!
//! # Example
//!
//! ```ignore
//! match chat_agent.generate(prompt).await {
//!     Err(e) => {
//!         if let Some(ce) = e.downcast_ref::<ragrig::RagrigError>() {
//!             eprintln!("{}", ce);
//!             eprintln!("Suggestion: {}", ce.suggested_action());
//!         }
//!     }
//!     Ok(_response) => { /* handle success */ }
//! }
//! ```

use thiserror::Error;

/// An operation was cancelled via a [`Cancel`](crate::progress::Cancel)
/// source (e.g. a GUI cancel button or a REPL escape key).
///
/// Downcast from an [`anyhow::Error`] with
/// [`downcast_ref::<Cancelled>()`](anyhow::Error::downcast_ref) — a
/// dedicated marker type instead of a [`RagrigError`] variant, so new
/// error conditions can ship without touching the frozen error enum.
#[derive(Error, Debug, Clone, Copy, PartialEq, Eq)]
#[error("operation cancelled")]
pub struct Cancelled;

/// Errors that library consumers can match on programmatically.
///
/// New variants may be added in minor releases (or new error conditions
/// ship as marker structs like [`Cancelled`]); downstream matches must
/// keep a wildcard arm.
#[non_exhaustive]
#[derive(Error, Debug, Clone)]
pub enum RagrigError {
    /// The generated prompt exceeded the model's context window.
    #[error("prompt size {current} tokens exceeds model context window of {max} tokens")]
    ContextSizeExceeded {
        /// Number of tokens the prompt required.
        current: usize,
        /// Model's maximum context window (tokens).
        max: usize,
    },
    /// The embedding model is not available locally.
    #[error("embedding model '{model}' not found — is it pulled? Try: ollama pull {model}")]
    EmbedModelNotFound {
        /// Model name that was requested, e.g. "nomic-embed-text".
        model: String,
    },
    /// The vector store file could not be deserialised.
    #[error("vector store at '{path}' is corrupt — delete it and re-index")]
    StoreCorrupt {
        /// Path to the store file that failed to load.
        path: String,
    },
    /// Indexing produced zero chunks — the document corpus may be empty
    /// or no supported file formats were found.
    #[error(
        "no documents found in '{folder}' — it is empty or contains no supported files (PDF, EPUB, HTML, DOCX, Markdown)"
    )]
    NoDocumentsFound {
        /// Folder that was scanned for documents.
        folder: String,
    },
    /// Ollama is not reachable — the server is not running or the
    /// address is wrong.
    #[error(
        "cannot reach Ollama server while trying to {context} — is it running? Try: ollama serve"
    )]
    OllamaUnreachable {
        /// What was being attempted when the connection failed
        /// (e.g. "create chat client", "embed documents").
        context: String,
    },
    /// The chat model produced a generation error that is not a
    /// context-overflow (timeout, model crash, invalid response, …).
    #[error("{backend} ({model}) generation failed: {detail}")]
    GenerationFailed {
        /// Backend name, e.g. "ollama", "deepseek".
        backend: String,
        /// Model that was being used.
        model: String,
        /// The upstream error message.
        detail: String,
    },
    /// The current embedding model is incompatible with the stored index.
    ///
    /// Retrieving cosine similarities across incompatible vector spaces
    /// produces meaningless results.  Re-index with the current embedder
    /// to resolve this.
    #[error(
        "index was created with {stored_model} ({stored_dims} dims) \
         but current embedder is {current_model} ({current_dims} dims)"
    )]
    EmbeddingMismatch {
        /// Model name used to create this index, e.g. "nomic-embed-text".
        stored_model: String,
        /// Dimensionality of the stored embeddings.
        stored_dims: usize,
        /// Model name of the currently active embedder.
        current_model: String,
        /// Dimensionality of the currently active embedder.
        current_dims: usize,
    },
    /// The download from a URL exceeded the configured size limit.
    ///
    /// This protects against unbounded memory consumption when fetching
    /// remote documents.  Increase the limit if you need larger files.
    #[error(
        "download from '{url}' exceeded the {limit_bytes}-byte limit \
         after receiving {received_bytes} bytes"
    )]
    DownloadLimitExceeded {
        /// The URL being fetched.
        url: String,
        /// Size limit in bytes.
        limit_bytes: u64,
        /// Number of bytes received before the limit was hit.
        received_bytes: u64,
    },
}

impl RagrigError {
    /// Prompt size in tokens that triggered the error.
    pub fn current_size(&self) -> usize {
        match self {
            Self::ContextSizeExceeded { current, .. } => *current,
            _ => 0,
        }
    }

    /// Model's maximum context window in tokens.
    pub fn max_size(&self) -> usize {
        match self {
            Self::ContextSizeExceeded { max, .. } => *max,
            _ => 0,
        }
    }

    /// The model name that was not found.
    pub fn model_name(&self) -> &str {
        match self {
            Self::EmbedModelNotFound { model } => model,
            _ => "",
        }
    }

    /// Path to the corrupt store file.
    pub fn store_path(&self) -> &str {
        match self {
            Self::StoreCorrupt { path } => path,
            _ => "",
        }
    }

    /// Folder that produced zero documents.
    pub fn folder(&self) -> &str {
        match self {
            Self::NoDocumentsFound { folder } => folder,
            _ => "",
        }
    }

    /// Suggested context-token budget to avoid the overflow, if this is a
    /// [`Self::ContextSizeExceeded`] error.  Returns `None` for other variants.
    pub fn suggested_context_tokens(&self) -> Option<usize> {
        match self {
            Self::ContextSizeExceeded { max, .. } => Some(max.saturating_sub(512)),
            _ => None,
        }
    }

    /// Log this error at `error` level along with its suggested action.
    ///
    /// Convenience for the common pattern:
    ///
    /// ```ignore
    /// if let Some(re) = err.downcast_ref::<RagrigError>() {
    ///     re.log();
    /// } else {
    ///     error!("something failed: {}", err);
    /// }
    /// ```
    pub fn log(&self) {
        log::error!("{}", self);
        log::error!("  -> {}", self.suggested_action());
    }

    /// Downcast `err` to `RagrigError`.  If found, delegates to
    /// [`log`](Self::log).  Otherwise logs `fallback_msg` with the raw error.
    pub fn log_or(err: &anyhow::Error, fallback_msg: &str) {
        if let Some(re) = err.downcast_ref::<Self>() {
            re.log();
        } else {
            log::error!("{}: {}", fallback_msg, err);
        }
    }

    /// Human-readable recovery hint for this error.
    pub fn suggested_action(&self) -> String {
        match self {
            Self::ContextSizeExceeded { max, .. } => {
                format!(
                    "Reduce the context budget with `/chat context {}` or use a model with a larger window.",
                    max.saturating_sub(512)
                )
            }
            Self::EmbedModelNotFound { model } => {
                format!("Pull the model first: ollama pull {model}")
            }
            Self::StoreCorrupt { path } => {
                format!("Delete the corrupt store at '{path}' and re-index with `/embed index`.")
            }
            Self::NoDocumentsFound { folder } => {
                format!("Add PDF, EPUB, or HTML files to '{folder}'.")
            }
            Self::OllamaUnreachable { .. } => {
                "Start Ollama with `ollama serve` and try again.".into()
            }
            Self::GenerationFailed { model, .. } => {
                format!(
                    "Check that '{model}' is pulled (`ollama pull {model}`) and that it fits in VRAM. \
                     Try a smaller model or restart Ollama."
                )
            }
            Self::EmbeddingMismatch { current_model, .. } => {
                format!(
                    "Re-index your documents with the current embedder '{current_model}', \
                     or switch back to the original embedding model."
                )
            }
            Self::DownloadLimitExceeded { limit_bytes, .. } => {
                format!(
                    "Increase the download limit (currently {limit_bytes} bytes) or \
                     fetch the document from a different corpus."
                )
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn context_size_exceeded_accessors() {
        let err = RagrigError::ContextSizeExceeded {
            current: 5000,
            max: 4096,
        };
        assert_eq!(err.current_size(), 5000);
        assert_eq!(err.max_size(), 4096);
    }

    #[test]
    fn context_size_exceeded_display() {
        let err = RagrigError::ContextSizeExceeded {
            current: 5000,
            max: 4096,
        };
        let msg = err.to_string();
        assert!(msg.contains("5000"));
        assert!(msg.contains("4096"));
    }

    #[test]
    fn embed_model_not_found_display() {
        let err = RagrigError::EmbedModelNotFound {
            model: "nomic-embed-text:latest".into(),
        };
        let msg = err.to_string();
        assert!(msg.contains("nomic-embed-text"));
        assert!(msg.contains("ollama pull"));
    }

    #[test]
    fn store_corrupt_display() {
        let err = RagrigError::StoreCorrupt {
            path: "/tmp/store".into(),
        };
        let msg = err.to_string();
        assert!(msg.contains("/tmp/store"));
        assert!(msg.contains("corrupt"));
    }

    #[test]
    fn no_documents_found_display() {
        let err = RagrigError::NoDocumentsFound {
            folder: "/docs".into(),
        };
        let msg = err.to_string();
        assert!(msg.contains("/docs"));
        assert!(msg.contains("no documents"));
    }

    #[test]
    fn ollama_unreachable_display() {
        let err = RagrigError::OllamaUnreachable {
            context: "create chat client".into(),
        };
        let msg = err.to_string();
        assert!(msg.contains("cannot reach Ollama"));
        assert!(msg.contains("ollama serve"));
    }

    #[test]
    fn generation_failed_display() {
        let err = RagrigError::GenerationFailed {
            backend: "ollama".into(),
            model: "llama3.2".into(),
            detail: "connection timed out".into(),
        };
        let msg = err.to_string();
        assert!(msg.contains("ollama"));
        assert!(msg.contains("llama3.2"));
        assert!(msg.contains("connection timed out"));
    }

    #[test]
    fn embedding_mismatch_display() {
        let err = RagrigError::EmbeddingMismatch {
            stored_model: "nomic-embed-text".into(),
            stored_dims: 768,
            current_model: "bge-large".into(),
            current_dims: 1024,
        };
        let msg = err.to_string();
        assert!(msg.contains("nomic-embed-text"));
        assert!(msg.contains("768"));
        assert!(msg.contains("bge-large"));
        assert!(msg.contains("1024"));
    }

    #[test]
    fn download_limit_exceeded_display() {
        let err = RagrigError::DownloadLimitExceeded {
            url: "https://example.com/big.pdf".into(),
            limit_bytes: 1048576,
            received_bytes: 1048577,
        };
        let msg = err.to_string();
        assert!(msg.contains("example.com"));
        assert!(msg.contains("1048576"));
    }

    #[test]
    fn downcast_from_anyhow() {
        let err = anyhow::Error::new(RagrigError::ContextSizeExceeded {
            current: 100,
            max: 50,
        });
        let downcast = err.downcast_ref::<RagrigError>();
        assert!(downcast.is_some());
        assert_eq!(downcast.unwrap().current_size(), 100);
    }

    #[test]
    fn accessors_return_default_on_wrong_variant() {
        let err = RagrigError::NoDocumentsFound { folder: "x".into() };
        assert_eq!(err.current_size(), 0);
        assert_eq!(err.max_size(), 0);
        assert_eq!(err.store_path(), "");
        assert_eq!(err.model_name(), "");
    }

    #[test]
    fn suggested_context_tokens_for_overflow() {
        let err = RagrigError::ContextSizeExceeded {
            current: 5000,
            max: 4096,
        };
        assert_eq!(err.suggested_context_tokens(), Some(4096 - 512));
    }

    #[test]
    fn suggested_context_tokens_none_for_other_variants() {
        let err = RagrigError::NoDocumentsFound { folder: "x".into() };
        assert_eq!(err.suggested_context_tokens(), None);
    }

    #[test]
    fn suggested_action_not_empty_for_all_variants() {
        let variants = [
            RagrigError::ContextSizeExceeded {
                current: 100,
                max: 50,
            },
            RagrigError::EmbedModelNotFound { model: "x".into() },
            RagrigError::StoreCorrupt {
                path: "/tmp".into(),
            },
            RagrigError::NoDocumentsFound {
                folder: "/tmp".into(),
            },
            RagrigError::OllamaUnreachable {
                context: "test".into(),
            },
            RagrigError::GenerationFailed {
                backend: "ollama".into(),
                model: "x".into(),
                detail: "test".into(),
            },
            RagrigError::EmbeddingMismatch {
                stored_model: "nomic-embed-text".into(),
                stored_dims: 768,
                current_model: "bge-large".into(),
                current_dims: 1024,
            },
            RagrigError::DownloadLimitExceeded {
                url: "https://example.com".into(),
                limit_bytes: 52428800,
                received_bytes: 52428801,
            },
        ];
        for v in &variants {
            assert!(!v.suggested_action().is_empty(), "empty action for {v:?}");
        }
    }
}