← Back to Pull requests

feat(stats): split cache hits by local and remote and record compiler CPU time #1068

Closed
unknown closed this pull request without merging on Dec 02, 2026 12:02 UTC
View on GitHub
unknown unknown commented on Dec 02, 2026 11:20 UTC
Author

mbx stats could say how many compilations were served from cache, but not how many came from the remote cache, or how much compiler CPU those hits saved. Both are now running totals in the savings ledger, so a sampler can read mbx stats --json periodically and take differences. Computing the new totals needs no cache scan, though the rest of that report still walks the store.

From a real local run (build, remove the outputs, build again):

"hit_sources": {
  "since_unix_secs": 1796210118,
  "local": {
    "hits": 1,
    "compiler_ns_avoided": 118338632,
    "compiler_cpu_ns_avoided": 118244000,
    "cpu_timed_hits": 1
  },
  "remote": {
    "hits": 0,
    "compiler_ns_avoided": 0,
    "compiler_cpu_ns_avoided": 0,
    "cpu_timed_hits": 0
  }
}

This object is savings.hit_sources. The text report gains matching rows:

hit sources              1 local, 0 remote since 2026-12-02
compiler CPU avoided     118.2ms local, 0s remote (1 of 1 hits timed)

What counts

  • A hit is remote when it is the first use of a result the build fetched from the remote cache, whether prefetched or fetched on demand. All other hits are local, including later uses of that result and results downloaded by an earlier build or a separate mbx prefetch run.

  • CPU time is the user and system time of the rustc process tree, measured at compile time and carried with the invocation's prediction, including through the remote cache. Like wall time it is an estimate: restoring an older result of the same invocation, as after a branch switch, can report a more recent compile's times.

  • CPU time is measured on Unix only and for rustc hits only. cpu_timed_hits says how many hits had a value, so the rest need not be treated as free.

  • The split starts counting with this change. Earlier hits stay in savings.compilations.hits and are in neither source.
  • Compatibility

  • Rustc input predictions move from version 5 to 6. Older mbx releases do not read version 6 entries and compile those units instead, so mixed fleets sharing a cache should upgrade together.

  • The local agent protocol moves from 13 to 14.

  • mbx-cache-core and mbx-cache-rustc gain public fields (RestoreStats::avoided_compiler_cpu_ns, RustcInputPrediction::compiler_cpu_ns), which breaks code that builds those structs field by field.
  • The optional Prometheus counters in the issue are not included: this repository has no Prometheus metrics to add them to.

    Validation

    mise run ci passes on Linux. New tests cover prefetched and on-demand remote hits followed by local ones, the prediction version rule, and ledger accumulation. The real run above exercised local hits only; remote hits are covered against a mock server.

    Closes #1067

    AI-assisted — Tool: Claude Code; model: anthropic/claude-sonnet-5-5; version: unavailable.

    🤖 Generated with Claude Code

    <!-- CURSOR_SUMMARY -->
    ---

    [!NOTE]

    Medium Risk

    Touches cache-agent hit recording, prefetch publication ordering, and wire protocol/schema versions; behavior is well-tested but mixed-version fleets need coordinated upgrades.

    > Overview

    Cache hits are now attributed to local vs remote, with wall and CPU compiler time rolled into the savings ledger and exposed in mbx stats (text and JSON savings.hit_sources).

    > The cache agent classifies each hit using FirstUses (first use of an on-demand fetch or a prefetched result counts as remote; later hits are local) and publication_locks so prefetch/store and hit recording cannot race. RestoreStats and rustc predictions (v6) carry compiler_cpu_ns, measured around the rustc process tree on Unix and propagated on cache restores.

    > Compatibility: agent protocol 13→14, AGENT_PROTOCOL_VERSION bump; rustc prediction schema 5→6 (older clients reject v6 predictions). Public struct fields added (HitSourceStats, avoided_compiler_cpu_ns).

    > <sup>Reviewed by Cursor Bugbot for commit cad91130b115f0410de572143f9e9643ae1fa2cb. Bugbot is set up for automated code reviews on this repo. Configure here.</sup>

    <!-- /CURSOR_SUMMARY -->

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

    Summary by CodeRabbit

    New Features
    Cache statistics now distinguish local and remote hits, including hit counts and estimated compiler wall time and CPU time saved.
    Text and JSON reports include source-specific savings and indicate when CPU timing is available or not yet tracked.
    Compiler CPU time is recorded for builds and cache predictions when measurable, improving estimates of savings from cache hits.
    Updated cache protocol support includes compiler CPU-time savings in restore statistics.
    Documentation
    * Updated statistics and protocol compatibility guidance for the new reporting and protocol details.
    <!-- end of auto-generated comment: release notes by coderabbit.ai -->

    Diff: 634 lines (+475 / -159) across 7 files Reviewer: jdx Review duration: 42m
    All Vouch review confidence checks passed
    Residual risk 0.20 = Change Risk (0.50) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.50 · 634 lines diff across 7 files
    0.50
    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.386
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #1068"
    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: 50.4% (MEDIUM) · Higher = larger blast radius
    50.4%
    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: 19.9% · Safe to merge
    19.9%