Phase 1: Add topological sorting to fix import ordering

- Add sort.go with depth-based utilities (GetHierarchyDepth, SortByDepth, GroupByDepth)
- Sort issues by hierarchy depth before batch creation
- Create in depth-order batches (0→1→2→3)
- Fixes latent bug: parent-child pairs in same batch could fail if wrong order
- Comprehensive tests for all sorting functions
- Closes bd-37dd, bd-3433, bd-8b65

Part of bd-d19a (Fix import failure on missing parent issues)

Amp-Thread-ID: https://ampcode.com/threads/T-44a36985-b59c-426f-834c-60a0faa0f9fb
Co-authored-by: Amp <amp@ampcode.com>
This commit is contained in:
Steve Yegge
2025-11-04 13:03:20 -08:00
parent b92e79f98e
commit d6e2ff6151
3 changed files with 181 additions and 4 deletions

View File

@@ -537,12 +537,28 @@ func upsertIssues(ctx context.Context, sqliteStore *sqlite.SQLiteStorage, issues
}
}
// Batch create all new 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 {
if err := sqliteStore.CreateIssues(ctx, newIssues, "import"); err != nil {
return fmt.Errorf("error creating issues: %w", err)
SortByDepth(newIssues)
// Create issues in depth-order batches (max depth 3)
// This handles parent-child pairs in the same import batch
for depth := 0; depth <= 3; depth++ {
var batchForDepth []*types.Issue
for _, issue := range newIssues {
if GetHierarchyDepth(issue.ID) == depth {
batchForDepth = append(batchForDepth, issue)
}
}
if len(batchForDepth) > 0 {
if err := sqliteStore.CreateIssues(ctx, batchForDepth, "import"); err != nil {
return fmt.Errorf("error creating depth-%d issues: %w", depth, err)
}
result.Created += len(batchForDepth)
}
}
result.Created += len(newIssues)
}
// REMOVED (bd-c7af): Counter sync after import - no longer needed with hash IDs