From 26bea2cb14c539a5d7ecb9410334fc27191ae6b3 Mon Sep 17 00:00:00 2001 From: soul Date: Tue, 27 Oct 2026 04:38:08 +0100 Subject: [PATCH] AetherDb economy: unknown batch names are not echoed; held-cycle rise is relative Run takes the output writer like Report and Author do. The unknown-batch refusal says how many names matched no batch and lists the compiled batches. Introduced compares a held cycle's radius relatively with RiseNoise = 1e-12, replacing the bare absolute 1e-9. Co-Authored-By: Claude Opus 5.5 --- .../EconomyCommandTests.cs | 38 ++++++++++++++++++- tools/AetherDb/EconomyCommand.cs | 15 +++++--- tools/AetherDb/Program.cs | 2 +- 3 files changed, 47 insertions(+), 8 deletions(-) diff --git a/tests/Aetheria.Shared.Tests/EconomyCommandTests.cs b/tests/Aetheria.Shared.Tests/EconomyCommandTests.cs index 7fc19f641..f76b56aeb 100644 --- a/tests/Aetheria.Shared.Tests/EconomyCommandTests.cs +++ b/tests/Aetheria.Shared.Tests/EconomyCommandTests.cs @@ -263,7 +263,23 @@ public sealed class EconomyCommandTests } [Fact] - public void AnUnknownSubcommandFails() => Assert.Equal(1, EconomyCommand.Run(new[] { "bogus" })); + public void AnUnknownSubcommandFails() + { + var output = new StringWriter(); + Assert.Equal(1, EconomyCommand.Run(new[] { "bogus" }, output)); + Assert.Contains("economy report | author", output.ToString()); + } + + [Fact] + public void AnUnknownBatchNameIsRefusedWithoutEchoingIt() + { + var output = new StringWriter(); + Assert.Equal(1, EconomyCommand.Run(new[] { "author", "CANARY-9f2e-batch" }, output)); + var text = output.ToString(); + Assert.Contains("Batches:", text); + Assert.DoesNotContain("CANARY", text); + Assert.DoesNotContain("9f2e", text); + } private static CultRecordKey K(string id) => new CultRecordKey(EconomyFixture.Key(id)); @@ -448,6 +464,26 @@ public sealed class EconomyCommandTests Assert.Contains("Wrote 1 records", output); } + [Fact] + public void AHeldCycleWhoseRadiusRisesByLessThanABillionthIsRefused() + { + using var f = HeldLoop(); + Flush(f); + var bytes = File.ReadAllBytes(f.CatalogPath); + // loopb takes 2000000001 loopa per 1000000000 loopb: the cycle's radius rises from sqrt 2 by about 2.5e-10 relative. + var code = Author(f, true, out var output, a => + { + var key = RecipeData.KeyOf(K("loopb")); + var recipe = a.Catalog.Get(key); + recipe.OutputCount = 1000000000; + recipe.Inputs = new List { new CommodityCount { Commodity = EconomyFixture.Item(EconomyFixture.Key("loopa")), Count = 2000000001 } }; + a.Put(recipe, key); + }); + Assert.Equal(1, code); + Assert.Contains("L-productive", output); + Assert.Equal(bytes, File.ReadAllBytes(f.CatalogPath)); + } + [Fact] public void AHeldUnproductiveCatalogStillAppliesABatchThatLeavesTheCycleAlone() { diff --git a/tools/AetherDb/EconomyCommand.cs b/tools/AetherDb/EconomyCommand.cs index 6981cc2e3..d0dc1b725 100644 --- a/tools/AetherDb/EconomyCommand.cs +++ b/tools/AetherDb/EconomyCommand.cs @@ -61,12 +61,12 @@ public static class EconomyCommand // A batch is compiled code under tools/AetherDb/Economy/ and runs with the operator's authority: reviewed code, never an input. private const string Usage = "economy report | author ... [apply]\nA batch is reviewed code: it runs on a trial copy and must write only through EconomyAuthor."; - public static int Run(string[] args) + public static int Run(string[] args, TextWriter output) { if (args.Length > 0 && args[0] == "report") { using var db = AetherDb.Open(); - return Report(db.Cache, Console.Out); + return Report(db.Cache, output); } if (args.Length > 1 && args[0] == "author") { @@ -77,13 +77,13 @@ public static class EconomyCommand var unknown = names.Where(n => known.All(t => !string.Equals(t.Name, n, StringComparison.OrdinalIgnoreCase))).ToArray(); if (unknown.Length > 0 || names.Length == 0) { - Console.WriteLine($"No batch named {string.Join(", ", unknown)}. Batches: {string.Join(", ", known.Select(t => t.Name))}"); + output.WriteLine($"{unknown.Length} of the names given match no batch. Batches: {string.Join(", ", known.Select(t => t.Name))}"); return 1; } var batches = names.Select(n => (EconomyBatch) Activator.CreateInstance(known.Single(t => string.Equals(t.Name, n, StringComparison.OrdinalIgnoreCase)))).ToList(); - return Author(Path.Combine(AetherDb.FindRoot(), "GameData", "Aetheria.cc"), batches, apply, Console.Out); + return Author(Path.Combine(AetherDb.FindRoot(), "GameData", "Aetheria.cc"), batches, apply, output); } - Console.WriteLine(Usage); + output.WriteLine(Usage); return 1; } @@ -225,6 +225,9 @@ public static class EconomyCommand private static string Held(string verb, Exception e) => $"The batches are refused: Aetheria.cc could not be {verb} ({e.Message}). Something holds the file: close Unity and any other editor or tool that has it open, then run again. The catalog is unchanged."; + // A held cycle's radius may rise by this fraction without being a worse fault: it is float noise from rebuilding the same graph. + private const double RiseNoise = 1e-12; + // A fault is the same fault by rule, record and message, so fixing one fault on a record cannot hide a new one there. L-productive // is one fault per cycle: its identity is the cycle's member set, and the same cycle with a higher radius is a worse fault. private static string Identity(EconomyFault fault) => @@ -235,7 +238,7 @@ public static class EconomyCommand // "No recipe" is pending for a commodity that had none; it refuses where the batch took away a recipe the catalog held. if (fault.Pending) return recipesHeld.Contains(RecipeData.KeyOf(new CultRecordKey(fault.Record)).Value); if (!held.TryGetValue(Identity(fault), out var was)) return true; - return fault.Members != null && fault.Radius > was.Radius + 1e-9; + return fault.Members != null && fault.Radius > was.Radius * (1 + RiseNoise); } private static readonly MessagePackSerializerOptions Wire = CultDocumentMessagePackSerialization.OptionsFor(typeof(ItemData).Assembly); diff --git a/tools/AetherDb/Program.cs b/tools/AetherDb/Program.cs index 665ce766b..99963e8dd 100644 --- a/tools/AetherDb/Program.cs +++ b/tools/AetherDb/Program.cs @@ -76,7 +76,7 @@ public static class Program if (apply) db.Cache.FlushAsync().GetAwaiter().GetResult(); return code; } - case "economy": return EconomyCommand.Run(args.Skip(1).ToArray()); + case "economy": return EconomyCommand.Run(args.Skip(1).ToArray(), Console.Out); case "auto-guns": return AutoGunsCatalog.Run(AetherDb.Open(catalogWritable: args.Contains("apply")).Cache, args.Contains("apply"), Console.Out); case "doctrine-catalog": return DoctrineCatalog.Run(AetherDb.Open(catalogWritable: args.Contains("apply")).Cache, args.Contains("apply"), Console.Out); default: