Repository navigation
ci: restore main's Vitest transform cache in PR unit shards - #34512
Conversation
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the
Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
No actionable comments were generated in the recent review.
|
There was a problem hiding this comment.
Actionable comments posted: 2

Review info
-
Run configuration-
- Source mode: Auto
- Repository: Unavailable
- Branch: Unavailable
- File: No configuration file from this repository applied
- Commit: Unavailable
- Layers: Repository UI
- Review profile: CHILL
- Plan: Advanced
- Run ID:
8b667bc1-1b46-45dc-8b90-2bc177d8cb80
Files selected for processing: 9-
.github/workflows/pr.yml.github/workflows/unit-module-cache.yml.github/workflows/unit-plan.yml.github/workflows/unit-tests.ymlconfig/scripts/ci-unit-module-cache.mjsconfig/scripts/ci-unit-module-cache.test.mjsconfig/scripts/vitest-module-cache-resolution-key.test.tsconfig/scripts/vitest-module-cache-resolution-key.tsconfig/vitest.config.ts
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 7 remain after this review.
-
| paths: | ||
| - '.github/workflows/unit-module-cache.yml' | ||
| - '.github/workflows/unit-tests.yml' | ||
| - 'config/vitest.config.ts' | ||
| - 'config/scripts/vitest*' | ||
| - 'config/scripts/ci-unit-module-cache*.mjs' | ||
| - 'config/patches/**' | ||
| - 'package.json' | ||
| - 'pnpm-lock.yaml' | ||
| - 'pnpm-workspace.yaml' | ||
| - 'config/.bun-version' |
There was a problem hiding this comment.
Trigger a producer when the new policy inputs change.
If a main push changes only config/dark-build-switches.cjs, a nested package.json, or a tsconfig*.json, the policy hash changes but this push workflow does not run. PR shards then miss the new cache identity until the scheduled producer runs. Add those policy inputs to paths so the promised immediate reseed occurs. (raw.githubusercontent.com)
| // `foo.ts` beside `foo/index.ts` would keep serving the old resolution. Fold every | ||
| // candidate a literal specifier could resolve to into the key instead. | ||
| const LITERAL = /(?=(['"`])([^'"`\n]*)\1)/g | ||
| const DYNAMIC_IMPORT = /\bimport\s*\(\s*/g |
There was a problem hiding this comment.
Detect commented dynamic imports before caching their transforms.
If a module uses import /* comment */ (\./locales/${name}.ts`), DYNAMIC_IMPORT` does not match it. Vite can expand that import from directory contents, but this plugin permits a cached transform. A newly added locale can then leave the restored transform stale. Detect this valid import form or decline caching when the import shape is uncertain. (raw.githubusercontent.com)
61600ef to
eee3cd8
Compare
There was a problem hiding this comment.
Actionable comments posted: 1

Review info
-
Run configuration-
- Source mode: Auto
- Repository: Unavailable
- Branch: Unavailable
- File: No configuration file from this repository applied
- Commit: Unavailable
- Layers: Repository UI
- Review profile: CHILL
- Plan: Advanced
- Run ID:
0ab806ec-2897-427c-bc0f-95517b7e1d56
Files selected for processing: 2-
.github/workflows/pr.yml.github/workflows/unit-tests.yml
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 3 remain after this review.
-
| runner: blacksmith-8vcpu-ubuntu-2404 | ||
| phase_diagnostics: true | ||
| # Restore main's transform cache; the `ci:cold-unit-cache` label or ORCA_UNIT_MODULE_CACHE=off forces cold. | ||
| module_cache: ${{ (vars.ORCA_UNIT_MODULE_CACHE == 'off' || contains(github.event.pull_request.labels.*.name, 'ci:cold-unit-cache')) && 'off' || 'warm' }} |
There was a problem hiding this comment.
Refresh the label state for cold-cache runs.
Adding ci:cold-unit-cache does not start a run because this workflow does not subscribe to labeled events. A rerun also uses the original event payload, so it still selects warm mode. (github.blog)
Trigger a fresh run on label changes, or read the current labels before selecting the cache mode.
Main produces per-shard transform caches on a schedule; PR shards restore the newest one for the same config/lockfile/runtime policy. A Vitest cache key generator folds each literal specifier's resolution candidates into the per-module key so newly added files that shadow a resolution invalidate the importer, and bails on glob-expanded dynamic imports.
Vitest 5.0.5 tags its cache root, which made every seal fail with 'invalid cache entry name' so no producer cache was ever saved.
Co-Authored-By: Claude <noreply@anthropic.com>
eee3cd8 to
652053a
Compare
…#34512) * ci: restore main's Vitest transform cache in PR unit shards Main produces per-shard transform caches on a schedule; PR shards restore the newest one for the same config/lockfile/runtime policy. A Vitest cache key generator folds each literal specifier's resolution candidates into the per-module key so newly added files that shadow a resolution invalidate the importer, and bails on glob-expanded dynamic imports. * ci: list the resolution key plugin in the node tsconfig project * ci: accept Vitest's CACHEDIR.TAG when sealing the unit module cache Vitest 5.0.5 tags its cache root, which made every seal fail with 'invalid cache entry name' so no producer cache was ever saved. * ci: reseed the unit module cache when any cache-key input lands on main Co-Authored-By: Claude <noreply@anthropic.com> --------- Co-authored-by: OrcaWin <293788423+OrcaWin@users.noreply.github.com> Co-authored-by: Claude <noreply@anthropic.com> (cherry picked from commit 8b64ec2)
ELI5
PR unit shards spend most of their worker time importing and transforming ~13k test files. Main now saves those transforms to the Actions cache every 6 hours, and PR shards reuse them. A reused transform has to be byte-for-byte what a cold run would produce. The Vitest per-module key covers most of what a transform depends on. This PR covers the rest in two places: the Actions cache key, and an extra per-module key.
What Changed
unit-module-cache.yml(new): a cold full unit run on main, on a schedule, by manual dispatch, or when a cache-key input lands. It publishes one cache per shard with the keyunit-module-v2-…-<policy digest>-<run>.unit-tests.yml: PR shards restore the newest producer for the same policy digest, usingrestore-keyson the digest prefix. Only cold main runs, or manually seeded runs, can save a cache. PRs never save one, so one PR can't seed another.pr.yml: the unit job runs withmodule_cache: warm. There are two ways to bypass the cache:ci:cold-unit-cachelabel (add it and then push, because labeling alone doesn't trigger a run)ORCA_UNIT_MODULE_CACHE=offvitest-module-cache-resolution-key.ts(new): a VitestdefineCacheKeyGeneratorplugin. It's only loaded whenORCA_UNIT_MODULE_CACHE_PATHis set, so local runs stay cold and unchanged.ci-unit-module-cache-contents.mjs: accepts Vitest 5.0.5'sCACHEDIR.TAG(and checks its signature). Before this, every seal failed withinvalid cache entry name: CACHEDIR.TAG, so since the Vitest bump no producer had ever saved a cache.ci-unit-module-cache.mjs: the policy digest now also covers every non-vendoredpackage.jsonandconfig/dark-build-switches.cjs, and the key prefix is bumped tov2.Staleness analysis (Vitest 5.0.5, our patch doesn't touch the cache)
How Vitest computes
FileSystemModuleCache.generateCachePath:sha1(generators + id + fileContent + envHash), whereenvHashcoversNODE_ENV, the cache format, root, mode,resolve(aliases), plugin names only, the bytes of eachconfigFileDependenciesfile, the environment name and css.define, Vite/Node/Bun versions,tsconfig, or the patch hash. The only guard for those is a whole-cache wipe when the lockfile changes.resolveCachedImports), but not the original specifiers.import.meta.glob(.I wrote a deterministic probe (warm the cache, change one indirect input, compare against a cold run):
foo.tsadded besidefoo/index.ts(test file or transitive)deep.mts(higher-precedence extension), orfoo/index.mtsimport(\./dyn/${n}.ts`)` (expanded to a glob)tsconfig.jsonuseDefineForClassFieldsflip**/tsconfig*.jsonis in the Actions key)foo.tsmoved tofoo/index.tslocale-catalog-test-assetsload()?urlentry is keyed by the JSON bytes)definevalue from acreateRequired.cjsfile or env vardefineat runtime)Our plugins:
phone-contract-oxc: its output is a pure function of the source plus the oxc version from the lockfile, withtsconfig: false.locale-catalog-test-assets: proven fresh above.resolvekey.No plugin has to be excluded from caching.
Remaining trust boundary: a transform could still depend on a file that is neither its own source, a literal specifier's candidates, nor an input covered by the policy digest. No current plugin does this. Any new plugin or config input changes the digest, which forces a cold cache. The scheduled cold run on main also runs the full suite with no cache, so if a cached transform ever let a PR pass that it shouldn't have, that run fails on main.
Key design
node_modules/.pnpm/lock.yaml, NODE_ENV, the checkout root, and the shard. It also includes a sha256 over:config/scripts/vitest*config/patches/**tsconfig*.jsonandpackage.jsonpnpm-lock.yaml,pnpm-workspace.yaml,.npmrc,pnpmfile,.bun-version,dark-build-switches.cjsindex.*/package.jsonentries of any such directory, and thatpackage.json's content. It returnsfalse(don't cache) forimport.meta.globand for template or concatenated dynamic imports.Why
PR unit shards are about $350/day of Blacksmith time. With a warm restore, the production runner finishes them about 18% sooner, and the slowest shard gains as much. Restoring the per-shard tarball takes 2–3 s, and validating it takes 1–4 s. Both are included in the timings below. Locally, with 1,240
src/sharedfiles, the resolution key costs nothing measurable (warm 25.1–25.2 s with the key, 24.9–25.7 s without) and produces the same 3,359 entries.Measured on the production runner (
blacksmith-8vcpu-ubuntu-2404, x64,--maxWorkers=8, node 24)These are two dispatched comparison runs on this branch, rebased onto main: 40617071599 and 40617074080. Each runs baseline, then producer, then warm on the same SHA. The figures are whole-job seconds, the mean of the 2 repetitions:
Test shardstep alone took 382 s with no cache and 302 s warm (−21%). The slowest step went from 421 s to 340 s.Cost (last 24 h, Blacksmith 8 vCPU at $0.016/min)
test / tests node 24 N/5jobs: 3,111 jobs over 575 runs, totalling 21,976 min/day, or $351.6/day.package.jsonortsconfigrun cold), that's about $53.5/day.Linked Issue
Maintainer CI performance work; no existing issue.
Visual Proof
N/A: CI and test configuration only.
Testing
I manually tested these changes locally
Automated tests added/updated, or explained why not below
Staleness probe: 11 cases. Without this PR, 5 come back stale (shadowing, extension precedence, nested index, template glob, tsconfig). With it, 0 do at the per-module level, and tsconfig is covered by the Actions key.
vitest-module-cache-resolution-key.test.tscovers the invalidation contract. The workflow guard test is updated for the producer-only save policy.pnpm check:changedpasses.In this PR's own CI, the shards run warm against a cache that doesn't exist yet (main has no producer until this merges), so they exercise the miss → cold path. A dispatched run of the comparison workflow on this branch exercises producer → prefix restore → warm.
Review
Please check the key-material list and the remaining trust boundary described above.
Agent skill upstream boundary
Notes
Linux CI only. Local runs and Windows/macOS are unaffected because the cache is only enabled when
ORCA_UNIT_MODULE_CACHE_PATHis set.Cache pressure: about 5 × 120 MB per producer run, in Blacksmith's cache store (not GitHub's 10 GB budget), with LRU eviction of older producer runs.
Checklist
N/Awith reasonpnpm lint,pnpm typecheck,pnpm test, andpnpm buildpass (or CI will cover; local preferred)