test(cli): close mc-zndi7.76's --if-revision single-winner test gaps (#7233)

* test(cli): close mc-zndi7.76's --if-revision single-winner test gaps

T4.4 exercised the Update guard against the library only; embedded CLI
tests never catch a CLI wiring bypass because bd's local exclusive
flock serializes every mutation regardless of whether --if-revision is
actually threaded through. This closes the remaining mc-zndi7.76 gaps:

- gap 1 (mutant MA): new Lifecycle contract cases
  (RunLifecycleUpdateExpectedVersionSingleWinnerWithDisjointColumnsUnderConcurrency,
  RunLifecycleCloseExpectedVersionSingleWinnerUnderConcurrency,
  RunLifecycleUpdateExpectedVersionSingleWinnerAcrossUpdateAndCloseUnderConcurrency),
  wired into all three backends (dolt, embeddeddolt, uow).
- gap 2 (mutant MC): a new dolt-server-tier CLI concurrency test,
  TestSharedServerIfRevisionSingleWinner, spawning N real bd
  subprocesses that race update/close/assign --if-revision against one
  shared, unlocked Dolt server. Delete is deliberately excluded --
  its production guard is independently known-broken under real
  concurrency and is tracked as its own schema-touching fix,
  mc-zndi7.73.
- gap 4 (mutants ML/MO/MD1/MD2): close's --if-revision bypass now has
  CLI-level coverage for --continue/--suggest-next/--claim-next
  rejection, cascade-delete fencing, and a proxied-route stale-guard
  refusal.

TDD: hand-applying mutant MC (dropping update.go's ExpectedVersion
wiring) reproduces the bug report's symptom exactly -- 10/10
concurrent racers exit 0 instead of 1 winner + 9 guard refusals.
Restoring the real code returns to exactly 1 winner, confirmed across
4 repeated real-container runs plus -race.

Gates: gofmt, go vet, go build (both tags), golangci-lint (native,
0 issues), bazel go_srcs.py --check, and targeted go test runs
(embeddeddolt/dolt/uow lifecycle contract suites and the full set of
cmd/bd if-revision tests) all green, with and without -race, against a
real dolthub/dolt-sql-server:2.2.0 container.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* build(bazel): sync cmd/bd BUILD.bazel srcs for the new if-revision test files

make bazel-sync-check only validates the go_srcs.py-managed filegroups,
not gazelle's own go_test srcs list, so the two new test files from the
previous commit were invisible to Bazel. CI's BUILD-sync and go-test
equivalence gates caught it; this is gazelle's own generated diff,
applied via the CI job's uploaded bazel-sync-patch artifact.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
Julian Knutsen 2026-10-05 01:57:10 -07:00 • committed by GitHub
commit cd2507735e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 782 additions and 0 deletions

View file

