verify-subsystem-pages.ts 5.3 KB

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