retro-core 3.1.0

Core library for retro, the active context curator for AI coding agents
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
//! Knowledge node: the atomic unit of the v3 store.
//! One node == one markdown file with strict frontmatter.

use chrono::NaiveDate;

use crate::errors::CoreError;

/// Ids and project slugs must be safe path segments: lowercase ASCII
/// alphanumerics and dashes, starting alphanumeric (the slugify charset).
/// Public so surfaces (dashboard API, CLI) can reject untrusted ids
/// before they reach any path construction.
pub fn is_valid_slug(s: &str) -> bool {
    let mut chars = s.chars();
    match chars.next() {
        Some(c) if c.is_ascii_lowercase() || c.is_ascii_digit() => {}
        _ => return false,
    }
    s.chars()
        .all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '-')
}

/// Node type. v3 collapses v2's six types to four
/// (`directive` → `rule`, `skill` → `pattern`, handled at migration).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NodeType {
    Rule,
    Preference,
    Pattern,
    /// Context-only: stored and browsable, never projected.
    Memory,
}

impl NodeType {
    pub fn as_str(&self) -> &'static str {
        match self {
            NodeType::Rule => "rule",
            NodeType::Preference => "preference",
            NodeType::Pattern => "pattern",
            NodeType::Memory => "memory",
        }
    }

    pub fn parse(s: &str) -> Result<Self, CoreError> {
        match s {
            "rule" => Ok(NodeType::Rule),
            "preference" => Ok(NodeType::Preference),
            "pattern" => Ok(NodeType::Pattern),
            "memory" => Ok(NodeType::Memory),
            other => Err(CoreError::Parse(format!("unknown node type: {other:?}"))),
        }
    }
}

/// Where a node applies: everywhere, or one project (by slug).
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Scope {
    Global,
    Project(String),
}

impl std::fmt::Display for Scope {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Scope::Global => write!(f, "global"),
            Scope::Project(slug) => write!(f, "project/{slug}"),
        }
    }
}

impl Scope {
    pub fn parse(s: &str) -> Result<Self, CoreError> {
        if s == "global" {
            return Ok(Scope::Global);
        }
        if let Some(slug) = s.strip_prefix("project/") {
            if is_valid_slug(slug) {
                return Ok(Scope::Project(slug.to_string()));
            }
        }
        Err(CoreError::Parse(format!("invalid scope: {s:?}")))
    }
}

/// One knowledge node. Serialized as one markdown file:
/// strict frontmatter between `---` delimiters, then the body.
/// The body is stored WITHOUT a trailing newline; `to_markdown`
/// appends exactly one (normalization keeps round-trips stable).
#[derive(Debug, Clone, PartialEq)]
pub struct Node {
    pub id: String,
    pub scope: Scope,
    pub node_type: NodeType,
    pub confidence: f64,
    pub sources: Vec<String>,
    pub created: NaiveDate,
    pub updated: NaiveDate,
    pub invalidated_by: Option<String>,
    pub body: String,
}

impl Node {
    pub fn is_active(&self) -> bool {
        self.invalidated_by.is_none()
    }

    pub fn to_markdown(&self) -> String {
        // NOTE: source IDs must not contain commas (comma-joined list format).
        let sources = self.sources.join(", ");
        let invalidated = self.invalidated_by.as_deref().unwrap_or("null");
        format!(
            "---\nid: {}\nscope: {}\ntype: {}\nconfidence: {:.2}\nsources: [{}]\ncreated: {}\nupdated: {}\ninvalidated_by: {}\n---\n{}\n",
            self.id,
            self.scope,
            self.node_type.as_str(),
            self.confidence,
            sources,
            self.created,
            self.updated,
            invalidated,
            self.body.trim_end_matches('\n'),
        )
    }

