fix: staleness check fails after write in git worktrees (#399)

After write operations in git worktrees, subsequent reads failed with
"Database out of sync with JSONL" even though the hash check passed.

Root cause: flushToJSONLWithState() updated last_import_hash but not
last_import_time after export. CheckStaleness() compares last_import_time
against JSONL mtime, so after export the JSONL appeared "newer" than the
last import.

Additional issue: RFC3339 only has second precision but file mtimes have
nanosecond precision, causing false positives when times were within the
same second.

Fix:
- Update last_import_time after export in flushToJSONLWithState()
- Use RFC3339Nano format for nanosecond precision
- Update CheckStaleness() to parse both formats for backward compatibility

🤖 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-26 19:04:32 -08:00
parent eeda2053b8
commit d0bb0ad7d4
3 changed files with 72 additions and 54 deletions

View File

@@ -274,10 +274,15 @@ func CheckStaleness(ctx context.Context, store storage.Storage, dbPath string) (
return false, nil
}
lastImportTime, err := time.Parse(time.RFC3339, lastImportStr)
// Try RFC3339Nano first (has nanosecond precision), fall back to RFC3339
// This supports both old timestamps (RFC3339) and new ones with nanoseconds (fixes #399)
lastImportTime, err := time.Parse(time.RFC3339Nano, lastImportStr)
if err != 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)
lastImportTime, err = time.Parse(time.RFC3339, lastImportStr)
if err != 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