diff --git a/cmd/bd/create.go b/cmd/bd/create.go index 7ddc64ddd..c26320251 100644 --- a/cmd/bd/create.go +++ b/cmd/bd/create.go @@ -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) } diff --git a/cmd/bd/create_deps.go b/cmd/bd/create_deps.go index 9d982a025..c2d1ad221 100644 --- a/cmd/bd/create_deps.go +++ b/cmd/bd/create_deps.go @@ -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 == "" { diff --git a/cmd/bd/create_deps_atomic_test.go b/cmd/bd/create_deps_atomic_test.go index cef6ee6aa..20dd2c286 100644 --- a/cmd/bd/create_deps_atomic_test.go +++ b/cmd/bd/create_deps_atomic_test.go @@ -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) } diff --git a/cmd/bd/create_deps_test.go b/cmd/bd/create_deps_test.go index 0c58e6fda..668241765 100644 --- a/cmd/bd/create_deps_test.go +++ b/cmd/bd/create_deps_test.go @@ -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) } diff --git a/cmd/bd/create_input.go b/cmd/bd/create_input.go index fd984fa77..f719c2015 100644 --- a/cmd/bd/create_input.go +++ b/cmd/bd/create_input.go @@ -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") diff --git a/cmd/bd/create_proxied_server.go b/cmd/bd/create_proxied_server.go index 7bb95e0c0..89ca285de 100644 --- a/cmd/bd/create_proxied_server.go +++ b/cmd/bd/create_proxied_server.go @@ -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) }