beads/internal/storage/embeddeddolt/batch_creator.go
CI Bot 8386871574 feat(create): put bd create --file behind the BatchCreator role (bd-lu170)
`bd create --file plan.md` had two implementations of one bulk create. The
direct route built types.Issue values in cmd/bd/markdown.go and handed them to
store.CreateIssues; the proxied route parsed the same templates again, opened a
unit of work, read a create context, checked the types itself and looped the
domain use case. Both now build ONE issueops.CreateBatchRequest from one shared
projection, and the difference between the two routes is which accessor answers.

THE ROLE. A batch's REQUEST is its transaction boundary, which is the same
reason BatchCloser exists and cannot be a loop over Lifecycle.Close: N creates
one call at a time are N transactions and N history entries, and the caller who
wants them to land together has no way to say so. It is its own role rather than
a Create overload because it is ALL OR NOTHING where the batch close keeps its
survivors, and one method cannot make both promises. The difference is what has
already happened: a close records work that is done, so a rollback destroys
information the batch was carrying; a create makes the work, so a refused item
leaves no survivors, and half a created plan cannot be re-run without
duplicating the half that landed.

It is born with Provenance, which CreateRequest does not have. Both --file
routes have always written `bd: create N issue(s) from <path>`, and a label
composed from the result cannot spell a file the result does not carry, so
without the field this commit would have caused the regression Q16 recorded
rather than inherited it.

USER-VISIBLE, and bigger than the bead described. The bead and the Q11 batch say
direct --file is non-atomic today - "a bad row can leave earlier rows created".
Reading the path does not support it: markdown.go built every issue first and
made ONE store.CreateIssues call, which both stores run in a single SQL
transaction. What was true is worse, and is recorded as Q25/Q26 in
AMBIGUITIES.md with A-bcr-2:

  - a dependency naming an id that does not exist was SILENTLY DROPPED on the
    direct route (the batch engine skips a dangling edge and nothing passed
    OnSkippedDependency), while the proxied route already failed on it. Every
    requested edge is now written or the batch refuses. An `external:` target
    and an id belonging to another repository are still written, exactly as
    `bd dep add` accepts them.
  - --ephemeral, --no-history, --mol-type and --validate were accepted and
    IGNORED on the direct route while the proxied route acted on all four, so
    `--file --ephemeral` created durable issues locally and wisps on a team
    server. All four now mean what they mean everywhere else, templates are
    linted on both routes, and created_by/owner are set as the single-issue
    create sets them.
  - --file --json emits the stored post-create rows on both routes. Additive.
  - proxied --file loses its config.yaml fallback for types.custom, the
    convergence Q17 already recorded for single-issue create.

--graph is untouched, per Q10: an upsert over a declared shape is a different
contract and gets its own role when it gets one.

CONFORMANCE. backend/conformance/batch_creator_contract.go holds nine cases,
each citing the leaf doc by line, run at all three implementations with ZERO
parks: 27 subtests green. They are TWO independent bodies plus an engine check,
not three - dolt and embeddeddolt share issueops.ExecuteCreateBatch, which
prepares every item and hands the slice to one CreateIssuesInTxWithResult, while
the unit-of-work body creates item by item through the domain use case and keeps
"all or nothing" by returning early and letting the transaction roll back. That
is also why no case asks for a FORWARD reference inside a batch: the store body
could resolve one and the uow body provably cannot, so the role promises only an
edge onto an EARLIER item and the contract says so at the case site (A-bcr-4).

The store-backed body is an InTx function rather than an internal/workapi/store
package, the documented variant EdgeReader introduced: it must assign every id
before any row is written so an item's edge can name a sibling, which cannot be
expressed above storage.DoltStorage. It needs no depguard entry for the same
reason - the shared function takes a transaction, so no front door can call it.

HTTP. POST /v0/beads/issues:batchCreate is new canonical surface, spec written
first and the types generated from it. It is a collection-level custom method
spelled the way ready:count is, which leaves POST /v0/beads/issues free for the
single create that belongs there; it does not collide with the claim row's wide
POST wildcard, because that pattern requires the separating slash
(TestBatchCreateDoesNotShadowTheClaimRoute drives both). BatchCreateResponse
carries no allOf, so the oapi-codegen v2.6.0 pin-dropping trap does not apply,
and its Items are pinned to []types.Issue in pinning.go.

The wire publishes no `id` member, which is what keeps the role's
ErrAlreadyExists off a vocabulary that has no already_exists code (A-bcr-5): the
server assigns every id and `bd import` remains the upsert surface. The role's
ErrValidation is answered by the operation's own failure path rather than by
ClassifyError, because the dangling-edge refusal wraps BOTH ErrValidation and
ErrNotFound and the shared classifier reaches ErrNotFound first - a 404 for a
request that addressed no resource. Neither branch quotes the role's message: a
refused edge arrives as a driver error naming tables and constraints, and 4xx
details here reflect the caller's input.

httpapi.Config's roles source grows to eight roles. The half-set cases that
built a Config by hand now go through rolesConfig, so the next role does not
have to edit six literals that are not about the role set.

Verified: build, vet, gofmt clean; golangci-lint 0 issues; make api-check clean;
TestBatchCreatorContract green at dolt (9/9), embeddeddolt (9/9) and uow (9/9);
./internal/httpapi green including eleven new batch-create tests; TestCorpusGolden
green (the corpus has no --file capture); TestCreateFromMarkdownFile green; the
cmd/bd create suites green with BEADS_TEST_EMBEDDED_DOLT=1 and the proxied
integration create suites green with BEADS_TEST_PROXIED_SERVER=1;
TestProxiedLookup green.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-05 16:43:11 +00:00

53 lines
1.7 KiB
Go

//go:build cgo
package embeddeddolt
import (
"context"
"database/sql"
"github.com/steveyegge/beads/internal/storage"
storageissueops "github.com/steveyegge/beads/internal/storage/issueops"
"github.com/steveyegge/beads/issueops"
)
// BatchCreator returns the guarded create-many surface for this store.
func (s *EmbeddedDoltStore) BatchCreator() (issueops.BatchCreator, error) {
return NewBatchCreator(s)
}
// NewBatchCreator returns a guarded batch creator backed by store.
func NewBatchCreator(store *EmbeddedDoltStore) (issueops.BatchCreator, error) {
if store == nil {
return nil, &storage.ErrUnsupported{Op: "NewBatchCreator", Backend: "nil"}
}
return &batchCreator{store: store}, nil
}
type batchCreator struct{ store *EmbeddedDoltStore }
// CreateBatch runs every create in ONE transaction with one commit. The
// message is composed inside the body because its default names how much
// LANDED, which is not knowable until every item has been prepared and routed
// to its plane.
func (o *batchCreator) CreateBatch(ctx context.Context, request issueops.CreateBatchRequest) (issueops.CreateBatchResult, error) {
if err := storageissueops.ValidateCreateBatchRequest(request); err != nil {
return issueops.CreateBatchResult{}, err
}
var result issueops.CreateBatchResult
err := o.store.runIssueOperationTxWithMessage(ctx, func(tx *sql.Tx) (storageissueops.ChangedTables, string, error) {
attempt, tables, err := storageissueops.ExecuteCreateBatch(ctx, tx, request)
if err != nil {
return nil, "", err
}
result = attempt
return tables, storageissueops.CreateBatchCommitMessage(request, attempt), nil
})
if err != nil {
return issueops.CreateBatchResult{}, err
}
return result, nil
}
var _ issueops.BatchCreator = (*batchCreator)(nil)