C#: compatible schema ids publish a schema on open, Mesh takes only the older layout, the refusal names its file

A record whose schema id the catalog lists only as a compatible id now opens (the cache resolved a persisted schema by entry id
alone). CultMesh's fallback to the older catalog-entry reader fires only for a store in that layout, decided by the entry's slot 5,
not for any decode failure, so a current-layout store with a malformed slot is refused as CultCache refuses it.
CultStoreUnreadableException.Path names the refused file. New vectors: compatible-id-only-v3 (accepted by all four) and three
current-layout catalogs with one malformed slot. The contract states the writer's invariant.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-09-30 06:54:17 +02:00
commit 969d78de39
16 changed files with 118 additions and 16 deletions

View file

@ -234,7 +234,7 @@ public static class CultDocumentMessagePackSerialization
}
catch (Exception ex) when (ex is not OutOfMemoryException)
{
throw new CultStoreUnreadableException(ex.Message, ex);
throw new CultStoreUnreadableException(ex.Message, innerException: ex);
}
}
@ -268,7 +268,7 @@ public static class CultDocumentMessagePackSerialization
}
catch (Exception ex) when (ex is EndOfStreamException or MessagePackSerializationException or InvalidOperationException)
{
throw new CultStoreUnreadableException("The store is not one complete MessagePack array.", ex);
throw new CultStoreUnreadableException("The store is not one complete MessagePack array.", innerException: ex);
}
if (!whole.End)

View file

@ -451,7 +451,16 @@ public sealed class DirectoryMessagePackBackingStore : CacheBackingStore
};
}
var snapshot = CultDocumentMessagePackSerialization.DeserializeSnapshot(manifestBytes);
CultPersistedStoreSnapshot snapshot;
try
{
snapshot = CultDocumentMessagePackSerialization.DeserializeSnapshot(manifestBytes);
}
catch (CultStoreUnreadableException ex) when (ex.Path == null)
{
throw new CultStoreUnreadableException(ex.Message, _manifestFile.FullName, ex.InnerException);
}
if (!string.Equals(snapshot.FormatVersion, IndexedFormatVersion, StringComparison.Ordinal) &&
!string.Equals(snapshot.FormatVersion, IndexedFormatVersionWithIds, StringComparison.Ordinal))
{

View file

@ -122,6 +122,10 @@ A reader never recovers a schema from a record's payload: a record whose schema
catalog does not publish (by id or as a compatible id) makes the store unreadable, whatever
the payload's first field says. The catalog is the store's own description of itself.
A writer keeps the same invariant: every record it writes carries a schema id that some catalog
entry it writes publishes, as that entry's id or as one of its compatible ids. A cache stamps a
record with its registered schema's id, whatever id the record arrived under.
This is not decorative paperwork. It is what allows another CultCache
implementation to inspect an old store, resolve the schema for each record, and
decide whether it can:

View file

@ -529,7 +529,9 @@ namespace GameCult.Caching
});
}
var persisted = catalog.FirstOrDefault(entry => string.Equals(entry.SchemaId, schemaId, StringComparison.Ordinal));
// A schema is published by its id or as a compatible id.
var persisted = catalog.FirstOrDefault(entry => string.Equals(entry.SchemaId, schemaId, StringComparison.Ordinal))
?? catalog.FirstOrDefault(entry => entry.CompatibleSchemaIds.Contains(schemaId, StringComparer.Ordinal));
if (persisted == null)
{
throw new InvalidOperationException($"Persisted schema '{schemaId}' is not present in the embedded catalog.");
@ -3601,7 +3603,16 @@ namespace GameCult.Caching
private CultPersistedStoreSnapshot? ReadSnapshot()
{
var bytes = ReadDisk();
return bytes == null ? null : DeserializeSnapshot(bytes);
if (bytes == null)
return null;
try
{
return DeserializeSnapshot(bytes);
}
catch (CultStoreUnreadableException ex) when (ex.Path == null)
{
throw new CultStoreUnreadableException(ex.Message, FileInfo.FullName, ex.InnerException);
}
}
private byte[]? ReadDisk()

View file

@ -6,13 +6,17 @@ namespace GameCult.Caching
/// A store file this runtime cannot read: not exactly one complete store (truncated, bytes after it, a value that is not a
/// store, a missing or extra top-level slot), a format or record it does not know, or a body it cannot decode. Open, flush,
/// commit and CultMesh's single-file reads all refuse a file with this one exception, and a refused file is left as it was.
/// The underlying cause, where there is one, is the inner exception.
/// The underlying cause, where there is one, is the inner exception, and <see cref="Path"/> names the file.
/// </summary>
public sealed class CultStoreUnreadableException : NotSupportedException
{
public CultStoreUnreadableException(string message, Exception? innerException = null)
public CultStoreUnreadableException(string message, string? path = null, Exception? innerException = null)
: base(message, innerException)
{
Path = path;
}
/// <summary>The store file that was refused, or null when the bytes were read without a file.</summary>
public string? Path { get; }
}
}

