Pārlūkot izejas kodu

docs: record the 2.16.0 collection-schema design and when a capability check is sound

fszontagh 1 mēnesi atpakaļ
vecāks
revīzija
03d4f7aa77
1 mainītis faili ar 2 papildinājumiem un 0 dzēšanām
  1. 2 0
      CLAUDE.md

+ 2 - 0
CLAUDE.md

@@ -152,6 +152,8 @@ docker run -d --name svapi-dev -p 8080:8080 -e SMARTBOTIC_VECTORAPI_KEY=dev svap
   - **`filter_mode` is on the response now, and a function's `.search()` carries it too.** `"server"`, `"walk"` or `"none"`, from the single `filterModeName()` so the HTTP route and the runner cannot spell it differently. On the function surface the return **stays an ARRAY** and the flags ride as NON-ENUMERABLE properties, so indexing, `length`, iteration, spread and `JSON.stringify` are byte-identical to before and a script written earlier is unaffected - tests assert each of those, and that the flags do NOT appear in `JSON.stringify`. The prelude also tolerates a host answering the old bare-array shape, so the two halves deploy in either order. The console warning STAYS: it is what an author reading a run log sees, and the structured flag is for the function's logic and for any model downstream of it - a consumer's own bug was exactly that their assistant was never told a search had degraded and said "nothing in memory" with confidence.
   - **`VectorSearchResult::serverFiltered` records which path answered**, because `scanCapped == false` means two different things: false BY CONSTRUCTION here (a sweep of a filtered set is sound) versus "complete this time" on the walk (it is not). A caller cannot infer it from `/api/v1/version`, which reports the CLIENT library version while the path depends on the SERVER - a 2.14 client against a 2.13 server reads as the new path while running the old capped one. Whether to expose it to callers is still open.
   - **The `Depends` floor had to move to `>= 2.14.0`, and that is load-bearing.** Verified rather than assumed, with `nm` on both shipped `.so` files: the 5-argument symbol exists only in 2.14.0, while 2.13.1 carries just the 4-argument form. Since every 2.x client shares one SONAME, leaving the old `>= 2.12.0` floor would have let apt install this binary against a client that cannot resolve the call - the failure that "installs fine and only breaks on the next restart".
+- **DATABASE 2.16.0 ADDS AN OPTIONAL COLLECTION SCHEMA, and for THIS feature the capability check IS the right gate** - the exact inverse of the filtered-search rule, so keep the two straight. `collection_schema` is introduced in 2.16.0, and capability names gate features from 2.15.0 onward, so `serverSupports(Capability::kCollectionSchema)` answers correctly; a 2.16.0 client calling the schema methods against an older server gets `FailedPrecondition` / `server_unsupported`. `filtered_similarity` is NOT gated that way because it predates the capability API and a 2.14.0 server reports no capabilities at all while fully supporting filters. The rule: **a capability name is sound for a feature introduced in 2.15.0 or later, and unsound for anything older.**
+  Design as the database team specified it, for when we implement: a JSON Schema SUBSET with draft 2020-12 meanings (type incl. `["string","null"]` for nullable, `required`, `additionalProperties`, lengths, ranges, `enum`, `const`, `pattern` as RE2, `format`, `items`, nested `properties`), and anything outside it REFUSED at set time with `invalid_schema` so a schema can never look stricter than what is enforced. `_vector` is NOT part of the validated body - dimension stays the collection's `vector_dimension`. A violation is INVALID_ARGUMENT / `schema_violation` whose `fields` carry the violating PATHS (`addr.zip`, `tags[2]`) and whose message names the rule and size, with values deliberately not echoed. `getCollectionSchema()` is a separate call (a read grant suffices) so no public struct changes size. A schema may be set, replaced or removed at any time in **enforce** mode (every existing row validated FIRST, refused with counts and up to 10 example ids if any violate) or **audit** mode (applied regardless, violations counted and let through) - and it is armed under the collection's write lock BEFORE the scan, so no write slips past. Validated on insert/update/upsert/patch (on the MERGED result)/increment/batch/set ops/restoreVersion; NOT on WAL replay or replication apply, which replay already-accepted writes. `suggestCollectionSchema()` generates one from the data, which suits a button in the editor.
 - **DATABASE 2.15.0 EXISTS AND IS NOT ADOPTED. Do not gate filtered search on a capability name.** 2.15.0 adds `GetServerInfo`, `serverInfo()`, `serverSupports(name)` and `Client::Capability` constants, and from it the client and server release independently (2.15.0 is the last lockstep release; the server no longer pins `libsmartbotic-db-client (= VERSION)`). Nothing in it changes our 2.14.0 alignment. **The trap, flagged by the database team: `serverSupports(Capability::kFilteredSimilarity)` is FALSE against a 2.14.0 server, which has no `GetServerInfo` but fully supports filters.** Capability names gate only features introduced in 2.15.0 or later, so a capability check there would silently drop us onto the emulation against the very server we just deployed. Keep what is built: call the filtered overload, and fall back only on `FailedPrecondition` with reason `filters_unsupported`. Adopting 2.15.0 would also make `serverInfo()` a cheaper answer than a `filter_mode` response field, because it finally exposes the SERVER version - which is the thing that decides support, and the gap that made version inference unsound. Note it costs one cached `GetServerInfo` per client per 60 s.
 - **Two database semantics worth knowing, unchanged by 2.14.0 but easy to misread.** `minScore 0.0` DROPS rows whose cosine is negative, so "fewer matches than topK returns all of them" means all that score at or above `minScore` - pass a negative `minScore` to see genuinely every match. And from 2.14.0 rows with EQUAL scores are ordered by id, lower first, including when a tie straddles the cut; that applies to the unfiltered search too and was previously unspecified, so it is observable only on duplicate vectors.
 - **The filtered walk's EMULATION remains, for any server older than 2.14.0, and its cost is ROUND TRIPS.** The client has no filtered `similaritySearch`, so a filtered search fetches the filter's ids with `find()` and then re-runs the WHOLE similarity search with a growing limit, intersecting in our process. Measured on prod with an explicit `query_vector` (no embedding in the path), on 12447 rows at dim 4096: unfiltered `top_k` 500 is **0.26 s**, filtered `top_k` 500 is **3.21 s** having issued three searches (2000, 8000, 10000), and a filter matching nothing is 0.05 s. Time tracks rows fetched almost linearly, ~0.3 ms per candidate - neither the vector arithmetic nor the 1 MB response is the cost. `firstScanDepth()`/`nextScanDepth()` (`vector_search.hpp`, pure, 8 tests) therefore go straight to the cap when the filter matches fewer rows than `topK` (the early exit can never fire, so every step before the cap is certain waste) and otherwise widen by the hit rate the previous pass actually measured. **`scan * 4` stays the FLOOR**, so a mis-estimate can only skip steps and never shrink one - progress stays guaranteed and nothing can get slower. The RESULT is unchanged in every case: each pass is a prefix of the same ranking, so the first `topK` matches within it are the same rows whatever depth the walk stopped at. The real fix is a server-side filtered similarity search, requested and designed for database 2.14.0; this emulation must keep working regardless, as the path for any older server.