feat(deps): detect/prevent child→parent dependency anti-pattern (bd-nim5)

This prevents a common mistake where users add dependencies from child
issues to their parent epics. This creates a deadlock:
- Child can't start (blocked by open parent)
- Parent can't close (children not done)

Changes:
- dep.go: Reject child→parent deps at creation time with clear error
- server_labels_deps_comments.go: Same check for daemon RPC
- doctor/validation.go: New check detects existing bad deps
- doctor/fix/validation.go: Auto-fix removes bad deps
- doctor.go: Wire up check and fix handler

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Steve Yegge
2025-12-24 13:03:13 -08:00
parent 566e4f5225
commit ce2b05356a
7 changed files with 379 additions and 0 deletions

View File

@@ -162,6 +162,70 @@ func OrphanedDependencies(path string) error {
return nil
}
// ChildParentDependencies removes child→parent dependencies (anti-pattern).
// This fixes the deadlock where children depend on their parent epic.
func ChildParentDependencies(path string) error {
if err := validateBeadsWorkspace(path); err != nil {
return err
}
beadsDir := filepath.Join(path, ".beads")
dbPath := filepath.Join(beadsDir, "beads.db")
// Open database
db, err := openDB(dbPath)
if err != nil {
return fmt.Errorf("failed to open database: %w", err)
}
defer db.Close()
// Find child→parent dependencies where issue_id starts with depends_on_id + "."
query := `
SELECT d.issue_id, d.depends_on_id
FROM dependencies d
WHERE d.issue_id LIKE d.depends_on_id || '.%'
`
rows, err := db.Query(query)
if err != nil {
return fmt.Errorf("failed to query child-parent dependencies: %w", err)
}
defer rows.Close()
type badDep struct {
issueID string
dependsOnID string
}
var badDeps []badDep
for rows.Next() {
var d badDep
if err := rows.Scan(&d.issueID, &d.dependsOnID); err == nil {
badDeps = append(badDeps, d)
}
}
if len(badDeps) == 0 {
fmt.Println(" No child→parent dependencies to fix")
return nil
}
// Delete child→parent dependencies
for _, d := range badDeps {
_, err := db.Exec("DELETE FROM dependencies WHERE issue_id = ? AND depends_on_id = ?",
d.issueID, d.dependsOnID)
if err != nil {
fmt.Printf(" Warning: failed to remove %s→%s: %v\n", d.issueID, d.dependsOnID, err)
} else {
// Mark issue as dirty for export
_, _ = db.Exec("INSERT OR IGNORE INTO dirty_issues (issue_id) VALUES (?)", d.issueID)
fmt.Printf(" Removed child→parent dependency: %s→%s\n", d.issueID, d.dependsOnID)
}
}
fmt.Printf(" Fixed %d child→parent dependency anti-pattern(s)\n", len(badDeps))
return nil
}
// openDB opens a SQLite database for read-write access
func openDB(dbPath string) (*sql.DB, error) {
return sql.Open("sqlite3", dbPath)

View File

@@ -1,7 +1,12 @@
package fix
import (
"os"
"path/filepath"
"testing"
_ "github.com/ncruces/go-sqlite3/driver"
_ "github.com/ncruces/go-sqlite3/embed"
)
// TestFixFunctions_RequireBeadsDir verifies all fix functions properly validate
@@ -22,6 +27,7 @@ func TestFixFunctions_RequireBeadsDir(t *testing.T) {
{"SyncBranchHealth", func(dir string) error { return SyncBranchHealth(dir, "beads-sync") }},
{"UntrackedJSONL", UntrackedJSONL},
{"MigrateTombstones", MigrateTombstones},
{"ChildParentDependencies", ChildParentDependencies},
}
for _, tc := range funcs {
@@ -35,3 +41,100 @@ func TestFixFunctions_RequireBeadsDir(t *testing.T) {
})
}
}
func TestChildParentDependencies_NoBadDeps(t *testing.T) {
// Set up test database with no child→parent deps
dir := t.TempDir()
beadsDir := filepath.Join(dir, ".beads")
if err := os.MkdirAll(beadsDir, 0755); err != nil {
t.Fatal(err)
}
dbPath := filepath.Join(beadsDir, "beads.db")
db, err := openDB(dbPath)
if err != nil {
t.Fatal(err)
}
// Create minimal schema
_, err = db.Exec(`
CREATE TABLE issues (id TEXT PRIMARY KEY);
CREATE TABLE dependencies (issue_id TEXT, depends_on_id TEXT, type TEXT);
CREATE TABLE dirty_issues (issue_id TEXT PRIMARY KEY);
INSERT INTO issues (id) VALUES ('bd-abc'), ('bd-abc.1'), ('bd-xyz');
INSERT INTO dependencies (issue_id, depends_on_id, type) VALUES ('bd-abc.1', 'bd-xyz', 'blocks');
`)
if err != nil {
t.Fatal(err)
}
db.Close()
// Run fix - should find no bad deps
err = ChildParentDependencies(dir)
if err != nil {
t.Errorf("ChildParentDependencies failed: %v", err)
}
// Verify the good dependency still exists
db, _ = openDB(dbPath)
defer db.Close()
var count int
db.QueryRow("SELECT COUNT(*) FROM dependencies").Scan(&count)
if count != 1 {
t.Errorf("Expected 1 dependency, got %d", count)
}
}
func TestChildParentDependencies_FixesBadDeps(t *testing.T) {
// Set up test database with child→parent deps
dir := t.TempDir()
beadsDir := filepath.Join(dir, ".beads")
if err := os.MkdirAll(beadsDir, 0755); err != nil {
t.Fatal(err)
}
dbPath := filepath.Join(beadsDir, "beads.db")
db, err := openDB(dbPath)
if err != nil {
t.Fatal(err)
}
// Create minimal schema with child→parent dependency
_, err = db.Exec(`
CREATE TABLE issues (id TEXT PRIMARY KEY);
CREATE TABLE dependencies (issue_id TEXT, depends_on_id TEXT, type TEXT);
CREATE TABLE dirty_issues (issue_id TEXT PRIMARY KEY);
INSERT INTO issues (id) VALUES ('bd-abc'), ('bd-abc.1'), ('bd-abc.1.2');
INSERT INTO dependencies (issue_id, depends_on_id, type) VALUES
('bd-abc.1', 'bd-abc', 'blocks'),
('bd-abc.1.2', 'bd-abc', 'blocks'),
('bd-abc.1.2', 'bd-abc.1', 'blocks');
`)
if err != nil {
t.Fatal(err)
}
db.Close()
// Run fix
err = ChildParentDependencies(dir)
if err != nil {
t.Errorf("ChildParentDependencies failed: %v", err)
}
// Verify all bad dependencies were removed
db, _ = openDB(dbPath)
defer db.Close()
var count int
db.QueryRow("SELECT COUNT(*) FROM dependencies").Scan(&count)
if count != 0 {
t.Errorf("Expected 0 dependencies after fix, got %d", count)
}
// Verify dirty_issues was updated for affected issues
// Note: 2 unique issue_ids (bd-abc.1 appears once, bd-abc.1.2 appears twice but INSERT OR IGNORE dedupes)
var dirtyCount int
db.QueryRow("SELECT COUNT(*) FROM dirty_issues").Scan(&dirtyCount)
if dirtyCount != 2 {
t.Errorf("Expected 2 dirty issues (unique issue_ids from removed deps), got %d", dirtyCount)
}
}