beads/cmd/bd/update_conditional_proxied_test.go
Steve Yegge 6e8af8bf80
feat(update): conditional compare-and-set guards --if-assignee/--if-status (bd-wsqvw) (#5008)
* feat(update): conditional compare-and-set guards --if-assignee/--if-status (bd-wsqvw)

From wyvern's wheelhouse hostile-review (epic wy-mdi5h): the fleet's most
common bug class is check-then-act on bd assignee/status — read state,
then issue a blind 'bd update -a X', and lose to a racing writer in the
gap. Design: PROPOSAL-cas-conditional-update.md.

Adds --if-assignee/--if-status precondition flags to 'bd update'. When
present, the mutation applies only if the issue's current
assignee/status still equals the expected value — one atomic
transaction, nothing written on a mismatch, loud non-zero exit naming
actual vs expected. --if-assignee '' means "expected unassigned"
(Changed()-detected, the unclaim idiom). Covers the two transitions no
existing verb expresses: reassign X->Y (park) and claim-on-behalf with
a status guard (restore).

Per the reconciliation with #4911, the guards extend the existing
UpdateIssueOptions on UpdateIssueChecked (ExpectedAssignee /
ExpectedStatus, new issueops.CheckExpectedFieldsInTx beside
CheckVersionInTx, typed storage.ErrStatusMismatch beside the existing
ErrAssigneeMismatch) rather than minting a parallel interface method —
no Storage interface break, decorators pass through unchanged. Wired
through all three write stacks: DoltStore (server), EmbeddedDoltStore,
and the proxied-server domain.UpdateSpec/ApplyUpdate path. In Dolt
server mode a guarded update that writes assignee/status is
claim-family and resolves through verifiedClaimWrite (bd-zccb9) with a
guarded-update postcondition, so its exit code stays truthful under a
degraded server; replay after a verified rollback re-checks the guards
in the replayed transaction.

CLI rules: guards are mutually exclusive with --claim (its own CAS),
require a regular field update to ride on (label/parent edits run
outside the guarded transaction), and --if-status is validated against
the live status set so a typo fails fast instead of mismatching
forever. Guard failures flow through the finding-#10 exit contract on
both the classic and proxied paths.

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

* feat(update): exit code 13 distinguishes --if-assignee/--if-status guard mismatch from infra failure (bd-wsqvw)

A stale guard now exits ExitGuardMismatch (13) when every failure in the
run was a guard refusal — scripts can tell 'a racer won, skip gracefully'
from 'server down, retry/abort' without parsing prose. Mixed batches keep
the conservative exit 1. JSON failure entries carry guard_mismatch:true;
stderr keeps the greppable 'assignee mismatch'/'status mismatch' sentinels.
Requested by fly (wyvern) for the wy-mdi5h.3/.6 park/restore adopters.

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

* docs(claim): note guardedUpdatePostcondition attribution narrowness (lion review note 1, PR #5008)

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

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-07-24 09:11:15 -07:00

106 lines
4.1 KiB
Go

//go:build cgo
package main
import (
"errors"
"os/exec"
"strings"
"testing"
)
// bdProxiedUpdateFailCode runs a proxied "bd update" expecting failure and
// returns combined output plus the exit code, for asserting the
// ExitGuardMismatch contract on the proxied path.
func bdProxiedUpdateFailCode(t *testing.T, bd, dir string, args ...string) (string, int) {
t.Helper()
stdout, stderr, err := bdProxiedUpdateRaw(t, bd, dir, args...)
if err == nil {
t.Fatalf("bd update %s should have failed; got:\nstdout:\n%s\nstderr:\n%s",
strings.Join(args, " "), stdout, stderr)
}
var ee *exec.ExitError
if !errors.As(err, &ee) {
t.Fatalf("bd update %s failed without an exit code: %v", strings.Join(args, " "), err)
}
return stdout + stderr, ee.ExitCode()
}
// TestProxiedServerUpdateIfGuards proves the bd-wsqvw conditional-update
// guards hold on the proxied-server path: the guards ride domain.UpdateSpec
// into ApplyUpdate, where the guard read shares the unit of work's
// transaction. A stale guard is a terminal per-issue failure (loud, non-zero,
// nothing written — the finding-#10 exit contract), a matching guard applies,
// and `--if-assignee ""` guards on unassigned.
func TestProxiedServerUpdateIfGuards(t *testing.T) {
requireSharedProxiedServer(t)
t.Parallel()
bd := buildEmbeddedBD(t)
t.Run("guarded_reassign_wins_and_then_loses", func(t *testing.T) {
t.Parallel()
p := newSharedProxiedProject(t, bd, "ugp")
issue := bdProxiedCreate(t, bd, p.dir, "Park via proxy")
if out, err := bdProxiedRun(t, bd, p.dir, "update", issue.ID, "--assignee", "worker", "--status", "in_progress"); err != nil {
t.Fatalf("seed assign failed: %v\n%s", err, out)
}
// Matching guard applies.
if out, err := bdProxiedRun(t, bd, p.dir, "update", issue.ID, "--if-assignee", "worker", "--assignee", "mayor"); err != nil {
t.Fatalf("guarded reassign failed: %v\n%s", err, out)
}
got := bdProxiedShow(t, bd, p.dir, issue.ID)
if got.Assignee != "mayor" {
t.Fatalf("assignee = %q after guarded reassign, want mayor", got.Assignee)
}
// The same guard now loses with the distinct guard-mismatch exit code,
// names the actual holder, and writes nothing.
out, code := bdProxiedUpdateFailCode(t, bd, p.dir, issue.ID, "--if-assignee", "worker", "--assignee", "thief")
if code != ExitGuardMismatch {
t.Errorf("stale guard exit code = %d, want %d\n%s", code, ExitGuardMismatch, out)
}
if !strings.Contains(out, "mayor") {
t.Errorf("mismatch error should name the current holder mayor, got:\n%s", out)
}
got = bdProxiedShow(t, bd, p.dir, issue.ID)
if got.Assignee != "mayor" {
t.Errorf("lost guard clobbered the row: assignee = %q, want mayor", got.Assignee)
}
})
t.Run("claim_on_behalf_guards_on_unassigned", func(t *testing.T) {
t.Parallel()
p := newSharedProxiedProject(t, bd, "ugr")
issue := bdProxiedCreate(t, bd, p.dir, "Restore via proxy")
if out, err := bdProxiedRun(t, bd, p.dir, "update", issue.ID,
"--if-assignee", "", "--if-status", "open", "--assignee", "owner", "--status", "in_progress"); err != nil {
t.Fatalf("claim-on-behalf failed: %v\n%s", err, out)
}
got := bdProxiedShow(t, bd, p.dir, issue.ID)
if got.Assignee != "owner" {
t.Fatalf("assignee = %q after claim-on-behalf, want owner", got.Assignee)
}
// Second restore loses: no longer unassigned.
out, code := bdProxiedUpdateFailCode(t, bd, p.dir, issue.ID,
"--if-assignee", "", "--if-status", "open", "--assignee", "other", "--status", "in_progress")
if code != ExitGuardMismatch {
t.Errorf("lost restore exit code = %d, want %d\n%s", code, ExitGuardMismatch, out)
}
if !strings.Contains(out, "owner") {
t.Errorf("mismatch error should name the holder owner, got:\n%s", out)
}
})
t.Run("guards_with_claim_rejected", func(t *testing.T) {
t.Parallel()
p := newSharedProxiedProject(t, bd, "ugx")
issue := bdProxiedCreate(t, bd, p.dir, "Flag combo via proxy")
out := bdProxiedUpdateFail(t, bd, p.dir, issue.ID, "--if-assignee", "", "--claim")
if !strings.Contains(out, "--claim") {
t.Errorf("expected the --claim exclusion in the error, got:\n%s", out)
}
})
}