Quellcode durchsuchen

docs: the runner DOES consult the embedding cache - the per-process cache misled me (SV-41)

Corrects a claim I committed hours earlier in the entry directly above it,
which is the entry warning that a cited claim is only as good as the part
of the citation you read.

It said runner/src/host_context.cpp calls embed() "with no cache
consultation", so a function pays the provider on every call. Line 1657 is
`if (auto hit = embeddingCache().get(key)) return *hit;`, the put is at
1688, and the doc comment four lines above says it mirrors
handlers/vectors.cpp deliberately. It has been there since fc63a26, the
commit that created the runner, and merge-base confirms it is in the
deployed 0.18.0.

The measurement behind the wrong claim was real: /api/v1/stats did report
cache_size 3, hits 1, misses 3. But embeddingCache() is a function-local
static, so the cache is PER PROCESS - that route reports vectorapi's
instance while /fn is served by the runner's own separate one. Both facts
hold together, and "the cache has served nothing" was a statement about one
of two caches presented as a statement about the feature.

Rewritten rather than labelled, per this file's own rule: the claim was
false when written, not overtaken by events. The honest gap is a missing
route - the runner's cache is unobservable - not a missing lookup.
fszontagh vor 3 Wochen
Ursprung
Commit
f842e842ed
1 geänderte Dateien mit 3 neuen und 1 gelöschten Zeilen
  1. 3 1
      CLAUDE.md

+ 3 - 1
CLAUDE.md

@@ -493,7 +493,9 @@ docker run -d --name svapi-dev -p 8080:8080 -e SMARTBOTIC_VECTORAPI_KEY=dev svap
   **It corroborates the long tail recorded above from the other side.** A cold `query_text` is 0.5-1.0 s at the median and the tail reaches at least 8.17 s, so a 6 s client timeout is not generously set - it is set INSIDE our distribution, and nothing on either side of the call says so. **This is the split-latency-budget trap again** (their timeout is their number, our tail is ours, and neither side can see the other), with the sharper form that **two different faults produced one symptom**: the hourly snapshot stall and this boundary, plus a third explanation they had written up that morning. When a consumer reports an intermittent degradation, the question is not which cause it is but **how many causes it has**.
   The fix is the one already recommended here on independent grounds - embed ONCE and pass an explicit `query_vector` to every collection - which halves the draws from the tail, moves the cold call off the boundary, and is separately the only reproducible baseline.
 - **THE QUERY-EMBEDDING CACHE EXISTS, HAS SINCE 0.1.4 (2026-06-04), AND THIS FILE DENIED IT THREE TIMES IN OCTOBER WHILE CITING THE FILE THAT CONTAINS IT.** `src/embedding_cache.cpp` is a process-singleton LRU keyed on (apiBase, model, text), and on prod it is 4096 entries / 256 MiB / TTL 86400 / **normalize ON** (trim + lowercase, so `"Foo "` and `"foo"` share an entry). The claim was not overtaken by events - it was false when written, about code five months old, and the citation offered as evidence (`emb.embed(cfg.model, text)` in `handlers/vectors.cpp`) sits ELEVEN LINES BELOW the `embeddingCache().get(key)` that disproves it. **The lesson is not "check the citation" but that a citation gets read for the clause it was fetched to support**; I went to that function for the embed call and did not see the cache around it. A cited claim is only as good as the part of the citation you read.
