A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.
GDD 0.3 (1/n): the gdd-0.3 ruleset and save schema 4 (Refs #1687) (#1732)
Eyal Golan2cf9bf02f6b5
diff --git a/changelog.d/1687-save-ruleset.md b/changelog.d/1687-save-ruleset.mdnew file mode 100644index 000000000..f65ed696f--- /dev/null+++ b/changelog.d/1687-save-ruleset.md@@ -0,0 +1,4 @@+### Changed++- Saves know a third ruleset, `gdd-0.3`, written as save schema 4, for the GDD 0.3 game built beside the live one.+ A gdd-0.3 game refuses older-rules saves before replaying them; today's games and their schema 3 saves are unchanged.diff --git a/docs/rules/mvp.md b/docs/rules/mvp.mdindex 8ed1ff2db..ff848c84a 100644--- a/docs/rules/mvp.md+++ b/docs/rules/mvp.md@@ -76,9 +76,9 @@ R-CTRL-022: `DeportNeverDominates` (impact plan section 4.1 item 5; Q-TECH-4 as## Saves-R-SAVE-001: A save names the ruleset it was played under (`ruleset legacy` or `ruleset gdd-0.1`, save schema 2). A game refuses a save made under another ruleset. A schema 1 save has no ruleset line and is legacy, so the GDD game refuses it as made under earlier rules. The refusal carries a code (`older_rules`) the UI translates, and the sim's reason does not itself ask for a new game: the player reads that once (N4).+R-SAVE-001: A save names the ruleset it was played under (`ruleset legacy` or `ruleset gdd-0.1`, save schema 2). A game refuses a save made under another ruleset. A schema 1 save has no ruleset line and is legacy, so the GDD game refuses it as made under earlier rules. The refusal carries a code (`older_rules`) the UI translates, and the sim's reason does not itself ask for a new game: the player reads that once (N4). GDD 0.3 has its own ruleset, `gdd-0.3`, saved as schema 4 (#1687; owner ruling 26 October 2026: GDD 0.3 is built behind its own ruleset); `legacy` and `gdd-0.1` saves stay schema 3. A gdd-0.3 game refuses a `legacy` or `gdd-0.1` save as made under earlier rules (`older_rules`), before any replay. No content selects `gdd-0.3` yet, so the live game is unchanged.-R-SAVE-002: A save that names a ruleset this build does not know, or a newer save schema, was made by a newer version of the game and is refused with the code `newer_version` (N2).+R-SAVE-002: A save that names a ruleset this build does not know, or a newer save schema, was made by a newer version of the game and is refused with the code `newer_version` (N2). A `legacy` or `gdd-0.1` game refuses a `gdd-0.3` save the same way, and a schema 4 save naming `legacy` or `gdd-0.1` is newer too. A `gdd-0.3` save under schema 2 or 3 was never written by any build, so it is refused as damaged, with no code (#1687).R-SAVE-003: Save schema 3 adds the open turn: after the last `turn`, a line `open` and the turn in progress's committed orders (R-TURN-004), one a line (a waiting order there is refused: the save was edited); the `end` hash is still the state's after the last finished turn. Loading replays the turns, then commits the open orders again; one refused on the same content means the save is damaged (`SaveException`), and on other content the rest are dropped with the `replay_differs` warning. A schema 2 save has no open turn and loads as before (but see R-SAVE-004).diff --git a/sim/Statecraft.Sim.Tests/Gdd03SaveTests.cs b/sim/Statecraft.Sim.Tests/Gdd03SaveTests.csnew file mode 100644index 000000000..b1ebc094e--- /dev/null+++ b/sim/Statecraft.Sim.Tests/Gdd03SaveTests.cs@@ -0,0 +1,76 @@+using Statecraft.Sim;++/// <summary>#1687, R-SAVE-001/002: a separate envelope, never a live-loop cutover.</summary>+public class Gdd03SaveTests+{+ static string Text(int version, string ruleset) =>+ $"statecraft-save {version}\ncontent {new string('a', 64)}\nseed 1\nmode Historical\nruleset {ruleset}\nend {new string('b', 64)}\n";++ [Fact]+ public void Gdd03WritesSchemaFourAndRoundTrips()+ {+ var save = new SaveGame(new string('a', 64), 1, Mode.Historical, "gdd-0.3", new string('b', 64));+ Assert.Equal(4, save.Version);+ Assert.StartsWith("statecraft-save 4\n", save.Write());+ Assert.Equal("gdd-0.3", SaveGame.Parse(save.Write()).Ruleset);+ Assert.Equal(save.Write(), SaveGame.Parse(save.Write()).Write());+ }++ [Theory]+ [InlineData(2)]+ [InlineData(3)]+ public void Gdd03UnderAnEarlierSchemaIsDamage(int schema)+ {+ // No build writes it, so neither the live game nor gdd-0.3 may call it older or newer rules.+ Assert.Null(Assert.Throws<SaveException>(() => SaveGame.Parse(Text(schema, "gdd-0.3"))).Code);+ Assert.Null(Assert.Throws<SaveException>(() => SaveGame.Parse(Text(schema, "gdd-0.3"), SaveGame.Gdd03Ruleset)).Code);+ Assert.Null(Assert.Throws<SaveException>(() => SaveGame.Parse(Text(schema, "gdd-0.3"), SaveGame.GddRuleset)).Code);+ }++ [Theory]+ [InlineData(4, "gdd-future")]+ [InlineData(5, "gdd-0.3")]+ [InlineData(4, "legacy")]+ [InlineData(4, "gdd-0.1")]+ public void UnknownRulesOrNewerSchemasAreRefused(int schema, string ruleset) =>+ Assert.Equal(SaveException.NewerVersion, Assert.Throws<SaveException>(() => SaveGame.Parse(Text(schema, ruleset))).Code);++ [Theory]+ [InlineData("legacy")]+ [InlineData("gdd-0.1")]+ public void ExistingRulesStillWriteSchemaThree(string ruleset)+ {+ var save = new SaveGame(new string('a', 64), 1, Mode.Historical, ruleset, new string('b', 64));+ Assert.Equal(3, save.Version);+ Assert.StartsWith("statecraft-save 3\n", save.Write());+ }++ // R-SAVE-001/002: future gdd-0.3 callers must not replay the live rules' saves.+ [Theory]+ [InlineData(2, "legacy")]+ [InlineData(3, "legacy")]+ [InlineData(2, "gdd-0.1")]+ [InlineData(3, "gdd-0.1")]+ public void EarlierRulesAreRefusedByGdd03(int schema, string ruleset)+ {+ var error = Assert.Throws<SaveException>(() => SaveGame.Parse(Text(schema, ruleset), SaveGame.Gdd03Ruleset));+ Assert.Equal(SaveException.OlderRules, error.Code);+ Assert.DoesNotContain("new game", error.Message);+ }++ [Theory]+ [InlineData("legacy")]+ [InlineData("gdd-0.1")]+ public void ExistingGamesRefuseGdd03AsNewer(string ruleset) =>+ Assert.Equal(SaveException.NewerVersion, Assert.Throws<SaveException>(() => SaveGame.Parse(Text(4, "gdd-0.3"), ruleset)).Code);++ [Fact]+ public void Gdd03AcceptsItsOwnEnvelope() =>+ Assert.Equal("gdd-0.3", SaveGame.Parse(Text(4, "gdd-0.3"), SaveGame.Gdd03Ruleset).Ruleset);++ [Theory]+ [InlineData(4, "gdd-future")]+ [InlineData(5, "gdd-0.3")]+ public void Gdd03RefusesUnknownRulesAndFutureSchemas(int schema, string ruleset) =>+ Assert.Equal(SaveException.NewerVersion, Assert.Throws<SaveException>(() => SaveGame.Parse(Text(schema, ruleset), SaveGame.Gdd03Ruleset)).Code);+}diff --git a/sim/Statecraft.Sim/GameSession.cs b/sim/Statecraft.Sim/GameSession.csindex 7bd2f2052..553135d0c 100644--- a/sim/Statecraft.Sim/GameSession.cs+++ b/sim/Statecraft.Sim/GameSession.cs@@ -86,13 +86,8 @@ namespace Statecraft.Simpublic static GameSession Load(Content c, string saveText, out IReadOnlyList<string> warnings){if (c == null) throw new ArgumentNullException(nameof(c));- var saved = SaveGame.Parse(saveText);- // R-SAVE-001: a save replays only under the rules it was played with (spec 2026-10-12 section 2.4, decision D-E4).- // The refusal carries a code the UI translates; its own message tells the player a new game started (N4).- if (saved.Ruleset != c.Ruleset)- throw saved.Ruleset == SaveGame.LegacyRuleset- ? new SaveException(0, "This save was made with an earlier version of the game's rules.", SaveException.OlderRules)- : new SaveException(0, "This save was made with a newer version of the game's rules.", SaveException.NewerVersion);+ // R-SAVE-001/002: the parser enforces the content's ruleset before any replay.+ var saved = SaveGame.Parse(saveText, c.Ruleset);// R-SAVE-004: schema 2 stored player attacks as standing orders, which the current rules no longer replay alike.if (saved.Version < 3 && saved.Ruleset == SaveGame.GddRuleset&& saved.Turns.Any(t => t.Any(x => x is UnitOrder o && o.Order == Combat.Attack)))diff --git a/sim/Statecraft.Sim/SaveGame.cs b/sim/Statecraft.Sim/SaveGame.csindex 8de4e76db..1f19d8b62 100644--- a/sim/Statecraft.Sim/SaveGame.cs+++ b/sim/Statecraft.Sim/SaveGame.cs@@ -30,10 +30,10 @@ namespace Statecraft.Sim/// <summary>/// A save is seed + command log + content hash + schema version (spec section 3); loading replays it. Text format,/// one item per line:- /// statecraft-save 3 schema version (a schema 1 save has no ruleset line and is legacy)+ /// statecraft-save 4 gdd-0.3; existing rulesets still write schema 3 (a schema 1 save has no ruleset line and is legacy)/// content <hex> Content.Hash of the content it was played with/// seed <ulong> / mode Historical- /// ruleset legacy|gdd-0.1 the rules it was played under (Content.Ruleset); another ruleset refuses it (R-SAVE-001),+ /// ruleset legacy|gdd-0.1|gdd-0.3 the rules it was played under (Content.Ruleset); another ruleset refuses it (R-SAVE-001),/// and one this build does not know was made by a newer version (R-SAVE-002)/// turn starts the next turn's command list (one per Simulation.Step)/// <command> ... policy ID LEVEL | budget OFFICE=SHARE,... | event ID OPTION | mobilize LEVEL |@@ -47,11 +47,18 @@ namespace Statecraft.Sim/// </summary>public sealed class SaveGame{- public const int SchemaVersion = 3;+ /// <summary>Newest understood schema, reserved for the separate GDD v0.3 ruleset (#1687).</summary>+ public const int SchemaVersion = 4;+ const int PreviousSchemaVersion = 3;/// <summary>The ruleset of the legacy loop (Content.Ruleset).</summary>public const string LegacyRuleset = "legacy";/// <summary>The ruleset of the GDD v0.1 loop (eras.yaml systems.loop: gdd).</summary>public const string GddRuleset = "gdd-0.1";+ /// <summary>GDD v0.3 save identity only; this does not switch the live simulation/content.</summary>+ public const string Gdd03Ruleset = "gdd-0.3";++ static bool KnownRuleset(string ruleset) => ruleset == LegacyRuleset || ruleset == GddRuleset || ruleset == Gdd03Ruleset;+ static int WritingSchema(string ruleset) => ruleset == Gdd03Ruleset ? SchemaVersion : PreviousSchemaVersion;/// <summary>Turns are at least a week long, so 2000 turns is 38 years, far past any content; a save with more is hostile or damaged.</summary>public const int MaxTurns = 2000;const string Magic = "statecraft-save";@@ -61,12 +68,12 @@ namespace Statecraft.Simreadonly List<List<string>> turns = new List<List<string>>();readonly List<string> open = new List<string>();- /// <summary>The schema version parsed from this save; a new save uses <see cref="SchemaVersion"/>.</summary>+ /// <summary>The schema version parsed from this save; a new save uses its ruleset's writing schema (4 for gdd-0.3, otherwise 3).</summary>public int Version { get; private set; }public string ContentHash { get; }public ulong Seed { get; }public Mode Mode { get; }- /// <summary>The ruleset the game was played under: <see cref="LegacyRuleset"/> or <see cref="GddRuleset"/> (R-SAVE-001).</summary>+ /// <summary>The ruleset the game was played under: <see cref="LegacyRuleset"/>, <see cref="GddRuleset"/> or <see cref="Gdd03Ruleset"/> (R-SAVE-001).</summary>public string Ruleset { get; }/// <summary>The recorded turns as new command objects each time it is read (changing them changes nothing in the save).</summary>public IReadOnlyList<IReadOnlyList<Command>> Turns =>@@ -79,8 +86,8 @@ namespace Statecraft.Simpublic SaveGame(string contentHash, ulong seed, Mode mode, string ruleset, string initialHash){ContentHash = contentHash ?? throw new ArgumentNullException(nameof(contentHash));- if (ruleset != LegacyRuleset && ruleset != GddRuleset) throw new ArgumentException($"unknown ruleset '{ruleset}'", nameof(ruleset));- Version = SchemaVersion; Seed = seed; Mode = mode; Ruleset = ruleset;+ if (!KnownRuleset(ruleset)) throw new ArgumentException($"unknown ruleset '{ruleset}'", nameof(ruleset));+ Version = WritingSchema(ruleset); Seed = seed; Mode = mode; Ruleset = ruleset;FinalHash = initialHash ?? throw new ArgumentNullException(nameof(initialHash));}@@ -107,7 +114,7 @@ namespace Statecraft.Simpublic string Write(){var sb = new StringBuilder();- sb.Append(Magic).Append(' ').Append(SchemaVersion.ToString(CultureInfo.InvariantCulture)).Append('\n');+ sb.Append(Magic).Append(' ').Append(WritingSchema(Ruleset).ToString(CultureInfo.InvariantCulture)).Append('\n');sb.Append("content ").Append(ContentHash).Append('\n');sb.Append("seed ").Append(Seed.ToString(CultureInfo.InvariantCulture)).Append('\n');sb.Append("mode ").Append(Mode.ToString()).Append('\n');@@ -159,10 +166,12 @@ namespace Statecraft.Sim_ => throw new ArgumentException($"Cannot save command kind '{cmd.Kind}'", nameof(cmd)),};- /// <summary>Parses save text; any malformed line throws SaveException naming the line.</summary>- public static SaveGame Parse(string text)+ /// <summary>Parses save text; any malformed line throws SaveException naming the line. When supplied, expectedRuleset enforces R-SAVE-001 before any replay.</summary>+ public static SaveGame Parse(string text, string? expectedRuleset = null){if (text == null) throw new ArgumentNullException(nameof(text));+ if (expectedRuleset != null && !KnownRuleset(expectedRuleset))+ throw new ArgumentException($"unknown ruleset '{expectedRuleset}'", nameof(expectedRuleset));if (text.Length > 0 && text[0] == '\uFEFF') text = text.Substring(1); // a byte-order mark is not part of the savevar lines = text.Replace("\r\n", "\n").Split('\n');int n = lines.Length;@@ -194,10 +203,21 @@ namespace Statecraft.Simif (n < 6) throw new SaveException(0, "Not a Statecraft save (too short)");ruleset = Head(4, "ruleset", 2)[1];// R-SAVE-002 (N2): a ruleset this build does not know comes from a newer version, like a newer schema.- if (ruleset != LegacyRuleset && ruleset != GddRuleset)+ if (!KnownRuleset(ruleset))throw new SaveException(5, $"This save was made with a newer version of the game (ruleset '{ruleset}').", SaveException.NewerVersion);first = 5;}+ // #1687, R-SAVE-001/002: schema 4 belongs only to the isolated gdd-0.3 envelope.+ // No build writes gdd-0.3 under an earlier schema, so such a save was edited: damage, not older or newer rules.+ if (ruleset == Gdd03Ruleset && version < SchemaVersion)+ throw new SaveException(1, FormattableString.Invariant($"a gdd-0.3 save is schema {SchemaVersion}, not {version}"));+ if (version > PreviousSchemaVersion && ruleset != Gdd03Ruleset)+ throw new SaveException(1, FormattableString.Invariant($"save schema version {version} is newer than this game ({PreviousSchemaVersion})"), SaveException.NewerVersion);+ // Check compatibility before replay, including gdd-0.1 -> gdd-0.3 as earlier rules.+ if (expectedRuleset != null && ruleset != expectedRuleset)+ throw ruleset == LegacyRuleset || (ruleset == GddRuleset && expectedRuleset == Gdd03Ruleset)+ ? new SaveException(0, "This save was made with an earlier version of the game's rules.", SaveException.OlderRules)+ : new SaveException(0, "This save was made with a newer version of the game's rules.", SaveException.NewerVersion);var end = Head(n - 1, "end", 2)[1];if (!IsHash(end)) throw new SaveException(n, "bad end hash");var save = new SaveGame(content, seed, mode, ruleset, end);