← Back to Pull requests

feat(cache): seed incremental state across checkouts by default #766

Closed
unknown closed this pull request without merging on Oct 20, 2026 14:58 UTC
View on GitHub
unknown unknown commented on Oct 19, 2026 22:33 UTC
Author

mbx can already start a new checkout's incremental state from another checkout (#754), so the first edit in a new worktree compiles incrementally. Today that needs the user to add an unstable rustc flag and, on stable Rust, set RUSTC_BOOTSTRAP=1 for the whole build. Few users will find it. This PR turns seeding on by default with rustc 1.98 or later. Nobody has to configure anything.

What changes for users

When a crate gets mbx's private incremental state, mbx now adds -Zremap-cwd-prefix=. to that one compiler command. The first edit to that crate in a fresh worktree then reuses another checkout's state.

On our build host, we measured mbx's own workspace (commit 5ee4195, rustc 1.99.1, dev profile). We timed the first edit to the mbx crate in three fresh clones for each condition:

| First edit in a fresh clone | Runs (s) | Median (s) |
|---|---|---|
| Without seeding | 50.6, 15.4, 18.0 | 18.0 |
| With seeding | 5.8, 6.2, 23.2 | 6.2 |
| Later edits in a warm checkout, for reference | 4.0 to 5.8 | about 5 |

The machine was busy during these runs, with a load average between 30 and 130, so the ratio is approximate. The measurement used the existing opt-in flag, which works by the same mechanism this PR applies automatically. After we installed this branch on the same host, mbx seeded state for 148 crate units in about 2.4 hours of normal work, with no failed compiles caused by the flag.

To turn it off:

mbx settings set seed_incremental false
# or for one build
MBX_SEED_INCREMENTAL=0 mbx build

A user-supplied -Zremap-cwd-prefix still enables seeding, as before.

Visible effects, only for crates compiled with private state:

  • Debug info records . as the working directory.

  • Backtrace frames show ./src/lib.rs instead of an absolute path. This is the same as a normal build run from the checkout.

  • For sources passed by absolute path, file!() and panic locations become relative.

  • gdb started from the checkout finds the sources. A debugger started elsewhere needs a source mapping, and docs/incremental.md gives the gdb and lldb commands. We tested gdb; we did not test lldb.
  • Why this design

    rustc rejects incremental state recorded under a different working directory. The fix has to make every checkout look the same to rustc. We checked the options with plain rustc 1.97.1, 1.98.0 and 1.99.1 by copying state from one directory to another and asserting whether rustc loaded it:

    | Option | State reused in another checkout? |
    |---|---|
    | No flag | No, on every version |
    | --remap-path-prefix=<checkout>=. (stable) | No, on every version, even with --remap-path-scope=all |
    | -Zremap-cwd-prefix=. | Yes, on 1.98.0 and 1.99.1; no on 1.97.1 |

    The stable flag fails because rustc tracks its explicit path list, which names a different checkout each time. Passing one identical list that names every checkout works, but the list changes whenever a worktree is added, which defeats the purpose. Since rust-lang/rust#157348, merged for 1.98, -Zremap-cwd-prefix is the only option that removes the real directory from what rustc checks.

    Alternatives we rejected:

  • Keep it opt-in. Users would need to know about an unstable flag. They would also have to set RUSTC_BOOTSTRAP=1 for all of Cargo, which exposes unstable features to every crate and build script. Setting RUSTFLAGS to add the flag also makes Cargo ignore a project's configured rustflags.

  • Remap every compilation. That would change every shared cache key once and the debug info of every published artifact. mbx never publishes outputs built with private state, so limiting the flag to those compilations leaves the shared cache and its keys unchanged.

  • Default on for nightly only. This avoids RUSTC_BOOTSTRAP, but most users build on stable.

  • An opt-in mbx setting that adds the flag. This is simpler than the first option, but people would still need to know it exists. The goal is fast builds without having to know mbx's settings.
  • Safeguards

    On stable and beta, mbx sets RUSTC_BOOTSTRAP=1 only on that one rustc child process. It appends -Zallow-features=, so #![feature] is still rejected as on stable. mbx already does the same for rustdoc in rustdoc.rs, from #226. Cargo, build scripts, and every other compilation are unaffected. In addition:

  • mbx adds nothing when RUSTC_BOOTSTRAP is already set to any value, including -1.

  • On stable and beta, mbx adds nothing when the command already has a -Z flag of its own. Response files are expanded first. Without bootstrap such a build fails on stable, and mbx should not change that.

  • When Cargo moves a long command into a response file, mbx rewrites the checkout paths into a new response file, so those compilations are seeded too. An argument that cannot be written to that file unchanged, such as one containing a newline, turns automatic seeding off for that compilation.

  • mbx adds nothing to build-script compilations. Their binaries run through mbx's execution cache, which is keyed by the compile action.

  • If rustc reports that a crate read RUSTC_BOOTSTRAP through env! or option_env!, mbx discards that attempt and compiles once more without the flag. It then stops adding the flag for that crate. This also happens when the first attempt fails, for example on a const assertion. A genuine compile error does not trigger a retry.

  • mbx checks that the compiler accepts the flag and caches the answer with the compiler's identity. A future rustc that renames the flag disables seeding instead of failing builds.

  • The private-state directory key includes whether mbx added the flag, so turning the setting on or off never mixes the two kinds of state.
  • One exposure remains. A proc macro running inside that rustc process can read RUSTC_BOOTSTRAP from its environment and see 1. We found no proc macro that does this among the crates on our build host. The flag has been unstable since 2021, tracked in rust-lang/rust#87325. If you would rather ship this off by default first, that is a one-line change to the default.

    Validation

    New unit tests cover the injection policy on each channel, existing user flags and bootstrap values, the MBX_SEED_INCREMENTAL spellings, build scripts including @argfile compiles, rewritten response files, the bootstrap retry on success and failure, and the state-directory marker. Integration tests in tests/build.rs cover seeding from a donor checkout by default, also through Cargo's response files, a build script that reads an absolute file!(), and the retry cases. An independent review traced every path to the action cache and the build-script execution cache. The branch has been running on our build host since 2026-10-19T20:06Z. It overlaps #753 in five files, and whichever PR merges second will be rebased.

    AI-assisted — Tool: Claude Code via Mjolnir; model: anthropic/claude-opus-5-5, openai/gpt-6.1-sol.

    🤖 Generated with Claude Code

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

    Summary by CodeRabbit

    New Features
    Private incremental build state can be reused across checkouts automatically with rustc 1.98 or later, helping new checkouts start with warm build caches.
    Automatic seeding is enabled by default and can be disabled with seed_incremental or MBX_SEED_INCREMENTAL=0. A custom -Zremap-cwd-prefix can still enable seeding.
    Private-state compilations may show relative paths in debug information, backtraces, and panic locations.
    Stable and beta builds preserve feature-gate checks while supporting private-state seeding.

    Documentation
    * Updated configuration and incremental-build guidance with setup details and debugger source-mapping examples.
    <!-- end of auto-generated comment: release notes by coderabbit.ai -->

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: jbellis Review duration: 985m
    All Vouch review confidence checks passed
    Residual risk 0.21 = Change Risk (0.52) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 650 lines diff across 8 files
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #766"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52% (MEDIUM) · Higher = larger blast radius
    52%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 20.5% · Safe to merge
    20.5%