ascent-research 0.4.2

ascent-research — an incremental research workflow CLI for AI agents. Every session resumes; knowledge accretes across runs. Mixes HTTP, browser, and local file ingest into a durable per-session wiki + figure-rich HTML report.
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
//! Wiki page data layer.
//!
//! Owns the on-disk `<session>/wiki/<slug>.md` files — slug validation,
//! CRUD, and YAML frontmatter parsing. This is the v3 layer that turns
//! a single-file session.md into a karpathy-style multi-file knowledge
//! store.
//!
//! Design constraints:
//! - Slugs are `[a-z0-9_-]{1,64}` — no slashes, no dots, no uppercase.
//!   This keeps filesystem semantics predictable cross-platform and
//!   makes `[[wiki-link]]` round-tripping trivial.
//! - Frontmatter is optional YAML-ish (leading `---\nkey: value\n...---\n`).
//!   Parser is deliberately minimal — we only need `kind`, `sources`,
//!   `related`, `updated`. A full YAML dep isn't worth the build weight.
//! - Write semantics: `create` (fail if exists), `replace` (overwrite),
//!   `append` (preserve prior content; add a dated block). The agent
//!   picks via the Action variant.
//! - Every CRUD returns absolute path so the caller can log it.

use std::fs;
use std::path::PathBuf;

use crate::session::layout;

/// Slug character / length constraints. Keep in sync with spec §3.
const SLUG_MAX_LEN: usize = 64;

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum WikiError {
    SlugInvalid(String),
    AlreadyExists(String),
    NotFound(String),
    Io(String),
}

impl std::fmt::Display for WikiError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            WikiError::SlugInvalid(s) => write!(f, "wiki_slug_invalid: {s}"),
            WikiError::AlreadyExists(s) => write!(f, "wiki_page_exists: {s}"),
            WikiError::NotFound(s) => write!(f, "wiki_page_not_found: {s}"),
            WikiError::Io(s) => write!(f, "wiki_io: {s}"),
        }
    }
}

/// Validate a wiki slug. Returns Ok on pass, WikiError::SlugInvalid with
/// a specific reason otherwise.
pub fn validate_slug(slug: &str) -> Result<(), WikiError> {
    if slug.is_empty() {
        return Err(WikiError::SlugInvalid("empty".into()));
    }
    if slug.len() > SLUG_MAX_LEN {
        return Err(WikiError::SlugInvalid(format!(
            "length {} > {SLUG_MAX_LEN}",
            slug.len()
        )));
    }
    for (i, c) in slug.chars().enumerate() {
        let ok = c.is_ascii_lowercase() || c.is_ascii_digit() || c == '-' || c == '_';
        if !ok {
            return Err(WikiError::SlugInvalid(format!(
                "char {c:?} at pos {i} — allowed: [a-z0-9_-]"
            )));
        }
    }
    Ok(())
}

/// Create a new wiki page. Errors if the page already exists — use
/// `replace_page` to overwrite.
pub fn create_page(session_slug: &str, page_slug: &str, body: &str) -> Result<PathBuf, WikiError> {
    create_page_in(&layout::session_wiki_dir(session_slug), page_slug, body)
}

/// Replace a wiki page's contents. Creates the page if missing —
/// callers that want a create-vs-replace distinction should check
/// existence first.
pub fn replace_page(session_slug: &str, page_slug: &str, body: &str) -> Result<PathBuf, WikiError> {
    replace_page_in(&layout::session_wiki_dir(session_slug), page_slug, body)
}

/// Append `body` to a wiki page, preceded by a `<!-- YYYY-MM-DD -->`
/// timestamp comment so history is visible when re-reading. Missing
/// pages are created with the body as first content.
pub fn append_page(
    session_slug: &str,
    page_slug: &str,
    body: &str,
    stamp: &str,
) -> Result<PathBuf, WikiError> {
    append_page_in(
        &layout::session_wiki_dir(session_slug),
        page_slug,
        body,
        stamp,
    )
}

/// Read a wiki page. NotFound when the file is missing.
pub fn read_page(session_slug: &str, page_slug: &str) -> Result<String, WikiError> {
    read_page_in(&layout::session_wiki_dir(session_slug), page_slug)
}

/// Remove a wiki page. NotFound if missing.
pub fn remove_page(session_slug: &str, page_slug: &str) -> Result<PathBuf, WikiError> {
    remove_page_in(&layout::session_wiki_dir(session_slug), page_slug)
}