@ -1000,6 +1000,292 @@ func RunLifecycleUpdateExpectedVersionSingleWinnerUnderConcurrency(t *testing.T,
}
}
// RunLifecycleUpdateExpectedVersionSingleWinnerWithDisjointColumnsUnderConcurrency
// is RunLifecycleUpdateExpectedVersionSingleWinnerUnderConcurrency's sibling,
// and the reason that one is not enough on its own (mc-zndi7.76, gap 1).
//
// Every racer there writes the SAME two cells (Priority and Notes), so a
// backend whose storage layer auto-merges disjoint-cell changes from a common
// base — Dolt's native behavior — could pass that test on native cell-level
// conflict detection ALONE, with the ExpectedVersion/row_lock compare-and-swap
// doing nothing at all: sixteen racers colliding on one cell look identical to
// sixteen racers colliding on one ROW VERSION. A hand-applied mutant that
// reminted row_lock to an unconditional fresh random value on every write
// (rather than deriving it from, and checking it against, ExpectedVersion)
// still passed the shared-cell test on this suite's in-tree backends 3 of 3
// runs each, and only showed its true colors — 3 to 7 winners per round — on a
// real Dolt server CLI race with disjoint-column patches, which is the shape
// this case restores.
//
// Each racer here writes a column NO OTHER RACER TOUCHES, so Dolt's per-cell
// auto-merge cannot provide false mutual exclusion for any pair of them:
// whatever excludes all but one racer here has to be the row_lock/
// ExpectedVersion compare-and-swap itself, not an accident of which cells
// collide.
func RunLifecycleUpdateExpectedVersionSingleWinnerWithDisjointColumnsUnderConcurrency(t *testing.T, ctx context.Context, fixture LifecycleUpdateFixture) {
t.Helper()
type disjointField struct {
name string
apply func(*publicops.IssuePatch, string)
read func(*types.Issue) string
}
fields := []disjointField{
{"Description", func(p *publicops.IssuePatch, v string) { p.Description = publicops.Field[string]{Set: true, Value: v} }, func(i *types.Issue) string { return i.Description }},
{"Design", func(p *publicops.IssuePatch, v string) { p.Design = publicops.Field[string]{Set: true, Value: v} }, func(i *types.Issue) string { return i.Design }},
{"AcceptanceCriteria", func(p *publicops.IssuePatch, v string) {
p.AcceptanceCriteria = publicops.Field[string]{Set: true, Value: v}
}, func(i *types.Issue) string { return i.AcceptanceCriteria }},
{"Notes", func(p *publicops.IssuePatch, v string) { p.Notes = publicops.Field[string]{Set: true, Value: v} }, func(i *types.Issue) string { return i.Notes }},
{"SpecID", func(p *publicops.IssuePatch, v string) { p.SpecID = publicops.Field[string]{Set: true, Value: v} }, func(i *types.Issue) string { return i.SpecID }},
{"AwaitID", func(p *publicops.IssuePatch, v string) { p.AwaitID = publicops.Field[string]{Set: true, Value: v} }, func(i *types.Issue) string { return i.AwaitID }},
{"Owner", func(p *publicops.IssuePatch, v string) { p.Owner = publicops.Field[string]{Set: true, Value: v} }, func(i *types.Issue) string { return i.Owner }},
// Assignee rides the same Update/ExpectedVersion machinery `bd assign`
// dispatches onto (cmd/bd/mutate_proxied_server.go's proxiedAssign, and
// its direct-route twin), so a disjoint racer on this column is this
// suite's single-winner coverage for assign (mc-zndi7.76, gap 3): there
// is no separate Assign method on Lifecycle to race independently.
{"Assignee", func(p *publicops.IssuePatch, v string) { p.Assignee = publicops.Field[string]{Set: true, Value: v} }, func(i *types.Issue) string { return i.Assignee }},
}
racers := len(fields)
id := fixture.IssuePrefix + "-lup-singlewinner-disjoint"
seedLifecycleUpdateIssue(t, ctx, fixture, lifecycleUpdateIssue(id))
seed := lifecycleUpdateRow(t, ctx, fixture, id)
racedToken := seed.RowVersion
var wg sync.WaitGroup
won := make([]bool, racers)
errs := make([]error, racers)
wg.Add(racers)
for i := 0; i < racers; i++ {
i := i
go func() {
defer wg.Done()
version := racedToken
var patch publicops.IssuePatch
fields[i].apply(&patch, fmt.Sprintf("racer-%d-%s", i, fields[i].name))
result, err := fixture.Lifecycle.Update(ctx, publicops.UpdateRequest{
Actor: "racer",
IssueID: id,
Patch: patch,
ExpectedVersion: &version,
})
errs[i] = err
won[i] = err == nil && result.Changed
}()
}
wg.Wait()
winners := 0
winner := -1
for i, win := range won {
if win {
winners++
winner = i
continue
}
if !errors.Is(errs[i], storage.ErrVersionMismatch) {
t.Errorf("racer %d (%s): err = %v, want nil (won) or ErrVersionMismatch (lost the race)", i, fields[i].name, errs[i])
}
}
if winners != 1 {
t.Fatalf("%d racers on disjoint columns shared one --if-revision token %d; got %d winners, want exactly 1 (errs=%v)",
racers, racedToken, winners, errs)
}
final := lifecycleUpdateRow(t, ctx, fixture, id)
wantValue := fmt.Sprintf("racer-%d-%s", winner, fields[winner].name)
for i, f := range fields {
got := f.read(final)
if i == winner {
if got != wantValue {
t.Errorf("final %s = %q, want the single winner's %q — a torn or lost write", f.name, got, wantValue)
}
continue
}
if got != "" {
t.Errorf("final %s = %q, want empty — a disjoint loser's write was not supposed to apply (false mutual exclusion from native cell-merge, not the version guard)", f.name, got)
}
}
if final.RowVersion == racedToken {
t.Errorf("final row version = %d, want it to have advanced past the raced token %d", final.RowVersion, racedToken)
}
}
// RunLifecycleCloseExpectedVersionSingleWinnerUnderConcurrency is
// RunLifecycleUpdateExpectedVersionSingleWinnerUnderConcurrency's Close twin
// (mc-zndi7.76, gap 3): CloseRequest.ExpectedVersion rides the same row_lock
// compare-and-swap Update's does (CloseRequest.ExpectedVersion's doc), and
// until now nothing proved that under real concurrency for Close specifically.
// N racers close the SAME open issue racing the SAME pre-close token; exactly
// one may apply its close, every other racer must see ErrVersionMismatch (not
// the idempotent-reclose no-op — the token is stale by the time it would
// observe that), and the row left behind must be exactly the single winner's.
func RunLifecycleCloseExpectedVersionSingleWinnerUnderConcurrency(t *testing.T, ctx context.Context, fixture LifecycleUpdateFixture) {
t.Helper()
const racers = 16
id := fixture.IssuePrefix + "-lup-close-singlewinner"
seedLifecycleUpdateIssue(t, ctx, fixture, lifecycleUpdateIssue(id))
seed := lifecycleUpdateRow(t, ctx, fixture, id)
racedToken := seed.RowVersion
var wg sync.WaitGroup
won := make([]bool, racers)
errs := make([]error, racers)
wg.Add(racers)
for i := 0; i < racers; i++ {
i := i
go func() {
defer wg.Done()
version := racedToken
result, err := fixture.Lifecycle.Close(ctx, publicops.CloseRequest{
Actor: "racer",
IssueID: id,
Reason: fmt.Sprintf("racer-%d", i),
ExpectedVersion: &version,
})
errs[i] = err
won[i] = err == nil && result.Changed
}()
}
wg.Wait()
winners := 0
winner := -1
for i, win := range won {
if win {
winners++
winner = i
continue
}
if !errors.Is(errs[i], storage.ErrVersionMismatch) {
t.Errorf("racer %d: err = %v, want nil (won) or ErrVersionMismatch (lost the race)", i, errs[i])
}
}
if winners != 1 {
t.Fatalf("%d racers shared one close --if-revision token %d; got %d winners, want exactly 1 (errs=%v)",
racers, racedToken, winners, errs)
}
final := lifecycleUpdateRow(t, ctx, fixture, id)
if final.Status != types.StatusClosed {
t.Errorf("final status = %q, want closed", final.Status)
}
wantReason := fmt.Sprintf("racer-%d", winner)
if final.CloseReason != wantReason {
t.Errorf("final close reason = %q, want the single winner's %q — a torn or lost write", final.CloseReason, wantReason)
}
if final.RowVersion == racedToken {
t.Errorf("final row version = %d, want it to have advanced past the raced token %d", final.RowVersion, racedToken)
}
}
// RunLifecycleUpdateExpectedVersionSingleWinnerAcrossUpdateAndCloseUnderConcurrency
// is this suite's mixed-verb single-winner case (mc-zndi7.76, gap 3): Update
// and Close share one row_lock compare-and-swap, so a token raced across BOTH
// verbs at once must still admit exactly one winner, whichever verb it turns
// out to be — an exclusion mechanism keyed to the row rather than to a given
// verb's own code path. Delete is deliberately NOT a racer here: delete's
// production code does not remint row_lock on a deletion (nothing is left to
// remint), so it does not share this exclusion at all today. That is a real,
// separately tracked bug (the sibling delete bead, mc-zndi7.73) and not a gap
// in this test — folding Delete in here would either mask the bug behind
// Update/Close's real exclusion winning the race, or make this case flake
// red on current main, neither of which belongs in a test pinning what DOES
// work.
func RunLifecycleUpdateExpectedVersionSingleWinnerAcrossUpdateAndCloseUnderConcurrency(t *testing.T, ctx context.Context, fixture LifecycleUpdateFixture) {
t.Helper()
const racers = 8 // four Update racers, four Close racers, interleaved.
id := fixture.IssuePrefix + "-lup-mixed-singlewinner"
seedLifecycleUpdateIssue(t, ctx, fixture, lifecycleUpdateIssue(id))
seed := lifecycleUpdateRow(t, ctx, fixture, id)
racedToken := seed.RowVersion
type outcome struct {
kind string
changed bool
err error
}
outcomes := make([]outcome, racers)
var wg sync.WaitGroup
wg.Add(racers)
for i := 0; i < racers; i++ {
i := i
go func() {
defer wg.Done()
version := racedToken
if i%2 == 0 {
result, err := fixture.Lifecycle.Update(ctx, publicops.UpdateRequest{
Actor: "racer",
IssueID: id,
Patch: publicops.IssuePatch{
Owner: publicops.Field[string]{Set: true, Value: fmt.Sprintf("update-racer-%d", i)},
},
ExpectedVersion: &version,
})
outcomes[i] = outcome{kind: "update", changed: err == nil && result.Changed, err: err}
return
}
result, err := fixture.Lifecycle.Close(ctx, publicops.CloseRequest{
Actor: "racer",
IssueID: id,
Reason: fmt.Sprintf("close-racer-%d", i),
ExpectedVersion: &version,
})
outcomes[i] = outcome{kind: "close", changed: err == nil && result.Changed, err: err}
}()
}
wg.Wait()
winners := 0
winner := -1
for i, o := range outcomes {
if o.changed {
winners++
winner = i
continue
}
if !errors.Is(o.err, storage.ErrVersionMismatch) {
t.Errorf("racer %d (%s): err = %v, want nil (won) or ErrVersionMismatch (lost the race)", i, o.kind, o.err)
}
}
if winners != 1 {
t.Fatalf("%d update/close racers shared one --if-revision token %d; got %d winners, want exactly 1 (outcomes=%+v)",
racers, racedToken, winners, outcomes)
}
final := lifecycleUpdateRow(t, ctx, fixture, id)
switch outcomes[winner].kind {
case "update":
wantOwner := fmt.Sprintf("update-racer-%d", winner)
if final.Owner != wantOwner {
t.Errorf("final owner = %q, want the single winner's %q — a torn or lost write", final.Owner, wantOwner)
}
if final.Status == types.StatusClosed {
t.Errorf("the winning racer was an update, not a close; final status must not be closed")
}
case "close":
if final.Status != types.StatusClosed {
t.Errorf("final status = %q, want closed — the winning racer was a close", final.Status)
}
wantReason := fmt.Sprintf("close-racer-%d", winner)
if final.CloseReason != wantReason {
t.Errorf("final close reason = %q, want the single winner's %q — a torn or lost write", final.CloseReason, wantReason)
}
}
if final.RowVersion == racedToken {
t.Errorf("final row version = %d, want it to have advanced past the raced token %d", final.RowVersion, racedToken)
}
}
// RunLifecycleUpdateConditionalGuardAcceptsRespelledAssignee pins the
// ga-5ksp5 fix beside RunLifecycleUpdateConditionalGuardsGateOrdinaryEdits
// above rather than inside it: that test's later "order-dependent composition"

View file

@ -452,6 +452,9 @@ var roleContractCases = []roleContract{
RunLifecycleUpdateRefusalWritesNoMemberOfThePatch,
RunLifecycleUpdateConditionalGuardsGateOrdinaryEdits,
RunLifecycleUpdateExpectedVersionSingleWinnerUnderConcurrency,
RunLifecycleUpdateExpectedVersionSingleWinnerWithDisjointColumnsUnderConcurrency,
RunLifecycleCloseExpectedVersionSingleWinnerUnderConcurrency,
RunLifecycleUpdateExpectedVersionSingleWinnerAcrossUpdateAndCloseUnderConcurrency,
RunLifecycleUpdateConditionalGuardAcceptsRespelledAssignee,
RunLifecycleUpdateMetadataPatchOrdersMergeSetUnset,
RunLifecycleUpdateClosePolicy,

View file

@ -814,6 +814,7 @@ go_test(
"identity_preview_gate_test.go",
"identity_preview_refusal_test.go",
"if_revision_embedded_test.go",
"if_revision_proxied_close_delete_test.go",
"if_revision_reassign_fence_embedded_test.go",
"if_revision_reassign_fence_proxied_test.go",
"if_revision_test.go",
@ -1099,6 +1100,7 @@ go_test(
"shared_migrate_consent_test.go",
"shared_server_cfgnil_test.go",
"shared_server_cleanup_test.go",
"shared_server_if_revision_concurrency_test.go",
"shared_server_integration_test.go",
"shared_store_migrate_gate_test.go",
"ship_embedded_test.go",

View file

@ -196,6 +196,70 @@ func TestIfRevisionMatchAndMismatch(t *testing.T) {
})
}
// TestIfRevisionCascadeDelete pins mc-zndi7.76 (gap 4): a single named id with
// --cascade takes the SAME deleteBatch path a multi-id delete does
// (cmd/bd/delete.go:105, "len(issueIDs) > 1 || cascade"), which is the only
// way a single-id --if-revision request ever reaches deleteBatch's own
// ExpectedVersion wiring (requireSingleIfRevisionID refuses a guard beside
// more than one explicit id, so a literal multi-id request can never get
// here). A stale guard must refuse the WHOLE cascade — the dependent survives
// right alongside the named parent — on both the real run and the unconfirmed
// --dry-run preview, which reports the same conditional-write envelope rather
// than falling through to the generic preview-with-error path.
func TestIfRevisionCascadeDelete(t *testing.T) {
if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" {
t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt integration tests")
}
t.Parallel()
bd := buildEmbeddedBD(t)
dir, _, _ := bdInit(t, bd, "--prefix", "cd")
parent := bdCreate(t, bd, dir, "Cascade parent", "--type", "task")
child := bdCreate(t, bd, dir, "Cascade child", "--type", "task")
bdDepAdd(t, bd, dir, child.ID, parent.ID)
rev0 := bdShowRevision(t, bd, dir, parent.ID)
bdUpdate(t, bd, dir, parent.ID, "--notes", "bump")
rev1 := bdShowRevision(t, bd, dir, parent.ID)
// A stale guard refuses the preview too: the dedicated conditional-write
// envelope, not the generic "here is what cascade would delete" preview.
out, code := bdRunFailCode(t, bd, dir, "delete", parent.ID, "--cascade", "--dry-run", "--if-revision", revStr(rev0))
if code != ExitGuardMismatch {
t.Errorf("stale --if-revision cascade dry-run exit code = %d, want %d\n%s", code, ExitGuardMismatch, out)
}
if !strings.Contains(out, "revision mismatch") {
t.Errorf("cascade dry-run mismatch error should say \"revision mismatch\", got:\n%s", out)
}
// MO: a stale guard reports the dedicated conditional-write envelope
// instead of falling through to the generic "here is what cascade would
// delete" preview render -- the two are mutually exclusive outcomes for
// the same failed dry-run, not a report-then-preview sequence.
if strings.Contains(out, "DELETE PREVIEW") {
t.Errorf("stale --if-revision dry-run rendered the generic deletion preview instead of the guard-mismatch envelope:\n%s", out)
}
bdShow(t, bd, dir, parent.ID)
bdShow(t, bd, dir, child.ID)
// A stale guard refuses the real cascade entirely -- neither the named
// parent nor the dependent cascade pulled in alongside it is deleted.
out, code = bdRunFailCode(t, bd, dir, "delete", parent.ID, "--cascade", "--force", "--if-revision", revStr(rev0))
if code != ExitGuardMismatch {
t.Errorf("stale --if-revision cascade delete exit code = %d, want %d\n%s", code, ExitGuardMismatch, out)
}
if !strings.Contains(out, "revision mismatch") {
t.Errorf("cascade delete mismatch error should say \"revision mismatch\", got:\n%s", out)
}
bdShow(t, bd, dir, parent.ID)
bdShow(t, bd, dir, child.ID)
// The matching token applies the cascade: both rows are gone.
bdRunOK(t, bd, dir, "delete", parent.ID, "--cascade", "--force", "--if-revision", revStr(rev1))
bdShowFail(t, bd, dir, parent.ID)
bdShowFail(t, bd, dir, child.ID)
}
// TestIfRevisionComposesWithIfAssigneeIfStatus pins T4.5: on `bd update`,
// --if-revision composes with --if-assignee/--if-status as a conjunction —
// every guard present must hold, not just one of them, regardless of which

