Skip to content

Avoid copying the prompt name before a parse error needs it - #21

Merged
justinwilaby merged 1 commit into
cppalliance:masterfrom
justinwilaby:fix/20
Nov 18, 2026
Merged

justinwilaby merged 1 commit into
cppalliance:masterfrom
justinwilaby:fix/20

Conversation

@justinwilaby

@justinwilaby justinwilaby commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Why

The parser copied the frontmatter name as soon as frontmatter parsing succeeded, but used that copy only if body parsing failed. On a successful parse, we cloned the name and then threw the copy away.

This is a minor cleanup, but it illustrates the ownership issue discussed in Slack: a copy can work around an ownership constraint that we can instead remove by changing where the value is owned.

Plan

  • Keep the frontmatter in parse_source while the body is parsed.
  • Have parse_body borrow the split source and return sections, rather than consume the frontmatter and construct the whole prompt.
  • Borrow frontmatter.name() inside the error closure, and assemble the prompt only after body parsing succeeds.
  • Store the body as Arc<str> in the split source so sections can retain shared ownership without copying the body text. Arc::clone shares the allocation; it does not clone the text.

Error facts, file names, source locations, and parsing order remain unchanged. This does not remove every allocation from error handling or introduce a new lint.

Validation

Passed locally:

  • cargo test --locked -p runvil-prompt --all-features: 233 tests.
  • cargo test --locked -p build-xtask: 204 passed, 21 ignored.
  • cargo fmt --all --check and git diff --check.
  • CARGO_BUILD_WARNINGS=deny cargo clippy --workspace --all-targets --all-features.
  • Documentation builds with RUSTDOCFLAGS="-D warnings": workspace/all features, default-feature runvil-engine, and default-feature runvil.
  • node --test tools/docs-claims.mjs: 3 tests.
  • Independent review of the complete two-file diff found no correctness blockers.

The fallback cargo test --locked --workspace --all-features run failed at lifecycle::the_fixture_is_loaded_invoked_unloaded_once_and_closed in the Plugin test fixture, on the assertion that the dynamic library is closed. That test also failed in isolation on this macOS machine. Its code is outside this change; the cause has not been established, and the full workspace suite is not claimed as passing.

The prescribed nextest, custom Dylint, and facade API checks were not run locally: cargo-nextest, cargo-dylint, and the pinned nightly-2026-09-05 toolchain are not installed. CI results should be checked before merging.

Follow-up

Once the Engine stabilizes, review clone and to_owned calls across the crates for similar patterns. Restructure ownership first where practical; when a copy really is needed to resolve a borrow conflict, explain the conflict. A broader audit, lint evaluation, or improved borrow-checker guidance is separate work—not something this PR claims to solve. Decision pages are unchanged because this is an internal refactor, not a change to a caller-visible decision.

Closes #20.

Summary by CodeRabbit

Bug Fixes

  • Improved error reporting during prompt parsing. Errors encountered in a prompt’s body now include the prompt name, helping identify which prompt has a problem. Frontmatter errors are reported without a prompt name because it may not yet be available. Frontmatter and body parsing behavior otherwise remains unchanged.

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 cppalliance#20.
@coderabbitai

coderabbitai Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The parser now stores the split Markdown body as Arc<str>. It parses frontmatter and body in separate stages, applies error context at each stage, and constructs the prompt after both stages succeed.

Changes: Prompt parsing

Layer / File(s) Summary
Split source body storage
crates/runvil-internal/prompt/src/frontmatter.rs
SplitSource::body now uses Arc<str>. split_frontmatter converts the collected body to that type.
Parser stages and error context
crates/runvil-internal/prompt/src/parse.rs
parse_source handles frontmatter errors without a prompt name and body errors with the parsed frontmatter name. parse_body borrows SplitSource and returns sections; parse_source constructs the Prompt after both stages succeed.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Refactor

Suggested reviewers: vinniefalco

Merge Risk: Minimal · up to ff365