/// List all wiki page slugs (alphabetically sorted). Missing wiki/
/// returns an empty vec rather than an error so newborn sessions Just Work.
pub fn list_pages(session_slug: &str) -> Vec<String> {
    list_pages_in(&layout::session_wiki_dir(session_slug))
}

// ── `_in` variants: operate on an explicit wiki directory path ───────
// These power the session-slug wrappers above AND are directly testable
// without touching any env var / global state. Public so tests (and any
// future "query an arbitrary wiki root" flow) can use them.

pub fn create_page_in(
    wiki_dir: &std::path::Path,
    page_slug: &str,
    body: &str,
) -> Result<PathBuf, WikiError> {
    validate_slug(page_slug)?;
    let path = wiki_dir.join(format!("{page_slug}.md"));
    if path.exists() {
        return Err(WikiError::AlreadyExists(page_slug.to_string()));
    }
    fs::create_dir_all(wiki_dir).map_err(|e| WikiError::Io(format!("mkdir wiki/: {e}")))?;
    fs::write(&path, body).map_err(|e| WikiError::Io(format!("write {page_slug}: {e}")))?;
    Ok(path)
}

pub fn replace_page_in(
    wiki_dir: &std::path::Path,
    page_slug: &str,
    body: &str,
) -> Result<PathBuf, WikiError> {
    validate_slug(page_slug)?;
    fs::create_dir_all(wiki_dir).map_err(|e| WikiError::Io(format!("mkdir wiki/: {e}")))?;
    let path = wiki_dir.join(format!("{page_slug}.md"));
    fs::write(&path, body).map_err(|e| WikiError::Io(format!("write {page_slug}: {e}")))?;
    Ok(path)
}

pub fn append_page_in(
    wiki_dir: &std::path::Path,
    page_slug: &str,
    body: &str,
    stamp: &str,
) -> Result<PathBuf, WikiError> {
    validate_slug(page_slug)?;
    fs::create_dir_all(wiki_dir).map_err(|e| WikiError::Io(format!("mkdir wiki/: {e}")))?;
    let path = wiki_dir.join(format!("{page_slug}.md"));
    let prior = fs::read_to_string(&path).unwrap_or_default();
    let sep = if prior.trim().is_empty() { "" } else { "\n\n" };
    let block = format!("{prior}{sep}<!-- appended {stamp} -->\n{body}");
    fs::write(&path, block.trim_start())
        .map_err(|e| WikiError::Io(format!("append {page_slug}: {e}")))?;
    Ok(path)
}

pub fn read_page_in(wiki_dir: &std::path::Path, page_slug: &str) -> Result<String, WikiError> {
    validate_slug(page_slug)?;
    let path = wiki_dir.join(format!("{page_slug}.md"));
    fs::read_to_string(&path).map_err(|e| match e.kind() {
        std::io::ErrorKind::NotFound => WikiError::NotFound(page_slug.to_string()),
        _ => WikiError::Io(e.to_string()),
    })
}

pub fn remove_page_in(wiki_dir: &std::path::Path, page_slug: &str) -> Result<PathBuf, WikiError> {
    validate_slug(page_slug)?;
    let path = wiki_dir.join(format!("{page_slug}.md"));
    if !path.exists() {
        return Err(WikiError::NotFound(page_slug.to_string()));
    }
    fs::remove_file(&path).map_err(|e| WikiError::Io(e.to_string()))?;
    Ok(path)
}

pub fn list_pages_in(wiki_dir: &std::path::Path) -> Vec<String> {
    let Ok(entries) = fs::read_dir(wiki_dir) else {
        return Vec::new();
    };
    let mut out: Vec<String> = entries
        .filter_map(|e| e.ok())
        .filter(|e| e.path().extension().and_then(|s| s.to_str()) == Some("md"))
        .filter_map(|e| {
            e.path()
                .file_stem()
                .and_then(|s| s.to_str())
                .map(str::to_string)
        })
        .collect();
    out.sort();
    out
}

