mars-schema 0.25.0

Pure schema reader and editor for Mars agent packages
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
use indexmap::{IndexMap, IndexSet};
use serde_yaml::{Mapping, Value};

/// Parsed markdown frontmatter and body.
#[derive(Debug, Clone)]
pub struct Frontmatter {
    yaml: Mapping,
    body: String,
    has_frontmatter: bool,
    has_bom: bool,
}

/// Structured agent skill references.
///
/// A flat `skills: [a, b]` list is represented as all `load` entries for
/// backward compatibility. A structured `skills: { load: [...], available: [...] }`
/// keeps the two launch-bundle channels separate.
#[derive(Debug, Clone, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct SkillsSpec {
    pub load: Vec<String>,
    pub available: Vec<String>,
}

impl SkillsSpec {
    pub fn all(&self) -> Vec<String> {
        self.load
            .iter()
            .chain(self.available.iter())
            .cloned()
            .collect()
    }
}

/// The most frontmatter bytes a file may carry. Parsing time grows with the square of the
/// nesting depth the text can hold, so an unbounded block is a way to stall a server that reads
/// untrusted files; real frontmatter is a few hundred bytes.
pub const MAX_FRONTMATTER_BYTES: usize = 64 * 1024;

/// The deepest run of `[` and `{` the frontmatter may open, counted over the raw text. The
/// YAML scanners take time quadratic in that depth, so a size cap alone still lets 64 KiB of `[`
/// stall a parse for seconds; real frontmatter nests a few levels.
pub const MAX_FLOW_DEPTH: usize = 64;

/// Errors from frontmatter parsing.
#[derive(Debug, thiserror::Error)]
pub enum FrontmatterError {
    #[error("frontmatter is {0} bytes; the limit is {MAX_FRONTMATTER_BYTES}")]
    TooLarge(usize),

    #[error("frontmatter nests `[` or `{{` more than {MAX_FLOW_DEPTH} deep")]
    TooDeep,

    #[error("malformed YAML frontmatter: {0}")]
    MalformedYaml(#[from] serde_yaml::Error),

    #[error("frontmatter is not closed with an exact `---` line")]
    Unclosed,

    #[error("YAML anchors and aliases are not supported")]
    Anchor,

    #[error("YAML document-end markers are not supported")]
    DocumentEnd,

    #[error(
        "YAML syntax accepted by the semantic parser is unsupported by the structural scanner: {0}"
    )]
    UnsupportedSyntax(String),

    #[error("frontmatter is not a YAML mapping")]
    NotAMapping,
}

/// Parse markdown content into frontmatter and body.
pub fn parse(content: &str) -> Result<Frontmatter, FrontmatterError> {
    Frontmatter::parse(content)
}

impl Frontmatter {
    /// Parse a markdown document into frontmatter + body.
    pub fn parse(content: &str) -> Result<Self, FrontmatterError> {
        let (frontmatter, mut structural_errors) = Self::parse_preserving_structure(content)?;
        if structural_errors.is_empty() {
            Ok(frontmatter)
        } else {
            Err(structural_errors.remove(0))
        }
    }

    /// Parse semantic content while returning every structural refusal found.
    ///
    /// Schema readers use this to avoid losing an anchor diagnostic when the
    /// structural parser later encounters syntax it cannot scan.
    pub(crate) fn parse_preserving_structure(
        content: &str,
    ) -> Result<(Self, Vec<FrontmatterError>), FrontmatterError> {
        let Some(regions) = split_regions(content)? else {
            return Ok((
                Self {
                    yaml: Mapping::new(),
                    body: content.to_string(),
                    has_frontmatter: false,
                    has_bom: false,
                },
                Vec::new(),
            ));
        };

        let yaml_text = regions.yaml;
        if yaml_text.trim().is_empty() {
            return Ok((
                Self {
                    yaml: Mapping::new(),
                    body: regions.body.to_string(),
                    has_frontmatter: true,
                    has_bom: regions.has_bom,
                },
                Vec::new(),
            ));
        }

        let value: Value = serde_yaml::from_str(yaml_text)?;
        let structural_errors = validate_yaml_structure(yaml_text);
        let yaml = match value {
            Value::Mapping(mapping) => mapping,
            Value::Null => Mapping::new(),
            _ => return Err(FrontmatterError::NotAMapping),
        };

        Ok((
            Self {
                yaml,
                body: regions.body.to_string(),
                has_frontmatter: true,
                has_bom: regions.has_bom,
            },
            structural_errors,
        ))
    }

