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
This commit is contained in:
Jacob Hausler 2026-07-19 15:38:24 -05:00
commit 16de398257
6 changed files with 73 additions and 10 deletions

View file

@ -534,7 +534,7 @@ var createCmd = &cobra.Command{
if err != nil {
return HandleErrorRespectJSON("%v", err)
}
waitsForSpec, err := buildWaitsFor(waitsFor, waitsForGate)
waitsForSpec, err := buildWaitsFor(waitsFor, waitsForGate, cmd.Flags().Changed("waits-for-gate"))
if err != nil {
return HandleError("%v", err)
}

View file

@ -62,9 +62,16 @@ func parseDepSpec(raw string) (domain.DependencySpec, error) {
return spec, nil
}
func buildWaitsFor(spawnerID, gate string) (*domain.WaitsForSpec, error) {
// buildWaitsFor validates and constructs a WaitsForSpec from the --waits-for
// and --waits-for-gate flag values. gateExplicit must be true when the caller
// explicitly passed --waits-for-gate (not relying on its default); in that case
// a missing spawnerID is rejected rather than silently ignored.
func buildWaitsFor(spawnerID, gate string, gateExplicit bool) (*domain.WaitsForSpec, error) {
spawnerID = strings.TrimSpace(spawnerID)
if spawnerID == "" {
if gateExplicit {
return nil, fmt.Errorf("--waits-for-gate requires --waits-for (no spawner ID specified)")
}
return nil, nil
}
if gate == "" {

View file

@ -167,9 +167,49 @@ func TestCreateDepsAtomicity(t *testing.T) {
}
})
// Defect A: --waits-for-gate without --waits-for silently no-ops.
// Before the fix, both commands below exited 0 and created a normal bead
// with no gate or dependency wired, regardless of the gate value (including
// invalid values). An operator who wrote --waits-for-gate believing they had
// armed a hold got a dispatchable bead instead.
t.Run("waits_for_gate_without_waits_for_is_rejected", func(t *testing.T) {
for _, gate := range []string{"all-children", "TOTALLY-BOGUS"} {
title := "gate-no-spawner-" + gate
out, err := runCreateDepsBDRaw(bd, dir, "create", title, "--json",
"--waits-for-gate", gate)
if err == nil {
t.Errorf("create --waits-for-gate %s (no --waits-for) exited 0 (was silently ignored); output:\n%s", gate, out)
}
if createDepsIssueTitles(t, bd, dir)[title] {
t.Errorf("issue %q persisted despite rejected command (should not exist)", title)
}
}
})
// Defect B: with both --waits-for and --waits-for-gate INVALID, the bead
// was written before validation ran, leaving a dep-less dispatchable orphan.
// After the refactor in create_atomic.go, validation runs pre-write; this
// test documents the contract and guards against regressions.
t.Run("invalid_waits_for_gate_value_is_rejected_before_write", func(t *testing.T) {
out, err := runCreateDepsBDRaw(bd, dir, "create", "invalid-gate-probe", "--json",
"--waits-for", blocker, "--waits-for-gate", "TOTALLY-BOGUS")
if err == nil {
t.Errorf("create with invalid --waits-for-gate exited 0; output:\n%s", out)
}
if !strings.Contains(out, "TOTALLY-BOGUS") {
t.Errorf("error should name the invalid gate value; got:\n%s", out)
}
if createDepsIssueTitles(t, bd, dir)["invalid-gate-probe"] {
t.Error("issue \"invalid-gate-probe\" persisted despite failed command (half-write: bead created before validation)")
}
})
t.Run("ready_never_offers_a_failed_create", func(t *testing.T) {
out := runCreateDepsBD(t, bd, dir, "ready", "--json")
for _, title := range []string{"orphan candidate", "partial dep issue", "waits-for orphan"} {
for _, title := range []string{
"orphan candidate", "partial dep issue", "waits-for orphan",
"gate-no-spawner-all-children", "gate-no-spawner-TOTALLY-BOGUS", "invalid-gate-probe",
} {
if strings.Contains(out, title) {
t.Errorf("bd ready offers %q, a bead whose create should have been rolled back:\n%s", title, out)
}

View file

@ -108,8 +108,8 @@ func TestParseDepSpecs(t *testing.T) {
}
func TestBuildWaitsFor(t *testing.T) {
t.Run("empty spawner returns nil", func(t *testing.T) {
got, err := buildWaitsFor("", "")
t.Run("empty spawner without explicit gate returns nil", func(t *testing.T) {
got, err := buildWaitsFor("", "", false)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
@ -117,8 +117,22 @@ func TestBuildWaitsFor(t *testing.T) {
t.Fatalf("expected nil, got %+v", got)
}
})
t.Run("explicit gate without spawner returns error", func(t *testing.T) {
// --waits-for-gate set but --waits-for absent: must be rejected,
// not silently ignored. Applies to any gate value, including the
// valid "all-children" — the operator clearly intended a dep they
// did not get.
_, err := buildWaitsFor("", types.WaitsForAllChildren, true)
if err == nil {
t.Fatal("expected error when --waits-for-gate is explicit but spawner is empty")
}
_, err = buildWaitsFor("", "TOTALLY-BOGUS", true)
if err == nil {
t.Fatal("expected error when --waits-for-gate is explicit (invalid value) but spawner is empty")
}
})
t.Run("empty gate defaults to all-children", func(t *testing.T) {
got, err := buildWaitsFor("bd-1", "")
got, err := buildWaitsFor("bd-1", "", false)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
@ -128,7 +142,7 @@ func TestBuildWaitsFor(t *testing.T) {
}
})
t.Run("any-children gate accepted", func(t *testing.T) {
got, err := buildWaitsFor("bd-1", types.WaitsForAnyChildren)
got, err := buildWaitsFor("bd-1", types.WaitsForAnyChildren, false)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
@ -137,13 +151,13 @@ func TestBuildWaitsFor(t *testing.T) {
}
})
t.Run("invalid gate rejected", func(t *testing.T) {
_, err := buildWaitsFor("bd-1", "bogus")
_, err := buildWaitsFor("bd-1", "bogus", false)
if err == nil {
t.Fatal("expected error for invalid gate")
}
})
t.Run("whitespace spawner treated as empty", func(t *testing.T) {
got, err := buildWaitsFor(" ", "")
got, err := buildWaitsFor(" ", "", false)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}

View file

@ -38,6 +38,7 @@ type createInput struct {
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
@ -164,6 +165,7 @@ func gatherCreateInput(cmd *cobra.Command, args []string) (createInput, error) {
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")

View file

@ -52,7 +52,7 @@ func runCreateProxiedSingle(_ *cobra.Command, ctx context.Context, in createInpu
if err != nil {
return HandleError("%v", err)
}
waitsFor, err := buildWaitsFor(in.waitsFor, in.waitsForGate)
waitsFor, err := buildWaitsFor(in.waitsFor, in.waitsForGate, in.waitsForGateSet)
if err != nil {
return HandleError("%v", err)
}