Environment:23blocks OS Ai maestro ONNX Runtime
| Knowledge Sources | |
|---|---|
| Domains | Embeddings, Search |
| Last Updated | 2026-10-26 18:00 GMT |
Overview
ONNX Runtime (onnxruntime-node) running the `Xenova/bge-small-en-v1.5` model in a disposable child process for local, CPU-based 384-dimensional text embeddings.
Description
AI Maestro generates text embeddings locally — no external API — for semantic search and memory consolidation. The `@huggingface/transformers` pipeline runs `feature-extraction` on the `Xenova/bge-small-en-v1.5` model with `dtype: 'q8'` (quantized). Embeddings are L2-normalized, producing 384-dimensional `Float32Array` vectors stored in CozoDB.
The embedding model runs in a single bounded, recyclable child process (`embedding-worker.mjs`), not in the web server. The web server never imports Transformers or ONNX. The `EmbeddingClient` serializes all callers, sends at most 2 texts per batch, and recycles the worker after `maxBatches` (64) or when RSS exceeds `maxBytes` (512MB) — bounding memory even if a native allocator's growth is hidden by swap.
The model cache lives at `~/.aimaestro/cache/transformers` (configurable via `AIMAESTRO_EMBEDDING_CACHE_DIR`), kept outside `.next/` because a production rebuild deletes `.next/server/`. A previous cache location is auto-migrated.
Usage
Use this environment for all semantic search and memory embedding operations. Required by the delta indexing engine (embeds new conversation lines), hybrid RAG search (generates query embeddings), and memory consolidation (embeds extracted memories for deduplication). The first run downloads the model (~50MB), so the first embedding request has a longer cold timeout (10 minutes).
System Requirements
| Category | Requirement | Notes |
|---|---|---|
| OS | Linux / macOS | onnxruntime-node ships CPU-only builds |
| CPU | Any x86_64 | ARM supported on Linux |
| Disk | ~50-100MB for model cache | Stored at `~/.aimaestro/cache/transformers` |
| Memory | 512MB worker cap | Worker recycled when RSS exceeds `AIMAESTRO_EMBEDDING_MAX_MB` (default 512) |
Dependencies
Node Packages
- `@huggingface/transformers` >= 3.8.1
- `onnxruntime-node` 1.17.0 (pinned via `resolutions` in package.json)
System Packages
- None (ONNX Runtime ships as a native Node addon)
Credentials
The following environment variables tune the embedding worker:
- `AIMAESTRO_EMBEDDING_CACHE_DIR`: Custom model cache directory (default `~/.aimaestro/cache/transformers`)
- `AIMAESTRO_EMBEDDING_DEVICE`: Override device (default `cpu`; on Linux `auto` offers CUDA first but onnxruntime-node is CPU-only)
- `AIMAESTRO_EMBEDDING_MAX_BATCHES`: Worker recycle batch count (default 64)
- `AIMAESTRO_EMBEDDING_MAX_MB`: Worker recycle RSS limit in MB (default 512)
Quick Install
yarn install # transformers.js and onnxruntime-node are dependencies
# The model downloads automatically on first embedding request
Code Evidence
CPU device selection from `lib/rag/embedding-worker.mjs:51-55`:
// CPU explicitly, not 'auto': on Linux 'auto' offers ONNX Runtime the
// cuda provider first, onnxruntime-node ships CPU-only, and every batch
// fails while the model download still looks healthy.
device: process.env.AIMAESTRO_EMBEDDING_DEVICE || 'cpu',
Worker recycle logic from `lib/rag/embedding-worker.mjs:30-31,87`:
const maxBatches = positive('AIMAESTRO_EMBEDDING_MAX_BATCHES', 64)
const maxBytes = positive('AIMAESTRO_EMBEDDING_MAX_MB', 512) * 1024 ** 2
// ...
process.send?.({ id, vectors, retire: batches >= maxBatches || Math.max(rss, peakRss, footprint) >= maxBytes, metrics })
Process-global singleton from `lib/rag/embeddings.ts:8-15`:
// Next can bundle this module more than once; a module-local singleton would
// create duplicate ONNX sessions. Keep the queue on the process global instead.
const shared = globalThis as typeof globalThis & { _maestroEmbeddings?: EmbeddingClient }
function getClient(): EmbeddingClient {
if (!shared._maestroEmbeddings) {
shared._maestroEmbeddings = new EmbeddingClient()
process.once('exit', () => shared._maestroEmbeddings?.close())
}
return shared._maestroEmbeddings
}
Vector validation (384-dim, finite) from `lib/rag/embedding-client.mjs:115-116`:
const valid = Array.isArray(msg.vectors) && msg.vectors.length === this.active.texts.length &&
msg.vectors.every(v => v instanceof Float32Array && v.length === 384 && v.every(Number.isFinite))
Common Errors
| Error Message | Cause | Solution |
|---|---|---|
| `Embedding worker timed out` | Model download slow on first run | Cold timeout is 10 min; ensure network access for model download |
| `Embedding queue is full; retry later` | More than 64 pending batches or 16MB queued | Wait; the queue drains as batches complete |
| Every batch fails on Linux | `device: 'auto'` offers CUDA but onnxruntime-node is CPU-only | Set `AIMAESTRO_EMBEDDING_DEVICE=cpu` (this is the default) |
| `Unexpected embedding tensor shape` | Model output not 384-dimensional | Ensure `Xenova/bge-small-en-v1.5` is the model; do not swap models without updating the 384 constant |
Compatibility Notes
- Linux: Default device is `cpu`. Setting `device: 'auto'` would offer CUDA first, but `onnxruntime-node` ships CPU-only, so every batch fails while the download looks healthy. A CUDA-enabled ONNX build would need `AIMAESTRO_EMBEDDING_DEVICE` override.
- macOS: RSS footprint sampling (`processFootprint`) may fail occasionally; the peak RSS check (`process.resourceUsage().maxRSS`) is the fallback.
- Model cache: Keep outside `.next/` — a production rebuild deletes `.next/server/`. The default `~/.aimaestro/cache/transformers` follows the state volume. The old `.next/server/.cache/Xenova` location is auto-migrated.
- Worker isolation: Only the disposable child process loads Transformers/ONNX. The web server never imports them, so a model crash or memory leak cannot take down the server.
- Version pinning: `onnxruntime-node` is pinned to 1.17.0 via `resolutions` in `package.json` to prevent version drift across Next.js bundles.