mentra 0.30.0

An agent runtime for tool-using LLM applications
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
//! Markdown frontmatter: splitting a file at its `---` delimiters, and
//! reading the flat `key: value` block between them.
//!
//! The delimiter rules are markdown frontmatter's: a leading `---` line
//! opens, the next bare `---` line closes, and everything after it is the
//! body. Three conventions read this shape — `SKILL.md` here, prompt
//! templates and memory files in basis — and the scanner lives once so they
//! cannot drift on delimiters, byte-order marks, or line endings.
//!
//! What the block holds is read by [`parse`], which understands the YAML
//! those conventions actually use and nothing more: one `key: value` per
//! line, a value plain or quoted, `true`/`false`, blank lines and `#`
//! comments. A list, a nested mapping or a block scalar is refused with a
//! message that says so. Every field those conventions define is a string
//! or a boolean, so a full YAML parser bought nothing but an unmaintained
//! C-library port underneath it; what a writer needs is [`quote`], which
//! produces a value [`parse`] reads back exactly.
//!
//! What a field *means* — which keys, which are required, which file to
//! blame — stays with each caller.

use std::fmt;

/// A file split at its frontmatter delimiters.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Scanned<'a> {
    /// The raw block between the delimiters, or `None` when the file never
    /// opened frontmatter and is all body.
    pub frontmatter: Option<&'a str>,
    pub body: &'a str,
}

/// A file that opened frontmatter and never closed it — an error, not a file
/// whose body happens to start with `---`. Carries no path: the caller is the
/// one holding a file name to blame.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Unterminated;

impl fmt::Display for Unterminated {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("missing closing frontmatter delimiter")
    }
}

impl std::error::Error for Unterminated {}

/// Splits `raw` into its frontmatter and the body below it.
///
/// A file with no frontmatter is not an error here — it is all body. Whether
/// that is *usable* is the caller's question. Empty frontmatter (`---\n---`)
/// scans as `Some("")`, distinguishable from a file that never opened one.
pub fn scan(raw: &str) -> Result<Scanned<'_>, Unterminated> {
    // A byte-order mark would keep the `---` from starting the file, turning
    // frontmatter into prose and the failure into a confusing one.
    let raw = raw.strip_prefix('\u{feff}').unwrap_or(raw);

    let Some(opening) = raw
        .strip_prefix("---\r\n")
        .or_else(|| raw.strip_prefix("---\n"))
    else {
        return Ok(Scanned {
            frontmatter: None,
            body: raw,
        });
    };

    let mut cursor = 0usize;
    for segment in opening.split_inclusive('\n') {
        if segment.trim_end_matches(['\n', '\r']) == "---" {
            return Ok(Scanned {
                frontmatter: Some(&opening[..cursor]),
                body: &opening[cursor + segment.len()..],
            });
        }
        cursor += segment.len();
    }

    // The closing delimiter can also be the last line, with no newline after
    // it; then the body is empty rather than missing.
    if opening[cursor..].trim_end_matches('\r') == "---" {
        return Ok(Scanned {
            frontmatter: Some(&opening[..cursor]),
            body: "",
        });
    }

    Err(Unterminated)
}

/// One value in a frontmatter block.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Scalar {
    /// A string, plain or quoted.
    Text(String),
    /// A plain `true` or `false`.
    Bool(bool),
    /// `key:` with nothing after it, or a plain `null` / `~`.
    Null,
}

/// The fields of a frontmatter block, in file order.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Fields {
    entries: Vec<(String, Scalar)>,
}

impl Fields {
    /// The value under `key`, if the block has it.
    pub fn get(&self, key: &str) -> Option<&Scalar> {
        self.entries
            .iter()
            .find(|(known, _)| known == key)
            .map(|(_, value)| value)
    }

    /// The string under `key`: a text value as written, a boolean as
    /// `true`/`false`, `None` for an absent key or a null.
    pub fn text(&self, key: &str) -> Option<&str> {
        match self.get(key)? {
            Scalar::Text(text) => Some(text),
            Scalar::Bool(true) => Some("true"),
            Scalar::Bool(false) => Some("false"),
            Scalar::Null => None,
        }
    }

