run-gates.ts 31 KB

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