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.

test(sim): pin Prisoners.Return's refusal on a Historical ledger; #1706 hand-off note (#2971 follow-up) (#2979)

Eyal Golancdd9ba376461
diff --git a/docs/rules/mvp.md b/docs/rules/mvp.md
index 8dc4e4427..cdbb8d004 100644
--- a/docs/rules/mvp.md
+++ b/docs/rules/mvp.md
@@ -404,7 +404,7 @@ R-POW-003: Prisoners are never labour, Money, Arms, housing occupants, jobs or a
R-POW-004: An event with `prisoners: <front>` (egypt, jordan, lebanon or syria) is that front's exchange card. It fires only while the front holds prisoners either way, a stored 0 holding nobody (the Jordan card also returns the Iraqi prisoners: the Jordan agreement took over the Iraqi sector, Art. VI, Q-OBJ-1; the ALA and the local forces belong to no front, so no card returns theirs). It has exactly one option and resolves without an RNG draw, like an objective's card. Resolving it moves its sides' prisoners from `PrisonersHeld` to `PrisonersReturned` and adds `mvp.points_per_prisoner_unit_returned` (real value 10) to the score for each population unit returned (Q-POW-2). Nothing scores at the capture (impact plan §4.3 item 3). It holds unchanged under gdd-0.3, played with a synthetic exchange card: a returned unit scores 10 (owner, 1 November 2026, O7: "10 per unit, main (Recommended)"; `PrisonersGdd03Tests.AReturnedUnitScoresTenPoints`); the card fires once, so prisoners taken after it has resolved stay held to the end of the game with no points (27M-19; `PrisonersGdd03Tests.PrisonersTakenAfterTheCardStayHeldWithNoPoints`); and a loaded save pays no return twice (`PrisonersGdd03Tests.ASaveAndReloadCannotPayTwice`).
-R-POW-005: A surrendered unit of the player's is added to `GameState.SoldiersHeld` under the side of the engagement's lowest-id attacker. In step 7, after the prisoners eat, the Treasury pays `mvp.held_soldier_cost_per_turn` (real value IL 0.005m) for each population unit held, borrowing what it lacks (R-MONEY-003; Q-POW-4). They come home when that side's front's exchange card resolves, with no score effect. On the dark start's Historical ledger (#1710, #1725 PR 7) the surrender also makes the formation's serving soldier records Captured, in one validated ledger commit after every check. Their captor is that same side: every enemy side with military units (a `unit_types.yaml` side other than `israel` and `""`) is bound as a captor when the Historical ledger is initialized (owner, 8 November 2026: "Winning side, set at start (Recommended)"). Their Home is the formation's region, region only ("Formation's region (Recommended)"). Each keeps its id, history and issued military item, unchanged while held; what happens to the item on return is #2473's (docs/GDD.md Q10; "Stays on record (Recommended)"). `GameState.SoldiersHeld` keeps its count beside the records, and per captor side it must equal the Captured records with that captor: Take and Upkeep check this before any write ("Keep count + check (Recommended)"). The held soldiers keep this Money cost ("Yes, as today (Recommended)"; docs/gdd/military-combat.md calls the exemption non-operative). A surrender whose serving records do not number its strength, to a side that is not a bound captor, or of a unit that is not a `units_1948.yaml` formation (a trained formation, whose records come with #2964), is refused: Take throws before it writes anything. Called from combat, the throw comes after the engagement's draw and losses and after any earlier engagement of the same command or turn; on a direct call to `Combat.Strike` or `Combat.Resolve` those writes stay on the state object given. In the game that object is `Simulation.Step`'s clone (`sim/Statecraft.Sim/Simulation.cs:51`; combat's callers are `Simulation.cs:268` and `TurnResolver.cs:20`), so the whole command's or turn's result is discarded and the caller's state is unchanged. Today Step refuses an initialized ledger (`Simulation.cs:48`) and `Combat.Resolve` refuses one at its entry, so only a direct `Combat.Strike` reaches the refusal in combat (`LedgerPrisonersTests.ARefusedCaptureInCombatThrowsAndAPriorCloneIsUnchanged`).
+R-POW-005: A surrendered unit of the player's is added to `GameState.SoldiersHeld` under the side of the engagement's lowest-id attacker. In step 7, after the prisoners eat, the Treasury pays `mvp.held_soldier_cost_per_turn` (real value IL 0.005m) for each population unit held, borrowing what it lacks (R-MONEY-003; Q-POW-4). They come home when that side's front's exchange card resolves, with no score effect. On the dark start's Historical ledger (#1710, #1725 PR 7) the surrender also makes the formation's serving soldier records Captured, in one validated ledger commit after every check. Their captor is that same side: every enemy side with military units (a `unit_types.yaml` side other than `israel` and `""`) is bound as a captor when the Historical ledger is initialized (owner, 8 November 2026: "Winning side, set at start (Recommended)"). Their Home is the formation's region, region only ("Formation's region (Recommended)"). Each keeps its id, history and issued military item, unchanged while held; what happens to the item on return is #2473's (docs/GDD.md Q10; "Stays on record (Recommended)"). `GameState.SoldiersHeld` keeps its count beside the records, and per captor side it must equal the Captured records with that captor: Take and Upkeep check this before any write ("Keep count + check (Recommended)"). The held soldiers keep this Money cost ("Yes, as today (Recommended)"; docs/gdd/military-combat.md calls the exemption non-operative). A surrender whose serving records do not number its strength, to a side that is not a bound captor, or of a unit that is not a `units_1948.yaml` formation (a trained formation, whose records come with #2964), is refused: Take throws before it writes anything. Called from combat, the throw comes after the engagement's draw and losses and after any earlier engagement of the same command or turn; on a direct call to `Combat.Strike` or `Combat.Resolve` those writes stay on the state object given. In the game that object is `Simulation.Step`'s clone (`sim/Statecraft.Sim/Simulation.cs:51`; combat's callers are `Simulation.cs:268` and `TurnResolver.cs:20`), so the whole command's or turn's result is discarded and the caller's state is unchanged. Today Step refuses an initialized ledger (`Simulation.cs:48`) and `Combat.Resolve` refuses one at its entry, so only a direct `Combat.Strike` reaches the refusal in combat (`LedgerPrisonersTests.ARefusedCaptureInCombatThrowsAndAPriorCloneIsUnchanged`). `Prisoners.Return` still refuses any initialized ledger, a Historical one included (`LedgerPrisonersTests.ReturnStillRefusesAHistoricalLedger`): it removes the `SoldiersHeld` count but moves no Captured record, so the change that lifts that refusal (#2473) must move the Captured records back in the same commit. Hand-off to #1706: in combat the defenders' losses lower the surrendering unit's strength before `Prisoners.Take` sees its records, and no ledger loss is written for them, so before `Combat.Resolve` runs on a ledger, #1706 must write those losses to the ledger first or Take refuses the surrender.
Under gdd-0.3 (#1710), the prisoners in these pins come from a real surrender (R-POW-001). `PrisonersGdd03Tests.PrisonersEatAndAFoodShortfallCostsCohesion` pins R-POW-002, `PrisonersGdd03Tests.PrisonersAreNeverAResource` pins R-POW-003, and `PrisonersGdd03Tests.OwnSoldiersAreCapturedCostMoneyAndComeHome` pins R-POW-005. The real per-front exchange cards wait on #751, so the return and homecoming pins use the synthetic card until those cards land. On the dark start, `LedgerPrisonersTests` pins R-POW-001, R-POW-002 and R-POW-005 against the aggregate start with the same counts.
diff --git a/sim/Statecraft.Sim.Tests/LedgerPrisonersTests.cs b/sim/Statecraft.Sim.Tests/LedgerPrisonersTests.cs
index 8fb84f277..ac365a54a 100644
--- a/sim/Statecraft.Sim.Tests/LedgerPrisonersTests.cs
+++ b/sim/Statecraft.Sim.Tests/LedgerPrisonersTests.cs
@@ -218,16 +218,16 @@ public class LedgerPrisonersTests
Assert.Equal(before, s.Describe()); Assert.Equal(hash, s.Hash()); Assert.Empty(why);
}
- // Where Take's refusal can be reached from combat, and what it leaves. Combat.Resolve (step 2, TurnResolver.cs:20) refuses
- // any initialized ledger at its entry (Combat.cs:267), before any engagement, so on this branch only the player's instant
- // attack, Combat.Strike (its one caller is Step, Simulation.cs:268), can reach Prisoners.Take on a dark start. Here an
+ // Where Take's refusal can be reached from combat, and what it leaves. Combat.Resolve (step 2, called by TurnResolver) refuses
+ // any initialized ledger at its entry, before any engagement, so on this branch only the player's instant
+ // attack, Combat.Strike (its one caller is Step, through Simulation.RunOrders), can reach Prisoners.Take on a dark start. Here an
// enemy unit boxed in by own units surrenders to a Strike while SoldiersHeld disagrees with the Captured records, so Take
// throws InvalidOperationException before it writes anything. The throw comes after the Strike's own writes (the
// attacker's order and the engagement's draw and losses), and those stay on the state object a direct call is given. In
- // the game that object is never the caller's: Step works on a clone (Simulation.cs:51), so the throw discards the whole
+ // the game that object is never the caller's: Step works on a clone, so the throw discards the whole
// command's result and Step's caller is unchanged. A Clone() taken before the call pins that isolation. Step itself
// cannot be driven there: the refusal needs an initialized Historical ledger, and Step refuses any initialized ledger
- // at Simulation.cs:48, while a game without a ledger skips every ledger check in Take.
+ // before it clones, while a game without a ledger skips every ledger check in Take.
[Fact]
public void ARefusedCaptureInCombatThrowsAndAPriorCloneIsUnchanged()
{
@@ -284,6 +284,24 @@ public class LedgerPrisonersTests
Assert.Contains(lines, x => x.Key == WhyKeys.Engagement);
}
+ // R-POW-005, #2473: Prisoners.Return still refuses a Historical ledger. It removes a front's SoldiersHeld count but moves
+ // no Captured record, so on a dark start after a real capture it must throw before any write; lifting that refusal must
+ // move the records home in the same change, or the next Take or Upkeep finds the count and the records disagree.
+ [Fact]
+ public void ReturnStillRefusesAHistoricalLedger()
+ {
+ var c = Gdd03();
+ var s = Dark(c);
+ Take(c, s, Own(c, s)[0], Enemy(s, "egypt"));
+ Assert.True(s.SoldiersHeld["egypt"] > 0);
+ Assert.True(s.PopulationLedger.IsHistorical);
+ var before = s.Describe(); var hash = s.Hash(); var ledger = s.PopulationLedger.Canonical().ToList(); var why = new List<Cause>();
+ Assert.Contains("integration is held", Assert.Throws<NotSupportedException>(() => Prisoners.Return(c, s, "egypt", why)).Message);
+ Assert.Equal(before, s.Describe()); Assert.Equal(hash, s.Hash());
+ Assert.Equal(ledger, s.PopulationLedger.Canonical()); Assert.Empty(why);
+ AssertHeldMatchesRecords(s);
+ }
+
// Determinism: Take and Upkeep draw no RNG; two dark games given the same captures and upkeep end with the same hash and
// ledger text, and a capture changes the hash.
[Fact]
diff --git a/sim/Statecraft.Sim/Combat.cs b/sim/Statecraft.Sim/Combat.cs
index 09b9fdbbb..43350a325 100644
--- a/sim/Statecraft.Sim/Combat.cs
+++ b/sim/Statecraft.Sim/Combat.cs
@@ -388,12 +388,18 @@ namespace Statecraft.Sim
// The only way a unit is captured (impact plan section 4.3 item 1; R-POW-001). Its captor is the engagement's
// lowest-id attacker (not attackers[0]: the guns are listed last, R-COMBAT-007), whose side holds the
// player's soldiers (R-POW-005). Take runs before the unit is removed (#1710). On a Historical ledger game
- // it may refuse: it throws InvalidOperationException before it writes anything, but this engagement's draw
- // and losses, and any earlier write to s (an earlier engagement, or the Strike's own), stay on s. In the
- // game s is always a clone: Strike's caller (Simulation.cs:268) and Resolve's (TurnResolver.cs:20) run
- // inside Simulation.Step on its clone (Simulation.cs:51; Preview and DelegatedTo clone too), so the whole
- // command's or turn's result is discarded and the caller's state is unchanged. Today Step refuses a ledger
- // game (Simulation.cs:48), and Resolve refuses one at its entry.
+ // it may refuse, before it writes anything: InvalidOperationException for a held count that disagrees with
+ // the Captured records, a unit that is not a units_1948 formation or a strength its records do not number,
+ // and ArgumentException from PopulationLedger.Capture for an unbound captor or a record that is not an
+ // active soldier (a synthetic or Open ledger is NotSupportedException). This engagement's draw and losses,
+ // and any earlier write to s (an earlier engagement, or the Strike's own), stay on s. In the game s is
+ // always a clone: Strike's caller (Simulation.RunOrders) and Resolve's (TurnResolver) run inside
+ // Simulation.Step on its clone (Preview and DelegatedTo clone too), so the whole command's or turn's result
+ // is discarded and the caller's state is unchanged. Today Step refuses a ledger game before it clones, and
+ // Resolve refuses one at its entry.
+ // Hand-off to #1706: the defenders' combat losses above have already lowered u.Strength, and no ledger
+ // loss is written for them, so Take sees records that still number the strength before the losses and
+ // refuses. When #1706 runs Resolve on a ledger, it must write those losses to the ledger before this call.
var held = Prisoners.Take(c, s, u, attackers.OrderBy(x => x.Id).First());
s.Units.Remove(u.Id);
why.Add(UnitLines.Line(u.Side == Movement.PlayerSide ? WhyKeys.SurrenderedHeld : WhyKeys.SurrenderedPrisoners, // Prisoners.Take's two cases