fix(cache): stop concurrent repairs and collection from discarding valid action records #809
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
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
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
jbellis
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):mbx cache importafter 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.What changed. The new
mbx_cache_core::ActionSlotowns 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.rsalready 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:
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 -->