gen-config-catalog.ts 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883
  1. /**
  2. * Generate `docs/config-catalog.md` from package entry points, config types,
  3. * JSDoc, and static Schemastery schemas. Every package must classify, referenced
  4. * types must resolve without collisions, and every enumerable schema path must
  5. * exist on the declared config type. External and dynamic shapes stay unknown;
  6. * declared runtime-only fields need not appear in the schema. `--check` verifies
  7. * the committed artifact.
  8. */
  9. import { globSync, readFileSync, writeFileSync } from 'node:fs'
  10. import { dirname, resolve, sep } from 'node:path'
  11. import ts from 'typescript'
  12. import { LINK_MAP } from './gen-cordis-catalog.ts'
  13. import { parseJsDoc, pointer, rawJsDoc } from './jsdoc.ts'
  14. const root = resolve(import.meta.dirname, '..')
  15. const OUT = 'docs/config-catalog.md'
  16. /** The fenced-block info string for pasted config declarations (skipped by
  17. * doc-typecheck, since a lone declaration referencing imports is not
  18. * standalone-compilable). */
  19. const FENCE = 'ts config-catalog'
  20. /** TypeScript/Node global type names a config declaration may reference
  21. * without importing; never treated as unresolved. Extend when a new global
  22. * legitimately appears — the generator hard-errors on unknown names, so an
  23. * omission is loud, not silent. */
  24. const GLOBAL_TYPES = new Set([
  25. 'Array', 'ReadonlyArray', 'Record', 'Partial', 'Required', 'Readonly', 'Pick', 'Omit',
  26. 'Promise', 'Map', 'Set', 'Date', 'Error', 'RegExp', 'Exclude', 'Extract', 'NonNullable',
  27. 'ReturnType', 'Parameters', 'AbortSignal', 'URL', 'Buffer', 'NodeJS', 'Iterable', 'AsyncIterable',
  28. ])
  29. /** How a package classifies for the catalog. */
  30. type Kind = 'config' | 'no-config' | 'seam' | 'library'
  31. /** One name a pasted declaration references but the paste does not contain. */
  32. interface TypeRef {
  33. /** The name as it appears in the pasted text (the local import alias). */
  34. alias: string
  35. /** The name the source module exports it under (pre-alias). */
  36. imported: string
  37. /** The import module specifier (package name or external module). */
  38. specifier: string
  39. }
  40. /** One verbatim declaration paste. */
  41. interface Paste {
  42. /** Full source text: leading JSDoc (when present) through the closing token. */
  43. text: string
  44. /** Source pointer `packages/…/file.ts:line` of the declaration. */
  45. source: string
  46. }
  47. /** One package's catalog entry. */
  48. export interface CatalogEntry {
  49. /** npm package name, e.g. `@deepseek-ai/dsh-agent-loop`. */
  50. pkg: string
  51. /** Repo-relative package dir, e.g. `packages/core/agent-loop`. */
  52. dir: string
  53. /** Repo-relative entry file, `<dir>/src/index.ts`. */
  54. entry: string
  55. kind: Kind
  56. /** Service keys the plugin `inject`s (empty when none declared). */
  57. inject: string[]
  58. /** Seam/service class name (kinds `seam` and class-based plugins). */
  59. className?: string
  60. /** Name of the config type (kind `config`). */
  61. configTypeName?: string
  62. /** Verbatim declaration pastes, the config type first (kind `config`). */
  63. pastes?: Paste[]
  64. /** References the pastes leave unresolved locally (kind `config`). */
  65. refs?: TypeRef[]
  66. /** Top-level keys and nested key paths (`agents[].id`) of the runtime
  67. * schema, `null` when no schema exists (kind `config`). */
  68. schemaKeys?: string[] | null
  69. /** Package names whose schemas an intersect composes (kind `config`). */
  70. schemaComposes?: string[]
  71. }
  72. /** A parsed source file plus its import map (local name → origin). */
  73. interface FileCtx {
  74. abs: string
  75. rel: string
  76. text: string
  77. sf: ts.SourceFile
  78. /** Local binding name → `{ imported, specifier }`; default imports record
  79. * `imported: 'default'`. */
  80. imports: Map<string, { imported: string; specifier: string }>
  81. }
  82. /** Throw one aggregate error for every violation the walk collected. */
  83. function report(violations: string[]): void {
  84. if (violations.length === 0) return
  85. throw new Error(
  86. `gen-config-catalog: ${violations.length} violation(s):\n`
  87. + violations.map(v => ` ${v}`).join('\n'),
  88. )
  89. }
  90. /** Parse a source file and index its import declarations. */
  91. function loadFile(abs: string, rel: string, cache: Map<string, FileCtx>): FileCtx {
  92. const cached = cache.get(abs)
  93. if (cached) return cached
  94. const text = readFileSync(abs, 'utf8')
  95. const sf = ts.createSourceFile(abs, text, ts.ScriptTarget.Latest, true)
  96. const imports = new Map<string, { imported: string; specifier: string }>()
  97. for (const stmt of sf.statements) {
  98. if (!ts.isImportDeclaration(stmt) || !ts.isStringLiteral(stmt.moduleSpecifier)) continue
  99. const specifier = stmt.moduleSpecifier.text
  100. const clause = stmt.importClause
  101. if (!clause) continue
  102. if (clause.name) imports.set(clause.name.text, { imported: 'default', specifier })
  103. if (clause.namedBindings && ts.isNamedImports(clause.namedBindings)) {
  104. for (const el of clause.namedBindings.elements) {
  105. imports.set(el.name.text, { imported: (el.propertyName ?? el.name).text, specifier })
  106. }
  107. }
  108. if (clause.namedBindings && ts.isNamespaceImport(clause.namedBindings)) {
  109. imports.set(clause.namedBindings.name.text, { imported: '*', specifier })
  110. }
  111. }
  112. const ctx = { abs, rel, text, sf, imports }
  113. cache.set(abs, ctx)
  114. return ctx
  115. }
  116. /** A type declaration a paste can contain. */
  117. type TypeDecl = ts.InterfaceDeclaration | ts.TypeAliasDeclaration
  118. /** Find an interface/type-alias declaration by name in a file, or null. */
  119. function findTypeDecl(ctx: FileCtx, name: string): TypeDecl | null {
  120. for (const stmt of ctx.sf.statements) {
  121. if ((ts.isInterfaceDeclaration(stmt) || ts.isTypeAliasDeclaration(stmt)) && stmt.name.text === name) return stmt
  122. }
  123. return null
  124. }
  125. /**
  126. * Resolve a type name from a file to its declaration (following package-local
  127. * relative imports transitively) or to the import that brings it in. Returns
  128. * `null` when the name is neither declared, imported, nor a known global.
  129. */
  130. function resolveTypeName(
  131. ctx: FileCtx,
  132. name: string,
  133. cache: Map<string, FileCtx>,
  134. violations: string[],
  135. ): { decl: TypeDecl; ctx: FileCtx } | { ref: TypeRef } | null {
  136. const local = findTypeDecl(ctx, name)
  137. if (local) return { decl: local, ctx }
  138. const imp = ctx.imports.get(name)
  139. if (!imp) return null
  140. if (imp.specifier.startsWith('.')) {
  141. if (!imp.specifier.endsWith('.ts')) {
  142. violations.push(`${ctx.rel}: relative import '${imp.specifier}' lacks the explicit .ts extension the repo convention requires.`)
  143. return null
  144. }
  145. if (imp.imported !== name) {
  146. violations.push(`${ctx.rel}: '${name}' aliases '${imp.imported}' across a package-local import; the catalog pastes declarations verbatim, so keep package-local config types unaliased.`)
  147. return null
  148. }
  149. const abs = resolve(dirname(ctx.abs), imp.specifier)
  150. const rel = ctx.rel.slice(0, ctx.rel.lastIndexOf('/') + 1) + imp.specifier.replace(/^\.\//, '')
  151. const target = loadFile(abs, rel, cache)
  152. return resolveTypeName(target, imp.imported, cache, violations)
  153. }
  154. return { ref: { alias: name, imported: imp.imported, specifier: imp.specifier } }
  155. }
  156. /** Collect every type NAME referenced in type positions under a node. */
  157. function collectTypeNames(node: ts.Node, out: Set<string>): void {
  158. const visit = (n: ts.Node): void => {
  159. if (ts.isTypeReferenceNode(n)) {
  160. let head: ts.EntityName = n.typeName
  161. while (ts.isQualifiedName(head)) head = head.left
  162. out.add(head.text)
  163. } else if (ts.isExpressionWithTypeArguments(n) && ts.isIdentifier(n.expression)) {
  164. out.add(n.expression.text) // heritage clause: `extends X`
  165. }
  166. ts.forEachChild(n, visit)
  167. }
  168. visit(node)
  169. }
  170. /** The verbatim paste text of a declaration: leading JSDoc through the end. */
  171. function pasteText(ctx: FileCtx, decl: TypeDecl): string {
  172. const raw = rawJsDoc(ctx.text, decl)
  173. const start = raw ? ctx.text.indexOf(raw, decl.getFullStart()) : decl.getStart(ctx.sf)
  174. return ctx.text.slice(start, decl.end)
  175. }
  176. /** Enforce non-empty JSDoc prose on every property of a pasted declaration,
  177. * recursing into nested type literals (e.g. an array-of-objects field). */
  178. function checkMemberDocs(ctx: FileCtx, decl: TypeDecl, violations: string[]): void {
  179. const walkMembers = (members: ts.NodeArray<ts.TypeElement>, path: string): void => {
  180. for (const member of members) {
  181. if (!ts.isPropertySignature(member)) continue
  182. const name = member.name.getText(ctx.sf)
  183. const where = `config field '${path}.${name}' (${pointer(ctx.rel, ctx.sf, member)})`
  184. if (!parseJsDoc(rawJsDoc(ctx.text, member)).doc) violations.push(`${where} has no JSDoc prose.`)
  185. if (member.type) walkNested(member.type, `${path}.${name}`)
  186. }
  187. }
  188. const walkNested = (type: ts.Node, path: string): void => {
  189. if (ts.isTypeLiteralNode(type)) walkMembers(type.members, path)
  190. else ts.forEachChild(type, (n) => { walkNested(n, path) })
  191. }
  192. if (ts.isInterfaceDeclaration(decl)) walkMembers(decl.members, decl.name.text)
  193. else walkNested(decl.type, decl.name.text)
  194. }
  195. /** Cross-file resolution context for the schema-path check. */
  196. interface World {
  197. scanRoot: string
  198. cache: Map<string, FileCtx>
  199. /** Workspace package name → repo-relative package dir. */
  200. pkgDirByName: Map<string, string>
  201. }
  202. /** How a schema key path fared against the declared config type: definitely
  203. * present, definitely absent, or crossing a shape the walk cannot enumerate
  204. * (only `missing` is a violation — `unknown` must never mis-report). */
  205. type PathLookup = 'found' | 'missing' | 'unknown'
  206. /** One step of a schema key path: a named member, or an array-element hop. */
  207. type PathStep = { member: string } | { array: true }
  208. /** Parse a schema key path (`agents[].id`) into member/array steps. */
  209. function parsePath(path: string): PathStep[] {
  210. const steps: PathStep[] = []
  211. for (const seg of path.split('.')) {
  212. let name = seg
  213. let arrays = 0
  214. while (name.endsWith('[]')) {
  215. name = name.slice(0, -2)
  216. arrays += 1
  217. }
  218. steps.push({ member: name })
  219. for (let i = 0; i < arrays; i += 1) steps.push({ array: true })
  220. }
  221. return steps
  222. }
  223. /** Load a package-relative import target as a FileCtx. */
  224. function loadRelative(world: World, from: FileCtx, specifier: string): FileCtx {
  225. const abs = resolve(dirname(from.abs), specifier)
  226. const rel = from.rel.slice(0, from.rel.lastIndexOf('/') + 1) + specifier.replace(/^\.\//, '')
  227. return loadFile(abs, rel, world.cache)
  228. }
  229. /** Find a type declaration EXPORTED (directly or via re-export chains) from a
  230. * file, following `export … from './x.ts'` and `export * from './x.ts'`. */
  231. function findExportedTypeDecl(world: World, ctx: FileCtx, name: string, seen = new Set<string>()): { decl: TypeDecl; ctx: FileCtx } | null {
  232. const key = `${ctx.abs}#${name}`
  233. if (seen.has(key)) return null
  234. seen.add(key)
  235. const local = findTypeDecl(ctx, name)
  236. if (local) return { decl: local, ctx }
  237. for (const stmt of ctx.sf.statements) {
  238. if (!ts.isExportDeclaration(stmt) || !stmt.moduleSpecifier || !ts.isStringLiteral(stmt.moduleSpecifier)) continue
  239. const spec = stmt.moduleSpecifier.text
  240. if (!spec.startsWith('.') || !spec.endsWith('.ts')) continue
  241. let lookFor: string | null = null
  242. if (!stmt.exportClause) {
  243. lookFor = name // export * from './x.ts'
  244. } else if (ts.isNamedExports(stmt.exportClause)) {
  245. const el = stmt.exportClause.elements.find(e => e.name.text === name)
  246. if (el) lookFor = (el.propertyName ?? el.name).text
  247. }
  248. if (lookFor === null) continue
  249. const hit = findExportedTypeDecl(world, loadRelative(world, ctx, spec), lookFor, seen)
  250. if (hit) return hit
  251. }
  252. return null
  253. }
  254. /** Resolve a referenced type NAME to its declaration: declared locally, via a
  255. * package-relative import, or via a workspace-package import (entry file +
  256. * re-export chains). `'unknown'` = external or otherwise out of reach. */
  257. function declForTypeName(world: World, ctx: FileCtx, name: string): { decl: TypeDecl; ctx: FileCtx } | 'unknown' {
  258. const local = findTypeDecl(ctx, name)
  259. if (local) return { decl: local, ctx }
  260. const imp = ctx.imports.get(name)
  261. if (!imp) return 'unknown'
  262. if (imp.specifier.startsWith('.')) {
  263. if (!imp.specifier.endsWith('.ts')) return 'unknown'
  264. return findExportedTypeDecl(world, loadRelative(world, ctx, imp.specifier), imp.imported) ?? 'unknown'
  265. }
  266. const dir = world.pkgDirByName.get(imp.specifier)
  267. if (dir === undefined) return 'unknown'
  268. const entryRel = `${dir}/src/index.ts`
  269. let entry: FileCtx
  270. try {
  271. entry = loadFile(resolve(world.scanRoot, entryRel), entryRel, world.cache)
  272. } catch {
  273. // A workspace package without a readable entry is reported by its own
  274. // classification pass; for a lookup it is out of reach.
  275. return 'unknown'
  276. }
  277. return findExportedTypeDecl(world, entry, imp.imported) ?? 'unknown'
  278. }
  279. /** Utility wrappers that pass a member lookup through to their type argument. */
  280. const PASSTHROUGH_WRAPPERS = new Set(['Partial', 'Required', 'Readonly', 'NonNullable'])
  281. /**
  282. * Walk a schema key path against a declared type. This is a PRESENCE check,
  283. * not a shape check: it answers "does the declared config type have a member
  284. * here", resolving interfaces (heritage included), type aliases, literals,
  285. * intersections, unions, arrays, indexed access, pass-through utility
  286. * wrappers, and type references across package-local and workspace imports.
  287. * Anything it cannot see through resolves `'unknown'`, never `'missing'`.
  288. */
  289. function lookupPath(world: World, ctx: FileCtx, node: ts.Node, steps: PathStep[], seen: Set<string>): PathLookup {
  290. if (steps.length === 0) return 'found'
  291. // Guard only named declarations, where recursive types can loop. Structural
  292. // children can share a source position with their parent, so guarding them
  293. // would mistake ordinary descent for a cycle.
  294. if (ts.isInterfaceDeclaration(node) || ts.isTypeAliasDeclaration(node)) {
  295. const key = `${ctx.abs}:${node.pos}:${steps.length}`
  296. if (seen.has(key)) return 'unknown' // recursive type — bail rather than loop
  297. seen.add(key)
  298. }
  299. const step = steps[0]
  300. if (step === undefined) return 'found'
  301. // Combine branch results: any found wins, else any unknown taints, else missing.
  302. const combine = (results: PathLookup[]): PathLookup => {
  303. if (results.includes('found')) return 'found'
  304. if (results.includes('unknown')) return 'unknown'
  305. return 'missing'
  306. }
  307. const intoMembers = (members: ts.NodeArray<ts.TypeElement>): PathLookup | null => {
  308. if (!('member' in step)) return null
  309. for (const m of members) {
  310. if (!ts.isPropertySignature(m) || m.name.getText(ctx.sf) !== step.member) continue
  311. if (steps.length === 1) return 'found'
  312. return m.type ? lookupPath(world, ctx, m.type, steps.slice(1), seen) : 'unknown'
  313. }
  314. return null // not among these members; caller consults heritage/parts
  315. }
  316. if (ts.isInterfaceDeclaration(node)) {
  317. if (!('member' in step)) return 'unknown' // an array step cannot land on an interface
  318. const direct = intoMembers(node.members)
  319. if (direct !== null) return direct
  320. const bases: PathLookup[] = []
  321. for (const clause of node.heritageClauses ?? []) {
  322. for (const base of clause.types) {
  323. if (!ts.isIdentifier(base.expression)) {
  324. bases.push('unknown')
  325. continue
  326. }
  327. const resolved = declForTypeName(world, ctx, base.expression.text)
  328. bases.push(resolved === 'unknown' ? 'unknown' : lookupPath(world, resolved.ctx, resolved.decl, steps, seen))
  329. }
  330. }
  331. return bases.length ? combine(bases) : 'missing'
  332. }
  333. if (ts.isTypeAliasDeclaration(node)) return lookupPath(world, ctx, node.type, steps, seen)
  334. if (ts.isTypeLiteralNode(node)) {
  335. if (!('member' in step)) return 'unknown'
  336. return intoMembers(node.members) ?? 'missing'
  337. }
  338. if (ts.isParenthesizedTypeNode(node)) return lookupPath(world, ctx, node.type, steps, seen)
  339. if (ts.isIntersectionTypeNode(node)) {
  340. return combine(node.types.map(t => lookupPath(world, ctx, t, steps, seen)))
  341. }
  342. if (ts.isUnionTypeNode(node)) {
  343. // Presence on a union is only definite when every branch agrees.
  344. const results = node.types.map(t => lookupPath(world, ctx, t, steps, seen))
  345. if (results.every(r => r === 'found')) return 'found'
  346. if (results.every(r => r === 'missing')) return 'missing'
  347. return 'unknown'
  348. }
  349. if (ts.isArrayTypeNode(node)) {
  350. return 'array' in step ? lookupPath(world, ctx, node.elementType, steps.slice(1), seen) : 'unknown'
  351. }
  352. if (ts.isTypeOperatorNode(node)) return lookupPath(world, ctx, node.type, steps, seen)
  353. if (ts.isIndexedAccessTypeNode(node)) {
  354. const index = node.indexType
  355. if (ts.isLiteralTypeNode(index) && ts.isStringLiteral(index.literal)) {
  356. return lookupPath(world, ctx, node.objectType, [{ member: index.literal.text }, ...steps], seen)
  357. }
  358. return 'unknown'
  359. }
  360. if (ts.isTypeReferenceNode(node)) {
  361. let head: ts.EntityName = node.typeName
  362. while (ts.isQualifiedName(head)) head = head.left
  363. const name = head.text
  364. if (PASSTHROUGH_WRAPPERS.has(name) && node.typeArguments?.[0]) {
  365. return lookupPath(world, ctx, node.typeArguments[0], steps, seen)
  366. }
  367. if ((name === 'Array' || name === 'ReadonlyArray') && node.typeArguments?.[0]) {
  368. return 'array' in step ? lookupPath(world, ctx, node.typeArguments[0], steps.slice(1), seen) : 'unknown'
  369. }
  370. if (!ts.isIdentifier(node.typeName)) return 'unknown' // namespace-qualified: out of reach
  371. const resolved = declForTypeName(world, ctx, name)
  372. return resolved === 'unknown' ? 'unknown' : lookupPath(world, resolved.ctx, resolved.decl, steps, seen)
  373. }
  374. return 'unknown'
  375. }
  376. /** Unwrap `as` / `satisfies` / parenthesized wrappers around an expression. */
  377. function unwrapExpr(expr: ts.Expression): ts.Expression {
  378. let e = expr
  379. while (ts.isAsExpression(e) || ts.isSatisfiesExpression(e) || ts.isParenthesizedExpression(e)) e = e.expression
  380. return e
  381. }
  382. /**
  383. * Statically walk a schemastery schema expression to its key paths plus the
  384. * packages whose schemas an intersect composes. A key path is the top-level
  385. * key or a nested path through object/array compositions (`agents[].id`).
  386. * Handles the shapes the repo declares — `z.object({…})` (possibly behind
  387. * chained calls) and `z.intersect([X.Config, …])` — and hard-errors on
  388. * anything else, so a schema the walk cannot see fails the gate instead of
  389. * silently thinning it. Nested values that are neither `object` nor `array`
  390. * compositions (primitives, unions, dynamic-key dicts) contribute no paths.
  391. */
  392. function walkSchemaExpr(
  393. ctx: FileCtx,
  394. expr: ts.Expression,
  395. where: string,
  396. violations: string[],
  397. ): { keys: string[]; composes: string[] } {
  398. const keys: string[] = []
  399. const composes: string[] = []
  400. // Nested paths under one object property's VALUE expression: recurse through
  401. // chained refinements toward the base call, descending into object/array.
  402. const collectValuePaths = (value: ts.Expression, base: string): void => {
  403. const call = unwrapExpr(value)
  404. if (!ts.isCallExpression(call) || !ts.isPropertyAccessExpression(call.expression)) return
  405. const method = call.expression.name.text
  406. if (method === 'object' && call.arguments[0] && ts.isObjectLiteralExpression(call.arguments[0])) {
  407. for (const prop of call.arguments[0].properties) {
  408. if (!ts.isPropertyAssignment(prop)) continue
  409. const key = ts.isStringLiteral(prop.name) ? prop.name.text : prop.name.getText(ctx.sf)
  410. keys.push(`${base}.${key}`)
  411. collectValuePaths(prop.initializer, `${base}.${key}`)
  412. }
  413. return
  414. }
  415. if (method === 'array' && call.arguments[0]) {
  416. collectValuePaths(call.arguments[0], `${base}[]`)
  417. return
  418. }
  419. const inner = unwrapExpr(call.expression.expression)
  420. if (ts.isCallExpression(inner)) collectValuePaths(inner, base)
  421. }
  422. const visit = (e: ts.Expression): void => {
  423. const call = unwrapExpr(e)
  424. if (!ts.isCallExpression(call) || !ts.isPropertyAccessExpression(call.expression)) {
  425. violations.push(`${where}: schema expression is not a statically walkable schemastery call.`)
  426. return
  427. }
  428. const method = call.expression.name.text
  429. if (method === 'object' && call.arguments[0] && ts.isObjectLiteralExpression(call.arguments[0])) {
  430. for (const prop of call.arguments[0].properties) {
  431. if (ts.isPropertyAssignment(prop) || ts.isShorthandPropertyAssignment(prop)) {
  432. const key = ts.isStringLiteral(prop.name) ? prop.name.text : prop.name.getText(ctx.sf)
  433. keys.push(key)
  434. if (ts.isPropertyAssignment(prop)) collectValuePaths(prop.initializer, key)
  435. } else {
  436. violations.push(`${where}: schema object property '${prop.getText(ctx.sf)}' is not a plain key.`)
  437. }
  438. }
  439. return
  440. }
  441. if (method === 'intersect' && call.arguments[0] && ts.isArrayLiteralExpression(call.arguments[0])) {
  442. for (const el of call.arguments[0].elements) {
  443. const part = unwrapExpr(el)
  444. if (ts.isPropertyAccessExpression(part) && part.name.text === 'Config' && ts.isIdentifier(part.expression)) {
  445. const imp = ctx.imports.get(part.expression.text)
  446. if (imp && !imp.specifier.startsWith('.')) { composes.push(imp.specifier); continue }
  447. }
  448. if (ts.isCallExpression(part)) { visit(part); continue }
  449. violations.push(`${where}: intersect element '${part.getText(ctx.sf)}' is neither a workspace plugin's Config nor an inline schema call.`)
  450. }
  451. return
  452. }
  453. // A union of objects (discriminated union config): collect keys from all
  454. // variants. Each variant is visited the same way as an intersect element.
  455. if (method === 'union' && call.arguments[0] && ts.isArrayLiteralExpression(call.arguments[0])) {
  456. for (const el of call.arguments[0].elements) {
  457. const part = unwrapExpr(el)
  458. if (ts.isCallExpression(part)) { visit(part); continue }
  459. }
  460. return
  461. }
  462. // A chained refinement (`z.object({…}).default(…)` etc.): the keys live on
  463. // the call the chain hangs off — keep unwrapping toward it.
  464. const base = unwrapExpr(call.expression.expression)
  465. if (ts.isCallExpression(base)) { visit(base); return }
  466. violations.push(`${where}: schema call '${method}' is not object/intersect and hangs off no walkable base call.`)
  467. }
  468. visit(expr)
  469. return { keys, composes }
  470. }
  471. /** Find a plugin's schemastery schema expression: an exported `const Config`
  472. * in the entry file, else a `static Config` on the plugin class. */
  473. function findSchemaExpr(ctx: FileCtx, pluginClass: ts.ClassDeclaration | null): ts.Expression | null {
  474. for (const stmt of ctx.sf.statements) {
  475. if (!ts.isVariableStatement(stmt)) continue
  476. if (!stmt.modifiers?.some(m => m.kind === ts.SyntaxKind.ExportKeyword)) continue
  477. for (const decl of stmt.declarationList.declarations) {
  478. if (ts.isIdentifier(decl.name) && decl.name.text === 'Config' && decl.initializer) return decl.initializer
  479. }
  480. }
  481. for (const member of pluginClass?.members ?? []) {
  482. if (!ts.isPropertyDeclaration(member) || member.name.getText() !== 'Config') continue
  483. if (!member.modifiers?.some(m => m.kind === ts.SyntaxKind.StaticKeyword)) continue
  484. if (member.initializer) return member.initializer
  485. }
  486. return null
  487. }
  488. /** Read an `inject` service-key list: `export const inject = […]` in the entry
  489. * file, else `static inject = […]` on the plugin class. */
  490. function findInject(ctx: FileCtx, pluginClass: ts.ClassDeclaration | null, violations: string[]): string[] {
  491. const fromArray = (expr: ts.Expression, where: string): string[] => {
  492. if (!ts.isArrayLiteralExpression(expr)) {
  493. violations.push(`${where}: inject is not a plain string-array literal; teach the generator the new shape.`)
  494. return []
  495. }
  496. return expr.elements.map(el => ts.isStringLiteral(el) ? el.text : el.getText(ctx.sf))
  497. }
  498. for (const stmt of ctx.sf.statements) {
  499. if (!ts.isVariableStatement(stmt)) continue
  500. for (const decl of stmt.declarationList.declarations) {
  501. if (ts.isIdentifier(decl.name) && decl.name.text === 'inject' && decl.initializer) {
  502. return fromArray(decl.initializer, ctx.rel)
  503. }
  504. }
  505. }
  506. for (const member of pluginClass?.members ?? []) {
  507. if (ts.isPropertyDeclaration(member) && member.name.getText() === 'inject' && member.initializer) {
  508. return fromArray(member.initializer, ctx.rel)
  509. }
  510. }
  511. return []
  512. }
  513. /** Resolve the entry file's default export to its class/function declaration
  514. * (mirroring the Loader's `unwrapExports`), or null when there is none. */
  515. function defaultExport(ctx: FileCtx): ts.ClassDeclaration | ts.FunctionDeclaration | null {
  516. for (const stmt of ctx.sf.statements) {
  517. if (ts.isExportAssignment(stmt) && !stmt.isExportEquals && ts.isIdentifier(stmt.expression)) {
  518. const name = stmt.expression.text
  519. for (const s of ctx.sf.statements) {
  520. if ((ts.isClassDeclaration(s) || ts.isFunctionDeclaration(s)) && s.name?.text === name) return s
  521. }
  522. return null
  523. }
  524. if ((ts.isClassDeclaration(stmt) || ts.isFunctionDeclaration(stmt))
  525. && stmt.modifiers?.some(m => m.kind === ts.SyntaxKind.DefaultKeyword)) return stmt
  526. }
  527. return null
  528. }
  529. /** Find the exported `apply` function declaration in the entry file, or null. */
  530. function applyExport(ctx: FileCtx): ts.FunctionDeclaration | null {
  531. for (const stmt of ctx.sf.statements) {
  532. if (ts.isFunctionDeclaration(stmt) && stmt.name?.text === 'apply'
  533. && stmt.modifiers?.some(m => m.kind === ts.SyntaxKind.ExportKeyword)) return stmt
  534. }
  535. return null
  536. }
  537. /**
  538. * Walk every `packages/<group>/<pkg>` entry and build the catalog entries.
  539. * Hard-errors (aggregated) on any violation listed in the module doc.
  540. * `scanRoot` defaults to the repo root; tests pass a fixture dir.
  541. */
  542. export function collectConfigCatalog(scanRoot: string = root): CatalogEntry[] {
  543. const violations: string[] = []
  544. const cache = new Map<string, FileCtx>()
  545. const entries: CatalogEntry[] = []
  546. // Pre-pass: package name → dir, so schema-path lookups can follow
  547. // workspace-package imports while individual packages are still being walked.
  548. const pkgDirByName = new Map<string, string>()
  549. const manifests: { dir: string; pkg: string }[] = []
  550. for (const manifestRel of globSync('packages/*/*/package.json', { cwd: scanRoot }).map(path => path.split(sep).join('/')).sort()) {
  551. const dir = manifestRel.slice(0, -'/package.json'.length)
  552. const manifest = JSON.parse(readFileSync(resolve(scanRoot, manifestRel), 'utf8')) as { name?: string; os?: string[]; cpu?: string[] }
  553. const pkg = manifest.name
  554. if (!pkg) {
  555. violations.push(`${manifestRel} has no "name".`)
  556. continue
  557. }
  558. if (manifest.os !== undefined && manifest.cpu !== undefined) {
  559. // A per-platform native-binary package (npm os/cpu selection) ships no
  560. // JavaScript at all — nothing to classify, no Config to catalog.
  561. continue
  562. }
  563. pkgDirByName.set(pkg, dir)
  564. manifests.push({ dir, pkg })
  565. }
  566. const world: World = { scanRoot, cache, pkgDirByName }
  567. for (const { dir, pkg } of manifests) {
  568. const entryRel = `${dir}/src/index.ts`
  569. let ctx: FileCtx
  570. try {
  571. ctx = loadFile(resolve(scanRoot, entryRel), entryRel, cache)
  572. } catch {
  573. // A package without src/index.ts cannot be classified — that is the
  574. // violation itself; nothing else in this loop body can run without it.
  575. violations.push(`${pkg}: entry ${entryRel} is missing or unreadable.`)
  576. continue
  577. }
  578. // Classify, mirroring the Loader's unwrapExports: the default export IS
  579. // the plugin when present; else an exported `apply` makes the module
  580. // namespace the plugin; else the package is a plain library.
  581. const dflt = defaultExport(ctx)
  582. const apply = applyExport(ctx)
  583. let pluginClass: ts.ClassDeclaration | null = null
  584. let configParam: ts.ParameterDeclaration | undefined
  585. let kind: Kind
  586. let className: string | undefined
  587. if (dflt && ts.isClassDeclaration(dflt)) {
  588. className = dflt.name?.text
  589. if (dflt.modifiers?.some(m => m.kind === ts.SyntaxKind.AbstractKeyword)) {
  590. kind = 'seam'
  591. } else {
  592. pluginClass = dflt
  593. const ctor = dflt.members.find(ts.isConstructorDeclaration)
  594. configParam = ctor?.parameters[1]
  595. kind = configParam ? 'config' : 'no-config'
  596. }
  597. } else if (dflt) {
  598. configParam = dflt.parameters[1]
  599. kind = configParam ? 'config' : 'no-config'
  600. } else if (apply) {
  601. configParam = apply.parameters[1]
  602. kind = configParam ? 'config' : 'no-config'
  603. } else {
  604. kind = 'library'
  605. }
  606. const entry: CatalogEntry = {
  607. pkg,
  608. dir,
  609. entry: entryRel,
  610. kind,
  611. inject: kind === 'library' || kind === 'seam' ? [] : findInject(ctx, pluginClass, violations),
  612. ...className !== undefined ? { className } : {},
  613. }
  614. entries.push(entry)
  615. if (kind !== 'config' || !configParam) continue
  616. // Resolve the config type and paste its package-local transitive closure.
  617. if (!configParam.type || !ts.isTypeReferenceNode(configParam.type) || !ts.isIdentifier(configParam.type.typeName)) {
  618. violations.push(`${pkg}: config parameter type (${pointer(entryRel, ctx.sf, configParam)}) is not a plain type-name reference; declare a named config type.`)
  619. continue
  620. }
  621. const typeName = configParam.type.typeName.text
  622. entry.configTypeName = typeName
  623. const pastes: Paste[] = []
  624. const refs = new Map<string, TypeRef>()
  625. // A bare name is the fence's whole namespace: two DIFFERENT declarations
  626. // (or a declaration in one file and an import in another) sharing a name
  627. // cannot both render unambiguously, so every resolution is identity-checked
  628. // by source pointer and a collision is a violation, never a silent skip.
  629. const pastedDeclByName = new Map<string, string>()
  630. const queue: { name: string; from: FileCtx }[] = [{ name: typeName, from: ctx }]
  631. for (let item = queue.shift(); item !== undefined; item = queue.shift()) {
  632. const { name, from } = item
  633. const resolved = resolveTypeName(from, name, cache, violations)
  634. if (resolved === null) {
  635. violations.push(`${pkg}: config declaration references '${name}' (via ${from.rel}), which is neither declared in the package, imported, nor a known global type.`)
  636. continue
  637. }
  638. if ('ref' in resolved) {
  639. if (name === typeName) {
  640. violations.push(`${pkg}: config type '${name}' is imported from '${resolved.ref.specifier}'; a plugin's config type must live in its own package.`)
  641. continue
  642. }
  643. if (pastedDeclByName.has(name)) {
  644. violations.push(`${pkg}: '${name}' resolves to a package-local declaration (${pastedDeclByName.get(name) ?? ''}) in one file and an import from '${resolved.ref.specifier}' in another; rename one so the fence is unambiguous.`)
  645. continue
  646. }
  647. const existing = refs.get(name)
  648. if (existing && (existing.specifier !== resolved.ref.specifier || existing.imported !== resolved.ref.imported)) {
  649. violations.push(`${pkg}: '${name}' is imported from both '${existing.specifier}' (${existing.imported}) and '${resolved.ref.specifier}' (${resolved.ref.imported}) across the pasted closure; disambiguate the aliases.`)
  650. continue
  651. }
  652. refs.set(name, resolved.ref)
  653. continue
  654. }
  655. const declKey = pointer(resolved.ctx.rel, resolved.ctx.sf, resolved.decl)
  656. const prior = pastedDeclByName.get(name)
  657. if (prior === declKey) continue // same declaration reached again — benign
  658. if (prior !== undefined) {
  659. violations.push(`${pkg}: type name '${name}' resolves to two different declarations (${prior} and ${declKey}) across the pasted closure; rename one — a verbatim fence cannot carry two same-named declarations.`)
  660. continue
  661. }
  662. if (refs.has(name)) {
  663. violations.push(`${pkg}: '${name}' resolves to an import from '${refs.get(name)?.specifier ?? ''}' in one file and a package-local declaration (${declKey}) in another; rename one so the fence is unambiguous.`)
  664. continue
  665. }
  666. pastedDeclByName.set(name, declKey)
  667. pastes.push({ text: pasteText(resolved.ctx, resolved.decl), source: declKey })
  668. checkMemberDocs(resolved.ctx, resolved.decl, violations)
  669. const names = new Set<string>()
  670. collectTypeNames(resolved.decl, names)
  671. for (const n of names) {
  672. if (GLOBAL_TYPES.has(n)) continue
  673. queue.push({ name: n, from: resolved.ctx })
  674. }
  675. }
  676. entry.pastes = pastes
  677. entry.refs = [...refs.values()].sort((a, b) => a.alias.localeCompare(b.alias))
  678. // Statically walk the runtime schema (when one exists) for the subset check.
  679. const schemaExpr = findSchemaExpr(ctx, pluginClass)
  680. if (schemaExpr) {
  681. const { keys, composes } = walkSchemaExpr(ctx, unwrapExpr(schemaExpr), `${pkg} (${entryRel})`, violations)
  682. entry.schemaKeys = keys
  683. entry.schemaComposes = composes
  684. } else {
  685. entry.schemaKeys = null
  686. }
  687. }
  688. // Fold composed schemas' key paths in, then check each path against the type.
  689. // Only a definite miss fails; shapes the walk cannot enumerate stay unknown.
  690. const byName = new Map(entries.map(e => [e.pkg, e]))
  691. for (const entry of entries) {
  692. if (entry.kind !== 'config' || entry.schemaKeys === null || entry.schemaKeys === undefined) continue
  693. const seen = new Set<string>()
  694. const foldComposed = (e: CatalogEntry): string[] => {
  695. if (seen.has(e.pkg)) return []
  696. seen.add(e.pkg)
  697. const keys = [...e.schemaKeys ?? []]
  698. for (const composed of e.schemaComposes ?? []) {
  699. const target = byName.get(composed)
  700. if (!target) {
  701. violations.push(`${entry.pkg}: schema intersects '${composed}', which is not a workspace package the walk collected.`)
  702. continue
  703. }
  704. keys.push(...foldComposed(target))
  705. }
  706. return keys
  707. }
  708. const allKeys = foldComposed(entry)
  709. const mainPaste = entry.pastes?.[0]
  710. const mainFile = mainPaste?.source.split(':')[0]
  711. const mainCtx = mainFile !== undefined ? cache.get(resolve(scanRoot, mainFile)) : undefined
  712. const mainDecl = mainCtx && entry.configTypeName !== undefined ? findTypeDecl(mainCtx, entry.configTypeName) : null
  713. if (!mainCtx || !mainDecl) {
  714. violations.push(`${entry.pkg}: cannot locate config type '${entry.configTypeName ?? ''}' for the schema-path check.`)
  715. continue
  716. }
  717. for (const keyPath of allKeys) {
  718. if (lookupPath(world, mainCtx, mainDecl, parsePath(keyPath), new Set()) === 'missing') {
  719. violations.push(`${entry.pkg}: schema validates key '${keyPath}' but config type '${entry.configTypeName ?? ''}' declares no such member — the catalog paste would hide a loader-accepted field.`)
  720. }
  721. }
  722. }
  723. report(violations)
  724. return entries.sort((a, b) => a.pkg.localeCompare(b.pkg))
  725. }
  726. /** GitHub-style anchor slug for a `## \`pkg\`` heading. */
  727. function slug(heading: string): string {
  728. return heading.toLowerCase().replace(/[^a-z0-9 -]/g, '').replace(/ /g, '-')
  729. }
  730. /** Render the `Requires:` service-key line, or '' when the plugin injects nothing. */
  731. function requiresLine(inject: string[]): string {
  732. return inject.length ? `Requires: ${inject.map(k => `\`${k}\``).join(' · ')}` : ''
  733. }
  734. /** Render one reference as a link: another plugin's config type → its section,
  735. * a curated core-data-structures name → its page, any other workspace type →
  736. * its source file, an external type → named with its module, unlinked. */
  737. function refLink(ref: TypeRef, byName: Map<string, CatalogEntry>): string {
  738. const target = byName.get(ref.specifier)
  739. if (target?.kind === 'config' && ref.imported === target.configTypeName) {
  740. return `[\`${ref.alias}\`](#${slug(target.pkg)})`
  741. }
  742. const page = LINK_MAP[ref.imported]
  743. if (page) return `[\`${ref.alias}\`](core-data-structures/${page})`
  744. if (target) return `[\`${ref.alias}\`](../${target.entry})`
  745. return `\`${ref.alias}\` (\`${ref.specifier}\`)`
  746. }
  747. /** Render one configurable plugin's section. */
  748. function renderConfigEntry(entry: CatalogEntry, byName: Map<string, CatalogEntry>): string[] {
  749. const out = [`## \`${entry.pkg}\``, '']
  750. const requires = requiresLine(entry.inject)
  751. if (requires) out.push(requires, '')
  752. out.push('```' + FENCE, ...(entry.pastes ?? []).map(p => p.text).join('\n\n').split('\n'), '```', '')
  753. if (entry.refs && entry.refs.length > 0) {
  754. out.push(`Depends on: ${entry.refs.map(r => refLink(r, byName)).join(' · ')}`, '')
  755. }
  756. const source = entry.pastes?.[0]?.source ?? entry.entry
  757. out.push(`Source: [\`${source}\`](../${source.split(':')[0]})`, '')
  758. return out
  759. }
  760. /** Render one terse list line (the no-config / seam / library sections). */
  761. function renderTerse(entry: CatalogEntry, detail: string): string {
  762. const requires = entry.inject.length ? ` — requires ${entry.inject.map(k => `\`${k}\``).join(' · ')}` : ''
  763. return `- \`${entry.pkg}\`${detail}${requires} ([\`${entry.entry}\`](../${entry.entry}))`
  764. }
  765. /** Render the full catalog (pure, deterministic given sorted entries). */
  766. export function render(entries: CatalogEntry[]): string {
  767. const byName = new Map(entries.map(e => [e.pkg, e]))
  768. const lines: string[] = [
  769. '<!-- Generated by scripts/gen-config-catalog.ts — do not edit by hand.',
  770. ' Run `pnpm run gen-config-catalog` to regenerate. -->',
  771. '',
  772. '# Plugin Config Catalog',
  773. '',
  774. 'Every `config:` block a `cordis.yml` entry can set: for each loadable harness package, the verbatim config declaration (JSDoc included) its `apply` function or service constructor receives, with every referenced type pasted alongside (package-local types) or linked (everything else). The paste is the plugin\'s full declared config type — a field the runtime schema deliberately excludes is a runtime-only seam (its own JSDoc says so) and is not settable from `cordis.yml`. This is the **deployment**-axis reference — the wiring a plugin author works against is the cordis [events](cordis-catalog/events.md) + [services](cordis-catalog/services.md) catalogs, the model-facing tool schemas are the [tool catalog](tool-catalog.md), and [core-data-structures/](core-data-structures/core.md) documents the types these declarations reference.',
  775. '',
  776. 'This file is GENERATED from source (`scripts/gen-config-catalog.ts`) and verified fresh by `pnpm run verify-config-catalog` (part of `doc-sync`) — do not edit it by hand. Declaration blocks use a `ts config-catalog` fence (skipped by doc-typecheck, since a lone declaration referencing imports is not standalone-compilable). The generator also cross-checks the runtime schemastery schema against the pasted declaration — every schema-validated key, nested keys included, must be locatable on the declared config type — so the paste cannot hide a loader-accepted field.',
  777. '',
  778. 'A `Requires:` line lists the service keys the plugin `inject`s: its `cordis.yml` tree must also load providers for those services. Scope is the harness tier (`packages/`); the vendored cordis plugins a config tree may also load (`hmr`, the console logger, …) are pinned upstream source ([vendoring policy](../vendor/README.md)) and not catalogued here.',
  779. '',
  780. ]
  781. for (const entry of entries.filter(e => e.kind === 'config')) {
  782. lines.push(...renderConfigEntry(entry, byName))
  783. }
  784. lines.push(
  785. '## Loadable plugins with no config',
  786. '',
  787. 'These load from a `cordis.yml` entry with no `config:` block; they declare no config surface.',
  788. '',
  789. ...entries.filter(e => e.kind === 'no-config').map(e => renderTerse(e, '')),
  790. '',
  791. '## Seam packages (not directly loadable)',
  792. '',
  793. 'Abstract service classes — a deployment loads a concrete implementation package instead ([capability seams](../.agents/notes/implemented/architecture/2026-06-13-capability-seams.md)).',
  794. '',
  795. ...entries.filter(e => e.kind === 'seam').map(e => renderTerse(e, ` — abstract \`${e.className ?? ''}\``)),
  796. '',
  797. '## Library packages (no plugin entry)',
  798. '',
  799. 'Imported as libraries by other packages; a `cordis.yml` cannot load them.',
  800. '',
  801. ...entries.filter(e => e.kind === 'library').map(e => renderTerse(e, '')),
  802. '',
  803. )
  804. return lines.join('\n')
  805. }
  806. /** CLI entry: default writes the catalog, `--check` fails if the committed
  807. * copy is stale. Guarded behind an entry-point check so importing this module
  808. * for tests neither regenerates the committed file nor calls process.exit. */
  809. function main(): void {
  810. const content = render(collectConfigCatalog())
  811. if (process.argv.includes('--check')) {
  812. let committed: string | null = null
  813. try {
  814. committed = readFileSync(resolve(root, OUT), 'utf8')
  815. } catch {
  816. // Only ENOENT (not yet generated) is expected; a present-but-unreadable
  817. // file is not a state this repo produces. Either way the remedy is the
  818. // same — regenerate — so treat a read failure as "stale".
  819. committed = null
  820. }
  821. if (committed === content) {
  822. console.log(`gen-config-catalog: ${OUT} is up to date.`)
  823. process.exit(0)
  824. }
  825. console.error(`gen-config-catalog: ${OUT} is stale. Run \`pnpm run gen-config-catalog\` and commit ${OUT}.`)
  826. process.exit(1)
  827. }
  828. writeFileSync(resolve(root, OUT), content)
  829. console.log(`gen-config-catalog: wrote ${OUT}.`)
  830. }
  831. // Run only when invoked as a script, not when imported by a test.
  832. if (process.argv[1] && import.meta.filename === resolve(process.argv[1])) {
  833. main()
  834. }