garudust-core 0.3.0

Core traits, types, and error definitions for the Garudust AI agent framework
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
use async_trait::async_trait;
use chrono::{NaiveDate, Utc};

use crate::{
    config::MemoryExpiryConfig,
    error::{AgentError, ToolError},
};

pub const ENTRY_DELIMITER: &str = "\n§\n";

/// Maximum allowed characters in a single memory entry content string.
pub const MAX_ENTRY_CHARS: usize = 500;

/// Validate a memory entry content string before persisting.
///
/// Rejects content that exceeds the length limit or embeds XML tags that
/// could break out of the `<untrusted_memory>` boundary in the system prompt.
pub fn validate_memory_content(content: &str) -> Result<(), ToolError> {
    if content.chars().count() > MAX_ENTRY_CHARS {
        return Err(ToolError::InvalidArgs(
            "content exceeds 500 character limit".into(),
        ));
    }
    let lower = content.to_lowercase();
    if lower.contains("<untrusted_memory")
        || lower.contains("</untrusted_memory>")
        || lower.contains("</untrusted")
        || lower.contains("<untrusted_external_content")
    {
        return Err(ToolError::InvalidArgs(
            "content contains disallowed XML tags".into(),
        ));
    }
    Ok(())
}

/// Strip `<` and `>` from a memory entry content string at render time.
///
/// Existing entries written before validation was introduced may still contain
/// tag-breaking characters; this ensures nothing slips through to the prompt.
fn strip_angle_brackets(s: &str) -> String {
    s.replace(['<', '>'], "")
}

#[derive(Debug, Clone, PartialEq, Default)]
pub enum MemoryCategory {
    Fact,
    Preference,
    Skill,
    Project,
    #[default]
    Other,
}

impl MemoryCategory {
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Fact => "fact",
            Self::Preference => "preference",
            Self::Skill => "skill",
            Self::Project => "project",
            Self::Other => "other",
        }
    }

    pub fn from_name(s: &str) -> Self {
        match s {
            "fact" => Self::Fact,
            "preference" => Self::Preference,
            "skill" => Self::Skill,
            "project" => Self::Project,
            _ => Self::Other,
        }
    }

    pub fn display_name(&self) -> &'static str {
        match self {
            Self::Fact => "Facts",
            Self::Preference => "Preferences",
            Self::Skill => "Skills",
            Self::Project => "Project",
            Self::Other => "Other",
        }
    }
}

#[derive(Debug, Clone)]
pub struct MemoryEntry {
    pub category: MemoryCategory,
    pub content: String,
    pub created_at: String, // YYYY-MM-DD
}

impl MemoryEntry {
    pub fn new(category: MemoryCategory, content: String) -> Self {
        let created_at = Utc::now().format("%Y-%m-%d").to_string();
        Self {
            category,
            content,
            created_at,
        }
    }

    /// Parse `[category|date] content` or plain `content` (backward compat → Other).
    fn parse(raw: &str) -> Self {
        let raw = raw.trim();
        if let Some(rest) = raw.strip_prefix('[') {
            if let Some(bracket_end) = rest.find(']') {
                let meta = &rest[..bracket_end];
                let content = rest[bracket_end + 1..].trim().to_string();
                let mut parts = meta.splitn(2, '|');
                let cat_str = parts.next().unwrap_or("other");
                let date = parts.next().unwrap_or("").to_string();
                return Self {
                    category: MemoryCategory::from_name(cat_str),
                    content,
                    created_at: date,
                };
            }
        }
        Self {
            category: MemoryCategory::Other,
            content: raw.to_string(),
            created_at: String::new(),
        }
    }

    fn serialize(&self) -> String {
        format!(
            "[{}|{}] {}",
            self.category.as_str(),
            self.created_at,
            self.content
        )
    }
}

#[derive(Debug, Clone, Default)]
pub struct MemoryContent {
    pub entries: Vec<MemoryEntry>,
}

impl MemoryContent {
    pub fn parse(raw: &str) -> Self {
        let entries = raw
            .split(ENTRY_DELIMITER)
            .map(str::trim)
            .filter(|s| !s.is_empty())
            .map(MemoryEntry::parse)
            .collect();
        Self { entries }
    }

