← Back to Pull requests

fix(cache): stop concurrent repairs and collection from discarding valid action records #809

Closed
unknown closed this pull request without merging on Oct 25, 2026 21:37 UTC
View on GitHub
unknown unknown commented on Oct 23, 2026 22:28 UTC
Author

mbx processes that share a cache coordinate through the filesystem, and several recent bugs had the same shape: a process checks some shared state, acts on it later, and another process changes the state in between. Fixing each case on its own adds another lock or another special case. This change is the first of three that replace those one-off fixes with one owner type per kind of shared object: code can change the object only while it holds the owner, and the owner checks that the object is still the one it inspected. This PR does that for action records. Separate PRs do the same for incremental and generated-source trees (#810) and for managed target directories (#811). None of them adds a lock file.

So it is a refactoring, and it fixes two races on action records (action-results/v1/…/<hash>-<size>.json):

  • Two repairs of one damaged record. Two writers that both found a damaged or unrestorable record each replaced it, and the second overwrote the first writer's valid repair without reading it. The fallback in mbx cache import after a failed no-clobber write had the same window. A key could change under a reader, and the store's rule that the first intact result wins did not hold.

  • Collection deleting a fresh repair. Store collection read a record, found its objects missing, and then unlinked the path. If a writer repaired the record in between, collection deleted the valid record, and the next lookup missed.
  • What changed. The new mbx_cache_core::ActionSlot owns one action record. Claiming it opens the record, takes an exclusive lock on that file, and checks that the path still names the locked file, retrying if another process replaced it meanwhile. Stores and store collection still decide from a lock-free read first. Only when that read says a record needs repair or removal do they claim its slot, read it again through the locked handle, and act through the slot: replace it, publish without clobbering, or remove it. So only damaged or dangling records are ever locked. Collection claims without waiting and leaves a record another process holds for the next sweep. Lookups stay lock-free, and lookups and writes share one parser, so they cannot disagree about what is valid.

    With #825. Since #825, a sweep that evicted objects also checks every file and directory in each result's output tree. The locked re-check applies that same test. It walks the tree again instead of reusing the verdicts the lock-free pass cached, so a record is kept if another process restored its missing file in between.

    The lock is on the record itself, so nothing new appears on disk. It is the pattern materialize.rs already uses when it relabels a cache object's mode: lock the object, re-read it under the lock, act, and confirm by inode.

    Compatibility. Older mbx versions don't take the lock, so in a cache shared with them the old races remain between old and new processes, as they are today. On Windows the lock is mandatory, so a lookup that reads a record while another process repairs or removes it can fail; such a record is damaged or dangling and would have missed anyway. Claims open the record for writing, because Linux NFS implements these locks with fcntl, which refuses an exclusive lock on a read-only file; cleanup of an old cache on NFS keeps working. Windows was not run locally; CI covers it.

    Validation. Subprocess tests hold a record from a second process:

  • a writer that finds a damaged record and waits behind another process's repair keeps the winner's record (run against main's store logic, this test fails);

  • storing over an intact record, and collecting one, never claims it;

  • collection skips a dangling record another process holds, and keeps it once that process repairs it;

  • a claim that waits on a record which is replaced meanwhile ends up holding the new record.

  • collection keeps a record whose missing output-tree file was restored after the lock-free pass found it dangling (with the lock-free pass's cached tree verdicts, this test fails).
  • Live run. The reviewed version of this change (head c194e7d), together with #807, #808, #810 and #811, has run on the maintainer's host since 2026-10-24T00:44Z. Hourly checks of its session logs found no cache divergences and no alerts. The merge with main that added the output-tree check to the locked re-check has been tested but has not had a live run.

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

    🤖 Generated with Claude Code

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

    Summary by CodeRabbit

    Bug Fixes
    Improved reliability when cache records are updated or repaired concurrently, helping prevent one operation from overwriting another’s changes.
    Garbage collection rechecks records before removing them, preserving records that have been repaired or updated.
    Cleanup skips records held by another process rather than failing the sweep.
    Dry-run garbage collection reports dangling records without deleting them.
    Cache updates preserve identical records and avoid replacing valid data during concurrent changes.
    <!-- end of auto-generated comment: release notes by coderabbit.ai -->

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: jbellis Review duration: 2829m
    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 #809"
    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%