run-gates.ts 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  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 { CLIENT_BUILD_PROFILE_SELECTOR } from './client-build-environment.ts'
  13. import { COVERAGE_EXEMPT_ENV, coverageExemptHeavySuites } from './coverage-exempt.ts'
  14. import {
  15. COVERAGE_PARTITIONS_ENV,
  16. COVERAGE_TEST_TIMEOUT_ENV,
  17. coverageTestTimeoutArgs,
  18. parseCoveragePartitionCount,
  19. } from './coverage-partitions.ts'
  20. import { pnpmInvocation } from './pnpm-invocation.ts'
  21. /** A named aggregate exposed by the gate runner. */
  22. export type Mode =
  23. | 'ci-primary'
  24. | 'ci-linux-primary'
  25. | 'ci-static'
  26. | 'ci-lint-contracts-ready'
  27. | 'ci-coverage'
  28. | 'ci-snapshot'
  29. | 'ci-artifacts'
  30. | 'ci-consumers'
  31. | 'ci-windows-blocking'
  32. | 'ci-windows-complete'
  33. | 'ci-windows-observational'
  34. | 'node-compat'
  35. | 'check-all'
  36. | 'hygiene'
  37. | 'doc-sync'
  38. | 'doc-quick'
  39. type GateResultStatus = 'passed' | 'failed' | 'skipped'
  40. type GateState = 'pending' | 'running' | GateResultStatus
  41. /** A command and its dependency metadata inside one aggregate. */
  42. export interface Gate {
  43. id: string
  44. label: string
  45. displayCommand: string
  46. command: string
  47. args: string[]
  48. needs?: string[]
  49. /** Gate ids that must settle, regardless of outcome, before this gate starts. */
  50. after?: string[]
  51. env?: Record<string, string | undefined>
  52. /** Include this leaf in the build-free documentation aggregate. */
  53. quick?: boolean
  54. /** Keep a failure visible without failing the aggregate. */
  55. allowFailure?: boolean
  56. /** Write child output as it arrives instead of buffering it until completion. */
  57. streamOutput?: boolean
  58. }
  59. /** The observed outcome of one gate process. */
  60. export interface GateResult {
  61. gate: Gate
  62. status: GateResultStatus
  63. durationMs: number
  64. output: GateOutputChunk[]
  65. exitCode: number | null
  66. signalCode: NodeJS.Signals | null
  67. error?: string
  68. }
  69. interface GateOutputChunk {
  70. stream: 'stdout' | 'stderr'
  71. text: string
  72. }
  73. interface RunningGate {
  74. gate: Gate
  75. promise: Promise<GateResult>
  76. }
  77. interface ConcurrencyDefault {
  78. workers: number
  79. source: string
  80. }
  81. type GateExecutor = (gate: Gate) => Promise<GateResult>
  82. type ResultObserver = (result: GateResult) => void
  83. const root = resolve(import.meta.dirname, '..')
  84. if (import.meta.main) {
  85. process.exitCode = await main(process.argv.slice(2))
  86. }
  87. async function main(args: string[]): Promise<number> {
  88. const mode = parseMode(args[0])
  89. const gates = gatesForMode(mode)
  90. const concurrencyDefault = defaultConcurrency(mode, gates.length)
  91. const concurrencyOverride = process.env.DSH_GATE_CONCURRENCY
  92. const maxConcurrency = concurrencyFromEnv('DSH_GATE_CONCURRENCY', concurrencyDefault.workers)
  93. const concurrencySource = concurrencyOverride === undefined || concurrencyOverride === ''
  94. ? concurrencyDefault.source
  95. : '$DSH_GATE_CONCURRENCY'
  96. const startedAt = performance.now()
  97. console.log(`run-gates: ${mode} running ${gates.length} gate(s) with ${maxConcurrency} worker(s) from ${concurrencySource}.`)
  98. const results = await runGates(gates, maxConcurrency, runGate, printResult)
  99. printSummary(results, performance.now() - startedAt)
  100. return results.some(result => result.gate.allowFailure !== true && (result.status === 'failed' || result.status === 'skipped'))
  101. ? 1
  102. : 0
  103. }
  104. function parseMode(raw: string | undefined): Mode {
  105. switch (raw) {
  106. case 'ci-primary':
  107. case 'ci-linux-primary':
  108. case 'ci-static':
  109. case 'ci-lint-contracts-ready':
  110. case 'ci-coverage':
  111. case 'ci-snapshot':
  112. case 'ci-artifacts':
  113. case 'ci-consumers':
  114. case 'ci-windows-blocking':
  115. case 'ci-windows-complete':
  116. case 'ci-windows-observational':
  117. case 'node-compat':
  118. case 'check-all':
  119. case 'hygiene':
  120. case 'doc-sync':
  121. case 'doc-quick':
  122. return raw
  123. default:
  124. throw new Error(
  125. `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)}.`,
  126. )
  127. }
  128. }
  129. /**
  130. * Resolve the default worker count for one aggregate.
  131. * @param selectedMode - aggregate whose resource posture applies.
  132. * @param total - number of gates in the aggregate.
  133. * @param available - host CPU availability for ordinary modes.
  134. * @returns the default worker count and its diagnostic source.
  135. */
  136. export function defaultConcurrency(
  137. selectedMode: Mode,
  138. total: number,
  139. available = availableParallelism(),
  140. ): ConcurrencyDefault {
  141. if (selectedMode === 'ci-consumers') return { workers: total, source: 'ci-consumers gate count' }
  142. // Local modes cap workers: several doc gates each build a full ts.Program,
  143. // so an uncapped default on a large host trades wall clock for memory blowups.
  144. const localCap = selectedMode === 'check-all'
  145. || selectedMode === 'hygiene'
  146. || selectedMode === 'doc-sync'
  147. || selectedMode === 'doc-quick'
  148. const modeLimit = localCap ? Math.min(4, available) : available
  149. return {
  150. workers: Math.min(total, modeLimit),
  151. source: localCap
  152. ? `${available} available CPU(s), ${selectedMode} cap 4`
  153. : `${available} available CPU(s)`,
  154. }
  155. }
  156. function concurrencyFromEnv(name: string, fallback: number): number {
  157. const raw = process.env[name]
  158. if (raw === undefined || raw === '') return fallback
  159. const parsed = Number.parseInt(raw, 10)
  160. if (!Number.isSafeInteger(parsed) || parsed < 1) {
  161. throw new Error(`run-gates: ${name} must be a positive integer, got ${JSON.stringify(raw)}.`)
  162. }
  163. return parsed
  164. }
  165. function pnpmScript(id: string, script: string, options: Partial<Gate> = {}): Gate {
  166. return {
  167. id,
  168. label: options.label ?? script,
  169. displayCommand: `pnpm run ${script}`,
  170. ...pnpmInvocation(['run', script]),
  171. ...options,
  172. }
  173. }
  174. /** Build official client artifacts inside a CI aggregate without changing sibling gate environments. */
  175. function ciBuildGate(id = 'build', options: Partial<Gate> = {}): Gate {
  176. return pnpmScript(id, 'build', {
  177. ...options,
  178. env: { ...options.env, [CLIENT_BUILD_PROFILE_SELECTOR]: 'official' },
  179. })
  180. }
  181. function pnpmExec(id: string, args: string[], options: Partial<Gate> = {}): Gate {
  182. return {
  183. id,
  184. label: options.label ?? `pnpm exec ${args.join(' ')}`,
  185. displayCommand: `pnpm exec ${args.join(' ')}`,
  186. ...pnpmInvocation(['exec', ...args]),
  187. ...options,
  188. }
  189. }
  190. /**
  191. * Construct the complete gate list for a named aggregate.
  192. * @param selected - aggregate mode to construct.
  193. * @returns the aggregate's gate graph.
  194. */
  195. export function gatesForMode(selected: Mode): Gate[] {
  196. switch (selected) {
  197. case 'ci-primary':
  198. return ciPrimaryGates()
  199. case 'ci-linux-primary':
  200. return [...ciPrimaryGates(), webSnapshotGate(['built-package-invariants'])]
  201. case 'ci-static':
  202. return ciStaticGates({ ownsBuild: false })
  203. case 'ci-lint-contracts-ready':
  204. return [
  205. lintGate(),
  206. pnpmScript('duplication', 'duplication'),
  207. ]
  208. case 'ci-coverage':
  209. return coverageGates()
  210. case 'ci-snapshot':
  211. return [ciBuildGate(), snapshotGate()]
  212. case 'ci-artifacts':
  213. return ciArtifactGates()
  214. case 'ci-consumers':
  215. return ciConsumerGates()
  216. case 'ci-windows-blocking':
  217. return ciWindowsBlockingGates()
  218. case 'ci-windows-complete':
  219. return ciWindowsCompleteGates()
  220. case 'ci-windows-observational':
  221. return ciWindowsObservationalGates()
  222. case 'node-compat':
  223. return nodeCompatGates()
  224. case 'check-all':
  225. return [
  226. pnpmScript('runtime-closure', 'verify-runtime-closure', { label: 'runtime closure' }),
  227. pnpmScript('cordis-config', 'verify-cordis-config', { label: 'Cordis config' }),
  228. pnpmScript('client-domain-graph', 'verify-client-domain-graph', { label: 'client domain graph' }),
  229. pnpmScript('test', 'test'),
  230. pnpmScript('issue-management', 'test:issue-management', { label: 'Issue management policy' }),
  231. pnpmScript('duplication', 'duplication'),
  232. snapshotGate(),
  233. expectedOutputGate(),
  234. pnpmScript('build', 'build'),
  235. pnpmScript('build:web', 'build:web'),
  236. ...hygieneLeafGates({ artifactNeeds: ['build'] }),
  237. ...docSyncLeafGates({
  238. docTypecheckNeeds: ['build'],
  239. docTypecheckEnv: { DSH_DOC_TYPECHECK_USE_BUILD_OUTPUT: '1' },
  240. docTypecheckScript: 'doc-typecheck:contracts-ready',
  241. }),
  242. pnpmScript('module-graph', 'verify-module-graph', { label: 'module graph' }),
  243. ]
  244. case 'hygiene':
  245. return [
  246. ...hygieneLeafGates(),
  247. pnpmScript('cordis-config', 'verify-cordis-config', { label: 'Cordis config' }),
  248. pnpmScript('runtime-closure', 'verify-runtime-closure', { label: 'runtime closure' }),
  249. pnpmScript('vendored-links', 'verify-vendored-links', { label: 'vendored links' }),
  250. ]
  251. case 'doc-sync':
  252. return docSyncLeafGates()
  253. case 'doc-quick':
  254. return docQuickLeafGates()
  255. }
  256. }
  257. function ciSharedStaticGates(): Gate[] {
  258. return [
  259. pnpmScript('runtime-closure', 'verify-runtime-closure', { label: 'runtime closure' }),
  260. pnpmScript('application-entrypoints', 'verify-application-entrypoints', { label: 'application entrypoints' }),
  261. pnpmScript('constraints', 'constraints'),
  262. pnpmScript('dsh-package-licenses', 'verify-dsh-package-licenses', { label: 'DSH package licenses' }),
  263. pnpmScript('package-invariants', 'verify-package-invariants', { label: 'package invariants' }),
  264. pnpmScript('cordis-config', 'verify-cordis-config', { label: 'Cordis config' }),
  265. pnpmScript('optional-dependency-imports', 'verify-optional-dependency-imports', {
  266. label: 'optional dependency imports',
  267. }),
  268. pnpmScript('client-packages', 'verify-client-packages', { label: 'client packages' }),
  269. pnpmScript('client-ui-i18n', 'verify-client-ui-i18n', { label: 'client UI i18n' }),
  270. pnpmScript('issue-management', 'test:issue-management', { label: 'Issue management policy' }),
  271. ]
  272. }
  273. function ciPrimaryGates(): Gate[] {
  274. return [
  275. ...ciSharedStaticGates(),
  276. typertContractsGate(),
  277. pnpmScript('typecheck', 'typecheck:contracts-ready', { needs: ['typert-contracts'] }),
  278. lintGate({ needs: ['typert-contracts'] }),
  279. pnpmScript('duplication', 'duplication'),
  280. ...coverageGates(),
  281. ...nodeCompatSmokeGates(),
  282. snapshotGate(),
  283. ...docSyncLeafGates({
  284. docTypecheckNeeds: ['typert-contracts'],
  285. docTypecheckScript: 'doc-typecheck:contracts-ready',
  286. }),
  287. pnpmScript('module-graph', 'verify-module-graph', { label: 'module graph' }),
  288. pnpmScript('knip', 'knip'),
  289. // The prepared typecheck and build both drive Client tsc, while build also
  290. // repeats the Host contract pass. Wait for all three consumers so build
  291. // neither races tsbuildinfo nor replaces declarations while they are read.
  292. ciBuildGate('build', { needs: ['typecheck', 'lint', 'doc-typecheck'] }),
  293. pnpmScript('publint', 'publint', { needs: ['build'] }),
  294. pnpmScript('node-next-types', 'verify-node-next-types', {
  295. label: 'node-next types',
  296. needs: ['build'],
  297. }),
  298. builtPackageInvariantsGate(['build']),
  299. builtBinSmokeGate(),
  300. ]
  301. }
  302. function nodeCompatGates(): Gate[] {
  303. const typecheck = flagEnabled('DSH_NODE_COMPAT_SKIP_TYPECHECK')
  304. ? []
  305. : [pnpmScript('typecheck', 'typecheck')]
  306. if (runningNodeMajor() !== 22) {
  307. return [...typecheck, ...nodeCompatSmokeGates()]
  308. }
  309. return [
  310. ...typecheck,
  311. pnpmScript('build', 'build', {
  312. ...typecheck.length === 0 ? {} : { needs: ['typecheck'] },
  313. }),
  314. pnpmScript('build:web', 'build:web', {
  315. label: 'Web frontend build',
  316. needs: ['build'],
  317. }),
  318. ...nodeCompatSmokeGates({ cliSmoke: true }),
  319. ]
  320. }
  321. function nodeCompatSmokeGates(options: { cliSmoke?: boolean } = {}): Gate[] {
  322. const gates: Gate[] = [
  323. pnpmExec('source-worker-smoke', [
  324. 'vitest',
  325. 'run',
  326. 'packages/workflow/workflow-worker-thread/tests/source-worker.compat.spec.ts',
  327. ], { label: 'source worker smoke' }),
  328. pnpmExec('jsonl-zstd-smoke', [
  329. 'vitest',
  330. 'run',
  331. 'packages/session/session-persistence-jsonl/tests/zstd.compat.spec.ts',
  332. ], { label: 'JSONL Zstandard smoke' }),
  333. pnpmExec('dsh-source-launch-smoke', [
  334. 'vitest',
  335. 'run',
  336. 'apps/cli/tests/source-launch.compat.spec.ts',
  337. ], { label: 'dsh source-launch smoke' }),
  338. pnpmExec('vitest-jsdom-smoke', [
  339. 'vitest',
  340. 'run',
  341. 'scripts/vitest-environment.compat.spec.ts',
  342. ], { label: 'Vitest jsdom smoke' }),
  343. ]
  344. if (options.cliSmoke) {
  345. gates.push(
  346. pnpmExec('cli-lazy-search-startup-smoke', [
  347. 'vitest',
  348. 'run',
  349. 'apps/cli/tests/lazy-search-startup.compat.spec.ts',
  350. ], {
  351. label: 'CLI lazy-search startup smoke',
  352. env: { DSH_REQUIRE_BUILT_CLI_SMOKE: '1' },
  353. needs: ['build:web'],
  354. }),
  355. )
  356. }
  357. return gates
  358. }
  359. /** Active Node major used to select version-specific compatibility checks. */
  360. function runningNodeMajor(): number {
  361. const major = Number.parseInt(process.versions.node.split('.')[0] ?? '', 10)
  362. if (!Number.isSafeInteger(major)) {
  363. throw new Error(`run-gates: cannot parse Node version ${JSON.stringify(process.versions.node)}.`)
  364. }
  365. return major
  366. }
  367. function ciStaticGates(options: { ownsBuild: boolean }): Gate[] {
  368. return [
  369. ...ciSharedStaticGates(),
  370. ...options.ownsBuild ? [ciBuildGate()] : [],
  371. ...docSyncLeafGates({
  372. includeDocTypecheck: options.ownsBuild,
  373. ...options.ownsBuild
  374. ? {
  375. docTypecheckNeeds: ['build'],
  376. docTypecheckEnv: { DSH_DOC_TYPECHECK_USE_BUILD_OUTPUT: '1' },
  377. docTypecheckScript: 'doc-typecheck:contracts-ready',
  378. }
  379. : {},
  380. docsBuildScript: 'docs:build:mpa',
  381. }),
  382. pnpmScript('module-graph', 'verify-module-graph', { label: 'module graph' }),
  383. pnpmScript('knip', 'knip'),
  384. ]
  385. }
  386. function ciArtifactGates(): Gate[] {
  387. return [
  388. ciBuildGate(),
  389. pnpmScript('publint', 'publint', { needs: ['build'] }),
  390. pnpmScript('node-next-types', 'verify-node-next-types', {
  391. label: 'node-next types',
  392. needs: ['build'],
  393. }),
  394. builtPackageInvariantsGate(['build']),
  395. builtBinSmokeGate(),
  396. ]
  397. }
  398. function ciConsumerGates(): Gate[] {
  399. const builtTree = ['build']
  400. const validatedBuild = ['built-package-invariants']
  401. // The HMR web test starts `dev:web`, which rewrites the shared `lib/` and
  402. // `apps/web/dist/` trees. Let every build-artifact reader settle before that
  403. // writer starts; `after` preserves the web diagnostic even if a reader fails.
  404. const buildArtifactReaders = [
  405. 'publint',
  406. 'lint-and-duplication',
  407. 'snapshot',
  408. 'expected-output',
  409. 'doc-typecheck',
  410. 'node-next-types',
  411. 'built-bin-smoke',
  412. ]
  413. return [
  414. ciBuildGate(),
  415. pnpmScript('node-compat', 'check:node-compat', {
  416. label: 'Node compatibility',
  417. env: { [CLIENT_BUILD_PROFILE_SELECTOR]: 'official' },
  418. }),
  419. pnpmScript('publint', 'publint', { needs: builtTree }),
  420. builtPackageInvariantsGate(builtTree),
  421. pnpmScript('lint-and-duplication', 'check:ci:lint:contracts-ready', {
  422. label: 'lint and duplication',
  423. needs: validatedBuild,
  424. }),
  425. snapshotGate(validatedBuild),
  426. expectedOutputGate(validatedBuild),
  427. webSnapshotGate(validatedBuild, buildArtifactReaders),
  428. pnpmScript('doc-typecheck', 'doc-typecheck:contracts-ready', {
  429. needs: validatedBuild,
  430. env: { DSH_DOC_TYPECHECK_USE_BUILD_OUTPUT: '1' },
  431. }),
  432. pnpmScript('node-next-types', 'verify-node-next-types', {
  433. label: 'node-next types',
  434. needs: validatedBuild,
  435. }),
  436. builtBinSmokeGate(validatedBuild),
  437. ]
  438. }
  439. function webSnapshotGate(needs: string[], after?: string[]): Gate {
  440. const order = after === undefined ? { needs } : { needs, after }
  441. const workerRaw = process.env.DSH_WEB_SNAPSHOT_WORKERS
  442. if (workerRaw !== undefined && workerRaw !== '') {
  443. const workers = Number.parseInt(workerRaw, 10)
  444. if (!Number.isSafeInteger(workers) || workers < 2 || String(workers) !== workerRaw) {
  445. throw new Error(`run-gates: DSH_WEB_SNAPSHOT_WORKERS must be an integer greater than 1, got ${JSON.stringify(workerRaw)}.`)
  446. }
  447. return pnpmScript('web-snapshot', 'test:web:ci', {
  448. label: 'web browser snapshot',
  449. displayCommand: `DSH_SNAPSHOT=replay DSH_WEB_SNAPSHOT_WORKERS=${workers} pnpm run test:web:ci`,
  450. env: { DSH_SNAPSHOT: 'replay' },
  451. ...order,
  452. streamOutput: true,
  453. })
  454. }
  455. return pnpmScript('web-snapshot', 'test:web:built', {
  456. label: 'web browser snapshot',
  457. displayCommand: 'DSH_SNAPSHOT=replay pnpm run test:web:built',
  458. env: { DSH_SNAPSHOT: 'replay' },
  459. ...order,
  460. })
  461. }
  462. function ciWindowsBlockingGates(): Gate[] {
  463. return [
  464. ciBuildGate('windows-build', { label: 'build' }),
  465. pnpmScript('windows-site', 'docs:build', { label: 'production site' }),
  466. ]
  467. }
  468. function ciWindowsCompleteGates(): Gate[] {
  469. const coverage = coverageGates().map(gate => ({
  470. ...gate,
  471. needs: [...new Set(['build', ...(gate.needs ?? [])])],
  472. }))
  473. const coverageAfter = coverage.map(gate => gate.id)
  474. const observational = ciWindowsObservationalGates()
  475. // The required production site replaces the observational MPA build; both
  476. // VitePress modes write the same output directory and cannot overlap.
  477. .filter(gate => gate.id !== 'build' && gate.id !== 'docs-site-build')
  478. .map(gate => ({
  479. ...gate,
  480. allowFailure: true,
  481. after: [...new Set([...coverageAfter, ...(gate.after ?? [])])],
  482. }))
  483. return [
  484. ciBuildGate(),
  485. pnpmScript('windows-site', 'docs:build', { label: 'production site' }),
  486. ...coverage,
  487. ...observational,
  488. ]
  489. }
  490. function ciWindowsObservationalGates(): Gate[] {
  491. return [
  492. ...ciStaticGates({ ownsBuild: true }),
  493. // Linux owns required lint and snapshots; Windows omits those duplicates.
  494. pnpmScript('duplication', 'duplication'),
  495. pnpmScript('publint', 'publint', { needs: ['build'] }),
  496. pnpmScript('node-next-types', 'verify-node-next-types', {
  497. label: 'node-next types',
  498. needs: ['build'],
  499. }),
  500. builtPackageInvariantsGate(['build']),
  501. builtBinSmokeGate(),
  502. ]
  503. }
  504. function typertContractsGate(): Gate {
  505. return pnpmScript('typert-contracts', 'build:lib:host', { label: 'Typert contracts' })
  506. }
  507. function lintGate(options: { needs?: string[] } = {}): Gate {
  508. const raw = process.env.DSH_OXLINT_THREADS
  509. const script = 'lint:contracts-ready'
  510. return pnpmScript('lint', script, {
  511. ...raw === undefined || raw === ''
  512. ? {}
  513. : { displayCommand: `DSH_OXLINT_THREADS=${raw} pnpm run ${script}` },
  514. ...options.needs === undefined ? {} : { needs: options.needs },
  515. })
  516. }
  517. // The heavy suites run uninstrumented beside the thresholded gate: their
  518. // compiler- and subprocess-bound fixtures pay a multiple of their runtime
  519. // under v8 instrumentation while contributing nothing the thresholds need
  520. // (membership rules in scripts/coverage-exempt.ts).
  521. //
  522. // DSH_COVERAGE_MAX_WORKERS is the ordinary lane's worker budget, so the two
  523. // parallel gates split it instead of each claiming it whole. When
  524. // DSH_COVERAGE_PARTITIONS is set, its single-worker processes replace the
  525. // instrumented share while this budget still sizes the exempt gate. The exempt
  526. // gate's wall clock is dominated by its longest single file, so it takes the
  527. // small share. A budget of 1 gives each gate 1 worker; lanes that need a strict
  528. // total of one (the serial reference jobs) also set DSH_GATE_CONCURRENCY=1,
  529. // which keeps the gates from overlapping at all.
  530. // DSH_COVERAGE_TEST_TIMEOUT_MS raises Vitest's per-test and expect.poll
  531. // defaults together for instrumented lanes whose scheduling overhead exceeds
  532. // those defaults. Explicit fixture timeouts remain authoritative.
  533. function coverageWorkerArgs(): { instrumented: string[]; exempt: string[] } {
  534. const [flag] = positiveIntArg('DSH_COVERAGE_MAX_WORKERS', '--maxWorkers')
  535. if (flag === undefined) return { instrumented: [], exempt: [] }
  536. const total = Number.parseInt(flag.split('=')[1] ?? '', 10)
  537. const exempt = Math.max(1, Math.floor(total / 3))
  538. const instrumented = Math.max(1, total - exempt)
  539. return {
  540. instrumented: [`--maxWorkers=${String(instrumented)}`],
  541. exempt: [`--maxWorkers=${String(exempt)}`],
  542. }
  543. }
  544. function coverageGates(): Gate[] {
  545. const workers = coverageWorkerArgs()
  546. const timeouts = coverageTestTimeoutArgs(process.env[COVERAGE_TEST_TIMEOUT_ENV])
  547. const partitions = parseCoveragePartitionCount(process.env[COVERAGE_PARTITIONS_ENV])
  548. const instrumented = partitions === undefined
  549. ? pnpmExec('coverage', [
  550. 'vitest',
  551. 'run',
  552. '--coverage',
  553. ...workers.instrumented,
  554. ...timeouts,
  555. ], {
  556. label: 'test:coverage',
  557. env: { [COVERAGE_EXEMPT_ENV]: '1' },
  558. })
  559. : pnpmScript('coverage', 'test:coverage:partitioned', {
  560. label: 'test:coverage',
  561. displayCommand: `${COVERAGE_PARTITIONS_ENV}=${partitions} pnpm run test:coverage:partitioned`,
  562. env: { [COVERAGE_EXEMPT_ENV]: '1' },
  563. streamOutput: true,
  564. })
  565. return [
  566. instrumented,
  567. pnpmExec('coverage-exempt-heavy', [
  568. 'vitest',
  569. 'run',
  570. ...coverageExemptHeavySuites.map(suite => suite.filter),
  571. ...workers.exempt,
  572. ...timeouts,
  573. ], {
  574. label: 'test:coverage-exempt-heavy',
  575. }),
  576. ]
  577. }
  578. // Recorded-session adapters boot process scenarios in `lib` mode. Callers wait
  579. // either on `build` or on a validation gate that transitively owns that build.
  580. function snapshotGate(needs: string[] = ['build']): Gate {
  581. return pnpmScript('snapshot', 'test:snapshot', {
  582. env: { DSH_EXAMPLE_MODE: 'lib' },
  583. needs,
  584. })
  585. }
  586. // Owner-local process expectations consume built package exports without entering
  587. // the recorded-session corpus or the credentialed provider lane.
  588. function expectedOutputGate(needs: string[] = ['build']): Gate {
  589. return pnpmScript('expected-output', 'test:expected', {
  590. env: { DSH_EXAMPLE_MODE: 'lib' },
  591. needs,
  592. })
  593. }
  594. function builtPackageInvariantsGate(needs?: string[]): Gate {
  595. return pnpmScript('built-package-invariants', 'verify-built-package-invariants', {
  596. label: 'built package invariants',
  597. ...needs === undefined ? {} : { needs },
  598. })
  599. }
  600. function positiveIntArg(envName: string, flag: string): string[] {
  601. const raw = process.env[envName]
  602. if (raw === undefined || raw === '') return []
  603. const parsed = Number.parseInt(raw, 10)
  604. if (!Number.isSafeInteger(parsed) || parsed < 1 || String(parsed) !== raw) {
  605. throw new Error(`run-gates: ${envName} must be a positive integer, got ${JSON.stringify(raw)}.`)
  606. }
  607. return [`${flag}=${raw}`]
  608. }
  609. function flagEnabled(envName: string): boolean {
  610. const raw = process.env[envName]
  611. if (raw === undefined || raw === '') return false
  612. if (raw !== '1') throw new Error(`run-gates: ${envName} must be 1 when set, got ${JSON.stringify(raw)}.`)
  613. return true
  614. }
  615. function hygieneLeafGates(options: { artifactNeeds?: string[] } = {}): Gate[] {
  616. const artifactOptions = options.artifactNeeds === undefined ? {} : { needs: options.artifactNeeds }
  617. return [
  618. pnpmScript('rescope-vendor', 'rescope-vendor:check', { label: 'vendor rescope' }),
  619. pnpmScript('knip', 'knip'),
  620. pnpmScript('publint', 'publint', artifactOptions),
  621. pnpmScript('constraints', 'constraints'),
  622. pnpmScript('application-entrypoints', 'verify-application-entrypoints', { label: 'application entrypoints' }),
  623. pnpmScript('dsh-package-licenses', 'verify-dsh-package-licenses', { label: 'DSH package licenses' }),
  624. pnpmScript('package-invariants', 'verify-package-invariants', { label: 'package invariants' }),
  625. builtPackageInvariantsGate(options.artifactNeeds),
  626. pnpmScript('node-next-types', 'verify-node-next-types', {
  627. label: 'node-next types',
  628. ...artifactOptions,
  629. }),
  630. pnpmScript('optional-dependency-imports', 'verify-optional-dependency-imports', {
  631. label: 'optional dependency imports',
  632. }),
  633. pnpmScript('client-packages', 'verify-client-packages', { label: 'client packages' }),
  634. pnpmScript('client-ui-i18n', 'verify-client-ui-i18n', { label: 'client UI i18n' }),
  635. ]
  636. }
  637. function docSyncLeafGates(options: {
  638. includeDocTypecheck?: boolean
  639. docTypecheckNeeds?: string[]
  640. docTypecheckEnv?: Record<string, string | undefined>
  641. docTypecheckScript?: 'doc-typecheck' | 'doc-typecheck:contracts-ready'
  642. docsBuildScript?: 'docs:build' | 'docs:build:mpa'
  643. } = {}): Gate[] {
  644. const docTypecheckOptions: Partial<Gate> = {}
  645. if (options.docTypecheckNeeds !== undefined) docTypecheckOptions.needs = options.docTypecheckNeeds
  646. if (options.docTypecheckEnv !== undefined) docTypecheckOptions.env = options.docTypecheckEnv
  647. return [
  648. // Stable FIFO starts the longest leaves first; only docs-site-build writes website/.generated.
  649. ...options.includeDocTypecheck === false
  650. ? []
  651. : [pnpmScript('doc-typecheck', options.docTypecheckScript ?? 'doc-typecheck', docTypecheckOptions)],
  652. pnpmScript('docs-site-build', options.docsBuildScript ?? 'docs:build', { label: 'documentation build' }),
  653. pnpmScript('doc-graphs', 'verify-doc-graphs', { label: 'doc graphs' }),
  654. pnpmScript('markdown-links', 'verify-md-links', { label: 'markdown links', quick: true }),
  655. pnpmScript('type-equivalence', 'verify-type-equiv', { label: 'type equivalence', quick: true }),
  656. pnpmScript('cordis-catalog', 'verify-cordis-catalog', { label: 'cordis catalog' }),
  657. pnpmScript('cordis-inspect-catalog', 'verify-cordis-inspect-catalog', { label: 'Cordis inspect catalog' }),
  658. pnpmScript('mermaid', 'verify-mermaid'),
  659. pnpmScript('scoped-events', 'verify-scoped-events', { label: 'scoped events' }),
  660. pnpmScript('translation-pairing', 'verify-translation-pairing', { label: 'translation pairing', quick: true }),
  661. pnpmScript('markdown-wrap', 'verify-md-wrap', { label: 'markdown wrap', quick: true }),
  662. pnpmScript('client-catalog', 'verify-client-catalog', { label: 'client catalog' }),
  663. pnpmScript('export-jsdoc', 'verify-export-jsdoc', { label: 'export jsdoc' }),
  664. pnpmScript('tool-catalog', 'verify-tool-catalog', { label: 'tool catalog' }),
  665. pnpmScript('config-catalog', 'verify-config-catalog', { label: 'config catalog' }),
  666. pnpmScript('persistence-catalog', 'verify-persistence-catalog', { label: 'persistence catalog' }),
  667. pnpmScript('public-repository-links', 'verify-public-repository-links', { label: 'public repository links', quick: true }),
  668. pnpmScript('doc-refs', 'verify-doc-refs', { label: 'doc refs', quick: true }),
  669. pnpmScript('subsystem-pages', 'verify-subsystem-pages', { label: 'subsystem pages' }),
  670. pnpmScript('package-paths', 'verify-package-paths', { label: 'package paths' }),
  671. pnpmScript('config-source-ownership', 'verify-config-source-ownership', { label: 'config source ownership' }),
  672. pnpmScript('package-readme-model-experience', 'verify-package-readme-model-experience', { label: 'package README model experience', quick: true }),
  673. pnpmScript('agent-note-classification', 'verify-agent-note-classification', { label: 'agent note classification', quick: true }),
  674. pnpmScript('agent-note-format', 'verify-agent-note-format', { label: 'agent note format', quick: true }),
  675. pnpmScript('archived-agent-notes', 'verify-archived-agent-notes', { label: 'archived agent notes', quick: true }),
  676. pnpmScript('skill-invocation-metadata', 'verify-skill-invocation-metadata', { label: 'skill invocation metadata', quick: true }),
  677. pnpmScript('translation-prompt', 'verify-translation-prompt', { label: 'translation prompt', quick: true }),
  678. pnpmScript('doc-budgets', 'verify-doc-budgets', { label: 'doc budgets', quick: true }),
  679. pnpmExec('doc-standard-tests', ['vitest', 'run', 'scripts/doc-standard.spec.ts'], {
  680. label: 'documentation standard tests',
  681. quick: true,
  682. }),
  683. pnpmExec('docs-site-projection', ['vitest', 'run', 'scripts/project-doc-site.spec.ts', 'scripts/verify-doc-site-fragments.spec.ts'], {
  684. label: 'documentation site checks',
  685. }),
  686. pnpmScript('package-readme-limitations', 'verify-package-readme-limitations', { label: 'package README limitations', quick: true }),
  687. ]
  688. }
  689. /**
  690. * The quick comprehensive documentation-standard aggregate for `test:docs`.
  691. * It covers the prose, pairing, README, budget, and Agent Note gates
  692. * without builds, generator regeneration, or the VitePress site build.
  693. */
  694. function docQuickLeafGates(): Gate[] {
  695. return docSyncLeafGates({ includeDocTypecheck: false }).filter(gate => gate.quick === true)
  696. }
  697. function builtBinSmokeGate(needs: string[] = ['build']): Gate {
  698. return pnpmExec('built-bin-smoke', [
  699. 'vitest',
  700. 'run',
  701. '--config',
  702. 'vitest.e2e.config.ts',
  703. 'apps/cli/tests/profiles/headless/tests/keyless-smoke.e2e.ts',
  704. 'apps/cli/tests/built-bin.e2e.ts',
  705. 'packages/host/directory-picker-native/tests/built-worker.e2e.ts',
  706. 'packages/sdk/server/tests/built-scope-carrier.e2e.ts',
  707. 'packages/subagent/subagent-codex/tests/loader-composition.e2e.ts',
  708. 'packages/subagent/subagent-claude-code/tests/loader-composition.e2e.ts',
  709. 'packages/api/remotes/tests/built-lib.e2e.ts',
  710. 'packages/experimental/agent-team/tests/built-lib.e2e.ts',
  711. // Built execution consumers: the only automated proof that package-name
  712. // imports reach their lib/ entrypoints under plain Node. The e2e lane runs
  713. // unbuilt, so these files self-skip there.
  714. 'packages/workflow/workflow-worker-thread/tests/built-worker.e2e.ts',
  715. 'packages/code-runtime/code-runtime-worker-thread/tests/built-lib.e2e.ts',
  716. 'packages/lsp/lsp-stdio/tests/built-lib.e2e.ts',
  717. ], {
  718. label: 'built-bin smoke',
  719. needs,
  720. env: { DSH_EXAMPLE_MODE: 'lib' },
  721. })
  722. }
  723. /**
  724. * Reject a gate list whose graph cannot be executed unambiguously.
  725. * @param gates - complete aggregate to validate.
  726. */
  727. function validateGateGraph(gates: readonly Gate[]): void {
  728. if (gates.length === 0) throw new Error('run-gates: gate graph has no gates.')
  729. const ids = new Set<string>()
  730. for (const gate of gates) {
  731. if (ids.has(gate.id)) throw new Error(`run-gates: duplicate gate id ${JSON.stringify(gate.id)}.`)
  732. ids.add(gate.id)
  733. }
  734. for (const gate of gates) {
  735. for (const dependency of gate.needs ?? []) {
  736. if (!ids.has(dependency)) {
  737. throw new Error(`run-gates: gate ${JSON.stringify(gate.id)} depends on unknown gate ${JSON.stringify(dependency)}.`)
  738. }
  739. }
  740. for (const predecessor of gate.after ?? []) {
  741. if (!ids.has(predecessor)) {
  742. throw new Error(`run-gates: gate ${JSON.stringify(gate.id)} waits for unknown gate ${JSON.stringify(predecessor)}.`)
  743. }
  744. }
  745. }
  746. const cycle = findDependencyCycle(gates)
  747. if (cycle !== undefined) throw new Error(`run-gates: dependency cycle: ${cycle.join(' -> ')}.`)
  748. }
  749. function findDependencyCycle(gates: readonly Gate[]): string[] | undefined {
  750. const byId = new Map(gates.map(gate => [gate.id, gate]))
  751. const complete = new Set<string>()
  752. const active = new Map<string, number>()
  753. const path: string[] = []
  754. const visit = (id: string): string[] | undefined => {
  755. if (complete.has(id)) return undefined
  756. const cycleStart = active.get(id)
  757. if (cycleStart !== undefined) return [...path.slice(cycleStart), id]
  758. const gate = byId.get(id)
  759. if (gate === undefined) return undefined
  760. active.set(id, path.length)
  761. path.push(id)
  762. for (const predecessor of [...(gate.needs ?? []), ...(gate.after ?? [])]) {
  763. const cycle = visit(predecessor)
  764. if (cycle !== undefined) return cycle
  765. }
  766. path.pop()
  767. active.delete(id)
  768. complete.add(id)
  769. return undefined
  770. }
  771. for (const gate of gates) {
  772. const cycle = visit(gate.id)
  773. if (cycle !== undefined) return cycle
  774. }
  775. return undefined
  776. }
  777. /**
  778. * Validate and run one aggregate before the injected executor can start a child.
  779. * @param gates - complete aggregate to execute.
  780. * @param maxActive - maximum concurrent child count.
  781. * @param execute - child-process executor.
  782. * @param observe - result observer invoked when each gate settles.
  783. * @returns results in aggregate order.
  784. */
  785. export async function runGates(
  786. gates: Gate[],
  787. maxActive: number,
  788. execute: GateExecutor,
  789. observe: ResultObserver = () => {},
  790. ): Promise<GateResult[]> {
  791. validateGateGraph(gates)
  792. if (!Number.isSafeInteger(maxActive) || maxActive < 1) {
  793. throw new Error(`run-gates: max concurrency must be a positive integer, got ${JSON.stringify(maxActive)}.`)
  794. }
  795. const states = new Map<string, GateState>(gates.map(gate => [gate.id, 'pending']))
  796. const results = new Map<string, GateResult>()
  797. const running: RunningGate[] = []
  798. for (;;) {
  799. let madeProgress = false
  800. while (running.length < maxActive) {
  801. const ready = gates.find(gate => states.get(gate.id) === 'pending' && predecessorsReady(gate, states))
  802. if (ready === undefined) break
  803. states.set(ready.id, 'running')
  804. running.push({ gate: ready, promise: execute(ready) })
  805. console.log(`run-gates: start ${ready.label}`)
  806. madeProgress = true
  807. }
  808. if (running.length === 0) {
  809. const pending = gates.filter(gate => states.get(gate.id) === 'pending')
  810. if (pending.length === 0) break
  811. const gate = pending.find(item => (item.needs ?? []).some(id => gateFailed(states.get(id))))
  812. if (gate === undefined) throw new Error('run-gates: validated graph stalled without a failed dependency.')
  813. const failedDeps = (gate.needs ?? []).filter(id => gateFailed(states.get(id)))
  814. const result: GateResult = {
  815. gate,
  816. status: 'skipped',
  817. durationMs: 0,
  818. output: [],
  819. exitCode: null,
  820. signalCode: null,
  821. error: `dependency failed or skipped: ${failedDeps.join(', ')}`,
  822. }
  823. states.set(gate.id, 'skipped')
  824. results.set(gate.id, result)
  825. observe(result)
  826. continue
  827. }
  828. if (!madeProgress) {
  829. const settled = await Promise.race(running.map(async item => ({ item, result: await item.promise })))
  830. running.splice(running.indexOf(settled.item), 1)
  831. states.set(settled.item.gate.id, settled.result.status)
  832. results.set(settled.item.gate.id, settled.result)
  833. observe(settled.result)
  834. }
  835. }
  836. return gates.map((gate) => {
  837. const result = results.get(gate.id)
  838. if (result === undefined) throw new Error(`run-gates: missing result for ${gate.id}.`)
  839. return result
  840. })
  841. }
  842. function predecessorsReady(gate: Gate, states: Map<string, GateState>): boolean {
  843. return (gate.needs ?? []).every(id => states.get(id) === 'passed')
  844. && (gate.after ?? []).every(id => gateSettled(states.get(id)))
  845. }
  846. function gateSettled(state: GateState | undefined): boolean {
  847. return state === 'passed' || state === 'failed' || state === 'skipped'
  848. }
  849. function gateFailed(state: GateState | undefined): boolean {
  850. return state === 'failed' || state === 'skipped'
  851. }
  852. /**
  853. * Execute one gate through the real shell-free child-process boundary.
  854. * @param gate - command and scheduler environment to execute.
  855. * @returns the complete process outcome.
  856. */
  857. export async function runGate(gate: Gate): Promise<GateResult> {
  858. const started = performance.now()
  859. const output: GateOutputChunk[] = []
  860. let spawnError: string | undefined
  861. const outcome = await new Promise<{
  862. exitCode: number | null
  863. signalCode: NodeJS.Signals | null
  864. }>((resolveExit) => {
  865. const child = spawn(gate.command, gate.args, {
  866. cwd: root,
  867. env: { ...process.env, ...gate.env },
  868. stdio: ['pipe', 'pipe', 'pipe'],
  869. })
  870. child.stdout.setEncoding('utf8')
  871. child.stderr.setEncoding('utf8')
  872. child.stdout.on('data', (chunk: string) => {
  873. if (gate.streamOutput === true) process.stdout.write(chunk)
  874. else output.push({ stream: 'stdout', text: chunk })
  875. })
  876. child.stderr.on('data', (chunk: string) => {
  877. if (gate.streamOutput === true) process.stderr.write(chunk)
  878. else output.push({ stream: 'stderr', text: chunk })
  879. })
  880. child.on('error', (error) => {
  881. spawnError = `failed to start command: ${error.message}`
  882. resolveExit({ exitCode: null, signalCode: null })
  883. })
  884. child.on('close', (exitCode, signalCode) => {
  885. resolveExit({ exitCode, signalCode })
  886. })
  887. child.stdin.end()
  888. })
  889. const { exitCode, signalCode } = outcome
  890. const status: GateResultStatus = exitCode === 0 && signalCode === null && spawnError === undefined ? 'passed' : 'failed'
  891. const result: GateResult = {
  892. gate,
  893. status,
  894. durationMs: performance.now() - started,
  895. output,
  896. exitCode,
  897. signalCode,
  898. }
  899. if (spawnError !== undefined) result.error = spawnError
  900. return result
  901. }
  902. /**
  903. * Format every independently observed failure fact for the aggregate summary.
  904. * @param result - unsuccessful gate result.
  905. * @returns error, exit, and signal facts without allowing one to hide another.
  906. */
  907. export function formatGateResultReason(result: GateResult): string {
  908. const facts: string[] = []
  909. if (result.error !== undefined) facts.push(result.error)
  910. if (result.exitCode !== null) facts.push(`exit ${result.exitCode}`)
  911. if (result.signalCode !== null) facts.push(`signal ${result.signalCode}`)
  912. return facts.length === 0 ? 'no exit code or signal' : facts.join(', ')
  913. }
  914. function printResult(result: GateResult): void {
  915. const verbose = process.env.DSH_GATE_VERBOSE === '1'
  916. const seconds = (result.durationMs / 1000).toFixed(2)
  917. if (result.status === 'passed' && !verbose) {
  918. console.log(`run-gates: PASS ${result.gate.label} (${seconds}s)`)
  919. return
  920. }
  921. const heading = `${result.status.toUpperCase()} ${result.gate.label} (${seconds}s)`
  922. const writeHeading = result.status === 'passed' ? console.log : console.error
  923. writeHeading(`\n== ${heading} ==`)
  924. if (result.status !== 'passed') {
  925. console.error(`command: ${result.gate.displayCommand}`)
  926. console.error(`outcome: ${formatGateResultReason(result)}`)
  927. }
  928. if (result.gate.streamOutput !== true) printOutput(result.output)
  929. }
  930. function printSummary(results: GateResult[], durationMs: number): void {
  931. const passed = results.filter(result => result.status === 'passed').length
  932. const failed = results.filter(result => result.status === 'failed').length
  933. const skipped = results.filter(result => result.status === 'skipped').length
  934. const seconds = (durationMs / 1000).toFixed(2)
  935. console.log(`\nrun-gates: ${passed} passed, ${failed} failed, ${skipped} skipped in ${seconds}s.`)
  936. const unsuccessful = results.filter(result => result.status === 'failed' || result.status === 'skipped')
  937. if (unsuccessful.length === 0) return
  938. console.error('run-gates: unsuccessful gates:')
  939. for (const result of unsuccessful) {
  940. const duration = (result.durationMs / 1000).toFixed(2)
  941. const reason = formatGateResultReason(result)
  942. const disposition = result.gate.allowFailure === true ? 'NON-BLOCKING ' : ''
  943. console.error(` - ${disposition}${result.status.toUpperCase()} ${result.gate.label} (${duration}s, ${reason})`)
  944. console.error(` ${result.gate.displayCommand}`)
  945. }
  946. }
  947. function printOutput(output: GateOutputChunk[]): void {
  948. for (const chunk of output) {
  949. if (chunk.stream === 'stdout') process.stdout.write(chunk.text)
  950. else process.stderr.write(chunk.text)
  951. }
  952. }