Преглед изворни кода

feat(prod): cap history on the operational collections - 316 MB down to 117 MB

Ran after a full data+config backup, copied off the box to a second
machine and verified there: sizes match and the archive reads with all
five target files present.

The registry went 197.56 MiB to 8.85 MiB in 3389 ms. Under 6 seconds of
lock in total across five collections, each confined to its own
collection, document counts unchanged everywhere, and the job queue was
verified still enqueueing afterwards. Versioning stays ON for the
registry with a cap of 5, so restore-after-delete still works for a
function definition - the reason it is treated differently from the
queue.

A cap reclaims in proportion to how many old versions exist and the
spread is two orders of magnitude: the run log gave back 1.5% because a
run row is written once, the registry 95.5% because 18 rows had each
accumulated about a thousand full copies of their script. File size
alone does not predict which you have.

The real fix was the consumer's and it is at the right layer: their
deploy script PUT all 18 functions every time, so a one-line change to a
shared library wrote 18 whole rows, about 2.9 MB per deploy. It now
compares content_hash and pushes only what changed. The prune was the
treatment; that is the cure. content_hash is the right field for it,
because a match is OUR statement that we hold their bytes rather than
the 200 their own PUT returned.

Verified against their corrected condition rather than a count: 18
present, 17 enabled, event-reminders the only disabled one, all 18 with
a non-empty content_hash, and a full document body read back out.
fszontagh пре 1 месец
родитељ
комит
68f6d97b8a
1 измењених фајлова са 14 додато и 0 уклоњено
  1. 14 0
      CLAUDE.md

+ 14 - 0
CLAUDE.md

@@ -247,6 +247,20 @@ docker run -d --name svapi-dev -p 8080:8080 -e SMARTBOTIC_VECTORAPI_KEY=dev svap
   **MOVING THE CLIENT PIN WIDENS THE OBSERVABLE SLUG SET ON EVERY READ ROUTE, AND IN ONE CASE IT FIXES A RETRY-FOREVER BUG.** `translate()` branches on NINE server reasons - `delete_restricted`, `document_too_large`, `duplicate_id`, `encrypted_field`, `memory_pressure`, `message_too_large`, `missing_parent`, `outcome_unknown`, `read_only` - and forwarding the reason on READ calls (`find`, a filtered search) is a 2.17.0 CLIENT change, so on anything older every one of those collapses on a read into whatever the bare code maps to. **The important instance: `ResourceExhausted` with no reason falls through to 429 `rate_limited`, which says WAIT AND RETRY. A document grown too large to read back (repeated patches, measured) is exactly that case, and no amount of waiting fixes it.** With the reason it becomes 422 `message_too_large`, which is permanent and honest. So the pin move does not merely add `encrypted_field`: it corrects a read failure that is currently reported as retryable when nothing can ever succeed.
   **Tell consumers the GENERAL form before the pin moves, not the one slug.** A consumer with an exhaustive code map plus a default branch starts meeting codes it has never seen, and the dangerous default is not "unknown error" but "retry" - a caller that retries its default would begin retrying a permanent verdict. The consumer who raised this is inert by construction (their retryable set is keyed on STATUS, not on code, which is the design that makes a widening slug set safe), and that is the right question to ask each consumer: not "do you handle this slug" but **"what does your default branch do, and is your retry decision keyed on status or on code"**. Filing this as "additive, becomes live" understates it in the asymmetric direction - the same understatement I made about `filter_mode`, where being wrong cost an opportunity rather than a retry loop.
   **IT IS DORMANT UNTIL THE CLIENT MOVES TO 2.17.x, AND I TOLD A CONSUMER IT WAS LIVE.** Measured against a 2.17.1 server with our shipped 2.14.0 client: a filter on an encrypted field comes back as slug **`validation`**, not `encrypted_field`, even though the server's message arrives intact. The reason is the client, not our mapping - carrying the server's error reason on READ calls (`find`, `similaritySearch`) is a 2.17.0 client change, so on 2.14.0 the reason is simply absent and `translate()` falls through to `validation`. So the slug shipped in 0.12.0 cannot fire on prod today. It starts working when `DB_CLIENT_VERSION` moves, which is the same dependency as the index-route mapping, and it covers the FILTER path too rather than only indexes.
