verify-subsystem-pages.ts 5.7 KB

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