feat(8.0): reserved-field contract — one canonical location, typed prevention, unified read/write

Brainy-owned field names (noun/verb, subtype, createdAt, updatedAt,
confidence, weight, service, data, createdBy, _rev) now have exactly one
home — top level — enforced by three layers driven from a single source of
truth, src/types/reservedFields.ts (RESERVED_ENTITY_FIELDS /
RESERVED_RELATION_FIELDS, exported):

1. Compile time — AddParams/UpdateParams/RelateParams/UpdateRelationParams
   metadata (and the transact() ops that extend them) reject a literal
   reserved key as a TypeScript error while keeping generic T ergonomics
   (typed bags, untyped brains, index-signature shapes, and a documented
   exemption for T-declared reserved keys). Pinned by @ts-expect-error
   type tests run under vitest typecheck mode on every unit run.

2. Write time — the 7.x update() remap is ported to 8.0 and extended to
   every write path: add/update/relate/updateRelation, their transact()
   mirrors, and db.with() overlays. User-settable fields lift to their
   dedicated param (top-level wins when both are supplied — closes the 7.x
   trap where update({metadata:{confidence}}) silently no-oped), and
   system-managed fields drop with a one-shot warning naming the right
   path. A remapped subtype satisfies subtype-pairing enforcement exactly
   like a top-level one.

3. Read time — every storage combine goes through one canonical hydration
   helper (hydrateNounWithMetadata / hydrateVerbWithMetadata over
   splitNoun/VerbMetadataRecord), so reserved fields surface ONLY top-level
   and entity/relation.metadata carry ONLY custom fields on live reads,
   batch reads, paginated listings, getRelations by source/target, streamed
   verbs, and historical asOf() materialization alike.

Read-path echoes found and fixed (previously the full stored record —
including the verb type key — leaked inside metadata): noun pagination,
verb pagination, getVerbsBySource/ByTarget (adjacency + shard fallback),
getVerbsBySourceBatch (which also dropped subtype/data), and the
filesystem verb stream. getRelations() results now surface
confidence/updatedAt top-level via verbsToRelations, updateRelation() no
longer erases service/createdBy, relate() persists its top-level
confidence/service params, and the dead convertHNSWVerbToGraphVerb echo
path is deleted. Import paths (CLI extract, deduplicator, coordinators,
neural import) write confidence through the dedicated param instead of the
bag. UpdateRelationParams is now exported from the package root.

Documented for consumers in docs/concepts/consistency-model.md ("Reserved
fields") and RELEASES.md. Regression tests ported from the 7.x fix and
extended to the full 8.0 contract (17 runtime tests + 41 type-level
assertions); full unit suite 1427/1427, db-mvcc integration 24/24.
This commit is contained in:
David Snelling 2026-06-11 13:12:50 -07:00
parent c44678390e
commit 970e08c466
18 changed files with 1688 additions and 452 deletions

View file

@ -5,7 +5,7 @@ public: true
category: concepts
template: concept
order: 4
description: The exact guarantees behind Brainy's Db API — snapshot isolation, atomic transactions, two levels of compare-and-swap, time travel, retention, snapshots, and crash recovery.
description: The exact guarantees behind Brainy's Db API — snapshot isolation, atomic transactions, two levels of compare-and-swap, time travel, retention, snapshots, crash recovery, and the reserved-field contract.
next:
- guides/snapshots-and-time-travel
- guides/optimistic-concurrency
@ -251,6 +251,55 @@ Two rules keep snapshots honest:
self-contained **read-only** store with the full query surface, including
vector search.
## Reserved fields
Some field names belong to Brainy, not to your metadata. They live at **top
level** on every entity and relationship, have dedicated write paths, and may
never appear inside a `metadata` bag:
| Entities (nouns) | Relationships (verbs) | Canonical write path |
|---|---|---|
| `noun` | `verb` | the `type` param of `add()` / `relate()` |
| `subtype` | `subtype` | the `subtype` param |
| `confidence` | `confidence` | the `confidence` param |
| `weight` | `weight` | the `weight` param |
| `service` | `service` | the `service` param (fixed at create time) |
| `data` | `data` | the `data` param |
| `createdBy` | `createdBy` | the `createdBy` param of `add()` (system-managed on verbs) |
| `createdAt`, `updatedAt`, `_rev` | `createdAt`, `updatedAt`, `_rev` | system-managed (`ifRev` for CAS) |
The canonical machine-readable lists are exported as
`RESERVED_ENTITY_FIELDS` and `RESERVED_RELATION_FIELDS` (defined in
`src/types/reservedFields.ts`, the single source of truth). Three layers
enforce the contract:
1. **Compile time** — every `metadata` param (`add`, `update`, `relate`,
`updateRelation`, and the matching `transact()` operations) rejects a
literal reserved key as a TypeScript error.
2. **Write time** — untyped (JavaScript) callers that pass one anyway are
normalized: user-settable fields (`confidence`, `weight`, `subtype`, and
`service`/`createdBy` at create time) are remapped to their dedicated
param — **top-level wins** when both are supplied — and system-managed
fields are dropped with a one-shot warning naming the correct write path.
`update({ metadata: { confidence: 0.9 } })` therefore behaves exactly
like `update({ confidence: 0.9 })`.
3. **Read time** — every read path (`get`, `find`, `search`,
`getRelations`, batch reads, and historical `asOf()` materialization)
surfaces reserved fields **only at top level**: `entity.metadata` and
`relation.metadata` contain only your custom fields, always.
```typescript
const id = await brain.add({
type: 'document', subtype: 'invoice',
data: 'Invoice #42', confidence: 0.95, // reserved → top-level params
metadata: { customer: 'acme', total: 129.5 } // custom fields only
})
const entity = await brain.get(id)
entity.confidence // 0.95 — top level
entity.metadata // { customer: 'acme', total: 129.5 }
```
## What is not guaranteed
Stated plainly, so nothing surprises you in production: