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
//! Tests for the compose-time instruction fold (#7616).
//!
//! Why: the fold is the one place trusty-mpm rewrites the bytes it delivers to
//! the PM, so every test here is about what must SURVIVE it, not only about what
//! it removes. A fold that loses a table row or a rule is worse than no fold.
//! What: the removal cases (comments, padding, blank runs), the preservation
//! cases (fenced blocks, every table row, indentation), idempotence, and the
//! corpus-level assertion that the bundled sections actually shrink.
//! Test: this file IS the suite.
use super::*;
use crate::core::content_source::test_support::rc;
/// The canonical section sources, read from the checkout's content (#9012).
fn section_sources() -> Vec<(&'static str, &'static str)> {
crate::core::instruction_pipeline::SECTION_FILES
.iter()
.map(|path| (*path, rc().required(path)))
.collect()
}
#[test]
fn authoring_comments_are_not_delivered() {
// `core.md` opens with a version stamp and a two-line PURPOSE block. Both
// are notes to whoever edits the file; neither is an instruction.
let src = "<!-- PM_INSTRUCTIONS_VERSION: 0024 -->\n\
<!-- PURPOSE: notes for the\n\
\x20 author only. -->\n\
# PM Agent\n\n\
Delegate by default.\n";
let folded = fold_delivered_prompt(src);
assert_eq!(folded, "# PM Agent\n\nDelegate by default.\n");
}
#[test]
fn a_fenced_block_is_emitted_verbatim() {
// The override-marker grammar is taught inside a fence, and a fence is the
// one place an HTML comment IS content. Stripping it there would delete the
// syntax a project needs to customize anything.
let src = "Use this:\n\
```\n\
<!-- TRUSTY-MPM: WORKFLOW START v=1 -->\n\
| padded | cell |\n\
```\n\
after\n";
let folded = fold_delivered_prompt(src);
assert!(
folded.contains("<!-- TRUSTY-MPM: WORKFLOW START v=1 -->"),
"a comment inside a fence is content: {folded}"
);
assert!(
folded.contains("| padded | cell |"),
"a fenced table row keeps its padding: {folded}"
);
}
/// #7616 REGRESSION: a fence-shaped line inside an OPEN HTML comment must not
/// desynchronise the parser.
///
/// Why this is the worst shape the fold can get wrong: it fails in both
/// directions at once. Testing the fence before the comment flipped `in_fence`
/// on a line that was really comment content, so the comment's own text and its
/// `-->` leaked into the delivered prompt — and when that phantom fence toggled
/// back off, the parser was still `in_comment` and silently DELETED real
/// instruction lines until the next `-->`. `assemble_sections` folds the joined
/// output including a project's `CLAUDE.md` override bodies, so a project author
/// documenting fence syntax inside a multi-line comment would lose real rules
/// from every PM prompt, with nothing anywhere saying so.
/// FAILS BEFORE THIS CHANGE: `hidden` and `-->` appear in the output and
/// `CODE INSIDE A REAL FENCE` is gone.
/// Test: itself.
#[test]
fn a_fence_inside_an_open_comment_does_not_desynchronise_the_parser() {
let src = "<!-- example:\n\
```\n\
hidden\n\
-->\n\
REAL RULE: never do X.\n\
```\n\
CODE INSIDE A REAL FENCE\n\
```\n\
AFTER\n";
let folded = fold_delivered_prompt(src);
assert!(
!folded.contains("hidden"),
"comment content leaked into the delivered prompt: {folded}"
);
assert!(
!folded.contains(COMMENT_CLOSE),
"the comment's closing delimiter leaked: {folded}"
);
for survivor in [
"REAL RULE: never do X.",
"CODE INSIDE A REAL FENCE",
"AFTER",
] {
assert!(
folded.contains(survivor),
"the fold deleted the real line {survivor:?}: {folded}"
);
}
assert_eq!(
folded,
"REAL RULE: never do X.\n```\nCODE INSIDE A REAL FENCE\n```\nAFTER\n"
);
}
// ---------------------------------------------------------------------------
// #7616 — whole-line semantics.
//
// A line whose comment runs to the end of the line is dropped. A line that
// MIXES comment and content is delivered VERBATIM — no strip, no compaction, no
// trailing-whitespace trim. Three review rounds of partial-line handling each
// produced a new interaction (fence state, table-row indentation, the
// continuation branch), so the fold now edits only lines it can classify with
// certainty and delivers the rest untouched.
// ---------------------------------------------------------------------------
/// #7616: content after a same-line close survives, as the whole line.
///
/// Why verbatim rather than the content alone: handing a stripped remainder back
/// to the pipeline is what produced three rounds of findings. Delivering the
/// physical line costs the comment's own bytes and cannot lose a rule.
/// Test: itself.
#[test]
fn content_after_a_same_line_comment_close_survives() {
let folded = fold_delivered_prompt("<!-- note -->REAL CONTENT HERE\nafter\n");
assert_eq!(folded, "<!-- note -->REAL CONTENT HERE\nafter\n");
}
/// #7616: two complete spans on one line — the whole line is delivered, so the
/// text between them cannot be lost.
/// Test: itself.
#[test]
fn two_comments_on_one_line_keep_the_text_between_them() {
let folded = fold_delivered_prompt("<!-- a --> text <!-- b -->\n");
assert_eq!(folded, "<!-- a --> text <!-- b -->\n");
}
/// #7616: the accepted cost of classifying on the FIRST `-->`.
///
/// Why: a line of two adjacent spans carries no content, yet the first close is
/// not the end of the line, so it is delivered. Classifying on the LAST `-->`
/// would drop it — and would also drop `<!-- a --> text <!-- b -->`, deleting
/// ` text `. Keeping a few comment bytes is the safe side of that trade, and
/// this test exists so the cost is visible rather than discovered.
/// Test: itself.
#[test]
fn a_line_of_two_comment_spans_is_delivered_rather_than_risk_deleting_content() {
let folded = fold_delivered_prompt("<!-- a --><!-- b -->\n");
assert_eq!(folded, "<!-- a --><!-- b -->\n");
}
/// #7616: the ordinary case must not regress — a line that is ONLY a comment
/// still vanishes, and leaves no blank line behind where it stood.
///
/// Why this is asserted separately: it is the branch the three bundled goldens
/// depend on. An implementation that emitted an empty string here would insert a
/// blank at every authoring comment and change all three.
/// Test: itself.
#[test]
fn a_line_that_is_only_a_comment_leaves_no_blank_behind() {
let folded = fold_delivered_prompt("before\n<!-- note -->\nafter\n");
assert_eq!(folded, "before\nafter\n");
}
/// #7616 REGRESSION (critic input 1): a mixed comment/fence line is delivered
/// verbatim and does NOT open a fence.
///
/// Why the fence state matters: the delivered text ends in a fence marker, but
/// the LINE does not satisfy `trim_start().starts_with("```")`, so `in_fence`
/// stays false. The row after it is therefore outside a fence and compacts; the
/// next bare fence marker opens one, and the row inside it is verbatim. That is
/// the contract, pinned here rather than left to be rediscovered.
/// FAILS BEFORE THIS CHANGE: the stripped remainder was compacted through
/// `compact_row` without toggling fence state, so the fenced row was compacted
/// and the unfenced one was left padded.
/// Test: itself.
#[test]
fn a_mixed_comment_and_fence_line_does_not_open_a_fence() {
let src = "<!-- lang -->```\n\
| a | b |\n\
```\n\
| c | d |\n";
let folded = fold_delivered_prompt(src);
assert_eq!(
folded,
"<!-- lang -->```\n\
|a|b|\n\
```\n\
| c | d |\n"
);
}
/// #7616 REGRESSION (critic input 2): the line that CLOSES an open comment is
/// delivered verbatim when content follows the close.
///
/// FAILS BEFORE THIS CHANGE: the continuation branch dropped the whole closing
/// line, so the output was `\n`.
/// Test: itself.
#[test]
fn a_closing_line_that_carries_content_is_delivered_verbatim() {
let folded = fold_delivered_prompt("<!-- open\nstuff\nclose --> REAL CONTENT\n");
assert_eq!(folded, "close --> REAL CONTENT\n");
}
/// #7616 REGRESSION (critic input 3): an indented table row on a mixed line
/// keeps its indentation.
///
/// Why: `compact_row` exempts an INDENTED table-shaped line, because indentation
/// inside a list item is structural. The partial-line path trimmed the remainder
/// before handing it over, which silently removed that exemption.
/// FAILS BEFORE THIS CHANGE: the row came back as `|a|b|`.
/// Test: itself.
#[test]
fn an_indented_table_row_after_a_comment_is_delivered_verbatim() {
let folded = fold_delivered_prompt("<!-- c --> | a | b |\n");
assert_eq!(folded, "<!-- c --> | a | b |\n");
}
/// #7616: state pair — an open comment that never closes swallows the rest of
/// the input, including fence-shaped lines, and delivers nothing.
/// Test: itself.
#[test]
fn an_unclosed_comment_swallows_the_rest_of_the_input() {
let folded = fold_delivered_prompt("real\n<!-- open\n```\nnever closed\n");
assert_eq!(folded, "real\n");
}
/// #7616: state pair — a comment-shaped line INSIDE a fence is content, so it is
/// emitted verbatim and never opens comment state.
/// Test: itself.
#[test]
fn a_comment_inside_a_fence_is_content_not_comment_state() {
let src = "```\n<!-- TRUSTY-MPM: WORKFLOW START v=1 -->\n```\nafter\n";
assert_eq!(fold_delivered_prompt(src), src);
}
#[test]
fn table_padding_is_stripped() {
let src = "| # | Forbidden Action | CB# |\n\
|---|-----------------|-----|\n\
| P1 | Edit of source | 1 |\n";
let folded = fold_delivered_prompt(src);
assert_eq!(
folded,
"|#|Forbidden Action|CB#|\n|---|-----------------|-----|\n|P1|Edit of source|1|\n"
);
}
#[test]
fn no_table_row_is_lost() {
// The brief's hard constraint: P1-P11 and every circuit-breaker row must
// survive the fold. Row COUNT is the mechanical form of that guarantee.
for (path, body) in section_sources() {
let rows = |text: &str| {
text.lines()
.filter(|l| l.trim_start().starts_with('|'))
.count()
};
assert_eq!(
rows(&fold_delivered_prompt(body)),
rows(body),
"{path} lost a table row to the fold"
);
}
}
#[test]
fn every_prohibition_and_circuit_breaker_row_survives() {
// Named rather than counted: a row renamed away is as bad as one deleted.
let enforcement = rc().required("sections/enforcement.md");
let folded = fold_delivered_prompt(enforcement);
for n in 1..=11 {
assert!(
folded.contains(&format!("|P{n}|")),
"prohibition P{n} is missing from the folded section"
);
}
for n in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 14] {
assert!(
folded.contains(&format!("|{n}|")),
"circuit breaker CB#{n} is missing from the folded section"
);
}
}
#[test]
fn every_skill_pointer_survives() {
// Progressive disclosure is the corpus's whole design: a dropped pointer
// silently removes the PM's access to a skill's contents.
for (path, body) in section_sources() {
let pointers = |text: &str| text.matches("Skill(skill=").count();
assert_eq!(
pointers(&fold_delivered_prompt(body)),
pointers(body),
"{path} lost a Skill() pointer to the fold"
);
}
}
#[test]
fn blank_line_runs_collapse() {
let folded = fold_delivered_prompt("a\n\n\n\n\nb\n");
assert_eq!(folded, "a\n\nb\n");
}
#[test]
fn leading_and_trailing_blanks_go() {
let folded = fold_delivered_prompt("\n\n a \n\n\n");
assert_eq!(folded, " a\n");
}
#[test]
fn an_indented_table_row_keeps_its_indentation() {
// Indentation inside a list item is structural, so a table-shaped line that
// is indented is left exactly as authored.
let src = "- item:\n | a | b |\n";
assert_eq!(fold_delivered_prompt(src), src);
}
#[test]
fn the_fold_is_idempotent() {
for (path, body) in section_sources() {
let once = fold_delivered_prompt(body);
assert_eq!(
fold_delivered_prompt(&once),
once,
"{path} folds differently the second time"
);
}
}
#[test]
fn the_fold_never_grows_a_section() {
for (path, body) in section_sources() {
assert!(
fold_delivered_prompt(body).len() <= body.len(),
"{path} grew under the fold"
);
}
}
/// #7616: the corpus-level claim. Before the fold existed this was 0 B.
#[test]
fn the_bundled_corpus_actually_shrinks() {
let authored: usize = section_sources().iter().map(|(_, body)| body.len()).sum();
let folded: usize = section_sources()
.iter()
.map(|(_, body)| fold_delivered_prompt(body).len())
.sum();
assert!(
folded < authored,
"the fold recovered nothing: authored {authored} B, folded {folded} B"
);
}
#[test]
fn an_unclosed_comment_in_one_part_hides_nothing_in_the_next() {
// #8533: a project body ending inside `<!--` beside base text. Folded as one
// string the base text vanished; folded per part it survives.
let parts = [
"Project body.\n<!-- TODO",
"> **Agent selection.** base",
"",
];
assert_eq!(
fold_parts(&parts, "\n\n"),
"Project body.\n\n> **Agent selection.** base"
);
assert_eq!(
fold_delivered_prompt(&parts[..2].join("\n\n")),
"Project body."
);
}
#[test]
fn tilde_and_long_backtick_fences_pair_by_character_and_length() {
// #8533: a fence closes only on its own character, at least as long, with
// nothing else on the line. Inside it, a comment is code and survives.
let text = "~~~\n<!-- kept -->\n```\n~~~\n\n````md\n```\n<!-- kept too -->\n```\n````\n\
<!-- dropped -->\nafter";
assert_eq!(
fold_delivered_prompt(text),
"~~~\n<!-- kept -->\n```\n~~~\n\n````md\n```\n<!-- kept too -->\n```\n````\nafter"
);
let mut open = None;
assert!(step_fence(&mut open, "```rust"));
assert!(
!step_fence(&mut open, "```rust"),
"an info string never closes"
);
assert!(
!step_fence(&mut open, "~~~"),
"the other character never closes"
);
assert!(step_fence(&mut open, " ```` "));
assert_eq!(open, None);
assert_eq!(open_fence_at_end("``` a`b"), None, "a backtick info string");
}
#[test]
fn a_block_closes_its_own_tilde_or_long_backtick_fence() {
// #8533: a block that leaves a fence open is closed with the same character
// and length, so the next block joined after it is prose again.
for (text, closed) in [
("Body.\n```text\nopen", "Body.\n```text\nopen\n```"),
("Body.\n~~~~\nopen", "Body.\n~~~~\nopen\n~~~~"),
(
"Body.\n````md\n```\ninner",
"Body.\n````md\n```\ninner\n````",
),
("Body.\n```\nclosed\n```", "Body.\n```\nclosed\n```"),
] {
assert_eq!(fold_block(text), closed, "{text:?}");
assert_eq!(open_fence_at_end(&fold_block(text)), None, "{text:?}");
}
assert_eq!(
fold_parts(&["Body.\n~~~\nopen", "Base text."], "\n\n"),
"Body.\n~~~\nopen\n~~~\n\nBase text."
);
}