Skip to main content

sqlite_graphrag/commands/
remember.rs

1//! Handler for the `remember` CLI subcommand.
2
3use 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
17/// Returns the number of rows that will be written to `memory_chunks` for the
18/// given chunk count. Single-chunk bodies are stored directly in the
19/// `memories` row, so no chunk row is appended (returns `0`). Multi-chunk
20/// bodies persist every chunk and the count equals `chunks_created`.
21///
22/// Centralized as a function so the H-M8 invariant is unit-testable without
23/// running the full handler. The schema for `chunks_persisted` documents this
24/// contract explicitly (see `docs/schemas/remember.schema.json`).
25fn 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")]
48pub struct RememberArgs {
49    /// Memory name in kebab-case (lowercase letters, digits, hyphens).
50    /// Acts as unique key within the namespace; collisions trigger merge or rejection.
51    #[arg(long)]
52    pub name: String,
53    #[arg(
54        long,
55        value_enum,
56        long_help = "Memory kind stored in `memories.type`. This is NOT the graph `entity_type` used in `--entities-file`. Valid values: user, feedback, project, reference, decision, incident, skill, document, note."
57    )]
58    pub r#type: MemoryType,
59    /// Short description (≤500 chars) summarizing the memory for use in `list` and `recall` snippets.
60    #[arg(long)]
61    pub description: String,
62    /// Inline body content. Mutually exclusive with --body-file, --body-stdin, --graph-stdin.
63    /// Maximum 512000 bytes; rejected if empty without an external graph.
64    #[arg(
65        long,
66        help = "Inline body content (max 500 KB / 512000 bytes; for larger inputs split into multiple memories or use --body-file)",
67        conflicts_with_all = ["body_file", "body_stdin", "graph_stdin"]
68    )]
69    pub body: Option<String>,
70    #[arg(
71        long,
72        help = "Read body from a file instead of --body",
73        conflicts_with_all = ["body", "body_stdin", "graph_stdin"]
74    )]
75    pub body_file: Option<std::path::PathBuf>,
76    /// Read body from stdin until EOF. Useful in pipes (echo "..." | sqlite-graphrag remember ...).
77    /// Mutually exclusive with --body, --body-file, --graph-stdin.
78    #[arg(
79        long,
80        conflicts_with_all = ["body", "body_file", "graph_stdin"]
81    )]
82    pub body_stdin: bool,
83    #[arg(
84        long,
85        help = "JSON file containing entities to associate with this memory"
86    )]
87    pub entities_file: Option<std::path::PathBuf>,
88    #[arg(
89        long,
90        help = "JSON file containing relationships to associate with this memory"
91    )]
92    pub relationships_file: Option<std::path::PathBuf>,
93    #[arg(
94        long,
95        help = "Read graph JSON (body + entities + relationships) from stdin",
96        conflicts_with_all = [
97            "body",
98            "body_file",
99            "body_stdin",
100            "entities_file",
101            "relationships_file"
102        ]
103    )]
104    pub graph_stdin: bool,
105    #[arg(long, default_value = "global")]
106    pub namespace: Option<String>,
107    /// Inline JSON object with arbitrary metadata key-value pairs. Mutually exclusive with --metadata-file.
108    #[arg(long)]
109    pub metadata: Option<String>,
110    #[arg(long, help = "JSON file containing metadata key-value pairs")]
111    pub metadata_file: Option<std::path::PathBuf>,
112    #[arg(long)]
113    pub force_merge: bool,
114    #[arg(
115        long,
116        value_name = "EPOCH_OR_RFC3339",
117        value_parser = crate::parsers::parse_expected_updated_at,
118        long_help = "Optimistic lock: reject if updated_at does not match. \
119Accepts Unix epoch (e.g. 1700000000) or RFC 3339 (e.g. 2026-04-19T12:00:00Z)."
120    )]
121    pub expected_updated_at: Option<i64>,
122    #[arg(
123        long,
124        env = "SQLITE_GRAPHRAG_ENABLE_NER",
125        value_parser = crate::parsers::parse_bool_flexible,
126        action = clap::ArgAction::Set,
127        num_args = 0..=1,
128        default_missing_value = "true",
129        default_value = "false",
130        help = "Enable automatic GLiNER NER entity/relationship extraction from body"
131    )]
132    pub enable_ner: bool,
133    #[arg(
134        long,
135        env = "SQLITE_GRAPHRAG_GLINER_VARIANT",
136        default_value = "fp32",
137        help = "GLiNER model variant: fp32 (best quality, 1.1GB), fp16 (580MB), int8 (349MB, fastest)"
138    )]
139    pub gliner_variant: String,
140    #[arg(long, hide = true)]
141    pub skip_extraction: bool,
142    /// Optional opaque session identifier for tracing memory provenance across multi-agent runs.
143    #[arg(long)]
144    pub session_id: Option<String>,
145    #[arg(long, value_enum, default_value_t = JsonOutputFormat::Json)]
146    pub format: JsonOutputFormat,
147    #[arg(long, hide = true, help = "No-op; JSON is always emitted on stdout")]
148    pub json: bool,
149    #[arg(long, env = "SQLITE_GRAPHRAG_DB_PATH")]
150    pub db: Option<String>,
151}
152
153#[derive(Deserialize, Default)]
154#[serde(deny_unknown_fields)]
155struct GraphInput {
156    #[serde(default)]
157    body: Option<String>,
158    #[serde(default)]
159    entities: Vec<NewEntity>,
160    #[serde(default)]
161    relationships: Vec<NewRelationship>,
162}
163
164fn normalize_and_validate_graph_input(graph: &mut GraphInput) -> Result<(), AppError> {
165    for rel in &mut graph.relationships {
166        rel.relation = crate::parsers::normalize_relation(&rel.relation);
167        if let Err(e) = crate::parsers::validate_relation_format(&rel.relation) {
168            return Err(AppError::Validation(format!(
169                "{e} for relationship '{}' -> '{}'",
170                rel.source, rel.target
171            )));
172        }
173        crate::parsers::warn_if_non_canonical(&rel.relation);
174        if !(0.0..=1.0).contains(&rel.strength) {
175            return Err(AppError::Validation(format!(
176                "invalid strength {} for relationship '{}' -> '{}'; expected value in [0.0, 1.0]",
177                rel.strength, rel.source, rel.target
178            )));
179        }
180    }
181
182    Ok(())
183}
184
185pub fn run(args: RememberArgs) -> Result<(), AppError> {
186    use crate::constants::*;
187
188    let inicio = std::time::Instant::now();
189    let _ = args.format;
190    let namespace = crate::namespace::resolve_namespace(args.namespace.as_deref())?;
191
192    // Capture the original `--name` before normalization so the JSON response can
193    // surface `name_was_normalized` + `original_name` (B_4 in v1.0.32). Stored as
194    // an owned String because `args.name` is moved into the response below.
195    let original_name = args.name.clone();
196
197    // Auto-normalize to kebab-case before validation (P2-H).
198    // v1.0.20: also trims hyphens at the boundary (including trailing) to avoid rejection
199    // after truncation by a long filename ending in a hyphen.
200    let normalized_name = {
201        let lower = args.name.to_lowercase().replace(['_', ' '], "-");
202        let trimmed = lower.trim_matches('-').to_string();
203        if trimmed != args.name {
204            tracing::warn!(
205                original = %args.name,
206                normalized = %trimmed,
207                "name auto-normalized to kebab-case"
208            );
209        }
210        trimmed
211    };
212    let name_was_normalized = normalized_name != original_name;
213
214    if normalized_name.is_empty() {
215        return Err(AppError::Validation(
216            "name cannot be empty after normalization (input was blank or contained only hyphens/underscores/spaces)".to_string(),
217        ));
218    }
219    if normalized_name.len() > MAX_MEMORY_NAME_LEN {
220        return Err(AppError::LimitExceeded(
221            crate::i18n::validation::name_length(MAX_MEMORY_NAME_LEN),
222        ));
223    }
224
225    if normalized_name.starts_with("__") {
226        return Err(AppError::Validation(
227            crate::i18n::validation::reserved_name(),
228        ));
229    }
230
231    {
232        let slug_re = regex::Regex::new(crate::constants::NAME_SLUG_REGEX)
233            .map_err(|e| AppError::Internal(anyhow::anyhow!("regex: {e}")))?;
234        if !slug_re.is_match(&normalized_name) {
235            return Err(AppError::Validation(crate::i18n::validation::name_kebab(
236                &normalized_name,
237            )));
238        }
239    }
240
241    if args.description.len() > MAX_MEMORY_DESCRIPTION_LEN {
242        return Err(AppError::Validation(
243            crate::i18n::validation::description_exceeds(MAX_MEMORY_DESCRIPTION_LEN),
244        ));
245    }
246
247    let mut raw_body = if let Some(b) = args.body {
248        b
249    } else if let Some(path) = args.body_file {
250        std::fs::read_to_string(&path).map_err(AppError::Io)?
251    } else if args.body_stdin || args.graph_stdin {
252        crate::stdin_helper::read_stdin_with_timeout(60)?
253    } else {
254        String::new()
255    };
256
257    let entities_provided_externally =
258        args.entities_file.is_some() || args.relationships_file.is_some() || args.graph_stdin;
259
260    let mut graph = GraphInput::default();
261    if let Some(path) = args.entities_file {
262        let content = std::fs::read_to_string(&path).map_err(AppError::Io)?;
263        graph.entities = serde_json::from_str(&content)?;
264    }
265    if let Some(path) = args.relationships_file {
266        let content = std::fs::read_to_string(&path).map_err(AppError::Io)?;
267        graph.relationships = serde_json::from_str(&content)?;
268    }
269    if args.graph_stdin {
270        graph = serde_json::from_str::<GraphInput>(&raw_body).map_err(|e| {
271            AppError::Validation(format!("invalid JSON payload on --graph-stdin: {e}"))
272        })?;
273        raw_body = graph.body.take().unwrap_or_default();
274    }
275
276    if graph.entities.len() > max_entities_per_memory() {
277        return Err(AppError::LimitExceeded(errors_msg::entity_limit_exceeded(
278            max_entities_per_memory(),
279        )));
280    }
281    if graph.relationships.len() > MAX_RELATIONSHIPS_PER_MEMORY {
282        return Err(AppError::LimitExceeded(
283            errors_msg::relationship_limit_exceeded(MAX_RELATIONSHIPS_PER_MEMORY),
284        ));
285    }
286    normalize_and_validate_graph_input(&mut graph)?;
287
288    if raw_body.len() > MAX_MEMORY_BODY_LEN {
289        return Err(AppError::LimitExceeded(
290            crate::i18n::validation::body_exceeds(MAX_MEMORY_BODY_LEN),
291        ));
292    }
293
294    // v1.0.22 P1: reject empty or whitespace-only body when no external graph is provided.
295    // Without this check, empty embeddings would be persisted, breaking recall semantics.
296    if !entities_provided_externally && graph.entities.is_empty() && raw_body.trim().is_empty() {
297        return Err(AppError::Validation(crate::i18n::validation::empty_body()));
298    }
299
300    let metadata: serde_json::Value = if let Some(m) = args.metadata {
301        serde_json::from_str(&m)?
302    } else if let Some(path) = args.metadata_file {
303        let content = std::fs::read_to_string(&path).map_err(AppError::Io)?;
304        serde_json::from_str(&content)?
305    } else {
306        serde_json::json!({})
307    };
308
309    let body_hash = blake3::hash(raw_body.as_bytes()).to_hex().to_string();
310    let snippet: String = raw_body.chars().take(200).collect();
311
312    let paths = AppPaths::resolve(args.db.as_deref())?;
313    paths.ensure_dirs()?;
314
315    // v1.0.20: use .trim().is_empty() to reject bodies that are only whitespace.
316    let mut extraction_method: Option<String> = None;
317    let mut extracted_urls: Vec<crate::extraction::ExtractedUrl> = Vec::new();
318    let mut relationships_truncated = false;
319    if args.enable_ner && args.skip_extraction {
320        tracing::warn!(
321            "--enable-ner and --skip-extraction are contradictory; --enable-ner takes precedence"
322        );
323    }
324    if args.skip_extraction && !args.enable_ner {
325        tracing::warn!("--skip-extraction is deprecated and has no effect (NER is disabled by default since v1.0.45); remove this flag");
326    }
327    let gliner_variant: crate::extraction::GlinerVariant =
328        args.gliner_variant.parse().unwrap_or_else(|e| {
329            tracing::warn!("invalid --gliner-variant: {e}; using fp32");
330            crate::extraction::GlinerVariant::Fp32
331        });
332    if args.enable_ner && graph.entities.is_empty() && !raw_body.trim().is_empty() {
333        match crate::extraction::extract_graph_auto(&raw_body, &paths, gliner_variant) {
334            Ok(extracted) => {
335                extraction_method = Some(extracted.extraction_method.clone());
336                extracted_urls = extracted.urls;
337                graph.entities = extracted.entities;
338                graph.relationships = extracted.relationships;
339                relationships_truncated = extracted.relationships_truncated;
340
341                if graph.entities.len() > max_entities_per_memory() {
342                    graph.entities.truncate(max_entities_per_memory());
343                }
344                if graph.relationships.len() > MAX_RELATIONSHIPS_PER_MEMORY {
345                    relationships_truncated = true;
346                    graph.relationships.truncate(MAX_RELATIONSHIPS_PER_MEMORY);
347                }
348                normalize_and_validate_graph_input(&mut graph)?;
349            }
350            Err(e) => {
351                tracing::warn!("auto-extraction failed (graceful degradation): {e:#}");
352                extraction_method = Some("none:extraction-failed".to_string());
353            }
354        }
355    }
356
357    let mut conn = open_rw(&paths.db)?;
358    ensure_schema(&mut conn)?;
359
360    {
361        use crate::constants::MAX_NAMESPACES_ACTIVE;
362        let active_count: u32 = conn.query_row(
363            "SELECT COUNT(DISTINCT namespace) FROM memories WHERE deleted_at IS NULL",
364            [],
365            |r| r.get::<_, i64>(0).map(|v| v as u32),
366        )?;
367        let ns_exists: bool = conn.query_row(
368            "SELECT EXISTS(SELECT 1 FROM memories WHERE namespace = ?1 AND deleted_at IS NULL)",
369            rusqlite::params![namespace],
370            |r| r.get::<_, i64>(0).map(|v| v > 0),
371        )?;
372        if !ns_exists && active_count >= MAX_NAMESPACES_ACTIVE {
373            return Err(AppError::NamespaceError(format!(
374                "active namespace limit of {MAX_NAMESPACES_ACTIVE} reached while trying to create '{namespace}'"
375            )));
376        }
377    }
378
379    let existing_memory = memories::find_by_name(&conn, &namespace, &normalized_name)?;
380    if existing_memory.is_some() && !args.force_merge {
381        return Err(AppError::Duplicate(errors_msg::duplicate_memory(
382            &normalized_name,
383            &namespace,
384        )));
385    }
386
387    let duplicate_hash_id = memories::find_by_hash(&conn, &namespace, &body_hash)?;
388
389    output::emit_progress_i18n(
390        &format!(
391            "Remember stage: validated input; available memory {} MB",
392            crate::memory_guard::available_memory_mb()
393        ),
394        &format!(
395            "Stage remember: input validated; available memory {} MB",
396            crate::memory_guard::available_memory_mb()
397        ),
398    );
399
400    let tokenizer = crate::tokenizer::get_tokenizer(&paths.models)?;
401    let model_max_length = crate::tokenizer::get_model_max_length(&paths.models)?;
402    let total_passage_tokens = crate::tokenizer::count_passage_tokens(tokenizer, &raw_body)?;
403    let chunks_info = chunking::split_into_chunks_hierarchical(&raw_body, tokenizer);
404    let chunks_created = chunks_info.len();
405    // For single-chunk bodies the memory row itself stores the content and no
406    // entry is appended to `memory_chunks` (see line ~545). For multi-chunk
407    // bodies every chunk is persisted via `insert_chunk_slices`.
408    let chunks_persisted = compute_chunks_persisted(chunks_info.len());
409
410    output::emit_progress_i18n(
411        &format!(
412            "Remember stage: tokenizer counted {total_passage_tokens} passage tokens (model max {model_max_length}); chunking produced {} chunks; process RSS {} MB",
413            chunks_created,
414            crate::memory_guard::current_process_memory_mb().unwrap_or(0)
415        ),
416        &format!(
417            "Stage remember: tokenizer counted {total_passage_tokens} passage tokens (model max {model_max_length}); chunking produced {} chunks; process RSS {} MB",
418            chunks_created,
419            crate::memory_guard::current_process_memory_mb().unwrap_or(0)
420        ),
421    );
422
423    if chunks_created > crate::constants::REMEMBER_MAX_SAFE_MULTI_CHUNKS {
424        return Err(AppError::LimitExceeded(format!(
425            "document produces {chunks_created} chunks; current safe operational limit is {} chunks; split the document before using remember",
426            crate::constants::REMEMBER_MAX_SAFE_MULTI_CHUNKS
427        )));
428    }
429
430    output::emit_progress_i18n("Computing embedding...", "Calculando embedding...");
431    let mut chunk_embeddings_cache: Option<Vec<Vec<f32>>> = None;
432
433    let embedding = if chunks_info.len() == 1 {
434        crate::daemon::embed_passage_or_local(&paths.models, &raw_body)?
435    } else {
436        let chunk_texts: Vec<&str> = chunks_info
437            .iter()
438            .map(|c| chunking::chunk_text(&raw_body, c))
439            .collect();
440        output::emit_progress_i18n(
441            &format!(
442                "Embedding {} chunks serially to keep memory bounded...",
443                chunks_info.len()
444            ),
445            &format!(
446                "Embedding {} chunks serially to keep memory bounded...",
447                chunks_info.len()
448            ),
449        );
450        let mut chunk_embeddings = Vec::with_capacity(chunk_texts.len());
451        for chunk_text in &chunk_texts {
452            chunk_embeddings.push(crate::daemon::embed_passage_or_local(
453                &paths.models,
454                chunk_text,
455            )?);
456        }
457        output::emit_progress_i18n(
458            &format!(
459                "Remember stage: chunk embeddings complete; process RSS {} MB",
460                crate::memory_guard::current_process_memory_mb().unwrap_or(0)
461            ),
462            &format!(
463                "Stage remember: chunk embeddings completed; process RSS {} MB",
464                crate::memory_guard::current_process_memory_mb().unwrap_or(0)
465            ),
466        );
467        let aggregated = chunking::aggregate_embeddings(&chunk_embeddings);
468        chunk_embeddings_cache = Some(chunk_embeddings);
469        aggregated
470    };
471    let body_for_storage = raw_body;
472
473    let memory_type = args.r#type.as_str();
474    let new_memory = NewMemory {
475        namespace: namespace.clone(),
476        name: normalized_name.clone(),
477        memory_type: memory_type.to_string(),
478        description: args.description.clone(),
479        body: body_for_storage,
480        body_hash: body_hash.clone(),
481        session_id: args.session_id.clone(),
482        source: "agent".to_string(),
483        metadata,
484    };
485
486    let mut warnings = Vec::new();
487    let mut entities_persisted = 0usize;
488    let mut relationships_persisted = 0usize;
489
490    let graph_entity_embeddings = graph
491        .entities
492        .iter()
493        .map(|entity| {
494            let entity_text = match &entity.description {
495                Some(desc) => format!("{} {}", entity.name, desc),
496                None => entity.name.clone(),
497            };
498            crate::daemon::embed_passage_or_local(&paths.models, &entity_text)
499        })
500        .collect::<Result<Vec<_>, _>>()?;
501
502    let tx = conn.transaction_with_behavior(rusqlite::TransactionBehavior::Immediate)?;
503
504    let (memory_id, action, version) = match existing_memory {
505        Some((existing_id, _updated_at, _current_version)) => {
506            if let Some(hash_id) = duplicate_hash_id {
507                if hash_id != existing_id {
508                    warnings.push(format!(
509                        "identical body already exists as memory id {hash_id}"
510                    ));
511                }
512            }
513
514            storage_chunks::delete_chunks(&tx, existing_id)?;
515
516            let next_v = versions::next_version(&tx, existing_id)?;
517            memories::update(&tx, existing_id, &new_memory, args.expected_updated_at)?;
518            versions::insert_version(
519                &tx,
520                existing_id,
521                next_v,
522                &normalized_name,
523                memory_type,
524                &args.description,
525                &new_memory.body,
526                &serde_json::to_string(&new_memory.metadata)?,
527                None,
528                "edit",
529            )?;
530            memories::upsert_vec(
531                &tx,
532                existing_id,
533                &namespace,
534                memory_type,
535                &embedding,
536                &normalized_name,
537                &snippet,
538            )?;
539            (existing_id, "updated".to_string(), next_v)
540        }
541        None => {
542            if let Some(hash_id) = duplicate_hash_id {
543                warnings.push(format!(
544                    "identical body already exists as memory id {hash_id}"
545                ));
546            }
547            let id = memories::insert(&tx, &new_memory)?;
548            versions::insert_version(
549                &tx,
550                id,
551                1,
552                &normalized_name,
553                memory_type,
554                &args.description,
555                &new_memory.body,
556                &serde_json::to_string(&new_memory.metadata)?,
557                None,
558                "create",
559            )?;
560            memories::upsert_vec(
561                &tx,
562                id,
563                &namespace,
564                memory_type,
565                &embedding,
566                &normalized_name,
567                &snippet,
568            )?;
569            (id, "created".to_string(), 1)
570        }
571    };
572
573    if chunks_info.len() > 1 {
574        storage_chunks::insert_chunk_slices(&tx, memory_id, &new_memory.body, &chunks_info)?;
575
576        let chunk_embeddings = chunk_embeddings_cache.take().ok_or_else(|| {
577            AppError::Internal(anyhow::anyhow!(
578                "cache de embeddings de chunks ausente no caminho multi-chunk do remember"
579            ))
580        })?;
581
582        for (i, emb) in chunk_embeddings.iter().enumerate() {
583            storage_chunks::upsert_chunk_vec(&tx, i as i64, memory_id, i as i32, emb)?;
584        }
585        output::emit_progress_i18n(
586            &format!(
587                "Remember stage: persisted chunk vectors; process RSS {} MB",
588                crate::memory_guard::current_process_memory_mb().unwrap_or(0)
589            ),
590            &format!(
591                "Etapa remember: vetores de chunks persistidos; RSS do processo {} MB",
592                crate::memory_guard::current_process_memory_mb().unwrap_or(0)
593            ),
594        );
595    }
596
597    if !graph.entities.is_empty() || !graph.relationships.is_empty() {
598        for entity in &graph.entities {
599            let entity_id = entities::upsert_entity(&tx, &namespace, entity)?;
600            let entity_embedding = &graph_entity_embeddings[entities_persisted];
601            entities::upsert_entity_vec(
602                &tx,
603                entity_id,
604                &namespace,
605                entity.entity_type,
606                entity_embedding,
607                &entity.name,
608            )?;
609            entities::link_memory_entity(&tx, memory_id, entity_id)?;
610            entities::increment_degree(&tx, entity_id)?;
611            entities_persisted += 1;
612        }
613        let entity_types: std::collections::HashMap<&str, EntityType> = graph
614            .entities
615            .iter()
616            .map(|entity| (entity.name.as_str(), entity.entity_type))
617            .collect();
618
619        for rel in &graph.relationships {
620            let source_entity = NewEntity {
621                name: rel.source.clone(),
622                entity_type: entity_types
623                    .get(rel.source.as_str())
624                    .copied()
625                    .unwrap_or(EntityType::Concept),
626                description: None,
627            };
628            let target_entity = NewEntity {
629                name: rel.target.clone(),
630                entity_type: entity_types
631                    .get(rel.target.as_str())
632                    .copied()
633                    .unwrap_or(EntityType::Concept),
634                description: None,
635            };
636            let source_id = entities::upsert_entity(&tx, &namespace, &source_entity)?;
637            let target_id = entities::upsert_entity(&tx, &namespace, &target_entity)?;
638            let rel_id = entities::upsert_relationship(&tx, &namespace, source_id, target_id, rel)?;
639            entities::link_memory_relationship(&tx, memory_id, rel_id)?;
640            relationships_persisted += 1;
641        }
642    }
643    tx.commit()?;
644
645    // v1.0.24 P0-2: persist URLs in a dedicated table, outside the main transaction.
646    // Failures do not propagate — non-critical path with graceful degradation.
647    let urls_persisted = if !extracted_urls.is_empty() {
648        let url_entries: Vec<storage_urls::MemoryUrl> = extracted_urls
649            .into_iter()
650            .map(|u| storage_urls::MemoryUrl {
651                url: u.url,
652                offset: Some(u.offset as i64),
653            })
654            .collect();
655        storage_urls::insert_urls(&conn, memory_id, &url_entries)
656    } else {
657        0
658    };
659
660    let created_at_epoch = chrono::Utc::now().timestamp();
661    let created_at_iso = crate::tz::format_iso(chrono::Utc::now());
662
663    output::emit_json(&RememberResponse {
664        memory_id,
665        // Persist the normalized (kebab-case) slug as `name` since that is the
666        // storage key. The original input is exposed via `original_name` only
667        // when normalization actually changed something (B_4 in v1.0.32).
668        name: normalized_name.clone(),
669        namespace,
670        action: action.clone(),
671        operation: action,
672        version,
673        entities_persisted,
674        relationships_persisted,
675        relationships_truncated,
676        chunks_created,
677        chunks_persisted,
678        urls_persisted,
679        extraction_method,
680        merged_into_memory_id: None,
681        warnings,
682        created_at: created_at_epoch,
683        created_at_iso,
684        elapsed_ms: inicio.elapsed().as_millis() as u64,
685        name_was_normalized,
686        original_name: name_was_normalized.then_some(original_name),
687    })?;
688
689    Ok(())
690}
691
692#[cfg(test)]
693mod tests {
694    use super::compute_chunks_persisted;
695    use crate::output::RememberResponse;
696
697    // Bug H-M8: chunks_persisted contract is unit-testable and matches schema.
698    #[test]
699    fn chunks_persisted_zero_for_zero_chunks() {
700        assert_eq!(compute_chunks_persisted(0), 0);
701    }
702
703    #[test]
704    fn chunks_persisted_zero_for_single_chunk_body() {
705        // Single-chunk bodies live in the memories row itself; no row is
706        // appended to memory_chunks. This is the documented contract.
707        assert_eq!(compute_chunks_persisted(1), 0);
708    }
709
710    #[test]
711    fn chunks_persisted_equals_count_for_multi_chunk_body() {
712        // Every chunk above the first triggers a row in memory_chunks.
713        assert_eq!(compute_chunks_persisted(2), 2);
714        assert_eq!(compute_chunks_persisted(7), 7);
715        assert_eq!(compute_chunks_persisted(64), 64);
716    }
717
718    #[test]
719    fn remember_response_serializes_required_fields() {
720        let resp = RememberResponse {
721            memory_id: 42,
722            name: "minha-mem".to_string(),
723            namespace: "global".to_string(),
724            action: "created".to_string(),
725            operation: "created".to_string(),
726            version: 1,
727            entities_persisted: 0,
728            relationships_persisted: 0,
729            relationships_truncated: false,
730            chunks_created: 1,
731            chunks_persisted: 0,
732            urls_persisted: 0,
733            extraction_method: None,
734            merged_into_memory_id: None,
735            warnings: vec![],
736            created_at: 1_705_320_000,
737            created_at_iso: "2024-01-15T12:00:00Z".to_string(),
738            elapsed_ms: 55,
739            name_was_normalized: false,
740            original_name: None,
741        };
742
743        let json = serde_json::to_value(&resp).expect("serialization failed");
744        assert_eq!(json["memory_id"], 42);
745        assert_eq!(json["action"], "created");
746        assert_eq!(json["operation"], "created");
747        assert_eq!(json["version"], 1);
748        assert_eq!(json["elapsed_ms"], 55u64);
749        assert!(json["warnings"].is_array());
750        assert!(json["merged_into_memory_id"].is_null());
751    }
752
753    #[test]
754    fn remember_response_action_e_operation_sao_aliases() {
755        let resp = RememberResponse {
756            memory_id: 1,
757            name: "mem".to_string(),
758            namespace: "global".to_string(),
759            action: "updated".to_string(),
760            operation: "updated".to_string(),
761            version: 2,
762            entities_persisted: 3,
763            relationships_persisted: 1,
764            relationships_truncated: false,
765            extraction_method: None,
766            chunks_created: 2,
767            chunks_persisted: 2,
768            urls_persisted: 0,
769            merged_into_memory_id: None,
770            warnings: vec![],
771            created_at: 0,
772            created_at_iso: "1970-01-01T00:00:00Z".to_string(),
773            elapsed_ms: 0,
774            name_was_normalized: false,
775            original_name: None,
776        };
777
778        let json = serde_json::to_value(&resp).expect("serialization failed");
779        assert_eq!(
780            json["action"], json["operation"],
781            "action e operation devem ser iguais"
782        );
783        assert_eq!(json["entities_persisted"], 3);
784        assert_eq!(json["relationships_persisted"], 1);
785        assert_eq!(json["chunks_created"], 2);
786    }
787
788    #[test]
789    fn remember_response_warnings_lista_mensagens() {
790        let resp = RememberResponse {
791            memory_id: 5,
792            name: "dup-mem".to_string(),
793            namespace: "global".to_string(),
794            action: "created".to_string(),
795            operation: "created".to_string(),
796            version: 1,
797            entities_persisted: 0,
798            extraction_method: None,
799            relationships_persisted: 0,
800            relationships_truncated: false,
801            chunks_created: 1,
802            chunks_persisted: 0,
803            urls_persisted: 0,
804            merged_into_memory_id: None,
805            warnings: vec!["identical body already exists as memory id 3".to_string()],
806            created_at: 0,
807            created_at_iso: "1970-01-01T00:00:00Z".to_string(),
808            elapsed_ms: 10,
809            name_was_normalized: false,
810            original_name: None,
811        };
812
813        let json = serde_json::to_value(&resp).expect("serialization failed");
814        let warnings = json["warnings"]
815            .as_array()
816            .expect("warnings deve ser array");
817        assert_eq!(warnings.len(), 1);
818        assert!(warnings[0].as_str().unwrap().contains("identical body"));
819    }
820
821    #[test]
822    fn invalid_name_reserved_prefix_returns_validation_error() {
823        use crate::errors::AppError;
824        // Validates the rejection logic for names with the "__" prefix directly
825        let nome = "__reservado";
826        let resultado: Result<(), AppError> = if nome.starts_with("__") {
827            Err(AppError::Validation(
828                crate::i18n::validation::reserved_name(),
829            ))
830        } else {
831            Ok(())
832        };
833        assert!(resultado.is_err());
834        if let Err(AppError::Validation(msg)) = resultado {
835            assert!(!msg.is_empty());
836        }
837    }
838
839    #[test]
840    fn name_too_long_returns_validation_error() {
841        use crate::errors::AppError;
842        let nome_longo = "a".repeat(crate::constants::MAX_MEMORY_NAME_LEN + 1);
843        let resultado: Result<(), AppError> =
844            if nome_longo.is_empty() || nome_longo.len() > crate::constants::MAX_MEMORY_NAME_LEN {
845                Err(AppError::Validation(crate::i18n::validation::name_length(
846                    crate::constants::MAX_MEMORY_NAME_LEN,
847                )))
848            } else {
849                Ok(())
850            };
851        assert!(resultado.is_err());
852    }
853
854    #[test]
855    fn remember_response_merged_into_memory_id_some_serializes_integer() {
856        let resp = RememberResponse {
857            memory_id: 10,
858            name: "mem-mergeada".to_string(),
859            namespace: "global".to_string(),
860            action: "updated".to_string(),
861            operation: "updated".to_string(),
862            version: 3,
863            extraction_method: None,
864            entities_persisted: 0,
865            relationships_persisted: 0,
866            relationships_truncated: false,
867            chunks_created: 1,
868            chunks_persisted: 0,
869            urls_persisted: 0,
870            merged_into_memory_id: Some(7),
871            warnings: vec![],
872            created_at: 0,
873            created_at_iso: "1970-01-01T00:00:00Z".to_string(),
874            elapsed_ms: 0,
875            name_was_normalized: false,
876            original_name: None,
877        };
878
879        let json = serde_json::to_value(&resp).expect("serialization failed");
880        assert_eq!(json["merged_into_memory_id"], 7);
881    }
882
883    #[test]
884    fn remember_response_urls_persisted_serializes_field() {
885        // v1.0.24 P0-2: garante que urls_persisted aparece no JSON e aceita valor > 0.
886        let resp = RememberResponse {
887            memory_id: 3,
888            name: "mem-com-urls".to_string(),
889            namespace: "global".to_string(),
890            action: "created".to_string(),
891            operation: "created".to_string(),
892            version: 1,
893            entities_persisted: 0,
894            relationships_persisted: 0,
895            relationships_truncated: false,
896            chunks_created: 1,
897            chunks_persisted: 0,
898            urls_persisted: 3,
899            extraction_method: Some("regex-only".to_string()),
900            merged_into_memory_id: None,
901            warnings: vec![],
902            created_at: 0,
903            created_at_iso: "1970-01-01T00:00:00Z".to_string(),
904            elapsed_ms: 0,
905            name_was_normalized: false,
906            original_name: None,
907        };
908        let json = serde_json::to_value(&resp).expect("serialization failed");
909        assert_eq!(json["urls_persisted"], 3);
910    }
911
912    #[test]
913    fn empty_name_after_normalization_returns_specific_message() {
914        // P0-4 regression: name consisting only of hyphens normalizes to empty string;
915        // must produce a distinct error message, not the "too long" message.
916        use crate::errors::AppError;
917        let normalized = "---".to_lowercase().replace(['_', ' '], "-");
918        let normalized = normalized.trim_matches('-').to_string();
919        let resultado: Result<(), AppError> = if normalized.is_empty() {
920            Err(AppError::Validation(
921                "name cannot be empty after normalization (input was blank or contained only hyphens/underscores/spaces)".to_string(),
922            ))
923        } else {
924            Ok(())
925        };
926        assert!(resultado.is_err());
927        if let Err(AppError::Validation(msg)) = resultado {
928            assert!(
929                msg.contains("empty after normalization"),
930                "mensagem deve mencionar 'empty after normalization', obteve: {msg}"
931            );
932        }
933    }
934
935    #[test]
936    fn name_only_underscores_after_normalization_returns_specific_message() {
937        // P0-4 regression: name consisting only of underscores normalizes to empty string.
938        use crate::errors::AppError;
939        let normalized = "___".to_lowercase().replace(['_', ' '], "-");
940        let normalized = normalized.trim_matches('-').to_string();
941        assert!(
942            normalized.is_empty(),
943            "underscores devem normalizar para string vazia"
944        );
945        let resultado: Result<(), AppError> = if normalized.is_empty() {
946            Err(AppError::Validation(
947                "name cannot be empty after normalization (input was blank or contained only hyphens/underscores/spaces)".to_string(),
948            ))
949        } else {
950            Ok(())
951        };
952        assert!(resultado.is_err());
953        if let Err(AppError::Validation(msg)) = resultado {
954            assert!(
955                msg.contains("empty after normalization"),
956                "mensagem deve mencionar 'empty after normalization', obteve: {msg}"
957            );
958        }
959    }
960
961    #[test]
962    fn remember_response_relationships_truncated_serializes_field() {
963        // P1-D: garante que relationships_truncated aparece no JSON como bool.
964        let resp_false = RememberResponse {
965            memory_id: 1,
966            name: "test".to_string(),
967            namespace: "global".to_string(),
968            action: "created".to_string(),
969            operation: "created".to_string(),
970            version: 1,
971            entities_persisted: 2,
972            relationships_persisted: 1,
973            relationships_truncated: false,
974            chunks_created: 1,
975            chunks_persisted: 0,
976            urls_persisted: 0,
977            extraction_method: None,
978            merged_into_memory_id: None,
979            warnings: vec![],
980            created_at: 0,
981            created_at_iso: "1970-01-01T00:00:00Z".to_string(),
982            elapsed_ms: 0,
983            name_was_normalized: false,
984            original_name: None,
985        };
986        let json_false = serde_json::to_value(&resp_false).expect("serialization failed");
987        assert_eq!(json_false["relationships_truncated"], false);
988
989        let resp_true = RememberResponse {
990            relationships_truncated: true,
991            ..resp_false
992        };
993        let json_true = serde_json::to_value(&resp_true).expect("serialization failed");
994        assert_eq!(json_true["relationships_truncated"], true);
995    }
996}