Fix: Improve staleness check error handling (bd-2q6d, bd-o4qy, bd-n4td)

Changes:
- CheckStaleness now returns errors for corrupted last_import_time metadata
  instead of silently returning false (bd-o4qy)
- Added handling for empty string metadata (memory store behavior)
- Enhanced warning messages when staleness check fails to be more explicit
  that operation continues with potentially stale data (bd-n4td)
- Added test coverage for corrupted metadata scenario

Closes bd-2q6d, bd-o4qy, bd-n4td

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

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Steve Yegge
2025-11-23 20:54:18 -08:00
parent c39243710c
commit b75914b8ca
3 changed files with 34 additions and 3 deletions

View File

@@ -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
}

View File

@@ -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

View File

@@ -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