/// Catalog-seed helper. Writes `<wiki_dir>/<page_slug>.md` with a YAML
/// frontmatter block + body. Skip semantics:
///
/// - file exists + `reseed = false` → `Err(WikiError::AlreadyExists)`
///   (caller treats this as a silent skip per spec § 去重 / idempotency)
/// - file exists + `reseed = true` → overwrite
/// - file missing → create
///
/// `frontmatter_lines` is the pre-formatted `key: value` lines that go
/// inside the `---\n...\n---\n` block. We deliberately don't take a typed
/// struct because the catalog-seed frontmatter fields don't slot into the
/// existing `Frontmatter` model (it lacks `host`, `site`, `kind:
/// actionbook-manual`, etc.) and going through that intermediate would
/// lose schema-tolerance on unknown keys. The caller (catalog module)
/// owns the schema.
pub fn seed_manual_page_in(
    wiki_dir: &std::path::Path,
    page_slug: &str,
    frontmatter_lines: &[(&str, String)],
    body: &str,
    reseed: bool,
) -> Result<PathBuf, WikiError> {
    validate_slug(page_slug)?;
    let path = wiki_dir.join(format!("{page_slug}.md"));
    if path.exists() && !reseed {
        return Err(WikiError::AlreadyExists(page_slug.to_string()));
    }
    fs::create_dir_all(wiki_dir).map_err(|e| WikiError::Io(format!("mkdir wiki/: {e}")))?;

    let mut content = String::with_capacity(256 + body.len());
    content.push_str("---\n");
    for (k, v) in frontmatter_lines {
        content.push_str(k);
        content.push_str(": ");
        // Quote ONLY if the value starts with whitespace or contains
        // a quote — `:` itself is fine because `parse_simple_yaml` uses
        // `split_once(':')` which keys-on the FIRST colon, so values
        // like RFC3339 timestamps round-trip without quoting.
        // The spec § Frontmatter schema renders unquoted: `fetched_at:
        // 2026-05-17T12:34:56Z`.
        if v.starts_with(' ') || v.contains('"') || v.contains('\n') {
            content.push('"');
            content.push_str(&v.replace('"', "\\\""));
            content.push('"');
        } else {
            content.push_str(v);
        }
        content.push('\n');
    }
    content.push_str("---\n");
    content.push_str(body);

    fs::write(&path, &content).map_err(|e| WikiError::Io(format!("write {page_slug}: {e}")))?;
    Ok(path)
}

// ── Frontmatter ─────────────────────────────────────────────────────────────

/// Lightweight representation of a wiki page's YAML-ish frontmatter.
/// Unknown keys are preserved in `extra` verbatim so round-tripping
/// doesn't lose data.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Frontmatter {
    pub kind: Option<String>,
    pub sources: Vec<String>,
    pub related: Vec<String>,
    pub updated: Option<String>,
    /// Composite source part labels. Empty when the page comes from a
    /// single-backend source (legacy v0.3 wiki pages parse with an
    /// empty `parts` vec — additive, no breakage). Spec § "Wiki
    /// frontmatter 扩展".
    pub parts: Vec<String>,
    pub extra: Vec<(String, String)>,
}

/// Split a wiki page body into `(frontmatter, remaining_markdown)`.
/// If the page has no frontmatter block, returns `(Frontmatter::default(),
/// body)`.
pub fn split_frontmatter(body: &str) -> (Frontmatter, &str) {
    // Must start with `---\n` to be a frontmatter block.
    let trimmed = body.trim_start_matches('\u{feff}'); // strip BOM
    if !trimmed.starts_with("---\n") && !trimmed.starts_with("---\r\n") {
        return (Frontmatter::default(), body);
    }
    // Find closing `\n---\n`.
    let after_open = &trimmed[4..];
    let Some(close_rel) = after_open
        .find("\n---\n")
        .or_else(|| after_open.find("\n---\r\n"))
    else {
        return (Frontmatter::default(), body);
    };
    let yaml = &after_open[..close_rel];
    let rest_start = 4 + close_rel + "\n---\n".len();
    let rest = trimmed.get(rest_start..).unwrap_or("");
    let fm = parse_simple_yaml(yaml);
    (fm, rest)
}

/// Render a `Frontmatter` value into the on-disk YAML-ish block (without
/// the surrounding `---` fences). Only emits keys that carry data —
/// `parts: []` is omitted entirely so single-source wiki pages stay
/// byte-identical to the v0.3 schema (spec § "Wiki frontmatter 扩展").
/// `extra` is appended verbatim (key/value pairs) so unknown fields
/// round-trip through this writer.
pub fn render_frontmatter_body(fm: &Frontmatter) -> String {
    let mut out = String::new();
    if let Some(kind) = &fm.kind {
        out.push_str(&format!("kind: {kind}\n"));
    }
    if !fm.sources.is_empty() {
        out.push_str(&format!("sources: {}\n", render_yaml_list(&fm.sources)));
    }
    if !fm.parts.is_empty() {
        out.push_str(&format!("parts: {}\n", render_yaml_list(&fm.parts)));
    }
    if !fm.related.is_empty() {
        out.push_str(&format!("related: {}\n", render_yaml_list(&fm.related)));
    }
    if let Some(updated) = &fm.updated {
        out.push_str(&format!("updated: {updated}\n"));
    }
    for (k, v) in &fm.extra {
        out.push_str(&format!("{k}: {v}\n"));
    }
    out
}

