Core: the route model in Python, built from a declarative game.yaml (#10)

Step 2 of the Python-first, declarative core.

- core/spec/game.yaml: the game as data: every decision and its options, every roll and the
  earlier steps its odds may depend on, every bonding encounter, and the flow of a run from the
  entrance to the dragons (set / keep / drop / go / end / encounter). Also how the form's coded
  answers map onto the game.
- core/atlas/model.py: what each coded run reveals about each step, count tables, empirical-Bayes
  shrinkage, and the posterior draws (same random numbers as before, reproducible from the seed).
- core/atlas/solver.py: one interpreter of game.yaml used three ways (solve, evaluate a fixed
  policy, export the published tree) instead of the tree written out five times. Vectorised
  over the 200 posterior draws: the build takes about 2 minutes instead of 4, with no memory flag.
- planner/build_policy.py and policy_report.py replace the JS build and report.

Checked against the JavaScript model: every number in policy.json agrees to within 4e-12
(last-digit differences in log/cos). Three "still best" shares differ by one draw in 200, at
near-ties under the "eventually" objective. POLICY.md is otherwise identical.
tests/parity/compare_solver.py keeps checking JS against Python in CI until the site's JS advisor
replaces solver.js; tests/core/test_game.py checks game.yaml is complete and consistent.


Claude-Session: https://claude.ai/code/session_01UCzTd31GphpMXDhQqWrCMQ

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
ekcSchiano
2026-11-03 21:04:21 +00:00
committed by GitHub
co-authored by Claude Opus 5.5
parent 3536007ade
commit cc86343668
19 changed files with 1291 additions and 110 deletions
+56
View File
@@ -0,0 +1,56 @@
"""The game tree in core/spec/game.yaml must be complete and consistent."""
import unittest
from atlas import spec
G = spec.game()
class GameSpec(unittest.TestCase):
def test_every_step_is_reachable_and_every_go_exists(self):
seen, todo = set(), [G["start"]]
while todo:
s = todo.pop()
if s in seen:
continue
seen.add(s)
self.assertIn(s, G["steps"], f"go: {s} has no step")
for t in G["steps"][s]["then"].values():
if "go" in t:
todo.append(t["go"])
self.assertEqual(seen, set(G["steps"]), "steps nobody goes to")
def test_decisions_and_rolls_are_defined(self):
for name, st in G["steps"].items():
if "decision" in st:
d = G["decisions"][st["decision"]]
for opt in st["then"]:
self.assertTrue(opt == "default" or opt in d["options"], f"{name}: unknown option {opt}")
else:
c = G["chance"][st["chance"]]
for out in st["then"]:
self.assertTrue(out == "default" or out in c["outcomes"], f"{name}: unknown outcome {out}")
def test_every_branch_says_what_happens(self):
for name, st in G["steps"].items():
for k, t in st["then"].items():
self.assertEqual(sum(x in t for x in ("go", "end", "encounter")), 1, f"{name}/{k}")
def test_encounters_exist(self):
for name, st in G["steps"].items():
for t in st["then"].values():
if "encounter" in t and "$" not in t["encounter"]:
self.assertIn(t["encounter"], G["encounters"])
for c, encs in G["by_colour"].items():
for e in encs:
self.assertIn(e, G["encounters"])
def test_chance_contexts_are_earlier_steps(self):
known = {"company", "left", "direction", "fs", "react", "scream", "clearing", "fight", "dagger", "cadet", "post",
"hour", "shadow", "descend", "red", "valley", "meadow"}
for k, c in G["chance"].items():
self.assertTrue(set(c["context"]) <= known, k)
if __name__ == "__main__":
unittest.main()
+46
View File
@@ -0,0 +1,46 @@
"""Compare two policy.json files structurally: same keys, same choices and labels, numbers equal within a tolerance.
python tests/parity/compare_policy.py A.json B.json [--tol 1e-9]
"""
import json
import math
import sys
def walk(a, b, path, out, tol):
if isinstance(a, dict) and isinstance(b, dict):
for k in sorted(set(a) | set(b)):
if k == "id" and path == "":
continue
if k not in a or k not in b:
out["keys"].append(f"{path}/{k} only in {'B' if k not in a else 'A'}")
continue
walk(a[k], b[k], f"{path}/{k}", out, tol)
elif isinstance(a, list) and isinstance(b, list):
if len(a) != len(b):
out["other"].append(f"{path}: length {len(a)} vs {len(b)}")
for i, (x, y) in enumerate(zip(a, b)):
walk(x, y, f"{path}[{i}]", out, tol)
elif isinstance(a, (int, float)) and isinstance(b, (int, float)) and not isinstance(a, bool) and not isinstance(b, bool):
d = abs(a - b) / max(1.0, abs(a), abs(b))
out["max"] = max(out["max"], d)
if d > tol:
out["numbers"].append(f"{path}: {a} vs {b}")
elif a != b:
out["other"].append(f"{path}: {a!r} vs {b!r}")
def compare(fa, fb, tol=1e-9):
out = {"keys": [], "numbers": [], "other": [], "max": 0.0}
walk(json.load(open(fa)), json.load(open(fb)), "", out, tol)
return out
if __name__ == "__main__":
tol = float(sys.argv[sys.argv.index("--tol") + 1]) if "--tol" in sys.argv else 1e-9
o = compare(sys.argv[1], sys.argv[2], tol)
for k in ("keys", "other", "numbers"):
if o[k]:
print(f"{len(o[k])} {k} differ, e.g.:", *o[k][:8], sep="\n ")
print(f"largest relative difference: {o['max']:.3g}")
sys.exit(1 if o["keys"] or o["other"] or o["numbers"] else 0)
+37
View File
@@ -0,0 +1,37 @@
"""The Python route model (core/atlas/solver.py, built from core/spec/game.yaml) must give the same policy as the
JavaScript one the site and the bot still use (core/solver.js): same choices everywhere, same numbers.
A quick version for CI: one colour, both samples' coding, 3 posterior draws.
python tests/parity/compare_solver.py
"""
import json
import subprocess
import sys
import tempfile
from pathlib import Path
ROOT = Path(__file__).resolve().parents[2]
sys.path.insert(0, str(ROOT / "core"))
sys.path.insert(0, str(ROOT / "planner"))
sys.path.insert(0, str(Path(__file__).parent))
import build_policy # noqa: E402
from atlas import solver # noqa: E402
from compare_policy import compare # noqa: E402
COLOURS, DRAWS = ["Blue"], 3
runs = build_policy.coded_runs(ROOT / "data/public/form_responses.json")
py = build_policy.clean(solver.build_policy(runs, draws=DRAWS, colours=COLOURS))
js = json.loads(subprocess.check_output(["node", "-e", f"""
const S=require('./core/solver.js'),E=require('./core/engine.js'),fs=require('fs');
const raw=JSON.parse(fs.readFileSync('data/public/form_responses.json'));const {{runs}}=E.codeRows(raw.rows,raw.headers);
console.log(JSON.stringify(S.buildPolicy(runs,{{draws:{DRAWS},colours:{json.dumps(COLOURS)}}})));"""], cwd=ROOT))
with tempfile.TemporaryDirectory() as d:
a, b = Path(d, "js.json"), Path(d, "py.json")
a.write_text(json.dumps(js))
b.write_text(json.dumps(py))
o = compare(a, b, 1e-9)
bad = o["keys"] + o["other"] + o["numbers"]
print(f"route model ({', '.join(COLOURS)}, {DRAWS} draws): " + ("ALL MATCH" if not bad else f"{len(bad)} DIFFER"), f"(largest relative difference {o['max']:.2g})")
for x in bad[:10]:
print(" ", x)
sys.exit(1 if bad else 0)