* fix(create): reject --waits-for-gate when --waits-for is absent; run validation before write
Two bugs in bd create's --waits-for-gate flag, both of the silent-drop family:
**Defect A — silent no-op without --waits-for.**
`bd create "title" --waits-for-gate all-children` (or any value, including
invalid ones) exited 0 and created a normal bead with no gate or dependency.
`buildWaitsFor` returned nil when spawnerID was empty, regardless of whether
--waits-for-gate was explicitly supplied. An operator who wrote --waits-for-gate
believing they had armed a hold got a success message and a fully-dispatchable
bead instead.
Fix: thread a `gateExplicit bool` parameter through `buildWaitsFor` (and the
`waitsForGateSet` field on `createInput` for the proxied-server path). When the
flag was explicitly supplied but no spawner is provided, return an error before
any write.
**Defect B — half-write when both flags are present with an invalid gate value.**
`bd create "title" --waits-for <id> --waits-for-gate TOTALLY-BOGUS` created the
bead first, then validated the gate value and errored. Callers treating a nonzero
exit as "nothing was created" were left with a dep-less, dispatchable orphan. This
was already fixed structurally by the create_atomic.go refactor (validation in
`buildWaitsFor` runs before `createIssueWithDeps`), but had no regression test.
Tests: two new sub-tests in `TestCreateDepsAtomicity` exercise both failure modes
end-to-end against a real embedded-Dolt store: the command must exit nonzero, and
no issue must appear in `bd list` or `bd ready` afterward.
Binary: go version -m $(which bd)
path github.com/steveyegge/beads/cmd/bd
mod github.com/steveyegge/beads (devel) vcs.modified=true
* fix(create): reject waits-for-gate on batch routes; reserve child IDs after validation
Addresses the two should-fix items from the 2026-07-23 maintainer review of
gastownhall/beads#4918 (commit 16de3982), which fixed --waits-for-gate
validation on the single-issue create path but left two gaps:
1. The non-proxied --file/--graph dispatch in create.go's RunE called
createIssuesFromMarkdown/createIssuesFromGraph directly, bypassing
rejectSingleIssueFlagsForMarkdown/rejectSingleIssueFlagsForGraph (which
already list "waits-for"/"waits-for-gate" in singleIssueOnlyFlags), so
`bd create --file plan.md --waits-for-gate BOGUS` (non-proxied) still
exited 0. Now call the rejecters before dispatching to the batch routes,
matching what the proxied path's gatherCreateInput already does.
2. store.GetNextChildID reserved (and committed) a child ID before
buildWaitsFor validation ran, so `bd create --parent X --waits-for-gate
BOGUS` (no --waits-for) burned a child ID even though the create itself
was rejected. Moved the parseDepSpecs/buildWaitsFor validation block
above the child-ID reservation so a failed validation leaves no gap in
child numbering.
Adds regression tests in create_deps_atomic_test.go for both gaps: batch
route rejection for --file and --graph, and a child-ID-not-burned check
that creates a parent, fails a --waits-for-gate-without-waits-for child
create, then asserts the next real child still gets .1.
Agent-Signature: claude-sonnet-5-medium on behalf of maphew
---------
Co-authored-by: Jacob Hausler <jacob@hausler.cc>
Co-authored-by: matt wilkie <maphew@gmail.com>
350 lines
11 KiB
Go
350 lines
11 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/spf13/cobra"
|
|
"github.com/steveyegge/beads/internal/config"
|
|
"github.com/steveyegge/beads/internal/debug"
|
|
"github.com/steveyegge/beads/internal/timeparsing"
|
|
"github.com/steveyegge/beads/internal/types"
|
|
"github.com/steveyegge/beads/internal/ui"
|
|
"github.com/steveyegge/beads/internal/validation"
|
|
)
|
|
|
|
type createInput struct {
|
|
markdownFile string
|
|
graphFile string
|
|
title string
|
|
explicitID string
|
|
parentID string
|
|
issueType string
|
|
status string
|
|
priority int
|
|
assignee string
|
|
externalRef string
|
|
specID string
|
|
description string
|
|
design string
|
|
acceptanceCriteria string
|
|
notes string
|
|
appendNotes string
|
|
labels []string
|
|
noInheritLabels bool
|
|
deps []string
|
|
waitsFor string
|
|
waitsForGate string
|
|
waitsForGateSet bool // true when --waits-for-gate was explicitly passed (not relying on default)
|
|
silent bool
|
|
dryRun bool
|
|
force bool
|
|
validate bool
|
|
ephemeral bool
|
|
noHistory bool
|
|
molType types.MolType
|
|
wispType types.WispType
|
|
eventCategory string
|
|
eventActor string
|
|
eventTarget string
|
|
eventPayload string
|
|
dueAt *time.Time
|
|
deferUntil *time.Time
|
|
metadata json.RawMessage
|
|
metadataSet bool
|
|
estimatedMinutes *int
|
|
repoOverride string
|
|
repoOverrideSet bool
|
|
createdBy string
|
|
owner string
|
|
jsonOutput bool
|
|
validationMode string
|
|
}
|
|
|
|
// graphApplyOptions projects the plan-wide CLI flags into the options every
|
|
// graph validation/materialization helper takes.
|
|
func (in createInput) graphApplyOptions() GraphApplyOptions {
|
|
return GraphApplyOptions{Ephemeral: in.ephemeral, NoHistory: in.noHistory, Force: in.force}
|
|
}
|
|
|
|
// graphApplyOptionsFromFlags is the embedded-path projection of the plan-wide
|
|
// flags, delegating to graphApplyOptions so both transports share one
|
|
// flags→options mapping (the embedded create path reads flags directly
|
|
// rather than through gatherCreateInput).
|
|
func graphApplyOptionsFromFlags(cmd *cobra.Command) GraphApplyOptions {
|
|
var in createInput
|
|
in.ephemeral, _ = cmd.Flags().GetBool("ephemeral")
|
|
in.noHistory, _ = cmd.Flags().GetBool("no-history")
|
|
in.force, _ = cmd.Flags().GetBool("force")
|
|
return in.graphApplyOptions()
|
|
}
|
|
|
|
func gatherCreateInput(cmd *cobra.Command, args []string) (createInput, error) {
|
|
in := createInput{}
|
|
|
|
in.markdownFile, _ = cmd.Flags().GetString("file")
|
|
in.graphFile, _ = cmd.Flags().GetString("graph")
|
|
in.dryRun, _ = cmd.Flags().GetBool("dry-run")
|
|
|
|
if in.markdownFile != "" && in.graphFile != "" {
|
|
return in, HandleError("cannot specify both --file and --graph")
|
|
}
|
|
if in.markdownFile != "" {
|
|
if len(args) > 0 {
|
|
return in, HandleError("cannot specify both title and --file flag")
|
|
}
|
|
if in.dryRun {
|
|
return in, HandleError("--dry-run is not supported with --file flag")
|
|
}
|
|
if err := rejectSingleIssueFlagsForMarkdown(cmd); err != nil {
|
|
return in, err
|
|
}
|
|
}
|
|
if in.graphFile != "" {
|
|
if len(args) > 0 {
|
|
return in, HandleError("cannot specify both title and --graph flag")
|
|
}
|
|
if err := rejectSingleIssueFlagsForGraph(cmd); err != nil {
|
|
return in, err
|
|
}
|
|
}
|
|
|
|
in.silent, _ = cmd.Flags().GetBool("silent")
|
|
in.force, _ = cmd.Flags().GetBool("force")
|
|
in.validate, _ = cmd.Flags().GetBool("validate")
|
|
in.noInheritLabels, _ = cmd.Flags().GetBool("no-inherit-labels")
|
|
in.ephemeral, _ = cmd.Flags().GetBool("ephemeral")
|
|
in.noHistory, _ = cmd.Flags().GetBool("no-history")
|
|
|
|
if in.ephemeral && in.noHistory {
|
|
return in, HandleError("--ephemeral and --no-history are mutually exclusive")
|
|
}
|
|
|
|
titleFlag, _ := cmd.Flags().GetString("title")
|
|
title, err := resolveTitle(args, titleFlag, in.markdownFile, in.graphFile)
|
|
if err != nil {
|
|
return in, err
|
|
}
|
|
in.title = title
|
|
|
|
desc, _, err := getDescriptionFlag(cmd)
|
|
if err != nil {
|
|
return in, err
|
|
}
|
|
in.description = desc
|
|
skills, _ := cmd.Flags().GetString("skills")
|
|
if skills != "" {
|
|
if in.description != "" {
|
|
in.description += "\n\n"
|
|
}
|
|
in.description += "## Required Skills\n" + skills
|
|
}
|
|
ctxStr, _ := cmd.Flags().GetString("context")
|
|
if ctxStr != "" {
|
|
if in.description != "" {
|
|
in.description += "\n\n"
|
|
}
|
|
in.description += "## Context\n" + ctxStr
|
|
}
|
|
|
|
design, _, err := getDesignFlag(cmd)
|
|
if err != nil {
|
|
return in, err
|
|
}
|
|
in.design = design
|
|
in.acceptanceCriteria, _ = cmd.Flags().GetString("acceptance")
|
|
in.notes, _ = cmd.Flags().GetString("notes")
|
|
in.appendNotes, _ = cmd.Flags().GetString("append-notes")
|
|
in.specID, _ = cmd.Flags().GetString("spec-id")
|
|
|
|
if in.markdownFile == "" && in.graphFile == "" {
|
|
if in.description == "" && !isTestIssue(in.title) {
|
|
if config.GetBool("create.require-description") {
|
|
return in, HandleError("description is required (set create.require-description: false in config.yaml to disable)")
|
|
}
|
|
}
|
|
}
|
|
|
|
priorityStr, _ := cmd.Flags().GetString("priority")
|
|
priority, err := validation.ValidatePriority(priorityStr)
|
|
if err != nil {
|
|
return in, HandleError("%v", err)
|
|
}
|
|
in.priority = priority
|
|
|
|
in.issueType, _ = cmd.Flags().GetString("type")
|
|
in.status, _ = cmd.Flags().GetString("status")
|
|
in.assignee, _ = cmd.Flags().GetString("assignee")
|
|
in.externalRef, _ = cmd.Flags().GetString("external-ref")
|
|
in.explicitID, _ = cmd.Flags().GetString("id")
|
|
in.parentID, _ = cmd.Flags().GetString("parent")
|
|
in.waitsFor, _ = cmd.Flags().GetString("waits-for")
|
|
in.waitsForGate, _ = cmd.Flags().GetString("waits-for-gate")
|
|
in.waitsForGateSet = cmd.Flags().Changed("waits-for-gate")
|
|
|
|
if in.explicitID != "" && in.parentID != "" {
|
|
return in, HandleError("cannot specify both --id and --parent flags")
|
|
}
|
|
|
|
in.labels, _ = cmd.Flags().GetStringSlice("labels")
|
|
labelAlias, _ := cmd.Flags().GetStringSlice("label")
|
|
if len(labelAlias) > 0 {
|
|
in.labels = append(in.labels, labelAlias...)
|
|
}
|
|
in.deps, _ = cmd.Flags().GetStringSlice("deps")
|
|
|
|
in.repoOverride, _ = cmd.Flags().GetString("repo")
|
|
in.repoOverrideSet = cmd.Flags().Changed("repo")
|
|
|
|
if molTypeStr, _ := cmd.Flags().GetString("mol-type"); molTypeStr != "" {
|
|
mt := types.MolType(molTypeStr)
|
|
if !mt.IsValid() {
|
|
return in, HandleError("invalid mol-type %q (must be %s)", molTypeStr, types.ValidMolTypeNames())
|
|
}
|
|
in.molType = mt
|
|
}
|
|
if wispTypeStr, _ := cmd.Flags().GetString("wisp-type"); wispTypeStr != "" {
|
|
wt := types.WispType(wispTypeStr)
|
|
if !wt.IsValid() {
|
|
return in, HandleError("invalid wisp-type %q (must be %s)", wispTypeStr, types.ValidWispTypeNames())
|
|
}
|
|
in.wispType = wt
|
|
}
|
|
|
|
in.eventCategory, _ = cmd.Flags().GetString("event-category")
|
|
in.eventActor, _ = cmd.Flags().GetString("event-actor")
|
|
in.eventTarget, _ = cmd.Flags().GetString("event-target")
|
|
in.eventPayload, _ = cmd.Flags().GetString("event-payload")
|
|
if (in.eventCategory != "" || in.eventActor != "" || in.eventTarget != "" || in.eventPayload != "") && in.issueType != "event" {
|
|
return in, HandleError("--event-category, --event-actor, --event-target, and --event-payload flags require --type=event")
|
|
}
|
|
|
|
if dueStr, _ := cmd.Flags().GetString("due"); dueStr != "" {
|
|
t, err := timeparsing.ParseRelativeTime(dueStr, time.Now())
|
|
if err != nil {
|
|
return in, HandleError("invalid --due format %q. Examples: +6h, tomorrow, next monday, 2025-01-15", dueStr)
|
|
}
|
|
in.dueAt = &t
|
|
}
|
|
|
|
if deferStr, _ := cmd.Flags().GetString("defer"); deferStr != "" {
|
|
t, err := timeparsing.ParseRelativeTime(deferStr, time.Now())
|
|
if err != nil {
|
|
return in, HandleError("invalid --defer format %q. Examples: +1h, tomorrow, next monday, 2025-01-15", deferStr)
|
|
}
|
|
if t.Before(time.Now()) && !in.silent && !debug.IsQuiet() {
|
|
fmt.Fprintf(os.Stderr, "%s Defer date %q is in the past. Issue will appear in bd ready immediately.\n",
|
|
ui.RenderWarn("!"), t.Format("2006-01-02 15:04"))
|
|
fmt.Fprintf(os.Stderr, " Did you mean a future date? Use --defer=+1h or --defer=tomorrow\n")
|
|
}
|
|
in.deferUntil = &t
|
|
}
|
|
|
|
if cmd.Flags().Changed("metadata") {
|
|
metadataValue, _ := cmd.Flags().GetString("metadata")
|
|
var metadataJSON string
|
|
if strings.HasPrefix(metadataValue, "@") {
|
|
filePath := metadataValue[1:]
|
|
// #nosec G304 -- user explicitly provides file path via @file.json syntax
|
|
data, err := os.ReadFile(filePath)
|
|
if err != nil {
|
|
return in, HandleError("failed to read metadata file %s: %v", filePath, err)
|
|
}
|
|
metadataJSON = string(data)
|
|
} else {
|
|
metadataJSON = metadataValue
|
|
}
|
|
if !json.Valid([]byte(metadataJSON)) {
|
|
return in, HandleError("invalid JSON in --metadata: must be valid JSON")
|
|
}
|
|
in.metadata = json.RawMessage(metadataJSON)
|
|
in.metadataSet = true
|
|
}
|
|
|
|
if cmd.Flags().Changed("estimate") {
|
|
est, _ := cmd.Flags().GetInt("estimate")
|
|
if est < 0 {
|
|
return in, HandleError("estimate must be a non-negative number of minutes")
|
|
}
|
|
in.estimatedMinutes = &est
|
|
}
|
|
|
|
in.createdBy = getActorWithGit()
|
|
in.owner = getOwner()
|
|
|
|
in.jsonOutput = jsonOutput
|
|
|
|
in.validationMode = config.GetString("validation.on-create")
|
|
if in.validate {
|
|
in.validationMode = "error"
|
|
}
|
|
|
|
return in, nil
|
|
}
|
|
|
|
var singleIssueOnlyFlags = []string{
|
|
"title",
|
|
"id", "parent", "no-inherit-labels",
|
|
"deps", "waits-for", "waits-for-gate",
|
|
"type", "priority", "assignee", "external-ref", "spec-id",
|
|
"status",
|
|
"description", "body", "message", "body-file", "description-file", "stdin",
|
|
"design", "design-file", "acceptance", "notes", "append-notes",
|
|
"labels", "label", "skills", "context",
|
|
"event-category", "event-actor", "event-target", "event-payload",
|
|
"due", "defer",
|
|
"metadata", "estimate", "wisp-type",
|
|
}
|
|
|
|
func rejectSingleIssueFlagsForMarkdown(cmd *cobra.Command) error {
|
|
for _, name := range singleIssueOnlyFlags {
|
|
if cmd.Flags().Changed(name) {
|
|
return HandleError("--%s is not valid with --file (markdown templates supply per-issue fields)", name)
|
|
}
|
|
}
|
|
// --force is plan-wide for --graph (foreign-prefix explicit IDs) but the
|
|
// markdown path never consults it, so reject it here rather than accept
|
|
// and silently ignore.
|
|
if cmd.Flags().Changed("force") {
|
|
return HandleError("--force is not valid with --file (markdown templates supply per-issue fields)")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func rejectSingleIssueFlagsForGraph(cmd *cobra.Command) error {
|
|
for _, name := range singleIssueOnlyFlags {
|
|
if cmd.Flags().Changed(name) {
|
|
return HandleError("--%s is not valid with --graph (graph plans supply per-node fields)", name)
|
|
}
|
|
}
|
|
if cmd.Flags().Changed("mol-type") {
|
|
return HandleError("--mol-type is not valid with --graph (set mol_type per node in the plan instead)")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func resolveTitle(args []string, titleFlag, markdownFile, graphFile string) (string, error) {
|
|
if markdownFile != "" || graphFile != "" {
|
|
return "", nil
|
|
}
|
|
|
|
switch {
|
|
case len(args) > 0 && titleFlag != "":
|
|
if args[0] != titleFlag {
|
|
return "", HandleError("cannot specify different titles as both positional argument and --title flag\n Positional: %q\n --title: %q", args[0], titleFlag)
|
|
}
|
|
return args[0], nil
|
|
case len(args) > 0:
|
|
if strings.HasPrefix(args[0], "-") {
|
|
return "", HandleError("title %q looks like a flag (starts with '-').\n Run 'bd create --help' for available options.\n To use this title anyway, pass it explicitly: bd create --title=%q", args[0], args[0])
|
|
}
|
|
return args[0], nil
|
|
case titleFlag != "":
|
|
return titleFlag, nil
|
|
default:
|
|
return "", HandleError("title required (or use --file to create from markdown)")
|
|
}
|
|
}
|