caliban-memory 0.2.0

File-backed memory tiers spliced into the caliban system prompt — internal crate for the caliban binary; no API stability, pin exact versions
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
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
//! Per-project auto-memory: topic file enumerator, reader, and writer.
//!
//! See `docs/superpowers/specs/2026-05-24-auto-memory-design.md` and
//! `adrs/0035-auto-memory.md`.

use std::path::{Path, PathBuf};

use serde::Deserialize;

use crate::error::{MemoryError, Result};

/// The four memory-type categories the model classifies a topic file under.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TopicKind {
    /// Durable facts about the user.
    User,
    /// User-issued corrections / preferences for future interactions.
    Feedback,
    /// Durable project facts not already in the repo.
    Project,
    /// Stable external context (account IDs, URLs, API quotas).
    Reference,
}

impl TopicKind {
    /// Lower-case wire form.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::User => "user",
            Self::Feedback => "feedback",
            Self::Project => "project",
            Self::Reference => "reference",
        }
    }

    /// Parse from a string, accepting case-insensitively. Returns `None` for
    /// any input that is not one of the four valid types.
    #[must_use]
    pub fn parse(s: &str) -> Option<Self> {
        match s.trim().to_ascii_lowercase().as_str() {
            "user" => Some(Self::User),
            "feedback" => Some(Self::Feedback),
            "project" => Some(Self::Project),
            "reference" => Some(Self::Reference),
            _ => None,
        }
    }
}

/// Lightweight summary of a topic file (frontmatter only).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TopicSummary {
    /// The slug (kebab-case, must match filename stem).
    pub name: String,
    /// One-line description (≤ 120 chars by convention).
    pub description: String,
    /// Memory type classification.
    pub kind: TopicKind,
    /// Absolute path to the topic file.
    pub path: PathBuf,
}

/// A fully-loaded topic file.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TopicFile {
    /// The slug (kebab-case).
    pub name: String,
    /// One-line description.
    pub description: String,
    /// Memory type classification.
    pub kind: TopicKind,
    /// Markdown body (everything after the closing frontmatter `---`).
    pub body: String,
    /// Absolute path.
    pub path: PathBuf,
}

/// Draft passed to [`TopicLoader::write`]. The loader fills in path / on-disk
/// frontmatter from these fields.
#[derive(Debug, Clone)]
pub struct TopicDraft {
    /// The slug (kebab-case). Must pass [`validate_slug`].
    pub name: String,
    /// One-line description for the `MEMORY.md` index entry + frontmatter.
    pub description: String,
    /// Memory type classification.
    pub kind: TopicKind,
    /// Raw markdown body (no frontmatter — the loader emits it).
    pub body: String,
}

/// Frontmatter shape used by [`TopicLoader::read`] / [`TopicLoader::list`].
#[derive(Debug, Deserialize)]
struct RawFrontmatter {
    name: String,
    description: String,
    #[serde(default)]
    metadata: RawMetadata,
}

#[derive(Debug, Default, Deserialize)]
struct RawMetadata {
    #[serde(rename = "type")]
    kind: Option<String>,
}

/// Enumerator + reader/writer for topic `.md` files under a single memory
/// directory.
#[derive(Debug, Clone)]
pub struct TopicLoader {
    dir: PathBuf,
}

impl TopicLoader {
    /// Construct a loader over the given memory directory. The directory does
    /// not have to exist yet — `list` returns an empty vec, and `write` will
    /// create it on demand.
    #[must_use]
    pub fn new(dir: impl Into<PathBuf>) -> Self {
        Self { dir: dir.into() }
    }

    /// The directory this loader manages.
    #[must_use]
    pub fn dir(&self) -> &Path {
        &self.dir
    }

    /// Enumerate every `.md` sibling of `MEMORY.md`, parsing frontmatter for
    /// each. Files with malformed frontmatter are silently skipped with a
    /// `warn!` log entry (rationale: a single corrupted topic file should not
    /// brick the whole memory tier).
    ///
    /// # Errors
    ///
    /// Returns [`MemoryError::Io`] if the directory exists but cannot be read.
    pub fn list(&self) -> Result<Vec<TopicSummary>> {
        let mut out = Vec::new();
        if !self.dir.exists() {
            return Ok(out);
        }
        let entries = std::fs::read_dir(&self.dir).map_err(|source| MemoryError::Io {
            path: self.dir.clone(),
            source,
        })?;
        for entry in entries.flatten() {
            let path = entry.path();
            if !path.is_file() {
                continue;
            }
            let Some(stem) = path.file_stem().and_then(|s| s.to_str()) else {
                continue;
            };
            if path.extension().and_then(|s| s.to_str()) != Some("md") {
                continue;
            }
            // Skip the index file itself.
            if path.file_name().and_then(|s| s.to_str()) == Some("MEMORY.md") {
                continue;
            }
            match Self::read_summary(&path) {
                Ok(mut summary) => {
                    if summary.name != stem {
                        tracing::warn!(
                            target: caliban_common::tracing_targets::TARGET_MEMORY_AUTO,
                            path = %path.display(),
                            frontmatter_name = %summary.name,
                            file_stem = %stem,
                            "topic frontmatter name does not match filename; using filename",
                        );
                        summary.name = stem.to_string();
                    }
                    out.push(summary);
                }
                Err(e) => {
                    tracing::warn!(
                        target: caliban_common::tracing_targets::TARGET_MEMORY_AUTO,
                        path = %path.display(),
                        error = %e,
                        "skipping malformed topic file",
                    );
                }
            }
        }
        out.sort_by(|a, b| a.name.cmp(&b.name));
        Ok(out)
    }

