verify-rfc-format.ts 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /**
  2. * Enforce RFC headers, lifecycle-specific sections, alternatives, and retired
  3. * marker rules. Classification and filenames belong to the sibling tree gate;
  4. * translation structure belongs to the pairing gate. Exact format and
  5. * grandfathering rules live in `docs/rfc/README.md`.
  6. */
  7. import { readFileSync } from 'node:fs'
  8. import { resolve } from 'node:path'
  9. import { rfcRoot, walkRfcTree } from './rfc-index.ts'
  10. /** The date the format contract landed; the grandfather comment is valid only before it. */
  11. const FORMAT_ADOPTED = '2026-07-05'
  12. /** The exact comment a pre-format RFC carries in place of `## Alternatives considered`. */
  13. const GRANDFATHER = '<!-- rfc-format: alternatives-not-recorded (pre-format RFC) -->'
  14. /** The retired debt marker that flagged pre-format bodies; banned so it cannot creep back. */
  15. const LEGACY_MARKER = 'XXX: legacy ADR/RFC body format'
  16. /** Status-line grammar per lifecycle folder. */
  17. const STATUS: Record<string, RegExp> = {
  18. proposed: /^Status: proposed$/,
  19. implemented: /^Status: implemented$/,
  20. rejected: /^Status: rejected — .+$/,
  21. }
  22. /** Required `##` headings per lifecycle, beyond the universal `## Problem` opener. */
  23. const REQUIRED: Record<string, string[]> = {
  24. proposed: ['## Proposal', '## Acceptance criteria', '## Risks'],
  25. implemented: ['## Decision', '## Consequences'],
  26. rejected: ['## Proposal'],
  27. }
  28. /** Headings banned in `implemented/` — proposal-era spec-speak per the slop checklist. */
  29. const BANNED_IMPLEMENTED = /^## (?:Proposal\b|Plan\b|Migration plan\b|Acceptance criteria\b)/i
  30. const { rfcs, errors } = walkRfcTree()
  31. for (const rfc of rfcs) {
  32. const fail = (msg: string): void => {
  33. errors.push(`format: ${rfc.rel} — ${msg}`)
  34. }
  35. const lines = readFileSync(resolve(rfcRoot, rfc.rel), 'utf8').split('\n')
  36. // Format tokens inside fenced examples are not document structure.
  37. let inFence = false
  38. const prose = lines.filter((l) => {
  39. if (l.startsWith('```')) {
  40. inFence = !inFence
  41. return false
  42. }
  43. return !inFence
  44. })
  45. if (!/^# RFC: \S/.test(lines[0] ?? '')) fail('line 1 must be `# RFC: <title>`')
  46. if (lines[1] !== '') fail('line 2 must be blank')
  47. const status = STATUS[rfc.lifecycle]
  48. if (status !== undefined && !status.test(lines[2] ?? '')) {
  49. fail(`line 3 must match the ${rfc.lifecycle} status grammar (${String(status)})`)
  50. }
  51. if (lines[3] !== '') fail('line 4 must be blank')
  52. const statusLines = prose.filter(l => l.startsWith('Status:') && l !== lines[2])
  53. if (statusLines.length > 0 || prose.filter(l => l === lines[2]).length > 1) {
  54. fail('the line-3 `Status:` line must be the only one in the file')
  55. }
  56. const h2s = prose.filter(l => l.startsWith('## ')).map(l => l.trimEnd())
  57. if (h2s[0] !== '## Problem') fail(`the first section must be \`## Problem\` (got ${JSON.stringify(h2s[0] ?? '<none>')})`)
  58. for (const required of REQUIRED[rfc.lifecycle] ?? []) {
  59. if (!h2s.includes(required)) fail(`missing the required \`${required}\` section`)
  60. }
  61. if (rfc.lifecycle === 'implemented') {
  62. for (const h2 of h2s.filter(h => BANNED_IMPLEMENTED.test(h))) {
  63. fail(`\`${h2}\` is a proposal-era heading; an implemented RFC states what is (fold it into Decision/Consequences/Testing)`)
  64. }
  65. }
  66. const hasSection = h2s.includes('## Alternatives considered')
  67. const hasGrandfather = prose.includes(GRANDFATHER)
  68. if (hasSection && hasGrandfather) fail('carries both `## Alternatives considered` and the grandfather comment — drop the comment')
  69. if (!hasSection && !hasGrandfather) fail('missing `## Alternatives considered` (a pre-format RFC whose alternatives are not reconstructible carries the grandfather comment instead — see docs/rfc/README.md § The file format)')
  70. if (hasGrandfather && rfc.date >= FORMAT_ADOPTED) fail(`the grandfather comment is only valid for RFCs dated before ${FORMAT_ADOPTED}`)
  71. if (prose.some(l => l.includes(LEGACY_MARKER))) fail('carries the retired legacy-format debt marker')
  72. }
  73. if (errors.length === 0) {
  74. console.log(`verify-rfc-format: ${rfcs.length} RFC(s) checked, all conform to docs/rfc/README.md § The file format.`)
  75. process.exit(0)
  76. }
  77. console.error('verify-rfc-format: violations found:')
  78. for (const e of errors) console.error(` ${e}`)
  79. process.exit(1)