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
//! YAML scalar encoding for the composed frontmatter block.
//!
//! Why: `builder.rs` reached the 500-SLOC production cap while `provenance:`
//! (#4698) was being added to it. These four functions are the one cohesive
//! group in that file that nothing outside the composer calls and that carries
//! no knowledge of agents, inheritance, or `Frontmatter` — they take a `&str`
//! and return a `String`. Splitting them out is the smallest cut that leaves
//! both halves whole.
//! What: [`escape_yaml_double_quoted`] and [`unescape_yaml_double_quoted`] are
//! exact inverses, so a compose -> deploy -> re-compose cycle round-trips a
//! value verbatim; [`needs_quoting`] decides whether a scalar must be quoted at
//! all, and [`render_scalar`] applies that decision. Every item is
//! `pub(crate)` — this is the composer's private encoding, not a public API.
//! Test: the round-trip and quoting tests live with the composer they serve, in
//! `builder_tests.rs`: `initial_prompt_with_embedded_quote_round_trips`,
//! `initial_prompt_with_backslash_round_trips`,
//! `description_with_embedded_newline_round_trips`,
//! `compose_description_with_colon_is_quoted_and_strict_yaml_valid`,
//! `compose_description_without_colon_is_unquoted`.
/// Escape a string for emission inside a YAML double-quoted scalar.
///
/// Why: `merge_frontmatter` wraps the `initialPrompt` value — and, since
/// issue #3556, any other scalar [`render_scalar`] decided needs quoting —
/// in double-quotes. A raw `"` or `\` in the value would otherwise terminate
/// the quote early or be misread as an escape, and a raw embedded newline
/// would break the single-line frontmatter grammar entirely; either produces
/// malformed YAML. claude-mpm parity requires the emitted frontmatter always
/// be parseable.
/// What: a single pass over `value`'s characters that escapes `\` → `\\`,
/// `"` → `\"`, and a literal newline → the two-character sequence `\n` (so a
/// multi-line description/model/etc. value still composes to one physical
/// frontmatter line). Every other character passes through unchanged. The
/// exact inverse of [`unescape_yaml_double_quoted`].
/// Test: `initial_prompt_with_embedded_quote_round_trips`,
/// `initial_prompt_with_backslash_round_trips`,
/// `description_with_embedded_newline_round_trips` in builder_tests.rs.
pub
/// Reverse [`escape_yaml_double_quoted`] on a parsed scalar value.
///
/// Why: a value parsed back from emitted frontmatter still carries the `\"`
/// and `\\` escapes after [`parse_kv_line`] strips the single outer quote pair.
/// Decoding them here makes a compose→deploy→re-compose round-trip yield the
/// original `initialPrompt` string unchanged.
/// What: collapses `\\` → `\`, `\"` → `"`, and `\n` (the two-character
/// escape sequence) → a literal newline; any other `\x` sequence (and a
/// trailing lone `\`) is left verbatim so non-escaped backslashes survive.
/// Test: `initial_prompt_with_embedded_quote_round_trips`,
/// `initial_prompt_with_backslash_round_trips`,
/// `description_with_embedded_newline_round_trips` in builder_tests.rs.
pub
/// Whether a frontmatter scalar value requires YAML quoting to stay parseable
/// by a strict YAML reader.
///
/// Why (issue #3556): `merge_frontmatter` used to emit every scalar field
/// (`name`, `role`, `description`, `model`, `resource_tier`) as a bare plain
/// scalar regardless of content, even though `parse_kv_line` had already
/// stripped any quotes the SOURCE file used. `split_frontmatter`'s own
/// lenient parser (first-colon-only split) tolerates a colon anywhere in the
/// value, so a source template quoting `description: 'Rust 2024 edition
/// specialist: memory-safe systems...'` composed to the exact same UNQUOTED
/// `description: Rust 2024 edition specialist: memory-safe systems...` line
/// regardless of the source's quoting style — invalid YAML a strict parser
/// (`serde_yaml`, used by `trusty-agents::agents::registry::md_agent::parse_md_agent`)
/// rejects with "mapping values are not allowed in this context". Because
/// compose output was invariant to source quoting, re-provisioning alone
/// could never have fixed the 11 affected agents — recomposing reproduced
/// byte-identical broken output. Quoting-on-emit whenever a value is unsafe
/// as a plain scalar fixes it at the one place all consumers share.
/// What: `true` when `value` is empty, opens with a YAML indicator
/// character, has leading/trailing whitespace, contains an embedded newline,
/// is one of the YAML core-schema null tokens (`null`/`Null`/`NULL`/`~`,
/// which would otherwise round-trip through `Option<String>` as `None`
/// instead of the literal string), or contains a `": "` / trailing `:` / a
/// mid-string `" #"` sequence a plain scalar cannot represent. The `" #"`
/// check exists because a space followed by `#` starts a YAML comment
/// ANYWHERE in a plain scalar, not just at the start of the line — the
/// leading-character check above only catches `#` as the very first
/// character, so a value like `Model context protocol #1 tool for
/// delegating` silently truncated to `Model context protocol` at the first
/// `" #"` with no error anywhere in the pipeline (code-critic review of
/// #3556's PR #3565): `compose_agent` succeeds, `validate_frontmatter`
/// accepts it (truncated-but-still-valid YAML is syntactically fine), and
/// the real consumer (`trusty-agents`' `.md` loader) silently drops
/// everything from the `#` onward. Same blind spot as the #3556 root cause
/// (trusty-mpm's own lenient `parse_kv_line` only treats `#` as a comment
/// marker at the start of the trimmed line, so it never notices either) —
/// just a different trigger character.
/// Test: `needs_quoting_true_for_colon_space`, `needs_quoting_true_for_empty`,
/// `needs_quoting_false_for_plain_value`, `needs_quoting_true_for_mid_string_hash_comment`,
/// `needs_quoting_true_for_embedded_newline`, `needs_quoting_true_for_null_tokens`,
/// `needs_quoting_indicator_characters` (table-driven) in builder_tests.rs.
pub
/// Render one frontmatter scalar value, quoting it when [`needs_quoting`]
/// says a plain scalar would not survive a strict YAML parse.
///
/// Why: shared by every scalar field `merge_frontmatter` emits (`name`,
/// `role`, `description`, `model`, `resource_tier`) so the quote-when-needed
/// policy is applied uniformly rather than ad hoc per field (issue #3556).
/// What: returns `value` unchanged when it is safe as a plain scalar;
/// otherwise a double-quoted, escaped scalar via
/// [`escape_yaml_double_quoted`] — the same quoting style `initialPrompt`
/// already used, so `split_frontmatter`'s existing `unescape_yaml_double_quoted`
/// decode (now applied to these fields too) round-trips it.
/// Test: `compose_description_with_colon_is_quoted_and_strict_yaml_valid`,
/// `compose_description_without_colon_is_unquoted` in builder_tests.rs.
pub