From e3e228e0369cc5f6b71222066e85619266fb5797 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sat, 19 Sep 2026 05:47:31 +0200 Subject: [PATCH] F7: route AetherDb's identity-preserving repairs through the same validation CultRecordRefs.Upsert is the one function every catalog write is supposed to go through, but tools/AetherDb/Program.cs's Dangling, ShieldMigrate and BrownoutMigrate commands each repair an existing record in place and must preserve its key -- Upsert's own cache.UpsertAsync(document) cannot take a caller-supplied key, so all three wrote raw and unvalidated through `db.Cache.Commit(batch => batch.Upsert(document.GetType(), document, key))`. `brownout-migrate apply` is how the shipped catalog's PowerSupply terms were actually authored, unvalidated, the whole time. Extracted Upsert's validation body into CultRecordRefs.Validate(object), and call it directly from all three tool commands immediately before their own identity-preserving commit -- the same rules, invoked where Upsert itself cannot reach. The comment above StatValidation naming the write-bypass holes (landed with F6) now names all four instead of two: CultCache Studio's generic editor (still open -- not this repository's to patch) plus the three tool commands above (closed). Pinned in tests/Aetheria.Shared.Tests/AetheriaStoresTests.cs: ValidateRefusesAnEquippableItemWithAZeroSpanHeatRangeTheSameWayUpsertWould and ValidateRefusesAPowerRequestCarryingAPowerSupplyTermTheSameWayUpsertWould pin that the extracted method actually refuses what Upsert would (fail under a reverted Validate that is a no-op); ValidateIsANoOpForADocumentTypeItDoesNotGovern pins that a document type it has no opinion about doesn't crash it. Verified tools/AetherDb/AetherDb.csproj still builds clean against the pinned CultLib worktree. Co-Authored-By: Claude Opus 5 --- Assets/Scripts/ServerShared/AetheriaStores.cs | 27 ++++++++--- .../AetheriaStoresTests.cs | 48 +++++++++++++++++++ tools/AetherDb/Program.cs | 15 ++++++ 3 files changed, 84 insertions(+), 6 deletions(-) diff --git a/Assets/Scripts/ServerShared/AetheriaStores.cs b/Assets/Scripts/ServerShared/AetheriaStores.cs index 47e935999..641e431e6 100644 --- a/Assets/Scripts/ServerShared/AetheriaStores.cs +++ b/Assets/Scripts/ServerShared/AetheriaStores.cs @@ -69,12 +69,16 @@ public static class CultRecordRefs new CultRecordRef(cache.TryGetHandle(document)?.Key ?? throw new InvalidOperationException($"This cache holds no such {document.GetType().Name}.")); - // Writes a document to its home store; the cache mints its key the first time. Every catalog write in this - // repository (tools, tests, migration scripts) goes through this one function, so it is where R-heat's - // validation closes the gap AetheriaStores.Open leaves: Open only checks what is already on disk when a - // session starts, not what a session then writes. Validating here means an invalid heat response is refused - // before the write ever reaches disk, not just the next time someone reopens the file. - public static CultRecordRef Upsert(this CultCache cache, T document) where T : class + // F7 (docs/stats-and-power-cut.md, Soul pass 2026-09-19): the validation body itself, pulled out of Upsert + // below so it has exactly one owner. Every catalog write in this repository was supposed to go through + // Upsert, but three of tools/AetherDb/Program.cs's own migration commands (Dangling, ShieldMigrate, + // BrownoutMigrate) cannot: they repair an EXISTING record in place and must preserve its key, which Upsert's + // own cache.UpsertAsync(document) does not support (Upsert mints or resolves a key from the document itself, + // not from a caller-supplied one). Rather than leave those three writing raw and unvalidated -- as they did + // before this fix -- they call this method directly, immediately before their own identity-preserving + // batch.Upsert(document.GetType(), document, key) commit, so the same rules run either way. Takes `object` + // (not a generic T) because that is what those three tools' own (Document, Key) change lists are typed as. + public static void Validate(object document) { if (document is EquippableItemData data) { @@ -87,6 +91,17 @@ public static class CultRecordRefs StatValidation.ValidateStatModifiers(consumable.Name, consumable.Behaviors); StatValidation.ValidateNoPowerSupplyOnRequest(consumable.Name, consumable.Behaviors); } + } + + // Writes a document to its home store; the cache mints its key the first time. Every catalog write in this + // repository (tools, tests, migration scripts) goes through this one function, or through Validate above + // directly when the caller must preserve an existing key (see Validate's own comment) -- so this is where + // R-heat's validation closes the gap AetheriaStores.Open leaves: Open only checks what is already on disk + // when a session starts, not what a session then writes. Validating here means an invalid heat response is + // refused before the write ever reaches disk, not just the next time someone reopens the file. + public static CultRecordRef Upsert(this CultCache cache, T document) where T : class + { + Validate(document); return new CultRecordRef(cache.UpsertAsync(document).Result.Key); } } diff --git a/tests/Aetheria.Shared.Tests/AetheriaStoresTests.cs b/tests/Aetheria.Shared.Tests/AetheriaStoresTests.cs index 1542950f0..9f2d906d2 100644 --- a/tests/Aetheria.Shared.Tests/AetheriaStoresTests.cs +++ b/tests/Aetheria.Shared.Tests/AetheriaStoresTests.cs @@ -149,6 +149,54 @@ public sealed class AetheriaStoresTests : IDisposable Assert.Contains("aetheria.tests.catalogglobal", error.Message); } + // --- F7 (docs/stats-and-power-cut.md, Soul pass 2026-09-19): "there are at least four [write bypasses]." + // --- tools/AetherDb/Program.cs's Dangling, ShieldMigrate and BrownoutMigrate commands repair an existing + // --- record in place through `cache.Commit(batch => batch.Upsert(document.GetType(), document, key))` -- + // --- CultCache's own batch API, keyed explicitly to preserve identity -- because CultRecordRefs.Upsert's own + // --- cache.UpsertAsync(document) cannot take a caller-supplied key. That is real, not an excuse to skip + // --- validation: all three now call CultRecordRefs.Validate(document) directly, immediately before their own + // --- commit. This pins that the extracted method actually refuses what Upsert would have, proving the tools' + // --- direct call is wired to the same rule rather than a hollow copy. --- + [Fact] + public void ValidateRefusesAnEquippableItemWithAZeroSpanHeatRangeTheSameWayUpsertWould() + { + var badDesign = new GearData + { + Name = "BadDangling", Hardpoint = HardpointType.Tool, Shape = new Shape(), Durability = 10, + MinimumTemperature = 100, MaximumTemperature = 100, OptimalTemperature = 100, PlateauWidth = 0 + }; + var error = Assert.Throws(() => CultRecordRefs.Validate(badDesign)); + Assert.Contains("BadDangling", error.Message); + } + + // The same direct-call path must also catch F6's rule (a power request stat may not carry a PowerSupply + // term), not only R-heat -- a repair tool landing a bad ThrusterData.EnergyUsage is exactly as dangerous as + // one landing a bad heat response. + [Fact] + public void ValidateRefusesAPowerRequestCarryingAPowerSupplyTermTheSameWayUpsertWould() + { + var badEnergy = new PerformanceStat { Min = 1, Max = 1, Terms = { new StatTerm { Source = StatSource.PowerSupply, Exponent = 1 } } }; + var badDesign = new GearData + { + Name = "BadMigratedThruster", Hardpoint = HardpointType.Tool, Shape = new Shape(), Durability = 10, + MinimumTemperature = 0, MaximumTemperature = 1000, OptimalTemperature = 280, PlateauWidth = 400, + Behaviors = { new ThrusterData { EnergyUsage = badEnergy } } + }; + var error = Assert.Throws(() => CultRecordRefs.Validate(badDesign)); + Assert.Contains("BadMigratedThruster", error.Message); + Assert.Contains(nameof(ThrusterData.EnergyUsage), error.Message); + } + + // A document type Validate has no opinion about (neither EquippableItemData nor ConsumableItemData) must be + // a pure no-op, not a crash -- the three tools' change lists carry `object`, and not every migration touches + // an equippable design (e.g. a future repair over FactionProductData). + [Fact] + public void ValidateIsANoOpForADocumentTypeItDoesNotGovern() + { + var record = Record.Exception(() => CultRecordRefs.Validate(new Faction { Name = "Unrelated" })); + Assert.Null(record); + } + private static string Hash(string path) => Convert.ToHexString(SHA256.HashData(File.ReadAllBytes(path))); private static string[] SchemaNames(string path) diff --git a/tools/AetherDb/Program.cs b/tools/AetherDb/Program.cs index 3b17c8b67..16284326a 100644 --- a/tools/AetherDb/Program.cs +++ b/tools/AetherDb/Program.cs @@ -270,6 +270,11 @@ public static class Program return 0; } + // F7 (docs/stats-and-power-cut.md, Soul pass 2026-09-19): this repair must preserve the record's existing + // key, which the validating CultRecordRefs.Upsert extension does not support (see AetheriaStores.cs's own + // Validate comment) -- so it calls the same validation directly, right before the identity-preserving + // write, instead of writing raw and unvalidated the way this line used to. + foreach (var (document, _) in changed) CultRecordRefs.Validate(document); db.Cache.Commit(batch => { foreach (var (document, key) in changed) batch.Upsert(document.GetType(), document, key); @@ -552,6 +557,11 @@ public static class Program return 0; } + // F7 (docs/stats-and-power-cut.md, Soul pass 2026-09-19): this repair must preserve the record's existing + // key, which the validating CultRecordRefs.Upsert extension does not support (see AetheriaStores.cs's own + // Validate comment) -- so it calls the same validation directly, right before the identity-preserving + // write, instead of writing raw and unvalidated the way this line used to. + foreach (var (document, _) in changed) CultRecordRefs.Validate(document); db.Cache.Commit(batch => { foreach (var (document, key) in changed) batch.Upsert(document.GetType(), document, key); @@ -643,6 +653,11 @@ public static class Program return 0; } + // F7 (docs/stats-and-power-cut.md, Soul pass 2026-09-19): this repair must preserve the record's existing + // key, which the validating CultRecordRefs.Upsert extension does not support (see AetheriaStores.cs's own + // Validate comment) -- so it calls the same validation directly, right before the identity-preserving + // write, instead of writing raw and unvalidated the way this line used to. + foreach (var (document, _) in changed) CultRecordRefs.Validate(document); db.Cache.Commit(batch => { foreach (var (document, key) in changed) batch.Upsert(document.GetType(), document, key);