-  **IT COVERS ONE SURFACE AND NOT THE OTHER, WHICH IS THE PART THAT MATTERS IN PRACTICE.** It is consulted in exactly one place, `handlers/vectors.cpp` (the `/search` and vector-insert routes). `runner/src/host_context.cpp` builds an `EmbeddingClient` and calls `embed()` directly with no cache consultation, so **`ctx...search({query_text})` inside a function pays the provider on every call.** Measured on prod 2026-11-02, six days into the process's uptime: `/api/v1/stats` reported cache_size **3**, hits **1**, misses **3** - and three of those four lookups were mine from ten minutes earlier. The cache has served essentially nothing in production because the only `query_text` traffic on the box goes through `/fn`. SV-38.
+  **IT COVERS BOTH SURFACES, AND THE CLAIM THAT IT COVERS ONLY ONE WAS MINE AND WAS FALSE WITHIN HOURS OF WRITING THE ENTRY ABOVE.** This paragraph used to read "it is consulted in exactly one place, `handlers/vectors.cpp` ... `runner/src/host_context.cpp` builds an `EmbeddingClient` and calls `embed()` directly with no cache consultation, so `ctx...search({query_text})` inside a function pays the provider on every call." **Every clause of that is wrong.** `runner/src/host_context.cpp:1657` does `if (auto hit = embeddingCache().get(key)) return *hit;` and line 1688 does `embeddingCache().put(key, v)`, and the function's own doc comment four lines up says **"Mirrors handlers/vectors.cpp: the same cache, keyed the same way, so a function and an HTTP call that embed the same string cost one provider call between them."** It has been there since `fc63a26`, the commit that created the runner, which `git merge-base --is-ancestor fc63a26 b98b160` confirms is in the deployed 0.18.0.
+  **The measurement I built it on was real and I drew a conclusion it could not reach.** `/api/v1/stats` genuinely reported cache_size 3, hits 1, misses 3 - because **`embeddingCache()` is a function-local static, so the cache is PER PROCESS, and `/api/v1/stats` reports vectorapi's instance while `/fn` traffic is served by the RUNNER's own separate instance.** Both facts are true together: vectorapi's cache is near-empty because no `query_text` reaches `/api`, and the runner's cache is the one doing the work and has no route reporting it. "The cache has served nothing" was a statement about one of two caches, presented as a statement about the feature.
+  **So this is the entry above's own lesson committed inside the entry above.** I went to the runner to check the embed call and did not read the four lines around it, having just written that a cited claim is only as good as the part of the citation you read. The honest shape of the gap is not "the runner bypasses the cache" but **"there is no way to observe the runner's cache"** - `/api/v1/stats` cannot see it, so the one surface that uses the cache heavily is the one surface whose hit rate is unmeasurable. That is the real finding and it is a missing route, not a missing lookup. SV-38 and SV-41.
   **`Cache-Control: no-store` on a request skips both the get and the put** (`wantsNoStore()`, `vectors.cpp:42-45`), which is how to measure the provider through this route - and it does NOT reach the provider: the outbound call builds a fresh body and `applyOutboundDefaults()` sets exactly one default header, the User-Agent. No inbound header is ever copied outbound.
 - **A CACHE MAKES SEARCH LATENCY BIMODAL, so do not derive a timeout from a post-cache p50.** The same endpoint answers from the hit path or the provider path depending on state nothing outside can see, and a timeout or retry budget tuned against the hit path will fire on every miss. Same family as the startup loop that was bounded by an attempt COUNT: a budget encoding a duration as a count carries a hidden assumption about per-item cost. Note also that **caching would NOT make a search reproducible** - per-process plus a TTL means a restart or the TTL boundary clears it, so results would be stable across an afternoon and silently not across a deploy, which is stable long enough to be trusted and not long enough to be true. An explicit `query_vector` stays the only reproducible baseline.
 - **MEASURED ON PROD, 2026-10-09, small collections (126 and 49 rows, dim 4096), server-side end to end.** The filtered similarity search costs **~25 ms** and the filter costs nothing measurable at that size - filtered and unfiltered are identical within noise. A cold `query_text` is **0.49-0.95 s**, essentially all of it the provider round trip; the SAME text repeated is 0.022 s, because the provider caches it. **That cache is useless to a workload of unique queries** such as a voice assistant, which is therefore permanently on the 0.5-1.0 s path. Eight samples in one window is a MEDIAN, not a tail: a 12-15 s text search was observed earlier the same day and did not reproduce, so the bad tail is real and unexplained rather than bounded at 0.95 s. Quote these as medians and say so.