beads/internal/storage/dolt/dependency_editor_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

159 lines
6.2 KiB
Go

package dolt
import (
"testing"
"github.com/steveyegge/beads/internal/types"
publicops "github.com/steveyegge/beads/issueops"
)
// What is left here is the DependencyEditor's PERSISTENCE tier: how a history
// entry lands and what it says. The role's semantics — result shape,
// idempotency verdicts, the typed refusals, all-or-nothing, and the number of
// history entries a call records — moved to
// conformance.RunDependencyEditor*, which runs on all three backends.
//
// These stay dolt-only on purpose. The commit-message spellings are
// single-sourced in internal/storage/issueops, so one backend pinning a
// spelling is enough and re-pinning it three times is duplication; the staging
// assertions read dolt_status and plant a dirty working set, and the working
// set is not a caller-visible concept on the unit-of-work route.
// TestDoltStoreDependencyEditorBatchNamesItsHistoryEntry pins the count
// spelling and the staging half: N edges commit as one entry that names the
// count, and the table it staged is clean afterwards.
func TestDoltStoreDependencyEditorBatchNamesItsHistoryEntry(t *testing.T) {
store, cleanup := setupTestStore(t)
defer cleanup()
ctx, cancel := testContext(t)
defer cancel()
seedIssues(ctx, t, store, "test-dep-add-a", "test-dep-add-b", "test-dep-add-c")
editor, err := store.DependencyEditor()
if err != nil {
t.Fatalf("DependencyEditor(): %v", err)
}
if _, err := editor.AddDependencies(ctx, publicops.AddDependenciesRequest{
Actor: "writer",
Edges: []publicops.DependencyEdge{
{IssueID: "test-dep-add-a", DependsOnID: "test-dep-add-b", Type: publicops.DepBlocks},
{IssueID: "test-dep-add-a", DependsOnID: "test-dep-add-c", Type: publicops.DepRelated},
},
}); err != nil {
t.Fatalf("AddDependencies: %v", err)
}
if !doltHasCommitMessage(ctx, t, store, "dependency: add 2 edges") {
t.Fatal("no commit named the edge count")
}
requireCleanTables(ctx, t, store, "dependencies")
}
// TestDoltStoreDependencyEditorSingleEdgeNamesTheHistoryEntry pins the other
// spelling: one edge names both endpoints, however the caller spelled the
// request.
func TestDoltStoreDependencyEditorSingleEdgeNamesTheHistoryEntry(t *testing.T) {
store, cleanup := setupTestStore(t)
defer cleanup()
ctx, cancel := testContext(t)
defer cancel()
seedIssues(ctx, t, store, "test-dep-msg-a", "test-dep-msg-b")
editor, err := store.DependencyEditor()
if err != nil {
t.Fatalf("DependencyEditor(): %v", err)
}
if _, err := editor.AddDependencies(ctx, publicops.AddDependenciesRequest{
Actor: "writer",
Edges: []publicops.DependencyEdge{{IssueID: "test-dep-msg-a", DependsOnID: "test-dep-msg-b", Type: publicops.DepBlocks}},
}); err != nil {
t.Fatalf("AddDependencies single: %v", err)
}
if !doltHasCommitMessage(ctx, t, store, "bd: dep add test-dep-msg-a test-dep-msg-b") {
t.Fatal("a single edge did not name both endpoints in history")
}
}
// TestDoltStoreDependencyEditorRemoveNamesTheHistoryEntry pins the removal's
// spelling and its staging.
func TestDoltStoreDependencyEditorRemoveNamesTheHistoryEntry(t *testing.T) {
store, cleanup := setupTestStore(t)
defer cleanup()
ctx, cancel := testContext(t)
defer cancel()
seedIssues(ctx, t, store, "test-dep-rm-a", "test-dep-rm-b")
editor, err := store.DependencyEditor()
if err != nil {
t.Fatalf("DependencyEditor(): %v", err)
}
if _, err := editor.AddDependencies(ctx, publicops.AddDependenciesRequest{
Actor: "writer",
Edges: []publicops.DependencyEdge{{IssueID: "test-dep-rm-a", DependsOnID: "test-dep-rm-b", Type: publicops.DepBlocks}},
}); err != nil {
t.Fatalf("AddDependencies: %v", err)
}
if _, err := editor.RemoveDependency(ctx, publicops.RemoveDependencyRequest{
Actor: "writer", IssueID: "test-dep-rm-a", DependsOnID: "test-dep-rm-b",
}); err != nil {
t.Fatalf("RemoveDependency: %v", err)
}
if !doltHasCommitMessage(ctx, t, store, "bd: dep remove test-dep-rm-a test-dep-rm-b") {
t.Fatal("the removal did not name the edge in history")
}
requireCleanTables(ctx, t, store, "dependencies")
}
// TestDoltStoreDependencyEditorNoOpRemoveLeavesPendingRowsUncommitted is the
// GH#2455 pin for this role: an operation that wrote nothing must stage
// nothing, because DOLT_ADD stages a whole TABLE and a commit made for a no-op
// carries away whatever else was pending in it.
//
// The commit COUNT cannot see this on its own — Dolt swallows a commit with an
// empty diff, so a leaky no-op only shows up when there is something in the
// table for it to sweep. That is why this case survives the conformance
// contract's own "a removal that found nothing records no history" assertion:
// they fail on different bugs.
func TestDoltStoreDependencyEditorNoOpRemoveLeavesPendingRowsUncommitted(t *testing.T) {
store, cleanup := setupTestStore(t)
defer cleanup()
ctx, cancel := testContext(t)
defer cancel()
seedIssues(ctx, t, store, "test-dep-sweep-a", "test-dep-sweep-b", "test-dep-sweep-c")
editor, err := store.DependencyEditor()
if err != nil {
t.Fatalf("DependencyEditor(): %v", err)
}
// A pending edge in the working set that belongs to nobody's transaction.
if _, err := store.db.ExecContext(ctx, `
INSERT INTO dependencies (id, issue_id, depends_on_issue_id, type, created_at, created_by)
VALUES (?, ?, ?, ?, NOW(), ?)`,
"00000000-0000-4000-8000-00000000d09e", "test-dep-sweep-b", "test-dep-sweep-c", types.DepBlocks, "someone-else",
); err != nil {
t.Fatalf("stage a pending dependency row: %v", err)
}
before := doltCommitCount(ctx, t, store)
removed, err := editor.RemoveDependency(ctx, publicops.RemoveDependencyRequest{
Actor: "writer", IssueID: "test-dep-sweep-a", DependsOnID: "test-dep-sweep-b",
})
if err != nil {
t.Fatalf("RemoveDependency on a missing edge: %v", err)
}
if removed.Removed {
t.Fatal("Removed = true, want false")
}
if after := doltCommitCount(ctx, t, store); after != before {
t.Fatalf("commit count = %d, want %d: the no-op swept the pending row into a commit", after, before)
}
var dirty int
if err := store.db.QueryRowContext(ctx,
"SELECT COUNT(*) FROM dolt_status WHERE table_name = 'dependencies'").Scan(&dirty); err != nil {
t.Fatalf("query dolt_status: %v", err)
}
if dirty == 0 {
t.Fatal("dependencies is clean: the no-op removal committed a row it never wrote")
}
}