    pub fn serialize(&self) -> String {
        self.entries
            .iter()
            .map(MemoryEntry::serialize)
            .collect::<Vec<_>>()
            .join(ENTRY_DELIMITER)
    }

    /// Remove entries older than the per-category thresholds defined in `config`.
    /// Entries with no `created_at` date are never expired.
    /// Returns the number of entries removed.
    pub fn expire(&mut self, config: &MemoryExpiryConfig) -> usize {
        let today = Utc::now().date_naive();
        let before = self.entries.len();

        self.entries.retain(|e| {
            let max_days = match e.category {
                MemoryCategory::Fact => config.fact_days,
                MemoryCategory::Project => config.project_days,
                MemoryCategory::Other => config.other_days,
                MemoryCategory::Preference => config.preference_days,
                MemoryCategory::Skill => config.skill_days,
            };

            let Some(days) = max_days else {
                return true; // no limit for this category
            };

            let Ok(date) = NaiveDate::parse_from_str(&e.created_at, "%Y-%m-%d") else {
                return true; // no parseable date → keep
            };

            let age = (today - date).num_days();
            age <= i64::from(days)
        });

        before - self.entries.len()
    }

    /// Max entries returned by [`Self::prefetch`] to prevent context bloat.
    const PREFETCH_LIMIT: usize = 8;

    /// Five-or-more-char English stop words excluded from keyword matching.
    /// Only words that survive the `alpha.len() < 5` filter need to be listed here.
    const STOP_WORDS: &'static [&'static str] = &[
        "there", "about", "which", "where", "their", "those", "these", "every", "after", "other",
        "never", "still", "under", "again", "being", "since", "while", "shall", "might", "until",
        "above", "below", "maybe", "often", "quite", "would", "could", "whose", "whether",
        "however", "although", "because", "without", "within", "around", "before", "should",
        "through", "always", "almost", "already",
    ];

    /// Keyword-match recall: entries whose content contains any significant word
    /// from `query`. Tokens < 5 alphabetic chars and stop words are excluded to
    /// reduce false positives. Returns at most [`Self::PREFETCH_LIMIT`] entries,
    /// newest first.
    pub fn prefetch(&self, query: &str) -> Vec<&MemoryEntry> {
        let words: Vec<String> = query
            .split_whitespace()
            .filter_map(|w| {
                let alpha: String = w.chars().filter(|c| c.is_alphabetic()).collect();
                if alpha.len() < 5 {
                    return None;
                }
                let lower = alpha.to_lowercase();
                if Self::STOP_WORDS.contains(&lower.as_str()) {
                    return None;
                }
                Some(lower)
            })
            .collect();
        if words.is_empty() {
            return vec![];
        }
        let mut hits: Vec<&MemoryEntry> = self
            .entries
            .iter()
            .filter(|e| {
                let lower = e.content.to_lowercase();
                words.iter().any(|w| lower.contains(w.as_str()))
            })
            .collect();
        // Prefer newest entries (created_at is YYYY-MM-DD, lexicographically sortable).
        hits.sort_by(|a, b| b.created_at.cmp(&a.created_at));
        hits.truncate(Self::PREFETCH_LIMIT);
        hits
    }

    /// Format prefetch hits as a compact block for injection into the user message.
    /// Returns empty string when no hits.
    pub fn prefetch_for_prompt(&self, query: &str) -> String {
        let hits = self.prefetch(query);
        if hits.is_empty() {
            return String::new();
        }
        hits.iter()
            .map(|e| {
                let safe = strip_angle_brackets(&e.content);
                if e.created_at.is_empty() {
                    format!("- {safe} [{}]", e.category.display_name())
                } else {
                    format!(
                        "- {safe} [{}] ({})",
                        e.category.display_name(),
                        e.created_at
                    )
                }
            })
            .collect::<Vec<_>>()
            .join("\n")
    }

    /// Grouped markdown for the system prompt, wrapped in `<untrusted_memory>` tags.
    pub fn serialize_for_prompt(&self) -> String {
        if self.entries.is_empty() {
            return String::new();
        }

        let order = [
            MemoryCategory::Fact,
            MemoryCategory::Preference,
            MemoryCategory::Skill,
            MemoryCategory::Project,
            MemoryCategory::Other,
        ];

        let mut sections = Vec::new();
        for cat in &order {
            let items: Vec<&MemoryEntry> =
                self.entries.iter().filter(|e| &e.category == cat).collect();
            if items.is_empty() {
                continue;
            }
            let lines: Vec<String> = items
                .iter()
                .map(|e| {
                    let safe = strip_angle_brackets(&e.content);
                    if e.created_at.is_empty() {
                        format!("- {safe}")
                    } else {
                        format!("- {safe} ({})", e.created_at)
                    }
                })
                .collect();
            sections.push(format!("## {}\n{}", cat.display_name(), lines.join("\n")));
        }

        let inner = sections.join("\n\n");
        format!("<untrusted_memory>\n{inner}\n</untrusted_memory>")
    }
}

