Fix bd-3xq: Import gracefully handles missing parents

Implemented hybrid approach (topological sort + resurrection):

Phase 1: Import ordering (fixes latent bug)
- Sort issues by hierarchy depth before batch creation
- Create in depth-ordered batches (0→1→2→3)
- Ensures parents always created before children

Phase 2: Parent resurrection
- Attempt to resurrect missing parents from import batch
- Only fail if parent truly doesn't exist anywhere
- Enables deleted parent scenarios to work correctly

Benefits:
- Fixes import failure when parents deleted via bd-delete
- Handles parent-child pairs in same import batch
- Maintains referential integrity
- Enables multi-repo workflows with divergent deletion states

Amp-Thread-ID: https://ampcode.com/threads/T-14d3a206-aeac-4499-8ae9-47f3715e18fa
Co-authored-by: Amp <amp@ampcode.com>
This commit is contained in:
Steve Yegge
2025-11-04 12:39:43 -08:00
parent b7f2c34dcd
commit d38a312583
3 changed files with 45 additions and 23 deletions

View File

@@ -537,19 +537,24 @@ func upsertIssues(ctx context.Context, sqliteStore *sqlite.SQLiteStorage, issues
}
}
// Batch create all new issues with topological sorting
// Sort by hierarchy depth to ensure parents are created before children
// This prevents "parent does not exist" errors when importing hierarchical issues
if len(newIssues) > 0 {
SortByDepth(newIssues)
// Batch create all new issues
// Sort by hierarchy depth to ensure parents are created before children
if len(newIssues) > 0 {
sort.Slice(newIssues, func(i, j int) bool {
depthI := strings.Count(newIssues[i].ID, ".")
depthJ := strings.Count(newIssues[j].ID, ".")
if depthI != depthJ {
return depthI < depthJ // Shallower first
}
return newIssues[i].ID < newIssues[j].ID // Stable sort
})
// Create issues in depth-order batches (max depth 3)
// This handles parent-child pairs in the same import batch
// Create in batches by depth level (max depth 3)
for depth := 0; depth <= 3; depth++ {
var batchForDepth []*types.Issue
for _, issue := range newIssues {
if GetHierarchyDepth(issue.ID) == depth {
batchForDepth = append(batchForDepth, issue)
var batchForDepth []*types.Issue
for _, issue := range newIssues {
if strings.Count(issue.ID, ".") == depth {
batchForDepth = append(batchForDepth, issue)
}
}
if len(batchForDepth) > 0 {