From 466d2bc8abcaf188c221727a7d7e21a8313da4c0 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Mon, 20 Jul 2026 00:56:46 +0100 Subject: [PATCH] Reuse unchanged compiled runtime catalog --- Aetheria.State.Daemon.Smoke/Program.cs | 5 +- .../AetheriaDaemonNativeCatalog.cs | 56 +++++++++++++++---- Aetheria.State.Daemon/Program.cs | 15 +++-- docs/aetheria-perfect-machine-map.md | 3 + 4 files changed, 63 insertions(+), 16 deletions(-) diff --git a/Aetheria.State.Daemon.Smoke/Program.cs b/Aetheria.State.Daemon.Smoke/Program.cs index a2d648eab..7d8b6d8af 100644 --- a/Aetheria.State.Daemon.Smoke/Program.cs +++ b/Aetheria.State.Daemon.Smoke/Program.cs @@ -5858,9 +5858,12 @@ internal sealed class AetheriaDaemonYmirSmokeChecks DockingMaxSizeY = 12 }).GetAwaiter().GetResult(); - AetheriaDaemonNativeCatalog.EnsureAsync(node).GetAwaiter().GetResult(); + Require(AetheriaDaemonNativeCatalog.EnsureAsync(node).GetAwaiter().GetResult(), + "daemon-native catalog must report its first materialization"); node.FlushAsync().GetAwaiter().GetResult(); node.RefreshRuntimeCatalogAsync().GetAwaiter().GetResult(); + Require(!AetheriaDaemonNativeCatalog.EnsureAsync(node).GetAwaiter().GetResult(), + "daemon-native catalog must not rewrite an unchanged generated revision"); var catalog = node.RuntimeCatalog().Latest(); var hull = catalog.FindItem(AetheriaDaemonNativeCatalog.DockyardHullItemKey); var bay = catalog.FindItem(AetheriaDaemonNativeCatalog.DockyardBayItemKey); diff --git a/Aetheria.State.Daemon/AetheriaDaemonNativeCatalog.cs b/Aetheria.State.Daemon/AetheriaDaemonNativeCatalog.cs index 8cfce1334..d439fd186 100644 --- a/Aetheria.State.Daemon/AetheriaDaemonNativeCatalog.cs +++ b/Aetheria.State.Daemon/AetheriaDaemonNativeCatalog.cs @@ -1,8 +1,11 @@ using Aetheria.State; using Aetheria.State.Documents; +using MessagePack; +using System.Security.Cryptography; internal static class AetheriaDaemonNativeCatalog { + private const string NativeCatalogRevisionTag = "native-catalog-revision:1"; private const string ZenithHullLegacyId = "82efc0a5-1ba5-4ff3-a281-b2e6e247521d"; private const string MediumDockingBayLegacyId = "3e930a2c-ac72-4385-98aa-1c5b0b90db46"; private const string DockyardHullLegacyId = "ca098005-8cc8-47f4-be99-7bc842805359"; @@ -11,7 +14,7 @@ internal static class AetheriaDaemonNativeCatalog public static string DockyardHullItemKey => ItemKey(DockyardHullLegacyId); public static string DockyardBayItemKey => ItemKey(DockyardBayLegacyId); - public static async Task EnsureAsync(AetheriaStateNode node) + public static async Task EnsureAsync(AetheriaStateNode node) { ArgumentNullException.ThrowIfNull(node); @@ -26,14 +29,23 @@ internal static class AetheriaDaemonNativeCatalog .ConfigureAwait(false) ?? throw new InvalidDataException("The daemon-native dockyard requires the canonical medium docking bay."); - await node.MutableDocument( - AetheriaCatalogKeys.ItemDefinitionFromLegacyId(DockyardHullLegacyId)) - .ReplaceAsync(CreateDockyardHull(zenith)) - .ConfigureAwait(false); - await node.MutableDocument( - AetheriaCatalogKeys.ItemDefinitionFromLegacyId(DockyardBayLegacyId)) - .ReplaceAsync(CreateDockyardBay(mediumBay)) - .ConfigureAwait(false); + var expectedHull = CreateDockyardHull(zenith); + var expectedBay = CreateDockyardBay(mediumBay); + var hullDocument = node.MutableDocument( + AetheriaCatalogKeys.ItemDefinitionFromLegacyId(DockyardHullLegacyId)); + var bayDocument = node.MutableDocument( + AetheriaCatalogKeys.ItemDefinitionFromLegacyId(DockyardBayLegacyId)); + var existingHull = await hullDocument.ReadAsync().ConfigureAwait(false); + var existingBay = await bayDocument.ReadAsync().ConfigureAwait(false); + var hullCurrent = HasGenerationTags(existingHull, expectedHull.Tags); + var bayCurrent = HasGenerationTags(existingBay, expectedBay.Tags); + + if (!hullCurrent) + await hullDocument.ReplaceAsync(expectedHull).ConfigureAwait(false); + if (!bayCurrent) + await bayDocument.ReplaceAsync(expectedBay).ConfigureAwait(false); + + return !hullCurrent || !bayCurrent; } private static AetheriaItemDefinition CreateDockyardHull(AetheriaItemDefinition source) @@ -77,6 +89,8 @@ internal static class AetheriaDaemonNativeCatalog hull.Tags = Tags( source.Tags, "origin:aetheria-daemon", + NativeCatalogRevisionTag, + SourceFingerprintTag(source), "station-role:dockyard", "docking-capacity:12", $"presentation-hull-item:{ItemKey(ZenithHullLegacyId)}"); @@ -89,10 +103,32 @@ internal static class AetheriaDaemonNativeCatalog bay.Description = "A medium docking bay packaged for a dedicated station docking hardpoint."; bay.HardpointType = "DockingBay"; bay.Price = checked(source.Price * 2); - bay.Tags = Tags(source.Tags, "origin:aetheria-daemon", "station-role:dockyard"); + bay.Tags = Tags( + source.Tags, + "origin:aetheria-daemon", + NativeCatalogRevisionTag, + SourceFingerprintTag(source), + "station-role:dockyard"); return bay; } + private static bool HasGenerationTags(AetheriaItemDefinition? existing, IEnumerable expectedTags) + { + if (existing == null) + return false; + + var expectedGenerationTags = expectedTags.Where(tag => + tag.StartsWith("native-catalog-revision:", StringComparison.Ordinal) || + tag.StartsWith("native-source-sha256:", StringComparison.Ordinal)); + return expectedGenerationTags.All(tag => existing.Tags.Contains(tag, StringComparer.Ordinal)); + } + + private static string SourceFingerprintTag(AetheriaItemDefinition source) + { + var payload = MessagePackSerializer.Serialize(source); + return "native-source-sha256:" + Convert.ToHexString(SHA256.HashData(payload)).ToLowerInvariant(); + } + private static AetheriaItemDefinition Copy(AetheriaItemDefinition source, string legacyId, string name) => new() { Name = name, diff --git a/Aetheria.State.Daemon/Program.cs b/Aetheria.State.Daemon/Program.cs index c2e07db53..9431c2021 100644 --- a/Aetheria.State.Daemon/Program.cs +++ b/Aetheria.State.Daemon/Program.cs @@ -78,13 +78,17 @@ using var soaPublisher = new AetheriaRuntimeDaemonSoaFramePublisher( await EnsureWorldDocumentAsync(node).ConfigureAwait(false); TraceStartup("world-document"); -await EnsureTradeValuePolicyAsync(node, startedAtUtc).ConfigureAwait(false); +var persistedRuntimeCatalog = await node.Database + .GetAsync(AetheriaStateNode.RuntimeCatalogKey) + .ConfigureAwait(false); +var tradePolicyChanged = await EnsureTradeValuePolicyAsync(node, startedAtUtc).ConfigureAwait(false); TraceStartup("trade-policy"); -await AetheriaDaemonNativeCatalog.EnsureAsync(node).ConfigureAwait(false); +var nativeCatalogChanged = await AetheriaDaemonNativeCatalog.EnsureAsync(node).ConfigureAwait(false); TraceStartup("native-catalog"); await node.FlushAsync().ConfigureAwait(false); TraceStartup("initial-flush"); -await node.RefreshRuntimeCatalogAsync().ConfigureAwait(false); +if (persistedRuntimeCatalog == null || tradePolicyChanged || nativeCatalogChanged) + await node.RefreshRuntimeCatalogAsync().ConfigureAwait(false); TraceStartup("runtime-catalog"); await EnsurePlayableRunDocumentsAsync(node, options, startedAtUtc).ConfigureAwait(false); TraceStartup("playable-run"); @@ -2832,17 +2836,18 @@ static async Task EnsureWorldDocumentAsync(AetheriaStateNode node) }).ConfigureAwait(false); } -static async Task EnsureTradeValuePolicyAsync(AetheriaStateNode node, string now) +static async Task EnsureTradeValuePolicyAsync(AetheriaStateNode node, string now) { var tradeValuePolicy = node.MutableDocument(AetheriaStateNode.TradeValuePolicyKey); var existing = await tradeValuePolicy.ReadAsync().ConfigureAwait(false); if (existing != null) - return; + return false; await tradeValuePolicy.ReplaceAsync( AetheriaRuntimeStateMapper.ToTradeValuePolicy( AetheriaRuntimeTradeValueSettings.Default, now)).ConfigureAwait(false); + return true; } static async Task EnsurePlayableRunDocumentsAsync( diff --git a/docs/aetheria-perfect-machine-map.md b/docs/aetheria-perfect-machine-map.md index 751b0a2bc..53a9a7462 100644 --- a/docs/aetheria-perfect-machine-map.md +++ b/docs/aetheria-perfect-machine-map.md @@ -3622,6 +3622,9 @@ The runtime catalog has one compiled-state owner: and surface refreshes cannot silently recompile or replace the catalog. - Shared paths: import, daemon-native catalog augmentation, and the explicit Eve catalog-refresh command use the same compile-and-publish primitive. + Daemon-native generated items carry a generator revision and a hash of their + authored source item; unchanged generations do not rewrite source records or + force recompilation on restart. - Cut line: runtime reads and catalog-surface watches no longer reopen the CultCache store or subscribe to daemon-frame changes. - Verification layer: state verification requires the typed record/watch path