View file

@ -0,0 +1,100 @@
//go:build cgo
package main
import (
"errors"
"os/exec"
"strings"
"testing"
"github.com/steveyegge/beads/internal/types"
)
// TestProxiedIfRevisionCloseMismatchRefuses pins mc-zndi7.76 (gap 4 / mutant
// MD2): close_if_revision.go's proxied route wires ExpectedVersion into both
// of its SingleIssueUpdate calls (the plain close and the --force one), but
// until now nothing on the proxied route ever gave it a STALE token --
// TestProxiedIfRevisionCloseReplaysMoleculeAutoClose and
// TestProxiedIfRevisionCloseWarnsOnForcedOpenChildren only ever close with the
// current revision. A stale guard must refuse the close outright, before any
// write, on the real shared Dolt server.
func TestProxiedIfRevisionCloseMismatchRefuses(t *testing.T) {
requireSharedProxiedServer(t)
t.Parallel()
bd := buildEmbeddedBD(t)
p := newSharedProxiedProject(t, bd, "ivcm")
issue := bdProxiedCreate(t, bd, p.dir, "Guarded proxied close vs stale revision")
rev0 := bdProxiedShowRevision(t, bd, p.dir, issue.ID)
bdProxiedUpdateOne(t, bd, p.dir, issue.ID, "--notes", "bump the revision out from under the guard")
stdout, stderr, err := bdProxiedRunBuffers(t, bd, p.dir, "close", issue.ID, "--if-revision", proxiedRevStr(rev0))
if err == nil {
t.Fatalf("stale --if-revision close should have failed, got:\nstdout:\n%s\nstderr:\n%s", stdout, stderr)
}
var ee *exec.ExitError
if !errors.As(err, &ee) {
t.Fatalf("stale --if-revision close failed without an exit code: %v", err)
}
out := stdout + stderr
if ee.ExitCode() != ExitGuardMismatch {
t.Errorf("stale --if-revision close exit code = %d, want %d\n%s", ee.ExitCode(), ExitGuardMismatch, out)
}
if !strings.Contains(out, "revision mismatch") {
t.Errorf("stale --if-revision close error should say \"revision mismatch\", got:\n%s", out)
}
db := openProxiedDB(t, p)
if got := readStatus(t, db, issue.ID); got == types.StatusClosed {
t.Errorf("stale --if-revision close must not have applied: status = %q", got)
}
}
// TestProxiedIfRevisionDeleteMatchAndMismatch pins mc-zndi7.76 (gap 4 / mutant
// MD4): delete_proxied_server.go wires ExpectedVersion: ifRevision into the
// issueops.DeleteRequest it sends to the real Deleter, but no proxied test
// gave --if-revision to delete at all before now. A matching guard deletes
// the row; a stale guard refuses, and the row survives.
func TestProxiedIfRevisionDeleteMatchAndMismatch(t *testing.T) {
requireSharedProxiedServer(t)
t.Parallel()
bd := buildEmbeddedBD(t)
p := newSharedProxiedProject(t, bd, "ivdm")
db := openProxiedDB(t, p)
t.Run("mismatch_refuses", func(t *testing.T) {
t.Parallel()
issue := bdProxiedCreate(t, bd, p.dir, "Guarded proxied delete vs stale revision")
rev0 := bdProxiedShowRevision(t, bd, p.dir, issue.ID)
bdProxiedUpdateOne(t, bd, p.dir, issue.ID, "--notes", "bump the revision out from under the guard")
stdout, stderr, err := bdProxiedRunBuffers(t, bd, p.dir, "delete", issue.ID, "--force", "--if-revision", proxiedRevStr(rev0))
if err == nil {
t.Fatalf("stale --if-revision delete should have failed, got:\nstdout:\n%s\nstderr:\n%s", stdout, stderr)
}
var ee *exec.ExitError
if !errors.As(err, &ee) {
t.Fatalf("stale --if-revision delete failed without an exit code: %v", err)
}
out := stdout + stderr
if ee.ExitCode() != ExitGuardMismatch {
t.Errorf("stale --if-revision delete exit code = %d, want %d\n%s", ee.ExitCode(), ExitGuardMismatch, out)
}
if !strings.Contains(out, "revision mismatch") {
t.Errorf("stale --if-revision delete error should say \"revision mismatch\", got:\n%s", out)
}
row := readStatus(t, db, issue.ID)
if row == "" {
t.Errorf("stale --if-revision delete must not have applied: row %s is gone", issue.ID)
}
})
t.Run("match_applies", func(t *testing.T) {
t.Parallel()
issue := bdProxiedCreate(t, bd, p.dir, "Guarded proxied delete vs current revision")
rev := bdProxiedShowRevision(t, bd, p.dir, issue.ID)
bdProxiedDelete(t, bd, p.dir, issue.ID, "--force", "--if-revision", proxiedRevStr(rev))
assertRowAbsent(t, db, "issues", issue.ID)
})
}

