Files
threshing-atlas/tests/core/advisor.test.js
T
ekcSchianoandClaude Opus 5.5 c0355616b6 Route model learns from bot runs; Thompson sampling in the advisor (work in progress)
- planner/policy.yaml: which sources the route model learns from (spreadsheet,
  reported, bot) and the low-bias sample
- bot runs: deaths coded by stage, Malek intent for runs with a target
- model: survival of the first dragon is not counted when nothing after it is
  known; the meadow dragon falls back to what the bot saw
- solver.js: advisor(..., {sampling: "thompson", seed, draws}) follows one
  posterior draw per attempt and reports each pick's propensity

Not shipped yet: mixing sources lowers the fitted shrinkage for the shadow roll
(alpha 500 -> 50 in the low-bias sample), see the conversation of 2026-11-09.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UCzTd31GphpMXDhQqWrCMQ
2026-11-10 02:29:09 +00:00

128 lines
7.9 KiB
JavaScript

// 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)");
});
test("Thompson sampling: one posterior draw per seed, with the chance of each pick", () => {
const S = require("../../core/solver.js");
const P = require("../../data/public/policy.json");
const best = S.advisor(P.samples.all, "Blue", "eventually").next({});
assert.strictEqual(best.propensity, 1);
const a = S.advisor(P.samples.all, "Blue", "eventually", { sampling: "thompson", seed: 3, draws: 8 });
const b = S.advisor(P.samples.all, "Blue", "eventually", { sampling: "thompson", seed: 3, draws: 8 });
const ra = a.next({}), rb = b.next({});
assert.strictEqual(ra.choice, rb.choice); // same seed, same draw
assert.ok(ra.propensity >= 0 && ra.propensity <= 1);
const picks = new Set();
for (let s = 1; s <= 12; s++) picks.add(S.advisor(P.samples.all, "Blue", "eventually", { sampling: "thompson", seed: s, draws: 1 }).next({}).choice);
assert.ok(picks.size > 1, "different seeds explore different options");
});