diff --git a/CHANGELOG.md b/CHANGELOG.md index ea1413265..d1dceed0c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -40,6 +40,22 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 statements it cannot parse (for example `PRAGMA`) instead of treating them as reads. +- **`bd types` now lists exactly the types `bd create --type` accepts** + ([#6934](https://github.com/gastownhall/beads/pull/6934)). The two resolved + the type set through different code and could disagree: a server-mode + workspace's `bd types` dropped `.beads/config.yaml` `types.custom` entries + once the database had any custom type, although create accepts them; in + proxied mode `bd config set storage-class.` checked `config.yaml` alone + and refused custom types registered in the database; and the built-in system + types `message`, `molecule`, `gate` and `event` were never listed. Every + mode now composes custom types by one rule (the `custom_types` table, else + the `types.custom` config row, always unioned with `config.yaml`). `bd types` + gains a "System types" section, and `bd types --json` an additive + `system_types` field; `core_types` and `custom_types` are unchanged. A + failure to read the custom types now fails `bd types`, `bd create --graph` + and `bd config set storage-class.*` instead of being silently treated as "no + custom types". + - **`notion.token` is kept out of the Dolt database** ([#6676](https://github.com/gastownhall/beads/issues/6676)). It was missing from the yaml-only key list that holds the other tracker secrets, so diff --git a/cmd/bd/BUILD.bazel b/cmd/bd/BUILD.bazel index 9a0066ff2..a1875450a 100644 --- a/cmd/bd/BUILD.bazel +++ b/cmd/bd/BUILD.bazel @@ -318,7 +318,6 @@ go_library( "todo_proxied_server.go", "tracker_store.go", "types.go", - "types_proxied_server.go", "unclaim.go", "unclaim_proxied_server.go", "undefer.go", @@ -1102,6 +1101,8 @@ go_test( "todo_embedded_test.go", "tracker_frontdoor_proxy_test.go", "tracker_proxy_uow_conformance_integration_test.go", + "types_builtin_test.go", + "types_create_agreement_test.go", "types_embedded_test.go", "unclaim_conditional_embedded_test.go", "unclaim_conditional_parity_test.go", diff --git a/cmd/bd/config.go b/cmd/bd/config.go index f39a33d77..ebdfb6ed6 100644 --- a/cmd/bd/config.go +++ b/cmd/bd/config.go @@ -1264,7 +1264,11 @@ func validateStorageClassConfig(key, value string) error { if canonical := issueType.Normalize(); canonical != issueType { return fmt.Errorf("invalid key %q: %q is an alias of %q, and create-time lookup uses the canonical type; set storage-class.%s instead", key, suffix, canonical, canonical) } - if !issueType.IsValidWithCustom(loadEmbeddedCustomTypes()) { + customTypes, err := resolveWorkspaceCustomTypes(rootCtx) + if err != nil { + return err + } + if !issueType.IsValidWithCustom(customTypes) { return fmt.Errorf("invalid key %q: unknown issue type %q (use a built-in type, or add it to types.custom first)", key, suffix) } if _, err := types.ParseStorageClass(value); err != nil { diff --git a/cmd/bd/create_proxied_server.go b/cmd/bd/create_proxied_server.go index 70e725a85..dcc409dd6 100644 --- a/cmd/bd/create_proxied_server.go +++ b/cmd/bd/create_proxied_server.go @@ -11,7 +11,6 @@ import ( "github.com/spf13/cobra" - "github.com/steveyegge/beads/internal/config" "github.com/steveyegge/beads/internal/storage" "github.com/steveyegge/beads/internal/storage/domain" "github.com/steveyegge/beads/internal/storage/uow" @@ -21,13 +20,6 @@ import ( "github.com/steveyegge/beads/issueops" ) -func resolveProxiedCustomTypes(dbTypes []string) []string { - if len(dbTypes) > 0 { - return dbTypes - } - return config.GetCustomTypesFromYAML() -} - func runCreateProxiedServer(cmd *cobra.Command, ctx context.Context, in createInput) error { if in.repoOverrideSet { // Defense in depth: validateProxyCapabilitiesBeforeProvider already @@ -394,7 +386,7 @@ func runCreateProxiedGraph(_ *cobra.Command, ctx context.Context, in createInput // The returned useWisp is the plan-wide table routing decision. func validateProxiedGraphPlan(plan *GraphApplyPlan, in createInput, cctx domain.CreateContext, issueExists func(id string) (bool, error)) (useWisp bool, err error) { cfg := graphPlanConfig{ - customTypes: resolveProxiedCustomTypes(cctx.CustomTypes), + customTypes: cctx.CustomTypes, // No YAML fallback for statuses — the server database is authoritative // (that's where 'bd config set status.custom' writes) and statuses are // store-only everywhere (single-issue create, list filters), unlike diff --git a/cmd/bd/create_proxied_server_test.go b/cmd/bd/create_proxied_server_test.go index b06bd6b7f..a1a327920 100644 --- a/cmd/bd/create_proxied_server_test.go +++ b/cmd/bd/create_proxied_server_test.go @@ -3,13 +3,10 @@ package main import ( "encoding/json" "fmt" - "os" - "path/filepath" "reflect" "testing" "time" - "github.com/steveyegge/beads/internal/config" "github.com/steveyegge/beads/internal/types" "github.com/steveyegge/beads/issueops" ) @@ -473,55 +470,3 @@ func TestParseMarkdownDependencies_DoesNotSwapBlocks(t *testing.T) { t.Errorf("got %#v, want %#v (no swap-direction)", got, want) } } - -func TestResolveProxiedCustomTypes_PrefersDBTypes(t *testing.T) { - got := resolveProxiedCustomTypes([]string{"db-a", "db-b"}) - if !reflect.DeepEqual(got, []string{"db-a", "db-b"}) { - t.Errorf("got %#v, want [db-a db-b] — DB types must win when non-empty", got) - } -} - -func TestResolveProxiedCustomTypes_FallsBackToYAML(t *testing.T) { - restore := withTestYAMLCustomTypes(t, "molecule,gate,convoy") - defer restore() - - for _, dbTypes := range [][]string{nil, {}} { - got := resolveProxiedCustomTypes(dbTypes) - if !reflect.DeepEqual(got, []string{"molecule", "gate", "convoy"}) { - t.Errorf("dbTypes=%v: got %#v, want YAML fallback [molecule gate convoy]", dbTypes, got) - } - } -} - -func TestResolveProxiedCustomTypes_EmptyEverywhere(t *testing.T) { - restore := withTestYAMLCustomTypes(t, "") - defer restore() - - got := resolveProxiedCustomTypes(nil) - if len(got) != 0 { - t.Errorf("expected empty result, got %#v", got) - } -} - -func withTestYAMLCustomTypes(t *testing.T, customCSV string) func() { - t.Helper() - tmpDir := t.TempDir() - beadsDir := filepath.Join(tmpDir, ".beads") - if err := os.MkdirAll(beadsDir, 0o755); err != nil { - t.Fatalf("mkdir .beads: %v", err) - } - var content string - if customCSV != "" { - content = "types:\n custom: \"" + customCSV + "\"\n" - } - if err := os.WriteFile(filepath.Join(beadsDir, "config.yaml"), []byte(content), 0o644); err != nil { - t.Fatalf("write config.yaml: %v", err) - } - t.Chdir(tmpDir) - - config.ResetForTesting() - if err := config.Initialize(); err != nil { - t.Fatalf("config.Initialize: %v", err) - } - return func() { config.ResetForTesting() } -} diff --git a/cmd/bd/graph_apply.go b/cmd/bd/graph_apply.go index 05750e30f..9f40f7e6d 100644 --- a/cmd/bd/graph_apply.go +++ b/cmd/bd/graph_apply.go @@ -12,7 +12,6 @@ import ( "strings" "time" - "github.com/steveyegge/beads/internal/config" "github.com/steveyegge/beads/internal/storage" "github.com/steveyegge/beads/internal/types" "github.com/steveyegge/beads/internal/validation" @@ -305,15 +304,6 @@ func warnUnknownGraphFields(w io.Writer, unknown map[string][]string) []string { return hintFields } -func loadEmbeddedCustomTypes() []string { - if store != nil { - if ct, err := store.GetCustomTypes(rootCtx); err == nil && len(ct) > 0 { - return ct - } - } - return config.GetCustomTypesFromYAML() -} - // loadEmbeddedCustomStatuses reads custom statuses from the store only — no // YAML fallback, matching single-issue create and loadListFilterConfig // (custom types fall back to YAML; custom statuses deliberately do not). @@ -348,9 +338,13 @@ func createIssuesFromGraph(planFile string, dryRun bool, opts GraphApplyOptions) return HandleErrorRespectJSON("parsing graph plan: %v", err) } + customTypes, err := resolveWorkspaceCustomTypes(rootCtx) + if err != nil { + return HandleErrorRespectJSON("%v", err) + } dbPrefix, allowedPrefixes := loadEmbeddedIDPrefixes() cfg := graphPlanConfig{ - customTypes: loadEmbeddedCustomTypes(), + customTypes: customTypes, customStatuses: loadEmbeddedCustomStatuses(), dbPrefix: dbPrefix, allowedPrefixes: allowedPrefixes, diff --git a/cmd/bd/types.go b/cmd/bd/types.go index a10d65a52..727833260 100644 --- a/cmd/bd/types.go +++ b/cmd/bd/types.go @@ -6,7 +6,9 @@ import ( "strings" "github.com/spf13/cobra" + "github.com/steveyegge/beads/internal/config" "github.com/steveyegge/beads/internal/metrics" + "github.com/steveyegge/beads/internal/storage/issueops" "github.com/steveyegge/beads/internal/types" ) @@ -26,14 +28,45 @@ var coreWorkTypes = []struct { {types.TypeMilestone, "Marks completion of a set of related issues (contains no work itself)"}, } +// systemTypeDescriptions describes the built-in types that are not core work +// types. bd's own commands create them (mail, molecules, gates, set-state), and +// like the core types they validate without any types.custom entry. +var systemTypeDescriptions = map[types.IssueType]string{ + types.TypeMessage: "Message between agents or users", + types.TypeMolecule: "Molecule root for swarm coordination (bd mol)", + types.TypeGate: "Async coordination gate (bd gate, formula gates)", + types.TypeEvent: "Audit-trail event (bd set-state)", +} + +// systemWorkTypes lists every built-in type that is not a core work type, in +// the types package's declaration order. Together with coreWorkTypes it covers +// exactly the types IssueType.IsBuiltIn accepts. +func systemWorkTypes() []typeInfo { + core := make(map[types.IssueType]bool, len(coreWorkTypes)) + for _, t := range coreWorkTypes { + core[t.Type] = true + } + var out []typeInfo + for _, t := range append(append([]types.IssueType{}, types.AllIssueTypes...), types.TypeEvent) { + if core[t] { + continue + } + out = append(out, typeInfo{Name: string(t), Description: systemTypeDescriptions[t]}) + } + return out +} + var typesCmd = &cobra.Command{ Use: "types", GroupID: "views", Short: "List valid issue types", Long: `List all valid issue types that can be used with bd create --type. -Core work types (bug, task, feature, chore, epic, decision, spike, story, milestone) are always valid. -Additional types require configuration via types.custom in .beads/config.yaml. +Core work types (bug, task, feature, chore, epic, decision, spike, story, milestone) +and system types (message, molecule, gate, event) are always valid. +Custom types are registered with 'bd config set types.custom' or declared under +types.custom in .beads/config.yaml; the list shown is exactly the set that +bd create --type accepts. Examples: bd types # List all types with descriptions @@ -55,32 +88,56 @@ Examples: return printSections(jsonOutput) } - if usesProxiedServer() { - return runTypesProxiedServer(rootCtx) - } - - if err := ensureDirectMode("types command requires direct database access"); err != nil { - return HandleError("%v", err) - } - - var customTypes []string - ctx := context.Background() - if store != nil { - if ct, err := store.GetCustomTypes(ctx); err == nil { - customTypes = ct + if !usesProxiedServer() { + if err := ensureDirectMode("types command requires direct database access"); err != nil { + return HandleError("%v", err) } } + customTypes, err := resolveWorkspaceCustomTypes(rootCtx) + if err != nil { + return HandleError("%v", err) + } return renderTypes(customTypes) }, } +// resolveWorkspaceCustomTypes returns the workspace's registered custom issue +// types in either storage mode. Both routes end in issueops.ComposeCustomTypes, +// the rule issue create/update validation also resolves through, so what +// `bd types` lists is exactly what `bd create --type` accepts. +func resolveWorkspaceCustomTypes(ctx context.Context) ([]string, error) { + if usesProxiedServer() { + uw, err := openProxiedListUOW(ctx) + if err != nil { + return nil, err + } + defer uw.Close(ctx) + customTypes, err := uw.ConfigUseCase().GetCustomTypes(ctx) + if err != nil { + return nil, fmt.Errorf("reading custom types: %w", err) + } + return customTypes, nil + } + if store == nil { + // No database is open (e.g. a --dry-run plan check): the config.yaml + // layer is the only one available, composed by the same rule. + return issueops.ComposeCustomTypes(nil, "", config.GetCustomTypesFromYAML()), nil + } + customTypes, err := store.GetCustomTypes(ctx) + if err != nil { + return nil, fmt.Errorf("reading custom types: %w", err) + } + return customTypes, nil +} + func renderTypes(customTypes []string) error { if jsonOutput { result := struct { CoreTypes []typeInfo `json:"core_types"` + SystemTypes []typeInfo `json:"system_types"` CustomTypes []string `json:"custom_types,omitempty"` - }{} + }{SystemTypes: systemWorkTypes()} for _, t := range coreWorkTypes { result.CoreTypes = append(result.CoreTypes, typeInfo{ @@ -97,6 +154,11 @@ func renderTypes(customTypes []string) error { fmt.Printf(" %-14s %s\n", t.Type, t.Description) } + fmt.Println("\nSystem types (built-in, created by bd commands):") + for _, t := range systemWorkTypes() { + fmt.Printf(" %-14s %s\n", t.Name, t.Description) + } + if len(customTypes) > 0 { fmt.Println("\nConfigured custom types:") for _, t := range customTypes { diff --git a/cmd/bd/types_builtin_test.go b/cmd/bd/types_builtin_test.go new file mode 100644 index 000000000..bed351643 --- /dev/null +++ b/cmd/bd/types_builtin_test.go @@ -0,0 +1,40 @@ +package main + +import ( + "testing" + + "github.com/steveyegge/beads/internal/types" +) + +// TestBdTypesListsEveryBuiltIn pins that `bd types` (core + system sections) +// lists exactly the types that validate without configuration, i.e. the set +// IssueType.IsBuiltIn accepts, each with a description. +func TestBdTypesListsEveryBuiltIn(t *testing.T) { + listed := map[types.IssueType]bool{} + for _, ct := range coreWorkTypes { + if listed[ct.Type] { + t.Errorf("core type %q listed twice", ct.Type) + } + listed[ct.Type] = true + } + for _, st := range systemWorkTypes() { + it := types.IssueType(st.Name) + if listed[it] { + t.Errorf("type %q listed as both core and system", it) + } + if st.Description == "" { + t.Errorf("system type %q has no description", it) + } + listed[it] = true + } + for it := range listed { + if !it.IsBuiltIn() { + t.Errorf("bd types lists %q as built-in, but IsBuiltIn rejects it", it) + } + } + for _, it := range append(append([]types.IssueType{}, types.AllIssueTypes...), types.TypeEvent) { + if !listed[it] { + t.Errorf("built-in type %q validates without configuration but bd types does not list it", it) + } + } +} diff --git a/cmd/bd/types_create_agreement_test.go b/cmd/bd/types_create_agreement_test.go new file mode 100644 index 000000000..a371dab96 --- /dev/null +++ b/cmd/bd/types_create_agreement_test.go @@ -0,0 +1,130 @@ +//go:build cgo + +package main + +import ( + "encoding/json" + "os" + "os/exec" + "path/filepath" + "strings" + "testing" +) + +// typesAgreementRunner runs bd in a workspace and returns stdout (or stdout+stderr on +// failure) plus the exit error. +type typesAgreementRunner func(t *testing.T, args ...string) ([]byte, error) + +// assertTypesAgreeWithCreate is the `bd types` ⇔ `bd create --type` contract: +// every type `bd types --json` lists is accepted by create and by +// storage-class config validation, and an unlisted type is rejected. +// Custom types are seeded from both sources — the database (bd config set) +// and the project's .beads/config.yaml — because each storage mode resolves +// them; the listing and validation paths must compose them identically. +func assertTypesAgreeWithCreate(t *testing.T, run typesAgreementRunner, beadsDir string) { + t.Helper() + + if out, err := run(t, "config", "set", "types.custom", "dbtype"); err != nil { + t.Fatalf("config set types.custom: %v\n%s", err, out) + } + cfgPath := filepath.Join(beadsDir, "config.yaml") + cfg, err := os.ReadFile(cfgPath) + if err != nil { + t.Fatalf("read config.yaml: %v", err) + } + if strings.Contains(string(cfg), "types.custom") || strings.Contains(string(cfg), "\ntypes:") { + t.Fatalf("fixture config.yaml already declares types; test assumes it does not:\n%s", cfg) + } + cfg = append(cfg, []byte("\ntypes.custom: \"yamltype\"\n")...) + if err := os.WriteFile(cfgPath, cfg, 0o644); err != nil { + t.Fatalf("write config.yaml: %v", err) + } + + out, err := run(t, "types", "--json") + if err != nil { + t.Fatalf("types --json: %v\n%s", err, out) + } + var listed struct { + CoreTypes []typeInfo `json:"core_types"` + SystemTypes []typeInfo `json:"system_types"` + CustomTypes []string `json:"custom_types"` + } + if err := json.Unmarshal(out, &listed); err != nil { + t.Fatalf("unmarshal types --json: %v\n%s", err, out) + } + names := map[string]bool{} + for _, ti := range append(listed.CoreTypes, listed.SystemTypes...) { + names[ti.Name] = true + } + for _, c := range listed.CustomTypes { + names[c] = true + } + + // Types create is known to accept must be listed. + for _, want := range []string{"task", "message", "molecule", "gate", "event", "dbtype", "yamltype"} { + if !names[want] { + t.Errorf("bd types does not list %q, but bd create accepts it; listed=%v", want, names) + } + } + + // Every listed type must be accepted by create ... + for name := range names { + if out, err := run(t, "create", "--silent", "--type", name, "agreement "+name); err != nil { + t.Errorf("bd types lists %q but bd create --type %s failed: %v\n%s", name, name, err, out) + } + } + // ... and by storage-class validation, which names issue types too. + for _, name := range listed.CustomTypes { + if out, err := run(t, "config", "set", "storage-class."+name, "versioned"); err != nil { + t.Errorf("bd types lists %q but config set storage-class.%s failed: %v\n%s", name, name, err, out) + } + } + + // An unlisted type is rejected. + if out, err := run(t, "create", "--silent", "--type", "notregistered", "agreement unlisted"); err == nil { + t.Errorf("bd create accepted unlisted type notregistered: %s", out) + } +} + +func TestProxiedServerTypesAgreeWithCreate(t *testing.T) { + requireSharedProxiedServer(t) + t.Parallel() + bd := buildEmbeddedBD(t) + p := newSharedProxiedProject(t, bd, "tac") + assertTypesAgreeWithCreate(t, func(t *testing.T, args ...string) ([]byte, error) { + return bdProxiedRun(t, bd, p.dir, args...) + }, p.beadsDir) +} + +func TestEmbeddedTypesAgreeWithCreate(t *testing.T) { + if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" { + t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt integration tests") + } + t.Parallel() + bd := buildEmbeddedBD(t) + dir, beadsDir, _ := bdInit(t, bd, "--prefix", "tac") + assertTypesAgreeWithCreate(t, func(t *testing.T, args ...string) ([]byte, error) { + return bdRunWithFlockRetry(t, bd, dir, args...) + }, beadsDir) +} + +// Server mode (`bd init --server` against an external dolt sql-server) lists +// through DoltStore.GetCustomTypes, the resolver that used to drop +// config.yaml types once the database had any. +func TestServerModeTypesAgreeWithCreate(t *testing.T) { + requireSharedProxiedServer(t) + t.Parallel() + bd := buildEmbeddedBD(t) + p := newServerModeProject(t, bd, "tas") + assertTypesAgreeWithCreate(t, func(t *testing.T, args ...string) ([]byte, error) { + t.Helper() + cmd := exec.Command(bd, args...) + cmd.Dir = p.dir + cmd.Env = p.env + stdout, stderr, err := runCommandBuffers(t, cmd) + if err != nil { + return append(stdout.Bytes(), stderr.Bytes()...), err + } + return stdout.Bytes(), nil + }, p.beadsDir) +} diff --git a/cmd/bd/types_proxied_server.go b/cmd/bd/types_proxied_server.go deleted file mode 100644 index cefb89af6..000000000 --- a/cmd/bd/types_proxied_server.go +++ /dev/null @@ -1,20 +0,0 @@ -package main - -import ( - "context" -) - -func runTypesProxiedServer(ctx context.Context) error { - uw, err := openProxiedListUOW(ctx) - if err != nil { - return HandleError("%v", err) - } - defer uw.Close(ctx) - - var customTypes []string - if ct, err := uw.ConfigUseCase().GetCustomTypes(ctx); err == nil { - customTypes = ct - } - - return renderTypes(customTypes) -} diff --git a/internal/storage/domain/db/config.go b/internal/storage/domain/db/config.go index 8436a09d5..19115ba8e 100644 --- a/internal/storage/domain/db/config.go +++ b/internal/storage/domain/db/config.go @@ -126,22 +126,25 @@ func (r *configSQLRepositoryImpl) GetAllConfig(ctx context.Context) (map[string] return out, nil } +// GetCustomTypes resolves the workspace's custom issue types through +// issueops.ComposeCustomTypes, the same rule the embedded and server-mode +// stores use, so proxied `bd types` and proxied create/update validation +// accept exactly the same set. func (r *configSQLRepositoryImpl) GetCustomTypes(ctx context.Context) ([]string, error) { fromTable, err := r.readCustomTypesTable(ctx) if err != nil { return nil, err } - fromDB := fromTable - if len(fromDB) == 0 { - fromConfig, err := r.readCustomTypesConfig(ctx) + var configValue string + if len(fromTable) == 0 { + configValue, err = r.GetConfig(ctx, "types.custom") if err != nil { - return nil, err + return nil, fmt.Errorf("db: GetCustomTypes: %w", err) } - fromDB = fromConfig } - return unionWithYAMLCustomTypes(fromDB, config.GetCustomTypesFromYAML()), nil + return issueops.ComposeCustomTypes(fromTable, configValue, config.GetCustomTypesFromYAML()), nil } func (r *configSQLRepositoryImpl) readCustomTypesTable(ctx context.Context) ([]string, error) { @@ -169,39 +172,6 @@ func (r *configSQLRepositoryImpl) readCustomTypesTable(ctx context.Context) ([]s return out, nil } -func (r *configSQLRepositoryImpl) readCustomTypesConfig(ctx context.Context) ([]string, error) { - value, err := r.GetConfig(ctx, "types.custom") - if err != nil { - return nil, fmt.Errorf("db: GetCustomTypes: %w", err) - } - return issueops.ParseTypesConfigValue(value), nil -} - -func unionWithYAMLCustomTypes(dbTypes, yamlTypes []string) []string { - if len(dbTypes) == 0 && len(yamlTypes) == 0 { - return nil - } - seen := make(map[string]struct{}, len(dbTypes)+len(yamlTypes)) - out := make([]string, 0, len(dbTypes)+len(yamlTypes)) - for _, src := range [][]string{dbTypes, yamlTypes} { - for _, t := range src { - t = strings.TrimSpace(t) - if t == "" { - continue - } - if _, ok := seen[t]; ok { - continue - } - seen[t] = struct{}{} - out = append(out, t) - } - } - if len(out) == 0 { - return nil - } - return out -} - func (r *configSQLRepositoryImpl) GetAllowedPrefixes(ctx context.Context) (string, error) { value, err := r.GetConfig(ctx, "allowed_prefixes") if err != nil { diff --git a/internal/storage/issueops/BUILD.bazel b/internal/storage/issueops/BUILD.bazel index 32fb4d17e..12dd98b64 100644 --- a/internal/storage/issueops/BUILD.bazel +++ b/internal/storage/issueops/BUILD.bazel @@ -132,6 +132,7 @@ go_test( "create_only_errors_test.go", "create_only_guard_test.go", "create_test.go", + "custom_types_parity_test.go", "cycle_report_test.go", "cycles_test.go", "delete_cascade_tables_test.go", diff --git a/internal/storage/issueops/config_helpers.go b/internal/storage/issueops/config_helpers.go index d21de3b98..f17d858d4 100644 --- a/internal/storage/issueops/config_helpers.go +++ b/internal/storage/issueops/config_helpers.go @@ -56,7 +56,7 @@ func ResolveCustomConfigInTx(ctx context.Context, tx DBTX) (statuses []types.Cus return nil, nil, err } if statusesFromTable && typesFromTable { - return statuses, customTypes, nil + return statuses, ComposeCustomTypes(customTypes, "", config.GetCustomTypesFromYAML()), nil } cfg, err := getConfigKeysInTx(ctx, tx, "status.custom", "types.custom") @@ -66,12 +66,8 @@ func ResolveCustomConfigInTx(ctx context.Context, tx DBTX) (statuses []types.Cus statuses = ParseStatusFallback(yamlStatuses) } } - if !typesFromTable { - if yamlTypes := config.GetCustomTypesFromYAML(); len(yamlTypes) > 0 { - customTypes = yamlTypes - } - } - return statuses, customTypes, nil + // The types.custom row is unreadable; the remaining layers still compose. + return statuses, ComposeCustomTypes(customTypes, "", config.GetCustomTypesFromYAML()), nil } if !statusesFromTable { @@ -83,14 +79,7 @@ func ResolveCustomConfigInTx(ctx context.Context, tx DBTX) (statuses []types.Cus statuses = ParseStatusFallback(yamlStatuses) } } - if !typesFromTable { - if v := cfg["types.custom"]; v != "" { - customTypes = ParseTypesConfigValue(v) - } else if yamlTypes := config.GetCustomTypesFromYAML(); len(yamlTypes) > 0 { - customTypes = yamlTypes - } - } - return statuses, customTypes, nil + return statuses, ComposeCustomTypes(customTypes, cfg["types.custom"], config.GetCustomTypesFromYAML()), nil } func resolveCustomStatusesFromTableInTx(ctx context.Context, tx DBTX) ([]types.CustomStatus, bool, error) { @@ -280,21 +269,42 @@ func ResolveCustomTypesInTx(ctx context.Context, tx DBTX) ([]string, error) { // fallback. The table-empty case is intentional: schema migration creates the // table but may not have populated it from the existing types.custom config // string yet. + var configValue string if len(fromDB) == 0 { value, err := GetConfigInTx(ctx, tx, "types.custom") if err != nil { return customTypesYAMLFallback(config.GetCustomTypesFromYAML, err) } - if value != "" { - fromDB = ParseTypesConfigValue(value) - } + configValue = value } + return ComposeCustomTypes(fromDB, configValue, config.GetCustomTypesFromYAML()), nil +} - // Overlay-union with YAML types.custom regardless of the DB-side result. - // gastownhall/beads#4024: project-extension types declared in .beads/config.yaml - // must validate server-side even when the database side hasn't been migrated - // or populated with those types. - return mergeWithYAMLCustomTypes(fromDB, config.GetCustomTypesFromYAML), nil +// ComposeCustomTypes is the single rule for which custom issue types a +// workspace has. Every reader of the three sources — the resolvers here, the +// proxied-server config repository, and therefore both `bd types` and +// create/update validation in every mode — composes them through this +// function, so the listed set and the accepted set cannot drift apart: +// +// 1. the custom_types table, when it has rows; +// 2. otherwise the types.custom config row (CSV or JSON array), which covers +// databases whose table has not been populated yet; +// 3. always unioned with config.yaml types.custom, the project-extension +// overlay (gastownhall/beads#4024). +// +// Names are trimmed, empties dropped and duplicates removed; database types +// keep their order and YAML-only types are appended in declared order. +// Returns nil when no layer supplies a type. +func ComposeCustomTypes(fromTable []string, configValue string, yamlTypes []string) []string { + fromDB := dedupePreservingOrder(fromTable) + if len(fromDB) == 0 && configValue != "" { + fromDB = dedupePreservingOrder(ParseTypesConfigValue(configValue)) + } + merged := mergeWithYAMLCustomTypes(fromDB, func() []string { return yamlTypes }) + if len(merged) == 0 { + return nil + } + return merged } // mergeWithYAMLCustomTypes returns the union of dbTypes and the YAML-declared diff --git a/internal/storage/issueops/custom_types_parity_test.go b/internal/storage/issueops/custom_types_parity_test.go new file mode 100644 index 000000000..f4147ccc0 --- /dev/null +++ b/internal/storage/issueops/custom_types_parity_test.go @@ -0,0 +1,173 @@ +package issueops + +import ( + "context" + "os" + "path/filepath" + "reflect" + "regexp" + "testing" + + "github.com/DATA-DOG/go-sqlmock" + "github.com/steveyegge/beads/internal/config" +) + +// withYAMLCustomTypes points the process config at a throwaway +// .beads/config.yaml declaring types.custom, with HOME and XDG_CONFIG_HOME +// isolated so no user-level config can leak in. +func withYAMLCustomTypes(t *testing.T, customCSV string) { + t.Helper() + dir := t.TempDir() + t.Setenv("HOME", filepath.Join(dir, "home")) + t.Setenv("XDG_CONFIG_HOME", filepath.Join(dir, "xdg")) + t.Setenv("BEADS_DIR", "") + beadsDir := filepath.Join(dir, ".beads") + if err := os.MkdirAll(beadsDir, 0o755); err != nil { + t.Fatalf("mkdir .beads: %v", err) + } + content := "" + if customCSV != "" { + content = "types:\n custom: \"" + customCSV + "\"\n" + } + if err := os.WriteFile(filepath.Join(beadsDir, "config.yaml"), []byte(content), 0o644); err != nil { + t.Fatalf("write config.yaml: %v", err) + } + t.Chdir(dir) + config.ResetForTesting() + if err := config.Initialize(); err != nil { + t.Fatalf("config.Initialize: %v", err) + } + t.Cleanup(config.ResetForTesting) +} + +// customTypeSources describes one database + config.yaml state. +type customTypeSources struct { + table []string // custom_types rows + configRow string // config types.custom value ("" = unset) + yaml string // config.yaml types.custom CSV ("" = unset) +} + +func expectCustomTypesTable(mock sqlmock.Sqlmock, names []string) { + rows := sqlmock.NewRows([]string{"name"}) + for _, n := range names { + rows.AddRow(n) + } + mock.ExpectQuery(regexp.QuoteMeta("SELECT name FROM custom_types ORDER BY name")).WillReturnRows(rows) +} + +// resolveViaCustomTypesInTx is the resolver issue create/update validation uses. +func resolveViaCustomTypesInTx(t *testing.T, src customTypeSources) []string { + t.Helper() + db, mock, tx := beginMockTx(t) + defer db.Close() + expectCustomTypesTable(mock, src.table) + if len(src.table) == 0 { + rows := sqlmock.NewRows([]string{"value"}) + if src.configRow != "" { + rows.AddRow(src.configRow) + } + mock.ExpectQuery(regexp.QuoteMeta("SELECT value FROM config WHERE `key` = ?")). + WithArgs("types.custom").WillReturnRows(rows) + } + got, err := ResolveCustomTypesInTx(context.Background(), tx) + if err != nil { + t.Fatalf("ResolveCustomTypesInTx: %v", err) + } + if err := mock.ExpectationsWereMet(); err != nil { + t.Fatalf("unmet SQL expectations: %v", err) + } + return got +} + +// resolveViaCustomConfigInTx is the resolver the server-mode store's +// GetCustomTypes (and so direct-mode `bd types`) uses. +func resolveViaCustomConfigInTx(t *testing.T, src customTypeSources) []string { + t.Helper() + db, mock, tx := beginMockTx(t) + defer db.Close() + // One custom status so the status half resolves from its table and the + // config-row read below is driven by the types half alone. + mock.ExpectQuery(regexp.QuoteMeta("SELECT name, category FROM custom_statuses ORDER BY name")). + WillReturnRows(sqlmock.NewRows([]string{"name", "category"}).AddRow("review", "wip")) + expectCustomTypesTable(mock, src.table) + if len(src.table) == 0 { + rows := sqlmock.NewRows([]string{"key", "value"}) + if src.configRow != "" { + rows.AddRow("types.custom", src.configRow) + } + mock.ExpectQuery(regexp.QuoteMeta("SELECT `key`, value FROM config WHERE `key` IN (?,?)")). + WithArgs("status.custom", "types.custom").WillReturnRows(rows) + } + _, got, err := ResolveCustomConfigInTx(context.Background(), tx) + if err != nil { + t.Fatalf("ResolveCustomConfigInTx: %v", err) + } + if err := mock.ExpectationsWereMet(); err != nil { + t.Fatalf("unmet SQL expectations: %v", err) + } + return got +} + +// TestCustomTypeResolversAgree pins the single composition rule for custom +// issue types (custom_types table, else the types.custom config row, always +// unioned with config.yaml types.custom). The listing path (`bd types`) and +// the validation path (create/update) must resolve the same set, or `bd types` +// advertises types that create rejects or vice versa. +func TestCustomTypeResolversAgree(t *testing.T) { + cases := []struct { + name string + src customTypeSources + want []string + }{ + {"nothing configured", customTypeSources{}, nil}, + {"table only", customTypeSources{table: []string{"convoy", "rig"}}, []string{"convoy", "rig"}}, + {"config row only", customTypeSources{configRow: "convoy,rig"}, []string{"convoy", "rig"}}, + {"yaml only", customTypeSources{yaml: "gizmo"}, []string{"gizmo"}}, + {"table plus yaml", customTypeSources{table: []string{"convoy"}, yaml: "gizmo"}, []string{"convoy", "gizmo"}}, + {"config row plus yaml", customTypeSources{configRow: "convoy", yaml: "gizmo,convoy"}, []string{"convoy", "gizmo"}}, + {"table wins over config row", customTypeSources{table: []string{"convoy"}, configRow: "convoy,rig", yaml: "gizmo"}, []string{"convoy", "gizmo"}}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + withYAMLCustomTypes(t, tc.src.yaml) + validate := resolveViaCustomTypesInTx(t, tc.src) + list := resolveViaCustomConfigInTx(t, tc.src) + if len(validate) == 0 && len(tc.want) == 0 && len(list) == 0 { + return + } + if !reflect.DeepEqual(validate, tc.want) { + t.Errorf("ResolveCustomTypesInTx = %#v, want %#v", validate, tc.want) + } + if !reflect.DeepEqual(list, tc.want) { + t.Errorf("ResolveCustomConfigInTx custom types = %#v, want %#v (must match the validation resolver)", list, tc.want) + } + }) + } +} + +func TestComposeCustomTypes(t *testing.T) { + cases := []struct { + name string + table []string + configRow string + yaml []string + want []string + }{ + {"empty", nil, "", nil, nil}, + {"table trimmed and deduped", []string{" convoy ", "", "convoy", "rig"}, "", nil, []string{"convoy", "rig"}}, + {"config row json form", nil, `["convoy","rig"]`, nil, []string{"convoy", "rig"}}, + {"table ignores config row", []string{"convoy"}, "rig", nil, []string{"convoy"}}, + {"yaml appended after db", []string{"rig"}, "", []string{"gizmo", "rig"}, []string{"rig", "gizmo"}}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + got := ComposeCustomTypes(tc.table, tc.configRow, tc.yaml) + if len(got) == 0 && len(tc.want) == 0 { + return + } + if !reflect.DeepEqual(got, tc.want) { + t.Errorf("ComposeCustomTypes = %#v, want %#v", got, tc.want) + } + }) + } +}