Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
1e3bd96
wip(knowledge-turso): approximate vector index for large single-organ…
claude Nov 27, 2026
931665e
test(knowledge-turso): pins for the approximate vector index path
claude Nov 27, 2026
84f8293
docs(knowledge-turso): the approximate index in the README and the kn…
claude Nov 27, 2026
d7a66e7
chore(knowledge-turso): changeset for the approximate vector index
claude Nov 27, 2026
f0a28fd
Merge remote-tracking branch 'origin/main' into claude/area-o1C-37975
claude Nov 27, 2026
09b0571
docs(knowledge-turso): name the recall of the index settings on unstr…
claude Nov 27, 2026
ceb6c4c
feat(knowledge-turso): rank the candidates by one-bit vectors instead…
claude Nov 27, 2026
19841ef
docs(knowledge-turso): the measured latency and recall of the one-bit…
claude Nov 27, 2026
4dbdd5b
fix(knowledge-turso): a thin in-scope pool and a refused one-bit DDL …
claude Nov 27, 2026
d256d2d
test(knowledge-turso): read the optional record id of the scoped fixt…
claude Nov 27, 2026
212c147
test(knowledge-turso): a host without one-bit vectors accepts the DDL…
claude Nov 28, 2026
0fc18f0
feat(knowledge-turso): the health check names a refused approximate v…
claude Nov 28, 2026
d3772d1
fix(knowledge-turso): the approximate vector leg needs 8 x topK candi…
claude Nov 28, 2026
c77819e
Merge remote-tracking branch 'origin/main' into claude/area-o1C-37975
claude Nov 28, 2026
f5d171e
Merge remote-tracking branch 'origin/main' into claude/area-o1C-37975
claude Nov 28, 2026
98ffbe4
fix(knowledge-turso): the index memory is now keyed per generation in…
claude Nov 28, 2026
b113f08
test(knowledge-turso): an adapter that did not build the source reads…
claude Nov 28, 2026
50e245a
Merge remote-tracking branch 'origin/main' into claude/area-o1C-37975
claude Nov 28, 2026
73d2774
test(knowledge-turso): the two 2,000-passage recall cases get a 30 s …
claude Nov 28, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .changeset/knowledge-turso-approximate-index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
'@objectstack/knowledge-turso': patch
---

feat(knowledge-turso): an approximate vector index for a large source of one organization

Clause-②: yes (widening)
Metadata-protocol: none

Every search of a libSQL store read every vector in scope: at 1,536 dimensions about 1.2 GB at 200,000 passages. A complete build that completes on a generation of `vectorIndexThreshold` passages or more (default 20,000), all of one organization, now makes that generation's approximate index: each passage's vector quantized to one bit a dimension, in a side table a 32nd the size of the vectors. The vector leg then takes the `topK × vectorIndexOverfetch` passages nearest by that index (default 40 a hit), runs the same organization, record-scope and metadata `WHERE` over them, and ranks those in scope by their stored float32 vector; its ranks feed the reciprocal rank fusion with the keyword leg as the exact scan's did. A leg the filter leaves with fewer than 8 × `topK` candidates in scope, or short of `topK` hits, runs again as the exact scan, so a narrow record scope or filter keeps its recall and a search never answers fewer hits than before. Measured at 200,000 passages of 1,536 dimensions: a search whose keyword leg matches nothing took 81 ms at p95 instead of 1,054 ms, with a recall@10 against the exact scan of 0.998 over 100 queries.

