Skip to content

A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.

feat(sim): the Food term runs on a Historical ledger game (#1704, #1725 PR 7) (#2965)

Eyal Golanfa721ee6f014
diff --git a/changelog.d/1704-foodterm.md b/changelog.d/1704-foodterm.md
new file mode 100644
index 000000000..3484c5950
--- /dev/null
+++ b/changelog.d/1704-foodterm.md
@@ -0,0 +1,3 @@
+- GDD 0.3 starting population (Refs #1704, Refs #1725): food_supply's Food stock term (R-FOOD-003) now runs on the
+ inactive dark start's Historical ledger and gives what the aggregate start gives for the same Food state. It reads no
+ record, so no ledger record changes. No new-game wiring, so there is no change in the game.
diff --git a/docs/population-ledger.md b/docs/population-ledger.md
index 8b736613b..a132348ad 100644
--- a/docs/population-ledger.md
+++ b/docs/population-ledger.md
@@ -1,6 +1,6 @@
# Population identity foundation — #1689 steps 1–2
-This is a fixture-only data/helper contract under exact `gdd-0.3` identity (R-POP-001/002/003), not a playable campaign. No production era, shipped content, catalogue or save envelope changes. The existing synthetic Attention/Arms harnesses still exercise their pre-ledger aggregate model; that does not activate population identity. A state with an initialized ledger is rejected by Simulation.Step, Preview and DelegatedTo and at direct aggregate population/resource producer boundaries before any mutation (PopulationLedger.RefuseAggregate). The guarded sites include Cities, PopulationSystem, Pools, starting forces, training, qualification/demobilisation, military/combat/prisoners, event mutation, the economy and objectives (the readers of GameState.TotalPopulation, which reads the ledger once it is initialized) and the turn resolver. Direct integration into Cities, PopulationSystem, Training, Combat, Prisoners, Pools and other systems remains held, except the step-7 Food settlement given a month's synthetic context: the internal `Pools.Consume` overload reads its need from the month's frozen opening snapshot (`Gdd03SyntheticMonthContext.OpeningPeople`), one ration per living own population id in it (R-FOOD-002, #2470). The public `Pools.Consume`, `Pools.Init`, `Harvest`, `BuyArms` and `FoodTerm` still refuse.
+This is a fixture-only data/helper contract under exact `gdd-0.3` identity (R-POP-001/002/003), not a playable campaign. No production era, shipped content, catalogue or save envelope changes. The existing synthetic Attention/Arms harnesses still exercise their pre-ledger aggregate model; that does not activate population identity. A state with an initialized ledger is rejected by Simulation.Step, Preview and DelegatedTo and at direct aggregate population/resource producer boundaries before any mutation (PopulationLedger.RefuseAggregate). The guarded sites include Cities, PopulationSystem, Pools, starting forces, training, qualification/demobilisation, military/combat/prisoners, event mutation, the economy and objectives (the readers of GameState.TotalPopulation, which reads the ledger once it is initialized) and the turn resolver. Direct integration into Cities, PopulationSystem, Training, Combat, Prisoners, Pools and other systems remains held, except the step-7 Food settlement given a month's synthetic context: the internal `Pools.Consume` overload reads its need from the month's frozen opening snapshot (`Gdd03SyntheticMonthContext.OpeningPeople`), one ration per living own population id in it (R-FOOD-002, #2470). The public `Pools.Consume` and `Harvest` still refuse; `Pools.Init`, `BuyArms` and `FoodTerm` run only on a Historical ledger (#1725 PR 5 and PR 7; R-FOOD-003, #1704) and refuse a synthetic or Open one.
`PopulationLedger.CreateFixture` is internal (test assembly friend access), constructs a fresh GameState without Simulation.NewGame, validates explicit frozen location/target/captor/qualification bindings and builds inventory in source-key/ordinal order. It never converts census headcounts, historical formations, cohorts or events into people. Fixture rows are visibly synthetic, supplied in test code; no new files are loaded or hashed as content. Each reception tick is one whole reception turn, capped by the Agreed catalogue `reception.turns`; course limits are explicit synthetic target data, not production defaults.
diff --git a/docs/rules/mvp.md b/docs/rules/mvp.md
index 631b7e60a..16af3ecfd 100644
--- a/docs/rules/mvp.md
+++ b/docs/rules/mvp.md
@@ -454,7 +454,7 @@ R-FOOD-002: In step 7 the nation eats the turn's need from the stock as far as i
For R-FOOD-001 and R-FOOD-002 under gdd-0.3 with an initialized population ledger (#2470), the need is the month's: one ration for each living own population id in the month's frozen opening snapshot (the #1716 synthetic month context, `OpeningPeople`), whatever its status or assignment (idle, at work, teaching, on a course, travelling, serving, in reception or held by the enemy), each id once and in any storage order, so a ledger change before step 7 (a combat loss, an arrival) does not move that month's need. The live ledger is reconciled first, so an invalid one moves no Food. The settlement after it is the one above: the stock stops at 0, `FoodNeed` and `FoodUnmet` record that need, and the import refills to `cover_months` of it unless the foreign-currency crisis is on. Counting changes no id, status or assignment. Legacy and gdd-0.1 keep the aggregate head count. Counting a captured own soldier is the owner's decision (5 November 2026, the recommended option, [recorded on #2678](https://github.com/eyalgolan/statecraft-israel/pull/2678#issuecomment-6652800885) as "Captured own-population records do eat the national ration"), the reading of `docs/gdd/balance.md` `population.food_per_turn` ("Food per own population unit, every status"); the ledger's head count (`TotalPeople`) already includes them. No turn reaches this yet: only synthetic fixtures create a month context, and without one an initialized ledger is refused (R-POP-002).
-R-FOOD-003: `food_supply` stays the 0 to 1 level the UI, the situations and the events read (Q-POOL-3, decided 15 October 2026), with a stock term inside its effect sum: `k_food * (stock - full) / full - unmet / need`, where `full` is `cover_months` of the last need. The term is exactly 0 at a full stock, `-k_food` when the stock is empty, and the unmet share lowers it further; rationing, the other effects and event shifts act on top as before, and the level's clamp to 0..1 bounds the result. The GDD loop requires the `food_supply` indicator.
+R-FOOD-003: `food_supply` stays the 0 to 1 level the UI, the situations and the events read (Q-POOL-3, decided 15 October 2026), with a stock term inside its effect sum: `k_food * (stock - full) / full - unmet / need`, where `full` is `cover_months` of the last need. The term is exactly 0 at a full stock, `-k_food` when the stock is empty, and the unmet share lowers it further; rationing, the other effects and event shifts act on top as before, and the level's clamp to 0..1 bounds the result. The GDD loop requires the `food_supply` indicator. The term runs on a game with an initialized Historical ledger (the dark start, R-POP-002; #1704, #1725 PR 7) as in the aggregate game: it reads only the stock, the last need and the unmet share, never a record, and the need is already the ledger's (R-FOOD-001), so no ledger record changes; a synthetic or Open ledger is still refused (R-POP-002).
## Arms
@@ -605,7 +605,7 @@ These contracts cover #1689 steps 1–2 and the Historical start inventory (R-PO
R-POP-001: Only exact `gdd-0.3` enables the population-ledger capability, independently of the broad GDD loop and Attention (R-LOOP-002/005). An immutable own-population record has a dedicated nonnegative ordinal id. Synthetic fixture inventory is ordered by source key (ordinal string comparison), then within-source ordinal. Registered arrivals allocate monotonically; only explicit combat losses remove living ids. Sources remain registered after loss, and ids are never reused. Reconciliation checks the exact created-minus-lost id set against the living ledger, not just equal totals. Status/region counts include every matching record once. The gdd-0.3 scale of a synthetic or Open fixture reads one positive Agreed integer from `population.people_per_unit` in its catalogue (currently 10,000, Open mode's scale under D2); a Historical fixture reads `rules.yaml`'s `people_per_unit` for the start day (1,000), as R-UNIT-006 does (owner, 1 November 2026, D2: "Approve all six (Recommended)", "The Historical MVP keeps main's start: census cities at 1,000 people a unit (`R-CITY-007`)"; R-POP-004); rows with a `census/`, `units_1948/` or `field/` source key are the Historical start's and are refused in any other fixture. Old identities retain the common rules scale. Missing, fractional, nonpositive, overflowing or Deferred values have no fallback, in either source.
-R-POP-002: A ledger record has exactly one typed status and assignment, validated location, home and captor references, whole reception-turn ticks capped by the catalogue requirement, civilian profession-specific tier and actual-work XP, separate military qualification/combat XP, and distinct issued civilian/military equipment and paid facts. Unknown references, unresolved teacher ids, incompatible statuses/assignments, inconsistent region/hex/facility bindings, invalid progress/tiers and duplicate issued-item ownership are refused before mutation. Moves and atomic status/location/assignment transitions retain identity/history. Capture retains home context without inventing a prison hex or refunding equipment. Ledger changes never touch resources, causes, RNG or Attention. Initialization is internal and fixture-only; initialized ledger fixtures refuse aggregate Simulation commands and turns. The dark start (#1725 PR 5) is the internal, fixture-only `Simulation.NewGame` overload that initializes the Historical ledger (R-POP-004) in place before `Pools.Init`: `Pools.Init` then reads the ledger's need, one ration for each living own population id (R-FOOD-001, #2470), after the ledger reconciles, and `StartingForces.Init` places the forces once each own starting formation's strength is its ledger soldiers, refusing otherwise. Both read only a ledger initialized with mode Historical, the dark start's; a synthetic or Open ledger is refused by both, as by the other aggregate systems, with the state unchanged. An aggregate system whose own rule says it runs on that Historical ledger (#1725 PR 7, civilian upkeep, R-MONEY-002, the first) also reads only that ledger; a synthetic or Open ledger is still refused by it. `EventSystem.Trigger` still refuses a ledger, so a dark start fires no opening event; the public `NewGame` is unchanged. The step-7 Food settlement given a month's synthetic context is another exception to that refusal: it reads its need from that month's frozen snapshot (R-FOOD-002, #2470). Old identities refuse ledger mutation. Prices, progression awards, return destinations, tool lock-in and captive-equipment outcomes remain with their owning issues. A craftsman's tier is novice, apprentice or expert (R-CRAFT-003); a farmer's or pioneer's is novice, trained or veteran.
+R-POP-002: A ledger record has exactly one typed status and assignment, validated location, home and captor references, whole reception-turn ticks capped by the catalogue requirement, civilian profession-specific tier and actual-work XP, separate military qualification/combat XP, and distinct issued civilian/military equipment and paid facts. Unknown references, unresolved teacher ids, incompatible statuses/assignments, inconsistent region/hex/facility bindings, invalid progress/tiers and duplicate issued-item ownership are refused before mutation. Moves and atomic status/location/assignment transitions retain identity/history. Capture retains home context without inventing a prison hex or refunding equipment. Ledger changes never touch resources, causes, RNG or Attention. Initialization is internal and fixture-only; initialized ledger fixtures refuse aggregate Simulation commands and turns. The dark start (#1725 PR 5) is the internal, fixture-only `Simulation.NewGame` overload that initializes the Historical ledger (R-POP-004) in place before `Pools.Init`: `Pools.Init` then reads the ledger's need, one ration for each living own population id (R-FOOD-001, #2470), after the ledger reconciles, and `StartingForces.Init` places the forces once each own starting formation's strength is its ledger soldiers, refusing otherwise. Both read only a ledger initialized with mode Historical, the dark start's; a synthetic or Open ledger is refused by both, as by the other aggregate systems, with the state unchanged. An aggregate system whose own rule says it runs on that Historical ledger (#1725 PR 7: civilian upkeep, R-MONEY-002, the first, and food_supply's stock term, R-FOOD-003) also reads only that ledger; a synthetic or Open ledger is still refused by it. `EventSystem.Trigger` still refuses a ledger, so a dark start fires no opening event; the public `NewGame` is unchanged. The step-7 Food settlement given a month's synthetic context is another exception to that refusal: it reads its need from that month's frozen snapshot (R-FOOD-002, #2470). Old identities refuse ledger mutation. Prices, progression awards, return destinations, tool lock-in and captive-equipment outcomes remain with their owning issues. A craftsman's tier is novice, apprentice or expert (R-CRAFT-003); a farmer's or pioneer's is novice, trained or veteran.
R-POP-003: Clone copies all ledger containers; immutable records and frozen bindings may be shared. The canonical writer emits a gdd-0.3-only section ordered by numeric population id, with fixed field order, stable machine tokens, invariant integer formatting and length-prefixed reference strings. It covers every record/assignment/equipment/reference field, binding, creation/loss provenance and allocator even after the last combat loss. Legacy/gdd-0.1 emit no ledger section or allocator marker. A location's optional city (a `regions.yaml` member location of its region that is a census city of `cities.yaml` or one of that region's field hosts, where R-POP-004 houses own people: a settlement hosts only F19 and F20, owner, 7 November 2026, #1689: "These two only (Recommended)"; any other, including a locality, a `sites.yaml` id or a map location, is refused when a fixture is created, naming the city and the region, R-POP-002) is written after its other fields as `|city=` with a length-prefixed id, only when set, as R-UNIT-002's `|home=` is, so a location without a city keeps its canonical text and hash. R-SAVE-001/002 still govern identity mismatch refusal and replay: no ledger snapshot or new command syntax is added in this foundation, and fixture injection is not replay evidence. Existing unpublished schema-4 draft hashes change; no migration promise is made.
diff --git a/sim/Statecraft.Sim.Tests/LedgerFoodTermTests.cs b/sim/Statecraft.Sim.Tests/LedgerFoodTermTests.cs
new file mode 100644
index 000000000..589b2cd15
--- /dev/null
+++ b/sim/Statecraft.Sim.Tests/LedgerFoodTermTests.cs
@@ -0,0 +1,84 @@
+using Statecraft.Sim;
+
+/// <summary>#1704, #1725 PR 7: food_supply's stock term (R-FOOD-003, Pools.FoodTerm) runs on a game with an initialized
+/// Historical ledger, the dark start and the ledger fixture. It reads only the Food stock, the last need and the unmet
+/// share, never a record, so it returns what the aggregate game returns for the same Food state, and the ledger and the
+/// hash are unchanged. A synthetic or Open ledger is still refused (R-POP-002). Step 7's Pools.Consume is not in scope.</summary>
+public class LedgerFoodTermTests
+{
+ static Content Gdd03() => HistoricalPopulationTests.Real(true);
+
+ static GameState Dark(Content c) => Simulation.NewGame(c, 1, Mode.Historical, darkStart: true);
+
+ // R-FOOD-003, R-FOOD-001: the dark start's stock is cover_months of the ledger's need, so the term is exactly 0.
+ [Fact]
+ public void TheDarkStartsFoodTermIsZeroAtAFullStock()
+ {
+ var c = Gdd03();
+ var s = Dark(c);
+ Assert.True(s.PopulationLedger.IsHistorical);
+ Assert.True(s.FoodNeed > 0);
+ Assert.Equal((long)c.RulesAt(s.Date).Mvp!.CoverMonths!.Value * s.FoodNeed, s.Food);
+ Assert.Equal(0, s.FoodUnmet);
+ var hash = s.Hash(); var ledger = s.PopulationLedger.Canonical();
+ Assert.Equal(0.0, Pools.FoodTerm(c, s));
+ Assert.Equal(hash, s.Hash());
+ s.PopulationLedger.Reconcile();
+ Assert.Equal(ledger, s.PopulationLedger.Canonical());
+ }
+
+ // R-FOOD-003: with the same Food, need and unmet share, the dark start and the aggregate start of the same content give
+ // the same double, the formula k_food * (stock - full) / full - unmet / need; the ledger and the hash do not move.
+ [Theory]
+ [InlineData(0, 0)] [InlineData(10, 0)] [InlineData(0, 60)] [InlineData(40, 60)] [InlineData(900, 0)]
+ public void TheDarkStartsFoodTermIsTheAggregateGamesTerm(int food, int unmet)
+ {
+ var c = Gdd03();
+ var dark = Dark(c); var pub = Simulation.NewGame(c, 1, Mode.Historical);
+ Assert.Equal(pub.Date, dark.Date);
+ foreach (var s in new[] { dark, pub }) { s.Food = food; s.FoodNeed = 100; s.FoodUnmet = unmet; }
+ var hash = dark.Hash(); var ledger = dark.PopulationLedger.Canonical();
+ var r = c.RulesAt(dark.Date).Mvp!;
+ var full = (long)r.CoverMonths!.Value * 100;
+ var expected = r.KFood!.Value * (food - full) / full - (double)unmet / 100;
+ var term = Pools.FoodTerm(c, dark);
+ Assert.Equal(Pools.FoodTerm(c, pub), term);
+ Assert.Equal(expected, term);
+ Assert.Equal(hash, dark.Hash());
+ dark.PopulationLedger.Reconcile();
+ Assert.Equal(ledger, dark.PopulationLedger.Canonical());
+ }
+
+ // R-FOOD-003: the ledger fixture reads the term too; with no need it is 0, as in the aggregate game.
+ [Fact]
+ public void TheLedgerFixtureReadsTheTerm()
+ {
+ var c = Gdd03();
+ var s = HistoricalPopulation.LedgerFixture(c);
+ s.Date = c.Rules.Start;
+ var ledger = s.PopulationLedger.Canonical();
+ s.FoodNeed = 0;
+ Assert.Equal(0.0, Pools.FoodTerm(c, s));
+ s.Food = 0; s.FoodNeed = 50; s.FoodUnmet = 50;
+ var k = c.RulesAt(s.Date).Mvp!.KFood!.Value;
+ Assert.Equal(-k - 1.0, Pools.FoodTerm(c, s), 12);
+ s.PopulationLedger.Reconcile();
+ Assert.Equal(ledger, s.PopulationLedger.Canonical());
+ }
+
+ // R-POP-002: only a ledger initialized with mode Historical is let through. An initialized Open ledger, like a
+ // synthetic one (no mode), is still refused, with the state unchanged.
+ [Theory]
+ [InlineData(true)] [InlineData(false)]
+ public void AnOpenOrSyntheticLedgerIsStillRefused(bool open)
+ {
+ var c = PopulationLedgerTests.Content();
+ var s = PopulationLedger.CreateFixture(c, PopulationLedgerTests.Bindings(),
+ new[] { new PopulationInventoryRow("fixture", 0, PopulationLedgerTests.Person()) }, mode: open ? Mode.Open : null);
+ Assert.Equal((true, false), (s.PopulationLedger.IsInitialized, s.PopulationLedger.IsHistorical));
+ s.Food = 10; s.FoodNeed = 5; s.FoodUnmet = 1;
+ var before = s.Describe(); var hash = s.Hash();
+ Assert.Contains("integration is held", Assert.Throws<NotSupportedException>(() => Pools.FoodTerm(c, s)).Message);
+ Assert.Equal(before, s.Describe()); Assert.Equal(hash, s.Hash());
+ }
+}
diff --git a/sim/Statecraft.Sim/Pools.cs b/sim/Statecraft.Sim/Pools.cs
index 2afaf6090..764234eda 100644
--- a/sim/Statecraft.Sim/Pools.cs
+++ b/sim/Statecraft.Sim/Pools.cs
@@ -192,10 +192,13 @@ namespace Statecraft.Sim
/// <summary>R-FOOD-003: food_supply's stock term, k_food * (stock - full) / full - unmet / need, where full is
/// cover_months of the last need. It is exactly 0 at a full stock, -k_food when the stock is empty, and the unmet
- /// share lowers it further; food_supply's clamp to 0..1 bounds it. 0 in the legacy loop and with no need.</summary>
+ /// share lowers it further; food_supply's clamp to 0..1 bounds it. 0 in the legacy loop and with no need. It reads
+ /// only the stock, the last need and the unmet share, never a record, so it runs on an initialized Historical ledger
+ /// (the dark start, #1704, #1725 PR 7), where the need is already the ledger's rations (R-FOOD-001). A synthetic or
+ /// Open ledger stays refused (R-POP-002).</summary>
public static double FoodTerm(Content c, GameState s)
{
- PopulationLedger.RefuseAggregate(s);
+ PopulationLedger.RefuseUnlessHistorical(s);
if (!c.IsGdd || s.FoodNeed == 0) return 0.0;
var r = c.RulesAt(s.Date).Mvp!;
var full = (long)r.CoverMonths!.Value * s.FoodNeed;