    /// The boolean under `key`, or `None` when absent or null. A text
    /// value spelled `true`/`false` (quoted, say) counts; anything else is
    /// an error naming the key.
    pub fn boolean(&self, key: &str) -> Result<Option<bool>, ParseError> {
        match self.get(key) {
            None | Some(Scalar::Null) => Ok(None),
            Some(Scalar::Bool(value)) => Ok(Some(*value)),
            Some(Scalar::Text(text)) => match text.as_str() {
                "true" => Ok(Some(true)),
                "false" => Ok(Some(false)),
                other => Err(ParseError {
                    line: 0,
                    message: format!("`{key}` must be true or false, not `{other}`"),
                }),
            },
        }
    }

    /// The first of `keys` the block has, for a field with two spellings.
    pub fn text_any<'a>(&'a self, keys: &[&str]) -> Option<&'a str> {
        keys.iter().find_map(|key| self.text(key))
    }

    /// The first of `keys` the block has, as a boolean.
    pub fn boolean_any(&self, keys: &[&str]) -> Result<Option<bool>, ParseError> {
        for key in keys {
            if let Some(value) = self.boolean(key)? {
                return Ok(Some(value));
            }
        }
        Ok(None)
    }

    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }

    /// Every key, in file order.
    pub fn keys(&self) -> impl Iterator<Item = &str> {
        self.entries.iter().map(|(key, _)| key.as_str())
    }
}

/// A block [`parse`] could not read, with the line it stopped at (1-based;
/// 0 when the fault is in a value rather than a line).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParseError {
    pub line: usize,
    pub message: String,
}

impl fmt::Display for ParseError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.line == 0 {
            f.write_str(&self.message)
        } else {
            write!(f, "line {}: {}", self.line, self.message)
        }
    }
}

impl std::error::Error for ParseError {}

/// Reads a frontmatter block: one `key: value` per line.
///
/// A key is letters, digits, `_` and `-`. A value is plain (trimmed, up to a
/// ` #` comment), double-quoted with `\"`, `\\`, `\n`, `\t`, `\r` and
/// `\uXXXX` escapes, or single-quoted with `''` for a quote. A plain `true`
/// or `false` is a boolean; a plain `null` or `~`, or nothing, is null.
/// Blank lines and `#` comment lines are skipped. A duplicate key, a list
/// item, an indented continuation or a nested mapping is an error that says
/// what it found.
pub fn parse(block: &str) -> Result<Fields, ParseError> {
    let mut fields = Fields::default();
    for (index, raw) in block.lines().enumerate() {
        let number = index + 1;
        let line = raw.trim_end_matches('\r');
        let trimmed = line.trim();
        if trimmed.is_empty() || trimmed.starts_with('#') {
            continue;
        }
        if line.starts_with([' ', '\t']) {
            return Err(ParseError {
                line: number,
                message: "an indented line; frontmatter here is one `key: value` per line, \
                          with no nesting or continuation"
                    .to_string(),
            });
        }
        if trimmed.starts_with("- ") || trimmed == "-" {
            return Err(ParseError {
                line: number,
                message: "a list item; frontmatter here is one `key: value` per line".to_string(),
            });
        }
        let Some((key, rest)) = line.split_once(':') else {
            return Err(ParseError {
                line: number,
                message: format!("`{trimmed}` is not `key: value`"),
            });
        };
        let key = key.trim();
        if key.is_empty()
            || !key
                .chars()
                .all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '-')
        {
            return Err(ParseError {
                line: number,
                message: format!("`{key}` is not a key: letters, digits, `_` and `-`"),
            });
        }
        if !rest.is_empty() && !rest.starts_with([' ', '\t']) {
            // `key:value` with no space is a plain scalar in YAML, and almost
            // always a mistake; `http://…` is the common victim.
            return Err(ParseError {
                line: number,
                message: format!("`{key}:` needs a space before its value"),
            });
        }
        if fields.get(key).is_some() {
            return Err(ParseError {
                line: number,
                message: format!("`{key}` is given twice"),
            });
        }
        let value = scalar(rest.trim(), number)?;
        fields.entries.push((key.to_string(), value));
    }
    Ok(fields)
}

/// One value, as written after the colon.
fn scalar(text: &str, line: usize) -> Result<Scalar, ParseError> {
    if text.is_empty() {
        return Ok(Scalar::Null);
    }
    if let Some(inner) = text.strip_prefix('"') {
        return double_quoted(inner, line).map(Scalar::Text);
    }
    if let Some(inner) = text.strip_prefix('\'') {
        return single_quoted(inner, line).map(Scalar::Text);
    }
    if text.starts_with(['[', '{', '|', '>', '&', '*', '!', '%', '@', '`']) {
        return Err(ParseError {
            line,
            message: format!(
                "`{text}` begins a value this frontmatter does not read (a list, a mapping, a \
                 block scalar or a tag); quote it to use it as text"
            ),
        });
    }
    // A plain scalar runs to a ` #` comment, as YAML's does.
    let plain = match text.find(" #") {
        Some(at) => text[..at].trim_end(),
        None => text,
    };
    Ok(match plain {
        "true" => Scalar::Bool(true),
        "false" => Scalar::Bool(false),
        "null" | "~" => Scalar::Null,
        other => Scalar::Text(other.to_string()),
    })
}

/// The text of a double-quoted value, `inner` starting after the opening
/// quote; anything after the closing quote may only be a comment.
fn double_quoted(inner: &str, line: usize) -> Result<String, ParseError> {
    let mut out = String::new();
    let mut chars = inner.chars();
    while let Some(c) = chars.next() {
        match c {
            '"' => return trailing(chars.as_str(), line).map(|()| out),
            '\\' => match chars.next() {
                Some('"') => out.push('"'),
                Some('\\') => out.push('\\'),
                Some('/') => out.push('/'),
                Some('n') => out.push('\n'),
                Some('t') => out.push('\t'),
                Some('r') => out.push('\r'),
                Some('0') => out.push('\0'),
                Some('u') => {
                    let hex: String = chars.by_ref().take(4).collect();
                    let code = u32::from_str_radix(&hex, 16).ok().and_then(char::from_u32);
                    match code {
                        Some(code) => out.push(code),
                        None => {
                            return Err(ParseError {
                                line,
                                message: format!("`\\u{hex}` is not a character escape"),
                            });
                        }
                    }
                }
                Some(other) => {
                    return Err(ParseError {
                        line,
                        message: format!("`\\{other}` is not an escape this frontmatter reads"),
                    });
                }
                None => break,
            },
            other => out.push(other),
        }
    }
    Err(ParseError {
        line,
        message: "a double-quoted value with no closing quote".to_string(),
    })
}

/// The text of a single-quoted value, where `''` is one quote.
fn single_quoted(inner: &str, line: usize) -> Result<String, ParseError> {
    let mut out = String::new();
    let mut rest = inner;
    loop {
        let Some(at) = rest.find('\'') else {
            return Err(ParseError {
                line,
                message: "a single-quoted value with no closing quote".to_string(),
            });
        };
        out.push_str(&rest[..at]);
        rest = &rest[at + 1..];
        if let Some(after) = rest.strip_prefix('\'') {
            out.push('\'');
            rest = after;
            continue;
        }
        return trailing(rest, line).map(|()| out);
    }
}

/// What may follow a closing quote: whitespace, then nothing or a comment.
fn trailing(rest: &str, line: usize) -> Result<(), ParseError> {
    let rest = rest.trim_start();
    if rest.is_empty() || rest.starts_with('#') {
        Ok(())
    } else {
        Err(ParseError {
            line,
            message: format!("`{rest}` after a closing quote"),
        })
    }
}

