beads/internal/storage/issueops/execution_test.go
Athos Bernardes ad2346e821
fix(bd): canonicalize remaining actor==assignee comparisons in claim/heartbeat CAS (ga-v2k49) (#5479)
* fix(bd): canonicalize remaining actor==assignee comparisons in claim/heartbeat CAS (ga-v2k49)

Follow-up to ga-wzl83/ga-5ksp5, closing the gap steveyegge's #5438 review
enumerated: claim CAS + idempotent re-claim (issueops/claim.go), the
domain/uow twin claim path (domain/issue.go's claim(), domain/db/issue.go's
Claim — a documented "must stay in lockstep" dual), and the lease heartbeat
holder check (issueops/lease.go). --expected-assignee (update_cas.go,
domain/issue.go's ApplyUpdate) is already covered by #5439 — verified against
its live diff, not duplicated here.

claim.go and its domain/db twin needed the same row_lock-based CAS
restructuring #5439 used for unclaim.go, not a naive comparison swap (per the
reviewer's own warning): assignee-claimability is now judged in Go against
the pre-image, and the UPDATE is CASed on row_lock instead of re-checking a
spelling-sensitive assignee predicate in SQL — skipping the write entirely
when the actor never had a chance, mirroring unclaim.go's ownership precheck.

lease.go's primary UPDATE predicate deliberately stays verbatim SQL (heartbeat
is the hottest write in the fleet; fixing it would cost a pre-read on every
call) — the disambiguation/re-arm fallback is fixed instead, which is what
the caller actually observes and is enough to self-heal a spelling mismatch
under the caller's current spelling for the next heartbeat.

Also fixes public_claim.go's ExecuteClaim, a site not in the original
enumeration: its own "was this a real change" check had the identical
verbatim-comparison bug, caught by a new test during this fix rather than by
static reading — it would otherwise have staged a phantom mutation for an
idempotent re-claim spelled under a different layer's separator convention.

Tests: new coverage at the issueops mock layer (idempotent reclaim across
spelling + a control case for a genuinely different identity, both from
canonicalActor's own doc-comment example pair) and a real embedded-Dolt
integration test for the heartbeat holder fix, including that it re-arms the
lease under the caller's current spelling. Full existing suites for
issueops, domain, domain/db, embeddeddolt (isolated, 706s), and uow's
claim/lease/heartbeat subset (75s) all pass; go vet and golangci-lint clean.

* fix(bd): canonicalize the update/batch claim-changed checks steveyegge's re-review found (ga-v2k49)

steveyegge's #5479 re-review (2026-08-09T18:49:30Z) found the actor==assignee
sweep was not exhaustive: three residual sites of the identical shape, all in
the update path rather than the claim path already fixed —

  - issueops/execution.go's ExecuteUpdate (the --claim branch, line 170)
  - uow/issue_operations.go's issueOperations.Update (line 231)
  - uow/batch_applier.go's uowApplyRun.runUpdate, the batch/plan twin (line 278)

Each staged a phantom issues+events mutation (or history entry) for a
`bd update --claim` that ClaimIssueInTx/domain-db's Claim already treated as
an idempotent no-op under a respelled identity. Fixed with the same
actorMatches/ActorMatches conversion as the sites already fixed, each pinned
by a new test in the established shape (idempotent-across-spelling positive
case + a not-a-no-op control using canonicalActor's own documented pair).

execution.go's fix extracts claimAdvancedTheRow, matching this file's own
established test shape (ManageLeaseOnUpdate) rather than inventing a new
sqlmock harness for ExecuteUpdate, which no existing test exercises directly.
The uow fake claim contract (claimContractIssues.ApplyUpdate) needed a
fidelity fix first: it unconditionally overwrote assignee/status, so a
cross-spelling reclaim looked like a real mutation via semanticIssueEqual for
a reason that had nothing to do with claimChanged. Fixed the fake to mirror
domain/db's real idempotent-preserves-spelling behavior, then the actual fix.

Also addresses the review's four non-blocking items:

  - Documents the four sites that stay deliberately verbatim: the pool-alias
    membership tests in claim.go (x2) and aggregate.go (a pool alias is a
    claim.pools config string, not a respelled identity — canonicalizing it
    could only blur two distinct pools together), and update.go's sameClaim
    (a respelled self-reassign genuinely rewrites the row, so clearing the
    lease is defensible — just wasn't documented as intentional).
  - Adds an in-code comment on lease.go's reconcile join explaining why it
    can't canonicalize (a SQL equality join can't run Go-side
    canonicalization) and that the gap is bounded/self-healing.
  - Corrects runUpdate's own doc comment, which claimed it's "reached from"
    issueOperations.Update "so the two cannot drift" — not accurate (the two
    share no call path, confirmed by grep for every caller of runUpdate),
    and this exact bug is proof they can. Left a false guarantee in place
    next to code that just needed independent fixing would be worse than no
    comment at all.
  - Adds RunClaimerReclaimsAcrossSpellingWithoutWriting to the Claimer
    conformance contract, wired into all three backends (dolt/embeddeddolt/
    uow) plus the completeness guard: the conformance-level pin for
    domain/db's Claim restructure, until now proven only at the mock layer
    (issue_operations_claim_contract_test.go's fakes). Passes against
    embeddeddolt and uow locally against real backends; dolt skips in this
    sandbox (no Docker) same as its siblings.

Rebased onto current main first (#5439 merged in the meantime), which
cleanly dropped the three already-upstream commits and left this as one
incremental commit.

Verification: go build/vet clean; golangci-lint clean on every touched
package (3 pre-existing gosec findings in untouched files, unrelated to this
diff); issueops, domain, domain/db, backend/conformance suites pass; full
embeddeddolt and uow suites pass against real backends (embeddeddolt alone:
~600s, right at Go's default 10m test timeout on a loaded machine when run
concurrently with the uow suite — re-verified clean in isolation with an
explicit longer timeout).

---------

Co-authored-by: Steve Yegge <steve.yegge@gmail.com>
2026-08-09 17:19:00 -07:00

64 lines
2.6 KiB
Go

package issueops
import (
"testing"
"github.com/steveyegge/beads/internal/types"
)
// TestClaimAdvancedTheRow pins ga-v2k49 (steveyegge's #5479 re-review, the
// blocking fix-class): ExecuteUpdate's --claim path decided whether to stage
// a mutation by comparing claimed.OldIssue.Assignee to attempt.Actor
// verbatim, so a holder re-claiming under a respelled identity (dotted vs
// sanitized separator, ga-wzl83's repro shape) staged a phantom issues+events
// mutation for a CAS that ClaimIssueInTx itself correctly treated as a no-op.
// claimAdvancedTheRow is the extracted decision, tested directly the same
// way ManageLeaseOnUpdate is above: no SQL involved, ClaimIssueInTx's own
// CAS semantics are pinned separately by public_claim_test.go.
func TestClaimAdvancedTheRow(t *testing.T) {
tests := []struct {
name string
oldStatus types.Status
oldAssignee string
actor string
want bool
}{
{
name: "fresh claim from open and unassigned", oldStatus: types.StatusOpen, oldAssignee: "",
actor: "alice", want: true,
},
{
name: "idempotent reclaim by the holder, same spelling", oldStatus: types.StatusInProgress,
oldAssignee: "alice", actor: "alice", want: false,
},
{
// ga-v2k49's own repro shape (ga-wzl83): the same identity, spelled
// under two layers' different separator conventions.
name: "idempotent reclaim by the holder, cross-spelling", oldStatus: types.StatusInProgress,
oldAssignee: "gastown__mayor", actor: "gastown.mayor", want: false,
},
{
// Not-a-no-op control (canonicalActor's own doc-comment pair): a
// canonicalization broad enough to match everyone would pass the
// case above for the wrong reason.
name: "genuinely different identity despite similar separator style", oldStatus: types.StatusInProgress,
oldAssignee: "gastown.mayor", actor: "gastown.dog-3", want: true,
},
{
// A pool-alias claim (ClaimPoolAliasesInTx) can win the CAS while
// already in_progress: the row transitions from the pool alias to
// the real claimant, which is a genuine mutation despite the status
// staying in_progress throughout.
name: "pool-alias claim transitions to the real claimant", oldStatus: types.StatusInProgress,
oldAssignee: "pool:dogs", actor: "alice", want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
claimed := &ClaimResult{OldIssue: &types.Issue{Status: tt.oldStatus, Assignee: tt.oldAssignee}}
if got := claimAdvancedTheRow(claimed, tt.actor); got != tt.want {
t.Errorf("claimAdvancedTheRow() = %v, want %v", got, tt.want)
}
})
}
}