Files
threshing-atlas/analysis/network/build-graph.js
T
11b4a0398e 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>
2026-11-03 22:01:32 +00:00

53 lines
2.7 KiB
JavaScript

// Build data/public/graph.json from every source: community form runs + DragonKind bot trajectories.
// node analysis/network/build-graph.js
const fs = require("fs");
const path = require("path");
const G = require("../../core/graph.js");
const root = path.resolve(__dirname, "../..");
const rel = (p) => path.join(root, p);
/** 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");
const scenes = fs.existsSync(path.join(botDir, "scenes.json")) ? JSON.parse(fs.readFileSync(path.join(botDir, "scenes.json"), "utf8")) : {};
const trajs = [];
for (const f of fs.readdirSync(botDir).filter((f) => /^trajectories-.*\.jsonl$/.test(f)).sort()) {
for (const line of fs.readFileSync(path.join(botDir, f), "utf8").split("\n")) if (line.trim()) trajs.push(JSON.parse(line));
}
const unmapped = {};
for (const t of trajs) {
const { vars, unmapped: u } = G.trajectoryVariables(t);
records.push({ source: "bot", vars });
for (const x of u) unmapped[x] = (unmapped[x] || 0) + 1;
}
const graph = {
sources: records.reduce((m, r) => ((m[r.source] = (m[r.source] || 0) + 1), m), {}),
variables: G.buildVariableGraph(records),
scenes: G.buildSceneGraph(scenes, trajs)
};
fs.writeFileSync(rel("data/public/graph.json"), JSON.stringify(graph));
console.log(`graph.json: ${JSON.stringify(graph.sources)}, ${Object.keys(graph.variables.nodes).length} variable nodes, ${Object.keys(graph.scenes).length} scenes`);
const um = Object.keys(unmapped).filter((t) => !/^(head deeper|investigate the scream|lower your|show your bravery|hide behind|run and hope|reds are too|jump|daggers or not|engage the smaller)/i.test(t));
if (um.length) console.log("bot choices with no form equivalent:", um);