Fix bd-dvd and bd-ymj: Parent resurrection and export metadata

Bug 1 (bd-dvd): GetNextChildID now attempts parent resurrection from JSONL
before failing. Added TryResurrectParent call to match CreateIssue behavior.

Bug 2 (bd-ymj): Export now updates last_import_hash metadata to prevent
'JSONL content has changed' errors on subsequent exports.

Files changed:
- internal/storage/sqlite/hash_ids.go: Add resurrection attempt
- cmd/bd/daemon_sync.go: Add metadata updates after export
- Tests added for both fixes
- Fixed pre-existing bug in integrity_content_test.go

Follow-up work tracked in epic bd-ar2 (9 issues for improvements).

Fixes GH #334
This commit is contained in:
Steve Yegge
2025-11-21 10:29:30 -05:00
parent ff3ccdd26e
commit 4c5f99c5bd
6 changed files with 230 additions and 20 deletions

View File

@@ -129,8 +129,10 @@ func TestGetNextChildID_ParentNotExists(t *testing.T) {
if err == nil {
t.Errorf("expected error for non-existent parent, got nil")
}
if err != nil && err.Error() != "parent issue bd-nonexistent does not exist" {
t.Errorf("unexpected error message: %v", err)
// With resurrection feature (bd-dvd fix), error message includes JSONL history check
expectedErr := "parent issue bd-nonexistent does not exist and could not be resurrected from JSONL history"
if err != nil && err.Error() != expectedErr {
t.Errorf("unexpected error message: got %q, want %q", err.Error(), expectedErr)
}
}
@@ -203,3 +205,72 @@ func TestCreateIssue_HierarchicalID_ParentNotExists(t *testing.T) {
t.Errorf("unexpected error message: got %q, want %q", err.Error(), expectedErr)
}
}
func TestGetNextChildID_ResurrectParent(t *testing.T) {
tmpDir := t.TempDir()
tmpFile := tmpDir + "/test.db"
defer os.Remove(tmpFile)
store := newTestStore(t, tmpFile)
defer store.Close()
ctx := context.Background()
// Create parent issue
parent := &types.Issue{
ID: "bd-test123",
ContentHash: "abc123",
Title: "Parent Issue",
Description: "Parent to be resurrected",
Status: types.StatusOpen,
Priority: 1,
IssueType: types.TypeEpic,
}
if err := store.CreateIssue(ctx, parent, "test"); err != nil {
t.Fatalf("failed to create parent: %v", err)
}
// Delete the parent from database (simulating deletion)
if err := store.DeleteIssue(ctx, parent.ID); err != nil {
t.Fatalf("failed to delete parent: %v", err)
}
// Create JSONL file with the deleted parent (simulating JSONL history)
// Note: This requires the JSONL to be in .beads/issues.jsonl relative to dbPath
// The resurrection logic looks for issues.jsonl in the same directory as the database
beadsDir := tmpDir
jsonlPath := beadsDir + "/issues.jsonl"
// Write parent to JSONL
jsonlFile, err := os.Create(jsonlPath)
if err != nil {
t.Fatalf("failed to create JSONL file: %v", err)
}
parentJSON := `{"id":"bd-test123","content_hash":"abc123","title":"Parent Issue","description":"Parent to be resurrected","status":"open","priority":1,"type":"epic","created_at":"2025-01-01T00:00:00Z","updated_at":"2025-01-01T00:00:00Z"}`
if _, err := jsonlFile.WriteString(parentJSON + "\n"); err != nil {
jsonlFile.Close()
t.Fatalf("failed to write to JSONL: %v", err)
}
jsonlFile.Close()
// Now attempt to get next child ID - should resurrect parent
childID, err := store.GetNextChildID(ctx, parent.ID)
if err != nil {
t.Fatalf("GetNextChildID should have resurrected parent, but got error: %v", err)
}
expectedID := "bd-test123.1"
if childID != expectedID {
t.Errorf("expected child ID %s, got %s", expectedID, childID)
}
// Verify parent was resurrected as tombstone
resurrectedParent, err := store.GetIssue(ctx, parent.ID)
if err != nil {
t.Fatalf("failed to get resurrected parent: %v", err)
}
if resurrectedParent.Status != types.StatusClosed {
t.Errorf("expected resurrected parent to be closed, got %s", resurrectedParent.Status)
}
if resurrectedParent.Title != "Parent Issue" {
t.Errorf("expected resurrected parent title to be preserved, got %s", resurrectedParent.Title)
}
}

View File

@@ -34,7 +34,14 @@ func (s *SQLiteStorage) GetNextChildID(ctx context.Context, parentID string) (st
return "", fmt.Errorf("failed to check parent existence: %w", err)
}
if count == 0 {
return "", fmt.Errorf("parent issue %s does not exist", parentID)
// Try to resurrect parent from JSONL history before failing (bd-dvd fix)
resurrected, err := s.TryResurrectParent(ctx, parentID)
if err != nil {
return "", fmt.Errorf("failed to resurrect parent %s: %w", parentID, err)
}
if !resurrected {
return "", fmt.Errorf("parent issue %s does not exist and could not be resurrected from JSONL history", parentID)
}
}
// Calculate current depth by counting dots