    /// Read a topic by slug. The slug must pass [`validate_slug`] — no path
    /// separators, no `..`, no leading `.`.
    ///
    /// # Errors
    ///
    /// Returns [`MemoryError::InvalidSlug`] for traversal / illegal slugs,
    /// [`MemoryError::Io`] if the file does not exist or cannot be read, and
    /// [`MemoryError::InvalidTopic`] if the frontmatter is malformed.
    pub fn read(&self, name: &str) -> Result<TopicFile> {
        validate_slug(name)?;
        let path = self.dir.join(format!("{name}.md"));
        let raw = std::fs::read_to_string(&path).map_err(|source| MemoryError::Io {
            path: path.clone(),
            source,
        })?;
        let (fm, body) = parse_frontmatter(&raw, &path)?;
        let kind =
            TopicKind::parse(fm.metadata.kind.as_deref().unwrap_or("")).ok_or_else(|| {
                MemoryError::InvalidTopic {
                    path: path.clone(),
                    reason: format!(
                        "metadata.type must be one of user|feedback|project|reference (got {:?})",
                        fm.metadata.kind
                    ),
                }
            })?;
        Ok(TopicFile {
            name: fm.name,
            description: fm.description,
            kind,
            body: body.to_string(),
            path,
        })
    }

    /// Atomically write a topic file (`<slug>.md`) and update the `MEMORY.md`
    /// index line for it. Returns the topic's absolute path on success.
    ///
    /// Write semantics:
    /// 1. Write the topic body + frontmatter to `<slug>.md.tmp`.
    /// 2. Rename to `<slug>.md` (atomic on the same filesystem).
    /// 3. Rewrite `MEMORY.md` with an updated index line for the slug
    ///    (`MEMORY.md` is rewritten via the same tmp+rename dance).
    ///
    /// A crash between (2) and (3) leaves an orphan topic file that
    /// `rebuild-index` can re-detect.
    ///
    /// # Errors
    ///
    /// Returns [`MemoryError::InvalidSlug`] for bad slugs, [`MemoryError::Io`]
    /// on any IO failure.
    pub fn write(&self, draft: &TopicDraft) -> Result<PathBuf> {
        validate_slug(&draft.name)?;
        std::fs::create_dir_all(&self.dir).map_err(|source| MemoryError::Io {
            path: self.dir.clone(),
            source,
        })?;

        let path = self.dir.join(format!("{}.md", draft.name));
        let serialized = render_topic_file(draft);
        caliban_common::fs::write_atomic(&path, serialized.as_bytes()).map_err(|source| {
            MemoryError::Io {
                path: path.clone(),
                source,
            }
        })?;

        update_index_line(&self.dir, draft)?;
        Ok(path)
    }

    /// Delete a topic file by slug and remove its `MEMORY.md` index line.
    /// Missing files are treated as success (idempotent delete).
    ///
    /// # Errors
    ///
    /// Returns [`MemoryError::InvalidSlug`] for bad slugs or [`MemoryError::Io`]
    /// on IO failure.
    pub fn delete(&self, name: &str) -> Result<()> {
        validate_slug(name)?;
        let path = self.dir.join(format!("{name}.md"));
        match std::fs::remove_file(&path) {
            Ok(()) | Err(_) if !path.exists() => {}
            Err(e) => {
                return Err(MemoryError::Io {
                    path: path.clone(),
                    source: e,
                });
            }
            Ok(()) => {}
        }
        remove_index_line(&self.dir, name)?;
        Ok(())
    }

    fn read_summary(path: &Path) -> Result<TopicSummary> {
        let raw = std::fs::read_to_string(path).map_err(|source| MemoryError::Io {
            path: path.to_path_buf(),
            source,
        })?;
        let (fm, _) = parse_frontmatter(&raw, path)?;
        let kind =
            TopicKind::parse(fm.metadata.kind.as_deref().unwrap_or("")).ok_or_else(|| {
                MemoryError::InvalidTopic {
                    path: path.to_path_buf(),
                    reason: format!(
                        "metadata.type must be one of user|feedback|project|reference (got {:?})",
                        fm.metadata.kind
                    ),
                }
            })?;
        Ok(TopicSummary {
            name: fm.name,
            description: fm.description,
            kind,
            path: path.to_path_buf(),
        })
    }
}

