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:
co-authored by
Claude Opus 5.5
parent
d04b88e7b4
commit
11b4a0398e
@@ -0,0 +1,112 @@
|
||||
// The route advisor (core/solver.js): it must give exactly the Python route model's answers, and walk a run in the
|
||||
// game's order. node --test tests/core/advisor.test.js
|
||||
const test = require("node:test");
|
||||
const assert = require("node:assert");
|
||||
const path = require("node:path");
|
||||
const fs = require("node:fs");
|
||||
const os = require("node:os");
|
||||
const { execFileSync } = require("node:child_process");
|
||||
const S = require("../../core/solver");
|
||||
|
||||
const ROOT = path.join(__dirname, "../..");
|
||||
const published = JSON.parse(fs.readFileSync(path.join(ROOT, "data/public/policy.json"), "utf8"));
|
||||
// a synthetic world with known answers, built by the Python model (tests/fixtures/synthetic_policy.py)
|
||||
const tmp = path.join(fs.mkdtempSync(path.join(os.tmpdir(), "atlas-")), "synthetic.json");
|
||||
execFileSync("python3", [path.join(ROOT, "tests/fixtures/synthetic_policy.py"), tmp], { stdio: "inherit" });
|
||||
const policy = JSON.parse(fs.readFileSync(tmp, "utf8"));
|
||||
const choice = (X, h, obj = "thisAttempt") => S.advisor({ model: policy.model }, X, obj).next(h).choice;
|
||||
|
||||
test("the advisor gives exactly the Python model's values and choices, for every colour and goal", () => {
|
||||
let checked = 0;
|
||||
for (const smp of Object.values(published.samples)) {
|
||||
for (const [X, t] of Object.entries(smp.targets)) {
|
||||
for (const obj of ["thisAttempt", "eventually"]) {
|
||||
const adv = S.advisor(smp, X, obj);
|
||||
assert.ok(Math.abs(adv.value.x - t[obj].pX.point) < 1e-12, `${X} ${obj}: ${adv.value.x} vs ${t[obj].pX.point}`);
|
||||
// follow the published tree: every decision on the route must match, option by option
|
||||
let n = t[obj].tree, h = {};
|
||||
while (n && n.type !== "end") {
|
||||
if (n.type === "decision") {
|
||||
const s = adv.step(h);
|
||||
assert.strictEqual(s.var, n.var); assert.strictEqual(s.choice, n.choice, `${X} ${obj} ${n.var}`);
|
||||
for (const [o, v] of Object.entries(n.options)) assert.ok(Math.abs(s.options[o].pX - v.pX) < 1e-12, `${X} ${obj} ${n.var}=${o}`);
|
||||
checked++;
|
||||
if (n.var === "encounter") break;
|
||||
h[n.var] = n.choice; n = n.next;
|
||||
} else {
|
||||
const [k, b] = Object.entries(n.branches).filter(([k]) => k !== "die").sort((a, c) => c[1].p - a[1].p)[0];
|
||||
if (!n.var.startsWith("die")) h[n.var] = k;
|
||||
n = b.next;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
assert.ok(checked > 100, checked);
|
||||
});
|
||||
|
||||
test("synthetic world: Blue goes to the valley, passes the Red, attempts only the Blue", () => {
|
||||
const h = { company: "Alone", direction: "Center", fs: "Red", react: "Hold still", fate: "Killed the bullies yourself", post: "Forest" };
|
||||
assert.strictEqual(choice("Blue", h), "Valley");
|
||||
assert.strictEqual(choice("Blue", { ...h, hour: "Valley" }), "Pass the Red (meadow)");
|
||||
const meadow = { ...h, hour: "Valley", valley: "Pass the Red (meadow)" };
|
||||
assert.strictEqual(choice("Blue", { ...meadow, meadow: "Blue" }), "Attempt the bond");
|
||||
assert.strictEqual(choice("Blue", { ...meadow, meadow: "Orange" }), "Die on purpose");
|
||||
assert.strictEqual(choice("Red", { ...h, hour: "Valley" }), "Face the Red");
|
||||
assert.strictEqual(choice("Black", h), "Tree");
|
||||
});
|
||||
|
||||
test("routing to one encounter: Blue only at the meadow equals the Blue policy; only at the tree Greens gives 0", () => {
|
||||
const smp = { model: policy.model };
|
||||
const meadow = S.advisor(smp, "Blue", "thisAttempt", { only: ["meadow-Blue"] });
|
||||
const all = S.advisor(smp, "Blue", "thisAttempt");
|
||||
assert.ok(Math.abs(meadow.value.x - all.value.x) < 1e-9);
|
||||
assert.ok(S.advisor(smp, "Blue", "thisAttempt", { only: ["tree-Greens"] }).value.x < 1e-9);
|
||||
assert.ok(S.advisor(smp, "Brown", "thisAttempt", { only: ["tree-Greens"] }).value.x > 0.05); // the Greens rescue
|
||||
assert.ok(meadow.attempts("meadow-Blue") > 100);
|
||||
});
|
||||
|
||||
test("a run is walked in the game's order", () => {
|
||||
const adv = S.advisor(published.samples.all, "Blue", "thisAttempt");
|
||||
assert.strictEqual(adv.step({}).var, "company");
|
||||
assert.strictEqual(adv.step({ company: "1 Squadmate" }).var, "leave");
|
||||
assert.strictEqual(adv.step({ company: "1 Squadmate", leave: "Stay together" }).var, "direction");
|
||||
assert.strictEqual(adv.step({ company: "Alone", direction: "Center" }).kind, "chance"); // the first dragon is a roll
|
||||
const seen = { company: "Alone", direction: "Center", fs: "Red" };
|
||||
assert.strictEqual(adv.step(seen).var, "react");
|
||||
const base = { ...seen, react: "Hold still" };
|
||||
const scream = adv.step(base);
|
||||
assert.strictEqual(scream.var, "scream");
|
||||
assert.strictEqual(scream.risk.label, "Killed at the first sighting");
|
||||
const help = { ...base, scream: "Investigate the scream", clearing: "Help your squadmate" };
|
||||
assert.strictEqual(adv.step({ ...base, scream: "Investigate the scream" }).var, "clearing");
|
||||
assert.strictEqual(adv.step(help).var, "fight");
|
||||
assert.strictEqual(adv.step({ ...help, fight: "Pause to assess" }).var, "dagger");
|
||||
assert.strictEqual(adv.step({ ...help, fight: "Charge the smaller cadet" }).var, "fate"); // charging skips the dagger
|
||||
const fate = adv.step({ ...help, fight: "Pause to assess", dagger: "Engage the smaller cadet" });
|
||||
assert.strictEqual(fate.var, "fate");
|
||||
assert.strictEqual(fate.risk.label, "Killed in the fight");
|
||||
assert.strictEqual(adv.step({ ...base, scream: "Keep walking" }).var, "wounded");
|
||||
assert.strictEqual(adv.step({ ...base, scream: "Investigate the scream", clearing: "Walk away" }).var, "hour");
|
||||
// an older record that only knows the bullies' fate still lands in the right place (the skipped steps are
|
||||
// assumed to be the recommended ones)
|
||||
assert.strictEqual(adv.next({ ...base, fate: "Killed the bullies yourself" }).decision, "post");
|
||||
});
|
||||
|
||||
test("recommendOption points the bot at the recommended option on its screen", () => {
|
||||
const adv = S.advisor(published.samples.all, "Blue", "thisAttempt");
|
||||
const h = { company: "Alone", direction: "Center", fs: "Red", react: "Hold still", scream: "Keep walking", wounded: "Tourniquet" };
|
||||
const rec = S.recommendOption(adv, h, ["Continue toward the center of the valley", "Find the tallest tree and climb for a better view"]);
|
||||
assert.strictEqual(rec.decision, "hour");
|
||||
assert.strictEqual(rec.index, rec.choice === "Valley" ? 0 : 1);
|
||||
assert.strictEqual(S.recommendOption(adv, {}, ["Something we have never seen"]).index, null);
|
||||
});
|
||||
|
||||
test("solverHistory turns form variables into a history keyed by the game's steps", () => {
|
||||
S.useGame(published.samples.all);
|
||||
const h = S.solverHistory({ company: "Alone", direction: "Center", first_sighting: "Red", scream: "Ignore the scream", resolution: "Tourniquet", hour: "Tree", branch: "Descend slowly: two Greens" });
|
||||
assert.deepStrictEqual(h, { company: "Alone", direction: "Center", fs: "Red", scream: "Keep walking", wounded: "Tourniquet", hour: "Tree", descend: "Slowly (two Greens)", shadow: "no" });
|
||||
const l = S.solverHistory({ company: "1 Squadmate", left_squadmates: true, branch: "Descend quickly: let the Red pass", hour: "Tree" });
|
||||
assert.strictEqual(l.leave, "Leave them");
|
||||
assert.strictEqual(l.red, "Let it pass (two Greens)");
|
||||
});
|
||||
Reference in New Issue
Block a user