Skip to content

fix(recall): comprehensive cache invalidation + FTS5 scoring - #854

Merged
ajianaz merged 1 commit into
developfrom
fix/cache-and-scoring-bugs
Aug 5, 2026
Merged

ajianaz merged 1 commit into
developfrom
fix/cache-and-scoring-bugs

Conversation

@ajianaz

@ajianaz ajianaz commented Aug 4, 2026

Copy link
Copy Markdown
Collaborator

What

Comprehensive fix for 6 cache invalidation bugs + FTS5 scoring bug found during audit round 2.

Why

The RecallCache (introduced for performance) had incomplete invalidation coverage — 7 out of 10 mutation operations either skipped cache invalidation entirely or failed to flush cross-namespace ("all") entries. Additionally, recall_fts5_only() hardcoded score=1.0 for all results, making min_score filtering useless.

Root cause: 67% of bugs stem from incomplete cache invalidation when RecallCache was introduced. The invalidate_namespace() function itself had a blind spot for "all" entries.

How

Bug #1 (Major) — invalidate_namespace blind spot

recall_cache.rs: invalidate_namespace("ns1") flushed entries where namespace == "ns1" but NOT entries where namespace == "all". Any namespace-scoped mutation (remember, forget, delete_tag) left stale cross-namespace query results.

Fix: Also flush entries with namespace == "all".

Bug #2 (Major) — rename_tag missing invalidation

operations.rs: rename_tag() updated the DB but never invalidated the cache. Renamed tags would show stale FTS5 results indefinitely.

Fix: Add invalidate_namespace() or clear() after rename.

Bug #3 (Major) — FTS5 score=1.0

operations.rs: recall_fts5_only() ignored the BM25 rank from search_fts5() and hardcoded score = 1.0f32 for all results. This made min_score filtering completely non-functional and destroyed relevance ranking.

Fix: Min-max normalization of BM25 ranks → ((rank - max_rank) / (min_rank - max_rank)).clamp(0.0, 1.0). Single result or tied ranks → 1.0 (guard with EPSILON).

Bug #4 (Minor) — pin/unpin/set_importance/feedback

lib.rs: These single-memory mutations affect ranking but did not flush cache.

Fix: Added invalidate_cache_for_id() helper that looks up namespace via get_by_id(), then calls invalidate_namespace(). Falls back to clear() on lookup failure.

Bug #5 (Minor) — consolidate/dream

consolidate.rs + dream.rs: These operations delete memories but did not invalidate cache.

Fix: Post-mutation flush (skip on dry_run for dream).

Bug #6 (Minor) — recompute_importance

lib.rs: Updates all memories but did not flush cache.

Fix: clear() when updated > 0.

Testing

  • cargo test --workspace passes — 432 passed, 0 failed, 24 ignored
  • cargo clippy --workspace --all-targets -- -D warnings passes — 0 warnings
  • Cora review — LLM provider temporarily unavailable, manual review performed
  • Manual verification: all mutation→cache paths traced and confirmed

Affected subsystem: RecallCache (load-bearing), FTS5 search scoring

Related Issues

Closes #849

Checklist

  • Code follows project conventions
  • No new warnings introduced
  • All tests pass
  • Changes are backward-compatible (no API changes)

Bug #1 (Major): invalidate_namespace() did not flush 'all' namespace
entries, causing stale results for cross-namespace queries after any
namespace-scoped mutation (remember, forget, delete_tag).

Bug #2 (Major): rename_tag() missing cache invalidation entirely.

Bug #3 (Major): recall_fts5_only() hardcoded score=1.0 for ALL results,
making min_score filter useless. Replaced with min-max normalization
of BM25 ranks (0.0..1.0).

Bug #4 (Minor): pin/unpin/set_importance/feedback missing invalidation.
Added invalidate_cache_for_id() helper.

Bug #5 (Minor): consolidate() and dream() delete memories but did not
invalidate cache. Added post-mutation flush.

Bug #6 (Minor): recompute_importance() did not flush cache.

Tests: 432 passed, 0 failed, 24 ignored. Clippy: 0 warnings.
@github-actions

github-actions Bot commented Aug 4, 2026

Copy link
Copy Markdown

🔍 Cora AI Code Review

⚠️ Review could not complete. Cora was unable to reach the LLM API after retries. Check the workflow logs for errors.


Review powered by cora-code · BYOK · MIT

@ajianaz
ajianaz merged commit aa9a0bc into develop Aug 5, 2026
13 checks passed
@ajianaz
ajianaz deleted the fix/cache-and-scoring-bugs branch August 5, 2026 00:13
ajianaz added a commit that referenced this pull request Aug 5, 2026
* fix(recall): normalize FTS5 scores AFTER filtering, not before (#854)

Cora review caught: min-max normalization was computed from ALL
fts_results before namespace/tag filtering. If the best/worst items
get filtered out, surviving scores no longer span 0.0..1.0 correctly.

Restructured: filter → collect → compute range → map → take.

Tests: 432 passed, 0 failed.

* style: cargo fmt

---------

Co-authored-by: ajianaz <[email protected]>
ajianaz added a commit that referenced this pull request Aug 13, 2026
Bug #1 (Major): invalidate_namespace() did not flush 'all' namespace
entries, causing stale results for cross-namespace queries after any
namespace-scoped mutation (remember, forget, delete_tag).

Bug #2 (Major): rename_tag() missing cache invalidation entirely.

Bug #3 (Major): recall_fts5_only() hardcoded score=1.0 for ALL results,
making min_score filter useless. Replaced with min-max normalization
of BM25 ranks (0.0..1.0).

Bug #4 (Minor): pin/unpin/set_importance/feedback missing invalidation.
Added invalidate_cache_for_id() helper.

Bug #5 (Minor): consolidate() and dream() delete memories but did not
invalidate cache. Added post-mutation flush.

Bug #6 (Minor): recompute_importance() did not flush cache.

Tests: 432 passed, 0 failed, 24 ignored. Clippy: 0 warnings.

Co-authored-by: ajianaz <[email protected]>
ajianaz added a commit that referenced this pull request Aug 13, 2026
* fix(recall): normalize FTS5 scores AFTER filtering, not before (#854)

Cora review caught: min-max normalization was computed from ALL
fts_results before namespace/tag filtering. If the best/worst items
get filtered out, surviving scores no longer span 0.0..1.0 correctly.

Restructured: filter → collect → compute range → map → take.

Tests: 432 passed, 0 failed.

* style: cargo fmt

---------

Co-authored-by: ajianaz <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant