fix(list): print bd list --format dot in a deterministic order (#7688)
outputDotFormat declared nodes and edges in the order of the list page it was handed. The default page order breaks priority ties by created_at at one-second precision, so two equal-priority issues swapped places depending on whether they were created in the same second, and the proxied route's edge query orders by source only. The same graph printed different bytes (Oracle A list_dot_format_dependency_edges flipped between runs). Declare nodes in byte order of id and each node's edges in byte order of (target, type), on copies of the caller's slices. --sort and --limit still choose which issues are listed. Agent-Signature: claude-code-claude-opus-5-5-unknown-reasoning on behalf of Julian Knutsen Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
d855ac6c88
commit
4c48829c07
5 changed files with 150 additions and 1 deletions
|
|
@ -834,6 +834,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||
error envelope on stdout under `--json`. A batch that reads at least one of
|
||||
its ids still exits 0. Both the direct and the proxied-server routes
|
||||
changed.
|
||||
- **`bd list --format dot` prints the same graph the same way every time.**
|
||||
Nodes are declared in id order and edges in source, target and type order.
|
||||
They used to follow the listing order, which breaks a priority tie by
|
||||
creation time to the second, so two equal-priority issues swapped places
|
||||
depending on whether they were created in the same second, and the
|
||||
proxied-server route did not pin the order of one issue's edges. `--sort`
|
||||
and `--limit` still choose which issues are listed.
|
||||
- **`bd init --quiet` no longer prints "Removed SessionStart hook" or "Removed
|
||||
PreCompact hook"** when the project's `.claude/settings.json` (or the legacy
|
||||
`.claude/settings.local.json`) still carries the bare `bd prime` hooks an
|
||||
|
|
|
|||
|
|
@ -811,6 +811,7 @@ func TestEmbeddedListSearchAndOutputFormats(t *testing.T) {
|
|||
if !strings.Contains(out, "digraph") {
|
||||
t.Errorf("dot output should contain 'digraph' header, got: %s", out)
|
||||
}
|
||||
assertDotCanonicalOrder(t, out)
|
||||
})
|
||||
|
||||
t.Run("format_dot_output_error", func(t *testing.T) {
|
||||
|
|
|
|||
|
|
@ -2,9 +2,11 @@ package main
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"cmp"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"slices"
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
|
|
@ -38,7 +40,27 @@ func composeTruncationHint(render func(string) string, text string) string {
|
|||
return "\n" + rendered + "\n"
|
||||
}
|
||||
|
||||
// outputDotFormat renders the listed issues, and the dependency edges between
|
||||
// them, as a Graphviz digraph.
|
||||
//
|
||||
// The bytes depend on the graph alone: nodes are declared in byte order of id,
|
||||
// and each node's edges in byte order of target id, then type (the order
|
||||
// issueops.EdgeReader pins). The page arrives in display order, by default
|
||||
// priority, then created_at DESC at one-second precision, then id, so two
|
||||
// equal-priority issues swapped places depending on whether they were created
|
||||
// in the same second; and the proxied route's edge query orders by source
|
||||
// only. Declaration order means nothing in the graph but steers Graphviz's
|
||||
// layout and shows in every diff, so it is pinned here, as bd graph --dot pins
|
||||
// its layers. --sort and --limit still choose which issues are listed.
|
||||
//
|
||||
// Byte order, not the utils.NaturalCompareIDs order --sort id uses: that
|
||||
// comparator is not a total order (x-9 < x-10 < x-1a < x-9; bd-01 ties bd-1),
|
||||
// so a sort under it could still depend on the order the page arrived in.
|
||||
// Both sorts work on copies; the caller's slices keep their order.
|
||||
func outputDotFormat(out io.Writer, issues []*types.Issue, depsByIssueID map[string][]*types.Dependency) error {
|
||||
issues = slices.Clone(issues)
|
||||
slices.SortFunc(issues, func(a, b *types.Issue) int { return cmp.Compare(a.ID, b.ID) })
|
||||
|
||||
w := &graphExportWriter{out: out}
|
||||
w.println("digraph dependencies {")
|
||||
w.println(" rankdir=TB;")
|
||||
|
|
@ -82,7 +104,11 @@ func outputDotFormat(out io.Writer, issues []*types.Issue, depsByIssueID map[str
|
|||
|
||||
// Output edges with labels for dependency type
|
||||
for _, issue := range issues {
|
||||
for _, dep := range depsByIssueID[issue.ID] {
|
||||
deps := slices.Clone(depsByIssueID[issue.ID])
|
||||
slices.SortFunc(deps, func(a, b *types.Dependency) int {
|
||||
return cmp.Or(cmp.Compare(a.DependsOnID, b.DependsOnID), cmp.Compare(a.Type, b.Type))
|
||||
})
|
||||
for _, dep := range deps {
|
||||
// Only output edges where both nodes are in the filtered list
|
||||
if issueMap[dep.DependsOnID] != nil {
|
||||
// Color code by dependency type
|
||||
|
|
|
|||
|
|
@ -2,9 +2,12 @@ package main
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"cmp"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"regexp"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
|
|
@ -65,6 +68,117 @@ func TestOutputDotFormatExactBytes(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
// TestOutputDotFormatCanonicalOrder pins that the DOT rendering is a function
|
||||
// of the graph alone. The page it is handed is in display order (priority,
|
||||
// then created_at DESC at one-second precision, then id), so the same two
|
||||
// equal-priority issues arrive in either order depending on whether they were
|
||||
// created in the same second; and the proxied route's edge read does not order
|
||||
// one issue's edges. Every order of the page, with each issue's edges either
|
||||
// way round, must print these bytes: nodes by id, edges by source, target and
|
||||
// type, all in byte order (so dot-10 sorts before dot-2). It also pins that the
|
||||
// caller's page and edge slices are not reordered.
|
||||
func TestOutputDotFormatCanonicalOrder(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
issues := []*types.Issue{
|
||||
{ID: "dot-2", Title: "Two", Status: types.StatusInProgress, Priority: 0, IssueType: types.TypeBug},
|
||||
{ID: "dot-10", Title: "Ten", Status: types.StatusOpen, Priority: 1, IssueType: types.TypeEpic},
|
||||
{ID: "dot-1", Title: "One", Status: types.StatusClosed, Priority: 2, IssueType: types.TypeTask},
|
||||
}
|
||||
edges := map[string][]*types.Dependency{
|
||||
"dot-2": {
|
||||
{IssueID: "dot-2", DependsOnID: "dot-10", Type: types.DepRelated},
|
||||
{IssueID: "dot-2", DependsOnID: "dot-unlisted", Type: types.DepBlocks},
|
||||
{IssueID: "dot-2", DependsOnID: "dot-1", Type: types.DepBlocks},
|
||||
},
|
||||
"dot-1": {{IssueID: "dot-1", DependsOnID: "dot-10", Type: types.DepParentChild}},
|
||||
}
|
||||
const want = `digraph dependencies {
|
||||
rankdir=TB;
|
||||
node [shape=box, style=rounded];
|
||||
|
||||
"dot-1" [label="dot-1\n[task P2]\nOne\n(closed)", style="rounded,filled", fillcolor="lightgray", fontcolor="dimgray"];
|
||||
"dot-10" [label="dot-10\n[epic P1]\nTen\n(open)", style="rounded,filled", fillcolor="white", fontcolor="black"];
|
||||
"dot-2" [label="dot-2\n[bug P0]\nTwo\n(in_progress)", style="rounded,filled", fillcolor="lightyellow", fontcolor="black"];
|
||||
|
||||
"dot-1" -> "dot-10" [label="parent-child", color=blue, style=solid];
|
||||
"dot-2" -> "dot-1" [label="blocks", color=red, style=bold];
|
||||
"dot-2" -> "dot-10" [label="related", color=gray, style=dashed];
|
||||
}
|
||||
`
|
||||
for _, perm := range [][]int{{0, 1, 2}, {0, 2, 1}, {1, 0, 2}, {1, 2, 0}, {2, 0, 1}, {2, 1, 0}} {
|
||||
for _, reversed := range []bool{false, true} {
|
||||
page := []*types.Issue{issues[perm[0]], issues[perm[1]], issues[perm[2]]}
|
||||
pageEdges := map[string][]*types.Dependency{}
|
||||
for id, deps := range edges {
|
||||
deps = slices.Clone(deps)
|
||||
if reversed {
|
||||
slices.Reverse(deps)
|
||||
}
|
||||
pageEdges[id] = deps
|
||||
}
|
||||
wantPage := slices.Clone(page)
|
||||
wantEdges := map[string][]*types.Dependency{}
|
||||
for id, deps := range pageEdges {
|
||||
wantEdges[id] = slices.Clone(deps)
|
||||
}
|
||||
|
||||
var out bytes.Buffer
|
||||
if err := outputDotFormat(&out, page, pageEdges); err != nil {
|
||||
t.Fatalf("outputDotFormat: %v", err)
|
||||
}
|
||||
name := fmt.Sprintf("page order %v, edges reversed %v", perm, reversed)
|
||||
if got := out.String(); got != want {
|
||||
t.Errorf("%s: DOT output bytes differ\ngot:\n%s\nwant:\n%s", name, got, want)
|
||||
}
|
||||
if !slices.Equal(page, wantPage) {
|
||||
t.Errorf("%s: outputDotFormat reordered the caller's page", name)
|
||||
}
|
||||
for id, deps := range wantEdges {
|
||||
if !slices.Equal(pageEdges[id], deps) {
|
||||
t.Errorf("%s: outputDotFormat reordered the caller's edges of %s", name, id)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// dotNodeDecl and dotEdgeDecl match the node and edge lines outputDotFormat
|
||||
// writes.
|
||||
var (
|
||||
dotNodeDecl = regexp.MustCompile(`(?m)^ "([^"]+)" \[label=`)
|
||||
dotEdgeDecl = regexp.MustCompile(`(?m)^ "([^"]+)" -> "([^"]+)" \[label="([^"]+)"`)
|
||||
)
|
||||
|
||||
// assertDotCanonicalOrder is TestOutputDotFormatCanonicalOrder's check for a
|
||||
// real `bd list --format dot` run: nodes in byte order of id, edges in byte
|
||||
// order of source, target and type. The embedded and proxied list seeds mint
|
||||
// their ids across five priorities, so a rendering that followed the page's
|
||||
// priority order fails it.
|
||||
func assertDotCanonicalOrder(t *testing.T, out string) {
|
||||
t.Helper()
|
||||
var nodes []string
|
||||
for _, m := range dotNodeDecl.FindAllStringSubmatch(out, -1) {
|
||||
nodes = append(nodes, m[1])
|
||||
}
|
||||
if len(nodes) < 2 {
|
||||
t.Fatalf("dot output declares %d nodes, want every listed issue:\n%s", len(nodes), out)
|
||||
}
|
||||
if !slices.IsSorted(nodes) {
|
||||
t.Errorf("dot output declares its nodes out of id order: %v", nodes)
|
||||
}
|
||||
type edge struct{ from, to, typ string }
|
||||
var edges []edge
|
||||
for _, m := range dotEdgeDecl.FindAllStringSubmatch(out, -1) {
|
||||
edges = append(edges, edge{m[1], m[2], m[3]})
|
||||
}
|
||||
if !slices.IsSortedFunc(edges, func(a, b edge) int {
|
||||
return cmp.Or(cmp.Compare(a.from, b.from), cmp.Compare(a.to, b.to), cmp.Compare(a.typ, b.typ))
|
||||
}) {
|
||||
t.Errorf("dot output declares its edges out of (source, target, type) order: %v", edges)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOutputFormattedListExactBytes(t *testing.T) {
|
||||
t.Parallel()
|
||||
issues, deps := listOutputFixture()
|
||||
|
|
|
|||
|
|
@ -495,6 +495,7 @@ func TestProxiedServerList(t *testing.T) {
|
|||
if !strings.Contains(out, "digraph") {
|
||||
t.Errorf("dot output should contain 'digraph' header, got: %s", out)
|
||||
}
|
||||
assertDotCanonicalOrder(t, out)
|
||||
})
|
||||
|
||||
t.Run("format_digraph_output_error", func(t *testing.T) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue