Fix daemon race condition: prevent stale exports
- Add JSONL timestamp check in validatePreExport - Refuse export if JSONL is newer than database - Force daemon to import before exporting when JSONL updated - Add test case for JSONL-newer-than-DB scenario - Fixes bd-89e2
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
"math"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/steveyegge/beads/internal/storage"
|
||||
"github.com/steveyegge/beads/internal/types"
|
||||
@@ -14,6 +15,20 @@ import (
|
||||
// validatePreExport performs integrity checks before exporting database to JSONL.
|
||||
// Returns error if critical issues found that would cause data loss.
|
||||
func validatePreExport(ctx context.Context, store storage.Storage, jsonlPath string) error {
|
||||
// Check if JSONL is newer than database - if so, must import first
|
||||
jsonlInfo, jsonlStatErr := os.Stat(jsonlPath)
|
||||
if jsonlStatErr == nil {
|
||||
beadsDir := filepath.Dir(jsonlPath)
|
||||
dbPath := filepath.Join(beadsDir, "beads.db")
|
||||
dbInfo, dbStatErr := os.Stat(dbPath)
|
||||
if dbStatErr == nil {
|
||||
// If JSONL is newer, refuse export - caller must import first
|
||||
if jsonlInfo.ModTime().After(dbInfo.ModTime()) {
|
||||
return fmt.Errorf("refusing to export: JSONL is newer than database (import first to avoid data loss)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Get database issue count (fast path with COUNT(*) if available)
|
||||
dbCount, err := countDBIssuesFast(ctx, store)
|
||||
if err != nil {
|
||||
@@ -22,13 +37,12 @@ func validatePreExport(ctx context.Context, store storage.Storage, jsonlPath str
|
||||
|
||||
// Get JSONL issue count
|
||||
jsonlCount := 0
|
||||
fileInfo, statErr := os.Stat(jsonlPath)
|
||||
if statErr == nil {
|
||||
if jsonlStatErr == nil {
|
||||
jsonlCount, err = countIssuesInJSONL(jsonlPath)
|
||||
if err != nil {
|
||||
// Conservative: if JSONL exists with content but we can't count it,
|
||||
// and DB is empty, refuse to export (potential data loss)
|
||||
if dbCount == 0 && fileInfo.Size() > 0 {
|
||||
if dbCount == 0 && jsonlInfo.Size() > 0 {
|
||||
return fmt.Errorf("refusing to export empty DB over existing JSONL whose contents couldn't be verified: %w", err)
|
||||
}
|
||||
// Warning for other cases
|
||||
|
||||
@@ -4,7 +4,9 @@ import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/steveyegge/beads/internal/types"
|
||||
)
|
||||
@@ -111,6 +113,55 @@ func TestValidatePreExport(t *testing.T) {
|
||||
t.Error("Expected error for empty DB over unreadable non-empty JSONL, got nil")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("JSONL newer than DB fails", func(t *testing.T) {
|
||||
// Create temp directory
|
||||
tmpDir := t.TempDir()
|
||||
beadsDir := filepath.Join(tmpDir, ".beads")
|
||||
if err := os.MkdirAll(beadsDir, 0755); err != nil {
|
||||
t.Fatalf("Failed to create .beads dir: %v", err)
|
||||
}
|
||||
dbPath := filepath.Join(beadsDir, "beads.db")
|
||||
jsonlPath := filepath.Join(beadsDir, "beads.jsonl")
|
||||
|
||||
// Create database with issue
|
||||
store := newTestStoreWithPrefix(t, dbPath, "bd")
|
||||
ctx := context.Background()
|
||||
issue := &types.Issue{
|
||||
ID: "bd-1",
|
||||
Title: "Test",
|
||||
Status: types.StatusOpen,
|
||||
Priority: 1,
|
||||
IssueType: types.TypeTask,
|
||||
Description: "Test issue",
|
||||
}
|
||||
if err := store.CreateIssue(ctx, issue, "test"); err != nil {
|
||||
t.Fatalf("Failed to create issue: %v", err)
|
||||
}
|
||||
|
||||
// Create JSONL file with newer timestamp
|
||||
jsonlContent := `{"id":"bd-1","title":"Test","status":"open","priority":1}
|
||||
`
|
||||
if err := os.WriteFile(jsonlPath, []byte(jsonlContent), 0600); err != nil {
|
||||
t.Fatalf("Failed to write JSONL: %v", err)
|
||||
}
|
||||
|
||||
// Touch JSONL to make it newer than DB
|
||||
// (in real scenario, this happens when git pull updates JSONL but daemon hasn't imported yet)
|
||||
futureTime := time.Now().Add(1 * time.Second)
|
||||
if err := os.Chtimes(jsonlPath, futureTime, futureTime); err != nil {
|
||||
t.Fatalf("Failed to touch JSONL: %v", err)
|
||||
}
|
||||
|
||||
// Should fail validation (JSONL is newer, must import first)
|
||||
err := validatePreExport(ctx, store, jsonlPath)
|
||||
if err == nil {
|
||||
t.Error("Expected error for JSONL newer than DB, got nil")
|
||||
}
|
||||
if err != nil && !strings.Contains(err.Error(), "JSONL is newer than database") {
|
||||
t.Errorf("Expected 'JSONL is newer' error, got: %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestValidatePostImport(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user