Skip to content

Background Reindex ​

Reindex runs embeddings over all chunks — a large batch job that can take minutes. The system runs it as a background job: embeddings happen in a dedicated worker thread, the main-thread event loop stays free, and SSE, live interviews and every other request keep responding.

Design: dedicated worker + async bridge ​

AsyncOnnxEmbedder (server/capabilities/async-embed.ts) moves ONNX inference into a dedicated worker:

  • A dedicated worker runs ONNX (local-embed-worker.mjs); the model is loaded once;
  • The main thread postMessages then awaits a completion message, never blocking the event loop;
  • Requests are queue-serialized (this.queue.then(...)) to exclusively own the shared buffer (SharedArrayBuffer);
  • On success the worker returns { dims, count } and the main thread slices vectors out of the buffer; on failure it rejects.

There are three embedding bridges, each with a role:

BridgeFileMechanismUse
SyncOnnxEmbeddersync-embed.tsSync bridge (worker + shared buffer)Synchronous search path (~17 ms per call)
LocalOnnxEmbedderlocal-embed.tsMain-thread ORTSmall embeddings for import / search
AsyncOnnxEmbedderasync-embed.tsDedicated worker + await messageReindex (large batches, non-blocking)

ONNX inference is a CPU-intensive native call. Placing it in a dedicated worker thread while the main thread only awaits a completion message is what keeps large-batch embedding from freezing the event loop — awaiting a native inference that runs on the main thread does not itself yield the loop.

Performance ​

On an isolated copy of 4000 chunks, probing event-loop responsiveness at a 25 ms interval:

MetricValue
Max event-loop pause43 ms
Probe ticks2241 (near the ideal interval)
Service during rebuildKeeps responding (SSE / live interviews uninterrupted)

A max pause close to the probe interval means the UI and service stay fluid throughout the rebuild.

The benchmark runs on an isolated temp copy of the live data (a chunk subset, with the model copied to a temp dir); it never touches the day-to-day data directory.

Job + polling pattern ​

The UI uses the same "return a job immediately + poll progress" pattern as model download:

  • Job registry: a Map per profile for the private KB, a single field for the shared KB; a repeated POST joins the running job rather than starting another.
  • Short final transaction swaps the index atomically: the old index stays searchable during embedding; once all vectors are computed, one short transaction does DELETE kb_fts + per-chunk UPDATE embedding + INSERT fts, an atomic swap.
  • Consistent state: when UI / CLI polling gets job = null (e.g. a server restart), it errors explicitly rather than falsely reporting success.
  • Shared-KB permission first: startReindex does the reindexScope 403 check before the running-join.

The synchronous reindexKb is kept for CLI and tests. In attached mode the CLI polls GET status every second, throws on failure, and prints { chunks, collections } on success.

Frontend interaction ​

After triggering a rebuild on the Knowledge page, the button enters loading and shows Rebuilding {done}/{total}, polling every second; on completion a toast reads N chunks · FTS5 + local vectors. The shared library shows per-collection progress by ID. Throughout, the user can keep operating other screens — which is exactly what "never blocks the event loop" means in the UI.

For the retrieval design itself see Retrieval Pipeline; for design rationale and benchmarks see Decisions & Benchmarks.

Local-first · inference egresses on demand · fully traceable