    /// Read all referenced skills as a flat list (`load` followed by `available`).
    pub fn skills(&self) -> Vec<String> {
        self.skills_structured().all()
    }

    /// Read `skills` in structured load/available form.
    pub fn skills_structured(&self) -> SkillsSpec {
        match self.get("skills") {
            Some(Value::Mapping(mapping)) => SkillsSpec {
                load: mapping
                    .get(yaml_key("load"))
                    .map(yaml_str_list)
                    .unwrap_or_default(),
                available: mapping
                    .get(yaml_key("available"))
                    .map(yaml_str_list)
                    .unwrap_or_default(),
            },
            Some(value) => SkillsSpec {
                load: yaml_str_list(value),
                available: Vec::new(),
            },
            None => SkillsSpec::default(),
        }
    }

    /// Read the `name` field if present.
    pub fn name(&self) -> Option<&str> {
        self.get("name").and_then(Value::as_str)
    }

    /// Read any YAML field by key.
    pub fn get(&self, key: &str) -> Option<&Value> {
        self.yaml.get(yaml_key(key))
    }

    /// Mutably read a YAML field by key.
    pub fn get_mut(&mut self, key: &str) -> Option<&mut Value> {
        self.yaml.get_mut(yaml_key(key))
    }

    /// Markdown body after frontmatter.
    pub fn body(&self) -> &str {
        &self.body
    }

    /// Replace the Markdown body while preserving the parsed frontmatter.
    pub fn set_body(&mut self, body: String) {
        self.body = body;
    }

    /// Whether this document contains frontmatter delimiters.
    pub fn has_frontmatter(&self) -> bool {
        self.has_frontmatter
    }

    /// All frontmatter keys as strings.
    pub fn keys(&self) -> Vec<String> {
        self.yaml
            .keys()
            .filter_map(|k| k.as_str().map(str::to_owned))
            .collect()
    }

    /// Parsed top-level YAML, for schema transforms that must verify an edited source.
    pub(crate) fn mapping(&self) -> &Mapping {
        &self.yaml
    }

    /// Insert or replace a top-level frontmatter field.
    pub fn insert(&mut self, key: &str, value: Value) {
        self.has_frontmatter = true;
        self.yaml.insert(yaml_key(key), value);
    }

    /// Remove a top-level frontmatter field.
    pub fn remove(&mut self, key: &str) -> Option<Value> {
        self.yaml.shift_remove(yaml_key(key))
    }

    /// Serialize back to full markdown.
    pub fn render(&self) -> String {
        if !self.has_frontmatter && self.yaml.is_empty() {
            return self.body.clone();
        }
        let mut out = self.render_frontmatter();
        out.push_str(&self.body);
        out
    }

    /// Serialize only the frontmatter block, delimiters included.
    ///
    /// A caller that changes the frontmatter and must keep the authored body byte for byte
    /// (`mars migrate`'s full rewrite) renders this and appends the parsed body itself.
    pub(crate) fn render_frontmatter(&self) -> String {
        if !self.has_frontmatter && self.yaml.is_empty() {
            return String::new();
        }

        let mut out = if self.has_bom {
            String::from("\u{feff}---\n")
        } else {
            String::from("---\n")
        };
        if !self.yaml.is_empty() {
            let mut yaml = serde_yaml::to_string(&self.yaml)
                .expect("serializing frontmatter mapping should succeed");
            if let Some(stripped) = yaml.strip_prefix("---\n") {
                yaml = stripped.to_string();
            }
            out.push_str(&yaml);
            if !yaml.ends_with('\n') {
                out.push('\n');
            }
        }
        out.push_str("---\n");
        out
    }
}

