Files
beads/cmd/bd/direct_mode.go
jasper b0a6a456ba
Some checks failed
CI / Check version consistency (push) Successful in 4s
CI / Check for .beads changes (push) Has been skipped
CI / Test (ubuntu-latest) (push) Failing after 8m12s
CI / Lint (push) Failing after 2m40s
CI / Test Nix Flake (push) Failing after 54s
Deploy Documentation / build (push) Failing after 2m15s
Deploy Documentation / deploy (push) Has been skipped
Nightly Full Tests / Full Test Suite (push) Failing after 36m33s
CI / Test (macos-latest) (push) Has been cancelled
CI / Test (Windows - smoke) (push) Has been cancelled
fix(sqlite): respect --lock-timeout flag in direct mode (bd-2zd.4)
The ensureStoreActive() function was ignoring the user-configured
--lock-timeout flag and always using the 30s default via sqlite.New().

This fix changes ensureStoreActive() to use sqlite.NewWithTimeout()
with the configured lockTimeout, allowing users to specify shorter
timeouts for multi-agent scenarios.

With this change, users can now run:
  bd --lock-timeout=500ms sync

to fail fast if the database is locked, rather than waiting 30s.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-25 10:57:05 -08:00

113 lines
3.2 KiB
Go

package main
import (
"fmt"
"os"
"path/filepath"
"github.com/steveyegge/beads/internal/beads"
"github.com/steveyegge/beads/internal/debug"
"github.com/steveyegge/beads/internal/storage/factory"
)
// ensureDirectMode makes sure the CLI is operating in direct-storage mode.
// If the daemon is active, it is cleanly disconnected and the shared store is opened.
func ensureDirectMode(reason string) error {
if getDaemonClient() != nil {
if err := fallbackToDirectMode(reason); err != nil {
return err
}
return nil
}
return ensureStoreActive()
}
// fallbackToDirectMode disables the daemon client and ensures a local store is ready.
func fallbackToDirectMode(reason string) error {
disableDaemonForFallback(reason)
return ensureStoreActive()
}
// disableDaemonForFallback closes the daemon client and updates status metadata.
func disableDaemonForFallback(reason string) {
if client := getDaemonClient(); client != nil {
_ = client.Close()
setDaemonClient(nil)
}
ds := getDaemonStatus()
ds.Mode = "direct"
ds.Connected = false
ds.Degraded = true
if reason != "" {
ds.Detail = reason
}
if ds.FallbackReason == FallbackNone {
ds.FallbackReason = FallbackDaemonUnsupported
}
setDaemonStatus(ds)
if reason != "" {
debug.Logf("Debug: %s\n", reason)
}
}
// ensureStoreActive guarantees that a storage backend is initialized and tracked.
// Uses the factory to respect metadata.json backend configuration (SQLite, Dolt embedded, or Dolt server).
func ensureStoreActive() error {
lockStore()
active := isStoreActive() && getStore() != nil
unlockStore()
if active {
return nil
}
// Find the .beads directory
beadsDir := beads.FindBeadsDir()
if beadsDir == "" {
return fmt.Errorf("no beads database found.\n" +
"Hint: run 'bd init' to create a database in the current directory,\n" +
" or use 'bd --no-db' for JSONL-only mode")
}
// Check if this is a JSONL-only project
jsonlPath := filepath.Join(beadsDir, "issues.jsonl")
if _, err := os.Stat(jsonlPath); err == nil {
// JSONL exists - check if no-db mode is configured
if isNoDbModeConfigured(beadsDir) {
return fmt.Errorf("this project uses JSONL-only mode (no SQLite database).\n" +
"Hint: use 'bd --no-db <command>' or set 'no-db: true' in config.yaml")
}
}
// Use factory to create the appropriate backend (SQLite, Dolt embedded, or Dolt server)
// based on metadata.json configuration, with lock timeout support for multi-agent operations
opts := factory.Options{LockTimeout: lockTimeout}
store, err := factory.NewFromConfigWithOptions(getRootContext(), beadsDir, opts)
if err != nil {
// Check for fresh clone scenario (JSONL exists but no database)
if _, statErr := os.Stat(jsonlPath); statErr == nil {
return fmt.Errorf("found JSONL file but no database: %s\n"+
"Hint: run 'bd init' to create the database and import issues,\n"+
" or use 'bd --no-db' for JSONL-only mode", jsonlPath)
}
return fmt.Errorf("failed to open database: %w", err)
}
// Update the database path for compatibility with code that expects it
if dbPath := beads.FindDatabasePath(); dbPath != "" {
setDBPath(dbPath)
}
lockStore()
setStore(store)
setStoreActive(true)
unlockStore()
if isAutoImportEnabled() {
autoImportIfNewer()
}
return nil
}