This commit implements three related improvements to database staleness checking: **bd-n4td (P2): Add warning when staleness check errors** - Added stderr warnings when CheckStaleness fails in ensureDatabaseFresh - Users now see "Warning: could not check database staleness: <error>" - Provides visibility into staleness check failures while allowing operations to proceed **bd-o4qy (P2): Improve CheckStaleness error handling** - Updated CheckStaleness to distinguish between expected and abnormal conditions: * Returns (false, nil) for expected "no data yet" scenarios (missing metadata, missing JSONL) * Returns (false, err) for abnormal errors (glob failures, permission errors) - Updated RPC server (2 locations) to log staleness errors but allow requests to proceed - Prevents blocking operations due to transient staleness check issues - Added comprehensive function documentation **bd-c4rq (P3): Refactor staleness check to avoid function call overhead** - Moved daemon check from inside ensureDatabaseFresh to all 8 call sites - Avoids unnecessary function call when running in daemon mode - Updated: list.go, info.go, status.go, show.go, stale.go, duplicates.go, ready.go, validate.go - Extracted staleness functions to new staleness.go for better organization **Code review fixes:** - Removed dead code in CheckStaleness (unreachable jsonlPath == "" check) - Removed unused ensureDatabaseFreshQuiet function **Files changed:** - New: cmd/bd/staleness.go (extracted staleness checking functions) - Modified: 8 command files (added daemon check before staleness calls) - Modified: internal/autoimport/autoimport.go (improved error handling) - Modified: internal/rpc/server_export_import_auto.go (handle errors gracefully)
52 lines
1.7 KiB
Go
52 lines
1.7 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
|
|
"github.com/steveyegge/beads/internal/autoimport"
|
|
)
|
|
|
|
// ensureDatabaseFresh checks if the database is in sync with JSONL before read operations.
|
|
// If JSONL is newer than database, refuses to operate with an error message.
|
|
// This prevents users from making decisions based on stale/incomplete data.
|
|
//
|
|
// NOTE: Callers must check if daemonClient != nil and skip calling this function
|
|
// when running in daemon mode (daemon auto-imports on staleness).
|
|
//
|
|
// Implements bd-2q6d: All read operations should validate database freshness.
|
|
// Implements bd-c4rq: Daemon check moved to call sites to avoid function call overhead.
|
|
func ensureDatabaseFresh(ctx context.Context) error {
|
|
// Skip check if no storage available (shouldn't happen in practice)
|
|
if store == nil {
|
|
return nil
|
|
}
|
|
|
|
// Check if database is stale
|
|
isStale, err := autoimport.CheckStaleness(ctx, store, dbPath)
|
|
if err != nil {
|
|
// If we can't determine staleness, allow operation to proceed
|
|
// (better to show potentially stale data than block user)
|
|
fmt.Fprintf(os.Stderr, "Warning: could not check database staleness: %v\n", err)
|
|
return nil
|
|
}
|
|
|
|
if !isStale {
|
|
// Database is fresh, proceed
|
|
return nil
|
|
}
|
|
|
|
// Database is stale - refuse to operate
|
|
return fmt.Errorf(
|
|
"Database out of sync with JSONL. Run 'bd import' first.\n\n"+
|
|
"The JSONL file has been updated (e.g., after 'git pull') but the database\n"+
|
|
"hasn't been imported yet. This would cause you to see stale/incomplete data.\n\n"+
|
|
"To fix:\n"+
|
|
" bd import # Import JSONL updates to database\n\n"+
|
|
"Or use daemon mode (auto-imports on every operation):\n"+
|
|
" bd daemon start\n"+
|
|
" bd <command> # Will auto-import before executing",
|
|
)
|
|
}
|