/// Rename skills in frontmatter using exact-match replacement.
fn rewrite_skills(fm: &mut Frontmatter, renames: &IndexMap<String, String>) -> IndexSet<String> {
    let mut renamed = IndexSet::new();
    let key = yaml_key("skills");
    if let Some(value) = fm.yaml.get_mut(&key) {
        rewrite_skill_value(value, renames, &mut renamed);
    }

    renamed
}

/// Rename subagents in frontmatter using exact-match replacement.
fn rewrite_subagents(fm: &mut Frontmatter, renames: &IndexMap<String, String>) -> IndexSet<String> {
    rewrite_string_list_field(fm, "subagents", renames)
}

/// Parse content, rewrite skills, and render updated content if changed.
pub fn rewrite_content_skills(
    content: &str,
    renames: &IndexMap<String, String>,
) -> Result<Option<String>, FrontmatterError> {
    let mut fm = Frontmatter::parse(content)?;
    let renamed = rewrite_skills(&mut fm, renames);
    if renamed.is_empty() {
        Ok(None)
    } else {
        Ok(Some(fm.render()))
    }
}

/// Parse content, rewrite subagents, and render updated content if changed.
pub fn rewrite_content_subagents(
    content: &str,
    renames: &IndexMap<String, String>,
) -> Result<Option<String>, FrontmatterError> {
    let mut fm = Frontmatter::parse(content)?;
    let renamed = rewrite_subagents(&mut fm, renames);
    if renamed.is_empty() {
        Ok(None)
    } else {
        Ok(Some(fm.render()))
    }
}

fn rewrite_string_list_field(
    fm: &mut Frontmatter,
    field_name: &str,
    renames: &IndexMap<String, String>,
) -> IndexSet<String> {
    let mut renamed = IndexSet::new();
    let key = yaml_key(field_name);
    if let Some(value) = fm.yaml.get_mut(&key) {
        rewrite_string_list_value(value, renames, &mut renamed);
    }

    renamed
}

fn yaml_str_list(val: &Value) -> Vec<String> {
    match val {
        Value::Sequence(seq) => seq
            .iter()
            .filter_map(Value::as_str)
            .map(str::to_owned)
            .collect(),
        Value::String(s) => vec![s.clone()],
        _ => vec![],
    }
}

fn rewrite_skill_value(
    value: &mut Value,
    renames: &IndexMap<String, String>,
    renamed: &mut IndexSet<String>,
) {
    match value {
        Value::Sequence(_) | Value::String(_) => rewrite_string_list_value(value, renames, renamed),
        Value::Mapping(mapping) => {
            for field in ["load", "available"] {
                if let Some(child) = mapping.get_mut(yaml_key(field)) {
                    rewrite_skill_value(child, renames, renamed);
                }
            }
        }
        _ => {}
    }
}

fn rewrite_string_list_value(
    value: &mut Value,
    renames: &IndexMap<String, String>,
    renamed: &mut IndexSet<String>,
) {
    match value {
        Value::Sequence(seq) => {
            for item in seq {
                let Some(name) = item.as_str() else {
                    continue;
                };
                if let Some(new_name) = renames.get(name) {
                    renamed.insert(name.to_string());
                    *item = Value::String(new_name.clone());
                }
            }
        }
        Value::String(name) => {
            if let Some(new_name) = renames.get(name.as_str()) {
                renamed.insert(name.clone());
                *name = new_name.clone();
            }
        }
        _ => {}
    }
}

fn exceeds_flow_depth(yaml: &str) -> bool {
    let mut depth = 0usize;
    for byte in yaml.bytes() {
        match byte {
            b'[' | b'{' => {
                depth += 1;
                if depth > MAX_FLOW_DEPTH {
                    return true;
                }
            }
            b']' | b'}' => depth = depth.saturating_sub(1),
            _ => {}
        }
    }
    false
}

/// A document's frontmatter region: the YAML between the fences, where it starts in the
/// document, and the Markdown after the closing fence.
struct Regions<'a> {
    has_bom: bool,
    /// Byte offset of `yaml`: past a byte-order mark and the opening fence line.
    yaml_start: usize,
    yaml: &'a str,
    body: &'a str,
}

