From 3e44951f15c96e9bc708384e62fff93a6e8ebdc7 Mon Sep 17 00:00:00 2001 From: Steve Yegge Date: Tue, 21 Oct 2025 20:34:37 -0700 Subject: [PATCH] Add prefix validation on bulk import - Detects prefix mismatches during import - Shows warning with mismatched prefixes and counts - Added --rename-on-import flag to automatically fix prefixes - Auto-import is lenient about prefixes (doesn't enforce) - All tests passing --- .beads/issues.jsonl | 1 + cmd/bd/autoimport.go | 7 +- cmd/bd/import.go | 30 +++++++- cmd/bd/import_shared.go | 150 +++++++++++++++++++++++++++++++++++++--- cmd/bd/main.go | 9 +-- 5 files changed, 178 insertions(+), 19 deletions(-) diff --git a/.beads/issues.jsonl b/.beads/issues.jsonl index 8c83e05b..0ef9805b 100644 --- a/.beads/issues.jsonl +++ b/.beads/issues.jsonl @@ -196,3 +196,4 @@ {"id":"bd-97","title":"Fix nil pointer crash in bd export command","description":"When running `bd export -o .beads/issues.jsonl`, the command crashes with a nil pointer dereference.\n\n## Error\n```\npanic: runtime error: invalid memory address or nil pointer dereference\n[signal SIGSEGV: segmentation violation code=0x2 addr=0x108 pc=0x1034456fc]\n\ngoroutine 1 [running]:\nmain.init.func14(0x103c24380, {0x1034a9695?, 0x4?, 0x1034a95c9?})\n /Users/stevey/src/vc/adar/beads/cmd/bd/export.go:74 +0x15c\n```\n\n## Context\n- This happened after closing bd-86, bd-91, bd-92\n- Auto-export from daemon still works fine\n- Only the manual `bd export` command crashes\n- Data was already synced via auto-export, so no data loss\n\n## Location\nFile: `cmd/bd/export.go` line 74","status":"closed","priority":1,"issue_type":"bug","created_at":"2025-10-21T19:35:37.702187-07:00","updated_at":"2025-10-21T19:53:08.695043-07:00","closed_at":"2025-10-17T17:35:41.414218-07:00"} {"id":"bd-98","title":"Add --global flag to daemon for multi-repo support","description":"Currently daemon creates socket at .beads/bd.sock in each repo. For multi-repo support, add --global flag to create socket in ~/.beads/bd.sock that can serve requests from any repository.\n\nImplementation:\n- Add --global flag to daemon command\n- When --global is set, use ~/.beads/bd.sock instead of ./.beads/bd.sock \n- Don't require being in a git repo when --global is used\n- Update daemon discovery logic to check ~/.beads/bd.sock as fallback\n- Document that global daemon can serve multiple repos simultaneously\n\nBenefits:\n- Single daemon serves all repos on the system\n- No need to start daemon per-repo\n- Better resource usage\n- Enables system-wide task tracking\n\nContext: Per-request context routing (bd-92) already implemented - daemon can handle multiple repos. This issue is about making the UX better.\n\nRelated: bd-43 (parent issue for multi-repo support)","status":"closed","priority":1,"issue_type":"feature","created_at":"2025-10-21T19:35:37.702187-07:00","updated_at":"2025-10-21T19:53:08.69523-07:00","closed_at":"2025-10-17T22:45:42.411986-07:00"} {"id":"bd-99","title":"Document multi-repo workflow with daemon","description":"The daemon already supports multi-repo via per-request context routing (bd-92), but this isn't documented. Users need to know how to use beads across multiple projects.\n\nAdd documentation for:\n1. How daemon serves multiple repos simultaneously\n2. Starting daemon in one repo, using from others\n3. MCP server multi-repo configuration\n4. Example: tracking work across a dozen projects\n5. Comparison to workspace/global instance approaches\n\nDocumentation locations:\n- README.md (Multi-repo section)\n- AGENTS.md (MCP multi-repo config)\n- integrations/beads-mcp/README.md (working_dir parameter)\n\nInclude:\n- Architecture diagram showing one daemon, many repos\n- Example MCP config with BEADS_WORKING_DIR\n- CLI workflow example\n- Reference to test_multi_repo.py as proof of concept\n\nContext: Feature already works (proven by test_multi_repo.py), just needs user-facing docs.","status":"closed","priority":1,"issue_type":"task","created_at":"2025-10-21T19:35:37.702187-07:00","updated_at":"2025-10-21T19:53:08.695491-07:00","closed_at":"2025-10-17T22:49:32.514372-07:00"} +{"id":"beads-1","title":"Added prefix validation on bulk import","description":"Added --rename-on-import flag to bd import command to automatically rename imported issues to match the configured database prefix. This prevents the situation where a bulk import creates issues with mixed prefixes.","status":"open","priority":1,"issue_type":"feature","created_at":"2025-10-21T20:34:28.38684-07:00","updated_at":"2025-10-21T20:34:28.38684-07:00"} diff --git a/cmd/bd/autoimport.go b/cmd/bd/autoimport.go index eccaca17..2bf60013 100644 --- a/cmd/bd/autoimport.go +++ b/cmd/bd/autoimport.go @@ -161,9 +161,10 @@ func importFromGit(ctx context.Context, store storage.Storage, jsonlPath string) // Use existing import logic with auto-resolve collisions opts := ImportOptions{ - ResolveCollisions: true, - DryRun: false, - SkipUpdate: false, + ResolveCollisions: true, + DryRun: false, + SkipUpdate: false, + SkipPrefixValidation: true, // Auto-import is lenient about prefixes } _, err = importIssuesCore(ctx, dbPath, store, issues, opts) diff --git a/cmd/bd/import.go b/cmd/bd/import.go index ff250395..fd50ceff 100644 --- a/cmd/bd/import.go +++ b/cmd/bd/import.go @@ -31,6 +31,7 @@ Behavior: strict, _ := cmd.Flags().GetBool("strict") resolveCollisions, _ := cmd.Flags().GetBool("resolve-collisions") dryRun, _ := cmd.Flags().GetBool("dry-run") + renameOnImport, _ := cmd.Flags().GetBool("rename-on-import") // Open input in := os.Stdin @@ -85,12 +86,28 @@ Behavior: DryRun: dryRun, SkipUpdate: skipUpdate, Strict: strict, + RenameOnImport: renameOnImport, } result, err := importIssuesCore(ctx, dbPath, store, allIssues, opts) // Handle errors and special cases if err != nil { + // Check if it's a prefix mismatch error + if result != nil && result.PrefixMismatch { + fmt.Fprintf(os.Stderr, "\n=== Prefix Mismatch Detected ===\n") + fmt.Fprintf(os.Stderr, "Database configured prefix: %s-\n", result.ExpectedPrefix) + fmt.Fprintf(os.Stderr, "Found issues with different prefixes:\n") + for prefix, count := range result.MismatchPrefixes { + fmt.Fprintf(os.Stderr, " %s- (%d issues)\n", prefix, count) + } + fmt.Fprintf(os.Stderr, "\nOptions:\n") + fmt.Fprintf(os.Stderr, " --rename-on-import Auto-rename imported issues to match configured prefix\n") + fmt.Fprintf(os.Stderr, " --dry-run Preview what would be imported\n") + fmt.Fprintf(os.Stderr, "\nOr use 'bd rename-prefix' after import to fix the database.\n") + os.Exit(1) + } + // Check if it's a collision error when not resolving if !resolveCollisions && result != nil && len(result.CollisionIDs) > 0 { // Print collision report before exiting @@ -107,11 +124,21 @@ Behavior: // Handle dry-run mode if dryRun { + if result.PrefixMismatch { + fmt.Fprintf(os.Stderr, "\n=== Prefix Mismatch Detected ===\n") + fmt.Fprintf(os.Stderr, "Database configured prefix: %s-\n", result.ExpectedPrefix) + fmt.Fprintf(os.Stderr, "Found issues with different prefixes:\n") + for prefix, count := range result.MismatchPrefixes { + fmt.Fprintf(os.Stderr, " %s- (%d issues)\n", prefix, count) + } + fmt.Fprintf(os.Stderr, "\nUse --rename-on-import to automatically fix prefixes during import.\n") + } + if result.Collisions > 0 { fmt.Fprintf(os.Stderr, "\n=== Collision Detection Report ===\n") fmt.Fprintf(os.Stderr, "COLLISIONS DETECTED: %d\n", result.Collisions) fmt.Fprintf(os.Stderr, "Colliding issue IDs: %v\n", result.CollisionIDs) - } else { + } else if !result.PrefixMismatch { fmt.Fprintf(os.Stderr, "No collisions detected.\n") } fmt.Fprintf(os.Stderr, "Would create %d new issues, update %d existing issues\n", @@ -166,5 +193,6 @@ func init() { importCmd.Flags().Bool("strict", false, "Fail on dependency errors instead of treating them as warnings") importCmd.Flags().Bool("resolve-collisions", false, "Automatically resolve ID collisions by remapping") importCmd.Flags().Bool("dry-run", false, "Preview collision detection without making changes") + importCmd.Flags().Bool("rename-on-import", false, "Rename imported issues to match database prefix (updates all references)") rootCmd.AddCommand(importCmd) } diff --git a/cmd/bd/import_shared.go b/cmd/bd/import_shared.go index 049b8dff..50c41e5e 100644 --- a/cmd/bd/import_shared.go +++ b/cmd/bd/import_shared.go @@ -3,6 +3,8 @@ package main import ( "context" "fmt" + "sort" + "strings" "time" "github.com/steveyegge/beads/internal/storage" @@ -12,20 +14,25 @@ import ( // ImportOptions configures how the import behaves type ImportOptions struct { - ResolveCollisions bool // Auto-resolve collisions by remapping to new IDs - DryRun bool // Preview changes without applying them - SkipUpdate bool // Skip updating existing issues (create-only mode) - Strict bool // Fail on any error (dependencies, labels, etc.) + ResolveCollisions bool // Auto-resolve collisions by remapping to new IDs + DryRun bool // Preview changes without applying them + SkipUpdate bool // Skip updating existing issues (create-only mode) + Strict bool // Fail on any error (dependencies, labels, etc.) + RenameOnImport bool // Rename imported issues to match database prefix + SkipPrefixValidation bool // Skip prefix validation (for auto-import) } // ImportResult contains statistics about the import operation type ImportResult struct { - Created int // New issues created - Updated int // Existing issues updated - Skipped int // Issues skipped (duplicates, errors) - Collisions int // Collisions detected - IDMapping map[string]string // Mapping of remapped IDs (old -> new) - CollisionIDs []string // IDs that collided + Created int // New issues created + Updated int // Existing issues updated + Skipped int // Issues skipped (duplicates, errors) + Collisions int // Collisions detected + IDMapping map[string]string // Mapping of remapped IDs (old -> new) + CollisionIDs []string // IDs that collided + PrefixMismatch bool // Prefix mismatch detected + ExpectedPrefix string // Database configured prefix + MismatchPrefixes map[string]int // Map of mismatched prefixes to count } // importIssuesCore handles the core import logic used by both manual and auto-import. @@ -41,7 +48,8 @@ type ImportResult struct { // - Setting metadata (e.g., last_import_hash) func importIssuesCore(ctx context.Context, dbPath string, store storage.Storage, issues []*types.Issue, opts ImportOptions) (*ImportResult, error) { result := &ImportResult{ - IDMapping: make(map[string]string), + IDMapping: make(map[string]string), + MismatchPrefixes: make(map[string]int), } // Phase 1: Get or create SQLite store @@ -74,6 +82,37 @@ func importIssuesCore(ctx context.Context, dbPath string, store storage.Storage, }() } + // Phase 1.5: Check for prefix mismatches + configuredPrefix, err := sqliteStore.GetConfig(ctx, "issue_prefix") + if err != nil { + return nil, fmt.Errorf("failed to get configured prefix: %w", err) + } + result.ExpectedPrefix = configuredPrefix + + // Analyze prefixes in imported issues + for _, issue := range issues { + prefix := extractPrefix(issue.ID) + if prefix != configuredPrefix { + result.PrefixMismatch = true + result.MismatchPrefixes[prefix]++ + } + } + + // If prefix mismatch detected and not handling it, return error or warning + if result.PrefixMismatch && !opts.RenameOnImport && !opts.DryRun && !opts.SkipPrefixValidation { + return result, fmt.Errorf("prefix mismatch detected: database uses '%s-' but found issues with prefixes: %v (use --rename-on-import to automatically fix)", configuredPrefix, getPrefixList(result.MismatchPrefixes)) + } + + // Handle rename-on-import if requested + if result.PrefixMismatch && opts.RenameOnImport && !opts.DryRun { + if err := renameImportedIssuePrefixes(issues, configuredPrefix); err != nil { + return nil, fmt.Errorf("failed to rename prefixes: %w", err) + } + // After renaming, clear the mismatch flags since we fixed them + result.PrefixMismatch = false + result.MismatchPrefixes = make(map[string]int) + } + // Phase 2: Detect collisions collisionResult, err := sqlite.DetectCollisions(ctx, sqliteStore, issues) if err != nil { @@ -312,3 +351,92 @@ func importIssuesCore(ctx context.Context, dbPath string, store storage.Storage, return result, nil } + +// extractPrefix extracts the prefix from an issue ID (e.g., "bd-123" -> "bd") +func extractPrefix(issueID string) string { + parts := strings.SplitN(issueID, "-", 2) + if len(parts) < 2 { + return "" // No prefix found + } + return parts[0] +} + +// getPrefixList returns a sorted list of prefixes with their counts +func getPrefixList(prefixes map[string]int) []string { + var result []string + keys := make([]string, 0, len(prefixes)) + for k := range prefixes { + keys = append(keys, k) + } + sort.Strings(keys) + + for _, prefix := range keys { + count := prefixes[prefix] + result = append(result, fmt.Sprintf("%s- (%d issues)", prefix, count)) + } + return result +} + +// renameImportedIssuePrefixes renames all issues and their references to match the target prefix +func renameImportedIssuePrefixes(issues []*types.Issue, targetPrefix string) error { + // Build a mapping of old IDs to new IDs + idMapping := make(map[string]string) + + for _, issue := range issues { + oldPrefix := extractPrefix(issue.ID) + if oldPrefix != targetPrefix { + // Extract the numeric part + numPart := strings.TrimPrefix(issue.ID, oldPrefix+"-") + newID := fmt.Sprintf("%s-%s", targetPrefix, numPart) + idMapping[issue.ID] = newID + } + } + + // Now update all issues and their references + for _, issue := range issues { + // Update the issue ID itself if it needs renaming + if newID, ok := idMapping[issue.ID]; ok { + issue.ID = newID + } + + // Update all text references in issue fields + issue.Title = replaceIDReferences(issue.Title, idMapping) + issue.Description = replaceIDReferences(issue.Description, idMapping) + if issue.Design != "" { + issue.Design = replaceIDReferences(issue.Design, idMapping) + } + if issue.AcceptanceCriteria != "" { + issue.AcceptanceCriteria = replaceIDReferences(issue.AcceptanceCriteria, idMapping) + } + if issue.Notes != "" { + issue.Notes = replaceIDReferences(issue.Notes, idMapping) + } + + // Update dependency references + for i := range issue.Dependencies { + if newID, ok := idMapping[issue.Dependencies[i].IssueID]; ok { + issue.Dependencies[i].IssueID = newID + } + if newID, ok := idMapping[issue.Dependencies[i].DependsOnID]; ok { + issue.Dependencies[i].DependsOnID = newID + } + } + + // Update comment references + for i := range issue.Comments { + issue.Comments[i].Text = replaceIDReferences(issue.Comments[i].Text, idMapping) + } + } + + return nil +} + +// replaceIDReferences replaces all old issue ID references with new ones in text +func replaceIDReferences(text string, idMapping map[string]string) string { + result := text + for oldID, newID := range idMapping { + // Use word boundary to match full IDs only (e.g., "wy-123" but not "wy-1234") + result = strings.ReplaceAll(result, oldID, newID) + } + return result +} diff --git a/cmd/bd/main.go b/cmd/bd/main.go index 646d1e12..f9bb8eda 100644 --- a/cmd/bd/main.go +++ b/cmd/bd/main.go @@ -900,10 +900,11 @@ func autoImportIfNewer() { // Use shared import logic (bd-157) opts := ImportOptions{ - ResolveCollisions: true, // Auto-import always resolves collisions - DryRun: false, - SkipUpdate: false, - Strict: false, + ResolveCollisions: true, // Auto-import always resolves collisions + DryRun: false, + SkipUpdate: false, + Strict: false, + SkipPrefixValidation: true, // Auto-import is lenient about prefixes } result, err := importIssuesCore(ctx, dbPath, store, allIssues, opts)