From 0657cec8465e9f06fc0e1befcbfc286a2b05c78c Mon Sep 17 00:00:00 2001 From: ai-chess Date: Fri, 15 May 2026 00:20:40 +0300 Subject: [PATCH] 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. --- public/app.js | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/public/app.js b/public/app.js index d8f4c09..2c86c47 100644 --- a/public/app.js +++ b/public/app.js @@ -9,10 +9,20 @@ const STATE = { settings: null, bindings: null, // {white: {agent, model, ...}, black: {...}} 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 +// 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"), { viewOnly: true, fen: "start", @@ -74,7 +84,7 @@ function renderBoard() { } // Pass shapes via set() to atomically replace last move + arrow. // 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) ? [{ 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 || [], fen: e.fen, }; - STATE.evalByFen.set(e.fen, norm); + STATE.evalByFen.set(fenKey(e.fen), norm); } 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"); if (!e) { txt.textContent = "—";