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
use crate::commands::*;
use crate::i18n::{current, Language};
use clap::{Parser, Subcommand};
/// Retorna o número máximo de invocações simultâneas permitidas pela heurística de CPU.
fn max_concurrency_ceiling() -> usize {
std::thread::available_parallelism()
.map(|n| n.get() * 2)
.unwrap_or(8)
}
#[derive(Copy, Clone, Debug, clap::ValueEnum)]
pub enum RelationKind {
AppliesTo,
Uses,
DependsOn,
Causes,
Fixes,
Contradicts,
Supports,
Follows,
Related,
Mentions,
Replaces,
TrackedIn,
}
impl RelationKind {
pub fn as_str(&self) -> &'static str {
match self {
Self::AppliesTo => "applies_to",
Self::Uses => "uses",
Self::DependsOn => "depends_on",
Self::Causes => "causes",
Self::Fixes => "fixes",
Self::Contradicts => "contradicts",
Self::Supports => "supports",
Self::Follows => "follows",
Self::Related => "related",
Self::Mentions => "mentions",
Self::Replaces => "replaces",
Self::TrackedIn => "tracked_in",
}
}
}
#[derive(Copy, Clone, Debug, clap::ValueEnum)]
pub enum GraphExportFormat {
Json,
Dot,
Mermaid,
}
#[derive(Parser)]
#[command(name = "sqlite-graphrag")]
#[command(version)]
#[command(about = "Local GraphRAG memory for LLMs in a single SQLite file")]
#[command(arg_required_else_help = true)]
pub struct Cli {
/// Número máximo de invocações CLI simultâneas permitidas (default: 4).
///
/// Limita o semáforo de contagem de slots de concorrência. O valor é restrito
/// ao intervalo [1, 2×nCPUs]. Valores acima do teto são rejeitados com exit 2.
#[arg(long, global = true, value_name = "N")]
pub max_concurrency: Option<usize>,
/// Aguardar até SECONDS por um slot livre antes de desistir (exit 75).
///
/// Útil em pipelines de agentes que fazem retry: a instância faz polling a
/// cada 500 ms até o timeout ou um slot abrir. Default: 300s (5 minutos).
#[arg(long, global = true, value_name = "SECONDS")]
pub wait_lock: Option<u64>,
/// Pular a verificação de memória disponível antes de carregar o modelo.
///
/// Uso exclusivo em testes automatizados onde a alocação real não ocorre.
#[arg(long, global = true, hide = true, default_value_t = false)]
pub skip_memory_guard: bool,
/// Idioma das mensagens humanas (stderr). Aceita `en` ou `pt`.
///
/// Sem a flag, detecta via env `SQLITE_GRAPHRAG_LANG` e depois `LC_ALL`/`LANG`.
/// JSON de stdout é determinístico e idêntico entre idiomas — apenas
/// strings destinadas a humanos são afetadas.
#[arg(long, global = true, value_enum, value_name = "LANG")]
pub lang: Option<crate::i18n::Language>,
/// Fuso horário para campos `*_iso` no JSON de saída (ex: `America/Sao_Paulo`).
///
/// Aceita qualquer nome IANA da IANA Time Zone Database. Sem a flag, usa
/// `SQLITE_GRAPHRAG_DISPLAY_TZ`; se ausente, usa UTC. Não afeta campos epoch inteiros.
#[arg(long, global = true, value_name = "IANA")]
pub tz: Option<chrono_tz::Tz>,
#[command(subcommand)]
pub command: Commands,
}
#[cfg(test)]
mod testes_formato_json_only {
use super::Cli;
use clap::Parser;
#[test]
fn restore_aceita_apenas_format_json() {
assert!(Cli::try_parse_from([
"sqlite-graphrag",
"restore",
"--name",
"mem",
"--version",
"1",
"--format",
"json",
])
.is_ok());
assert!(Cli::try_parse_from([
"sqlite-graphrag",
"restore",
"--name",
"mem",
"--version",
"1",
"--format",
"text",
])
.is_err());
}
#[test]
fn hybrid_search_aceita_apenas_format_json() {
assert!(Cli::try_parse_from([
"sqlite-graphrag",
"hybrid-search",
"query",
"--format",
"json",
])
.is_ok());
assert!(Cli::try_parse_from([
"sqlite-graphrag",
"hybrid-search",
"query",
"--format",
"markdown",
])
.is_err());
}
#[test]
fn remember_recall_rename_vacuum_json_only() {
assert!(Cli::try_parse_from([
"sqlite-graphrag",
"remember",
"--name",
"mem",
"--type",
"project",
"--description",
"desc",
"--format",
"json",
])
.is_ok());
assert!(Cli::try_parse_from([
"sqlite-graphrag",
"remember",
"--name",
"mem",
"--type",
"project",
"--description",
"desc",
"--format",
"text",
])
.is_err());
assert!(
Cli::try_parse_from(["sqlite-graphrag", "recall", "query", "--format", "json",])
.is_ok()
);
assert!(
Cli::try_parse_from(["sqlite-graphrag", "recall", "query", "--format", "text",])
.is_err()
);
assert!(Cli::try_parse_from([
"sqlite-graphrag",
"rename",
"--name",
"old",
"--new-name",
"new",
"--format",
"json",
])
.is_ok());
assert!(Cli::try_parse_from([
"sqlite-graphrag",
"rename",
"--name",
"old",
"--new-name",
"new",
"--format",
"markdown",
])
.is_err());
assert!(Cli::try_parse_from(["sqlite-graphrag", "vacuum", "--format", "json",]).is_ok());
assert!(Cli::try_parse_from(["sqlite-graphrag", "vacuum", "--format", "text",]).is_err());
}
}
impl Cli {
/// Valida flags de concorrência e retorna erro descritivo localizado se inválidas.
///
/// Requer que `crate::i18n::init()` já tenha sido chamado (ocorre antes desta função
/// no fluxo de `main`). Em inglês emite mensagens EN; em português emite PT.
pub fn validate_flags(&self) -> Result<(), String> {
if let Some(n) = self.max_concurrency {
if n == 0 {
return Err(match current() {
Language::English => "--max-concurrency must be >= 1".to_string(),
Language::Portugues => "--max-concurrency deve ser >= 1".to_string(),
});
}
let teto = max_concurrency_ceiling();
if n > teto {
return Err(match current() {
Language::English => format!(
"--max-concurrency {n} exceeds the ceiling of {teto} (2×nCPUs) on this system"
),
Language::Portugues => format!(
"--max-concurrency {n} excede o teto de {teto} (2×nCPUs) neste sistema"
),
});
}
}
Ok(())
}
}
impl Commands {
/// Retorna true para subcomandos que carregam o modelo ONNX localmente.
pub fn is_embedding_heavy(&self) -> bool {
matches!(
self,
Self::Init(_) | Self::Remember(_) | Self::Recall(_) | Self::HybridSearch(_)
)
}
pub fn uses_cli_slot(&self) -> bool {
!matches!(self, Self::Daemon(_))
}
}
#[derive(Subcommand)]
pub enum Commands {
/// Initialize database and download embedding model
Init(init::InitArgs),
/// Run or control the persistent embedding daemon
Daemon(daemon::DaemonArgs),
/// Save a memory with optional entity graph
Remember(remember::RememberArgs),
/// Search memories semantically
Recall(recall::RecallArgs),
/// Read a memory by exact name
Read(read::ReadArgs),
/// List memories with filters
List(list::ListArgs),
/// Soft-delete a memory
Forget(forget::ForgetArgs),
/// Permanently delete soft-deleted memories
Purge(purge::PurgeArgs),
/// Rename a memory preserving history
Rename(rename::RenameArgs),
/// Edit a memory's body or description
Edit(edit::EditArgs),
/// List all versions of a memory
History(history::HistoryArgs),
/// Restore a memory to a previous version
Restore(restore::RestoreArgs),
/// Search using hybrid vector + full-text search
HybridSearch(hybrid_search::HybridSearchArgs),
/// Show database health
Health(health::HealthArgs),
/// Apply pending schema migrations
Migrate(migrate::MigrateArgs),
/// Resolve namespace precedence for the current invocation
NamespaceDetect(namespace_detect::NamespaceDetectArgs),
/// Run PRAGMA optimize on the database
Optimize(optimize::OptimizeArgs),
/// Show database statistics
Stats(stats::StatsArgs),
/// Create a checkpointed copy safe for file sync
SyncSafeCopy(sync_safe_copy::SyncSafeCopyArgs),
/// Run VACUUM after checkpointing the WAL
Vacuum(vacuum::VacuumArgs),
/// Create an explicit relationship between two entities
Link(link::LinkArgs),
/// Remove a specific relationship between two entities
Unlink(unlink::UnlinkArgs),
/// List memories connected via the entity graph
Related(related::RelatedArgs),
/// Export a graph snapshot in json, dot or mermaid
Graph(graph_export::GraphArgs),
/// Remove entities that have no memories and no relationships
CleanupOrphans(cleanup_orphans::CleanupOrphansArgs),
#[command(name = "__debug_schema", hide = true)]
DebugSchema(debug_schema::DebugSchemaArgs),
}
#[derive(Copy, Clone, Debug, clap::ValueEnum)]
pub enum MemoryType {
User,
Feedback,
Project,
Reference,
Decision,
Incident,
Skill,
}
#[cfg(test)]
mod testes_concorrencia_pesada {
use super::*;
#[test]
fn command_heavy_detecta_init_e_embeddings() {
let init = Cli::try_parse_from(["sqlite-graphrag", "init"]).expect("parse init");
assert!(init.command.is_embedding_heavy());
let remember = Cli::try_parse_from([
"sqlite-graphrag",
"remember",
"--name",
"memoria-teste",
"--type",
"project",
"--description",
"desc",
])
.expect("parse remember");
assert!(remember.command.is_embedding_heavy());
let recall =
Cli::try_parse_from(["sqlite-graphrag", "recall", "consulta"]).expect("parse recall");
assert!(recall.command.is_embedding_heavy());
let hybrid = Cli::try_parse_from(["sqlite-graphrag", "hybrid-search", "consulta"])
.expect("parse hybrid");
assert!(hybrid.command.is_embedding_heavy());
}
#[test]
fn command_light_nao_marca_stats() {
let stats = Cli::try_parse_from(["sqlite-graphrag", "stats"]).expect("parse stats");
assert!(!stats.command.is_embedding_heavy());
}
}
impl MemoryType {
pub fn as_str(&self) -> &'static str {
match self {
Self::User => "user",
Self::Feedback => "feedback",
Self::Project => "project",
Self::Reference => "reference",
Self::Decision => "decision",
Self::Incident => "incident",
Self::Skill => "skill",
}
}
}