Skip to content

fix(knowledge): walk a large bounded set on the row before ranking it exactly - #8106

Merged
waleedlatif1 merged 5 commits into
stagingfrom
fix/source-filter-reach
Sep 21, 2026
Merged

waleedlatif1 merged 5 commits into
stagingfrom
fix/source-filter-reach

Conversation

@waleedlatif1

Copy link
Copy Markdown
Collaborator

Summary

  • A member who can read most of a large source, searching with that source selected as a filter, resolved a bounded permitted set of tens of thousands of documents and then ranked every chunk of it exactly on both legs. The vector leg read every chunk's projected vector; the keyword leg materialized every chunk of the set before matching the term, so its cost followed the set's size rather than the term's matches. On a cold cache each leg outran its budget and the search returned nothing
  • A bounded set past PERMITTED_EXACT_DOCUMENT_LIMIT (5,000 documents) is now walked on the row first: the plan's source and ACL decide readability on the rows the walk visits and the walk stops at its tuple cap. If the walk cannot fill its pool the exact ranking that was always complete takes over, so recall is never below today's and the usual cost is the walk's. Measured on a cold cache, the walk restricted to the source costs milliseconds where the exact ranking costs over a second
  • The keyword leg treats the same set as a narrow on-row reader: Tin windows where Tin serves, otherwise the unfenced GIN shape whose cost follows what the term matches. Deployments without Tin keep GIN throughout
  • Sets under the limit, unbounded readers and broad readers keep their existing paths unchanged; the on-row walk is hoisted into one helper and used by both callers
  • The size limit sits where exact ranking reads about as many rows as the walk's tuple cap

Type of Change

  • Bug fix

Testing

  • Six new tests: a large bounded set walks on the row and sends no identifiers, an underfilled walk falls back to the exact ranking with the set, a set under the limit ranks exactly without a walk; the keyword leg ranks the large set with Tin as a narrow reader, falls back to an unfenced GIN read, and keeps the fenced read under the limit. Each was verified to fail with its half of the fix reverted
  • vitest over lib/knowledge, app/api/knowledge, app/api/v1/knowledge, app/api/v2/knowledge, lib/copilot/tools/server/knowledge: 218 files, 3548 tests passing
  • bun run lint, bun run check:audits, docs-manifest:check and native tsc --noEmit 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)

… exactly

A member reading most of a large source, with that source selected as a filter, enumerated a
bounded permitted set of tens of thousands of documents and then ranked every chunk of it
exactly on both legs: the vector leg read every chunk's projected vector, and the keyword leg
materialized every chunk of the set before it matched the term. Cold, each leg outran its budget
and the search returned nothing.

A bounded set past a size limit is now walked on the row first, where the plan's source and ACL
decide readability and the walk stops at its tuple cap, and ranked exactly only when the walk
cannot fill its pool, so recall is never below the exact ranking's. The keyword leg treats the
same set as a narrow on-row reader: Tin windows where Tin serves, otherwise the GIN shape whose
cost follows the term's matches. Sets under the limit keep their exact paths.
@vercel

vercel Bot commented Sep 21, 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 21, 2026 5:29pm UTC

Request Review

@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 2 files

Reply with feedback, questions, or to request a fix.

Fix all with cubic | Re-trigger cubic

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

greptile-apps Bot commented Sep 21, 2026

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

The PR appears safe to merge; the previously reported vector refill and Tin-to-GIN offset issues are fixed, and no new actionable defects remain.

Summary

This PR optimizes searches over large bounded permitted-document sets while preserving exact-ranking fallbacks.

  • Vector search first performs a bounded on-row graph walk, then falls back to exact ranking when the walk or subsequent hydration cannot supply enough authorized candidates.
  • Exact refills exclude candidates already read so each refill supplies a fresh ranking window.
  • Keyword search treats large bounded sets as on-row readers and uses Tin when available, with a first-page GIN fallback that avoids carrying offsets between incompatible rankers.
  • Tests cover the optimized paths, exact fallbacks, refill behavior, size threshold, and later-page Tin behavior.
Diagram
%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[Search over permitted documents] --> B{Bounded set at least 5,000 documents?}
    B -->|No| C[Use existing exact or bounded path]
    B -->|Yes, vector| D[Walk graph with on-row visibility]
    D --> E{Pool filled and hydration sufficient?}
    E -->|Yes| F[Return hydrated results]
    E -->|No| G[Exact-rank permitted set]
    G --> H[Exclude previously read chunks on refill]
    H --> F
    B -->|Yes, keyword| I{Tin available?}
    I -->|Yes| J[Rank through Tin windows]
    J --> K{First page still short?}
    K -->|Yes| L[Restart with unfenced GIN ranking]
    K -->|No or later page| M[Return Tin page]
    I -->|No| L
Loading

Reviews (5) · Last reviewed commit: "fix(knowledge): hand only a large bounde..."

Comment thread apps/sim/lib/knowledge/search/queries.ts Outdated
…ing once hydration runs it short

The walk decides readability on the projection row, which is broader than the document predicate hydration applies, so a pool the walk filled can still run short of readable rows. The refill for a large bounded set is now the exact ranking, complete over the set, placed behind the rows already read so the pages keep their offsets.
@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 21, 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.

Comment thread apps/sim/lib/knowledge/search/queries.ts
…dy read

The refill's exact ranking excludes the chunks the pool already holds inside the statement, so every refill is a full window of fresh rows rather than a window thinned by the rows the walk found first.

@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 2 files

Reply with feedback, questions, or to request a fix.

Fix all with cubic | Re-trigger cubic

Comment thread apps/sim/lib/knowledge/search/queries.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 21, 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.

…he GIN ranking

A narrow reader's page is left short by design once the widest window cannot fill it; a large bounded set's read was exhaustive before, so its widest window that still falls short now hands the page to the GIN ranking, which covers every match.
@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 21, 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 2 files

Confidence score: 5/5

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

Re-trigger cubic

Comment thread apps/sim/lib/knowledge/search/queries.ts Outdated
… ranking

Tin and GIN order candidates differently, so an offset advanced through one ranking cannot resume the other. A large bounded set's first page that Tin's widest window cannot fill goes to GIN; a later page stays with Tin and is left short as a narrow reader's is.
@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 21, 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 2 files

Confidence score: 5/5

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

Re-trigger cubic

@waleedlatif1
waleedlatif1 merged commit c9d73bb into staging Sep 21, 2026
25 checks passed
@waleedlatif1
waleedlatif1 deleted the fix/source-filter-reach branch September 21, 2026 17:33

This branch was previously deployed

1 inactive deployment
Preview 4f82d22f Deployed Sep 21, 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