beads/cmd/bd/dep_bulk_role_test.go
Julian Knutsen d801ec43dc
feat(issueops): put every issue capability behind one library surface
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>
2026-08-04 12:27:56 -07:00

186 lines
6.3 KiB
Go

//go:build cgo
package main
import (
"context"
"errors"
"path/filepath"
"testing"
"time"
"github.com/steveyegge/beads/internal/storage"
"github.com/steveyegge/beads/internal/storage/dolt"
"github.com/steveyegge/beads/internal/storage/domain"
"github.com/steveyegge/beads/internal/types"
"github.com/steveyegge/beads/issueops"
)
// The bulk `bd dep add --file` route asserts its edges through the
// DependencyEditor role, so the invariants the hand-rolled bulk transaction
// used to carry are pinned here against the role the route now calls — entered
// through the store's own accessor, exactly as addDependencyEdgesDirect enters
// it.
// bd-wg7ve: parent-child edges are applied before blocking ones regardless of
// request order, so the complete planned hierarchy is visible when a blocking
// edge is validated against it.
//
// The ordering is what decides the ANSWER here. Applied hierarchy-first, the
// grandparent is an ancestor of the child by the time `child blocks grand` is
// checked, so the whole request is refused and nothing is written. Applied in
// request order, that blocking edge lands before any hierarchy exists, nothing
// notices, and all three edges commit.
func TestBulkDepAddRoleAppliesHierarchyBeforeBlockingEdges(t *testing.T) {
t.Parallel()
tmpDir := t.TempDir()
s := newTestStore(t, filepath.Join(tmpDir, ".beads", "beads.db"))
ctx := context.Background()
const grand, parent, child = "test-bulk-order-grand", "test-bulk-order-parent", "test-bulk-order-child"
seedBulkRoleIssues(ctx, t, s, grand, parent, child)
editor, err := s.DependencyEditor()
if err != nil {
t.Fatalf("DependencyEditor(): %v", err)
}
_, err = editor.AddDependencies(ctx, issueops.AddDependenciesRequest{
Actor: "tester",
Edges: []issueops.DependencyEdge{
{IssueID: child, DependsOnID: grand, Type: types.DepBlocks},
{IssueID: child, DependsOnID: parent, Type: types.DepParentChild},
{IssueID: parent, DependsOnID: grand, Type: types.DepParentChild},
},
})
var conflict *domain.DependencyHierarchyConflictError
if !errors.As(err, &conflict) {
t.Fatalf("error = %v, want *domain.DependencyHierarchyConflictError: the hierarchy is applied first", err)
}
if !conflict.BlockerIsAncestor {
t.Fatalf("conflict = %#v, want the blocker reported as the child's ancestor", conflict)
}
for _, id := range []string{child, parent} {
deps, err := s.GetDependencyRecords(ctx, id)
if err != nil {
t.Fatalf("GetDependencyRecords(%s): %v", id, err)
}
if len(deps) != 0 {
t.Fatalf("%s edges after a refused request = %#v, want none: the request is all-or-nothing", id, deps)
}
}
}
// bd-6dnrw.8: with the per-edge probe off the whole-graph gate is the check
// that actually holds the invariant. Its refusal must roll the request back —
// no cycle may ever commit — and must render the message the bulk route has
// always printed while still matching the typed sentinel.
func TestBulkDepAddRoleWholeGraphGateRollsBack(t *testing.T) {
t.Parallel()
tmpDir := t.TempDir()
s := newTestStore(t, filepath.Join(tmpDir, ".beads", "beads.db"))
ctx := context.Background()
const a, b = "test-cyc-a", "test-cyc-b"
seedBulkRoleIssues(ctx, t, s, a, b)
if err := s.AddDependency(ctx, &types.Dependency{
IssueID: a, DependsOnID: b, Type: types.DepBlocks,
}, "test"); err != nil {
t.Fatalf("seed dependency: %v", err)
}
editor, err := s.DependencyEditor()
if err != nil {
t.Fatalf("DependencyEditor(): %v", err)
}
_, err = editor.AddDependencies(ctx, issueops.AddDependenciesRequest{
Actor: "tester",
SkipPerEdgeCycleCheck: true,
Edges: []issueops.DependencyEdge{
{IssueID: b, DependsOnID: a, Type: types.DepBlocks},
},
})
if !errors.Is(err, domain.ErrDependencyCycle) {
t.Fatalf("final gate error = %v, want errors.Is domain.ErrDependencyCycle", err)
}
// Typing must not alter the rendered text: no sentinel string appended.
const wantMsg = "dependency cycle would be created: test-cyc-b → test-cyc-a → test-cyc-b (no edges added; run 'bd dep cycles' for analysis)"
if err.Error() != wantMsg {
t.Fatalf("final gate message = %q, want byte-identical %q", err.Error(), wantMsg)
}
deps, err := s.GetDependencyRecords(ctx, b)
if err != nil {
t.Fatalf("GetDependencyRecords: %v", err)
}
if len(deps) != 0 {
t.Fatalf("cycle edge was committed despite gate: %#v", deps)
}
}
// bd-578h9.9: a pre-existing committed cycle touching an endpoint of the
// request must not block unrelated bulk wiring — only cycles that traverse a
// new edge gate the commit.
func TestBulkDepAddRoleIgnoresPreexistingCycleAtEndpoint(t *testing.T) {
t.Parallel()
tmpDir := t.TempDir()
s := newTestStore(t, filepath.Join(tmpDir, ".beads", "beads.db"))
ctx := context.Background()
const a, b, c = "test-pre-a", "test-pre-b", "test-pre-c"
seedBulkRoleIssues(ctx, t, s, a, b, c)
// Commit the cycle a <-> b first (SkipCycleCheck stands in for legacy data
// that predates cycle validation).
if err := s.RunInTransaction(ctx, "test: seed cycle", func(tx storage.Transaction) error {
for _, pair := range [][2]string{{a, b}, {b, a}} {
dep := &types.Dependency{IssueID: pair[0], DependsOnID: pair[1], Type: types.DepBlocks}
if err := tx.AddDependencyWithOptions(ctx, dep, "test", storage.DependencyAddOptions{SkipCycleCheck: true}); err != nil {
return err
}
}
return nil
}); err != nil {
t.Fatalf("seed cycle: %v", err)
}
editor, err := s.DependencyEditor()
if err != nil {
t.Fatalf("DependencyEditor(): %v", err)
}
if _, err := editor.AddDependencies(ctx, issueops.AddDependenciesRequest{
Actor: "tester",
Edges: []issueops.DependencyEdge{
{IssueID: a, DependsOnID: c, Type: types.DepBlocks},
},
}); err != nil {
t.Fatalf("unrelated bulk edge was blocked: %v", err)
}
deps, err := s.GetDependencyRecords(ctx, a)
if err != nil {
t.Fatalf("GetDependencyRecords: %v", err)
}
var foundC bool
for _, dep := range deps {
if dep.DependsOnID == c {
foundC = true
}
}
if !foundC {
t.Fatalf("edge a -> c did not commit: %#v", deps)
}
}
func seedBulkRoleIssues(ctx context.Context, t *testing.T, s *dolt.DoltStore, ids ...string) {
t.Helper()
for _, id := range ids {
issue := &types.Issue{
ID: id, Title: id, Status: types.StatusOpen,
Priority: 1, IssueType: types.TypeTask, CreatedAt: time.Now(),
}
if err := s.CreateIssue(ctx, issue, "test"); err != nil {
t.Fatalf("create %s: %v", id, err)
}
}
}