ソースを参照

docs(export): name the base64 vector field, and specify the digest

Two documentation errors found by the first real consumer of the route,
on their first run.

THE ROW SHAPE WAS WRONG FOR BASE64 MODE. llms.txt showed
`{...,"vector_state":"present","vector":[...]}` for every row, but
in base64 mode the field is `vector_b64`. Their verification script did
exactly what the documentation said and died on b64decode(None) - a row
reporting vector_state present with no vector key. For a few seconds
they thought they had found data loss in the first collection ever
exported, which is the worst possible confusion for this feature: the
documented shape made a correct export look like the exact failure
`vector_state` exists to report. Both names are now given wherever the
row shape appears, with that failure mode spelled out.

THE DIGEST WAS UNVERIFIABLE. content_sha256 was described as being over
the id and raw vector bytes, which is true but not a recipe: they tried
two reasonable constructions, got neither, and correctly refused to
report it as a mismatch - a guess that does not match proves nothing
about the data, only that the byte layout is unknown. Their position
was that the digest was 'a value I can only echo back' rather than a
check, which left the id set as the only independent verification in
their backup.

Now specified exactly: one rolling SHA-256 over every row in stream
order, feeding id (UTF-8, not length-prefixed) || 0x1F || the vector as
float32 little-endian (zero bytes when absent) || 0x1E. The separators
are called out as load-bearing, and it is stated that this is NOT a
per-row hash combined afterwards - which was one of their two guesses.

Verified before documenting rather than after: a script implementing
the recipe above, run against the live 12447-row production export,
reproduces the trailer's value exactly. Writing down a construction I
had not proven would have repeated the day's pattern.
fszontagh 1 ヶ月 前
親
コミット
74fdd31b73
2 ファイル変更、5 行追加、2 行削除
  1. 4 1
      api/llms.txt
  2. 1 1
      api/openapi.json

+ 4 - 1
api/llms.txt

@@ -78,7 +78,10 @@ Arbitrary JSON CRUD under a `kind=json` collection.
 
   **Conditional writes: `?if_version=N`** on PUT and PATCH. The write applies only if the document is still at version `N`; otherwise **409** `version_conflict` with `error.details.expected_version` and, when it can be read, `error.details.current_version`. Read `_version` from a GET or from a `?filter=` listing, write with `?if_version=` that value, and retry from a fresh read on 409. `N` must be a positive integer (**422** `validation` otherwise; versions start at 1, so `if_version=0` is not "must not exist"). A conditional write never creates: `?if_version=` on an absent document is **404**, on PUT as well as PATCH. On PATCH it cannot be combined with `?ttl_seconds=` (**422** `conditional_unsupported`): the version-checked write has no expiry field, and silently dropping one of the two would be worse than refusing. It IS available on a `kind=vector` collection - a conditional PATCH is a server-side MERGE and preserves the stored embedding (measured). What is refused there is a whole-document **replace**: see `PUT` below.
 - `POST  /api/v1/projects/{project}/collections/{name}/documents/{id}/increment` `{field: n, ...}` (or `{data:{...}}`) - **Atomic increment.** Adds each number to the named field in ONE locked server-side read-modify-write, so concurrent increments cannot lose each other. Dotted keys address nested fields (`counts.sent`). Returns **200** `{id, values}` where `values` holds the NEW value of every field you named. An absent or null field counts as 0. Integer + integer stays an integer (overflow is refused); anything else becomes a double. A field holding a non-number is **422** `validation` and **nothing at all is applied** - the whole set applies or none of it does. Needs the `update` grant (it is a write, so a read-only scope gets **403**); it is a separate route rather than a flag on PATCH because `{"calls":1}` means "set calls to 1" on PATCH and "add 1 to calls" here.
