From 2cea2079593a73e2246a99a9db759693625c1fcf Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sat, 28 Nov 2026 02:25:32 +0100 Subject: [PATCH] C# snapshot lists are whole-value ImmutableArray holders; CultLib pin 9fc4bdda Owned snapshot lists (sceneObjects, assets, components, properties) become ImmutableArray, which CultLib treats as one value with no element ids (brokkr-msgpack:ruling:snapshot-lists-whole-value). Record payload bytes are unchanged, pinned by a golden test. A default array (a nil list on the wire) is refused on read and on publish. The mirror flushes plainly: since CultLib 558526c1 a soft flush over an existing store writes nothing. Co-Authored-By: Claude Sonnet 5.5 --- Directory.Build.props | 4 +- Directory.Build.targets | 4 +- brokkr-command/Program.cs | 8 ++- .../BrokkrHostSnapshotTests.cs | 60 +++++++++++++++++++ brokkr-contracts.Tests/EditorProbe.cs | 4 +- .../Editor/BrokkrCultMeshMirror.cs | 15 ++--- .../Editor/BrokkrUnitySnapshotBuilder.cs | 17 +++--- .../Runtime/BrokkrHostSnapshot.cs | 25 ++++++-- 8 files changed, 110 insertions(+), 27 deletions(-) create mode 100644 brokkr-contracts.Tests/BrokkrHostSnapshotTests.cs diff --git a/Directory.Build.props b/Directory.Build.props index c8a3c67..0b65fe5 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -2,8 +2,8 @@ $(CULTLIB_ROOT) $(MSBuildThisFileDirectory)..\CultLib - - 42dce3611a81adc5c5fac54cadbaa5637d6022d3 + + 9fc4bdda6a8620f846b58e89ddf476f4676d3e89 C:\Program Files\Git\cmd\git.exe git diff --git a/Directory.Build.targets b/Directory.Build.targets index 46d436a..624e78a 100644 --- a/Directory.Build.targets +++ b/Directory.Build.targets @@ -1,6 +1,6 @@ + worktree of that revision: git worktree add , detached (in CultLib). --> @@ -17,7 +17,7 @@ + Text="CultLib must be revision $(CultLibRevision); found $([System.String]::Copy($(ActualCultLibRevision)).Trim()). Set CultLibRoot to a clean worktree of that revision." /> - cache.TryGet(new CultRecordKey("unity/host/current"), out BrokkrHostSnapshot? host) ? host : null; + private static BrokkrHostSnapshot? ReadHostSnapshot(CultCache cache) + { + if (!cache.TryGet(new CultRecordKey("unity/host/current"), out BrokkrHostSnapshot? host)) return null; + host.RefuseDefaultLists(); + return host; + } private static Dictionary Parse(string[] args) { diff --git a/brokkr-contracts.Tests/BrokkrHostSnapshotTests.cs b/brokkr-contracts.Tests/BrokkrHostSnapshotTests.cs new file mode 100644 index 0000000..4a7fc06 --- /dev/null +++ b/brokkr-contracts.Tests/BrokkrHostSnapshotTests.cs @@ -0,0 +1,60 @@ +using System.Collections.Immutable; +using GameCult.Brokkr; +using MessagePack; + +namespace Brokkr.Contracts.Tests; + +public sealed class BrokkrHostSnapshotTests +{ + // Captured before the lists became ImmutableArray holders: the record payload must not change. + private const string PayloadHex = + "DC0013D92567616D6563756C742E62726F6B6B722E746F6F6C5F686F73745F736E617073686F742E7630B362726F6B6B722E756E6974795F656469746F72AC756E6974792D656469746F72A170A174A0A0A000900090919EA16FA0A0A0C2A00000A09195A163A0A0C29195A178A0A0A131C2A0A0A0909196A161A0A0C2C2A0C2C2C2C2C2"; + + private static BrokkrHostSnapshot Populated() => new() + { + projectPath = "p", + observedAt = "t", + sceneObjects = ImmutableArray.Create(new BrokkrGameObjectSnapshot + { + objectId = "o", + components = ImmutableArray.Create(new BrokkrComponentSnapshot + { + componentId = "c", + properties = ImmutableArray.Create(new BrokkrSerializedPropertySnapshot { path = "x", value = "1" }) + }) + }), + assets = ImmutableArray.Create(new BrokkrAssetSnapshot { path = "a" }) + }; + + [Fact] + public void ListsAreWholeValuesWithTheSameBytesAsArrays() => + Assert.Equal(PayloadHex, Convert.ToHexString(MessagePackSerializer.Serialize(Populated()))); + + [Fact] + public void PopulatedSnapshotIsAccepted() => Populated().RefuseDefaultLists(); + + [Theory] + [InlineData(12)] + [InlineData(13)] + public void NilListsDecodeToDefaultAndAreRefused(int key) + { + var fields = MessagePackSerializer.Deserialize(MessagePackSerializer.Serialize(Populated())); + fields[key] = null; + var snapshot = MessagePackSerializer.Deserialize(MessagePackSerializer.Serialize(fields)); + + var error = Assert.Throws(snapshot.RefuseDefaultLists); + Assert.Contains("default", error.Message); + } + + [Fact] + public void DefaultNestedListsAreRefused() + { + var snapshot = Populated(); + snapshot.sceneObjects[0].components = default; + Assert.Throws(snapshot.RefuseDefaultLists); + + snapshot = Populated(); + snapshot.sceneObjects[0].components[0].properties = default; + Assert.Throws(snapshot.RefuseDefaultLists); + } +} diff --git a/brokkr-contracts.Tests/EditorProbe.cs b/brokkr-contracts.Tests/EditorProbe.cs index 1445697..45fb41e 100644 --- a/brokkr-contracts.Tests/EditorProbe.cs +++ b/brokkr-contracts.Tests/EditorProbe.cs @@ -9,7 +9,7 @@ namespace Brokkr.Contracts.Tests; // The scenario harness: an editor without Unity. It opens the store exactly as BrokkrCultMeshMirror does (a CultMesh // node with StartServer=false over a directory store, no shard log) and runs one service tick in the // service order: pull, drain pending intents through BrokkrCommandDrain (the same class the service uses), host -// snapshot, soft flush. "Executing" an intent only records it, so the scenarios measure delivery and once-only +// snapshot, flush. "Executing" an intent only records it, so the scenarios measure delivery and once-only // execution, not Unity. It is also a thin IBrokkrCommandStore in front of the real BrokkrCommandStore, with knobs to // make writes fail and to land a writer between the enable's pull and its commit. internal sealed class EditorProbe : IDisposable, IBrokkrCommandStore @@ -132,7 +132,7 @@ internal sealed class EditorProbe : IDisposable, IBrokkrCommandStore projectPath = "probe", agentCommandsEnabled = AgentCommandsEnabled }); - await node.FlushAsync(soft: true); + await node.FlushAsync(); return handled; } diff --git a/surfaces/unity/Packages/com.gamecult.brokkr/Editor/BrokkrCultMeshMirror.cs b/surfaces/unity/Packages/com.gamecult.brokkr/Editor/BrokkrCultMeshMirror.cs index fccfe07..1c46ed7 100644 --- a/surfaces/unity/Packages/com.gamecult.brokkr/Editor/BrokkrCultMeshMirror.cs +++ b/surfaces/unity/Packages/com.gamecult.brokkr/Editor/BrokkrCultMeshMirror.cs @@ -77,8 +77,9 @@ namespace GameCult.Brokkr.Editor internal async Task PublishSnapshotAsync(BrokkrHostSnapshot snapshot) { RequireRunning(); + snapshot.RefuseDefaultLists(); await node.Database.PutAsync(new CultRecordKey("unity/host/current"), snapshot); - await node.FlushAsync(soft: true); + await node.FlushAsync(); } // Command ids are single-use: a caller with no id gets a fresh one, and reusing an answered id is a defect. @@ -91,42 +92,42 @@ namespace GameCult.Brokkr.Editor } await node.Database.PutAsync(BrokkrCommandLedger.CommandKey(command.commandId), command); - await node.FlushAsync(soft: true); + await node.FlushAsync(); } internal async Task PublishQuestRouteAsync(BrokkrQuestRoute route) { RequireRunning(); await node.Database.PutAsync(new CultRecordKey($"unity/quest-routes/{route.id}"), route); - await node.FlushAsync(soft: true); + await node.FlushAsync(); } internal async Task PublishSyncSessionAsync(BrokkrSyncSession session) { RequireRunning(); await node.Database.PutAsync(new CultRecordKey($"sync/sessions/{session.sessionId}"), session); - await node.FlushAsync(soft: true); + await node.FlushAsync(); } internal async Task PublishSyncObjectBindingAsync(BrokkrSyncObjectBinding binding) { RequireRunning(); await node.Database.PutAsync(new CultRecordKey($"sync/bindings/objects/{binding.bindingId}"), binding); - await node.FlushAsync(soft: true); + await node.FlushAsync(); } internal async Task PublishSyncVarAsync(BrokkrSyncVar syncVar) { RequireRunning(); await node.Database.PutAsync(new CultRecordKey($"sync/vars/{syncVar.syncVarId}"), syncVar); - await node.FlushAsync(soft: true); + await node.FlushAsync(); } internal async Task PublishTimelineBindingAsync(BrokkrSyncTimelineBinding binding) { RequireRunning(); await node.Database.PutAsync(new CultRecordKey($"sync/bindings/timelines/{binding.bindingId}"), binding); - await node.FlushAsync(soft: true); + await node.FlushAsync(); } internal bool TryDequeueSyncReceipt(out BrokkrSyncReceipt receipt) diff --git a/surfaces/unity/Packages/com.gamecult.brokkr/Editor/BrokkrUnitySnapshotBuilder.cs b/surfaces/unity/Packages/com.gamecult.brokkr/Editor/BrokkrUnitySnapshotBuilder.cs index 45ae610..3536a50 100644 --- a/surfaces/unity/Packages/com.gamecult.brokkr/Editor/BrokkrUnitySnapshotBuilder.cs +++ b/surfaces/unity/Packages/com.gamecult.brokkr/Editor/BrokkrUnitySnapshotBuilder.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Collections.Immutable; using System.Globalization; using System.Linq; using System.Reflection; @@ -55,7 +56,7 @@ namespace GameCult.Brokkr.Editor }; } - private static BrokkrGameObjectSnapshot[] CaptureSceneObjects() + private static ImmutableArray CaptureSceneObjects() { var objects = new List(); @@ -73,7 +74,7 @@ namespace GameCult.Brokkr.Editor } } - return objects.ToArray(); + return objects.ToImmutableArray(); } private static void CaptureGameObject( @@ -109,7 +110,7 @@ namespace GameCult.Brokkr.Editor } } - private static BrokkrComponentSnapshot[] CaptureComponents(GameObject gameObject) + private static ImmutableArray CaptureComponents(GameObject gameObject) { return gameObject.GetComponents() .Where(component => component != null) @@ -121,7 +122,7 @@ namespace GameCult.Brokkr.Editor enabled = ReadEnabled(component), properties = CaptureProperties(component) }) - .ToArray(); + .ToImmutableArray(); } private static string[] CaptureMaterialNames(GameObject gameObject) @@ -135,7 +136,7 @@ namespace GameCult.Brokkr.Editor .ToArray(); } - private static BrokkrSerializedPropertySnapshot[] CaptureProperties(UnityEngine.Object target) + private static ImmutableArray CaptureProperties(UnityEngine.Object target) { var properties = new List(); using var serializedObject = new SerializedObject(target); @@ -165,10 +166,10 @@ namespace GameCult.Brokkr.Editor } } - return properties.ToArray(); + return properties.ToImmutableArray(); } - private static BrokkrAssetSnapshot[] CaptureAssets() + private static ImmutableArray CaptureAssets() { return AssetDatabase.GetAllAssetPaths() .Where(path => path.StartsWith("Assets/", StringComparison.Ordinal)) @@ -185,7 +186,7 @@ namespace GameCult.Brokkr.Editor name = System.IO.Path.GetFileNameWithoutExtension(path) }; }) - .ToArray(); + .ToImmutableArray(); } internal static string GetObjectId(UnityEngine.Object target) diff --git a/surfaces/unity/Packages/com.gamecult.brokkr/Runtime/BrokkrHostSnapshot.cs b/surfaces/unity/Packages/com.gamecult.brokkr/Runtime/BrokkrHostSnapshot.cs index a33b9d2..f30466e 100644 --- a/surfaces/unity/Packages/com.gamecult.brokkr/Runtime/BrokkrHostSnapshot.cs +++ b/surfaces/unity/Packages/com.gamecult.brokkr/Runtime/BrokkrHostSnapshot.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Immutable; using GameCult.Caching; using MessagePack; @@ -21,14 +22,30 @@ namespace GameCult.Brokkr [Key(9)] public string[] selectedObjectNames = Array.Empty(); [Key(10)] public int assetCount; [Key(11)] public string[] capabilities = Array.Empty(); - [Key(12)] public BrokkrGameObjectSnapshot[] sceneObjects = Array.Empty(); - [Key(13)] public BrokkrAssetSnapshot[] assets = Array.Empty(); + [Key(12)] public ImmutableArray sceneObjects = ImmutableArray.Empty; + [Key(13)] public ImmutableArray assets = ImmutableArray.Empty; [Key(14)] public bool isPlaying; [Key(15)] public bool isPaused; [Key(16)] public bool isCompiling; [Key(17)] public bool isUpdating; // False means the editor executes no agent commands: the operator has not enabled the sink. [Key(18)] public bool agentCommandsEnabled; + + // The snapshot lists are whole values (ImmutableArray, no element ids): a store row from a writer that left + // one nil decodes to a default array, which this refuses instead of letting a reader throw later. + public void RefuseDefaultLists() + { + if (sceneObjects.IsDefault) throw new InvalidOperationException("Brokkr host snapshot sceneObjects is default, not a list."); + if (assets.IsDefault) throw new InvalidOperationException("Brokkr host snapshot assets is default, not a list."); + foreach (var sceneObject in sceneObjects) + { + if (sceneObject.components.IsDefault) + throw new InvalidOperationException($"Brokkr host snapshot object '{sceneObject.path}' components is default, not a list."); + foreach (var component in sceneObject.components) + if (component.properties.IsDefault) + throw new InvalidOperationException($"Brokkr host snapshot component '{component.typeName}' properties is default, not a list."); + } + } } // Written by the editor, into the store, when the operator enables the command sink. The token is also kept in @@ -73,7 +90,7 @@ namespace GameCult.Brokkr [Key(6)] public int layer; [Key(7)] public int childCount; [Key(8)] public string parentId = ""; - [Key(9)] public BrokkrComponentSnapshot[] components = Array.Empty(); + [Key(9)] public ImmutableArray components = ImmutableArray.Empty; [Key(10)] public string localPosition = ""; [Key(11)] public string localEulerAngles = ""; [Key(12)] public string localScale = ""; @@ -88,7 +105,7 @@ namespace GameCult.Brokkr [Key(1)] public string typeName = ""; [Key(2)] public string assemblyQualifiedName = ""; [Key(3)] public bool enabled; - [Key(4)] public BrokkrSerializedPropertySnapshot[] properties = Array.Empty(); + [Key(4)] public ImmutableArray properties = ImmutableArray.Empty; } [MessagePackObject] -- 2.47.3