CultLib/src/GameCult.Caching/Contracts/cultcache-schema-compatibility.md
Meta Vens dbc87b9447 C# core: variant records, top-level Set overrides, resolution in the admitting hold
A variant record stores [baseKey, overrides[]] in record slot 4 and an empty
payload; the cache resolves it against its base inside the hold that admits it or
any record on its base chain, and re-resolves dependents in the same Apply, so a
base change reaches its variants with contiguous sequences and no stamp. Plain
Upsert at a variant key is refused (Flatten is the explicit way); cycle, missing
base, different concrete type, evicted base with survivors and a name that
shadows the base are refused naming the keys. Overrides of a dropped slot or an
undecodable value are ignored with a warning and reported as compatible drift.

The store writes cultcache.store.v2 only when it holds a variant and reads v1 and
v2. The codec pair CultInspectorModel took becomes CultCodec, shared with the
cache, which needs it to clone documents and overlay override values. The
directory store, CultMesh single-file reads and CultNet row building refuse a
variant by name until their own cuts.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-29 03:51:53 +02:00

4.9 KiB

CultCache Schema Compatibility

CultCache compatibility is slot-driven and explicit. The cache will not pretend that "same-ish looking" schemas are fine just because the names feel friendly.

Semantic identity

Two CultCache document schemas are considered the same shape when they agree on:

  • schemaName
  • schemaVersion
  • member slots
  • member persisted type names
  • reference/cardinality semantics
  • target schema names for references
  • name/index lookup participation

The canonical fixtures currently exercised in C# are:

  • tests.named_entry
    • schema id: sha256:e7b97801b94190f3159012ede45b0069bb09ebf7920f7432c971bc86a0e08de8
    • content hash: sha256:23150930afcc1d84f0cb3012ccc2debcb9b4685f62083033bbaab0083f1e832e
  • tests.reference_holder
    • schema id: sha256:bd85064961cc74565fb73e3ccbc4217cfba4dc4869e365a08bea4f704739bd8f

See GameCult.Caching.Tests/BackingStoreTests.cs for the canonical fixture documents and the expected receipts.

Slots

A member's slot is its MessagePack [Key(n)] integer. MessagePack's [Key] and [IgnoreMember] are the single slot authority, so GameCult.Caching depends on MessagePack.Annotations. Payloads are written by MessagePackSerializer, so the registry refuses a document type without [MessagePackObject] and a non-public document type without [MessagePackObject(AllowPrivate = true)]. A member's type name is the CLR full name; a nested type is written Outer+Inner. The registry also refuses a public member without [Key] or [IgnoreMember] (get-only properties included; MessagePack throws on them), a string [Key], two members sharing a slot (including new-hidden members), a hidden persisted member, and an override whose [Key] or [IgnoreMember] differs from its base declaration's (MessagePack reads the base declaration).

Visibility follows MessagePack's rule: the registry accepts what MessagePack round-trips and refuses what it would silently lose. The registry persists every keyed public field and property, get-only ones included, because MessagePack writes them all. A [Key] on a non-public member without AllowPrivate is refused because MessagePack skips that member. With AllowPrivate, MessagePack also reads non-public members, so every non-public instance field or property must be [IgnoreMember].

A class needs a constructor MessagePack can call: the [SerializationConstructor] if one is marked, otherwise the longest constructor (public unless AllowPrivate) whose every parameter takes the keyed member at its position ([Key(0)], [Key(1)], ...). A parameterless constructor always qualifies; a primary constructor usually does not. MessagePack reads a member back through its setter, or through that constructor when the member's slot is below the constructor's parameter count. A setter counts when it is public, or any setter (private, internal, init-only) under AllowPrivate; a readonly field counts as writable under AllowPrivate only. A keyed member MessagePack cannot read back either way is refused: a get-only property, a readonly field without AllowPrivate, or a property whose setter is non-public without AllowPrivate, when the constructor does not fill it.

Which schema a record carries

A record is written under the schema of its runtime type. Writing a derived document through a base-typed handle or generic parameter (UpsertAsync<Gear> with a Weapon) persists the Weapon schema, and the record reloads as a Weapon. A runtime type without [CultDocument] is refused.

Typed lookups and watches (Get<T>, GetAll<T>, GetByName<T>, GetByIndex<T>, GetGlobal<T>, Watch<T>) match every record whose runtime type is assignable to T. A single-result lookup with several candidates throws.

Soft-migratable drift

CultCache accepts compatible drift only when the local reader can still map the persisted slots honestly:

  • persisted schema id differs, but the embedded catalog points to a compatible local schema id
  • persisted slot is missing locally and can be ignored
  • local slot is missing in persisted data and can fall back to the local default value

The cache emits a typed migration report with:

  • exact vs compatible-drift classification
  • ignored extra slots
  • defaulted missing slots
  • warning codes and messages

Hard rejection

CultCache rejects persisted schemas when any slot changes in a way that would lie about the payload:

  • type change
  • reference vs value change
  • one vs many change
  • target schema change
  • name/index lookup semantics change
  • missing embedded catalog entry
  • no compatible local schema candidate

This is not negotiable. Better a loud refusal than quiet bit-rot.

Variant overrides

A variant's override drifts exactly as the same member would on a plain record: an override naming a slot the type no longer has, or holding a value that no longer decodes into the member's type, is ignored with a warning naming the variant and the member, and the variant inherits the base value. There is no separate rule for variants.