Fix Dolt backend init/daemon/doctor; prevent accidental SQLite artifacts; add integration tests; clean up lint (#1218)

* /{cmd,internal}: get dolt backend init working and allow issue creation

* /{website,internal,docs,cmd}: integration tests and more split backend fixes

* /{cmd,internal}: fix lint issues

* /cmd/bd/doctor/integrity.go: fix unable to query issues bug with dolt backend

* /cmd/bd/daemon.go: remove debug logging
This commit is contained in:
Dustin Brown
2026-01-20 17:34:00 -08:00
committed by GitHub
parent c1ac69da3e
commit d3ccd5cfba
31 changed files with 1071 additions and 305 deletions
+8 -7
View File
@@ -216,8 +216,9 @@ func findLocalBeadsDir() string {
// findDatabaseInBeadsDir searches for a database file within a .beads directory.
// It implements the standard search order:
// 1. Check metadata.json first (single source of truth)
// - For SQLite backend: returns path to .db file
// - For Dolt backend: returns path to dolt/ directory
// - For SQLite backend: returns path to .db file
// - For Dolt backend: returns path to dolt/ directory
//
// 2. Fall back to canonical beads.db
// 3. Search for *.db files, filtering out backups and vc.db
//
@@ -231,8 +232,8 @@ func findDatabaseInBeadsDir(beadsDir string, warnOnIssues bool) string {
if cfg, err := configfile.Load(beadsDir); err == nil && cfg != nil {
backend := cfg.GetBackend()
if backend == configfile.BackendDolt {
// For Dolt, check if the dolt directory exists
doltPath := filepath.Join(beadsDir, "dolt")
// For Dolt, check if the configured database directory exists
doltPath := cfg.DatabasePath(beadsDir)
if info, err := os.Stat(doltPath); err == nil && info.IsDir() {
return doltPath
}
@@ -575,9 +576,9 @@ func FindJSONLPath(dbPath string) string {
// DatabaseInfo contains information about a discovered beads database
type DatabaseInfo struct {
Path string // Full path to the .db file
BeadsDir string // Parent .beads directory
IssueCount int // Number of issues (-1 if unknown)
Path string // Full path to the .db file
BeadsDir string // Parent .beads directory
IssueCount int // Number of issues (-1 if unknown)
}
// findGitRoot returns the root directory of the current git repository,
+1 -1
View File
@@ -21,7 +21,7 @@ var ConfigWarningWriter io.Writer = os.Stderr
// logConfigWarning logs a warning message if ConfigWarnings is enabled.
func logConfigWarning(format string, args ...interface{}) {
if ConfigWarnings && ConfigWarningWriter != nil {
fmt.Fprintf(ConfigWarningWriter, format, args...)
_, _ = fmt.Fprintf(ConfigWarningWriter, format, args...)
}
}
+39 -11
View File
@@ -5,6 +5,7 @@ import (
"fmt"
"os"
"path/filepath"
"strings"
)
const ConfigFileName = "metadata.json"
@@ -37,7 +38,7 @@ func ConfigPath(beadsDir string) string {
func Load(beadsDir string) (*Config, error) {
configPath := ConfigPath(beadsDir)
data, err := os.ReadFile(configPath) // #nosec G304 - controlled path from config
if os.IsNotExist(err) {
// Try legacy config.json location (migration path)
@@ -49,52 +50,79 @@ func Load(beadsDir string) (*Config, error) {
if err != nil {
return nil, fmt.Errorf("reading legacy config: %w", err)
}
// Migrate: parse legacy config, save as metadata.json, remove old file
var cfg Config
if err := json.Unmarshal(data, &cfg); err != nil {
return nil, fmt.Errorf("parsing legacy config: %w", err)
}
// Save to new location
if err := cfg.Save(beadsDir); err != nil {
return nil, fmt.Errorf("migrating config to metadata.json: %w", err)
}
// Remove legacy file (best effort)
_ = os.Remove(legacyPath)
return &cfg, nil
}
if err != nil {
return nil, fmt.Errorf("reading config: %w", err)
}
var cfg Config
if err := json.Unmarshal(data, &cfg); err != nil {
return nil, fmt.Errorf("parsing config: %w", err)
}
return &cfg, nil
}
func (c *Config) Save(beadsDir string) error {
configPath := ConfigPath(beadsDir)
data, err := json.MarshalIndent(c, "", " ")
if err != nil {
return fmt.Errorf("marshaling config: %w", err)
}
if err := os.WriteFile(configPath, data, 0600); err != nil {
return fmt.Errorf("writing config: %w", err)
}
return nil
}
func (c *Config) DatabasePath(beadsDir string) string {
return filepath.Join(beadsDir, c.Database)
backend := c.GetBackend()
// Treat Database as the on-disk storage location:
// - SQLite: filename (default: beads.db)
// - Dolt: directory name (default: dolt)
//
// Backward-compat: early dolt configs wrote "beads.db" even when Backend=dolt.
// In that case, treat it as "dolt".
if backend == BackendDolt {
db := strings.TrimSpace(c.Database)
if db == "" || db == "beads.db" {
db = "dolt"
}
if filepath.IsAbs(db) {
return db
}
return filepath.Join(beadsDir, db)
}
// SQLite (default)
db := strings.TrimSpace(c.Database)
if db == "" {
db = "beads.db"
}
if filepath.IsAbs(db) {
return db
}
return filepath.Join(beadsDir, db)
}
func (c *Config) JSONLPath(beadsDir string) string {
+41 -19
View File
@@ -8,7 +8,7 @@ import (
func TestDefaultConfig(t *testing.T) {
cfg := DefaultConfig()
if cfg.Database != "beads.db" {
t.Errorf("Database = %q, want beads.db", cfg.Database)
}
@@ -25,26 +25,26 @@ func TestLoadSaveRoundtrip(t *testing.T) {
if err := os.MkdirAll(beadsDir, 0750); err != nil {
t.Fatalf("failed to create .beads directory: %v", err)
}
cfg := DefaultConfig()
if err := cfg.Save(beadsDir); err != nil {
t.Fatalf("Save() failed: %v", err)
}
loaded, err := Load(beadsDir)
if err != nil {
t.Fatalf("Load() failed: %v", err)
}
if loaded == nil {
t.Fatal("Load() returned nil config")
}
if loaded.Database != cfg.Database {
t.Errorf("Database = %q, want %q", loaded.Database, cfg.Database)
}
if loaded.JSONLExport != cfg.JSONLExport {
t.Errorf("JSONLExport = %q, want %q", loaded.JSONLExport, cfg.JSONLExport)
}
@@ -52,12 +52,12 @@ func TestLoadSaveRoundtrip(t *testing.T) {
func TestLoadNonexistent(t *testing.T) {
tmpDir := t.TempDir()
cfg, err := Load(tmpDir)
if err != nil {
t.Fatalf("Load() returned error for nonexistent config: %v", err)
}
if cfg != nil {
t.Errorf("Load() = %v, want nil for nonexistent config", cfg)
}
@@ -66,22 +66,44 @@ func TestLoadNonexistent(t *testing.T) {
func TestDatabasePath(t *testing.T) {
beadsDir := "/home/user/project/.beads"
cfg := &Config{Database: "beads.db"}
got := cfg.DatabasePath(beadsDir)
want := filepath.Join(beadsDir, "beads.db")
if got != want {
t.Errorf("DatabasePath() = %q, want %q", got, want)
}
}
func TestDatabasePath_Dolt(t *testing.T) {
beadsDir := "/home/user/project/.beads"
t.Run("explicit dolt dir", func(t *testing.T) {
cfg := &Config{Database: "dolt", Backend: BackendDolt}
got := cfg.DatabasePath(beadsDir)
want := filepath.Join(beadsDir, "dolt")
if got != want {
t.Errorf("DatabasePath() = %q, want %q", got, want)
}
})
t.Run("backward compat: dolt backend with beads.db field", func(t *testing.T) {
cfg := &Config{Database: "beads.db", Backend: BackendDolt}
got := cfg.DatabasePath(beadsDir)
want := filepath.Join(beadsDir, "dolt")
if got != want {
t.Errorf("DatabasePath() = %q, want %q", got, want)
}
})
}
func TestJSONLPath(t *testing.T) {
beadsDir := "/home/user/project/.beads"
tests := []struct {
name string
cfg *Config
want string
name string
cfg *Config
want string
}{
{
name: "default",
@@ -99,7 +121,7 @@ func TestJSONLPath(t *testing.T) {
want: filepath.Join(beadsDir, "issues.jsonl"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.cfg.JSONLPath(beadsDir)
@@ -122,9 +144,9 @@ func TestConfigPath(t *testing.T) {
func TestGetDeletionsRetentionDays(t *testing.T) {
tests := []struct {
name string
cfg *Config
want int
name string
cfg *Config
want int
}{
{
name: "zero uses default",
+24 -2
View File
@@ -122,11 +122,33 @@ func GetGitCommonDir() (string, error) {
// and live in the common git directory (e.g., /repo/.git/hooks), not in
// the worktree-specific directory (e.g., /repo/.git/worktrees/feature/hooks).
func GetGitHooksDir() (string, error) {
commonDir, err := GetGitCommonDir()
ctx, err := getGitContext()
if err != nil {
return "", err
}
return filepath.Join(commonDir, "hooks"), nil
// Respect core.hooksPath if configured.
// This is used by beads' Dolt backend (hooks installed to .beads/hooks/).
cmd := exec.Command("git", "config", "--get", "core.hooksPath")
cmd.Dir = ctx.repoRoot
if out, err := cmd.Output(); err == nil {
hooksPath := strings.TrimSpace(string(out))
if hooksPath != "" {
if filepath.IsAbs(hooksPath) {
return hooksPath, nil
}
// Git treats relative core.hooksPath as relative to the repo root in common usage.
// (e.g., ".beads/hooks", ".githooks").
p := filepath.Join(ctx.repoRoot, hooksPath)
if abs, err := filepath.Abs(p); err == nil {
return abs, nil
}
return p, nil
}
}
// Default: hooks are stored in the common git directory.
return filepath.Join(ctx.commonDir, "hooks"), nil
}
// GetGitRefsDir returns the path to the Git refs directory.
+1 -1
View File
@@ -140,7 +140,7 @@ func findJSONLPath(beadsDir string) string {
func acquireBootstrapLock(lockPath string, timeout time.Duration) (*os.File, error) {
// Create lock file
// #nosec G304 - controlled path
f, err := os.OpenFile(lockPath, os.O_CREATE|os.O_RDWR, 0644)
f, err := os.OpenFile(lockPath, os.O_CREATE|os.O_RDWR, 0600)
if err != nil {
return nil, fmt.Errorf("failed to create lock file: %w", err)
}
+30 -11
View File
@@ -439,25 +439,23 @@ func (s *DoltStore) GetStaleIssues(ctx context.Context, filter types.StaleFilter
func (s *DoltStore) GetStatistics(ctx context.Context) (*types.Statistics, error) {
stats := &types.Statistics{}
// Count by status
// Get counts (mirror SQLite semantics: exclude tombstones from TotalIssues, report separately).
// Important: COALESCE to avoid NULL scans when the table is empty.
err := s.db.QueryRowContext(ctx, `
SELECT
COUNT(*) as total,
SUM(CASE WHEN status = 'open' THEN 1 ELSE 0 END) as open_count,
SUM(CASE WHEN status = 'in_progress' THEN 1 ELSE 0 END) as in_progress,
SUM(CASE WHEN status = 'closed' THEN 1 ELSE 0 END) as closed,
SUM(CASE WHEN status = 'blocked' THEN 1 ELSE 0 END) as blocked,
SUM(CASE WHEN status = 'deferred' THEN 1 ELSE 0 END) as deferred,
SUM(CASE WHEN status = 'tombstone' THEN 1 ELSE 0 END) as tombstone,
SUM(CASE WHEN pinned = 1 THEN 1 ELSE 0 END) as pinned
COALESCE(SUM(CASE WHEN status != 'tombstone' THEN 1 ELSE 0 END), 0) as total,
COALESCE(SUM(CASE WHEN status = 'open' THEN 1 ELSE 0 END), 0) as open_count,
COALESCE(SUM(CASE WHEN status = 'in_progress' THEN 1 ELSE 0 END), 0) as in_progress,
COALESCE(SUM(CASE WHEN status = 'closed' THEN 1 ELSE 0 END), 0) as closed,
COALESCE(SUM(CASE WHEN status = 'deferred' THEN 1 ELSE 0 END), 0) as deferred,
COALESCE(SUM(CASE WHEN status = 'tombstone' THEN 1 ELSE 0 END), 0) as tombstone,
COALESCE(SUM(CASE WHEN pinned = 1 THEN 1 ELSE 0 END), 0) as pinned
FROM issues
WHERE status != 'tombstone'
`).Scan(
&stats.TotalIssues,
&stats.OpenIssues,
&stats.InProgressIssues,
&stats.ClosedIssues,
&stats.BlockedIssues,
&stats.DeferredIssues,
&stats.TombstoneIssues,
&stats.PinnedIssues,
@@ -466,6 +464,27 @@ func (s *DoltStore) GetStatistics(ctx context.Context) (*types.Statistics, error
return nil, fmt.Errorf("failed to get statistics: %w", err)
}
// Blocked count (same semantics as SQLite: blocked by open deps).
err = s.db.QueryRowContext(ctx, `
SELECT COUNT(DISTINCT i.id)
FROM issues i
JOIN dependencies d ON i.id = d.issue_id
JOIN issues blocker ON d.depends_on_id = blocker.id
WHERE i.status IN ('open', 'in_progress', 'blocked', 'deferred', 'hooked')
AND d.type = 'blocks'
AND blocker.status IN ('open', 'in_progress', 'blocked', 'deferred', 'hooked')
`).Scan(&stats.BlockedIssues)
if err != nil {
return nil, fmt.Errorf("failed to get blocked count: %w", err)
}
// Ready count (use the ready_issues view).
// Note: view already excludes ephemeral issues and blocked transitive deps.
err = s.db.QueryRowContext(ctx, `SELECT COUNT(*) FROM ready_issues`).Scan(&stats.ReadyIssues)
if err != nil {
return nil, fmt.Errorf("failed to get ready count: %w", err)
}
return stats, nil
}
+1 -4
View File
@@ -4,7 +4,6 @@ package factory
import (
"context"
"fmt"
"path/filepath"
"time"
"github.com/steveyegge/beads/internal/configfile"
@@ -84,9 +83,7 @@ func NewFromConfigWithOptions(ctx context.Context, beadsDir string, opts Options
case configfile.BackendSQLite:
return NewWithOptions(ctx, backend, cfg.DatabasePath(beadsDir), opts)
case configfile.BackendDolt:
// For Dolt, use a subdirectory to store the Dolt database
doltPath := filepath.Join(beadsDir, "dolt")
return NewWithOptions(ctx, backend, doltPath, opts)
return NewWithOptions(ctx, backend, cfg.DatabasePath(beadsDir), opts)
default:
return nil, fmt.Errorf("unknown storage backend in config: %s", backend)
}