beads/cmd/bd/doctor_artifacts.go
jane 2c24ce6904 fix: block data-dir in server mode + fix doctor --clean --json (GH#2438)
Three related fixes for GH#2438 workspace identity preflight:

1. doctor_artifacts.go: --clean --json combo previously returned early and
   skipped cleanup. Now performs cleanup before JSON output.

2. dolt.go: Reject `bd dolt set data-dir` in server mode with clear error.
   Setting data-dir silently disconnects from the configured database.

3. open.go: Runtime warning when stale data-dir is detected in server mode,
   guiding users to clear it.

All changes include tests.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-11 18:24:34 -07:00

137 lines
3.6 KiB
Go

package main
import (
"fmt"
"strings"
"github.com/steveyegge/beads/cmd/bd/doctor"
"github.com/steveyegge/beads/cmd/bd/doctor/fix"
"github.com/steveyegge/beads/internal/ui"
)
// runArtifactsCheck runs detailed classic artifact detection.
// With --clean, removes safe-to-delete artifacts after confirmation.
func runArtifactsCheck(path string, clean bool, yes bool) {
report, err := doctor.ScanForArtifacts(path)
if err != nil {
FatalError("scanning artifacts: %v", err)
}
if report.TotalCount == 0 {
if !jsonOutput {
fmt.Println("No classic artifacts detected.")
} else {
outputJSON(map[string]interface{}{
"total_count": 0,
})
}
return
}
if jsonOutput {
// GH#2438: When --clean is also set, perform cleanup before outputting
// JSON. Previously, --json returned early and skipped the clean logic,
// so `bd doctor --check=artifacts --clean --json` would report findings
// but never delete them.
if clean && report.SafeDeleteCount > 0 {
if err := fix.ClassicArtifacts(path); err != nil {
FatalError("during cleanup: %v", err)
}
// Re-scan to show post-cleanup state
report, err = doctor.ScanForArtifacts(path)
if err != nil {
FatalError("re-scanning artifacts: %v", err)
}
}
result := map[string]interface{}{
"total_count": report.TotalCount,
"safe_delete_count": report.SafeDeleteCount,
"sqlite_artifacts": len(report.SQLiteArtifacts),
"cruft_beads_dirs": len(report.CruftBeadsDirs),
"redirect_issues": len(report.RedirectIssues),
}
var findings []map[string]interface{}
for _, lists := range [][]doctor.ArtifactFinding{
report.SQLiteArtifacts,
report.CruftBeadsDirs, report.RedirectIssues,
} {
for _, f := range lists {
findings = append(findings, map[string]interface{}{
"path": f.Path,
"type": f.Type,
"description": f.Description,
"safe_delete": f.SafeDelete,
})
}
}
result["findings"] = findings
outputJSON(result)
return
}
// Human-readable output
fmt.Printf("Found %d classic artifact(s) (%d safe to delete):\n\n", report.TotalCount, report.SafeDeleteCount)
if len(report.SQLiteArtifacts) > 0 {
fmt.Printf("SQLite Artifacts (%d):\n", len(report.SQLiteArtifacts))
for _, f := range report.SQLiteArtifacts {
safeTag := ""
if f.SafeDelete {
safeTag = " [safe]"
}
fmt.Printf(" %s%s\n", f.Path, safeTag)
fmt.Printf(" %s\n", ui.RenderMuted(f.Description))
}
fmt.Println()
}
if len(report.CruftBeadsDirs) > 0 {
fmt.Printf("Cruft .beads Directories (%d):\n", len(report.CruftBeadsDirs))
for _, f := range report.CruftBeadsDirs {
safeTag := ""
if f.SafeDelete {
safeTag = " [safe]"
}
fmt.Printf(" %s%s\n", f.Path, safeTag)
fmt.Printf(" %s\n", ui.RenderMuted(f.Description))
}
fmt.Println()
}
if len(report.RedirectIssues) > 0 {
fmt.Printf("Redirect Issues (%d):\n", len(report.RedirectIssues))
for _, f := range report.RedirectIssues {
fmt.Printf(" %s\n", f.Path)
fmt.Printf(" %s\n", ui.RenderMuted(f.Description))
}
fmt.Println()
}
if !clean {
fmt.Println("Run 'bd doctor --check=artifacts --clean' to remove safe-to-delete artifacts.")
return
}
if report.SafeDeleteCount == 0 {
fmt.Println("No artifacts are safe to auto-delete. Manual review required.")
return
}
// Confirmation prompt
if !yes {
fmt.Printf("Delete %d safe-to-delete artifact(s)? [y/N] ", report.SafeDeleteCount)
var response string
_, _ = fmt.Scanln(&response)
if strings.ToLower(response) != "y" {
fmt.Println("Canceled.")
return
}
}
// Perform cleanup
if err := fix.ClassicArtifacts(path); err != nil {
FatalError("during cleanup: %v", err)
}
}