    /// Parse a node from markdown with strict frontmatter.
    /// Unknown keys are an error (catches human typos); the fixed
    /// schema is owned by this binary — migration controls format changes.
    /// Parsing normalizes on rewrite: CRLF becomes LF, confidence is written back with two decimals.
    pub fn from_markdown(content: &str) -> Result<Node, CoreError> {
        let content = content.replace("\r\n", "\n");
        let rest = content
            .strip_prefix("---\n")
            .ok_or_else(|| CoreError::Parse("missing frontmatter open delimiter".to_string()))?;
        let (front, body) = rest
            .split_once("\n---\n")
            .ok_or_else(|| CoreError::Parse("missing frontmatter close delimiter".to_string()))?;

        let mut id: Option<String> = None;
        let mut scope: Option<Scope> = None;
        let mut node_type: Option<NodeType> = None;
        let mut confidence: Option<f64> = None;
        let mut sources: Vec<String> = Vec::new();
        let mut created: Option<NaiveDate> = None;
        let mut updated: Option<NaiveDate> = None;
        let mut invalidated_by: Option<String> = None;
        let mut seen_keys: Vec<String> = Vec::new();

        for line in front.lines() {
            if line.trim().is_empty() {
                continue;
            }
            let (key, value) = line
                .split_once(':')
                .ok_or_else(|| CoreError::Parse(format!("invalid frontmatter line: {line:?}")))?;
            let key = key.trim();
            let value = value.trim();
            if seen_keys.iter().any(|k| k == key) {
                return Err(CoreError::Parse(format!(
                    "duplicate frontmatter key: {key:?}"
                )));
            }
            seen_keys.push(key.to_string());
            match key {
                "id" => id = Some(value.to_string()),
                "scope" => scope = Some(Scope::parse(value)?),
                "type" => node_type = Some(NodeType::parse(value)?),
                "confidence" => {
                    let c: f64 = value
                        .parse()
                        .map_err(|_| CoreError::Parse(format!("invalid confidence: {value:?}")))?;
                    if !c.is_finite() || !(0.0..=1.0).contains(&c) {
                        return Err(CoreError::Parse(format!(
                            "confidence out of range [0.0, 1.0]: {value:?}"
                        )));
                    }
                    confidence = Some(c);
                }
                "sources" => {
                    let inner = value
                        .strip_prefix('[')
                        .and_then(|v| v.strip_suffix(']'))
                        .ok_or_else(|| {
                            CoreError::Parse(format!("invalid sources list: {value:?}"))
                        })?;
                    sources = inner
                        .split(',')
                        .map(str::trim)
                        .filter(|s| !s.is_empty())
                        .map(String::from)
                        .collect();
                }
                "created" => created = Some(parse_date(value)?),
                "updated" => updated = Some(parse_date(value)?),
                "invalidated_by" => {
                    invalidated_by = match value {
                        "null" | "" => None,
                        other => Some(other.to_string()),
                    }
                }
                other => {
                    return Err(CoreError::Parse(format!(
                        "unknown frontmatter key: {other:?}"
                    )));
                }
            }
        }

        let missing = |k: &str| CoreError::Parse(format!("missing frontmatter key: {k}"));
        let id = id.ok_or_else(|| missing("id"))?;
        if !is_valid_slug(&id) {
            return Err(CoreError::Parse(format!(
                "invalid id (must be lowercase kebab-case): {id:?}"
            )));
        }
        Ok(Node {
            id,
            scope: scope.ok_or_else(|| missing("scope"))?,
            node_type: node_type.ok_or_else(|| missing("type"))?,
            confidence: confidence.ok_or_else(|| missing("confidence"))?,
            sources,
            created: created.ok_or_else(|| missing("created"))?,
            updated: updated.ok_or_else(|| missing("updated"))?,
            invalidated_by,
            body: body.trim_end_matches('\n').to_string(),
        })
    }
}

fn parse_date(s: &str) -> Result<NaiveDate, CoreError> {
    NaiveDate::parse_from_str(s, "%Y-%m-%d")
        .map_err(|_| CoreError::Parse(format!("invalid date: {s:?}")))
}

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

    fn sample_node() -> Node {
        Node {
            id: "ab-paired-observations".to_string(),
            scope: Scope::Project("my-api-service".to_string()),
            node_type: NodeType::Rule,
            confidence: 0.9,
            sources: vec![
                "session:1a2b3c4d".to_string(),
                "session:5e6f7a8b".to_string(),
            ],
            created: NaiveDate::from_ymd_opt(2026, 5, 19).unwrap(),
            updated: NaiveDate::from_ymd_opt(2026, 6, 2).unwrap(),
            invalidated_by: None,
            body: "A/B comparisons must always use paired observations.\n\n**Why:** Unpaired comparisons mix traffic distributions.".to_string(),
        }
    }

    #[test]
    fn scope_roundtrip() {
        assert_eq!(Scope::Global.to_string(), "global");
        assert_eq!(
            Scope::Project("my-api".to_string()).to_string(),
            "project/my-api"
        );
        assert_eq!(Scope::parse("global").unwrap(), Scope::Global);
        assert_eq!(
            Scope::parse("project/my-api").unwrap(),
            Scope::Project("my-api".to_string())
        );
        assert!(Scope::parse("team/my-api").is_err());
        assert!(Scope::parse("project/").is_err());
        assert!(Scope::parse("").is_err());
    }

    #[test]
    fn node_type_roundtrip() {
        for (t, s) in [
            (NodeType::Rule, "rule"),
            (NodeType::Preference, "preference"),
            (NodeType::Pattern, "pattern"),
            (NodeType::Memory, "memory"),
        ] {
            assert_eq!(t.as_str(), s);
            assert_eq!(NodeType::parse(s).unwrap(), t);
        }
        assert!(NodeType::parse("skill").is_err());
        assert!(NodeType::parse("").is_err());
    }

    #[test]
    fn is_active_reflects_invalidated_by() {
        let mut n = sample_node();
        assert!(n.is_active());
        n.invalidated_by = Some("newer-rule".to_string());
        assert!(!n.is_active());
    }

    #[test]
    fn to_markdown_emits_fixed_frontmatter_order() {
        let md = sample_node().to_markdown();
        let expected = "\
---
id: ab-paired-observations
scope: project/my-api-service
type: rule
confidence: 0.90
sources: [session:1a2b3c4d, session:5e6f7a8b]
created: 2026-05-19
updated: 2026-06-02
invalidated_by: null
---
A/B comparisons must always use paired observations.

**Why:** Unpaired comparisons mix traffic distributions.
";
        assert_eq!(md, expected);
    }

    #[test]
    fn to_markdown_empty_sources_and_invalidated() {
        let mut n = sample_node();
        n.sources = vec![];
        n.invalidated_by = Some("other-node".to_string());
        let md = n.to_markdown();
        assert!(md.contains("sources: []\n"));
        assert!(md.contains("invalidated_by: other-node\n"));
    }

    #[test]
    fn to_markdown_empty_body_single_trailing_newline() {
        let mut n = sample_node();
        n.body = String::new();
        assert!(n.to_markdown().ends_with("---\n\n"));
    }

    #[test]
    fn from_markdown_roundtrip() {
        let n = sample_node();
        let parsed = Node::from_markdown(&n.to_markdown()).unwrap();
        assert_eq!(parsed, n);
    }

    #[test]
    fn from_markdown_roundtrip_with_invalidated_and_empty_sources() {
        let mut n = sample_node();
        n.sources = vec![];
        n.invalidated_by = Some("other".to_string());
        let parsed = Node::from_markdown(&n.to_markdown()).unwrap();
        assert_eq!(parsed, n);
    }

    #[test]
    fn from_markdown_body_may_contain_dashes() {
        let mut n = sample_node();
        n.body = "line one\n---\nline after a dash rule".to_string();
        let parsed = Node::from_markdown(&n.to_markdown()).unwrap();
        assert_eq!(parsed.body, n.body);
    }

    #[test]
    fn from_markdown_missing_required_key_errors() {
        let md = "---\nid: x\nscope: global\ntype: rule\n---\nbody\n";
        let err = Node::from_markdown(md).unwrap_err();
        assert!(err.to_string().contains("confidence"), "got: {err}");
    }

    #[test]
    fn from_markdown_unknown_key_errors() {
        let md = sample_node().to_markdown().replace("updated:", "updatedd:");
        let err = Node::from_markdown(&md).unwrap_err();
        assert!(err.to_string().contains("updatedd"), "got: {err}");
    }

    #[test]
    fn from_markdown_bad_values_error() {
        let base = sample_node().to_markdown();
        for (needle, replacement) in [
            ("confidence: 0.90", "confidence: high"),
            ("created: 2026-05-19", "created: yesterday"),
            ("type: rule", "type: law"),
            ("scope: project/my-api-service", "scope: team/x"),
        ] {
            let md = base.replace(needle, replacement);
            assert!(
                Node::from_markdown(&md).is_err(),
                "should fail: {replacement}"
            );
        }
    }

    #[test]
    fn from_markdown_requires_frontmatter_delimiters() {
        assert!(Node::from_markdown("no frontmatter here").is_err());
        assert!(Node::from_markdown("---\nid: x\nno closing delimiter").is_err());
    }

    #[test]
    fn from_markdown_accepts_crlf_line_endings() {
        let crlf = sample_node().to_markdown().replace('\n', "\r\n");
        let parsed = Node::from_markdown(&crlf).unwrap();
        assert_eq!(parsed.id, "ab-paired-observations");
    }

    #[test]
    fn from_markdown_duplicate_key_errors() {
        let md = sample_node()
            .to_markdown()
            .replace("type: rule\n", "type: rule\ntype: pattern\n");
        let err = Node::from_markdown(&md).unwrap_err();
        assert!(err.to_string().contains("duplicate"), "got: {err}");
    }

    #[test]
    fn from_markdown_rejects_unsafe_id_and_scope() {
        let base = sample_node().to_markdown();
        for (needle, replacement) in [
            ("id: ab-paired-observations", "id: ../escape"),
            ("id: ab-paired-observations", "id: UPPER_case"),
            ("scope: project/my-api-service", "scope: project/../escape"),
            ("scope: project/my-api-service", "scope: project/has space"),
        ] {
            let md = base.replace(needle, replacement);
            assert!(
                Node::from_markdown(&md).is_err(),
                "should fail: {replacement}"
            );
        }
    }

    #[test]
    fn from_markdown_confidence_out_of_range_errors() {
        for bad in ["1.5", "-0.1", "NaN", "inf"] {
            let md = sample_node()
                .to_markdown()
                .replace("confidence: 0.90", &format!("confidence: {bad}"));
            assert!(Node::from_markdown(&md).is_err(), "should fail: {bad}");
        }
    }
}