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

@@ -698,11 +698,24 @@ func flushToJSONLWithState(state flushState) {
if err := store.SetMetadata(ctx, "last_import_hash", exportedHash); err != nil {
fmt.Fprintf(os.Stderr, "Warning: failed to update last_import_hash after export: %v\n", err)
}
// Store JSONL file hash for integrity validation (bd-160)
if err := store.SetJSONLFileHash(ctx, exportedHash); err != nil {
fmt.Fprintf(os.Stderr, "Warning: failed to update jsonl_file_hash after export: %v\n", err)
}
// Update last_import_time so staleness check doesn't see JSONL as "newer" (fixes #399)
// CheckStaleness() compares last_import_time against JSONL mtime. After export,
// the JSONL mtime is updated, so we must also update last_import_time to prevent
// false "stale" detection on subsequent reads.
//
// Use RFC3339Nano to preserve nanosecond precision. The file mtime has nanosecond
// precision, so using RFC3339 (second precision) would cause the stored time to be
// slightly earlier than the file mtime, triggering false staleness.
exportTime := time.Now().Format(time.RFC3339Nano)
if err := store.SetMetadata(ctx, "last_import_time", exportTime); err != nil {
fmt.Fprintf(os.Stderr, "Warning: failed to update last_import_time after export: %v\n", err)
}
}
// Success! Don't clear global flags here - let the caller manage its own state.