0
Fork 0
mirror of https://github.com/mastra-ai/mastra.git synced 2026-11-17 08:44:46 +00:00
mastra/scripts/affected-tests-cache.mjs
Tyler Barnes 3a01ed16f4
ci: cache what affected-test detection parses, so PR test selection takes ~4s instead of ~40s (#29133)
## What

"Detect affected tests" decides which test files a PR runs. Today it
parses ~15,000 files on every PR run: madge builds the import graph,
then a second pass extracts exported names and symbol edges. That takes
~40s on the 5 vCPU runner, and almost none of those files changed since
the last run.

This change caches the parse results in a file, keyed by a hash of each
file's content:

- `scripts/affected-tests-cache.mjs` stores, per file, only what its own
text decides: the import specifiers precinct finds and the exports and
symbol edges the second pass extracts. Records are keyed by
`sha1(extension + content)`.
- Resolving those specifiers to files is **not** cached. It still runs
live through filing-cabinet with madge's exact options, so a rename, a
moved file or an alias change can't be hidden by a cache hit.
- The cache file records its schema version and the precinct and
typescript versions. If any of them differ, the run starts cold. It is
never wrong.
- `.github/workflows/prebuild.yml` restores the cache before detection
(restore only; PRs never save). The key is
`affected-graph-v1-<os>-<sha>` with the prefix restore-key
`affected-graph-v1-<os>-`, so a PR gets the newest main seed.
- The new `.github/workflows/affected-graph-seed.yml` refreshes the seed
on every push to main. It runs detection once on core's entry point,
which touches the whole graph, then saves the cache. A newer push
cancels an older seed run.

The detection output (selected files, graph nodes, test files) is
unchanged.

## Numbers

Measured on a 5 vCPU / 16 GB container like the CI runner, Node 24.22.1:

| | time |
|---|---|
| Uncached (today) | 40.3s |
| Cached, current seed | 4.0s |
| Cached, seed ~300 commits old | ~13s |
| Seed run on main | ~40s |

The seed file is 14.8 MB raw and 1.7 MB in the actions cache.

Compute: about 36s saved per PR run, against about 90s per merge to main
for the seed run.

## Testing

- Ran detection uncached and cached on 20 recent merged PRs (between 1
and 3,532 files selected): output identical on all 20.
- Renamed `retry-after.ts`, updated its importers and added a new test
file importing the renamed module: identical output (2,849 selected),
the new test is selected and the old path is gone from the graph.
- `packages/core/src/affected-tests-cache.test.ts` covers cold, warm and
changed-import runs. It fails if records are keyed by file name instead
of content.
- `packages/core/src/affected-tests-resolution.test.ts` and
`.github/scripts/ci-routing.test.ts` pass. oxlint and prettier are
clean.

Stacked on #29091 (cancelled runs stop downstream jobs). It will be
retargeted to main once that merges.


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
<!-- coderabbit-ai-generated:v1 -->

## ELI5
The affected-test detector checks which tests may need to run after code
changes. This PR saves file-parsing results so later checks can reuse
them, while still resolving file links against the current code.

## What changed
- Added a content-addressed cache for file parsing. It stores import
specifiers, exported names, and symbol edges, and checks a schema and
extractor-version string before reusing records.
- Kept file resolution live through filing-cabinet and Madge’s existing
options. Updated affected-test detection to use the cache and save
records used during the run.
- Added a workflow to refresh the cache seed on pushes to `main`.
Updated prebuild to restore the seed for affected-test detection.
- Added a test for uncached, cold-cache, and warm-cache graphs,
including after a file’s imports change.

## Validation
Test, lint, formatting, output-comparison, runtime, and review-finding
results: not available.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: remy-builder[bot] <324360501+remy-builder[bot]@users.noreply.github.com>
2026-11-05 20:02:44 +00:00

145 lines
5 KiB