View file

@ -0,0 +1,264 @@
//go:build cgo
package main
import (
"context"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strconv"
"sync"
"testing"
"github.com/steveyegge/beads/internal/testutil"
)
// sharedServerIfRevisionRacers is how many concurrent bd subprocesses race
// the same --if-revision token per verb below. The bug report (mc-zndi7.76
// gap 2) observed 12-13 "winners" out of comparably sized rounds on a real
// Dolt server when the CLI only compared a pre-read RowVersion in Go and
// then sent an unguarded write (mutant MC): embedded-mode CLI tests can
// never catch that, because bd's local exclusive flock serializes every
// mutation regardless of whether the guard is actually wired in. This test
// spawns real concurrent bd processes, each with its own SQL connection,
// against one shared, unlocked Dolt server, so an unguarded write really can
// race — and "win" alongside others — exactly as on a real deployment.
const sharedServerIfRevisionRacers = 10
// TestSharedServerIfRevisionSingleWinner pins mc-zndi7.76 gap 2 (mutant MC):
// N concurrent `bd update|close|assign --if-revision <shared-stale-token>`
// CLI subprocesses race the same row against a real, shared Dolt server with
// no local lock to serialize them. Exactly one must exit 0 and apply its
// write; the rest must exit ExitGuardMismatch (13) having changed nothing.
//
// Delete is deliberately excluded here: delete's production guard is
// independently known-broken under real concurrency (16 of 16 "winners" on
// a real Dolt server, since a deleted row leaves no cell for a concurrent
// deleter to collide on) and is tracked, with its own goroutine-level
// single-winner conformance test, as mc-zndi7.73 — a schema-touching fix
// that is out of scope for this test-only change.
func TestSharedServerIfRevisionSingleWinner(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("not supported on Windows")
}
bdBinary := buildSharedServerTestBinary(t)
cp, err := testutil.NewContainerProvider()
if err != nil {
// A lane that exists to run the Dolt server suites must not pass
// green having skipped this test.
if os.Getenv(testutil.EnvRequireDoltContainer) == "1" {
t.Fatalf("cannot start Dolt server, but %s=1: %v", testutil.EnvRequireDoltContainer, err)
}
t.Skipf("cannot start Dolt container: %v", err)
}
t.Cleanup(func() { _ = cp.Stop() })
containerPort := cp.Port()
sharedDir := t.TempDir()
if err := cp.WritePortFile(sharedDir); err != nil {
t.Fatalf("write port file: %v", err)
}
baseEnv := []string{
"PATH=" + os.Getenv("PATH"),
"HOME=" + t.TempDir(),
"GOPATH=" + os.Getenv("GOPATH"),
"GOROOT=" + os.Getenv("GOROOT"),
"BEADS_SHARED_SERVER_DIR=" + sharedDir,
"BEADS_DOLT_SHARED_SERVER=1",
"BEADS_DOLT_SERVER_PORT=" + strconv.Itoa(containerPort),
"BEADS_DOLT_AUTO_START=0",
"BEADS_TEST_MODE=1",
"BD_DISABLE_METRICS=1",
"BD_DISABLE_EVENT_FLUSH=1",
"GIT_TERMINAL_PROMPT=0",
"GIT_ASKPASS=",
"SSH_ASKPASS=",
"GT_ROOT=",
}
ctx := context.Background()
if dl, ok := t.Deadline(); ok {
var cancel context.CancelFunc
ctx, cancel = context.WithDeadline(ctx, dl)
defer cancel()
}
projectDir := filepath.Join(t.TempDir(), "ivrace")
if err := os.MkdirAll(projectDir, 0o755); err != nil {
t.Fatalf("mkdir project dir: %v", err)
}
if err := gitInit(ctx, projectDir); err != nil {
t.Fatalf("git init: %v", err)
}
if out, err := ssExec(ctx, bdBinary, projectDir, baseEnv,
"init", "--shared-server", "--external", "--prefix", "ivrace", "--quiet", "--non-interactive"); err != nil {
t.Fatalf("bd init: %s: %v", out, err)
}
// ssShowRevision runs "bd show <id> --json" and returns the issue as a
// map plus its revision token, the way ssParseShowJSON already does for
// the rest of this package's shared-server helpers.
ssShowRevision := func(t *testing.T, id string) (map[string]any, string) {
t.Helper()
out, err := ssExec(ctx, bdBinary, projectDir, baseEnv, "show", id, "--json")
if err != nil {
t.Fatalf("bd show %s --json: %s: %v", id, out, err)
}
m, err := ssParseShowJSON(out)
if err != nil {
t.Fatalf("parse bd show %s --json: %v\nraw: %s", id, err, out)
}
rev, _ := m["revision"].(string)
if rev == "" {
t.Fatalf("bd show %s --json has no revision field:\n%s", id, out)
}
return m, rev
}
// race runs sharedServerIfRevisionRacers concurrent `bd <args>` subprocesses,
// each built from argsFor(i), and returns their exit codes and combined
// output (the latter only surfaced by assertSingleWinner on failure, to
// keep passing runs quiet).
race := func(t *testing.T, argsFor func(i int) []string) ([]int, []string) {
t.Helper()
codes := make([]int, sharedServerIfRevisionRacers)
outputs := make([]string, sharedServerIfRevisionRacers)
var wg sync.WaitGroup
for i := range sharedServerIfRevisionRacers {
wg.Add(1)
go func(i int) {
defer wg.Done()
cmd := exec.CommandContext(ctx, bdBinary, argsFor(i)...)
cmd.Dir = projectDir
cmd.Env = baseEnv
out, err := cmd.CombinedOutput()
outputs[i] = string(out)
switch {
case err == nil:
codes[i] = 0
default:
var ee *exec.ExitError
if !errors.As(err, &ee) {
t.Errorf("racer %d: bd %v failed without an exit code: %v\n%s", i, argsFor(i), err, out)
return
}
codes[i] = ee.ExitCode()
}
}(i)
}
wg.Wait()
return codes, outputs
}
// assertSingleWinner checks that exactly one racer exited 0 and every
// other racer exited ExitGuardMismatch, and returns the winner's index.
assertSingleWinner := func(t *testing.T, codes []int, outputs []string) int {
t.Helper()
winner := -1
wins, guarded, other := 0, 0, 0
for i, code := range codes {
switch code {
case 0:
wins++
winner = i
case ExitGuardMismatch:
guarded++
default:
other++
}
}
if wins != 1 || guarded != sharedServerIfRevisionRacers-1 || other != 0 {
t.Fatalf("single-winner race: got %d exit-0 winners, %d exit-%d guard refusals, %d other; want exactly 1 and %d\ncodes: %v\noutputs: %v",
wins, guarded, ExitGuardMismatch, other, sharedServerIfRevisionRacers-1, codes, outputs)
}
return winner
}
t.Run("update", func(t *testing.T) {
out, err := ssExec(ctx, bdBinary, projectDir, baseEnv, "create", "Shared-server update race", "--json")
if err != nil {
t.Fatalf("create: %s: %v", out, err)
}
id, err := ssJSONField(out, "id")
if err != nil {
t.Fatalf("create: %v", err)
}
_, rev0 := ssShowRevision(t, id)
codes, outputs := race(t, func(i int) []string {
return []string{"update", id, "--if-revision", rev0, "--spec-id", fmt.Sprintf("racer-%d", i)}
})
winner := assertSingleWinner(t, codes, outputs)
after, rev1 := ssShowRevision(t, id)
if rev1 == rev0 {
t.Errorf("update race: revision did not advance past %s", rev0)
}
wantSpecID := fmt.Sprintf("racer-%d", winner)
if got, _ := after["spec_id"].(string); got != wantSpecID {
t.Errorf("update race: spec_id = %q, want %q (racer %d's value)", got, wantSpecID, winner)
}
})
t.Run("close", func(t *testing.T) {
out, err := ssExec(ctx, bdBinary, projectDir, baseEnv, "create", "Shared-server close race", "--json")
if err != nil {
t.Fatalf("create: %s: %v", out, err)
}
id, err := ssJSONField(out, "id")
if err != nil {
t.Fatalf("create: %v", err)
}
_, rev0 := ssShowRevision(t, id)
codes, outputs := race(t, func(i int) []string {
return []string{"close", id, "--if-revision", rev0, "--reason", fmt.Sprintf("racer-%d", i)}
})
winner := assertSingleWinner(t, codes, outputs)
after, rev1 := ssShowRevision(t, id)
if rev1 == rev0 {
t.Errorf("close race: revision did not advance past %s", rev0)
}
if got, _ := after["status"].(string); got != "closed" {
t.Errorf("close race: status = %q, want \"closed\"", got)
}
wantReason := fmt.Sprintf("racer-%d", winner)
if got, _ := after["close_reason"].(string); got != wantReason {
t.Errorf("close race: close_reason = %q, want %q (racer %d's value)", got, wantReason, winner)
}
})
t.Run("assign", func(t *testing.T) {
out, err := ssExec(ctx, bdBinary, projectDir, baseEnv, "create", "Shared-server assign race", "--json")
if err != nil {
t.Fatalf("create: %s: %v", out, err)
}
id, err := ssJSONField(out, "id")
if err != nil {
t.Fatalf("create: %v", err)
}
_, rev0 := ssShowRevision(t, id)
codes, outputs := race(t, func(i int) []string {
return []string{"assign", id, fmt.Sprintf("racer%d", i), "--if-revision", rev0}
})
winner := assertSingleWinner(t, codes, outputs)
after, rev1 := ssShowRevision(t, id)
if rev1 == rev0 {
t.Errorf("assign race: revision did not advance past %s", rev0)
}
wantAssignee := fmt.Sprintf("racer%d", winner)
if got, _ := after["assignee"].(string); got != wantAssignee {
t.Errorf("assign race: assignee = %q, want %q (racer %d's value)", got, wantAssignee, winner)
}
})
}

