gen-doc-graphs.ts 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  1. /**
  2. * Generate the relationship layer above the module, Cordis, and tool catalogs.
  3. * Enumerable facts come from source; hybrid graphs add manifests for policy the
  4. * source cannot infer, while curated graphs explain flow and ownership.
  5. * `--check` verifies the generated set.
  6. */
  7. import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'
  8. import { dirname, relative, resolve } from 'node:path'
  9. import ts from 'typescript'
  10. import { collectEvents, collectServices } from './gen-cordis-catalog.ts'
  11. import {
  12. collectPackageGraph,
  13. escapeMermaidLabel as escLabel,
  14. graphNodeId as nodeId,
  15. type PackageGraphNode,
  16. } from './package-graph.ts'
  17. import { TypeScriptProject } from './ts-project.ts'
  18. const root = resolve(import.meta.dirname, '..')
  19. type Pkg = PackageGraphNode
  20. interface GraphDoc {
  21. rel: string
  22. content: string
  23. }
  24. interface ServiceRole {
  25. key: string
  26. pkg: string
  27. title: string
  28. mode: 'core' | 'seam' | 'bundle'
  29. implementations?: string[]
  30. consumers?: string[]
  31. companions?: string[]
  32. note: string
  33. }
  34. interface ExamplePlugin {
  35. id: string
  36. name: string
  37. }
  38. interface EventRelation {
  39. dispatchers: Map<string, Set<string>>
  40. listeners: Set<string>
  41. }
  42. interface PackageSource {
  43. rel: string
  44. pkg: string
  45. sourceFile: ts.SourceFile
  46. }
  47. type EventReceiverKind = 'context' | 'agent-dispatch' | 'events-service'
  48. const GROUP_ORDER = [
  49. 'util',
  50. 'llm',
  51. 'core',
  52. 'bash',
  53. 'sandbox',
  54. 'fs',
  55. 'skill',
  56. 'compact',
  57. 'subagent',
  58. 'tasks',
  59. 'workflow',
  60. 'web',
  61. 'spill',
  62. 'todo',
  63. 'cordis',
  64. 'hooks',
  65. 'session-persistence',
  66. 'session-query',
  67. 'support',
  68. 'ui',
  69. ]
  70. const SERVICE_ROLES: ServiceRole[] = [
  71. {
  72. key: 'llm',
  73. pkg: 'llm',
  74. title: 'LLM adapter registry',
  75. mode: 'seam',
  76. implementations: ['llm-deepseek', 'llm-pi-ai', 'llm-replay'],
  77. consumers: ['agent-loop', 'compact-basic'],
  78. note: 'Adapters register provider implementations; the loop and compaction call the provider-neutral stream service.',
  79. },
  80. {
  81. key: 'tokenMeter',
  82. pkg: 'token-meter',
  83. title: 'Replay token measurement',
  84. mode: 'core',
  85. consumers: ['compact-basic'],
  86. note: 'Owns isolated per-session replay folds; pressure consumers share immutable revisioned measurements.',
  87. },
  88. {
  89. key: 'sessions',
  90. pkg: 'session',
  91. title: 'In-memory session store',
  92. mode: 'core',
  93. consumers: ['agent-loop', 'agent', 'cli-demo', 'session-persistence', 'session-query', 'subagent-inprocess', 'invariants'],
  94. note: 'Owns append-only Session instances and emits the durable session event feed.',
  95. },
  96. {
  97. key: 'sessionPersistence',
  98. pkg: 'session-persistence',
  99. title: 'Durable session persistence seam',
  100. mode: 'seam',
  101. implementations: ['session-persistence-jsonl', 'session-persistence-sqlite'],
  102. consumers: ['agent-loop', 'tool-bash', 'hooks-claude', 'hooks-codex', 'acp', 'session-query'],
  103. note: 'Backends persist the same SessionEvent vocabulary; apps choose a backend at composition time.',
  104. },
  105. {
  106. key: 'sessionQuery',
  107. pkg: 'session-query',
  108. title: 'Exact session-history reads and traces',
  109. mode: 'seam',
  110. note: 'Resolves live and optional persisted logs into one logical corpus for exact reads and relationship traces.',
  111. },
  112. {
  113. key: 'systemPrompt',
  114. pkg: 'system-prompt',
  115. title: 'System prompt assembly registry',
  116. mode: 'core',
  117. consumers: ['agent-loop', 'tools', 'tool-fs', 'tool-web'],
  118. note: 'Collects prompt sections and model-facing tool schemas for each step.',
  119. },
  120. {
  121. key: 'tools',
  122. pkg: 'tools',
  123. title: 'Tool registry and guarded execution pipeline',
  124. mode: 'core',
  125. consumers: ['agent-loop', 'tool-ask-user', 'tool-bash', 'tool-cordis', 'tool-fs', 'tool-skill', 'tool-subagent', 'tool-todo', 'tool-web', 'acp'],
  126. note: 'Registers capabilities, owns Code Mode transport, and routes calls through pre-policy, monotonic guards, around dispatch, post-policy, and final-result observation.',
  127. },
  128. {
  129. key: 'userInteraction',
  130. pkg: 'user-interaction',
  131. title: 'Human question/answer seam',
  132. mode: 'seam',
  133. implementations: ['stdio-demo', 'acp'],
  134. consumers: ['tool-ask-user', 'stdio-demo', 'acp'],
  135. note: 'UI front doors provide the active human-answer provider; tool-ask-user pauses a tool call on the provider-neutral ask() promise.',
  136. },
  137. {
  138. key: 'skills',
  139. pkg: 'skill',
  140. title: 'Skill provider registry',
  141. mode: 'seam',
  142. implementations: ['skill-local'],
  143. consumers: ['tool-skill'],
  144. note: 'Merges provider skill catalogs; tool-skill renders the session-prefix catalog and loads complete skill bodies.',
  145. },
  146. {
  147. key: 'agents',
  148. pkg: 'agent',
  149. title: 'Agent registry',
  150. mode: 'core',
  151. consumers: ['agent-loop', 'acp', 'cli-demo', 'subagent-inprocess', 'stdio-demo', 'invariants'],
  152. note: 'Owns live Agent handles and the create/resume factory seam.',
  153. },
  154. {
  155. key: 'agentLoop',
  156. pkg: 'agent-loop',
  157. title: 'Concrete loop driver',
  158. mode: 'bundle',
  159. consumers: ['agent-spine-demo'],
  160. note: 'The one concrete loop plugin; extension packages depend on dsh-agent events and services, not on this package.',
  161. },
  162. {
  163. key: 'bash',
  164. pkg: 'bash',
  165. title: 'Bash executor seam',
  166. mode: 'seam',
  167. implementations: ['bash-local', 'bash-sandbox'],
  168. consumers: ['tool-bash', 'hooks-claude', 'hooks-codex'],
  169. note: 'The model-facing bash tools and hook bridges consume this seam; sandboxed or remote executors replace bash-local without touching them.',
  170. },
  171. {
  172. key: 'bashEnv',
  173. pkg: 'tool-bash',
  174. title: 'Managed bash environment registry',
  175. mode: 'core',
  176. note: 'Plugins declare effect-scoped DSH_* facts; tool-bash collects one trusted snapshot per execution and the executor rebuilds the namespace.',
  177. },
  178. {
  179. key: 'sandbox',
  180. pkg: 'sandbox',
  181. title: 'Process-sandbox seam',
  182. mode: 'seam',
  183. implementations: ['sandbox-local'],
  184. consumers: ['bash-sandbox'],
  185. note: 'Consumers hand over the exact argv they are about to spawn; same-world backends wrap it under a per-call policy and report enforcement.',
  186. },
  187. {
  188. key: 'approval',
  189. pkg: 'approval',
  190. title: 'Approval seam',
  191. mode: 'seam',
  192. implementations: ['acp'],
  193. consumers: ['tools', 'tool-bash'],
  194. note: 'One-shot permission decisions dispatched over the `approval/request` waterfall; answerers are listeners (the ACP bridge for its own agents), absence fails closed to `unavailable`.',
  195. },
  196. {
  197. key: 'permission',
  198. pkg: 'permission',
  199. title: 'Permission presets',
  200. mode: 'core',
  201. implementations: [],
  202. consumers: ['acp'],
  203. note: 'User-facing preset table (`workspace-write`/`danger-full-access`) bundling the sandbox-mode and approval-policy knobs; a switch writes one `permission/preset` event through to both knob events.',
  204. },
  205. {
  206. key: 'codeRuntime',
  207. pkg: 'code-runtime',
  208. title: 'Code-execution seam',
  209. mode: 'seam',
  210. implementations: ['code-runtime-worker'],
  211. consumers: ['tools'],
  212. 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).',
  213. },
  214. {
  215. key: 'fs',
  216. pkg: 'fs',
  217. title: 'Filesystem provider seam',
  218. mode: 'seam',
  219. implementations: ['fs-local'],
  220. consumers: ['tool-fs'],
  221. companions: ['fs-policy'],
  222. note: 'tool-fs executes read/write/edit through ctx.fs; fs-policy contributes observed-state checks through the fs/* event gate.',
  223. },
  224. {
  225. key: 'compact',
  226. pkg: 'compact',
  227. title: 'Compaction seam',
  228. mode: 'seam',
  229. implementations: ['compact-basic'],
  230. consumers: ['compact-basic'],
  231. note: 'The basic backend consumes post-step pressure and request-error recovery events; a model-facing compact tool remains deferred.',
  232. },
  233. {
  234. key: 'subagents',
  235. pkg: 'subagent',
  236. title: 'Subagent provider registry',
  237. mode: 'seam',
  238. implementations: ['subagent-spawn', 'subagent-fork', 'subagent-acp'],
  239. consumers: ['tool-subagent'],
  240. note: 'Providers implement transports; tool-subagent exposes one configured provider as a model-facing tool name.',
  241. },
  242. {
  243. key: 'tasks',
  244. pkg: 'tasks',
  245. title: 'Background task registry',
  246. mode: 'core',
  247. consumers: ['tool-bash', 'tool-subagent', 'tool-tasks'],
  248. note: 'Producers (tool-bash background commands, tool-subagent background delegations) register running work; tool-tasks is the model-facing control surface that reads, lists, and kills it.',
  249. },
  250. {
  251. key: 'web',
  252. pkg: 'web',
  253. title: 'Web access provider registry',
  254. mode: 'seam',
  255. implementations: ['web-search-exa', 'web-search-perplexity', 'web-search-deepseek', 'web-fetch-local'],
  256. consumers: ['tool-web'],
  257. note: 'Search and fetch providers register into one ctx.web seam; tool-web owns the stable model-facing names.',
  258. },
  259. {
  260. key: 'spillStore',
  261. pkg: 'spill',
  262. title: 'Spill storage seam',
  263. mode: 'seam',
  264. implementations: ['spill-local'],
  265. consumers: ['spill-policy'],
  266. note: 'The backend saves oversized tool text and returns a model-facing locator plus retrieval hint; spill-policy is the tools/post-execute consumer that decides when to spill.',
  267. },
  268. {
  269. key: 'workflows',
  270. pkg: 'workflow',
  271. title: 'Workflow script engine',
  272. mode: 'seam',
  273. implementations: ['workflow-workerthread'],
  274. consumers: ['tool-workflow'],
  275. note: 'One engine per context (bash shape, no named-provider registry); the worker-thread engine fans agent() calls out through ctx.subagents.',
  276. },
  277. ]
  278. function generatedHeader(title: string): string[] {
  279. return [
  280. '<!-- Generated by scripts/gen-doc-graphs.ts - do not edit by hand.',
  281. ' Run `pnpm run gen-doc-graphs` to regenerate. -->',
  282. '',
  283. `# ${title}`,
  284. '',
  285. ]
  286. }
  287. function maintenanceFooter(source: string): string[] {
  288. return [`Maintenance mode: ${source}.`, '']
  289. }
  290. function graphIndexLink(rel: string): string {
  291. return relative('docs', rel).replaceAll('\\', '/')
  292. }
  293. function linkFromDoc(docRel: string, targetRel: string): string {
  294. return relative(dirname(docRel), targetRel).replaceAll('\\', '/')
  295. }
  296. function mermaidCode(value: string): string {
  297. return `<code>${value.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;')}</code>`
  298. }
  299. function repoLink(path: string, label: string, up = '..'): string {
  300. return `[${label}](${up}/${path})`
  301. }
  302. function sourceLink(source: string, up = '..'): string {
  303. return repoLink(source.split(':')[0] ?? source, `\`${source}\``, up)
  304. }
  305. function pkgLink(pkg: Pkg | undefined, fallback: string, up = '..'): string {
  306. return pkg ? repoLink(pkg.rel, `\`${pkg.short}\``, up) : `\`${fallback}\``
  307. }
  308. function pkgList(names: string[] | undefined, pkgsByShort: Map<string, Pkg>): string {
  309. if (!names || names.length === 0) return '-'
  310. return names.map(name => pkgLink(pkgsByShort.get(name), name)).join(', ')
  311. }
  312. function tableCell(value: string): string {
  313. return value.replace(/\|/g, '\\|').replace(/\n/g, '<br>')
  314. }
  315. function assertServiceRolesComplete(): void {
  316. const discovered = new Set(collectServices().map(service => service.key))
  317. const classified = new Set(SERVICE_ROLES.map(role => role.key))
  318. const missing = [...discovered].filter(key => !classified.has(key)).sort()
  319. const stale = [...classified].filter(key => !discovered.has(key)).sort()
  320. if (missing.length || stale.length) {
  321. throw new Error([
  322. missing.length ? `missing service role classification: ${missing.join(', ')}` : '',
  323. stale.length ? `stale service role classification: ${stale.join(', ')}` : '',
  324. ].filter(Boolean).join('; '))
  325. }
  326. }
  327. function renderCapabilitySeams(pkgs: Pkg[]): string {
  328. assertServiceRolesComplete()
  329. const pkgsByShort = new Map(pkgs.map(pkg => [pkg.short, pkg]))
  330. 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'
  331. const nodes = new Map<string, string>()
  332. const edges = new Set<string>()
  333. const companionEdges = new Set<string>()
  334. const addNode = (id: string, label: string): void => {
  335. if (!nodes.has(id)) nodes.set(id, ` ${id}["${escLabel(label)}"]`)
  336. }
  337. const addEdge = (from: string, to: string): void => { edges.add(` ${from} --> ${to}`) }
  338. const lines = generatedHeader('Capability Seams And Core Services')
  339. lines.push(
  340. '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.',
  341. '',
  342. '```mermaid',
  343. 'flowchart LR',
  344. )
  345. for (const role of SERVICE_ROLES) {
  346. const svc = nodeId('svc', role.key)
  347. const owner = nodeId('pkg', role.pkg)
  348. addNode(owner, role.pkg)
  349. addNode(svc, `ctx.${role.key}<br/>${role.title}`)
  350. addEdge(owner, svc)
  351. for (const impl of role.implementations ?? []) {
  352. addNode(nodeId('pkg', impl), impl)
  353. addEdge(nodeId('pkg', impl), svc)
  354. }
  355. for (const consumer of role.consumers ?? []) {
  356. addNode(nodeId('pkg', consumer), consumer)
  357. addEdge(svc, nodeId('pkg', consumer))
  358. }
  359. for (const companion of role.companions ?? []) {
  360. addNode(nodeId('pkg', companion), companion)
  361. companionEdges.add(` ${svc} -. event gate .-> ${nodeId('pkg', companion)}`)
  362. }
  363. }
  364. lines.push(...nodes.values(), ...[...edges].sort(), ...[...companionEdges].sort())
  365. lines.push('```', '', '| ctx key | Role | Owner | Implementations | Direct consumers | Companion plugins | Note |', '| --- | --- | --- | --- | --- | --- | --- |')
  366. for (const role of SERVICE_ROLES) {
  367. 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)} |`)
  368. }
  369. lines.push('', ...maintenanceFooter(maintenance))
  370. return lines.join('\n')
  371. }
  372. function parseExampleCordis(rel: string): ExamplePlugin[] {
  373. const text = readFileSync(resolve(root, rel), 'utf8')
  374. const plugins: ExamplePlugin[] = []
  375. let current: { id: string; name?: string } | null = null
  376. const flush = (): void => {
  377. if (current?.name) plugins.push({ id: current.id, name: current.name })
  378. }
  379. for (const line of text.split('\n')) {
  380. const id = /^-\s+id:\s+(.+?)\s*$/.exec(line)
  381. if (id?.[1] !== undefined) {
  382. flush()
  383. current = { id: stripYamlScalar(id[1]) }
  384. continue
  385. }
  386. const name = /^\s+name:\s+(.+?)\s*$/.exec(line)
  387. if (name?.[1] !== undefined && current) current.name = stripYamlScalar(name[1])
  388. }
  389. flush()
  390. return plugins
  391. }
  392. function stripYamlScalar(value: string): string {
  393. return value.trim().replace(/^['"]|['"]$/g, '')
  394. }
  395. const APP_EXAMPLES = [
  396. {
  397. id: 'echo',
  398. rel: 'examples/echo-agent/composition.md',
  399. title: 'Echo Agent App Composition',
  400. label: 'examples/echo-agent',
  401. config: 'examples/echo-agent/cordis.yml',
  402. 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.',
  403. },
  404. {
  405. id: 'repl',
  406. rel: 'examples/repl-agent/composition.md',
  407. title: 'REPL Agent App Composition',
  408. label: 'examples/repl-agent',
  409. config: 'examples/repl-agent/cordis.yml',
  410. summary: 'The REPL agent demo adds the real DeepSeek adapter, filesystem tools, todo_write, compaction, and both subagent transports on top of the stdio app package.',
  411. },
  412. {
  413. id: 'tui',
  414. rel: 'examples/tui-agent/composition.md',
  415. title: 'TUI Agent App Composition',
  416. label: 'examples/tui-agent',
  417. config: 'examples/tui-agent/cordis.yml',
  418. summary: 'The TUI agent reuses the repl-agent backend and tool composition while fixing the shared terminal app to the full-screen dsh-tui front door.',
  419. },
  420. {
  421. id: 'headless',
  422. rel: 'examples/headless-agent/composition.md',
  423. title: 'Headless Agent App Composition',
  424. label: 'examples/headless-agent',
  425. config: 'examples/headless-agent/cordis.yml',
  426. summary: 'The headless demo combines the real DeepSeek adapter and coding capabilities with the one-shot app package, format-pure stdout, and one fresh persisted top-level session.',
  427. },
  428. {
  429. id: 'cordis',
  430. rel: 'examples/cordis-agent/composition.md',
  431. title: 'Cordis Agent App Composition',
  432. label: 'examples/cordis-agent',
  433. config: 'examples/cordis-agent/cordis.yml',
  434. 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.',
  435. },
  436. {
  437. id: 'acp',
  438. rel: 'examples/acp-agent/composition.md',
  439. title: 'ACP Agent App Composition',
  440. label: 'examples/acp-agent',
  441. config: 'examples/acp-agent/cordis.yml',
  442. 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.',
  443. },
  444. ]
  445. type AppExample = typeof APP_EXAMPLES[number]
  446. function renderAppExpansion(lines: string[], appNode: string, pluginName: string, exampleId: string): void {
  447. const agentCore = nodeId('bundle', 'agent_core')
  448. const jsonl = nodeId('bundle', 'jsonl')
  449. lines.push(` ${appNode} --> ${agentCore}["@deepseek-ai/dsh-agent-spine-demo"]`)
  450. lines.push(` ${appNode} --> ${jsonl}["@deepseek-ai/dsh-session-persistence-jsonl"]`)
  451. if (pluginName === '@deepseek-ai/dsh-stdio-demo') {
  452. const frontDoor = exampleId === 'tui'
  453. ? '@deepseek-ai/dsh-tui<br/>pre-created main agent'
  454. : exampleId === 'repl'
  455. ? '@deepseek-ai/dsh-stdio<br/>pre-created main agent'
  456. : 'dsh-tui (TTY) / dsh-stdio (pipes)<br/>pre-created main agent'
  457. lines.push(` ${appNode} --> ${nodeId('frontdoor', 'stdio')}["${frontDoor}"]`)
  458. } else if (pluginName === '@deepseek-ai/dsh-cli-demo') {
  459. lines.push(` ${appNode} --> ${nodeId('frontdoor', 'cli')}["one-shot driver<br/>format-pure stdout<br/>fresh top-level agent"]`)
  460. } else if (pluginName === '@deepseek-ai/dsh-acp-demo') {
  461. lines.push(` ${appNode} --> ${nodeId('frontdoor', 'acp')}["@deepseek-ai/dsh-acp<br/>JSON-RPC stdio bridge<br/>sessions created by client"]`)
  462. }
  463. lines.push(
  464. ` ${agentCore} --> ${nodeId('spine', 'llm')}["ctx.llm"]`,
  465. ` ${agentCore} --> ${nodeId('spine', 'sessions')}["ctx.sessions"]`,
  466. ` ${agentCore} --> ${nodeId('spine', 'tools')}["ctx.tools + tool-bash"]`,
  467. ` ${agentCore} --> ${nodeId('spine', 'loop')}["ctx.agents + ctx.agentLoop"]`,
  468. )
  469. }
  470. function renderAppComposition(example: AppExample): string {
  471. const plugins = parseExampleCordis(example.config)
  472. const maintenance = 'hybrid: the leaf plugin list is parsed from its `cordis.yml`; app package expansion is curated from package source'
  473. const lines = generatedHeader(example.title)
  474. lines.push(
  475. example.summary,
  476. '',
  477. '```mermaid',
  478. 'flowchart LR',
  479. ` cfg["${escLabel(example.label)}<br/>cordis.yml"]`,
  480. )
  481. for (const plugin of plugins) {
  482. const pluginNode = nodeId(`plugin_${example.id}`, plugin.id)
  483. lines.push(` ${pluginNode}["${escLabel(plugin.id)}<br/>${escLabel(plugin.name)}"]`)
  484. lines.push(` cfg --> ${pluginNode}`)
  485. if (plugin.name === '@deepseek-ai/dsh-stdio-demo' || plugin.name === '@deepseek-ai/dsh-cli-demo' || plugin.name === '@deepseek-ai/dsh-acp-demo') {
  486. renderAppExpansion(lines, pluginNode, plugin.name, example.id)
  487. }
  488. }
  489. lines.push(
  490. '```',
  491. '',
  492. '| Plugin id | Package / module |',
  493. '| --- | --- |',
  494. ...plugins.map(plugin => `| \`${plugin.id}\` | \`${plugin.name}\` |`),
  495. '',
  496. `Source config: [\`${example.config}\`](${linkFromDoc(example.rel, example.config)}).`,
  497. )
  498. lines.push('', ...maintenanceFooter(maintenance))
  499. return lines.join('\n')
  500. }
  501. /** Collect event dispatch/listener relations from real cross-file receiver types. */
  502. class EventRelationCollector {
  503. private readonly relations = new Map<string, EventRelation>()
  504. private readonly callSites = new Map<ts.SignatureDeclaration | ts.JSDocSignature, ts.CallExpression[]>()
  505. private readonly contextType: ts.Type
  506. private readonly agentDispatchType: ts.Type
  507. private readonly eventsServiceType: ts.Type
  508. constructor(
  509. private readonly project: TypeScriptProject,
  510. private readonly sources: readonly PackageSource[],
  511. ) {
  512. this.contextType = this.declaredType('vendor/cordis/src/context.ts', 'Context')
  513. this.agentDispatchType = this.declaredType('packages/core/agent/src/dispatch.ts', 'AgentEventDispatch')
  514. this.eventsServiceType = this.declaredType('vendor/cordis/src/events.ts', 'EventsService')
  515. this.indexCallSites()
  516. }
  517. /** Return all event relations discovered from the Program. */
  518. collect(): Map<string, EventRelation> {
  519. for (const source of this.sources) this.visitSource(source)
  520. return this.relations
  521. }
  522. /** Resolve one named class/interface declaration to its merged instance type. */
  523. private declaredType(relativePath: string, name: string): ts.Type {
  524. const sourceFile = this.project.sourceFile(relativePath)
  525. const declaration = sourceFile.statements.find((statement): statement is ts.ClassDeclaration | ts.InterfaceDeclaration => {
  526. return (ts.isClassDeclaration(statement) || ts.isInterfaceDeclaration(statement)) && statement.name?.text === name
  527. })
  528. const symbol = declaration?.name && this.project.checker.getSymbolAtLocation(declaration.name)
  529. if (!symbol) throw new Error(`cannot resolve TypeScript type ${name} from ${relativePath}`)
  530. return this.project.checker.getDeclaredTypeOfSymbol(symbol)
  531. }
  532. /** Index resolved local function calls for narrow argument-flow recovery. */
  533. private indexCallSites(): void {
  534. const visit = (node: ts.Node): void => {
  535. if (ts.isCallExpression(node)) {
  536. const declaration = this.project.checker.getResolvedSignature(node)?.declaration
  537. if (declaration) {
  538. const calls = this.callSites.get(declaration) ?? []
  539. calls.push(node)
  540. this.callSites.set(declaration, calls)
  541. }
  542. }
  543. ts.forEachChild(node, visit)
  544. }
  545. for (const source of this.sources) visit(source.sourceFile)
  546. }
  547. /** Walk one package source file and classify event API calls by receiver type. */
  548. private visitSource(source: PackageSource): void {
  549. const visit = (node: ts.Node): void => {
  550. if (ts.isCallExpression(node) && ts.isPropertyAccessExpression(node.expression)) {
  551. const receiverKind = this.receiverKind(node.expression.expression)
  552. const method = node.expression.name.text
  553. if (receiverKind === 'events-service' && method === 'dispatch') {
  554. const argumentList = node.arguments[1]
  555. if (argumentList) {
  556. for (const event of this.eventNamesFromArgumentList(argumentList, new Set())) {
  557. this.addDispatcher(event, source.pkg, 'events.dispatch')
  558. }
  559. }
  560. } else if (receiverKind === 'context' || receiverKind === 'agent-dispatch') {
  561. const eventNames = this.eventNamesFromCall(node, receiverKind)
  562. if (method === 'on' || method === 'once') {
  563. for (const event of eventNames) this.ensure(event).listeners.add(source.pkg)
  564. } else if (method === 'emit' || method === 'parallel' || method === 'serial' || method === 'waterfall') {
  565. for (const event of eventNames) this.addDispatcher(event, source.pkg, method)
  566. }
  567. }
  568. }
  569. ts.forEachChild(node, visit)
  570. }
  571. visit(source.sourceFile)
  572. }
  573. /** Classify a receiver using assignability to the repository's actual event API types. */
  574. private receiverKind(receiver: ts.Expression): EventReceiverKind | undefined {
  575. const type = this.project.checker.getTypeAtLocation(receiver)
  576. if (type.flags & (ts.TypeFlags.Any | ts.TypeFlags.Unknown | ts.TypeFlags.Never)) return undefined
  577. if (this.project.checker.isTypeAssignableTo(type, this.eventsServiceType)) return 'events-service'
  578. if (this.project.checker.isTypeAssignableTo(type, this.contextType)) return 'context'
  579. if (this.project.checker.isTypeAssignableTo(type, this.agentDispatchType)) return 'agent-dispatch'
  580. return undefined
  581. }
  582. /** Resolve the event-name argument for Context and fused agent dispatch calls. */
  583. private eventNamesFromCall(call: ts.CallExpression, receiverKind: Exclude<EventReceiverKind, 'events-service'>): Set<string> {
  584. const candidates = receiverKind === 'context' ? call.arguments.slice(0, 2) : call.arguments.slice(0, 1)
  585. for (const candidate of candidates) {
  586. const values = this.finiteStringValues(candidate)
  587. if (values) return values
  588. }
  589. return new Set()
  590. }
  591. /** Recover the event slot from the argument array handed to EventsService.dispatch(). */
  592. private eventNamesFromArgumentList(expression: ts.Expression, seen: Set<ts.Node>): Set<string> {
  593. const current = unwrapExpression(expression)
  594. if (seen.has(current)) return new Set()
  595. seen.add(current)
  596. if (ts.isArrayLiteralExpression(current)) {
  597. for (const element of current.elements.slice(0, 2)) {
  598. if (ts.isOmittedExpression(element) || ts.isSpreadElement(element)) continue
  599. const values = this.finiteStringValues(element)
  600. if (values) return values
  601. }
  602. return new Set()
  603. }
  604. if (ts.isConditionalExpression(current)) {
  605. return unionSets(
  606. this.eventNamesFromArgumentList(current.whenTrue, new Set(seen)),
  607. this.eventNamesFromArgumentList(current.whenFalse, new Set(seen)),
  608. )
  609. }
  610. if (!ts.isIdentifier(current)) return new Set()
  611. const symbol = this.project.checker.getSymbolAtLocation(current)
  612. if (!symbol) return new Set()
  613. const events = new Set<string>()
  614. for (const declaration of symbol.declarations ?? []) {
  615. if (ts.isVariableDeclaration(declaration) && declaration.initializer && isConstDeclaration(declaration)) {
  616. addAll(events, this.eventNamesFromArgumentList(declaration.initializer, new Set(seen)))
  617. } else if (ts.isParameter(declaration)) {
  618. addAll(events, this.eventNamesFromParameter(declaration, seen))
  619. }
  620. }
  621. return events
  622. }
  623. /** Follow a non-exported local helper parameter back to every resolved call site. */
  624. private eventNamesFromParameter(parameter: ts.ParameterDeclaration, seen: Set<ts.Node>): Set<string> {
  625. const owner = parameter.parent
  626. if (!ts.isFunctionDeclaration(owner) || hasExportModifier(owner)) return new Set()
  627. const index = owner.parameters.indexOf(parameter)
  628. if (index < 0) return new Set()
  629. const events = new Set<string>()
  630. for (const call of this.callSites.get(owner) ?? []) {
  631. const argument = call.arguments[index]
  632. if (argument) addAll(events, this.eventNamesFromArgumentList(argument, new Set(seen)))
  633. }
  634. return events
  635. }
  636. /** Return a finite string-literal value set, rejecting widened and generic strings. */
  637. private finiteStringValues(expression: ts.Expression): Set<string> | undefined {
  638. const current = unwrapExpression(expression)
  639. if (ts.isStringLiteralLike(current)) return new Set([current.text])
  640. if (this.isForwardedAgentEventParameter(current)) return undefined
  641. return finiteStringTypeValues(this.project.checker.getTypeAtLocation(current))
  642. }
  643. /** Reject the contextual parameter inside the AgentEventDispatch forwarding object. */
  644. private isForwardedAgentEventParameter(expression: ts.Expression): boolean {
  645. if (!ts.isIdentifier(expression)) return false
  646. const declarations = this.project.checker.getSymbolAtLocation(expression)?.declarations ?? []
  647. return declarations.some((declaration) => {
  648. if (!ts.isParameter(declaration)) return false
  649. const method = declaration.parent
  650. if (!ts.isMethodDeclaration(method) || !ts.isObjectLiteralExpression(method.parent)) return false
  651. const contextualType = this.project.checker.getContextualType(method.parent)
  652. return contextualType !== undefined
  653. && this.project.checker.isTypeAssignableTo(contextualType, this.agentDispatchType)
  654. })
  655. }
  656. /** Get or create one relation row. */
  657. private ensure(event: string): EventRelation {
  658. const existing = this.relations.get(event)
  659. if (existing) return existing
  660. const relation = { dispatchers: new Map<string, Set<string>>(), listeners: new Set<string>() }
  661. this.relations.set(event, relation)
  662. return relation
  663. }
  664. /** Add one dispatcher method without duplicating package/method labels. */
  665. private addDispatcher(event: string, pkg: string, method: string): void {
  666. const relation = this.ensure(event)
  667. const methods = relation.dispatchers.get(pkg) ?? new Set<string>()
  668. methods.add(method)
  669. relation.dispatchers.set(pkg, methods)
  670. }
  671. }
  672. /** Peel syntax-only wrappers that do not change an expression's runtime value. */
  673. function unwrapExpression(expression: ts.Expression): ts.Expression {
  674. let current = expression
  675. while (
  676. ts.isParenthesizedExpression(current)
  677. || ts.isAsExpression(current)
  678. || ts.isTypeAssertionExpression(current)
  679. || ts.isNonNullExpression(current)
  680. || ts.isSatisfiesExpression(current)
  681. ) {
  682. current = current.expression
  683. }
  684. return current
  685. }
  686. /** Return every value only when a type is a closed string-literal union. */
  687. function finiteStringTypeValues(type: ts.Type): Set<string> | undefined {
  688. if (type.flags & ts.TypeFlags.StringLiteral) {
  689. return new Set([(type as ts.StringLiteralType).value])
  690. }
  691. if (type.flags & ts.TypeFlags.Never) return new Set()
  692. if (!type.isUnion()) return undefined
  693. const values = new Set<string>()
  694. for (const member of type.types) {
  695. const memberValues = finiteStringTypeValues(member)
  696. if (!memberValues) return undefined
  697. addAll(values, memberValues)
  698. }
  699. return values
  700. }
  701. /** Return whether a variable declaration belongs to a const declaration list. */
  702. function isConstDeclaration(declaration: ts.VariableDeclaration): boolean {
  703. return (declaration.parent.flags & ts.NodeFlags.Const) !== 0
  704. }
  705. /** Return whether a declaration is visible to callers outside its source module. */
  706. function hasExportModifier(node: ts.Node): boolean {
  707. return ts.canHaveModifiers(node) && (ts.getModifiers(node)?.some((modifier) => {
  708. return modifier.kind === ts.SyntaxKind.ExportKeyword || modifier.kind === ts.SyntaxKind.DefaultKeyword
  709. }) ?? false)
  710. }
  711. /** Add every member of source to target. */
  712. function addAll<T>(target: Set<T>, source: ReadonlySet<T>): void {
  713. for (const value of source) target.add(value)
  714. }
  715. /** Return the union of two sets without mutating either input. */
  716. function unionSets<T>(left: ReadonlySet<T>, right: ReadonlySet<T>): Set<T> {
  717. const out = new Set(left)
  718. addAll(out, right)
  719. return out
  720. }
  721. function collectEventRelations(): Map<string, EventRelation> {
  722. const project = new TypeScriptProject(root)
  723. const sources = project.sourceFiles().flatMap((sourceFile): PackageSource[] => {
  724. const rel = project.relativePath(sourceFile)
  725. const match = /^packages\/[^/]+\/([^/]+)\/src\/.+\.ts$/.exec(rel)
  726. return match?.[1] ? [{ rel, pkg: match[1], sourceFile }] : []
  727. }).sort((left, right) => left.rel.localeCompare(right.rel))
  728. return new EventRelationCollector(project, sources).collect()
  729. }
  730. function relationPackages(map: Map<string, Set<string>>, pkgsByShort: Map<string, Pkg>): string {
  731. if (map.size === 0) return '-'
  732. return [...map.entries()]
  733. .sort(([a], [b]) => a.localeCompare(b))
  734. .map(([pkg, methods]) => `${pkgLink(pkgsByShort.get(pkg), pkg)} (${[...methods].sort().map(m => `\`${m}\``).join(', ')})`)
  735. .join(', ')
  736. }
  737. function listenerPackages(listeners: Set<string>, pkgsByShort: Map<string, Pkg>): string {
  738. if (listeners.size === 0) return '-'
  739. return [...listeners].sort().map(pkg => pkgLink(pkgsByShort.get(pkg), pkg)).join(', ')
  740. }
  741. function renderEventRelations(pkgs: Pkg[]): string {
  742. const events = collectEvents()
  743. const relations = collectEventRelations()
  744. const pkgsByShort = new Map(pkgs.map(pkg => [pkg.short, pkg]))
  745. const maintenance = 'generated: Cordis event declarations and producer/listener edges are resolved from the repository TypeScript Program'
  746. const lines = generatedHeader('Event Producer And Consumer Matrix')
  747. lines.push(
  748. '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. Receiver and event-name types also cover contained dispatch sites that deliberately bypass `ctx.emit`, such as subagent lifecycle containment.',
  749. '',
  750. '| Event | Mode | Declared in | Dispatchers | Listeners |',
  751. '| --- | --- | --- | --- | --- |',
  752. )
  753. for (const event of [...events].sort((a, b) => a.name.localeCompare(b.name))) {
  754. const relation = relations.get(event.name) ?? { dispatchers: new Map<string, Set<string>>(), listeners: new Set<string>() }
  755. lines.push(`| \`${event.name}\` | \`${event.mode}\` | ${sourceLink(event.source)} | ${relationPackages(relation.dispatchers, pkgsByShort)} | ${listenerPackages(relation.listeners, pkgsByShort)} |`)
  756. }
  757. // Every declared event needs a dispatcher: zero means dead vocabulary or an
  758. // unrecognized semantic dispatch shape. Listener-free extension points remain valid.
  759. const undispatched = [...events]
  760. .filter(event => (relations.get(event.name)?.dispatchers.size ?? 0) === 0)
  761. .map(event => event.name)
  762. .sort()
  763. if (undispatched.length > 0) {
  764. throw new Error(
  765. `event-producer-consumer matrix: no dispatcher found for declared event${undispatched.length > 1 ? 's' : ''} `
  766. + `${undispatched.map(name => `"${name}"`).join(', ')} — dead vocabulary, or a dispatch shape the semantic scan misses `
  767. + '(teach scripts/gen-doc-graphs.ts the shape)',
  768. )
  769. }
  770. const declared = new Set(events.map(event => event.name))
  771. const extra = [...relations.keys()].filter(event => !declared.has(event)).sort()
  772. if (extra.length > 0) {
  773. lines.push('', '## Non-harness or undeclared event strings seen in package source', '', '| Event string | Dispatchers | Listeners |', '| --- | --- | --- |')
  774. for (const event of extra) {
  775. const relation = relations.get(event)
  776. if (!relation) continue
  777. lines.push(`| \`${event}\` | ${relationPackages(relation.dispatchers, pkgsByShort)} | ${listenerPackages(relation.listeners, pkgsByShort)} |`)
  778. }
  779. }
  780. lines.push('', ...maintenanceFooter(maintenance))
  781. return lines.join('\n')
  782. }
  783. function renderLifecycle(): string {
  784. const maintenance = 'curated Mermaid sequence; exact event signatures live in the generated Cordis catalog'
  785. return [
  786. ...generatedHeader('Agent Turn And Step Lifecycle'),
  787. '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/*`.',
  788. '',
  789. '```mermaid',
  790. 'sequenceDiagram',
  791. ' participant User',
  792. ' participant Agent',
  793. ' participant Driver',
  794. ' participant Hooks as hook listeners',
  795. ' participant Prompt as ctx.systemPrompt',
  796. ' participant LLM as ctx.llm',
  797. ' participant Tools as ctx.tools',
  798. ' participant Session',
  799. ' participant Persistence',
  800. ' participant SDK as UI or SDK listener',
  801. ' User->>Agent: send(content)',
  802. ` Agent-->>SDK: ${mermaidCode('agent/queued')}`,
  803. ' Agent->>Driver: queued work wakes driver',
  804. ` Driver-->>SDK: ${mermaidCode('agent/status')} running`,
  805. ` Driver->>Session: ${mermaidCode('turn/start')}`,
  806. ` Driver->>Hooks: ${mermaidCode('agent/prompt-submit')} waterfall`,
  807. ' Hooks-->>Driver: allow, block, or add context',
  808. ` Driver->>Session: ${mermaidCode('user/message')} or rejected ${mermaidCode('turn/end')}`,
  809. ` Driver->>Prompt: ${mermaidCode('system-prompt/assemble')} waterfall`,
  810. ` Driver-->>Driver: ${mermaidCode('agent/pre-step')} serial checkpoint`,
  811. ` Driver->>Session: ${mermaidCode('step/start')}`,
  812. ` Driver->>LLM: ${mermaidCode('agent/request')} waterfall, then ${mermaidCode('llm/stream')} waterfall`,
  813. ' LLM-->>Driver: StreamChunk*',
  814. ` Driver->>Session: ${mermaidCode('assistant/chunk')}*`,
  815. ` Session-->>SDK: ${mermaidCode('session/event')} ${mermaidCode('assistant/chunk')}*`,
  816. ' alt final adapter or terminal in-band request failure',
  817. ` Driver->>Session: ${mermaidCode('step/end')}`,
  818. ` Driver->>Hooks: ${mermaidCode('agent/request-error')} waterfall`,
  819. ' Hooks-->>Driver: retry in a new step or preserve the original error',
  820. ' else model request succeeded',
  821. ` Driver->>Hooks: ${mermaidCode('agent/step-result')} waterfall`,
  822. ` Driver->>Session: ${mermaidCode('assistant/message')}`,
  823. ' Driver->>Tools: classify pending call by executionMode',
  824. ' loop barriers and bounded rolling pool, reclassify before start',
  825. ' opt call starts',
  826. ` Driver->>Session: ${mermaidCode('tool/call')}`,
  827. ' Driver->>Tools: ordered pre, concurrent execute',
  828. ' Tools-->>Session: tool-owned events when applicable',
  829. ' end',
  830. ' opt next model-order result ready',
  831. ' Driver->>Tools: ordered post',
  832. ` Driver->>Session: ${mermaidCode('tool/result')}`,
  833. ' end',
  834. ' end',
  835. ' Driver->>Session: post-tool context and steering',
  836. ` Driver->>Hooks: ${mermaidCode('agent/post-step')} serial checkpoint`,
  837. ` Driver->>Session: ${mermaidCode('step/end')}`,
  838. ` Driver->>Hooks: ${mermaidCode('agent/turn-continuation')} waterfall`,
  839. ` Driver->>Hooks: ${mermaidCode('agent/turn-stop')} serial terminal checkpoint`,
  840. ' end',
  841. ` Driver->>Session: ${mermaidCode('turn/end')}`,
  842. ` Driver->>Persistence: ${mermaidCode('session/flush')} parallel checkpoint`,
  843. ` Driver-->>SDK: ${mermaidCode('agent/status')} idle`,
  844. '```',
  845. '',
  846. 'The `assistant/message` edge records every successful provider call, including content-less and `max-tokens` finishes. Empty content stays out of derived history while the durable anchor retains usage and exact chunk provenance, including an explicit empty source set.',
  847. '',
  848. '`dsh-compact-basic` uses `agent/post-step` for pressure after those durable facts and `agent/request-error` only for canonical context overflow. Recovery compacts between the closed failed step and a fresh retry step, and returns retry only when the surface replacement generation advances; otherwise the original request error remains authoritative.',
  849. '',
  850. '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.',
  851. '',
  852. ...maintenanceFooter(maintenance),
  853. ].join('\n')
  854. }
  855. function renderToolPipeline(): string {
  856. const maintenance = 'curated Mermaid flow; exact tool schemas and event signatures live in generated catalogs'
  857. return [
  858. ...generatedHeader('Tool Execution Pipeline'),
  859. 'This graph shows where policy, hooks, sandboxing, filesystem guards, result rewriting, final-outcome observation, and UI rendering fit without changing the loop. The transformable extension points are the `tools/pre-execute`, `tools/execute`, and `tools/post-execute` waterfalls; monotonic guards and `tools/result` are the owner-enforced boundaries around them.',
  860. '',
  861. '```mermaid',
  862. 'flowchart TD',
  863. ' model["Assistant message contains tool-call block"]',
  864. ` toolCall["Session event: ${mermaidCode('tool/call')}<br/>logged before execution"]`,
  865. ' presentCall["UI pending card<br/>presentCall(args)"]',
  866. ` pre["${mermaidCode('tools/pre-execute')} waterfall<br/>hooks, permission, sandbox"]`,
  867. ' guards["Registered monotonic guards<br/>deny or abstain; identity protected"]',
  868. ' denied["denied or approval refused<br/>tool body skipped"]',
  869. ` approval["${mermaidCode('ctx.approval')} one-shot prompt<br/>absent or unanswerable: deny"]`,
  870. ` around["${mermaidCode('tools/execute')} waterfall<br/>timeout, retry, metrics (around dispatch)"]`,
  871. ' toolBody["Registered tool execute() body"]',
  872. ` fsGate["${mermaidCode('fs/write-intent')} or ${mermaidCode('fs/edit-intent')}<br/>tool-fs mutations only"]`,
  873. ` owned["Tool-owned session events<br/>${mermaidCode('todo/write')}, ${mermaidCode('fs/observed')}, ${mermaidCode('hook/invoked')}, ${mermaidCode('hook/result')}, ${mermaidCode('tool/code-dispatch')}"]`,
  874. ` post["${mermaidCode('tools/post-execute')} waterfall<br/>accept, block, replace, add context"]`,
  875. ` final["${mermaidCode('tools/result')} synchronous notification<br/>frozen authoritative outcome"]`,
  876. ' context["Active-batch additionalContexts FIFO<br/>context/message after recorded tool results"]',
  877. ` toolResult["Session event: ${mermaidCode('tool/result')}<br/>single model-facing outcome"]`,
  878. ' allResults["Tool batch settled<br/>recorded tool/result events complete"]',
  879. ' presentResult["UI completed card<br/>presentResult(args, result)"]',
  880. ' model --> toolCall',
  881. ' toolCall --> presentCall',
  882. ' toolCall --> pre',
  883. ' pre -->|allow| guards',
  884. ' guards -->|allow| around',
  885. ' guards -->|deny| denied',
  886. ' around --> toolBody',
  887. ' pre -->|deny| denied',
  888. ' pre -->|ask| approval',
  889. ' approval -->|allowed-once| guards',
  890. ' approval -->|rejected, cancelled, unavailable| denied',
  891. ' denied --> post',
  892. ' toolBody --> fsGate',
  893. ' fsGate --> toolBody',
  894. ' toolBody --> owned',
  895. ' toolBody --> around',
  896. ' around --> post',
  897. ' post --> final',
  898. ' final --> toolResult',
  899. ' toolResult --> presentResult',
  900. ' toolResult --> allResults',
  901. ' allResults --> context',
  902. '```',
  903. '',
  904. 'Filesystem read-before-edit checks stay below `tool-fs` on `fs/*` events. Generic pre/post waterfalls host hooks and approval policy; `ctx.approval` resolves asks before monotonic guards, and owner policy that must not be reordered remains a registered guard. Around-dispatch concerns such as timeouts wrap `tools/execute`, while `tools/result` observes the immutable outcome after transforms, lossless-JSON validation, and outer error normalization. This lets hooks span tool families without coupling the tools to one policy service. Code Mode sends both the reserved `run_code` transport and its serialized sub-calls through the pipeline; sub-calls carry the parent token, log `tool/code-dispatch`, surface denials as binding rejections, and omit `additionalContexts` to preserve call/result adjacency.',
  905. '',
  906. ...maintenanceFooter(maintenance),
  907. ].join('\n')
  908. }
  909. function renderSnapshotReplay(): string {
  910. const maintenance = 'curated Mermaid sequence based on the snapshot test harness'
  911. return [
  912. ...generatedHeader('ACP Snapshot Replay'),
  913. '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.',
  914. '',
  915. '```mermaid',
  916. 'sequenceDiagram',
  917. ' participant Recorder as Real API recording',
  918. ' participant Fixture as snapshot fixture',
  919. ' participant Workspace',
  920. ' participant Replay as llm-replay adapter',
  921. ' participant ACP as acp-agent subprocess',
  922. ' participant Golden as stdout golden',
  923. ' Recorder->>Fixture: session.jsonl + workspace inputs',
  924. ' Fixture->>Workspace: seed files and hook configs',
  925. ' Fixture->>Replay: recorded StreamChunk script',
  926. ` Replay->>ACP: deterministic ${mermaidCode('llm/stream')} chunks`,
  927. ' ACP->>Workspace: bash, fs, and hook side effects',
  928. ' ACP->>Golden: normalized sessionUpdate stream',
  929. ' Golden-->>ACP: diff must be empty',
  930. '```',
  931. '',
  932. '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.',
  933. '',
  934. ...maintenanceFooter(maintenance),
  935. ].join('\n')
  936. }
  937. function renderDocs(): GraphDoc[] {
  938. const pkgs = collectPackageGraph(root, GROUP_ORDER, 'gen-doc-graphs')
  939. const docs: GraphDoc[] = [
  940. { rel: 'docs/capability-seams.md', content: renderCapabilitySeams(pkgs) },
  941. ...APP_EXAMPLES.map(example => ({ rel: example.rel, content: renderAppComposition(example) })),
  942. { rel: 'docs/event-producer-consumer.md', content: renderEventRelations(pkgs) },
  943. { rel: 'docs/agent-lifecycle.md', content: renderLifecycle() },
  944. { rel: 'docs/tool-execution-pipeline.md', content: renderToolPipeline() },
  945. { rel: 'packages/ui/acp/snapshot-replay.md', content: renderSnapshotReplay() },
  946. ]
  947. docs.unshift({ rel: 'docs/graph-atlas.md', content: renderIndex(docs) })
  948. return docs
  949. }
  950. function renderIndex(docs: GraphDoc[]): string {
  951. const labels: Record<string, string> = {
  952. 'docs/capability-seams.md': 'capability seams and core services',
  953. 'examples/echo-agent/composition.md': 'echo-agent app composition',
  954. 'examples/repl-agent/composition.md': 'repl-agent app composition',
  955. 'examples/headless-agent/composition.md': 'headless-agent app composition',
  956. 'examples/tui-agent/composition.md': 'tui-agent app composition',
  957. 'examples/cordis-agent/composition.md': 'cordis-agent app composition',
  958. 'examples/acp-agent/composition.md': 'acp-agent app composition',
  959. 'docs/event-producer-consumer.md': 'event producer/consumer matrix',
  960. 'docs/agent-lifecycle.md': 'agent turn and step lifecycle',
  961. 'docs/tool-execution-pipeline.md': 'tool execution pipeline',
  962. 'packages/ui/acp/snapshot-replay.md': 'ACP snapshot replay',
  963. }
  964. const modes: Record<string, string> = {
  965. 'docs/capability-seams.md': 'hybrid generated',
  966. 'examples/echo-agent/composition.md': 'hybrid generated',
  967. 'examples/repl-agent/composition.md': 'hybrid generated',
  968. 'examples/headless-agent/composition.md': 'hybrid generated',
  969. 'examples/tui-agent/composition.md': 'hybrid generated',
  970. 'examples/cordis-agent/composition.md': 'hybrid generated',
  971. 'examples/acp-agent/composition.md': 'hybrid generated',
  972. 'docs/event-producer-consumer.md': 'hybrid generated',
  973. 'docs/agent-lifecycle.md': 'curated',
  974. 'docs/tool-execution-pipeline.md': 'curated',
  975. 'packages/ui/acp/snapshot-replay.md': 'curated',
  976. }
  977. const rows = [
  978. '| [module dependency graph](module-graph.md) | `generated` |',
  979. '| [tool schema catalog and package map](tool-catalog.md) | `generated` |',
  980. ...docs.map((doc) => {
  981. const link = graphIndexLink(doc.rel)
  982. return `| [${labels[doc.rel] ?? link}](${link}) | \`${modes[doc.rel] ?? 'generated'}\` |`
  983. }),
  984. ]
  985. const maintenance = 'mixed: each linked page declares generated, hybrid, or curated mode'
  986. return [
  987. ...generatedHeader('Documentation Graph Index'),
  988. '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).',
  989. '',
  990. 'The process decision behind this index is recorded in [the documentation graph RFC](rfc/implemented/process/2026-07-03-documentation-graph-atlas.md).',
  991. '',
  992. '| Graph | Mode |',
  993. '| --- | --- |',
  994. ...rows,
  995. '',
  996. 'Regenerate with `pnpm run gen-doc-graphs`; verify freshness with `pnpm run verify-doc-graphs`.',
  997. '',
  998. ...maintenanceFooter(maintenance),
  999. ].join('\n')
  1000. }
  1001. function main(): void {
  1002. const docs = renderDocs()
  1003. if (process.argv.includes('--check')) {
  1004. const stale: string[] = []
  1005. for (const doc of docs) {
  1006. const abs = resolve(root, doc.rel)
  1007. const committed = existsSync(abs) ? readFileSync(abs, 'utf8') : null
  1008. if (committed !== doc.content) stale.push(doc.rel)
  1009. }
  1010. if (stale.length === 0) {
  1011. console.log(`gen-doc-graphs: ${docs.length} graph doc(s) are up to date.`)
  1012. return
  1013. }
  1014. console.error(`gen-doc-graphs: stale graph doc(s): ${stale.join(', ')}. Run \`pnpm run gen-doc-graphs\` and commit the result.`)
  1015. process.exit(1)
  1016. }
  1017. for (const doc of docs) {
  1018. mkdirSync(dirname(resolve(root, doc.rel)), { recursive: true })
  1019. writeFileSync(resolve(root, doc.rel), doc.content)
  1020. }
  1021. console.log(`gen-doc-graphs: wrote ${docs.length} graph doc(s).`)
  1022. }
  1023. if (process.argv[1] && import.meta.filename === resolve(process.argv[1])) {
  1024. main()
  1025. }