Refactor: lay the repo out by section (core, collection, analysis, planner, site) (#8)

Moves only; no behaviour changes. Every file now lives under the part of the project it
belongs to, each section has a README, and docs/PRIVACY.md collects the rules all parts follow.

  core/        engine.js, graph.js, solver.js, python/clean.py
  collection/  sources/ (form, register, PDF check, daily update), bot/ (importer)
  analysis/    stats/ (Python statistics), network/ (build-graph.js), reports/ (POLICY.md)
  planner/     build-policy.js, policy-report.js
  site/        atlas/ (the page), build.js, qa/
  tests/       core/ (unit), parity/ (JS vs Python), fixtures/

Checked against the previous layout: tests pass; coded runs, test outputs, graph.json and
policy.json are byte-identical; the built site differs only in comments that name file paths.
The published file names Ken's bot downloads (solver.js, graph.js, data/policy.json) are unchanged.

Also removes the undefined/ folder of QA screenshots committed by mistake (the QA scripts now
default to a temp folder) and test.txt, and drops the unused npm workspaces.


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 19:46:33 +00:00
committed by GitHub
co-authored by Claude Opus 5.5
parent dcd5117373
commit 432e04c2c7
66 changed files with 201 additions and 2695 deletions
+874
View File
@@ -0,0 +1,874 @@
/*
* solver.js: best decisions for bonding each dragon colour.
*
* Model: the threshing as a decision tree with perfect recall (an influence diagram).
* decision nodes company, direction, the cadet encounter in the game's order (the scream: investigate or keep walking; the
* clearing: help your squadmate or walk away; your first move in the fight and, after pausing, the dagger; the bullies' fate; or the wounded cadet), fork in the road, tree or valley, how to climb down,
* whether to face the valley Red, and at every bonding encounter: attempt the bond or die on purpose
* chance nodes first dragon colour, dying at the sighting / in the bully fight / before the hour,
* the shadow at the tree and its colour, which dragon waits in the meadow, the encounter outcome
*
* Chance probabilities depend on the history that came before them (not Markov). Each one is estimated with
* hierarchical Dirichlet back-off: the estimate for a full history is shrunk toward the estimate for a shorter
* history, which is shrunk toward the overall rate (concentration ALPHA). Uncertainty comes from Monte Carlo
* draws of all those Dirichlet posteriors; each draw is solved again so we can say how often the recommended
* choice is still the best one.
*
* Two objectives per target colour X, reported side by side:
* 1. "this attempt": maximise P(bond X on this attempt).
* 2. "eventually": deaths are free (you can always try again after a cooldown), so what matters is not to
* end the account on the wrong dragon: maximise P(X) / (P(X) + P(another colour)).
* Solved by Dinkelbach iteration; ties broken by P(X), and the expected number of attempts
* (1 / P(X)) is reported with it.
*
* UMD: window.ThreshSolver in the browser, module.exports in Node. No dependencies.
*/
(function (root, factory) {
if (typeof module === "object" && module.exports) module.exports = factory();
else root.ThreshSolver = factory();
})(typeof self !== "undefined" ? self : this, function () {
"use strict";
const COLOURS = ["Red", "Orange", "Brown", "Green", "Blue", "Black"];
const RARE_GOALS = ["Blue", "Black", "Blue or Black"];
const ALPHA = 20;
/* ---------- 1. From coded form runs to solver variables ---------- */
const HELP = { "Killed the bullies yourself": 1, "Squadmate's revenge": 1, "Left bullies to the dragons": 1 };
const IGNORE = { "Tourniquet": 1, "Left injured cadet": 1, "Took their weapons": 1 };
// the resolution reached so far, whichever way the history was written down
const cadetOf = (h) => h.cadet || h.fate || h.wounded || (h.clearing === "Walk away" ? "Left them to it" : null);
const screamOf = (h) => h.scream || (cadetOf(h) ? (IGNORE[cadetOf(h)] ? "Keep walking" : "Investigate the scream") : null);
const clearingOf = (h) => h.clearing || (cadetOf(h) && !IGNORE[cadetOf(h)] ? (cadetOf(h) === "Left them to it" ? "Walk away" : "Help your squadmate") : null);
const DECISIONS = {
company: ["Alone", "1 Squadmate", "2 Squadmates"],
// entering with squadmates, you can later leave them and go on alone
leave: ["Stay together", "Leave them"],
direction: ["East", "West", "Center"],
// how you react when the first dragon appears (the form's categories; which ones are offered depends on the dragon)
react: ["Hold still", "Respect / face it", "Retreat / run", "Hide", "Weapon", "Meet its gaze", "Protect squadmate"],
cadet: ["Killed the bullies yourself", "Squadmate's revenge", "Left bullies to the dragons",
"Tourniquet", "Left injured cadet", "Took their weapons", "Left them to it"],
// the cadet encounter, in the game's own order: the scream, then (if you investigate) the clearing, then the
// bullies' fate, or (if you keep walking) the wounded cadet
scream: ["Investigate the scream", "Keep walking"],
clearing: ["Help your squadmate", "Walk away"],
// the fight itself: your first move, and (if you paused to assess) what to do about the smaller cadet's dagger
fight: ["Pause to assess", "Charge the smaller cadet", "Charge between the bigger cadet and your squadmate"],
dagger: ["Engage the smaller cadet", "Charge the bigger cadet anyway"],
fate: ["Killed the bullies yourself", "Squadmate's revenge", "Left bullies to the dragons"],
wounded: ["Tourniquet", "Left injured cadet", "Took their weapons"],
post: ["Forest", "West", "East"],
hour: ["Tree", "Valley"],
descend: ["Quickly (Red)", "Slowly (two Greens)"],
// descending quickly puts you above a Red: jump onto it, or let it pass and go on to the two Greens
red: ["Jump onto the Red", "Let it pass (two Greens)"],
valley: ["Face the Red", "Pass the Red (meadow)"],
encounter: ["Attempt the bond", "Die on purpose"],
};
const LABEL = {
company: "Company", leave: "Your squadmates", direction: "First direction", react: "Facing the first dragon", red: "The Red below the tree", fs: "First dragon", dieSight: "Survive the first dragon",
cadet: "Cadet encounter", scream: "The scream", clearing: "The clearing (your squadmate vs two bullies)",
fight: "The fight: your first move", dagger: "The smaller cadet's dagger", fate: "The bullies' fate", wounded: "The wounded cadet", dieCombat: "Survive the fight", post: "Fork in the road", dieHour: "Survive the hour",
hour: "After an hour", shadow: "Shadow at the tree", shadowColour: "Shadow dragon", descend: "Climb down",
valley: "Valley centre", meadow: "Meadow dragon", encounter: "Bonding encounter",
};
// community-form codes for the fight moves -> the solver's option names
const FIGHT_OF = { "Assess the situation": "Pause to assess", "Even the numbers (smaller cadet)": "Charge the smaller cadet",
"Put yourself in the middle (bigger cadet)": "Charge between the bigger cadet and your squadmate" };
const DAGGER_OF = { "Engage the smaller cadet": "Engage the smaller cadet", "Daggers or not (protect squadmate)": "Charge the bigger cadet anyway" };
const NO_DAGGER = "(no dagger choice)";
// the fight moves in a history; a run that charged straight in never faces the dagger
const daggerAfter = (fight, dagger) => (fight == null ? null : fight === "Pause to assess" ? dagger || null : NO_DAGGER);
const ALONE = "(alone)";
const leftOf = (h) => (h.company === "Alone" ? ALONE : h.left || null);
const encColour = (ctx) => (ctx === "tree-Greens" ? "Green" : ctx.split("-")[1]);
/** The colour (or colours) a rider's stated goal commits them to, for deciding whether an attempt was genuine. */
function genuineAttempt(r, ctx) {
const c = encColour(ctx);
const g = r.goal || "";
const matches = g === c || g.startsWith("Any") || (g === "Blue or Black" && (c === "Blue" || c === "Black")) ||
(g === "Red (tree)" && ctx === "tree-Red");
if (matches) return true;
if (r.malek_intent === "No") return true;
if (r.malek_intent === "Yes") return false;
const specificOther = (COLOURS.includes(g) && g !== c) || (g === "Blue or Black") || g === "Red (tree)";
return !specificOther;
}
function prepareRun(r) {
const v = { company: DECISIONS.company.includes(r.company) ? r.company : null,
direction: DECISIONS.direction.includes(r.direction) ? r.direction : null,
fs: ["Red", "Brown", "Green", "Orange"].includes(r.first_sighting) ? r.first_sighting : null };
v.left = v.company === "Alone" ? ALONE : v.company ? (String(r.left_squadmates) === "true" || r.left_squadmates === true ? "Leave them" : "Stay together") : null;
v.react = DECISIONS.react.includes(r.sighting_action) ? r.sighting_action : null;
v.diedSight = r.scream === "DIED at first sighting";
if (v.diedSight) return v;
const res = r.resolution;
if (r.scream === "Investigate & help") {
v.help = true;
v.fight = FIGHT_OF[r.combat_first] || null;
v.dagger = daggerAfter(v.fight, DAGGER_OF[r.combat_second]);
v.diedCombat = res === "DIED in combat";
v.cadet = HELP[res] ? res : null;
if (v.diedCombat) return v;
v.post = DECISIONS.post.includes(r.post_path) ? r.post_path : null;
} else if (r.scream === "Ignore the scream") {
v.cadet = IGNORE[res] ? res : null; v.post = "(no fork)";
} else if (r.scream === "Investigate & leave") {
v.cadet = "Left them to it"; v.post = "(no fork)";
} else return v;
v.survivedCadets = true;
v.diedHour = r.hour === "DIED before the hour";
if (v.diedHour) return v;
v.hour = DECISIONS.hour.includes(r.hour) ? r.hour : null;
const b = r.branch;
if (v.hour === "Tree" && b) {
v.shadow = b === "Shadow (rare)";
if (v.shadow) { v.shadowColour = r.enc_Black ? "Black" : "Blue"; v.encCtx = "shadow-" + v.shadowColour; }
else if (b === "Descend quickly: Red") {
v.descend = "Quickly (Red)";
// a rider who let the Red pass and met the Greens instead
if (r.enc_Red === "Changed my mind" && r.enc_Green) { v.red = "Let it pass (two Greens)"; v.encCtx = "tree-Greens"; }
else { v.red = "Jump onto the Red"; v.encCtx = "tree-Red"; }
}
else if (b === "Descend slowly: two Greens") { v.descend = "Slowly (two Greens)"; v.encCtx = "tree-Greens"; }
} else if (v.hour === "Valley" && b) {
if (b === "Valley Red") { v.valley = "Face the Red"; v.encCtx = "valley-Red"; }
else if (b === "Passed the Red: meadow") {
v.valley = "Pass the Red (meadow)";
// the meadow dragon: Blue if one appeared, otherwise the one the rider ended the run with
const ended = (c) => r["enc_" + c] && r["enc_" + c] !== "Changed my mind";
v.meadow = r.enc_Blue || r.rare_reported === "Blue (valley)" ? "Blue" :
ended("Orange") ? "Orange" : ended("Brown") ? "Brown" : r.enc_Orange ? "Orange" : r.enc_Brown ? "Brown" : null;
if (v.meadow) v.encCtx = "meadow-" + v.meadow;
}
}
if (v.encCtx) {
v.encOutcome = r.outcome === "Bonded" && r.outcome_color ? r.outcome_color : r.outcome === "Incinerated" ? "burn" : null;
if (v.encOutcome && !genuineAttempt(r, v.encCtx)) v.encOutcome = null;
}
return v;
}
/* ---------- 2. Chance nodes with hierarchical Dirichlet back-off ---------- */
// ctx: earlier variables the probability may depend on, most important first. Back-off drops from the end.
const CHANCE = {
fs: { cats: ["Red", "Brown", "Green", "Orange"], ctx: ["direction", "company", "left"], get: (v) => v.fs },
dieSight: { cats: ["die", "live"], ctx: ["fs", "direction", "company", "left", "react"], get: (v) => (v.fs ? (v.diedSight ? "die" : "live") : null) },
dieCombat: { cats: ["die", "live"], ctx: ["fight", "dagger", "fs", "direction"], get: (v) => (v.help ? (v.diedCombat ? "die" : "live") : null) },
dieHour: { cats: ["die", "live"], ctx: ["cadet", "post"], get: (v) => (v.survivedCadets ? (v.diedHour ? "die" : "live") : null) },
shadow: { cats: ["yes", "no"], ctx: ["cadet", "post", "fs", "direction", "company"], get: (v) => (v.shadow === undefined ? null : v.shadow ? "yes" : "no") },
shadowColour: { cats: ["Black", "Blue"], ctx: [], get: (v) => v.shadowColour || null },
meadow: { cats: ["Orange", "Brown", "Blue"], ctx: ["cadet", "post", "fs", "direction", "company"], get: (v) => v.meadow || null },
};
const ENC_CATS = ["burn"].concat(COLOURS);
const ctxKey = (v, vars) => vars.map((k) => (v[k] == null ? "?" : v[k])).join("|");
/** Counts for every chance node at every back-off level. */
function countTables(prepared) {
const T = {};
for (const [node, spec] of Object.entries(CHANCE)) {
const t = (T[node] = {});
for (const v of prepared) {
const y = spec.get(v);
if (!y) continue;
for (let L = 0; L <= spec.ctx.length; L++) {
const vars = spec.ctx.slice(0, L);
if (vars.some((k) => v[k] == null)) break;
const key = L + ":" + ctxKey(v, vars);
const c = t[key] || (t[key] = {});
c[y] = (c[y] || 0) + 1;
}
}
}
const enc = (T.encounter = {});
for (const v of prepared) {
if (!v.encCtx) continue;
const c = enc[v.encCtx] || (enc[v.encCtx] = { outcomes: {}, seen: {} });
if (v.encOutcome) c.outcomes[v.encOutcome] = (c.outcomes[v.encOutcome] || 0) + 1;
}
// which colours can ever end a run in this context (from all runs, genuine or not)
for (const v of prepared) if (v.encCtx && v.encOutcome && v.encOutcome !== "burn") enc[v.encCtx].seen[v.encOutcome] = 1;
return T;
}
/* ---------- Empirical-Bayes concentration for each chance node ---------- */
function lgamma(x) {
const c = [76.18009172947146, -86.50532032941677, 24.01409824083091, -1.231739572450155, 0.1208650973866179e-2, -0.5395239384953e-5];
let y = x, tmp = x + 5.5, ser = 1.000000000190015;
tmp -= (x + 0.5) * Math.log(tmp);
for (const v of c) ser += v / ++y;
return -tmp + Math.log(2.5066282746310005 * ser / x);
}
const ALPHA_GRID = [1, 2, 5, 10, 20, 50, 100, 200, 500, 1000, 2000, 5000, 20000, 100000];
/**
* How much should a specific history be allowed to differ from the shorter history above it?
* Choose the concentration alpha that maximises the Dirichlet-multinomial marginal likelihood of the counts
* at every back-off level given the level above. A large alpha means the data show little history dependence,
* so the solver will not chase noise in small groups (the "optimiser's curse").
*/
function fitAlpha(T, node) {
const spec = CHANCE[node], t = T[node], K = spec.cats.length;
const pooled = (key) => { const c = t[key] || {}; const a = spec.cats.map((k) => (c[k] || 0) + 0.5); const n = a.reduce((x, y) => x + y, 0); return a.map((x) => x / n); };
const pairs = [];
for (const key of Object.keys(t)) {
const L = +key.split(":")[0];
if (L === 0) continue;
const ctx = key.slice(key.indexOf(":") + 1).split("|");
const parent = (L - 1) + ":" + ctx.slice(0, L - 1).join("|");
pairs.push([spec.cats.map((k) => t[key][k] || 0), pooled(parent)]);
}
if (!pairs.length) return { alpha: ALPHA, logLik: null };
let bestA = ALPHA, bestLL = -Infinity;
const lls = {};
for (const a of ALPHA_GRID) {
let ll = 0;
for (const [c, p] of pairs) {
const n = c.reduce((x, y) => x + y, 0);
ll += lgamma(a) - lgamma(n + a);
for (let i = 0; i < K; i++) ll += lgamma(c[i] + a * p[i]) - lgamma(a * p[i]);
}
lls[a] = ll;
if (ll > bestLL) { bestLL = ll; bestA = a; }
}
return { alpha: bestA, logLik: lls };
}
/* random numbers: seeded, so results are reproducible */
function rng(seed) {
let s = seed >>> 0 || 1;
return () => { s ^= s << 13; s >>>= 0; s ^= s >>> 17; s ^= s << 5; s >>>= 0; return s / 4294967296; };
}
function gamma(shape, rand) {
if (shape < 1) return gamma(shape + 1, rand) * Math.pow(rand() || 1e-12, 1 / shape);
const d = shape - 1 / 3, c = 1 / Math.sqrt(9 * d);
for (;;) {
let x, v;
do {
const u1 = rand() || 1e-12, u2 = rand();
x = Math.sqrt(-2 * Math.log(u1)) * Math.cos(2 * Math.PI * u2);
v = 1 + c * x;
} while (v <= 0);
v = v * v * v;
const u = rand();
if (u < 1 - 0.0331 * x ** 4 || Math.log(u) < 0.5 * x * x + d * (1 - v + Math.log(v))) return d * v;
}
}
function dirichlet(alpha, rand) {
const g = alpha.map((a) => gamma(Math.max(a, 1e-6), rand));
const s = g.reduce((a, b) => a + b, 0);
return g.map((x) => x / s);
}
/**
* A probability source. mode "mean" gives posterior means; mode "draw" gives one joint posterior draw
* (each distribution is drawn once and reused, so a draw is a consistent world).
*/
function makeParams(T, { mode = "mean", seed = 1, alpha = ALPHA, alphas = {} } = {}) {
const rand = mode === "draw" ? rng(seed) : null;
const memo = new Map();
function chance(node, h) {
const spec = CHANCE[node];
// deepest level with this exact context, then build top-down
const levels = [];
for (let L = 0; L <= spec.ctx.length; L++) {
const vars = spec.ctx.slice(0, L);
if (vars.some((k) => h[k] == null)) break;
levels.push(L + ":" + ctxKey(h, vars));
}
let p = null;
for (const key of levels) {
const mk = node + "#" + key;
if (memo.has(mk)) { p = memo.get(mk); continue; }
const c = T[node][key] || {};
const al = alphas[node] || alpha;
const a = spec.cats.map((k, i) => (c[k] || 0) + (p ? al * p[i] : 0.5));
p = rand ? dirichlet(a, rand) : a.map((x) => x / a.reduce((s, y) => s + y, 0));
memo.set(mk, p);
}
return Object.fromEntries(spec.cats.map((k, i) => [k, p[i]]));
}
function encounter(ctx) {
const mk = "enc#" + ctx;
if (!memo.has(mk)) {
const e = T.encounter[ctx] || { outcomes: {}, seen: {} };
const cats = ENC_CATS.filter((k) => k === "burn" || e.seen[k] || (e.outcomes[k] || 0) > 0 || k === encColour(ctx));
const a = cats.map((k) => (e.outcomes[k] || 0) + 0.5);
const p = rand ? dirichlet(a, rand) : a.map((x) => x / a.reduce((s, y) => s + y, 0));
memo.set(mk, Object.fromEntries(cats.map((k, i) => [k, p[i]])));
}
return memo.get(mk);
}
return { chance, encounter };
}
/* ---------- 3. The decision tree and backward induction ---------- */
// x = P(target colour), o = P(another colour), r = P(reaching a bonding encounter alive)
const ZERO = { x: 0, o: 0, r: 0 };
const REACHED = { x: 0, o: 0, r: 1 };
const add = (a, b, w) => ({ x: a.x + w * b.x, o: a.o + w * b.o, r: a.r + w * b.r });
/**
* Solve for target colour X. score(v) = (1 - lam) * P(X) - lam * P(other) (+ tiny tie-break on P(X)).
* Returns the value at the root and a decide(name, h) function giving the best option at any history.
* `support` lists the options allowed at each decision (options without enough data are left out).
*/
function solve(params, X, { lam = 0, support = DECISIONS, record = false, only = null } = {}) {
// only: bond only at these encounters (e.g. ["meadow-Blue"]); everywhere else the rider dies on purpose
const encOptions = (ctx) => (only && !only.includes(ctx) ? ["Die on purpose"] : support.encounter);
const reactOptions = (fs) => support["react:" + fs] || DECISIONS.react;
const score = (v) => (1 - lam) * v.x - lam * v.o + 1e-9 * v.x - 1e-12 * v.o;
const memo = new Map();
const choices = new Map();
const optVals = record ? new Map() : null;
function best(name, h, options, next) {
const key = name + "@" + JSON.stringify(h);
if (memo.has(key)) return memo.get(key);
let top = null, arg = null;
const vals = {};
for (const o of options) {
const v = next(o);
vals[o] = v;
if (!top || score(v) > score(top)) { top = v; arg = o; }
}
memo.set(key, top);
choices.set(key, arg);
if (optVals) optVals.set(key, vals);
return top;
}
const mix = (dist, f) => Object.entries(dist).reduce((acc, [k, p]) => (p ? add(acc, f(k), p) : acc), ZERO);
const die = () => ZERO;
const encounter = (h, ctx) => best("encounter", { ...h, enc: ctx }, encOptions(ctx), (o) => {
if (o === "Die on purpose") return REACHED;
const d = params.encounter(ctx);
let x = 0, oth = 0;
for (const [k, p] of Object.entries(d)) if (k !== "burn") { if (k === X) x += p; else oth += p; }
return { x, o: oth, r: 1 };
});
const tree = (h) => mix(params.chance("shadow", h), (s) => s === "yes"
? mix(params.chance("shadowColour", h), (c) => encounter({ ...h, shadow: "yes" }, "shadow-" + c))
: best("descend", { ...h, shadow: "no" }, support.descend, (o) => o.startsWith("Quickly")
? best("red", { ...h, shadow: "no", descend: o }, support.red, (rd) => encounter({ ...h, shadow: "no", descend: o, red: rd }, rd.startsWith("Jump") ? "tree-Red" : "tree-Greens"))
: encounter({ ...h, shadow: "no", descend: o }, "tree-Greens")));
const valley = (h) => best("valley", h, support.valley, (o) => o === "Face the Red"
? encounter({ ...h, valley: o }, "valley-Red")
: mix(params.chance("meadow", h), (c) => encounter({ ...h, valley: o, meadow: c }, "meadow-" + c)));
const hour = (h) => mix(params.chance("dieHour", h), (s) => s === "die" ? die() :
best("hour", h, support.hour, (o) => (o === "Tree" ? tree : valley)({ ...h, hour: o })));
const cadet = (h) => best("scream", h, support.scream, (sc) => {
const h1 = { ...h, scream: sc };
if (sc === "Keep walking") return best("wounded", h1, support.wounded, (o) => hour({ ...h1, cadet: o, post: "(no fork)" }));
return best("clearing", h1, support.clearing, (cl) => {
const h2 = { ...h1, clearing: cl };
if (cl === "Walk away") return hour({ ...h2, cadet: "Left them to it", post: "(no fork)" });
const combat = (hf) => mix(params.chance("dieCombat", hf), (s) => s === "die" ? die() :
best("fate", hf, support.fate, (o) => { const h3 = { ...h2, cadet: o }; return best("post", h3, support.post, (p) => hour({ ...h3, post: p })); }));
return best("fight", h2, support.fight, (f) => f === "Pause to assess"
? best("dagger", { ...h2, fight: f }, support.dagger, (d) => combat({ ...h2, fight: f, dagger: d }))
: combat({ ...h2, fight: f, dagger: NO_DAGGER }));
});
});
// the first dragon: which one appears (chance), how you react (decision), whether you survive (chance).
// Leaving your squadmates and your reaction only matter here, so the rest of the history leaves them out.
const sighting = (h) => mix(params.chance("fs", h), (c) => {
const h2 = { ...h, fs: c }, rest = { company: h.company, direction: h.direction, fs: c };
return best("react", h2, reactOptions(c), (re) =>
mix(params.chance("dieSight", { ...h2, react: re }), (s) => (s === "die" ? die() : cadet(rest))));
});
const company = (c, f) => (c === "Alone" ? f({ company: c, left: ALONE }) : best("leave", { company: c }, support.leave, (l) => f({ company: c, left: l })));
const root = best("company", {}, support.company, (c) => company(c, (hc) =>
best("direction", hc, support.direction, (d) => sighting({ ...hc, direction: d }))));
memo.clear(); // the values are no longer needed; posterior draws keep only their choices
const decide = (name, h) => choices.get(name + "@" + JSON.stringify(h));
return { value: root, decide, optionValues: (name, h) => optVals && optVals.get(name + "@" + JSON.stringify(h)) };
}
/** Objective 2 by Dinkelbach: find lam* = max P(X) / (P(X) + P(other)). */
function solveRatio(params, X, opts = {}) {
let lam = 0, s = solve(params, X, { ...opts, lam });
for (let i = 0; i < 30; i++) {
const v = s.value, r = v.x + v.o > 0 ? v.x / (v.x + v.o) : 0;
if (Math.abs(r - lam) < 1e-10) break;
lam = r;
s = solve(params, X, { ...opts, lam });
}
return { ...s, lam };
}
/* ---------- 4. Evaluate a fixed policy (for credible intervals) ---------- */
function evaluate(params, X, decide) {
// same tree as solve(), but decisions come from `decide` (the policy chosen on the posterior means)
const pick = (name, h, f) => f(decide(name, h));
const mix = (dist, f) => Object.entries(dist).reduce((acc, [k, p]) => (p ? add(acc, f(k), p) : acc), ZERO);
const encounter = (h, ctx) => pick("encounter", { ...h, enc: ctx }, (o) => {
if (o === "Die on purpose") return REACHED;
const d = params.encounter(ctx);
let x = 0, oth = 0;
for (const [k, p] of Object.entries(d)) if (k !== "burn") { if (k === X) x += p; else oth += p; }
return { x, o: oth, r: 1 };
});
const tree = (h) => mix(params.chance("shadow", h), (s) => s === "yes"
? mix(params.chance("shadowColour", h), (c) => encounter({ ...h, shadow: "yes" }, "shadow-" + c))
: pick("descend", { ...h, shadow: "no" }, (o) => o.startsWith("Quickly")
? pick("red", { ...h, shadow: "no", descend: o }, (rd) => encounter({ ...h, shadow: "no", descend: o, red: rd }, rd.startsWith("Jump") ? "tree-Red" : "tree-Greens"))
: encounter({ ...h, shadow: "no", descend: o }, "tree-Greens")));
const valley = (h) => pick("valley", h, (o) => o === "Face the Red"
? encounter({ ...h, valley: o }, "valley-Red")
: mix(params.chance("meadow", h), (c) => encounter({ ...h, valley: o, meadow: c }, "meadow-" + c)));
const hour = (h) => mix(params.chance("dieHour", h), (s) => s === "die" ? ZERO :
pick("hour", h, (o) => (o === "Tree" ? tree : valley)({ ...h, hour: o })));
const cadet = (h) => pick("scream", h, (sc) => {
const h1 = { ...h, scream: sc };
if (sc === "Keep walking") return pick("wounded", h1, (o) => hour({ ...h1, cadet: o, post: "(no fork)" }));
return pick("clearing", h1, (cl) => {
const h2 = { ...h1, clearing: cl };
if (cl === "Walk away") return hour({ ...h2, cadet: "Left them to it", post: "(no fork)" });
const combat = (hf) => mix(params.chance("dieCombat", hf), (s) => s === "die" ? ZERO :
pick("fate", hf, (o) => { const h3 = { ...h2, cadet: o }; return pick("post", h3, (p) => hour({ ...h3, post: p })); }));
return pick("fight", h2, (f) => f === "Pause to assess"
? pick("dagger", { ...h2, fight: f }, (d) => combat({ ...h2, fight: f, dagger: d }))
: combat({ ...h2, fight: f, dagger: NO_DAGGER }));
});
});
const sighting = (h) => mix(params.chance("fs", h), (c) => {
const h2 = { ...h, fs: c }, rest = { company: h.company, direction: h.direction, fs: c };
return pick("react", h2, (re) => mix(params.chance("dieSight", { ...h2, react: re }), (s) => (s === "die" ? ZERO : cadet(rest))));
});
const company = (c, f) => (c === "Alone" ? f({ company: c, left: ALONE }) : pick("leave", { company: c }, (l) => f({ company: c, left: l })));
return pick("company", {}, (c) => company(c, (hc) => pick("direction", hc, (d) => sighting({ ...hc, direction: d }))));
}
/* ---------- 5. The published policy tree ---------- */
/**
* Walk the point-estimate policy and write it out as a tree: at decisions, the chosen option plus the value of
* every option (each followed by the best play afterwards) and how often it stays best across posterior draws;
* at chance nodes, every branch with its probability. Only the chosen option's subtree is expanded.
*/
function exportTree(params, X, sol, draws, minShow = 1e-4) {
const vals = (name, h) => sol.optionValues(name, h) || {};
const conf = (name, h, chosen) => (draws.length ? draws.filter((d) => d.decide(name, h) === chosen).length / draws.length : null);
const fmt = (v) => ({ pX: v.x, pOther: v.o, pReach: v.r });
const decision = (name, h, cont) => {
const chosen = sol.decide(name, h);
const ov = vals(name, h);
const xs = Object.values(ov).map((v) => v.x).sort((p, q) => q - p);
const tie = xs.length > 1 && xs[0] - xs[1] < 1e-12; // nothing to choose between (e.g. every option ends the run)
return {
type: "decision", var: name, label: LABEL[name], choice: chosen, tie, stillBest: tie ? null : conf(name, h, chosen),
options: Object.fromEntries(Object.entries(ov).map(([o, v]) => [o, fmt(v)])),
next: cont(chosen),
};
};
const chance = (name, dist, cont) => ({
type: "chance", var: name, label: LABEL[name],
branches: Object.fromEntries(Object.entries(dist).filter(([, p]) => p >= minShow).map(([k, p]) => [k, { p, next: cont(k) }])),
});
const end = (what) => ({ type: "end", what });
const encounter = (h, ctx) => decision("encounter", { ...h, enc: ctx }, (o) => o === "Die on purpose" ? end("die on purpose") :
({ type: "end", what: `encounter: ${ctx}`, outcomes: params.encounter(ctx) }));
const tree = (h) => chance("shadow", params.chance("shadow", h), (s) => s === "yes"
? chance("shadowColour", params.chance("shadowColour", h), (c) => encounter({ ...h, shadow: "yes" }, "shadow-" + c))
: decision("descend", { ...h, shadow: "no" }, (o) => o.startsWith("Quickly")
? decision("red", { ...h, shadow: "no", descend: o }, (rd) => encounter({ ...h, shadow: "no", descend: o, red: rd }, rd.startsWith("Jump") ? "tree-Red" : "tree-Greens"))
: encounter({ ...h, shadow: "no", descend: o }, "tree-Greens")));
const valley = (h) => decision("valley", h, (o) => o === "Face the Red"
? encounter({ ...h, valley: o }, "valley-Red")
: chance("meadow", params.chance("meadow", h), (c) => encounter({ ...h, valley: o, meadow: c }, "meadow-" + c)));
const hour = (h) => chance("dieHour", params.chance("dieHour", h), (s) => s === "die" ? end("incinerated") :
decision("hour", h, (o) => (o === "Tree" ? tree : valley)({ ...h, hour: o })));
const cadet = (h) => decision("scream", h, (sc) => {
const h1 = { ...h, scream: sc };
if (sc === "Keep walking") return decision("wounded", h1, (o) => hour({ ...h1, cadet: o, post: "(no fork)" }));
return decision("clearing", h1, (cl) => {
const h2 = { ...h1, clearing: cl };
if (cl === "Walk away") return hour({ ...h2, cadet: "Left them to it", post: "(no fork)" });
const combat = (hf) => chance("dieCombat", params.chance("dieCombat", hf), (s) => s === "die" ? end("killed in the fight") :
decision("fate", hf, (o) => { const h3 = { ...h2, cadet: o }; return decision("post", h3, (p) => hour({ ...h3, post: p })); }));
return decision("fight", h2, (f) => f === "Pause to assess"
? decision("dagger", { ...h2, fight: f }, (d) => combat({ ...h2, fight: f, dagger: d }))
: combat({ ...h2, fight: f, dagger: NO_DAGGER }));
});
});
const sighting = (h) => chance("fs", params.chance("fs", h), (c) => {
const h2 = { ...h, fs: c }, rest = { company: h.company, direction: h.direction, fs: c };
return decision("react", h2, (re) => chance("dieSight", params.chance("dieSight", { ...h2, react: re }), (s) => (s === "die" ? end("incinerated at the first sighting") : cadet(rest))));
});
const company = (c, f) => (c === "Alone" ? f({ company: c, left: ALONE }) : decision("leave", { company: c }, (l) => f({ company: c, left: l })));
return decision("company", {}, (c) => company(c, (hc) => decision("direction", hc, (d) => sighting({ ...hc, direction: d }))));
}
const quantile = (xs, q) => {
const s = xs.slice().sort((a, b) => a - b);
const i = (s.length - 1) * q, lo = Math.floor(i), hi = Math.ceil(i);
return s[lo] + (s[hi] - s[lo]) * (i - lo);
};
const summarise = (xs) => ({ mean: xs.reduce((a, b) => a + b, 0) / xs.length, lo: quantile(xs, 0.025), hi: quantile(xs, 0.975) });
/** Options seen fewer than `min` times in the data are not recommended. */
function supportFrom(prepared, min) {
const n = {};
const bump = (k, o) => { n[k] = n[k] || {}; n[k][o] = (n[k][o] || 0) + 1; };
for (const v of prepared) {
for (const k of ["company", "direction", "cadet", "hour", "descend", "valley"]) if (v[k]) bump(k, v[k]);
if (v.left && v.left !== ALONE) bump("leave", v.left);
if (v.fs && v.react) bump("react:" + v.fs, v.react);
if (v.post && v.post !== "(no fork)") bump("post", v.post);
// the cadet encounter's steps, including riders killed in the fight (they chose to help)
if (v.help || v.cadet) {
const ignore = !!(v.cadet && IGNORE[v.cadet]);
bump("scream", ignore ? "Keep walking" : "Investigate the scream");
if (ignore) bump("wounded", v.cadet);
else {
bump("clearing", v.cadet === "Left them to it" ? "Walk away" : "Help your squadmate");
if (HELP[v.cadet]) bump("fate", v.cadet);
if (v.help && v.fight) { bump("fight", v.fight); if (v.dagger && v.dagger !== NO_DAGGER) bump("dagger", v.dagger); }
}
}
}
const s = { encounter: DECISIONS.encounter };
for (const k of ["company", "leave", "direction", "cadet", "scream", "clearing", "fight", "dagger", "fate", "wounded", "post", "hour", "descend", "valley"])
{
s[k] = DECISIONS[k].filter((o) => ((n[k] || {})[o] || 0) >= min);
// a step the data never record (e.g. the fight moves in an old export) is not restricted
if (!s[k].length) s[k] = DECISIONS[k].slice();
}
// reactions to the first dragon, per dragon (each dragon offers its own options)
for (const fs of ["Red", "Brown", "Green", "Orange"]) {
const k = "react:" + fs, ok = DECISIONS.react.filter((o) => ((n[k] || {})[o] || 0) >= min);
s[k] = ok.length ? ok : DECISIONS.react.slice();
}
// letting the Red pass is rarely logged, but its outcome is the two Greens' encounter, which has plenty of data
s.red = DECISIONS.red.slice();
return { support: s, counts: n };
}
/**
* Build the full policy for every colour.
* runs: coded runs from engine.codeRows(); opts.draws: Monte Carlo draws for intervals.
*/
function buildPolicy(runs, { draws = 200, seed = 20261101, minSupport = 20, alpha = null, colours = COLOURS } = {}) {
const prepared = runs.map(prepareRun);
const T = countTables(prepared);
const { support, counts } = supportFrom(prepared, minSupport);
const fitted = Object.fromEntries(Object.keys(CHANCE).map((k) => [k, fitAlpha(T, k)]));
const alphas = alpha ? Object.fromEntries(Object.keys(CHANCE).map((k) => [k, alpha])) : Object.fromEntries(Object.entries(fitted).map(([k, f]) => [k, f.alpha]));
const mean = makeParams(T, { mode: "mean", alphas });
const drawParams = Array.from({ length: draws }, (_, i) => makeParams(T, { mode: "draw", seed: seed + i * 7919, alphas }));
// Cross-fitting: choose the policy on one random half of the runs and score it on the other half.
// The gap between the in-sample and held-out values shows how much of a route's edge is noise.
const r = rng(seed);
const fold = prepared.map(() => (r() < 0.5 ? 0 : 1));
const halves = [0, 1].map((f) => countTables(prepared.filter((_, i) => fold[i] === f)));
const halfParams = halves.map((Th) => makeParams(Th, { mode: "mean", alphas }));
const targets = {};
for (const X of colours) {
const one = solve(mean, X, { support, record: true });
const ratio = solveRatio(mean, X, { support });
const ratioRec = solve(mean, X, { support, lam: ratio.lam, record: true });
const dOne = drawParams.map((p) => solve(p, X, { support }));
const dRatio = drawParams.map((p) => solveRatio(p, X, { support }));
const evOne = drawParams.map((p) => evaluate(p, X, one.decide));
const evRatio = drawParams.map((p) => evaluate(p, X, ratioRec.decide));
const block = (sol, ev, dr) => ({
pX: { ...summarise(ev.map((v) => v.x)), point: sol.value.x },
pOther: { ...summarise(ev.map((v) => v.o)), point: sol.value.o },
ratio: summarise(ev.map((v) => (v.x + v.o ? v.x / (v.x + v.o) : 0))),
expectedAttempts: summarise(ev.map((v) => (v.x > 0 ? 1 / v.x : Infinity))),
tree: exportTree(mean, X, sol, dr),
});
const cross = [0, 1].map((f) => {
const pol = solve(halfParams[f], X, { support });
return { chosenOn: f, inSample: pol.value.x, heldOut: evaluate(halfParams[1 - f], X, pol.decide).x };
});
targets[X] = { thisAttempt: block(one, evOne, dOne), eventually: block(ratioRec, evRatio, dRatio),
crossFit: { inSample: (cross[0].inSample + cross[1].inSample) / 2, heldOut: (cross[0].heldOut + cross[1].heldOut) / 2 } };
}
return {
schema: 1,
model: {
description: "Decision tree with perfect recall; chance probabilities depend on earlier choices via hierarchical Dirichlet back-off.",
alphas, alphaFit: Object.fromEntries(Object.entries(fitted).map(([k, f]) => [k, f.logLik])), draws, seed, minSupport,
chanceContext: Object.fromEntries(Object.entries(CHANCE).map(([k, s]) => [k, s.ctx])),
decisions: DECISIONS, support, supportCounts: counts,
tables: T,
},
data: { runs: runs.length },
targets,
};
}
/* ---------- 6. Bot helper: what to pick on this screen ---------- */
const OPTION_PATTERNS = {
company: { "Alone": /^enter the valley alone/, "1 Squadmate": /^enter the valley with one/, "2 Squadmates": /^enter the valley with two/ },
direction: { East: /^head east/, West: /^head west/, Center: /^head toward the center of the forest/ },
post: { East: /^walk toward the east/, West: /^walk toward the west/, Forest: /^walk farther into the forest/ },
hour: { Valley: /^continue toward the center of the valley/, Tree: /^find the tallest tree/ },
descend: { "Quickly (Red)": /^descend quickly/, "Slowly (two Greens)": /^climb down slowly/ },
resolution: {
"Killed the bullies yourself": /^end the lives of the bullies/, "Squadmate's revenge": /^allow your squadmate to exact/,
"Left bullies to the dragons": /^leave the bullies alive/, "Tourniquet": /tourniquet/, "Took their weapons": /weapon/,
},
scream: { "Investigate the scream": /^investigate the scream/, "Keep walking": /^keep walking/ },
clearing: { "Help your squadmate": /^help your squadmate/, "Walk away": /^play it safe and leave them/ },
fight: { "Pause to assess": /^pause to assess/, "Charge the smaller cadet": /^charge the other cadet/,
"Charge between the bigger cadet and your squadmate": /^unsheathe your weapon/ },
dagger: { "Engage the smaller cadet": /^engage the smaller cadet/, "Charge the bigger cadet anyway": /^daggers or not/ },
react: { "Hold still": /hold still/, "Respect / face it": /in respect|turn to face it/, "Retreat / run": /^run and hope/,
"Hide": /^hide/, "Meet its gaze": /meeting its gaze/ },
red: { "Jump onto the Red": /^jump/, "Let it pass (two Greens)": /^reds are too hot tempered/ },
};
const norm = (t) => String(t || "").toLowerCase().replace(/[‘’']/g, "'").replace(/[–—-]/g, " ").replace(/[^a-z0-9'\s]/g, " ").replace(/\s+/g, " ").trim();
/**
* Convert the community-form variables of a (possibly unfinished) run into the solver's history keys.
* For the bot: graph.trajectoryVariables(trajectory).vars gives the form variables; add what the bot saw
* on screen itself: `meadow` (colour in the meadow), `shadow` ("yes"/"no" at the treetop), `shadowColour`.
*/
function solverHistory(vars, seen = {}) {
const h = {};
if (DECISIONS.company.includes(vars.company)) h.company = vars.company;
if (h.company && h.company !== "Alone" && vars.left_squadmates != null) h.left = vars.left_squadmates === true || vars.left_squadmates === "true" ? "Leave them" : "Stay together";
if (DECISIONS.direction.includes(vars.direction)) h.direction = vars.direction;
if (DECISIONS.react.includes(vars.sighting_action)) h.react = vars.sighting_action;
if (["Red", "Brown", "Green", "Orange"].includes(vars.first_sighting)) h.fs = vars.first_sighting;
if (vars.scream === "Ignore the scream") h.scream = "Keep walking";
else if (vars.scream === "Investigate & help" || vars.scream === "Investigate & leave" || vars.scream_investigate) h.scream = "Investigate the scream";
if (vars.scream === "Investigate & help") h.clearing = "Help your squadmate";
if (vars.scream === "Investigate & leave") h.clearing = "Walk away";
if (FIGHT_OF[vars.combat_first]) h.fight = FIGHT_OF[vars.combat_first];
if (DAGGER_OF[vars.combat_second]) h.dagger = DAGGER_OF[vars.combat_second];
if (DECISIONS.cadet.includes(vars.resolution)) {
h.cadet = vars.resolution;
if (HELP[h.cadet]) h.fate = h.cadet;
if (IGNORE[h.cadet]) h.wounded = h.cadet;
}
if (DECISIONS.post.includes(vars.post_path)) h.post = vars.post_path;
else if (h.cadet && !HELP[h.cadet]) h.post = "(no fork)";
if (DECISIONS.hour.includes(vars.hour)) h.hour = vars.hour;
if (vars.branch === "Descend quickly: Red") h.descend = "Quickly (Red)";
if (DECISIONS.red.includes(vars.red)) h.red = vars.red;
if (vars.branch === "Descend slowly: two Greens") h.descend = "Slowly (two Greens)";
if (vars.branch === "Valley Red") h.valley = "Face the Red";
if (vars.branch === "Passed the Red: meadow") h.valley = "Pass the Red (meadow)";
for (const k of ["meadow", "shadow", "shadowColour"]) if (seen[k]) h[k] = seen[k];
if (h.hour === "Tree" && h.descend && !h.shadow) h.shadow = "no";
return h;
}
/** Follow a published policy tree along a partial history; returns the node where the rider now stands. */
function locate(tree, h) {
let node = tree;
while (node && node.type !== "end") {
if (node.type === "decision") {
const val = h[node.var];
if (val == null) return node; // the next decision to make
if (val !== node.choice) return { type: "off-policy", var: node.var, recommended: node.choice, actual: val };
node = node.next;
} else {
// a rider who is asking for advice survived every earlier risk
const val = node.var.startsWith("die") ? "live" : h[node.var];
if (val == null) return node; // waiting for chance to resolve
node = node.branches[val] ? node.branches[val].next : { type: "unknown-branch", var: node.var, value: val };
}
}
return node;
}
/**
* The next decision a rider faces after history h, as the (name, key) used inside solve().
* Returns null when the next step is chance (e.g. the first dragon has not appeared yet) or the run is over.
* Keys are built in the same order as in solve() so they match its memo keys.
*/
function nextDecision(h) {
if (!h.company) return ["company", {}];
const left = leftOf(h);
if (!left) return ["leave", { company: h.company }];
if (!h.direction) return ["direction", { company: h.company, left }];
if (!h.fs) return null;
const a = { company: h.company, direction: h.direction, fs: h.fs };
const sc = screamOf(h), cad = cadetOf(h);
// a history past the first dragon without a recorded reaction: no advice is needed for it any more
if (!h.react && !sc) return ["react", { company: h.company, left, direction: h.direction, fs: h.fs }];
if (!sc) return ["scream", a];
const h1 = { ...a, scream: sc };
let c;
if (sc === "Keep walking") {
if (!cad) return ["wounded", h1];
c = { ...h1, cadet: cad, post: "(no fork)" };
} else {
const cl = clearingOf(h);
if (!cl) return ["clearing", h1];
const h2 = { ...h1, clearing: cl };
if (cl === "Walk away") c = { ...h2, cadet: "Left them to it", post: "(no fork)" };
else {
// older records may skip the fight moves; assume the usual ones (pause, then engage) so advice can continue
const f = h.fight || (cad ? "Pause to assess" : null);
if (!f) return ["fight", h2];
const dg = daggerAfter(f, h.dagger || (cad ? "Engage the smaller cadet" : null));
if (!dg) return ["dagger", { ...h2, fight: f }];
const hf = { ...h2, fight: f, dagger: dg };
if (!cad) return ["fate", hf];
const h3 = { ...h2, cadet: cad };
if (!h.post) return ["post", h3];
c = { ...h3, post: h.post };
}
}
if (!h.hour) return ["hour", c];
const d = { ...c, hour: h.hour };
if (h.hour === "Tree") {
if (!h.shadow) return null;
if (h.shadow === "yes") return h.shadowColour ? ["encounter", { ...d, shadow: "yes", enc: "shadow-" + h.shadowColour }] : null;
if (!h.descend) return ["descend", { ...d, shadow: "no" }];
if (h.descend.startsWith("Quickly")) {
if (!h.red) return ["red", { ...d, shadow: "no", descend: h.descend }];
return ["encounter", { ...d, shadow: "no", descend: h.descend, red: h.red, enc: h.red.startsWith("Jump") ? "tree-Red" : "tree-Greens" }];
}
return ["encounter", { ...d, shadow: "no", descend: h.descend, enc: "tree-Greens" }];
}
if (!h.valley) return ["valley", d];
if (h.valley === "Face the Red") return ["encounter", { ...d, valley: h.valley, enc: "valley-Red" }];
return h.meadow ? ["encounter", { ...d, valley: h.valley, meadow: h.meadow, enc: "meadow-" + h.meadow }] : null;
}
/**
* An advisor for one colour and objective, built from a published policy sample (policy.json -> samples.all).
* It re-solves the model locally (a few milliseconds), so it can advise from ANY history, including histories
* that already left the recommended route.
*/
function advisor(sample, X, objective = "eventually", { only = null } = {}) {
const m = sample.model;
const params = makeParams(m.tables, { mode: "mean", alphas: m.alphas });
const sol = objective === "eventually"
? (() => { const r = solveRatio(params, X, { support: m.support, only }); return solve(params, X, { support: m.support, lam: r.lam, record: true, only }); })()
: solve(params, X, { support: m.support, record: true, only });
const opts = (name, key) => Object.fromEntries(Object.entries(sol.optionValues(name, key) || {}).map(([o, v]) => [o, { pX: v.x, pOther: v.o, pReach: v.r }]));
const dec = (name, key, extra = {}) => ({ kind: "decision", var: name, label: LABEL[name], choice: sol.decide(name, key), options: opts(name, key), ...extra });
const cha = (name, key, extra = {}) => ({ kind: "chance", var: name, label: LABEL[name], probs: params.chance(name, key), ...extra });
/**
* Everything the planner needs at history h: the next decision (every option with P(target), P(other colour)
* and P(reaching an encounter alive), each followed by the best play afterwards), or the next chance event with
* its probabilities, or the end of the run with the encounter's outcome distribution. `risk` is the chance of
* being killed at the step just survived. h may also hold `encounter` ("Attempt the bond" / "Die on purpose").
*/
function step(h) {
if (!h.company) return dec("company", {});
const left = leftOf(h);
if (!left) return dec("leave", { company: h.company });
if (!h.direction) return dec("direction", { company: h.company, left });
const a0 = { company: h.company, left, direction: h.direction };
if (!h.fs) return cha("fs", a0);
const sc = screamOf(h), cad = cadetOf(h);
if (!h.react && !sc) return dec("react", { ...a0, fs: h.fs });
const riskSight = params.chance("dieSight", { ...a0, fs: h.fs, react: h.react || undefined }).die;
const a = { company: h.company, direction: h.direction, fs: h.fs };
if (!sc) return dec("scream", a, { risk: { label: "Killed at the first sighting", p: riskSight } });
const h1 = { ...a, scream: sc };
let c;
if (sc === "Keep walking") {
if (!cad) return dec("wounded", h1);
c = { ...h1, cadet: cad, post: "(no fork)" };
} else {
const cl = clearingOf(h);
if (!cl) return dec("clearing", h1);
const h2 = { ...h1, clearing: cl };
if (cl === "Walk away") c = { ...h2, cadet: "Left them to it", post: "(no fork)" };
else {
const f = h.fight || (cad ? "Pause to assess" : null);
if (!f) return dec("fight", h2);
const dg = daggerAfter(f, h.dagger || (cad ? "Engage the smaller cadet" : null));
if (!dg) return dec("dagger", { ...h2, fight: f });
const hf = { ...h2, fight: f, dagger: dg };
if (!cad) return dec("fate", hf, { risk: { label: "Killed in the fight", p: params.chance("dieCombat", hf).die } });
// nothing after the fight depends on its moves, so the rest of the history leaves them out (shared subtree)
const h3 = { ...h2, cadet: cad };
if (!h.post) return dec("post", h3);
c = { ...h3, post: h.post };
}
}
const riskHour = params.chance("dieHour", c).die;
if (!h.hour) return dec("hour", c, { risk: { label: "Killed before the hour", p: riskHour } });
const d = { ...c, hour: h.hour };
let enc = null, key = null;
if (h.hour === "Tree") {
if (!h.shadow) return cha("shadow", d);
if (h.shadow === "yes") {
if (!h.shadowColour) return cha("shadowColour", d);
enc = "shadow-" + h.shadowColour; key = { ...d, shadow: "yes", enc };
} else {
if (!h.descend) return dec("descend", { ...d, shadow: "no" });
if (h.descend.startsWith("Quickly")) {
if (!h.red) return dec("red", { ...d, shadow: "no", descend: h.descend });
enc = h.red.startsWith("Jump") ? "tree-Red" : "tree-Greens"; key = { ...d, shadow: "no", descend: h.descend, red: h.red, enc };
} else { enc = "tree-Greens"; key = { ...d, shadow: "no", descend: h.descend, enc }; }
}
} else {
if (!h.valley) return dec("valley", d);
if (h.valley === "Face the Red") { enc = "valley-Red"; key = { ...d, valley: h.valley, enc }; }
else {
if (!h.meadow) return cha("meadow", d);
enc = "meadow-" + h.meadow; key = { ...d, valley: h.valley, meadow: h.meadow, enc };
}
}
if (!h.encounter) return dec("encounter", key, { encounter: enc });
return { kind: "end", encounter: enc, action: h.encounter, outcomes: h.encounter === "Die on purpose" ? { burn: 1 } : params.encounter(enc) };
}
return {
colour: X, objective, only, value: sol.value, step,
/** attempts recorded at an encounter (genuine attempts only), to show how much data a bond chance rests on */
attempts: (ctx) => Object.values(((m.tables.encounter || {})[ctx] || { outcomes: {} }).outcomes).reduce((a, b) => a + b, 0),
next(h) {
const nd = nextDecision(h);
if (!nd) return null;
const [name, key] = nd;
const ov = sol.optionValues(name, key) || {};
return { decision: name, label: LABEL[name], choice: sol.decide(name, key),
options: Object.fromEntries(Object.entries(ov).map(([o, v]) => [o, { pX: v.x, pOther: v.o, pReach: v.r }])) };
},
};
}
/**
* Given an advisor, the rider's history and the options on screen, return
* { index, decision, choice, reason } where index is null when there is no advice for this screen
* (the bot should then use its own rules). The encounter decision ("Attempt the bond" / "Die on purpose")
* is returned without an index: the bot knows which of its options attempt the bond and which burn.
*/
function recommendOption(adv, h, offered) {
const rec = adv.next(h);
if (!rec) return null;
const texts = offered.map(norm);
const find = (re) => texts.findIndex((t) => re.test(t));
let idx = -1;
if (rec.decision === "fate" || rec.decision === "wounded") {
idx = find(OPTION_PATTERNS.resolution[rec.choice] || /$^/);
} else if (OPTION_PATTERNS[rec.decision]) {
idx = find(OPTION_PATTERNS[rec.decision][rec.choice] || /$^/);
}
const values = Object.values(rec.options).map((v) => v.pX).sort((a, b) => b - a);
const margin = values.length > 1 ? values[0] - values[1] : null;
return { index: idx < 0 ? null : idx, decision: rec.decision, choice: rec.choice, margin, options: rec.options,
reason: idx < 0 && rec.decision !== "encounter" ? "the recommended choice is not on this screen; use the bot's own rules" : `${rec.label}: ${rec.choice}` };
}
/** Where each colour can be bonded (encounter contexts). The Greens can also end in an Orange or Brown rescue. */
const ENCOUNTERS = {
Red: ["valley-Red", "tree-Red"], Orange: ["meadow-Orange", "tree-Greens"], Brown: ["meadow-Brown", "tree-Greens"],
Green: ["tree-Greens"], Blue: ["meadow-Blue", "shadow-Blue"], Black: ["shadow-Black"],
};
return { ENCOUNTERS, COLOURS, DECISIONS, CHANCE, prepareRun, countTables, makeParams, fitAlpha, solverHistory, nextDecision, advisor, solve, solveRatio, evaluate, buildPolicy, locate, recommendOption, genuineAttempt };
});