Beads had two implementations of most issue verbs: one for the direct route
(the CLI talking to a store it opened itself) and one for the proxied-server
route (the CLI talking through a unit of work over a running Dolt server).
They drifted — different commit messages, different transaction boundaries,
different refusals, occasionally different answers.
Seven role interfaces now live in the leaf package issueops: Lifecycle,
Reader, ReadyClaimer, BatchCloser, DependencyEditor, Commenter and Relations.
Each is reached only through a store accessor, never a constructor, because
the accessor is where the hook and telemetry decorators add their layers.
Three implementations back every role — dolt, embeddeddolt, and the
unit-of-work provider the proxied route uses. Both routes call the same role
for close, dep add/remove/list, comment, ready --claim and update.
A depguard rule (httpapi-domain-boundary) denies internal/storage/domain from
internal/httpapi, so a handler cannot reach uw.IssueUseCase() past the roles
and open a second front door one lint-clean commit at a time.
Behaviour users will notice, all in the CHANGELOG:
- bd close a b c is one Dolt commit, not three, and --claim-next moved
inside that transaction so it fires only when something landed
- the open-children guard moved into the transaction and its hand-rolled
cmd/bd copy is gone, removing a read-then-write window that made
bd close <child> <parent> depend on argument order
- bd dep add --file honours deferred version commit
- embedded bd comment writes the role's message and author
- dep verb commit messages are the role's on the direct route
Wisp-sourced dependency edges are restored. Moving bd dep add onto the role
had pinned an edge's SOURCE to the issues plane, and the two routes did not
even fail alike: direct refused with "issue <id> not found", proxied threw an
fk_dep_issue violation. The comment justifying the pin claimed the issues
plane was "the only one its unit-of-work sibling can reach" — that was false;
domain.DependencyUseCase declared a full set of wisp twins with no production
caller. All three implementations now route per edge by the edge's own
source. A mixed request stays one transaction: parent-child-first ordering
and the whole-graph cycle gate span both planes, and a refusal rolls back
both. Behind the role, three bulk add verbs collapsed into one source-routed
AddDependencies, so no plane-pinned variant is left to reach for by mistake.
Six Provenance fields with no production caller were deleted rather than
wired — CloseRequest, CloseBatchRequest, ClaimNextRequest, AddCommentRequest,
AddDependenciesRequest, RemoveDependencyRequest. Each role already composes an
id-bearing message, so a caller could only restate the default and make it
unreachable from production. Two are not caller-settable even in principle:
CloseBatchCommitMessage names what LANDED and ClaimNextCommitMessage names the
id the claim WON. UpdateRequest and ReopenRequest keep theirs; commit messages
users see are unchanged.
Verified: build, vet and gofmt clean; golangci-lint 0 issues; protocol corpus
byte-identical under BEADS_PROTOCOL_REQUIRE_DOLT=1; all twelve
DependencyEditor conformance wirings green (four contract functions against
each implementation); TestEmbeddedDepAddWispSource runs bd dep add <wisp-id>
at the front door and was demonstrated RED against the pinned routing first.
Three internal/storage/dolt failures reproduce on pristine origin/main.
Known gaps are enumerated in the PR description rather than left to be found:
comment listing is on no role, bd dep tree and bd dep cycles have no role,
bd create and bd reopen are on Lifecycle only on the direct route, per-edge
line attribution is lost in bd dep add --file, multi-store bd close
--claim-next rides only the first store's batch, and the is_blocked staging gap
(bd-2y9ke, predates this change). Seven of the eight role interfaces now have a
shared conformance contract in backend/conformance, run at all three
implementations; main's Claimer, added in parallel by #5334, is the eighth and
has none yet.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
143 lines
5.7 KiB
Go
143 lines
5.7 KiB
Go
package dolt
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
|
|
"github.com/steveyegge/beads/internal/storage"
|
|
storageissueops "github.com/steveyegge/beads/internal/storage/issueops"
|
|
"github.com/steveyegge/beads/internal/types"
|
|
"github.com/steveyegge/beads/issueops"
|
|
)
|
|
|
|
// IssueLifecycle returns the guarded issue-lifecycle surface for this store.
|
|
func (s *DoltStore) IssueLifecycle() (issueops.Lifecycle, error) {
|
|
return NewIssueOperations(s)
|
|
}
|
|
|
|
// NewIssueOperations returns guarded issue operations backed by store.
|
|
func NewIssueOperations(store *DoltStore) (issueops.Lifecycle, error) {
|
|
if store == nil {
|
|
return nil, &storage.ErrUnsupported{Op: "NewIssueOperations", Backend: "nil"}
|
|
}
|
|
return &issueOperations{store: store}, nil
|
|
}
|
|
|
|
type issueOperations struct{ store *DoltStore }
|
|
|
|
// updateCommitMessage names the updated issue in the Dolt commit message.
|
|
func updateCommitMessage(issueID string) string {
|
|
if issueID == "" {
|
|
return "bd: update issue"
|
|
}
|
|
return "bd: update " + issueID
|
|
}
|
|
|
|
func (o *issueOperations) Create(ctx context.Context, request issueops.CreateRequest) (issueops.CreateResult, error) {
|
|
snapshot := storageissueops.CloneCreateRequest(request)
|
|
var result issueops.CreateResult
|
|
err := o.store.runIssueOperationTx(ctx, "bd: create issue", func(tx *sql.Tx) (storageissueops.ChangedTables, error) {
|
|
var err error
|
|
var tables storageissueops.ChangedTables
|
|
result, tables, err = storageissueops.ExecuteCreate(ctx, tx, snapshot)
|
|
return tables, err
|
|
})
|
|
return result, err
|
|
}
|
|
|
|
func (o *issueOperations) Update(ctx context.Context, request issueops.UpdateRequest) (issueops.UpdateResult, error) {
|
|
snapshot := storageissueops.CloneUpdateRequest(request)
|
|
var result issueops.UpdateResult
|
|
err := o.verifiedUpdate(ctx, snapshot, func() error {
|
|
// The message names the issue because that is the one `bd dolt log`
|
|
// affordance callers actually grep, and it is what the CLI's own
|
|
// per-command commit wrote before updates moved onto this path.
|
|
return o.store.runIssueOperationTx(ctx, storageissueops.HistoryEntry(snapshot.Provenance, updateCommitMessage(snapshot.IssueID)), func(tx *sql.Tx) (storageissueops.ChangedTables, error) {
|
|
var err error
|
|
var tables storageissueops.ChangedTables
|
|
result, tables, err = storageissueops.ExecuteUpdate(ctx, tx, snapshot)
|
|
return tables, err
|
|
})
|
|
})
|
|
return result, err
|
|
}
|
|
|
|
// verifiedUpdate runs a facade update under claim-family verify-after-write
|
|
// (bd-zccb9) when the request writes coordination state, and unwrapped
|
|
// otherwise. The facade reaches the same writes as ClaimIssue and
|
|
// UpdateIssueWithOptions, so under a degraded server its exit status is no more
|
|
// trustworthy than theirs. Replay is safe: the claim CAS and the compare-and-set
|
|
// guards are re-checked inside the replayed transaction, so a racing writer
|
|
// makes the replay refuse rather than clobber.
|
|
func (o *issueOperations) verifiedUpdate(ctx context.Context, request issueops.UpdateRequest, write func() error) error {
|
|
post, verify := updateClaimPostcondition(request)
|
|
if !verify {
|
|
return write()
|
|
}
|
|
return o.store.verifiedClaimWrite(ctx, request.IssueID, post, write)
|
|
}
|
|
|
|
// updateClaimPostcondition derives the row state a facade update must leave
|
|
// behind to count as applied, and whether the update is claim-family at all. A
|
|
// claim always is; an ordinary update is only when a compare-and-set guard
|
|
// authorizes a write to assignee or status. The postcondition describes the
|
|
// state the request intends, so a patch that overrides the claim's own
|
|
// assignee or status is honored rather than read as a lost write.
|
|
//
|
|
// A request that also moves the issue across the persistence boundary is
|
|
// exempt: the row can legitimately leave the issues table mid-write, which the
|
|
// re-read would report as an unverifiable claim.
|
|
func updateClaimPostcondition(request issueops.UpdateRequest) (claimPostcondition, bool) {
|
|
if request.Patch.Persistence.Set {
|
|
return claimPostcondition{}, false
|
|
}
|
|
updates := map[string]interface{}{}
|
|
if request.Patch.Assignee.Set {
|
|
updates["assignee"] = request.Patch.Assignee.Value
|
|
}
|
|
if request.Patch.Status.Set {
|
|
updates["status"] = string(request.Patch.Status.Value)
|
|
}
|
|
if request.Claim {
|
|
assignee, status := request.Actor, types.StatusInProgress
|
|
if request.Patch.Assignee.Set {
|
|
assignee = request.Patch.Assignee.Value
|
|
}
|
|
if request.Patch.Status.Set {
|
|
status = request.Patch.Status.Value
|
|
}
|
|
return claimedAs(assignee, status), true
|
|
}
|
|
opts := storage.UpdateIssueOptions{ExpectedAssignee: request.ExpectedAssignee}
|
|
if request.ExpectedStatus != nil {
|
|
expected := string(*request.ExpectedStatus)
|
|
opts.ExpectedStatus = &expected
|
|
}
|
|
return guardedUpdatePostcondition(opts, updates)
|
|
}
|
|
|
|
func (o *issueOperations) Close(ctx context.Context, request issueops.CloseRequest) (issueops.CloseResult, error) {
|
|
snapshot := storageissueops.CloneCloseRequest(request)
|
|
var result issueops.CloseResult
|
|
err := o.store.runIssueOperationTx(ctx, "bd: close issue", func(tx *sql.Tx) (storageissueops.ChangedTables, error) {
|
|
var err error
|
|
var tables storageissueops.ChangedTables
|
|
result, tables, err = storageissueops.ExecuteClose(ctx, tx, snapshot)
|
|
return tables, err
|
|
})
|
|
return result, err
|
|
}
|
|
|
|
func (o *issueOperations) Reopen(ctx context.Context, request issueops.ReopenRequest) (issueops.ReopenResult, error) {
|
|
snapshot := storageissueops.CloneReopenRequest(request)
|
|
var result issueops.ReopenResult
|
|
err := o.store.runIssueOperationTx(ctx, storageissueops.HistoryEntry(snapshot.Provenance, "bd: reopen issue"), func(tx *sql.Tx) (storageissueops.ChangedTables, error) {
|
|
var err error
|
|
var tables storageissueops.ChangedTables
|
|
result, tables, err = storageissueops.ExecuteReopen(ctx, tx, snapshot)
|
|
return tables, err
|
|
})
|
|
return result, err
|
|
}
|
|
|
|
var _ issueops.Lifecycle = (*issueOperations)(nil)
|