Skip to content

Prune React Compiler cache entries older than 30 days - #8406

Merged
ymichael merged 2 commits into
mainfrom
bb/8227-cache-react-compiler-output-in-app-tests-an-thr_224svhsmxn
Nov 30, 2026
Merged

ymichael merged 2 commits into
mainfrom
bb/8227-cache-react-compiler-output-in-app-tests-an-thr_224svhsmxn

Conversation

@ymichael

@ymichael ymichael commented Nov 30, 2026 •

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

The React Compiler transform cache at <git-common-dir>/bb-cache/react-compiler never removes anything. Builds alone had grown it to 4.1 GB (702k index files, 2.7 GB of which is index) since Sep 10. #8227 now also caches app tests and the app dev server. A full app test run adds about 38 MB whenever the lockfile, a package.json or a Vite/Vitest config changes (38 commits on main touched one in the last 30 days), and the dev server adds a similar set of entries. Deleting the directory once only resets the clock for that one clone.

What changed

  • apps/app/react-compiler-cache-prune.ts (new):
    • pruneCompilerCache deletes index buckets whose newest entry is older than 30 days.
    • It then deletes stored content that no remaining entry references and that is itself older than 30 days, so content a concurrent writer has just stored, before it appends the index entry, is kept. One race remains: cacache does not rewrite content that already exists, so a new entry whose output is byte-identical to old unreferenced content can lose that content to a prune that already scanned the index. That read, like any read racing a deletion, fails and falls back to a fresh compile that stores the content again, the same path a corrupt entry takes today.
    • Files that disappear mid-run (a second prune, or cacache.rm.content on a corrupt entry) are skipped instead of aborting the run.
    • Run as a script (Node ≥22.19 strips the types), it prunes the directory given as its argument.
    • schedulePrune checks a pruned-at marker in the cache directory. When the marker is more than a day old, it touches the marker and starts the script as a detached, unref'd process, so no build, test run or dev server waits on it.
  • apps/app/vite-react-compiler.ts calls schedulePrune once the cache is set up in configResolved. Failures only log a warning.
  • docs/configuration.md and docs/debugging-and-qa.md describe the pruning.

Entries are dated by when they were written, so a cache hit does not refresh an entry. A module unchanged for more than 30 days recompiles once after a prune. cacache.verify was not used: it loads the whole index into memory, re-checksums all content and clears tmp/ while other worktrees may be writing.

CI is effectively unaffected. Its cache key includes the lockfile and apps/app/vite*.ts hashes, so it starts over whenever those change, and the prune finds little to delete there.

How you verified

  • New apps/app/src/test/react-compiler-cache-prune.test.ts:
    • The first test runs the real script in a child process over expired, shared and recent entries. It checks that expired entries and their unreferenced content are removed, and that content a recent entry still uses survives. Breaking the liveness check makes it fail with ENOENT on the shared content. It also stores content with no index entry and checks that it survives; dropping the 30-day content age check makes that fail.
    • The second test checks that pruning is scheduled at most once a day.
  • On a real shared cache, the first prune took it from 4.1 GB to 1.4 GB: index files from 702k to 234k, content files from 22k to 9.7k. It took about 15 minutes, with a large copy competing for the disk. A steady-state run with nothing to delete took 3 min 54 s of wall time and 59 s of CPU.
  • A backdated marker made pnpm exec turbo run build --filter=@bb/app --force update the marker and leave a detached prune running after the build exited. The build itself reported React Compiler cache: 1476 hits, 0 misses. The Vitest config path also schedules it.
  • pnpm exec turbo run typecheck lint lint:type-aware --filter=@bb/app passes; the 8 React Compiler cache tests pass.

AGENT GENERATED

🤖 Generated with Claude Code

ymichael and others added 2 commits November 30, 2026 13:15
The React Compiler cache at <git-common-dir>/bb-cache/react-compiler
never removed anything. Builds alone grew it to 4.1 GB (702k index
files) since Sep 10, and #8227 added test and dev-server entries.

When the cache wrapper resolves its config and the cache's pruned-at
marker is more than a day old, it touches the marker and starts a
detached prune process. The prune deletes index buckets whose newest
entry is older than 30 days, then deletes content that no remaining
entry references and that is itself older than 30 days, so content a
concurrent writer has just stored is never removed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A missing bucket or content file, removed by a concurrent prune or by
cacache.rm.content on a corrupt entry, made readFile or stat throw and
the detached prune exit with the rest of the cache unpruned until the
next day. The prune now skips files that are gone.

The prune test also stores content with no index entry, as a writer does
before it appends the entry, and checks that it survives.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ymichael
ymichael merged commit 1ea1e75 into main Nov 30, 2026
53 checks passed
@ymichael
ymichael deleted the bb/8227-cache-react-compiler-output-in-app-tests-an-thr_224svhsmxn branch November 30, 2026 21:42
scowalt pushed a commit to scowalt/bb that referenced this pull request Dec 1, 2026
## Human comments

## What was wrong

The React Compiler transform cache at
`<git-common-dir>/bb-cache/react-compiler` never removes anything.
Builds alone had grown it to 4.1 GB (702k index files, 2.7 GB of which
is index) since Sep 10. get-bb#8227 now also caches app tests and the app dev
server. A full app test run adds about 38 MB whenever the lockfile, a
`package.json` or a Vite/Vitest config changes (38 commits on main
touched one in the last 30 days), and the dev server adds a similar set
of entries. Deleting the directory once only resets the clock for that
one clone.

## What changed

- `apps/app/react-compiler-cache-prune.ts` (new):
- `pruneCompilerCache` deletes index buckets whose newest entry is older
than 30 days.
- It then deletes stored content that no remaining entry references and
that is itself older than 30 days, so content a concurrent writer has
just stored, before it appends the index entry, is kept. One race
remains: cacache does not rewrite content that already exists, so a new
entry whose output is byte-identical to old unreferenced content can
lose that content to a prune that already scanned the index. That read,
like any read racing a deletion, fails and falls back to a fresh compile
that stores the content again, the same path a corrupt entry takes
today.
- Files that disappear mid-run (a second prune, or `cacache.rm.content`
on a corrupt entry) are skipped instead of aborting the run.
- Run as a script (Node ≥22.19 strips the types), it prunes the
directory given as its argument.
- `schedulePrune` checks a `pruned-at` marker in the cache directory.
When the marker is more than a day old, it touches the marker and starts
the script as a detached, unref'd process, so no build, test run or dev
server waits on it.
- `apps/app/vite-react-compiler.ts` calls `schedulePrune` once the cache
is set up in `configResolved`. Failures only log a warning.
- `docs/configuration.md` and `docs/debugging-and-qa.md` describe the
pruning.

Entries are dated by when they were written, so a cache hit does not
refresh an entry. A module unchanged for more than 30 days recompiles
once after a prune. `cacache.verify` was not used: it loads the whole
index into memory, re-checksums all content and clears `tmp/` while
other worktrees may be writing.

CI is effectively unaffected. Its cache key includes the lockfile and
`apps/app/vite*.ts` hashes, so it starts over whenever those change, and
the prune finds little to delete there.

## How you verified

- New `apps/app/src/test/react-compiler-cache-prune.test.ts`:
- The first test runs the real script in a child process over expired,
shared and recent entries. It checks that expired entries and their
unreferenced content are removed, and that content a recent entry still
uses survives. Breaking the liveness check makes it fail with `ENOENT`
on the shared content. It also stores content with no index entry and
checks that it survives; dropping the 30-day content age check makes
that fail.
  - The second test checks that pruning is scheduled at most once a day.
- On a real shared cache, the first prune took it from 4.1 GB to 1.4 GB:
index files from 702k to 234k, content files from 22k to 9.7k. It took
about 15 minutes, with a large copy competing for the disk. A
steady-state run with nothing to delete took 3 min 54 s of wall time and
59 s of CPU.
- A backdated marker made `pnpm exec turbo run build --filter=@bb/app
--force` update the marker and leave a detached prune running after the
build exited. The build itself reported `React Compiler cache: 1476
hits, 0 misses`. The Vitest config path also schedules it.
- `pnpm exec turbo run typecheck lint lint:type-aware --filter=@bb/app`
passes; the 8 React Compiler cache tests pass.

> AGENT GENERATED

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
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