This refactor removes an eager string copy in prompt parsing without changing parsed output or error reporting. No concrete merge-blocking risk was identified; check CI for the reported workspace test failure before merging.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names a real change in the PR: the parser stops eagerly copying the prompt name, so the error path can borrow it. It matches the stated objective of issue #20 and the change to parse_source …
Linked Issues check Passed [#20] Issue #20 requires removing the eager prompt-name copy from successful parsing, preserving sections and all error facts, and limiting the change to the parser ownership cleanup.

Evidence at hea…
Out of Scope Changes check Passed The whole-PR change covers two files: crates/runvil-internal/prompt/src/parse.rs and crates/runvil-internal/prompt/src/frontmatter.rs. Both are in the prompt parser ownership path that #20 names.

The…
Full details: Title check

Explanation

The title names a real change in the PR: the parser stops eagerly copying the prompt name, so the error path can borrow it. It matches the stated objective of issue #20 and the change to parse_source and parse_body. It omits the Arc<str> body change, which is a supporting detail. The title is 59 characters and reads as a single clear sentence.

Full details: Linked Issues check

Explanation

[#20] Issue #20 requires removing the eager prompt-name copy from successful parsing, preserving sections and all error facts, and limiting the change to the parser ownership cleanup.

Evidence at head (crates/runvil-internal/prompt/src/parse.rs): parse_source calls parse_frontmatter, which returns the Frontmatter and SplitSource and keeps the frontmatter owned by the caller. parse_body(&amp;split, file) borrows the split source and returns Vec&lt;Section&gt;. The name is read only in the error closure: .map_err(|error| error::with_prompt_context(error, Some(frontmatter.name()), file)). On success, no name copy is made. The Prompt is assembled only after body parsing succeeds. The frontmatter-stage error path passes None for the name, which keeps the previous fact behavior.

Error facts are unchanged in the visible code: the same with_prompt_context calls, the same stage order, and the same key_lines placement. The Prompt fields are populated from the same values.

Remaining uncertainty: the reported test results (233 and 204 passed, 21 ignored, plus Clippy and doc builds) were not re-run in this assessment. The full workspace suite has an unexplained failure in lifecycle::the_fixture_is_loaded_invoked_unloaded_once_and_closed, which the author reports is outside this change. The issue's verification criterion depends on existing tests passing in CI, which the PR says is still pending. The code requirements are met by the source read.

Full details: Out of Scope Changes check

Explanation

The whole-PR change covers two files: crates/runvil-internal/prompt/src/parse.rs and crates/runvil-internal/prompt/src/frontmatter.rs. Both are in the prompt parser ownership path that #20 names.

The SplitSource::body change from String to Arc&lt;str&gt; supports the ownership restructure. parse_body uses Arc::clone(&amp;split.body) to share the body with SectionWalker without a second copy. This is supporting work for the borrow-based design, not an unrelated change.

The PR does not change the Prompt public shape, decision pages, or the Engine. It does not add a lint or detector, which #20 excludes. The PR description's claim that Arc::clone avoids copying body text is accurate for the sharing step. The one-time conversion from String to Arc&lt;str&gt; in split_frontmatter still allocates. This is a minor note on the description, not a scope violation.

No unrelated change was found in the reviewed sources.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 8893f5be-64a0-4304-bb65-9ad0f4c25c59
Commits

Reviewing files that changed from the base of the PR and between db4cf1f and ff365d8.

Files selected for processing: 2

  • crates/runvil-internal/prompt/src/frontmatter.rs
  • crates/runvil-internal/prompt/src/parse.rs

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

@vinniefalco

Copy link
Copy Markdown
Member

This still leaves a body copy in the failure path, Arc::from(body) ?

@justinwilaby

Copy link
Copy Markdown
Contributor Author

This still leaves a body copy in the failure path, Arc::from(body) ?

body was already owned by SplitSource and is cloned at the lines.collect() call , this leaves that clone unchanged.

@justinwilaby
justinwilaby merged commit ff365d8 into cppalliance:master Nov 18, 2026
12 checks passed
@justinwilaby
justinwilaby deleted the fix/20 branch November 18, 2026 17:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Avoid copying the prompt name before a parse error needs it

2 participants