Skip to content

fix(kb): invalidate semantic and topic retrieval caches on every KB write (#20958) - #21049

Merged
mrveiss merged 9 commits into
mainfrom
issue-20958-kb-cache-invalidation
Nov 12, 2026
Merged

mrveiss merged 9 commits into
mainfrom
issue-20958-kb-cache-invalidation

Conversation

@mrveiss

@mrveiss mrveiss commented Nov 7, 2026 •

Copy link
Copy Markdown
Owner

Thinking Path

Semantic and topic retrieval caches could keep serving results after a knowledge-base write, so answers went stale. The fix keys the retrieval caches on the KB epoch and bumps it from one hook that every write path goes through, rather than patching each cache or each call site.

What Changed

  • Retrieval caches are keyed on the KB epoch.
  • One hook in autobot-backend/knowledge/retrieval_invalidation.py: invalidate_retrieval_caches and @invalidates_retrieval_caches (invalidates in finally, so a failed write still invalidates).
  • @invalidating_writes is applied at the composition point knowledge/_composed.py, driven by a single WRITE_ENTRY_POINTS list.
  • Covered write paths: fact store/update/delete/delete-by-session, delete_collection, bulk_delete_collection_facts, the write-buffer chroma flush, doc reindex_file and drop_collection, and KBSynthesizer._index_documents.
  • Guard retrieval_invalidation_20958_test.py: entry points must keep the decorator, and the 26 raw vector-write sites are each classified.
  • Out of scope, filed: bug(kb): doc index dimension-mismatch rebuild does not invalidate retrieval caches #20992 (sync dim-mismatch rebuild) and bug(kb): tag and category metadata writes leave stale labels in cached retrieval results #20993 (tag/category metadata writes).

Verification

  • Tests run locally: 50 + 306 + 26 passed. Local tool versions are below the CI floors, so CI is authoritative.
  • Guard: retrieval_invalidation_20958_test.py fails if an entry point loses the decorator or a raw vector-write site is unclassified.
  • AC2 is tested at the cache level, not end to end through update_fact, hence "Refs" rather than "Closes".

Risks

Extra epoch bumps on writes cost one cache miss per write; no data is changed. Metadata-only writes (#20993) and the sync rebuild (#20992) remain uncovered until those issues land.

Model Used

Claude Sonnet

Issue Link

Refs #20958

Changelog fragment

  • N/A (internal change)

Checklist

  • Code follows AutoBot patterns from CLAUDE.md
  • Tests added or updated
  • Documentation updated if behavior changed (N/A)
  • PR targets main (not release)
  • No secrets or credentials in the diff

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Retrieval results are refreshed after knowledge-base content is added, updated, indexed or removed, helping prevent outdated cached results from being served.
    • Cache invalidation also runs when a knowledge-base write fails.

@mrveiss mrveiss added bug Something isn't working priority: high backend rag phase:M1.8 Knowledge base: upload, web sources and chat retrieval work end to end labels Nov 7, 2026
@coderabbitai

coderabbitai Bot commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 11 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 19ebd20 and ce6840c.

Files selected for processing: 9

  • autobot-backend/knowledge/_composed.py
  • autobot-backend/knowledge/retrieval_invalidation.py
  • autobot-backend/knowledge/retrieval_invalidation_20958_test.py
  • autobot-backend/knowledge/write_buffer.py
  • autobot-backend/services/knowledge/doc_index_orphans.py
  • autobot-backend/services/knowledge/doc_indexer.py
  • autobot-backend/services/knowledge/kb_synthesizer.py
  • autobot-backend/services/rag_visibility.py
  • repo_tests/composed_is_facade_18122_test.py
Walkthrough

The change adds retrieval-cache invalidation based on ACL epoch updates. It applies invalidation to registered composed knowledge-base writes and selected vector, document, and synthesis write paths. Tests cover cache behaviour, decorator coverage, and registered write-path classification.

Changes: Retrieval cache invalidation

Layer / File(s) Summary
Invalidation contract and composed writes
autobot-backend/knowledge/retrieval_invalidation.py, autobot-backend/knowledge/_composed.py
A registry maps knowledge-base write methods to invalidation reasons. Async decorators bump the ACL epoch, including when a wrapped write raises. The class decorator applies invalidation to registered methods on KnowledgeBase.
Document, vector, and synthesis write paths
autobot-backend/knowledge/write_buffer.py, autobot-backend/services/knowledge/doc_index_orphans.py, autobot-backend/services/knowledge/doc_indexer.py, autobot-backend/services/knowledge/kb_synthesizer.py
Vector flushes, document collection deletion and indexing, and synthesis indexing now trigger cache invalidation. Rebuilds use the decorated collection-deletion helper.
Invalidation and write-path coverage tests
autobot-backend/knowledge/retrieval_invalidation_20958_test.py, repo_tests/composed_is_facade_18122_test.py
Tests cover epoch changes, cache behaviour, decorator coverage, and registered write paths. The composed-facade fixture adds no-op write methods to its mixin stubs.
Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: KB writes now invalidate semantic and topic retrieval caches. The changeset adds a shared invalidation decorator, applies it to fact, collection, vector flush, documen…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: KB writes now invalidate semantic and topic retrieval caches. The changeset adds a shared invalidation decorator, applies it to fact, collection, vector flush, document indexing and synthesis write paths, and adds tests. The title is specific and matches the primary objective. The '(#20958)' reference is an issue link, not a file list, and does not reduce clarity.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@github-actions

github-actions Bot commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

Notice: 29 open PRs — past the runaway threshold (25)

This is not a request to defer this PR. There is no cap on open PRs (#19397) -- nothing blocks opening or pushing a PR.

This notice only means the count is high enough to be worth a glance for a runaway — something opening PRs in a loop, or a merge pipeline that has stalled so nothing is draining.

Currently open:

If the queue is draining normally, ignore this. Otherwise:

  1. Check whether CI is dispatching at all — see the ci-dispatch-watchdog status on these PRs
  2. Approved PRs land through the merge train the coordinator sequences (merge commits, never squash) -- check the open chore(merge-train) PR is moving, not each PR on its own
  3. Look for a loop opening near-identical PRs

Warn-only runaway detector — .github/workflows/pr-queue-gate.yml. It never blocks a merge.

@github-actions

github-actions Bot commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

✅ SSOT Configuration Compliance: Passing

🎉 No new hardcoded values of either class — ssot and other both block.

Known backlog in pipeline-scripts/hardcoded_values_baseline.txt is suppressed and tracked in #14371.

This was referenced Nov 7, 2026
@mrveiss
mrveiss merged commit 85cc252 into main Nov 12, 2026
76 of 80 checks passed
mrveiss added a commit that referenced this pull request Nov 12, 2026
@mrveiss
mrveiss deleted the issue-20958-kb-cache-invalidation branch November 12, 2026 06:19
@mrveiss

mrveiss commented Nov 12, 2026

Copy link
Copy Markdown
Owner Author

Landed on main through merge train #21348 as 5293da2 (merged 2026-11-12 08:19 Riga). Member head ce6840c is an ancestor of origin/main; no revert of it is on main. Content check on origin/main: autobot-backend/knowledge/retrieval_invalidation.py (invalidate_retrieval_caches :121, invalidation_pending :139, invalidating_writes :179), imported at knowledge/_composed.py:24 and knowledge/write_buffer.py:32; retrieval_invalidation_20958_test.py present. Earlier train revert 6dcfc2b was undone by a6021f3; the files are byte-identical to the member head.

timothybrush pushed a commit to timothybrush/AutoBot-AI that referenced this pull request Nov 12, 2026
This was referenced Nov 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend bug Something isn't working phase:M1.8 Knowledge base: upload, web sources and chat retrieval work end to end priority: high rag

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant