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

@ -785,25 +785,24 @@ namespace GameCult.Caching.Tests
Assert.That(HeaderOf(path), Is.EqualTo("cultcache.store.v3"), "read without one, it cannot be seen, so it is marked");
}
// A flush replaces the whole file, but never one whose header it cannot read (Rust, TypeScript and Python refuse the same):
// it throws and the file is left as it was. An empty file and a legacy envelope array carry no header and are replaced.
// A flush replaces the whole file, but only a file that is exactly one store this runtime reads (Rust, TypeScript and Python
// refuse the same): anything else throws and is left as it was. An empty file and a legacy envelope array (an array whose
// first slot is not a string, empty included) carry no header and are replaced.
[TestCase("truncated", false)]
[TestCase("v9", false)]
[TestCase("trailing", false)]
[TestCase("scalar", false)]
[TestCase("map", false)]
[TestCase("string-first", false)]
[TestCase("empty", true)]
[TestCase("legacy", true)]
public void AFlushRefusesAFileWhoseHeaderItCannotReadAndLeavesItUntouched(string file, bool replaced)
[TestCase("empty-array", true)]
public void AFlushRefusesAFileThatIsNotExactlyOneStoreAndLeavesItUntouched(string file, bool replaced)
{
var path = PathOf($"flush-over-{file}.cc");
using var cache = Open(path);
cache.Commit(batch => batch.Upsert(typeof(IdDeck), Deck("d"), new CultRecordKey("d")));
var whole = File.ReadAllBytes(path);
var bytes = file switch
{
"truncated" => whole[..^1],
"v9" => MessagePackSerializer.Serialize(new object[] { "cultcache.store.v9", Array.Empty<object>(), Array.Empty<object>() }),
"empty" => Array.Empty<byte>(),
_ => MessagePackSerializer.Serialize(new object[] { new Dictionary<string, object> { ["key"] = "old", ["type"] = "old", ["storedAt"] = "then" } })
};
var bytes = NotAStore(file, File.ReadAllBytes(path));
File.WriteAllBytes(path, bytes);
if (replaced)
@ -817,6 +816,35 @@ namespace GameCult.Caching.Tests
Assert.That(File.ReadAllBytes(path), Is.EqualTo(bytes), "the file was rewritten");
}
// An unconditional commit rewrites the file it read: a store with bytes after its array is refused, not rewritten.
[Test]
public void AnUnconditionalCommitRefusesAStoreWithBytesAfterItsArray()
{
var path = PathOf("commit-over-trailing.cc");
using var cache = Open(path);
cache.Commit(batch => batch.Upsert(typeof(IdDeck), Deck("d"), new CultRecordKey("d")));
var bytes = NotAStore("trailing", File.ReadAllBytes(path));
File.WriteAllBytes(path, bytes);
Assert.Throws<NotSupportedException>(() => cache.Commit(batch => batch.Upsert(typeof(IdDeck), Deck("e"), new CultRecordKey("e"))));
Assert.That(File.ReadAllBytes(path), Is.EqualTo(bytes), "the file was rewritten");
}
// What a store file is not, built from a whole store file.
private static byte[] NotAStore(string file, byte[] whole) => file switch
{
"truncated" => whole[..^1],
"v9" => MessagePackSerializer.Serialize(new object[] { "cultcache.store.v9", Array.Empty<object>(), Array.Empty<object>() }),
"trailing" => whole.Concat(new byte[] { 1, 2, 3 }).ToArray(),
"scalar" => new byte[] { 0x01 },
"map" => new byte[] { 0x80 },
"string-first" => MessagePackSerializer.Serialize(new object[] { "hello", 1 }),
"empty" => Array.Empty<byte>(),
"empty-array" => new byte[] { 0x90 },
"legacy" => MessagePackSerializer.Serialize(new object[] { new Dictionary<string, object> { ["key"] = "old", ["type"] = "old", ["storedAt"] = "then" } }),
_ => throw new ArgumentOutOfRangeException(nameof(file))
};
[Test]
public void ADirectoryStoreIsMarkedByTheIdsItWritesAndAnEmptyDeckDoesNotMarkIt()
{

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");
}