Embedding & Vector Index
The semantic leg's quality rests on four things: model, pooling, execution bridge, and index structure. This page records their implementation and invariants; fusion and gating live in Retrieval Pipeline, rebuild scheduling in Background Reindex.
Model catalog
| Model | Dims | Size | Notes |
|---|---|---|---|
bge-small-zh-v1.5 | 512 | ≈25 MB | Default; Xenova quantized ONNX |
bge-base-zh-v1.5 | 768 | ≈98 MB | Optional; no product-path gain and slower rebuild, not enabled by default |
Runtime is onnxruntime-node (InferenceSession.create); tokenization uses the model's own tokenizer.json (WordPiece). Model file candidates: model.onnx / model_quantized.onnx / model_int8.onnx / bge-m3.onnx.
Pooling: decided by metadata
resolvePooling reads ifg-embed.json written at install/enable time: bge family ⇒ CLS pooling; other models mean-pool over attention-masked tokens then L2-normalize. The path heuristic (/bge/i) is only a legacy-install fallback. Matching pooling to training is what makes vectors semantically correct — same corpus, CLS vs mean: heading 88.3% vs 85.0%, paraphrase 33.3%/MRR 0.167 vs 22.2%/0.093.
Three execution bridges
The same ONNX graph is invoked three ways, chosen by "may the main thread block?":
| Bridge | Mechanism | Main thread | Used by |
|---|---|---|---|
SyncOnnxEmbedder | worker + SharedArrayBuffer (4 MiB data + 16 B signal), Atomics.wait in 250 ms slices, 120 s timeout | blocks | sync search embed(), CLI/test sync reindex, topic hierarchy |
LocalOnnxEmbedder | main-thread await session.run() (the graph itself runs synchronously) | brief block | import / small-batch search |
AsyncOnnxEmbedder | same worker, but postMessage reply + serialized promise queue | non-blocking | background reindexKbAsync |
Worker protocol (local-embed-worker.mjs): receives {type:'embed', texts} → writes L2-normalized vectors into the shared buffer → sets flag[1]=dims, flag[2]=count, flag[0]=1 → Atomics.notify and postMessage({dims,count}); errors set flag[3]. The sync bridge waits on Atomics, the async bridge on messages — they never interfere.
Fallback chain
embed() tries, in order: sync ONNX → IFG_EMBED_CMD subprocess (20 s timeout) → FNV-1a hashed n-gram vector (256 dims), signed hashing-trick + L2 normalization, identity {kind:'hashed', dims:256}. The hash fallback keeps search working without a model but never joins RRF fusion (it is a lexical/character feature and adds no signal on semantic paraphrase).
Vector index structure
- Vectors are stored as raw Float32 BLOBs in
kb_chunks.embedding; dims =blob.length / 4. - Brute-force cosine (dot product on normalized vectors), no ANN — at 14k chunks: 17–20 ms (small) / 24–29 ms (base); ANN complexity isn't worth it.
- One
WeakMapcache per Profile, invalidated againstkb_meta.version; the cache is a contiguous flattenedVectorMatrix, no per-chunk objects. topVectorsuses a bounded top-K buffer with in-place min replacement, never a full sort; candidates are hydrated only when unioning the two legs.
Fingerprint & staleness
Index fingerprint = engine + width + pooling. Same width but different pooling/engine makes old vectors incomparable with new queries → indexIsStale true → semantic leg disabled and rebuild prompted. Reindex runs 32 chunks/batch, length-sorted to reduce padding, embeds heading + ' ' + text, then swaps the index in a single atomic transaction (old index stays searchable until commit).
Eval hook
The vectorOnly option lets the eval harness run the semantic leg alone, measured separately from the product path (with its rescue gate), so gating can't mask model differences. Benchmarks and model-selection rationale: Design Rationale.
Structure at a glance
Figure 1 · Metadata → bridge → index and query