/// The frontmatter region of `content`, and `None` when it has no frontmatter.
fn split_regions(content: &str) -> Result<Option<Regions<'_>>, FrontmatterError> {
    let (has_bom, document) = content
        .strip_prefix('\u{feff}')
        .map_or((false, content), |document| (true, document));
    let (first_line, after_first_line) = split_first_line(document);
    if !is_delimiter_line(first_line) {
        return Ok(None);
    }

    let mut yaml_end = None;
    let mut offset = 0usize;
    for line in after_first_line.split_inclusive('\n') {
        if is_delimiter_line(line) {
            yaml_end = Some((offset, line.len()));
            break;
        }
        offset += line.len();
    }

    let Some((yaml_len, closing_len)) = yaml_end else {
        return Err(FrontmatterError::Unclosed);
    };

    if yaml_len > MAX_FRONTMATTER_BYTES {
        return Err(FrontmatterError::TooLarge(yaml_len));
    }
    let yaml = &after_first_line[..yaml_len];
    if exceeds_flow_depth(yaml) {
        return Err(FrontmatterError::TooDeep);
    }
    Ok(Some(Regions {
        has_bom,
        yaml_start: content.len() - after_first_line.len(),
        yaml,
        body: &after_first_line[yaml_len + closing_len..],
    }))
}

/// The YAML text between a document's frontmatter fences, and where it starts in the document.
pub(crate) fn yaml_text(content: &str) -> Result<Option<(&str, usize)>, FrontmatterError> {
    Ok(split_regions(content)?.map(|regions| (regions.yaml, regions.yaml_start)))
}

fn split_first_line(content: &str) -> (&str, &str) {
    match content.split_once('\n') {
        Some((first, rest)) => (first, rest),
        None => (content, ""),
    }
}

fn is_delimiter_line(line: &str) -> bool {
    line.strip_suffix('\n')
        .unwrap_or(line)
        .strip_suffix('\r')
        .unwrap_or(line.strip_suffix('\n').unwrap_or(line))
        == "---"
}

fn validate_yaml_structure(text: &str) -> Vec<FrontmatterError> {
    use yaml_rust2::parser::{Event, MarkedEventReceiver, Parser};
    use yaml_rust2::scanner::Marker;

    #[derive(Default)]
    struct StructuralEvents {
        anchor_or_alias: bool,
        document_end: bool,
        source: String,
        char_to_byte: Vec<usize>,
    }
    impl MarkedEventReceiver for StructuralEvents {
        fn on_event(&mut self, event: Event, marker: Marker) {
            match event {
                Event::Alias(_) | Event::Scalar(_, _, 1.., _) => self.anchor_or_alias = true,
                Event::SequenceStart(1.., _) | Event::MappingStart(1.., _) => {
                    self.anchor_or_alias = true;
                }
                Event::DocumentEnd => {
                    let byte = self.char_to_byte.get(marker.index()).copied();
                    if byte.is_some_and(|byte| self.source[byte..].starts_with("...")) {
                        self.document_end = true;
                    }
                }
                _ => {}
            }
        }
    }

    let mut char_to_byte: Vec<usize> = text.char_indices().map(|(byte, _)| byte).collect();
    char_to_byte.push(text.len());
    let mut events = StructuralEvents {
        source: text.to_owned(),
        char_to_byte,
        ..StructuralEvents::default()
    };
    // serde_yaml already accepted the document. The event parser is used only
    // to distinguish YAML structure from identical text inside scalar values.
    let parse_error = Parser::new_from_str(text).load(&mut events, true).err();
    let mut errors = Vec::new();
    if events.anchor_or_alias {
        errors.push(FrontmatterError::Anchor);
    }
    if events.document_end {
        errors.push(FrontmatterError::DocumentEnd);
    }
    if let Some(error) = parse_error {
        errors.push(FrontmatterError::UnsupportedSyntax(error.to_string()));
    }
    errors
}

