run-gates.ts 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  1. /**
  2. * Run local and CI quality gates with bounded in-process scheduling.
  3. *
  4. * The gate vocabulary stays in package.json; this runner only decides which
  5. * independent commands can overlap and which commands wait for built artifacts.
  6. */
  7. import { spawn } from 'node:child_process'
  8. import { availableParallelism } from 'node:os'
  9. import { resolve } from 'node:path'
  10. import { performance } from 'node:perf_hooks'
  11. import { coverageArgs } from './coverage-shards.ts'
  12. import { selectLintShard } from './lint-shards.ts'
  13. import { selectSnapshotLane } from './snapshot-shards.ts'
  14. import { selectStaticGates } from './static-shards.ts'
  15. type Mode =
  16. | 'ci-primary'
  17. | 'ci-primary-large-runner'
  18. | 'ci-static'
  19. | 'ci-lint'
  20. | 'ci-coverage'
  21. | 'ci-snapshot'
  22. | 'ci-artifacts'
  23. | 'ci-windows-blocking'
  24. | 'ci-windows-complete'
  25. | 'ci-windows-observational'
  26. | 'node-compat'
  27. | 'pre-push'
  28. | 'doc-sync'
  29. type GateStatus = 'pending' | 'running' | 'passed' | 'failed' | 'skipped'
  30. 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. input?: string
  39. verify?: (result: GateResult) => Promise<void>
  40. allowFailure?: boolean
  41. }
  42. interface GateResult {
  43. gate: Gate
  44. status: GateStatus
  45. durationMs: number
  46. stdout: string
  47. stderr: string
  48. output: GateOutputChunk[]
  49. exitCode: number | null
  50. error?: string
  51. }
  52. interface GateOutputChunk {
  53. stream: 'stdout' | 'stderr'
  54. text: string
  55. }
  56. interface RunningGate {
  57. gate: Gate
  58. promise: Promise<GateResult>
  59. }
  60. interface ConcurrencyDefault {
  61. workers: number
  62. source: string
  63. }
  64. const root = resolve(import.meta.dirname, '..')
  65. const mode = parseMode(process.argv[2])
  66. const gates = gatesForMode(mode)
  67. const concurrencyDefault = defaultConcurrency(mode, gates.length)
  68. const concurrencyOverride = process.env.DSH_GATE_CONCURRENCY
  69. const maxConcurrency = concurrencyFromEnv('DSH_GATE_CONCURRENCY', concurrencyDefault.workers)
  70. const verbose = process.env.DSH_GATE_VERBOSE === '1'
  71. const startedAt = performance.now()
  72. const concurrencySource = concurrencyOverride === undefined || concurrencyOverride === ''
  73. ? concurrencyDefault.source
  74. : '$DSH_GATE_CONCURRENCY'
  75. console.log(`run-gates: ${mode} running ${gates.length} gate(s) with ${maxConcurrency} worker(s) from ${concurrencySource}.`)
  76. const results = await runGates(gates, maxConcurrency)
  77. printSummary(results, performance.now() - startedAt)
  78. if (results.some(result => result.gate.allowFailure !== true && (result.status === 'failed' || result.status === 'skipped'))) {
  79. process.exit(1)
  80. }
  81. function parseMode(raw: string | undefined): Mode {
  82. switch (raw) {
  83. case 'ci-primary':
  84. case 'ci-primary-large-runner':
  85. case 'ci-static':
  86. case 'ci-lint':
  87. case 'ci-coverage':
  88. case 'ci-snapshot':
  89. case 'ci-artifacts':
  90. case 'ci-windows-blocking':
  91. case 'ci-windows-complete':
  92. case 'ci-windows-observational':
  93. case 'node-compat':
  94. case 'pre-push':
  95. case 'doc-sync':
  96. return raw
  97. default:
  98. throw new Error(
  99. `run-gates: expected mode ci-primary | ci-primary-large-runner | ci-static | ci-lint | ci-coverage | ci-snapshot | ci-artifacts | ci-windows-blocking | ci-windows-complete | ci-windows-observational | node-compat | pre-push | doc-sync, got ${JSON.stringify(raw)}.`,
  100. )
  101. }
  102. }
  103. function defaultConcurrency(selectedMode: Mode, total: number): ConcurrencyDefault {
  104. const available = availableParallelism()
  105. // Local modes cap workers: several doc gates each build a full ts.Program,
  106. // so an uncapped default on a large host trades wall clock for memory blowups.
  107. const localCap = selectedMode === 'pre-push' || selectedMode === 'doc-sync'
  108. const modeLimit = localCap ? Math.min(4, available) : available
  109. return {
  110. workers: Math.min(total, modeLimit),
  111. source: localCap
  112. ? `${available} available CPU(s), ${selectedMode} cap 4`
  113. : `${available} available CPU(s)`,
  114. }
  115. }
  116. function concurrencyFromEnv(name: string, fallback: number): number {
  117. const raw = process.env[name]
  118. if (raw === undefined || raw === '') return fallback
  119. const parsed = Number.parseInt(raw, 10)
  120. if (!Number.isSafeInteger(parsed) || parsed < 1) {
  121. throw new Error(`run-gates: ${name} must be a positive integer, got ${JSON.stringify(raw)}.`)
  122. }
  123. return parsed
  124. }
  125. function pnpmScript(id: string, script: string, options: Partial<Gate> = {}): Gate {
  126. return {
  127. id,
  128. label: options.label ?? script,
  129. displayCommand: `pnpm run ${script}`,
  130. ...pnpmInvocation(['run', script]),
  131. ...options,
  132. }
  133. }
  134. function pnpmExec(id: string, args: string[], options: Partial<Gate> = {}): Gate {
  135. return {
  136. id,
  137. label: options.label ?? `pnpm exec ${args.join(' ')}`,
  138. displayCommand: `pnpm exec ${args.join(' ')}`,
  139. ...pnpmInvocation(['exec', ...args]),
  140. ...options,
  141. }
  142. }
  143. function pnpmInvocation(args: string[]): Pick<Gate, 'command' | 'args'> {
  144. const entrypoint = process.env.npm_execpath
  145. if (entrypoint === undefined || entrypoint === '') {
  146. throw new Error('run-gates: npm_execpath is unavailable; invoke the runner through a pnpm package script.')
  147. }
  148. // Windows cannot spawn the pnpm.cmd shim directly; the JavaScript entrypoint keeps every host shell-free.
  149. return { command: process.execPath, args: [entrypoint, ...args] }
  150. }
  151. function nodeOptions(...options: string[]): string {
  152. return [process.env.NODE_OPTIONS, ...options].filter(option => option !== undefined && option !== '').join(' ')
  153. }
  154. function gatesForMode(selected: Mode): Gate[] {
  155. switch (selected) {
  156. case 'ci-primary':
  157. return ciPrimaryGates()
  158. case 'ci-primary-large-runner':
  159. return ciPrimaryLargeRunnerGates()
  160. case 'ci-static':
  161. return ciStaticGates()
  162. case 'ci-lint': {
  163. const selection = selectLintShard(process.env.DSH_LINT_SHARD)
  164. return [
  165. lintGate(selection.eslintTargets),
  166. ...selection.includeDuplication ? [pnpmScript('duplication', 'duplication')] : [],
  167. ]
  168. }
  169. case 'ci-coverage':
  170. return [coverageGate()]
  171. case 'ci-snapshot':
  172. return flagEnabled('DSH_SNAPSHOT_PREBUILT')
  173. ? [snapshotGate([])]
  174. : [pnpmScript('build', 'build'), snapshotGate()]
  175. case 'ci-artifacts':
  176. return ciArtifactGates()
  177. case 'ci-windows-blocking':
  178. return ciWindowsBlockingGates()
  179. case 'ci-windows-complete':
  180. return ciWindowsCompleteGates()
  181. case 'ci-windows-observational':
  182. return ciWindowsObservationalGates()
  183. case 'node-compat':
  184. return [
  185. ...flagEnabled('DSH_NODE_COMPAT_SKIP_TYPECHECK') ? [] : [pnpmScript('typecheck', 'typecheck')],
  186. pnpmExec('source-worker-smoke', [
  187. 'vitest',
  188. 'run',
  189. 'packages/workflow/workflow-workerthread/tests/source-worker.compat.spec.ts',
  190. ], { label: 'source worker smoke' }),
  191. pnpmExec('jsonl-zstd-smoke', [
  192. 'vitest',
  193. 'run',
  194. 'packages/session-persistence/session-persistence-jsonl/tests/zstd.compat.spec.ts',
  195. ], { label: 'JSONL Zstandard smoke' }),
  196. ]
  197. case 'pre-push':
  198. return [
  199. pnpmScript('runtime-closure', 'verify-runtime-closure', { label: 'runtime closure' }),
  200. pnpmScript('cordis-config', 'verify-cordis-config', { label: 'Cordis config' }),
  201. pnpmScript('test', 'test'),
  202. pnpmScript('duplication', 'duplication'),
  203. snapshotGate(),
  204. pnpmScript('build', 'build'),
  205. ...hygieneLeafGates({ artifactNeeds: ['build'] }),
  206. ...docSyncLeafGates({
  207. docTypecheckNeeds: ['build'],
  208. docTypecheckEnv: { DSH_DOC_TYPECHECK_USE_BUILD_OUTPUT: '1' },
  209. }),
  210. pnpmScript('module-graph', 'verify-module-graph', { label: 'module graph' }),
  211. ]
  212. case 'doc-sync':
  213. return docSyncLeafGates()
  214. }
  215. }
  216. function ciPrimaryGates(): Gate[] {
  217. return [
  218. pnpmScript('runtime-closure', 'verify-runtime-closure', { label: 'runtime closure' }),
  219. pnpmScript('constraints', 'constraints'),
  220. pnpmScript('package-invariants', 'verify-package-invariants', { label: 'package invariants' }),
  221. pnpmScript('cordis-config', 'verify-cordis-config', { label: 'Cordis config' }),
  222. pnpmScript('typecheck', 'typecheck'),
  223. lintGate(),
  224. pnpmScript('duplication', 'duplication'),
  225. coverageGate(),
  226. snapshotGate(),
  227. ...docSyncLeafGates(),
  228. pnpmScript('module-graph', 'verify-module-graph', { label: 'module graph' }),
  229. pnpmScript('knip', 'knip'),
  230. pnpmScript('build', 'build', { needs: ['typecheck'] }),
  231. pnpmScript('publint', 'publint', { needs: ['build'] }),
  232. pnpmScript('node-next-types', 'verify-node-next-types', {
  233. label: 'node-next types',
  234. needs: ['build'],
  235. }),
  236. builtPackageInvariantsGate(['build']),
  237. builtBinSmokeGate(),
  238. ]
  239. }
  240. function ciPrimaryLargeRunnerGates(): Gate[] {
  241. // Typecheck does not consume build output, so a large runner can start both
  242. // together while snapshot and artifact consumers still wait for the build.
  243. return ciPrimaryGates()
  244. .filter(gate => gate.id !== 'docs-site-build')
  245. .map((gate) => {
  246. if (gate.id !== 'build') return gate
  247. const eagerBuild = { ...gate }
  248. delete eagerBuild.needs
  249. return eagerBuild
  250. })
  251. }
  252. function ciStaticGates(): Gate[] {
  253. const gates = [
  254. pnpmScript('runtime-closure', 'verify-runtime-closure', { label: 'runtime closure' }),
  255. pnpmScript('constraints', 'constraints'),
  256. pnpmScript('package-invariants', 'verify-package-invariants', { label: 'package invariants' }),
  257. pnpmScript('cordis-config', 'verify-cordis-config', { label: 'Cordis config' }),
  258. pnpmScript('build', 'build'),
  259. ...docSyncLeafGates({
  260. docTypecheckNeeds: ['build'],
  261. docTypecheckEnv: { DSH_DOC_TYPECHECK_USE_BUILD_OUTPUT: '1' },
  262. docsBuildScript: 'docs:build:mpa',
  263. }),
  264. pnpmScript('module-graph', 'verify-module-graph', { label: 'module graph' }),
  265. pnpmScript('knip', 'knip'),
  266. ]
  267. return selectStaticGates(gates, process.env.DSH_STATIC_SHARD)
  268. }
  269. function ciArtifactGates(): Gate[] {
  270. const shard = process.env.DSH_ARTIFACT_SHARD
  271. const metadataGates = [
  272. pnpmScript('build', 'build'),
  273. pnpmScript('publint', 'publint', { needs: ['build'] }),
  274. pnpmScript('node-next-types', 'verify-node-next-types', {
  275. label: 'node-next types',
  276. needs: ['build'],
  277. }),
  278. builtPackageInvariantsGate(['build']),
  279. ]
  280. if (shard === 'metadata') return metadataGates
  281. if (shard === 'smoke') return [pnpmScript('build', 'build'), builtBinSmokeGate()]
  282. if (shard !== undefined && shard !== '') {
  283. throw new Error(`run-gates: unknown DSH_ARTIFACT_SHARD ${JSON.stringify(shard)}.`)
  284. }
  285. return [...metadataGates, builtBinSmokeGate()]
  286. }
  287. function ciWindowsBlockingGates(): Gate[] {
  288. return [
  289. pnpmScript('windows-build', 'build', { label: 'build' }),
  290. pnpmScript('windows-site', 'docs:build', { label: 'production site' }),
  291. ]
  292. }
  293. function ciWindowsCompleteGates(): Gate[] {
  294. const observational = ciWindowsObservationalGates()
  295. // The required production site replaces the observational MPA build; both
  296. // VitePress modes write the same output directory and cannot overlap.
  297. .filter(gate => gate.id !== 'build' && gate.id !== 'docs-site-build')
  298. .map(gate => ({ ...gate, allowFailure: true }))
  299. return [
  300. pnpmScript('build', 'build'),
  301. pnpmScript('windows-site', 'docs:build', { label: 'production site' }),
  302. ...observational,
  303. ]
  304. }
  305. function ciWindowsObservationalGates(): Gate[] {
  306. return [
  307. ...ciStaticGates(),
  308. lintGate(),
  309. pnpmScript('duplication', 'duplication'),
  310. pnpmScript('publint', 'publint', { needs: ['build'] }),
  311. pnpmScript('node-next-types', 'verify-node-next-types', {
  312. label: 'node-next types',
  313. needs: ['build'],
  314. }),
  315. builtPackageInvariantsGate(['build']),
  316. builtBinSmokeGate(),
  317. ]
  318. }
  319. function lintGate(eslintTargets: readonly string[] = ['.']): Gate {
  320. const concurrencyArgs = eslintConcurrencyArgs()
  321. if (process.env.DSH_ESLINT_CACHE === '1') {
  322. return pnpmExec('lint', [
  323. 'eslint',
  324. ...eslintTargets,
  325. ...concurrencyArgs,
  326. '--cache',
  327. '--cache-location',
  328. '.cache/eslint/',
  329. '--cache-strategy',
  330. 'content',
  331. ], {
  332. label: 'lint',
  333. env: { NODE_OPTIONS: nodeOptions('--max-old-space-size=8192') },
  334. })
  335. }
  336. if (concurrencyArgs.length > 0) {
  337. return pnpmExec('lint', ['eslint', ...eslintTargets, ...concurrencyArgs], {
  338. label: 'lint',
  339. env: { NODE_OPTIONS: nodeOptions('--max-old-space-size=8192') },
  340. })
  341. }
  342. return pnpmScript('lint', 'lint', {
  343. env: { NODE_OPTIONS: nodeOptions('--max-old-space-size=8192') },
  344. })
  345. }
  346. function eslintConcurrencyArgs(): string[] {
  347. const raw = process.env.DSH_ESLINT_CONCURRENCY
  348. if (raw === undefined || raw === '') return []
  349. if (raw === 'auto') return ['--concurrency=auto']
  350. const parsed = Number.parseInt(raw, 10)
  351. if (!Number.isSafeInteger(parsed) || parsed < 1 || String(parsed) !== raw) {
  352. throw new Error(`run-gates: DSH_ESLINT_CONCURRENCY must be a positive integer or auto, got ${JSON.stringify(raw)}.`)
  353. }
  354. return [`--concurrency=${raw}`]
  355. }
  356. function coverageGate(): Gate {
  357. const shard = process.env.DSH_COVERAGE_SHARD
  358. return pnpmExec('coverage', [
  359. 'vitest',
  360. 'run',
  361. '--coverage',
  362. ...(shard === undefined || shard === '' ? [] : coverageArgs(shard)),
  363. ...positiveIntArg('DSH_COVERAGE_MAX_WORKERS', '--maxWorkers'),
  364. ], {
  365. label: 'test:coverage',
  366. })
  367. }
  368. // The snapshot suite boots the example bins in `lib` mode (built artifact under plain Node,
  369. // plugins via real exports) — CI and pre-push already build, so they exercise what ships rather
  370. // than the tsx/source path dev uses. It therefore waits on `build`.
  371. function snapshotGate(needs: string[] = ['build']): Gate {
  372. const lane = selectSnapshotLane(process.env.DSH_SNAPSHOT_LANE)
  373. return pnpmExec('snapshot', [
  374. 'vitest',
  375. 'run',
  376. '--config',
  377. 'vitest.snapshot.config.ts',
  378. ...lane.files,
  379. ], {
  380. label: 'test:snapshot',
  381. env: {
  382. DSH_EXAMPLE_MODE: 'lib',
  383. ...lane.scenarioShard === undefined ? {} : { DSH_SNAPSHOT_SCENARIO_SHARD: lane.scenarioShard },
  384. },
  385. ...needs.length === 0 ? {} : { needs },
  386. })
  387. }
  388. function builtPackageInvariantsGate(needs?: string[]): Gate {
  389. return pnpmScript('built-package-invariants', 'verify-built-package-invariants', {
  390. label: 'built package invariants',
  391. ...needs === undefined ? {} : { needs },
  392. })
  393. }
  394. function positiveIntArg(envName: string, flag: string): string[] {
  395. const raw = process.env[envName]
  396. if (raw === undefined || raw === '') return []
  397. const parsed = Number.parseInt(raw, 10)
  398. if (!Number.isSafeInteger(parsed) || parsed < 1 || String(parsed) !== raw) {
  399. throw new Error(`run-gates: ${envName} must be a positive integer, got ${JSON.stringify(raw)}.`)
  400. }
  401. return [`${flag}=${raw}`]
  402. }
  403. function flagEnabled(envName: string): boolean {
  404. const raw = process.env[envName]
  405. if (raw === undefined || raw === '') return false
  406. if (raw !== '1') throw new Error(`run-gates: ${envName} must be 1 when set, got ${JSON.stringify(raw)}.`)
  407. return true
  408. }
  409. function hygieneLeafGates(options: { artifactNeeds?: string[] } = {}): Gate[] {
  410. const artifactOptions = options.artifactNeeds === undefined ? {} : { needs: options.artifactNeeds }
  411. return [
  412. pnpmScript('knip', 'knip'),
  413. pnpmScript('publint', 'publint', artifactOptions),
  414. pnpmScript('constraints', 'constraints'),
  415. pnpmScript('package-invariants', 'verify-package-invariants', { label: 'package invariants' }),
  416. builtPackageInvariantsGate(options.artifactNeeds),
  417. pnpmScript('node-next-types', 'verify-node-next-types', {
  418. label: 'node-next types',
  419. ...artifactOptions,
  420. }),
  421. ]
  422. }
  423. function docSyncLeafGates(options: {
  424. docTypecheckNeeds?: string[]
  425. docTypecheckEnv?: Record<string, string | undefined>
  426. docsBuildScript?: 'docs:build' | 'docs:build:mpa'
  427. } = {}): Gate[] {
  428. const docTypecheckOptions: Partial<Gate> = {}
  429. if (options.docTypecheckNeeds !== undefined) docTypecheckOptions.needs = options.docTypecheckNeeds
  430. if (options.docTypecheckEnv !== undefined) docTypecheckOptions.env = options.docTypecheckEnv
  431. return [
  432. pnpmScript('doc-typecheck', 'doc-typecheck', docTypecheckOptions),
  433. pnpmScript('cordis-catalog', 'verify-cordis-catalog', { label: 'cordis catalog' }),
  434. pnpmScript('cordis-api', 'verify-cordis-api', { label: 'cordis api' }),
  435. pnpmScript('export-jsdoc', 'verify-export-jsdoc', { label: 'export jsdoc' }),
  436. pnpmScript('tool-catalog', 'verify-tool-catalog', { label: 'tool catalog' }),
  437. pnpmScript('config-catalog', 'verify-config-catalog', { label: 'config catalog' }),
  438. pnpmScript('persistence-catalog', 'verify-persistence-catalog', { label: 'persistence catalog' }),
  439. pnpmScript('doc-graphs', 'verify-doc-graphs', { label: 'doc graphs' }),
  440. pnpmScript('scoped-events', 'verify-scoped-events', { label: 'scoped events' }),
  441. pnpmScript('markdown-wrap', 'verify-md-wrap', { label: 'markdown wrap' }),
  442. pnpmScript('markdown-links', 'verify-md-links', { label: 'markdown links' }),
  443. pnpmScript('doc-refs', 'verify-doc-refs', { label: 'doc refs' }),
  444. pnpmScript('package-paths', 'verify-package-paths', { label: 'package paths' }),
  445. pnpmScript('package-readme-model-experience', 'verify-package-readme-model-experience', { label: 'package README model experience' }),
  446. pnpmScript('mermaid', 'verify-mermaid'),
  447. pnpmScript('agent-note-classification', 'verify-agent-note-classification', { label: 'agent note classification' }),
  448. pnpmScript('agent-note-format', 'verify-agent-note-format', { label: 'agent note format' }),
  449. pnpmScript('type-equivalence', 'verify-type-equiv', { label: 'type equivalence' }),
  450. pnpmScript('translation-prompt', 'verify-translation-prompt', { label: 'translation prompt' }),
  451. pnpmScript('translation-pairing', 'verify-translation-pairing', { label: 'translation pairing' }),
  452. pnpmScript('doc-budgets', 'verify-doc-budgets', { label: 'doc budgets' }),
  453. pnpmExec('docs-site-projection', ['vitest', 'run', 'scripts/project-doc-site.spec.ts'], {
  454. label: 'documentation projection',
  455. }),
  456. // Keep the VitePress build itself in one gate because projection rewrites website/.generated.
  457. pnpmScript('docs-site-build', options.docsBuildScript ?? 'docs:build', { label: 'documentation build' }),
  458. pnpmScript('package-readme-limitations', 'verify-package-readme-limitations', { label: 'package README limitations' }),
  459. ]
  460. }
  461. function builtBinSmokeGate(shard?: string): Gate {
  462. return pnpmExec('built-bin-smoke', [
  463. 'vitest',
  464. 'run',
  465. '--config',
  466. 'vitest.e2e.config.ts',
  467. 'examples/headless-agent/tests/keyless-smoke.e2e.ts',
  468. 'examples/tui-agent/tests/tui-keyless-smoke.e2e.ts',
  469. 'packages/examples/cli-demo/tests/built-bin.e2e.ts',
  470. 'packages/examples/acp-demo/tests/built-bin.e2e.ts',
  471. 'packages/ui/jsonrpc/tests/built-scope-carrier.e2e.ts',
  472. // The worker-entry packages' built bundles: the only automated proof
  473. // that lib/index.js resolves its sibling lib/worker.cjs under plain node
  474. // (the e2e lane runs unbuilt, so these files self-skip there).
  475. 'packages/workflow/workflow-workerthread/tests/built-worker.e2e.ts',
  476. 'packages/code-runtime/code-runtime-worker/tests/built-lib.e2e.ts',
  477. ...(shard === undefined ? [] : [`--shard=${shard}`]),
  478. ], {
  479. label: 'built-bin smoke',
  480. needs: ['build'],
  481. env: { DSH_EXAMPLE_MODE: 'lib' },
  482. })
  483. }
  484. async function runGates(allGates: Gate[], maxActive: number): Promise<GateResult[]> {
  485. const states = new Map<string, GateStatus>(allGates.map(gate => [gate.id, 'pending']))
  486. const results = new Map<string, GateResult>()
  487. const running: RunningGate[] = []
  488. for (;;) {
  489. let madeProgress = false
  490. while (running.length < maxActive) {
  491. const ready = allGates.find(gate => states.get(gate.id) === 'pending' && dependenciesPassed(gate, states))
  492. if (ready === undefined) break
  493. states.set(ready.id, 'running')
  494. running.push({ gate: ready, promise: runGate(ready) })
  495. console.log(`run-gates: start ${ready.label}`)
  496. madeProgress = true
  497. }
  498. if (running.length === 0) {
  499. const pending = allGates.filter(gate => states.get(gate.id) === 'pending')
  500. for (const gate of pending) {
  501. const failedDeps = (gate.needs ?? []).filter(id => states.get(id) !== 'passed')
  502. const result: GateResult = {
  503. gate,
  504. status: 'skipped',
  505. durationMs: 0,
  506. stdout: '',
  507. stderr: '',
  508. output: [],
  509. exitCode: null,
  510. error: `dependency failed or skipped: ${failedDeps.join(', ')}`,
  511. }
  512. states.set(gate.id, 'skipped')
  513. results.set(gate.id, result)
  514. printResult(result)
  515. }
  516. break
  517. }
  518. if (!madeProgress) {
  519. const settled = await Promise.race(running.map(async item => ({ item, result: await item.promise })))
  520. running.splice(running.indexOf(settled.item), 1)
  521. states.set(settled.item.gate.id, settled.result.status)
  522. results.set(settled.item.gate.id, settled.result)
  523. printResult(settled.result)
  524. }
  525. }
  526. return allGates.map((gate) => {
  527. const result = results.get(gate.id)
  528. if (result === undefined) throw new Error(`run-gates: missing result for ${gate.id}.`)
  529. return result
  530. })
  531. }
  532. function dependenciesPassed(gate: Gate, states: Map<string, GateStatus>): boolean {
  533. return (gate.needs ?? []).every(id => states.get(id) === 'passed')
  534. }
  535. async function runGate(gate: Gate): Promise<GateResult> {
  536. const started = performance.now()
  537. let stdout = ''
  538. let stderr = ''
  539. const output: GateOutputChunk[] = []
  540. let spawnError: string | undefined
  541. const exitCode = await new Promise<number | null>((resolveExit) => {
  542. const child = spawn(gate.command, gate.args, {
  543. cwd: root,
  544. env: { ...process.env, ...gate.env },
  545. stdio: ['pipe', 'pipe', 'pipe'],
  546. })
  547. child.stdout.setEncoding('utf8')
  548. child.stderr.setEncoding('utf8')
  549. child.stdout.on('data', (chunk: string) => {
  550. stdout += chunk
  551. output.push({ stream: 'stdout', text: chunk })
  552. })
  553. child.stderr.on('data', (chunk: string) => {
  554. stderr += chunk
  555. output.push({ stream: 'stderr', text: chunk })
  556. })
  557. child.on('error', (error) => {
  558. spawnError = `failed to start command: ${error.message}`
  559. resolveExit(null)
  560. })
  561. child.on('close', resolveExit)
  562. if (gate.input !== undefined) child.stdin.end(gate.input)
  563. else child.stdin.end()
  564. })
  565. let status: GateStatus = exitCode === 0 && spawnError === undefined ? 'passed' : 'failed'
  566. let error = spawnError
  567. if (status === 'passed' && gate.verify !== undefined) {
  568. try {
  569. await gate.verify({ gate, status, durationMs: performance.now() - started, stdout, stderr, output, exitCode })
  570. } catch (verifyError: unknown) {
  571. status = 'failed'
  572. error = verifyError instanceof Error ? verifyError.message : String(verifyError)
  573. }
  574. }
  575. const result: GateResult = {
  576. gate,
  577. status,
  578. durationMs: performance.now() - started,
  579. stdout,
  580. stderr,
  581. output,
  582. exitCode,
  583. }
  584. if (error !== undefined) result.error = error
  585. return result
  586. }
  587. function printResult(result: GateResult): void {
  588. const seconds = (result.durationMs / 1000).toFixed(2)
  589. if (result.status === 'passed' && !verbose) {
  590. console.log(`run-gates: PASS ${result.gate.label} (${seconds}s)`)
  591. return
  592. }
  593. const heading = `${result.status.toUpperCase()} ${result.gate.label} (${seconds}s)`
  594. const writeHeading = result.status === 'passed' ? console.log : console.error
  595. writeHeading(`\n== ${heading} ==`)
  596. if (result.status !== 'passed') console.error(`command: ${result.gate.displayCommand}`)
  597. printOutput(result.output)
  598. if (result.error !== undefined) console.error(result.error)
  599. }
  600. function printSummary(results: GateResult[], durationMs: number): void {
  601. const passed = results.filter(result => result.status === 'passed').length
  602. const failed = results.filter(result => result.status === 'failed').length
  603. const skipped = results.filter(result => result.status === 'skipped').length
  604. const seconds = (durationMs / 1000).toFixed(2)
  605. console.log(`\nrun-gates: ${passed} passed, ${failed} failed, ${skipped} skipped in ${seconds}s.`)
  606. const unsuccessful = results.filter(result => result.status === 'failed' || result.status === 'skipped')
  607. if (unsuccessful.length === 0) return
  608. console.error('run-gates: unsuccessful gates:')
  609. for (const result of unsuccessful) {
  610. const duration = (result.durationMs / 1000).toFixed(2)
  611. const reason = result.error ?? (result.exitCode === null ? 'no exit code' : `exit ${result.exitCode}`)
  612. const disposition = result.gate.allowFailure === true ? 'NON-BLOCKING ' : ''
  613. console.error(` - ${disposition}${result.status.toUpperCase()} ${result.gate.label} (${duration}s, ${reason})`)
  614. console.error(` ${result.gate.displayCommand}`)
  615. }
  616. }
  617. function printOutput(output: GateOutputChunk[]): void {
  618. for (const chunk of output) {
  619. if (chunk.stream === 'stdout') process.stdout.write(chunk.text)
  620. else process.stderr.write(chunk.text)
  621. }
  622. }