Merge pull request 'Known answers for the bot: the guide's encounter answers and the bot's own results, by choice text' (#39) from atlas/known-answers into main
Send the route model to the bot / send (push) Successful in 7s
CI / test (push) Successful in 1m58s

This commit was merged in pull request #39.
This commit is contained in:
2026-11-19 14:00:17 +00:00
7 changed files with 2542 additions and 7 deletions
+4 -3
View File
@@ -1,6 +1,6 @@
# When the route model changes on main, send it to the bot: open (or update) a pull request on the bot's repo,
# DragonKind/DragonKind, that replaces vendor/threshing-atlas/{policy.json,solver.js,graph.js} with this repo's
# data/public/policy.json, core/solver.js and core/graph.js. The three always travel together, so the policy and the
# DragonKind/DragonKind, that replaces vendor/threshing-atlas/{policy.json,solver.js,graph.js,known_answers.json} with
# this repo's data/public/policy.json, core/solver.js, core/graph.js and data/public/known_answers.json. They always travel together, so the policy and the
# solver that reads it never get out of step. Ken reviews and merges it; the running bot reloads the files after a pull.
#
# Secrets: ATLAS_BOT_TOKEN (repository: read and write, with write access to DragonKind/DragonKind).
@@ -12,6 +12,7 @@ on:
- data/public/policy.json
- core/solver.js
- core/graph.js
- data/public/known_answers.json
workflow_dispatch:
jobs:
send:
@@ -34,7 +35,7 @@ jobs:
GIT_CONFIG_VALUE_0="AUTHORIZATION: token $ATLAS_BOT_TOKEN"
git clone --quiet --depth 1 https://git.kenschiano.net/DragonKind/DragonKind.git "$dst"
mkdir -p "$dst/vendor/threshing-atlas"
cp data/public/policy.json core/solver.js core/graph.js "$dst/vendor/threshing-atlas/"
cp data/public/policy.json core/solver.js core/graph.js data/public/known_answers.json "$dst/vendor/threshing-atlas/"
id=$(python3 -c "import json; print(json.load(open('data/public/policy.json')).get('id', ''))")
sha=$(git rev-parse --short HEAD)
cd "$dst"
+131
View File
@@ -0,0 +1,131 @@
"""Known answers: what the Atlas knows about every answer the bot can be offered, for screens its other sources don't
cover (issue #35). The bot looks the screen's choices up here first (solver.js `knownAnswer`), and passes what it finds
to Clef as evidence when nothing is certain.
python analysis/known/build_known.py [--check] -> data/public/known_answers.json
Two sources, keyed by the answer text normalised exactly as the bot does (collection/etl/transform/bot.py normalize):
guide every answer in the community route PDF's encounter trees (data/public/pdf_encounters.json): the
encounter, how it ends there (or "continues"), the guide's rate, whether the guide marks it unconfirmed,
and `on_path`: the colours whose recommended path in the guide goes through it
bot every answer in the bot's screen tree (data/public/tree.json): times played, times it died straight
after, and the bonds the runs that took it ended in
"""
import argparse
import json
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parents[2]
sys.path.insert(0, str(ROOT))
from collection.etl.transform.bot import normalize # noqa: E402
GUIDE = ROOT / "data/public/pdf_encounters.json"
TREE = ROOT / "data/public/tree.json"
OUT = ROOT / "data/public/known_answers.json"
# the colours each of the guide's encounters can end in a bond with (the two Greens can also end with an Orange or a
# Brown swooping in), and the encounter a colour's route continues to from there (the shadow leads on to the Black)
ENC_COLOURS = {"valley-Red": ["Red"], "tree-Red": ["Red"], "tree-Greens": ["Green", "Brown", "Orange"],
"meadow-Orange": ["Orange"], "meadow-Brown": ["Brown"], "meadow-Blue": ["Blue"],
"shadow": ["Black", "Blue"], "black": ["Black"]}
ONWARD = {("shadow", "Black"): "Black"}
def is_goal(end, colour, onward):
end = end or ""
if onward:
return end.startswith(f"On to the {onward}")
if not (end.startswith("Bonded") or "Bonded (" in end):
return False
return "(" not in end or colour in end
def leads_to(o, colour, onward):
if "end" in o:
return is_goal(o["end"], colour, onward)
return any(not c.get("roll") and leads_to(c, colour, onward) for c in o.get("then", []))
def guide_path(options, colour, onward):
"""The guide's recommended answers toward `colour` (the same walk as the page's encounter panel)."""
out, opts = [], options
while opts:
live = [o for o in opts if not o.get("roll")]
pick = next((o for o in live if o.get("best") and leads_to(o, colour, onward)), None) \
or next((o for o in live if leads_to(o, colour, onward)), None)
if not pick:
break
out.append(id(pick))
if "end" in pick:
break
opts = pick.get("then", [])
return out
def walk(options, enc, on_path, out):
"""Every answer of an encounter, with its colours (where it happens) and on_path (whose recommended path)."""
for o in options:
if o.get("roll"):
continue
entry = {"encounter": enc, "colours": ENC_COLOURS.get(enc, []), "end": o.get("end", "continues")}
if o.get("pct"):
entry["pct"] = o["pct"]
if o.get("best"):
entry["best"] = True
if o.get("unconfirmed"):
entry["unconfirmed"] = True
cols = sorted(c for c, ids in on_path.items() if id(o) in ids)
if cols:
entry["on_path"] = cols
for text in [s.strip() for s in o["say"].split(", or ")]: # "Jump on its back, or jump to land…"
out.setdefault(normalize(text), {"text": text, "guide": []})["guide"].append(entry)
walk(o.get("then", []), enc, on_path, out)
def build():
g = json.loads(GUIDE.read_text(encoding="utf-8"))
out = {}
for enc, E in g["encounters"].items():
on_path = {c: set(guide_path(E["options"], c, ONWARD.get((enc, c)))) for c in ENC_COLOURS.get(enc, [])}
walk(E["options"], enc, on_path, out)
t = json.loads(TREE.read_text(encoding="utf-8")) if TREE.exists() else {"screens": {}}
for sc in t["screens"].values():
for o in sc["options"].values():
if not o.get("n"):
continue
k = normalize(o["text"])
e = out.setdefault(k, {"text": o["text"], "guide": []})
b = e.setdefault("bot", {"n": 0, "died": 0, "bonded": {}})
b["n"] += o["n"]
for dest, d in o.get("to", {}).items():
if dest == "DIED":
b["died"] += d["n"]
elif dest.startswith("BONDED:"):
c = dest.split(":", 1)[1]
b["bonded"][c] = b["bonded"].get(c, 0) + d["n"]
for e in out.values():
if not e["guide"]:
del e["guide"]
src = g["source"]
return {"schema": 1, "guide": {"title": src["title"], "version": src["version"], "link": src["link"]},
"bot": {"runs": (t.get("builtFrom") or {}).get("runs")}, "answers": dict(sorted(out.items()))}
def main(argv=None):
ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
ap.add_argument("--check", action="store_true")
a = ap.parse_args(argv)
text = json.dumps(build(), ensure_ascii=False, indent=1) + "\n"
if a.check:
if not OUT.exists() or OUT.read_text(encoding="utf-8") != text:
print(f"{OUT.relative_to(ROOT)} is out of date (run python analysis/known/build_known.py)", file=sys.stderr)
return 1
return 0
OUT.write_text(text, encoding="utf-8")
print(f"wrote {OUT.relative_to(ROOT)}")
return 0
if __name__ == "__main__":
sys.exit(main())
+1
View File
@@ -87,6 +87,7 @@ def rebuild():
_step("experiment plan", [py, "planner/build_experiments.py"])
_step("what the runs show about the Black", [py, "analysis/black/evidence.py"])
_step("screen tree", [py, "analysis/tree/build_tree.py"])
_step("known answers for the bot (guide + screen tree)", [py, "analysis/known/build_known.py"])
try:
import scipy # noqa: F401
_step("statistics", [py, "analysis/stats/site_stats.py"])
+64 -1
View File
@@ -15,6 +15,9 @@
* shadow odds), `fix` (hold some decisions to given options: an experiment arm) and sampling "uniform" (mapping);
* assign(plan, { accountSeq, attempt }) reads the experiment plan (experiments.json) and says what an attempt is for;
* screenPick(tree, sceneId, offered, { mode }) picks from the screen tree (tree.json): least-tried, or Thompson.
* knownAnswer(known, offered, { target, sceneColour }) looks a screen's choices up in known_answers.json (the guide's
* encounter answers and the bot's own results) for screens nothing else covers: a pick when the evidence is clear,
* and the evidence for every choice either way, to hand to Clef (issue #35).
* UMD: window.ThreshSolver in the browser, module.exports in Node. No dependencies.
*/
(function (root, factory) {
@@ -446,8 +449,68 @@
reason: mode === "thompson" ? `survived ${pick.survived} of ${pick.n} so far` : `played ${pick.n} times, the fewest on this screen` };
}
/* ---------- known answers (analysis/known/build_known.py -> data/public/known_answers.json) ---------- */
const STOP = new Set("a an the to and or of your you its it is for at in on with this that be".split(" "));
const words = (t) => norm(t).split(" ").filter((w) => w && !STOP.has(w));
/** The guide labels a game text carries: the guide paraphrases ("Touch its talon"), the game says it in full ("Reach a
* hand slowly to touch its talon"), so a label matches when every word of it that matters is in the text. Labels of
* fewer than two such words ("Is your dragon") only match a text that is exactly the label. */
function guideMatches(known, text) {
const n = norm(text), have = new Set(words(text)), out = [];
for (const [k, e] of Object.entries(known.answers || {})) {
if (!e.guide) continue;
const w = words(e.text);
const exact = k === n;
if (exact || (w.length >= 2 && w.every((x) => have.has(x)))) e.guide.forEach((g) => out.push({ ...g, label: e.text, exact }));
}
return out;
}
/**
* A screen nothing else covers: what the Atlas knows about each choice, and a pick when it is clear.
* known data/public/known_answers.json
* offered the choice texts on screen, in order
* target the colour the account is after ("Blue"); sceneColour: the colour of this scene's dragon, if the
* bot knows it (only the guide's answers for that dragon's encounters are used then)
* Returns { pick, confidence, reason, choices }: pick is the index to take (null if nothing is clear), confidence
* "strong" (exactly one choice is on the guide's recommended path toward the target, and the bot's own runs don't
* contradict it) or "weak" (several fit, or only the bot's own record speaks for it). choices[i].verdict is "good",
* "bad" (the guide ends it in incineration, or the bot died on it at least 3 times in 4) or "unknown", with the
* evidence behind it. Anything weak or unknown goes to Clef with this evidence; a pick never skips the bot's guard.
*/
function knownAnswer(known, offered, { target, sceneColour = null } = {}) {
const T = target ? target[0].toUpperCase() + target.slice(1).toLowerCase() : null;
const choices = offered.map((text, index) => {
const e = (known.answers || {})[norm(text)] || {};
const bot = e.bot || null;
const guide = guideMatches(known, text).filter((g) => !sceneColour || (g.colours || []).includes(sceneColour));
const onPath = guide.filter((g) => T && (g.on_path || []).includes(T));
const botBad = bot && bot.n >= 2 && bot.died / bot.n >= 0.75;
const botGood = bot && bot.n >= 3 && bot.died === 0;
const guideBad = guide.length > 0 && guide.every((g) => /^Incinerated/.test(g.end));
const verdict = onPath.length && !botBad ? "good" : guideBad || botBad ? "bad" : "unknown";
return { index, text, verdict, guide, bot, onPath: onPath.length > 0, botGood: !!botGood };
});
const good = choices.filter((c) => c.verdict === "good");
let pick = null, confidence = null, reason = "nothing the Atlas knows covers these choices";
if (good.length === 1) {
pick = good[0].index; confidence = "strong";
reason = `on the guide's recommended path to the ${T} ("${good[0].onPath && good[0].guide.find((g) => (g.on_path || []).includes(T)).label}")`;
} else if (good.length > 1) {
const best = good.find((c) => c.botGood) || good[0];
pick = best.index; confidence = "weak"; reason = `${good.length} choices are on the guide's paths; the bot's record favours none clearly`;
} else {
const safe = choices.filter((c) => c.botGood && c.verdict !== "bad");
if (safe.length) {
const b = safe.reduce((x, y) => (y.bot.n > x.bot.n ? y : x));
pick = b.index; confidence = "weak"; reason = `the bot got through it ${b.bot.n} of ${b.bot.n} times`;
}
}
return { pick, confidence, reason, choices };
}
return Object.assign(api, {
advisor, recommendOption, solverHistory, norm, assign, armFor, givenFor, screenPick,
advisor, recommendOption, solverHistory, norm, assign, armFor, givenFor, screenPick, knownAnswer,
makeParams: (T, { alphas, game } = {}) => makeParams(T, { alphas, game: game || api.game }),
/** remember the game of a policy sample (sets ENCOUNTERS) without solving anything */
useGame(sample) { api.game = sample.model.game; api.ENCOUNTERS = api.game.by_colour; return api.game; },
File diff suppressed because it is too large Load Diff
+3 -3
View File
@@ -6,11 +6,11 @@
"scripts": {
"build": "node site/build.js",
"dev": "node site/build.js && python3 -m http.server 8000 -d dist",
"test:py": "python3 -m collection build --check && python3 planner/build_experiments.py --check && python3 analysis/black/evidence.py --check && python3 analysis/tree/build_tree.py --check && PYTHONPATH=core python3 -m unittest discover -s tests/core -p 'test_*.py' && python3 -m unittest discover -s tests/collection -p 'test_*.py'",
"test:unit": "node --test tests/core/graph.test.js tests/core/advisor.test.js",
"test:py": "python3 -m collection build --check && python3 planner/build_experiments.py --check && python3 analysis/black/evidence.py --check && python3 analysis/tree/build_tree.py --check && python3 analysis/known/build_known.py --check && PYTHONPATH=core python3 -m unittest discover -s tests/core -p 'test_*.py' && python3 -m unittest discover -s tests/collection -p 'test_*.py'",
"test:unit": "node --test tests/core/graph.test.js tests/core/advisor.test.js tests/core/known.test.js",
"test": "npm run test:py && npm run test:unit",
"graph": "node analysis/network/build-graph.js",
"policy": "python3 planner/build_policy.py && python3 planner/policy_report.py && python3 planner/build_experiments.py && python3 analysis/black/evidence.py && python3 analysis/tree/build_tree.py",
"policy": "python3 planner/build_policy.py && python3 planner/policy_report.py && python3 planner/build_experiments.py && python3 analysis/black/evidence.py && python3 analysis/tree/build_tree.py && python3 analysis/known/build_known.py",
"cycle": "python3 -m collection cycle",
"stats": "python3 analysis/stats/site_stats.py",
"collect": "python3 -m collection update",
+32
View File
@@ -0,0 +1,32 @@
// knownAnswer (core/solver.js) against the published data/public/known_answers.json: the guide's recommended answers
// are found on the game's full texts, wrong colours and deadly answers are not picked, and unknown screens stay open.
const test = require("node:test");
const assert = require("node:assert/strict");
const path = require("node:path");
const S = require("../../core/solver.js");
const known = require(path.join(__dirname, "../../data/public/known_answers.json"));
test("the Black: 'Hold its gaze' is the strong pick, 'Lower your eyes' is bad", () => {
const r = S.knownAnswer(known, ["Lower your eyes", "Hold its gaze"], { target: "black", sceneColour: "Black" });
assert.equal(r.pick, 1);
assert.equal(r.confidence, "strong");
assert.equal(r.choices[0].verdict, "bad");
});
test("a guide paraphrase matches the game's full text", () => {
const r = S.knownAnswer(known, ["Climb onto its foreleg", "Reach a hand slowly to touch its talon"], { target: "Red", sceneColour: "Red" });
assert.equal(r.pick, 1);
assert.match(r.reason, /Touch its talon/);
});
test("the two Greens: the answer for the right Green is not the pick for a Brown target", () => {
const offered = ["You respect their nature", "You don't know", "Intellect match"];
assert.equal(S.knownAnswer(known, offered, { target: "Green" }).pick, 0);
assert.equal(S.knownAnswer(known, offered, { target: "Brown" }).pick, 2);
});
test("a screen nobody has seen: no pick, every choice unknown", () => {
const r = S.knownAnswer(known, ["Whistle a tune", "Count the clouds"], { target: "Blue" });
assert.equal(r.pick, null);
assert.deepEqual(r.choices.map((c) => c.verdict), ["unknown", "unknown"]);
});