verify-readme-limitations.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /**
  2. * Doc-sync gate: every package README carries the standard
  3. * `## Known Limitations and Deferred Work` section — the per-package home for
  4. * consumer-visible gaps and consciously postponed work that the
  5. * [documentation standard](../docs/AGENTS.md) assigns to the package-README
  6. * tier. One canonical heading instead of per-package variants ("Limitations",
  7. * "What is NOT here", …) keeps the section greppable across the repo and makes
  8. * its absence a gate failure rather than an oversight.
  9. *
  10. * A package with genuinely nothing to declare is listed in NO_LIMITATIONS
  11. * below and must NOT carry the section — an empty section invites boilerplate,
  12. * and a whitelisted package that gains real limitations leaves the whitelist
  13. * in the same change. Whitelist entries are validated against the scanned
  14. * package set, so a rename or removal fails loud instead of silently
  15. * un-gating a README.
  16. *
  17. * Checks, per packages/<group>/<pkg>/README.md (fenced code excluded):
  18. * 1. Non-whitelisted: exactly one limitations-like heading, byte-equal to the
  19. * canonical h2, with at least one top-level `- ` bullet before the next
  20. * heading.
  21. * 2. Whitelisted: no limitations-like heading at all.
  22. * 3. Every whitelist entry names a scanned package.
  23. *
  24. * "Limitations-like" also matches near-miss headings at any level ("known
  25. * limitations", "deferred work", "what is not here", a heading starting with
  26. * "limitations"/"deferred") so a drifted heading cannot impersonate the
  27. * canonical section and a second competing section cannot coexist with it.
  28. *
  29. * Checker, not fixer: it reports and never rewrites.
  30. * Run: `tsx scripts/verify-readme-limitations.ts`.
  31. */
  32. import { globSync, readFileSync } from 'node:fs'
  33. import { resolve } from 'node:path'
  34. const root = resolve(import.meta.dirname, '..')
  35. /** The one canonical section heading, required verbatim as an h2. */
  36. const CANONICAL = '## Known Limitations and Deferred Work'
  37. /**
  38. * Packages with genuinely no known limitations or deferred work (keyed by
  39. * package directory relative to the repo root). Their READMEs must NOT carry
  40. * the section; adding one moves the package off this list in the same change.
  41. */
  42. const NO_LIMITATIONS: readonly string[] = [
  43. 'packages/support/subagent-mock',
  44. 'packages/ui/app-boot',
  45. 'packages/util/brand',
  46. 'packages/util/timeout',
  47. ]
  48. /** A heading that reads as a limitations section — canonical or drifted. */
  49. function isLimitationsLike(headingText: string): boolean {
  50. return (
  51. /known limitation/i.test(headingText)
  52. || /deferred work/i.test(headingText)
  53. || /what is not here/i.test(headingText)
  54. || /^limitations?\b/i.test(headingText)
  55. || /^deferred\b/i.test(headingText)
  56. )
  57. }
  58. interface Line {
  59. index: number
  60. raw: string
  61. }
  62. /** Split a README into prose lines (fenced code dropped), keeping 1-based line numbers. */
  63. function proseLines(text: string): Line[] {
  64. let inFence = false
  65. const kept: Line[] = []
  66. text.split('\n').forEach((raw, i) => {
  67. if (raw.startsWith('```')) {
  68. inFence = !inFence
  69. return
  70. }
  71. if (!inFence) kept.push({ index: i + 1, raw })
  72. })
  73. return kept
  74. }
  75. const readmes = globSync('packages/*/*/README.md', { cwd: root }).sort()
  76. const scannedPackages = new Set(readmes.map(path => path.slice(0, -'/README.md'.length)))
  77. const failures: string[] = []
  78. for (const entry of NO_LIMITATIONS) {
  79. if (!scannedPackages.has(entry)) {
  80. failures.push(`whitelist entry ${entry} does not name a scanned package — renamed or removed? update NO_LIMITATIONS in scripts/verify-readme-limitations.ts in the same change`)
  81. }
  82. }
  83. for (const readme of readmes) {
  84. const pkg = readme.slice(0, -'/README.md'.length)
  85. const lines = proseLines(readFileSync(resolve(root, readme), 'utf8'))
  86. const headings = lines.filter(line => /^#{1,6} /.test(line.raw))
  87. const limitations = headings.filter(line => isLimitationsLike(line.raw.replace(/^#{1,6}\s+/, '')))
  88. if (NO_LIMITATIONS.includes(pkg)) {
  89. for (const heading of limitations) {
  90. failures.push(`${readme}:${heading.index}: whitelisted as having no known limitations, but carries ${JSON.stringify(heading.raw)} — drop the section or remove the package from NO_LIMITATIONS`)
  91. }
  92. continue
  93. }
  94. const heading = limitations.at(0)
  95. if (heading === undefined) {
  96. failures.push(`${readme}: missing the \`${CANONICAL}\` section (a package with genuinely nothing to declare joins NO_LIMITATIONS in scripts/verify-readme-limitations.ts instead)`)
  97. continue
  98. }
  99. if (limitations.length > 1) {
  100. failures.push(`${readme}: ${limitations.length} limitations-like headings (lines ${limitations.map(line => line.index).join(', ')}) — keep exactly one \`${CANONICAL}\` section`)
  101. continue
  102. }
  103. if (heading.raw.trimEnd() !== CANONICAL) {
  104. failures.push(`${readme}:${heading.index}: non-canonical heading ${JSON.stringify(heading.raw)} — use \`${CANONICAL}\``)
  105. continue
  106. }
  107. const headingAt = lines.indexOf(heading)
  108. const body = lines.slice(headingAt + 1)
  109. const end = body.findIndex(line => /^#{1,6} /.test(line.raw))
  110. const section = end === -1 ? body : body.slice(0, end)
  111. if (!section.some(line => /^- /.test(line.raw))) {
  112. failures.push(`${readme}:${heading.index}: the \`${CANONICAL}\` section has no top-level \`- \` bullet — state the limitations, or whitelist the package if there are genuinely none`)
  113. }
  114. }
  115. if (failures.length > 0) {
  116. console.error('verify-readme-limitations: violations found:')
  117. for (const failure of failures) console.error(` ${failure}`)
  118. process.exit(1)
  119. }
  120. console.log(`verify-readme-limitations: ${readmes.length} package READMEs checked (${NO_LIMITATIONS.length} whitelisted), all conform.`)