beads/internal/storage/issueops/batch_apply_test.go
Julian Knutsen e9e2c49431
Add issueops.BatchApplier: one plan, one transaction (#5480)
* Add issueops.BatchApplier: one plan, one transaction

A heterogeneous, ORDERED list of creates, updates, closes and edges applied
as one durable act. Each of BatchCreator, BatchCloser and DependencyEditor is
one verb repeated, so a caller that needs two of them needs two transactions
with a window in between; this role's unit is a PLAN.

The role, the two bodies, both decorators and the accessors on every
implementer. The wire half and the conformance contract land beside this.

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

* Close the empty-commit-message hole a no-op batch could open

ApplyBatchCommitMessage now reads the write as well as the result. The counts
are ItemResult.Changed, which is what a CALLER observes; a request can still
write a durable row with every count at zero — a same-type edge re-add
rewrites that edge row's metadata, a re-close can settle blocked state. An
empty message for one of those is an unlabelled Dolt commit on the store legs
and a rollback of a real write on the unit-of-work leg.

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

* Give BatchApplier the contract its two bodies justify

Thirty-nine cases over three legs and TWO independent bodies — the store legs
share issueops.ApplyBatchInTx, the unit-of-work leg cannot reach it, and the
contract header says so rather than claiming three backends agree.

The end-gate case is the one that could not have been written before: every
edge in it is legal when it is written, and only a re-validation against the
closure the whole request produced refuses the pair.

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

* Publish batchApply on v0, and delete batchCreate's false promise

The spec first, the types from it, the handler against the types. The item
union is a TAGGED single-shape object rather than a oneOf, because the
document's x-go-type doctrine forbids composition keywords; the create item
publishes sender, metadata, status, ephemeral and no_history, none of which
batchCreateIssues has — which is why no measured caller could use that one.

The wire result stays lean: ids, changed and a revision. The hydrated
snapshot on the Go contract is what the completion hooks consume, and hooks
never fire on this surface.

OQ2: batchCreateIssues' description claimed a dependency target could name an
earlier item of the same batch. It never could — the server mints the ids and
a wire item has no name — so the paragraph is deleted rather than qualified.

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

* Partition the two new dependency probes on the recorder's surface

The gate halves BatchApplier re-runs are repository PROBES — an ancestry walk
and a reachability walk — so recordingDepUC declares neither and the parity
test has to say so out loud rather than let embedding answer for it.

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

* Teach the proxied topology's capability golden about batchApply

The one expectation stored as data outside the package whose gates would
otherwise have caught it — which is what its own header warns about.

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

* Restore the CAS hook case, mint 409 already_exists, and stop over-claiming

Three fixes from review.

The rebase that brought MetadataCAS in left `case *hookMetadataCAS:` with an
EMPTY BODY: a Go type switch does not fall through, so RoleFiresHooks answered
FALSE for it and #5473's Listen refusal was silently disarmed. The compiler,
vet, lint and CI all pass on that. The case is restored and — because it had no
precondition of its own, which is why nothing caught it — serve_source_test now
pins CAS the way it pins the other four, with its peel assertion.

An occupied explicit create id becomes 409 already_exists rather than 400. The
body is well-formed and stays well-formed; what refuses it is state the client
must READ, and recovery is to look at that state. A dead 409 retires for free
where a narrowed 400 breaks clients.

Two headers claimed all three legs share the store body — every clause false,
one citing the contract header as agreeing when it says the opposite — and the
plan file gave a placement reason that is MetadataCAS's, not this role's. The
end-gate claim in ADDING_AN_ISSUEOPS_ROLE.md is narrowed to the true thing: the
two LEAF checks are shared, the orchestration around them forks.

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

* Give the scheduling-edge set one definition, in types

Four packages walked {blocks, conditional-blocks, parent-child} under four
names — isSchedulingEdge, isSchedulingDep, isSchedulingDependency, and the
isApplySchedulingEdge this branch added as the fourth. Adding a fifth
scheduling type was four edits with nothing to catch a missed one.

It goes in internal/types rather than the issueops leaf the review named,
because internal/storage/issueops imports internal/storage/domain, so domain
cannot import back and two of the four copies would have survived there. Beside
the Dep* constants themselves, every walker reaches it: nine call sites, one
definition, one edit for a fifth type.

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

---------

Co-authored-by: Review Sim <ci@beads.test>
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-08-09 06:59:34 -07:00

123 lines
5.3 KiB
Go

package issueops
import (
"strings"
"testing"
"github.com/steveyegge/beads/internal/storage"
publicops "github.com/steveyegge/beads/issueops"
)
// applyBatchCommitResult builds a result from a (kind, changed) list, which is
// all the message rule reads.
func applyBatchCommitResult(items ...publicops.ItemResult) publicops.ApplyBatchResult {
return publicops.ApplyBatchResult{Items: items}
}
func applyBatchItem(kind publicops.ItemKind, changed bool) publicops.ItemResult {
return publicops.ItemResult{Kind: kind, Changed: changed}
}
// TestApplyBatchCommitMessageNamesWhatLanded pins the entry a request records.
// It is a pure function over the result and the write, so the whole rule is
// pinned here without a database and the contract is left to assert that the
// entry actually appears.
func TestApplyBatchCommitMessageNamesWhatLanded(t *testing.T) {
for _, test := range []struct {
name string
provenance string
result publicops.ApplyBatchResult
write BatchApplyWrite
want string
}{
{
name: "one of each kind",
result: applyBatchCommitResult(applyBatchItem(publicops.ItemCreate, true), applyBatchItem(publicops.ItemUpdate, true), applyBatchItem(publicops.ItemClose, true), applyBatchItem(publicops.ItemDepAdd, true)),
write: BatchApplyWrite{Changed: true},
want: "bd: apply 1 create, 1 update, 1 close, 1 edge",
},
{
name: "counts are pluralized and ordered by kind, never by request position",
result: applyBatchCommitResult(applyBatchItem(publicops.ItemDepAdd, true), applyBatchItem(publicops.ItemCreate, true), applyBatchItem(publicops.ItemDepAdd, true), applyBatchItem(publicops.ItemCreate, true)),
write: BatchApplyWrite{Changed: true},
want: "bd: apply 2 creates, 2 edges",
},
{
name: "an item that changed nothing is not counted",
result: applyBatchCommitResult(applyBatchItem(publicops.ItemCreate, true), applyBatchItem(publicops.ItemClose, false)),
write: BatchApplyWrite{Changed: true},
want: "bd: apply 1 create",
},
{
// The counts are ItemResult.Changed, which is what a CALLER can
// observe. A same-type edge re-add rewrites that edge row's metadata
// and a re-close can still settle blocked state, so the transaction
// has something to commit while every count is zero. An empty
// message here would be an unlabelled Dolt commit on the store legs
// and a rollback of a real write on the unit-of-work leg.
name: "a request that wrote without landing anything still names an entry",
result: applyBatchCommitResult(applyBatchItem(publicops.ItemDepAdd, false)),
write: BatchApplyWrite{Changed: true, Tables: ChangedTables{"dependencies": true}},
want: "bd: apply batch",
},
{
name: "nothing changed and nothing was staged is the empty message",
result: applyBatchCommitResult(applyBatchItem(publicops.ItemUpdate, false), applyBatchItem(publicops.ItemClose, false)),
write: BatchApplyWrite{},
want: "",
},
{
// The wisp trap, generalized: the store legs stage nothing for an
// all-ephemeral batch and record no entry whatever this returns, but
// the unit-of-work leg reads "" as "roll this attempt back". A
// durable-only count would delete an ephemeral batch's work there.
name: "an all-ephemeral batch still gets a message",
result: applyBatchCommitResult(applyBatchItem(publicops.ItemCreate, true), applyBatchItem(publicops.ItemCreate, true)),
write: BatchApplyWrite{Changed: true},
want: "bd: apply 2 creates",
},
{
name: "provenance replaces the default",
provenance: "bd: apply plan.md",
result: applyBatchCommitResult(applyBatchItem(publicops.ItemCreate, true)),
write: BatchApplyWrite{Changed: true},
want: "bd: apply plan.md",
},
{
// Provenance changes how the entry READS, never WHETHER one is
// recorded — UpdateRequest.Provenance's rule, inherited whole.
name: "provenance does not conjure an entry out of a request that wrote nothing",
provenance: "bd: apply plan.md",
result: applyBatchCommitResult(applyBatchItem(publicops.ItemUpdate, false)),
write: BatchApplyWrite{},
want: "",
},
} {
t.Run(test.name, func(t *testing.T) {
plan := storage.ApplyBatchPlan{Provenance: test.provenance}
if got := ApplyBatchCommitMessage(plan, test.result, test.write); got != test.want {
t.Fatalf("ApplyBatchCommitMessage = %q, want %q", got, test.want)
}
})
}
}
// TestApplyBatchCommitMessageNamesNoIDs pins the rule CreateBatchCommitMessage
// states for its own default: a request can carry a hundred items and mint a
// hundred ids, and an entry naming them all is the diff written twice.
func TestApplyBatchCommitMessageNamesNoIDs(t *testing.T) {
items := make([]publicops.ItemResult, 0, 3)
for _, id := range []string{"bd-1", "bd-2", "bd-3"} {
items = append(items, publicops.ItemResult{Kind: publicops.ItemCreate, IssueID: id, Changed: true})
}
message := ApplyBatchCommitMessage(storage.ApplyBatchPlan{}, publicops.ApplyBatchResult{Items: items},
BatchApplyWrite{Changed: true})
for _, id := range []string{"bd-1", "bd-2", "bd-3"} {
if strings.Contains(message, id) {
t.Fatalf("commit message %q names %s; the default names a COUNT and never an id", message, id)
}
}
if message != "bd: apply 3 creates" {
t.Fatalf("commit message = %q, want the count spelling", message)
}
}