Skip to content

perf: bind the query vector in knowledge base vector search - #4042

Merged
themez merged 1 commit into
chatboxai:mainfrom
b84330808:perf-kb-vector-search-bind-query
Nov 12, 2026
Merged

themez merged 1 commit into
chatboxai:mainfrom
b84330808:perf-kb-vector-search-bind-query

Conversation

@b84330808

Copy link
Copy Markdown
Contributor

Description

Knowledge base search can freeze the app (#3989). The cause is LibSQLVector.query() in @mastra/libsql 0.13.2, which Chatbox pins through overrides. It puts the query vector into the SQL text as a string literal, vector_distance_cos(embedding, '[0.1,0.2,...]'), so libSQL parses the whole vector again for every row it scans. The search runs in the main process and libSQL runs synchronously, so the window stops responding until the query finishes.

This adds a pnpm patch for @mastra/[email protected] that binds the vector as a parameter, vector_distance_cos(embedding, vector32(?)). It's the same change upstream made in mastra-ai/mastra#27895, released in @mastra/libsql 1.27.0. Moving to 1.x would also mean a major @mastra/core upgrade, so patching the pinned version seemed like the smaller step. Knowledge base search and session attachment search both use this method.

Results don't change: same ids and same scores, with and without a metadata filter.

Additional Notes

Measured on Windows 11, Node 24.15, @libsql/client 0.15.15, calling query({ topK: 20 }) the way searchKnowledgeBase does, 7 alternating rounds each:

Table Before (median, min to max) After
2,000 chunks x 1536 dims, made with createIndex 2,159 ms (2,116 to 2,383) 97 ms (95 to 145)
2,000 chunks x 4096 dims 5,316 ms (5,259 to 5,533) 219 ms (208 to 229)

While the 4096-dim query ran, the event loop didn't get a turn at all (one 5,132 ms gap), which is the freeze users see.

pnpm install --frozen-lockfile accepts the updated lockfile, and pnpm applies the patch to both dist/index.js and dist/index.cjs.

The DiskANN index mentioned in the issue still isn't used by query(); that would be a separate change.

Fixes #3989

Screenshots

N/A

Contributor Agreement

By submitting this Pull Request, I confirm that I have read and agree to the following terms:

  • I agree to contribute all code submitted in this PR to the open-source community edition licensed under GPLv3 and the proprietary official edition without compensation.
  • I grant the official edition development team the rights to freely use, modify, and distribute this code, including for commercial purposes.
  • I confirm that this code is my original work, or I have obtained the appropriate authorization from the copyright holder to submit this code under these terms.
  • I understand that the submitted code will be publicly released under the GPLv3 license, and may also be used in the proprietary official edition.

Please check the box below to confirm:

[x] I have read and agree with the above statement.

@mastra/libsql 0.13.2 (pinned by the overrides) inlines the query vector
into the SQL text of LibSQLVector.query(), so libSQL parses the whole
vector literal again for every row it scans. Knowledge base and session
attachment searches run this in the main process, which blocks the app
for seconds on a medium knowledge base and for over a minute on large
ones (chatboxai#3989).

Patch it to pass the vector as a bound vector32(?) parameter, the same
change upstream made in mastra-ai/mastra#27895 (@mastra/libsql 1.27.0).
Results are unchanged.
@coderabbitai

coderabbitai Bot commented Nov 11, 2026

Copy link
Copy Markdown
Contributor

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: ec14e1cf-2d99-4655-838b-181382686056

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • 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

@themez
themez merged commit 704d4af into chatboxai:main Nov 12, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants