From ff365d80f6824b474bb2a89452cef75fac09bcd4 Mon Sep 17 00:00:00 2001 From: Justin Wilaby Date: Wed, 18 Nov 2026 09:02:18 -0700 Subject: [PATCH] Avoid copying the prompt name before a parse error needs it Keep frontmatter ownership in parse_source and borrow its name only when body parsing fails. Let parse_body borrow the split source and return sections, with shared body text retained in an Arc. Closes #20. --- .../runvil-internal/prompt/src/frontmatter.rs | 5 ++- crates/runvil-internal/prompt/src/parse.rs | 38 +++++++++---------- 2 files changed, 21 insertions(+), 22 deletions(-) diff --git a/crates/runvil-internal/prompt/src/frontmatter.rs b/crates/runvil-internal/prompt/src/frontmatter.rs index 494a5ed8d..b7b78cb76 100644 --- a/crates/runvil-internal/prompt/src/frontmatter.rs +++ b/crates/runvil-internal/prompt/src/frontmatter.rs @@ -2,6 +2,7 @@ use std::collections::BTreeMap; use std::num::NonZeroU32; +use std::sync::Arc; use runvil_types::error::ErrorCode; use runvil_types::models::ModelRole; @@ -161,7 +162,7 @@ pub(crate) struct SplitSource { /// The YAML text between the two `---` delimiters. pub(crate) yaml: String, /// The markdown body after the closing `---` line. - pub(crate) body: String, + pub(crate) body: Arc, /// The number of lines consumed by the frontmatter block (both `---` /// delimiters and everything between them). pub(crate) frontmatter_lines: u32, @@ -234,7 +235,7 @@ pub(crate) fn split_frontmatter(input: &str) -> Result { let body = lines.collect::>().join("\n"); Ok(SplitSource { yaml, - body, + body: Arc::from(body), frontmatter_lines: line_count, key_lines: declared .into_iter() diff --git a/crates/runvil-internal/prompt/src/parse.rs b/crates/runvil-internal/prompt/src/parse.rs index c37901dd7..10b32e237 100644 --- a/crates/runvil-internal/prompt/src/parse.rs +++ b/crates/runvil-internal/prompt/src/parse.rs @@ -152,14 +152,18 @@ impl Prompt { fn parse_source(input: &str, file: &str) -> std::result::Result { // Everything past the frontmatter postdates the prompt's name, so a // failure from there on names it. - let (name, result) = match Self::parse_frontmatter(input) { - Ok((frontmatter, split)) => ( - Some(frontmatter.name().to_owned()), - Self::parse_body(frontmatter, split, file), - ), - Err(error) => (None, Err(error)), - }; - result.map_err(|error| error::with_prompt_context(error, name.as_deref(), file)) + let (frontmatter, split) = Self::parse_frontmatter(input) + .map_err(|error| error::with_prompt_context(error, None, file))?; + let sections = Self::parse_body(&split, file) + .map_err(|error| error::with_prompt_context(error, Some(frontmatter.name()), file))?; + + Ok(Prompt { + file: file.to_owned(), + frontmatter, + sections, + key_lines: split.key_lines, + linked: BTreeMap::new(), + }) } /// Splits the frontmatter from the body, decodes it, and checks its @@ -178,12 +182,11 @@ impl Prompt { Ok((frontmatter, split)) } - fn parse_body(frontmatter: Frontmatter, split: SplitSource, file: &str) -> Result { + fn parse_body(split: &SplitSource, file: &str) -> Result> { let frontmatter_lines = split.frontmatter_lines; - let shared: Arc = Arc::from(split.body); - let body: &str = &shared; - let headings = sections::collect_headings(body)?; - if let Some(violation) = fence::first_fence_violation(body, frontmatter_lines) { + + let headings = sections::collect_headings(&split.body)?; + if let Some(violation) = fence::first_fence_violation(&split.body, frontmatter_lines) { return Err(violation); } @@ -191,16 +194,11 @@ impl Prompt { // every heading at that level is a top-level section, and a first // heading deeper than a later one fails the walker's level check. let top = headings.iter().map(|h| h.level).min().unwrap_or(1); + let shared = Arc::clone(&split.body); let mut walker = SectionWalker::new(&shared, &headings, frontmatter_lines, file); let mut sections: Vec
= walker.build_preamble(top)?.into_iter().collect(); sections.extend(walker.build_sections(top.saturating_sub(1))?); - Ok(Prompt { - file: file.to_owned(), - frontmatter, - sections, - key_lines: split.key_lines, - linked: BTreeMap::new(), - }) + Ok(sections) } }