1use crate::chunking;
4use crate::cli::MemoryType;
5use crate::entity_type::EntityType;
6use crate::errors::AppError;
7use crate::i18n::errors_msg;
8use crate::output::{self, JsonOutputFormat, RememberResponse};
9use crate::paths::AppPaths;
10use crate::storage::chunks as storage_chunks;
11use crate::storage::connection::{ensure_schema, open_rw};
12use crate::storage::entities::{NewEntity, NewRelationship};
13use crate::storage::memories::NewMemory;
14use crate::storage::{entities, memories, urls as storage_urls, versions};
15use serde::Deserialize;
16
17fn compute_chunks_persisted(chunks_created: usize) -> usize {
26 if chunks_created > 1 {
27 chunks_created
28 } else {
29 0
30 }
31}
32
33#[derive(clap::Args)]
34#[command(after_long_help = "EXAMPLES:\n \
35 # Create a memory with inline body\n \
36 sqlite-graphrag remember --name design-auth --type decision \\\n \
37 --description \"auth design\" --body \"JWT for stateless auth\"\n\n \
38 # Create with curated graph via --graph-stdin\n \
39 echo '{\"body\":\"...\",\"entities\":[],\"relationships\":[]}' | \\\n \
40 sqlite-graphrag remember --name my-mem --type note --description \"desc\" --graph-stdin\n\n \
41 # Enable GLiNER NER extraction with --graph-stdin\n \
42 echo '{\"body\":\"Alice from Microsoft...\",\"entities\":[],\"relationships\":[]}' | \\\n \
43 sqlite-graphrag remember --name ner-test --type note --description \"test\" \\\n \
44 --graph-stdin --enable-ner --gliner-variant int8\n\n \
45 # Idempotent upsert with --force-merge\n \
46 sqlite-graphrag remember --name my-mem --type note --description \"updated\" \\\n \
47 --body \"new content\" --force-merge\n\n\
48NOTE:\n \
49 remember does NOT accept positional arguments.\n \
50 Use --body \"text\" for inline content\n \
51 Use --body-file path for file content\n \
52 Use --body-stdin for piped content\n \
53 Use --graph-stdin for JSON with entities and relationships\n\n\
54ENTITY TYPES (for --graph-stdin entities, NOT memory --type):\n \
55 concept, tool, person, file, project, decision, incident,\n \
56 organization, location, date, dashboard, issue_tracker, memory\n \
57 WARNING: reference, skill, document, note, user, feedback are\n \
58 MEMORY types only — NOT valid for entities.\n \
59 Mapping: reference→concept, document→file, user→person")]
60pub struct RememberArgs {
61 #[arg(long)]
64 pub name: String,
65 #[arg(
66 long,
67 value_enum,
68 long_help = "Memory kind stored in `memories.type`. Required when creating a new memory. Optional with --force-merge: if omitted the existing memory type is inherited. This is NOT the graph `entity_type` used in `--entities-file`. Valid values: user, feedback, project, reference, decision, incident, skill, document, note."
69 )]
70 pub r#type: Option<MemoryType>,
71 #[arg(long)]
74 pub description: Option<String>,
75 #[arg(
78 long,
79 help = "Inline body content (max 500 KB / 512000 bytes; for larger inputs split into multiple memories or use --body-file)",
80 conflicts_with_all = ["body_file", "body_stdin", "graph_stdin"]
81 )]
82 pub body: Option<String>,
83 #[arg(
84 long,
85 help = "Read body from a file instead of --body",
86 conflicts_with_all = ["body", "body_stdin", "graph_stdin"]
87 )]
88 pub body_file: Option<std::path::PathBuf>,
89 #[arg(
92 long,
93 conflicts_with_all = ["body", "body_file", "graph_stdin"]
94 )]
95 pub body_stdin: bool,
96 #[arg(
97 long,
98 help = "JSON file containing entities to associate with this memory"
99 )]
100 pub entities_file: Option<std::path::PathBuf>,
101 #[arg(
102 long,
103 help = "JSON file containing relationships to associate with this memory"
104 )]
105 pub relationships_file: Option<std::path::PathBuf>,
106 #[arg(
107 long,
108 help = "Read graph JSON (body + entities + relationships) from stdin",
109 conflicts_with_all = [
110 "body",
111 "body_file",
112 "body_stdin",
113 "entities_file",
114 "relationships_file"
115 ]
116 )]
117 pub graph_stdin: bool,
118 #[arg(
119 long,
120 help = "Namespace (env: SQLITE_GRAPHRAG_NAMESPACE, default: global)"
121 )]
122 pub namespace: Option<String>,
123 #[arg(long)]
125 pub metadata: Option<String>,
126 #[arg(long, help = "JSON file containing metadata key-value pairs")]
127 pub metadata_file: Option<std::path::PathBuf>,
128 #[arg(long)]
129 pub force_merge: bool,
130 #[arg(
131 long,
132 value_name = "EPOCH_OR_RFC3339",
133 value_parser = crate::parsers::parse_expected_updated_at,
134 long_help = "Optimistic lock: reject if updated_at does not match. \
135Accepts Unix epoch (e.g. 1700000000) or RFC 3339 (e.g. 2026-04-19T12:00:00Z)."
136 )]
137 pub expected_updated_at: Option<i64>,
138 #[arg(
139 long,
140 env = "SQLITE_GRAPHRAG_ENABLE_NER",
141 value_parser = crate::parsers::parse_bool_flexible,
142 action = clap::ArgAction::Set,
143 num_args = 0..=1,
144 default_missing_value = "true",
145 default_value = "false",
146 help = "Enable automatic GLiNER NER entity/relationship extraction from body"
147 )]
148 pub enable_ner: bool,
149 #[arg(
150 long,
151 env = "SQLITE_GRAPHRAG_GLINER_VARIANT",
152 default_value = "fp32",
153 help = "GLiNER model variant: fp32 (1.1GB, best quality), fp16 (580MB), int8 (349MB, fastest but may miss entities on short texts), q4, q4f16"
154 )]
155 pub gliner_variant: String,
156 #[arg(long, hide = true)]
157 pub skip_extraction: bool,
158 #[arg(
162 long,
163 default_value_t = false,
164 help = "Explicitly clear body content during --force-merge (without this flag, an empty body is ignored and the existing body is kept)"
165 )]
166 pub clear_body: bool,
167 #[arg(
169 long,
170 default_value_t = false,
171 help = "Validate input and report planned actions without persisting"
172 )]
173 pub dry_run: bool,
174 #[arg(long)]
176 pub session_id: Option<String>,
177 #[arg(long, value_enum, default_value_t = JsonOutputFormat::Json)]
178 pub format: JsonOutputFormat,
179 #[arg(long, hide = true, help = "No-op; JSON is always emitted on stdout")]
180 pub json: bool,
181 #[arg(long, env = "SQLITE_GRAPHRAG_DB_PATH")]
182 pub db: Option<String>,
183 #[arg(long, default_value_t = crate::constants::DEFAULT_MAX_RSS_MB,
185 help = "Maximum process RSS in MiB; abort if exceeded during embedding (default: 8192)")]
186 pub max_rss_mb: u64,
187 #[arg(long, default_value_t = 50, value_name = "N")]
190 pub max_entity_degree: u32,
191}
192
193#[derive(Deserialize, Default)]
194#[serde(deny_unknown_fields)]
195struct GraphInput {
196 #[serde(default)]
197 body: Option<String>,
198 #[serde(default)]
199 entities: Vec<NewEntity>,
200 #[serde(default)]
201 relationships: Vec<NewRelationship>,
202}
203
204fn normalize_and_validate_graph_input(graph: &mut GraphInput) -> Result<(), AppError> {
205 for rel in &mut graph.relationships {
206 rel.relation = crate::parsers::normalize_relation(&rel.relation);
207 if let Err(e) = crate::parsers::validate_relation_format(&rel.relation) {
208 return Err(AppError::Validation(format!(
209 "{e} for relationship '{}' -> '{}'",
210 rel.source, rel.target
211 )));
212 }
213 crate::parsers::warn_if_non_canonical(&rel.relation);
214 if !(0.0..=1.0).contains(&rel.strength) {
215 return Err(AppError::Validation(format!(
216 "invalid strength {} for relationship '{}' -> '{}'; expected value in [0.0, 1.0]",
217 rel.strength, rel.source, rel.target
218 )));
219 }
220 }
221
222 Ok(())
223}
224
225#[tracing::instrument(skip_all, level = "debug", name = "remember")]
226pub fn run(args: RememberArgs) -> Result<(), AppError> {
227 use crate::constants::*;
228
229 let inicio = std::time::Instant::now();
230 let _ = args.format;
231 tracing::debug!(target: "remember", name = %args.name, "persisting memory");
232 let namespace = crate::namespace::resolve_namespace(args.namespace.as_deref())?;
233
234 let original_name = args.name.clone();
238
239 let normalized_name = {
243 let lower = args.name.to_lowercase().replace(['_', ' '], "-");
244 let trimmed = lower.trim_matches('-').to_string();
245 if trimmed != args.name {
246 tracing::warn!(target: "remember",
247 original = %args.name,
248 normalized = %trimmed,
249 "name auto-normalized to kebab-case"
250 );
251 }
252 trimmed
253 };
254 let name_was_normalized = normalized_name != original_name;
255
256 if normalized_name.is_empty() {
257 return Err(AppError::Validation(
258 "name cannot be empty after normalization (input was blank or contained only hyphens/underscores/spaces)".to_string(),
259 ));
260 }
261 if normalized_name.len() > MAX_MEMORY_NAME_LEN {
262 return Err(AppError::LimitExceeded(
263 crate::i18n::validation::name_length(MAX_MEMORY_NAME_LEN),
264 ));
265 }
266
267 if normalized_name.starts_with("__") {
268 return Err(AppError::Validation(
269 crate::i18n::validation::reserved_name(),
270 ));
271 }
272
273 {
274 let slug_re = crate::constants::name_slug_regex();
275 if !slug_re.is_match(&normalized_name) {
276 return Err(AppError::Validation(crate::i18n::validation::name_kebab(
277 &normalized_name,
278 )));
279 }
280 }
281
282 if let Some(ref desc) = args.description {
283 if desc.len() > MAX_MEMORY_DESCRIPTION_LEN {
284 return Err(AppError::Validation(
285 crate::i18n::validation::description_exceeds(MAX_MEMORY_DESCRIPTION_LEN),
286 ));
287 }
288 }
289
290 let mut raw_body = if let Some(b) = args.body {
291 b
292 } else if let Some(ref path) = args.body_file {
293 let file_size = std::fs::metadata(path).map_err(AppError::Io)?.len();
294 if file_size > MAX_MEMORY_BODY_LEN as u64 {
295 return Err(AppError::LimitExceeded(
296 crate::i18n::validation::body_exceeds(MAX_MEMORY_BODY_LEN),
297 ));
298 }
299 match std::fs::read_to_string(path) {
300 Ok(s) => s,
301 Err(e) if e.kind() == std::io::ErrorKind::InvalidData => {
302 let bytes = std::fs::read(path).map_err(AppError::Io)?;
303 tracing::warn!(target: "remember", "body file contains invalid UTF-8; replacing invalid sequences");
304 String::from_utf8_lossy(&bytes).into_owned()
305 }
306 Err(e) => return Err(AppError::Io(e)),
307 }
308 } else if args.body_stdin || args.graph_stdin {
309 crate::stdin_helper::read_stdin_with_timeout(60)?
310 } else {
311 String::new()
312 };
313
314 let mut entities_provided_externally =
315 args.entities_file.is_some() || args.relationships_file.is_some();
316
317 let mut graph = GraphInput::default();
318 if let Some(path) = args.entities_file {
319 let file_size = std::fs::metadata(&path).map_err(AppError::Io)?.len();
320 if file_size > MAX_MEMORY_BODY_LEN as u64 {
321 return Err(AppError::LimitExceeded(
322 crate::i18n::validation::body_exceeds(MAX_MEMORY_BODY_LEN),
323 ));
324 }
325 let content = std::fs::read_to_string(&path).map_err(AppError::Io)?;
326 graph.entities = serde_json::from_str(&content)?;
327 }
328 if let Some(path) = args.relationships_file {
329 let file_size = std::fs::metadata(&path).map_err(AppError::Io)?.len();
330 if file_size > MAX_MEMORY_BODY_LEN as u64 {
331 return Err(AppError::LimitExceeded(
332 crate::i18n::validation::body_exceeds(MAX_MEMORY_BODY_LEN),
333 ));
334 }
335 let content = std::fs::read_to_string(&path).map_err(AppError::Io)?;
336 graph.relationships = serde_json::from_str(&content)?;
337 }
338 if args.graph_stdin {
339 graph = serde_json::from_str::<GraphInput>(&raw_body).map_err(|e| {
340 AppError::Validation(format!("invalid JSON payload on --graph-stdin: {e}"))
341 })?;
342 raw_body = graph.body.take().unwrap_or_default();
343 }
344 if args.graph_stdin && !graph.entities.is_empty() {
345 entities_provided_externally = true;
346 }
347
348 if graph.entities.len() > max_entities_per_memory() {
349 return Err(AppError::LimitExceeded(errors_msg::entity_limit_exceeded(
350 max_entities_per_memory(),
351 )));
352 }
353 let mut relationships_truncated = false;
354 let rel_cap = max_relationships_per_memory();
355 if graph.relationships.len() > rel_cap {
356 tracing::warn!(target: "remember",
357 count = graph.relationships.len(),
358 cap = rel_cap,
359 "truncating relationships to cap"
360 );
361 graph.relationships.truncate(rel_cap);
362 relationships_truncated = true;
363 }
364 normalize_and_validate_graph_input(&mut graph)?;
365
366 if raw_body.len() > MAX_MEMORY_BODY_LEN {
367 return Err(AppError::LimitExceeded(
368 crate::i18n::validation::body_exceeds(MAX_MEMORY_BODY_LEN),
369 ));
370 }
371
372 let body_will_be_preserved = args.force_merge && raw_body.trim().is_empty() && !args.clear_body;
377 if !entities_provided_externally
378 && graph.entities.is_empty()
379 && raw_body.trim().is_empty()
380 && !body_will_be_preserved
381 && !args.clear_body
382 {
383 return Err(AppError::Validation(crate::i18n::validation::empty_body()));
384 }
385
386 let metadata: serde_json::Value = if let Some(m) = args.metadata {
387 serde_json::from_str(&m)?
388 } else if let Some(path) = args.metadata_file {
389 let file_size = std::fs::metadata(&path).map_err(AppError::Io)?.len();
390 if file_size > MAX_MEMORY_BODY_LEN as u64 {
391 return Err(AppError::LimitExceeded(
392 crate::i18n::validation::body_exceeds(MAX_MEMORY_BODY_LEN),
393 ));
394 }
395 let content = std::fs::read_to_string(&path).map_err(AppError::Io)?;
396 serde_json::from_str(&content)?
397 } else {
398 serde_json::json!({})
399 };
400
401 let mut body_hash = blake3::hash(raw_body.as_bytes()).to_hex().to_string();
402 let mut snippet: String = raw_body.chars().take(200).collect();
403
404 let paths = AppPaths::resolve(args.db.as_deref())?;
405 paths.ensure_dirs()?;
406
407 let mut extraction_method: Option<String> = None;
409 let mut extracted_urls: Vec<crate::extraction::ExtractedUrl> = Vec::with_capacity(4);
410 if args.enable_ner && args.skip_extraction {
411 return Err(AppError::Validation(
412 "--enable-ner and --skip-extraction are mutually exclusive; remove one".to_string(),
413 ));
414 }
415 if args.skip_extraction && !args.enable_ner {
416 tracing::warn!(
423 "--skip-extraction is deprecated since v1.0.45 and has no effect (NER is disabled by default); remove this flag to silence the warning"
424 );
425 }
426 let gliner_variant: crate::extraction::GlinerVariant =
427 args.gliner_variant.parse().unwrap_or_else(|e| {
428 tracing::warn!(target: "remember", error = %e, "invalid --gliner-variant, defaulting to fp32");
429 crate::extraction::GlinerVariant::Fp32
430 });
431 if args.enable_ner && graph.entities.is_empty() && !raw_body.trim().is_empty() {
432 match crate::extraction::extract_graph_auto(&raw_body, &paths, gliner_variant) {
433 Ok(extracted) => {
434 extraction_method = Some(extracted.extraction_method.clone());
435 extracted_urls = extracted.urls;
436 graph.entities = extracted.entities;
437 graph.relationships = extracted.relationships;
438 relationships_truncated = extracted.relationships_truncated;
439
440 if graph.entities.len() > max_entities_per_memory() {
441 graph.entities.truncate(max_entities_per_memory());
442 }
443 if graph.relationships.len() > max_relationships_per_memory() {
444 relationships_truncated = true;
445 graph.relationships.truncate(max_relationships_per_memory());
446 }
447 normalize_and_validate_graph_input(&mut graph)?;
448 }
449 Err(e) => {
450 tracing::warn!(target: "remember", error = %e, "auto-extraction failed, graceful degradation");
451 extraction_method = Some("none:extraction-failed".to_string());
452 }
453 }
454 }
455
456 let mut conn = open_rw(&paths.db)?;
457 ensure_schema(&mut conn)?;
458
459 if args.dry_run {
461 let existing = memories::find_by_name(&conn, &namespace, &normalized_name)?;
462 let planned_action = if existing.is_some() && args.force_merge {
463 "would_update"
464 } else {
465 "would_create"
466 };
467 output::emit_json(&serde_json::json!({
468 "dry_run": true,
469 "name": normalized_name,
470 "namespace": namespace,
471 "planned_action": planned_action,
472 }))?;
473 return Ok(());
474 }
475
476 {
477 use crate::constants::MAX_NAMESPACES_ACTIVE;
478 let active_count: u32 = conn.query_row(
479 "SELECT COUNT(DISTINCT namespace) FROM memories WHERE deleted_at IS NULL",
480 [],
481 |r| r.get::<_, i64>(0).map(|v| v as u32),
482 )?;
483 let ns_exists: bool = conn.query_row(
484 "SELECT EXISTS(SELECT 1 FROM memories WHERE namespace = ?1 AND deleted_at IS NULL)",
485 rusqlite::params![namespace],
486 |r| r.get::<_, i64>(0).map(|v| v > 0),
487 )?;
488 if !ns_exists && active_count >= MAX_NAMESPACES_ACTIVE {
489 return Err(AppError::NamespaceError(format!(
490 "active namespace limit of {MAX_NAMESPACES_ACTIVE} reached while trying to create '{namespace}'"
491 )));
492 }
493 }
494
495 if let Some((sd_id, true)) =
497 memories::find_by_name_any_state(&conn, &namespace, &normalized_name)?
498 {
499 if args.force_merge {
500 memories::clear_deleted_at(&conn, sd_id)?;
501 } else {
502 return Err(AppError::Duplicate(
503 errors_msg::duplicate_memory_soft_deleted(&normalized_name, &namespace),
504 ));
505 }
506 }
507
508 let existing_memory = memories::find_by_name(&conn, &namespace, &normalized_name)?;
509 if existing_memory.is_some() && !args.force_merge {
510 return Err(AppError::Duplicate(errors_msg::duplicate_memory(
511 &normalized_name,
512 &namespace,
513 )));
514 }
515
516 let (resolved_type, resolved_description) = if existing_memory.is_none() {
520 let t = args.r#type.ok_or_else(|| {
522 AppError::Validation(
523 "--type and --description are required when creating a new memory".to_string(),
524 )
525 })?;
526 let d = args.description.clone().ok_or_else(|| {
527 AppError::Validation(
528 "--type and --description are required when creating a new memory".to_string(),
529 )
530 })?;
531 (t.as_str().to_string(), d)
532 } else {
533 let existing_row = memories::read_by_name(&conn, &namespace, &normalized_name)?
535 .ok_or_else(|| {
536 AppError::NotFound(format!(
537 "memory '{normalized_name}' not found in namespace '{namespace}'"
538 ))
539 })?;
540 let t = args
541 .r#type
542 .map(|v| v.as_str().to_string())
543 .unwrap_or_else(|| existing_row.memory_type.clone());
544 let d = args
545 .description
546 .clone()
547 .unwrap_or_else(|| existing_row.description.clone());
548 (t, d)
549 };
550
551 if body_will_be_preserved {
556 if let Some(existing_row) = memories::read_by_name(&conn, &namespace, &normalized_name)? {
557 if !existing_row.body.is_empty() {
558 tracing::debug!(target: "remember",
559 name = %normalized_name,
560 "GAP-08: empty body with --force-merge and no --clear-body; preserving existing body"
561 );
562 raw_body = existing_row.body;
563 body_hash = blake3::hash(raw_body.as_bytes()).to_hex().to_string();
564 snippet = raw_body.chars().take(200).collect();
565 }
566 }
567 }
568
569 let duplicate_hash_id = memories::find_by_hash(&conn, &namespace, &body_hash)?;
570
571 output::emit_progress_i18n(
572 &format!(
573 "Remember stage: validated input; available memory {} MB",
574 crate::memory_guard::available_memory_mb()
575 ),
576 &format!(
577 "Stage remember: input validated; available memory {} MB",
578 crate::memory_guard::available_memory_mb()
579 ),
580 );
581
582 let tokenizer = crate::tokenizer::get_tokenizer(&paths.models)?;
583 let model_max_length = crate::tokenizer::get_model_max_length(&paths.models)?;
584 let total_passage_tokens = crate::tokenizer::count_passage_tokens(tokenizer, &raw_body)?;
585 let chunks_info = chunking::split_into_chunks_hierarchical(&raw_body, tokenizer);
586 let chunks_created = chunks_info.len();
587 let chunks_persisted = compute_chunks_persisted(chunks_info.len());
591
592 output::emit_progress_i18n(
593 &format!(
594 "Remember stage: tokenizer counted {total_passage_tokens} passage tokens (model max {model_max_length}); chunking produced {} chunks; process RSS {} MB",
595 chunks_created,
596 crate::memory_guard::current_process_memory_mb().unwrap_or(0)
597 ),
598 &format!(
599 "Stage remember: tokenizer counted {total_passage_tokens} passage tokens (model max {model_max_length}); chunking produced {} chunks; process RSS {} MB",
600 chunks_created,
601 crate::memory_guard::current_process_memory_mb().unwrap_or(0)
602 ),
603 );
604
605 if chunks_created > crate::constants::REMEMBER_MAX_SAFE_MULTI_CHUNKS {
606 return Err(AppError::LimitExceeded(format!(
607 "document produces {chunks_created} chunks; current safe operational limit is {} chunks; split the document before using remember",
608 crate::constants::REMEMBER_MAX_SAFE_MULTI_CHUNKS
609 )));
610 }
611
612 output::emit_progress_i18n("Computing embedding...", "Calculando embedding...");
613 let mut chunk_embeddings_cache: Option<Vec<Vec<f32>>> = None;
614
615 let embedding = if chunks_info.len() == 1 {
616 crate::daemon::embed_passage_or_local(&paths.models, &raw_body)?
617 } else {
618 let chunk_texts: Vec<&str> = chunks_info
619 .iter()
620 .map(|c| chunking::chunk_text(&raw_body, c))
621 .collect();
622 output::emit_progress_i18n(
623 &format!(
624 "Embedding {} chunks serially to keep memory bounded...",
625 chunks_info.len()
626 ),
627 &format!(
628 "Embedding {} chunks serially to keep memory bounded...",
629 chunks_info.len()
630 ),
631 );
632 let embed_cap = chunk_texts.len();
633 let mut chunk_embeddings = Vec::new();
634 chunk_embeddings.try_reserve(embed_cap).map_err(|_| {
635 AppError::LimitExceeded(format!(
636 "allocation of {embed_cap} chunk embeddings would exceed available memory"
637 ))
638 })?;
639 for chunk_text in &chunk_texts {
640 if let Some(rss) = crate::memory_guard::current_process_memory_mb() {
641 if rss > args.max_rss_mb {
642 tracing::error!(target: "remember",
643 rss_mb = rss,
644 max_rss_mb = args.max_rss_mb,
645 "RSS exceeded --max-rss-mb threshold; aborting to prevent system instability"
646 );
647 return Err(AppError::LowMemory {
648 available_mb: crate::memory_guard::available_memory_mb(),
649 required_mb: args.max_rss_mb,
650 });
651 }
652 }
653 chunk_embeddings.push(crate::daemon::embed_passage_or_local(
654 &paths.models,
655 chunk_text,
656 )?);
657 }
658 output::emit_progress_i18n(
659 &format!(
660 "Remember stage: chunk embeddings complete; process RSS {} MB",
661 crate::memory_guard::current_process_memory_mb().unwrap_or(0)
662 ),
663 &format!(
664 "Stage remember: chunk embeddings completed; process RSS {} MB",
665 crate::memory_guard::current_process_memory_mb().unwrap_or(0)
666 ),
667 );
668 let aggregated = chunking::aggregate_embeddings(&chunk_embeddings);
669 chunk_embeddings_cache = Some(chunk_embeddings);
670 aggregated
671 };
672 let body_for_storage = raw_body;
673
674 let memory_type = resolved_type.as_str();
675 let new_memory = NewMemory {
676 namespace: namespace.clone(),
677 name: normalized_name.clone(),
678 memory_type: memory_type.to_string(),
679 description: resolved_description.clone(),
680 body: body_for_storage,
681 body_hash: body_hash.clone(),
682 session_id: args.session_id.clone(),
683 source: "agent".to_string(),
684 metadata,
685 };
686
687 let mut warnings = Vec::with_capacity(4);
688 let mut entities_persisted = 0usize;
689 let mut relationships_persisted = 0usize;
690
691 let graph_entity_embeddings = graph
692 .entities
693 .iter()
694 .map(|entity| {
695 let entity_text = match &entity.description {
696 Some(desc) => format!("{} {}", entity.name, desc),
697 None => entity.name.clone(),
698 };
699 crate::daemon::embed_passage_or_local(&paths.models, &entity_text)
700 })
701 .collect::<Result<Vec<_>, _>>()?;
702
703 let tx = conn.transaction_with_behavior(rusqlite::TransactionBehavior::Immediate)?;
704
705 let mut skip_reindex = false;
706 let (memory_id, action, version) = match existing_memory {
707 Some((existing_id, _updated_at, _current_version)) => {
708 if let Some(hash_id) = duplicate_hash_id {
709 if hash_id != existing_id {
710 warnings.push(format!(
711 "identical body already exists as memory id {hash_id}"
712 ));
713 }
714 }
715
716 let (old_fts_name, old_fts_desc, old_fts_body): (String, String, String) = tx
718 .query_row(
719 "SELECT name, description, body FROM memories WHERE id = ?1",
720 rusqlite::params![existing_id],
721 |r| Ok((r.get(0)?, r.get(1)?, r.get(2)?)),
722 )?;
723
724 let existing_body_hash: Option<String> = tx
726 .query_row(
727 "SELECT body_hash FROM memories WHERE id = ?1",
728 rusqlite::params![existing_id],
729 |r| r.get(0),
730 )
731 .ok();
732 let body_unchanged = existing_body_hash.as_deref() == Some(&body_hash);
733 skip_reindex = body_unchanged;
734 if !body_unchanged {
735 storage_chunks::delete_chunks(&tx, existing_id)?;
736 }
737
738 let next_v = versions::next_version(&tx, existing_id)?;
739 memories::update(&tx, existing_id, &new_memory, args.expected_updated_at)?;
740
741 memories::sync_fts_after_update(
744 &tx,
745 existing_id,
746 &old_fts_name,
747 &old_fts_desc,
748 &old_fts_body,
749 &normalized_name,
750 &resolved_description,
751 &new_memory.body,
752 )?;
753
754 versions::insert_version(
755 &tx,
756 existing_id,
757 next_v,
758 &normalized_name,
759 memory_type,
760 &resolved_description,
761 &new_memory.body,
762 &serde_json::to_string(&new_memory.metadata)?,
763 None,
764 "edit",
765 )?;
766 if !body_unchanged {
767 memories::upsert_vec(
768 &tx,
769 existing_id,
770 &namespace,
771 memory_type,
772 &embedding,
773 &normalized_name,
774 &snippet,
775 )?;
776 }
777 (existing_id, "updated".to_string(), next_v)
778 }
779 None => {
780 if let Some(hash_id) = duplicate_hash_id {
781 warnings.push(format!(
782 "identical body already exists as memory id {hash_id}"
783 ));
784 }
785 let id = memories::insert(&tx, &new_memory)?;
786 versions::insert_version(
787 &tx,
788 id,
789 1,
790 &normalized_name,
791 memory_type,
792 &resolved_description,
793 &new_memory.body,
794 &serde_json::to_string(&new_memory.metadata)?,
795 None,
796 "create",
797 )?;
798 memories::upsert_vec(
799 &tx,
800 id,
801 &namespace,
802 memory_type,
803 &embedding,
804 &normalized_name,
805 &snippet,
806 )?;
807 (id, "created".to_string(), 1)
808 }
809 };
810
811 if chunks_info.len() > 1 && !skip_reindex {
812 storage_chunks::insert_chunk_slices(&tx, memory_id, &new_memory.body, &chunks_info)?;
813
814 let chunk_embeddings = chunk_embeddings_cache.take().ok_or_else(|| {
815 AppError::Internal(anyhow::anyhow!(
816 "chunk embeddings cache missing in multi-chunk remember path"
817 ))
818 })?;
819
820 for (i, emb) in chunk_embeddings.iter().enumerate() {
821 storage_chunks::upsert_chunk_vec(&tx, i as i64, memory_id, i as i32, emb)?;
822 }
823 output::emit_progress_i18n(
824 &format!(
825 "Remember stage: persisted chunk vectors; process RSS {} MB",
826 crate::memory_guard::current_process_memory_mb().unwrap_or(0)
827 ),
828 &format!(
829 "Etapa remember: vetores de chunks persistidos; RSS do processo {} MB",
830 crate::memory_guard::current_process_memory_mb().unwrap_or(0)
831 ),
832 );
833 }
834
835 if !graph.entities.is_empty() || !graph.relationships.is_empty() {
836 for entity in &graph.entities {
837 let entity_id = entities::upsert_entity(&tx, &namespace, entity)?;
838 let entity_embedding = &graph_entity_embeddings[entities_persisted];
839 entities::upsert_entity_vec(
840 &tx,
841 entity_id,
842 &namespace,
843 entity.entity_type,
844 entity_embedding,
845 &entity.name,
846 )?;
847 entities::link_memory_entity(&tx, memory_id, entity_id)?;
848 entities::increment_degree(&tx, entity_id)?;
849 if args.max_entity_degree > 0 {
851 let cap = args.max_entity_degree as i64;
852 let degree: i64 = tx.query_row(
853 "SELECT degree FROM entities WHERE id = ?1",
854 rusqlite::params![entity_id],
855 |r| r.get(0),
856 )?;
857 if degree > cap {
858 tracing::warn!(target: "remember",
859 entity = %entity.name,
860 degree = degree,
861 cap = cap,
862 "entity degree cap exceeded"
863 );
864 }
865 }
866 entities_persisted += 1;
867 }
868 let entity_types: std::collections::HashMap<&str, EntityType> = graph
869 .entities
870 .iter()
871 .map(|entity| (entity.name.as_str(), entity.entity_type))
872 .collect();
873
874 for rel in &graph.relationships {
875 let source_entity = NewEntity {
876 name: rel.source.clone(),
877 entity_type: entity_types
878 .get(rel.source.as_str())
879 .copied()
880 .unwrap_or(EntityType::Concept),
881 description: None,
882 };
883 let target_entity = NewEntity {
884 name: rel.target.clone(),
885 entity_type: entity_types
886 .get(rel.target.as_str())
887 .copied()
888 .unwrap_or(EntityType::Concept),
889 description: None,
890 };
891 let source_id = entities::upsert_entity(&tx, &namespace, &source_entity)?;
892 let target_id = entities::upsert_entity(&tx, &namespace, &target_entity)?;
893 let rel_id = entities::upsert_relationship(&tx, &namespace, source_id, target_id, rel)?;
894 entities::link_memory_relationship(&tx, memory_id, rel_id)?;
895 relationships_persisted += 1;
896 }
897 }
898 tx.commit()?;
899
900 let urls_persisted = if !extracted_urls.is_empty() {
903 let url_entries: Vec<storage_urls::MemoryUrl> = extracted_urls
904 .into_iter()
905 .map(|u| storage_urls::MemoryUrl {
906 url: u.url,
907 offset: Some(u.offset as i64),
908 })
909 .collect();
910 storage_urls::insert_urls(&conn, memory_id, &url_entries)
911 } else {
912 0
913 };
914
915 conn.execute_batch("PRAGMA wal_checkpoint(TRUNCATE);")?;
916
917 let created_at_epoch = chrono::Utc::now().timestamp();
918 let created_at_iso = crate::tz::format_iso(chrono::Utc::now());
919
920 output::emit_json(&RememberResponse {
921 memory_id,
922 name: normalized_name.clone(),
926 namespace,
927 action: action.clone(),
928 operation: action,
929 version,
930 entities_persisted,
931 relationships_persisted,
932 relationships_truncated,
933 chunks_created,
934 chunks_persisted,
935 urls_persisted,
936 extraction_method,
937 merged_into_memory_id: None,
938 warnings,
939 created_at: created_at_epoch,
940 created_at_iso,
941 elapsed_ms: inicio.elapsed().as_millis() as u64,
942 name_was_normalized,
943 original_name: name_was_normalized.then_some(original_name),
944 })?;
945
946 Ok(())
947}
948
949#[cfg(test)]
950mod tests {
951 use super::compute_chunks_persisted;
952 use crate::output::RememberResponse;
953
954 #[test]
956 fn chunks_persisted_zero_for_zero_chunks() {
957 assert_eq!(compute_chunks_persisted(0), 0);
958 }
959
960 #[test]
961 fn chunks_persisted_zero_for_single_chunk_body() {
962 assert_eq!(compute_chunks_persisted(1), 0);
965 }
966
967 #[test]
968 fn chunks_persisted_equals_count_for_multi_chunk_body() {
969 assert_eq!(compute_chunks_persisted(2), 2);
971 assert_eq!(compute_chunks_persisted(7), 7);
972 assert_eq!(compute_chunks_persisted(64), 64);
973 }
974
975 #[test]
976 fn remember_response_serializes_required_fields() {
977 let resp = RememberResponse {
978 memory_id: 42,
979 name: "minha-mem".to_string(),
980 namespace: "global".to_string(),
981 action: "created".to_string(),
982 operation: "created".to_string(),
983 version: 1,
984 entities_persisted: 0,
985 relationships_persisted: 0,
986 relationships_truncated: false,
987 chunks_created: 1,
988 chunks_persisted: 0,
989 urls_persisted: 0,
990 extraction_method: None,
991 merged_into_memory_id: None,
992 warnings: vec![],
993 created_at: 1_705_320_000,
994 created_at_iso: "2024-01-15T12:00:00Z".to_string(),
995 elapsed_ms: 55,
996 name_was_normalized: false,
997 original_name: None,
998 };
999
1000 let json = serde_json::to_value(&resp).expect("serialization failed");
1001 assert_eq!(json["memory_id"], 42);
1002 assert_eq!(json["action"], "created");
1003 assert_eq!(json["operation"], "created");
1004 assert_eq!(json["version"], 1);
1005 assert_eq!(json["elapsed_ms"], 55u64);
1006 assert!(json["warnings"].is_array());
1007 assert!(json["merged_into_memory_id"].is_null());
1008 }
1009
1010 #[test]
1011 fn remember_response_action_e_operation_sao_aliases() {
1012 let resp = RememberResponse {
1013 memory_id: 1,
1014 name: "mem".to_string(),
1015 namespace: "global".to_string(),
1016 action: "updated".to_string(),
1017 operation: "updated".to_string(),
1018 version: 2,
1019 entities_persisted: 3,
1020 relationships_persisted: 1,
1021 relationships_truncated: false,
1022 extraction_method: None,
1023 chunks_created: 2,
1024 chunks_persisted: 2,
1025 urls_persisted: 0,
1026 merged_into_memory_id: None,
1027 warnings: vec![],
1028 created_at: 0,
1029 created_at_iso: "1970-01-01T00:00:00Z".to_string(),
1030 elapsed_ms: 0,
1031 name_was_normalized: false,
1032 original_name: None,
1033 };
1034
1035 let json = serde_json::to_value(&resp).expect("serialization failed");
1036 assert_eq!(
1037 json["action"], json["operation"],
1038 "action e operation devem ser iguais"
1039 );
1040 assert_eq!(json["entities_persisted"], 3);
1041 assert_eq!(json["relationships_persisted"], 1);
1042 assert_eq!(json["chunks_created"], 2);
1043 }
1044
1045 #[test]
1046 fn remember_response_warnings_lista_mensagens() {
1047 let resp = RememberResponse {
1048 memory_id: 5,
1049 name: "dup-mem".to_string(),
1050 namespace: "global".to_string(),
1051 action: "created".to_string(),
1052 operation: "created".to_string(),
1053 version: 1,
1054 entities_persisted: 0,
1055 extraction_method: None,
1056 relationships_persisted: 0,
1057 relationships_truncated: false,
1058 chunks_created: 1,
1059 chunks_persisted: 0,
1060 urls_persisted: 0,
1061 merged_into_memory_id: None,
1062 warnings: vec!["identical body already exists as memory id 3".to_string()],
1063 created_at: 0,
1064 created_at_iso: "1970-01-01T00:00:00Z".to_string(),
1065 elapsed_ms: 10,
1066 name_was_normalized: false,
1067 original_name: None,
1068 };
1069
1070 let json = serde_json::to_value(&resp).expect("serialization failed");
1071 let warnings = json["warnings"]
1072 .as_array()
1073 .expect("warnings deve ser array");
1074 assert_eq!(warnings.len(), 1);
1075 assert!(warnings[0].as_str().unwrap().contains("identical body"));
1076 }
1077
1078 #[test]
1079 fn invalid_name_reserved_prefix_returns_validation_error() {
1080 use crate::errors::AppError;
1081 let nome = "__reservado";
1083 let resultado: Result<(), AppError> = if nome.starts_with("__") {
1084 Err(AppError::Validation(
1085 crate::i18n::validation::reserved_name(),
1086 ))
1087 } else {
1088 Ok(())
1089 };
1090 assert!(resultado.is_err());
1091 if let Err(AppError::Validation(msg)) = resultado {
1092 assert!(!msg.is_empty());
1093 }
1094 }
1095
1096 #[test]
1097 fn name_too_long_returns_validation_error() {
1098 use crate::errors::AppError;
1099 let nome_longo = "a".repeat(crate::constants::MAX_MEMORY_NAME_LEN + 1);
1100 let resultado: Result<(), AppError> =
1101 if nome_longo.is_empty() || nome_longo.len() > crate::constants::MAX_MEMORY_NAME_LEN {
1102 Err(AppError::Validation(crate::i18n::validation::name_length(
1103 crate::constants::MAX_MEMORY_NAME_LEN,
1104 )))
1105 } else {
1106 Ok(())
1107 };
1108 assert!(resultado.is_err());
1109 }
1110
1111 #[test]
1112 fn remember_response_merged_into_memory_id_some_serializes_integer() {
1113 let resp = RememberResponse {
1114 memory_id: 10,
1115 name: "mem-mergeada".to_string(),
1116 namespace: "global".to_string(),
1117 action: "updated".to_string(),
1118 operation: "updated".to_string(),
1119 version: 3,
1120 extraction_method: None,
1121 entities_persisted: 0,
1122 relationships_persisted: 0,
1123 relationships_truncated: false,
1124 chunks_created: 1,
1125 chunks_persisted: 0,
1126 urls_persisted: 0,
1127 merged_into_memory_id: Some(7),
1128 warnings: vec![],
1129 created_at: 0,
1130 created_at_iso: "1970-01-01T00:00:00Z".to_string(),
1131 elapsed_ms: 0,
1132 name_was_normalized: false,
1133 original_name: None,
1134 };
1135
1136 let json = serde_json::to_value(&resp).expect("serialization failed");
1137 assert_eq!(json["merged_into_memory_id"], 7);
1138 }
1139
1140 #[test]
1141 fn remember_response_urls_persisted_serializes_field() {
1142 let resp = RememberResponse {
1144 memory_id: 3,
1145 name: "mem-com-urls".to_string(),
1146 namespace: "global".to_string(),
1147 action: "created".to_string(),
1148 operation: "created".to_string(),
1149 version: 1,
1150 entities_persisted: 0,
1151 relationships_persisted: 0,
1152 relationships_truncated: false,
1153 chunks_created: 1,
1154 chunks_persisted: 0,
1155 urls_persisted: 3,
1156 extraction_method: Some("regex-only".to_string()),
1157 merged_into_memory_id: None,
1158 warnings: vec![],
1159 created_at: 0,
1160 created_at_iso: "1970-01-01T00:00:00Z".to_string(),
1161 elapsed_ms: 0,
1162 name_was_normalized: false,
1163 original_name: None,
1164 };
1165 let json = serde_json::to_value(&resp).expect("serialization failed");
1166 assert_eq!(json["urls_persisted"], 3);
1167 }
1168
1169 #[test]
1170 fn empty_name_after_normalization_returns_specific_message() {
1171 use crate::errors::AppError;
1174 let normalized = "---".to_lowercase().replace(['_', ' '], "-");
1175 let normalized = normalized.trim_matches('-').to_string();
1176 let resultado: Result<(), AppError> = if normalized.is_empty() {
1177 Err(AppError::Validation(
1178 "name cannot be empty after normalization (input was blank or contained only hyphens/underscores/spaces)".to_string(),
1179 ))
1180 } else {
1181 Ok(())
1182 };
1183 assert!(resultado.is_err());
1184 if let Err(AppError::Validation(msg)) = resultado {
1185 assert!(
1186 msg.contains("empty after normalization"),
1187 "mensagem deve mencionar 'empty after normalization', obteve: {msg}"
1188 );
1189 }
1190 }
1191
1192 #[test]
1193 fn name_only_underscores_after_normalization_returns_specific_message() {
1194 use crate::errors::AppError;
1196 let normalized = "___".to_lowercase().replace(['_', ' '], "-");
1197 let normalized = normalized.trim_matches('-').to_string();
1198 assert!(
1199 normalized.is_empty(),
1200 "underscores devem normalizar para string vazia"
1201 );
1202 let resultado: Result<(), AppError> = if normalized.is_empty() {
1203 Err(AppError::Validation(
1204 "name cannot be empty after normalization (input was blank or contained only hyphens/underscores/spaces)".to_string(),
1205 ))
1206 } else {
1207 Ok(())
1208 };
1209 assert!(resultado.is_err());
1210 if let Err(AppError::Validation(msg)) = resultado {
1211 assert!(
1212 msg.contains("empty after normalization"),
1213 "mensagem deve mencionar 'empty after normalization', obteve: {msg}"
1214 );
1215 }
1216 }
1217
1218 #[test]
1219 fn remember_response_relationships_truncated_serializes_field() {
1220 let resp_false = RememberResponse {
1222 memory_id: 1,
1223 name: "test".to_string(),
1224 namespace: "global".to_string(),
1225 action: "created".to_string(),
1226 operation: "created".to_string(),
1227 version: 1,
1228 entities_persisted: 2,
1229 relationships_persisted: 1,
1230 relationships_truncated: false,
1231 chunks_created: 1,
1232 chunks_persisted: 0,
1233 urls_persisted: 0,
1234 extraction_method: None,
1235 merged_into_memory_id: None,
1236 warnings: vec![],
1237 created_at: 0,
1238 created_at_iso: "1970-01-01T00:00:00Z".to_string(),
1239 elapsed_ms: 0,
1240 name_was_normalized: false,
1241 original_name: None,
1242 };
1243 let json_false = serde_json::to_value(&resp_false).expect("serialization failed");
1244 assert_eq!(json_false["relationships_truncated"], false);
1245
1246 let resp_true = RememberResponse {
1247 relationships_truncated: true,
1248 ..resp_false
1249 };
1250 let json_true = serde_json::to_value(&resp_true).expect("serialization failed");
1251 assert_eq!(json_true["relationships_truncated"], true);
1252 }
1253
1254 fn should_preserve_body(force_merge: bool, raw_body_is_empty: bool, clear_body: bool) -> bool {
1263 force_merge && raw_body_is_empty && !clear_body
1264 }
1265
1266 #[test]
1267 fn gap08_empty_body_force_merge_no_clear_body_preserves() {
1268 assert!(
1271 should_preserve_body(true, true, false),
1272 "empty body + force-merge + no clear-body should trigger preservation"
1273 );
1274 }
1275
1276 #[test]
1277 fn gap08_empty_body_force_merge_with_clear_body_does_not_preserve() {
1278 assert!(
1280 !should_preserve_body(true, true, true),
1281 "--clear-body must bypass preservation"
1282 );
1283 }
1284
1285 #[test]
1286 fn gap08_non_empty_body_force_merge_does_not_preserve() {
1287 assert!(
1289 !should_preserve_body(true, false, false),
1290 "non-empty body must overwrite, not preserve"
1291 );
1292 }
1293
1294 #[test]
1295 fn gap08_empty_body_no_force_merge_does_not_preserve() {
1296 assert!(
1298 !should_preserve_body(false, true, false),
1299 "no --force-merge means no preservation logic applies"
1300 );
1301 }
1302}