Write an unset CultRecordRef as "" in every position

452f928 made nil the unset wire form, including dictionary keys: a
Dictionary<CultRecordRef<H>, float> holding the empty key encoded it as C0.
Python msgpack refuses nil map keys under strict_map_key, its default, so
cultcache-py cannot decode such a record. Nil cannot be the one canonical
form; "" can, and every runtime reads it.

The writer always writes "". The reader still takes nil as unset, because
452f928/b85a828 stores and Aetheria's Cut 9 import wrote nil; such a record
converts to "" on its next save and is byte-stable after that. The
store-composition contract names "" for value and key position, requires
readers to accept nil, and tells other runtimes to write "".

The tests pin payload bytes, so each claim is consequential: an unset ref
writes "", reloads through a fresh cache and resaves byte-identical; a
nil-written legacy payload reads as unset and resaves as ""; a ref-keyed
dictionary with an empty key writes "", reads back unset and resaves
byte-identical. The nil writer fails every byte assertion, a reader taking
nil as a set key fails the legacy pair, and a reader taking "" as a set key
fails the read-back assertions.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-09-14 21:34:23 +02:00
commit a92381e589
3 changed files with 59 additions and 42 deletions

View file

@ -3,15 +3,12 @@ using MessagePack.Formatters;
namespace GameCult.Caching;
// An unset reference (the empty key) is nil on the wire and default(CultRecordRef<T>) in memory; a "" read from older
// stores is the same unset reference, so load then save is byte-stable.
// An unset reference (the empty key) is "" on the wire in every position, map keys included (other runtimes refuse nil
// map keys), and default(CultRecordRef<T>) in memory. Nil, written by 1.0.58 stores, reads as unset and saves as "".
public sealed class CultRecordRefFormatter<T> : IMessagePackFormatter<CultRecordRef<T>>
{
public void Serialize(ref MessagePackWriter writer, CultRecordRef<T> value, MessagePackSerializerOptions options)
{
if (value.Key.Value.Length == 0) writer.WriteNil();
else writer.Write(value.Key.Value);
}
public void Serialize(ref MessagePackWriter writer, CultRecordRef<T> value, MessagePackSerializerOptions options) =>
writer.Write(value.Key.Value);
public CultRecordRef<T> Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options)
{

View file

@ -239,13 +239,15 @@ another runtime need not obey the `storedAt` rule.
## Record references
A record reference is its target's key string. An unset reference, the empty
key, is **nil** on the wire, never `""`. A reader takes nil and `""` as the same
unset reference and writes it back as nil, so loading and re-saving an unchanged
record is byte-stable. In C# the unset reference is `default(CultRecordRef<T>)`,
equal to one built from `""`; `CultRecordKey.Value` is never null. Rust,
TypeScript and Python have no reference type: a reference member is a string in
the consumer's own payload value, so a consumer writes an unset one as its
language's null (`null`, `None`, `Option::None`), not as an empty string.
key, is **`""`** on the wire in value and map-key position alike. Nil cannot be
the form: msgpack readers in other runtimes (Python under `strict_map_key`)
refuse nil map keys. Readers must also accept nil as unset, because C# 1.0.58
stores wrote it; the writer always writes `""`, so such a record converts on its
next save and is byte-stable after that. In C# the unset reference is
`default(CultRecordRef<T>)`, equal to one built from `""`;
`CultRecordKey.Value` is never null. Rust, TypeScript and Python have no
reference type: a reference member is a string in the consumer's own payload,
and a consumer writes an unset one as `""`.
## Value-type encoding