Преглед на файлове

docs: our unconditional createCollection costs a second window on 2.17.x

Under 2.17.x the re-call performs the online encryption upgrade whenever it
changes anything, and a collection reaching encryption through the
bare-alias option merge still has its own flag unset - so our call does
change something. That requests a reseal at the next boot, and the reseal
compacts every project: the 167-second term from this morning's measurement.

Observed rather than predicted: the dev box's restart onto 2.17.1 took ~3.5
minutes instead of ~15 seconds, because our bootstraps had flipped those
flags under 2.17.0. The database session traced it and told me.

So the prod 2.17.x window is three steps and two compactions - upgrade the
database, start vectorapi so the bootstraps move the flags, then restart the
database inside the SAME window so the reseal lands while an outage is
expected. Omit the last step and a routine restart weeks later takes three
minutes for no visible reason, which is the same shape as the 0751 trap.

Today's 0.12.0 is unaffected: prod is on 2.14.0, where that branch does not
exist and the re-call is a no-op.
fszontagh преди 1 месец
родител
ревизия
ba2ed99afe
променени са 1 файла, в които са добавени 3 реда и са изтрити 0 реда
  1. 3 0
      CLAUDE.md

+ 3 - 0
CLAUDE.md

@@ -91,6 +91,9 @@ docker run -d --name svapi-dev -p 8080:8080 -e SMARTBOTIC_VECTORAPI_KEY=dev svap
   **THE COMING FIX IS SAFE FOR US, and the reason is worth keeping because it is not obvious: NOTHING queries a credential collection by value.** The database asked before releasing, because full-value encryption uses a random IV, so a FILTER on an encrypted field never matches and an INDEX on one indexes ciphertext. Audited: `KeyStore::lookup()` scans an IN-MEMORY snapshot (`k.key == key`) loaded by a `find()` carrying only a limit, so authentication is a full read compared in process - it never asks the database to match a key. Settings and project secrets are addressed by DOCUMENT ID throughout (the secret's NAME is its id). The global settings document is told apart from the per-project ones by `p.project.empty()`, evaluated in our process after a full read, which LOOKS like a value query and is not. `opts.filters` is set in exactly three places service-wide - `vector_search.cpp`, `function_run_log.cpp` (`function_id`) and `host_context.cpp` (`ctx.collections`) - none of them a credential collection, and no index is declared on any of the three.
   **The adjacent hazard that WOULD break: `vectorapi_function_runs` is filtered by `function_id`.** It, the job queue, the function registry and the collection registry are all deliberately `encrypted=false`, and user collections are always created `encrypted=false` (`handlers/collections.cpp:74`) so a user's index is never over ciphertext. If anyone ever flips the flag on those, that filter stops matching silently.
   **THE FLAG WAS ASKED FOR AND NEVER APPLIED, AND THIS NOTE USED TO CLAIM THE OPPOSITE.** It said our startup calls `createCollection(..., encrypted=true, ...)` "unconditionally", so the three stores would "repair themselves on the next restart". Both halves were false. Every call site was guarded by `if (!getCollectionInfo(...).has_value())`, so the create was skipped for a collection that ALREADY EXISTED - which is exactly the case that needed it, because the collection had been created implicitly by an earlier write, with the default flag of `false`. Measured on the dev database: `vectorapi_keys` and `vectorapi_settings` report `encrypted: no` while the code reads as though it had asked for `true`, and only `<project>:vectorapi_secrets` (a later store, whose collection our code did create) reports `yes`. A guard that skips the repair in precisely the broken case is worse than no guard, and the suite could not see it because the flag test deliberately points each store at a FRESH collection name - correct for testing the creating path, and the reason it was blind to this one.
+  **THAT UNCONDITIONAL CALL COSTS A SECOND WINDOW ON 2.17.x, AND IT MUST BE PLANNED RATHER THAN DISCOVERED.** Under 2.17.x, re-issuing `createCollection(existing, encrypted=true)` performs the ONLINE encryption upgrade when it changes anything - and a collection reaching encryption through the bare-alias option merge still has its OWN flag unset, so our call does change something: it moves the flag where it belongs. That requests a RESEAL at the next boot, and the reseal **compacts every project**, which is the 167-second term measured above. Seen for real: the dev box's restart onto 2.17.1 took ~3.5 minutes instead of ~15 seconds, caused by our own bootstraps having flipped those flags under 2.17.0.
+  So the prod sequence has THREE steps and two compactions, not one: upgrade the database (first-boot migration, 190 s measured), start vectorapi so the bootstraps move the flags, then **restart the database inside the SAME window** so the reseal happens while everyone is expecting an outage. Skip that last step and a routine restart weeks later silently takes three minutes with no visible cause - the same shape as the `/etc` mode 0751 trap below, and the reason "under 10 minutes" is the right thing to ask consumers to approve for that window rather than three.
+  **Today's release is unaffected**: prod runs 2.14.0, where the upgrade branch does not exist and the re-call is a plain no-op. The cost arrives only with 2.17.x.
   **So `KeyStore::bootstrap` and `SettingsStore::bootstrap` now call `createCollection` UNCONDITIONALLY**, which puts the flag on the collection we actually read and write. Read the correction two paragraphs down before relying on the reasoning that motivated it. Re-issuing it against an existing collection switches encryption ON (never off), encrypting the stored rows and their history; against an older server it is a no-op, so it is safe on every version we support. Verified safe before shipping, not reasoned about: `MemoryStore::createCollection`'s existing-collection path only acts on a `vectorDimension > 0` and on `defaultTtlSeconds`, and we pass 0 for every option but the flag, while `upgradeEncryptionOptions` only ever flips false to true and appends sensitive fields. `BootstrapAsksForEncryptionOnAnAlreadyEXISTINGCollection` pins it with a CONTROL collection that is never bootstrapped, so the assertion cannot pass for an unrelated reason, and it asserts the pre-existing row survives the re-call.
   **`ProjectSecretStore::ensure` deliberately KEEPS its guard**, and the difference is the call path rather than the intent: it runs on every secret READ (the runner builds `ctx.env` through it) and `CreateCollection` is REFUSED by a read-only database, so making it unconditional would turn every function run into a 503 during read-only recovery - trading a live outage for a hardening step that collection does not need.
   **What database 2.17.0 actually does, confirmed from their source and then independently by them:** collection-level `encrypted=true` encrypts EVERY non-null value in the collection whatever the field is called (`isEncryptedPath` returns true for any non-empty path when the rule is whole-document). Strings become `$ENC$...`, and numbers, bools, objects and arrays become `$ENCJ$...` and read back with their original type. The built-in sensitive-name patterns match a WHOLE path segment, so `openai_api_key` does NOT match `api_key` - which is why the collection flag, not the field name, is the only thing that was ever going to protect us. Grep a data file with `grep -F`: a bare `$ENC$` pattern is a regex anchor and matches nothing.