gen-doc-graphs.ts 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371
  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 { projectCordisCatalog } from '@deepseek-ai/dsh-typert-generator'
  11. import { CORDIS_CATALOG_POLICY } from './gen-cordis-catalog.ts'
  12. import type { EventEntry, ServiceEntry } from '@deepseek-ai/dsh-typert-generator'
  13. import {
  14. collectPackageGraph,
  15. escapeMermaidLabel as escLabel,
  16. graphNodeId as nodeId,
  17. type PackageGraphNode,
  18. } from './package-graph.ts'
  19. import { TypeScriptProject } from './ts-project.ts'
  20. const root = resolve(import.meta.dirname, '..')
  21. type Pkg = PackageGraphNode
  22. interface GraphDoc {
  23. rel: string
  24. content: string
  25. }
  26. interface ServiceRole {
  27. key: string
  28. pkg: string
  29. title: string
  30. mode: 'core' | 'seam' | 'bundle'
  31. implementations?: string[]
  32. consumers?: string[]
  33. companions?: string[]
  34. note: string
  35. }
  36. interface ExamplePlugin {
  37. id: string
  38. name: string
  39. }
  40. interface EventRelation {
  41. dispatchers: Map<string, Set<string>>
  42. listeners: Set<string>
  43. }
  44. /** One scanned package source file and its owning package short name. */
  45. export interface PackageSource {
  46. /** Repository-relative path. */
  47. rel: string
  48. /** Package short name from the `packages/<group>/<pkg>/src` path. */
  49. pkg: string
  50. /** The bound program source file. */
  51. sourceFile: ts.SourceFile
  52. }
  53. type EventReceiverKind = 'context' | 'agent-dispatch' | 'events-service'
  54. const GROUP_ORDER = [
  55. 'util',
  56. 'llm',
  57. 'core',
  58. 'typert',
  59. 'goal',
  60. 'process',
  61. 'bash',
  62. 'pty',
  63. 'sandbox',
  64. 'fs',
  65. 'skill',
  66. 'compact',
  67. 'subagent',
  68. 'tasks',
  69. 'workflow',
  70. 'web',
  71. 'spill',
  72. 'todo',
  73. 'plan',
  74. 'cordis',
  75. 'hooks',
  76. 'session-persistence',
  77. 'session-query',
  78. 'session-title',
  79. 'telemetry',
  80. 'storage',
  81. 'workspace',
  82. 'support',
  83. 'acp',
  84. 'ui',
  85. ]
  86. const SERVICE_ROLES: ServiceRole[] = [
  87. {
  88. key: 'llm',
  89. pkg: 'llm',
  90. title: 'LLM adapter registry',
  91. mode: 'seam',
  92. implementations: ['llm-deepseek', 'llm-pi-ai', 'llm-replay'],
  93. consumers: ['agent-loop', 'compact-basic'],
  94. note: 'Adapters register provider implementations; the loop and compaction call the provider-neutral stream service.',
  95. },
  96. {
  97. key: 'tokenMeter',
  98. pkg: 'token-meter',
  99. title: 'Replay token measurement',
  100. mode: 'core',
  101. consumers: ['compact-basic'],
  102. note: 'Owns isolated per-session replay folds; pressure consumers share immutable revisioned measurements.',
  103. },
  104. {
  105. key: 'toolResultPrune',
  106. pkg: 'compact-tool-result-prune',
  107. title: 'Model-free tool-result pruning',
  108. mode: 'core',
  109. consumers: ['compact-basic'],
  110. note: 'Rewrites oversized current tool results through replayable single-node surface replacements before summary compaction.',
  111. },
  112. {
  113. key: 'sessions',
  114. pkg: 'session',
  115. title: 'In-memory session store',
  116. mode: 'core',
  117. consumers: ['agent-loop', 'agent', 'cli-demo', 'session-persistence', 'session-query', 'session-query-sqlite', 'subagent-inprocess', 'invariants'],
  118. note: 'Owns append-only Session instances and emits the durable session event feed.',
  119. },
  120. {
  121. key: 'invariants',
  122. pkg: 'invariants',
  123. title: 'Package-owned invariant registry',
  124. mode: 'core',
  125. consumers: ['session', 'agent', 'scope', 'agent-loop'],
  126. note: 'Companion subpaths register owner-local checks; the service owns selection, uniqueness, child fibers, and package-attributed failures.',
  127. },
  128. {
  129. key: 'typert',
  130. pkg: 'typert-registry',
  131. title: 'Runtime type registry',
  132. mode: 'core',
  133. consumers: ['typert-loader'],
  134. note: 'Plugins register live zod contributions directly or through dsh-typert-loader; runtime consumers query schemas and reflection metadata at their own edges.',
  135. },
  136. {
  137. key: 'sessionPersistence',
  138. pkg: 'session-persistence',
  139. title: 'Durable session persistence seam',
  140. mode: 'seam',
  141. implementations: ['session-persistence-jsonl', 'session-persistence-sqlite'],
  142. consumers: ['agent-loop', 'tool-bash', 'hooks-claude', 'hooks-codex', 'session-query', 'session-query-sqlite'],
  143. note: 'Backends persist the same SessionEvent vocabulary; apps choose a backend at composition time.',
  144. },
  145. {
  146. key: 'settings',
  147. pkg: 'settings',
  148. title: 'User-settings seam',
  149. mode: 'seam',
  150. implementations: ['settings-local'],
  151. consumers: ['llm-deepseek', 'llm-pi-ai', 'apiproxy'],
  152. note: 'Plugins register namespace schemas and resolve layered values; providers store the raw document. The LLM adapters register their entry config as the composition base under the user section; the web gateway serves redacted layered descriptors and writes the user layer.',
  153. },
  154. {
  155. key: 'credentials',
  156. pkg: 'credentials',
  157. title: 'Credential seam',
  158. mode: 'seam',
  159. implementations: ['credentials-local'],
  160. consumers: ['llm-deepseek', 'llm-pi-ai', 'apiproxy'],
  161. note: 'Configuration carries references to secrets; providers own the values. Consumers resolve per operation, so a rotated credential reaches the very next request; the web gateway exposes value-free views and write-only storage.',
  162. },
  163. {
  164. key: 'telemetry',
  165. pkg: 'session-telemetry',
  166. title: 'Session telemetry seam',
  167. mode: 'seam',
  168. implementations: ['session-telemetry-otel'],
  169. consumers: [],
  170. note: 'The seam captures, redacts, and hands session records to one backend; nothing else consumes the service — its output leaves the process.',
  171. },
  172. {
  173. key: 'storage',
  174. pkg: 'storage',
  175. title: 'Non-session storage hub',
  176. mode: 'seam',
  177. implementations: ['storage-json', 'storage-sqlite'],
  178. consumers: ['storage-domain'],
  179. note: 'Backends register side by side under names; data forms (domain first) mount on the hub and translate typed operations into opaque KV-unit primitives.',
  180. },
  181. {
  182. key: 'storageDomain',
  183. pkg: 'storage-domain',
  184. title: 'Domain data facility',
  185. mode: 'core',
  186. consumers: ['workspace'],
  187. note: 'Waits for every configured backend, then publishes the domain form as one lifecycle-bound service for typed durable state.',
  188. },
  189. {
  190. key: 'workspace',
  191. pkg: 'workspace',
  192. title: 'Workspace entity registry',
  193. mode: 'core',
  194. consumers: ['apiproxy'],
  195. note: 'Owns WorkspaceId-branded records over the domain facility; stable sessionIds accounts drive Host RPC and GUI projections.',
  196. },
  197. {
  198. key: 'sessionQuery',
  199. pkg: 'session-query',
  200. title: 'Session reads, traces, filters, and search',
  201. mode: 'seam',
  202. implementations: ['session-query-sqlite'],
  203. consumers: ['session-reference', 'tool-session-query'],
  204. note: 'The interface supplies exact reads, filters, and traces; its concrete backend adds full-text reconciliation, ranking, snippets, and cursor generations, while the model consumer owns workspace authority and cursor-free rendering.',
  205. },
  206. {
  207. key: 'sessionReferences',
  208. pkg: 'session-reference',
  209. title: 'Cross-session snapshot preparation',
  210. mode: 'core',
  211. note: 'Projects bounded current-surface conversation snapshots into durable untrusted message context; host adapters own mention syntax.',
  212. },
  213. {
  214. key: 'sessionTitle',
  215. pkg: 'session-title',
  216. title: 'Log-backed session titles',
  217. mode: 'seam',
  218. implementations: ['session-title-first-message-llm', 'session-title-all-messages-llm'],
  219. note: 'Owns the deterministic fallback, latest-title fold, and sole optional asynchronous provider registration.',
  220. },
  221. {
  222. key: 'systemPrompt',
  223. pkg: 'system-prompt',
  224. title: 'System prompt assembly registry',
  225. mode: 'core',
  226. consumers: ['agent-loop', 'tools', 'tool-fs', 'tool-pty', 'tool-web'],
  227. note: 'Collects prompt sections and model-facing tool schemas for each step.',
  228. },
  229. {
  230. key: 'tools',
  231. pkg: 'tools',
  232. title: 'Tool registry and guarded execution pipeline',
  233. mode: 'core',
  234. consumers: ['agent-loop', 'tool-ask-user', 'tool-bash', 'tool-cordis', 'tool-fs', 'tool-pty', 'tool-skill', 'tool-subagent', 'tool-todo', 'tool-web'],
  235. note: 'Registers capabilities, owns Code Mode transport, and routes calls through pre-policy, monotonic guards, around dispatch, post-policy, and final-result observation.',
  236. },
  237. {
  238. key: 'userInteraction',
  239. pkg: 'user-interaction',
  240. title: 'Human question/answer seam',
  241. mode: 'seam',
  242. consumers: ['tool-ask-user'],
  243. note: 'UI front doors provide the active human-answer provider; tool-ask-user pauses a tool call on the provider-neutral ask() promise.',
  244. },
  245. {
  246. key: 'planMode',
  247. pkg: 'plan-mode',
  248. title: 'Plan collaboration state',
  249. mode: 'core',
  250. 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.',
  251. },
  252. {
  253. key: 'commands',
  254. pkg: 'commands',
  255. title: 'Human command registry',
  256. mode: 'core',
  257. note: 'Plugins register direct human commands without sending invocations to the model.',
  258. },
  259. {
  260. key: 'sessionProjections',
  261. pkg: 'session-projection',
  262. title: 'Session projection units',
  263. mode: 'core',
  264. consumers: ['tool-todo', 'session-title', 'host-apiproxy'],
  265. note: 'Domains register state-driven fold units; the eager drive keeps per-session watermark states and api-proxy serves baselines and pushes changed values.',
  266. },
  267. {
  268. key: 'sessionProjectionCache',
  269. pkg: 'session-projection-cache',
  270. title: 'Persisted projection cache',
  271. mode: 'core',
  272. consumers: ['host-apiproxy'],
  273. note: 'Durably checkpoints projection unit states per session (throttled + turn/end/detach mandatory points) and serves the cold-read ladder: cache row + persistence tail replay, so listings never load full logs.',
  274. },
  275. {
  276. key: 'skills',
  277. pkg: 'skill',
  278. title: 'Skill provider registry',
  279. mode: 'seam',
  280. implementations: ['skill-local'],
  281. consumers: ['tool-skill'],
  282. note: 'Merges provider skill catalogs; tool-skill renders the session-prefix catalog and loads complete skill bodies.',
  283. },
  284. {
  285. key: 'agents',
  286. pkg: 'agent',
  287. title: 'Agent service',
  288. mode: 'core',
  289. consumers: ['agent-loop', 'acp', 'cli-demo', 'subagent-inprocess'],
  290. note: 'Owns live Agent handles, the create/resume factory seam, and process-local initiator propagation.',
  291. },
  292. {
  293. key: 'agentLoop',
  294. pkg: 'agent-loop',
  295. title: 'Concrete loop driver',
  296. mode: 'bundle',
  297. consumers: ['agent-spine-demo'],
  298. note: 'The one concrete loop plugin; extension packages depend on dsh-agent events and services, not on this package.',
  299. },
  300. {
  301. key: 'goals',
  302. pkg: 'goal',
  303. title: 'Same-session goal domain',
  304. mode: 'core',
  305. note: 'Folds revisioned objective state from the session log and keeps live continuation activation process-local.',
  306. },
  307. {
  308. key: 'subprocess',
  309. pkg: 'subprocess',
  310. title: 'Subprocess seam',
  311. mode: 'seam',
  312. implementations: ['subprocess-local'],
  313. consumers: ['bash-local', 'bash-sandbox', 'lsp-local', 'subagent-acp'],
  314. note: 'The bash executors, the LSP host, and the ACP subagent backend spawn their children through ctx.subprocess; the service owns tree lifetime, stdio dispositions (pipes, inherit, bounded spill-backed collection), and kill escalation.',
  315. },
  316. {
  317. key: 'bash',
  318. pkg: 'bash',
  319. title: 'Bash executor seam',
  320. mode: 'seam',
  321. implementations: ['bash-local', 'bash-sandbox'],
  322. consumers: ['tool-bash', 'hooks-claude', 'hooks-codex'],
  323. note: 'The model-facing bash tools and hook bridges consume this seam; sandboxed or remote executors replace bash-local without touching them.',
  324. },
  325. {
  326. key: 'bashEnv',
  327. pkg: 'tool-bash',
  328. title: 'Managed bash environment registry',
  329. mode: 'core',
  330. note: 'Plugins declare effect-scoped DSH_* facts; tool-bash collects one trusted snapshot per execution and the executor rebuilds the namespace.',
  331. },
  332. {
  333. key: 'pty',
  334. pkg: 'pty',
  335. title: 'Persistent PTY session registry',
  336. mode: 'seam',
  337. implementations: ['pty-local'],
  338. consumers: ['tool-pty'],
  339. note: 'The registry owns exact-Agent session identity and cleanup; backends own terminal mechanics, while tool-pty exposes the owner-scoped model surface.',
  340. },
  341. {
  342. key: 'sandbox',
  343. pkg: 'sandbox',
  344. title: 'Process-sandbox seam',
  345. mode: 'seam',
  346. implementations: ['sandbox-local'],
  347. consumers: ['bash-sandbox', 'pty-local'],
  348. 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.',
  349. },
  350. {
  351. key: 'sandboxPolicy',
  352. pkg: 'sandbox-policy',
  353. title: 'Sandbox policy home',
  354. mode: 'core',
  355. implementations: [],
  356. consumers: ['bash-sandbox', 'fs-sandbox', 'pty-local'],
  357. 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.',
  358. },
  359. {
  360. key: 'approval',
  361. pkg: 'approval',
  362. title: 'Approval seam',
  363. mode: 'seam',
  364. implementations: ['acp'],
  365. consumers: ['tools', 'tool-bash'],
  366. 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`.',
  367. },
  368. {
  369. key: 'permission',
  370. pkg: 'permission',
  371. title: 'Permission presets',
  372. mode: 'core',
  373. implementations: [],
  374. 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.',
  375. },
  376. {
  377. key: 'codeRuntime',
  378. pkg: 'code-runtime',
  379. title: 'Code-execution seam',
  380. mode: 'seam',
  381. implementations: ['code-runtime-worker'],
  382. consumers: ['tools'],
  383. 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).',
  384. },
  385. {
  386. key: 'fs',
  387. pkg: 'fs',
  388. title: 'Filesystem provider seam',
  389. mode: 'seam',
  390. implementations: ['fs-local', 'fs-sandbox'],
  391. consumers: ['tool-fs'],
  392. companions: ['fs-policy'],
  393. 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.',
  394. },
  395. {
  396. key: 'compact',
  397. pkg: 'compact',
  398. title: 'Compaction seam',
  399. mode: 'seam',
  400. implementations: ['compact-basic'],
  401. consumers: ['compact-basic'],
  402. note: 'The basic backend consumes post-step pressure and request-error recovery events; a model-facing compact tool remains deferred.',
  403. },
  404. {
  405. key: 'subagents',
  406. pkg: 'subagent',
  407. title: 'Subagent provider and continuation service',
  408. mode: 'seam',
  409. implementations: ['subagent-spawn', 'subagent-fork', 'subagent-acp'],
  410. consumers: ['tool-subagent', 'tool-subagent-control', 'tool-ralph'],
  411. note: 'Providers implement transports; the service also owns optional Activation-based continuation orchestration, tool-subagent selects one-shot or continuable delegation, tool-subagent-control delivers follow-ups, and tool-ralph requires one fresh structured-output route.',
  412. },
  413. {
  414. key: 'tasks',
  415. pkg: 'tasks',
  416. title: 'Background task registry',
  417. mode: 'seam',
  418. implementations: ['tasks-local'],
  419. consumers: ['tool-bash', 'tool-pty', 'tool-subagent', 'tool-tasks'],
  420. 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; tasks-local is the process-local registry.',
  421. },
  422. {
  423. key: 'web',
  424. pkg: 'web',
  425. title: 'Web access provider registry',
  426. mode: 'seam',
  427. implementations: ['web-search-exa', 'web-search-perplexity', 'web-search-deepseek', 'web-fetch-local'],
  428. consumers: ['tool-web'],
  429. note: 'Search and fetch providers register into one ctx.web seam; tool-web owns the stable model-facing names.',
  430. },
  431. {
  432. key: 'spillStore',
  433. pkg: 'spill',
  434. title: 'Spill storage seam',
  435. mode: 'seam',
  436. implementations: ['spill-local'],
  437. consumers: ['spill-policy'],
  438. 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.',
  439. },
  440. {
  441. key: 'directoryPicker',
  442. pkg: 'directory-picker',
  443. title: 'Workspace-directory picking seam',
  444. mode: 'seam',
  445. implementations: ['directory-picker-native', 'directory-picker-browse'],
  446. consumers: ['apiproxy'],
  447. note: 'Discriminated interaction capability: the native backend opens one OS chooser on the host display, the browse backend serves listing/creation primitives for the in-app browser; dual-face backends fill ui-workspace directory-flow slots from their browser halves (no wire advertisement).',
  448. },
  449. {
  450. key: 'httpServer',
  451. pkg: 'webserver',
  452. title: 'HTTP route registration',
  453. mode: 'core',
  454. consumers: ['connection', 'modules', 'hmr'],
  455. note: 'Plain node:http carrier: named-route registry, index transform taps, and the static dist fallback; web-transport plugins register their own routes.',
  456. },
  457. {
  458. key: 'clientModuleHost',
  459. pkg: 'modules',
  460. title: 'Client plugin graph host',
  461. mode: 'core',
  462. consumers: ['hmr'],
  463. note: 'Composes the __DSH_BOOT__ entry graph from an incremental dshClient scan, serves plugin bundles, and notifies rebuilt/graph-changed subscribers.',
  464. },
  465. {
  466. key: 'workflows',
  467. pkg: 'workflow',
  468. title: 'Workflow script engine',
  469. mode: 'seam',
  470. implementations: ['workflow-workerthread'],
  471. consumers: ['tool-workflow', 'tool-ralph'],
  472. 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.',
  473. },
  474. ]
  475. function generatedHeader(title: string): string[] {
  476. return [
  477. '<!-- Generated by scripts/gen-doc-graphs.ts - do not edit by hand.',
  478. ' Run `pnpm run gen-doc-graphs` to regenerate. -->',
  479. '',
  480. `# ${title}`,
  481. '',
  482. ]
  483. }
  484. function maintenanceFooter(source: string): string[] {
  485. return [`Maintenance mode: ${source}.`, '']
  486. }
  487. function graphIndexLink(rel: string): string {
  488. return relative('docs', rel).replaceAll('\\', '/')
  489. }
  490. function linkFromDoc(docRel: string, targetRel: string): string {
  491. return relative(dirname(docRel), targetRel).replaceAll('\\', '/')
  492. }
  493. function mermaidCode(value: string): string {
  494. return `<code>${value.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;')}</code>`
  495. }
  496. function repoLink(path: string, label: string, up = '..'): string {
  497. return `[${label}](${up}/${path})`
  498. }
  499. function sourceLink(source: string, up = '..'): string {
  500. return repoLink(source.split(':')[0] ?? source, `\`${source}\``, up)
  501. }
  502. function pkgLink(pkg: Pkg | undefined, fallback: string, up = '..'): string {
  503. return pkg ? repoLink(pkg.rel, `\`${pkg.short}\``, up) : `\`${fallback}\``
  504. }
  505. function pkgList(names: string[] | undefined, pkgsByShort: Map<string, Pkg>): string {
  506. if (!names || names.length === 0) return '-'
  507. return names.map(name => pkgLink(pkgsByShort.get(name), name)).join(', ')
  508. }
  509. function tableCell(value: string): string {
  510. return value.replace(/\|/g, '\\|').replace(/\n/g, '<br>')
  511. }
  512. function assertServiceRolesComplete(services: readonly ServiceEntry[]): void {
  513. const discovered = new Set(services.map(service => service.key))
  514. const classified = new Set(SERVICE_ROLES.map(role => role.key))
  515. const missing = [...discovered].filter(key => !classified.has(key)).sort()
  516. const stale = [...classified].filter(key => !discovered.has(key)).sort()
  517. if (missing.length || stale.length) {
  518. throw new Error([
  519. missing.length ? `missing service role classification: ${missing.join(', ')}` : '',
  520. stale.length ? `stale service role classification: ${stale.join(', ')}` : '',
  521. ].filter(Boolean).join('; '))
  522. }
  523. }
  524. function renderCapabilitySeams(pkgs: Pkg[], services: readonly ServiceEntry[]): string {
  525. assertServiceRolesComplete(services)
  526. const pkgsByShort = new Map(pkgs.map(pkg => [pkg.short, pkg]))
  527. 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'
  528. const nodes = new Map<string, string>()
  529. const edges = new Set<string>()
  530. const companionEdges = new Set<string>()
  531. const addNode = (id: string, label: string): void => {
  532. if (!nodes.has(id)) nodes.set(id, ` ${id}["${escLabel(label)}"]`)
  533. }
  534. const addEdge = (from: string, to: string): void => { edges.add(` ${from} --> ${to}`) }
  535. const lines = generatedHeader('Capability Seams And Core Services')
  536. lines.push(
  537. '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.',
  538. '',
  539. '```mermaid',
  540. 'flowchart LR',
  541. )
  542. for (const role of SERVICE_ROLES) {
  543. const svc = nodeId('svc', role.key)
  544. const owner = nodeId('pkg', role.pkg)
  545. addNode(owner, role.pkg)
  546. addNode(svc, `ctx.${role.key}<br/>${role.title}`)
  547. addEdge(owner, svc)
  548. for (const impl of role.implementations ?? []) {
  549. addNode(nodeId('pkg', impl), impl)
  550. addEdge(nodeId('pkg', impl), svc)
  551. }
  552. for (const consumer of role.consumers ?? []) {
  553. addNode(nodeId('pkg', consumer), consumer)
  554. addEdge(svc, nodeId('pkg', consumer))
  555. }
  556. for (const companion of role.companions ?? []) {
  557. addNode(nodeId('pkg', companion), companion)
  558. companionEdges.add(` ${svc} -. event gate .-> ${nodeId('pkg', companion)}`)
  559. }
  560. }
  561. lines.push(...nodes.values(), ...[...edges].sort(), ...[...companionEdges].sort())
  562. lines.push('```', '', '| ctx key | Role | Owner | Implementations | Direct consumers | Companion plugins | Note |', '| --- | --- | --- | --- | --- | --- | --- |')
  563. for (const role of SERVICE_ROLES) {
  564. 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)} |`)
  565. }
  566. lines.push('', ...maintenanceFooter(maintenance))
  567. return lines.join('\n')
  568. }
  569. function parseExampleCordis(rel: string): ExamplePlugin[] {
  570. const text = readFileSync(resolve(root, rel), 'utf8')
  571. const plugins: ExamplePlugin[] = []
  572. let current: { id: string; name?: string } | null = null
  573. const flush = (): void => {
  574. if (current?.name) plugins.push({ id: current.id, name: current.name })
  575. }
  576. for (const line of text.split('\n')) {
  577. const id = /^-\s+id:\s+(.+?)\s*$/.exec(line)
  578. if (id?.[1] !== undefined) {
  579. flush()
  580. current = { id: stripYamlScalar(id[1]) }
  581. continue
  582. }
  583. const name = /^\s+name:\s+(.+?)\s*$/.exec(line)
  584. if (name?.[1] !== undefined && current) current.name = stripYamlScalar(name[1])
  585. }
  586. flush()
  587. return plugins
  588. }
  589. function stripYamlScalar(value: string): string {
  590. return value.trim().replace(/^['"]|['"]$/g, '')
  591. }
  592. const APP_EXAMPLES = [
  593. {
  594. id: 'dsh_base',
  595. rel: 'apps/cli/composition.md',
  596. title: 'DSH Base Composition',
  597. label: 'apps/cli/config/base.cordis.yml',
  598. config: 'apps/cli/config/base.cordis.yml',
  599. summary: 'The raw CLI applies one required caller-selected patch list over this shared base; Web and headless apply their own shipped overlays.',
  600. },
  601. {
  602. id: 'headless',
  603. rel: 'examples/headless-agent/composition.md',
  604. title: 'Headless Agent App Composition',
  605. label: 'examples/headless-agent',
  606. config: 'examples/headless-agent/cordis.yml',
  607. 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.',
  608. },
  609. {
  610. id: 'acp',
  611. rel: 'examples/acp-agent/composition.md',
  612. title: 'ACP Automation App Composition',
  613. label: 'examples/acp-agent',
  614. config: 'examples/acp-agent/cordis.yml',
  615. summary: 'The ACP demo exposes fresh baseline-prompt agent sessions to programmatic clients over JSON-RPC stdio, with no stdout logger, human UI, or pre-created agent.',
  616. },
  617. ]
  618. type AppExample = typeof APP_EXAMPLES[number]
  619. function renderAppExpansion(lines: string[], appNode: string, pluginName: string): void {
  620. const agentCore = nodeId('bundle', 'agent_core')
  621. const jsonl = nodeId('bundle', 'jsonl')
  622. lines.push(` ${appNode} --> ${agentCore}["@deepseek-ai/dsh-agent-spine-demo"]`)
  623. lines.push(` ${appNode} --> ${jsonl}["@deepseek-ai/dsh-session-persistence-jsonl"]`)
  624. if (pluginName === '@deepseek-ai/dsh-cli-demo') {
  625. lines.push(` ${appNode} --> ${nodeId('frontdoor', 'cli')}["one-shot driver<br/>format-pure stdout<br/>fresh top-level agent"]`)
  626. } else if (pluginName === '@deepseek-ai/dsh-acp-demo') {
  627. lines.push(` ${appNode} --> ${nodeId('frontdoor', 'acp')}["@deepseek-ai/dsh-acp<br/>automation-only JSON-RPC stdio<br/>fresh sessions created by client"]`)
  628. }
  629. lines.push(
  630. ` ${agentCore} --> ${nodeId('spine', 'llm')}["ctx.llm"]`,
  631. ` ${agentCore} --> ${nodeId('spine', 'sessions')}["ctx.sessions"]`,
  632. ` ${agentCore} --> ${nodeId('spine', 'tools')}["ctx.tools + tool-bash"]`,
  633. ` ${agentCore} --> ${nodeId('spine', 'loop')}["ctx.agents + ctx.agentLoop"]`,
  634. )
  635. }
  636. function renderAppComposition(example: AppExample): string {
  637. const plugins = parseExampleCordis(example.config)
  638. const maintenance = 'hybrid: the leaf plugin list is parsed from its `cordis.yml`; app package expansion is curated from package source'
  639. const lines = generatedHeader(example.title)
  640. lines.push(
  641. example.summary,
  642. '',
  643. '```mermaid',
  644. 'flowchart LR',
  645. ` cfg["${escLabel(example.label)}<br/>cordis.yml"]`,
  646. )
  647. for (const plugin of plugins) {
  648. const pluginNode = nodeId(`plugin_${example.id}`, plugin.id)
  649. lines.push(` ${pluginNode}["${escLabel(plugin.id)}<br/>${escLabel(plugin.name)}"]`)
  650. lines.push(` cfg --> ${pluginNode}`)
  651. if (plugin.name === '@deepseek-ai/dsh-cli-demo' || plugin.name === '@deepseek-ai/dsh-acp-demo') {
  652. renderAppExpansion(lines, pluginNode, plugin.name)
  653. }
  654. }
  655. lines.push(
  656. '```',
  657. '',
  658. '| Plugin id | Package / module |',
  659. '| --- | --- |',
  660. ...plugins.map(plugin => `| \`${plugin.id}\` | \`${plugin.name}\` |`),
  661. '',
  662. `Source config: [\`${example.config}\`](${linkFromDoc(example.rel, example.config)}).`,
  663. )
  664. lines.push('', ...maintenanceFooter(maintenance))
  665. return lines.join('\n')
  666. }
  667. type CallSiteIndex = Map<ts.SignatureDeclaration | ts.JSDocSignature, ts.CallExpression[]>
  668. /**
  669. * The only method names visitSource classifies; receiver typing runs on these
  670. * alone. Obligation: every method name matched by a branch inside visitSource
  671. * must appear here — the prefilter drops non-members before any branch runs,
  672. * so a branch for an unlisted name is silently dead.
  673. */
  674. const EVENT_API_METHODS = new Set(['on', 'once', 'emit', 'parallel', 'serial', 'waterfall', 'dispatch'])
  675. /** Collect event dispatch/listener relations from real cross-file receiver types. */
  676. export class EventRelationCollector {
  677. private readonly relations = new Map<string, EventRelation>()
  678. private readonly fileCallSites = new Map<ts.SourceFile, CallSiteIndex>()
  679. private readonly localCalleeProofs = new Map<ts.FunctionDeclaration, boolean>()
  680. private globalCallSites: CallSiteIndex | null = null
  681. private readonly contextType: ts.Type
  682. private readonly agentDispatchType: ts.Type
  683. private readonly eventsServiceType: ts.Type
  684. private readonly packageSourceFiles: ReadonlySet<ts.SourceFile>
  685. constructor(
  686. private readonly project: TypeScriptProject,
  687. private readonly sources: readonly PackageSource[],
  688. ) {
  689. this.contextType = this.declaredType('vendor/cordis/src/context.ts', 'Context')
  690. this.agentDispatchType = this.declaredType('packages/core/agent/src/dispatch.ts', 'AgentEventDispatch')
  691. this.eventsServiceType = this.declaredType('vendor/cordis/src/events.ts', 'EventsService')
  692. this.packageSourceFiles = new Set(sources.map(source => source.sourceFile))
  693. }
  694. /** Return all event relations discovered from the Program. */
  695. collect(): Map<string, EventRelation> {
  696. for (const source of this.sources) this.visitSource(source)
  697. return this.relations
  698. }
  699. /** Resolve one named class/interface declaration to its merged instance type. */
  700. private declaredType(relativePath: string, name: string): ts.Type {
  701. const sourceFile = this.project.sourceFile(relativePath)
  702. const declaration = sourceFile.statements.find((statement): statement is ts.ClassDeclaration | ts.InterfaceDeclaration => {
  703. return (ts.isClassDeclaration(statement) || ts.isInterfaceDeclaration(statement)) && statement.name?.text === name
  704. })
  705. const symbol = declaration?.name && this.project.checker.getSymbolAtLocation(declaration.name)
  706. if (!symbol) throw new Error(`cannot resolve TypeScript type ${name} from ${relativePath}`)
  707. return this.project.checker.getDeclaredTypeOfSymbol(symbol)
  708. }
  709. /** Index resolved function calls in the given files for narrow argument-flow recovery. */
  710. private buildCallSiteIndex(files: Iterable<ts.SourceFile>): CallSiteIndex {
  711. const index: CallSiteIndex = new Map()
  712. const visit = (node: ts.Node): void => {
  713. if (ts.isCallExpression(node)) {
  714. const declaration = this.project.checker.getResolvedSignature(node)?.declaration
  715. if (declaration) {
  716. const calls = index.get(declaration) ?? []
  717. calls.push(node)
  718. index.set(declaration, calls)
  719. }
  720. }
  721. ts.forEachChild(node, visit)
  722. }
  723. for (const file of files) visit(file)
  724. return index
  725. }
  726. /**
  727. * Return every indexed call resolving to one local helper declaration.
  728. * Fast path: when every same-file reference to the non-exported helper is
  729. * provably a direct callee, module scoping confines all of its calls to that
  730. * file, so only that file is indexed. Any other reference shape may alias
  731. * the function value outward, so the original full package-source index
  732. * decides instead.
  733. */
  734. private callSitesFor(owner: ts.FunctionDeclaration): ts.CallExpression[] {
  735. if (!this.globalCallSites && !this.provenLocalCallee(owner)) {
  736. this.globalCallSites = this.buildCallSiteIndex(this.packageSourceFiles)
  737. }
  738. if (this.globalCallSites) return this.globalCallSites.get(owner) ?? []
  739. const file = owner.getSourceFile()
  740. let index = this.fileCallSites.get(file)
  741. if (!index) {
  742. index = this.buildCallSiteIndex([file])
  743. this.fileCallSites.set(file, index)
  744. }
  745. return index.get(owner) ?? []
  746. }
  747. /**
  748. * Prove every same-file reference to one helper is a direct callee. The
  749. * proof owns its premises: an exported helper or a helper in a global
  750. * script file (no import/export means program-wide scope, callable from
  751. * another file with no same-file reference at all) fails immediately.
  752. * Alias escapes (re-export statements, default exports, value reads)
  753. * resolve back to the owner symbol at a non-callee position and fail the
  754. * proof, as does anything the scan cannot positively classify.
  755. */
  756. private provenLocalCallee(owner: ts.FunctionDeclaration): boolean {
  757. const cached = this.localCalleeProofs.get(owner)
  758. if (cached !== undefined) return cached
  759. if (hasExportModifier(owner) || !ts.isExternalModule(owner.getSourceFile())) {
  760. this.localCalleeProofs.set(owner, false)
  761. return false
  762. }
  763. const name = owner.name
  764. const ownerSymbol = name && this.project.checker.getSymbolAtLocation(name)
  765. let proven = !!ownerSymbol
  766. const refersToOwner = (identifier: ts.Identifier): boolean => {
  767. // Shorthand properties resolve to the property symbol; ask for the value side.
  768. const local = ts.isShorthandPropertyAssignment(identifier.parent)
  769. ? this.project.checker.getShorthandAssignmentValueSymbol(identifier.parent)
  770. : this.project.checker.getSymbolAtLocation(identifier)
  771. if (!local) return false
  772. const symbol = local.flags & ts.SymbolFlags.Alias
  773. ? this.project.checker.getAliasedSymbol(local)
  774. : local
  775. return symbol === ownerSymbol
  776. }
  777. const visit = (node: ts.Node): void => {
  778. if (!proven) return
  779. if (ts.isIdentifier(node) && node !== name && node.text === name?.text
  780. && !isDirectCallee(node) && refersToOwner(node)) {
  781. proven = false
  782. return
  783. }
  784. ts.forEachChild(node, visit)
  785. }
  786. visit(owner.getSourceFile())
  787. this.localCalleeProofs.set(owner, proven)
  788. return proven
  789. }
  790. /** Walk one package source file and classify event API calls by receiver type. */
  791. private visitSource(source: PackageSource): void {
  792. const visit = (node: ts.Node): void => {
  793. if (ts.isCallExpression(node)) {
  794. if (this.isAgentEventEmitter(node.expression)) {
  795. const event = node.arguments[2]
  796. if (event) {
  797. for (const name of this.finiteStringValues(event) ?? []) {
  798. this.addDispatcher(name, source.pkg, 'emitAgentEvent')
  799. }
  800. }
  801. } else if (ts.isPropertyAccessExpression(node.expression) && EVENT_API_METHODS.has(node.expression.name.text)) {
  802. const receiverKind = this.receiverKind(node.expression.expression)
  803. const method = node.expression.name.text
  804. if (receiverKind === 'events-service' && method === 'dispatch') {
  805. const argumentList = node.arguments[1]
  806. if (argumentList) {
  807. for (const event of this.eventNamesFromArgumentList(argumentList, new Set())) {
  808. this.addDispatcher(event, source.pkg, 'events.dispatch')
  809. }
  810. }
  811. } else if (receiverKind === 'context' || receiverKind === 'agent-dispatch') {
  812. const eventNames = this.eventNamesFromCall(node, receiverKind)
  813. if (method === 'on' || method === 'once') {
  814. for (const event of eventNames) this.ensure(event).listeners.add(source.pkg)
  815. } else if (method === 'emit' || method === 'parallel' || method === 'serial' || method === 'waterfall') {
  816. for (const event of eventNames) this.addDispatcher(event, source.pkg, method)
  817. }
  818. }
  819. }
  820. }
  821. ts.forEachChild(node, visit)
  822. }
  823. visit(source.sourceFile)
  824. }
  825. /** Match the exported contained-notification helper by declaration identity. */
  826. private isAgentEventEmitter(expression: ts.Expression): boolean {
  827. if (!ts.isIdentifier(expression)) return false
  828. const local = this.project.checker.getSymbolAtLocation(expression)
  829. if (!local) return false
  830. const symbol = local.flags & ts.SymbolFlags.Alias
  831. ? this.project.checker.getAliasedSymbol(local)
  832. : local
  833. const declarations = symbol.declarations ?? []
  834. return declarations.some((declaration) => {
  835. return ts.isFunctionDeclaration(declaration)
  836. && declaration.name?.text === 'emitAgentEvent'
  837. && this.project.relativePath(declaration.getSourceFile()) === 'packages/core/agent/src/dispatch.ts'
  838. })
  839. }
  840. /** Classify a receiver using assignability to the repository's actual event API types. */
  841. private receiverKind(receiver: ts.Expression): EventReceiverKind | undefined {
  842. const type = this.project.checker.getTypeAtLocation(receiver)
  843. if (type.flags & (ts.TypeFlags.Any | ts.TypeFlags.Unknown | ts.TypeFlags.Never)) return undefined
  844. if (this.project.checker.isTypeAssignableTo(type, this.eventsServiceType)) return 'events-service'
  845. if (this.project.checker.isTypeAssignableTo(type, this.contextType)) return 'context'
  846. if (this.project.checker.isTypeAssignableTo(type, this.agentDispatchType)) return 'agent-dispatch'
  847. return undefined
  848. }
  849. /** Resolve the event-name argument for Context and fused agent dispatch calls. */
  850. private eventNamesFromCall(call: ts.CallExpression, receiverKind: Exclude<EventReceiverKind, 'events-service'>): Set<string> {
  851. const candidates = receiverKind === 'context' ? call.arguments.slice(0, 2) : call.arguments.slice(0, 1)
  852. for (const candidate of candidates) {
  853. const values = this.finiteStringValues(candidate)
  854. if (values) return values
  855. }
  856. return new Set()
  857. }
  858. /** Recover the event slot from the argument array handed to EventsService.dispatch(). */
  859. private eventNamesFromArgumentList(expression: ts.Expression, seen: Set<ts.Node>): Set<string> {
  860. const current = unwrapExpression(expression)
  861. if (seen.has(current)) return new Set()
  862. seen.add(current)
  863. if (ts.isArrayLiteralExpression(current)) {
  864. for (const element of current.elements.slice(0, 2)) {
  865. if (ts.isOmittedExpression(element) || ts.isSpreadElement(element)) continue
  866. const values = this.finiteStringValues(element)
  867. if (values) return values
  868. }
  869. return new Set()
  870. }
  871. if (ts.isConditionalExpression(current)) {
  872. return unionSets(
  873. this.eventNamesFromArgumentList(current.whenTrue, new Set(seen)),
  874. this.eventNamesFromArgumentList(current.whenFalse, new Set(seen)),
  875. )
  876. }
  877. if (!ts.isIdentifier(current)) return new Set()
  878. const symbol = this.project.checker.getSymbolAtLocation(current)
  879. if (!symbol) return new Set()
  880. const events = new Set<string>()
  881. for (const declaration of symbol.declarations ?? []) {
  882. if (ts.isVariableDeclaration(declaration) && declaration.initializer && isConstDeclaration(declaration)) {
  883. addAll(events, this.eventNamesFromArgumentList(declaration.initializer, new Set(seen)))
  884. } else if (ts.isParameter(declaration)) {
  885. addAll(events, this.eventNamesFromParameter(declaration, seen))
  886. }
  887. }
  888. return events
  889. }
  890. /** Follow a non-exported local helper parameter back to every resolved call site. */
  891. private eventNamesFromParameter(parameter: ts.ParameterDeclaration, seen: Set<ts.Node>): Set<string> {
  892. const owner = parameter.parent
  893. if (!ts.isFunctionDeclaration(owner) || hasExportModifier(owner)) return new Set()
  894. const index = owner.parameters.indexOf(parameter)
  895. if (index < 0) return new Set()
  896. const events = new Set<string>()
  897. for (const call of this.callSitesFor(owner)) {
  898. const argument = call.arguments[index]
  899. if (argument) addAll(events, this.eventNamesFromArgumentList(argument, new Set(seen)))
  900. }
  901. return events
  902. }
  903. /** Return a finite string-literal value set, rejecting widened and generic strings. */
  904. private finiteStringValues(expression: ts.Expression): Set<string> | undefined {
  905. const current = unwrapExpression(expression)
  906. if (ts.isStringLiteralLike(current)) return new Set([current.text])
  907. if (this.isForwardedAgentEventParameter(current)) return undefined
  908. return finiteStringTypeValues(this.project.checker.getTypeAtLocation(current))
  909. }
  910. /** Reject the contextual parameter inside the AgentEventDispatch forwarding object. */
  911. private isForwardedAgentEventParameter(expression: ts.Expression): boolean {
  912. if (!ts.isIdentifier(expression)) return false
  913. const declarations = this.project.checker.getSymbolAtLocation(expression)?.declarations ?? []
  914. return declarations.some((declaration) => {
  915. if (!ts.isParameter(declaration)) return false
  916. const method = declaration.parent
  917. if (!ts.isMethodDeclaration(method) || !ts.isObjectLiteralExpression(method.parent)) return false
  918. const contextualType = this.project.checker.getContextualType(method.parent)
  919. return contextualType !== undefined
  920. && this.project.checker.isTypeAssignableTo(contextualType, this.agentDispatchType)
  921. })
  922. }
  923. /** Get or create one relation row. */
  924. private ensure(event: string): EventRelation {
  925. const existing = this.relations.get(event)
  926. if (existing) return existing
  927. const relation = { dispatchers: new Map<string, Set<string>>(), listeners: new Set<string>() }
  928. this.relations.set(event, relation)
  929. return relation
  930. }
  931. /** Add one dispatcher method without duplicating package/method labels. */
  932. private addDispatcher(event: string, pkg: string, method: string): void {
  933. const relation = this.ensure(event)
  934. const methods = relation.dispatchers.get(pkg) ?? new Set<string>()
  935. methods.add(method)
  936. relation.dispatchers.set(pkg, methods)
  937. }
  938. }
  939. /** Return whether an identifier is the callee of a call, seen through value-preserving wrappers. */
  940. function isDirectCallee(identifier: ts.Identifier): boolean {
  941. let current: ts.Node = identifier
  942. while (
  943. ts.isParenthesizedExpression(current.parent)
  944. || ts.isAsExpression(current.parent)
  945. || ts.isTypeAssertionExpression(current.parent)
  946. || ts.isNonNullExpression(current.parent)
  947. || ts.isSatisfiesExpression(current.parent)
  948. ) {
  949. current = current.parent
  950. }
  951. return ts.isCallExpression(current.parent) && current.parent.expression === current
  952. }
  953. /** Peel syntax-only wrappers that do not change an expression's runtime value. */
  954. function unwrapExpression(expression: ts.Expression): ts.Expression {
  955. let current = expression
  956. while (
  957. ts.isParenthesizedExpression(current)
  958. || ts.isAsExpression(current)
  959. || ts.isTypeAssertionExpression(current)
  960. || ts.isNonNullExpression(current)
  961. || ts.isSatisfiesExpression(current)
  962. ) {
  963. current = current.expression
  964. }
  965. return current
  966. }
  967. /** Return every value only when a type is a closed string-literal union. */
  968. function finiteStringTypeValues(type: ts.Type): Set<string> | undefined {
  969. if (type.flags & ts.TypeFlags.StringLiteral) {
  970. return new Set([(type as ts.StringLiteralType).value])
  971. }
  972. if (type.flags & ts.TypeFlags.Never) return new Set()
  973. if (!type.isUnion()) return undefined
  974. const values = new Set<string>()
  975. for (const member of type.types) {
  976. const memberValues = finiteStringTypeValues(member)
  977. if (!memberValues) return undefined
  978. addAll(values, memberValues)
  979. }
  980. return values
  981. }
  982. /** Return whether a variable declaration belongs to a const declaration list. */
  983. function isConstDeclaration(declaration: ts.VariableDeclaration): boolean {
  984. return (declaration.parent.flags & ts.NodeFlags.Const) !== 0
  985. }
  986. /** Return whether a declaration is visible to callers outside its source module. */
  987. function hasExportModifier(node: ts.Node): boolean {
  988. return ts.canHaveModifiers(node) && (ts.getModifiers(node)?.some((modifier) => {
  989. return modifier.kind === ts.SyntaxKind.ExportKeyword || modifier.kind === ts.SyntaxKind.DefaultKeyword
  990. }) ?? false)
  991. }
  992. /** Add every member of source to target. */
  993. function addAll<T>(target: Set<T>, source: ReadonlySet<T>): void {
  994. for (const value of source) target.add(value)
  995. }
  996. /** Return the union of two sets without mutating either input. */
  997. function unionSets<T>(left: ReadonlySet<T>, right: ReadonlySet<T>): Set<T> {
  998. const out = new Set(left)
  999. addAll(out, right)
  1000. return out
  1001. }
  1002. /**
  1003. * Select the package source files of one project in deterministic order.
  1004. * @param project - the loaded repository TypeScript project.
  1005. * @returns `packages/<group>/<pkg>/src` files tagged with their package name.
  1006. */
  1007. export function collectPackageSources(project: TypeScriptProject): PackageSource[] {
  1008. return project.sourceFiles().flatMap((sourceFile): PackageSource[] => {
  1009. const rel = project.relativePath(sourceFile)
  1010. const match = /^packages\/[^/]+\/([^/]+)\/src\/.+\.ts$/.exec(rel)
  1011. return match?.[1] ? [{ rel, pkg: match[1], sourceFile }] : []
  1012. }).sort((left, right) => left.rel.localeCompare(right.rel))
  1013. }
  1014. function collectEventRelations(): Map<string, EventRelation> {
  1015. const project = new TypeScriptProject(root)
  1016. return new EventRelationCollector(project, collectPackageSources(project)).collect()
  1017. }
  1018. function relationPackages(map: Map<string, Set<string>>, pkgsByShort: Map<string, Pkg>): string {
  1019. if (map.size === 0) return '-'
  1020. return [...map.entries()]
  1021. .sort(([a], [b]) => a.localeCompare(b))
  1022. .map(([pkg, methods]) => `${pkgLink(pkgsByShort.get(pkg), pkg)} (${[...methods].sort().map(m => `\`${m}\``).join(', ')})`)
  1023. .join(', ')
  1024. }
  1025. function listenerPackages(listeners: Set<string>, pkgsByShort: Map<string, Pkg>): string {
  1026. if (listeners.size === 0) return '-'
  1027. return [...listeners].sort().map(pkg => pkgLink(pkgsByShort.get(pkg), pkg)).join(', ')
  1028. }
  1029. function renderEventRelations(pkgs: Pkg[], events: readonly EventEntry[]): string {
  1030. const relations = collectEventRelations()
  1031. const pkgsByShort = new Map(pkgs.map(pkg => [pkg.short, pkg]))
  1032. const maintenance = 'generated: Cordis event declarations and producer/listener edges are resolved from the repository TypeScript Program'
  1033. const lines = generatedHeader('Event Producer And Consumer Matrix')
  1034. lines.push(
  1035. '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.',
  1036. '',
  1037. '| Event | Mode | Declared in | Dispatchers | Listeners |',
  1038. '| --- | --- | --- | --- | --- |',
  1039. )
  1040. for (const event of [...events].sort((a, b) => a.name.localeCompare(b.name))) {
  1041. const relation = relations.get(event.name) ?? { dispatchers: new Map<string, Set<string>>(), listeners: new Set<string>() }
  1042. lines.push(`| \`${event.name}\` | \`${event.mode}\` | ${sourceLink(event.source)} | ${relationPackages(relation.dispatchers, pkgsByShort)} | ${listenerPackages(relation.listeners, pkgsByShort)} |`)
  1043. }
  1044. // Every declared event needs a dispatcher: zero means dead vocabulary or an
  1045. // unrecognized semantic dispatch shape. Listener-free extension points remain
  1046. // valid. Client-declared events are exempt: the relation scan seeds the HOST
  1047. // aggregate program only (host+client cannot share one program — the cordis
  1048. // Context merges collide), so client dispatch sites are structurally
  1049. // invisible here; their rows stay in the table for the declarations' sake.
  1050. const undispatched = [...events]
  1051. .filter(event => !event.source.startsWith('packages/client/'))
  1052. .filter(event => (relations.get(event.name)?.dispatchers.size ?? 0) === 0)
  1053. .map(event => event.name)
  1054. .sort()
  1055. if (undispatched.length > 0) {
  1056. throw new Error(
  1057. `event-producer-consumer matrix: no dispatcher found for declared event${undispatched.length > 1 ? 's' : ''} `
  1058. + `${undispatched.map(name => `"${name}"`).join(', ')} — dead vocabulary, or a dispatch shape the semantic scan misses `
  1059. + '(teach scripts/gen-doc-graphs.ts the shape)',
  1060. )
  1061. }
  1062. const declared = new Set(events.map(event => event.name))
  1063. const extra = [...relations.keys()].filter(event => !declared.has(event)).sort()
  1064. if (extra.length > 0) {
  1065. lines.push('', '## Non-harness or undeclared event strings seen in package source', '', '| Event string | Dispatchers | Listeners |', '| --- | --- | --- |')
  1066. for (const event of extra) {
  1067. const relation = relations.get(event)
  1068. if (!relation) continue
  1069. lines.push(`| \`${event}\` | ${relationPackages(relation.dispatchers, pkgsByShort)} | ${listenerPackages(relation.listeners, pkgsByShort)} |`)
  1070. }
  1071. }
  1072. lines.push('', ...maintenanceFooter(maintenance))
  1073. return lines.join('\n')
  1074. }
  1075. function renderLifecycle(): string {
  1076. const maintenance = 'curated Mermaid sequence; exact event signatures live in the generated Cordis catalog'
  1077. return [
  1078. ...generatedHeader('Agent Turn And Step Lifecycle'),
  1079. '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/*`.',
  1080. '',
  1081. '```mermaid',
  1082. 'sequenceDiagram',
  1083. ' participant User',
  1084. ' participant Agent',
  1085. ' participant Driver',
  1086. ' participant Hooks as hook listeners',
  1087. ' participant Prompt as ctx.systemPrompt',
  1088. ' participant LLM as ctx.llm',
  1089. ' participant Tools as ctx.tools',
  1090. ' participant Session',
  1091. ' participant SDK as UI or SDK listener',
  1092. ' User->>Agent: followup(content)',
  1093. ` Agent-->>SDK: ${mermaidCode('agent/inbox/spliced')}`,
  1094. ` Agent-->>SDK: ${mermaidCode('agent/inbox/inserted')} { message }`,
  1095. ' Agent->>Driver: queued work wakes driver',
  1096. ` Driver-->>SDK: ${mermaidCode('agent/status')} running`,
  1097. ' Note over Agent,Driver: claim pending next-step input plus one queued prompt',
  1098. ` Driver-->>SDK: ${mermaidCode('agent/inbox/spliced')} pure deletion`,
  1099. ` Driver-->>SDK: ${mermaidCode('agent/inbox/claimed')} { message, turn } per message`,
  1100. ` Driver->>Hooks: ${mermaidCode('agent/pre-step')} waterfall`,
  1101. ' Hooks-->>Driver: authoritative reject or enter(messages)',
  1102. ' alt proposed step rejected or pre-step failed',
  1103. ' Driver-->>Driver: claimed batch stays removed, no turn opens',
  1104. ' else enter proposed step',
  1105. ` Driver->>Session: ${mermaidCode('turn/start')}`,
  1106. ` Driver->>Session: ${mermaidCode('step/start')}`,
  1107. ` Driver->>Session: ${mermaidCode('user/message')} per entered message`,
  1108. ` Driver->>Prompt: ${mermaidCode('system-prompt/assemble')} waterfall`,
  1109. ` Driver->>LLM: ${mermaidCode('agent/request')} waterfall, then ${mermaidCode('llm/stream')} waterfall`,
  1110. ' LLM-->>Driver: StreamChunk*',
  1111. ` Driver->>Session: ${mermaidCode('assistant/chunk')}*`,
  1112. ` Session-->>SDK: ${mermaidCode('session/event')} ${mermaidCode('assistant/chunk')}*`,
  1113. ' alt final adapter or terminal in-band request failure',
  1114. ` Driver->>Session: ${mermaidCode('step/end')}`,
  1115. ` Driver->>Hooks: ${mermaidCode('agent/request-error')} waterfall`,
  1116. ' Hooks-->>Driver: return retry action or preserve the original error',
  1117. ' else model request succeeded',
  1118. ` Driver->>Session: ${mermaidCode('assistant/message')}`,
  1119. ' Driver->>Tools: classify pending call by executionMode',
  1120. ' loop barriers and bounded rolling pool, reclassify before start',
  1121. ' opt call starts',
  1122. ` Driver->>Session: ${mermaidCode('tool/call')}`,
  1123. ' Driver->>Tools: ordered pre, concurrent execute',
  1124. ' Tools-->>Session: tool-owned events when applicable',
  1125. ' end',
  1126. ' opt next model-order result ready',
  1127. ' Driver->>Tools: ordered post',
  1128. ` Driver->>Session: ${mermaidCode('tool/result')}`,
  1129. ' end',
  1130. ' end',
  1131. ` Driver->>Session: ${mermaidCode('step/end')}`,
  1132. ' opt natural stop and next-step inbox empty',
  1133. ` Driver->>Hooks: ${mermaidCode('agent/turn-stopping')} serial terminal checkpoint`,
  1134. ' end',
  1135. ' opt next-step input is pending',
  1136. ' Driver-->>Driver: claim pending next-step input',
  1137. ` Driver-->>SDK: ${mermaidCode('agent/inbox/claimed')} { message, turn } per message`,
  1138. ` Driver->>Hooks: ${mermaidCode('agent/pre-step')} waterfall`,
  1139. ' Hooks-->>Driver: authoritative reject or enter(messages)',
  1140. ' end',
  1141. ' end',
  1142. ` Driver->>Session: ${mermaidCode('turn/end')}`,
  1143. ' end',
  1144. ` Driver-->>SDK: ${mermaidCode('agent/status')} idle`,
  1145. '```',
  1146. '',
  1147. '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.',
  1148. '',
  1149. '`dsh-compact-basic` uses `agent/pre-step` for pressure before request derivation 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 failed turn close, and opens a fresh retry turn only when pruning or summarization advances the surface replacement generation; otherwise the original request error remains authoritative.',
  1150. '',
  1151. 'The returned `agent/pre-step` decision is authoritative; listeners wrapping `next()` preserve downstream messages unless replacement is intentional. Steering and injected context pass through the same waterfall after a later boundary claims their next-step batch.',
  1152. '',
  1153. '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.',
  1154. '',
  1155. ...maintenanceFooter(maintenance),
  1156. ].join('\n')
  1157. }
  1158. function renderToolPipeline(): string {
  1159. const maintenance = 'curated Mermaid flow; exact tool schemas and event signatures live in generated catalogs'
  1160. return [
  1161. ...generatedHeader('Tool Execution Pipeline'),
  1162. '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, definition-owned `finalizeContent`, and `tools/result` are the owner-enforced boundaries around them.',
  1163. '',
  1164. '```mermaid',
  1165. 'flowchart TD',
  1166. ' model["Assistant message contains tool-call block"]',
  1167. ` toolCall["Session event: ${mermaidCode('tool/call')}<br/>logged before execution"]`,
  1168. ' presentCall["UI pending card<br/>presentCall(args)"]',
  1169. ` pre["${mermaidCode('tools/pre-execute')} waterfall<br/>hooks, permission, sandbox"]`,
  1170. ' guards["Registered monotonic guards<br/>deny or abstain; identity protected"]',
  1171. ' denied["denied or approval refused<br/>tool body skipped"]',
  1172. ` approval["${mermaidCode('ctx.approval')} one-shot prompt<br/>absent or unanswerable: deny"]`,
  1173. ` around["${mermaidCode('tools/execute')} waterfall<br/>timeout, retry, metrics (around dispatch)"]`,
  1174. ' toolBody["Registered tool execute() body"]',
  1175. ` fsGate["${mermaidCode('fs/write-intent')} or ${mermaidCode('fs/edit-intent')}<br/>tool-fs mutations only"]`,
  1176. ` owned["Tool-owned session events<br/>${mermaidCode('todo/write')}, ${mermaidCode('fs/observed')}, ${mermaidCode('hook/invoked')}, ${mermaidCode('hook/result')}, ${mermaidCode('tool/code-dispatch')}"]`,
  1177. ` post["${mermaidCode('tools/post-execute')} waterfall<br/>accept, block, replace, add context"]`,
  1178. ' normalized["Registry outer normalization<br/>pipeline/result snapshot throws become isError"]',
  1179. ' finalize["ToolDefinition.finalizeContent<br/>last content-only invariant"]',
  1180. ` final["${mermaidCode('tools/result')} synchronous notification<br/>frozen authoritative outcome"]`,
  1181. ' context["Active-batch additionalContexts FIFO<br/>injected user/message after recorded tool results"]',
  1182. ` toolResult["Session event: ${mermaidCode('tool/result')}<br/>single model-facing outcome"]`,
  1183. ' allResults["Tool batch settled<br/>recorded tool/result events complete"]',
  1184. ' presentResult["UI completed card<br/>presentResult(args, result)"]',
  1185. ' model --> toolCall',
  1186. ' toolCall --> presentCall',
  1187. ' toolCall --> pre',
  1188. ' pre -->|allow| guards',
  1189. ' guards -->|allow| around',
  1190. ' guards -->|deny| denied',
  1191. ' guards -.->|throw| normalized',
  1192. ' around --> toolBody',
  1193. ' pre -->|deny| denied',
  1194. ' pre -->|ask| approval',
  1195. ' approval -->|allowed-once| guards',
  1196. ' approval -->|rejected, cancelled, unavailable| denied',
  1197. ' approval -.->|throw| normalized',
  1198. ' denied --> post',
  1199. ' pre -.->|throw| normalized',
  1200. ' toolBody --> fsGate',
  1201. ' fsGate --> toolBody',
  1202. ' toolBody --> owned',
  1203. ' toolBody --> around',
  1204. ' around --> post',
  1205. ' around -.->|wrapper throws| normalized',
  1206. ' post -.->|throw| normalized',
  1207. ' post --> finalize',
  1208. ' normalized --> finalize',
  1209. ' finalize --> final',
  1210. ' final --> toolResult',
  1211. ' toolResult --> presentResult',
  1212. ' toolResult --> allResults',
  1213. ' allResults --> context',
  1214. '```',
  1215. '',
  1216. '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`. The registry losslessly snapshots the candidate result and normalizes a snapshot failure before the visible definition\'s snapshotted `finalizeContent` callback enforces its synchronous content-only invariant. `tools/result` then observes the immutable, lossless-JSON outcome. 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.',
  1217. '',
  1218. ...maintenanceFooter(maintenance),
  1219. ].join('\n')
  1220. }
  1221. function renderDocs(): GraphDoc[] {
  1222. const pkgs = collectPackageGraph(root, GROUP_ORDER, 'gen-doc-graphs')
  1223. const { model } = projectCordisCatalog(root, CORDIS_CATALOG_POLICY)
  1224. const docs: GraphDoc[] = [
  1225. { rel: 'docs/capability-seams.md', content: renderCapabilitySeams(pkgs, model.services) },
  1226. ...APP_EXAMPLES.map(example => ({ rel: example.rel, content: renderAppComposition(example) })),
  1227. { rel: 'docs/event-producer-consumer.md', content: renderEventRelations(pkgs, model.events) },
  1228. { rel: 'docs/agent-lifecycle.md', content: renderLifecycle() },
  1229. { rel: 'docs/tool-execution-pipeline.md', content: renderToolPipeline() },
  1230. ]
  1231. docs.unshift({ rel: 'docs/graph-atlas.md', content: renderIndex(docs) })
  1232. return docs
  1233. }
  1234. function renderIndex(docs: GraphDoc[]): string {
  1235. const labels: Record<string, string> = {
  1236. 'docs/capability-seams.md': 'capability seams and core services',
  1237. 'apps/cli/composition.md': 'dsh shared base composition',
  1238. 'examples/headless-agent/composition.md': 'headless-agent app composition',
  1239. 'examples/cordis-agent/composition.md': 'cordis-agent app composition',
  1240. 'examples/acp-agent/composition.md': 'acp-agent app composition',
  1241. 'docs/event-producer-consumer.md': 'event producer/consumer matrix',
  1242. 'docs/agent-lifecycle.md': 'agent turn and step lifecycle',
  1243. 'docs/tool-execution-pipeline.md': 'tool execution pipeline',
  1244. }
  1245. const modes: Record<string, string> = {
  1246. 'docs/capability-seams.md': 'hybrid generated',
  1247. 'apps/cli/composition.md': 'hybrid generated',
  1248. 'examples/headless-agent/composition.md': 'hybrid generated',
  1249. 'examples/cordis-agent/composition.md': 'hybrid generated',
  1250. 'examples/acp-agent/composition.md': 'hybrid generated',
  1251. 'docs/event-producer-consumer.md': 'hybrid generated',
  1252. 'docs/agent-lifecycle.md': 'curated',
  1253. 'docs/tool-execution-pipeline.md': 'curated',
  1254. }
  1255. const rows = [
  1256. '| [module dependency graph](module-graph.md) | `generated` |',
  1257. '| [tool schema catalog and package map](tool-catalog.md) | `generated` |',
  1258. ...docs.map((doc) => {
  1259. const link = graphIndexLink(doc.rel)
  1260. return `| [${labels[doc.rel] ?? link}](${link}) | \`${modes[doc.rel] ?? 'generated'}\` |`
  1261. }),
  1262. ]
  1263. const maintenance = 'mixed: each linked page declares generated, hybrid, or curated mode'
  1264. return [
  1265. ...generatedHeader('Documentation Graph Index'),
  1266. '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).',
  1267. '',
  1268. 'The process decision behind this index is recorded in [the documentation graph Agent Note](../.agents/notes/archived/process/2026-07-03-documentation-graph-atlas.md).',
  1269. '',
  1270. '| Graph | Mode |',
  1271. '| --- | --- |',
  1272. ...rows,
  1273. '',
  1274. 'Regenerate with `pnpm run gen-doc-graphs`; verify freshness with `pnpm run verify-doc-graphs`.',
  1275. '',
  1276. ...maintenanceFooter(maintenance),
  1277. ].join('\n')
  1278. }
  1279. function main(): void {
  1280. const docs = renderDocs()
  1281. if (process.argv.includes('--check')) {
  1282. const stale: string[] = []
  1283. for (const doc of docs) {
  1284. const abs = resolve(root, doc.rel)
  1285. const committed = existsSync(abs) ? readFileSync(abs, 'utf8') : null
  1286. if (committed !== doc.content) stale.push(doc.rel)
  1287. }
  1288. if (stale.length === 0) {
  1289. console.log(`gen-doc-graphs: ${docs.length} graph doc(s) are up to date.`)
  1290. return
  1291. }
  1292. console.error(`gen-doc-graphs: stale graph doc(s): ${stale.join(', ')}. Run \`pnpm run gen-doc-graphs\` and commit the result.`)
  1293. process.exit(1)
  1294. }
  1295. for (const doc of docs) {
  1296. mkdirSync(dirname(resolve(root, doc.rel)), { recursive: true })
  1297. writeFileSync(resolve(root, doc.rel), doc.content)
  1298. }
  1299. console.log(`gen-doc-graphs: wrote ${docs.length} graph doc(s).`)
  1300. }
  1301. if (process.argv[1] && import.meta.filename === resolve(process.argv[1])) {
  1302. main()
  1303. }