Files
beads/cmd/bd/direct_mode.go
Steve Yegge 57253f93a3 Context propagation with graceful cancellation (bd-rtp, bd-yb8, bd-2o2)
Complete implementation of signal-aware context propagation for graceful
cancellation across all commands and storage operations.

Key changes:

1. Signal-aware contexts (bd-rtp):
   - Added rootCtx/rootCancel in main.go using signal.NotifyContext()
   - Set up in PersistentPreRun, cancelled in PersistentPostRun
   - Daemon uses same pattern in runDaemonLoop()
   - Handles SIGINT/SIGTERM for graceful shutdown

2. Context propagation (bd-yb8):
   - All commands now use rootCtx instead of context.Background()
   - sqlite.New() receives context for cancellable operations
   - Database operations respect context cancellation
   - Storage layer propagates context through all queries

3. Cancellation tests (bd-2o2):
   - Added import_cancellation_test.go with comprehensive tests
   - Added export cancellation test in export_test.go
   - Tests verify database integrity after cancellation
   - All cancellation tests passing

Fixes applied during review:
   - Fixed rootCtx lifecycle (removed premature defer from PersistentPreRun)
   - Fixed test context contamination (reset rootCtx in test cleanup)
   - Fixed export tests missing context setup

Impact:
   - Pressing Ctrl+C during import/export now cancels gracefully
   - No database corruption or hanging transactions
   - Clean shutdown of all operations

Tested:
   - go build ./cmd/bd ✓
   - go test ./cmd/bd -run TestImportCancellation ✓
   - go test ./cmd/bd -run TestExportCommand ✓
   - Manual Ctrl+C testing verified

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-20 21:57:23 -05:00

84 lines
1.9 KiB
Go

package main
import (
"fmt"
"github.com/steveyegge/beads/internal/beads"
"github.com/steveyegge/beads/internal/debug"
"github.com/steveyegge/beads/internal/storage/sqlite"
)
// 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 daemonClient != 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 daemonClient != nil {
_ = daemonClient.Close()
daemonClient = nil
}
daemonStatus.Mode = "direct"
daemonStatus.Connected = false
daemonStatus.Degraded = true
if reason != "" {
daemonStatus.Detail = reason
}
if daemonStatus.FallbackReason == FallbackNone {
daemonStatus.FallbackReason = FallbackDaemonUnsupported
}
if reason != "" {
debug.Logf("Debug: %s\n", reason)
}
}
// ensureStoreActive guarantees that a local SQLite store is initialized and tracked.
func ensureStoreActive() error {
storeMutex.Lock()
active := storeActive && store != nil
storeMutex.Unlock()
if active {
return nil
}
if dbPath == "" {
if found := beads.FindDatabasePath(); found != "" {
dbPath = found
} else {
return fmt.Errorf("no beads database found. Hint: run 'bd init' in this directory")
}
}
sqlStore, err := sqlite.New(rootCtx, dbPath)
if err != nil {
return fmt.Errorf("failed to open database: %w", err)
}
storeMutex.Lock()
store = sqlStore
storeActive = true
storeMutex.Unlock()
if autoImportEnabled {
autoImportIfNewer()
}
return nil
}