+- **DONE 2026-10-15: THE OPERATIONAL COLLECTIONS ARE CAPPED, 316 MB OF HISTORY DOWN TO 117 MB.** Run with `smartbotic-db-cli configure <coll> [--versioning on|off] [--max-versions N]` (client 2.18.1), after a full data+config backup copied OFF the box and verified there. Measured, each collection in turn:
+
+  | collection | before | after | time | setting |
+  |---|---|---|---|---|
+  | `justice-pa:vectorapi_function_runs` | 8.45 MiB | 8.32 MiB | 694 ms | versioning off, cap 1 |
+  | `default:vectorapi_function_jobs` | 24.69 MiB | 12.44 MiB | 1840 ms | versioning off, cap 1 |
+  | `mailtest:vectorapi_functions` | 0.08 MiB | 0.04 MiB | 3 ms | cap 5 |
+  | `toloajto:vectorapi_functions` | 0.05 MiB | 0.05 MiB | 2 ms | cap 5 |
+  | `justice-pa:vectorapi_functions` | **197.56 MiB** | **8.85 MiB** | 3389 ms | cap 5, versioning left ON |
+
+  Under 6 seconds of lock in total, each confined to its own collection; document counts unchanged everywhere; the job queue was verified still enqueueing afterwards. **The registry keeps versioning ON with a cap**, so restore-after-delete still works for a function definition - that is the whole reason it is treated differently from the queue.
+  **A CAP RECLAIMS IN PROPORTION TO HOW MANY OLD VERSIONS EXIST, AND THE SPREAD IS TWO ORDERS OF MAGNITUDE.** Same command, same box, same minute: the run log gave back **1.5%** because a run row is written once and never updated, while the registry gave back **95.5%** because 18 rows had each accumulated roughly a thousand full copies of their script. File size alone does not predict it - the run log and the registry both looked like "a few MB of history" at the start of the week.
+  **THE REAL FIX WAS THE CONSUMER'S, AND IT IS AT THE RIGHT LAYER.** The registry grew because their deploy script PUT all 18 functions every time, so a one-line change to a shared library wrote 18 whole rows, script included - about 2.9 MB per deploy. They changed it to read `GET /functions` once and skip any function whose stored `content_hash` already equals the sha256 of the bytes it would send. So the prune was the treatment and their change is the cure; without it the 188 MiB would have been partly re-spent by the next deploy. **`content_hash` is exactly the right field for that comparison: it is the hash of the source WE stored, so a match is our statement that we hold their bytes - strictly better evidence than the 200 their own PUT returned, which only says the write was accepted.**
+  Their verification note is worth keeping too: their first test of the skip used a COMMENT-only edit, got "0 pushed" and briefly read it as broken. esbuild strips comments, so the bundle was byte-identical and there was genuinely nothing to push. **A skip that works and a skip that is broken produce identical output when the input is identical**, so the only informative test is one where the two must differ.
 - **WE CREATE EVERY COLLECTION WITH UNLIMITED VERSION HISTORY, and most of prod's history is therefore our own operational churn.** `createCollection(..., maxVersions = 0, ...)` and 0 means "keep unlimited history", not "keep none" - so every code push to a function has appended a full copy of the script forever. Measured on prod: `justice-pa:vectorapi_functions.hlog` 114 MB (the function REGISTRY, a handful of rows), `default:vectorapi_function_jobs.hlog` 14 MB, `justice-pa:vectorapi_function_runs.hlog` 4.7 MB - about 133 MB of the box's 192 MB of history, roughly 69%, none of it data anyone would ever read. A consumer's 47 MB of `kb_chunks` history is retention of the same kind on a collection that has not changed size in days.
   **IT IS A DISK AND GROWTH PROBLEM ONLY. I claimed history bytes dominate the at-rest encryption migration, and measured on prod's own data they do not matter to it at all.** That claim was inherited from the database team's sizing model and amplified by me, and one measurement would have settled it before I asked them for a boot mode and asked a consumer to weigh an escape hatch. Measured with 2.17.1 on a copy of prod's data directory (1.6 GB, 24753 docs, 65 collections, prod's own config, 900 MB cap): **190 seconds total**, of which the ENCRYPTION pass was **13 seconds** (24733 rows scanned, 19 encrypted, 26 history versions) and **compaction was 176 seconds, 167 of them novitax's single 339 MB env**. The seal was under a second.
   **The structural reason, which is what makes it general rather than a quirk of this dataset: the migration only rewrites history belonging to ENCRYPTED collections.** Our 114 MB `vectorapi_functions.hlog` and the consumer's 47 MB `kb_chunks.hlog` sit on `encrypted=false` collections, so they are scanned and left alone - `encryption-verify` reports `novitax:kb_chunks` with `history 18648, plaintext-required 0`. So the migration's cost tracks **the largest project's `data.mdb`, through compaction**, and almost nothing else. An operator with a huge history and a small env would read the old model and expect the opposite of what happens.