#[async_trait]
pub trait MemoryStore: Send + Sync + 'static {
    async fn read_memory(&self) -> Result<MemoryContent, AgentError>;
    async fn write_memory(&self, content: &MemoryContent) -> Result<(), AgentError>;

    async fn read_user_profile(&self) -> Result<String, AgentError>;
    async fn write_user_profile(&self, content: &str) -> Result<(), AgentError>;
}

#[cfg(test)]
mod tests {
    use super::*;

    // ── MemoryCategory ────────────────────────────────────────────────────────

    #[test]
    fn category_roundtrip() {
        for (s, cat) in [
            ("fact", MemoryCategory::Fact),
            ("preference", MemoryCategory::Preference),
            ("skill", MemoryCategory::Skill),
            ("project", MemoryCategory::Project),
            ("other", MemoryCategory::Other),
        ] {
            assert_eq!(MemoryCategory::from_name(s), cat);
            assert_eq!(cat.as_str(), s);
        }
    }

    #[test]
    fn unknown_category_becomes_other() {
        assert_eq!(MemoryCategory::from_name("unknown"), MemoryCategory::Other);
        assert_eq!(MemoryCategory::from_name(""), MemoryCategory::Other);
    }

    // ── MemoryEntry parse ─────────────────────────────────────────────────────

    #[test]
    fn parse_structured_entry() {
        let e = MemoryEntry::parse("[fact|2026-04-27] user likes Rust");
        assert_eq!(e.category, MemoryCategory::Fact);
        assert_eq!(e.content, "user likes Rust");
        assert_eq!(e.created_at, "2026-04-27");
    }

    #[test]
    fn parse_all_category_prefixes() {
        for cat in ["fact", "preference", "skill", "project", "other"] {
            let raw = format!("[{cat}|2026-01-01] some content");
            let e = MemoryEntry::parse(&raw);
            assert_eq!(e.category, MemoryCategory::from_name(cat));
            assert_eq!(e.content, "some content");
        }
    }

    #[test]
    fn parse_plain_entry_backward_compat() {
        let e = MemoryEntry::parse("old plain memory entry");
        assert_eq!(e.category, MemoryCategory::Other);
        assert_eq!(e.content, "old plain memory entry");
        assert!(e.created_at.is_empty());
    }

    #[test]
    fn parse_trims_whitespace() {
        let e = MemoryEntry::parse("  [fact|2026-04-27]   trimmed content  ");
        assert_eq!(e.content, "trimmed content");
    }

    // ── MemoryContent parse / serialize ───────────────────────────────────────

    #[test]
    fn parse_empty_string() {
        let mc = MemoryContent::parse("");
        assert!(mc.entries.is_empty());
    }

    #[test]
    fn parse_whitespace_only() {
        let mc = MemoryContent::parse("   \n  ");
        assert!(mc.entries.is_empty());
    }

    #[test]
    fn serialize_roundtrip() {
        let raw = "[fact|2026-04-27] entry one\n§\n[preference|2026-04-27] entry two";
        let mc = MemoryContent::parse(raw);
        assert_eq!(mc.entries.len(), 2);
        let serialized = mc.serialize();
        let mc2 = MemoryContent::parse(&serialized);
        assert_eq!(mc2.entries.len(), 2);
        assert_eq!(mc2.entries[0].content, "entry one");
        assert_eq!(mc2.entries[1].content, "entry two");
        assert_eq!(mc2.entries[0].category, MemoryCategory::Fact);
        assert_eq!(mc2.entries[1].category, MemoryCategory::Preference);
    }

    #[test]
    fn serialize_roundtrip_backward_compat() {
        // Plain entries (no prefix) must survive roundtrip as Other
        let raw = "plain old entry\n§\n[fact|2026-01-01] new entry";
        let mc = MemoryContent::parse(raw);
        assert_eq!(mc.entries.len(), 2);
        assert_eq!(mc.entries[0].category, MemoryCategory::Other);
        assert_eq!(mc.entries[0].content, "plain old entry");
    }

    // ── serialize_for_prompt ─────────────────────────────────────────────────

    #[test]
    fn prompt_empty_when_no_entries() {
        let mc = MemoryContent::default();
        assert!(mc.serialize_for_prompt().is_empty());
    }

    #[test]
    fn prompt_groups_by_category() {
        let raw = "[preference|2026-04-27] short answers\n§\n[fact|2026-04-27] Rust is fast\n§\n[preference|2026-04-27] no emojis";
        let mc = MemoryContent::parse(raw);
        let prompt = mc.serialize_for_prompt();

        // Facts come before Preferences in the defined order
        let fact_pos = prompt.find("## Facts").unwrap();
        let pref_pos = prompt.find("## Preferences").unwrap();
        assert!(fact_pos < pref_pos);

        assert!(prompt.contains("- Rust is fast"));
        assert!(prompt.contains("- short answers"));
        assert!(prompt.contains("- no emojis"));
    }

    #[test]
    fn prompt_skips_empty_categories() {
        let raw = "[fact|2026-04-27] only facts here";
        let mc = MemoryContent::parse(raw);
        let prompt = mc.serialize_for_prompt();
        assert!(prompt.contains("## Facts"));
        assert!(!prompt.contains("## Preferences"));
        assert!(!prompt.contains("## Skills"));
        assert!(!prompt.contains("## Other"));
    }

    #[test]
    fn prompt_shows_date_when_present() {
        let raw = "[fact|2026-04-27] dated entry";
        let mc = MemoryContent::parse(raw);
        let prompt = mc.serialize_for_prompt();
        assert!(prompt.contains("(2026-04-27)"));
    }

    #[test]
    fn prompt_omits_date_for_plain_entries() {
        let raw = "plain entry no date";
        let mc = MemoryContent::parse(raw);
        let prompt = mc.serialize_for_prompt();
        assert!(prompt.contains("- plain entry no date"));
        assert!(!prompt.contains("()"));
    }

    // ── expire() ─────────────────────────────────────────────────────────────

    use crate::config::MemoryExpiryConfig;
    use chrono::{Duration, Utc};

    fn days_ago(n: i64) -> String {
        (Utc::now().date_naive() - Duration::days(n))
            .format("%Y-%m-%d")
            .to_string()
    }

    fn default_expiry() -> MemoryExpiryConfig {
        MemoryExpiryConfig::default() // fact=90, project=30, other=60
    }

    #[test]
    fn expire_removes_old_fact() {
        let raw = format!("[fact|{}] old fact", days_ago(91));
        let mut mc = MemoryContent::parse(&raw);
        let removed = mc.expire(&default_expiry());
        assert_eq!(removed, 1);
        assert!(mc.entries.is_empty());
    }

    #[test]
    fn expire_keeps_recent_fact() {
        let raw = format!("[fact|{}] recent fact", days_ago(10));
        let mut mc = MemoryContent::parse(&raw);
        let removed = mc.expire(&default_expiry());
        assert_eq!(removed, 0);
        assert_eq!(mc.entries.len(), 1);
    }

    #[test]
    fn expire_removes_old_project() {
        let raw = format!("[project|{}] stale project", days_ago(31));
        let mut mc = MemoryContent::parse(&raw);
        let removed = mc.expire(&default_expiry());
        assert_eq!(removed, 1);
    }

    #[test]
    fn expire_never_removes_preference() {
        let raw = format!("[preference|{}] old preference", days_ago(999));
        let mut mc = MemoryContent::parse(&raw);
        let removed = mc.expire(&default_expiry());
        assert_eq!(removed, 0);
        assert_eq!(mc.entries.len(), 1);
    }

    #[test]
    fn expire_never_removes_skill() {
        let raw = format!("[skill|{}] old skill", days_ago(999));
        let mut mc = MemoryContent::parse(&raw);
        let removed = mc.expire(&default_expiry());
        assert_eq!(removed, 0);
        assert_eq!(mc.entries.len(), 1);
    }

    #[test]
    fn expire_skips_entries_without_date() {
        let mut mc = MemoryContent::parse("plain old entry with no date");
        let removed = mc.expire(&default_expiry());
        assert_eq!(removed, 0);
        assert_eq!(mc.entries.len(), 1);
    }

    #[test]
    fn expire_returns_correct_count() {
        let raw = format!(
            "[fact|{}] keep\n§\n[fact|{}] drop one\n§\n[project|{}] drop two",
            days_ago(10),
            days_ago(91),
            days_ago(31),
        );
        let mut mc = MemoryContent::parse(&raw);
        let removed = mc.expire(&default_expiry());
        assert_eq!(removed, 2);
        assert_eq!(mc.entries.len(), 1);
        assert_eq!(mc.entries[0].content, "keep");
    }

    // ── prefetch ──────────────────────────────────────────────────────────────

    #[test]
    fn prefetch_returns_matching_entries() {
        let raw = "[preference|2026-04-29] user drinks black coffee\n§\n[fact|2026-04-29] user lives in Bangkok";
        let mc = MemoryContent::parse(raw);
        // "coffee" is 6 chars and not a stop word — should match only the first entry
        let hits = mc.prefetch("about coffee");
        assert_eq!(hits.len(), 1);
        assert!(hits[0].content.contains("coffee"));
    }

    #[test]
    fn prefetch_returns_empty_when_no_match() {
        let raw = "[preference|2026-04-29] user likes black coffee";
        let mc = MemoryContent::parse(raw);
        // "weather" doesn't appear in the entry
        let hits = mc.prefetch("current weather");
        assert!(hits.is_empty());
    }

    #[test]
    fn prefetch_is_case_insensitive() {
        let raw = "[preference|2026-04-29] user likes Black Coffee";
        let mc = MemoryContent::parse(raw);
        let hits = mc.prefetch("COFFEE");
        assert_eq!(hits.len(), 1);
    }

    #[test]
    fn prefetch_strips_punctuation_from_query_words() {
        let raw = "[preference|2026-04-29] user drinks black coffee";
        let mc = MemoryContent::parse(raw);
        let hits = mc.prefetch("coffee?");
        assert_eq!(hits.len(), 1);
    }

    #[test]
    fn prefetch_ignores_short_words() {
        let raw = "[preference|2026-04-29] user likes tea";
        let mc = MemoryContent::parse(raw);
        // all words are < 5 alpha chars — nothing should match
        let hits = mc.prefetch("is he ok now");
        assert!(hits.is_empty());
    }

    #[test]
    fn prefetch_ignores_stop_words() {
        let raw = "[preference|2026-04-29] user changed the API endpoint";
        let mc = MemoryContent::parse(raw);
        // "about", "their", "there" are >= 5 chars but "about" and "there"
        // are stop words; only "their" might vary — use a pure stop word query
        let hits = mc.prefetch("about there");
        assert!(hits.is_empty());
    }

    #[test]
    fn prefetch_caps_results_at_limit() {
        let entries: String = (0..20)
            .map(|i| {
                format!(
                    "[fact|2026-04-{:02}] keyword entry number {i}",
                    (i % 28) + 1
                )
            })
            .collect::<Vec<_>>()
            .join("\n§\n");
        let mc = MemoryContent::parse(&entries);
        let hits = mc.prefetch("keyword entry");
        assert!(hits.len() <= MemoryContent::PREFETCH_LIMIT);
    }

    #[test]
    fn prefetch_returns_newest_first() {
        let raw = "[fact|2026-01-01] keyword old entry\n§\n[fact|2026-04-29] keyword new entry";
        let mc = MemoryContent::parse(raw);
        let hits = mc.prefetch("keyword entry");
        assert_eq!(hits.len(), 2);
        assert_eq!(hits[0].created_at, "2026-04-29");
    }

