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
//! Compile-time constants shared across the crate.
//!
//! Grouped into embedding configuration, length and size limits, SQLite
//! pragmas and retrieval tuning knobs. Values are taken from the PRD and
//! must stay in sync with the migrations under `migrations/`.
//!
//! ## Cálculo dinâmico de permits de concorrência
//!
//! O número máximo de instâncias simultâneas pode ser ajustado em runtime
//! usando a fórmula:
//!
//! ```text
//! permits = min(cpus, available_memory_mb / EMBEDDING_LOAD_EXPECTED_RSS_MB) * 0.5
//! ```
//!
//! onde `available_memory_mb` é obtido via `sysinfo::System::available_memory()`
//! convertido para MiB. O resultado é limitado superiormente por
//! `MAX_CONCURRENT_CLI_INSTANCES` e inferiorizado em 1.
/// Embedding vector dimensionality produced by `multilingual-e5-small`.
pub const EMBEDDING_DIM: usize = 384;
/// Default `fastembed` model identifier used by `remember` and `recall`.
pub const FASTEMBED_MODEL_DEFAULT: &str = "multilingual-e5-small";
/// Batch size for `fastembed` encoding calls.
pub const FASTEMBED_BATCH_SIZE: usize = 32;
/// Maximum byte length for a memory `name` field in kebab-case.
pub const MAX_MEMORY_NAME_LEN: usize = 80;
/// Maximum character length for a memory `description` field.
pub const MAX_MEMORY_DESCRIPTION_LEN: usize = 500;
/// Hard upper bound on memory `body` length in characters.
pub const MAX_MEMORY_BODY_LEN: usize = 20_000;
/// Body character count above which the body is split into chunks.
pub const MAX_BODY_CHARS_BEFORE_CHUNK: usize = 8_000;
/// Maximum attempts when a statement returns `SQLITE_BUSY`.
pub const MAX_SQLITE_BUSY_RETRIES: u32 = 5;
/// Query timeout applied to statements in milliseconds.
pub const QUERY_TIMEOUT_MILLIS: u64 = 5_000;
/// Jaccard threshold above which two memories are considered fuzzy duplicates.
pub const DEDUP_FUZZY_THRESHOLD: f64 = 0.8;
/// Cosine distance threshold below which two memories are semantic duplicates.
pub const DEDUP_SEMANTIC_THRESHOLD: f32 = 0.1;
/// Maximum number of hops allowed in graph traversals.
pub const MAX_GRAPH_HOPS: u32 = 2;
/// Minimum relationship weight required for traversal inclusion.
pub const MIN_RELATION_WEIGHT: f64 = 0.3;
/// Default traversal depth for `related` when `--hops` is omitted.
pub const DEFAULT_MAX_HOPS: u32 = 2;
/// Default minimum weight filter applied during graph traversal.
pub const DEFAULT_MIN_WEIGHT: f64 = 0.3;
/// Default weight assigned to newly created relationships.
pub const DEFAULT_RELATION_WEIGHT: f64 = 0.5;
/// Default `k` used by `recall` when the caller omits `--k`.
pub const DEFAULT_K_RECALL: usize = 10;
/// Default `k` for memory KNN searches when the caller omits `--k`.
pub const K_MEMORIES_DEFAULT: usize = 10;
/// Default `k` for entity KNN searches during graph expansion.
pub const K_ENTITIES_SEARCH: usize = 5;
/// Upper bound on distinct entities persisted per memory.
pub const MAX_ENTITIES_PER_MEMORY: usize = 30;
/// Upper bound on distinct relationships persisted per memory.
pub const MAX_RELATIONSHIPS_PER_MEMORY: usize = 50;
/// Character length of the description preview shown in `list` output.
pub const TEXT_DESCRIPTION_PREVIEW_LEN: usize = 100;
/// `PRAGMA busy_timeout` value applied on every connection.
pub const BUSY_TIMEOUT_MILLIS: i32 = 5_000;
/// `PRAGMA cache_size` value in kibibytes (negative means KiB).
pub const CACHE_SIZE_KB: i32 = -64_000;
/// `PRAGMA mmap_size` value in bytes applied to each connection.
pub const MMAP_SIZE_BYTES: i64 = 268_435_456;
/// `PRAGMA wal_autocheckpoint` threshold in pages.
pub const WAL_AUTOCHECKPOINT_PAGES: i32 = 1_000;
/// Default `k` constant used by Reciprocal Rank Fusion in `hybrid-search`.
pub const RRF_K_DEFAULT: u32 = 60;
/// Chunk size expressed in tokens for body splitting.
pub const CHUNK_SIZE_TOKENS: usize = 400;
/// Token overlap between consecutive chunks.
pub const CHUNK_OVERLAP_TOKENS: usize = 50;
/// Timeout in milliseconds for a single ping probe against the daemon socket.
pub const DAEMON_PING_TIMEOUT_MS: u64 = 10;
/// Idle duration in seconds before the daemon shuts itself down.
pub const DAEMON_IDLE_SHUTDOWN_SECS: u64 = 600;
/// Prefix prepended to bodies before embedding as required by E5 models.
pub const PASSAGE_PREFIX: &str = "passage: ";
/// Prefix prepended to queries before embedding as required by E5 models.
pub const QUERY_PREFIX: &str = "query: ";
/// Crate version string sourced from `CARGO_PKG_VERSION` at build time.
pub const SQLITE_GRAPHRAG_VERSION: &str = env!;
/// PRD-canonical regex que valida nomes e namespaces. Permite 1 char `[a-z0-9]`
/// OU string de 2-80 chars começando com letra e terminando com letra/dígito,
/// contendo apenas `[a-z0-9-]`. Rejeita prefixo `__` (internal reserved).
pub const NAME_SLUG_REGEX: &str = r"^[a-z][a-z0-9-]{0,78}[a-z0-9]$|^[a-z0-9]$";
/// Retenção padrão (dias) usada por `purge` quando `--retention-days` é omitido.
pub const PURGE_RETENTION_DAYS_DEFAULT: u32 = 90;
/// Limite máximo de namespaces ativos (deleted_at IS NULL) simultâneos. Exit 5 ao exceder.
pub const MAX_NAMESPACES_ACTIVE: u32 = 100;
/// Máximo de tokens aceito por embedding input antes de chunking.
pub const EMBEDDING_MAX_TOKENS: usize = 512;
/// Limite máximo de resultados da CTE recursiva de grafo em `recall`.
pub const K_GRAPH_MATCHES_LIMIT: usize = 20;
/// Default `--limit` para `list` quando omitido.
pub const K_LIST_DEFAULT_LIMIT: usize = 100;
/// Default `--limit` para `graph entities` quando omitido.
pub const K_GRAPH_ENTITIES_DEFAULT_LIMIT: usize = 50;
/// Default `--limit` para `related` quando omitido.
pub const K_RELATED_DEFAULT_LIMIT: usize = 10;
/// Default `--limit` para `history` quando omitido.
pub const K_HISTORY_DEFAULT_LIMIT: usize = 20;
/// Peso padrão da contribuição vetorial na fórmula RRF de `hybrid-search`.
pub const WEIGHT_VEC_DEFAULT: f64 = 1.0;
/// Peso padrão da contribuição textual BM25 na fórmula RRF de `hybrid-search`.
pub const WEIGHT_FTS_DEFAULT: f64 = 1.0;
/// Tamanho em caracteres do preview do body emitido em formatos text/markdown.
pub const TEXT_BODY_PREVIEW_LEN: usize = 200;
/// Valor default injetado em ORT_NUM_THREADS quando não definido pelo usuário.
pub const ORT_NUM_THREADS_DEFAULT: &str = "1";
/// Valor default injetado em ORT_INTRA_OP_NUM_THREADS quando não definido.
pub const ORT_INTRA_OP_NUM_THREADS_DEFAULT: &str = "1";
/// Valor default injetado em OMP_NUM_THREADS quando não definido pelo usuário.
pub const OMP_NUM_THREADS_DEFAULT: &str = "1";
/// Exit code para falha parcial de batch (PRD linha 1822). Conflita com DbBusy em v1.x;
/// em v2.0.0 DbBusy migra para 15 e este código assume 13 conforme PRD.
pub const BATCH_PARTIAL_FAILURE_EXIT_CODE: i32 = 13;
/// Exit code para DbBusy em v2.0.0 (migrado de 13 para liberar 13 para batch failure).
pub const DB_BUSY_EXIT_CODE: i32 = 15;
/// Filename used for the advisory exclusive lock that prevents parallel invocations.
pub const CLI_LOCK_FILE: &str = "cli.lock";
/// Polling interval em milliseconds usado por `--wait-lock` entre tentativas de `try_lock_exclusive`.
pub const CLI_LOCK_POLL_INTERVAL_MS: u64 = 500;
/// Process exit code returned when the lock is busy and no wait was requested (EX_TEMPFAIL).
pub const CLI_LOCK_EXIT_CODE: i32 = 75;
/// Número máximo de instâncias CLI em execução simultânea.
///
/// Alinhado com `DAEMON_MAX_CONCURRENT_CLIENTS` do PRD. Limita o semáforo de
/// contagem em [`crate::lock`] para evitar sobrecarga de memória quando múltiplas
/// invocações paralelas tentam carregar o modelo ONNX simultaneamente.
pub const MAX_CONCURRENT_CLI_INSTANCES: usize = 4;
/// Memória disponível mínima em MiB exigida antes de iniciar o carregamento do modelo.
///
/// Se `sysinfo::System::available_memory() / 1_048_576` estiver abaixo deste
/// valor, a invocação é abortada com [`crate::errors::AppError::LowMemory`]
/// (exit code [`LOW_MEMORY_EXIT_CODE`]).
pub const MIN_AVAILABLE_MEMORY_MB: u64 = 2_048;
/// Tempo máximo em segundos que uma instância aguarda para adquirir um slot de concorrência.
///
/// Passado como default de `--max-wait-secs` na CLI. Após esgotar este limite,
/// a invocação retorna [`crate::errors::AppError::AllSlotsFull`] com exit code
/// [`CLI_LOCK_EXIT_CODE`] (75).
pub const CLI_LOCK_DEFAULT_WAIT_SECS: u64 = 300;
/// RSS esperado em MiB de uma única instância com o modelo ONNX carregado via fastembed.
///
/// Usado na fórmula `min(cpus, available_memory_mb / EMBEDDING_LOAD_EXPECTED_RSS_MB) * 0.5`
/// para calcular o número dinâmico de permits.
///
/// Valor calibrado em 2026-04-23 com `/usr/bin/time -v` sobre `sqlite-graphrag v1.0.3`
/// nos comandos pesados `remember`, `recall` e `hybrid-search`, todos com pico de RSS
/// próximo de 1.03 GiB por processo. O valor abaixo arredonda para cima com margem defensiva.
pub const EMBEDDING_LOAD_EXPECTED_RSS_MB: u64 = 1_100;
/// Process exit code retornado quando memória disponível está abaixo de [`MIN_AVAILABLE_MEMORY_MB`].
///
/// Valor `77` é `EX_NOPERM` na glibc sysexits, reutilizado aqui para indicar
/// "recurso de sistema insuficiente para prosseguir".
pub const LOW_MEMORY_EXIT_CODE: i32 = 77;
/// Valor canônico de `PRAGMA user_version` gravado após migrações.
///
/// Deve permanecer em sincronia com o identificador legível-por-humanos
/// da versão do schema. Refinery usa sua própria tabela de histórico;
/// `user_version` é um campo auxiliar de diagnóstico para ferramentas
/// externas (ex: `sqlite3 db.sqlite "PRAGMA user_version"`).
pub const SCHEMA_USER_VERSION: i64 = 49;