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
581
582
583
584
585
586
587
//! The PR-body contract `tm pr open` enforces before spawning `gh` (#6653).
//!
//! Why: `tm-workflow.md`'s "Minimal PR Body (nine fields)" section is the
//! repo's PR-body standard, and it is enforced today only by a reviewer
//! noticing a thin body after the PR is already open. Its nine numbered
//! fields are, verbatim from that section:
//!
//! 1. Primary outcome and linked issue(s), with the `Refs owner/repo#N` link.
//! 2. What changed, and what is intentionally out of scope.
//! 3. Risk / blast radius.
//! 4. Test evidence at the applicable levels.
//! 5. Baseline/pre-existing failures and their canonical issue.
//! 6. Gates not run, and why (#7336).
//! 7. Partial-red accounting (#7336).
//! 8. Documentation/changelog status.
//! 9. Review-finding disposition: fixed here, kept on the parent, or
//! separately ticketed.
//!
//! The same section fixes the attribution footer
//! (`🤖🤖🤖 Generated with trusty-mpm — …`) as the body's exact ending. Both
//! are string-shaped facts about a file, so both are checkable.
//!
//! What: [`Field`] names the nine, [`validate`] reports which are missing or
//! empty and whether the body ends with the attribution block — the footer,
//! optionally followed by the harness's session link (#7297) — and
//! [`apply_issue_link`] resolves this repo's `Refs #N` / `Closes #N` rule
//! (root `CLAUDE.md`, "Key Conventions": fix PRs use `Refs #N`, **never**
//! `Closes #N`).
//! Test: the sibling `tests.rs` — `body_*`, `footer_*`, `issue_link_*`.
// #6807: one definition of the footer, shared with the settings seed that
// writes Claude Code's `attribution` key — this validator must hold a body to
// the same text the setting produces.
pub use ATTRIBUTION_FOOTER;
/// One of the nine required PR-body fields.
///
/// Why: naming each field lets a failure report say WHICH one is missing
/// rather than "the body is incomplete", which is the whole point of moving
/// this off the reviewer.
/// What: the nine fields of `tm-workflow.md`'s "Minimal PR Body" section, in
/// its own order. [`Field::heading`] is the canonical heading; the aliases in
/// [`Field::matches`] accept the wordings a human body actually uses.
/// Test: `body_field_table_covers_nine`, `body_accepts_alias_headings`.
pub
/// The nine fields, in `tm-workflow.md`'s own order.
pub const FIELDS: = ;
/// What [`validate`] found wrong with a body.
///
/// Why: `tm pr open` must print WHICH check failed and exit 2 without calling
/// `gh`, so the failure has to be a value, not a formatted string.
/// What: the fields with no heading, the fields whose section held no content,
/// and the footer verdict.
/// Test: `body_reports_each_missing_field`, `body_reports_empty_section`.
pub
/// The opening words of every missing-heading failure line.
///
/// Why (#7574): `tm pr open` offers the paste-able skeleton only when a heading
/// was missing, and it recognises that case from the failure line itself. One
/// spelling keeps the message and its recogniser from drifting apart.
/// Test: `pr_7574_a_missing_heading_offers_the_body_skeleton`.
pub const MISSING_FIELD_PREFIX: &str = "missing required body field";
/// The opening words of every empty-section failure line.
///
/// Why (#8467): the `--minimal` hint fires on any contract gap, missing or
/// empty, and recognises both from the failure line the same way #7574 does.
pub const EMPTY_FIELD_PREFIX: &str = "body field";
/// Is `line` a nine-field contract gap — a missing or an empty section?
///
/// Test: `pr_8467_a_contract_gap_names_the_minimal_opt_out`.
pub
/// What a missing [`Field::accepts_none`] field's failure line adds (#7336).
///
/// Test: `pr_7336_the_failure_line_offers_the_minimal_none_form`.
const NONE_HINT: &str = " — an explicit `none` is a valid whole section";
/// The nine required headings as a body the caller can paste and fill.
///
/// Why (#7574): naming one missing heading per failure line still left the
/// author assembling the skeleton by hand. On trusty-things#253 the agent
/// guessed the shape and spent a second `tm pr open` invocation finding out
/// whether the guess was right. A block already in the right shape ends that
/// round trip.
/// What: `## <heading>` for each of [`FIELDS`] in contract order, blank lines
/// between, then the attribution footer — derived from the same table
/// [`validate`] checks, so it cannot drift from it. The sections are left empty
/// on purpose: pasting it unfilled fails the same gate again, naming each
/// section that still holds no content.
/// Test: `body_skeleton_names_every_field`,
/// `pr_7574_a_missing_heading_offers_the_body_skeleton`,
/// `pr_7336_the_skeleton_carries_both_new_fields`.
pub
/// The field's 1-based position in the contract, for messages.
/// Maximum `#` characters an ATX heading opener may carry.
///
/// Why (#7461): CommonMark caps the opening sequence at six; a seventh `#`
/// makes the line a paragraph. Spelled here so [`heading_text`] states the rule
/// rather than a literal.
const MAX_ATX_LEVEL: usize = 6;
/// Normalize a heading line to its comparable text.
///
/// Why: headings arrive as `## 4. Test evidence` / `### RISK / blast radius`,
/// and the contract is about the words, not the punctuation or the numbering.
/// What: strips the ATX opener, then digits, `.`/`)`/`:` and `*`/`_` emphasis,
/// then lowercases. Returns `None` for a line that is not an ATX heading.
///
/// The opener is the CommonMark one (#7461): one to six `#`, then a space, a
/// tab, or the end of the line. `#7459 flakes not seen.` is therefore a
/// paragraph that happens to start with an issue reference, not a heading —
/// reading it as one ended the previous field's section and reported every
/// field after it empty.
/// Test: `heading_text_strips_numbering_and_emphasis`,
/// `an_issue_ref_at_line_start_is_not_a_heading`.
/// Check `body` against the nine-field contract and the footer rule.
///
/// Why: this is the gate `tm pr open` runs before spawning `gh`, so a thin
/// body is caught by the author rather than by the review gate after the PR
/// is already open and labelled.
/// What: walks the body once. Each ATX heading claims the FIRST contract field
/// it matches that is not already claimed, so `## Docs / changelog` cannot
/// also satisfy `## Review`. A field's section is its lines up to the next
/// heading; no line of the attribution block — the footer, and the session link
/// the harness pairs with it — counts as section content, so a section holding
/// only those is reported empty. The footer must be the last non-blank line,
/// or the last one above a single trailing session link (#7297).
/// Test: `body_accepts_a_complete_body`, `body_reports_each_missing_field`,
/// `body_reports_empty_section`, `footer_must_be_last_line`,
/// `footer_alone_does_not_fill_a_section`,
/// `footer_accepts_the_trailing_session_link`,
/// `footer_accepts_a_session_link_before_the_footer`,
/// `footer_rejects_a_body_with_no_attribution_line`.
pub
/// The Claude Code session link the harness pairs with the footer.
///
/// Why (#7297): Claude Code's provisioned attribution instructs a session to
/// end a PR body with the footer, a blank line, then this link. The validator
/// required the footer to be the LAST non-blank line, so a session that
/// followed its own instructions could not open a PR with `tm pr open` and fell
/// back to `gh pr create` — which skips the nine-field gate entirely.
/// What: the URL prefix, matched with or without the `Claude-Session:` label
/// the commit-message form of the same block uses.
/// Test: `footer_accepts_the_trailing_session_link`,
/// `footer_accepts_a_session_link_before_the_footer`.
const SESSION_LINK_PREFIX: &str = "https://claude.ai/code/session_";
/// Label the commit-message form of the session line carries.
const SESSION_LINE_LABEL: &str = "Claude-Session:";
/// Is `line` a session link belonging to the attribution block?
///
/// Why: see [`SESSION_LINK_PREFIX`]. A prefix test alone accepted anything that
/// merely STARTED with the URL, so a line of prose trailing the link — or an
/// entire sentence after it — passed as attribution and could close a body or
/// be skipped as section content.
/// What: the whole line, after an optional [`SESSION_LINE_LABEL`] and the
/// whitespace behind it, must be the prefix plus a non-empty session id of
/// ASCII alphanumerics, `_` and `-`. Anything else — an empty id, a space, a
/// trailing word, punctuation — is not a session link.
/// Test: `footer_accepts_the_trailing_session_link`,
/// `footer_accepts_a_session_link_before_the_footer`,
/// `footer_rejects_a_session_link_with_trailing_junk`.
/// Does `body` end with the attribution block?
///
/// Why: the block is the footer, optionally followed by the session link — both
/// orderings are in live use, because agents blocked by the old rule moved the
/// link ABOVE the footer to get a PR open (#7297). Accepting both keeps those
/// bodies valid while letting a session follow its own attribution instruction.
/// What: true when the last non-blank line is [`ATTRIBUTION_FOOTER`], or when it
/// is a single session link whose preceding non-blank line is that footer. A
/// body with no footer at all is false, whatever else it ends with.
/// Test: `footer_must_be_last_line`, `footer_accepts_the_trailing_session_link`,
/// `footer_accepts_a_session_link_before_the_footer`,
/// `footer_rejects_a_body_with_no_attribution_line`,
/// `footer_rejects_a_session_link_with_trailing_junk`,
/// `footer_rejects_two_stacked_session_links`.
/// How an issue reference must appear in the body.
///
/// Why: root `CLAUDE.md` is explicit — fix PRs use `Refs #N`, **never**
/// `Closes #N`, because a merge must not auto-close the issue before live
/// verification. Opting into `Closes` has to be a deliberate flag, and a body
/// that smuggles a `Closes #N` past a caller who did not pass it is the exact
/// mistake the rule exists to stop.
/// What: `Refs` by default, `Closes` only under `--closes`.
/// Test: `issue_link_defaults_to_refs`, `issue_link_closes_is_opt_in`,
/// `issue_link_rejects_unrequested_closes`.
pub
/// Insert the issue link into `body`, or report why it cannot be.
///
/// Why: the link is field 1's obligation and the `Refs`/`Closes` choice is a
/// hard project rule, so `tm pr open` owns both rather than trusting the body
/// file to have got them right.
/// What: with no `--issue`, a body carrying an unrequested closing keyword is
/// rejected. With `--issue N`, a body already carrying the correct
/// `<keyword> #N` line is left alone; otherwise the line is inserted
/// immediately above the attribution footer. Any closing keyword present when
/// `--closes` was NOT passed is always an error, whatever `--issue` says.
/// Test: `issue_link_defaults_to_refs`, `issue_link_is_idempotent`,
/// `issue_link_rejects_unrequested_closes`,
/// `issue_link_rejects_every_closing_keyword`,
/// `issue_link_rejects_a_closing_keyword_outside_field_one`,
/// `issue_link_rejects_a_negated_closing_keyword`,
/// `issue_link_allows_prose_that_only_looks_like_a_keyword`,
/// `issue_link_without_issue_leaves_body_alone`,
/// `issue_link_lands_above_a_footer_a_session_link_trails`.
pub
/// GitHub's closing keywords, lowercased.
///
/// Shared with `metadata::first_linked_issue` (#7869): a body linking its issue
/// with a closing keyword links it exactly as `Refs #N` does, so one list
/// decides both "is this an unrequested auto-close?" and "which issue does this
/// PR inherit from?".
/// Test: `issue_link_rejects_every_closing_keyword`,
/// `pr_7869_a_closes_link_inherits_the_issues_metadata`.
pub const CLOSING_KEYWORDS: = ;
/// The first closing keyword + issue reference in `body`, if any.
///
/// Why: GitHub scans the whole body, not just field 1, and honours a keyword
/// inside a negation, so `Fixes #N` anywhere auto-closes the issue on
/// squash-merge before live verification. Matching only a line that STARTS
/// with `closes ` let PR #6894 close #6888 off a `Fixes #6888` in field 1 —
/// see #6895, and #5389 for the negation case.
/// What: scans each line token by token for a keyword whose next token is an
/// issue reference (`#12` or `owner/repo#12`), mirroring GitHub's grammar, and
/// returns the matched pair so the error can quote it.
/// Test: `issue_link_rejects_unrequested_closes`,
/// `issue_link_rejects_every_closing_keyword`,
/// `issue_link_rejects_a_closing_keyword_outside_field_one`,
/// `issue_link_rejects_a_negated_closing_keyword`,
/// `issue_link_allows_prose_that_only_looks_like_a_keyword`.
/// Is `token` a `#12` / `owner/repo#12` issue reference, trailing punctuation
/// and all?