verify-subsystem-pages.ts 5.4 KB

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