verify-client-domain-graph.ts 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /**
  2. * Enforce intra-package domain layering inside `packages/client/*\/src/client/`.
  3. * verify-module-graph covers package-level edges; this gate covers the
  4. * directory level the future package split will land on: domain directories
  5. * may import `contract/` and never each other, and only the assembly point
  6. * (`apply.ts` / `index.ts`) may import across domains.
  7. *
  8. * Layer model (lower may not import higher):
  9. * 0 contract/ shared contract surface (types + slot declarations)
  10. * 1 <domain>/ + service domain implementations (skeleton/, chat/, ...)
  11. * 2 apply.ts, index.ts assembly point and re-export shell
  12. *
  13. * Not yet wired into the gate sequence (loose-gate window); run directly:
  14. * pnpm exec tsx scripts/verify-client-domain-graph.ts
  15. */
  16. import { globSync, readdirSync, readFileSync, statSync } from 'node:fs'
  17. import { join, resolve, sep } from 'node:path'
  18. const root = resolve(import.meta.dirname, '..')
  19. const CLIENT_DIR = join(root, 'packages/client')
  20. /** Directory names treated as the shared contract layer (importable by all). */
  21. const CONTRACT_DIRS = new Set(['contract'])
  22. /** Top-level client files allowed to import across domains (assembly layer). */
  23. const ASSEMBLY_FILES = new Set(['apply.ts', 'index.ts', 'index.tsx'])
  24. interface Violation { file: string; imported: string; reason: string }
  25. /** Recursively list .ts/.tsx files under dir (relative paths). */
  26. function listSources(dir: string): string[] {
  27. return globSync('**/*.{ts,tsx}', { cwd: dir })
  28. .map(rel => rel.split(sep).join('/'))
  29. .filter(rel => !/\.legacy\./.test(rel.slice(rel.lastIndexOf('/') + 1)))
  30. .sort()
  31. }
  32. /** First path segment of a client-relative file, or '' for top-level files. */
  33. function domainOf(rel: string): string {
  34. const ix = rel.indexOf('/')
  35. return ix === -1 ? '' : rel.slice(0, ix)
  36. }
  37. function checkPackage(pkgName: string, clientDir: string): Violation[] {
  38. const violations: Violation[] = []
  39. const files = listSources(clientDir)
  40. for (const rel of files) {
  41. const fromDomain = domainOf(rel)
  42. const isAssembly = fromDomain === '' && ASSEMBLY_FILES.has(rel)
  43. if (isAssembly) continue
  44. const source = readFileSync(join(clientDir, rel), 'utf8')
  45. for (const match of source.matchAll(/from\s+['"](\.[^'"]+)['"]/g)) {
  46. const spec = match[1]
  47. if (spec === undefined) continue
  48. // Resolve the relative specifier against the importing file's directory
  49. // to a client-dir-relative path.
  50. const fromDir = rel.includes('/') ? rel.slice(0, rel.lastIndexOf('/')) : ''
  51. const parts = (fromDir ? fromDir.split('/') : [])
  52. for (const seg of spec.split('/')) {
  53. if (seg === '.') continue
  54. if (seg === '..') parts.pop()
  55. else parts.push(seg)
  56. }
  57. const target = parts.join('/')
  58. if (target.startsWith('..')) continue // out of client dir (package root) — package-level rules govern
  59. const toDomain = domainOf(target)
  60. if (toDomain === '' || CONTRACT_DIRS.has(toDomain)) continue // top-level shared file or contract layer
  61. if (fromDomain === toDomain) continue // inside one domain
  62. violations.push({
  63. file: `${pkgName}/src/client/${rel}`,
  64. imported: spec,
  65. reason: fromDomain === ''
  66. ? `top-level non-assembly file imports domain "${toDomain}" (only apply/index may assemble)`
  67. : `domain "${fromDomain}" imports sibling domain "${toDomain}" (route shared surface through contract/)`,
  68. })
  69. }
  70. }
  71. return violations
  72. }
  73. const violations: Violation[] = []
  74. for (const pkg of readdirSync(CLIENT_DIR)) {
  75. const clientDir = join(CLIENT_DIR, pkg, 'src/client')
  76. try {
  77. if (!statSync(clientDir).isDirectory()) continue
  78. } catch {
  79. // No client half in this package — nothing to layer-check.
  80. continue
  81. }
  82. violations.push(...checkPackage(pkg, clientDir))
  83. }
  84. if (violations.length > 0) {
  85. console.error(`verify-client-domain-graph: ${violations.length} violation(s):`)
  86. for (const v of violations) console.error(` ${v.file} -> ${v.imported}\n ${v.reason}`)
  87. process.exit(1)
  88. }
  89. console.log('verify-client-domain-graph: client domain layering clean.')