Retry on ErrRefMoved across mutating commands; fix SetRef snapshot desync#130
Merged
Conversation
…sync Mutating commands (start, create, update, delete, reopen, attach, label, comment, defer, undefer, dep add/remove) called store.Commit once and surfaced ErrRefMoved raw when a concurrent writer advanced the ref between snapshot and commit. Factor the close-command retry loop into commitWithRetry and apply it everywhere. Also: TreeFS.SetRef did not advance baseRef when called on the tracked ref, leaving the in-memory snapshot stale. Latent in current callers (init reopens TreeFS after) but proven by a new test and fixed.
iautom8things
approved these changes
May 21, 2026
iautom8things
left a comment
Collaborator
There was a problem hiding this comment.
Looks good! 🚀 🚀 🚀
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why we made the change
If you've ever seen this when running
bw startorbw create:…this PR fixes it. Here's what was going on, ELI5:
bwstores all your issue state on a hidden git branch (refs/heads/beadwork). When you run a command likebw start abc-123, here's what happens under the hood:bwtakes a snapshot of where the branch is right now (let's call it commitf2105ed3).abc-123as in_progress, set assignee to alice").f2105ed3. This is a safety check (compare-and-swap) so two commands can't silently clobber each other's work.The problem: if anything else moved that branch between step 1 and step 3 — another
bwprocess in a different terminal, abw syncfinishing in the background, an agent running in a worktree — the safety check fails and the whole command dies withref moved. You lose the work and have to retry by hand.Only
bw closeknew how to retry.start,create, and a bunch of other commands did not.What we changed
Two things:
1. A shared retry helper. New
cmd/bw/commit_retry.gowith a small function:If the commit hits
ErrRefMoved, it refreshes its view of the branch (picks up whatever the other process did), re-runs the closure against the fresh state, and tries again. Up to 3 times.Wired into:
start,create,update,delete,reopen,attach,label,comment,defer,undefer,dep add,dep remove. The old hand-rolled loop inclosegot replaced too, so there's now exactly one place that knows how to retry.2. A latent TreeFS bug. While investigating, TDD turned up something else:
TreeFS.SetRefwas setting the git ref but not updating its own in-memory snapshot. If anything calledSetRefon the ref it was tracking, the nextCommitwould falsely fail withref moved. Not currently triggered by any user-facing flow (the one caller reopens TreeFS right after), but it was a footgun. Fixed.How it works — a worked example
Before this PR, here's the race that caused your error:
After this PR:
The closure-based design matters because the second attempt has to re-derive everything from the fresh state — you can't just retry the bare commit, because the in-memory mutations were built on top of stale data.
Test plan
Tests are TDD-style — written first, drove the implementation:
internal/treefs/treefs_test.go: four newTestSnapshotConsistentAfter*tests prove thatReset,Refresh,MergeCommit, andSetRefall keep the in-memory snapshot consistent. TheSetRefone failed first, which is how the latent bug was caught.cmd/bw/commit_retry_test.go: three tests for the helper — recovers from a real ref-move race (using a second TreeFS to simulate a concurrent writer), bounds retries, propagates non-CAS errors immediately.go test ./...passes.