gen-config-catalog.ts 41 KB

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