-- `GET   /api/v1/projects/{project}/collections/{name}/export` - **Streaming backup, including stored vectors.** Answers **NDJSON** (`application/x-ndjson`): one JSON object per line, so the LINE is the unit of recovery and a truncated download loses the last row rather than the file. First line `{type:'header', collection, snapshot_at, vector_dimension, vector_dtype, rows_at_snapshot, batch_rows_cap, max_stream_seconds, include_vectors, vector_format}` (`snapshot_at` is server epoch seconds - "as of when" is the first question anyone asks of a backup, and a filesystem mtime does not survive a copy); then `{type:'row', id, document, vector_state, vector}` per document; then `{type:'trailer', rows_sent, last_id, complete, content_sha256}`. **`content_sha256` is over each row's id and its RAW VECTOR BYTES, not over the emitted text**, so it is IDENTICAL whichever `vector_format` you asked for - you can export as base64, restore, re-export as an array and still compare digests. It is omitted rather than empty if it could not be computed. **`rows_sent == rows_at_snapshot` with `complete: true` is your checksum** - one uninterrupted call is an exact snapshot with no row skipped or repeated, in id order. Needs BOTH the `list` and `read` grants: `list` alone could already learn the ids and must not get the contents, and `read` alone must not get an enumeration.
+- `GET   /api/v1/projects/{project}/collections/{name}/export` - **Streaming backup, including stored vectors.** Answers **NDJSON** (`application/x-ndjson`): one JSON object per line, so the LINE is the unit of recovery and a truncated download loses the last row rather than the file. First line `{type:'header', collection, snapshot_at, vector_dimension, vector_dtype, rows_at_snapshot, batch_rows_cap, max_stream_seconds, include_vectors, vector_format}` (`snapshot_at` is server epoch seconds - "as of when" is the first question anyone asks of a backup, and a filesystem mtime does not survive a copy); then `{type:'row', id, document, vector_state, vector|vector_b64}` per document - **the vector field is named `vector` (a JSON array) with `vector_format=array`, and `vector_b64` (base64 of float32-little-endian) with `vector_format=base64`.** Handle BOTH names: a consumer who read only `vector` and exported as base64 saw a row reporting `vector_state: present` with no `vector` key, which looks exactly like the data loss `vector_state` exists to report; then `{type:'trailer', rows_sent, last_id, complete, content_sha256}`. **`content_sha256` is over each row's id and its RAW VECTOR BYTES, not over the emitted text**, so it is IDENTICAL whichever `vector_format` you asked for - you can export as base64, restore, re-export as an array and still compare digests. It is omitted rather than empty if it could not be computed.
+  **The exact construction, so you can recompute it rather than only echo it.** ONE rolling SHA-256 over every row in stream order, feeding exactly:
+  `id` (UTF-8 bytes, not length-prefixed) || `0x1F` || the vector as float32 little-endian (ZERO bytes when the row has no vector) || `0x1E`
+  The two separators are load-bearing: without them `"ab"+"c"` and `"a"+"bc"` would hash alike. It is NOT a hash per row combined afterwards, and it is NOT over the vector alone. Verified against a 12447-row production export: the recipe above reproduces the trailer's value exactly. **`rows_sent == rows_at_snapshot` with `complete: true` is your checksum** - one uninterrupted call is an exact snapshot with no row skipped or repeated, in id order. Needs BOTH the `list` and `read` grants: `list` alone could already learn the ids and must not get the contents, and `read` alone must not get an enumeration.
   **Params:** `include_vectors` (default **true** for a vector collection, and refused **422** `not_vector` on a json one), `vector_format` (`array` default, or `base64` for float32-little-endian). **Both are BIT-EXACT** - tested over 200k float32 values including denormals and the extremes - so choose on size, not on fidelity. `array` is the default because it restores directly (`_vector` on a document write takes exactly that shape) but it is ~3.7x larger: nlohmann prints a float32 widened to double with shortest-round-trip for DOUBLE, giving 16 digits where float32 needs 9, so it is verbose for precisely the same reason it is exact. Measured on 12447 rows at dim 4096: array 963 MiB, base64 261 MiB. **DO NOT ASK FOR GZIP ON VECTOR DATA.** Responses are compressible and the export inherits it, but measured at level 6 an array export costs ~121 s of SERVER CPU to save ~68 s of download, which is a net loss and a real cost on a shared host; base64 of float data compresses only 1.35x. Send `Accept-Encoding: identity` for a vector export. Gzip the json collections happily - document JSON compresses ~3.9x), `after_id` (resume), `batch_rows` (clamped, never refused).
   **`vector_state` is `present`, `none`, `missing` or `unknown`,** and a row that is not `present` carries NO `vector` field at all rather than an empty one - so a restorer cannot mistake "we do not have it" for "it is empty". **`missing` means the row HAD a vector and the entry is gone** (real data loss) and `unknown` means the row predates the database's marker, so neither should be restored as a plain document: re-embed from the source text instead, or you will store a row that reads back fine and can never be found by a search.
   **To restore:** replay each row as `PUT .../documents/{id}` with `document` plus `_vector` set to `vector`. The restored row gets FRESH metadata (`_version` restarts at 1, `_created_at` is the restore time); the originals are still in the exported `document` if you need them as data.

