Key the tablebase cache by position instead of full FEN (#28)

Add FEN.position_key/1 (board, side, castling and en-passant; clocks dropped) and have Tablebase.new/1 key cache reads and writes on it, so transpositions and clock-only differences share an entry. The Lichess category is a position property, so dropping the clocks is safe.

Update the cache docs and tests, and give the play fixture's after_first/after_losing distinct positions since they previously differed only by the halfmove clock.
This commit is contained in:
2026-11-03 13:09:45 +01:00
parent 515196857f
commit 3fadd4397c
6 changed files with 96 additions and 14 deletions
+17 -3
View File
@@ -4,6 +4,7 @@ defmodule Chesstrainer.Endgames.TablebaseTest do
alias Chesstrainer.Endgames.Tablebase
alias Chesstrainer.Endgames.Tablebase.Cache, as: TablebaseCache
alias Chesstrainer.Endgames.Tablebase.Parser
alias Chesstrainer.FEN
alias Chesstrainer.Ratelimiters
describe "normalize_category/1" do
@@ -144,16 +145,29 @@ defmodule Chesstrainer.Endgames.TablebaseTest do
test "returns {:ok, tablebase} on a cache hit" do
fen = "8/8/3k4/8/8/3K1R2/8/8 w - - 0 1"
tablebase = %Tablebase{category: :win, moves: []}
key = FEN.position_key(fen)
TablebaseCache.put(fen, tablebase)
on_exit(fn -> TablebaseCache.delete(fen) end)
TablebaseCache.put(key, tablebase)
on_exit(fn -> TablebaseCache.delete(key) end)
assert {:ok, ^tablebase} = Tablebase.new(fen)
end
test "shares a cache entry for FENs that differ only in the clocks" do
fen = "8/8/3k4/8/8/3K1R2/8/8 w - - 0 1"
other_clock = "8/8/3k4/8/8/3K1R2/8/8 w - - 7 42"
tablebase = %Tablebase{category: :win, moves: []}
key = FEN.position_key(fen)
TablebaseCache.put(key, tablebase)
on_exit(fn -> TablebaseCache.delete(key) end)
assert {:ok, ^tablebase} = Tablebase.new(other_clock)
end
test "returns {:error, :not_covered} for a position with more than seven pieces" do
fen = "3k4/2n2B2/1KP5/2B2p2/5b1p/7P/8/8 b - - 0 1"
TablebaseCache.delete(fen)
TablebaseCache.delete(FEN.position_key(fen))
assert Tablebase.new(fen) == {:error, :not_covered}
end
+32
View File
@@ -60,6 +60,38 @@ defmodule Chesstrainer.FENTest do
end
end
describe "position_key/1" do
test "drops the halfmove and fullmove clocks" do
assert FEN.position_key("8/8/3k4/8/8/3K1R2/8/8 w - - 0 1") ==
"8/8/3k4/8/8/3K1R2/8/8 w - -"
end
test "shares a key for FENs that differ only in the clocks" do
a = "8/8/3k4/8/8/3K1R2/8/8 w - - 0 1"
b = "8/8/3k4/8/8/3K1R2/8/8 w - - 37 99"
assert FEN.position_key(a) == FEN.position_key(b)
end
test "keeps board, side to move, castling and en-passant distinct" do
base = "8/8/3k4/8/8/3K1R2/8/8 w - - 0 1"
refute FEN.position_key(base) == FEN.position_key("8/8/3k4/8/8/3K1R2/8/8 b - - 0 1")
refute FEN.position_key(base) == FEN.position_key("8/8/3k4/8/8/3K1R3/8/8 w - - 0 1")
refute FEN.position_key(base) == FEN.position_key("8/8/3k4/8/8/3K1R2/8/8 w K - 0 1")
refute FEN.position_key(base) == FEN.position_key("8/8/3k4/8/8/3K1R2/8/8 w - e3 0 1")
end
test "trims surrounding whitespace" do
assert FEN.position_key(" 8/8/3k4/8/8/3K1R2/8/8 w - - 0 1\n") ==
"8/8/3k4/8/8/3K1R2/8/8 w - -"
end
test "passes through a non-FEN string" do
assert FEN.position_key("not a fen") == "not a fen"
end
end
describe "piece_count/1" do
test "counts kings and pieces, ignoring empty squares and slashes" do
assert FEN.piece_count("8/8/3k4/8/8/3K1R2/8/8 w - - 0 1") == 3