Every runtime rewrites only a file that is exactly one store

A rewrite read the header of the file it replaces but not whether the file was
a store: C# ReadStoreHeader skipped one value and ignored bytes after it, and
took a non-array value or an array whose first slot is a string that is not a
store header for a headerless legacy file (so did the C# Mesh raw write, which
uses it); TypeScript's pushAll took any non-array value or non-header first slot
for v1; Rust ignored bytes after the array.

The rule, one per runtime: a file is a store on rewrite only when it is exactly
one complete MessagePack array. Bytes after it, a value that is not an array,
and a first slot that is a string but not a header this runtime reads are
refused, and the file is left untouched. An empty file and a legacy envelope
array (first slot not a string, empty included) carry no header and are still
rewritten.

- C#: ReadStoreHeader refuses non-array values, trailing bytes and a non-header
  first string, with a NotSupportedException naming which.
- TypeScript: storeHeader (store-format.ts) decides the rewrite header;
  @msgpack/msgpack's decode already refuses trailing bytes.
- Rust: store_header(bytes, whole) walks with a cursor; the rewrite path asks
  for the whole file, the read paths keep reading as before.
- Python: push_all already reads the whole file (unpackb refuses trailing
  bytes, the legacy decoder refuses anything but a list of maps); pinned by a
  test.

Tests in each runtime run the same file matrix (truncated, trailing, scalar,
map, string first; empty, empty array, legacy), plus a C# unconditional commit
over a store with trailing bytes and the C# Mesh raw write.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-09-30 00:01:58 +02:00
commit ea1bd6a797
8 changed files with 211 additions and 36 deletions

View file

@ -106,14 +106,32 @@ public sealed class CultMeshSingleFileElementIdTests
File.WriteAllBytes(truncated, whole[..^1]);
var unknown = Path_("v9.cc");
File.WriteAllBytes(unknown, MessagePackSerializer.Serialize(new object[] { "cultcache.store.v9", Array.Empty<object>(), Array.Empty<object>() }));
// A file is a store only when it is exactly one MessagePack array whose first slot, if a string, is a store header.
var trailing = Path_("trailing.cc");
File.WriteAllBytes(trailing, whole.Concat(new byte[] { 1, 2, 3 }).ToArray());
var scalar = Path_("scalar.cc");
File.WriteAllBytes(scalar, new byte[] { 0x01 });
var map = Path_("map.cc");
File.WriteAllBytes(map, new byte[] { 0x80 });
var stringFirst = Path_("string-first.cc");
File.WriteAllBytes(stringFirst, MessagePackSerializer.Serialize(new object[] { "hello", 1 }));
foreach (var path in new[] { truncated, unknown })
foreach (var path in new[] { truncated, unknown, trailing, scalar, map, stringFirst })
{
var before = File.ReadAllBytes(path);
Assert.Throws<NotSupportedException>(() => CultMesh.WriteSingleFileDocumentPayload(path, Key, schema, null, new byte[] { 0x90 }), path);
File.ReadAllBytes(path).Should().Equal(before, path + " was rewritten");
}
// An empty file and a legacy envelope array (first slot not a string, empty included) carry no header and are written.
foreach (var (name, bytes) in new[] { ("empty.cc", Array.Empty<byte>()), ("empty-array.cc", new byte[] { 0x90 }), ("legacy.cc", new byte[] { 0x91, 0x80 }) })
{
var path = Path_(name);
File.WriteAllBytes(path, bytes);
CultMesh.WriteSingleFileDocumentPayload(path, Key, schema, null, new byte[] { 0x90 });
HeaderOf(path).Should().Be("cultcache.store.v1", name);
}
CultMesh.WriteSingleFileDocumentPayload(marked, Key, schema, null, new byte[] { 0x90 });
HeaderOf(marked).Should().Be("cultcache.store.v3", "a whole marked file is still written and stays marked");
}