Repository navigation
feat(rag): folder-scoped search filter - #1865
Conversation
Add an optional folder_path filter to RAG search so retrieval can be scoped to one Document Hub folder and all of its subfolders (#662). RAG service: - documents.folder_path column + partial (org_slug, folder_path) index - boundary-safe hierarchical prefix filter as the first generic per-document condition in _build_scope_clause (the seam #1517's metadata filters extend); applied to both FTS and vector channels - folder_path on POST /api/v1/search (flat wire field, normalized); semantic cache bypassed for folder-filtered queries (cache key has no scope dimension) - ingestion: folder_path parsed from the existing upload metadata form field, written by _insert_processing_row (insert + conflict-update) - batch PATCH /api/v1/documents/folder-paths to keep paths fresh on folder moves without re-extraction or re-embedding Platform: - uploadDocument resolves folderPath from the linked Hub document and sends it in the upload metadata (single chokepoint for all upload paths); uploadDocumentToRag/reindexDocumentInRag pass it explicitly - syncRagFolderPaths internal action + hooks on document folder moves (updateDocumentInternal) and WebDAV moves (moveResource, folder subtree fixup) - rag_search agent tool gains an optional folderPath arg (hierarchical, exact — contrasted with document_find's fuzzy non-recursive filter) - backfill action migrations/backfill_rag_folder_path for documents indexed before the column existed file_ids remains the sole authorization boundary; folder_path is a pure narrowing filter, so staleness can only affect filter precision.
|
Warning Review limit reached
More reviews will be available in 23 minutes and 44 seconds. Learn how PR review limits work. Your organization has run out of usage credits. Purchase more in the billing tab. ⌛ How to resolve this issue?After more reviews become available, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available. Please see our Fair Usage Limits Policy for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: Organization UI Review profile: ASSERTIVE Plan: Pro Run ID: ⛔ Files ignored due to path filters (3)
📒 Files selected for processing (24)
✨ Finishing Touches🧪 Generate unit tests (beta)
Warning Billing warning: we have not been able to collect payment for this subscription for more than 72 hours. Please update the payment method or pay any pending invoices in Billing to avoid service interruption. 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. Comment |
Closes #662
What / why
Adds an optional folder filter to RAG search so retrieval can be scoped to one Document Hub folder and all of its subfolders — "search only in Contracts/2024". The issue's original sketch (
team_idscoping,(team_id, folder_path)index) is stale: search is scoped byorg_slug+ a requiredfile_idslist today, andfile_idsstays the sole authorization boundary —folder_pathis a pure narrowing filter, so staleness can only affect filter precision, never access.RAG service (
services/rag)20260611000001_add_documents_folder_path.sql:documents.folder_path TEXT+ partial(org_slug, folder_path)index._build_scope_clausenow collects document-level filters into a generic condition list applied through the single org-scoped documents subquery (both FTS and vector channels). This is the seam Improvement: Enhance RAG retrieval with metadata pre-filtering and optional cross-encoder re-ranking #1517's metadata filters extend —folder_pathis the first generic filter.(folder_path = $p OR left(folder_path, char_length($p) + 1) = $p || '/')—data-roommatchesdata-room/contractsbut never the siblingdata-room-x, with no LIKE-escaping surface.POST /api/v1/searchgains a flat optionalfolder_path(normalized + length-capped via pydantic). Semantic cache is bypassed (lookup and store) for folder-filtered queries — the cache key has no scope dimension.folder_pathparsed from the existing uploadmetadataform field, written by_insert_processing_row(insert + conflict-update; the_do_store/_do_cloneupserts never touch the column, so dedup/skip paths can't clobber it).PATCH /api/v1/documents/folder-paths(org-scoped, max 200, singleUPDATE … FROM unnest(...)) so folder moves stay fresh without re-extraction/re-embedding.Platform (
services/platform)uploadDocument(the chokepoint every RAG upload funnels through) resolvesfolderPathfrom the linked Hub document viafileMetadata.documentIdand mergesfolder_pathinto the upload metadata;uploadDocumentToRag/reindexDocumentInRagpassdocument.folderPathexplicitly (belt-and-suspenders vs. linkage races). Chat uploads carry no folder.syncRagFolderPathsinternal action (warn-and-skip on failure) + freshness hooks:updateDocumentInternalon folder change of an indexed doc (skipped when a re-index is scheduled), WebDAVmoveResourcedocument branch, and the folder-subtree fixup (one batch per move).rag_searchagent tool gains an optionalfolderPatharg (hierarchical/recursive,/separators, no leading slash — description contrasts withdocument_find's fuzzy non-recursive filter).migrations/backfill_rag_folder_path.ts(bunx convex run migrations/backfill_rag_folder_path:backfillRagFolderPath) sweeps already-indexed Hub documents through the PATCH endpoint — run once after deploy so pre-existing documents match folder filters.lib/helpers/rag_folder_path.ts, kept in sync withapp/utils/folder_path.py.Decision-gate recommendations baked in (veto at review)
folder_pathonQueryRequestnow; Improvement: Enhance RAG retrieval with metadata pre-filtering and optional cross-encoder re-ranking #1517 layers itsfiltersobject on the same_build_scope_clauseseam.folderPathon therag_searchtool only;query_rag_contextauto-injection deferred to Improvement: Enhance RAG retrieval with metadata pre-filtering and optional cross-encoder re-ranking #1517 (no current caller needs it)./generate: does NOT acceptfolder_path(issue covers/searchonly).file_idscache-scope gap is unchanged and worth a follow-up issue.Verification
Performed:
cd services/rag && uv run pytest— 346 passed (new: scope-clause folder tests incl. param offsets + boundary expression, cache-bypass both ways, BM25-fallback threading, org-isolation folder test, normalization/model validation,_insert_processing_row, PATCH handler incl. dedupe + 500 path).uv run ruff check app+ruff format --check app— clean.rag_folder_path,upload_document(Hub-doc resolution, explicit override, no-doc/no-path omission),update_document_internal(folder-move sync schedule/skip matrix) — all green.bun run checkat repo root — 40/40 tasks green (format, lint, typecheck, all tests incl. docs locale parity).convex/_generated/api.d.tsupdated for the new migrations module (codegen needs a live deployment; entry added in the generated-file format).Manual verification required (needs a running stack):
POST /api/v1/searchwith/withoutfolder_path, verifying the boundary case (data-roommust not matchdata-room-x).folderPath.Pre-PR checklist
bun run check(format, lint, typecheck, all tests).services/platform/messages/{en,de,fr}.json— N/A (no UI strings; agent tool descriptions are not i18n'd)./docs/{en,de,fr}/— RAG bullet inplatform/agents/tools.mdmentions folder-scoped retrieval in all three locales.bun run --filter @tale/docs lintandbun run --filter @tale/docs test.Note for #1517: branch
feat/rag-folder-scoped-searchis the base for the metadata pre-filter work —_build_scope_clause's condition list and the upload-metadata path are the shared seams.