verify-rfc-format.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /**
  2. * Doc-sync gate: enforce the RFC in-file format
  3. * ([README.md § The file format](../docs/rfc/README.md), the contract; rationale in
  4. * [the uniform-format RFC](../docs/rfc/implemented/process/2026-07-05-uniform-rfc-format.md)).
  5. * The classification gate owns WHERE a file sits and how it is named; this gate
  6. * owns what is INSIDE: the header block, the per-lifecycle body skeleton, and
  7. * the Alternatives-considered mandate.
  8. *
  9. * Per English RFC (`.zh.md` counterparts are the pairing gate's concern):
  10. *
  11. * 1. HEADER — line 1 is `# RFC: <title>`, line 2 blank, line 3 the one
  12. * `Status:` line in the file, line 4 blank. The status is the dateless enum
  13. * matching the lifecycle folder: `Status: proposed`, `Status: implemented`,
  14. * or `Status: rejected — <reason>`.
  15. * 2. SKELETON — the first `##` section is `## Problem`; the lifecycle's
  16. * required sections are present under their canonical names (`proposed/`:
  17. * Proposal, Acceptance criteria, Risks; `implemented/`: Decision,
  18. * Consequences; `rejected/`: Proposal); `implemented/` must not carry the
  19. * proposal-era headings (Proposal, Plan, Migration plan, Acceptance
  20. * criteria) that the docs standard's slop checklist outlaws there.
  21. * 3. ALTERNATIVES — `## Alternatives considered` is present, or the file is a
  22. * pre-format RFC (dated before the format landed) carrying the exact
  23. * grandfather comment instead. Carrying both, or grandfathering a
  24. * post-format RFC, fails.
  25. * 4. DEBT MARKER — the retired legacy-format debt comment may not reappear.
  26. *
  27. * Checker, not fixer: it reports and never rewrites.
  28. * Run: `tsx scripts/verify-rfc-format.ts`.
  29. */
  30. import { readFileSync } from 'node:fs'
  31. import { resolve } from 'node:path'
  32. import { rfcRoot, walkRfcTree } from './rfc-index.ts'
  33. /** The date the format contract landed; the grandfather comment is valid only before it. */
  34. const FORMAT_ADOPTED = '2026-07-05'
  35. /** The exact comment a pre-format RFC carries in place of `## Alternatives considered`. */
  36. const GRANDFATHER = '<!-- rfc-format: alternatives-not-recorded (pre-format RFC) -->'
  37. /** The retired debt marker that flagged pre-format bodies; banned so it cannot creep back. */
  38. const LEGACY_MARKER = 'XXX: legacy ADR/RFC body format'
  39. /** Status-line grammar per lifecycle folder. */
  40. const STATUS: Record<string, RegExp> = {
  41. proposed: /^Status: proposed$/,
  42. implemented: /^Status: implemented$/,
  43. rejected: /^Status: rejected — .+$/,
  44. }
  45. /** Required `##` headings per lifecycle, beyond the universal `## Problem` opener. */
  46. const REQUIRED: Record<string, string[]> = {
  47. proposed: ['## Proposal', '## Acceptance criteria', '## Risks'],
  48. implemented: ['## Decision', '## Consequences'],
  49. rejected: ['## Proposal'],
  50. }
  51. /** Headings banned in `implemented/` — proposal-era spec-speak per the slop checklist. */
  52. const BANNED_IMPLEMENTED = /^## (?:Proposal\b|Plan\b|Migration plan\b|Acceptance criteria\b)/i
  53. const { rfcs, errors } = walkRfcTree()
  54. for (const rfc of rfcs) {
  55. const fail = (msg: string): void => {
  56. errors.push(`format: ${rfc.rel} — ${msg}`)
  57. }
  58. const lines = readFileSync(resolve(rfcRoot, rfc.rel), 'utf8').split('\n')
  59. // Content scans ignore fenced code blocks: an RFC may legitimately QUOTE a
  60. // status line, a banned heading, or the grandfather comment inside a fence
  61. // (the README's own format section does), and only real prose counts.
  62. let inFence = false
  63. const prose = lines.filter((l) => {
  64. if (l.startsWith('```')) {
  65. inFence = !inFence
  66. return false
  67. }
  68. return !inFence
  69. })
  70. if (!/^# RFC: \S/.test(lines[0] ?? '')) fail('line 1 must be `# RFC: <title>`')
  71. if (lines[1] !== '') fail('line 2 must be blank')
  72. const status = STATUS[rfc.lifecycle]
  73. if (status !== undefined && !status.test(lines[2] ?? '')) {
  74. fail(`line 3 must match the ${rfc.lifecycle} status grammar (${String(status)})`)
  75. }
  76. if (lines[3] !== '') fail('line 4 must be blank')
  77. const statusLines = prose.filter(l => l.startsWith('Status:') && l !== lines[2])
  78. if (statusLines.length > 0 || prose.filter(l => l === lines[2]).length > 1) {
  79. fail('the line-3 `Status:` line must be the only one in the file')
  80. }
  81. const h2s = prose.filter(l => l.startsWith('## ')).map(l => l.trimEnd())
  82. if (h2s[0] !== '## Problem') fail(`the first section must be \`## Problem\` (got ${JSON.stringify(h2s[0] ?? '<none>')})`)
  83. for (const required of REQUIRED[rfc.lifecycle] ?? []) {
  84. if (!h2s.includes(required)) fail(`missing the required \`${required}\` section`)
  85. }
  86. if (rfc.lifecycle === 'implemented') {
  87. for (const h2 of h2s.filter(h => BANNED_IMPLEMENTED.test(h))) {
  88. fail(`\`${h2}\` is a proposal-era heading; an implemented RFC states what is (fold it into Decision/Consequences/Testing)`)
  89. }
  90. }
  91. const hasSection = h2s.includes('## Alternatives considered')
  92. const hasGrandfather = prose.includes(GRANDFATHER)
  93. if (hasSection && hasGrandfather) fail('carries both `## Alternatives considered` and the grandfather comment — drop the comment')
  94. 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)')
  95. if (hasGrandfather && rfc.date >= FORMAT_ADOPTED) fail(`the grandfather comment is only valid for RFCs dated before ${FORMAT_ADOPTED}`)
  96. if (prose.some(l => l.includes(LEGACY_MARKER))) fail('carries the retired legacy-format debt marker')
  97. }
  98. if (errors.length === 0) {
  99. console.log(`verify-rfc-format: ${rfcs.length} RFC(s) checked, all conform to docs/rfc/README.md § The file format.`)
  100. process.exit(0)
  101. }
  102. console.error('verify-rfc-format: violations found:')
  103. for (const e of errors) console.error(` ${e}`)
  104. process.exit(1)