+ 1 - 1
api/openapi.json

@@ -4730,7 +4730,7 @@
     "/api/v1/projects/{project}/collections/{name}/export": {
       "get": {
         "summary": "Stream a collection as NDJSON, including stored vectors",
-        "description": "A restorable backup. One JSON object per line (header, then a row per document, then a trailer), so the line is the unit of recovery and a truncated download loses the last row rather than the file. `rows_sent == rows_at_snapshot` with `complete: true` is the checksum: one uninterrupted call is an exact snapshot, in id order, with no row skipped or repeated. Requires BOTH the `list` and `read` grants. A row whose `vector_state` is not `present` carries no `vector` field at all, so a restorer cannot mistake an absent vector for an empty one; `missing` is real data loss and `unknown` predates the database's marker, and neither should be restored as a plain document. Restore by replaying each row as `PUT .../documents/{id}` with `_vector` set to `vector`. Resuming with `after_id` starts a new snapshot strictly after that id: never duplicates, but can miss a row inserted during the gap. A failure mid-stream emits a final `{\"type\":\"error\"}` line rather than stopping silently, because a stream that stops is indistinguishable from one whose trailer was lost. The header carries snapshot_at (server epoch seconds) and the trailer a content_sha256 over each row id and its raw vector bytes - identical across vector_format, so an export, restore and re-export in the other format can be compared. Both formats are bit-exact; array is ~3.7x larger because a float32 widened to double prints 16 digits where 9 would do. Do not request gzip for vector data: measured, an array export costs ~121s of server CPU to save ~68s of download.",
+        "description": "A restorable backup. One JSON object per line (header, then a row per document, then a trailer), so the line is the unit of recovery and a truncated download loses the last row rather than the file. `rows_sent == rows_at_snapshot` with `complete: true` is the checksum: one uninterrupted call is an exact snapshot, in id order, with no row skipped or repeated. Requires BOTH the `list` and `read` grants. A row whose `vector_state` is not `present` carries no `vector` field at all, so a restorer cannot mistake an absent vector for an empty one; `missing` is real data loss and `unknown` predates the database's marker, and neither should be restored as a plain document. Restore by replaying each row as `PUT .../documents/{id}` with `_vector` set to `vector`. Resuming with `after_id` starts a new snapshot strictly after that id: never duplicates, but can miss a row inserted during the gap. A failure mid-stream emits a final `{\"type\":\"error\"}` line rather than stopping silently, because a stream that stops is indistinguishable from one whose trailer was lost. The header carries snapshot_at (server epoch seconds) and the trailer a content_sha256 over each row id and its raw vector bytes - identical across vector_format, so an export, restore and re-export in the other format can be compared. Both formats are bit-exact; array is ~3.7x larger because a float32 widened to double prints 16 digits where 9 would do. Do not request gzip for vector data: measured, an array export costs ~121s of server CPU to save ~68s of download. content_sha256 construction, so it can be recomputed: one rolling SHA-256 over every row in stream order, feeding id (UTF-8, not length-prefixed) || 0x1F || the vector as float32 little-endian (zero bytes if none) || 0x1E. Not a per-row hash combined afterwards. Verified to reproduce the trailer value on a 12447-row production export.",
         "tags": [
           "documents"
         ],