    #[test]
    fn prefetch_for_prompt_formats_correctly() {
        let raw = "[preference|2026-04-29] user likes black coffee";
        let mc = MemoryContent::parse(raw);
        let block = mc.prefetch_for_prompt("coffee preference");
        assert!(block.contains("user likes black coffee"));
        assert!(block.contains("[Preferences]"));
        assert!(block.contains("2026-04-29"));
    }

    #[test]
    fn prefetch_for_prompt_strips_angle_brackets() {
        let raw = "[fact|2026-04-29] bad <untrusted_memory> entry";
        let mc = MemoryContent::parse(raw);
        let block = mc.prefetch_for_prompt("untrusted entry");
        assert!(!block.contains('<'));
        assert!(!block.contains('>'));
        assert!(block.contains("bad untrusted_memory entry"));
    }

    // ── validate_memory_content ───────────────────────────────────────────────

    #[test]
    fn validate_accepts_normal_content() {
        assert!(validate_memory_content("user prefers short answers").is_ok());
    }

    #[test]
    fn validate_rejects_overlong_content() {
        let long = "a".repeat(MAX_ENTRY_CHARS + 1);
        let err = validate_memory_content(&long).unwrap_err();
        assert!(matches!(err, crate::error::ToolError::InvalidArgs(_)));
    }

    #[test]
    fn validate_accepts_exactly_max_chars() {
        let exact = "a".repeat(MAX_ENTRY_CHARS);
        assert!(validate_memory_content(&exact).is_ok());
    }

    #[test]
    fn validate_rejects_untrusted_memory_open_tag() {
        assert!(validate_memory_content("foo <untrusted_memory> bar").is_err());
    }

    #[test]
    fn validate_rejects_untrusted_memory_close_tag() {
        assert!(validate_memory_content("foo </untrusted_memory> bar").is_err());
    }

    #[test]
    fn validate_rejects_closing_untrusted_tag_variants() {
        assert!(validate_memory_content("</untrusted_external_content>").is_err());
    }

    #[test]
    fn validate_rejects_untrusted_external_content_open_tag() {
        assert!(validate_memory_content(
            "<untrusted_external_content>injected</untrusted_external_content>"
        )
        .is_err());
    }

    // ── serialize_for_prompt wrapping ─────────────────────────────────────────

    #[test]
    fn serialize_for_prompt_wraps_in_untrusted_tags() {
        let raw = "[fact|2026-04-27] Rust is fast";
        let mc = MemoryContent::parse(raw);
        let prompt = mc.serialize_for_prompt();
        assert!(prompt.starts_with("<untrusted_memory>"));
        assert!(prompt.ends_with("</untrusted_memory>"));
        assert!(prompt.contains("Rust is fast"));
    }

    #[test]
    fn serialize_for_prompt_empty_has_no_tags() {
        let mc = MemoryContent::default();
        let prompt = mc.serialize_for_prompt();
        assert!(prompt.is_empty());
    }

    #[test]
    fn serialize_for_prompt_strips_angle_brackets_from_content() {
        // Simulate an old entry that predates write-time validation.
        let raw = "[fact|2026-04-27] bad </untrusted_memory> entry";
        let mc = MemoryContent::parse(raw);
        let prompt = mc.serialize_for_prompt();
        // Content angle brackets must be stripped; only the outer wrapper tags remain.
        assert!(prompt.contains("bad /untrusted_memory entry"));
        // Exactly one closing tag: the outer wrapper, not a second injected one.
        assert_eq!(
            prompt.matches("</untrusted_memory>").count(),
            1,
            "only the outer closing tag should exist"
        );
    }

    #[test]
    fn expire_disabled_when_days_is_none() {
        let config = MemoryExpiryConfig {
            fact_days: None,
            project_days: None,
            other_days: None,
            preference_days: None,
            skill_days: None,
        };
        let raw = format!("[fact|{}] very old", days_ago(9999));
        let mut mc = MemoryContent::parse(&raw);
        let removed = mc.expire(&config);
        assert_eq!(removed, 0);
    }
}