gen-doc-graphs.ts 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  1. /**
  2. * Generate (and verify) the relationship-diagram docs.
  3. *
  4. * This is the relationship layer above the existing catalogs:
  5. * - module-graph.md answers "which packages depend on which packages?"
  6. * - cordis-catalog/ answers "which events and services exist?"
  7. * - tool-catalog.md answers "which tools does the model see?"
  8. * - generated relationship diagrams answer "how do those pieces fit together?"
  9. *
  10. * Generated pages discover the enumerable facts from source. Hybrid pages use
  11. * discovered inventory plus small manifests for policy that source cannot infer
  12. * (for example, whether a package is an implementation or consumer in a seam).
  13. * Curated pages are still emitted here so the graph docs are one regenerated unit,
  14. * but their diagrams intentionally explain flow and ownership rather than
  15. * pretending to enumerate every source edge.
  16. *
  17. * `tsx scripts/gen-doc-graphs.ts` -> write generated diagram docs
  18. * `tsx scripts/gen-doc-graphs.ts --check` -> exit 1 if any file is stale
  19. */
  20. import { existsSync, globSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'
  21. import { dirname, relative, resolve } from 'node:path'
  22. import ts from 'typescript'
  23. import { collectEvents, collectServices } from './gen-cordis-catalog.ts'
  24. const root = resolve(import.meta.dirname, '..')
  25. const SCOPE = '@deepseek-ai/dsh-'
  26. interface PkgJson {
  27. name: string
  28. peerDependencies?: Record<string, string>
  29. }
  30. interface Pkg {
  31. short: string
  32. name: string
  33. group: string
  34. rel: string
  35. deps: string[]
  36. }
  37. interface GraphDoc {
  38. rel: string
  39. content: string
  40. }
  41. interface ServiceRole {
  42. key: string
  43. pkg: string
  44. title: string
  45. mode: 'core' | 'seam' | 'bundle'
  46. implementations?: string[]
  47. consumers?: string[]
  48. companions?: string[]
  49. note: string
  50. }
  51. interface ExamplePlugin {
  52. id: string
  53. name: string
  54. }
  55. interface EventRelation {
  56. dispatchers: Map<string, Set<string>>
  57. listeners: Set<string>
  58. }
  59. const GROUP_ORDER = [
  60. 'util',
  61. 'llm',
  62. 'core',
  63. 'bash',
  64. 'fs',
  65. 'compact',
  66. 'subagent',
  67. 'web',
  68. 'todo',
  69. 'mode',
  70. 'cordis',
  71. 'hooks',
  72. 'session-persistence',
  73. 'support',
  74. 'ui',
  75. ]
  76. const SERVICE_ROLES: ServiceRole[] = [
  77. {
  78. key: 'llm',
  79. pkg: 'llm',
  80. title: 'LLM adapter registry',
  81. mode: 'seam',
  82. implementations: ['llm-deepseek', 'llm-pi-ai', 'llm-replay'],
  83. consumers: ['agent-loop', 'compact-basic'],
  84. note: 'Adapters register provider implementations; the loop and compaction call the provider-neutral stream service.',
  85. },
  86. {
  87. key: 'sessions',
  88. pkg: 'session',
  89. title: 'In-memory session store',
  90. mode: 'core',
  91. consumers: ['agent-loop', 'agent', 'session-persistence', 'subagent-inprocess', 'invariants'],
  92. note: 'Owns append-only Session instances and emits the durable session event feed.',
  93. },
  94. {
  95. key: 'sessionPersistence',
  96. pkg: 'session-persistence',
  97. title: 'Durable session persistence seam',
  98. mode: 'seam',
  99. implementations: ['session-persistence-jsonl', 'session-persistence-sqlite'],
  100. consumers: ['agent-loop', 'acp'],
  101. note: 'Backends persist the same SessionEvent vocabulary; apps choose a backend at composition time.',
  102. },
  103. {
  104. key: 'systemPrompt',
  105. pkg: 'system-prompt',
  106. title: 'System prompt assembly registry',
  107. mode: 'core',
  108. consumers: ['agent-loop', 'tools', 'tool-fs', 'tool-web'],
  109. note: 'Collects prompt sections and model-facing tool schemas for each step.',
  110. },
  111. {
  112. key: 'tools',
  113. pkg: 'tools',
  114. title: 'Tool registry and execution waterfall',
  115. mode: 'core',
  116. consumers: ['agent-loop', 'tool-ask-user', 'tool-bash', 'tool-cordis', 'tool-fs', 'tool-subagent', 'tool-todo', 'tool-web', 'acp'],
  117. note: 'Registers tool definitions, exposes schemas to the prompt, and routes calls through tools/pre-execute and tools/post-execute.',
  118. },
  119. {
  120. key: 'userInteraction',
  121. pkg: 'user-interaction',
  122. title: 'Human question/answer seam',
  123. mode: 'seam',
  124. implementations: ['stdio-agent', 'acp'],
  125. consumers: ['tool-ask-user', 'stdio-agent', 'acp'],
  126. note: 'UI front doors provide the active human-answer provider; tool-ask-user pauses a tool call on the provider-neutral ask() promise.',
  127. },
  128. {
  129. key: 'modes',
  130. pkg: 'mode',
  131. title: 'Session-mode policy state',
  132. mode: 'core',
  133. consumers: ['stdio-agent'],
  134. note: 'Folds the logged per-agent mode (mode/set), flushes user flips at turn boundaries, and enforces the mode through the assemble filter and the tools/pre-execute gate.',
  135. },
  136. {
  137. key: 'agents',
  138. pkg: 'agent',
  139. title: 'Agent registry',
  140. mode: 'core',
  141. consumers: ['agent-loop', 'acp', 'subagent-inprocess', 'stdio-agent', 'invariants'],
  142. note: 'Owns live Agent handles and the create/resume factory seam.',
  143. },
  144. {
  145. key: 'agentLoop',
  146. pkg: 'agent-loop',
  147. title: 'Concrete loop driver',
  148. mode: 'bundle',
  149. consumers: ['agent-core'],
  150. note: 'The one concrete loop plugin; extension packages depend on dsh-agent events and services, not on this package.',
  151. },
  152. {
  153. key: 'bash',
  154. pkg: 'bash',
  155. title: 'Bash executor seam',
  156. mode: 'seam',
  157. implementations: ['bash-local'],
  158. consumers: ['tool-bash', 'hooks-claude', 'hooks-codex'],
  159. note: 'The model-facing bash tools and hook bridges consume this seam; sandboxed or remote executors can replace bash-local.',
  160. },
  161. {
  162. key: 'codeRuntime',
  163. pkg: 'code-runtime',
  164. title: 'Code-execution seam',
  165. mode: 'seam',
  166. implementations: ['code-runtime-worker'],
  167. consumers: ['tools'],
  168. note: 'Runs one model-written program against host-provided async bindings; backends differ by substrate and language (the tool registry consumes it for Code Mode).',
  169. },
  170. {
  171. key: 'fs',
  172. pkg: 'fs',
  173. title: 'Filesystem provider seam',
  174. mode: 'seam',
  175. implementations: ['fs-local'],
  176. consumers: ['tool-fs'],
  177. companions: ['fs-policy'],
  178. note: 'tool-fs executes read/write/edit through ctx.fs; fs-policy contributes observed-state checks through the fs/* event gate.',
  179. },
  180. {
  181. key: 'compact',
  182. pkg: 'compact',
  183. title: 'Compaction seam',
  184. mode: 'seam',
  185. implementations: ['compact-basic'],
  186. consumers: ['compact-basic'],
  187. note: 'The basic backend currently consumes the pre-step event directly; a model-facing compact tool remains deferred.',
  188. },
  189. {
  190. key: 'subagents',
  191. pkg: 'subagent',
  192. title: 'Subagent provider registry',
  193. mode: 'seam',
  194. implementations: ['subagent-spawn', 'subagent-fork', 'subagent-acp', 'subagent-mock'],
  195. consumers: ['tool-subagent'],
  196. note: 'Providers implement transports; tool-subagent exposes one configured provider as a model-facing tool name.',
  197. },
  198. {
  199. key: 'web',
  200. pkg: 'web',
  201. title: 'Web access provider registry',
  202. mode: 'seam',
  203. implementations: ['web-search-exa', 'web-search-perplexity', 'web-search-deepseek', 'web-fetch-local'],
  204. consumers: ['tool-web'],
  205. note: 'Search and fetch providers register into one ctx.web seam; tool-web owns the stable model-facing names.',
  206. },
  207. ]
  208. const DYNAMIC_EVENT_DISPATCHERS: Array<{ event: string; pkg: string; method: string }> = [
  209. // Subagent lifecycle events intentionally bypass ctx.emit and call
  210. // ctx.events.dispatch directly so one throwing listener cannot starve later
  211. // listeners or strand an already-started child run.
  212. { event: 'subagent/start', pkg: 'subagent', method: 'events.dispatch' },
  213. { event: 'subagent/end', pkg: 'subagent', method: 'events.dispatch' },
  214. ]
  215. function generatedHeader(title: string): string[] {
  216. return [
  217. '<!-- Generated by scripts/gen-doc-graphs.ts - do not edit by hand.',
  218. ' Run `pnpm run gen-doc-graphs` to regenerate. -->',
  219. '',
  220. `# ${title}`,
  221. '',
  222. ]
  223. }
  224. function maintenanceFooter(source: string): string[] {
  225. return [`Maintenance mode: ${source}.`, '']
  226. }
  227. function graphIndexLink(rel: string): string {
  228. return relative('docs', rel).replaceAll('\\', '/')
  229. }
  230. function linkFromDoc(docRel: string, targetRel: string): string {
  231. return relative(dirname(docRel), targetRel).replaceAll('\\', '/')
  232. }
  233. function collectPackages(): Pkg[] {
  234. const pkgs: Pkg[] = []
  235. for (const rel of globSync('packages/*/*/package.json', { cwd: root }).sort()) {
  236. const json = JSON.parse(readFileSync(resolve(root, rel), 'utf8')) as PkgJson
  237. if (!json.name.startsWith(SCOPE)) continue
  238. const [, group, leaf] = rel.split('/')
  239. if (group === undefined || leaf === undefined) throw new Error(`gen-doc-graphs: unexpected package path ${rel}`)
  240. const deps = Object.keys(json.peerDependencies ?? {})
  241. .filter(dep => dep.startsWith(SCOPE))
  242. .map(dep => dep.slice(SCOPE.length))
  243. .sort()
  244. pkgs.push({
  245. short: json.name.slice(SCOPE.length),
  246. name: json.name,
  247. group,
  248. rel: dirname(rel),
  249. deps,
  250. })
  251. }
  252. return topoSort(pkgs)
  253. }
  254. function topoSort(pkgs: Pkg[]): Pkg[] {
  255. const remaining = new Map(pkgs.map(p => [p.short, p]))
  256. const placed = new Set<string>()
  257. const out: Pkg[] = []
  258. while (remaining.size > 0) {
  259. const ready = [...remaining.values()]
  260. .filter(pkg => pkg.deps.every(dep => placed.has(dep)))
  261. .sort(comparePackages)
  262. if (ready.length === 0) throw new Error(`gen-doc-graphs: dependency cycle among ${[...remaining.keys()].join(', ')}`)
  263. for (const pkg of ready) {
  264. out.push(pkg)
  265. placed.add(pkg.short)
  266. remaining.delete(pkg.short)
  267. }
  268. }
  269. return out
  270. }
  271. function comparePackages(a: Pkg, b: Pkg): number {
  272. const groupA = GROUP_ORDER.indexOf(a.group)
  273. const groupB = GROUP_ORDER.indexOf(b.group)
  274. const normA = groupA === -1 ? Number.MAX_SAFE_INTEGER : groupA
  275. const normB = groupB === -1 ? Number.MAX_SAFE_INTEGER : groupB
  276. return normA - normB || a.group.localeCompare(b.group) || a.short.localeCompare(b.short)
  277. }
  278. function nodeId(prefix: string, value: string): string {
  279. return `${prefix}_${value.replace(/[^a-zA-Z0-9_]/g, '_')}`
  280. }
  281. function escLabel(value: string): string {
  282. return value.replace(/"/g, '\\"')
  283. }
  284. function mermaidCode(value: string): string {
  285. return `<code>${value.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;')}</code>`
  286. }
  287. function repoLink(path: string, label: string, up = '..'): string {
  288. return `[${label}](${up}/${path})`
  289. }
  290. function sourceLink(source: string, up = '..'): string {
  291. return repoLink(source.split(':')[0] ?? source, `\`${source}\``, up)
  292. }
  293. function pkgLink(pkg: Pkg | undefined, fallback: string, up = '..'): string {
  294. return pkg ? repoLink(pkg.rel, `\`${pkg.short}\``, up) : `\`${fallback}\``
  295. }
  296. function pkgList(names: string[] | undefined, pkgsByShort: Map<string, Pkg>): string {
  297. if (!names || names.length === 0) return '-'
  298. return names.map(name => pkgLink(pkgsByShort.get(name), name)).join(', ')
  299. }
  300. function tableCell(value: string): string {
  301. return value.replace(/\|/g, '\\|').replace(/\n/g, '<br>')
  302. }
  303. function assertServiceRolesComplete(): void {
  304. const discovered = new Set(collectServices().map(service => service.key))
  305. const classified = new Set(SERVICE_ROLES.map(role => role.key))
  306. const missing = [...discovered].filter(key => !classified.has(key)).sort()
  307. const stale = [...classified].filter(key => !discovered.has(key)).sort()
  308. if (missing.length || stale.length) {
  309. throw new Error([
  310. missing.length ? `missing service role classification: ${missing.join(', ')}` : '',
  311. stale.length ? `stale service role classification: ${stale.join(', ')}` : '',
  312. ].filter(Boolean).join('; '))
  313. }
  314. }
  315. function renderCapabilitySeams(pkgs: Pkg[]): string {
  316. assertServiceRolesComplete()
  317. const pkgsByShort = new Map(pkgs.map(pkg => [pkg.short, pkg]))
  318. const maintenance = 'hybrid: services are discovered from Cordis declarations; interface/implementation/consumer roles are classified in `scripts/gen-doc-graphs.ts` with a completeness guard'
  319. const nodes = new Map<string, string>()
  320. const edges = new Set<string>()
  321. const companionEdges = new Set<string>()
  322. const addNode = (id: string, label: string): void => {
  323. if (!nodes.has(id)) nodes.set(id, ` ${id}["${escLabel(label)}"]`)
  324. }
  325. const addEdge = (from: string, to: string): void => { edges.add(` ${from} --> ${to}`) }
  326. const lines = generatedHeader('Capability Seams And Core Services')
  327. lines.push(
  328. 'A service can be a core spine service, a swappable capability seam, or a bundle/composition point. The graph shows the package that owns the service declaration, known implementation packages, and packages that consume the service directly.',
  329. '',
  330. '```mermaid',
  331. 'flowchart LR',
  332. )
  333. for (const role of SERVICE_ROLES) {
  334. const svc = nodeId('svc', role.key)
  335. const owner = nodeId('pkg', role.pkg)
  336. addNode(owner, role.pkg)
  337. addNode(svc, `ctx.${role.key}<br/>${role.title}`)
  338. addEdge(owner, svc)
  339. for (const impl of role.implementations ?? []) {
  340. addNode(nodeId('pkg', impl), impl)
  341. addEdge(nodeId('pkg', impl), svc)
  342. }
  343. for (const consumer of role.consumers ?? []) {
  344. addNode(nodeId('pkg', consumer), consumer)
  345. addEdge(svc, nodeId('pkg', consumer))
  346. }
  347. for (const companion of role.companions ?? []) {
  348. addNode(nodeId('pkg', companion), companion)
  349. companionEdges.add(` ${svc} -. event gate .-> ${nodeId('pkg', companion)}`)
  350. }
  351. }
  352. lines.push(...nodes.values(), ...[...edges].sort(), ...[...companionEdges].sort())
  353. lines.push('```', '', '| ctx key | Role | Owner | Implementations | Direct consumers | Companion plugins | Note |', '| --- | --- | --- | --- | --- | --- | --- |')
  354. for (const role of SERVICE_ROLES) {
  355. lines.push(`| \`ctx.${role.key}\` | \`${role.mode}\` | ${pkgLink(pkgsByShort.get(role.pkg), role.pkg)} | ${pkgList(role.implementations, pkgsByShort)} | ${pkgList(role.consumers, pkgsByShort)} | ${pkgList(role.companions, pkgsByShort)} | ${tableCell(role.note)} |`)
  356. }
  357. lines.push('', ...maintenanceFooter(maintenance))
  358. return lines.join('\n')
  359. }
  360. function parseExampleCordis(rel: string): ExamplePlugin[] {
  361. const text = readFileSync(resolve(root, rel), 'utf8')
  362. const plugins: ExamplePlugin[] = []
  363. let current: { id: string; name?: string } | null = null
  364. const flush = (): void => {
  365. if (current?.name) plugins.push({ id: current.id, name: current.name })
  366. }
  367. for (const line of text.split('\n')) {
  368. const id = /^-\s+id:\s+(.+?)\s*$/.exec(line)
  369. if (id?.[1] !== undefined) {
  370. flush()
  371. current = { id: stripYamlScalar(id[1]) }
  372. continue
  373. }
  374. const name = /^\s+name:\s+(.+?)\s*$/.exec(line)
  375. if (name?.[1] !== undefined && current) current.name = stripYamlScalar(name[1])
  376. }
  377. flush()
  378. return plugins
  379. }
  380. function stripYamlScalar(value: string): string {
  381. return value.trim().replace(/^['"]|['"]$/g, '')
  382. }
  383. const APP_EXAMPLES = [
  384. {
  385. id: 'echo',
  386. rel: 'examples/echo-agent/composition.md',
  387. title: 'Echo Agent App Composition',
  388. label: 'examples/echo-agent',
  389. config: 'examples/echo-agent/cordis.yml',
  390. summary: 'The echo demo swaps in a local mock LLM and teaching echo tool, then loads the stdio app package for the shared spine and terminal front door.',
  391. },
  392. {
  393. id: 'coding',
  394. rel: 'examples/coding-agent/composition.md',
  395. title: 'Coding Agent App Composition',
  396. label: 'examples/coding-agent',
  397. config: 'examples/coding-agent/cordis.yml',
  398. summary: 'The coding REPL demo adds the real DeepSeek adapter, filesystem tools, todo_write, compaction, and both subagent transports on top of the stdio app package.',
  399. },
  400. {
  401. id: 'cordis',
  402. rel: 'examples/cordis-agent/composition.md',
  403. title: 'Cordis Agent App Composition',
  404. label: 'examples/cordis-agent',
  405. config: 'examples/cordis-agent/cordis.yml',
  406. summary: 'The self-referential demo puts @deepseek-ai/dsh-tool-cordis on the coding spine, letting the agent inspect its own runtime and mount/unmount plugins into it.',
  407. },
  408. {
  409. id: 'acp',
  410. rel: 'examples/acp-agent/composition.md',
  411. title: 'ACP Agent App Composition',
  412. label: 'examples/acp-agent',
  413. config: 'examples/acp-agent/cordis.yml',
  414. summary: 'The ACP demo exposes the same agent spine over JSON-RPC stdio, with no stdout logger and no pre-created agent; clients create sessions through the ACP bridge.',
  415. },
  416. ]
  417. type AppExample = typeof APP_EXAMPLES[number]
  418. function renderAppExpansion(lines: string[], appNode: string, pluginName: string): void {
  419. const agentCore = nodeId('bundle', 'agent_core')
  420. const jsonl = nodeId('bundle', 'jsonl')
  421. lines.push(` ${appNode} --> ${agentCore}["@deepseek-ai/dsh-agent-core"]`)
  422. lines.push(` ${appNode} --> ${jsonl}["@deepseek-ai/dsh-session-persistence-jsonl"]`)
  423. if (pluginName === '@deepseek-ai/dsh-stdio-agent') {
  424. lines.push(` ${appNode} --> ${nodeId('frontdoor', 'stdio')}["readline UI<br/>console logger<br/>pre-created main agent"]`)
  425. } else if (pluginName === '@deepseek-ai/dsh-acp-agent') {
  426. lines.push(` ${appNode} --> ${nodeId('frontdoor', 'acp')}["@deepseek-ai/dsh-acp<br/>JSON-RPC stdio bridge<br/>sessions created by client"]`)
  427. }
  428. lines.push(
  429. ` ${agentCore} --> ${nodeId('spine', 'llm')}["ctx.llm"]`,
  430. ` ${agentCore} --> ${nodeId('spine', 'sessions')}["ctx.sessions"]`,
  431. ` ${agentCore} --> ${nodeId('spine', 'tools')}["ctx.tools + tool-bash"]`,
  432. ` ${agentCore} --> ${nodeId('spine', 'loop')}["ctx.agents + ctx.agentLoop"]`,
  433. )
  434. }
  435. function renderAppComposition(example: AppExample): string {
  436. const plugins = parseExampleCordis(example.config)
  437. const maintenance = 'hybrid: the leaf plugin list is parsed from its `cordis.yml`; app package expansion is curated from package source'
  438. const lines = generatedHeader(example.title)
  439. lines.push(
  440. example.summary,
  441. '',
  442. '```mermaid',
  443. 'flowchart LR',
  444. ` cfg["${escLabel(example.label)}<br/>cordis.yml"]`,
  445. )
  446. for (const plugin of plugins) {
  447. const pluginNode = nodeId(`plugin_${example.id}`, plugin.id)
  448. lines.push(` ${pluginNode}["${escLabel(plugin.id)}<br/>${escLabel(plugin.name)}"]`)
  449. lines.push(` cfg --> ${pluginNode}`)
  450. if (plugin.name === '@deepseek-ai/dsh-stdio-agent' || plugin.name === '@deepseek-ai/dsh-acp-agent') {
  451. renderAppExpansion(lines, pluginNode, plugin.name)
  452. }
  453. }
  454. lines.push(
  455. '```',
  456. '',
  457. '| Plugin id | Package / module |',
  458. '| --- | --- |',
  459. ...plugins.map(plugin => `| \`${plugin.id}\` | \`${plugin.name}\` |`),
  460. '',
  461. `Source config: [\`${example.config}\`](${linkFromDoc(example.rel, example.config)}).`,
  462. )
  463. lines.push('', ...maintenanceFooter(maintenance))
  464. return lines.join('\n')
  465. }
  466. function collectEventRelations(): Map<string, EventRelation> {
  467. const out = new Map<string, EventRelation>()
  468. const ensure = (event: string): EventRelation => {
  469. const existing = out.get(event)
  470. if (existing) return existing
  471. const next = { dispatchers: new Map<string, Set<string>>(), listeners: new Set<string>() }
  472. out.set(event, next)
  473. return next
  474. }
  475. for (const rel of globSync('packages/*/*/src/**/*.ts', { cwd: root }).sort()) {
  476. const [, , leaf] = rel.split('/')
  477. if (leaf === undefined) continue
  478. const text = readFileSync(resolve(root, rel), 'utf8')
  479. const sf = ts.createSourceFile(rel, text, ts.ScriptTarget.Latest, true)
  480. const visit = (node: ts.Node): void => {
  481. if (ts.isCallExpression(node) && ts.isPropertyAccessExpression(node.expression)) {
  482. const method = node.expression.name.text
  483. if (!isCordisContextReceiver(node.expression, sf)) {
  484. ts.forEachChild(node, visit)
  485. return
  486. }
  487. if (method === 'on') {
  488. const event = eventArg(node.arguments, method)
  489. if (event) ensure(event).listeners.add(leaf)
  490. } else if (method === 'emit' || method === 'parallel' || method === 'serial' || method === 'waterfall') {
  491. const event = eventArg(node.arguments, method)
  492. if (event) {
  493. const relation = ensure(event)
  494. const methods = relation.dispatchers.get(leaf) ?? new Set<string>()
  495. methods.add(method)
  496. relation.dispatchers.set(leaf, methods)
  497. }
  498. }
  499. }
  500. ts.forEachChild(node, visit)
  501. }
  502. visit(sf)
  503. }
  504. for (const entry of DYNAMIC_EVENT_DISPATCHERS) {
  505. const relation = ensure(entry.event)
  506. const methods = relation.dispatchers.get(entry.pkg) ?? new Set<string>()
  507. methods.add(entry.method)
  508. relation.dispatchers.set(entry.pkg, methods)
  509. }
  510. return out
  511. }
  512. function isCordisContextReceiver(expr: ts.PropertyAccessExpression, sf: ts.SourceFile): boolean {
  513. const target = expr.expression.getText(sf)
  514. return target === 'ctx' || target === 'this.ctx'
  515. }
  516. function eventArg(args: ts.NodeArray<ts.Expression>, method: string): string | undefined {
  517. if (method === 'waterfall') {
  518. const arg = args.find(ts.isStringLiteralLike)
  519. return arg?.text
  520. }
  521. const first = args[0]
  522. return first && ts.isStringLiteralLike(first) ? first.text : undefined
  523. }
  524. function relationPackages(map: Map<string, Set<string>>, pkgsByShort: Map<string, Pkg>): string {
  525. if (map.size === 0) return '-'
  526. return [...map.entries()]
  527. .sort(([a], [b]) => a.localeCompare(b))
  528. .map(([pkg, methods]) => `${pkgLink(pkgsByShort.get(pkg), pkg)} (${[...methods].sort().map(m => `\`${m}\``).join(', ')})`)
  529. .join(', ')
  530. }
  531. function listenerPackages(listeners: Set<string>, pkgsByShort: Map<string, Pkg>): string {
  532. if (listeners.size === 0) return '-'
  533. return [...listeners].sort().map(pkg => pkgLink(pkgsByShort.get(pkg), pkg)).join(', ')
  534. }
  535. function renderEventRelations(pkgs: Pkg[]): string {
  536. const events = collectEvents()
  537. const relations = collectEventRelations()
  538. const pkgsByShort = new Map(pkgs.map(pkg => [pkg.short, pkg]))
  539. const maintenance = 'hybrid generated: Cordis event declarations and most producer/listener edges are AST-scanned; dynamic dispatch sites are classified in `scripts/gen-doc-graphs.ts`'
  540. const lines = generatedHeader('Event Producer And Consumer Matrix')
  541. lines.push(
  542. 'This matrix shows which packages dispatch each harness-owned event and which packages listen to it. It is intentionally a table rather than one large graph: events are many-to-many, and dense relation data is easier to review in rows. Dynamic dispatch overrides cover sites that deliberately bypass `ctx.emit`, such as subagent lifecycle containment.',
  543. '',
  544. '| Event | Mode | Declared in | Dispatchers | Listeners |',
  545. '| --- | --- | --- | --- | --- |',
  546. )
  547. for (const event of [...events].sort((a, b) => a.name.localeCompare(b.name))) {
  548. const relation = relations.get(event.name) ?? { dispatchers: new Map<string, Set<string>>(), listeners: new Set<string>() }
  549. lines.push(`| \`${event.name}\` | \`${event.mode}\` | ${sourceLink(event.source)} | ${relationPackages(relation.dispatchers, pkgsByShort)} | ${listenerPackages(relation.listeners, pkgsByShort)} |`)
  550. }
  551. const declared = new Set(events.map(event => event.name))
  552. const extra = [...relations.keys()].filter(event => !declared.has(event)).sort()
  553. if (extra.length > 0) {
  554. lines.push('', '## Non-harness or undeclared event strings seen in package source', '', '| Event string | Dispatchers | Listeners |', '| --- | --- | --- |')
  555. for (const event of extra) {
  556. const relation = relations.get(event)
  557. if (!relation) continue
  558. lines.push(`| \`${event}\` | ${relationPackages(relation.dispatchers, pkgsByShort)} | ${listenerPackages(relation.listeners, pkgsByShort)} |`)
  559. }
  560. }
  561. lines.push('', ...maintenanceFooter(maintenance))
  562. return lines.join('\n')
  563. }
  564. function renderLifecycle(): string {
  565. const maintenance = 'curated Mermaid sequence; exact event signatures live in the generated Cordis catalog'
  566. return [
  567. ...generatedHeader('Agent Turn And Step Lifecycle'),
  568. 'This sequence is the visual companion to [architecture.md](architecture.md#loop-lifecycle-session--turn--step). It keeps durable replay facts on `session/event` and live control/status on `agent/*`.',
  569. '',
  570. '```mermaid',
  571. 'sequenceDiagram',
  572. ' participant User',
  573. ' participant Agent',
  574. ' participant Driver',
  575. ' participant Hooks as hook listeners',
  576. ' participant Prompt as ctx.systemPrompt',
  577. ' participant LLM as ctx.llm',
  578. ' participant Tools as ctx.tools',
  579. ' participant Session',
  580. ' participant Persistence',
  581. ' participant SDK as UI or SDK listener',
  582. ' User->>Agent: send(content)',
  583. ` Agent-->>SDK: ${mermaidCode('agent/queued')}`,
  584. ' Agent->>Driver: queued work wakes driver',
  585. ` Driver-->>SDK: ${mermaidCode('agent/status')} running`,
  586. ` Driver->>Session: ${mermaidCode('turn/start')}`,
  587. ` Driver->>Hooks: ${mermaidCode('agent/prompt-submit')} waterfall`,
  588. ' Hooks-->>Driver: allow, block, or add context',
  589. ` Driver->>Session: ${mermaidCode('user/message')} or rejected ${mermaidCode('turn/end')}`,
  590. ` Driver->>Prompt: ${mermaidCode('system-prompt/assemble')} waterfall`,
  591. ` Driver-->>Driver: ${mermaidCode('agent/pre-step')} serial checkpoint`,
  592. ` Driver->>Session: ${mermaidCode('step/start')}`,
  593. ` Driver->>LLM: ${mermaidCode('agent/request')} waterfall, then ${mermaidCode('llm/stream')} waterfall`,
  594. ' LLM-->>Driver: StreamChunk*',
  595. ` Driver->>Session: ${mermaidCode('assistant/chunk')}*`,
  596. ` Session-->>SDK: ${mermaidCode('session/event')} ${mermaidCode('assistant/chunk')}*`,
  597. ` Driver->>Hooks: ${mermaidCode('agent/step-result')} waterfall`,
  598. ` Driver->>Session: ${mermaidCode('assistant/message')}`,
  599. ` Driver->>Session: ${mermaidCode('tool/call')}`,
  600. ' Driver->>Tools: execute through pre and post waterfalls',
  601. ' Tools-->>Session: tool-owned events when applicable',
  602. ` Driver->>Session: ${mermaidCode('tool/result')} and ${mermaidCode('step/end')}`,
  603. ` Driver->>Hooks: ${mermaidCode('agent/turn-continuation')} waterfall`,
  604. ` Driver->>Session: ${mermaidCode('turn/end')}`,
  605. ` Driver->>Persistence: ${mermaidCode('session/flush')} parallel checkpoint`,
  606. ` Driver-->>SDK: ${mermaidCode('agent/status')} idle`,
  607. '```',
  608. '',
  609. 'SDK users that need replayable transcript data should consume `session/event`; `agent/*` is the live coordination surface for queue/status, prompt interception, request shaping, steering, continuation, and errors.',
  610. '',
  611. ...maintenanceFooter(maintenance),
  612. ].join('\n')
  613. }
  614. function renderToolPipeline(): string {
  615. const maintenance = 'curated Mermaid flow; exact tool schemas and event signatures live in generated catalogs'
  616. return [
  617. ...generatedHeader('Tool Execution Pipeline'),
  618. 'This graph shows where policy, hooks, sandboxing, filesystem guards, result rewriting, and UI rendering fit without changing the loop. The key extension points are the `tools/pre-execute`, `tools/execute`, and `tools/post-execute` waterfalls.',
  619. '',
  620. '```mermaid',
  621. 'flowchart TD',
  622. ' model["Assistant message contains tool-call block"]',
  623. ` toolCall["Session event: ${mermaidCode('tool/call')}<br/>logged before execution"]`,
  624. ' presentCall["UI pending card<br/>presentCall(args)"]',
  625. ` pre["${mermaidCode('tools/pre-execute')} waterfall<br/>hooks, permission, sandbox"]`,
  626. ' denied["deny or ask<br/>tool body skipped"]',
  627. ` around["${mermaidCode('tools/execute')} waterfall<br/>timeout, retry, metrics (around dispatch)"]`,
  628. ' toolBody["Registered tool execute() body"]',
  629. ` fsGate["${mermaidCode('fs/write-intent')} or ${mermaidCode('fs/edit-intent')}<br/>tool-fs mutations only"]`,
  630. ` owned["Tool-owned session events<br/>${mermaidCode('todo/write')}, ${mermaidCode('fs/observed')}, ${mermaidCode('hook/invoked')}, ${mermaidCode('hook/result')}, ${mermaidCode('tool/code-dispatch')}"]`,
  631. ` post["${mermaidCode('tools/post-execute')} waterfall<br/>accept, block, replace, add context"]`,
  632. ' context["Buffered additionalContext<br/>context/message after all tool results"]',
  633. ` toolResult["Session event: ${mermaidCode('tool/result')}<br/>single model-facing outcome"]`,
  634. ' presentResult["UI completed card<br/>presentResult(args, result)"]',
  635. ' model --> toolCall',
  636. ' toolCall --> presentCall',
  637. ' toolCall --> pre',
  638. ' pre -->|allow| around',
  639. ' around --> toolBody',
  640. ' pre -->|deny or ask| denied',
  641. ' denied --> post',
  642. ' toolBody --> fsGate',
  643. ' fsGate --> toolBody',
  644. ' toolBody --> owned',
  645. ' toolBody --> around',
  646. ' around --> post',
  647. ' post --> context',
  648. ' post --> toolResult',
  649. ' toolResult --> presentResult',
  650. '```',
  651. '',
  652. 'Filesystem read-before-edit checks live below `tool-fs` on the `fs/*` event gate; hook bridges and future permission prompts live on the generic pre/post tool waterfalls; and around-dispatch concerns like the tool-call timeout policy (`@deepseek-ai/dsh-timeout-policy`) wrap core dispatch on `tools/execute`. That split lets the same hooks observe bash, fs, web, todo, and subagent calls without coupling those tools to one policy service. Code Mode rides the same pipeline twice over: `run_code` is itself a registered tool body, and each tool call its program makes re-enters `ctx.tools.execute()` through BOTH waterfalls — serialized one at a time, logged as a `tool/code-dispatch` session event, with a deny surfacing to the program as a binding rejection (a sub-call\'s `additionalContext` is deliberately dropped — no safe outlet mid-run preserves call/result adjacency).',
  653. '',
  654. ...maintenanceFooter(maintenance),
  655. ].join('\n')
  656. }
  657. function renderSnapshotReplay(): string {
  658. const maintenance = 'curated Mermaid sequence based on the snapshot test harness'
  659. return [
  660. ...generatedHeader('ACP Snapshot Replay'),
  661. 'This graph explains what a snapshot scenario proves: recorded real-model session logs are replayed keylessly, ACP stdout is normalized and diffed, and scenario workspaces preserve tool side effects that the UI stream alone cannot prove.',
  662. '',
  663. '```mermaid',
  664. 'sequenceDiagram',
  665. ' participant Recorder as Real API recording',
  666. ' participant Fixture as snapshot fixture',
  667. ' participant Workspace',
  668. ' participant Replay as llm-replay adapter',
  669. ' participant ACP as acp-agent subprocess',
  670. ' participant Golden as stdout golden',
  671. ' Recorder->>Fixture: session.jsonl + workspace inputs',
  672. ' Fixture->>Workspace: seed files and hook configs',
  673. ' Fixture->>Replay: recorded StreamChunk script',
  674. ` Replay->>ACP: deterministic ${mermaidCode('llm/stream')} chunks`,
  675. ' ACP->>Workspace: bash, fs, and hook side effects',
  676. ' ACP->>Golden: normalized sessionUpdate stream',
  677. ' Golden-->>ACP: diff must be empty',
  678. '```',
  679. '',
  680. 'The fs and hook snapshot matrix is valuable because it proves world state, hook decisions, and failed tool-card rendering, not just that replay returns text.',
  681. '',
  682. ...maintenanceFooter(maintenance),
  683. ].join('\n')
  684. }
  685. function renderDocs(): GraphDoc[] {
  686. const pkgs = collectPackages()
  687. const docs: GraphDoc[] = [
  688. { rel: 'docs/capability-seams.md', content: renderCapabilitySeams(pkgs) },
  689. ...APP_EXAMPLES.map(example => ({ rel: example.rel, content: renderAppComposition(example) })),
  690. { rel: 'docs/event-producer-consumer.md', content: renderEventRelations(pkgs) },
  691. { rel: 'docs/agent-lifecycle.md', content: renderLifecycle() },
  692. { rel: 'docs/tool-execution-pipeline.md', content: renderToolPipeline() },
  693. { rel: 'packages/ui/acp/snapshot-replay.md', content: renderSnapshotReplay() },
  694. ]
  695. docs.unshift({ rel: 'docs/graph-atlas.md', content: renderIndex(docs) })
  696. return docs
  697. }
  698. function renderIndex(docs: GraphDoc[]): string {
  699. const labels: Record<string, string> = {
  700. 'docs/capability-seams.md': 'capability seams and core services',
  701. 'examples/echo-agent/composition.md': 'echo-agent app composition',
  702. 'examples/coding-agent/composition.md': 'coding-agent app composition',
  703. 'examples/cordis-agent/composition.md': 'cordis-agent app composition',
  704. 'examples/acp-agent/composition.md': 'acp-agent app composition',
  705. 'docs/event-producer-consumer.md': 'event producer/consumer matrix',
  706. 'docs/agent-lifecycle.md': 'agent turn and step lifecycle',
  707. 'docs/tool-execution-pipeline.md': 'tool execution pipeline',
  708. 'packages/ui/acp/snapshot-replay.md': 'ACP snapshot replay',
  709. }
  710. const modes: Record<string, string> = {
  711. 'docs/capability-seams.md': 'hybrid generated',
  712. 'examples/echo-agent/composition.md': 'hybrid generated',
  713. 'examples/coding-agent/composition.md': 'hybrid generated',
  714. 'examples/cordis-agent/composition.md': 'hybrid generated',
  715. 'examples/acp-agent/composition.md': 'hybrid generated',
  716. 'docs/event-producer-consumer.md': 'hybrid generated',
  717. 'docs/agent-lifecycle.md': 'curated',
  718. 'docs/tool-execution-pipeline.md': 'curated',
  719. 'packages/ui/acp/snapshot-replay.md': 'curated',
  720. }
  721. const rows = [
  722. '| [module dependency graph](module-graph.md) | `generated` |',
  723. '| [tool schema catalog and package map](tool-catalog.md) | `generated` |',
  724. ...docs.map((doc) => {
  725. const link = graphIndexLink(doc.rel)
  726. return `| [${labels[doc.rel] ?? link}](${link}) | \`${modes[doc.rel] ?? 'generated'}\` |`
  727. }),
  728. ]
  729. const maintenance = 'mixed: each linked page declares generated, hybrid, or curated mode'
  730. return [
  731. ...generatedHeader('Documentation Graph Index'),
  732. 'These diagrams are the relationship layer above the generated catalogs. Use them to navigate package topology, capability seams, event flow, model-facing tools, app composition, and runtime lifecycle paths. Exact signatures and type shapes still live in the generated [events](cordis-catalog/events.md) / [services](cordis-catalog/services.md) catalogs, [tool-catalog.md](tool-catalog.md), and [core-data-structures/](core-data-structures/core.md).',
  733. '',
  734. 'The process decision behind this index is recorded in [the documentation graph RFC](rfc/implemented/process/2026-07-03-documentation-graph-atlas.md).',
  735. '',
  736. '| Graph | Mode |',
  737. '| --- | --- |',
  738. ...rows,
  739. '',
  740. 'Regenerate with `pnpm run gen-doc-graphs`; verify freshness with `pnpm run verify-doc-graphs`.',
  741. '',
  742. ...maintenanceFooter(maintenance),
  743. ].join('\n')
  744. }
  745. function main(): void {
  746. const docs = renderDocs()
  747. if (process.argv.includes('--check')) {
  748. const stale: string[] = []
  749. for (const doc of docs) {
  750. const abs = resolve(root, doc.rel)
  751. const committed = existsSync(abs) ? readFileSync(abs, 'utf8') : null
  752. if (committed !== doc.content) stale.push(doc.rel)
  753. }
  754. if (stale.length === 0) {
  755. console.log(`gen-doc-graphs: ${docs.length} graph doc(s) are up to date.`)
  756. return
  757. }
  758. console.error(`gen-doc-graphs: stale graph doc(s): ${stale.join(', ')}. Run \`pnpm run gen-doc-graphs\` and commit the result.`)
  759. process.exit(1)
  760. }
  761. for (const doc of docs) {
  762. mkdirSync(dirname(resolve(root, doc.rel)), { recursive: true })
  763. writeFileSync(resolve(root, doc.rel), doc.content)
  764. }
  765. console.log(`gen-doc-graphs: wrote ${docs.length} graph doc(s).`)
  766. }
  767. if (process.argv[1] && import.meta.filename === resolve(process.argv[1])) {
  768. main()
  769. }