@qvac/rag
v0.12.0
Published
Embedded ingestion and retrieval for QVAC apps. `@qvac/rag` stores document chunks and their embeddings in HyperDB and answers scoped vector searches over them. It runs on Bare and Node, and does not depend on `@qvac/sdk` or `@qvac/inference`.
Readme
@qvac/rag
Embedded ingestion and retrieval for QVAC apps. @qvac/rag stores document chunks and their embeddings in HyperDB and answers scoped vector searches over them. It runs on Bare and Node, and does not depend on @qvac/sdk or @qvac/inference.
It ingests text, pre-chunked text or chunks with precomputed embeddings. Text is chunked with rag-extract/chunk and embedded through an embeddings port you supply. Search uses a TurboVec index per namespace when @qvac/turbovec-ggml is installed, or an index the host supplies, and an exact scan of HyperDB otherwise.
Installation
npm install @qvac/rag corestoreOn runtimes without Node-style crypto (browser, React Native), install a polyfill such as crypto-browserify that provides createHash and either randomBytes or getRandomValues. Randomness is needed to create a namespace and on the first write to a namespace created by 0.10.0 or earlier.
For indexed search on Bare, also install the optional peer:
npm install @qvac/turbovec-ggmlTurboVec's binding only loads under Bare. On Node, pass an index through index.provider for indexed search.
Usage
import Corestore from 'corestore'
import { createRag } from '@qvac/rag'
const rag = createRag({
corestore: new Corestore('./rag-storage'),
index: { dir: './rag-index' }
})
await rag.ready()
const { spaceId } = await rag.createNamespace({
name: 'handbook',
space: {
model: 'embeddinggemma-300m',
revision: 'q8_0',
dimensions: 768,
metric: 'cosine',
normalized: true,
documentEncoding: 'title: none | text: ',
queryEncoding: 'task: search result | query: ',
preprocessing: 'none'
}
})
await rag.ingest({
namespace: 'handbook',
documents: [
{
id: 'leave-policy',
metadata: { title: 'Leave policy' },
chunks: [{ content: 'Employees get 25 days…', embedding: vector, spaceId }]
}
]
})
const hits = await rag.search({
scope: { namespace: 'handbook', documentIds: ['leave-policy'] },
vector: queryVector,
spaceId,
k: 5
})
await rag.close()With an embeddings port, ingest and search take text instead of vectors:
const rag = createRag({ corestore, embeddings: port }) // port.space is the namespace's space
await rag.ingest(
{ namespace: 'handbook', documents: [{ id: 'leave-policy', text: policyText }] },
{ signal, onProgress: ({ stage, done, total }) => console.log(stage, done, total) }
)
const hits = await rag.search({
scope: { namespace: 'handbook' },
query: 'How many leave days?',
k: 5
})See examples/quickstart.ts for a runnable version.
API
createRag({ corestore, embeddings?, chunking?, index?, onIndexFallback?, onSnapshotError? })
Creates a RAG instance on a caller-owned Corestore. embeddings is an EmbeddingsPort, needed only for text and pre-chunked ingest and for text query search:
interface EmbeddingsPort {
space: EmbeddingSpace
// One row per text, row-major, each with `space.dimensions` values.
embedDocuments(texts: string[], options?: { signal?: AbortSignal }): Promise<Float32Array>
embedQuery(text: string, options?: { signal?: AbortSignal }): Promise<Float32Array>
}The port must be in the namespace's space: computeSpaceId(port.space) has to equal the namespace's spaceId, or text ingest and text search fail with EMBEDDING_SPACE_MISMATCH before anything is embedded. Without a port they fail with DEPENDENCY_REQUIRED. A port that throws surfaces as EMBEDDING_FAILED, and one that returns the wrong number of values as EMBEDDING_DIMENSION_MISMATCH.
chunking is { chunkSize?, chunkOverlap? } in tokens, passed to rag-extract/chunk for text documents (defaults 256 and 50). chunkOverlap must be smaller than chunkSize.
index configures the vector index, used only when @qvac/turbovec-ggml loads or provider is given:
dir: directory for index snapshots. Without it, each namespace's index is built from HyperDB the first time it is searched afterready(), and kept in memory only. Snapshots go under<dir>/<core key>/; files there that follow the snapshot naming but aren't the namespace's current snapshot, such as the temp file of a killed write, are deleted when the namespace's index opens, after each snapshot, and bydropNamespace. Other files are left alone.snapshotEvery: committed batches between snapshots, default 256. A snapshot is also written onclose().provider: an index implementation to use instead of@qvac/turbovec-ggml; see Index providers.fs: the file system snapshots go through. Defaults tobare-fson Bare andfson Node. It needsmkdir,readFile,writeFile,renameandunlink; withoutreaddir, stale snapshot files are not cleaned up.
ready() loads @qvac/turbovec-ggml and the file system, unless provider and fs replace them, before it resolves, so a host that blocks module loading after startup keeps its index and snapshots. A snapshot that can't be read or written leaves the index in memory, and the next snapshot tries again.
onIndexFallback(event) is called when a namespace's searches switch to the scan, once per namespace and reason, with { namespace, reason, error? }. The reasons are the ones indexStatus reports. An error it throws, or a promise it returns that rejects, is ignored.
onSnapshotError(event) is called with { namespace, error } when a namespace's snapshot can't be read or written, including the write on close(). Searches still use the index, so this is not a fallback, but each reopen rebuilds it from HyperDB until a snapshot is written. It is called once per run of failures; a snapshot written successfully re-arms it. A RAG that opens on a directory whose snapshots are already failing is told during ready(). indexStatus reports the same error as snapshot: { error }. Errors it throws or rejects with are ignored, as for onIndexFallback.
The index is derived from HyperDB and never trusted on its own. On open it loads the snapshot if it matches the namespace's revision, replays the batches committed since if it is behind, and rebuilds from the stored chunks otherwise. Deleting the snapshot directory is always safe. close() waits for calls already in progress, then closes the RAG's HyperDB but leaves the Corestore open; calls made after it fail with NOT_READY. If the host closes the Corestore first, writes fail with DB_OPERATION_FAILED and close() still resolves, skipping the mutation pruning it can no longer write. Call ready() before any other method.
createNamespace({ name, space, index?, vectorEncoding? })
Creates a namespace pinned to one embedding space and returns { name, spaceId, revision, index: { storage }, vectorEncoding }. spaceId is a hash of every field of space, so two spaces with the same dimensions but a different model, encoding or metric never match. Calling it again with the same space returns the existing namespace; a different space fails with NAMESPACE_EXISTS.
documentEncoding, queryEncoding and preprocessing only identify the space; @qvac/rag never applies them. Add the document and query prefixes yourself before embedding, or in your embeddings port. Nothing checks that you did, and skipping them only makes results worse.
computeSpaceId(space) returns the same ID without touching storage.
index.storage sets how the namespace's index stores vectors and is fixed at creation; asking for a different one later fails with NAMESPACE_EXISTS.
| Storage | Dimensions | Notes |
| ---------------------------- | ----------------------------- | ---------------------------------------------------------------- |
| q8 (default) | any | 8-bit quantized |
| f32 | any | full precision |
| turbovec-q4, turbovec-q2 | divisible by 8, at most 1,024 | TurboVec 4- and 2-bit; embedding components must stay below 1e16 |
vectorEncoding sets how chunk vectors are stored in HyperDB and in exports: f32 (default) or f16. It is fixed at creation like index.storage. f16 halves the vector bytes; for 10,400 chunks × 768 dimensions with 1 KB of text each, the store shrinks from 49.3 MB to 33.0 MB, and with the q8 index snapshot from 57.4 MB to 41.1 MB (see Disk). Search scores come from the stored vectors, so an f16 namespace scores the rounded values: on synthetic 768-dimension sets, scores moved by 2e-6 to 6e-6 on average and at most 3e-5, the top hit never changed, and recall@10 against f32 was 0.9975–1.0, with the near-tied uniform random set at 0.999. Embedding components above 65,504 in magnitude fail with INVALID_INPUT in an f16 namespace.
ingest({ namespace, documents }, { signal?, onProgress? }?)
Each document is one of:
- Text
{ id, text, metadata? }: chunked withrag-extract/chunkusing thechunkingoptions, so chunk IDs are<id>:<index>and each chunk carriesrag-extract'smetadataJson. Text that is empty or only whitespace becomes a document without chunks. - Pre-chunked
{ id, metadata?, chunks: [{ id?, content, metadataJson? }] }: segmentation and chunk IDs are kept exactly. A chunk list fromrag-extractgoes in as-is; itstokenCountis accepted and not stored. - Precomputed
{ id, metadata?, spaceId?, chunks: [{ id?, content, metadataJson?, embedding, spaceId? }] }: needs no port. Each chunk takes its ownspaceIdor the document's, and it must equal the namespace's; the embedding must have the space's dimensions. Arag-extractartifact's chunks go in as-is withspaceIdon the document.
chunks may be empty. A document without chunks is stored, listed and exported with a chunkCount of 0, and search never returns it. Its document spaceId, when given, must equal the namespace's.
Within one document, either every chunk has an embedding or none does. Chunk IDs default to <documentId>:<ordinal>. Omit metadataJson rather than passing an empty string, which is rejected. Text and pre-chunked chunks are sent to the port in batches of 32.
Input limits, all failing with INVALID_INPUT:
Namespace names, document and chunk IDs, text, chunk
contentandmetadataJson,metadatakeys and string values, and thespacefields must be well-formed UTF-16: a lone surrogate, such as half an emoji left by cutting a string, is rejected rather than stored as U+FFFD. The same holds for names and IDs passed to every other call (INVALID_SCOPEin a scope).metadatacan't have a__proto__key.Each stored row must fit in 8 MiB (UTF-8): a chunk's namespace, document ID, chunk ID,
content,metadataJsonanddimensions × 4vector bytes, and a document's namespace, ID andmetadataas JSON. Text documents are checked once chunked.signalmust be anAbortSignal, withaborted,addEventListenerandremoveEventListener.The whole call is validated, including the limits above, and text is chunked, before anything is written.
Each document commits in its own HyperDB batch, in input order, as soon as all its chunks are embedded. If a later document fails, earlier ones stay committed.
signalis checked between embedding batches and before each document's commit. An abort does not throw: committed documents stay, the rest are listed inskipped, andcancelledistrue.onProgress({ stage, done, total })reportschunk(text documents chunked),embed(chunks embedded, once per batch) andcommit(documents written or found unchanged).Ingesting an existing document ID replaces it: the new chunks get a new generation and the old chunks are deleted in the same batch.
Re-ingesting a document whose content is unchanged writes nothing: no new generation and no revision bump, and its text is not sent to the port. Content is the chunk IDs, text and
metadataJsonafter chunking, the precomputed vectors, the documentmetadataand the namespace's space. So newchunkingoptions count as a change, while theindexedAttimestamp inrag-extract's chunk metadata does not. AnindexedAtkey in any other chunk metadata is compared like every other field.In an
f16namespace, vectors are rounded to float16 before they are hashed and indexed, so re-ingesting the same float32 input is stillunchanged.There is no content deduplication. Identical text in two documents is stored and returned twice.
Returns { documents, skipped, cancelled, revision }. Each entry in documents is { id, generation, chunkCount, status }, where status is created, replaced (with removedChunks, the previous generation's chunk count) or unchanged.
deleteDocuments({ namespace, documentIds })
Deletes the documents and their chunks in one batch. Unknown IDs are ignored. Returns { deleted, revision }. A deleted document's last generation is kept, so ingesting the same ID later continues at the next generation: a generation read before the delete never matches the new document.
search({ scope, vector, spaceId, k, perDocument?, minScore?, signal? }) / search({ scope, query, k, perDocument?, minScore?, signal? })
A text query is embedded with the port's embedQuery and searched in the port's space, so it takes no spaceId. Returns up to k hits { namespace, documentId, chunkId, ordinal, generation, chunkCount, content, metadataJson?, documentMetadata?, citation, score }, best first. metadataJson is the chunk's and documentMetadata is the document's metadata from ingest. generation and chunkCount describe the document version the chunk belongs to. spaceId is the space the query was embedded in; a different one fails with EMBEDDING_SPACE_MISMATCH, even when its dimensions match. The score is the dot product, or the cosine similarity for cosine spaces, computed from the stored float32 vectors either way. With the index, candidates come from TurboVec and are rescored exactly; a candidate HyperDB no longer maps to a current chunk is dropped. An aborted signal fails with OPERATION_CANCELLED; the scan checks it between batches, and a search waiting for an index to load or rebuild stops waiting, while the rebuild finishes for later searches. Equal scores rank in scope order for a documentIds list and in document ID order otherwise, with and without the index.
citation holds title, sourceUrl, sourcePath and mimeType from the document's metadata, falling back to the chunk's metadataJson (the rag-extract chunk format carries the same keys), and page from the chunk's metadataJson. A key is present only when it holds a non-empty string, or for page an integer of at least 1.
perDocument spreads the k hits across documents: each document's best hit is taken first, in score order, then further hits go round the documents again, up to perDocument each. The hits still come back best first. When scope.documentIds lists at most k documents, every listed document that has chunks gets a hit. Without the index the selection is exact. With it, candidates are fetched in a window that grows until hits outside it could no longer take a seat: k fills before any document that may hold more of them gets its turn (each fetch also takes the usual extra margin and any scores tied at its edge). Once the next window would reach an eighth of the chunks in scope, or pass 4,096, the exact scan answers instead, so one long document can't leave the result short.
minScore drops every hit scoring below it before perDocument and k apply, so a result can hold fewer than k hits, and a listed document is seated only when it has a chunk at or above the floor. On the index path the candidate window stops growing once every candidate in its margin scores below minScore.
const hits = await rag.search({
scope: { namespace: 'handbook' },
query,
k: 8,
perDocument: 2,
minScore: 0.4
})scope is { namespace, documentIds? }. Only chunks inside the scope are read and ranked, so a restricted search fills its top k from allowed documents. Search fails closed:
- a missing
namespaceor any other key inscopefails withINVALID_SCOPE; - a namespace that doesn't exist fails with
NAMESPACE_NOT_FOUND; - a document ID that isn't in the namespace fails with
DOCUMENT_OUT_OF_SCOPE; documentIds: []returns no hits.
readDocument({ namespace, documentId, generation?, fromOrdinal?, limit? })
Reads a document's chunks in ordinal order, one page at a time. Returns { namespace, documentId, generation, chunkCount, metadata?, chunks, nextOrdinal }; each chunk is { chunkId, ordinal, content, metadataJson? }, without its vector. fromOrdinal defaults to 0 and limit to 64 chunks, at most 1,024. nextOrdinal is the next page's fromOrdinal, or null after the last chunk.
Every page comes from one generation. Pass the first page's generation on later pages: if the document was re-ingested or deleted since, the read fails with DOCUMENT_CHANGED rather than mixing generations. Without generation the current one is read, and a document the namespace doesn't have fails with DOCUMENT_OUT_OF_SCOPE. To read around a hit, pass its generation and start a little before its ordinal:
await rag.readDocument({
namespace,
documentId: hit.documentId,
generation: hit.generation,
fromOrdinal: Math.max(0, hit.ordinal - 1),
limit: 3
})listDocuments({ namespace, after?, limit? })
Lists a namespace's documents in document ID order. Returns { documents, next }; each entry is { documentId, generation, chunkCount, revision, metadata? }. limit defaults to 100, at most 1,024. Pass next as after for the following page; it is null on the last one. revision is the namespace revision that committed the document's current generation, so sorting by it gives ingest order, for example to drop the oldest documents. Documents written by 0.10 report 0.
exportNamespace({ namespace, documentIds? }, { signal? }?)
Returns an AsyncIterable<Uint8Array> of the namespace in the QVRAGX1 format: a header with the full embedding space, its spaceId and the namespace's lineage, then each document with its chunks (IDs, text, metadata and vectors in the namespace's vectorEncoding), then the delete records of deleted documents, then a trailer with counts. Each document and delete record carries a hash over its ID and generation. Native IDs, index snapshots and quantized vectors are not exported; the target builds its own index.
An f32 namespace exports format 1 and an f16 namespace format 2, which names the encoding in the header. Import reads both, and format 1 streams from 0.11.0, which carry no hashes. 0.11.0 imports format 1 from this version as long as every document has chunks; it refuses format 2.
The stream reads one database snapshot, so commits made while it runs don't appear in it, and it yields one chunk at a time: memory stays flat whatever the namespace size, and a slow consumer slows the export. With documentIds, only those documents and their delete records are exported; IDs the namespace doesn't have are left out. Bad input throws at the call; an aborted signal fails the iteration with OPERATION_CANCELLED, and close() ends an open export with NOT_READY. A chunk or document that encodes to more than 64 MiB fails the iteration with INVALID_INPUT, since import would refuse it.
const out = fs.createWriteStream('handbook.qvragx')
for await (const bytes of rag.exportNamespace({ namespace: 'handbook' })) out.write(bytes)importNamespace({ namespace, source, index? }, { signal?, onProgress? }?)
Reads a QVRAGX1 stream from source (any AsyncIterable<Uint8Array>) into namespace. A missing namespace is created with the export's space, lineage and vectorEncoding, and index sets its storage as in createNamespace. Returns { documents, skipped, cancelled, revision }.
Before writing anything, the import checks that the header's space hashes to its spaceId (IMPORT_INVALID otherwise), that an existing namespace is in the same space (EMBEDDING_SPACE_MISMATCH) and uses the same vectorEncoding (NAMESPACE_EXISTS; converting would break the documents' content hashes), and that it has the same lineage (IMPORT_LINEAGE_MISMATCH). A namespace without documents takes the export's lineage, and its delete records, which belong to the old lineage, are removed. Two namespaces with different lineages are never merged; to combine them, ingest the exported chunks with their vectors and spaceId, which gives them fresh generations.
A missing namespace is created, and an empty one takes the lineage, in the same batch as the first document or delete record, so an import that fails or is cancelled before then leaves nothing behind. An export with neither creates the namespace once its trailer is read. If an ingest commits into an empty namespace before the import's first write, the import fails with IMPORT_LINEAGE_MISMATCH; an ingest still embedding when the import adopts the namespace commits normally. A source that throws fails the import with IMPORT_SOURCE_FAILED, with the source's error as cause.
Documents keep their exported generation and commit one at a time, like ingest. For each document:
| Target holds | Result |
| ------------------------------------------ | --------------------------------------------- |
| nothing | created |
| an older generation | replaced |
| the same generation and content | unchanged |
| the same generation, other content | skipped, conflict; the target keeps its own |
| a newer generation | skipped, superseded |
| a delete of this generation or a later one | skipped, deleted |
A delete record removes the target's document unless the target holds a newer generation (deleted in documents, or skipped as superseded), and is kept, so importing an older export later can't bring the document back.
Each document is checked before it commits: contiguous ordinals, dimensions × the encoding's bytes per component (4 for f32, 2 for f16) of vector bytes, finite components, a content hash that matches its text, metadata and, when the source document was ingested with vectors, its vectors, a check over its ID and generation, and the input limits of ingest. Each delete record is checked against its ID and generation. Generations above 2^32 are refused. Exports from 0.11.0 carry no ID checks and import without them. A bad document, a stream cut short, a missing or wrong trailer, or a frame over 64 MiB fails with IMPORT_INVALID; documents committed before it stay, and a document is never committed in part. Memory holds one document's chunks at a time. An abort through signal, or close(), stops the import, even one waiting on a source that has stopped sending: it keeps what is committed and returns cancelled: true. Before the header is read there is nothing to keep, so it fails instead, with OPERATION_CANCELLED (NOT_READY for close()). The check before each commit runs once the write lock is held. onProgress({ documents, deletes }) reports the documents and delete records read.
An export of a namespace that has since been dropped imports like any other: into a new or empty namespace it restores the documents with their lineage.
listNamespaces()
Returns every namespace as { name, spaceId, space, index: { storage }, vectorEncoding, revision, documentCount, chunkCount }, by name. The counts read one row per document.
dropNamespace({ name })
Deletes the namespace with all its documents, chunks, delete records and index snapshot, in one batch, and returns { name, documentCount, chunkCount }. Nothing comes back on reopen. createNamespace with the same name starts an empty namespace with a new lineage. An export taken before the drop can still be imported to restore it. The embedding space record stays for other namespaces. A namespace that doesn't exist fails with NAMESPACE_NOT_FOUND.
indexStatus({ namespace })
Reports how namespace is searched, without building its index:
{ mode: 'index', revision, snapshot }: by the index, caught up torevision.snapshotis'off'withoutindex.diror when the provider has noload,'none'before any snapshot was loaded or written,'ok'after one was, or{ error }after a snapshot read or write failed, until the next write succeeds.{ mode: 'scan', reason, error? }: by the scan.reasonis'addon-missing'(@qvac/turbovec-ggmlis not installed, or the runtime is not Bare),'addon-load-failed','rebuild-failed'or'search-failed';erroris the original failure. A namespace whose index failed scans until the RAG is reopened.{ mode: 'unopened' }: the index has not been built sinceready(); the next search builds it.
A namespace that doesn't exist fails with NAMESPACE_NOT_FOUND.
scoped(scope)
Returns { search, readDocument, listDocuments } bound to scope. The scope passed to its search is optional and is intersected with the bound one: it can narrow to fewer documents but never add a document or switch namespace. Its search takes the same vector and spaceId, or query, plus k, perDocument, minScore and signal, as search. A handle never fails on document identity: an ID that the namespace does not have, whether it was never ingested or has since been deleted, is skipped, so a handle whose documents are gone returns no hits instead of an error. Only the shape of the scope can fail (INVALID_SCOPE), and a namespace that does not exist fails with NAMESPACE_NOT_FOUND.
Its readDocument and listDocuments take the same parameters without namespace. readDocument of a document outside the bound scope fails with DOCUMENT_OUT_OF_SCOPE, with the same message as a missing one, so a handle can't probe for documents it wasn't granted. listDocuments lists the whole namespace for a namespace-wide handle, and otherwise only the granted documents that still exist.
const reader = rag.scoped({ namespace: 'handbook', documentIds: ['leave-policy', 'travel'] })
await reader.search({ vector: queryVector, spaceId, k: 5, scope: { documentIds: ['travel'] } })Index providers
index.provider replaces TurboVec, so a host can search through an index it already runs, for example one in a worker. RAGs on one core share an index per namespace only when they share the provider object and dir.
interface IndexProvider {
create(dim: number, storage: IndexStorage): IndexHandle | Promise<IndexHandle>
load?(path: string): Promise<IndexHandle>
}
interface IndexHandle {
readonly length: number // rows after the calls that have finished
readonly dim: number
readonly bitWidth?: number // checked against `storage` when a snapshot loads
addWithIds(vectors: Float32Array, ids: BigUint64Array): Promise<void>
remove(id: bigint): Promise<boolean> // whether the id was present
contains(id: bigint): Promise<boolean>
search(
queries: Float32Array,
k: number,
options?: { signal?: AbortSignal }
): Promise<IndexSearchResult>
dispose(): Promise<void>
searchFiltered?(
queries: Float32Array,
k: number,
allowedIds: BigUint64Array,
options?: { signal?: AbortSignal }
): Promise<IndexSearchResult>
compact?(): Promise<void>
prepare?(): Promise<void>
write?(path: string): Promise<void>
}
// Best first by dot product; ids past the last hit are 2^64 - 1.
interface IndexSearchResult {
scores: Float32Array
ids: BigUint64Array
k: number
}- Calls on one handle must take effect in the order they are made.
- Vectors are float32, row-major. Cosine namespaces pass unit vectors, so the dot product is the cosine. Every hit is rescored exactly from HyperDB, so the index only has to rank candidates well.
storageis the namespace'sindex.storage; a provider may ignore it.- Without
searchFiltered, a search scoped todocumentIdsaskssearchfor more rows and keeps the allowed ones, doubling until it has enough. Each fetch costssearchresults for rows outside the scope, which matters for a provider behind a process boundary; once a fetch would pass an eighth of the index, the exact scan of the scope answers instead, so a small scope in a large index is always scanned. - Without
compact, removed rows stay in the provider's memory until the index is rebuilt. - Snapshots need both
loadandhandle.write. Withoutload,diris ignored and the index is rebuilt from HyperDB on each open. - A snapshot directory serves one provider and
fsper core:ready()fails withINVALID_INPUTwhen another open RAG on the core uses the samedirwith a different one. Once the last RAG on the core using adircloses, its indexes are snapshotted and released, so a later RAG can use the directory with another provider orfs. Snapshot files carry no provider name, so across processes give each provider its owndir.
Memory
Process RSS and search latency for one namespace of 100,000 chunks × 768 dimensions with q8 storage, measured with scripts/bench-index.ts on an M-series Mac (k = 10, 50 searches):
| Open path | RSS after searches | Peak RSS | Open + first search | Search p50 / p95 |
| ----------------------------------- | ------------------ | ------------ | ------------------- | ---------------- |
| Cold open from a snapshot | ~290 MiB | ~320 MiB | 150–170 ms | 21 / 26–29 ms |
| Rebuild from HyperDB (no snapshot) | 610–775 MiB | 985–1020 MiB | 2.4–2.6 s | 21–23 / 24–31 ms |
| Scan, without @qvac/turbovec-ggml | 725–770 MiB | 775–785 MiB | ~2.1 s | 1.3 / 1.4–1.5 s |
Budget about 1 GiB for the first open of a namespace without a snapshot and about 320 MiB once a snapshot exists. Pass index.dir so later opens load the snapshot.
Replacing a document leaves its old rows in the index until it is compacted, which happens once removed rows reach a quarter of the live ones. Re-ingesting all of 20,000 × 768 q8 chunks with new vectors 20 times in one process ends at about 630 MiB RSS (710 MiB peak), against 1.3 GiB without compaction.
To reproduce:
npm run build
bare scripts/bench-index.ts ingest /tmp/rag-bench --chunks 100000 --dims 768 --storage q8
bare scripts/bench-index.ts rebuild /tmp/rag-bench
bare scripts/bench-index.ts cold /tmp/rag-bench
bare scripts/bench-index.ts scan /tmp/rag-bench
bare scripts/bench-index.ts ingest /tmp/rag-replace --chunks 20000 --dims 768 --storage q8
bare scripts/bench-index.ts replace /tmp/rag-replace --rounds 20Each phase runs in its own process and prints its RSS, peak RSS and search latency as JSON.
Export and import of the same namespace, measured with scripts/bench-transfer.ts (no index directory):
| Chunks × 768 | Stream | Export time / peak RSS / peak JS heap | Import time / peak RSS / peak JS heap | | ------------ | ------- | ------------------------------------- | ------------------------------------- | | 20,000 | 60 MiB | 0.4 s / ~250 MiB / ~33 MiB | 1.0 s / ~260 MiB / ~43 MiB | | 100,000 | 300 MiB | 1.7–2.0 s / 600–650 MiB / ~75 MiB | 5.1–5.2 s / ~435 MiB / ~60 MiB |
The stream itself holds one frame on export and one document on import; the JS heap stays far below the stream size. Most of the RSS is the database's read and write caches, as with the scan. Import peaks below the ~470 MiB that ingesting the same 100,000 chunks takes.
bare scripts/bench-index.ts ingest /tmp/rag-bench --chunks 100000 --dims 768
bare scripts/bench-transfer.ts export /tmp/rag-bench
bare scripts/bench-transfer.ts import /tmp/rag-benchDisk
On-disk size of one namespace of 10,400 chunks × 768 dimensions with 1,000 bytes of text per chunk and the default q8 index, measured with scripts/bench-disk.ts after a reopen (MB = 10^6 bytes):
| Component | f32 vectors | f16 vectors |
| ------------------------------------------ | ------------- | ------------- |
| Vectors | 31.9 MB | 16.0 MB |
| Chunk text | 10.4 MB | 10.4 MB |
| Keys, other fields and rows, Hyperbee tree | 3.3 MB | 3.3 MB |
| Hypercore and RocksDB storage | 3.7 MB | 3.3 MB |
| Store | 49.3 MB | 33.0 MB |
| q8 index snapshot (index.dir) | 8.1 MB | 8.1 MB |
| Total | 57.4 MB | 41.1 MB |
Nothing is compressed. Without index.dir there is no snapshot, at the cost of a rebuild on every open (see Memory). An f32 index snapshot takes 32.0 MB instead of 8.1 MB. Right after close() the store can read about 5 MB larger until RocksDB flushes its write-ahead log on the next open.
npm run build
bare scripts/bench-disk.ts /tmp/rag-disk --chunks 10400 --dims 768 --encoding f16
bare scripts/bench-f16.ts /tmp/rag-f16 --clusters 200 --noise 0.05 # recall@10 and score change against f32Errors
Every error is a QvacErrorRAG with a code from ERR_CODES, importable from @qvac/rag or @qvac/rag/errors. Corestore and HyperDB failures, including ready() on storage another process holds, surface as DB_OPERATION_FAILED with the original error as cause. An index that can't be built or searched is never an error: that namespace's searches scan until the RAG is reopened, and indexStatus and onIndexFallback say why.
Development
npm run build:hyperdb-spec # regenerate src/db/hyperspec after a schema change
npm run typecheck
npm run lint
npm run test:unit # index tests need @qvac/turbovec-ggml to load
bare scripts/bench-index.ts --help # index RAM and open-time benchmark, not run in CI
bare scripts/bench-transfer.ts --help # export/import time and RAM, not run in CI
bare scripts/bench-disk.ts --help # on-disk size by component, not run in CI
bare scripts/bench-f16.ts --help # f16 recall and score change, not run in CILicense
Apache-2.0