- **Never over two organizations.** The candidates are ranked before the filter, so a generation holding passages of more than one organization (passages of none count as one) gets no index, and each search reads the organization index and takes the exact scan once a second organization's passage is written.
- **Built at the end of a complete build, never on a search.** `os knowledge rebuild`, a fingerprint change, the boot build and the daily reconcile all end in one. The side table is made empty, then filled 2,048 passages to a transaction before the generation is switched to (200,000 passages: 3 s, 54 MB). A source that grows past the threshold between builds keeps the exact scan until its next complete build. After that, each write of a passage also writes its one-bit vector. A database that refuses the one-bit vectors or the index's DDL gets no index, the build completes on the exact scan, and the adapter's health check names the source and the reason. A process reads the catalog for a generation's index once (two reads) and remembers the answer; at `vectorIndexThreshold: Infinity` a search never reads the catalog for an index.
- **Names.** `knowledge_<source>_g<n>-vec` (the side table), `…-org` (the organization index) and three `…-vec-*` triggers, dropped with the generation by a switch and by `dropSource`. An object under one of those names that the store did not make is never dropped: the call is refused `SERVICE_UNAVAILABLE`, naming it.
- **Two new options**, on `KnowledgeTursoPlugin` and `TursoKnowledgeAdapter`: `vectorIndexThreshold` (`Infinity` never makes an index) and `vectorIndexOverfetch` (a whole number, 8 or more). Nothing to migrate: a store keeps the exact scan until its next complete build.
2 changes: 1 addition & 1 deletion content/docs/ai/knowledge-rag.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ Details — the tables, an approximate index as an operator's step, rebuilds —
new KnowledgeTursoPlugin(), // or { url: 'file:/var/lib/app/knowledge.db' }, ':memory:', a libsql:// URL
```

- It searches with two legs merged by reciprocal rank fusion: an exact cosine scan, and a keyword match in an FTS5 trigram index beside each passage table, with `LIKE` for a term under three characters (「出差」). The organization filter, the record scope and the metadata `filter` run in SQL in both legs, before ranking. 10,000 passages of 1,536 dimensions take about 82 MB and a search about 50 ms; larger sources belong on pgvector.
- It searches with two legs merged by reciprocal rank fusion: a cosine ranking, and a keyword match in an FTS5 trigram index beside each passage table, with `LIKE` for a term under three characters (「出差」). The organization filter, the record scope and the metadata `filter` run in SQL in both legs. The cosine ranking is an exact scan — 10,000 passages of 1,536 dimensions take about 82 MB and a search about 50 ms — until a complete build finds a source of 20,000 passages or more (`vectorIndexThreshold`), all of one organization: that source gets an approximate index of one-bit vectors, whose nearest candidates are filtered and re-ranked by the stored vector, never over two organizations. At 200,000 passages a vector search took 81 ms at p95 instead of about a second, with a recall@10 of 0.998 against the exact scan. Larger sources shared by many organizations belong on pgvector.
- A changed model or split is rebuilt beside the old index, which keeps answering until the reindex completes.
- Where libSQL has no native build (Windows on ARM), boot goes on and its sources answer `SERVICE_UNAVAILABLE`.

Expand Down
39 changes: 30 additions & 9 deletions content/docs/protocol/knowledge.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,8 @@ an exact scan.
libSQL's native vectors (`F32_BLOB`, `vector32`, `vector_distance_cos`): the
store for local development and for a deployment not on PostgreSQL. It takes
the service's passages, embeds them with the kernel's `embedder` service (or
the `embedder` it is given), and answers a search by an exact scan.
the `embedder` it is given), and answers a search by an exact scan, or, for a
large source of one organization, through an approximate index.

- **Which database.** The `url` option (`file:`, `:memory:` or a
`libsql://` Turso URL), else `knowledge.db` under the project's
Expand All @@ -757,16 +758,36 @@ the `embedder` it is given), and answers a search by an exact scan.
(`<source>` is the source's index key). A key that is not snake_case is
refused before any SQL.
- **Search.** Two legs merged by reciprocal rank fusion (`k` = 60, each leg
at most `topK`) before the service's re-check: an exact scan by cosine
distance, and a keyword leg in a contentless FTS5 trigram index beside each
at most `topK`) before the service's re-check: a vector leg by cosine
distance (an exact scan, or the approximate index below), and a keyword leg in a contentless FTS5 trigram index beside each
passage table (kept in step by triggers), a term of three characters or
more as a quoted phrase, a shorter one by `LIKE`. The organization filter,
the [record scope](#record-scope) and the metadata `filter` (JSON equality
per key) are one `WHERE`, run in both legs before either ranks. The store creates no DiskANN index: `vector_top_k` ranks
before it filters, and with one, 1,000 vectors of 1,536 dimensions grew a
file from 8.2 MB to 329 MB and took 21.8 s to index. Without it the same
1,000 passages take 8.3 MB and a search about 5 ms; 10,000 take 82 MB and
about 50 ms. A larger source belongs on `knowledge-pgvector`.
per key) are one `WHERE`, run in both legs. Without the approximate index
the vector leg reads every row in scope: 1,000 passages of 1,536 dimensions take 8.3 MB and a search about
5 ms; 10,000 take 82 MB and about 50 ms.
- **Approximate index.** A complete build that completes on a generation of
`vectorIndexThreshold` passages or more (default 20,000), all of one
organization, makes its approximate index: each passage's vector quantized
to one bit a dimension, in a side table kept in step by triggers, copied in
2,048 rows to a transaction before the switch, never on a search (200,000
passages: 3 s, 54 MB). The vector leg then takes the
`topK × vectorIndexOverfetch` passages nearest by that index (default 40 a
hit), runs the same `WHERE` over them, ranks those in scope by their stored
vector, and feeds the fusion as the exact leg does; a leg the filter leaves
with fewer than 8 × `topK` candidates in scope, or short of `topK` hits,
runs again as the exact scan, so a narrow scope keeps its recall and a
search never answers fewer hits than the exact scan. A database that
refuses the one-bit vectors or the index's DDL gets no index, its builds
complete, and the adapter's health check names the source and the reason. The candidates are ranked before the
filter, so the index is never made, or used, over the passages of two
organizations (passages of none count as one): each search reads the
organization index and takes the exact scan once a second organization's
passage is written. At 200,000 passages of 1,536
dimensions a search whose keyword leg matched nothing took 81 ms at p95
(the exact scan 1,054 ms), with a recall@10 against the exact scan of 0.998. libSQL's own DiskANN index (`vector_top_k`)
is not used: on the same 200,000 passages it took 24 minutes and 2.2 GB to
build and answered a recall@10 of 0.39.
- **Rebuilds.** A changed fingerprint is refused (§3.6). A complete reindex
under a new fingerprint builds a new generation table beside the active
one, which keeps answering its own fingerprint, and switches at the end of
Expand Down Expand Up @@ -1314,7 +1335,7 @@ followed (§4.1, §4.2) are the adapters to run in production.
| `knowledge-llamaindex` | ⏳ Phase 2 | |
| `knowledge-dify` | ⏳ Phase 3 / community | |
| `knowledge-pgvector` | ✅ | PostgreSQL + pgvector; exact scan and a `tsvector` keyword leg merged by RRF, organization, record scope and metadata filter in SQL, index fingerprint (§4.1) |
| `knowledge-turso` | ✅ | A libSQL file of its own; exact scan and an FTS5 trigram keyword leg merged by RRF, organization, record scope and metadata filter in SQL, a generation built beside the active one (§4.2) |
| `knowledge-turso` | ✅ | A libSQL file of its own; a vector leg (an exact scan, or a one-bit approximate index for a large source of one organization) and an FTS5 trigram keyword leg merged by RRF, organization, record scope and metadata filter in SQL, a generation built beside the active one (§4.2) |

### 7.1 Non-goals for Phase 1

Expand Down
Loading
Loading