View file

@ -270,10 +270,9 @@ namespace GameCult.Mesh
WriteFileAtomically(path, CultDocumentMessagePackSerialization.SerializeSnapshot(snapshot));
}
// Every refusal is a CultStoreUnreadableException. A store whose catalog entries are in the older layout is read by the
// same store reader with the older entry reader, so it is judged by the same framing, slot count and records. Only a
// catalog entry the current layout cannot decode (a MessagePack or shape failure) sends it there; a store refused for its
// framing, its slot count or a record keeps that refusal.
// Every refusal is a CultStoreUnreadableException naming the file. A store whose catalog entries are in the older layout is
// read by the same store reader with the older entry reader, so it is judged by the same framing, slot count and records.
// Only a store that is in the older layout goes there; any other refusal stands as the current reader gave it.
private static CultPersistedStoreSnapshot ReadSingleFileSnapshot(string path)
{
var bytes = File.ReadAllBytes(path);
@ -282,17 +281,55 @@ namespace GameCult.Mesh
CultPersistedStoreSnapshot snapshot;
try
{
snapshot = CultDocumentMessagePackSerialization.DeserializeSnapshot(bytes);
try
{
snapshot = CultDocumentMessagePackSerialization.DeserializeSnapshot(bytes);
}
catch (CultStoreUnreadableException) when (HasOlderCatalogLayout(bytes))
{
snapshot = CultDocumentMessagePackSerialization.ReadStore(bytes, ReadLegacySchemaCatalogEntry);
}
CultDocumentMessagePackSerialization.RequireSingleFileFormat(snapshot);
}
catch (CultStoreUnreadableException ex) when (ex.InnerException is MessagePackSerializationException or InvalidOperationException)
catch (CultStoreUnreadableException ex) when (ex.Path == null)
{
snapshot = CultDocumentMessagePackSerialization.ReadStore(bytes, ReadLegacySchemaCatalogEntry);
throw new CultStoreUnreadableException(ex.Message, path, ex.InnerException);
}
CultDocumentMessagePackSerialization.RequireSingleFileFormat(snapshot);
return snapshot;
}
// The older layout puts the content hash, a string, at catalog entry slot 5, where the current layout has the array of
// compatible schema ids.
private static bool HasOlderCatalogLayout(byte[] bytes)
{
try
{
var reader = new MessagePackReader(bytes);
if (reader.ReadArrayHeader() != 3)
return false;
reader.Skip();
var entries = reader.ReadArrayHeader();
for (var entry = 0; entry < entries; entry++)
{
var fields = reader.ReadArrayHeader();
for (var field = 0; field < fields; field++)
{
if (field == 5 && reader.NextMessagePackType == MessagePackType.String)
return true;
reader.Skip();
}
}
return false;
}
catch (Exception ex) when (ex is EndOfStreamException or MessagePackSerializationException or InvalidOperationException)
{
return false;
}
}
private static CultSchemaCatalogEntry ReadLegacySchemaCatalogEntry(ref MessagePackReader reader)
{
var fieldCount = reader.ReadArrayHeader();