Roles backfill: the exponent precedent is read before any target changes; the replay sees exponents and a one-of-two-roles seller

ExponentFor drew its pool from items.Except(targets), so growing the targets from six to twelve changed the exponents a rerun on master's catalog writes. The pool is now snapshotted once, from every design that declares roles and names a Quality term, before the loop mutates anything. Describe prints each named term's exponent, one test pins Autocannon's and LRMM72's exponents against the shipped blob, and one clears a single role of a two-role seller and expects only that role re-authored.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-10-09 01:59:25 +02:00
commit 9744d3969f
2 changed files with 65 additions and 8 deletions

View file

@ -95,12 +95,12 @@ public sealed class RolesBackfillCommandTests : IDisposable
private static PerformanceStat Stat(EquippableItemData design, string field) =>
Assert.Single(CatalogRoleTests.StatsOf(design), s => s.Name.EndsWith("." + field)).Stat;
// Everything the command decides about one design: its roles, each stat's range and role, and its sellers' role quality.
// Everything the command decides about one design: its roles, each stat's range, role and exponent, and its sellers' role quality.
private static string Describe(CultCache cache, EquippableItemData design, FactionProductData[] products)
{
static string F(float v) => float.IsInfinity(v) ? v.ToString() : ((double) v).ToString("G5");
var stats = CatalogRoleTests.StatsOf(design).Select(s =>
$"{s.Name} {F(s.Stat.Min)}..{F(s.Stat.Max)} {string.Join("+", CatalogRoleTests.NamedQualityRoles(s.Stat))}");
$"{s.Name} {F(s.Stat.Min)}..{F(s.Stat.Max)} {string.Join("+", s.Stat.Terms.Where(t => t.Source == StatSource.Quality && !string.IsNullOrEmpty(t.Role)).Select(t => $"{t.Role}^{F(t.Exponent)}"))}");
var sellers = products.Where(p => p.Design.Key.Equals(cache.RefOf(design).Key)).OrderBy(p => p.Name, StringComparer.Ordinal)
.Select(p => $"{p.Name}: " + string.Join(", ", (p.Roles ?? new List<ProductRole>()).OrderBy(r => r.Role, StringComparer.Ordinal)
.Select(r => $"{r.Role} {F(r.Mean)}/{F(r.StandardDeviation)}")));
@ -158,6 +158,57 @@ public sealed class RolesBackfillCommandTests : IDisposable
foreach (var (name, _) in Unnamed) Assert.Equal(_shipped[name], after[name]);
}
// The exponents a first apply authors do not depend on which designs it targets: applied to the base-shaped catalog, the
// command reproduces the shipped exponents of Autocannon and LRMM72, which differ across stats (the mode is not one value).
[Fact]
public void Apply_on_the_base_shaped_catalog_reproduces_the_shipped_exponents()
{
static Dictionary<string, float> Exponents(EquippableItemData d) => CatalogRoleTests.StatsOf(d)
.SelectMany(s => s.Stat.Terms.Where(t => t.Source == StatSource.Quality && !string.IsNullOrEmpty(t.Role)).Select(t => (s.Name, t.Exponent)))
.ToDictionary(x => x.Name, x => x.Exponent);
var designs = new[] { "Autocannon", "LRMM72" };
Dictionary<string, Dictionary<string, float>> Read(string root)
{
var db = AetherDb.Open(root: root);
try { return db.Cache.GetAll<EquippableItemData>().Where(d => designs.Contains(d.Name)).ToDictionary(d => d.Name, Exponents); }
finally { db.Cache.Dispose(); }
}
var shipped = Read(AetherDb.FindRoot());
Assert.All(designs, name => Assert.True(shipped[name].Values.Distinct().Count() > 1, name + " exponents are one value; the test would not tell a mode from a default"));
Reset(flattened: true, unnamed: true);
Assert.Equal(0, Program.RolesBackfill(apply: true, root: _root));
var after = Read(_root);
foreach (var name in designs) Assert.Equal(shipped[name].OrderBy(x => x.Key, StringComparer.Ordinal), after[name].OrderBy(x => x.Key, StringComparer.Ordinal));
}
// A seller that authors one of a design's two roles gets only the other one added, and keeps the one it authored.
[Fact]
public void A_seller_with_one_of_two_roles_authored_gains_only_the_missing_role()
{
Reset(flattened: false, unnamed: false);
var db = AetherDb.Open(catalogWritable: true, root: _root);
var autocannon = db.Cache.GetAll<WeaponItemData>().Single(weapon => weapon.Name == "Autocannon");
var seller = db.Cache.GetAll<FactionProductData>().OrderBy(p => p.Name, StringComparer.Ordinal).First(p => p.Design.Key.Equals(db.Cache.RefOf(autocannon).Key) && p.Roles.Count == 2);
var kept = seller.Roles.OrderBy(r => r.Role, StringComparer.Ordinal).First();
kept.Mean = .91f;
seller.Roles = new List<ProductRole> { kept };
var sellerName = seller.Name;
var keptRole = kept.Role;
db.Cache.Commit(batch => batch.Upsert(typeof(FactionProductData), seller, db.Cache.RefOf(seller).Key));
db.Cache.Dispose();
Assert.Equal(0, Program.RolesBackfill(apply: true, root: _root));
db = AetherDb.Open(root: _root);
try
{
var after = db.Cache.GetAll<FactionProductData>().Single(p => p.Name == sellerName);
Assert.Equal(new[] { "barrel", "feed mechanism" }, after.Roles.Select(r => r.Role).OrderBy(r => r, StringComparer.Ordinal).ToArray());
Assert.Equal(.91f, after.Roles.Single(r => r.Role == keptRole).Mean, 3);
}
finally { db.Cache.Dispose(); }
}
[Fact]
public void A_design_the_catalog_does_not_hold_refuses_and_lands_nothing()
{

View file

@ -1762,15 +1762,21 @@ public static class Program
.Select((found, i) => found.Length == 1 ? found[0] : throw new InvalidOperationException(
$"Expected exactly one design named \"{BackfillDesigns[i]}\", found {found.Length}.")).ToArray();
// The precedent is read once, before any target changes: the named Quality exponents of every design that declares
// roles, so a design the command names later never leaves the pool another target draws from, and a design that
// starts with no named term contributes nothing.
var precedent = items.Where(i => i.Roles != null && i.Roles.Count > 0)
.SelectMany(i => (i.Behaviors ?? new List<BehaviorData>()).Where(b => b != null)
.SelectMany(b => b.GetType().GetFields().Where(f => f.FieldType == typeof(PerformanceStat)).Select(f => (Kind: KindOf(i), Field: f.Name, Stat: f.GetValue(b) as PerformanceStat))))
.Where(x => x.Stat?.Terms != null && x.Stat.Min != x.Stat.Max)
.SelectMany(x => x.Stat.Terms.Where(t => t.Source == StatSource.Quality && !string.IsNullOrEmpty(t.Role)).Select(t => (x.Kind, x.Field, t.Exponent)))
.ToArray();
float ExponentFor(string kind, string field)
{
var precedent = items.Except(targets).Where(i => KindOf(i) == kind && i.Roles != null && i.Roles.Count > 0)
.SelectMany(i => i.Behaviors ?? new List<BehaviorData>()).Where(b => b != null)
.SelectMany(b => b.GetType().GetFields().Where(f => f.Name == field && f.FieldType == typeof(PerformanceStat)).Select(f => f.GetValue(b) as PerformanceStat))
.Where(s => s?.Terms != null && s.Min != s.Max)
.SelectMany(s => s.Terms.Where(t => t.Source == StatSource.Quality && !string.IsNullOrEmpty(t.Role)).Select(t => t.Exponent))
var modes = precedent.Where(p => p.Kind == kind && p.Field == field).Select(p => p.Exponent)
.GroupBy(e => e).OrderByDescending(g => g.Count()).ThenBy(g => g.Key).Select(g => g.Key).ToArray();
return precedent.Length > 0 ? precedent[0] : 1f;
return modes.Length > 0 ? modes[0] : 1f;
}
var changedDesigns = new List<(EquippableItemData Document, CultRecordKey Key)>();