Skip to content

fix(knowledge): apply the source and date filters inside a resolved scope's ranking - #8059

Merged
waleedlatif1 merged 4 commits into
stagingfrom
improvement/search-filters-on-row
Sep 20, 2026
Merged

waleedlatif1 merged 4 commits into
stagingfrom
improvement/search-filters-on-row

Conversation

@waleedlatif1

Copy link
Copy Markdown
Collaborator

Summary

  • The Search page's source and updated filters were not applied inside the ranking on the resolved-scope path — only at hydration — so a filtered search ranked candidates the filter then refused. On a production-shaped copy, updated: past week returned 0 rows although thousands of documents qualified, a small-source filter returned 4, and every filtered search's keyword leg ran to its deadline (this predates improvement(knowledge): resolve connector and member state once per search #8040)
  • A source filter now confines the access plan itself: every on-row predicate, the reach, and the sources the legs walk or rank are that kind of source alone; upload keeps only source-less documents
  • A date filter enumerates the documents it admits off a new (knowledge_base_id, source_modified_at) partial index and ranks them exactly while the planner estimates the window within the probe's limit; a wider window is walked with the date tested through the document, on the row and on the ranked keyword row
  • A date-bounded set is ranked exactly even when a member source has its own index, since a walk cannot see the date; the reach is keyed and counted by the plan's sources
  • A keyword leg whose deadline passes before its ranking is resolved is short rather than failed

Measurements

Same copy, same reader (reaches ~63% of the index), complete results; wall time over a ~80 ms round trip:

filter share of the index it admits before after
source (large) ~95% 20 rows, 2.9–5.1 s 20 rows, 2.1–2.7 s
source (small) ~0.2% 4–20 rows, ~8 s (keyword timed out) 20 rows, 3.0–3.6 s
updated: past month ~12% 20 rows, ~8 s (keyword timed out) 20 rows, 2.3–4.5 s
updated: past week ~0.4% 0 rows, ~8 s (keyword timed out) 20 rows, 2.8–4.0 s

Server side the new paths are hundreds of milliseconds: past week probes off the date index (~300 ms) then ranks the bounded set exactly (~200 ms); past month is one on-row walk at 340–450 ms. Without the index the date probe was a 320–640 ms sequential scan.

Type of Change

  • Bug fix

Testing

  • Unit tests for the plan restriction, the uploads flag in both predicates, the reach keyed by sources, the date probe (filter-first, estimate-gated), the on-row date test in walks and the Tin page, exact ranking of a date-bounded set, and the keyword-leg deadline; each verified to fail with its fix reverted
  • Real-Postgres predicate equivalence test extended with source- and upload-restricted plans
  • bun run check:migrations origin/staging passes; lint, block registry, check:audits, docs manifest pass

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • Tests added/updated and passing
  • No new warnings introduced
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

@vercel

vercel Bot commented Sep 20, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated
docs Skipped Skipped Sep 20, 2026 6:23pm UTC

Request Review

@greptile-apps

greptile-apps Bot commented Sep 20, 2026

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

The PR appears safe to merge, with the previously reported source-filter ranking issue fixed and no new actionable defects identified.

Summary

This PR applies source and modification-date filters during resolved-scope candidate planning and ranking rather than only during result hydration.

  • Restricts access plans, candidate predicates, reach calculations, and ranking paths to the selected source type.
  • Enumerates sufficiently small filtered document sets for exact vector and keyword ranking.
  • Falls back to filter-aware walks when the bounded planning estimate exceeds its deadline.
  • Adds a concurrent partial index for knowledge-base and source-modification lookups.
  • Adds coverage for source confinement, uploads, date filtering, exact ranking, reach caching, and planning timeouts.
Diagram
%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Resolved search scope] --> B[Resolve connector access plan]
  B --> C{Source filter?}
  C -- Yes --> D[Restrict plan to matching connector type or uploads]
  C -- No --> E[Keep resolved plan]
  D --> F{Source or date filter?}
  E --> F
  F -- No --> G[Resolve normal reach]
  F -- Yes --> H[Estimate filtered document count]
  H --> I{Small enough to enumerate?}
  I -- Yes --> J[Probe permitted documents]
  J --> K[Rank bounded set exactly]
  I -- No or estimate timeout --> L[Use filter-aware ranking walk]
  G --> M[Vector and keyword retrieval]
  K --> M
  L --> M
