Files
orca/config/scripts/ci-unit-selection.mjs
T
NeilandCopilot ac91ff6b05 perf(ci): start full unit shards before advisory selection analysis (#37699)
Emit the full matrix without dependency installation or graph discovery; retain independent full test discovery and selected-draft provenance. Reuse existing draft plans, and disable unpublished transform writes on warm misses. Automatic cache production was removed after review because the near-quota cache store has no budgeted retention plan. Full required PR CI passed on 658ea5275c.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-11-02 23:32:32 -08:00

100 lines
3.3 KiB
JavaScript

import { unitConsumers } from './ci-unit-dependency-graph.mjs'
import { balanceFiles } from './ci-shard-assignment.mjs'
import { UNIT_SETUP_FILES } from './ci-unit-files.mjs'
import { FULL_SHARD_COUNT } from './ci-unit-matrix.mjs'
export { FULL_SHARD_COUNT } from './ci-unit-matrix.mjs'
const RUNTIME_ENTRY_POINTS = new Set([
'config/vitest.config.ts',
'config/scripts/run-vitest.mjs',
...UNIT_SETUP_FILES
])
function isUnitRuntimeEntryPoint(file) {
return (
RUNTIME_ENTRY_POINTS.has(file) ||
(/^config\/scripts\/vitest[^/]*\.(?:ts|mjs)$/.test(file) && !file.includes('.test.'))
)
}
export function selectUnitFiles(files, changed, graphInput) {
const full = (reason) => ({ files, reason, full: true })
if (!changed.length) {
return full('Missing changed-path evidence')
}
if (changed.some((file) => !file.startsWith('src/'))) {
return full('Global, deleted, renamed or unknown input')
}
const graph = typeof graphInput === 'function' ? graphInput() : graphInput
if (changed.some((file) => !graph.files.has(file))) {
return full('Global, deleted, renamed or unknown input')
}
for (const file of changed) {
const consumers = unitConsumers([file], graph.reverse)
if ([...consumers].some(isUnitRuntimeEntryPoint)) {
return full('Shared unit runtime input')
}
if (!files.some((test) => consumers.has(test))) {
return full('No proven test coverage for changed inputs')
}
}
const affected = unitConsumers(changed, graph.reverse)
for (const file of graph.opaqueConsumers ?? unitConsumers(graph.opaque, graph.reverse)) {
affected.add(file)
}
const selected = files.filter((file) => affected.has(file))
if (!selected.length) {
return full('No proven test coverage for changed inputs')
}
return {
files: selected,
reason: 'Transitive imports plus indirect-input consumers',
full: false,
checkedInputOwners: graph.checkedInputOwners ?? []
}
}
export function planUnitSelection({
files,
changed,
graph,
timings,
event,
mode = 'shadow',
fullShardCount = FULL_SHARD_COUNT
}) {
const candidate = selectUnitFiles(files, changed, graph)
const selected = mode === 'selected' && event?.pull_request?.draft === true && !candidate.full
const executionFiles = selected ? candidate.files : files
const totalMs = balanceFiles(executionFiles, 1, timings).shards[0].durationMs
const count = selected
? Math.max(1, Math.min(FULL_SHARD_COUNT, Math.ceil(totalMs / 900_000), executionFiles.length))
: fullShardCount
return {
version: 1,
mode: selected ? 'selected' : 'shadow',
selectionAvailable: !candidate.full,
reason: candidate.reason,
checkedInputOwners: candidate.checkedInputOwners ?? [],
files,
candidateFiles: candidate.files,
executionFiles,
shards: Array.from({ length: count }, (_, index) => ({ index: index + 1, count }))
}
}
export function auditUnitSelection(plan, results) {
const candidates = new Set(plan.candidateFiles)
const omittedFailures = Object.entries(results)
.filter(([file, state]) => state === 'failed' && !candidates.has(file))
.map(([file]) => file)
return {
mode: plan.mode,
discovered: plan.files.length,
candidate: candidates.size,
executed: Object.keys(results).length,
omittedFailures
}
}