/** * Shared ONNX text embedders — SP-100 (MiniLM), SP-156 (Granite trial). * * Embeds prompt text for HyDRA requirement projection and semantic cluster * matching. One ONNX session per instance; share across matchers via a single * factory call and coordinated dispose(). */ import { createHash } from 'node:crypto'; import { promises as fs } from 'node:fs'; import path from 'node:path'; import type { Encoder, EncoderCascadeConfig } from '../types/schemas.js'; import { DEFAULT_ENCODER } from '../types/schemas.js'; import { selectEncoderForPrompt, type EncoderGateReasonCode, } from './encoder-gate.js'; export const EMBEDDING_DIM = 384; /** MiniLM ONNX model (384-dim, 512-token context). */ export const MINILM_ONNX_MODEL = 'Xenova/all-MiniLM-L6-v2'; /** * Granite 97M ONNX artifact for @huggingface/transformers. * Source weights: ibm-granite/granite-embedding-97m-multilingual-r2 (384-dim). */ export const GRANITE_ONNX_MODEL = 'onnx-community/granite-embedding-97m-multilingual-r2-ONNX'; export interface TextEmbedder { embed(text: string): Promise; dispose(): Promise; } // ─── ONNX artifact digest pinning (SP-259, #147) ───────────────────────────── /** * Artifact pin verification mode: * - `off` — no verification (default; preserves unpinned local dogfood downloads) * - `verify` — verify cached artifacts when pins exist for the model; fail * closed on digest mismatch or missing cached artifact. Models without pins * remain unpinned (first-run download allowed). * - `enforce` — CI/prod mode: pins are REQUIRED for the model; fail closed when * the pin file is missing/unparseable, the model has no pins, a pinned * artifact is missing from the cache, or any digest mismatches. */ export type OnnxArtifactPinMode = 'off' | 'verify' | 'enforce'; export const ONNX_ARTIFACT_PIN_MODES: readonly OnnxArtifactPinMode[] = [ 'off', 'verify', 'enforce', ]; /** Default pin file (relative to process CWD). Override via options or env. */ export const DEFAULT_ONNX_PIN_FILE = 'config/onnx-artifact-pins.json'; /** Env vars (operator wiring; schemas/hydra-matcher out of SP-259 scope). */ export const ONNX_PIN_MODE_ENV = 'SMART_ROUTER_ONNX_PIN_MODE'; export const ONNX_PIN_FILE_ENV = 'SMART_ROUTER_ONNX_PIN_FILE'; /** Pin file shape: per-model map of cache-relative file path → SHA-256 hex. */ export interface OnnxArtifactPinFile { readonly version: number; readonly pins: Record>; } export interface OnnxPinOptions { /** Pin mode. Default: $SMART_ROUTER_ONNX_PIN_MODE, else 'off'. */ readonly pinMode?: OnnxArtifactPinMode; /** Pin file path. Default: $SMART_ROUTER_ONNX_PIN_FILE, else DEFAULT_ONNX_PIN_FILE. */ readonly pinFilePath?: string; } const SHA256_HEX = /^[0-9a-f]{64}$/; function resolvePinMode(options?: OnnxPinOptions): OnnxArtifactPinMode { const raw = options?.pinMode ?? process.env[ONNX_PIN_MODE_ENV] ?? 'off'; if ((ONNX_ARTIFACT_PIN_MODES as readonly string[]).includes(raw)) { return raw as OnnxArtifactPinMode; } throw new Error( `Invalid ONNX artifact pin mode "${raw}" (${ONNX_PIN_MODE_ENV}). ` + `Expected one of: ${ONNX_ARTIFACT_PIN_MODES.join(', ')}`, ); } async function loadPinFile( pinFilePath: string, mode: OnnxArtifactPinMode, ): Promise { let raw: string; try { raw = await fs.readFile(pinFilePath, 'utf8'); } catch { if (mode === 'enforce') { throw new Error( `ONNX artifact pin mode 'enforce' requires a readable pin file at ${pinFilePath} (fail closed)`, ); } return undefined; } let parsed: unknown; try { parsed = JSON.parse(raw); } catch { throw new Error( `ONNX artifact pin file ${pinFilePath} is not valid JSON (fail closed)`, ); } const candidate = parsed as Partial; if ( typeof candidate !== 'object' || candidate === null || typeof candidate.pins !== 'object' || candidate.pins === null ) { throw new Error( `ONNX artifact pin file ${pinFilePath} must contain a "pins" object (fail closed)`, ); } return candidate as OnnxArtifactPinFile; } async function sha256File(filePath: string): Promise { const hash = createHash('sha256'); hash.update(await fs.readFile(filePath)); return hash.digest('hex'); } async function pathExists(filePath: string): Promise { try { await fs.access(filePath); return true; } catch { return false; } } /** * Locate a cached artifact. transformers.js FS cache stores files at * `//`; also support hub-style * `/models----/snapshots//`. */ async function findCachedArtifact( cachePath: string, modelId: string, relPath: string, ): Promise { const flat = path.join(cachePath, modelId, relPath); if (await pathExists(flat)) return flat; const hubDir = path.join(cachePath, `models--${modelId.replace(/\//g, '--')}`); try { const snapshotsDir = path.join(hubDir, 'snapshots'); for (const revision of await fs.readdir(snapshotsDir)) { const candidate = path.join(snapshotsDir, revision, relPath); if (await pathExists(candidate)) return candidate; } } catch { /* no hub-style cache for this model */ } return undefined; } /** * Verify cached ONNX artifacts for `modelId` against configured SHA-256 pins. * Fails closed on digest mismatch or missing pinned artifact; in `enforce` * mode also fails when the pin file or model pins are missing. * * Runs after pipeline load so first-run downloads are verified before the * embedder is returned; a mismatching session is never usable. */ export async function verifyOnnxArtifactPins( modelId: string, cachePath: string, options?: OnnxPinOptions, ): Promise { const mode = resolvePinMode(options); if (mode === 'off') return; const pinFilePath = options?.pinFilePath ?? process.env[ONNX_PIN_FILE_ENV] ?? DEFAULT_ONNX_PIN_FILE; const pinFile = await loadPinFile(pinFilePath, mode); const modelPins = pinFile?.pins[modelId]; if (modelPins === undefined || Object.keys(modelPins).length === 0) { if (mode === 'enforce') { throw new Error( `ONNX artifact pin mode 'enforce' requires pins for ${modelId} in ${pinFilePath} (fail closed)`, ); } return; // verify mode, no pins configured → unpinned dogfood allowed } for (const [relPath, expectedDigest] of Object.entries(modelPins)) { if (!SHA256_HEX.test(expectedDigest)) { throw new Error( `Invalid SHA-256 pin for ${modelId} ${relPath} in ${pinFilePath}: "${expectedDigest}" (fail closed)`, ); } const cached = await findCachedArtifact(cachePath, modelId, relPath); if (cached === undefined) { throw new Error( `Pinned ONNX artifact missing from cache: ${modelId} ${relPath} under ${cachePath} (fail closed)`, ); } const actualDigest = await sha256File(cached); if (actualDigest !== expectedDigest) { throw new Error( `ONNX artifact digest mismatch for ${modelId} ${relPath}: expected sha256 ${expectedDigest}, got ${actualDigest} (${cached}). Refusing to load (fail closed)`, ); } } } // ─── ONNX runtime types ─────────────────────────────────────────────────────── interface OnnxPipelineOutput { readonly data: Float32Array; } type OnnxExtractorFn = ( text: string, options: { readonly pooling: string; readonly normalize: boolean }, ) => Promise; /** * transformers.js pipeline callable plus its release handles. v3/v4 pipelines * expose `dispose()` (releases the underlying ONNX inference sessions); the * wrapped model's `dispose()` is the fallback handle (SP-260, #147). */ type OnnxPipeline = OnnxExtractorFn & { readonly dispose?: () => Promise; readonly model?: { readonly dispose?: () => Promise }; }; interface TransformersModule { pipeline( task: string, model: string, options: Record, ): Promise; } async function loadTransformersModule(): Promise { const moduleName = '@huggingface/transformers'; try { return (await import(moduleName)) as TransformersModule; } catch { throw new Error( `ONNX embedding requires ${moduleName}. Install: npm i ${moduleName}`, ); } } async function createOnnxFeatureEmbedder( modelId: string, artifactCachePath: string, pinOptions?: OnnxPinOptions, ): Promise { const mod = await loadTransformersModule(); const extractor: OnnxPipeline = await mod.pipeline( 'feature-extraction', modelId, { cache_dir: artifactCachePath }, ); // SP-259: verify cached artifact digests after load (first-run downloads are // verified before the embedder is returned; mismatch → fail closed). await verifyOnnxArtifactPins(modelId, artifactCachePath, pinOptions); // SP-260 (#147): disposed embedders fail closed on embed() rather than // silently recreating an ONNX session behind the caller's back. let disposed = false; return { async embed(text: string): Promise { if (disposed) { throw new Error( `TextEmbedder for ${modelId} has been disposed; embed() fails closed. ` + 'Create a new embedder via createTextEmbedder to continue.', ); } const output = await extractor(text, { pooling: 'mean', normalize: true, }); if (output.data.length !== EMBEDDING_DIM) { throw new Error( `Embedding shape mismatch: expected ${EMBEDDING_DIM}, got ${output.data.length}`, ); } return output.data; }, async dispose(): Promise { if (disposed) return; // idempotent: safe for shared-factory callers disposed = true; // Release the strongest available handle: pipeline.dispose() releases // the ONNX inference sessions (transformers.js v3/v4); model.dispose() // is the documented fallback. Fail loud when neither exists rather than // pretending resources were freed. if (typeof extractor.dispose === 'function') { await extractor.dispose(); return; } if (typeof extractor.model?.dispose === 'function') { await extractor.model.dispose(); return; } throw new Error( `No dispose handle on @huggingface/transformers pipeline for ${modelId}: ` + 'expected pipeline.dispose() or model.dispose(). ONNX sessions may be ' + 'leaked; upgrade @huggingface/transformers.', ); }, }; } /** * Creates a TextEmbedder backed by @huggingface/transformers ONNX runtime. * Model: Xenova/all-MiniLM-L6-v2 (384-dim). * * The package is loaded dynamically — not required at compile time. * Install: `npm i @huggingface/transformers` */ export async function createOnnxTextEmbedder( artifactCachePath: string, pinOptions?: OnnxPinOptions, ): Promise { return createOnnxFeatureEmbedder(MINILM_ONNX_MODEL, artifactCachePath, pinOptions); } /** * Granite 97M long-context embedder (384-dim ONNX drop-in for SP-115 head). * Model: ibm-granite/granite-embedding-97m-multilingual-r2 via ONNX runtime. */ export async function createGraniteOnnxTextEmbedder( artifactCachePath: string, pinOptions?: OnnxPinOptions, ): Promise { return createOnnxFeatureEmbedder(GRANITE_ONNX_MODEL, artifactCachePath, pinOptions); } /** Select ONNX text embedder by operator encoder flag. */ export async function createTextEmbedder( encoder: Encoder = DEFAULT_ENCODER, artifactCachePath: string, pinOptions?: OnnxPinOptions, ): Promise { switch (encoder) { case 'granite': return createGraniteOnnxTextEmbedder(artifactCachePath, pinOptions); case 'minilm': return createOnnxTextEmbedder(artifactCachePath, pinOptions); default: { const _exhaustive: never = encoder; throw new Error(`Unsupported encoder: ${String(_exhaustive)}`); } } } // ─── Cascading embedder (SP-292, #173 part 2) ──────────────────────────────── /** * Decision telemetry for the most recent cascade embed. Mirrors the routing * feature-sidecar fields from #173 so the pipeline can copy them onto live * decisions without reshaping (SP-292). */ export interface CascadeEmbedderTelemetry { /** Encoder that produced the embedding (never mixed across spaces). */ readonly encoder_selected: Encoder; /** Gate token estimate for the embedded prompt. */ readonly token_estimate: number; /** Configured cascade threshold in effect for the decision. */ readonly cascade_threshold: number; /** SP-291 gate reason code (`under_threshold` / `over_threshold` / `granite_fallback`). */ readonly reason_code: EncoderGateReasonCode; /** * Present when an over-threshold prompt degraded to the primary encoder * because the long-context session was unavailable (degrade, never mix). */ readonly cascade_fallback_reason: EncoderGateReasonCode | null; } export interface CascadingTextEmbedder extends TextEmbedder { /** * Telemetry from the most recently completed embed(); null before the * first embed. Concurrent embeds race on this snapshot — callers on the * routing hot path embed sequentially per request. */ lastTelemetry(): CascadeEmbedderTelemetry | null; } /** Type guard: does this embedder carry cascade decision telemetry? */ export function isCascadingTextEmbedder( embedder: TextEmbedder, ): embedder is CascadingTextEmbedder { return ( typeof (embedder as Partial).lastTelemetry === 'function' ); } export interface CreateCascadingTextEmbedderOptions { readonly pinOptions?: OnnxPinOptions; /** * Primary encoder for under-threshold / fallback embeds. * Default: DEFAULT_ENCODER ('minilm'), matching `hydra.encoder` defaults. */ readonly primaryEncoder?: Encoder; /** * Session factory override for tests. Default: createTextEmbedder bound to * the cascade artifact cache path and pin options. */ readonly sessionFactory?: (encoder: Encoder) => Promise; } /** * Lazy dual-session cascading embedder (SP-292, #173 part 2). * * Owns at most one ONNX session per encoder: the primary session loads on the * first embed, the long-context (Granite) session loads only on the first * over-threshold prompt — memory is paid only when the path fires. Selection * is the SP-291 pure gate; token estimates default to prompt length (the * turn-envelope-parity estimator). * * Failure behavior: degrade, never mix. If the long-context session fails to * load or embed, the request is served by the primary encoder with reason * code `granite_fallback` and the long-context encoder is marked unavailable * for subsequent prompts. Never silently compares cross-encoder vectors. * * dispose() closes every session that was actually loaded (idempotent); * embed() after dispose fails closed (SP-260 precedent). */ export function createCascadingTextEmbedder( config: EncoderCascadeConfig, artifactCachePath: string, options?: CreateCascadingTextEmbedderOptions, ): CascadingTextEmbedder { const primaryEncoder = options?.primaryEncoder ?? DEFAULT_ENCODER; const longContextEncoder = config.long_context_encoder; const factory = options?.sessionFactory ?? ((encoder: Encoder) => createTextEmbedder(encoder, artifactCachePath, options?.pinOptions)); const sessions = new Map>(); let longContextAvailable = true; let disposed = false; let last: CascadeEmbedderTelemetry | null = null; function sessionFor(encoder: Encoder): Promise { let pending = sessions.get(encoder); if (pending === undefined) { pending = factory(encoder); sessions.set(encoder, pending); // A rejected session promise must not be cached forever — drop it so a // later retry observes a fresh load attempt. pending.catch(() => sessions.delete(encoder)); } return pending; } function telemetry( decision: { readonly encoder: Encoder; readonly reason_code: EncoderGateReasonCode; readonly token_estimate: number; }, fallbackReason: EncoderGateReasonCode | null, ): CascadeEmbedderTelemetry { return { encoder_selected: decision.encoder, token_estimate: decision.token_estimate, cascade_threshold: config.token_threshold, reason_code: fallbackReason ?? decision.reason_code, cascade_fallback_reason: fallbackReason, }; } return { async embed(text: string): Promise { if (disposed) { throw new Error( 'CascadingTextEmbedder has been disposed; embed() fails closed. ' + 'Create a new embedder via createCascadingTextEmbedder to continue.', ); } const decision = selectEncoderForPrompt(text, config, { primaryEncoder, longContextAvailable, }); if (decision.encoder === longContextEncoder && longContextAvailable) { try { const session = await sessionFor(longContextEncoder); const vector = await session.embed(text); last = telemetry(decision, null); return vector; } catch (error) { // Degrade, never mix (#173): Granite session/artifacts unavailable → // serve with the primary encoder and make the fallback explicit. longContextAvailable = false; sessions.delete(longContextEncoder); console.warn( 'Encoder cascade: long-context encoder unavailable; degrading to primary encoder (granite_fallback)', { long_context_encoder: longContextEncoder, primary_encoder: primaryEncoder, token_estimate: decision.token_estimate, error: error instanceof Error ? error.message : String(error), }, ); const session = await sessionFor(primaryEncoder); const vector = await session.embed(text); last = telemetry( { ...decision, encoder: primaryEncoder }, 'granite_fallback', ); return vector; } } const session = await sessionFor(primaryEncoder); const vector = await session.embed(text); last = telemetry( { ...decision, encoder: primaryEncoder }, decision.reason_code === 'granite_fallback' ? 'granite_fallback' : null, ); return vector; }, lastTelemetry(): CascadeEmbedderTelemetry | null { return last; }, async dispose(): Promise { if (disposed) return; // idempotent: safe for shared-factory callers disposed = true; const pending = [...sessions.values()]; sessions.clear(); const failures: string[] = []; for (const sessionPromise of pending) { try { const session = await sessionPromise; await session.dispose(); } catch (error) { failures.push(error instanceof Error ? error.message : String(error)); } } // Fail loud: partially released cascade sessions must not pass silently. if (failures.length > 0) { throw new Error( `CascadingTextEmbedder dispose failed for ${failures.length} session(s): ${failures.join('; ')}`, ); } }, }; }