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.

tools: one golden manifest instead of three hand-edited hash copies (fixes #293) (#297)

agamrafaeli-instinct2b7212ed99c7
diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md
index 10a9b5d6e..d4ab51a6c 100644
--- a/.github/pull_request_template.md
+++ b/.github/pull_request_template.md
@@ -17,7 +17,7 @@
- [ ] Tests cover the change, and they fail without it.
- [ ] Test count did not go down, or this description explains why.
-- [ ] The goldens (`Hash1950`, `Hash1952`) did not move, or this description says why they moved (`docs/determinism.md`).
+- [ ] The goldens (`Hash1950`, `Hash1952`, smoke) did not move, or this description says why they moved and they were re-pinned in `sim/Statecraft.Sim.Tests/golden/manifest.txt` with `STATECRAFT_UPDATE_GOLDEN=1 dotnet test` (`docs/determinism.md`).
- [ ] `CHANGELOG.md` has a line under `## [Unreleased]` if this changes `sim/`, `content/` or `unity/`.
- [ ] Every fact added or changed in `content/` has `sources:` with a checked date (`docs/CONTENT_POLICY.md`).
- [ ] No quote is attributed to a real person unless it is sourced verbatim.
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 687ff8093..013c9593e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,6 +8,8 @@ adds a line under **Unreleased**; CI's `Changelog` job checks this (see `CONTRIB
## [Unreleased]
### Added
+- One golden manifest (`sim/Statecraft.Sim.Tests/golden/manifest.txt`) pins the golden hashes; `STATECRAFT_UPDATE_GOLDEN=1`
+ now rewrites it and the `GoldenRun.cs` constants, and a test names any doc that shows an old hash (fixes #293).
- Era system switches in eras.yaml (`systems`): `legislature: state | assembly` (an assembly's vote against a policy or
the budget is held and reported, but does not bind) and `map_actions` (the actions the era allows). The Cabinet and
Budget screens say when the body's vote does not bind, and the hex panel shows only the era's map actions. With no
@@ -17,6 +19,8 @@ adds a line under **Unreleased**; CI's `Changelog` job checks this (see `CONTRIB
and Ami Assaf as a second Deputy Minister of Education (from 11 June 1956). Event text, policies and effects cannot
name such an office, and the Cabinet refuses to appoint to it. The Cabinet screen shows a card for each holder. No
golden moves (#277).
+ now rewrites it and the `GoldenRun.cs` constants, `tools/art_checkpoint.sh` reads it, and a test names any doc that shows an
+ old hash (fixes #293).
- Rules per era (#58, second piece): the attention, economy, population, security, map-action and cabinet systems read
`RulesAt(date)` of the turn's starting date instead of `rules.yaml` directly, and the Defence and Info screens and the
HUD follow. With no `content/rules/<era>.yaml` files yet, nothing changes: the goldens do not move. A test fails if a
diff --git a/docs/determinism.md b/docs/determinism.md
index f955d387c..2ee6f084a 100644
--- a/docs/determinism.md
+++ b/docs/determinism.md
@@ -42,8 +42,11 @@ uv run tools/web_check.py unity/Builds/Web-<compression> --query "golden&smoke=5
and `hash1952.txt`, written by `GameState.Describe()`, which uses the same writer as `Hash()` (so the two cannot list
different fields) and shows every number as a plain value next to its IEEE bits. If the change is deliberate, run
`STATECRAFT_UPDATE_GOLDEN=1 dotnet test sim/Statecraft.sln`: it rewrites the snapshots, so the PR's diff of them shows
-reviewers what moved. Then update the hash constants in the three places below by hand, and say why in the commit
-message. The snapshot test also fails if a snapshot is stale while the hash is unchanged.
+reviewers what moved. The same command rewrites `sim/Statecraft.Sim.Tests/golden/manifest.txt` (the pinned hashes) and the
+`GoldenRun.cs` constants. That run still fails for every golden that moved, against the value it started with; the tests
+never take a hash from the run without the variable, so a second plain run is the check. Then update the docs below by hand (`GoldenManifestTests` names any place still showing the old
+hash), add a row to the rebaseline history, and say why in the commit message. The snapshot test also fails if a snapshot is
+stale while the hash is unchanged.
**Rules per era.** A system reads `Content.RulesAt(date)`, where `date` is the date of the state the turn starts from (`s.Date`
before the step); `AttentionSystem.Refill` runs after the date moves, so it fills the next turn from that turn's era. Rules
@@ -62,10 +65,12 @@ goldens (playtest 2 changed every line's casing with no hash change). They are s
`FinalWaveTests.AReloadedSaveReproducesTheLastTurnsCauses` checks a reloaded save gives the same last-turn Causes, and
`TheGoldenRunsCausesAreTheSameInEveryCulture` runs the 1952 golden under de-DE and ar-SA against the invariant culture.
-Each golden constant is written in three places that must agree:
-- `sim/Statecraft.Sim/GoldenRun.cs`, which Unity code and players compare against;
-- `sim/Statecraft.Sim.Tests/DeterminismTests.cs`, which pins the value on .NET and asserts the library constant equals it;
-- this file.
+Each golden hash is pinned in `sim/Statecraft.Sim.Tests/golden/manifest.txt` (`hash1950`, `hash1952`, `smoke5`). The .NET tests
+compare against it. These places must agree with it, and `GoldenManifestTests` fails naming the one that does not:
+- `sim/Statecraft.Sim/GoldenRun.cs`, which Unity code and players compare against (the update command rewrites it);
+- this file, `docs/RELEASING.md` and `tools/web_check.py`'s usage example (by hand).
+
+`tools/art_checkpoint.sh` reads its expected hashes from the manifest, so it needs no edit.
`unity/Assets/Tests/EditMode/SimSmokeTests.cs` runs the same playthroughs inside Unity against `GoldenRun`'s constants
(run `tools/build_sim_for_unity.sh` first so Unity has the current DLLs).
@@ -101,7 +106,7 @@ moved. A save made before it therefore loads with the "different game content" w
to the same state.
**Any change to a golden hash is a deliberate rules or content change.** Such a change could be to a rule, a system,
-content in `content/`, or the hash format. When it happens, update every place above in the same commit, add a row to
+content in `content/`, or the hash format. When it happens, update the manifest and every place above in the same commit, add a row to
the history below, and say in the commit message why the hash moved. If a hash changes and you did not mean to change
rules or content, treat it as a determinism bug.
diff --git a/sim/Statecraft.Sim.Tests/DeterminismTests.cs b/sim/Statecraft.Sim.Tests/DeterminismTests.cs
index dcc133f1c..7daa6049c 100644
--- a/sim/Statecraft.Sim.Tests/DeterminismTests.cs
+++ b/sim/Statecraft.Sim.Tests/DeterminismTests.cs
@@ -17,9 +17,9 @@ public class DeterminismTests
Assert.Equal(third, rng.NextULong());
}
- /// <summary>Any change to this value is a deliberate rules or content change: update it here, in
- /// GoldenRun.Hash1950 and in docs/determinism.md in the same commit.</summary>
- public const string GoldenHash = "71e9904d0ee48083db9bbba281f3f046136482ba83ee056e48d57b62e76a5b07";
+ /// <summary>Any change to a golden hash is a deliberate rules or content change. The pinned values are in
+ /// golden/manifest.txt (see GoldenManifest and docs/determinism.md).</summary>
+ static string GoldenHash => GoldenManifest.Expected("hash1950");
/// <summary>Real content, seed 1948, Historical mode, HistoricalAI until the date reaches 1950-01-01.</summary>
public static GameState GoldenPlaythrough(Content c) => GoldenRun.Play(c, GoldenRun.Until1950);
@@ -34,7 +34,7 @@ public class DeterminismTests
}
/// <summary>Same rule for the second golden run, which covers the M3 content range (to 1952-01-01).</summary>
- public const string GoldenHash1952 = "62f8ec61a14acff4eab503381994a7c94d7a2f786dcfde5c4c19439ac8b4c5e8";
+ static string GoldenHash1952 => GoldenManifest.Expected("hash1952");
[Fact]
public void GoldenPlaythroughTo1952HashIsFixed()
@@ -49,7 +49,7 @@ public class DeterminismTests
[Fact]
public void LibraryGoldenConstantsMatchThisTest()
{
- Assert.Equal(GoldenHash, GoldenRun.Hash1950);
- Assert.Equal(GoldenHash1952, GoldenRun.Hash1952);
+ Assert.Equal(GoldenRun.Hash1950, GoldenHash);
+ Assert.Equal(GoldenRun.Hash1952, GoldenHash1952);
}
}
diff --git a/sim/Statecraft.Sim.Tests/GoldenManifest.cs b/sim/Statecraft.Sim.Tests/GoldenManifest.cs
new file mode 100644
index 000000000..2ee5fbfac
--- /dev/null
+++ b/sim/Statecraft.Sim.Tests/GoldenManifest.cs
@@ -0,0 +1,52 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Text.RegularExpressions;
+
+/// <summary>The pinned golden hashes, read from sim/Statecraft.Sim.Tests/golden/manifest.txt (#293). Tests compare against
+/// it, so a deliberate change is one file in the PR diff. STATECRAFT_UPDATE_GOLDEN=1 rewrites it and the constants in
+/// GoldenRun.cs, which Unity players compare against.</summary>
+public static class GoldenManifest
+{
+ static string Root() => Directory.GetParent(RealContentTests.ContentRoot())!.FullName;
+ public static string Path_() => Path.Combine(Root(), "sim", "Statecraft.Sim.Tests", "golden", "manifest.txt");
+
+ public static bool UpdateRequested => Environment.GetEnvironmentVariable("STATECRAFT_UPDATE_GOLDEN") == "1";
+
+ // Test classes run in parallel, so an update run's writes (DeterminismTests, SliceSmokeTests) and reads take turns.
+ static readonly object Gate = new object();
+
+ public static Dictionary<string, string> Read()
+ {
+ lock (Gate) return File.ReadAllLines(Path_()).Select(l => l.Trim()).Where(l => l.Length > 0 && !l.StartsWith("#"))
+ .Select(l => l.Split(' ', StringSplitOptions.RemoveEmptyEntries))
+ .ToDictionary(p => p.Length == 2 ? p[0] : throw new InvalidDataException("manifest line must be 'name hash'"), p => p[1]);
+ }
+
+ public static string Expected(string name) =>
+ Read().TryGetValue(name, out var h) ? h : throw new KeyNotFoundException($"'{name}' is not in {Path_()}");
+
+ /// <summary>Writes the new hash into the manifest and, for the two playthroughs, into the GoldenRun.cs constant.</summary>
+ public static void Update(string name, string hash)
+ {
+ lock (Gate) UpdateLocked(name, hash);
+ }
+
+ static void UpdateLocked(string name, string hash)
+ {
+ var lines = File.ReadAllLines(Path_()).ToList();
+ var i = lines.FindIndex(l => l.StartsWith(name + " "));
+ if (i >= 0) lines[i] = name + " " + hash; else lines.Add(name + " " + hash);
+ File.WriteAllText(Path_(), string.Join("\n", lines) + "\n");
+ var constant = name switch { "hash1950" => "Hash1950", "hash1952" => "Hash1952", _ => null };
+ if (constant == null) return;
+ var cs = System.IO.Path.Combine(Root(), "sim", "Statecraft.Sim", "GoldenRun.cs");
+ var text = File.ReadAllText(cs);
+ File.WriteAllText(cs, RewriteConstant(text, constant, hash));
+ }
+
+ /// <summary>GoldenRun.cs source with the 64-hex value of <paramref name="constant"/> replaced.</summary>
+ public static string RewriteConstant(string source, string constant, string hash) =>
+ Regex.Replace(source, "(const string " + constant + " = \")[0-9a-f]{64}(\")", "${1}" + hash + "${2}");
+}
diff --git a/sim/Statecraft.Sim.Tests/GoldenManifestTests.cs b/sim/Statecraft.Sim.Tests/GoldenManifestTests.cs
new file mode 100644
index 000000000..c08fde25b
--- /dev/null
+++ b/sim/Statecraft.Sim.Tests/GoldenManifestTests.cs
@@ -0,0 +1,59 @@
+using System;
+using System.IO;
+using System.Linq;
+using Statecraft.Sim;
+
+/// <summary>golden/manifest.txt is the one place a golden hash is pinned (#293). These tests keep every other place that
+/// shows a hash in step with it, and name the place that is out of date.</summary>
+public class GoldenManifestTests
+{
+ static string Repo => Directory.GetParent(RealContentTests.ContentRoot())!.FullName;
+ static string Text(params string[] path) => File.ReadAllText(Path.Combine(new[] { Repo }.Concat(path).ToArray()));
+
+ [Fact]
+ public void TheManifestHoldsExactlyTheKnownGoldens()
+ {
+ var m = GoldenManifest.Read();
+ Assert.Equal(new[] { "hash1950", "hash1952", "smoke5" }, m.Keys.OrderBy(k => k).ToArray());
+ Assert.All(m.Values, h => Assert.Matches("^[0-9a-f]{64}$", h));
+ }
+
+ [Fact]
+ public void TheLibraryConstantsUnityComparesAgainstEqualTheManifest()
+ {
+ Assert.Equal(GoldenRun.Hash1950, GoldenManifest.Expected("hash1950"));
+ Assert.Equal(GoldenRun.Hash1952, GoldenManifest.Expected("hash1952"));
+ }
+
+ [Theory]
+ [InlineData("hash1950")]
+ [InlineData("hash1952")]
+ [InlineData("smoke5")]
+ public void TheDocsAndTheWebCheckShowTheManifestHash(string name)
+ {
+ var hash = GoldenManifest.Expected(name);
+ Assert.True(Text("docs", "determinism.md").Contains(hash), $"docs/determinism.md does not show the current {name} hash {hash}");
+ Assert.True(Text("docs", "RELEASING.md").Contains(hash), $"docs/RELEASING.md does not show the current {name} hash {hash}");
+ if (name != "smoke5")
+ Assert.True(Text("tools", "web_check.py").Contains(hash), $"tools/web_check.py does not show the current {name} hash {hash}");
+ }
+
+ [Fact]
+ public void TheArtCheckpointReadsTheManifestInsteadOfPinningAHash()
+ {
+ var script = Text("tools", "art_checkpoint.sh");
+ Assert.Contains("sim/Statecraft.Sim.Tests/golden/manifest.txt", script);
+ Assert.DoesNotMatch("[0-9a-f]{64}", script);
+ }
+
+ [Fact]
+ public void RewritingAConstantChangesOnlyThatConstant()
+ {
+ var src = Text("sim", "Statecraft.Sim", "GoldenRun.cs");
+ var new1950 = new string('a', 64);
+ var after = GoldenManifest.RewriteConstant(src, "Hash1950", new1950);
+ Assert.Contains($"Hash1950 = \"{new1950}\"", after);
+ Assert.Contains($"Hash1952 = \"{GoldenRun.Hash1952}\"", after);
+ Assert.NotEqual(src, after);
+ }
+}
diff --git a/sim/Statecraft.Sim.Tests/GoldenSnapshot.cs b/sim/Statecraft.Sim.Tests/GoldenSnapshot.cs
index fefc822c6..8b8a63cbf 100644
--- a/sim/Statecraft.Sim.Tests/GoldenSnapshot.cs
+++ b/sim/Statecraft.Sim.Tests/GoldenSnapshot.cs
@@ -21,8 +21,8 @@ public static class GoldenSnapshot
{
var now = state.Describe();
var path = Path_(name);
- var update = Environment.GetEnvironmentVariable("STATECRAFT_UPDATE_GOLDEN") == "1";
- if (update) File.WriteAllText(path, now);
+ var update = GoldenManifest.UpdateRequested;
+ if (update) { File.WriteAllText(path, now); GoldenManifest.Update(name, state.Hash()); }
var hashOk = state.Hash() == expectedHash;
var saved = File.Exists(path) ? File.ReadAllText(path).Replace("\r\n", "\n") : null; // a CRLF checkout is not a change
if (hashOk && saved == now) return;
@@ -31,8 +31,8 @@ public static class GoldenSnapshot
else sb.AppendLine($"Golden '{name}' hash is unchanged but its snapshot is out of date.");
if (saved == null) sb.AppendLine($"No snapshot at {path}; run STATECRAFT_UPDATE_GOLDEN=1 dotnet test to create it.");
else sb.Append(Diff(saved, now));
- if (update) sb.AppendLine("Snapshot rewritten (STATECRAFT_UPDATE_GOLDEN=1). Update the hash constants in GoldenRun.cs, DeterminismTests.cs and docs/determinism.md, and say why in the commit message.");
- else sb.AppendLine("If this is deliberate: STATECRAFT_UPDATE_GOLDEN=1 dotnet test, then update the three hash constants (docs/determinism.md).");
+ if (update) sb.AppendLine("Snapshot, manifest and GoldenRun.cs rewritten (STATECRAFT_UPDATE_GOLDEN=1). Run the tests again: GoldenManifestTests names the docs that still show the old hash. Add a row to the rebaseline history and say why in the commit message.");
+ else sb.AppendLine("If this is deliberate: STATECRAFT_UPDATE_GOLDEN=1 dotnet test, then follow docs/determinism.md.");
Assert.Fail(sb.ToString());
}
diff --git a/sim/Statecraft.Sim.Tests/SliceSmokeTests.cs b/sim/Statecraft.Sim.Tests/SliceSmokeTests.cs
index b6d0bd781..551a7189a 100644
--- a/sim/Statecraft.Sim.Tests/SliceSmokeTests.cs
+++ b/sim/Statecraft.Sim.Tests/SliceSmokeTests.cs
@@ -24,7 +24,7 @@ public class SliceSmokeTests
static readonly Content Real = ContentLoader.Load(RealContentTests.ContentRoot());
/// <summary>docs/determinism.md: the WebGL smoke (?smoke=5) ends here; its policy is FirstOption.</summary>
- const string WebSmoke5 = "SMOKE OK 5 turns 1948-06-19 d3fdd459ddb876bd8d775c20edd1202cf56ca7134a8ad06f873d4d92034aa5b6";
+ static string WebSmoke5 => "SMOKE OK 5 turns 1948-06-19 " + GoldenManifest.Expected("smoke5");
static readonly Regex IsoDate = new Regex(@"\d{4}-\d{2}-\d{2}");
@@ -230,7 +230,9 @@ public class SliceSmokeTests
Assert.True(run.Orders > 0);
// The same policy over five turns ends where the WebGL player's ?smoke=5 run ends (docs/determinism.md).
var five = Play(Real, FirstOption, reload: false, guard: null, maxTurns: 5).Final;
- Assert.Equal(WebSmoke5, FormattableString.Invariant($"SMOKE OK 5 turns {five.Date:yyyy-MM-dd} {five.Hash()}"));
+ var expected = WebSmoke5; // read before an update rewrites the manifest, so a moved value still fails this run
+ if (GoldenManifest.UpdateRequested) GoldenManifest.Update("smoke5", five.Hash());
+ Assert.Equal(expected, FormattableString.Invariant($"SMOKE OK 5 turns {five.Date:yyyy-MM-dd} {five.Hash()}"));
Assert.True(clock.Elapsed < TimeSpan.FromMinutes(1), $"took {clock.Elapsed}");
}
diff --git a/sim/Statecraft.Sim.Tests/golden/manifest.txt b/sim/Statecraft.Sim.Tests/golden/manifest.txt
new file mode 100644
index 000000000..32184a549
--- /dev/null
+++ b/sim/Statecraft.Sim.Tests/golden/manifest.txt
@@ -0,0 +1,7 @@
+# The pinned golden hashes (docs/determinism.md). One line per golden: name, then the SHA-256 of the final state.
+# Tests read this file. To accept a deliberate change: STATECRAFT_UPDATE_GOLDEN=1 dotnet test sim/Statecraft.sln
+# rewrites this file, the snapshots and the GoldenRun.cs constants; then update docs/determinism.md and tools/web_check.py
+# (GoldenManifestTests names any place that is out of date) and add a row to the rebaseline history.
+hash1950 71e9904d0ee48083db9bbba281f3f046136482ba83ee056e48d57b62e76a5b07
+hash1952 62f8ec61a14acff4eab503381994a7c94d7a2f786dcfde5c4c19439ac8b4c5e8
+smoke5 d3fdd459ddb876bd8d775c20edd1202cf56ca7134a8ad06f873d4d92034aa5b6
diff --git a/sim/Statecraft.Sim/GoldenRun.cs b/sim/Statecraft.Sim/GoldenRun.cs
index b23a5ac0b..438d9cc7a 100644
--- a/sim/Statecraft.Sim/GoldenRun.cs
+++ b/sim/Statecraft.Sim/GoldenRun.cs
@@ -9,8 +9,9 @@ namespace Statecraft.Sim
{
public const ulong Seed = 1948;
- /// <summary>Final hash of Play(content, Until1950). Changing it is a deliberate rules or content change:
- /// update it, DeterminismTests and docs/determinism.md in the same commit and say why in the message.</summary>
+ /// <summary>Final hash of Play(content, Until1950). Changing it is a deliberate rules or content change: the value
+ /// is pinned in sim/Statecraft.Sim.Tests/golden/manifest.txt, and STATECRAFT_UPDATE_GOLDEN=1 dotnet test rewrites
+ /// both (docs/determinism.md). Say why in the commit message.</summary>
public const string Hash1950 = "71e9904d0ee48083db9bbba281f3f046136482ba83ee056e48d57b62e76a5b07";
public static readonly DateTime Until1950 = new DateTime(1950, 1, 1);
diff --git a/tools/art_checkpoint.sh b/tools/art_checkpoint.sh
index 6b1176337..92fc651e0 100755
--- a/tools/art_checkpoint.sh
+++ b/tools/art_checkpoint.sh
@@ -44,7 +44,11 @@ fi
[[ -f unity/Builds/Web-disabled/index.html ]] || { echo "art_checkpoint: no build at unity/Builds/Web-disabled (run without --no-build)" >&2; exit 2; }
out="captures/visual/$name"
rm -rf "$out"; mkdir -p "$out"
-uv run tools/web_check.py unity/Builds/Web-disabled --query "golden&shot${units_query}${proof_query}" --expect "GOLDEN OK 1950 71e9904d0ee48083db9bbba281f3f046136482ba83ee056e48d57b62e76a5b07" --expect "GOLDEN OK 1952 62f8ec61a14acff4eab503381994a7c94d7a2f786dcfde5c4c19439ac8b4c5e8" --expect "SHOT done" --shots "$out" --stall 180 \
+# The expected hashes come from the golden manifest (docs/determinism.md), so a re-pin needs no edit here.
+golden() { awk -v k="$1" '$1 == k { print $2 }' sim/Statecraft.Sim.Tests/golden/manifest.txt; }
+hash1950=$(golden hash1950); hash1952=$(golden hash1952)
+[[ -n "$hash1950" && -n "$hash1952" ]] || { echo "art_checkpoint: no hash1950/hash1952 in sim/Statecraft.Sim.Tests/golden/manifest.txt" >&2; exit 2; }
+uv run tools/web_check.py unity/Builds/Web-disabled --query "golden&shot${units_query}${proof_query}" --expect "GOLDEN OK 1950 $hash1950" --expect "GOLDEN OK 1952 $hash1952" --expect "SHOT done" --shots "$out" --stall 180 \
--fail 'GOLDEN FAIL|SMOKE FAIL|BOOT FAIL|Statecraft/.*(not supported|Unsupported)|(not supported|Unsupported).*Statecraft/'
# The poses come from PromoPose.All's source, so a new pose is checked without editing this script (review M3).
poses=$(grep -o 'new PromoPose("[A-Za-z0-9_]*"' unity/Assets/Scripts/Map/PromoPose.cs | cut -d'"' -f2)