run-gates.ts 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  1. /**
  2. * Run local and CI quality gates with bounded in-process scheduling.
  3. *
  4. * Package scripts own public aggregate names; this runner owns their validated
  5. * dependency graphs, scheduler environment, and process diagnostics.
  6. * @see ../.agents/notes/implemented/process/2026-07-06-parallel-pre-push-gates.md
  7. */
  8. import { spawn } from 'node:child_process'
  9. import { availableParallelism } from 'node:os'
  10. import { resolve } from 'node:path'
  11. import { performance } from 'node:perf_hooks'
  12. /** A named aggregate exposed by the gate runner. */
  13. export type Mode =
  14. | 'ci-primary'
  15. | 'ci-static'
  16. | 'ci-lint'
  17. | 'ci-coverage'
  18. | 'ci-snapshot'
  19. | 'ci-artifacts'
  20. | 'ci-consumers'
  21. | 'ci-windows-blocking'
  22. | 'ci-windows-complete'
  23. | 'ci-windows-observational'
  24. | 'node-compat'
  25. | 'check-all'
  26. | 'doc-sync'
  27. type GateResultStatus = 'passed' | 'failed' | 'skipped'
  28. type GateState = 'pending' | 'running' | GateResultStatus
  29. /** A command and its dependency metadata inside one aggregate. */
  30. export interface Gate {
  31. id: string
  32. label: string
  33. displayCommand: string
  34. command: string
  35. args: string[]
  36. needs?: string[]
  37. env?: Record<string, string | undefined>
  38. allowFailure?: boolean
  39. }
  40. /** The observed outcome of one gate process. */
  41. export interface GateResult {
  42. gate: Gate
  43. status: GateResultStatus
  44. durationMs: number
  45. output: GateOutputChunk[]
  46. exitCode: number | null
  47. signalCode: NodeJS.Signals | null
  48. error?: string
  49. }
  50. interface GateOutputChunk {
  51. stream: 'stdout' | 'stderr'
  52. text: string
  53. }
  54. interface RunningGate {
  55. gate: Gate
  56. promise: Promise<GateResult>
  57. }
  58. interface ConcurrencyDefault {
  59. workers: number
  60. source: string
  61. }
  62. type GateExecutor = (gate: Gate) => Promise<GateResult>
  63. type ResultObserver = (result: GateResult) => void
  64. const root = resolve(import.meta.dirname, '..')
  65. if (import.meta.main) {
  66. process.exitCode = await main(process.argv.slice(2))
  67. }
  68. async function main(args: string[]): Promise<number> {
  69. const mode = parseMode(args[0])
  70. const gates = gatesForMode(mode)
  71. const concurrencyDefault = defaultConcurrency(mode, gates.length)
  72. const concurrencyOverride = process.env.DSH_GATE_CONCURRENCY
  73. const maxConcurrency = concurrencyFromEnv('DSH_GATE_CONCURRENCY', concurrencyDefault.workers)
  74. const concurrencySource = concurrencyOverride === undefined || concurrencyOverride === ''
  75. ? concurrencyDefault.source
  76. : '$DSH_GATE_CONCURRENCY'
  77. const startedAt = performance.now()
  78. console.log(`run-gates: ${mode} running ${gates.length} gate(s) with ${maxConcurrency} worker(s) from ${concurrencySource}.`)
  79. const results = await runGates(gates, maxConcurrency, runGate, printResult)
  80. printSummary(results, performance.now() - startedAt)
  81. return results.some(result => result.gate.allowFailure !== true && (result.status === 'failed' || result.status === 'skipped'))
  82. ? 1
  83. : 0
  84. }
  85. function parseMode(raw: string | undefined): Mode {
  86. switch (raw) {
  87. case 'ci-primary':
  88. case 'ci-static':
  89. case 'ci-lint':
  90. case 'ci-coverage':
  91. case 'ci-snapshot':
  92. case 'ci-artifacts':
  93. case 'ci-consumers':
  94. case 'ci-windows-blocking':
  95. case 'ci-windows-complete':
  96. case 'ci-windows-observational':
  97. case 'node-compat':
  98. case 'check-all':
  99. case 'doc-sync':
  100. return raw
  101. default:
  102. throw new Error(
  103. `run-gates: expected mode ci-primary | ci-static | ci-lint | ci-coverage | ci-snapshot | ci-artifacts | ci-consumers | ci-windows-blocking | ci-windows-complete | ci-windows-observational | node-compat | check-all | doc-sync, got ${JSON.stringify(raw)}.`,
  104. )
  105. }
  106. }
  107. /**
  108. * Resolve the default worker count for one aggregate.
  109. * @param selectedMode - aggregate whose resource posture applies.
  110. * @param total - number of gates in the aggregate.
  111. * @param available - host CPU availability for ordinary modes.
  112. * @returns the default worker count and its diagnostic source.
  113. */
  114. export function defaultConcurrency(
  115. selectedMode: Mode,
  116. total: number,
  117. available = availableParallelism(),
  118. ): ConcurrencyDefault {
  119. if (selectedMode === 'ci-consumers') return { workers: total, source: 'ci-consumers gate count' }
  120. // Local modes cap workers: several doc gates each build a full ts.Program,
  121. // so an uncapped default on a large host trades wall clock for memory blowups.
  122. const localCap = selectedMode === 'check-all' || selectedMode === 'doc-sync'
  123. const modeLimit = localCap ? Math.min(4, available) : available
  124. return {
  125. workers: Math.min(total, modeLimit),
  126. source: localCap
  127. ? `${available} available CPU(s), ${selectedMode} cap 4`
  128. : `${available} available CPU(s)`,
  129. }
  130. }
  131. function concurrencyFromEnv(name: string, fallback: number): number {
  132. const raw = process.env[name]
  133. if (raw === undefined || raw === '') return fallback
  134. const parsed = Number.parseInt(raw, 10)
  135. if (!Number.isSafeInteger(parsed) || parsed < 1) {
  136. throw new Error(`run-gates: ${name} must be a positive integer, got ${JSON.stringify(raw)}.`)
  137. }
  138. return parsed
  139. }
  140. function pnpmScript(id: string, script: string, options: Partial<Gate> = {}): Gate {
  141. return {
  142. id,
  143. label: options.label ?? script,
  144. displayCommand: `pnpm run ${script}`,
  145. ...pnpmInvocation(['run', script]),
  146. ...options,
  147. }
  148. }
  149. function pnpmExec(id: string, args: string[], options: Partial<Gate> = {}): Gate {
  150. return {
  151. id,
  152. label: options.label ?? `pnpm exec ${args.join(' ')}`,
  153. displayCommand: `pnpm exec ${args.join(' ')}`,
  154. ...pnpmInvocation(['exec', ...args]),
  155. ...options,
  156. }
  157. }
  158. function pnpmInvocation(args: string[]): Pick<Gate, 'command' | 'args'> {
  159. const entrypoint = process.env.npm_execpath
  160. if (entrypoint === undefined || entrypoint === '') {
  161. throw new Error('run-gates: npm_execpath is unavailable; invoke the runner through a pnpm package script.')
  162. }
  163. // Windows cannot spawn the pnpm.cmd shim directly; the JavaScript entrypoint keeps every host shell-free.
  164. return { command: process.execPath, args: [entrypoint, ...args] }
  165. }
  166. /**
  167. * Construct the complete gate list for a named aggregate.
  168. * @param selected - aggregate mode to construct.
  169. * @returns the aggregate's gate graph.
  170. */
  171. export function gatesForMode(selected: Mode): Gate[] {
  172. switch (selected) {
  173. case 'ci-primary':
  174. return ciPrimaryGates()
  175. case 'ci-static':
  176. return ciStaticGates()
  177. case 'ci-lint':
  178. return [
  179. lintGate(),
  180. pnpmScript('duplication', 'duplication'),
  181. ]
  182. case 'ci-coverage':
  183. return [coverageGate()]
  184. case 'ci-snapshot':
  185. return [pnpmScript('build', 'build'), snapshotGate()]
  186. case 'ci-artifacts':
  187. return ciArtifactGates()
  188. case 'ci-consumers':
  189. return ciConsumerGates()
  190. case 'ci-windows-blocking':
  191. return ciWindowsBlockingGates()
  192. case 'ci-windows-complete':
  193. return ciWindowsCompleteGates()
  194. case 'ci-windows-observational':
  195. return ciWindowsObservationalGates()
  196. case 'node-compat':
  197. return nodeCompatGates()
  198. case 'check-all':
  199. return [
  200. pnpmScript('runtime-closure', 'verify-runtime-closure', { label: 'runtime closure' }),
  201. pnpmScript('cordis-config', 'verify-cordis-config', { label: 'Cordis config' }),
  202. pnpmScript('client-domain-graph', 'verify-client-domain-graph', { label: 'client domain graph' }),
  203. pnpmScript('test', 'test'),
  204. pnpmScript('duplication', 'duplication'),
  205. snapshotGate(),
  206. pnpmScript('build', 'build'),
  207. pnpmScript('build:web', 'build:web'),
  208. ...hygieneLeafGates({ artifactNeeds: ['build'] }),
  209. ...docSyncLeafGates({
  210. docTypecheckNeeds: ['build'],
  211. docTypecheckEnv: { DSH_DOC_TYPECHECK_USE_BUILD_OUTPUT: '1' },
  212. }),
  213. pnpmScript('module-graph', 'verify-module-graph', { label: 'module graph' }),
  214. ]
  215. case 'doc-sync':
  216. return docSyncLeafGates()
  217. }
  218. }
  219. function ciPrimaryGates(): Gate[] {
  220. return [
  221. pnpmScript('runtime-closure', 'verify-runtime-closure', { label: 'runtime closure' }),
  222. pnpmScript('constraints', 'constraints'),
  223. pnpmScript('package-invariants', 'verify-package-invariants', { label: 'package invariants' }),
  224. pnpmScript('cordis-config', 'verify-cordis-config', { label: 'Cordis config' }),
  225. pnpmScript('typecheck', 'typecheck'),
  226. lintGate(),
  227. pnpmScript('duplication', 'duplication'),
  228. coverageGate(),
  229. ...nodeCompatSmokeGates(),
  230. snapshotGate(),
  231. ...docSyncLeafGates(),
  232. pnpmScript('module-graph', 'verify-module-graph', { label: 'module graph' }),
  233. pnpmScript('knip', 'knip'),
  234. // typecheck and build now drive the same root solution graph; without the
  235. // dependency two concurrent `tsc -b` runs race the same tsbuildinfo files.
  236. // The tsc step is an incremental no-op after typecheck.
  237. pnpmScript('build', 'build', { needs: ['typecheck'] }),
  238. pnpmScript('publint', 'publint', { needs: ['build'] }),
  239. pnpmScript('node-next-types', 'verify-node-next-types', {
  240. label: 'node-next types',
  241. needs: ['build'],
  242. }),
  243. builtPackageInvariantsGate(['build']),
  244. builtBinSmokeGate(),
  245. ]
  246. }
  247. function nodeCompatGates(): Gate[] {
  248. return [
  249. ...flagEnabled('DSH_NODE_COMPAT_SKIP_TYPECHECK') ? [] : [pnpmScript('typecheck', 'typecheck')],
  250. ...nodeCompatSmokeGates(),
  251. ]
  252. }
  253. function nodeCompatSmokeGates(): Gate[] {
  254. return [
  255. pnpmExec('source-worker-smoke', [
  256. 'vitest',
  257. 'run',
  258. 'packages/workflow/workflow-workerthread/tests/source-worker.compat.spec.ts',
  259. ], { label: 'source worker smoke' }),
  260. pnpmExec('jsonl-zstd-smoke', [
  261. 'vitest',
  262. 'run',
  263. 'packages/session-persistence/session-persistence-jsonl/tests/zstd.compat.spec.ts',
  264. ], { label: 'JSONL Zstandard smoke' }),
  265. pnpmExec('dsh-source-launch-smoke', [
  266. 'vitest',
  267. 'run',
  268. 'apps/cli/tests/source-launch.compat.spec.ts',
  269. ], { label: 'dsh source-launch smoke' }),
  270. ]
  271. }
  272. function ciStaticGates(): Gate[] {
  273. return [
  274. pnpmScript('runtime-closure', 'verify-runtime-closure', { label: 'runtime closure' }),
  275. pnpmScript('constraints', 'constraints'),
  276. pnpmScript('package-invariants', 'verify-package-invariants', { label: 'package invariants' }),
  277. pnpmScript('cordis-config', 'verify-cordis-config', { label: 'Cordis config' }),
  278. pnpmScript('build', 'build'),
  279. ...docSyncLeafGates({
  280. docTypecheckNeeds: ['build'],
  281. docTypecheckEnv: { DSH_DOC_TYPECHECK_USE_BUILD_OUTPUT: '1' },
  282. docsBuildScript: 'docs:build:mpa',
  283. }),
  284. pnpmScript('module-graph', 'verify-module-graph', { label: 'module graph' }),
  285. pnpmScript('knip', 'knip'),
  286. ]
  287. }
  288. function ciArtifactGates(): Gate[] {
  289. return [
  290. pnpmScript('build', 'build'),
  291. pnpmScript('publint', 'publint', { needs: ['build'] }),
  292. pnpmScript('node-next-types', 'verify-node-next-types', {
  293. label: 'node-next types',
  294. needs: ['build'],
  295. }),
  296. builtPackageInvariantsGate(['build']),
  297. builtBinSmokeGate(),
  298. ]
  299. }
  300. function ciConsumerGates(): Gate[] {
  301. const publicArtifacts = ['publint']
  302. const restoredBuild = ['built-package-invariants']
  303. return [
  304. pnpmScript('lint-and-duplication', 'check:ci:lint', {
  305. label: 'lint and duplication',
  306. needs: restoredBuild,
  307. }),
  308. pnpmScript('node-compat', 'check:node-compat', { label: 'Node compatibility' }),
  309. snapshotGate(restoredBuild),
  310. pnpmScript('publint', 'publint'),
  311. pnpmScript('node-next-types', 'verify-node-next-types', {
  312. label: 'node-next types',
  313. needs: restoredBuild,
  314. }),
  315. builtPackageInvariantsGate(publicArtifacts),
  316. builtBinSmokeGate(restoredBuild),
  317. ]
  318. }
  319. function ciWindowsBlockingGates(): Gate[] {
  320. return [
  321. pnpmScript('windows-build', 'build', { label: 'build' }),
  322. pnpmScript('windows-site', 'docs:build', { label: 'production site' }),
  323. ]
  324. }
  325. function ciWindowsCompleteGates(): Gate[] {
  326. const observational = ciWindowsObservationalGates()
  327. // The required production site replaces the observational MPA build; both
  328. // VitePress modes write the same output directory and cannot overlap.
  329. .filter(gate => gate.id !== 'build' && gate.id !== 'docs-site-build')
  330. .map(gate => ({ ...gate, allowFailure: true }))
  331. return [
  332. pnpmScript('build', 'build'),
  333. pnpmScript('windows-site', 'docs:build', { label: 'production site' }),
  334. ...observational,
  335. ]
  336. }
  337. function ciWindowsObservationalGates(): Gate[] {
  338. return [
  339. ...ciStaticGates(),
  340. // Linux owns required lint, coverage, and snapshots; Windows omits those duplicates.
  341. pnpmScript('duplication', 'duplication'),
  342. pnpmScript('publint', 'publint', { needs: ['build'] }),
  343. pnpmScript('node-next-types', 'verify-node-next-types', {
  344. label: 'node-next types',
  345. needs: ['build'],
  346. }),
  347. builtPackageInvariantsGate(['build']),
  348. builtBinSmokeGate(),
  349. ]
  350. }
  351. function lintGate(): Gate {
  352. const raw = process.env.DSH_OXLINT_THREADS
  353. return pnpmScript('lint', 'lint', raw === undefined || raw === ''
  354. ? {}
  355. : { displayCommand: `DSH_OXLINT_THREADS=${raw} pnpm run lint` })
  356. }
  357. function coverageGate(): Gate {
  358. return pnpmExec('coverage', [
  359. 'vitest',
  360. 'run',
  361. '--coverage',
  362. ...positiveIntArg('DSH_COVERAGE_MAX_WORKERS', '--maxWorkers'),
  363. ], {
  364. label: 'test:coverage',
  365. })
  366. }
  367. // Example and package snapshots boot their bins in `lib` mode (built artifacts under plain Node,
  368. // plugins via real exports); repository-script snapshots execute their real source entry path.
  369. // Build-owning modes wait on `build`; a restored-artifact mode passes its validation dependency.
  370. function snapshotGate(needs: string[] = ['build']): Gate {
  371. return pnpmScript('snapshot', 'test:snapshot', {
  372. env: { DSH_EXAMPLE_MODE: 'lib' },
  373. needs,
  374. })
  375. }
  376. function builtPackageInvariantsGate(needs?: string[]): Gate {
  377. return pnpmScript('built-package-invariants', 'verify-built-package-invariants', {
  378. label: 'built package invariants',
  379. ...needs === undefined ? {} : { needs },
  380. })
  381. }
  382. function positiveIntArg(envName: string, flag: string): string[] {
  383. const raw = process.env[envName]
  384. if (raw === undefined || raw === '') return []
  385. const parsed = Number.parseInt(raw, 10)
  386. if (!Number.isSafeInteger(parsed) || parsed < 1 || String(parsed) !== raw) {
  387. throw new Error(`run-gates: ${envName} must be a positive integer, got ${JSON.stringify(raw)}.`)
  388. }
  389. return [`${flag}=${raw}`]
  390. }
  391. function flagEnabled(envName: string): boolean {
  392. const raw = process.env[envName]
  393. if (raw === undefined || raw === '') return false
  394. if (raw !== '1') throw new Error(`run-gates: ${envName} must be 1 when set, got ${JSON.stringify(raw)}.`)
  395. return true
  396. }
  397. function hygieneLeafGates(options: { artifactNeeds?: string[] } = {}): Gate[] {
  398. const artifactOptions = options.artifactNeeds === undefined ? {} : { needs: options.artifactNeeds }
  399. return [
  400. pnpmScript('knip', 'knip'),
  401. pnpmScript('publint', 'publint', artifactOptions),
  402. pnpmScript('constraints', 'constraints'),
  403. pnpmScript('package-invariants', 'verify-package-invariants', { label: 'package invariants' }),
  404. builtPackageInvariantsGate(options.artifactNeeds),
  405. pnpmScript('node-next-types', 'verify-node-next-types', {
  406. label: 'node-next types',
  407. ...artifactOptions,
  408. }),
  409. ]
  410. }
  411. function docSyncLeafGates(options: {
  412. docTypecheckNeeds?: string[]
  413. docTypecheckEnv?: Record<string, string | undefined>
  414. docsBuildScript?: 'docs:build' | 'docs:build:mpa'
  415. } = {}): Gate[] {
  416. const docTypecheckOptions: Partial<Gate> = {}
  417. if (options.docTypecheckNeeds !== undefined) docTypecheckOptions.needs = options.docTypecheckNeeds
  418. if (options.docTypecheckEnv !== undefined) docTypecheckOptions.env = options.docTypecheckEnv
  419. return [
  420. pnpmScript('doc-typecheck', 'doc-typecheck', docTypecheckOptions),
  421. pnpmScript('cordis-catalog', 'verify-cordis-catalog', { label: 'cordis catalog' }),
  422. pnpmScript('export-jsdoc', 'verify-export-jsdoc', { label: 'export jsdoc' }),
  423. pnpmScript('tool-catalog', 'verify-tool-catalog', { label: 'tool catalog' }),
  424. pnpmScript('config-catalog', 'verify-config-catalog', { label: 'config catalog' }),
  425. pnpmScript('persistence-catalog', 'verify-persistence-catalog', { label: 'persistence catalog' }),
  426. pnpmScript('doc-graphs', 'verify-doc-graphs', { label: 'doc graphs' }),
  427. pnpmScript('scoped-events', 'verify-scoped-events', { label: 'scoped events' }),
  428. pnpmScript('markdown-wrap', 'verify-md-wrap', { label: 'markdown wrap' }),
  429. pnpmScript('markdown-links', 'verify-md-links', { label: 'markdown links' }),
  430. pnpmScript('doc-refs', 'verify-doc-refs', { label: 'doc refs' }),
  431. pnpmScript('package-paths', 'verify-package-paths', { label: 'package paths' }),
  432. pnpmScript('package-readme-model-experience', 'verify-package-readme-model-experience', { label: 'package README model experience' }),
  433. pnpmScript('mermaid', 'verify-mermaid'),
  434. pnpmScript('agent-note-classification', 'verify-agent-note-classification', { label: 'agent note classification' }),
  435. pnpmScript('agent-note-format', 'verify-agent-note-format', { label: 'agent note format' }),
  436. pnpmScript('archived-agent-notes', 'verify-archived-agent-notes', { label: 'archived agent notes' }),
  437. pnpmScript('type-equivalence', 'verify-type-equiv', { label: 'type equivalence' }),
  438. pnpmScript('translation-prompt', 'verify-translation-prompt', { label: 'translation prompt' }),
  439. pnpmScript('translation-pairing', 'verify-translation-pairing', { label: 'translation pairing' }),
  440. pnpmScript('doc-budgets', 'verify-doc-budgets', { label: 'doc budgets' }),
  441. pnpmExec('docs-site-projection', ['vitest', 'run', 'scripts/project-doc-site.spec.ts'], {
  442. label: 'documentation projection',
  443. }),
  444. // Keep the VitePress build itself in one gate because projection rewrites website/.generated.
  445. pnpmScript('docs-site-build', options.docsBuildScript ?? 'docs:build', { label: 'documentation build' }),
  446. pnpmScript('package-readme-limitations', 'verify-package-readme-limitations', { label: 'package README limitations' }),
  447. ]
  448. }
  449. function builtBinSmokeGate(needs: string[] = ['build']): Gate {
  450. return pnpmExec('built-bin-smoke', [
  451. 'vitest',
  452. 'run',
  453. '--config',
  454. 'vitest.e2e.config.ts',
  455. 'examples/headless-agent/tests/keyless-smoke.e2e.ts',
  456. 'examples/tui-agent/tests/tui-keyless-smoke.e2e.ts',
  457. 'packages/examples/cli-demo/tests/built-bin.e2e.ts',
  458. 'packages/examples/acp-demo/tests/built-bin.e2e.ts',
  459. 'packages/ui/jsonrpc/tests/built-scope-carrier.e2e.ts',
  460. // The worker-entry packages' built bundles: the only automated proof
  461. // that lib/index.js resolves its sibling lib/worker.cjs under plain node
  462. // (the e2e lane runs unbuilt, so these files self-skip there).
  463. 'packages/workflow/workflow-workerthread/tests/built-worker.e2e.ts',
  464. 'packages/code-runtime/code-runtime-worker/tests/built-lib.e2e.ts',
  465. ], {
  466. label: 'built-bin smoke',
  467. needs,
  468. env: { DSH_EXAMPLE_MODE: 'lib' },
  469. })
  470. }
  471. /**
  472. * Reject a gate list whose graph cannot be executed unambiguously.
  473. * @param gates - complete aggregate to validate.
  474. */
  475. function validateGateGraph(gates: readonly Gate[]): void {
  476. if (gates.length === 0) throw new Error('run-gates: gate graph has no gates.')
  477. const ids = new Set<string>()
  478. for (const gate of gates) {
  479. if (ids.has(gate.id)) throw new Error(`run-gates: duplicate gate id ${JSON.stringify(gate.id)}.`)
  480. ids.add(gate.id)
  481. }
  482. for (const gate of gates) {
  483. for (const dependency of gate.needs ?? []) {
  484. if (!ids.has(dependency)) {
  485. throw new Error(`run-gates: gate ${JSON.stringify(gate.id)} depends on unknown gate ${JSON.stringify(dependency)}.`)
  486. }
  487. }
  488. }
  489. const cycle = findDependencyCycle(gates)
  490. if (cycle !== undefined) throw new Error(`run-gates: dependency cycle: ${cycle.join(' -> ')}.`)
  491. }
  492. function findDependencyCycle(gates: readonly Gate[]): string[] | undefined {
  493. const byId = new Map(gates.map(gate => [gate.id, gate]))
  494. const complete = new Set<string>()
  495. const active = new Map<string, number>()
  496. const path: string[] = []
  497. const visit = (id: string): string[] | undefined => {
  498. if (complete.has(id)) return undefined
  499. const cycleStart = active.get(id)
  500. if (cycleStart !== undefined) return [...path.slice(cycleStart), id]
  501. const gate = byId.get(id)
  502. if (gate === undefined) return undefined
  503. active.set(id, path.length)
  504. path.push(id)
  505. for (const dependency of gate.needs ?? []) {
  506. const cycle = visit(dependency)
  507. if (cycle !== undefined) return cycle
  508. }
  509. path.pop()
  510. active.delete(id)
  511. complete.add(id)
  512. return undefined
  513. }
  514. for (const gate of gates) {
  515. const cycle = visit(gate.id)
  516. if (cycle !== undefined) return cycle
  517. }
  518. return undefined
  519. }
  520. /**
  521. * Validate and run one aggregate before the injected executor can start a child.
  522. * @param gates - complete aggregate to execute.
  523. * @param maxActive - maximum concurrent child count.
  524. * @param execute - child-process executor.
  525. * @param observe - result observer invoked when each gate settles.
  526. * @returns results in aggregate order.
  527. */
  528. export async function runGates(
  529. gates: Gate[],
  530. maxActive: number,
  531. execute: GateExecutor,
  532. observe: ResultObserver = () => {},
  533. ): Promise<GateResult[]> {
  534. validateGateGraph(gates)
  535. if (!Number.isSafeInteger(maxActive) || maxActive < 1) {
  536. throw new Error(`run-gates: max concurrency must be a positive integer, got ${JSON.stringify(maxActive)}.`)
  537. }
  538. const states = new Map<string, GateState>(gates.map(gate => [gate.id, 'pending']))
  539. const results = new Map<string, GateResult>()
  540. const running: RunningGate[] = []
  541. for (;;) {
  542. let madeProgress = false
  543. while (running.length < maxActive) {
  544. const ready = gates.find(gate => states.get(gate.id) === 'pending' && dependenciesPassed(gate, states))
  545. if (ready === undefined) break
  546. states.set(ready.id, 'running')
  547. running.push({ gate: ready, promise: execute(ready) })
  548. console.log(`run-gates: start ${ready.label}`)
  549. madeProgress = true
  550. }
  551. if (running.length === 0) {
  552. let pending = gates.filter(gate => states.get(gate.id) === 'pending')
  553. while (pending.length > 0) {
  554. const gate = pending.find(item => (item.needs ?? []).some((id) => {
  555. const state = states.get(id)
  556. return state === 'failed' || state === 'skipped'
  557. }))
  558. if (gate === undefined) throw new Error('run-gates: validated graph stalled without a failed dependency.')
  559. const failedDeps = (gate.needs ?? []).filter((id) => {
  560. const state = states.get(id)
  561. return state === 'failed' || state === 'skipped'
  562. })
  563. const result: GateResult = {
  564. gate,
  565. status: 'skipped',
  566. durationMs: 0,
  567. output: [],
  568. exitCode: null,
  569. signalCode: null,
  570. error: `dependency failed or skipped: ${failedDeps.join(', ')}`,
  571. }
  572. states.set(gate.id, 'skipped')
  573. results.set(gate.id, result)
  574. observe(result)
  575. pending = pending.filter(item => item !== gate)
  576. }
  577. break
  578. }
  579. if (!madeProgress) {
  580. const settled = await Promise.race(running.map(async item => ({ item, result: await item.promise })))
  581. running.splice(running.indexOf(settled.item), 1)
  582. states.set(settled.item.gate.id, settled.result.status)
  583. results.set(settled.item.gate.id, settled.result)
  584. observe(settled.result)
  585. }
  586. }
  587. return gates.map((gate) => {
  588. const result = results.get(gate.id)
  589. if (result === undefined) throw new Error(`run-gates: missing result for ${gate.id}.`)
  590. return result
  591. })
  592. }
  593. function dependenciesPassed(gate: Gate, states: Map<string, GateState>): boolean {
  594. return (gate.needs ?? []).every(id => states.get(id) === 'passed')
  595. }
  596. /**
  597. * Execute one gate through the real shell-free child-process boundary.
  598. * @param gate - command and scheduler environment to execute.
  599. * @returns the complete process outcome.
  600. */
  601. export async function runGate(gate: Gate): Promise<GateResult> {
  602. const started = performance.now()
  603. const output: GateOutputChunk[] = []
  604. let spawnError: string | undefined
  605. const outcome = await new Promise<{
  606. exitCode: number | null
  607. signalCode: NodeJS.Signals | null
  608. }>((resolveExit) => {
  609. const child = spawn(gate.command, gate.args, {
  610. cwd: root,
  611. env: { ...process.env, ...gate.env },
  612. stdio: ['pipe', 'pipe', 'pipe'],
  613. })
  614. child.stdout.setEncoding('utf8')
  615. child.stderr.setEncoding('utf8')
  616. child.stdout.on('data', (chunk: string) => {
  617. output.push({ stream: 'stdout', text: chunk })
  618. })
  619. child.stderr.on('data', (chunk: string) => {
  620. output.push({ stream: 'stderr', text: chunk })
  621. })
  622. child.on('error', (error) => {
  623. spawnError = `failed to start command: ${error.message}`
  624. resolveExit({ exitCode: null, signalCode: null })
  625. })
  626. child.on('close', (exitCode, signalCode) => {
  627. resolveExit({ exitCode, signalCode })
  628. })
  629. child.stdin.end()
  630. })
  631. const { exitCode, signalCode } = outcome
  632. const status: GateResultStatus = exitCode === 0 && signalCode === null && spawnError === undefined ? 'passed' : 'failed'
  633. const result: GateResult = {
  634. gate,
  635. status,
  636. durationMs: performance.now() - started,
  637. output,
  638. exitCode,
  639. signalCode,
  640. }
  641. if (spawnError !== undefined) result.error = spawnError
  642. return result
  643. }
  644. /**
  645. * Format every independently observed failure fact for the aggregate summary.
  646. * @param result - unsuccessful gate result.
  647. * @returns error, exit, and signal facts without allowing one to hide another.
  648. */
  649. export function formatGateResultReason(result: GateResult): string {
  650. const facts: string[] = []
  651. if (result.error !== undefined) facts.push(result.error)
  652. if (result.exitCode !== null) facts.push(`exit ${result.exitCode}`)
  653. if (result.signalCode !== null) facts.push(`signal ${result.signalCode}`)
  654. return facts.length === 0 ? 'no exit code or signal' : facts.join(', ')
  655. }
  656. function printResult(result: GateResult): void {
  657. const verbose = process.env.DSH_GATE_VERBOSE === '1'
  658. const seconds = (result.durationMs / 1000).toFixed(2)
  659. if (result.status === 'passed' && !verbose) {
  660. console.log(`run-gates: PASS ${result.gate.label} (${seconds}s)`)
  661. return
  662. }
  663. const heading = `${result.status.toUpperCase()} ${result.gate.label} (${seconds}s)`
  664. const writeHeading = result.status === 'passed' ? console.log : console.error
  665. writeHeading(`\n== ${heading} ==`)
  666. if (result.status !== 'passed') {
  667. console.error(`command: ${result.gate.displayCommand}`)
  668. console.error(`outcome: ${formatGateResultReason(result)}`)
  669. }
  670. printOutput(result.output)
  671. }
  672. function printSummary(results: GateResult[], durationMs: number): void {
  673. const passed = results.filter(result => result.status === 'passed').length
  674. const failed = results.filter(result => result.status === 'failed').length
  675. const skipped = results.filter(result => result.status === 'skipped').length
  676. const seconds = (durationMs / 1000).toFixed(2)
  677. console.log(`\nrun-gates: ${passed} passed, ${failed} failed, ${skipped} skipped in ${seconds}s.`)
  678. const unsuccessful = results.filter(result => result.status === 'failed' || result.status === 'skipped')
  679. if (unsuccessful.length === 0) return
  680. console.error('run-gates: unsuccessful gates:')
  681. for (const result of unsuccessful) {
  682. const duration = (result.durationMs / 1000).toFixed(2)
  683. const reason = formatGateResultReason(result)
  684. const disposition = result.gate.allowFailure === true ? 'NON-BLOCKING ' : ''
  685. console.error(` - ${disposition}${result.status.toUpperCase()} ${result.gate.label} (${duration}s, ${reason})`)
  686. console.error(` ${result.gate.displayCommand}`)
  687. }
  688. }
  689. function printOutput(output: GateOutputChunk[]): void {
  690. for (const chunk of output) {
  691. if (chunk.stream === 'stdout') process.stdout.write(chunk.text)
  692. else process.stderr.write(chunk.text)
  693. }
  694. }