feat: add multi-prefix support via allowed_prefixes config (#881)

feat: add multi-prefix support via allowed_prefixes config

Adds support for allowing multiple prefixes in a beads database via the
allowed_prefixes config key. This is needed for Gas Town which uses different
prefixes for different purposes (hq-, gt-, rig-specific prefixes).

Usage: bd config set allowed_prefixes "gt,hq,hmc"

PR #881 by @web3dev1337
This commit is contained in:
web3dev1337
2026-01-05 06:13:44 +11:00
committed by GitHub
parent 16af63dc73
commit 661556ae62
3 changed files with 72 additions and 3 deletions

View File

@@ -285,21 +285,27 @@ var createCmd = &cobra.Command{
// Validate prefix matches database prefix
ctx := rootCtx
// Get database prefix from config
var dbPrefix string
// Get database prefix and allowed prefixes from config
var dbPrefix, allowedPrefixes string
if daemonClient != nil {
// Daemon mode - use RPC to get config
configResp, err := daemonClient.GetConfig(&rpc.GetConfigArgs{Key: "issue_prefix"})
if err == nil {
dbPrefix = configResp.Value
}
// Also get allowed_prefixes for multi-prefix support (e.g., Gas Town)
allowedResp, err := daemonClient.GetConfig(&rpc.GetConfigArgs{Key: "allowed_prefixes"})
if err == nil {
allowedPrefixes = allowedResp.Value
}
// If error, continue without validation (non-fatal)
} else {
// Direct mode - check config
dbPrefix, _ = store.GetConfig(ctx, "issue_prefix")
allowedPrefixes, _ = store.GetConfig(ctx, "allowed_prefixes")
}
if err := validation.ValidatePrefix(requestedPrefix, dbPrefix, forceCreate); err != nil {
if err := validation.ValidatePrefixWithAllowed(requestedPrefix, dbPrefix, allowedPrefixes, forceCreate); err != nil {
FatalError("%v", err)
}

View File

@@ -72,10 +72,36 @@ func ValidateIDFormat(id string) (string, error) {
// ValidatePrefix checks that the requested prefix matches the database prefix.
// Returns an error if they don't match (unless force is true).
func ValidatePrefix(requestedPrefix, dbPrefix string, force bool) error {
return ValidatePrefixWithAllowed(requestedPrefix, dbPrefix, "", force)
}
// ValidatePrefixWithAllowed checks that the requested prefix is allowed.
// It matches if:
// - force is true
// - dbPrefix is empty
// - requestedPrefix matches dbPrefix
// - requestedPrefix is in the comma-separated allowedPrefixes list
// Returns an error if none of these conditions are met.
func ValidatePrefixWithAllowed(requestedPrefix, dbPrefix, allowedPrefixes string, force bool) error {
if force || dbPrefix == "" || dbPrefix == requestedPrefix {
return nil
}
// Check if requestedPrefix is in the allowed list
if allowedPrefixes != "" {
for _, allowed := range strings.Split(allowedPrefixes, ",") {
allowed = strings.TrimSpace(allowed)
if allowed == requestedPrefix {
return nil
}
}
}
// Build helpful error message
if allowedPrefixes != "" {
return fmt.Errorf("prefix mismatch: database uses '%s' (allowed: %s) but you specified '%s' (use --force to override)",
dbPrefix, allowedPrefixes, requestedPrefix)
}
return fmt.Errorf("prefix mismatch: database uses '%s' but you specified '%s' (use --force to override)", dbPrefix, requestedPrefix)
}

View File

@@ -195,6 +195,43 @@ func TestValidatePrefix(t *testing.T) {
}
}
func TestValidatePrefixWithAllowed(t *testing.T) {
tests := []struct {
name string
requestedPrefix string
dbPrefix string
allowedPrefixes string
force bool
wantError bool
}{
// Basic cases (same as ValidatePrefix)
{"matching prefixes", "bd", "bd", "", false, false},
{"empty db prefix", "bd", "", "", false, false},
{"mismatched with force", "foo", "bd", "", true, false},
{"mismatched without force", "foo", "bd", "", false, true},
// Multi-prefix cases (Gas Town use case)
{"allowed prefix gt", "gt", "hq", "gt,hmc", false, false},
{"allowed prefix hmc", "hmc", "hq", "gt,hmc", false, false},
{"primary prefix still works", "hq", "hq", "gt,hmc", false, false},
{"prefix not in allowed list", "foo", "hq", "gt,hmc", false, true},
// Edge cases
{"allowed with spaces", "gt", "hq", "gt, hmc, foo", false, false},
{"empty allowed list", "gt", "hq", "", false, true},
{"single allowed prefix", "gt", "hq", "gt", false, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidatePrefixWithAllowed(tt.requestedPrefix, tt.dbPrefix, tt.allowedPrefixes, tt.force)
if (err != nil) != tt.wantError {
t.Errorf("ValidatePrefixWithAllowed() error = %v, wantError %v", err, tt.wantError)
}
})
}
}
func TestValidateAgentID(t *testing.T) {
tests := []struct {
name string