A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.
WebGL golden check: a frame between goldens so the collector can run (Refs #2621) (#2949)
Eyal Golan9298b8a298b2
diff --git a/changelog.d/web-golden-check-frame-yield.md b/changelog.d/web-golden-check-frame-yield.mdnew file mode 100644index 000000000..84fd79e6d--- /dev/null+++ b/changelog.d/web-golden-check-frame-yield.md@@ -0,0 +1,5 @@+### Fixed++- Behind the scenes: the web build's golden check now waits a frame before each golden, so the browser can free each+ run's memory. The 8 November nightly's check ran out of memory at the 2 GB cap because all 11 goldens played in one+ frame.diff --git a/unity/Assets/Scripts/Boot/GameBoot.cs b/unity/Assets/Scripts/Boot/GameBoot.csindex 5c6dda67e..dac3fa741 100644--- a/unity/Assets/Scripts/Boot/GameBoot.cs+++ b/unity/Assets/Scripts/Boot/GameBoot.cs@@ -48,7 +48,10 @@ namespace Statecraft.Game.BootInputs = await load();if (GoldenCheck.Requested()){- var (ok, lines) = GoldenCheck.Run(Inputs.Content, Inputs.Goldens);+ // WebGL yields a frame between goldens so its collector can free each run's garbage (GoldenCheck.RunAsync).+ var (ok, lines) = Application.platform == RuntimePlatform.WebGLPlayer+ ? await GoldenCheck.RunAsync(Inputs.Content, Inputs.Goldens)+ : GoldenCheck.Run(Inputs.Content, Inputs.Goldens);foreach (var line in lines) Debug.Log(line);// Desktop players run the check headless (-batchmode -nographics) and report through the exit code.if (Application.platform != RuntimePlatform.WebGLPlayer) { Application.Quit(ok ? 0 : 1); return; }diff --git a/unity/Assets/Scripts/Boot/GoldenCheck.cs b/unity/Assets/Scripts/Boot/GoldenCheck.csindex 5efc34b77..bd2ea5567 100644--- a/unity/Assets/Scripts/Boot/GoldenCheck.cs+++ b/unity/Assets/Scripts/Boot/GoldenCheck.cs@@ -25,23 +25,56 @@ namespace Statecraft.Game.Boot/// empty manifest fails: a full-set pass needs every line.</summary>public static (bool ok, List<string> lines) Run(Content c, GoldenData data){+ var goldens = Manifest(data, out var fail);+ if (goldens == null) return (false, new List<string> { fail });+ var lines = new List<string>();+ bool ok = true;+ foreach (var (name, expected) in goldens) ok &= Check(name, expected, c, data, lines);+ return (ok, lines);+ }++ /// <summary><see cref="Run"/> with a frame between goldens, for the WebGL player: its garbage collector runs only+ /// between frames, so one call that plays every golden keeps all their garbage until the call ends (the 8 Nov 2026+ /// nightly's OOM at the 2048 MB heap cap). The lines and the verdict are Run's.</summary>+ public static async Awaitable<(bool ok, List<string> lines)> RunAsync(Content c, GoldenData data)+ {+ var goldens = Manifest(data, out var fail);+ if (goldens == null) return (false, new List<string> { fail });var lines = new List<string>();bool ok = true;- IReadOnlyList<(string name, string hash)> goldens;- try { goldens = GoldenData.Parse(data.Manifest); }- catch (Exception e) { return (false, new List<string> { $"GOLDEN FAIL manifest {e.Message}" }); }- if (goldens.Count == 0) return (false, new List<string> { "GOLDEN FAIL manifest has no goldens" });foreach (var (name, expected) in goldens){- string hash;- try { hash = Play(name, c, data).Hash(); }- catch (Exception e) { ok = false; lines.Add($"GOLDEN FAIL {name} {e.GetType().Name}: {e.Message}"); continue; }- if (hash == expected) lines.Add($"GOLDEN OK {name} {hash}");- else { ok = false; lines.Add($"GOLDEN FAIL {name} {hash} expected {expected}"); }+ await Awaitable.NextFrameAsync();+ ok &= Check(name, expected, c, data, lines);}return (ok, lines);}+ /// <summary>The manifest's goldens, or null with the FAIL line when it cannot be read or pins none.</summary>+ static IReadOnlyList<(string name, string hash)>? Manifest(GoldenData data, out string fail)+ {+ fail = "";+ try+ {+ var goldens = GoldenData.Parse(data.Manifest);+ if (goldens.Count != 0) return goldens;+ fail = "GOLDEN FAIL manifest has no goldens";+ }+ catch (Exception e) { fail = $"GOLDEN FAIL manifest {e.Message}"; }+ return null;+ }++ /// <summary>Plays one golden and adds its OK or FAIL line; true when it matched.</summary>+ static bool Check(string name, string expected, Content c, GoldenData data, List<string> lines)+ {+ string hash;+ try { hash = Play(name, c, data).Hash(); }+ catch (Exception e) { lines.Add($"GOLDEN FAIL {name} {e.GetType().Name}: {e.Message}"); return false; }+ if (hash == expected) { lines.Add($"GOLDEN OK {name} {hash}"); return true; }+ lines.Add($"GOLDEN FAIL {name} {hash} expected {expected}");+ return false;+ }+/// <summary>The run a manifest name pins (docs/determinism.md): hash1950 and hash1952 (GoldenRun.Play), mvp-end/// (GoldenRun.PlayToEnd), smoke<N> (GoldenRun.PlaySmoke), smoke-orders (GoldenRun.PlaySmokeOrders), scenario-<name> (that scenario file) and the/// era goldens hash_<era>_<year> (GoldenRun.PlayEra to 1 January of that year, as EraGolden names them).</summary>diff --git a/unity/Assets/Tests/EditMode/GoldenCheckTests.cs b/unity/Assets/Tests/EditMode/GoldenCheckTests.csindex 758b15434..db1e768d5 100644--- a/unity/Assets/Tests/EditMode/GoldenCheckTests.cs+++ b/unity/Assets/Tests/EditMode/GoldenCheckTests.cs@@ -1,8 +1,10 @@+using System.Collections;using NUnit.Framework;using Statecraft.Game;using Statecraft.Game.Boot;using Statecraft.Sim;using System.Linq;+using UnityEngine.TestTools;public class GoldenCheckTests{@@ -38,6 +40,32 @@ public class GoldenCheckTestsStringAssert.StartsWith("GOLDEN FAIL", emptyLines.Single());}+ /// <summary>The WebGL player's RunAsync, a frame between goldens (the 8 Nov 2026 nightly OOM), reports Run's lines and+ /// verdict, a FAIL included, and yields at least once per golden. Each tick queues a player-loop update, as+ /// GameInputsTests.Await does: a batch-mode editor runs the player loop (which resumes Awaitable.NextFrameAsync) only+ /// now and then while idle.</summary>+ [UnityTest]+ public IEnumerator RunAsyncGivesRunsLinesWithAFrameBetweenGoldens()+ {+ var c = ContentLoader.Load(Paths.Content);+ var data = new GoldenData { Manifest = "hash1950 " + GoldenRun.Hash1950 + "\nmade-up-golden " + GoldenRun.Hash1950 + "\n" };+ var expected = GoldenCheck.Run(c, data);+ var run = GoldenCheck.RunAsync(c, data).GetAwaiter();+ int frames = 0;+ while (!run.IsCompleted && frames < 600)+ {+ frames++;+ UnityEditor.EditorApplication.QueuePlayerLoopUpdate();+ yield return null;+ }+ Assert.IsTrue(run.IsCompleted, "RunAsync did not finish in 600 frames");+ var (ok, lines) = run.GetResult();+ Assert.AreEqual(expected.ok, ok);+ Assert.IsFalse(ok);+ CollectionAssert.AreEqual(expected.lines, lines);+ Assert.GreaterOrEqual(frames, 2, "one frame before each of the two goldens");+ }+/// <summary>The editor reads the repository's manifest and every scenario file it names (the players read the copies/// ContentSync ships), so the set the test plays is the .NET set.</summary>[Test]