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