fix(ui): normalize FEN key for eval cache
python-chess always sets the en-passant target after a 2-square pawn move; chess.js omits it when no capture is legally possible. That disagreement skipped evals for plies after every double pawn push (1.e4, 1...c5, 3.d4, ...). Use position + side-to-move + castling as the lookup key on both producer and consumer; halfmove/fullmove counters and en-passant target are dropped from the key.
This commit is contained in:
+14
-4
@@ -9,10 +9,20 @@ const STATE = {
|
|||||||
settings: null,
|
settings: null,
|
||||||
bindings: null, // {white: {agent, model, ...}, black: {...}}
|
bindings: null, // {white: {agent, model, ...}, black: {...}}
|
||||||
viewPly: null, // null = live; otherwise integer ply (1-based)
|
viewPly: null, // null = live; otherwise integer ply (1-based)
|
||||||
evalByFen: new Map(), // fen → eval payload (cp, mate, best_uci, pv) — from white's POV
|
evalByFen: new Map(), // fenKey → eval payload (cp, mate, best_uci, pv) — from white's POV
|
||||||
};
|
};
|
||||||
window._STATE = STATE; // for debugging
|
window._STATE = STATE; // for debugging
|
||||||
|
|
||||||
|
// chess.js and python-chess can disagree on the en-passant field of a FEN
|
||||||
|
// (the former drops it when no pawn can legally capture; the latter always
|
||||||
|
// sets it after a 2-square pawn move). Key the eval cache by just
|
||||||
|
// position + side-to-move + castling rights so both producers agree.
|
||||||
|
function fenKey(fen) {
|
||||||
|
if (!fen) return "";
|
||||||
|
const parts = fen.split(" ");
|
||||||
|
return parts.slice(0, 3).join(" ");
|
||||||
|
}
|
||||||
|
|
||||||
const cg = Chessground($("board"), {
|
const cg = Chessground($("board"), {
|
||||||
viewOnly: true,
|
viewOnly: true,
|
||||||
fen: "start",
|
fen: "start",
|
||||||
@@ -74,7 +84,7 @@ function renderBoard() {
|
|||||||
}
|
}
|
||||||
// Pass shapes via set() to atomically replace last move + arrow.
|
// Pass shapes via set() to atomically replace last move + arrow.
|
||||||
// Arrow comes from stored eval for this FEN, if any.
|
// Arrow comes from stored eval for this FEN, if any.
|
||||||
const e = STATE.evalByFen.get(fen);
|
const e = STATE.evalByFen.get(fenKey(fen));
|
||||||
const shapes = (e && e.best_uci && e.best_uci.length >= 4)
|
const shapes = (e && e.best_uci && e.best_uci.length >= 4)
|
||||||
? [{ orig: e.best_uci.slice(0, 2), dest: e.best_uci.slice(2, 4), brush: "green" }]
|
? [{ orig: e.best_uci.slice(0, 2), dest: e.best_uci.slice(2, 4), brush: "green" }]
|
||||||
: [];
|
: [];
|
||||||
@@ -185,11 +195,11 @@ function storeEval(e) {
|
|||||||
pv: e.pv || [],
|
pv: e.pv || [],
|
||||||
fen: e.fen,
|
fen: e.fen,
|
||||||
};
|
};
|
||||||
STATE.evalByFen.set(e.fen, norm);
|
STATE.evalByFen.set(fenKey(e.fen), norm);
|
||||||
}
|
}
|
||||||
|
|
||||||
function applyEvalForFen(fen) {
|
function applyEvalForFen(fen) {
|
||||||
const e = STATE.evalByFen.get(fen);
|
const e = STATE.evalByFen.get(fenKey(fen));
|
||||||
const col = $("eval-col"), bar = $("eval-fill"), txt = $("eval-text"), pv = $("sf-pv");
|
const col = $("eval-col"), bar = $("eval-fill"), txt = $("eval-text"), pv = $("sf-pv");
|
||||||
if (!e) {
|
if (!e) {
|
||||||
txt.textContent = "—";
|
txt.textContent = "—";
|
||||||
|
|||||||
Reference in New Issue
Block a user