---
title: "Your Build Cache Is Lying to You"
date: 2026-05-27
type: post
tags: [build-systems, caching, correctness, rage]
summary: "Every JS monorepo build cache works the same way: hash what you told it to hash. If you forgot to tell it about a file, the cache lies. Silently. For months."
---

Your CI passed. Your tests passed. The build cache said nothing changed. It was wrong.

You shipped a broken build to production because a cache returned stale output from three weeks ago. The bug took two days to find. The fix was one line. The root cause was not your code. It was your build system's trust model.

This is not a theoretical failure mode. I've seen it at Microsoft, in monorepos with hundreds of packages, where a stale cache returned test results that passed against an old version of a shared config. The tests looked green. The config had changed. Nobody declared it as an input. The cache never knew.

## The enemy: declared-input trust

Every mainstream JS monorepo build cache works the same way. Turborepo, Nx, and lage (which I co-created at Microsoft) all share the same architecture: you declare what files a task reads, the system hashes those files, and the hash becomes the cache key. If the hash matches a stored entry, the cache returns the stored output without running the task.

The contract is simple. You tell the cache what matters. The cache trusts you.

The problem is also simple. You will forget something. Not today. Not on the obvious files. You'll forget on the weird one: the shared `tsconfig.base.json` three directories up, the `.env.local` that a test helper reads at runtime, the codegen config that `graphql-codegen` pulls in during a build step. You won't forget because you're careless. You'll forget because the relationship between that file and your task is invisible until something breaks.

Here's what that looks like in code. Turborepo computes a cache key like this:

```
cache_key = blake3(
  command           // "tsc -b"
  + input_globs     // "src/**/*.ts", "tsconfig.json"
  + lockfile_hash   // yarn.lock content hash
  + env_vars        // NODE_ENV, etc.
)
```

Nx does the same thing with "named inputs." Lage does the same thing with `environmentGlob` and package content hashes. The surface syntax differs. The trust model is identical: **hash what the developer declared, skip what they didn't.**

## The lie in practice

Here's a concrete scenario. Your team has a monorepo with 200 packages. Package `api` has a build task that runs `tsc -b`. The cache key includes `src/**/*.ts` and `tsconfig.json`. Builds are fast, CI is green, everyone is happy.

Then a developer on another team adds a feature flag system. The flags live in `config/feature-flags.json` at the repo root. The `api` package reads this file at build time through a small codegen step baked into its `tsc` plugin. The developer who added the flags updated the build script. They did not update the Turborepo pipeline to declare `config/feature-flags.json` as an input to `api#build`.

What happens next:

1. CI runs. `api#build` executes with the new codegen. The cache stores the output, keyed to the current hash of `src/**/*.ts` + `tsconfig.json`.
2. A week later, someone changes `config/feature-flags.json` to disable a flag.
3. CI runs again. Turborepo hashes `src/**/*.ts` + `tsconfig.json`. Nothing changed in those files. Cache hit. Turborepo returns the stored output from step 1.
4. The build output still has the old feature flags baked in. Tests run against the stale output. They pass, because the tests don't check flag values.
5. The stale build ships to production. The feature that should be disabled is still enabled.

The developer who changed the flag file did everything right. The developer who set up the codegen did everything right except declare one input. The cache did exactly what it was told. The result is a production bug that traces back to a missing line in `turbo.json`.

No tool warned about this. No test caught it. The cache returned a stale result and reported it as a cache hit. The system lied, and the lie was silent.

