/** * Persistence for the content-hash embedding cache (FEAT-492, migration * 043). Backs `CachingEmbedProvider` (src/providers/embed/cache.ts) — this * module is pure SQLite access (batched lookup/insert/evict + cumulative * hit/miss counters), no provider or hashing logic of its own. * * Key = (content_hash, model, dim). `dim` rides alongside `model` (not * inferred from it) because a provider's *configured* output dim can change * independently of its model name — Azure's `dimensions` truncation param * is the concrete case — and a cache keyed on (hash, model) alone would then * serve a wrong-length vector for the caller's current `embed.dim`. */ import type { DB } from './db.js'; export interface EmbedCacheEntry { contentHash: string; vector: Float32Array; } /** * Batched lookup: returns a Map from content_hash -> cached vector for every * hash in `hashes` that has a live row for `model`/`dim`. Hashes with no * cache row are simply absent from the returned map (the caller's miss set * is `hashes` minus `map.keys()`) — this function never distinguishes "no * row" from an error; a query failure throws and the caller (CachingEmbedProvider) * treats the whole batch as a miss rather than serving partial/uncertain data. * * Also bumps `last_used_at` on every hit (single UPDATE, same statement * batch) so the LRU eviction order in `evictExcess` reflects real recency, * not just insertion order. */ export declare function getCachedEmbeddings(db: DB, hashes: string[], model: string, dim: number): Map; /** Batched insert-or-replace for newly-computed (miss-set) embeddings. No-op on an empty list. */ export declare function putCachedEmbeddings(db: DB, model: string, dim: number, entries: EmbedCacheEntry[]): void; /** * Evict the oldest-by-`last_used_at` rows once the table exceeds `maxEntries`. * Called after every insert batch rather than on a timer — cheap (COUNT(*) is * indexed via the table's own rowcount, and the DELETE only runs when the cap * is actually exceeded) and keeps the table bounded without a background job. */ export declare function evictExcess(db: DB, maxEntries: number): void; /** Bump the cumulative hit/miss counters (doctor "Embed cache hit rate" stat). One UPDATE per embed() call, not per item. */ export declare function recordCacheStats(db: DB, hits: number, misses: number): void; export interface EmbedCacheStats { hits: number; misses: number; } /** Read the cumulative hit/miss counters. Returns zeros if the singleton row is somehow missing (pre-migration DB, defensive only). */ export declare function getCacheStats(db: DB): EmbedCacheStats;