gen-doc-graphs.ts 49 KB

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