diff --git a/cmd/bd/staleness.go b/cmd/bd/staleness.go index 934d2224..5db36691 100644 --- a/cmd/bd/staleness.go +++ b/cmd/bd/staleness.go @@ -33,7 +33,8 @@ func ensureDatabaseFresh(ctx context.Context) error { 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) + fmt.Fprintf(os.Stderr, "Warning: Could not verify database freshness: %v\n", err) + fmt.Fprintf(os.Stderr, "Proceeding anyway. Data may be stale.\n\n") return nil } diff --git a/internal/autoimport/autoimport.go b/internal/autoimport/autoimport.go index 15d7d389..7d74b71b 100644 --- a/internal/autoimport/autoimport.go +++ b/internal/autoimport/autoimport.go @@ -262,10 +262,16 @@ func CheckStaleness(ctx context.Context, store storage.Storage, dbPath string) ( return false, nil } + // Empty string means no metadata was set (memory store behavior) + if lastImportStr == "" { + // No metadata yet - expected for first run + return false, nil + } + lastImportTime, err := time.Parse(time.RFC3339, lastImportStr) if err != nil { - // Corrupted metadata - not critical, assume not stale - return false, nil + // Corrupted metadata - this is abnormal and should be reported + return false, fmt.Errorf("corrupted last_import_time in metadata (cannot parse as RFC3339): %w", err) } // Find JSONL using database directory diff --git a/internal/autoimport/autoimport_test.go b/internal/autoimport/autoimport_test.go index fefa2f43..70d02964 100644 --- a/internal/autoimport/autoimport_test.go +++ b/internal/autoimport/autoimport_test.go @@ -343,6 +343,30 @@ func TestCheckStaleness_NewerJSONL(t *testing.T) { } } +func TestCheckStaleness_CorruptedMetadata(t *testing.T) { + store := memory.New("") + ctx := context.Background() + + tmpDir, err := os.MkdirTemp("", "bd-stale-test-*") + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(tmpDir) + + dbPath := filepath.Join(tmpDir, "bd.db") + + // Set invalid timestamp format + store.SetMetadata(ctx, "last_import_time", "not-a-valid-timestamp") + + _, err = CheckStaleness(ctx, store, dbPath) + if err == nil { + t.Error("Expected error for corrupted metadata, got nil") + } + if err != nil && !strings.Contains(err.Error(), "corrupted last_import_time") { + t.Errorf("Expected 'corrupted last_import_time' error, got: %v", err) + } +} + func TestCheckForMergeConflicts(t *testing.T) { tests := []struct { name string