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