fix(list): stabilize tree ordering for consistent --tree output (#1228)

Motivation:
The ○ bd-e3r ● P2 Responsible-Vibe Development: beads
├── ○ bd-e3r.7 ● P1 Test feature B
│   ├── ○ bd-e3r.7.1 ● P2 Subfeature B1
│   └── ○ bd-e3r.7.2 ● P2 Subfeature B2
├── ○ bd-e3r.6 ● P1 Test feature A
│   ├── ○ bd-e3r.6.2 ● P2 Subfeature A2
│   └── ○ bd-e3r.6.1 ● P2 Subfeature A1
│       ├── ○ bd-e3r.6.1.1 ● P3 Task A1.1
│       └── ○ bd-e3r.6.1.2 ● P3 Task A1.2
├── ○ bd-e3r.8 ● P2 Test identical priority A
├── ○ bd-e3r.9 ● P2 Test identical priority B
├── ○ bd-e3r.1 ● P3 Reproduce
├── ○ bd-e3r.2 ● P3 Analyze
├── ○ bd-e3r.4 ● P3 Verify
├── ○ bd-e3r.5 ● P3 Finalize
└── ○ bd-e3r.3 ● P3 Fix

--------------------------------------------------------------------------------
Total: 16 issues (16 open, 0 in progress)

Status: ○ open  ◐ in_progress  ● blocked  ✓ closed  ❄ deferred command produced non-deterministic ordering between
consecutive executions, making [?1049h(B[?7h[?25lEvery 2.0s: bd list --treebwpm-D6KDQ60Q6R: 08:29:46in 0.343s (0)○ bd-e3r ● P2 Responsible-Vibe Development: beads
├── ○ bd-e3r.6 ● P1 Test feature A
│   ├── ○ bd-e3r.6.1 ● P2 Subfeature A1
│   │   ├── ○ bd-e3r.6.1.1 ● P3 Task A1.1
│   │   └── ○ bd-e3r.6.1.2 ● P3 Task A1.2
│   └── ○ bd-e3r.6.2 ● P2 Subfeature A2
├── ○ bd-e3r.7 ● P1 Test feature B
│   ├── ○ bd-e3r.7.2 ● P2 Subfeature B2
│   └── ○ bd-e3r.7.1 ● P2 Subfeature B1
├── ○ bd-e3r.8 ● P2 Test identical priority A
├── ○ bd-e3r.9 ● P2 Test identical priority B
├── ○ bd-e3r.1 ● P3 Reproduce
├── ○ bd-e3r.4 ● P3 Verify
├── ○ bd-e3r.5 ● P3 Finalize
├── ○ bd-e3r.2 ● P3 Analyze
└── ○ bd-e3r.3 ● P3 Fix
--------------------------------------------------------------------------------Total: 16 issues (16 open, 0 in progress)
Status: ○ open  ◐ in_progress  ● blocked  ✓ closed  ❄ deferred
[?12l[?25h[?1049l
[?1l> unusable due to
constantly changing output. Root issues and children with identical
priorities appeared in different orders across runs.

Key Changes:
- Add compareIssuesByPriority() function with primary sort by priority
  and secondary sort by ID for deterministic behavior
- Apply stable sorting to root issues in buildIssueTreeWithDeps()
- Apply stable sorting to children in childrenMap for complete consistency
- Update printPrettyTree() to use same comparison function

Side-Effects:
- Tree output now consistently orders by priority (P0→P1→P2→P3→P4)
- Items with identical priority are sorted alphabetically by ID
- Adds comprehensive TestStableTreeOrdering test with 5-run stability verification
- Minor performance overhead from sorting (negligible for typical issue counts)
- Fixes indentation inconsistencies in existing test code via gofmt
This commit is contained in:
Oliver Jägle
2026-01-22 04:11:25 +01:00
committed by GitHub
parent 3daec76ac9
commit 0877e1428e
2 changed files with 328 additions and 141 deletions

View File

@@ -247,18 +247,37 @@ func buildIssueTreeWithDeps(issues []*types.Issue, allDeps map[string][]*types.D
}
}
// Sort roots for stable tree ordering (fixes unstable --tree output)
// Use same sorting logic as children for consistency
slices.SortFunc(roots, compareIssuesByPriority)
// Sort children within each parent for stable ordering in data structure
for parentID := range childrenMap {
slices.SortFunc(childrenMap[parentID], compareIssuesByPriority)
}
return roots, childrenMap
}
// compareIssuesByPriority provides stable sorting for tree display
// Primary sort: priority (P0 before P1 before P2...)
// Secondary sort: ID for deterministic ordering when priorities match
func compareIssuesByPriority(a, b *types.Issue) int {
// Primary: priority (ascending: P0 before P1 before P2...)
if result := cmp.Compare(a.Priority, b.Priority); result != 0 {
return result
}
// Secondary: ID for deterministic order when priorities match
return cmp.Compare(a.ID, b.ID)
}
// printPrettyTree recursively prints the issue tree
// Children are sorted by priority (P0 first) for intuitive reading
func printPrettyTree(childrenMap map[string][]*types.Issue, parentID string, prefix string) {
children := childrenMap[parentID]
// Sort children by priority (ascending: P0 before P1 before P2...)
slices.SortFunc(children, func(a, b *types.Issue) int {
return cmp.Compare(a.Priority, b.Priority)
})
// Sort children by priority using same comparison as roots for consistency
slices.SortFunc(children, compareIssuesByPriority)
for i, child := range children {
isLast := i == len(children)-1