verify-rfc-classification.ts 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /**
  2. * Doc-sync gate: enforce the RFC classification scheme
  3. * ([the classification RFC](../docs/rfc/implemented/process/2026-06-20-rfc-classification.md))
  4. * and the freshness of the generated index
  5. * ([the index-generation RFC](../docs/rfc/implemented/process/2026-07-04-generate-rfc-index-tables.md)).
  6. * Every RFC is filed at `docs/rfc/{lifecycle}/{class}/yyyy-mm-dd-topic.md`; the
  7. * folder IS the label. This gate is the machine source of truth for the closed
  8. * class set and keeps the generated index honest.
  9. *
  10. * Three checks (all against [rfc-index.ts](./rfc-index.ts), the shared walker
  11. * and renderer):
  12. *
  13. * 1. STRUCTURE — every `.md` under a lifecycle folder lives in a class folder
  14. * from CLASSES, is named `yyyy-mm-dd-*.md`, and opens with a parseable H1.
  15. * A loose `.md` directly under a lifecycle root (other than the
  16. * README/AGENTS allowlist) fails; an unknown class folder fails; a stray
  17. * file at an unexpected depth fails. This is what makes the set CLOSED: a
  18. * new class folder can't appear without amending CLASSES (and the README's
  19. * Classification section, per the RFC).
  20. * 2. FRESHNESS — the committed `docs/rfc/INDEX.md` byte-matches a fresh render
  21. * from the tree, so every RFC is listed exactly once, under the heading
  22. * matching its path, with its H1 title and filename date. The fix for a
  23. * stale index is `pnpm run gen-rfc-index`, never a hand edit. This is
  24. * checker, not fixer: it reports and never rewrites.
  25. * 3. NO STRAY ROWS — `docs/rfc/README.md` (the curated front door) carries no
  26. * index-shaped table rows; the list lives only in the generated INDEX.md.
  27. *
  28. * Run: `tsx scripts/verify-rfc-classification.ts`.
  29. */
  30. import { readFileSync } from 'node:fs'
  31. import { resolve } from 'node:path'
  32. import { INDEX_ROW, renderIndex, rfcRoot, walkRfcTree } from './rfc-index.ts'
  33. const { rfcs, errors } = walkRfcTree()
  34. if (errors.length === 0) {
  35. let index: string | undefined
  36. try {
  37. index = readFileSync(resolve(rfcRoot, 'INDEX.md'), 'utf8')
  38. } catch {
  39. // A missing INDEX.md is reported below as staleness, exactly like a drifted one.
  40. }
  41. if (renderIndex(rfcs) !== index) {
  42. errors.push('index: docs/rfc/INDEX.md is stale or missing — run `pnpm run gen-rfc-index` and commit the result')
  43. }
  44. const readme = readFileSync(resolve(rfcRoot, 'README.md'), 'utf8')
  45. for (const line of readme.split('\n')) {
  46. if (INDEX_ROW.test(line)) {
  47. errors.push(`readme: index-shaped row in the curated README (the list lives in INDEX.md): ${JSON.stringify(line.slice(0, 80))}`)
  48. }
  49. }
  50. }
  51. if (errors.length === 0) {
  52. console.log(`verify-rfc-classification: ${rfcs.length} RFC(s) checked, structure and index consistent.`)
  53. process.exit(0)
  54. }
  55. console.error('verify-rfc-classification: violations found:')
  56. for (const e of errors) console.error(` ${e}`)
  57. process.exit(1)