Core: a small JS advisor reads the game from policy.json; engine.js is gone (#13)

Step 4b of the Python-first core.

- core/solver.js is now a 250-line advisor (was 900 lines): it reads the game (core/spec/game.yaml,
  published inside policy.json as model.game) and the count tables, and re-solves from any point in
  a run with the same interpreter as the Python model. Same API for the site and Ken's bot
  (advisor, recommendOption, solverHistory). The bot's on-screen text for each option moves to
  game.yaml (bot_text); the deadly rolls carry their risk labels there too.
- tests/core/advisor.test.js: the advisor gives exactly the Python model's values and choices for
  every colour and goal (to 1e-12, every decision on every published route), walks a run in the
  game's order, and maps advice onto the bot's screen.
- Removed core/engine.js and everything that only compared it with Python (tests/parity/run_engine,
  compare_runs, compare_results, compare_routes, compare_solver, compare_stats, analysis/stats/
  analyze.py). In their place: tests/core/test_stats.py checks the statistics against known answers,
  test_solver.py checks the route model on a synthetic world (tests/fixtures/synthetic_policy.py).
- analysis/network/build-graph.js reads the Python-coded runs; graph.json is byte-identical.
- docs/BOT_INTEGRATION.md: what changed for the bot (history keys, new choices, re-sync all three files).


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 22:01:32 +00:00
committed by GitHub
co-authored by Claude Opus 5.5
parent d04b88e7b4
commit 11b4a0398e
27 changed files with 578 additions and 2260 deletions
+2 -2
View File
@@ -5,7 +5,7 @@ Making sense of the data.
| Folder | What it does |
|---|---|
| `stats/measures.yaml` | **Declarative.** What the site measures: outcomes and their denominators, route targets and comparison groups, the bonding-encounter contexts, the route map's stages, the filter presets and the route thresholds. |
| `stats/site_stats.py` | Computes every number the Atlas page shows for every preset and writes `data/public/stats.json`; the page computes nothing itself. `tests/parity/compare_stats.py` checks it against the old in-browser engine until that is removed. |
| `stats/` | General statistics in Python (`analyze.py`, `stats_core.py`): rates with Wilson intervals, odds ratios with Fisher's test and Benjamini–Hochberg, ridge logistic models that adjust for data source and chase goal. `core/engine.js` computes the same numbers for the site, and `tests/parity/` checks they match. |
| `stats/site_stats.py` | Computes every number the Atlas page shows for every preset and writes `data/public/stats.json`; the page computes nothing itself. |
| `stats/stats_core.py` | The statistics themselves: rates with Wilson intervals, odds ratios with Fisher's test and Benjamini–Hochberg, Mantel–Haenszel, ridge logistic models that adjust for data source and chase goal. Tested against known answers (`tests/core/test_stats.py`). |
| `network/` | `build-graph.js` combines every source into `data/public/graph.json` (the decision-variable graph and the bot's scene graph). CI fails if the committed graph is out of date. |
| `reports/` | Generated reports. `POLICY.md` is written by `planner/policy_report.py`; do not edit it by hand. |
+20 -3
View File
@@ -2,13 +2,30 @@
// node analysis/network/build-graph.js
const fs = require("fs");
const path = require("path");
const E = require("../../core/engine.js");
const G = require("../../core/graph.js");
const root = path.resolve(__dirname, "../..");
const rel = (p) => path.join(root, p);
const raw = JSON.parse(fs.readFileSync(rel("data/public/form_responses.json"), "utf8"));
const { runs } = E.codeRows(raw.rows, raw.headers);
/** The coded runs written by the Python coding rules (python -m atlas.coding -> data/derived/runs_clean.csv). */
function readCSV(file) {
const text = fs.readFileSync(file, "utf8"), rows = [];
let row = [], cell = "", q = false;
for (let i = 0; i < text.length; i++) {
const ch = text[i];
if (q) {
if (ch === '"' && text[i + 1] === '"') { cell += '"'; i++; }
else if (ch === '"') q = false;
else cell += ch;
} else if (ch === '"') q = true;
else if (ch === ",") { row.push(cell); cell = ""; }
else if (ch === "\n") { row.push(cell); rows.push(row); row = []; cell = ""; }
else if (ch !== "\r") cell += ch;
}
if (cell || row.length) { row.push(cell); rows.push(row); }
const [head, ...body] = rows;
return body.map((r) => Object.fromEntries(head.map((h, i) => [h, r[i] === "" || r[i] === undefined ? null : r[i]])));
}
const runs = readCSV(rel("data/derived/runs_clean.csv"));
const records = runs.map((r) => ({ source: r.source === "Form" ? "form" : "form-bulk", vars: G.formRunVariables(r) }));
const botDir = rel("data/bot");
-139
View File
@@ -1,139 +0,0 @@
"""
analyze.py — Python reference implementation of the statistics shown on the route-map page.
python analyze.py ../../data/derived/runs_clean.csv ../../tests/out/results_py.json
It mirrors analyse() in core/engine.js (same denominators, same tests, same model); tests/parity/compare_results.py
checks that both produce the same numbers. Edit either, then re-run the comparison.
"""
import json
import sys
import numpy as np
import pandas as pd
from stats_core import Z95, bh, logistic_ridge, odds_ratio, p_from_z, wilson
# colours and the answers that count as missing come from core/spec/form.yaml, like everything else
from atlas.coding import COLORS, MISSING, is_missing # noqa: E402
FACTORS = ["company", "direction", "first_sighting", "scream", "resolution", "combat_first",
"post_path", "hour", "wing", "squad", "attempt_group", "source", "goal", "sighting_action", "combat_second", "malek_intent"]
def W(k, n):
p, lo, hi = wilson(k, n)
return {"k": int(k), "n": int(n), "p": p, "lo": lo, "hi": hi}
def outcomes():
o = {
"blue_meadow": (lambda d: d.branch == "Passed the Red: meadow", lambda d: d.saw_blue_meadow),
"shadow": (lambda d: (d.hour == "Tree") & d.branch.notna(), lambda d: d.saw_shadow),
"bond_blue_meadow": (lambda d: (d.branch == "Passed the Red: meadow") & d.enc_Blue.isin(["Bonded!", "Incinerated"]),
lambda d: d.enc_Blue == "Bonded!"),
"reach_hour": (lambda d: pd.Series(True, index=d.index), lambda d: d.hour.isin(["Tree", "Valley"])),
}
for c in COLORS:
o["bonded_" + c] = (lambda d: d.hour.isin(["Tree", "Valley"]),
(lambda c: lambda d: (d.outcome == "Bonded") & (d.outcome_color == c))(c))
return o
OUT = outcomes()
def factor_table(d, key, min_n=5):
den, ev = OUT[key]
D = d[den(d)].copy()
D["_e"] = ev(D).astype(bool)
rows = []
for f in FACTORS:
sub = D[D[f].notna() & ~D[f].isin(MISSING)]
nf, ef = len(sub), int(sub._e.sum())
for lv in sorted(sub[f].astype(str).unique()):
g = sub[sub[f].astype(str) == lv]
k, n = int(g._e.sum()), len(g)
row = {"factor": f, "level": lv, **W(k, n)}
rk, rn = ef - k, nf - n
if n >= min_n and rn >= min_n and 0 < ef < nf:
or_, lo, hi, p, corr = odds_ratio(k, n - k, rk, rn - rk)
row.update({"or": or_, "or_lo": lo, "or_hi": hi, "fisher_p": p, "corrected": corr})
rows.append(row)
tested = [r for r in rows if "fisher_p" in r]
for r, q in zip(tested, bh([r["fisher_p"] for r in tested])):
r["q"] = q
return {"outcome": key, "overall": W(int(D._e.sum()), len(D)), "rows": rows}
def model(d, key, factors, lam=1.0):
den, ev = OUT[key]
D = d[den(d)].copy()
y = ev(D).astype(int).values
F = pd.DataFrame({name: D.apply(fn, axis=1) for name, fn in factors.items()}, index=D.index)
keep = F.notna().all(axis=1).values
F, y = F[keep], y[keep]
cols, names = [np.ones(len(F))], []
refs = {}
for f in factors:
counts = F[f].value_counts()
levels = sorted(counts.index, key=lambda lv: (-counts[lv], lv))
refs[f] = levels[0]
for lv in levels[1:]:
cols.append((F[f] == lv).astype(float).values)
names.append((f, lv, int(counts[lv])))
X = np.column_stack(cols)
b, se = logistic_ridge(X, y, lam)
terms = [{"term": f"{f} = {lv}", "factor": f, "level": lv, "n": n, "or": float(np.exp(b[i + 1])),
"lo": float(np.exp(b[i + 1] - Z95 * se[i + 1])), "hi": float(np.exp(b[i + 1] + Z95 * se[i + 1])),
"p": float(p_from_z(b[i + 1] / se[i + 1]))} for i, (f, lv, n) in enumerate(names)]
return {"outcome": key, "n": int(len(y)), "events": int(y.sum()), "refs": refs, "lambda": lam, "terms": terms}
def goal2(r):
if r.goal in ("Blue", "Black", "Blue or Black"):
return r.goal
return "Blank/not asked" if r.goal == "(not asked/blank)" else "Other colour/any"
def src2(r):
return "Form submission" if r.source == "Form" else "Bulk log (import)"
def first2(r):
return r.first_sighting if r.first_sighting in ("Red", "Brown", "Orange", "Green") else None
def known(f):
return lambda r: None if is_missing(r[f]) else r[f]
def period(r):
if not isinstance(r.date, str):
return "No date (import)"
return "Oct 4–10" if r.date < "2026-10-11" else "Oct 11 onward"
def analyse(d):
res = {"n": len(d), "factors": {k: factor_table(d, k) for k in OUT}}
res["models"] = {
"blue_meadow": model(d, "blue_meadow", {
"source": src2, "goal": goal2, "direction": known("direction"), "first_sighting": first2,
"resolution": known("resolution"), "company": known("company")}),
"shadow": model(d, "shadow", {
"source": src2, "goal": goal2, "attempt": known("attempt_group"),
"direction": known("direction")}),
"bond_blue_meadow": model(d, "bond_blue_meadow", {
"source": src2, "goal": goal2, "direction": known("direction"), "resolution": known("resolution")}),
}
return res
if __name__ == "__main__":
src = sys.argv[1] if len(sys.argv) > 1 else "../../data/derived/runs_clean.csv"
out = sys.argv[2] if len(sys.argv) > 2 else "../../tests/out/results_py.json"
d = pd.read_csv(src, keep_default_na=False, na_values=[""])
for c in ("saw_blue_meadow", "saw_shadow", "rescued"):
d[c] = d[c].astype(str) == "True"
res = analyse(d)
json.dump(res, open(out, "w"), indent=1, default=float)
print("wrote", out)
+3 -2
View File
@@ -1,4 +1,5 @@
"""Small, dependency-light statistics used by the analysis (mirrored in core/engine.js)."""
"""The statistics behind every number on the page: Wilson intervals, odds ratios with Fisher's test, Benjamini-Hochberg
q-values, Mantel-Haenszel, ridge logistic regression. Tested against known answers in tests/core/test_stats.py."""
import math
import numpy as np
@@ -79,7 +80,7 @@ def p_from_z(z):
def mantel_haenszel(tables):
"""Mantel-Haenszel common OR over 2x2 tables [a, b, c, d], Robins-Breslow-Greenland 95% CI,
and the CMH chi-square p-value (1 df, continuity-corrected). Mirrors mantelHaenszel() in engine.js."""
and the CMH chi-square p-value (1 df, continuity-corrected)."""
R = S = PR = PSQR = QS = sA = sE = sV = 0.0
for a, b, c, d in tables:
n = a + b + c + d