verify-subsystem-pages.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /**
  2. * Doc-sync gate for package-group subsystem references. Every package group
  3. * either links at least one existing `docs/subsystems/` page from its English
  4. * group README or carries an explicit, justified exemption below.
  5. */
  6. import { existsSync, globSync, readFileSync } from 'node:fs'
  7. import { resolve, sep } from 'node:path'
  8. import { parseMarkdown, visitMarkdown } from './markdown.ts'
  9. const root = resolve(import.meta.dirname, '..')
  10. /**
  11. * Package groups that do not own a standalone subsystem reference. Reasons
  12. * are reviewable policy: a new group cannot silently inherit an exemption.
  13. */
  14. export const GROUPS_WITHOUT_SUBSYSTEM_PAGE: Readonly<Record<string, string>> = {
  15. acp: 'Protocol transport entry point; the server package README owns its interoperability contract.',
  16. boot: 'Shared application-bin boot library rather than a runtime subsystem.',
  17. bundle: 'Composition patch carriers whose mounted packages own all runtime contracts.',
  18. examples: 'Non-product demonstration compositions whose mounted packages own all runtime contracts.',
  19. hooks: 'External hook-protocol bridges over existing interception points, not a new Harness service.',
  20. sdk: 'Out-of-process protocol and client packages whose package READMEs own the SDK contracts.',
  21. util: 'Low-level primitives whose business semantics remain with their consuming subsystems.',
  22. }
  23. /** Result of auditing package-group subsystem documentation. */
  24. export interface SubsystemPageAudit {
  25. /** Package groups discovered from group READMEs or child package manifests. */
  26. readonly groups: number
  27. /** Groups carrying at least one direct subsystem-page link. */
  28. readonly linked: number
  29. /** Groups covered by an explicit no-page policy. */
  30. readonly exempt: number
  31. /** Actionable contract violations. */
  32. readonly violations: readonly string[]
  33. }
  34. /** Normalize one filesystem glob result to repository slash form. */
  35. function normalize(path: string): string {
  36. return path.split(sep).join('/')
  37. }
  38. /** Extract the package-group segment from a repository-relative path. */
  39. function groupOf(path: string): string {
  40. const group = path.split('/')[1]
  41. if (group === undefined || group.length === 0) throw new Error(`invalid package path: ${path}`)
  42. return group
  43. }
  44. /** Return canonical subsystem-page targets linked by one group README. */
  45. function subsystemLinks(source: string): string[] {
  46. const links = new Set<string>()
  47. visitMarkdown(parseMarkdown(source), (node) => {
  48. if (node.type !== 'link') return
  49. const match = /^\.\.\/\.\.\/docs\/subsystems\/([^/#?]+\.md)(?:#[^?#]*)?$/.exec(node.url)
  50. const page = match?.[1]
  51. if (page !== undefined && page !== 'README.md' && !page.endsWith('.zh.md')) links.add(`docs/subsystems/${page}`)
  52. })
  53. return [...links].sort()
  54. }
  55. /**
  56. * Audit package-group subsystem ownership for one repository tree.
  57. * @param scanRoot - repository root containing `packages/` and `docs/`.
  58. * @param exemptions - groups intentionally carrying no subsystem-page link.
  59. * @returns counts plus every actionable violation.
  60. */
  61. export function auditSubsystemPages(
  62. scanRoot: string = root,
  63. exemptions: Readonly<Record<string, string>> = GROUPS_WITHOUT_SUBSYSTEM_PAGE,
  64. ): SubsystemPageAudit {
  65. const readmes = globSync('packages/*/README.md', { cwd: scanRoot }).map(normalize).sort()
  66. const manifests = globSync('packages/*/*/package.json', { cwd: scanRoot }).map(normalize).sort()
  67. const groups = new Set([...readmes, ...manifests].map(groupOf))
  68. const violations: string[] = []
  69. let linked = 0
  70. let exempt = 0
  71. for (const [group, reason] of Object.entries(exemptions)) {
  72. if (!groups.has(group)) {
  73. violations.push(`exemption ${group}: no matching package group; remove the stale entry`)
  74. }
  75. if (reason.trim().length === 0) {
  76. violations.push(`exemption ${group}: missing justification for omitting a subsystem page`)
  77. }
  78. }
  79. for (const group of [...groups].sort()) {
  80. const readme = `packages/${group}/README.md`
  81. const readmePath = resolve(scanRoot, readme)
  82. if (!existsSync(readmePath)) {
  83. violations.push(`${readme}: package group has no group README declaring subsystem ownership`)
  84. continue
  85. }
  86. const links = subsystemLinks(readFileSync(readmePath, 'utf8'))
  87. const isExempt = Object.hasOwn(exemptions, group)
  88. if (links.length === 0) {
  89. if (isExempt) {
  90. exempt += 1
  91. } else {
  92. violations.push(
  93. `${readme}: no reader-visible direct docs/subsystems/*.md link; add the owning page and link,`
  94. + ' or add a justified GROUPS_WITHOUT_SUBSYSTEM_PAGE entry',
  95. )
  96. }
  97. continue
  98. }
  99. linked += 1
  100. if (isExempt) {
  101. violations.push(`${readme}: links a subsystem page but remains exempt; remove the stale exemption`)
  102. }
  103. for (const page of links) {
  104. if (!existsSync(resolve(scanRoot, page))) {
  105. violations.push(`${readme}: linked subsystem page does not exist: ${page}`)
  106. }
  107. }
  108. }
  109. return { groups: groups.size, linked, exempt, violations }
  110. }
  111. /** Run the repository audit as a standalone doc-sync gate. */
  112. function main(): void {
  113. const audit = auditSubsystemPages()
  114. if (audit.violations.length > 0) {
  115. console.error('verify-subsystem-pages: package-group documentation violations found:')
  116. for (const violation of audit.violations) console.error(` ${violation}`)
  117. process.exit(1)
  118. }
  119. console.log(
  120. `verify-subsystem-pages: ${String(audit.groups)} group(s) checked`
  121. + ` (${String(audit.linked)} linked, ${String(audit.exempt)} explicitly exempt), all conform.`,
  122. )
  123. }
  124. if (process.argv[1] && import.meta.filename === resolve(process.argv[1])) main()