View file

@ -1699,6 +1699,42 @@ func TestParityCloseAlreadyClosedIsIdempotentSuccess(t *testing.T) {
}
}
// TestParityCloseIfRevisionRejectsContinueSuggestNextClaimNext pins
// mc-zndi7.76 (gap 4 / mutant ML): --if-revision's single-id compare-and-swap
// bypass (cmd/bd/close.go:109-117) never looks at --continue, --suggest-next
// or --claim-next, so honoring any of them would silently drop what the
// caller asked for instead of reporting it. Each of the three flags is
// refused independently, before any write.
func TestParityCloseIfRevisionRejectsContinueSuggestNextClaimNext(t *testing.T) {
for _, flag := range []string{"continue", "suggest-next", "claim-next"} {
t.Run(flag, func(t *testing.T) {
env := newParityEnv(t)
seeded := env.seed("test-clsifr-"+flag, "Guarded close vs "+flag, nil)
rev := env.get(seeded.ID).RowVersion
env.setFlags(closeCmd, map[string]string{
"if-revision": fmt.Sprintf("%d", rev),
flag: "true",
})
res := env.run(closeCmd, seeded.ID)
if res.exitCode != 1 {
t.Fatalf("exit = %d, want 1\nstderr:\n%s", res.exitCode, res.stderr)
}
const want = "Error: --if-revision does not support --continue, --suggest-next, or --claim-next\n"
if res.stderr != want {
t.Errorf("stderr = %q, want %q", res.stderr, want)
}
if got := env.get(seeded.ID); got.Status == types.StatusClosed {
t.Error("the issue must not have been closed")
}
if got := env.store.mutations(); len(got) != 0 {
t.Errorf("store mutations = %v, want none (rejected pre-write)", got)
}
})
}
}
// ===== bd reopen =====
// TestParityReopenJSONShape pins `bd reopen --json`: a JSON ARRAY of re-read

