Advance never spins on a stale run list: starts are validated against the catalog, a dropped run rebuilds the facility's list; the not-runnable drops are pinned

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
soul 2026-11-28 09:23:14 +01:00
commit ba8018d339
3 changed files with 90 additions and 12 deletions

View file

@ -23,7 +23,7 @@ public class FacilityData : BehaviorData
// Production.Advance for what is off screen. Its recipes are the run list of its design, fixed for a run because the catalog is.
public class Facility : Behavior
{
private readonly IReadOnlyList<RecipeData> _runList;
private IReadOnlyList<RecipeData> _runList;
private EntityHold _hold;
// Why the facility advanced no further this step: derived by Advance every step, never stored on the unit.
@ -47,6 +47,10 @@ public class Facility : Behavior
var idle = Item.EquippableItem.Run == null || Item.EquippableItem.Run.Progress <= 0;
if (idle && Stall == Stall.Inputs && _hold.Revision == _failedAt) return true;
Attempts++;
// A running cycle whose recipe left the catalog or stopped running here is dropped by Advance: the list it was built from is
// stale, so it is rebuilt from the catalog before that step.
if (!idle && Production.Live(ItemManager.ItemData, Item.EquippableItem.Run.Recipe) == null)
_runList = Production.RunList(ItemManager.ItemData, Item.EquippableItem.Data.Key);
var (run, stall) = Production.Advance(ItemManager.ItemData, _runList, Item.EquippableItem.Run, dt, _hold);
Item.EquippableItem.Run = run;
Stall = stall;

View file

@ -36,6 +36,13 @@ public static class Production
public static bool Runs(CultCache catalog, RecipeData recipe) =>
!Recipes.IsRaw(catalog, recipe) && catalog.Get(recipe.Output) is SimpleCommodityData;
// The recipe a run names as the catalog holds it now, or null when the catalog no longer has it or no longer runs it here.
public static RecipeData Live(CultCache catalog, CultRecordRef<RecipeData> recipe)
{
var held = catalog.Get(recipe);
return held != null && Runs(catalog, held) ? held : null;
}
// The recipes a facility design runs, in recipe-key order. Derived from the catalog, never stored.
public static IReadOnlyList<RecipeData> RunList(CultCache catalog, CultRecordKey facilityDesign) =>
Recipes.RunOn(catalog, facilityDesign).Where(recipe => Runs(catalog, recipe)).ToArray();
@ -78,10 +85,15 @@ public static class Production
// time; at Progress >= Cycle it stores the output and goes idle, the recipe staying as the cursor. A store the hold refuses
// holds the batch at Cycle (Room) and retries on the next call. Time left after a cycle ends runs the next one in the same call,
// so a long span runs several cycles (the rule that makes one span equal as many steps as it covers); time that no cycle can start is
// dropped. A facility with nothing to start writes nothing.
// dropped. A facility with nothing to start writes nothing.
public static (FacilityRun run, Stall stall) Advance(CultCache catalog, IReadOnlyList<RecipeData> runList, FacilityRun run,
double seconds, IProductionHold hold)
{
// A run whose recipe the catalog no longer has, or no longer runs here (a save outliving a catalog edit), is dropped: its
// progress is gone and the inputs it took stay consumed, as they are taken whole at cycle start and the recipe that named
// them is not there to return them against. The facility then starts its next recipe. Nothing below can meet a run that is not
// live: a start is of a recipe the catalog holds and runs, so Advance never throws on one and never spins.
if (run != null && run.Progress > 0 && Live(catalog, run.Recipe) == null) run = null;
if (runList.Count == 0) return (run, Stall.Unrunnable);
while (true)
{
@ -92,7 +104,10 @@ public static class Production
RecipeData start = null;
for (var step = 1; step <= runList.Count && start == null; step++)
{
var candidate = runList[(cursor + step + runList.Count) % runList.Count];
// The list may be older than the catalog: a candidate starts only as the catalog now holds it and runs it, so a
// started run is always one the next pass of this loop resolves, and a pass over the whole list ends the loop.
var candidate = Live(catalog, new CultRecordRef<RecipeData>(RecipeData.KeyOf(runList[(cursor + step + runList.Count) % runList.Count].Output.Key)));
if (candidate == null) continue;
var units = Take(catalog, candidate, hold);
if (units != null && hold.TryTake(units)) start = candidate;
}
@ -100,15 +115,7 @@ public static class Production
run = new FacilityRun { Recipe = new CultRecordRef<RecipeData>(RecipeData.KeyOf(start.Output.Key)), Progress = 0 };
}
var recipe = catalog.Get(run.Recipe);
// A run whose recipe the catalog no longer has, or no longer runs here (a save outliving a catalog edit), is dropped: its
// progress is gone and the inputs it took stay consumed, as they are taken whole at cycle start and the recipe that named
// them is not there to return them against. The facility then starts its next recipe, and Advance never throws on it.
if (recipe == null || !Runs(catalog, recipe))
{
run = null;
continue;
}
var recipe = Live(catalog, run.Recipe);
var progress = run.Progress;
if (progress < recipe.Cycle)
{

View file

@ -471,6 +471,73 @@ public sealed class FacilityProductionTests : IDisposable
Assert.Equal(0, Held(restored, IronFillers[0]));
}
private void Edit(CultRecordKey recipe, Action<RecipeData> change)
{
var held = _cache.Get<RecipeData>(recipe);
change(held);
_cache.Upsert(typeof(RecipeData), held, recipe);
}
private static T Within<T>(Func<T> work)
{
var task = System.Threading.Tasks.Task.Run(work);
Assert.True(task.Wait(TimeSpan.FromSeconds(20)), "Advance did not return: it spins on a stale run list");
return task.Result;
}
// A run list built before the catalog changed names a recipe the catalog no longer holds, or holds as a raw one: nothing starts
// from it, the stock is untouched, the call returns, and the recipes that still run still start.
[Theory]
[InlineData(true)]
[InlineData(false)]
public void AStaleRunListStartsNothingAndReturns(bool removed)
{
var world = Build();
var runList = Production.RunList(_cache, _cache.RefOf<ItemData>(_cache.GetByName<GearData>("Refinery")).Key);
Assert.Contains(runList, recipe => recipe.Output.Key.Equals(Iron));
var ironKey = RecipeData.KeyOf(Iron);
if (removed) _cache.Remove(ironKey);
else Edit(ironKey, recipe => recipe.RoleInputs.Clear());
Stock(world, IronFillers[0], 3);
var (run, stall) = Within(() => Production.Advance(_cache, runList, null, 1200, world.Hold));
Assert.Null(run);
Assert.Equal(Stall.Inputs, stall);
Assert.Equal(3, Held(world, IronFillers[0]));
Assert.Equal(0, Held(world, Iron));
var copperOre = Recipes.Fillers(_cache, "copper ore").Except(IronFillers).First();
Stock(world, copperOre, 4);
(run, _) = Within(() => Production.Advance(_cache, runList, null, 10, world.Hold));
Assert.Equal(RecipeData.KeyOf(new CultRecordKey("commodity:copper")), run.Recipe.Key);
}
// A running cycle whose recipe the catalog changed under it is dropped, whatever the change: the recipe removed, made raw (it takes
// nothing now) or given a compound output (a graded output needs a lot). The facility's list is rebuilt and it moves on.
[Theory]
[InlineData("removed")]
[InlineData("raw")]
[InlineData("compound")]
public void ARunningCycleWhoseRecipeStoppedRunningIsDropped(string change)
{
var world = Build();
Stock(world, IronFillers[0], 3);
Step(world, 101);
Assert.Equal(RecipeData.KeyOf(Iron), world.Unit.Run.Recipe.Key);
var ironKey = RecipeData.KeyOf(Iron);
if (change == "removed") _cache.Remove(ironKey);
else if (change == "raw") Edit(ironKey, recipe => recipe.RoleInputs.Clear());
else Edit(ironKey, recipe => recipe.Output = new CultRecordRef<ItemData>(_cache.RefOf<ItemData>(_cache.GetAll<CompoundCommodityData>().First()).Key));
Step(world, 2);
Assert.Null(world.Unit.Run);
Assert.Equal(0, Held(world, Iron));
Assert.Equal(0, Held(world, IronFillers[0]));
Stock(world, IronFillers[0], 3);
Step(world, 2);
Assert.Null(world.Unit.Run);
}
// Recipes.Fillers reads an index built once per catalog; it is rebuilt when a recipe changes.
[Fact]
public void FillersFollowTheCatalogThroughAChange()