gen-config-catalog.ts 41 KB

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