Skip to content

ci: restore main's Vitest transform cache in PR unit shards - #34512

Merged
OrcaWin merged 4 commits into
mainfrom
ci/unit-transform-cache-main-restore
Oct 28, 2026
Merged

OrcaWin merged 4 commits into
mainfrom
ci/unit-transform-cache-main-restore

Conversation

@OrcaWin

@OrcaWin OrcaWin commented Oct 27, 2026 •

Copy link
Copy Markdown
Collaborator
Files Added Deleted Net
Test 2 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​72 $\color{#cf222e}{\Huge{\mathbf{−}}}$​19 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​53
Prod 9 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​201 $\color{#cf222e}{\Huge{\mathbf{−}}}$​16 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​185

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 key unit-module-v2-…-<policy digest>-<run>.
  • unit-tests.yml: PR shards restore the newest producer for the same policy digest, using restore-keys on 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 with module_cache: warm. There are two ways to bypass the cache:
    • the ci:cold-unit-cache label (add it and then push, because labeling alone doesn't trigger a run)
    • the repository variable ORCA_UNIT_MODULE_CACHE=off
  • vitest-module-cache-resolution-key.ts (new): a Vitest defineCacheKeyGenerator plugin. It's only loaded when ORCA_UNIT_MODULE_CACHE_PATH is set, so local runs stay cold and unchanged.
  • ci-unit-module-cache-contents.mjs: accepts Vitest 5.0.5's CACHEDIR.TAG (and checks its signature). Before this, every seal failed with invalid 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-vendored package.json and config/dark-build-switches.cjs, and the key prefix is bumped to v2.

Staleness analysis (Vitest 5.0.5, our patch doesn't touch the cache)

How Vitest computes FileSystemModuleCache.generateCachePath:

  • It hashes sha1(generators + id + fileContent + envHash), where envHash covers NODE_ENV, the cache format, root, mode, resolve (aliases), plugin names only, the bytes of each configFileDependencies file, the environment name and css.
  • It does not include plugin versions or code, define, Vite/Node/Bun versions, tsconfig, or the patch hash. The only guard for those is a whole-cache wipe when the lockfile changes.
  • When a module is restored, Vitest re-resolves the resolved URLs it stored (resolveCachedImports), but not the original specifiers.
  • It skips caching when the source contains import.meta.glob(.

I wrote a deterministic probe (warm the cache, change one indirect input, compare against a cold run):

Change (importer untouched) Plain Vitest cache With this PR
New foo.ts added beside foo/index.ts (test file or transitive) STALE fresh
New deep.mts (higher-precedence extension), or foo/index.mts STALE fresh
New target file for a template dynamic import(\./dyn/${n}.ts`)` (expanded to a glob) STALE fresh (not cached)
tsconfig.json useDefineForClassFields flip STALE fresh (**/tsconfig*.json is in the Actions key)
foo.ts moved to foo/index.ts fresh fresh
Locale catalog read by locale-catalog-test-assets load() fresh (the ?url entry is keyed by the JSON bytes) fresh
define value from a createRequired .cjs file or env var fresh (SSR applies define at runtime) fresh

Our plugins:

  • phone-contract-oxc: its output is a pure function of the source plus the oxc version from the lockfile, with tsconfig: false.
  • locale-catalog-test-assets: proven fresh above.
  • Workspace aliases: regex rules that map to exact files, and they're part of the resolve key.

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

  • Actions key: platform, arch, exact Node/Bun/Vitest versions, a hash of the installed node_modules/.pnpm/lock.yaml, NODE_ENV, the checkout root, and the shard. It also includes a sha256 over:
    • the vitest config and its imported scripts
    • all config/scripts/vitest*
    • config/patches/**
    • every tsconfig*.json and package.json
    • workspace manifests
    • pnpm-lock.yaml, pnpm-workspace.yaml, .npmrc, pnpmfile, .bun-version, dark-build-switches.cjs
    • the cache scripts themselves
  • Per module (Vitest): id and source bytes, plus this PR's resolution fingerprint. For each relative, root-relative or aliased string literal, the fingerprint records the same-stem directory entries (marking directories), the index.*/package.json entries of any such directory, and that package.json's content. It returns false (don't cache) for import.meta.glob and 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/shared files, 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:

Shard 1 Shard 2 Shard 3 Shard 4 Shard 5 Mean Slowest shard (mean of 2)
No cache 438 456 430 436 470 446 484
Cold producer 452 458 424 444 397 435 458
Warm restore 368 402 360 351 350 366 (−17.9%) 402 (−17%)
  • The Test shard step alone took 382 s with no cache and 302 s warm (−21%). The slowest step went from 421 s to 340 s.
  • The cold producer is no slower than no cache, so seeding adds no overhead to a shard.
  • This is the best case: the warm job ran on the same SHA as the producer. Real PRs restore a main cache that can be up to 6 h old, and every source file changed since then transforms cold.

Cost (last 24 h, Blacksmith 8 vCPU at $0.016/min)

  • PR test / tests node 24 N/5 jobs: 3,111 jobs over 575 runs, totalling 21,976 min/day, or $351.6/day.
  • Gross saving at 17.9%: 3,934 min/day, or $62.9/day. Assuming an 85% hit rate (the policy digest changes on about 14 main pushes a day, and PRs that touch package.json or tsconfig run cold), that's about $53.5/day.
  • Seeding cost: each producer run is 5 shards × about 7.3 min, plus planning, so about 37 min. The 4 scheduled runs a day cost $2.4/day. Push-triggered reseeds (about 14 a day today, some cancelled) cost up to $8.3/day.
  • Net: about $43–51/day saved, and PR critical-path wall time drops by about 80 s per unit shard.
  • The cache lives in Blacksmith's colocated actions/cache store (25 GB per repo, 7-day LRU), so it doesn't touch GitHub's 10 GB budget. Each producer run stores about 5 × 120 MB.

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.ts covers the invalidation contract. The workflow guard test is updated for the producer-only save policy.

  • pnpm check:changed passes.

  • 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

  • Not applicable, or this change copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.

Notes

Linux CI only. Local runs and Windows/macOS are unaffected because the cache is only enabled when ORCA_UNIT_MODULE_CACHE_PATH is 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

  • This PR is small and focused
  • I explained what changed and why (ELI5, the user-facing before/after, the mechanism, and why over the alternatives)
  • Before/after screenshots or videos attached for UI changes, or N/A with reason
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote, and path/shortcut impact considered (or N/A)
  • pnpm lint, pnpm typecheck, pnpm test, and pnpm build pass (or CI will cover; local preferred)

@coderabbitai

coderabbitai Bot commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Note

Reviews paused

It 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 reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

No actionable comments were generated in the recent review.

Recent 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: 4db63afd-3b31-419c-b88a-82f80f8f504c
Commits

Reviewing files that changed from the base of the PR and between eee3cd8 and 652053a.

Files selected for processing: 1
  • .github/workflows/unit-module-cache.yml

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 0 remain after this review.

Walkthrough

The changes add resolution-aware keys for the Vitest filesystem module cache and expand the cache policy inputs. The Vitest configuration enables the plugin and filesystem cache when a cache path is set. CI cache restoration now accepts matching key prefixes, while cache sealing is limited to successful cold runs. A scheduled or manually triggered workflow runs a cold cache producer on main. Pull requests select warm or off cache mode based on the repository variable and PR label. Cache inventory now validates the signature of CACHEDIR.TAG.

Priority: Normal

Merge Risk: Low · up to 65205

Warm caching still has bounded invalidation gaps for dynamic imports, and the cold-cache escape hatch requires a subsequent push. The PR is otherwise mergeable with these limitations understood.

Pre-merge checks: Passed 5
Check name Status Explanation
Title check Passed The title clearly and concisely describes the main change: restoring main’s Vitest transform cache for pull-request unit shards.
Description check Passed The description is detailed and covers the required sections, including the user impact, implementation, rationale, testing, performance data, compatibility notes, and checklist. The linked issue fiel…
Docstring Coverage Passed Docstring coverage is inconclusive: files from earlier reviews still need to be rechecked within the check's analysis limits. Not blocking.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Description check

Explanation

The description is detailed and covers the required sections, including the user impact, implementation, rationale, testing, performance data, compatibility notes, and checklist. The linked issue field states that no existing issue exists, which is acceptable if the author is a maintainer under the template’s special case.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Commits

Reviewing files that changed from the base of the PR and between 2d89c35 and 9eb24a5.

Files selected for processing: 9
  • .github/workflows/pr.yml
  • .github/workflows/unit-module-cache.yml
  • .github/workflows/unit-plan.yml
  • .github/workflows/unit-tests.yml
  • config/scripts/ci-unit-module-cache.mjs
  • config/scripts/ci-unit-module-cache.test.mjs
  • config/scripts/vitest-module-cache-resolution-key.test.ts
  • config/scripts/vitest-module-cache-resolution-key.ts
  • config/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.

Comment on lines +11 to +21
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'

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor · Performance & Scalability · Quick win

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor · Functional Correctness · Quick win

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)

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Commits

Reviewing files that changed from the base of the PR and between 61600ef and eee3cd8.

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.

Comment thread .github/workflows/pr.yml
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' }}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor · Functional Correctness · Quick win

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.

OrcaWin and others added 4 commits October 27, 2026 19:25
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.
@OrcaWin
OrcaWin force-pushed the ci/unit-transform-cache-main-restore branch from eee3cd8 to 652053a Compare October 28, 2026 02:25
@OrcaWin
OrcaWin merged commit 8b64ec2 into main Oct 28, 2026
51 checks passed
audichuang added a commit to audichuang/orca that referenced this pull request Oct 28, 2026
…#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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant