feat: Phase 3 — semantic cache (local embeddings + per-tenant)#5
Merged
Conversation
Implements ROADMAP Phase 3 (PRP C4): embed each request locally and serve a semantically-similar prior response without calling the provider — the first cost/latency win. - SemanticCache (in-memory cosine) behind an interface; an Ollama /embeddings embedder (keyless) + a cosine util. Per-tenant buckets keyed on api-key hash plus model/params/stream/embed-model; semantic match within a bucket above a configurable threshold (default 0.92). - Caches non-streaming responses and streaming responses (buffer on miss, replay SSE on hit). Fails open: any embed error falls through to the provider. - cacheHit recorded on the span + trace; queryable via GET /traces?cacheHit=true. - Config: CACHE_ENABLED/SIMILARITY_THRESHOLD/TTL_SECONDS/MAX_ENTRIES, plus OLLAMA_BASE_URL/EMBED_MODEL. SECURITY_REVIEW_LOG SR-003 (area 5 ticked). - 89 tests, 91% branch coverage; pnpm verify + CI green. Live-verified in-process (hit served from cache, stream replay, cacheHit in /traces).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
ROADMAP Phase 3 — semantic cache (PRP C4). Embed each request locally and, when a new request is similar enough to a recent one, serve the stored response without calling the provider — the first real cost/latency win.
SemanticCache(in-memory, brute-force cosine) behind an interface — swappable to Redis/vector later. An Ollama/embeddingsembedder (keyless, reuses the providerfetchpattern) + a cosine util.model / temperature / max_tokens / stream / embed-model, so no cross-tenant hits and no collisions across requests that change the answer; semantic matching happens only within a bucket, above a configurable threshold (default0.92).cacheHitis recorded on the span + trace and is queryable viaGET /traces?cacheHit=true.CACHE_ENABLED,CACHE_SIMILARITY_THRESHOLD,CACHE_TTL_SECONDS,CACHE_MAX_ENTRIES, plusOLLAMA_BASE_URL/EMBED_MODEL.SECURITY_REVIEW_LOGSR-003 (area 5 ticked).How to verify
Live smoke test
In-process, real OTel bootstrap + real cache (deterministic stub embedder):
provider.chatcalled once;GET /traces?cacheHit=true-> returns the hit (cacheHit:true, tokens, hashed key).(Live Ollama embeddings depend on the machine's Ollama runtime, which currently crashes; if embeddings error in real use the cache fails open and the gateway works unchanged — so the smoke test uses a stub embedder for determinism.)
Notes
setis awaited on a miss for deterministic population; could be backgrounded later to shave miss latency.