Loading

Reviews (5) · Last reviewed commit: "fix(knowledge): estimate a filter's size..."

Comment thread apps/sim/lib/knowledge/search/queries.ts

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 10 files

Tip: cubic can generate docs of your entire codebase and keep them up to date. Try it here.

Fix all with cubic | Re-trigger cubic

Comment thread apps/sim/lib/knowledge/search/queries.ts Outdated
Comment thread apps/sim/lib/knowledge/search/queries.ts
Comment thread packages/db/schema.ts
@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cubic-dev-ai review this PR

@cubic-dev-ai

cubic-dev-ai Bot commented Sep 20, 2026

Copy link
Copy Markdown
Contributor

@cubic-dev-ai review this PR

@waleedlatif1 I have started the AI code review. It will take a few minutes to complete.

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cubic-dev-ai review this PR

@cubic-dev-ai

cubic-dev-ai Bot commented Sep 20, 2026

Copy link
Copy Markdown
Contributor

@cubic-dev-ai review this PR

@waleedlatif1 I have started the AI code review. It will take a few minutes to complete.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 10 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

Tip: cubic can generate docs of your entire codebase and keep them up to date. Try it here.

Re-trigger cubic

…cope's ranking

- a source filter confines the access plan itself, so every on-row predicate, the reach, and the sources the legs walk or rank are that kind of source alone; `upload` keeps only source-less documents
- a date filter enumerates the documents it admits off a new `(knowledge_base_id, source_modified_at)` index and ranks them exactly while the planner estimates the window within the probe's limit; a wider window is walked with the date tested through the document, on the row and on the ranked keyword row
- a date-bounded set is ranked exactly even when a member source has its own index, since a walk cannot see the date
- the reach is keyed and counted by the plan's sources
- a keyword leg whose deadline passes before its ranking is resolved is short rather than failed
@waleedlatif1
waleedlatif1 force-pushed the improvement/search-filters-on-row branch from 318f2eb to 1b59f04 Compare September 20, 2026 18:13
@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cubic-dev-ai review this PR

@cubic-dev-ai

cubic-dev-ai Bot commented Sep 20, 2026

Copy link
Copy Markdown
Contributor

@cubic-dev-ai review this PR

@waleedlatif1 I have started the AI code review. It will take a few minutes to complete.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 10 files

Tip: cubic can generate docs of your entire codebase and keep them up to date. Try it here.

Fix all with cubic | Re-trigger cubic

Comment thread apps/sim/lib/knowledge/search/queries.ts Outdated
@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cubic-dev-ai review this PR

@cubic-dev-ai

cubic-dev-ai Bot commented Sep 20, 2026

Copy link
Copy Markdown
Contributor

@cubic-dev-ai review this PR

@waleedlatif1 I have started the AI code review. It will take a few minutes to complete.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 10 files

Tip: cubic can generate docs of your entire codebase and keep them up to date. Try it here.

Fix all with cubic | Re-trigger cubic

Comment thread apps/sim/lib/knowledge/search/queries.ts
@waleedlatif1
waleedlatif1 merged commit 7093409 into staging Sep 20, 2026
34 checks passed
@waleedlatif1
waleedlatif1 deleted the improvement/search-filters-on-row branch September 20, 2026 18:31

This branch was previously deployed

1 inactive deployment
Preview 8ee7f85d Deployed Sep 20, 2026 by vercel[bot]
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