/// `value` as a double-quoted frontmatter scalar that [`parse`] reads back
/// exactly, whatever it holds — a colon, a quote, a newline, a `#`.
pub fn quote(value: &str) -> String {
    let mut out = String::with_capacity(value.len() + 2);
    out.push('"');
    for c in value.chars() {
        match c {
            '"' => out.push_str("\\\""),
            '\\' => out.push_str("\\\\"),
            '\n' => out.push_str("\\n"),
            '\t' => out.push_str("\\t"),
            '\r' => out.push_str("\\r"),
            c if c.is_control() => out.push_str(&format!("\\u{:04x}", c as u32)),
            c => out.push(c),
        }
    }
    out.push('"');
    out
}

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

    #[test]
    fn a_file_without_frontmatter_is_all_body() {
        let scanned = scan("just prose\n").expect("scans");
        assert_eq!(scanned.frontmatter, None);
        assert_eq!(scanned.body, "just prose\n");
    }

    #[test]
    fn frontmatter_is_separated_from_the_body_whatever_the_line_endings() {
        let scanned = scan("---\nkey: value\n---\nbody\n").expect("scans");
        assert_eq!(scanned.frontmatter, Some("key: value\n"));
        assert_eq!(scanned.body, "body\n");

        let scanned = scan("---\r\nkey: value\r\n---\r\nbody\r\n").expect("scans");
        assert_eq!(scanned.frontmatter, Some("key: value\r\n"));
        assert_eq!(scanned.body, "body\r\n");

        let scanned = scan("\u{feff}---\nkey: value\n---\nbody\n").expect("scans");
        assert_eq!(scanned.frontmatter, Some("key: value\n"));
    }

    #[test]
    fn a_closing_delimiter_at_end_of_file_leaves_an_empty_body() {
        let scanned = scan("---\nkey: value\n---").expect("scans");
        assert_eq!(scanned.frontmatter, Some("key: value\n"));
        assert_eq!(scanned.body, "");
    }

    #[test]
    fn unterminated_frontmatter_is_an_error_and_empty_frontmatter_is_not() {
        assert_eq!(scan("---\nkey: value\nbody\n"), Err(Unterminated));
        let scanned = scan("---\n---\nbody\n").expect("scans");
        assert_eq!(scanned.frontmatter, Some(""));
    }

    #[test]
    fn plain_quoted_and_boolean_values_are_read() {
        let fields = parse(
            "name: reviewer\n\
             description: \"fix: the thing\\n(two lines)\"\n\
             note: 'it''s fine' # a comment\n\
             url: https://example.com/a#b\n\
             disable-model-invocation: true\n\
             # a comment line\n\
             \n\
             empty:\n\
             nothing: ~\n",
        )
        .expect("parses");

        assert_eq!(fields.text("name"), Some("reviewer"));
        assert_eq!(
            fields.text("description"),
            Some("fix: the thing\n(two lines)")
        );
        assert_eq!(fields.text("note"), Some("it's fine"));
        assert_eq!(fields.text("url"), Some("https://example.com/a#b"));
        assert_eq!(fields.boolean("disable-model-invocation"), Ok(Some(true)));
        assert_eq!(fields.get("empty"), Some(&Scalar::Null));
        assert_eq!(fields.get("nothing"), Some(&Scalar::Null));
        assert_eq!(fields.text("empty"), None);
        assert_eq!(fields.boolean("missing"), Ok(None));
        assert_eq!(
            fields.keys().collect::<Vec<_>>(),
            [
                "name",
                "description",
                "note",
                "url",
                "disable-model-invocation",
                "empty",
                "nothing"
            ]
        );
    }

    #[test]
    fn a_plain_value_stops_at_a_comment_and_keeps_an_inner_colon() {
        let fields = parse("description: remember: the parser is strict # really\n").unwrap();
        assert_eq!(
            fields.text("description"),
            Some("remember: the parser is strict")
        );
    }

    #[test]
    fn two_spellings_of_one_key_are_one_field() {
        let fields = parse("disable_model_invocation: \"true\"\n").unwrap();
        assert_eq!(
            fields.boolean_any(&["disable-model-invocation", "disable_model_invocation"]),
            Ok(Some(true))
        );
        assert_eq!(fields.text_any(&["argument-hint", "argument_hint"]), None);
    }

    #[test]
    fn what_is_not_flat_is_refused_with_the_reason() {
        for (block, expected) in [
            ("tags:\n  - a\n", "indented"),
            ("- a\n", "list item"),
            ("just words\n", "not `key: value`"),
            ("a key: value\n", "not a key"),
            ("url:https://x\n", "needs a space"),
            ("name: a\nname: b\n", "given twice"),
            ("items: [a, b]\n", "quote it"),
            ("text: |\n  block\n", "quote it"),
            ("name: \"open\n", "no closing quote"),
            ("name: 'open\n", "no closing quote"),
            ("name: \"a\" b\n", "after a closing quote"),
            ("name: \"\\q\"\n", "not an escape"),
        ] {
            let error = parse(block).expect_err(block);
            assert!(error.to_string().contains(expected), "{block:?}: {error}");
        }
        assert_eq!(
            parse("flag: maybe\n")
                .unwrap()
                .boolean("flag")
                .unwrap_err()
                .message,
            "`flag` must be true or false, not `maybe`"
        );
    }

    #[test]
    fn quote_round_trips_anything() {
        for value in [
            "plain",
            "remember: the parser is strict",
            "a \"quoted\" word and a 'single'",
            "back\\slash",
            "two\nlines\twith\ttabs",
            "# not a comment",
            "",
            "ends with a backslash \\",
            "\u{1}control",
        ] {
            let block = format!("key: {}\n", quote(value));
            assert_eq!(
                parse(&block).expect(&block).text("key"),
                Some(value),
                "{block}"
            );
        }
    }
}