<div class="breakout">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 840 380" role="img" aria-label="Diagram: The Silent Lie, how an undeclared input causes a stale cache hit that ships to production">
  <rect width="840" height="380" fill="#F5F0E8" rx="4"/>
  <text x="420" y="32" text-anchor="middle" font-family="Georgia, serif" font-size="16" font-weight="bold" fill="#2B2421">The Silent Lie</text>
  <rect x="30" y="56" width="160" height="64" rx="4" fill="none" stroke="#2B2421" stroke-width="1.5"/>
  <text x="110" y="80" text-anchor="middle" font-family="monospace" font-size="11" fill="#2B2421">Developer changes</text>
  <text x="110" y="98" text-anchor="middle" font-family="monospace" font-size="11" font-weight="bold" fill="#A0522D">feature-flags.json</text>
  <line x1="190" y1="88" x2="224" y2="88" stroke="#2B2421" stroke-width="1.5" marker-end="url(#arrowhead)"/>
  <rect x="226" y="56" width="170" height="64" rx="4" fill="none" stroke="#2B2421" stroke-width="1.5"/>
  <text x="311" y="76" text-anchor="middle" font-family="monospace" font-size="11" fill="#2B2421">Cache check:</text>
  <text x="311" y="92" text-anchor="middle" font-family="monospace" font-size="11" fill="#2B2421">hash(src/**/*.ts</text>
  <text x="311" y="106" text-anchor="middle" font-family="monospace" font-size="11" fill="#2B2421">+ tsconfig.json)</text>
  <line x1="396" y1="88" x2="430" y2="88" stroke="#2B2421" stroke-width="1.5" marker-end="url(#arrowhead)"/>
  <rect x="432" y="56" width="100" height="64" rx="4" fill="#A0522D" stroke="none"/>
  <text x="482" y="84" text-anchor="middle" font-family="Georgia, serif" font-size="15" font-weight="bold" fill="#F5F0E8">MATCH</text>
  <text x="482" y="102" text-anchor="middle" font-family="monospace" font-size="10" fill="#F5F0E8">(stale!)</text>
  <line x1="532" y1="88" x2="566" y2="88" stroke="#2B2421" stroke-width="1.5" marker-end="url(#arrowhead)"/>
  <rect x="568" y="56" width="128" height="64" rx="4" fill="none" stroke="#2B2421" stroke-width="1.5"/>
  <text x="632" y="80" text-anchor="middle" font-family="monospace" font-size="11" fill="#2B2421">Returns stale</text>
  <text x="632" y="98" text-anchor="middle" font-family="monospace" font-size="11" fill="#2B2421">build output</text>
  <line x1="696" y1="88" x2="730" y2="88" stroke="#2B2421" stroke-width="1.5" marker-end="url(#arrowhead)"/>
  <rect x="732" y="56" width="84" height="64" rx="4" fill="#A0522D" stroke="none"/>
  <text x="774" y="80" text-anchor="middle" font-family="Georgia, serif" font-size="12" font-weight="bold" fill="#F5F0E8">Bug ships</text>
  <text x="774" y="96" text-anchor="middle" font-family="Georgia, serif" font-size="12" font-weight="bold" fill="#F5F0E8">to prod</text>
  <rect x="226" y="150" width="320" height="56" rx="4" fill="none" stroke="#A0522D" stroke-width="1.5" stroke-dasharray="6,3"/>
  <text x="386" y="174" text-anchor="middle" font-family="Georgia, serif" font-size="13" fill="#A0522D">feature-flags.json was never in the declared set.</text>
  <text x="386" y="192" text-anchor="middle" font-family="Georgia, serif" font-size="13" fill="#A0522D">The cache didn't know it existed.</text>
  <line x1="311" y1="150" x2="311" y2="120" stroke="#A0522D" stroke-width="1" stroke-dasharray="4,3"/>
  <text x="110" y="244" text-anchor="middle" font-family="Georgia, serif" font-size="14" font-weight="bold" fill="#2B2421">What the cache hashed:</text>
  <rect x="30" y="258" width="160" height="100" rx="4" fill="none" stroke="#2B2421" stroke-width="1"/>
  <text x="42" y="280" font-family="monospace" font-size="11" fill="#2B2421">src/index.ts</text>
  <text x="42" y="298" font-family="monospace" font-size="11" fill="#2B2421">src/routes.ts</text>
  <text x="42" y="316" font-family="monospace" font-size="11" fill="#2B2421">src/utils.ts</text>
  <text x="42" y="334" font-family="monospace" font-size="11" fill="#2B2421">tsconfig.json</text>
  <text x="490" y="244" text-anchor="middle" font-family="Georgia, serif" font-size="14" font-weight="bold" fill="#2B2421">What the task actually read:</text>
  <rect x="400" y="258" width="180" height="100" rx="4" fill="none" stroke="#2B2421" stroke-width="1"/>
  <text x="412" y="280" font-family="monospace" font-size="11" fill="#2B2421">src/index.ts</text>
  <text x="412" y="298" font-family="monospace" font-size="11" fill="#2B2421">src/routes.ts</text>
  <text x="412" y="316" font-family="monospace" font-size="11" fill="#2B2421">src/utils.ts</text>
  <text x="412" y="334" font-family="monospace" font-size="11" fill="#2B2421">tsconfig.json</text>
  <text x="412" y="352" font-family="monospace" font-size="11" font-weight="bold" fill="#A0522D">feature-flags.json ← missed</text>
  <line x1="220" y1="308" x2="390" y2="308" stroke="#A0522D" stroke-width="1.5" stroke-dasharray="6,3" marker-end="url(#arrowRed)"/>
  <text x="305" y="300" text-anchor="middle" font-family="Georgia, serif" font-size="11" fill="#A0522D">the gap</text>
  <defs>
    <marker id="arrowhead" markerWidth="8" markerHeight="6" refX="8" refY="3" orient="auto">
      <polygon points="0 0, 8 3, 0 6" fill="#2B2421"/>
    </marker>
    <marker id="arrowRed" markerWidth="8" markerHeight="6" refX="8" refY="3" orient="auto">
      <polygon points="0 0, 8 3, 0 6" fill="#A0522D"/>
    </marker>
  </defs>