fn yaml_key(key: &str) -> Value {
    Value::String(key.to_string())
}

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

    #[test]
    fn parse_and_render_roundtrip() {
        let input = "---\nname: coder\nskills:\n- plan\n- review\n---\n# Body\ntext";
        let fm = Frontmatter::parse(input).unwrap();
        assert_eq!(fm.name(), Some("coder"));
        assert_eq!(fm.skills(), vec!["plan", "review"]);
        assert_eq!(fm.body(), "# Body\ntext");
        assert!(fm.has_frontmatter());

        let rendered = fm.render();
        let reparsed = Frontmatter::parse(&rendered).unwrap();
        assert_eq!(reparsed.name(), Some("coder"));
        assert_eq!(reparsed.skills(), vec!["plan", "review"]);
        assert_eq!(reparsed.body(), "# Body\ntext");
    }

    #[test]
    fn parse_without_frontmatter_keeps_body() {
        let input = "# Markdown only\ntext";
        let fm = parse(input).unwrap();
        assert!(!fm.has_frontmatter());
        assert!(fm.skills().is_empty());
        assert_eq!(fm.body(), input);
        assert_eq!(fm.render(), input);
    }

    #[test]
    fn parse_empty_frontmatter_roundtrips_delimiters() {
        let input = "---\n---\nbody";
        let fm = Frontmatter::parse(input).unwrap();
        assert!(fm.has_frontmatter());
        assert!(fm.skills().is_empty());
        assert_eq!(fm.body(), "body");
        assert_eq!(fm.render(), input);
    }

    #[test]
    fn parse_malformed_yaml_errors() {
        let input = "---\ninvalid: [:\n---\nbody";
        assert!(matches!(
            Frontmatter::parse(input),
            Err(FrontmatterError::MalformedYaml(_))
        ));
    }

    #[test]
    fn parse_flow_style_skills() {
        let input = "---\nskills: [plan, review]\n---\nbody";
        let fm = Frontmatter::parse(input).unwrap();
        assert_eq!(fm.skills(), vec!["plan", "review"]);
        assert_eq!(fm.skills_structured().load, vec!["plan", "review"]);
        assert!(fm.skills_structured().available.is_empty());
    }

    #[test]
    fn parse_structured_skills() {
        let input = "---\nskills:\n  load: [principles]\n  available:\n    - planning\n    - spawn\n---\nbody";
        let fm = Frontmatter::parse(input).unwrap();
        assert_eq!(fm.skills(), vec!["principles", "planning", "spawn"]);
        assert_eq!(fm.skills_structured().load, vec!["principles"]);
        assert_eq!(fm.skills_structured().available, vec!["planning", "spawn"]);
    }

    #[test]
    fn rewrite_structured_skills_preserves_split() {
        let input = "---\nskills:\n  load: [plan]\n  available: [review]\n---\nbody\n";
        let renames = IndexMap::from([
            ("plan".to_string(), "plan__org".to_string()),
            ("review".to_string(), "review__org".to_string()),
        ]);

        let rewritten = rewrite_content_skills(input, &renames).unwrap().unwrap();
        let fm = Frontmatter::parse(&rewritten).unwrap();
        assert_eq!(fm.skills_structured().load, vec!["plan__org"]);
        assert_eq!(fm.skills_structured().available, vec!["review__org"]);
    }

    #[test]
    fn rewrite_does_not_corrupt_substrings() {
        let input = "---\nskills:\n- plan\n- planner\n- planning-extended\n---\nbody\n";
        let renames = IndexMap::from([(
            "plan".to_string(),
            "plan__meridian-flow_meridian-base".to_string(),
        )]);

        let rewritten = rewrite_content_skills(input, &renames).unwrap().unwrap();
        let fm = Frontmatter::parse(&rewritten).unwrap();
        assert_eq!(
            fm.skills(),
            vec![
                "plan__meridian-flow_meridian-base",
                "planner",
                "planning-extended"
            ]
        );
    }

    #[test]
    fn rewrite_subagents_uses_exact_matches() {
        let input = "---\nsubagents:\n- web-researcher\n- web\n---\nbody\n";
        let renames = IndexMap::from([(
            "web-researcher".to_string(),
            "web-researcher__source-a".to_string(),
        )]);

        let rewritten = rewrite_content_subagents(input, &renames).unwrap().unwrap();
        let fm = Frontmatter::parse(&rewritten).unwrap();
        assert_eq!(
            fm.get("subagents").map(yaml_str_list).unwrap(),
            vec!["web-researcher__source-a", "web"]
        );
    }
}