Forward-ports the 38 commits that shipped on release/1.3.0 (tagged v1.3.0 at
f45b249ce) into main, as RELEASING.md mandates after a tag. None of them had
ever been forward-ported. Recorded as a merge so main becomes a superset of
the release branch and the lineage is in the history.
Migration ordinal collision (the reason this needed a human-shaped merge):
both branches independently claimed clone-local ignored slot 0026 under
different filenames, so git merged both in with no conflict and left the
series with two migrations at ordinal 26 — a single-integer cursor cannot
address that, and migrationSource.list() panics on it.
Main's ignored/0026_add_wisps_current_revision is renumbered to 0027.
Release's 0026_dep_rekey_dedup_marker is in the shipped v1.3.0 tag, so field
stores have already recorded it at 26 and it can never move; main's twin has
never shipped in any release, so it is the one that is free to move. 0027 was
unused in the ignored series (both sides topped out at 26).
This trips check-migration-hygiene.sh Check C (frozen migrations), which has
no exemption mechanism. That is expected and accepted: breaking the collision
requires a rename, and the ignored series' cursor table is itself dolt-ignored,
so its content hashes are clone-local and never participate in the cross-clone
ContentHashSkew comparison that Check C exists to protect. Main's 0067 body is
deliberately NOT edited for the same reason in reverse — it is main-plane and
its hash IS compared across clones.
Conflicts resolved:
CHANGELOG.md release's finalized [1.3.0] section kept
byte-identical; main's two unreleased
entries (#6023, #5859) moved up under
[Unreleased] where they belong
cmd/bd/init_safety.go union — release's added paragraph is
cmd/bd/init_safety_help.go accurate against the merged code (three
exit-12 sources, incl. init.go:3147)
internal/storage/uow/dolt_sql_provider.go
kept main's newProvider refactor AND
release's readOnly field/assertion;
newProvider gained readOnly: opts.readOnly
so release's read-through-refused-migration
behavior survives main's refactor
.github/workflows/main.yml single -timeout=30m per release's #6091
scripts/ci_workflow_test.go design (flag on the invocation, not in
matrix.test-flags); the shadowed 25m in
matrix.test-flags removed
scripts/migration-test/lib/versions.sh
union — main's darwin/arm64 corpus plus
release's v1.2.2 linux/amd64 entry
integrations/beads-mcp/uv.lock ruff 0.16.5 (release, newer); block is
internally consistent, uv lock --check ok
default.nix vendorHash left at main's value and is
KNOWN STALE — see below
Version stamp lands at 1.3.0 (main had been sitting at the previously-released
1.2.2, this project's between-releases pattern). check-versions.sh: 24/24.
Known follow-up: default.nix vendorHash. The merged go.mod/go.sum is a genuine
union (release's kin-openapi/oapi-codegen bumps + main's newer Dolt stack) and
matches neither side, so neither recorded hash is correct. It must be
regenerated with scripts/update-nix-vendorhash.sh. The repo's automatic fixer
(update-vendor-hash.yml) is dependabot-only and will not run on this PR.
250 lines
10 KiB
Go
250 lines
10 KiB
Go
package issueops
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"fmt"
|
|
|
|
"github.com/steveyegge/beads/internal/storage"
|
|
"github.com/steveyegge/beads/internal/types"
|
|
publicops "github.com/steveyegge/beads/issueops"
|
|
)
|
|
|
|
// CloneCreateBatchRequest returns a deep copy of request, so a body can
|
|
// normalize an attempt without writing through to the caller's items — and, in
|
|
// particular, without leaving the ID it assigned on the caller's issue.
|
|
func CloneCreateBatchRequest(request publicops.CreateBatchRequest) publicops.CreateBatchRequest {
|
|
clone := request
|
|
clone.Items = make([]publicops.BatchCreateItem, len(request.Items))
|
|
for i, item := range request.Items {
|
|
clone.Items[i] = publicops.BatchCreateItem{
|
|
Issue: clonePublicIssue(item.Issue),
|
|
Dependencies: append([]publicops.CreateDependency(nil), item.Dependencies...),
|
|
}
|
|
}
|
|
return clone
|
|
}
|
|
|
|
// ValidateCreateBatchRequest applies the request rules every BatchCreator
|
|
// implementation shares. It runs BEFORE any transaction opens: everything it
|
|
// checks is knowable from the request alone, so a batch refused here has
|
|
// provably written nothing.
|
|
//
|
|
// Per-item CONTENT rules are not here. They need the workspace's configured
|
|
// prefix, statuses and types, so they run inside the transaction through the
|
|
// same PreparePublicCreateRequest a single create runs.
|
|
func ValidateCreateBatchRequest(request publicops.CreateBatchRequest) error {
|
|
if request.Actor == "" {
|
|
return fmt.Errorf("%w: create batch requires an actor", storage.ErrValidation)
|
|
}
|
|
if len(request.Items) == 0 {
|
|
return fmt.Errorf("%w: create batch requires at least one item", storage.ErrValidation)
|
|
}
|
|
for i, item := range request.Items {
|
|
if item.Issue == nil {
|
|
return fmt.Errorf("%w: create batch item %d requires an issue", storage.ErrValidation, i)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// CreateBatchItemRequest projects one item onto the single-create request the
|
|
// shared preparation and validation speak. Both front doors and both bodies read
|
|
// an item through it, so no item restates CreateRequest's field rules.
|
|
func CreateBatchItemRequest(request publicops.CreateBatchRequest, item publicops.BatchCreateItem) publicops.CreateRequest {
|
|
return publicops.CreateRequest{
|
|
Actor: request.Actor,
|
|
Issue: item.Issue,
|
|
Dependencies: item.Dependencies,
|
|
ForceIDPrefix: request.ForceIDPrefix,
|
|
}
|
|
}
|
|
|
|
// CreateBatchItemError names the item a batch refusal came from. The role
|
|
// promises the index appears in the message and nowhere else.
|
|
func CreateBatchItemError(index int, err error) error {
|
|
return fmt.Errorf("create batch item %d: %w", index, err)
|
|
}
|
|
|
|
// CreateBatchCommitMessage is the history entry a batch records: the caller's
|
|
// own label when it supplied one, otherwise a default naming how much landed.
|
|
//
|
|
// IT NAMES A COUNT AND NEVER AN ID: a create batch's ids are new, there can be
|
|
// hundreds from one file, and an entry naming them all is the diff written twice.
|
|
//
|
|
// An all-ephemeral batch writes only to the dolt-ignored wisp tables, so the
|
|
// store-backed bodies stage nothing and record no entry whatever this returns —
|
|
// but the unit-of-work backend reads "" as "roll this attempt back", so a
|
|
// wisp-only batch must still hand it a message or the wisps it created are
|
|
// discarded. That is the same trap CloseBatchCommitMessage documents.
|
|
func CreateBatchCommitMessage(request publicops.CreateBatchRequest, result publicops.CreateBatchResult) string {
|
|
durable, ephemeral := 0, 0
|
|
for _, issue := range result.Issues {
|
|
if IsWisp(issue) {
|
|
ephemeral++
|
|
continue
|
|
}
|
|
durable++
|
|
}
|
|
var fallback string
|
|
switch {
|
|
case durable > 0:
|
|
fallback = fmt.Sprintf("bd: create %d issue(s)", durable)
|
|
case ephemeral == 1:
|
|
fallback = "bd: create 1 ephemeral item"
|
|
case ephemeral > 1:
|
|
fallback = fmt.Sprintf("bd: create %d ephemeral items", ephemeral)
|
|
default:
|
|
return ""
|
|
}
|
|
return HistoryEntry(request.Provenance, fallback)
|
|
}
|
|
|
|
// ExecuteCreateBatch creates every item in tx and reports the durable tables
|
|
// changed. It is the store-backed body behind the BatchCreator accessor.
|
|
//
|
|
// THE ITEMS ARE PREPARED FIRST AND WRITTEN ONCE. Each goes through the same
|
|
// PreparePublicCreateRequest a single create goes through, and each is assigned
|
|
// its id before any row is written, which is what lets an item's edge name an
|
|
// item created earlier in the same batch. The rows and their edges then land
|
|
// through ONE CreateIssuesInTxWithResult.
|
|
//
|
|
// ANY error returns, and the enclosing transaction rolls the whole batch back.
|
|
// That includes an edge the engine declined to write: the batch engine drops a
|
|
// dangling edge so a partial import can still land, and this role refuses.
|
|
func ExecuteCreateBatch(ctx context.Context, tx *sql.Tx, request publicops.CreateBatchRequest) (publicops.CreateBatchResult, ChangedTables, error) {
|
|
attempt := CloneCreateBatchRequest(request)
|
|
if err := ValidateCreateBatchRequest(attempt); err != nil {
|
|
return publicops.CreateBatchResult{}, nil, err
|
|
}
|
|
options := storage.BatchCreateOptions{
|
|
CreateOnly: true,
|
|
SkipPrefixValidation: attempt.ForceIDPrefix,
|
|
}
|
|
batch, err := NewBatchContext(ctx, tx, options)
|
|
if err != nil {
|
|
return publicops.CreateBatchResult{}, nil, err
|
|
}
|
|
createContext := PublicCreateContext{
|
|
IssuePrefix: batch.ConfigPrefix,
|
|
AllowedPrefixes: batch.AllowedPrefixes,
|
|
CustomStatuses: batch.CustomStatuses,
|
|
CustomTypes: batch.CustomTypes,
|
|
}
|
|
infraTypes := ResolveInfraTypesInTx(ctx, tx)
|
|
|
|
issues := make([]*types.Issue, len(attempt.Items))
|
|
for i, item := range attempt.Items {
|
|
prepared, err := PreparePublicCreateRequest(CreateBatchItemRequest(attempt, item), createContext)
|
|
if err != nil {
|
|
return publicops.CreateBatchResult{}, nil, CreateBatchItemError(i, err)
|
|
}
|
|
issue := prepared.Issue
|
|
// Configured infra types live in the wisp tables, the same routing
|
|
// ExecuteCreate applies. Mark the issue BEFORE its ID is assigned so ID
|
|
// generation, the create-only guard and table routing all agree.
|
|
if !issue.Ephemeral && !issue.NoHistory && infraTypes[string(issue.IssueType)] {
|
|
issue.Ephemeral = true
|
|
}
|
|
// A wisp_type is a claim of ephemerality, same as ExecuteCreate: a
|
|
// typed wisp minted without the flag escapes every TTL/GC/purge tier.
|
|
if !issue.Ephemeral && !issue.NoHistory && issue.WispType != "" {
|
|
issue.Ephemeral = true
|
|
}
|
|
if err := assignCreateIssueIDInTx(ctx, tx, batch, issue, attempt.Actor); err != nil {
|
|
return publicops.CreateBatchResult{}, nil, CreateBatchItemError(i, ClassifyPublicCreateError(err))
|
|
}
|
|
issue.Dependencies = storage.CreatePublicCreateDependencies(issue.ID, prepared)
|
|
issues[i] = issue
|
|
}
|
|
|
|
var skipped []skippedDependency
|
|
options.OnSkippedDependency = func(issueID, dependsOnID, reason string) {
|
|
skipped = append(skipped, skippedDependency{issueID: issueID, dependsOnID: dependsOnID, reason: reason})
|
|
}
|
|
created, err := CreateIssuesInTxWithResult(ctx, tx, issues, attempt.Actor, options)
|
|
if err != nil {
|
|
return publicops.CreateBatchResult{}, nil, ClassifyPublicCreateError(err)
|
|
}
|
|
if len(skipped) > 0 {
|
|
return publicops.CreateBatchResult{}, nil, publicCreateValidationError(skippedDependencyError(skipped))
|
|
}
|
|
|
|
// CreateIssuesDirtyTables reports the child_counters advance itself, from
|
|
// the reconciliation the batch engine already ran, so there is no separate
|
|
// counter bookkeeping here.
|
|
tables := ChangedTables{}
|
|
tables.Merge(CreateIssuesDirtyTables(ctx, issues, created))
|
|
result := publicops.CreateBatchResult{Issues: make([]*types.Issue, len(issues))}
|
|
for i, issue := range issues {
|
|
hydrated, err := HydrateIssueOperationResult(ctx, tx, issue.ID, false)
|
|
if err != nil {
|
|
return publicops.CreateBatchResult{}, nil, err
|
|
}
|
|
OverlayCreateTimestamps(hydrated, issue)
|
|
result.Issues[i] = hydrated
|
|
}
|
|
return result, tables, nil
|
|
}
|
|
|
|
// CrossPlaneBatchEdgeError is the refusal the store-backed body raises from
|
|
// validateCreateIssuesMixedBucketDependencies, spelled here so the unit-of-work
|
|
// body can raise the identical one before it starts writing.
|
|
func CrossPlaneBatchEdgeError(sourceID, targetID string) error {
|
|
return fmt.Errorf("mixed regular/wisp CreateIssues batch cannot include cross-bucket dependency %s -> %s; create the issues first, then add the in-batch dependency after both issues exist%.0w",
|
|
sourceID, targetID, publicops.ErrValidation)
|
|
}
|
|
|
|
// ValidateCreateBatchPlanes refuses a batch whose items would write an edge
|
|
// BETWEEN the durable and ephemeral planes, which BatchCreator's contract says
|
|
// "cannot be written by the batch that creates both of its ends".
|
|
//
|
|
// THE STORE BODY GETS THIS FOR FREE and the unit-of-work body does not. The
|
|
// store body assigns every id first and then hands the whole slice to
|
|
// CreateIssuesInTxWithResult, which sees the batch as a set and refuses. The
|
|
// unit-of-work body creates item by item, so by the time an edge is written
|
|
// its target is an ordinary existing row and the domain layer writes the
|
|
// cross-plane edge happily — the identical request was refused whole by two
|
|
// backends and landed in full on the third.
|
|
//
|
|
// Only an EXPLICIT id can be an in-batch target: a caller cannot name an id the
|
|
// batch has not minted yet, so the ids known here are the only ones an edge can
|
|
// reach inside the batch. infraTypes promotes the same types both bodies
|
|
// promote, so the plane a row lands in is decided the same way it will be.
|
|
func ValidateCreateBatchPlanes(request publicops.CreateBatchRequest, infraTypes map[string]bool) error {
|
|
wispByID := make(map[string]bool, len(request.Items))
|
|
var hasDurable, hasWisp bool
|
|
planeOf := func(issue *publicops.Issue) bool {
|
|
return issue.Ephemeral || issue.NoHistory || infraTypes[string(issue.IssueType)]
|
|
}
|
|
for _, item := range request.Items {
|
|
if item.Issue == nil {
|
|
continue
|
|
}
|
|
wisp := planeOf(item.Issue)
|
|
if wisp {
|
|
hasWisp = true
|
|
} else {
|
|
hasDurable = true
|
|
}
|
|
if item.Issue.ID != "" {
|
|
wispByID[item.Issue.ID] = wisp
|
|
}
|
|
}
|
|
if !hasDurable || !hasWisp {
|
|
return nil
|
|
}
|
|
for _, item := range request.Items {
|
|
if item.Issue == nil {
|
|
continue
|
|
}
|
|
source := planeOf(item.Issue)
|
|
for _, dep := range item.Dependencies {
|
|
target, inBatch := wispByID[dep.TargetID]
|
|
if inBatch && source != target {
|
|
return CrossPlaneBatchEdgeError(item.Issue.ID, dep.TargetID)
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|