</svg>
</div>

## Why this is structural, not accidental

The stale cache bug above looks like a mistake someone made. It's tempting to say "just be more careful about declaring inputs." That's the official answer from every tool that uses this model. Turborepo's docs say to list all relevant files in your `inputs` configuration. Nx says to define "named inputs" correctly.

This advice is correct and useless. It's correct because yes, if you perfectly declare every file every task reads, the cache will be correct. It's useless because no human, across hundreds of packages maintained by dozens of developers over years, will sustain perfect declarations. The failure rate approaches 100% given enough time and enough packages.

The deeper problem is that "declare your inputs" conflates two architecturally different things:

**Trust declared inputs.** The developer tells the system what matters. The system believes them. If they're wrong, the system is wrong. This is the Turborepo/Nx/lage model.

**Verify observed inputs.** The system watches what the task actually reads at runtime. If the developer forgot to declare a file, the system catches it anyway because it observed the file access. This is the BuildXL model. It's also what [rage](https://github.com/kenotron-ms/rage) implements.

These are not two flavors of the same thing. They are fundamentally different trust architectures. One makes correctness a property of human discipline. The other makes correctness a property of the mechanism.

<div class="breakout">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 840 400" role="img" aria-label="Diagram: Declared vs Observed Inputs, two architectures compared">
  <rect width="840" height="400" fill="#F5F0E8" rx="4"/>
  <text x="210" y="36" text-anchor="middle" font-family="Georgia, serif" font-size="16" font-weight="bold" fill="#2B2421">Declared Inputs</text>
  <text x="210" y="56" text-anchor="middle" font-family="Georgia, serif" font-size="12" fill="#A0522D">Turborepo / Nx / lage</text>
  <text x="630" y="36" text-anchor="middle" font-family="Georgia, serif" font-size="16" font-weight="bold" fill="#2B2421">Observed Inputs</text>
  <text x="630" y="56" text-anchor="middle" font-family="Georgia, serif" font-size="12" fill="#A0522D">BuildXL / rage</text>
  <line x1="420" y1="20" x2="420" y2="380" stroke="#2B2421" stroke-width="1" stroke-dasharray="4,4"/>
  <rect x="60" y="76" width="300" height="180" rx="4" fill="none" stroke="#2B2421" stroke-width="1.5"/>
  <text x="80" y="100" font-family="Georgia, serif" font-size="13" fill="#2B2421">Developer declares:</text>
  <rect x="80" y="112" width="200" height="24" rx="3" fill="#fff" stroke="#2B2421" stroke-width="1"/>
  <text x="90" y="129" font-family="monospace" font-size="11" fill="#2B2421">src/**/*.ts</text>
  <rect x="80" y="142" width="200" height="24" rx="3" fill="#fff" stroke="#2B2421" stroke-width="1"/>
  <text x="90" y="159" font-family="monospace" font-size="11" fill="#2B2421">tsconfig.json</text>
  <rect x="80" y="172" width="200" height="24" rx="3" fill="#fff" stroke="#2B2421" stroke-width="1"/>
  <text x="90" y="189" font-family="monospace" font-size="11" fill="#2B2421">package.json</text>
  <rect x="80" y="206" width="200" height="28" rx="3" fill="none" stroke="#A0522D" stroke-width="1.5" stroke-dasharray="5,3"/>
  <text x="180" y="225" text-anchor="middle" font-family="Georgia, serif" font-size="11" font-style="italic" fill="#A0522D">?? missing file ??</text>
  <text x="210" y="276" text-anchor="middle" font-family="Georgia, serif" font-size="13" fill="#2B2421">Cache key covers 3 of 4 actual inputs.</text>
  <text x="210" y="296" text-anchor="middle" font-family="Georgia, serif" font-size="14" font-weight="bold" fill="#A0522D">Result: silent stale cache.</text>
  <rect x="480" y="76" width="300" height="180" rx="4" fill="none" stroke="#2B2421" stroke-width="1.5"/>
  <text x="500" y="100" font-family="Georgia, serif" font-size="13" fill="#2B2421">Sandbox observes at runtime:</text>
  <rect x="500" y="112" width="220" height="24" rx="3" fill="#fff" stroke="#2B2421" stroke-width="1"/>
  <text x="510" y="129" font-family="monospace" font-size="11" fill="#2B2421">src/**/*.ts</text>
  <rect x="500" y="142" width="220" height="24" rx="3" fill="#fff" stroke="#2B2421" stroke-width="1"/>
  <text x="510" y="159" font-family="monospace" font-size="11" fill="#2B2421">tsconfig.json</text>
  <rect x="500" y="172" width="220" height="24" rx="3" fill="#fff" stroke="#2B2421" stroke-width="1"/>
  <text x="510" y="189" font-family="monospace" font-size="11" fill="#2B2421">package.json</text>
  <rect x="500" y="206" width="220" height="28" rx="3" fill="#A0522D" stroke="none"/>
  <text x="610" y="225" text-anchor="middle" font-family="monospace" font-size="11" font-weight="bold" fill="#F5F0E8">feature-flags.json ← caught</text>
  <text x="630" y="276" text-anchor="middle" font-family="Georgia, serif" font-size="13" fill="#2B2421">Cache key covers all 4 actual inputs.</text>
  <text x="630" y="296" text-anchor="middle" font-family="Georgia, serif" font-size="14" font-weight="bold" fill="#2B2421">Result: correct cache, always.</text>
  <line x1="60" y1="328" x2="780" y2="328" stroke="#2B2421" stroke-width="0.5"/>
  <text x="420" y="352" text-anchor="middle" font-family="Georgia, serif" font-size="13" fill="#2B2421">The declared model asks: "did the developer list everything?"</text>
  <text x="420" y="372" text-anchor="middle" font-family="Georgia, serif" font-size="13" fill="#2B2421">The observed model asks: "what did the process actually read?"</text>
