- Extend write-path CTE and DFS cycle detection to include conditional-blocks (also creates blocking relationships, same as blocks) - Add explicit self-dependency check in AddDependencyInTx - Add bd graph check subcommand: runs DetectCycles, reports results in JSON or human-readable format, exits with code 1 if issues found waits-for is intentionally excluded from cycle detection — it uses gate semantics (fan-in) rather than direct blocking, so A waits-for B does not mean B blocks A in the traditional sense. Fixes: bd-2qr Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
98 lines
2.5 KiB
Go
98 lines
2.5 KiB
Go
package issueops
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"fmt"
|
|
|
|
"github.com/steveyegge/beads/internal/types"
|
|
)
|
|
|
|
// DetectCyclesInTx finds dependency cycles across both the dependencies and
|
|
// wisp_dependencies tables. Returns slices of issues forming each cycle.
|
|
// Only considers "blocks" and "conditional-blocks" dependencies for cycle detection.
|
|
//
|
|
//nolint:gosec // G201: depTable is hardcoded to "dependencies" or "wisp_dependencies"
|
|
func DetectCyclesInTx(ctx context.Context, tx *sql.Tx) ([][]*types.Issue, error) {
|
|
// Build adjacency list from both dependency tables.
|
|
graph := make(map[string][]string)
|
|
|
|
for _, depTable := range []string{"dependencies", "wisp_dependencies"} {
|
|
rows, err := tx.QueryContext(ctx, fmt.Sprintf(`
|
|
SELECT issue_id, depends_on_id, type
|
|
FROM %s
|
|
`, depTable))
|
|
if err != nil {
|
|
return nil, fmt.Errorf("detect cycles: query %s: %w", depTable, err)
|
|
}
|
|
for rows.Next() {
|
|
var issueID, dependsOnID, depType string
|
|
if err := rows.Scan(&issueID, &dependsOnID, &depType); err != nil {
|
|
_ = rows.Close()
|
|
return nil, fmt.Errorf("detect cycles: scan %s: %w", depTable, err)
|
|
}
|
|
if types.DependencyType(depType) == types.DepBlocks || types.DependencyType(depType) == types.DepConditionalBlocks {
|
|
graph[issueID] = append(graph[issueID], dependsOnID)
|
|
}
|
|
}
|
|
_ = rows.Close()
|
|
if err := rows.Err(); err != nil {
|
|
return nil, fmt.Errorf("detect cycles: rows %s: %w", depTable, err)
|
|
}
|
|
}
|
|
|
|
// Find cycles using DFS.
|
|
var cycles [][]*types.Issue
|
|
visited := make(map[string]bool)
|
|
recStack := make(map[string]bool)
|
|
path := make([]string, 0)
|
|
|
|
var dfs func(node string) bool
|
|
dfs = func(node string) bool {
|
|
visited[node] = true
|
|
recStack[node] = true
|
|
path = append(path, node)
|
|
|
|
for _, neighbor := range graph[node] {
|
|
if !visited[neighbor] {
|
|
if dfs(neighbor) {
|
|
return true
|
|
}
|
|
} else if recStack[neighbor] {
|
|
// Found cycle — extract it.
|
|
cycleStart := -1
|
|
for i, n := range path {
|
|
if n == neighbor {
|
|
cycleStart = i
|
|
break
|
|
}
|
|
}
|
|
if cycleStart >= 0 {
|
|
cyclePath := path[cycleStart:]
|
|
var cycleIssues []*types.Issue
|
|
for _, id := range cyclePath {
|
|
issue, _ := GetIssueInTx(ctx, tx, id)
|
|
if issue != nil {
|
|
cycleIssues = append(cycleIssues, issue)
|
|
}
|
|
}
|
|
if len(cycleIssues) > 0 {
|
|
cycles = append(cycles, cycleIssues)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
path = path[:len(path)-1]
|
|
recStack[node] = false
|
|
return false
|
|
}
|
|
|
|
for node := range graph {
|
|
if !visited[node] {
|
|
dfs(node)
|
|
}
|
|
}
|
|
|
|
return cycles, nil
|
|
}
|