run-gates.ts 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  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 threadBound = oxlintThreadBound()
  353. if (threadBound !== undefined) {
  354. return pnpmExec('lint', ['oxlint', '.', `--threads=${threadBound}`], {
  355. label: 'lint',
  356. env: { GOMAXPROCS: threadBound },
  357. })
  358. }
  359. return pnpmScript('lint', 'lint')
  360. }
  361. function oxlintThreadBound(): string | undefined {
  362. const raw = process.env.DSH_OXLINT_THREADS
  363. if (raw === undefined || raw === '') return undefined
  364. const parsed = Number.parseInt(raw, 10)
  365. if (!Number.isSafeInteger(parsed) || parsed < 1 || String(parsed) !== raw) {
  366. throw new Error(`run-gates: DSH_OXLINT_THREADS must be a positive integer, got ${JSON.stringify(raw)}.`)
  367. }
  368. return raw
  369. }
  370. function coverageGate(): Gate {
  371. return pnpmExec('coverage', [
  372. 'vitest',
  373. 'run',
  374. '--coverage',
  375. ...positiveIntArg('DSH_COVERAGE_MAX_WORKERS', '--maxWorkers'),
  376. ], {
  377. label: 'test:coverage',
  378. })
  379. }
  380. // Example and package snapshots boot their bins in `lib` mode (built artifacts under plain Node,
  381. // plugins via real exports); repository-script snapshots execute their real source entry path.
  382. // Build-owning modes wait on `build`; a restored-artifact mode passes its validation dependency.
  383. function snapshotGate(needs: string[] = ['build']): Gate {
  384. return pnpmScript('snapshot', 'test:snapshot', {
  385. env: { DSH_EXAMPLE_MODE: 'lib' },
  386. needs,
  387. })
  388. }
  389. function builtPackageInvariantsGate(needs?: string[]): Gate {
  390. return pnpmScript('built-package-invariants', 'verify-built-package-invariants', {
  391. label: 'built package invariants',
  392. ...needs === undefined ? {} : { needs },
  393. })
  394. }
  395. function positiveIntArg(envName: string, flag: string): string[] {
  396. const raw = process.env[envName]
  397. if (raw === undefined || raw === '') return []
  398. const parsed = Number.parseInt(raw, 10)
  399. if (!Number.isSafeInteger(parsed) || parsed < 1 || String(parsed) !== raw) {
  400. throw new Error(`run-gates: ${envName} must be a positive integer, got ${JSON.stringify(raw)}.`)
  401. }
  402. return [`${flag}=${raw}`]
  403. }
  404. function flagEnabled(envName: string): boolean {
  405. const raw = process.env[envName]
  406. if (raw === undefined || raw === '') return false
  407. if (raw !== '1') throw new Error(`run-gates: ${envName} must be 1 when set, got ${JSON.stringify(raw)}.`)
  408. return true
  409. }
  410. function hygieneLeafGates(options: { artifactNeeds?: string[] } = {}): Gate[] {
  411. const artifactOptions = options.artifactNeeds === undefined ? {} : { needs: options.artifactNeeds }
  412. return [
  413. pnpmScript('knip', 'knip'),
  414. pnpmScript('publint', 'publint', artifactOptions),
  415. pnpmScript('constraints', 'constraints'),
  416. pnpmScript('package-invariants', 'verify-package-invariants', { label: 'package invariants' }),
  417. builtPackageInvariantsGate(options.artifactNeeds),
  418. pnpmScript('node-next-types', 'verify-node-next-types', {
  419. label: 'node-next types',
  420. ...artifactOptions,
  421. }),
  422. ]
  423. }
  424. function docSyncLeafGates(options: {
  425. docTypecheckNeeds?: string[]
  426. docTypecheckEnv?: Record<string, string | undefined>
  427. docsBuildScript?: 'docs:build' | 'docs:build:mpa'
  428. } = {}): Gate[] {
  429. const docTypecheckOptions: Partial<Gate> = {}
  430. if (options.docTypecheckNeeds !== undefined) docTypecheckOptions.needs = options.docTypecheckNeeds
  431. if (options.docTypecheckEnv !== undefined) docTypecheckOptions.env = options.docTypecheckEnv
  432. return [
  433. pnpmScript('doc-typecheck', 'doc-typecheck', docTypecheckOptions),
  434. pnpmScript('cordis-catalog', 'verify-cordis-catalog', { label: 'cordis catalog' }),
  435. pnpmScript('cordis-api', 'verify-cordis-api', { label: 'cordis api' }),
  436. pnpmScript('export-jsdoc', 'verify-export-jsdoc', { label: 'export jsdoc' }),
  437. pnpmScript('tool-catalog', 'verify-tool-catalog', { label: 'tool catalog' }),
  438. pnpmScript('config-catalog', 'verify-config-catalog', { label: 'config catalog' }),
  439. pnpmScript('persistence-catalog', 'verify-persistence-catalog', { label: 'persistence catalog' }),
  440. pnpmScript('doc-graphs', 'verify-doc-graphs', { label: 'doc graphs' }),
  441. pnpmScript('scoped-events', 'verify-scoped-events', { label: 'scoped events' }),
  442. pnpmScript('markdown-wrap', 'verify-md-wrap', { label: 'markdown wrap' }),
  443. pnpmScript('markdown-links', 'verify-md-links', { label: 'markdown links' }),
  444. pnpmScript('doc-refs', 'verify-doc-refs', { label: 'doc refs' }),
  445. pnpmScript('package-paths', 'verify-package-paths', { label: 'package paths' }),
  446. pnpmScript('package-readme-model-experience', 'verify-package-readme-model-experience', { label: 'package README model experience' }),
  447. pnpmScript('mermaid', 'verify-mermaid'),
  448. pnpmScript('agent-note-classification', 'verify-agent-note-classification', { label: 'agent note classification' }),
  449. pnpmScript('agent-note-format', 'verify-agent-note-format', { label: 'agent note format' }),
  450. pnpmScript('archived-agent-notes', 'verify-archived-agent-notes', { label: 'archived agent notes' }),
  451. pnpmScript('type-equivalence', 'verify-type-equiv', { label: 'type equivalence' }),
  452. pnpmScript('translation-prompt', 'verify-translation-prompt', { label: 'translation prompt' }),
  453. pnpmScript('translation-pairing', 'verify-translation-pairing', { label: 'translation pairing' }),
  454. pnpmScript('doc-budgets', 'verify-doc-budgets', { label: 'doc budgets' }),
  455. pnpmExec('docs-site-projection', ['vitest', 'run', 'scripts/project-doc-site.spec.ts'], {
  456. label: 'documentation projection',
  457. }),
  458. // Keep the VitePress build itself in one gate because projection rewrites website/.generated.
  459. pnpmScript('docs-site-build', options.docsBuildScript ?? 'docs:build', { label: 'documentation build' }),
  460. pnpmScript('package-readme-limitations', 'verify-package-readme-limitations', { label: 'package README limitations' }),
  461. ]
  462. }
  463. function builtBinSmokeGate(needs: string[] = ['build']): Gate {
  464. return pnpmExec('built-bin-smoke', [
  465. 'vitest',
  466. 'run',
  467. '--config',
  468. 'vitest.e2e.config.ts',
  469. 'examples/headless-agent/tests/keyless-smoke.e2e.ts',
  470. 'examples/tui-agent/tests/tui-keyless-smoke.e2e.ts',
  471. 'packages/examples/cli-demo/tests/built-bin.e2e.ts',
  472. 'packages/examples/acp-demo/tests/built-bin.e2e.ts',
  473. 'packages/ui/jsonrpc/tests/built-scope-carrier.e2e.ts',
  474. // The worker-entry packages' built bundles: the only automated proof
  475. // that lib/index.js resolves its sibling lib/worker.cjs under plain node
  476. // (the e2e lane runs unbuilt, so these files self-skip there).
  477. 'packages/workflow/workflow-workerthread/tests/built-worker.e2e.ts',
  478. 'packages/code-runtime/code-runtime-worker/tests/built-lib.e2e.ts',
  479. ], {
  480. label: 'built-bin smoke',
  481. needs,
  482. env: { DSH_EXAMPLE_MODE: 'lib' },
  483. })
  484. }
  485. /**
  486. * Reject a gate list whose graph cannot be executed unambiguously.
  487. * @param gates - complete aggregate to validate.
  488. */
  489. function validateGateGraph(gates: readonly Gate[]): void {
  490. if (gates.length === 0) throw new Error('run-gates: gate graph has no gates.')
  491. const ids = new Set<string>()
  492. for (const gate of gates) {
  493. if (ids.has(gate.id)) throw new Error(`run-gates: duplicate gate id ${JSON.stringify(gate.id)}.`)
  494. ids.add(gate.id)
  495. }
  496. for (const gate of gates) {
  497. for (const dependency of gate.needs ?? []) {
  498. if (!ids.has(dependency)) {
  499. throw new Error(`run-gates: gate ${JSON.stringify(gate.id)} depends on unknown gate ${JSON.stringify(dependency)}.`)
  500. }
  501. }
  502. }
  503. const cycle = findDependencyCycle(gates)
  504. if (cycle !== undefined) throw new Error(`run-gates: dependency cycle: ${cycle.join(' -> ')}.`)
  505. }
  506. function findDependencyCycle(gates: readonly Gate[]): string[] | undefined {
  507. const byId = new Map(gates.map(gate => [gate.id, gate]))
  508. const complete = new Set<string>()
  509. const active = new Map<string, number>()
  510. const path: string[] = []
  511. const visit = (id: string): string[] | undefined => {
  512. if (complete.has(id)) return undefined
  513. const cycleStart = active.get(id)
  514. if (cycleStart !== undefined) return [...path.slice(cycleStart), id]
  515. const gate = byId.get(id)
  516. if (gate === undefined) return undefined
  517. active.set(id, path.length)
  518. path.push(id)
  519. for (const dependency of gate.needs ?? []) {
  520. const cycle = visit(dependency)
  521. if (cycle !== undefined) return cycle
  522. }
  523. path.pop()
  524. active.delete(id)
  525. complete.add(id)
  526. return undefined
  527. }
  528. for (const gate of gates) {
  529. const cycle = visit(gate.id)
  530. if (cycle !== undefined) return cycle
  531. }
  532. return undefined
  533. }
  534. /**
  535. * Validate and run one aggregate before the injected executor can start a child.
  536. * @param gates - complete aggregate to execute.
  537. * @param maxActive - maximum concurrent child count.
  538. * @param execute - child-process executor.
  539. * @param observe - result observer invoked when each gate settles.
  540. * @returns results in aggregate order.
  541. */
  542. export async function runGates(
  543. gates: Gate[],
  544. maxActive: number,
  545. execute: GateExecutor,
  546. observe: ResultObserver = () => {},
  547. ): Promise<GateResult[]> {
  548. validateGateGraph(gates)
  549. if (!Number.isSafeInteger(maxActive) || maxActive < 1) {
  550. throw new Error(`run-gates: max concurrency must be a positive integer, got ${JSON.stringify(maxActive)}.`)
  551. }
  552. const states = new Map<string, GateState>(gates.map(gate => [gate.id, 'pending']))
  553. const results = new Map<string, GateResult>()
  554. const running: RunningGate[] = []
  555. for (;;) {
  556. let madeProgress = false
  557. while (running.length < maxActive) {
  558. const ready = gates.find(gate => states.get(gate.id) === 'pending' && dependenciesPassed(gate, states))
  559. if (ready === undefined) break
  560. states.set(ready.id, 'running')
  561. running.push({ gate: ready, promise: execute(ready) })
  562. console.log(`run-gates: start ${ready.label}`)
  563. madeProgress = true
  564. }
  565. if (running.length === 0) {
  566. let pending = gates.filter(gate => states.get(gate.id) === 'pending')
  567. while (pending.length > 0) {
  568. const gate = pending.find(item => (item.needs ?? []).some((id) => {
  569. const state = states.get(id)
  570. return state === 'failed' || state === 'skipped'
  571. }))
  572. if (gate === undefined) throw new Error('run-gates: validated graph stalled without a failed dependency.')
  573. const failedDeps = (gate.needs ?? []).filter((id) => {
  574. const state = states.get(id)
  575. return state === 'failed' || state === 'skipped'
  576. })
  577. const result: GateResult = {
  578. gate,
  579. status: 'skipped',
  580. durationMs: 0,
  581. output: [],
  582. exitCode: null,
  583. signalCode: null,
  584. error: `dependency failed or skipped: ${failedDeps.join(', ')}`,
  585. }
  586. states.set(gate.id, 'skipped')
  587. results.set(gate.id, result)
  588. observe(result)
  589. pending = pending.filter(item => item !== gate)
  590. }
  591. break
  592. }
  593. if (!madeProgress) {
  594. const settled = await Promise.race(running.map(async item => ({ item, result: await item.promise })))
  595. running.splice(running.indexOf(settled.item), 1)
  596. states.set(settled.item.gate.id, settled.result.status)
  597. results.set(settled.item.gate.id, settled.result)
  598. observe(settled.result)
  599. }
  600. }
  601. return gates.map((gate) => {
  602. const result = results.get(gate.id)
  603. if (result === undefined) throw new Error(`run-gates: missing result for ${gate.id}.`)
  604. return result
  605. })
  606. }
  607. function dependenciesPassed(gate: Gate, states: Map<string, GateState>): boolean {
  608. return (gate.needs ?? []).every(id => states.get(id) === 'passed')
  609. }
  610. /**
  611. * Execute one gate through the real shell-free child-process boundary.
  612. * @param gate - command and scheduler environment to execute.
  613. * @returns the complete process outcome.
  614. */
  615. export async function runGate(gate: Gate): Promise<GateResult> {
  616. const started = performance.now()
  617. const output: GateOutputChunk[] = []
  618. let spawnError: string | undefined
  619. const outcome = await new Promise<{
  620. exitCode: number | null
  621. signalCode: NodeJS.Signals | null
  622. }>((resolveExit) => {
  623. const child = spawn(gate.command, gate.args, {
  624. cwd: root,
  625. env: { ...process.env, ...gate.env },
  626. stdio: ['pipe', 'pipe', 'pipe'],
  627. })
  628. child.stdout.setEncoding('utf8')
  629. child.stderr.setEncoding('utf8')
  630. child.stdout.on('data', (chunk: string) => {
  631. output.push({ stream: 'stdout', text: chunk })
  632. })
  633. child.stderr.on('data', (chunk: string) => {
  634. output.push({ stream: 'stderr', text: chunk })
  635. })
  636. child.on('error', (error) => {
  637. spawnError = `failed to start command: ${error.message}`
  638. resolveExit({ exitCode: null, signalCode: null })
  639. })
  640. child.on('close', (exitCode, signalCode) => {
  641. resolveExit({ exitCode, signalCode })
  642. })
  643. child.stdin.end()
  644. })
  645. const { exitCode, signalCode } = outcome
  646. const status: GateResultStatus = exitCode === 0 && signalCode === null && spawnError === undefined ? 'passed' : 'failed'
  647. const result: GateResult = {
  648. gate,
  649. status,
  650. durationMs: performance.now() - started,
  651. output,
  652. exitCode,
  653. signalCode,
  654. }
  655. if (spawnError !== undefined) result.error = spawnError
  656. return result
  657. }
  658. /**
  659. * Format every independently observed failure fact for the aggregate summary.
  660. * @param result - unsuccessful gate result.
  661. * @returns error, exit, and signal facts without allowing one to hide another.
  662. */
  663. export function formatGateResultReason(result: GateResult): string {
  664. const facts: string[] = []
  665. if (result.error !== undefined) facts.push(result.error)
  666. if (result.exitCode !== null) facts.push(`exit ${result.exitCode}`)
  667. if (result.signalCode !== null) facts.push(`signal ${result.signalCode}`)
  668. return facts.length === 0 ? 'no exit code or signal' : facts.join(', ')
  669. }
  670. function printResult(result: GateResult): void {
  671. const verbose = process.env.DSH_GATE_VERBOSE === '1'
  672. const seconds = (result.durationMs / 1000).toFixed(2)
  673. if (result.status === 'passed' && !verbose) {
  674. console.log(`run-gates: PASS ${result.gate.label} (${seconds}s)`)
  675. return
  676. }
  677. const heading = `${result.status.toUpperCase()} ${result.gate.label} (${seconds}s)`
  678. const writeHeading = result.status === 'passed' ? console.log : console.error
  679. writeHeading(`\n== ${heading} ==`)
  680. if (result.status !== 'passed') {
  681. console.error(`command: ${result.gate.displayCommand}`)
  682. console.error(`outcome: ${formatGateResultReason(result)}`)
  683. }
  684. printOutput(result.output)
  685. }
  686. function printSummary(results: GateResult[], durationMs: number): void {
  687. const passed = results.filter(result => result.status === 'passed').length
  688. const failed = results.filter(result => result.status === 'failed').length
  689. const skipped = results.filter(result => result.status === 'skipped').length
  690. const seconds = (durationMs / 1000).toFixed(2)
  691. console.log(`\nrun-gates: ${passed} passed, ${failed} failed, ${skipped} skipped in ${seconds}s.`)
  692. const unsuccessful = results.filter(result => result.status === 'failed' || result.status === 'skipped')
  693. if (unsuccessful.length === 0) return
  694. console.error('run-gates: unsuccessful gates:')
  695. for (const result of unsuccessful) {
  696. const duration = (result.durationMs / 1000).toFixed(2)
  697. const reason = formatGateResultReason(result)
  698. const disposition = result.gate.allowFailure === true ? 'NON-BLOCKING ' : ''
  699. console.error(` - ${disposition}${result.status.toUpperCase()} ${result.gate.label} (${duration}s, ${reason})`)
  700. console.error(` ${result.gate.displayCommand}`)
  701. }
  702. }
  703. function printOutput(output: GateOutputChunk[]): void {
  704. for (const chunk of output) {
  705. if (chunk.stream === 'stdout') process.stdout.write(chunk.text)
  706. else process.stderr.write(chunk.text)
  707. }
  708. }