/// Validate a topic slug. Rules: non-empty, no path separators (`/`, `\\`),
/// no `..`, no leading dot.
///
/// # Errors
///
/// Returns [`MemoryError::InvalidSlug`] if the slug fails any rule.
pub fn validate_slug(slug: &str) -> Result<()> {
    if slug.is_empty() {
        return Err(MemoryError::InvalidSlug {
            slug: slug.to_string(),
            reason: "slug must be non-empty".into(),
        });
    }
    if slug.contains('/') || slug.contains('\\') {
        return Err(MemoryError::InvalidSlug {
            slug: slug.to_string(),
            reason: "slug must not contain path separators".into(),
        });
    }
    if slug.contains("..") {
        return Err(MemoryError::InvalidSlug {
            slug: slug.to_string(),
            reason: "slug must not contain '..'".into(),
        });
    }
    if slug.starts_with('.') {
        return Err(MemoryError::InvalidSlug {
            slug: slug.to_string(),
            reason: "slug must not start with '.'".into(),
        });
    }
    if slug.contains('\0') {
        return Err(MemoryError::InvalidSlug {
            slug: slug.to_string(),
            reason: "slug must not contain NUL".into(),
        });
    }
    Ok(())
}

/// Split a raw file into frontmatter struct + body. Frontmatter delimiters are
/// `---\n` opening and `\n---\n` (or `\n---` at EOF) closing.
fn parse_frontmatter<'a>(raw: &'a str, path: &Path) -> Result<(RawFrontmatter, &'a str)> {
    let trimmed = raw.trim_start_matches('\u{feff}');
    let body_start = "---\n";
    if !trimmed.starts_with(body_start) {
        return Err(MemoryError::InvalidTopic {
            path: path.to_path_buf(),
            reason: "missing leading `---` frontmatter delimiter".into(),
        });
    }
    let after_start = &trimmed[body_start.len()..];
    let Some(end_idx) = after_start.find("\n---\n").or_else(|| {
        after_start
            .find("\n---")
            .filter(|i| after_start[*i..].starts_with("\n---"))
    }) else {
        return Err(MemoryError::InvalidTopic {
            path: path.to_path_buf(),
            reason: "missing closing `---` frontmatter delimiter".into(),
        });
    };
    let yaml_chunk = &after_start[..end_idx];
    let body_start_offset = end_idx + "\n---\n".len();
    let body = if body_start_offset >= after_start.len() {
        ""
    } else {
        &after_start[body_start_offset..]
    };
    let fm: RawFrontmatter =
        serde_yaml::from_str(yaml_chunk).map_err(|e| MemoryError::InvalidTopic {
            path: path.to_path_buf(),
            reason: format!("yaml: {e}"),
        })?;
    if fm.name.trim().is_empty() {
        return Err(MemoryError::InvalidTopic {
            path: path.to_path_buf(),
            reason: "name must be non-empty".into(),
        });
    }
    if fm.description.trim().is_empty() {
        return Err(MemoryError::InvalidTopic {
            path: path.to_path_buf(),
            reason: "description must be non-empty".into(),
        });
    }
    Ok((fm, body))
}

/// Render a [`TopicDraft`] to on-disk markdown (frontmatter + body).
fn render_topic_file(draft: &TopicDraft) -> String {
    let mut out = String::with_capacity(draft.body.len() + 256);
    out.push_str("---\n");
    out.push_str("name: ");
    out.push_str(&draft.name);
    out.push('\n');
    out.push_str("description: \"");
    out.push_str(&escape_yaml_string(&draft.description));
    out.push_str("\"\n");
    out.push_str("metadata:\n");
    out.push_str("  node_type: memory\n");
    out.push_str("  type: ");
    out.push_str(draft.kind.as_str());
    out.push('\n');
    out.push_str("---\n\n");
    out.push_str(&draft.body);
    if !draft.body.ends_with('\n') {
        out.push('\n');
    }
    out
}

/// Escape `"` and `\` for a double-quoted YAML scalar.
fn escape_yaml_string(s: &str) -> String {
    let mut out = String::with_capacity(s.len());
    for ch in s.chars() {
        match ch {
            '"' => out.push_str("\\\""),
            '\\' => out.push_str("\\\\"),
            '\n' => out.push_str("\\n"),
            '\r' => out.push_str("\\r"),
            c => out.push(c),
        }
    }
    out
}

/// Update (or insert) the index line for `draft.name` inside `MEMORY.md`.
/// Atomic via tmp + rename.
fn update_index_line(dir: &Path, draft: &TopicDraft) -> Result<()> {
    let index_path = dir.join("MEMORY.md");
    let existing = match std::fs::read_to_string(&index_path) {
        Ok(s) => s,
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => String::new(),
        Err(source) => {
            return Err(MemoryError::Io {
                path: index_path.clone(),
                source,
            });
        }
    };

    let new_line = format!(
        "- [{title}]({slug}.md) — {kind}: {desc}",
        title = draft.name,
        slug = draft.name,
        kind = draft.kind.as_str(),
        desc = draft.description.lines().next().unwrap_or("").trim(),
    );

    let new_body = rewrite_with_index_line(&existing, &draft.name, &new_line);
    caliban_common::fs::write_atomic(&index_path, new_body.as_bytes()).map_err(|source| {
        MemoryError::Io {
            path: index_path.clone(),
            source,
        }
    })?;
    Ok(())
}

/// Remove a topic's index line, if present.
fn remove_index_line(dir: &Path, slug: &str) -> Result<()> {
    let index_path = dir.join("MEMORY.md");
    let existing = match std::fs::read_to_string(&index_path) {
        Ok(s) => s,
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(()),
        Err(source) => {
            return Err(MemoryError::Io {
                path: index_path.clone(),
                source,
            });
        }
    };
    let needle = format!("]({slug}.md)");
    let kept: Vec<&str> = existing.lines().filter(|l| !l.contains(&needle)).collect();
    let mut new_body = kept.join("\n");
    if existing.ends_with('\n') && !new_body.ends_with('\n') {
        new_body.push('\n');
    }
    caliban_common::fs::write_atomic(&index_path, new_body.as_bytes()).map_err(|source| {
        MemoryError::Io {
            path: index_path.clone(),
            source,
        }
    })?;
    Ok(())
}

/// Insert-or-replace the index line for `slug`. We match on the
/// `](<slug>.md)` substring, which is robust against operators tweaking the
/// title or one-line summary in place.
fn rewrite_with_index_line(existing: &str, slug: &str, new_line: &str) -> String {
    if existing.is_empty() {
        let mut s = String::from("# Memory index\n\n");
        s.push_str(new_line);
        s.push('\n');
        return s;
    }
    let needle = format!("]({slug}.md)");
    let mut replaced = false;
    let mut out_lines: Vec<String> = Vec::with_capacity(existing.lines().count() + 1);
    for line in existing.lines() {
        if !replaced && line.contains(&needle) {
            out_lines.push(new_line.to_string());
            replaced = true;
        } else {
            out_lines.push(line.to_string());
        }
    }
    if !replaced {
        // Append after the last existing index-style line, otherwise at EOF.
        let mut insert_idx = out_lines.len();
        // Insert after the last `- [` bullet line if any exist.
        for (i, line) in out_lines.iter().enumerate().rev() {
            if line.trim_start().starts_with("- [") {
                insert_idx = i + 1;
                break;
            }
        }
        out_lines.insert(insert_idx, new_line.to_string());
    }
    let mut s = out_lines.join("\n");
    if existing.ends_with('\n') || !s.ends_with('\n') {
        s.push('\n');
    }
    s
}

/// Strip every `<!-- … -->` HTML comment (greedy, multi-line) from `body`.
/// Used by the memory loader before splicing into the system prompt — the
/// on-disk file is untouched.
#[must_use]
pub fn strip_html_comments(body: &str) -> String {
    let mut out = String::with_capacity(body.len());
    let bytes = body.as_bytes();
    let mut i = 0;
    while i < bytes.len() {
        if i + 3 < bytes.len() && &bytes[i..i + 4] == b"<!--" {
            // find closing -->; if not found, drop the rest.
            if let Some(end) = find_subslice(&bytes[i + 4..], b"-->") {
                i += 4 + end + 3;
                continue;
            }
            break;
        }
        out.push(bytes[i] as char);
        i += 1;
    }
    out
}

