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[1;24r(B[m[4l[?7h[?25l[H[2J[2d[AEvery 2.0s: bd list --tree[1;56Hbwpm-D6KDQ60Q6R: 08:29:46[2;68Hin 0.343s (0)[3;1H○ bd-e3r ● P2 Responsible-Vibe Development: beads [4d├── ○ bd-e3r.6 ● P1 Test feature A [5d│ ├── ○ bd-e3r.6.1 ● P2 Subfeature A1 [6d│ │ ├── ○ bd-e3r.6.1.1 ● P3 Task A1.1 [7d│ │ └── ○ bd-e3r.6.1.2 ● P3 Task A1.2 [8d│ └── ○ bd-e3r.6.2 ● P2 Subfeature A2 [9d├── ○ bd-e3r.7 ● P1 Test feature B [10d│ ├── ○ bd-e3r.7.2 ● P2 Subfeature B2 [11d│ └── ○ bd-e3r.7.1 ● P2 Subfeature B1 [12d├── ○ bd-e3r.8 ● P2 Test identical priority A [13d├── ○ bd-e3r.9 ● P2 Test identical priority B [14d├── ○ bd-e3r.1 ● P3 Reproduce [15d├── ○ bd-e3r.4 ● P3 Verify [16d├── ○ bd-e3r.5 ● P3 Finalize [17d├── ○ bd-e3r.2 ● P3 Analyze [18d└── ○ bd-e3r.3 ● P3 Fix [20d--------------------------------------------------------------------------------[21;1HTotal: 16 issues (16 open, 0 in progress) [23dStatus: ○ open ◐ in_progress ● blocked ✓ closed ❄ deferred [3d[24;1H[?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:
@@ -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
|
||||
|
||||
@@ -3,6 +3,7 @@ package main
|
||||
import (
|
||||
"context"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -456,6 +457,173 @@ func TestListQueryCapabilitiesSuite(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
// TestStableTreeOrdering tests that tree display order is stable across multiple invocations
|
||||
// This test specifically addresses the bug where --tree output was non-deterministic due to
|
||||
// unstable ordering of root issues and children within the same priority level
|
||||
func TestStableTreeOrdering(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
testDB := filepath.Join(tmpDir, ".beads", "beads.db")
|
||||
store := newTestStore(t, testDB)
|
||||
ctx := context.Background()
|
||||
|
||||
// Helper to create issue with specific priority for testing sort stability
|
||||
createIssue := func(title string, priority int) *types.Issue {
|
||||
issue := &types.Issue{
|
||||
Title: title,
|
||||
Priority: priority,
|
||||
IssueType: types.TypeTask,
|
||||
Status: types.StatusOpen,
|
||||
}
|
||||
if err := store.CreateIssue(ctx, issue, "test-user"); err != nil {
|
||||
t.Fatalf("Failed to create issue %s: %v", title, err)
|
||||
}
|
||||
return issue
|
||||
}
|
||||
|
||||
// Helper to add parent-child dependency
|
||||
addParentChild := func(child, parent *types.Issue) {
|
||||
dep := &types.Dependency{
|
||||
IssueID: child.ID,
|
||||
DependsOnID: parent.ID,
|
||||
Type: types.DepParentChild,
|
||||
CreatedAt: time.Now(),
|
||||
CreatedBy: "test-user",
|
||||
}
|
||||
if err := store.AddDependency(ctx, dep, "test-user"); err != nil {
|
||||
t.Fatalf("Failed to add dependency %s -> %s: %v", child.ID, parent.ID, err)
|
||||
}
|
||||
}
|
||||
|
||||
// Create a hierarchy with mixed priorities to test both primary and secondary sort:
|
||||
// - Multiple root issues with same priority (tests secondary sort by ID)
|
||||
// - Multiple children with same priority (tests children sorting stability)
|
||||
// - Mixed priorities (tests primary sort by priority)
|
||||
|
||||
// Root issues with different priorities
|
||||
rootP1A := createIssue("Root P1 A", 1) // Should be first (lowest priority number)
|
||||
rootP1B := createIssue("Root P1 B", 1) // Should be second (same priority, sorted by ID)
|
||||
rootP2 := createIssue("Root P2", 2) // Should be third
|
||||
rootP3 := createIssue("Root P3", 3) // Should be last
|
||||
|
||||
// Children with mixed priorities under rootP1A
|
||||
childP1 := createIssue("Child P1", 1) // Should be first child
|
||||
childP2A := createIssue("Child P2 A", 2) // Should be second child
|
||||
childP2B := createIssue("Child P2 B", 2) // Should be third child (same priority, sorted by ID)
|
||||
childP3 := createIssue("Child P3", 3) // Should be last child
|
||||
|
||||
// Add parent-child relationships
|
||||
addParentChild(childP1, rootP1A)
|
||||
addParentChild(childP2A, rootP1A)
|
||||
addParentChild(childP2B, rootP1A)
|
||||
addParentChild(childP3, rootP1A)
|
||||
|
||||
// Test that buildIssueTree produces stable ordering
|
||||
t.Run("stable_root_ordering", func(t *testing.T) {
|
||||
// Get only the test issues we created (filter by title pattern)
|
||||
testIssues := []*types.Issue{rootP1A, rootP1B, rootP2, rootP3}
|
||||
|
||||
// Build tree multiple times and verify identical ordering
|
||||
var rootOrderings [][]string
|
||||
for i := 0; i < 5; i++ {
|
||||
roots, _ := buildIssueTree(testIssues)
|
||||
|
||||
// Extract root IDs in order
|
||||
var rootIDs []string
|
||||
for _, root := range roots {
|
||||
rootIDs = append(rootIDs, root.ID)
|
||||
}
|
||||
rootOrderings = append(rootOrderings, rootIDs)
|
||||
}
|
||||
|
||||
// Verify all runs produce identical root ordering
|
||||
expectedRootOrder := rootOrderings[0]
|
||||
for i := 1; i < len(rootOrderings); i++ {
|
||||
if !slicesEqual(expectedRootOrder, rootOrderings[i]) {
|
||||
t.Errorf("Root ordering differs between runs:\nRun 1: %v\nRun %d: %v",
|
||||
expectedRootOrder, i+1, rootOrderings[i])
|
||||
}
|
||||
}
|
||||
|
||||
// Verify expected sort order (priority first, then ID)
|
||||
// Since IDs are auto-generated, we'll verify by comparing with sorted slice
|
||||
expectedRoots := []*types.Issue{rootP1A, rootP1B, rootP2, rootP3}
|
||||
slices.SortFunc(expectedRoots, compareIssuesByPriority)
|
||||
var expectedOrder []string
|
||||
for _, issue := range expectedRoots {
|
||||
expectedOrder = append(expectedOrder, issue.ID)
|
||||
}
|
||||
|
||||
if !slicesEqual(expectedOrder, expectedRootOrder) {
|
||||
t.Errorf("Root ordering incorrect:\nExpected: %v\nActual: %v",
|
||||
expectedOrder, expectedRootOrder)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("stable_children_ordering", func(t *testing.T) {
|
||||
// Get test issues including dependencies
|
||||
allTestIssues := []*types.Issue{rootP1A, rootP1B, rootP2, rootP3, childP1, childP2A, childP2B, childP3}
|
||||
|
||||
// Load dependencies for tree building
|
||||
allDeps, err := store.GetAllDependencyRecords(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to get dependencies: %v", err)
|
||||
}
|
||||
|
||||
// Build tree multiple times and verify identical children ordering
|
||||
var childOrderings [][]string
|
||||
for i := 0; i < 5; i++ {
|
||||
_, childrenMap := buildIssueTreeWithDeps(allTestIssues, allDeps)
|
||||
|
||||
// Extract children IDs of rootP1A in order
|
||||
children := childrenMap[rootP1A.ID]
|
||||
var childIDs []string
|
||||
for _, child := range children {
|
||||
childIDs = append(childIDs, child.ID)
|
||||
}
|
||||
childOrderings = append(childOrderings, childIDs)
|
||||
}
|
||||
|
||||
// Verify all runs produce identical children ordering
|
||||
if len(childOrderings) == 0 || len(childOrderings[0]) == 0 {
|
||||
t.Fatal("No children found for rootP1A")
|
||||
}
|
||||
|
||||
expectedChildOrder := childOrderings[0]
|
||||
for i := 1; i < len(childOrderings); i++ {
|
||||
if !slicesEqual(expectedChildOrder, childOrderings[i]) {
|
||||
t.Errorf("Children ordering differs between runs:\nRun 1: %v\nRun %d: %v",
|
||||
expectedChildOrder, i+1, childOrderings[i])
|
||||
}
|
||||
}
|
||||
|
||||
// Verify expected sort order by sorting the expected children and comparing
|
||||
expectedChildren := []*types.Issue{childP1, childP2A, childP2B, childP3}
|
||||
slices.SortFunc(expectedChildren, compareIssuesByPriority)
|
||||
var expectedOrder []string
|
||||
for _, child := range expectedChildren {
|
||||
expectedOrder = append(expectedOrder, child.ID)
|
||||
}
|
||||
|
||||
if !slicesEqual(expectedOrder, expectedChildOrder) {
|
||||
t.Errorf("Children ordering incorrect:\nExpected: %v\nActual: %v",
|
||||
expectedOrder, expectedChildOrder)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Helper function to compare string slices for equality
|
||||
func slicesEqual(a, b []string) bool {
|
||||
if len(a) != len(b) {
|
||||
return false
|
||||
}
|
||||
for i := range a {
|
||||
if a[i] != b[i] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func TestFormatIssueLong(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
|
||||
Reference in New Issue
Block a user