</svg>
</div>

## The receipts

Here's what Turborepo's cache key computation looks like, simplified from the [Turborepo source](https://github.com/vercel/turborepo):

```
// Turborepo: single-phase, declared inputs only
cache_key = blake3(
    task_command,
    sorted(hash(file) for file in resolve_globs(declared_input_globs)),
    hash(lockfile_relevant_section),
    sorted(env_var_pairs),
)
// If file X was read by the task but not in declared_input_globs,
// it is invisible. A change to X produces the same cache_key.
// The cache returns stale output. No warning.
```

Now here's what a two-phase fingerprint looks like. This is the model [rage](https://github.com/kenotron-ms/rage) uses, borrowed directly from [BuildXL](https://github.com/microsoft/BuildXL):

```
// Phase 1: Weak Fingerprint (cheap, runs every lookup)
weak_fp = blake3(
    task_command,
    hash(tool_binary),               // compiler upgrade = new key
    sorted(hash(file) for file in resolve_globs(declared_input_globs)),
    sorted(env_var_pairs),
    sorted(dep_abi_fingerprints),     // upstream package ABI hashes
)

// Phase 2: Strong Fingerprint (runs only on WF match)
pathset = pathset_store.lookup(weak_fp)  // files the sandbox OBSERVED
                                         // on a previous run of this task
strong_fp = blake3(
    weak_fp,
    sorted(hash(content(file)) for file in pathset.reads),
)
// Cache hit only if strong_fp matches a stored entry.
```

The difference is one word: `pathset`. The weak fingerprint uses declared inputs, same as Turborepo. The strong fingerprint adds the files the task *actually read*, as reported by a file-access sandbox that monitored the process. If a task reads `feature-flags.json` and nobody declared it, the sandbox catches the read. The pathset includes it. The strong fingerprint includes its content hash. Next time `feature-flags.json` changes, the strong fingerprint changes, the cache misses, and the task re-runs.

The failure mode is inverted. In Turborepo, a missing declaration is a silent stale cache. In rage, a missing declaration is a one-time cache miss while the sandbox observes the read. The next run is correct and cached. You don't need perfect declarations. The mechanism compensates.

## The cost of observation

This is not free. The sandbox adds overhead. On macOS, rage uses `DYLD_INSERT_LIBRARIES` to inject a shared library that intercepts filesystem calls. On Linux, it uses eBPF tracepoints. Both record every file the task reads and writes, then ship that log back to the cache layer.

The overhead is real: single-digit milliseconds per task on warm caches, more on cold runs. Turborepo's flat hash is faster when every input is correctly declared. If you have a small monorepo and a disciplined team that maintains perfect input declarations, the simpler model wins on raw speed.

But "perfect declarations" is a bet against human nature. I maintained [lage](https://github.com/microsoft/lage) at Microsoft for years. I watched teams get input declarations wrong. Not because they were bad engineers. Because the codebase evolved, new files appeared, implicit dependencies formed, and nobody updated the pipeline config. The stale cache bugs were always silent, always delayed, always expensive to diagnose.

The sandbox makes the mechanism do the work that humans can't sustain. The cost is milliseconds. The payoff is correctness you don't have to think about.

## What the sandbox actually sees

When rage runs a task, the sandbox records a `Pathset`:

```rust
pub struct Pathset {
    pub reads: BTreeSet<PathBuf>,   // every file the task read
    pub writes: BTreeSet<PathBuf>,  // every file the task wrote
}
```

This pathset is stored alongside the weak fingerprint. A single weak fingerprint can have multiple pathsets: the same `tsc -b` command might follow different code paths under different conditions and read different files. The strong fingerprint phase disambiguates them.

On cache lookup, rage iterates through candidate pathsets for the matching weak fingerprint, hashes the current content of each pathset's read files, and checks whether any resulting strong fingerprint matches a stored output. A hit means the task's actual inputs (not just declared inputs) are unchanged. A miss means something the task reads has changed, and it needs to re-run.

The cache tells the truth because the sandbox saw the truth.

## The next post

The two-phase fingerprint and the sandbox that feeds it are the core of what makes rage's cache correct. How the weak fingerprint and strong fingerprint interact, how pathsets accumulate and resolve, how ABI fingerprints cut off unnecessary downstream rebuilds. That's [Post 2: "Two-Phase Fingerprinting: BuildXL's Deep Magic."](/posts/two-phase-fingerprinting-buildxls-deep-magic)

For now, the point is simpler. If your build cache hashes only what you declared, it is exactly as correct as your declarations are complete. In a 200-package monorepo maintained by 40 engineers over 3 years, your declarations are not complete. Your cache is lying to you. You just haven't caught it yet.