fn render_yaml_list(items: &[String]) -> String {
    let inner: Vec<String> = items
        .iter()
        .map(|s| {
            if s.contains(',') || s.contains('[') || s.contains(']') || s.contains('"') {
                format!("\"{}\"", s.replace('"', "\\\""))
            } else {
                s.clone()
            }
        })
        .collect();
    format!("[{}]", inner.join(", "))
}

fn parse_simple_yaml(yaml: &str) -> Frontmatter {
    let mut fm = Frontmatter::default();
    for line in yaml.lines() {
        let line = line.trim_end();
        if line.is_empty() || line.starts_with('#') {
            continue;
        }
        let Some((k, v)) = line.split_once(':') else {
            continue;
        };
        let key = k.trim();
        let val = v.trim();
        match key {
            "kind" => fm.kind = Some(strip_quotes(val).to_string()),
            "updated" => fm.updated = Some(strip_quotes(val).to_string()),
            "sources" => fm.sources = parse_yaml_list(val),
            "related" => fm.related = parse_yaml_list(val),
            // Composite source parts (spec § "Wiki frontmatter 扩展").
            // Reuse the inline-list parser — same syntax as `sources`
            // / `related`. Missing key → leaves `parts` empty, which is
            // the legacy contract for single-source wiki pages.
            "parts" => fm.parts = parse_yaml_list(val),
            _ => fm.extra.push((key.to_string(), val.to_string())),
        }
    }
    fm
}

fn strip_quotes(s: &str) -> &str {
    let t = s.trim();
    if ((t.starts_with('"') && t.ends_with('"')) || (t.starts_with('\'') && t.ends_with('\'')))
        && t.len() >= 2
    {
        return &t[1..t.len() - 1];
    }
    t
}

/// Parse an inline YAML list `[a, b, "c d"]`. Multi-line block form is
/// NOT supported — frontmatter lists are short enough for inline.
fn parse_yaml_list(val: &str) -> Vec<String> {
    let t = val.trim();
    if !t.starts_with('[') || !t.ends_with(']') {
        // Allow bare scalar as single-element list.
        if t.is_empty() {
            return Vec::new();
        }
        return vec![strip_quotes(t).to_string()];
    }
    let inner = &t[1..t.len() - 1];
    // Respect quoted strings when splitting on commas.
    let mut out: Vec<String> = Vec::new();
    let mut buf = String::new();
    let mut quote: Option<char> = None;
    for c in inner.chars() {
        match (c, quote) {
            ('"', None) => quote = Some('"'),
            ('\'', None) => quote = Some('\''),
            (c, Some(q)) if c == q => quote = None,
            (',', None) => {
                let s = strip_quotes(buf.trim()).to_string();
                if !s.is_empty() {
                    out.push(s);
                }
                buf.clear();
            }
            _ => buf.push(c),
        }
    }
    let tail = strip_quotes(buf.trim()).to_string();
    if !tail.is_empty() {
        out.push(tail);
    }
    out
}

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

    // Each wiki test uses a private tempdir whose path is passed to the
    // wiki function's `_at` variant. This avoids the ACTIONBOOK_RESEARCH_
    // HOME env var entirely, so these tests are safe to run in parallel
    // with any other test that touches that variable.
    fn tmp_wiki_dir() -> tempfile::TempDir {
        tempfile::tempdir().unwrap()
    }

    // Slug validation

    #[test]
    fn slug_accepts_lowercase_digits_hyphen_underscore() {
        for s in ["a", "a-b", "a_b", "abc123", "x-1_2", &"a".repeat(64)] {
            assert!(validate_slug(s).is_ok(), "{s} should be valid");
        }
    }

    #[test]
    fn slug_rejects_empty_too_long_and_bad_chars() {
        assert!(matches!(validate_slug(""), Err(WikiError::SlugInvalid(_))));
        assert!(matches!(
            validate_slug(&"a".repeat(65)),
            Err(WikiError::SlugInvalid(_))
        ));
        for s in [
            "Scheduler",
            "with.dot",
            "with/slash",
            "spaces here",
            "bang!",
        ] {
            assert!(
                matches!(validate_slug(s), Err(WikiError::SlugInvalid(_))),
                "{s}"
            );
        }
    }

    // CRUD

    #[test]
    fn create_then_read() {
        let tmp = tmp_wiki_dir();
        let p = create_page_in(tmp.path(), "scheduler", "# Scheduler\nBody.").unwrap();
        assert!(p.exists());
        let got = read_page_in(tmp.path(), "scheduler").unwrap();
        assert!(got.contains("# Scheduler"));
    }

    #[test]
    fn create_twice_fails() {
        let tmp = tmp_wiki_dir();
        create_page_in(tmp.path(), "x", "a").unwrap();
        match create_page_in(tmp.path(), "x", "b") {
            Err(WikiError::AlreadyExists(_)) => {}
            other => panic!("expected AlreadyExists, got {other:?}"),
        }
    }

    #[test]
    fn replace_overwrites() {
        let tmp = tmp_wiki_dir();
        replace_page_in(tmp.path(), "x", "first").unwrap();
        replace_page_in(tmp.path(), "x", "second").unwrap();
        assert_eq!(read_page_in(tmp.path(), "x").unwrap(), "second");
    }

    #[test]
    fn append_preserves_prior_content() {
        let tmp = tmp_wiki_dir();
        create_page_in(tmp.path(), "x", "original body").unwrap();
        append_page_in(tmp.path(), "x", "new paragraph", "2026-04-21").unwrap();
        let got = read_page_in(tmp.path(), "x").unwrap();
        assert!(got.contains("original body"));
        assert!(got.contains("appended 2026-04-21"));
        assert!(got.contains("new paragraph"));
    }

    #[test]
    fn append_creates_when_missing() {
        let tmp = tmp_wiki_dir();
        append_page_in(tmp.path(), "brand-new", "first line", "2026-04-21").unwrap();
        let got = read_page_in(tmp.path(), "brand-new").unwrap();
        assert!(got.contains("appended 2026-04-21"));
        assert!(got.contains("first line"));
    }

    #[test]
    fn remove_works_and_reports_missing() {
        let tmp = tmp_wiki_dir();
        create_page_in(tmp.path(), "x", "body").unwrap();
        remove_page_in(tmp.path(), "x").unwrap();
        assert!(matches!(
            read_page_in(tmp.path(), "x"),
            Err(WikiError::NotFound(_))
        ));
        assert!(matches!(
            remove_page_in(tmp.path(), "x"),
            Err(WikiError::NotFound(_))
        ));
    }

    #[test]
    fn list_returns_sorted_slugs() {
        let tmp = tmp_wiki_dir();
        for name in ["beta", "alpha", "gamma"] {
            create_page_in(tmp.path(), name, "x").unwrap();
        }
        assert_eq!(list_pages_in(tmp.path()), vec!["alpha", "beta", "gamma"]);
    }

    #[test]
    fn list_on_missing_dir_returns_empty() {
        let missing = std::path::Path::new("/tmp/never-existed-wiki-xyz-abc");
        assert!(list_pages_in(missing).is_empty());
    }

    // Frontmatter

    #[test]
    fn split_frontmatter_absent() {
        let (fm, rest) = split_frontmatter("just body\n");
        assert_eq!(fm, Frontmatter::default());
        assert_eq!(rest, "just body\n");
    }

    #[test]
    fn split_frontmatter_parses_all_known_fields() {
        let body = "---\nkind: concept\nsources: [https://a.test, https://b.test]\nrelated: [foo, bar]\nupdated: 2026-04-21\ncustom: xyz\n---\n# Page\nBody.";
        let (fm, rest) = split_frontmatter(body);
        assert_eq!(fm.kind.as_deref(), Some("concept"));
        assert_eq!(fm.sources, vec!["https://a.test", "https://b.test"]);
        assert_eq!(fm.related, vec!["foo", "bar"]);
        assert_eq!(fm.updated.as_deref(), Some("2026-04-21"));
        assert_eq!(fm.extra, vec![("custom".into(), "xyz".into())]);
        assert_eq!(rest, "# Page\nBody.");
    }

    #[test]
    fn split_frontmatter_handles_quoted_values() {
        let body = "---\nkind: \"source-summary\"\nupdated: '2026-04-21'\n---\ndone";
        let (fm, _) = split_frontmatter(body);
        assert_eq!(fm.kind.as_deref(), Some("source-summary"));
        assert_eq!(fm.updated.as_deref(), Some("2026-04-21"));
    }

    #[test]
    fn split_frontmatter_tolerates_unclosed_block() {
        // An unclosed `---\n` at the top is NOT treated as frontmatter —
        // we return the original body untouched rather than eating
        // everything as YAML.
        let body = "---\nkind: bad\n(no close)";
        let (fm, rest) = split_frontmatter(body);
        assert_eq!(fm, Frontmatter::default());
        assert_eq!(rest, body);
    }
}