run-gates.ts 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569
  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, spawnSync } from 'node:child_process'
  9. import { readdirSync, readFileSync } from 'node:fs'
  10. import { availableParallelism } from 'node:os'
  11. import { resolve } from 'node:path'
  12. import { performance } from 'node:perf_hooks'
  13. import { CLIENT_BUILD_PROFILE_SELECTOR } from './client-build-environment.ts'
  14. import { COVERAGE_EXEMPT_ENV, coverageExemptHeavySuites } from './coverage-exempt.ts'
  15. import {
  16. COVERAGE_PARTITIONS_ENV,
  17. COVERAGE_TEST_TIMEOUT_ENV,
  18. coverageTestTimeoutArgs,
  19. parseCoveragePartitionCount,
  20. } from './coverage-partitions.ts'
  21. import { pnpmInvocation } from './pnpm-invocation.ts'
  22. /** A named aggregate exposed by the gate runner. */
  23. export type Mode =
  24. | 'ci-primary'
  25. | 'ci-linux-primary'
  26. | 'ci-static'
  27. | 'ci-lint-contracts-ready'
  28. | 'ci-coverage'
  29. | 'ci-snapshot'
  30. | 'ci-artifacts'
  31. | 'ci-consumers'
  32. | 'ci-windows-blocking'
  33. | 'ci-windows-complete'
  34. | 'ci-windows-observational'
  35. | 'node-compat'
  36. | 'check-all'
  37. | 'hygiene'
  38. | 'doc-sync'
  39. | 'doc-quick'
  40. type GateResultStatus = 'passed' | 'failed' | 'skipped'
  41. type GateState = 'pending' | 'running' | GateResultStatus
  42. /** A command and its dependency metadata inside one aggregate. */
  43. export interface Gate {
  44. id: string
  45. label: string
  46. displayCommand: string
  47. command: string
  48. args: string[]
  49. needs?: string[]
  50. /** Gate ids that must settle, regardless of outcome, before this gate starts. */
  51. after?: string[]
  52. env?: Record<string, string | undefined>
  53. /** Include this leaf in the build-free documentation aggregate. */
  54. quick?: boolean
  55. /** Keep a failure visible without failing the aggregate. */
  56. allowFailure?: boolean
  57. /** Write child output as it arrives instead of buffering it until completion. */
  58. streamOutput?: boolean
  59. }
  60. /** The observed outcome of one gate process. */
  61. export interface GateResult {
  62. gate: Gate
  63. status: GateResultStatus
  64. durationMs: number
  65. output: GateOutputChunk[]
  66. exitCode: number | null
  67. signalCode: NodeJS.Signals | null
  68. error?: string
  69. /** True when the shared abort signal terminated this gate before its outcome
  70. * was observed; such a result must not be reported as passed, even if the
  71. * child trapped the signal and exited zero. */
  72. aborted?: boolean
  73. }
  74. interface GateOutputChunk {
  75. stream: 'stdout' | 'stderr'
  76. text: string
  77. }
  78. interface RunningGate {
  79. gate: Gate
  80. promise: Promise<GateResult>
  81. }
  82. interface ConcurrencyDefault {
  83. workers: number
  84. source: string
  85. }
  86. type GateExecutor = (gate: Gate, signal?: AbortSignal) => Promise<GateResult>
  87. type ResultObserver = (result: GateResult) => void
  88. const root = resolve(import.meta.dirname, '..')
  89. if (import.meta.main) {
  90. process.exitCode = await main(process.argv.slice(2))
  91. }
  92. async function main(args: string[]): Promise<number> {
  93. const mode = parseMode(args[0])
  94. const gates = gatesForMode(mode)
  95. const concurrencyDefault = defaultConcurrency(mode, gates.length)
  96. const concurrencyOverride = process.env.DSH_GATE_CONCURRENCY
  97. const maxConcurrency = concurrencyFromEnv('DSH_GATE_CONCURRENCY', concurrencyDefault.workers)
  98. const concurrencySource = concurrencyOverride === undefined || concurrencyOverride === ''
  99. ? concurrencyDefault.source
  100. : '$DSH_GATE_CONCURRENCY'
  101. const failFast = flagEnabled('DSH_GATE_FAIL_FAST')
  102. const startedAt = performance.now()
  103. console.log(`run-gates: ${mode} running ${gates.length} gate(s) with ${maxConcurrency} worker(s) from ${concurrencySource}${failFast ? ', fail-fast after first blocking failure' : ''}.`)
  104. const results = await runGates(gates, maxConcurrency, runGate, printResult, cliGateOptions(failFast))
  105. printSummary(results, performance.now() - startedAt)
  106. return results.some(result => result.gate.allowFailure !== true && (result.status === 'failed' || result.status === 'skipped'))
  107. ? 1
  108. : 0
  109. }
  110. /**
  111. * The options the CLI entrypoint hands to the scheduler. Host signal
  112. * forwarding always follows fail-fast: children are detached only then, so
  113. * without it the forwarding would have no tree to drain.
  114. * @param failFast - whether `DSH_GATE_FAIL_FAST` is enabled.
  115. * @returns the scheduler options for the entrypoint.
  116. */
  117. export function cliGateOptions(failFast: boolean): RunGatesOptions {
  118. return { failFast, forwardProcessSignals: failFast }
  119. }
  120. function parseMode(raw: string | undefined): Mode {
  121. switch (raw) {
  122. case 'ci-primary':
  123. case 'ci-linux-primary':
  124. case 'ci-static':
  125. case 'ci-lint-contracts-ready':
  126. case 'ci-coverage':
  127. case 'ci-snapshot':
  128. case 'ci-artifacts':
  129. case 'ci-consumers':
  130. case 'ci-windows-blocking':
  131. case 'ci-windows-complete':
  132. case 'ci-windows-observational':
  133. case 'node-compat':
  134. case 'check-all':
  135. case 'hygiene':
  136. case 'doc-sync':
  137. case 'doc-quick':
  138. return raw
  139. default:
  140. throw new Error(
  141. `run-gates: expected mode ci-primary | ci-linux-primary | ci-static | ci-lint-contracts-ready | ci-coverage | ci-snapshot | ci-artifacts | ci-consumers | ci-windows-blocking | ci-windows-complete | ci-windows-observational | node-compat | check-all | hygiene | doc-sync | doc-quick, got ${JSON.stringify(raw)}.`,
  142. )
  143. }
  144. }
  145. /**
  146. * Resolve the default worker count for one aggregate.
  147. * @param selectedMode - aggregate whose resource posture applies.
  148. * @param total - number of gates in the aggregate.
  149. * @param available - host CPU availability for ordinary modes.
  150. * @returns the default worker count and its diagnostic source.
  151. */
  152. export function defaultConcurrency(
  153. selectedMode: Mode,
  154. total: number,
  155. available = availableParallelism(),
  156. ): ConcurrencyDefault {
  157. if (selectedMode === 'ci-consumers') return { workers: total, source: 'ci-consumers gate count' }
  158. // Local modes cap workers: several doc gates each build a full ts.Program,
  159. // so an uncapped default on a large host trades wall clock for memory blowups.
  160. const localCap = selectedMode === 'check-all'
  161. || selectedMode === 'hygiene'
  162. || selectedMode === 'doc-sync'
  163. || selectedMode === 'doc-quick'
  164. const modeLimit = localCap ? Math.min(4, available) : available
  165. return {
  166. workers: Math.min(total, modeLimit),
  167. source: localCap
  168. ? `${available} available CPU(s), ${selectedMode} cap 4`
  169. : `${available} available CPU(s)`,
  170. }
  171. }
  172. function concurrencyFromEnv(name: string, fallback: number): number {
  173. const raw = process.env[name]
  174. if (raw === undefined || raw === '') return fallback
  175. const parsed = Number.parseInt(raw, 10)
  176. if (!Number.isSafeInteger(parsed) || parsed < 1) {
  177. throw new Error(`run-gates: ${name} must be a positive integer, got ${JSON.stringify(raw)}.`)
  178. }
  179. return parsed
  180. }
  181. function pnpmScript(id: string, script: string, options: Partial<Gate> = {}): Gate {
  182. return {
  183. id,
  184. label: options.label ?? script,
  185. displayCommand: `pnpm run ${script}`,
  186. ...pnpmInvocation(['run', script]),
  187. ...options,
  188. }
  189. }
  190. /** Build official client artifacts inside a CI aggregate without changing sibling gate environments. */
  191. function ciBuildGate(id = 'build', options: Partial<Gate> = {}): Gate {
  192. return pnpmScript(id, 'build', {
  193. ...options,
  194. env: { ...options.env, [CLIENT_BUILD_PROFILE_SELECTOR]: 'official' },
  195. })
  196. }
  197. function pnpmExec(id: string, args: string[], options: Partial<Gate> = {}): Gate {
  198. return {
  199. id,
  200. label: options.label ?? `pnpm exec ${args.join(' ')}`,
  201. displayCommand: `pnpm exec ${args.join(' ')}`,
  202. ...pnpmInvocation(['exec', ...args]),
  203. ...options,
  204. }
  205. }
  206. /**
  207. * Construct the complete gate list for a named aggregate.
  208. * @param selected - aggregate mode to construct.
  209. * @returns the aggregate's gate graph.
  210. */
  211. export function gatesForMode(selected: Mode): Gate[] {
  212. switch (selected) {
  213. case 'ci-primary':
  214. return ciPrimaryGates()
  215. case 'ci-linux-primary':
  216. return [...ciPrimaryGates(), webSnapshotGate(['built-package-invariants'])]
  217. case 'ci-static':
  218. return ciStaticGates({ ownsBuild: false })
  219. case 'ci-lint-contracts-ready':
  220. return [
  221. lintGate(),
  222. pnpmScript('duplication', 'duplication'),
  223. ]
  224. case 'ci-coverage':
  225. return coverageGates()
  226. case 'ci-snapshot':
  227. return [ciBuildGate(), snapshotGate()]
  228. case 'ci-artifacts':
  229. return ciArtifactGates()
  230. case 'ci-consumers':
  231. return ciConsumerGates()
  232. case 'ci-windows-blocking':
  233. return ciWindowsBlockingGates()
  234. case 'ci-windows-complete':
  235. return ciWindowsCompleteGates()
  236. case 'ci-windows-observational':
  237. return ciWindowsObservationalGates()
  238. case 'node-compat':
  239. return nodeCompatGates()
  240. case 'check-all':
  241. return [
  242. pnpmScript('runtime-closure', 'verify-runtime-closure', { label: 'runtime closure' }),
  243. pnpmScript('cordis-config', 'verify-cordis-config', { label: 'Cordis config' }),
  244. pnpmScript('client-domain-graph', 'verify-client-domain-graph', { label: 'client domain graph' }),
  245. pnpmScript('test', 'test'),
  246. pnpmScript('issue-management', 'test:issue-management', { label: 'Issue management policy' }),
  247. pnpmScript('duplication', 'duplication'),
  248. snapshotGate(),
  249. expectedOutputGate(),
  250. pnpmScript('build', 'build'),
  251. pnpmScript('build:web', 'build:web'),
  252. ...hygieneLeafGates({ artifactNeeds: ['build'] }),
  253. ...docSyncLeafGates({
  254. docTypecheckNeeds: ['build'],
  255. docTypecheckEnv: { DSH_DOC_TYPECHECK_USE_BUILD_OUTPUT: '1' },
  256. docTypecheckScript: 'doc-typecheck:contracts-ready',
  257. }),
  258. pnpmScript('module-graph', 'verify-module-graph', { label: 'module graph' }),
  259. ]
  260. case 'hygiene':
  261. return [
  262. ...hygieneLeafGates(),
  263. pnpmScript('cordis-config', 'verify-cordis-config', { label: 'Cordis config' }),
  264. pnpmScript('runtime-closure', 'verify-runtime-closure', { label: 'runtime closure' }),
  265. pnpmScript('vendored-links', 'verify-vendored-links', { label: 'vendored links' }),
  266. ]
  267. case 'doc-sync':
  268. return docSyncLeafGates()
  269. case 'doc-quick':
  270. return docQuickLeafGates()
  271. }
  272. }
  273. function ciSharedStaticGates(): Gate[] {
  274. return [
  275. pnpmScript('runtime-closure', 'verify-runtime-closure', { label: 'runtime closure' }),
  276. pnpmScript('application-entrypoints', 'verify-application-entrypoints', { label: 'application entrypoints' }),
  277. pnpmScript('constraints', 'constraints'),
  278. pnpmScript('package-dependencies', 'verify-package-dependencies', { label: 'package dependencies' }),
  279. pnpmScript('dsh-package-licenses', 'verify-dsh-package-licenses', { label: 'DSH package licenses' }),
  280. pnpmScript('package-invariants', 'verify-package-invariants', { label: 'package invariants' }),
  281. pnpmScript('cordis-config', 'verify-cordis-config', { label: 'Cordis config' }),
  282. pnpmScript('optional-dependency-imports', 'verify-optional-dependency-imports', {
  283. label: 'optional dependency imports',
  284. }),
  285. pnpmScript('client-packages', 'verify-client-packages', { label: 'client packages' }),
  286. pnpmScript('client-ui-i18n', 'verify-client-ui-i18n', { label: 'client UI i18n' }),
  287. pnpmScript('issue-management', 'test:issue-management', { label: 'Issue management policy' }),
  288. ]
  289. }
  290. function ciPrimaryGates(): Gate[] {
  291. return [
  292. ...ciSharedStaticGates(),
  293. typertContractsGate(),
  294. pnpmScript('typecheck', 'typecheck:contracts-ready', { needs: ['typert-contracts'] }),
  295. lintGate({ needs: ['typert-contracts'] }),
  296. pnpmScript('duplication', 'duplication'),
  297. ...coverageGates(),
  298. ...nodeCompatSmokeGates(),
  299. snapshotGate(),
  300. ...docSyncLeafGates({
  301. docTypecheckNeeds: ['typert-contracts'],
  302. docTypecheckScript: 'doc-typecheck:contracts-ready',
  303. }),
  304. pnpmScript('module-graph', 'verify-module-graph', { label: 'module graph' }),
  305. // The prepared typecheck and build both drive Client tsc, while build also
  306. // repeats the Host contract pass. Wait for all three consumers so build
  307. // neither races tsbuildinfo nor replaces declarations while they are read.
  308. ciBuildGate('build', { needs: ['typecheck', 'lint', 'doc-typecheck'] }),
  309. pnpmScript('publint', 'publint', { needs: ['build'] }),
  310. pnpmScript('node-next-types', 'verify-node-next-types', {
  311. label: 'node-next types',
  312. needs: ['build'],
  313. }),
  314. builtPackageInvariantsGate(['build']),
  315. builtBinSmokeGate(),
  316. ]
  317. }
  318. function nodeCompatGates(): Gate[] {
  319. const typecheck = flagEnabled('DSH_NODE_COMPAT_SKIP_TYPECHECK')
  320. ? []
  321. : [pnpmScript('typecheck', 'typecheck')]
  322. if (runningNodeMajor() !== 22) {
  323. return [...typecheck, ...nodeCompatSmokeGates()]
  324. }
  325. return [
  326. ...typecheck,
  327. pnpmScript('build', 'build', {
  328. ...typecheck.length === 0 ? {} : { needs: ['typecheck'] },
  329. }),
  330. pnpmScript('build:web', 'build:web', {
  331. label: 'Web frontend build',
  332. needs: ['build'],
  333. }),
  334. ...nodeCompatSmokeGates({ cliSmoke: true }),
  335. ]
  336. }
  337. function nodeCompatSmokeGates(options: { cliSmoke?: boolean } = {}): Gate[] {
  338. const gates: Gate[] = [
  339. pnpmExec('source-worker-smoke', [
  340. 'vitest',
  341. 'run',
  342. 'packages/workflow/workflow-worker-thread/tests/source-worker.compat.spec.ts',
  343. ], { label: 'source worker smoke' }),
  344. pnpmExec('jsonl-zstd-smoke', [
  345. 'vitest',
  346. 'run',
  347. 'packages/session/session-persistence-jsonl/tests/zstd.compat.spec.ts',
  348. ], { label: 'JSONL Zstandard smoke' }),
  349. pnpmExec('dsh-source-launch-smoke', [
  350. 'vitest',
  351. 'run',
  352. 'apps/cli/tests/source-launch.compat.spec.ts',
  353. ], { label: 'dsh source-launch smoke' }),
  354. pnpmExec('vitest-jsdom-smoke', [
  355. 'vitest',
  356. 'run',
  357. 'scripts/vitest-environment.compat.spec.ts',
  358. ], { label: 'Vitest jsdom smoke' }),
  359. ]
  360. if (options.cliSmoke) {
  361. gates.push(
  362. pnpmExec('cli-lazy-search-startup-smoke', [
  363. 'vitest',
  364. 'run',
  365. 'apps/cli/tests/lazy-search-startup.compat.spec.ts',
  366. ], {
  367. label: 'CLI lazy-search startup smoke',
  368. env: { DSH_REQUIRE_BUILT_CLI_SMOKE: '1' },
  369. needs: ['build:web'],
  370. }),
  371. )
  372. }
  373. return gates
  374. }
  375. /** Active Node major used to select version-specific compatibility checks. */
  376. function runningNodeMajor(): number {
  377. const major = Number.parseInt(process.versions.node.split('.')[0] ?? '', 10)
  378. if (!Number.isSafeInteger(major)) {
  379. throw new Error(`run-gates: cannot parse Node version ${JSON.stringify(process.versions.node)}.`)
  380. }
  381. return major
  382. }
  383. function ciStaticGates(options: { ownsBuild: boolean }): Gate[] {
  384. return [
  385. ...ciSharedStaticGates(),
  386. ...options.ownsBuild ? [ciBuildGate()] : [],
  387. ...docSyncLeafGates({
  388. includeDocTypecheck: options.ownsBuild,
  389. ...options.ownsBuild
  390. ? {
  391. docTypecheckNeeds: ['build'],
  392. docTypecheckEnv: { DSH_DOC_TYPECHECK_USE_BUILD_OUTPUT: '1' },
  393. docTypecheckScript: 'doc-typecheck:contracts-ready',
  394. }
  395. : {},
  396. docsBuildScript: 'docs:build:mpa',
  397. }),
  398. pnpmScript('module-graph', 'verify-module-graph', { label: 'module graph' }),
  399. ]
  400. }
  401. function ciArtifactGates(): Gate[] {
  402. return [
  403. ciBuildGate(),
  404. pnpmScript('publint', 'publint', { needs: ['build'] }),
  405. pnpmScript('node-next-types', 'verify-node-next-types', {
  406. label: 'node-next types',
  407. needs: ['build'],
  408. }),
  409. builtPackageInvariantsGate(['build']),
  410. builtBinSmokeGate(),
  411. ]
  412. }
  413. function ciConsumerGates(): Gate[] {
  414. const builtTree = ['build']
  415. const validatedBuild = ['built-package-invariants']
  416. // The HMR web test starts `dev:web`, which rewrites the shared `lib/` and
  417. // `apps/web/dist/` trees. Let every build-artifact reader settle before that
  418. // writer starts; `after` preserves the web diagnostic even if a reader fails.
  419. const buildArtifactReaders = [
  420. 'publint',
  421. 'lint-and-duplication',
  422. 'snapshot',
  423. 'expected-output',
  424. 'doc-typecheck',
  425. 'node-next-types',
  426. 'built-bin-smoke',
  427. ]
  428. return [
  429. ciBuildGate(),
  430. pnpmScript('node-compat', 'check:node-compat', {
  431. label: 'Node compatibility',
  432. env: { [CLIENT_BUILD_PROFILE_SELECTOR]: 'official' },
  433. }),
  434. pnpmScript('publint', 'publint', { needs: builtTree }),
  435. builtPackageInvariantsGate(builtTree),
  436. pnpmScript('lint-and-duplication', 'check:ci:lint:contracts-ready', {
  437. label: 'lint and duplication',
  438. needs: validatedBuild,
  439. }),
  440. snapshotGate(validatedBuild),
  441. expectedOutputGate(validatedBuild),
  442. webSnapshotGate(validatedBuild, buildArtifactReaders),
  443. pnpmScript('doc-typecheck', 'doc-typecheck:contracts-ready', {
  444. needs: validatedBuild,
  445. env: { DSH_DOC_TYPECHECK_USE_BUILD_OUTPUT: '1' },
  446. }),
  447. pnpmScript('node-next-types', 'verify-node-next-types', {
  448. label: 'node-next types',
  449. needs: validatedBuild,
  450. }),
  451. builtBinSmokeGate(validatedBuild),
  452. ]
  453. }
  454. function webSnapshotGate(needs: string[], after?: string[]): Gate {
  455. const order = after === undefined ? { needs } : { needs, after }
  456. const workerRaw = process.env.DSH_WEB_SNAPSHOT_WORKERS
  457. if (workerRaw !== undefined && workerRaw !== '') {
  458. const workers = Number.parseInt(workerRaw, 10)
  459. if (!Number.isSafeInteger(workers) || workers < 2 || String(workers) !== workerRaw) {
  460. throw new Error(`run-gates: DSH_WEB_SNAPSHOT_WORKERS must be an integer greater than 1, got ${JSON.stringify(workerRaw)}.`)
  461. }
  462. return pnpmScript('web-snapshot', 'test:web:ci', {
  463. label: 'web browser snapshot',
  464. displayCommand: `DSH_SNAPSHOT=replay DSH_WEB_SNAPSHOT_WORKERS=${workers} pnpm run test:web:ci`,
  465. env: { DSH_SNAPSHOT: 'replay' },
  466. ...order,
  467. streamOutput: true,
  468. })
  469. }
  470. return pnpmScript('web-snapshot', 'test:web:built', {
  471. label: 'web browser snapshot',
  472. displayCommand: 'DSH_SNAPSHOT=replay pnpm run test:web:built',
  473. env: { DSH_SNAPSHOT: 'replay' },
  474. ...order,
  475. })
  476. }
  477. function ciWindowsBlockingGates(): Gate[] {
  478. return [
  479. ciBuildGate('windows-build', { label: 'build' }),
  480. pnpmScript('windows-site', 'docs:build', { label: 'production site' }),
  481. ]
  482. }
  483. function ciWindowsCompleteGates(): Gate[] {
  484. const coverage = coverageGates().map(gate => ({
  485. ...gate,
  486. needs: [...new Set(['build', ...(gate.needs ?? [])])],
  487. }))
  488. const coverageAfter = coverage.map(gate => gate.id)
  489. const observational = ciWindowsObservationalGates()
  490. // The required production site replaces the observational MPA build; both
  491. // VitePress modes write the same output directory and cannot overlap.
  492. .filter(gate => gate.id !== 'build' && gate.id !== 'docs-site-build')
  493. .map(gate => ({
  494. ...gate,
  495. allowFailure: true,
  496. after: [...new Set([
  497. ...coverageAfter,
  498. ...(gate.after ?? []).map(id => id === 'docs-site-build' ? 'windows-site' : id),
  499. ])],
  500. }))
  501. return [
  502. ciBuildGate(),
  503. pnpmScript('windows-site', 'docs:build', { label: 'production site' }),
  504. ...coverage,
  505. ...observational,
  506. ]
  507. }
  508. function ciWindowsObservationalGates(): Gate[] {
  509. const predecessors = [
  510. ...ciStaticGates({ ownsBuild: true }),
  511. // Linux owns required lint and snapshots; Windows omits those duplicates.
  512. pnpmScript('duplication', 'duplication'),
  513. pnpmScript('publint', 'publint', { needs: ['build'] }),
  514. pnpmScript('node-next-types', 'verify-node-next-types', {
  515. label: 'node-next types',
  516. needs: ['build'],
  517. }),
  518. builtPackageInvariantsGate(['build']),
  519. ]
  520. return [
  521. ...predecessors,
  522. {
  523. ...builtBinSmokeGate(),
  524. // This smoke starts real application children with bounded startup
  525. // deadlines. Let other Windows processes settle before measuring startup.
  526. after: predecessors.map(gate => gate.id),
  527. },
  528. ]
  529. }
  530. function typertContractsGate(): Gate {
  531. return pnpmScript('typert-contracts', 'build:lib:host', { label: 'Typert contracts' })
  532. }
  533. function lintGate(options: { needs?: string[] } = {}): Gate {
  534. const raw = process.env.DSH_OXLINT_THREADS
  535. const script = 'lint:contracts-ready'
  536. return pnpmScript('lint', script, {
  537. ...raw === undefined || raw === ''
  538. ? {}
  539. : { displayCommand: `DSH_OXLINT_THREADS=${raw} pnpm run ${script}` },
  540. ...options.needs === undefined ? {} : { needs: options.needs },
  541. })
  542. }
  543. // The heavy suites run uninstrumented beside the thresholded gate: their
  544. // compiler- and subprocess-bound fixtures pay a multiple of their runtime
  545. // under v8 instrumentation while contributing nothing the thresholds need
  546. // (membership rules in scripts/coverage-exempt.ts).
  547. //
  548. // DSH_COVERAGE_MAX_WORKERS is the ordinary lane's worker budget, so the two
  549. // parallel gates split it instead of each claiming it whole. When
  550. // DSH_COVERAGE_PARTITIONS is set, its single-worker processes replace the
  551. // instrumented share while this budget still sizes the exempt gate. The exempt
  552. // gate's wall clock is dominated by its longest single file, so it takes the
  553. // small share. A budget of 1 gives each gate 1 worker; lanes that need a strict
  554. // total of one (the serial reference jobs) also set DSH_GATE_CONCURRENCY=1,
  555. // which keeps the gates from overlapping at all.
  556. // DSH_COVERAGE_TEST_TIMEOUT_MS raises Vitest's per-test, expect.poll, and hook
  557. // defaults together for instrumented lanes whose scheduling overhead exceeds
  558. // those defaults. Explicit fixture timeouts remain authoritative.
  559. function coverageWorkerArgs(): { instrumented: string[]; exempt: string[] } {
  560. const [flag] = positiveIntArg('DSH_COVERAGE_MAX_WORKERS', '--maxWorkers')
  561. if (flag === undefined) return { instrumented: [], exempt: [] }
  562. const total = Number.parseInt(flag.split('=')[1] ?? '', 10)
  563. const exempt = Math.max(1, Math.floor(total / 3))
  564. const instrumented = Math.max(1, total - exempt)
  565. return {
  566. instrumented: [`--maxWorkers=${String(instrumented)}`],
  567. exempt: [`--maxWorkers=${String(exempt)}`],
  568. }
  569. }
  570. function coverageGates(): Gate[] {
  571. const workers = coverageWorkerArgs()
  572. const timeouts = coverageTestTimeoutArgs(process.env[COVERAGE_TEST_TIMEOUT_ENV])
  573. const partitions = parseCoveragePartitionCount(process.env[COVERAGE_PARTITIONS_ENV])
  574. const instrumented = partitions === undefined
  575. ? pnpmExec('coverage', [
  576. 'vitest',
  577. 'run',
  578. '--coverage',
  579. ...workers.instrumented,
  580. ...timeouts,
  581. ], {
  582. label: 'test:coverage',
  583. env: { [COVERAGE_EXEMPT_ENV]: '1' },
  584. })
  585. : pnpmScript('coverage', 'test:coverage:partitioned', {
  586. label: 'test:coverage',
  587. displayCommand: `${COVERAGE_PARTITIONS_ENV}=${partitions} pnpm run test:coverage:partitioned`,
  588. env: { [COVERAGE_EXEMPT_ENV]: '1' },
  589. streamOutput: true,
  590. })
  591. return [
  592. instrumented,
  593. pnpmExec('coverage-exempt-heavy', [
  594. 'vitest',
  595. 'run',
  596. ...coverageExemptHeavySuites.map(suite => suite.filter),
  597. ...workers.exempt,
  598. ...timeouts,
  599. ], {
  600. label: 'test:coverage-exempt-heavy',
  601. }),
  602. ]
  603. }
  604. // Recorded-session adapters boot process scenarios in `lib` mode. Callers wait
  605. // either on `build` or on a validation gate that transitively owns that build.
  606. function snapshotGate(needs: string[] = ['build']): Gate {
  607. return pnpmScript('snapshot', 'test:snapshot', {
  608. env: { DSH_EXAMPLE_MODE: 'lib' },
  609. needs,
  610. })
  611. }
  612. // Owner-local process expectations consume built package exports without entering
  613. // the recorded-session corpus or the credentialed provider lane.
  614. function expectedOutputGate(needs: string[] = ['build']): Gate {
  615. return pnpmScript('expected-output', 'test:expected', {
  616. env: { DSH_EXAMPLE_MODE: 'lib' },
  617. needs,
  618. })
  619. }
  620. function builtPackageInvariantsGate(needs?: string[]): Gate {
  621. return pnpmScript('built-package-invariants', 'verify-built-package-invariants', {
  622. label: 'built package invariants',
  623. ...needs === undefined ? {} : { needs },
  624. })
  625. }
  626. function positiveIntArg(envName: string, flag: string): string[] {
  627. const raw = process.env[envName]
  628. if (raw === undefined || raw === '') return []
  629. const parsed = Number.parseInt(raw, 10)
  630. if (!Number.isSafeInteger(parsed) || parsed < 1 || String(parsed) !== raw) {
  631. throw new Error(`run-gates: ${envName} must be a positive integer, got ${JSON.stringify(raw)}.`)
  632. }
  633. return [`${flag}=${raw}`]
  634. }
  635. function flagEnabled(envName: string): boolean {
  636. const raw = process.env[envName]
  637. if (raw === undefined || raw === '') return false
  638. if (raw !== '1') throw new Error(`run-gates: ${envName} must be 1 when set, got ${JSON.stringify(raw)}.`)
  639. return true
  640. }
  641. function hygieneLeafGates(options: { artifactNeeds?: string[] } = {}): Gate[] {
  642. const artifactOptions = options.artifactNeeds === undefined ? {} : { needs: options.artifactNeeds }
  643. return [
  644. pnpmScript('rescope-vendor', 'rescope-vendor:check', { label: 'vendor rescope' }),
  645. pnpmScript('publint', 'publint', artifactOptions),
  646. pnpmScript('constraints', 'constraints'),
  647. pnpmScript('package-dependencies', 'verify-package-dependencies', { label: 'package dependencies' }),
  648. pnpmScript('application-entrypoints', 'verify-application-entrypoints', { label: 'application entrypoints' }),
  649. pnpmScript('dsh-package-licenses', 'verify-dsh-package-licenses', { label: 'DSH package licenses' }),
  650. pnpmScript('package-invariants', 'verify-package-invariants', { label: 'package invariants' }),
  651. builtPackageInvariantsGate(options.artifactNeeds),
  652. pnpmScript('node-next-types', 'verify-node-next-types', {
  653. label: 'node-next types',
  654. ...artifactOptions,
  655. }),
  656. pnpmScript('optional-dependency-imports', 'verify-optional-dependency-imports', {
  657. label: 'optional dependency imports',
  658. }),
  659. pnpmScript('client-packages', 'verify-client-packages', { label: 'client packages' }),
  660. pnpmScript('client-ui-i18n', 'verify-client-ui-i18n', { label: 'client UI i18n' }),
  661. ]
  662. }
  663. function docSyncLeafGates(options: {
  664. includeDocTypecheck?: boolean
  665. docTypecheckNeeds?: string[]
  666. docTypecheckEnv?: Record<string, string | undefined>
  667. docTypecheckScript?: 'doc-typecheck' | 'doc-typecheck:contracts-ready'
  668. docsBuildScript?: 'docs:build' | 'docs:build:mpa'
  669. } = {}): Gate[] {
  670. const docTypecheckOptions: Partial<Gate> = {}
  671. if (options.docTypecheckNeeds !== undefined) docTypecheckOptions.needs = options.docTypecheckNeeds
  672. if (options.docTypecheckEnv !== undefined) docTypecheckOptions.env = options.docTypecheckEnv
  673. return [
  674. // Stable FIFO starts the longest leaves first; only docs-site-build writes website/.generated.
  675. ...options.includeDocTypecheck === false
  676. ? []
  677. : [pnpmScript('doc-typecheck', options.docTypecheckScript ?? 'doc-typecheck', docTypecheckOptions)],
  678. pnpmScript('docs-site-build', options.docsBuildScript ?? 'docs:build', { label: 'documentation build' }),
  679. pnpmScript('doc-graphs', 'verify-doc-graphs', { label: 'doc graphs' }),
  680. pnpmScript('markdown-links', 'verify-md-links', { label: 'markdown links', quick: true }),
  681. pnpmScript('type-equivalence', 'verify-type-equiv', { label: 'type equivalence', quick: true }),
  682. pnpmScript('cordis-catalog', 'verify-cordis-catalog', { label: 'cordis catalog' }),
  683. pnpmScript('cordis-inspect-catalog', 'verify-cordis-inspect-catalog', { label: 'Cordis inspect catalog' }),
  684. pnpmScript('mermaid', 'verify-mermaid'),
  685. pnpmScript('scoped-events', 'verify-scoped-events', { label: 'scoped events' }),
  686. pnpmScript('translation-pairing', 'verify-translation-pairing', { label: 'translation pairing', quick: true }),
  687. pnpmScript('markdown-wrap', 'verify-md-wrap', { label: 'markdown wrap', quick: true }),
  688. pnpmScript('client-catalog', 'verify-client-catalog', { label: 'client catalog' }),
  689. pnpmScript('export-jsdoc', 'verify-export-jsdoc', { label: 'export jsdoc' }),
  690. pnpmScript('tool-catalog', 'verify-tool-catalog', { label: 'tool catalog' }),
  691. pnpmScript('config-catalog', 'verify-config-catalog', { label: 'config catalog' }),
  692. pnpmScript('persistence-catalog', 'verify-persistence-catalog', { label: 'persistence catalog' }),
  693. pnpmScript('public-repository-links', 'verify-public-repository-links', { label: 'public repository links', quick: true }),
  694. pnpmScript('doc-refs', 'verify-doc-refs', { label: 'doc refs', quick: true }),
  695. pnpmScript('subsystem-pages', 'verify-subsystem-pages', { label: 'subsystem pages' }),
  696. pnpmScript('package-paths', 'verify-package-paths', { label: 'package paths' }),
  697. pnpmScript('tsconfig-paths', 'verify-tsconfig-paths', { label: 'tsconfig paths' }),
  698. pnpmScript('config-source-ownership', 'verify-config-source-ownership', { label: 'config source ownership' }),
  699. pnpmScript('package-readme-model-experience', 'verify-package-readme-model-experience', { label: 'package README model experience', quick: true }),
  700. pnpmScript('agent-note-classification', 'verify-agent-note-classification', { label: 'agent note classification', quick: true }),
  701. pnpmScript('agent-note-format', 'verify-agent-note-format', { label: 'agent note format', quick: true }),
  702. pnpmScript('archived-agent-notes', 'verify-archived-agent-notes', { label: 'archived agent notes', quick: true }),
  703. pnpmScript('skill-invocation-metadata', 'verify-skill-invocation-metadata', { label: 'skill invocation metadata', quick: true }),
  704. pnpmScript('translation-prompt', 'verify-translation-prompt', { label: 'translation prompt', quick: true }),
  705. pnpmScript('doc-budgets', 'verify-doc-budgets', { label: 'doc budgets', quick: true }),
  706. pnpmExec('doc-standard-tests', ['vitest', 'run', 'scripts/doc-standard.spec.ts'], {
  707. label: 'documentation standard tests',
  708. quick: true,
  709. }),
  710. pnpmExec('docs-site-projection', ['vitest', 'run', 'scripts/project-doc-site.spec.ts', 'scripts/verify-doc-site-fragments.spec.ts'], {
  711. label: 'documentation site checks',
  712. }),
  713. pnpmScript('package-readme-limitations', 'verify-package-readme-limitations', { label: 'package README limitations', quick: true }),
  714. ]
  715. }
  716. /**
  717. * The quick comprehensive documentation-standard aggregate for `test:docs`.
  718. * It covers the prose, pairing, README, budget, and Agent Note gates
  719. * without builds, generator regeneration, or the VitePress site build.
  720. */
  721. function docQuickLeafGates(): Gate[] {
  722. return docSyncLeafGates({ includeDocTypecheck: false }).filter(gate => gate.quick === true)
  723. }
  724. function builtBinSmokeGate(needs: string[] = ['build']): Gate {
  725. return pnpmExec('built-bin-smoke', [
  726. 'vitest',
  727. 'run',
  728. '--config',
  729. 'vitest.e2e.config.ts',
  730. 'apps/cli/tests/profiles/headless/tests/keyless-smoke.e2e.ts',
  731. 'apps/cli/tests/built-bin.e2e.ts',
  732. 'packages/host/directory-picker-native/tests/built-worker.e2e.ts',
  733. 'packages/sdk/server/tests/built-scope-carrier.e2e.ts',
  734. 'packages/subagent/subagent-codex/tests/loader-composition.e2e.ts',
  735. 'packages/subagent/subagent-claude-code/tests/loader-composition.e2e.ts',
  736. 'packages/api/remotes/tests/built-lib.e2e.ts',
  737. 'packages/experimental/agent-team/tests/built-lib.e2e.ts',
  738. // Built execution consumers: the only automated proof that package-name
  739. // imports reach their lib/ entrypoints under plain Node. The e2e lane runs
  740. // unbuilt, so these files self-skip there.
  741. 'packages/workflow/workflow-worker-thread/tests/built-worker.e2e.ts',
  742. 'packages/code-runtime/code-runtime-worker-thread/tests/built-lib.e2e.ts',
  743. 'packages/lsp/lsp-stdio/tests/built-lib.e2e.ts',
  744. ], {
  745. label: 'built-bin smoke',
  746. needs,
  747. env: { DSH_EXAMPLE_MODE: 'lib' },
  748. })
  749. }
  750. /**
  751. * Reject a gate list whose graph cannot be executed unambiguously.
  752. * @param gates - complete aggregate to validate.
  753. */
  754. function validateGateGraph(gates: readonly Gate[]): void {
  755. if (gates.length === 0) throw new Error('run-gates: gate graph has no gates.')
  756. const ids = new Set<string>()
  757. for (const gate of gates) {
  758. if (ids.has(gate.id)) throw new Error(`run-gates: duplicate gate id ${JSON.stringify(gate.id)}.`)
  759. ids.add(gate.id)
  760. }
  761. for (const gate of gates) {
  762. for (const dependency of gate.needs ?? []) {
  763. if (!ids.has(dependency)) {
  764. throw new Error(`run-gates: gate ${JSON.stringify(gate.id)} depends on unknown gate ${JSON.stringify(dependency)}.`)
  765. }
  766. }
  767. for (const predecessor of gate.after ?? []) {
  768. if (!ids.has(predecessor)) {
  769. throw new Error(`run-gates: gate ${JSON.stringify(gate.id)} waits for unknown gate ${JSON.stringify(predecessor)}.`)
  770. }
  771. }
  772. }
  773. const cycle = findDependencyCycle(gates)
  774. if (cycle !== undefined) throw new Error(`run-gates: dependency cycle: ${cycle.join(' -> ')}.`)
  775. }
  776. function findDependencyCycle(gates: readonly Gate[]): string[] | undefined {
  777. const byId = new Map(gates.map(gate => [gate.id, gate]))
  778. const complete = new Set<string>()
  779. const active = new Map<string, number>()
  780. const path: string[] = []
  781. const visit = (id: string): string[] | undefined => {
  782. if (complete.has(id)) return undefined
  783. const cycleStart = active.get(id)
  784. if (cycleStart !== undefined) return [...path.slice(cycleStart), id]
  785. const gate = byId.get(id)
  786. if (gate === undefined) return undefined
  787. active.set(id, path.length)
  788. path.push(id)
  789. for (const predecessor of [...(gate.needs ?? []), ...(gate.after ?? [])]) {
  790. const cycle = visit(predecessor)
  791. if (cycle !== undefined) return cycle
  792. }
  793. path.pop()
  794. active.delete(id)
  795. complete.add(id)
  796. return undefined
  797. }
  798. for (const gate of gates) {
  799. const cycle = visit(gate.id)
  800. if (cycle !== undefined) return cycle
  801. }
  802. return undefined
  803. }
  804. /**
  805. * Scheduling options for one aggregate.
  806. */
  807. export interface RunGatesOptions {
  808. /** Stop the aggregate at the first blocking gate failure. */
  809. failFast?: boolean
  810. /** Forward host SIGINT/SIGTERM to the abort path so detached gate trees are
  811. * terminated when the run itself is interrupted or the runner cancels it.
  812. * Tree termination additionally requires failFast, because only then is the
  813. * abort signal passed to the executor and children detached. */
  814. forwardProcessSignals?: boolean
  815. }
  816. /**
  817. * Validate and run one aggregate before the injected executor can start a child.
  818. * @param gates - complete aggregate to execute.
  819. * @param maxActive - maximum concurrent child count.
  820. * @param execute - child-process executor; receives the abort signal only when
  821. * fail-fast is enabled, so ordinary runs keep their children in the host
  822. * process group.
  823. * @param observe - result observer invoked when each gate settles.
  824. * @param options - scheduling options; fail-fast aborts the aggregate at the
  825. * first blocking gate failure by killing running children and skipping every
  826. * not-yet-run gate.
  827. * @returns results in aggregate order.
  828. */
  829. export async function runGates(
  830. gates: Gate[],
  831. maxActive: number,
  832. execute: GateExecutor,
  833. observe: ResultObserver = () => {},
  834. options: RunGatesOptions = {},
  835. ): Promise<GateResult[]> {
  836. validateGateGraph(gates)
  837. if (!Number.isSafeInteger(maxActive) || maxActive < 1) {
  838. throw new Error(`run-gates: max concurrency must be a positive integer, got ${JSON.stringify(maxActive)}.`)
  839. }
  840. if (options.forwardProcessSignals === true && options.failFast !== true) {
  841. throw new Error('run-gates: forwardProcessSignals requires failFast, otherwise no child is detached or killed.')
  842. }
  843. const states = new Map<string, GateState>(gates.map(gate => [gate.id, 'pending']))
  844. const results = new Map<string, GateResult>()
  845. const running: RunningGate[] = []
  846. const abort = new AbortController()
  847. let abortCause: string | undefined
  848. // Host interruption (terminal Ctrl+C, runner cancellation) drains through
  849. // the same abort path as a gate failure, so detached trees are killed and
  850. // never orphaned. Handlers are removed before returning.
  851. const hostSignals = options.forwardProcessSignals === true ? ['SIGINT', 'SIGTERM'] as const : []
  852. const hostHandlers = hostSignals.map((name) => {
  853. const handler = () => {
  854. abortCause = abortCause ?? 'host interruption'
  855. abort.abort()
  856. }
  857. process.on(name, handler)
  858. return { name, handler }
  859. })
  860. const failFastSignal = options.failFast === true ? abort.signal : undefined
  861. try {
  862. for (;;) {
  863. let madeProgress = false
  864. if (abortCause === undefined) {
  865. while (running.length < maxActive) {
  866. const ready = gates.find(gate => states.get(gate.id) === 'pending' && predecessorsReady(gate, states))
  867. if (ready === undefined) break
  868. states.set(ready.id, 'running')
  869. running.push({ gate: ready, promise: execute(ready, failFastSignal) })
  870. console.log(`run-gates: start ${ready.label}`)
  871. madeProgress = true
  872. }
  873. }
  874. if (running.length === 0) {
  875. if (abortCause !== undefined) {
  876. for (const gate of gates) {
  877. if (states.get(gate.id) !== 'pending') continue
  878. const skipped = skippedByFailFast(gate, abortCause)
  879. states.set(gate.id, 'skipped')
  880. results.set(gate.id, skipped)
  881. observe(skipped)
  882. }
  883. break
  884. }
  885. const pending = gates.filter(gate => states.get(gate.id) === 'pending')
  886. if (pending.length === 0) break
  887. const gate = pending.find(item => (item.needs ?? []).some(id => gateFailed(states.get(id))))
  888. if (gate === undefined) throw new Error('run-gates: validated graph stalled without a failed dependency.')
  889. const failedDeps = (gate.needs ?? []).filter(id => gateFailed(states.get(id)))
  890. const result: GateResult = {
  891. gate,
  892. status: 'skipped',
  893. durationMs: 0,
  894. output: [],
  895. exitCode: null,
  896. signalCode: null,
  897. error: `dependency failed or skipped: ${failedDeps.join(', ')}`,
  898. }
  899. states.set(gate.id, 'skipped')
  900. results.set(gate.id, result)
  901. observe(result)
  902. continue
  903. }
  904. if (!madeProgress) {
  905. const settled = await Promise.race(running.map(async item => ({ item, result: await item.promise })))
  906. running.splice(running.indexOf(settled.item), 1)
  907. const observed = abortCause === undefined || settled.result.aborted !== true
  908. ? settled.result
  909. : skippedByFailFast(settled.item.gate, abortCause)
  910. states.set(settled.item.gate.id, observed.status)
  911. results.set(settled.item.gate.id, observed)
  912. observe(observed)
  913. if (abortCause === undefined && options.failFast === true
  914. && observed.status === 'failed' && settled.item.gate.allowFailure !== true) {
  915. abortCause = `${observed.gate.label} failed`
  916. abort.abort()
  917. console.error(`run-gates: fail-fast aborting: ${abortCause}.`)
  918. for (const gate of gates) {
  919. if (states.get(gate.id) !== 'pending') continue
  920. const skipped = skippedByFailFast(gate, abortCause)
  921. states.set(gate.id, 'skipped')
  922. results.set(gate.id, skipped)
  923. observe(skipped)
  924. }
  925. }
  926. }
  927. }
  928. } finally {
  929. for (const { name, handler } of hostHandlers) process.removeListener(name, handler)
  930. }
  931. return gates.map((gate) => {
  932. const result = results.get(gate.id)
  933. if (result === undefined) throw new Error(`run-gates: missing result for ${gate.id}.`)
  934. return result
  935. })
  936. }
  937. /**
  938. * The result of a gate that produced no evidence because fail-fast aborted.
  939. * A gate whose process settled before the abort took effect keeps its real
  940. * result instead: it did produce evidence, and the summary must say so. Any
  941. * result settling after the abort — including a genuine independent failure
  942. * in the race window, and a child that trapped the signal and exited zero —
  943. * is recorded skipped with its partial output discarded, because on Windows a
  944. * killed process is indistinguishable from a failed one by exit code alone.
  945. * @param gate - the gate that produced no evidence.
  946. * @param cause - the full clause naming what aborted the aggregate, e.g.
  947. * `typecheck failed` or `host interruption`.
  948. * @returns the skipped record with the fail-fast error.
  949. */
  950. function skippedByFailFast(gate: Gate, cause: string): GateResult {
  951. return {
  952. gate,
  953. status: 'skipped',
  954. durationMs: 0,
  955. output: [],
  956. exitCode: null,
  957. signalCode: null,
  958. error: `aborted by fail-fast: ${cause}`,
  959. }
  960. }
  961. function predecessorsReady(gate: Gate, states: Map<string, GateState>): boolean {
  962. return (gate.needs ?? []).every(id => states.get(id) === 'passed')
  963. && (gate.after ?? []).every(id => gateSettled(states.get(id)))
  964. }
  965. function gateSettled(state: GateState | undefined): boolean {
  966. return state === 'passed' || state === 'failed' || state === 'skipped'
  967. }
  968. function gateFailed(state: GateState | undefined): boolean {
  969. return state === 'failed' || state === 'skipped'
  970. }
  971. /**
  972. * Execute one gate through the real shell-free child-process boundary.
  973. * @param gate - command and scheduler environment to execute.
  974. * @param signal - abort signal that terminates the whole gate process tree when
  975. * the aggregate fails fast; an already-aborted signal terminates it
  976. * immediately. A provided signal spawns the child detached so POSIX can signal
  977. * its process group and Windows can reach its tree through taskkill.
  978. * @returns the complete process outcome.
  979. */
  980. export async function runGate(gate: Gate, signal?: AbortSignal): Promise<GateResult> {
  981. const started = performance.now()
  982. const output: GateOutputChunk[] = []
  983. let spawnError: string | undefined
  984. let aborted = false
  985. const outcome = await new Promise<{
  986. exitCode: number | null
  987. signalCode: NodeJS.Signals | null
  988. }>((resolveExit) => {
  989. const child = spawn(gate.command, gate.args, {
  990. cwd: root,
  991. env: { ...process.env, ...gate.env },
  992. stdio: ['pipe', 'pipe', 'pipe'],
  993. detached: signal !== undefined && process.platform !== 'win32',
  994. })
  995. child.stdout.setEncoding('utf8')
  996. child.stderr.setEncoding('utf8')
  997. child.stdout.on('data', (chunk: string) => {
  998. if (gate.streamOutput === true) process.stdout.write(chunk)
  999. else output.push({ stream: 'stdout', text: chunk })
  1000. })
  1001. child.stderr.on('data', (chunk: string) => {
  1002. if (gate.streamOutput === true) process.stderr.write(chunk)
  1003. else output.push({ stream: 'stderr', text: chunk })
  1004. })
  1005. // Deliver one signal to the entire gate tree: the negative pid targets the
  1006. // POSIX process group the detached child leads; Windows has no groups, so
  1007. // taskkill walks the tree rooted at the child and force-terminates (a
  1008. // taskkill without `/F` does not terminate console processes, which is
  1009. // what gate commands are). Outcomes are deliberately unchecked because
  1010. // delivery races tree exit, and a missing taskkill binary is as tolerable
  1011. // as ESRCH. Mirrors the subprocess package's teardown contract
  1012. // (packages/subprocess/subprocess-local/src/spawn.ts).
  1013. const treeKill = (signalToSend: 'SIGTERM' | 'SIGKILL') => {
  1014. const pid = child.pid
  1015. if (pid === undefined) return
  1016. if (process.platform === 'win32') {
  1017. for (const args of taskkillArgs(pid, descendants)) {
  1018. spawnSync('taskkill', args, { stdio: 'ignore' })
  1019. }
  1020. return
  1021. }
  1022. try {
  1023. process.kill(-pid, signalToSend)
  1024. } catch {
  1025. // The group is gone; the direct child may still be alive alone.
  1026. child.kill(signalToSend)
  1027. }
  1028. // The captured list stays valid after the group kill reparents the
  1029. // detached descendants of a nested run-gates (the `check:node-compat`
  1030. // and `check:ci:lint:contracts-ready` gates in ci-consumers): pids do
  1031. // not change on reparenting, so the escalation reaches leaves that
  1032. // ignored SIGTERM without re-enumerating.
  1033. for (const descendantPid of descendants) {
  1034. try {
  1035. process.kill(descendantPid, signalToSend)
  1036. } catch {
  1037. // The descendant exited between the enumeration and the signal.
  1038. }
  1039. }
  1040. }
  1041. let escalation: ReturnType<typeof setTimeout> | undefined
  1042. let terminatedAt = 0
  1043. // Captured once at terminate and re-signalled on escalation: the group
  1044. // kill reaps the direct child, after which its detached descendants are
  1045. // reparented and unreachable by parent id, so the escalation cannot
  1046. // re-enumerate them.
  1047. let descendants: number[] = []
  1048. let pipeDrain: ReturnType<typeof setTimeout> | undefined
  1049. const terminate = () => {
  1050. aborted = true
  1051. const pid = child.pid
  1052. // Merge while the child is still alive: re-enumerating alone would drop
  1053. // a descendant that an exited intermediate reparented out of the parent
  1054. // chain, and replacing the list entirely would lose the sampler's
  1055. // last-known entries when the child already exited. Union preserves both.
  1056. // The sampler runs on every platform (including Windows, where an
  1057. // exited intermediate's table record vanishes and a fresh enumeration
  1058. // cannot cross the gap), so the cache is the source of truth once the
  1059. // child is gone.
  1060. if (pid !== undefined && child.exitCode === null && child.signalCode === null) {
  1061. descendants = [...new Set([...descendants, ...descendantPids(pid)])]
  1062. }
  1063. treeKill('SIGTERM')
  1064. if (escalation === undefined) {
  1065. terminatedAt = Date.now()
  1066. // Force-kill at the deadline regardless of the direct child's exit
  1067. // state: when the wrapper dies but a grandchild ignores SIGTERM and
  1068. // still holds the stdio pipes, `close` has not fired and the tree must
  1069. // still be killed. treeKill swallows an already-absent group.
  1070. escalation = setTimeout(() => { treeKill('SIGKILL') }, 5000)
  1071. }
  1072. if (pipeDrain === undefined) {
  1073. // `close` can stay pending past the direct child's exit when a
  1074. // descendant holds the stdio write ends (escaped process group, or
  1075. // uninterruptible I/O that keeps the SIGKILL pending). Bound the wait
  1076. // past the 5-second SIGKILL grace and force the streams closed so
  1077. // fail-fast settles instead of hanging to the job timeout. Only the
  1078. // abort path arms it: on an ordinary run a gate that outlives its
  1079. // descendants must keep waiting rather than report passed over a live
  1080. // leak. Armed in terminate (not only at `exit`) so the window where
  1081. // the child already exited before the abort is covered too.
  1082. pipeDrain = setTimeout(() => {
  1083. child.stdout.destroy()
  1084. child.stderr.destroy()
  1085. child.stdin.destroy()
  1086. }, 10000)
  1087. }
  1088. }
  1089. if (signal !== undefined) {
  1090. if (signal.aborted) terminate()
  1091. else signal.addEventListener('abort', terminate, { once: true })
  1092. }
  1093. // Refresh the descendant cache while the child runs, so an abort that
  1094. // arrives after the child already exited can still reach a detached
  1095. // descendant the child left behind: once the child is gone, its
  1096. // descendants are reparented (POSIX) or their intermediate's table record
  1097. // is gone (Windows), so a fresh enumeration cannot cross the gap. The
  1098. // cache is primed at spawn and refreshed every 5 seconds, so a descendant
  1099. // is captured once it appears in any enumeration whose parent chain is
  1100. // still fully present in the table; the residual window is a descendant
  1101. // that never appears in such a snapshot — created after one enumeration
  1102. // and orphaned before the next. Enumeration is asynchronous (a slow
  1103. // WMI/CIM call is bounded by its own 10-second timeout), so a gate's
  1104. // output draining and exit handling are never blocked while the sampler
  1105. // reads the process table. Fail-fast runs only; ordinary runs never
  1106. // abort.
  1107. let descendantSampler: ReturnType<typeof setInterval> | undefined
  1108. if (signal !== undefined) {
  1109. let enumerationInFlight: { cancel: () => void } | undefined
  1110. const refreshDescendants = () => {
  1111. const pid = child.pid
  1112. if (pid === undefined || child.exitCode !== null || child.signalCode !== null) return
  1113. if (enumerationInFlight !== undefined) return
  1114. const handle = descendantPidsAsync(pid, process.platform)
  1115. enumerationInFlight = handle
  1116. void handle.promise.then((fresh) => {
  1117. if (enumerationInFlight === handle) enumerationInFlight = undefined
  1118. // Merge regardless of the child's exit state: the enumeration
  1119. // started while the child was alive, so its snapshot is the last
  1120. // reliable view of the tree. The child may exit (its intermediate
  1121. // gone, its table record vanished) before the promise settles while
  1122. // a grandchild still holds the stdio write ends and keeps `close`
  1123. // pending — exactly when terminate needs this list.
  1124. // Merge instead of replacing, like terminate: an intermediate that
  1125. // exited since the last tick reparented its detached descendants
  1126. // out of the parent chain, so a fresh enumeration alone would drop
  1127. // them. Filter the cache to the still-executing so a long gate
  1128. // does not accumulate stale pids; while sampler ticks still run the
  1129. // live filter also keeps the escalation from signalling a reused
  1130. // pid, but once ticks stop (child exited) the cache can go stale,
  1131. // and a pid reused after that is the accepted sampling window.
  1132. descendants = [...new Set([...descendants.filter(processAlive), ...fresh])]
  1133. })
  1134. }
  1135. const cancelInFlightEnumeration = () => {
  1136. if (enumerationInFlight !== undefined) enumerationInFlight.cancel()
  1137. enumerationInFlight = undefined
  1138. }
  1139. refreshDescendants()
  1140. descendantSampler = setInterval(refreshDescendants, 5000)
  1141. // A gate that settles while an enumeration is still running must not
  1142. // leave the PowerShell subprocess holding stdio handles until its own
  1143. // timeout: stop it as soon as the child's outcome is known.
  1144. child.once('close', cancelInFlightEnumeration)
  1145. child.once('error', cancelInFlightEnumeration)
  1146. }
  1147. child.on('error', (error) => {
  1148. if (escalation !== undefined) clearTimeout(escalation)
  1149. if (pipeDrain !== undefined) clearTimeout(pipeDrain)
  1150. if (descendantSampler !== undefined) clearInterval(descendantSampler)
  1151. if (signal !== undefined) signal.removeEventListener('abort', terminate)
  1152. spawnError = `failed to start command: ${error.message}`
  1153. resolveExit({ exitCode: null, signalCode: null })
  1154. })
  1155. child.on('close', (exitCode, signalCode) => {
  1156. if (pipeDrain !== undefined) clearTimeout(pipeDrain)
  1157. if (descendantSampler !== undefined) clearInterval(descendantSampler)
  1158. if (signal !== undefined) signal.removeEventListener('abort', terminate)
  1159. if (escalation !== undefined && process.platform !== 'win32') {
  1160. // `close` only means the direct child's stdio closed; a grandchild
  1161. // that ignored SIGTERM and redirected its stdio can outlive it. Do
  1162. // not settle until the process group and the captured descendants are
  1163. // confirmed gone — the deadline SIGKILL covers members still alive at
  1164. // the grace end — so runGate returns only once the tree is quiescent.
  1165. const confirmGroupGone = () => {
  1166. if (!groupAlive(child.pid) && descendants.every(descendantPid => !processAlive(descendantPid))) {
  1167. clearTimeout(escalation)
  1168. resolveExit({ exitCode, signalCode })
  1169. return
  1170. }
  1171. if (Date.now() - terminatedAt < 8000) {
  1172. setTimeout(confirmGroupGone, 50)
  1173. return
  1174. }
  1175. // The grace ended with members still alive (e.g. uninterruptible
  1176. // I/O that even SIGKILL cannot cut). Fail loud instead of reporting
  1177. // a quiescent tree: the gate is recorded failed either way.
  1178. console.error(`run-gates: gate tree not quiescent after 8s (${gate.label}).`)
  1179. clearTimeout(escalation)
  1180. resolveExit({ exitCode, signalCode })
  1181. }
  1182. confirmGroupGone()
  1183. return
  1184. }
  1185. if (escalation !== undefined) clearTimeout(escalation)
  1186. resolveExit({ exitCode, signalCode })
  1187. })
  1188. child.stdin.end()
  1189. })
  1190. const { exitCode, signalCode } = outcome
  1191. const status: GateResultStatus = exitCode === 0 && signalCode === null && spawnError === undefined ? 'passed' : 'failed'
  1192. const result: GateResult = {
  1193. gate,
  1194. status,
  1195. durationMs: performance.now() - started,
  1196. output,
  1197. exitCode,
  1198. signalCode,
  1199. }
  1200. result.aborted = aborted
  1201. if (spawnError !== undefined) result.error = spawnError
  1202. return result
  1203. }
  1204. /**
  1205. * Parse the state, parent, and process-group fields from a `/proc/<pid>/stat`
  1206. * line. The comm field may contain spaces and parentheses, so the state starts
  1207. * after the last closing parenthesis.
  1208. * @param stat - one `/proc/<pid>/stat` line.
  1209. * @returns state, parent pid, and process-group pid; undefined when truncated.
  1210. */
  1211. function procStatFields(stat: string): { state: string; ppid: number; pgrp: number } | undefined {
  1212. const fields = stat.slice(stat.lastIndexOf(')') + 2).split(' ')
  1213. const state = fields[0]
  1214. const ppid = fields[1]
  1215. const pgrp = fields[2]
  1216. if (state === undefined || ppid === undefined || pgrp === undefined) return undefined
  1217. return { state, ppid: Number(ppid), pgrp: Number(pgrp) }
  1218. }
  1219. /**
  1220. * Whether one process is still executing. Zombies (state `Z`) do not count:
  1221. * they are dead records awaiting reaping, and kill(pid, 0) would report them
  1222. * as alive. Linux reads /proc/<pid>/stat to distinguish; other platforms fall
  1223. * back to the signal probe.
  1224. * @param pid - the process to probe.
  1225. */
  1226. function processAlive(pid: number): boolean {
  1227. if (process.platform === 'linux') {
  1228. try {
  1229. const parsed = procStatFields(readFileSync(`/proc/${pid}/stat`, 'utf8'))
  1230. return parsed !== undefined && parsed.state !== 'Z'
  1231. } catch {
  1232. return false
  1233. }
  1234. }
  1235. try {
  1236. process.kill(pid, 0)
  1237. return true
  1238. } catch {
  1239. return false
  1240. }
  1241. }
  1242. /**
  1243. * Whether any member of the child's POSIX process group is still executing.
  1244. * Zombie entries (state `Z`) do not count: they are dead records awaiting
  1245. * reaping, and the kill(-pid, 0) group probe would report them as alive.
  1246. * Linux enumerates /proc to distinguish after a fast-path group probe; other
  1247. * POSIX platforms fall back to the probe alone.
  1248. * @param pid - the group leader's pid; undefined or non-positive means the
  1249. * spawn failed and nothing is alive.
  1250. */
  1251. function groupAlive(pid: number | undefined): boolean {
  1252. if (pid === undefined || pid <= 0) return false
  1253. if (process.platform === 'linux') {
  1254. try {
  1255. process.kill(-pid, 0)
  1256. } catch {
  1257. // ESRCH: the group has no entries at all.
  1258. return false
  1259. }
  1260. try {
  1261. for (const entry of readdirSync('/proc')) {
  1262. if (!/^\d+$/.test(entry)) continue
  1263. try {
  1264. const parsed = procStatFields(readFileSync(`/proc/${entry}/stat`, 'utf8'))
  1265. if (parsed !== undefined && parsed.pgrp === pid && parsed.state !== 'Z') return true
  1266. } catch {
  1267. // The process exited mid-scan; it is not a live member.
  1268. }
  1269. }
  1270. return false
  1271. } catch {
  1272. return false
  1273. }
  1274. }
  1275. try {
  1276. process.kill(-pid, 0)
  1277. return true
  1278. } catch {
  1279. return false
  1280. }
  1281. }
  1282. /**
  1283. * The pids of every transitive descendant of `root`, read from the live
  1284. * process table. Linux walks /proc/<pid>/stat parent fields; other platforms
  1285. * parse `ps` (POSIX) or the CIM process table (Windows) output. This is one
  1286. * snapshot, not the full tree-ownership mechanism: terminate and the sampler
  1287. * rely on the 5-second cache to cross an intermediate that exited between
  1288. * ticks (reparented on POSIX, table record gone on Windows), so a single
  1289. * enumeration reaches only the descendants whose parent chain is still fully
  1290. * present in the table.
  1291. * @param root - the pid whose descendants are wanted.
  1292. * @returns descendant pids in breadth-first order; empty on enumeration failure.
  1293. */
  1294. function descendantPids(root: number): number[] {
  1295. if (root <= 0) return []
  1296. if (process.platform === 'linux') {
  1297. const rows: Array<[number, number]> = []
  1298. try {
  1299. for (const entry of readdirSync('/proc')) {
  1300. if (!/^\d+$/.test(entry)) continue
  1301. try {
  1302. const parsed = procStatFields(readFileSync(`/proc/${entry}/stat`, 'utf8'))
  1303. if (parsed !== undefined) rows.push([Number(entry), parsed.ppid])
  1304. } catch {
  1305. // The process exited mid-scan; skip it.
  1306. }
  1307. }
  1308. } catch {
  1309. return []
  1310. }
  1311. return collectDescendants(root, rows)
  1312. }
  1313. let ps: { error?: Error; stdout: string }
  1314. if (process.platform === 'win32') {
  1315. // taskkill /T covers the tree only while the root is alive; once the
  1316. // direct child exits (a descendant still holding the stdio write ends
  1317. // keeps `close` pending), abort must reach the survivors from a fresh
  1318. // enumeration. Windows keeps the exited parent's pid in its descendants'
  1319. // parent column, so this walk still finds the whole tree. A hung
  1320. // PowerShell (WMI/CIM service trouble) must not stall the abort path
  1321. // indefinitely, so the enumeration is bounded.
  1322. ps = spawnSync('powershell', processTableArgs('win32'), { encoding: 'utf8', timeout: 10000 })
  1323. } else {
  1324. ps = spawnSync('ps', processTableArgs('posix'), { encoding: 'utf8' })
  1325. }
  1326. if (ps.error !== undefined) return []
  1327. return collectDescendants(root, parsePidPpidLines(ps.stdout))
  1328. }
  1329. /**
  1330. * The process-table enumeration command for one platform. Windows queries the
  1331. * CIM provider through PowerShell (each line `pid ppid`); other platforms use
  1332. * `ps -axo pid=,ppid=`.
  1333. * @param platform - the target platform.
  1334. * @returns the command arguments to enumerate every live process's pid/ppid.
  1335. */
  1336. function processTableArgs(platform: 'win32' | 'posix'): string[] {
  1337. if (platform === 'win32') {
  1338. return ['-NoProfile', '-NonInteractive', '-Command', 'Get-CimInstance Win32_Process | ForEach-Object { "$($_.ProcessId) $($_.ParentProcessId)" }']
  1339. }
  1340. return ['-axo', 'pid=,ppid=']
  1341. }
  1342. /**
  1343. * Asynchronous descendant enumeration, so a slow WMI/CIM call (bounded by a
  1344. * 10-second timeout) cannot block the event loop: the sampler runs it while
  1345. * the gate's output streams and exit handling must keep flowing. Returns the
  1346. * same descendant list as {@link descendantPids}; used by the fail-fast
  1347. * sampler only, never on the abort path (which needs the synchronous walk to
  1348. * capture the tree before any member exits).
  1349. * @param root - the pid whose descendants are wanted.
  1350. * @param platform - the platform whose table the enumeration reads.
  1351. * @returns a promise of descendant pids in breadth-first order; empty on
  1352. * enumeration failure.
  1353. */
  1354. function descendantPidsAsync(root: number, platform: NodeJS.Platform): { promise: Promise<number[]>; cancel: () => void } {
  1355. if (root <= 0 || platform === 'linux') {
  1356. // The /proc walk is synchronous inside the async wrapper so the sampler
  1357. // keeps the same contract on every platform; /proc reads are fast and
  1358. // need no subprocess, and a completed enumeration needs no cancellation.
  1359. return { promise: Promise.resolve(descendantPids(root)), cancel: () => {} }
  1360. }
  1361. const [command, args] = platform === 'win32'
  1362. ? ['powershell', processTableArgs('win32')]
  1363. : ['ps', processTableArgs('posix')]
  1364. const child = spawn(command, args, {
  1365. stdio: ['ignore', 'pipe', 'ignore'],
  1366. timeout: platform === 'win32' ? 10000 : undefined,
  1367. })
  1368. child.stdout.setEncoding('utf8')
  1369. let stdout = ''
  1370. let settled = false
  1371. let settle!: (value: number[]) => void
  1372. const promise = new Promise<number[]>((resolve) => { settle = resolve })
  1373. const finish = (value: number[]) => {
  1374. if (settled) return
  1375. settled = true
  1376. // The enumeration completed (or was cancelled): stop the subprocess so
  1377. // the gate does not wait on its stdio handles.
  1378. child.kill('SIGTERM')
  1379. settle(value)
  1380. }
  1381. child.stdout.on('data', (chunk: string) => { stdout += chunk })
  1382. child.on('error', () => { finish([]) })
  1383. child.on('close', () => { finish(collectDescendants(root, parsePidPpidLines(stdout))) })
  1384. return {
  1385. promise,
  1386. cancel: () => { finish([]) },
  1387. }
  1388. }
  1389. /** Parse `pid ppid` rows from a process-table dump. Both the POSIX `ps -axo
  1390. * pid=,ppid=` output and the Windows PowerShell `Get-CimInstance Win32_Process`
  1391. * projection emit one `pid ppid` pair per line.
  1392. * @param output - the raw dump text.
  1393. * @returns the parsed pid/ppid rows in line order; blank and malformed lines
  1394. * are dropped.
  1395. */
  1396. export function parsePidPpidLines(output: string): Array<[number, number]> {
  1397. const rows: Array<[number, number]> = []
  1398. for (const line of output.split('\n')) {
  1399. const match = line.trim().match(/^(\d+)\s+(\d+)$/)
  1400. if (match !== null) rows.push([Number(match[1]), Number(match[2])])
  1401. }
  1402. return rows
  1403. }
  1404. /**
  1405. * The taskkill invocations that terminate one Windows gate tree. The direct
  1406. * child leads, because a live `taskkill /T` walks its whole subtree in one
  1407. * call; each captured descendant follows individually, because when the root
  1408. * already exited (a descendant holding the stdio write ends keeps `close`
  1409. * pending) `taskkill /T` rooted at the dead pid finds nothing — Windows never
  1410. * reparents, so the ppid chain captured at terminate still reaches the whole
  1411. * tree, and `/T` lets a surviving intermediate carry its own subtree. A pid
  1412. * that exited between capture and termination is as tolerable as ESRCH on
  1413. * POSIX: taskkill reports a nonzero status that is deliberately unchecked.
  1414. * @param rootPid - the direct child's pid.
  1415. * @param descendants - the captured descendant pids.
  1416. * @returns one `taskkill` argument list per pid, in termination order.
  1417. */
  1418. export function taskkillArgs(rootPid: number, descendants: number[]): string[][] {
  1419. return [rootPid, ...descendants].map(pid => ['/PID', String(pid), '/T', '/F'])
  1420. }
  1421. /** Breadth-first walk of the pid/ppid rows starting at `root`. */
  1422. function collectDescendants(root: number, rows: Array<[number, number]>): number[] {
  1423. const byParent = new Map<number, number[]>()
  1424. for (const [pid, ppid] of rows) {
  1425. const children = byParent.get(ppid) ?? []
  1426. children.push(pid)
  1427. byParent.set(ppid, children)
  1428. }
  1429. const result: number[] = []
  1430. const queue = byParent.get(root) ?? []
  1431. for (let index = 0; index < queue.length; index += 1) {
  1432. const pid = queue[index]
  1433. if (pid === undefined) continue
  1434. result.push(pid)
  1435. queue.push(...(byParent.get(pid) ?? []))
  1436. }
  1437. return result
  1438. }
  1439. /**
  1440. * Format every independently observed failure fact for the aggregate summary.
  1441. * @param result - unsuccessful gate result.
  1442. * @returns error, exit, and signal facts without allowing one to hide another.
  1443. */
  1444. export function formatGateResultReason(result: GateResult): string {
  1445. const facts: string[] = []
  1446. if (result.error !== undefined) facts.push(result.error)
  1447. if (result.exitCode !== null) facts.push(`exit ${result.exitCode}`)
  1448. if (result.signalCode !== null) facts.push(`signal ${result.signalCode}`)
  1449. return facts.length === 0 ? 'no exit code or signal' : facts.join(', ')
  1450. }
  1451. function printResult(result: GateResult): void {
  1452. const verbose = process.env.DSH_GATE_VERBOSE === '1'
  1453. const seconds = (result.durationMs / 1000).toFixed(2)
  1454. if (result.status === 'passed' && !verbose) {
  1455. console.log(`run-gates: PASS ${result.gate.label} (${seconds}s)`)
  1456. return
  1457. }
  1458. const heading = `${result.status.toUpperCase()} ${result.gate.label} (${seconds}s)`
  1459. const writeHeading = result.status === 'passed' ? console.log : console.error
  1460. writeHeading(`\n== ${heading} ==`)
  1461. if (result.status !== 'passed') {
  1462. console.error(`command: ${result.gate.displayCommand}`)
  1463. console.error(`outcome: ${formatGateResultReason(result)}`)
  1464. }
  1465. if (result.gate.streamOutput !== true) printOutput(result.output)
  1466. }
  1467. function printSummary(results: GateResult[], durationMs: number): void {
  1468. const passed = results.filter(result => result.status === 'passed').length
  1469. const failed = results.filter(result => result.status === 'failed').length
  1470. const skipped = results.filter(result => result.status === 'skipped').length
  1471. const seconds = (durationMs / 1000).toFixed(2)
  1472. console.log(`\nrun-gates: ${passed} passed, ${failed} failed, ${skipped} skipped in ${seconds}s.`)
  1473. const unsuccessful = results.filter(result => result.status === 'failed' || result.status === 'skipped')
  1474. if (unsuccessful.length === 0) return
  1475. console.error('run-gates: unsuccessful gates:')
  1476. for (const result of unsuccessful) {
  1477. const duration = (result.durationMs / 1000).toFixed(2)
  1478. const reason = formatGateResultReason(result)
  1479. const disposition = result.gate.allowFailure === true ? 'NON-BLOCKING ' : ''
  1480. console.error(` - ${disposition}${result.status.toUpperCase()} ${result.gate.label} (${duration}s, ${reason})`)
  1481. console.error(` ${result.gate.displayCommand}`)
  1482. }
  1483. }
  1484. function printOutput(output: GateOutputChunk[]): void {
  1485. for (const chunk of output) {
  1486. if (chunk.stream === 'stdout') process.stdout.write(chunk.text)
  1487. else process.stderr.write(chunk.text)
  1488. }
  1489. }