fix(doctor): make --fix automatically migrate tombstones
When bd doctor detects legacy deletions.jsonl, --fix now runs the tombstone migration automatically instead of requiring users to manually run bd migrate-tombstones. This makes the migration smoother for multi-clone scenarios where only one clone needs to do the actual migration, but other clones may still have local deletions.jsonl files that need cleanup. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -384,7 +384,7 @@ func applyFixList(path string, fixes []doctorCheck) {
|
||||
case "JSONL Config":
|
||||
err = fix.LegacyJSONLConfig(path)
|
||||
case "Deletions Manifest":
|
||||
err = fix.HydrateDeletionsManifest(path)
|
||||
err = fix.MigrateTombstones(path)
|
||||
case "Untracked Files":
|
||||
err = fix.UntrackedJSONL(path)
|
||||
case "Sync Branch Health":
|
||||
|
||||
130
cmd/bd/doctor/fix/migrate_tombstones.go
Normal file
130
cmd/bd/doctor/fix/migrate_tombstones.go
Normal file
@@ -0,0 +1,130 @@
|
||||
package fix
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/steveyegge/beads/internal/deletions"
|
||||
"github.com/steveyegge/beads/internal/types"
|
||||
)
|
||||
|
||||
// MigrateTombstones converts legacy deletions.jsonl entries to inline tombstones.
|
||||
// This is called by bd doctor --fix when legacy deletions are detected.
|
||||
func MigrateTombstones(path string) error {
|
||||
if err := validateBeadsWorkspace(path); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
beadsDir := filepath.Join(path, ".beads")
|
||||
deletionsPath := filepath.Join(beadsDir, "deletions.jsonl")
|
||||
jsonlPath := filepath.Join(beadsDir, "issues.jsonl")
|
||||
|
||||
// Check if deletions.jsonl exists
|
||||
if _, err := os.Stat(deletionsPath); os.IsNotExist(err) {
|
||||
fmt.Println(" No deletions.jsonl found - already using tombstones")
|
||||
return nil
|
||||
}
|
||||
|
||||
// Load deletions
|
||||
loadResult, err := deletions.LoadDeletions(deletionsPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to load deletions: %w", err)
|
||||
}
|
||||
|
||||
if len(loadResult.Records) == 0 {
|
||||
fmt.Println(" deletions.jsonl is empty - nothing to migrate")
|
||||
return nil
|
||||
}
|
||||
|
||||
// Load existing JSONL to check for already-existing tombstones
|
||||
existingTombstones := make(map[string]bool)
|
||||
if file, err := os.Open(jsonlPath); err == nil {
|
||||
scanner := bufio.NewScanner(file)
|
||||
scanner.Buffer(make([]byte, 0, 64*1024), 10*1024*1024)
|
||||
for scanner.Scan() {
|
||||
var issue struct {
|
||||
ID string `json:"id"`
|
||||
Status string `json:"status"`
|
||||
}
|
||||
if err := json.Unmarshal(scanner.Bytes(), &issue); err == nil {
|
||||
if issue.Status == string(types.StatusTombstone) {
|
||||
existingTombstones[issue.ID] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
file.Close()
|
||||
}
|
||||
|
||||
// Convert deletions to tombstones
|
||||
var toMigrate []deletions.DeletionRecord
|
||||
var skipped int
|
||||
for _, record := range loadResult.Records {
|
||||
if existingTombstones[record.ID] {
|
||||
skipped++
|
||||
continue
|
||||
}
|
||||
toMigrate = append(toMigrate, record)
|
||||
}
|
||||
|
||||
if len(toMigrate) == 0 {
|
||||
fmt.Printf(" All %d deletion(s) already have tombstones - archiving deletions.jsonl\n", skipped)
|
||||
} else {
|
||||
// Append tombstones to issues.jsonl
|
||||
file, err := os.OpenFile(jsonlPath, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0644)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to open issues.jsonl: %w", err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
for _, record := range toMigrate {
|
||||
tombstone := convertDeletionToTombstone(record)
|
||||
data, err := json.Marshal(tombstone)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to marshal tombstone for %s: %w", record.ID, err)
|
||||
}
|
||||
if _, err := file.Write(append(data, '\n')); err != nil {
|
||||
return fmt.Errorf("failed to write tombstone for %s: %w", record.ID, err)
|
||||
}
|
||||
}
|
||||
fmt.Printf(" Migrated %d deletion(s) to tombstones\n", len(toMigrate))
|
||||
if skipped > 0 {
|
||||
fmt.Printf(" Skipped %d (already had tombstones)\n", skipped)
|
||||
}
|
||||
}
|
||||
|
||||
// Archive deletions.jsonl
|
||||
migratedPath := deletionsPath + ".migrated"
|
||||
if err := os.Rename(deletionsPath, migratedPath); err != nil {
|
||||
return fmt.Errorf("failed to archive deletions.jsonl: %w", err)
|
||||
}
|
||||
fmt.Printf(" Archived deletions.jsonl → deletions.jsonl.migrated\n")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// convertDeletionToTombstone converts a DeletionRecord to a tombstone Issue.
|
||||
func convertDeletionToTombstone(record deletions.DeletionRecord) *types.Issue {
|
||||
now := time.Now()
|
||||
deletedAt := record.Timestamp
|
||||
if deletedAt.IsZero() {
|
||||
deletedAt = now
|
||||
}
|
||||
|
||||
return &types.Issue{
|
||||
ID: record.ID,
|
||||
Title: "[Deleted]",
|
||||
Status: types.StatusTombstone,
|
||||
IssueType: types.TypeTask, // Default type for validation
|
||||
Priority: 0, // Unknown priority
|
||||
CreatedAt: deletedAt,
|
||||
UpdatedAt: now,
|
||||
DeletedAt: &deletedAt,
|
||||
DeletedBy: record.Actor,
|
||||
DeleteReason: record.Reason,
|
||||
OriginalType: string(types.TypeTask),
|
||||
}
|
||||
}
|
||||
185
cmd/bd/doctor/fix/migrate_tombstones_test.go
Normal file
185
cmd/bd/doctor/fix/migrate_tombstones_test.go
Normal file
@@ -0,0 +1,185 @@
|
||||
package fix
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/steveyegge/beads/internal/deletions"
|
||||
"github.com/steveyegge/beads/internal/types"
|
||||
)
|
||||
|
||||
func TestMigrateTombstones(t *testing.T) {
|
||||
// Setup: create temp .beads directory
|
||||
tmpDir := t.TempDir()
|
||||
beadsDir := filepath.Join(tmpDir, ".beads")
|
||||
if err := os.MkdirAll(beadsDir, 0755); err != nil {
|
||||
t.Fatalf("failed to create .beads dir: %v", err)
|
||||
}
|
||||
|
||||
deletionsPath := filepath.Join(beadsDir, "deletions.jsonl")
|
||||
jsonlPath := filepath.Join(beadsDir, "issues.jsonl")
|
||||
|
||||
// Create an issue in issues.jsonl
|
||||
issue := &types.Issue{
|
||||
ID: "test-abc",
|
||||
Title: "Test Issue",
|
||||
Status: types.StatusOpen,
|
||||
IssueType: types.TypeTask,
|
||||
CreatedAt: time.Now(),
|
||||
UpdatedAt: time.Now(),
|
||||
}
|
||||
issueData, _ := json.Marshal(issue)
|
||||
if err := os.WriteFile(jsonlPath, append(issueData, '\n'), 0644); err != nil {
|
||||
t.Fatalf("failed to write issues.jsonl: %v", err)
|
||||
}
|
||||
|
||||
// Create deletions.jsonl with one entry
|
||||
record := deletions.DeletionRecord{
|
||||
ID: "test-deleted",
|
||||
Timestamp: time.Now().Add(-time.Hour),
|
||||
Actor: "testuser",
|
||||
Reason: "test deletion",
|
||||
}
|
||||
if err := deletions.AppendDeletion(deletionsPath, record); err != nil {
|
||||
t.Fatalf("failed to create deletions.jsonl: %v", err)
|
||||
}
|
||||
|
||||
// Run migration
|
||||
err := MigrateTombstones(tmpDir)
|
||||
if err != nil {
|
||||
t.Fatalf("MigrateTombstones failed: %v", err)
|
||||
}
|
||||
|
||||
// Verify deletions.jsonl was archived
|
||||
if _, err := os.Stat(deletionsPath); !os.IsNotExist(err) {
|
||||
t.Error("deletions.jsonl should have been archived")
|
||||
}
|
||||
if _, err := os.Stat(deletionsPath + ".migrated"); os.IsNotExist(err) {
|
||||
t.Error("deletions.jsonl.migrated should exist")
|
||||
}
|
||||
|
||||
// Verify tombstone was added to issues.jsonl
|
||||
data, err := os.ReadFile(jsonlPath)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to read issues.jsonl: %v", err)
|
||||
}
|
||||
|
||||
// Should have 2 lines now (original issue + tombstone)
|
||||
lines := 0
|
||||
var foundTombstone bool
|
||||
for _, line := range splitLines(data) {
|
||||
if len(line) == 0 {
|
||||
continue
|
||||
}
|
||||
lines++
|
||||
var iss struct {
|
||||
ID string `json:"id"`
|
||||
Status string `json:"status"`
|
||||
}
|
||||
if err := json.Unmarshal(line, &iss); err == nil {
|
||||
if iss.ID == "test-deleted" && iss.Status == string(types.StatusTombstone) {
|
||||
foundTombstone = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if lines != 2 {
|
||||
t.Errorf("expected 2 lines in issues.jsonl, got %d", lines)
|
||||
}
|
||||
if !foundTombstone {
|
||||
t.Error("tombstone for test-deleted not found in issues.jsonl")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMigrateTombstones_SkipsExisting(t *testing.T) {
|
||||
// Setup: create temp .beads directory
|
||||
tmpDir := t.TempDir()
|
||||
beadsDir := filepath.Join(tmpDir, ".beads")
|
||||
if err := os.MkdirAll(beadsDir, 0755); err != nil {
|
||||
t.Fatalf("failed to create .beads dir: %v", err)
|
||||
}
|
||||
|
||||
deletionsPath := filepath.Join(beadsDir, "deletions.jsonl")
|
||||
jsonlPath := filepath.Join(beadsDir, "issues.jsonl")
|
||||
|
||||
// Create issues.jsonl with an existing tombstone
|
||||
tombstone := &types.Issue{
|
||||
ID: "test-already-tombstone",
|
||||
Title: "[Deleted]",
|
||||
Status: types.StatusTombstone,
|
||||
IssueType: types.TypeTask,
|
||||
CreatedAt: time.Now(),
|
||||
UpdatedAt: time.Now(),
|
||||
}
|
||||
tombstoneData, _ := json.Marshal(tombstone)
|
||||
if err := os.WriteFile(jsonlPath, append(tombstoneData, '\n'), 0644); err != nil {
|
||||
t.Fatalf("failed to write issues.jsonl: %v", err)
|
||||
}
|
||||
|
||||
// Create deletions.jsonl with the same ID
|
||||
record := deletions.DeletionRecord{
|
||||
ID: "test-already-tombstone",
|
||||
Timestamp: time.Now().Add(-time.Hour),
|
||||
Actor: "testuser",
|
||||
Reason: "test deletion",
|
||||
}
|
||||
if err := deletions.AppendDeletion(deletionsPath, record); err != nil {
|
||||
t.Fatalf("failed to create deletions.jsonl: %v", err)
|
||||
}
|
||||
|
||||
// Run migration
|
||||
err := MigrateTombstones(tmpDir)
|
||||
if err != nil {
|
||||
t.Fatalf("MigrateTombstones failed: %v", err)
|
||||
}
|
||||
|
||||
// Verify issues.jsonl still has only 1 line (no duplicate tombstone)
|
||||
data, err := os.ReadFile(jsonlPath)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to read issues.jsonl: %v", err)
|
||||
}
|
||||
|
||||
lines := 0
|
||||
for _, line := range splitLines(data) {
|
||||
if len(line) > 0 {
|
||||
lines++
|
||||
}
|
||||
}
|
||||
|
||||
if lines != 1 {
|
||||
t.Errorf("expected 1 line in issues.jsonl (existing tombstone), got %d", lines)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMigrateTombstones_NoDeletionsFile(t *testing.T) {
|
||||
// Setup: create temp .beads directory without deletions.jsonl
|
||||
tmpDir := t.TempDir()
|
||||
beadsDir := filepath.Join(tmpDir, ".beads")
|
||||
if err := os.MkdirAll(beadsDir, 0755); err != nil {
|
||||
t.Fatalf("failed to create .beads dir: %v", err)
|
||||
}
|
||||
|
||||
// Run migration - should succeed without error
|
||||
err := MigrateTombstones(tmpDir)
|
||||
if err != nil {
|
||||
t.Fatalf("MigrateTombstones failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func splitLines(data []byte) [][]byte {
|
||||
var lines [][]byte
|
||||
start := 0
|
||||
for i, b := range data {
|
||||
if b == '\n' {
|
||||
lines = append(lines, data[start:i])
|
||||
start = i + 1
|
||||
}
|
||||
}
|
||||
if start < len(data) {
|
||||
lines = append(lines, data[start:])
|
||||
}
|
||||
return lines
|
||||
}
|
||||
Reference in New Issue
Block a user