View file

@ -54,6 +54,15 @@ func TestLifecycleUpdateContract(t *testing.T) {
t.Run("ExpectedVersionSingleWinnerUnderConcurrency", func(t *testing.T) {
conformance.RunLifecycleUpdateExpectedVersionSingleWinnerUnderConcurrency(t, ctx, fixture)
})
t.Run("ExpectedVersionSingleWinnerWithDisjointColumnsUnderConcurrency", func(t *testing.T) {
conformance.RunLifecycleUpdateExpectedVersionSingleWinnerWithDisjointColumnsUnderConcurrency(t, ctx, fixture)
})
t.Run("CloseExpectedVersionSingleWinnerUnderConcurrency", func(t *testing.T) {
conformance.RunLifecycleCloseExpectedVersionSingleWinnerUnderConcurrency(t, ctx, fixture)
})
t.Run("ExpectedVersionSingleWinnerAcrossUpdateAndCloseUnderConcurrency", func(t *testing.T) {
conformance.RunLifecycleUpdateExpectedVersionSingleWinnerAcrossUpdateAndCloseUnderConcurrency(t, ctx, fixture)
})
t.Run("ConditionalGuardAcceptsRespelledAssignee", func(t *testing.T) {
conformance.RunLifecycleUpdateConditionalGuardAcceptsRespelledAssignee(t, ctx, fixture)
})

View file

@ -55,6 +55,15 @@ func TestLifecycleUpdateContract(t *testing.T) {
t.Run("ExpectedVersionSingleWinnerUnderConcurrency", func(t *testing.T) {
conformance.RunLifecycleUpdateExpectedVersionSingleWinnerUnderConcurrency(t, ctx, fixture)
})
t.Run("ExpectedVersionSingleWinnerWithDisjointColumnsUnderConcurrency", func(t *testing.T) {
conformance.RunLifecycleUpdateExpectedVersionSingleWinnerWithDisjointColumnsUnderConcurrency(t, ctx, fixture)
})
t.Run("CloseExpectedVersionSingleWinnerUnderConcurrency", func(t *testing.T) {
conformance.RunLifecycleCloseExpectedVersionSingleWinnerUnderConcurrency(t, ctx, fixture)
})
t.Run("ExpectedVersionSingleWinnerAcrossUpdateAndCloseUnderConcurrency", func(t *testing.T) {
conformance.RunLifecycleUpdateExpectedVersionSingleWinnerAcrossUpdateAndCloseUnderConcurrency(t, ctx, fixture)
})
t.Run("ConditionalGuardAcceptsRespelledAssignee", func(t *testing.T) {
conformance.RunLifecycleUpdateConditionalGuardAcceptsRespelledAssignee(t, ctx, fixture)
})

View file

@ -56,6 +56,15 @@ func TestLifecycleUpdateContract(t *testing.T) {
t.Run("ExpectedVersionSingleWinnerUnderConcurrency", func(t *testing.T) {
conformance.RunLifecycleUpdateExpectedVersionSingleWinnerUnderConcurrency(t, ctx, fixture)
})
t.Run("ExpectedVersionSingleWinnerWithDisjointColumnsUnderConcurrency", func(t *testing.T) {
conformance.RunLifecycleUpdateExpectedVersionSingleWinnerWithDisjointColumnsUnderConcurrency(t, ctx, fixture)
})
t.Run("CloseExpectedVersionSingleWinnerUnderConcurrency", func(t *testing.T) {
conformance.RunLifecycleCloseExpectedVersionSingleWinnerUnderConcurrency(t, ctx, fixture)
})
t.Run("ExpectedVersionSingleWinnerAcrossUpdateAndCloseUnderConcurrency", func(t *testing.T) {
conformance.RunLifecycleUpdateExpectedVersionSingleWinnerAcrossUpdateAndCloseUnderConcurrency(t, ctx, fixture)
})
t.Run("ConditionalGuardAcceptsRespelledAssignee", func(t *testing.T) {
conformance.RunLifecycleUpdateConditionalGuardAcceptsRespelledAssignee(t, ctx, fixture)
})