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:
co-authored by
Claude Opus 5.5
parent
dcd5117373
commit
432e04c2c7
@@ -0,0 +1,131 @@
|
||||
// node --test tests/core/solver.test.js
|
||||
const test = require("node:test");
|
||||
const assert = require("node:assert");
|
||||
const S = require("../../core/solver");
|
||||
|
||||
// A synthetic world: the meadow shows Blue 30% of the time, the shadow appears 2% of the time,
|
||||
// facing the valley Red always bonds it, the Greens burn half the time and bond Brown (rescue) a quarter of the time.
|
||||
function world(n = 4000, seed = 7) {
|
||||
let s = seed;
|
||||
const r = () => ((s = (s * 1103515245 + 12345) % 2147483648) / 2147483648);
|
||||
const runs = [];
|
||||
for (let i = 0; i < n; i++) {
|
||||
const run = { company: "Alone", direction: ["East", "West", "Center"][i % 3], first_sighting: ["Red", "Brown"][i % 2],
|
||||
scream: "Investigate & help", resolution: ["Killed the bullies yourself", "Squadmate's revenge"][i % 2], post_path: "Forest",
|
||||
goal: "Any (for fun)", malek_intent: "No", enc_Red: null, enc_Green: null, enc_Orange: null, enc_Brown: null, enc_Blue: null, enc_Black: null };
|
||||
run.hour = r() < 0.5 ? "Tree" : "Valley";
|
||||
if (run.hour === "Tree") {
|
||||
if (r() < 0.02) { run.branch = "Shadow (rare)"; run.enc_Black = "Bonded!"; run.outcome = "Bonded"; run.outcome_color = "Black"; }
|
||||
else if (r() < 0.5) { run.branch = "Descend quickly: Red"; run.enc_Red = "Incinerated"; run.outcome = "Incinerated"; run.outcome_color = "Red"; }
|
||||
else {
|
||||
run.branch = "Descend slowly: two Greens";
|
||||
const u = r();
|
||||
if (u < 0.5) { run.enc_Green = "Incinerated"; run.outcome = "Incinerated"; run.outcome_color = "Green"; }
|
||||
else if (u < 0.75) { run.enc_Green = "Bonded!"; run.outcome = "Bonded"; run.outcome_color = "Green"; }
|
||||
else { run.enc_Green = "Saved by another"; run.enc_Brown = "Bonded!"; run.outcome = "Bonded"; run.outcome_color = "Brown"; }
|
||||
}
|
||||
} else if (r() < 0.2) { run.branch = "Valley Red"; run.enc_Red = "Bonded!"; run.outcome = "Bonded"; run.outcome_color = "Red"; }
|
||||
else {
|
||||
run.branch = "Passed the Red: meadow";
|
||||
const u = r(), c = u < 0.3 ? "Blue" : u < 0.65 ? "Orange" : "Brown";
|
||||
const bond = r() < 0.5;
|
||||
run["enc_" + c] = bond ? "Bonded!" : "Incinerated"; run.outcome = bond ? "Bonded" : "Incinerated"; run.outcome_color = c;
|
||||
}
|
||||
runs.push(run);
|
||||
}
|
||||
return runs;
|
||||
}
|
||||
|
||||
const policy = S.buildPolicy(world(), { draws: 10, colours: ["Blue", "Black", "Red", "Brown"] });
|
||||
const choice = (X, h, obj = "thisAttempt") => S.advisor(policy, X, obj).next(h).choice;
|
||||
|
||||
test("Blue: go to the valley, pass the Red, attempt only the Blue", () => {
|
||||
const t = policy.targets.Blue.thisAttempt;
|
||||
const h = { company: "Alone", direction: "Center", fs: "Red", cadet: "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(S.locate(t.tree, {}).var, "company");
|
||||
// 30% Blue in the meadow x 50% bond
|
||||
assert.ok(Math.abs(t.pX.point - 0.15) < 0.03, t.pX.point);
|
||||
});
|
||||
|
||||
test("Black: climb the tree; the value is about the shadow rate x bond rate", () => {
|
||||
const t = policy.targets.Black.thisAttempt;
|
||||
const h = { company: "Alone", direction: "East", fs: "Red", cadet: "Squadmate's revenge", post: "Forest" };
|
||||
assert.strictEqual(choice("Black", h), "Tree");
|
||||
assert.ok(t.pX.point > 0.01 && t.pX.point < 0.035, t.pX.point);
|
||||
});
|
||||
|
||||
test("Red: face the valley Red", () => {
|
||||
const h = { company: "Alone", direction: "West", fs: "Brown", cadet: "Killed the bullies yourself", post: "Forest", hour: "Valley" };
|
||||
assert.strictEqual(choice("Red", h), "Face the Red");
|
||||
});
|
||||
|
||||
test("Brown 'eventually' never risks a non-Brown bond that 'this attempt' accepts", () => {
|
||||
const a = policy.targets.Brown.thisAttempt, e = policy.targets.Brown.eventually;
|
||||
assert.ok(e.pOther.point <= a.pOther.point + 1e-12);
|
||||
assert.ok(e.ratio.mean >= a.ratio.mean - 1e-9);
|
||||
});
|
||||
|
||||
test("no history dependence in the data -> large fitted concentration", () => {
|
||||
assert.ok(policy.model.alphas.meadow >= 500, JSON.stringify(policy.model.alphas));
|
||||
});
|
||||
|
||||
test("recommendOption maps the policy onto the bot's screen text", () => {
|
||||
const adv = S.advisor(policy, "Blue", "thisAttempt");
|
||||
const rec = S.recommendOption(adv, { company: "Alone", direction: "Center", fs: "Red", cadet: "Killed the bullies yourself", post: "Forest" },
|
||||
["Continue toward the center of the valley", "Find the tallest tree and climb for a better view"]);
|
||||
assert.strictEqual(rec.index, 0);
|
||||
const none = S.recommendOption(adv, {}, ["Something we have never seen"]);
|
||||
assert.strictEqual(none.index, null);
|
||||
});
|
||||
|
||||
test("advisor matches the solver's own decisions on the recommended route", () => {
|
||||
const adv = S.advisor(policy, "Blue", "eventually");
|
||||
assert.strictEqual(adv.next({}).choice, policy.targets.Blue.eventually.tree.choice);
|
||||
});
|
||||
|
||||
test("routing to one encounter: Blue only at the meadow equals the Blue policy; only at the tree Greens gives 0", () => {
|
||||
const meadow = S.advisor(policy, "Blue", "thisAttempt", { only: ["meadow-Blue"] });
|
||||
const all = S.advisor(policy, "Blue", "thisAttempt");
|
||||
assert.ok(Math.abs(meadow.value.x - all.value.x) < 1e-6);
|
||||
const greens = S.advisor(policy, "Blue", "thisAttempt", { only: ["tree-Greens"] });
|
||||
assert.ok(greens.value.x < 1e-9);
|
||||
// Brown via the Greens rescue is possible in this world
|
||||
const rescue = S.advisor(policy, "Brown", "thisAttempt", { only: ["tree-Greens"] });
|
||||
assert.ok(rescue.value.x > 0.05, rescue.value.x);
|
||||
assert.ok(meadow.attempts("meadow-Blue") > 100);
|
||||
});
|
||||
|
||||
test("the cadet encounter is asked in the game's order: scream, clearing or wounded cadet, the fight's moves, then the fate", () => {
|
||||
const adv = S.advisor(policy, "Blue", "thisAttempt");
|
||||
// the first dragon: you react to it before the scream
|
||||
assert.strictEqual(adv.step({ company: "Alone", direction: "Center", fs: "Red" }).var, "react");
|
||||
// entering with squadmates asks whether you stay with them
|
||||
assert.strictEqual(adv.step({ company: "1 Squadmate" }).var, "leave");
|
||||
assert.strictEqual(adv.step({ company: "1 Squadmate", left: "Stay together" }).var, "direction");
|
||||
const base = { company: "Alone", direction: "Center", fs: "Red", react: "Hold still" };
|
||||
assert.strictEqual(adv.step(base).var, "scream");
|
||||
// the synthetic world has no one who kept walking, so that option is (correctly) not offered
|
||||
assert.deepStrictEqual(Object.keys(adv.step(base).options), ["Investigate the scream"]);
|
||||
assert.strictEqual(adv.step({ ...base, scream: "Investigate the scream" }).var, "clearing");
|
||||
const help = { ...base, scream: "Investigate the scream", clearing: "Help your squadmate" };
|
||||
assert.strictEqual(adv.step(help).var, "fight");
|
||||
assert.strictEqual(adv.step({ ...help, fight: "Pause to assess" }).var, "dagger");
|
||||
// charging straight in skips the dagger choice
|
||||
assert.strictEqual(adv.step({ ...help, fight: "Charge the smaller cadet" }).var, "fate");
|
||||
const fate = adv.step({ ...help, fight: "Pause to assess", dagger: "Engage the smaller cadet" });
|
||||
assert.strictEqual(fate.var, "fate");
|
||||
assert.ok(fate.risk && fate.risk.label === "Killed in the fight");
|
||||
assert.strictEqual(adv.step({ ...base, scream: "Investigate the scream", clearing: "Walk away" }).var, "hour");
|
||||
// a bot history that only knows the final resolution still lands in the right place
|
||||
assert.strictEqual(adv.next({ ...base, cadet: "Killed the bullies yourself" }).decision, "post");
|
||||
});
|
||||
|
||||
test("solverHistory converts form variables", () => {
|
||||
const h = S.solverHistory({ company: "Alone", direction: "Center", first_sighting: "Red", resolution: "Tourniquet", hour: "Tree", branch: "Descend slowly: two Greens" });
|
||||
assert.deepStrictEqual(h, { company: "Alone", direction: "Center", fs: "Red", cadet: "Tourniquet", wounded: "Tourniquet", post: "(no fork)", hour: "Tree", descend: "Slowly (two Greens)", shadow: "no" });
|
||||
});
|
||||
Reference in New Issue
Block a user