fn find_subslice(hay: &[u8], needle: &[u8]) -> Option<usize> {
    if needle.is_empty() || needle.len() > hay.len() {
        return None;
    }
    for i in 0..=hay.len() - needle.len() {
        if &hay[i..i + needle.len()] == needle {
            return Some(i);
        }
    }
    None
}

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

    fn topic_md(name: &str, kind: &str, desc: &str, body: &str) -> String {
        format!(
            "---\nname: {name}\ndescription: \"{desc}\"\nmetadata:\n  node_type: memory\n  type: {kind}\n---\n\n{body}\n",
        )
    }

    #[test]
    fn list_enumerates_topic_files_excluding_memory_md() {
        let tmp = TempDir::new().unwrap();
        let dir = tmp.path();
        std::fs::write(
            dir.join("MEMORY.md"),
            "# Memory index\n\n- [foo](foo.md) — user: foo\n",
        )
        .unwrap();
        std::fs::write(
            dir.join("foo.md"),
            topic_md("foo", "user", "foo desc", "body"),
        )
        .unwrap();
        std::fs::write(
            dir.join("bar.md"),
            topic_md("bar", "feedback", "bar desc", "body"),
        )
        .unwrap();

        let loader = TopicLoader::new(dir.to_path_buf());
        let topics = loader.list().unwrap();
        let names: Vec<_> = topics.iter().map(|t| t.name.as_str()).collect();
        assert_eq!(names, vec!["bar", "foo"]);
        assert!(topics.iter().any(|t| matches!(t.kind, TopicKind::User)));
        assert!(topics.iter().any(|t| matches!(t.kind, TopicKind::Feedback)));
    }

    #[test]
    fn read_round_trips_a_topic() {
        let tmp = TempDir::new().unwrap();
        let dir = tmp.path();
        std::fs::write(
            dir.join("user-role.md"),
            topic_md(
                "user-role",
                "user",
                "role + context",
                "# User role\n\nSenior engineer.\n",
            ),
        )
        .unwrap();

        let loader = TopicLoader::new(dir.to_path_buf());
        let topic = loader.read("user-role").unwrap();
        assert_eq!(topic.name, "user-role");
        assert_eq!(topic.kind, TopicKind::User);
        assert!(topic.body.contains("Senior engineer."));
    }

    #[test]
    fn write_creates_topic_and_updates_index() {
        let tmp = TempDir::new().unwrap();
        let dir = tmp.path();
        std::fs::write(dir.join("MEMORY.md"), "# Memory index\n\n").unwrap();
        let loader = TopicLoader::new(dir.to_path_buf());
        let path = loader
            .write(&TopicDraft {
                name: "personal-email".to_string(),
                description: "use personal email for ~/dev/personal/**".to_string(),
                kind: TopicKind::Feedback,
                body: "Use john.ford2002@gmail.com.\n".to_string(),
            })
            .unwrap();
        assert!(path.exists());
        assert!(!dir.join("personal-email.md.tmp").exists());

        // Topic file contains frontmatter + body.
        let written = std::fs::read_to_string(&path).unwrap();
        assert!(written.contains("name: personal-email"));
        assert!(written.contains("type: feedback"));
        assert!(written.contains("john.ford2002@gmail.com"));

        // Index updated.
        let index = std::fs::read_to_string(dir.join("MEMORY.md")).unwrap();
        assert!(index.contains("[personal-email](personal-email.md)"));
        assert!(index.contains("feedback:"));
    }

    #[test]
    fn write_updates_existing_index_line_in_place() {
        let tmp = TempDir::new().unwrap();
        let dir = tmp.path();
        std::fs::write(
            dir.join("MEMORY.md"),
            "# Memory index\n\n- [foo](foo.md) — user: old desc\n",
        )
        .unwrap();
        let loader = TopicLoader::new(dir.to_path_buf());
        loader
            .write(&TopicDraft {
                name: "foo".to_string(),
                description: "new desc".to_string(),
                kind: TopicKind::User,
                body: "body".to_string(),
            })
            .unwrap();
        let index = std::fs::read_to_string(dir.join("MEMORY.md")).unwrap();
        // exactly one entry for foo
        assert_eq!(index.matches("[foo](foo.md)").count(), 1);
        assert!(index.contains("new desc"));
        assert!(!index.contains("old desc"));
    }

    #[test]
    fn read_rejects_invalid_type_in_frontmatter() {
        let tmp = TempDir::new().unwrap();
        let dir = tmp.path();
        std::fs::write(dir.join("bad.md"), topic_md("bad", "junk", "desc", "body")).unwrap();
        let loader = TopicLoader::new(dir.to_path_buf());
        let err = loader.read("bad").unwrap_err();
        assert!(matches!(err, MemoryError::InvalidTopic { .. }));
    }

    #[test]
    fn read_rejects_missing_required_frontmatter_fields() {
        let tmp = TempDir::new().unwrap();
        let dir = tmp.path();
        std::fs::write(
            dir.join("incomplete.md"),
            "---\ndescription: \"no name\"\nmetadata:\n  type: user\n---\n\nbody\n",
        )
        .unwrap();
        let loader = TopicLoader::new(dir.to_path_buf());
        let err = loader.read("incomplete").unwrap_err();
        assert!(matches!(err, MemoryError::InvalidTopic { .. }));
    }

    #[test]
    fn cross_reference_brackets_preserved_in_body() {
        let tmp = TempDir::new().unwrap();
        let dir = tmp.path();
        let body = "Crosslinks: [[parity-gap-matrix]], [[sprint-mode]].\n".to_string();
        let loader = TopicLoader::new(dir.to_path_buf());
        loader
            .write(&TopicDraft {
                name: "user-role".to_string(),
                description: "role".to_string(),
                kind: TopicKind::User,
                body: body.clone(),
            })
            .unwrap();
        let topic = loader.read("user-role").unwrap();
        assert!(topic.body.contains("[[parity-gap-matrix]]"));
        assert!(topic.body.contains("[[sprint-mode]]"));
    }

    #[test]
    fn validate_slug_rejects_path_traversal() {
        assert!(validate_slug("ok").is_ok());
        assert!(validate_slug("ok-slug_1").is_ok());
        assert!(validate_slug("").is_err());
        assert!(validate_slug("a/b").is_err());
        assert!(validate_slug("a\\b").is_err());
        assert!(validate_slug("..").is_err());
        assert!(validate_slug("a..b").is_err());
        assert!(validate_slug(".hidden").is_err());
    }

    #[test]
    fn strip_html_comments_handles_single_and_multiline() {
        let single = "hello <!-- inline --> world";
        assert_eq!(strip_html_comments(single), "hello  world");

        let multi = "before\n<!-- line one\nline two\n-->\nafter";
        let stripped = strip_html_comments(multi);
        assert!(stripped.contains("before"));
        assert!(stripped.contains("after"));
        assert!(!stripped.contains("line one"));
        assert!(!stripped.contains("line two"));
    }

    #[test]
    fn delete_removes_file_and_index_line() {
        let tmp = TempDir::new().unwrap();
        let dir = tmp.path();
        let loader = TopicLoader::new(dir.to_path_buf());
        loader
            .write(&TopicDraft {
                name: "tmp-topic".to_string(),
                description: "tmp".to_string(),
                kind: TopicKind::Project,
                body: "body".to_string(),
            })
            .unwrap();
        loader.delete("tmp-topic").unwrap();
        assert!(!dir.join("tmp-topic.md").exists());
        let index = std::fs::read_to_string(dir.join("MEMORY.md")).unwrap();
        assert!(!index.contains("tmp-topic.md"));
    }

    // --- TopicKind ----------------------------------------------------------

    #[test]
    fn topic_kind_as_str_covers_all_variants() {
        assert_eq!(TopicKind::User.as_str(), "user");
        assert_eq!(TopicKind::Feedback.as_str(), "feedback");
        assert_eq!(TopicKind::Project.as_str(), "project");
        assert_eq!(TopicKind::Reference.as_str(), "reference");
    }

    #[test]
    fn topic_kind_parse_is_case_and_whitespace_insensitive() {
        assert_eq!(TopicKind::parse("USER"), Some(TopicKind::User));
        assert_eq!(TopicKind::parse("  Feedback  "), Some(TopicKind::Feedback));
        assert_eq!(TopicKind::parse("Project"), Some(TopicKind::Project));
        assert_eq!(TopicKind::parse("rEfErEnCe"), Some(TopicKind::Reference));
    }

    #[test]
    fn topic_kind_parse_rejects_unknown_and_empty() {
        assert_eq!(TopicKind::parse(""), None);
        assert_eq!(TopicKind::parse("   "), None);
        assert_eq!(TopicKind::parse("junk"), None);
    }

    // --- TopicLoader accessors / list edge cases ----------------------------

    #[test]
    fn loader_dir_returns_managed_directory() {
        let tmp = TempDir::new().unwrap();
        let loader = TopicLoader::new(tmp.path().to_path_buf());
        assert_eq!(loader.dir(), tmp.path());
    }

    #[test]
    fn list_on_nonexistent_dir_returns_empty() {
        let tmp = TempDir::new().unwrap();
        let missing = tmp.path().join("does-not-exist");
        let loader = TopicLoader::new(missing);
        assert!(loader.list().unwrap().is_empty());
    }

    #[test]
    fn list_skips_non_md_files_and_subdirectories() {
        let tmp = TempDir::new().unwrap();
        let dir = tmp.path();
        std::fs::write(dir.join("notes.txt"), "not markdown").unwrap();
        std::fs::create_dir(dir.join("subdir")).unwrap();
        // A `.md` directory entry must also be ignored (not a file).
        std::fs::create_dir(dir.join("dir.md")).unwrap();
        std::fs::write(
            dir.join("ok.md"),
            topic_md("ok", "project", "ok desc", "body"),
        )
        .unwrap();

        let loader = TopicLoader::new(dir.to_path_buf());
        let topics = loader.list().unwrap();
        let names: Vec<_> = topics.iter().map(|t| t.name.as_str()).collect();
        assert_eq!(names, vec!["ok"]);
    }

    #[test]
    fn list_skips_malformed_topic_file() {
        let tmp = TempDir::new().unwrap();
        let dir = tmp.path();
        // Malformed: no frontmatter delimiters at all.
        std::fs::write(dir.join("broken.md"), "no frontmatter here\n").unwrap();
        std::fs::write(
            dir.join("good.md"),
            topic_md("good", "reference", "good desc", "body"),
        )
        .unwrap();

        let loader = TopicLoader::new(dir.to_path_buf());
        let topics = loader.list().unwrap();
        let names: Vec<_> = topics.iter().map(|t| t.name.as_str()).collect();
        assert_eq!(names, vec!["good"]);
        assert_eq!(topics[0].kind, TopicKind::Reference);
    }

    #[test]
    fn list_uses_filename_when_frontmatter_name_mismatches() {
        let tmp = TempDir::new().unwrap();
        let dir = tmp.path();
        // Frontmatter name "wrong" but file stem is "actual-stem".
        std::fs::write(
            dir.join("actual-stem.md"),
            topic_md("wrong", "user", "desc", "body"),
        )
        .unwrap();

        let loader = TopicLoader::new(dir.to_path_buf());
        let topics = loader.list().unwrap();
        assert_eq!(topics.len(), 1);
        assert_eq!(topics[0].name, "actual-stem");
        assert_eq!(topics[0].description, "desc");
    }

    // --- read error paths ---------------------------------------------------

    #[test]
    fn read_rejects_invalid_slug() {
        let tmp = TempDir::new().unwrap();
        let loader = TopicLoader::new(tmp.path().to_path_buf());
        let err = loader.read("../escape").unwrap_err();
        assert!(matches!(err, MemoryError::InvalidSlug { .. }));
    }

    #[test]
    fn read_missing_file_is_io_error() {
        let tmp = TempDir::new().unwrap();
        let loader = TopicLoader::new(tmp.path().to_path_buf());
        let err = loader.read("nope").unwrap_err();
        assert!(matches!(err, MemoryError::Io { .. }));
    }

    // --- parse_frontmatter edge cases ---------------------------------------

    #[test]
    fn parse_frontmatter_strips_bom() {
        let raw = format!(
            "\u{feff}{}",
            topic_md("bom", "user", "with bom", "body line")
        );
        let path = Path::new("bom.md");
        let (fm, body) = parse_frontmatter(&raw, path).unwrap();
        assert_eq!(fm.name, "bom");
        assert!(body.contains("body line"));
    }

    #[test]
    fn parse_frontmatter_rejects_missing_leading_delimiter() {
        let raw = "name: x\ndescription: y\n---\nbody\n";
        let err = parse_frontmatter(raw, Path::new("x.md")).unwrap_err();
        match err {
            MemoryError::InvalidTopic { reason, .. } => assert!(reason.contains("leading")),
            other => panic!("unexpected: {other:?}"),
        }
    }

    #[test]
    fn parse_frontmatter_rejects_missing_closing_delimiter() {
        let raw = "---\nname: x\ndescription: y\nno closing here\n";
        let err = parse_frontmatter(raw, Path::new("x.md")).unwrap_err();
        match err {
            MemoryError::InvalidTopic { reason, .. } => assert!(reason.contains("closing")),
            other => panic!("unexpected: {other:?}"),
        }
    }

    #[test]
    fn parse_frontmatter_accepts_closing_delimiter_at_eof_without_body() {
        // Closing `\n---` with no trailing newline and no body.
        let raw = "---\nname: eof\ndescription: d\nmetadata:\n  type: user\n---";
        let (fm, body) = parse_frontmatter(raw, Path::new("eof.md")).unwrap();
        assert_eq!(fm.name, "eof");
        assert_eq!(body, "");
    }

    #[test]
    fn parse_frontmatter_rejects_empty_name() {
        let raw = "---\nname: \"  \"\ndescription: d\n---\nbody\n";
        let err = parse_frontmatter(raw, Path::new("x.md")).unwrap_err();
        match err {
            MemoryError::InvalidTopic { reason, .. } => assert!(reason.contains("name")),
            other => panic!("unexpected: {other:?}"),
        }
    }

    #[test]
    fn parse_frontmatter_rejects_empty_description() {
        let raw = "---\nname: x\ndescription: \"  \"\n---\nbody\n";
        let err = parse_frontmatter(raw, Path::new("x.md")).unwrap_err();
        match err {
            MemoryError::InvalidTopic { reason, .. } => assert!(reason.contains("description")),
            other => panic!("unexpected: {other:?}"),
        }
    }

    #[test]
    fn parse_frontmatter_rejects_invalid_yaml() {
        let raw = "---\nname: [unbalanced\ndescription: d\n---\nbody\n";
        let err = parse_frontmatter(raw, Path::new("x.md")).unwrap_err();
        match err {
            MemoryError::InvalidTopic { reason, .. } => assert!(reason.contains("yaml")),
            other => panic!("unexpected: {other:?}"),
        }
    }

    // --- render_topic_file / escape_yaml_string -----------------------------

    #[test]
    fn render_topic_file_appends_trailing_newline_when_missing() {
        let draft = TopicDraft {
            name: "no-nl".to_string(),
            description: "desc".to_string(),
            kind: TopicKind::Project,
            body: "body without newline".to_string(),
        };
        let rendered = render_topic_file(&draft);
        assert!(rendered.ends_with("body without newline\n"));
        assert!(rendered.contains("type: project"));
    }

    #[test]
    fn render_topic_file_preserves_single_trailing_newline() {
        let draft = TopicDraft {
            name: "has-nl".to_string(),
            description: "desc".to_string(),
            kind: TopicKind::User,
            body: "body\n".to_string(),
        };
        let rendered = render_topic_file(&draft);
        // Exactly one trailing newline (no double newline appended).
        assert!(rendered.ends_with("body\n"));
        assert!(!rendered.ends_with("body\n\n"));
    }

    #[test]
    fn escape_yaml_string_escapes_special_chars() {
        assert_eq!(escape_yaml_string("a\"b"), "a\\\"b");
        assert_eq!(escape_yaml_string("a\\b"), "a\\\\b");
        assert_eq!(escape_yaml_string("a\nb"), "a\\nb");
        assert_eq!(escape_yaml_string("a\rb"), "a\\rb");
        assert_eq!(escape_yaml_string("plain"), "plain");
    }

    #[test]
    fn write_then_read_round_trips_description_with_quotes() {
        let tmp = TempDir::new().unwrap();
        let dir = tmp.path();
        let loader = TopicLoader::new(dir.to_path_buf());
        loader
            .write(&TopicDraft {
                name: "quoted".to_string(),
                description: "use \"smart\" quotes \\ backslash".to_string(),
                kind: TopicKind::Reference,
                body: "body".to_string(),
            })
            .unwrap();
        let topic = loader.read("quoted").unwrap();
        assert_eq!(topic.description, "use \"smart\" quotes \\ backslash");
        assert_eq!(topic.kind, TopicKind::Reference);
    }

    // --- index handling -----------------------------------------------------

    #[test]
    fn write_creates_index_with_header_when_none_exists() {
        let tmp = TempDir::new().unwrap();
        let dir = tmp.path();
        let loader = TopicLoader::new(dir.to_path_buf());
        loader
            .write(&TopicDraft {
                name: "first".to_string(),
                description: "first desc".to_string(),
                kind: TopicKind::User,
                body: "body".to_string(),
            })
            .unwrap();
        let index = std::fs::read_to_string(dir.join("MEMORY.md")).unwrap();
        assert!(index.starts_with("# Memory index\n\n"));
        assert!(index.contains("[first](first.md)"));
    }

    #[test]
    fn write_appends_after_last_bullet_line() {
        let tmp = TempDir::new().unwrap();
        let dir = tmp.path();
        std::fs::write(
            dir.join("MEMORY.md"),
            "# Memory index\n\n- [aaa](aaa.md) — user: a\n\nTrailing prose paragraph.\n",
        )
        .unwrap();
        let loader = TopicLoader::new(dir.to_path_buf());
        loader
            .write(&TopicDraft {
                name: "bbb".to_string(),
                description: "b desc".to_string(),
                kind: TopicKind::User,
                body: "body".to_string(),
            })
            .unwrap();
        let index = std::fs::read_to_string(dir.join("MEMORY.md")).unwrap();
        let lines: Vec<&str> = index.lines().collect();
        let aaa_idx = lines.iter().position(|l| l.contains("aaa.md")).unwrap();
        let bbb_idx = lines.iter().position(|l| l.contains("bbb.md")).unwrap();
        let prose_idx = lines
            .iter()
            .position(|l| l.contains("Trailing prose"))
            .unwrap();
        // New bullet inserted right after the last existing bullet, before prose.
        assert_eq!(bbb_idx, aaa_idx + 1);
        assert!(bbb_idx < prose_idx);
    }

    #[test]
    fn rewrite_with_index_line_appends_at_eof_when_no_bullets() {
        let out = rewrite_with_index_line(
            "# Memory index\n\nSome prose.\n",
            "x",
            "- [x](x.md) — user: d",
        );
        assert!(out.contains("Some prose."));
        assert!(out.trim_end().ends_with("- [x](x.md) — user: d"));
        assert!(out.ends_with('\n'));
    }

    #[test]
    fn rewrite_with_index_line_adds_trailing_newline_when_existing_lacks_one() {
        // existing does not end with '\n' and is non-empty.
        let out =
            rewrite_with_index_line("- [x](x.md) — user: old", "x", "- [x](x.md) — user: new");
        assert!(out.contains("new"));
        assert!(!out.contains("old"));
        assert!(out.ends_with('\n'));
    }

    #[test]
    fn remove_index_line_on_missing_index_is_ok() {
        let tmp = TempDir::new().unwrap();
        let dir = tmp.path();
        // No MEMORY.md exists; remove_index_line must succeed silently.
        remove_index_line(dir, "ghost").unwrap();
        assert!(!dir.join("MEMORY.md").exists());
    }

    #[test]
    fn remove_index_line_preserves_other_entries_and_trailing_newline() {
        let tmp = TempDir::new().unwrap();
        let dir = tmp.path();
        std::fs::write(
            dir.join("MEMORY.md"),
            "# Memory index\n\n- [keep](keep.md) — user: k\n- [drop](drop.md) — user: d\n",
        )
        .unwrap();
        remove_index_line(dir, "drop").unwrap();
        let index = std::fs::read_to_string(dir.join("MEMORY.md")).unwrap();
        assert!(index.contains("[keep](keep.md)"));
        assert!(!index.contains("drop.md"));
        assert!(index.ends_with('\n'));
    }

    // --- delete edge cases --------------------------------------------------

    #[test]
    fn delete_rejects_invalid_slug() {
        let tmp = TempDir::new().unwrap();
        let loader = TopicLoader::new(tmp.path().to_path_buf());
        let err = loader.delete("a/b").unwrap_err();
        assert!(matches!(err, MemoryError::InvalidSlug { .. }));
    }

    #[test]
    fn delete_missing_topic_is_idempotent() {
        let tmp = TempDir::new().unwrap();
        let loader = TopicLoader::new(tmp.path().to_path_buf());
        // Deleting a topic that was never written succeeds.
        loader.delete("never-existed").unwrap();
    }

    // --- validate_slug NUL --------------------------------------------------

    #[test]
    fn validate_slug_rejects_nul() {
        let err = validate_slug("a\0b").unwrap_err();
        match err {
            MemoryError::InvalidSlug { reason, .. } => assert!(reason.contains("NUL")),
            other => panic!("unexpected: {other:?}"),
        }
    }

    // --- strip_html_comments edge cases -------------------------------------

    #[test]
    fn strip_html_comments_drops_unterminated_comment_tail() {
        let input = "keep me <!-- never closed";
        let out = strip_html_comments(input);
        assert_eq!(out, "keep me ");
    }

    #[test]
    fn strip_html_comments_no_comment_is_identity() {
        let input = "plain text with < and > but no comment";
        assert_eq!(strip_html_comments(input), input);
    }
}