fix(staleness): use RFC3339Nano precision for last_import_time (#399)
The staleness check compares last_import_time against JSONL file mtime. File mtime has nanosecond precision, but last_import_time was stored with only second precision (RFC3339). This caused a race condition where the stored time could be slightly earlier than the file mtime, triggering false "Database out of sync" errors - particularly in git worktrees. Changed all 6 locations that set last_import_time to use RFC3339Nano. The CheckStaleness parser already handles both formats, so this is backward compatible. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -231,7 +231,8 @@ func autoImportIfNewer() {
|
||||
}
|
||||
|
||||
// Store import timestamp (bd-159: for staleness detection)
|
||||
importTime := time.Now().Format(time.RFC3339)
|
||||
// Use RFC3339Nano for nanosecond precision to avoid race with file mtime (fixes #399)
|
||||
importTime := time.Now().Format(time.RFC3339Nano)
|
||||
if err := store.SetMetadata(ctx, "last_import_time", importTime); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Warning: failed to update last_import_time after import: %v\n", err)
|
||||
}
|
||||
|
||||
@@ -295,7 +295,8 @@ func updateExportMetadata(ctx context.Context, store storage.Storage, jsonlPath
|
||||
log.log("Next export may require running 'bd import' first")
|
||||
}
|
||||
|
||||
exportTime := time.Now().Format(time.RFC3339)
|
||||
// Use RFC3339Nano for nanosecond precision to avoid race with file mtime (fixes #399)
|
||||
exportTime := time.Now().Format(time.RFC3339Nano)
|
||||
if err := store.SetMetadata(ctx, timeKey, exportTime); err != nil {
|
||||
log.log("Warning: failed to update %s: %v", timeKey, err)
|
||||
}
|
||||
|
||||
@@ -357,7 +357,8 @@ NOTE: Import requires direct database access and does not work with daemon mode.
|
||||
// is unavailable. This ensures import operations always succeed even if metadata storage fails.
|
||||
debug.Logf("Warning: failed to update last_import_hash: %v", err)
|
||||
}
|
||||
importTime := time.Now().Format(time.RFC3339)
|
||||
// Use RFC3339Nano for nanosecond precision to avoid race with file mtime (fixes #399)
|
||||
importTime := time.Now().Format(time.RFC3339Nano)
|
||||
if err := store.SetMetadata(ctx, "last_import_time", importTime); err != nil {
|
||||
// Non-fatal warning (see above comment about graceful degradation)
|
||||
debug.Logf("Warning: failed to update last_import_time: %v", err)
|
||||
|
||||
@@ -945,7 +945,8 @@ func exportToJSONL(ctx context.Context, jsonlPath string) error {
|
||||
// is unavailable. This ensures export operations always succeed even if metadata storage fails.
|
||||
fmt.Fprintf(os.Stderr, "Warning: failed to update last_import_hash: %v\n", err)
|
||||
}
|
||||
exportTime := time.Now().Format(time.RFC3339)
|
||||
// Use RFC3339Nano for nanosecond precision to avoid race with file mtime (fixes #399)
|
||||
exportTime := time.Now().Format(time.RFC3339Nano)
|
||||
if err := store.SetMetadata(ctx, "last_import_time", exportTime); err != nil {
|
||||
// Non-fatal warning (see above comment about graceful degradation)
|
||||
fmt.Fprintf(os.Stderr, "Warning: failed to update last_import_time: %v\n", err)
|
||||
|
||||
Reference in New Issue
Block a user