Derive only-move, underpromotion and 50-move motif themes (#195)

Motifs describe the kind of problem, independent of the material family. One
local tablebase probe returns the root WDL, the DTZ and every legal move with
its own WDL, so each motif is proved without a search or a remote lookup.

- only move: more than one legal move and exactly one preserves the
  50-move-aware result, for wins and draws.
- underpromotion: a preserving underpromotion exists while no preserving queen
  promotion does.
- 50-move draw: cursed/blessed WDL, or a win whose DTZ exceeds the remaining
  halfmove budget.

Motif links are recorded as derived and refreshed as a whole, scoped to the
motif themes, so material, curated and community links survive. Loss endgames
are skipped because only-move is undefined there.

Stalemate is deferred: a stored endgame cannot itself be terminal, so it can
only describe a line and needs a definition before it becomes a predicate.

Release helpers run the derivation without starting the web application, so
`bin/chesstrainer eval` works on the server.

Closes #188
This commit is contained in:
2026-11-17 22:37:37 +01:00
committed by GitHub
parent e6f08e0ca6
commit 2759b34296
6 changed files with 493 additions and 0 deletions
@@ -6,6 +6,11 @@ defmodule Chesstrainer.Endgames.Tablebase.Backend.FathomTest.Stub do
def probe(_fen), do: {:ok, %Tablebase{category: :draw, checkmate: false, moves: []}}
end
defmodule Chesstrainer.Endgames.Tablebase.Backend.FathomTest.ExplodingFallback do
@moduledoc false
def probe(_fen), do: raise("the remote tablebase must not be called")
end
defmodule Chesstrainer.Endgames.Tablebase.Backend.FathomTest do
# Not async: the local backend reads app config and Fathom keeps global state.
use ExUnit.Case, async: false
@@ -18,6 +23,7 @@ defmodule Chesstrainer.Endgames.Tablebase.Backend.FathomTest do
@krk "4k3/8/8/8/8/8/8/R3K3 w - - 0 1"
@mate "7k/6Q1/5K2/8/8/8/8/8 b - - 0 1"
@kpk "8/8/3k4/8/8/3KP3/8/8 w - - 0 1"
test "probes a KRvK position locally" do
assert {:ok, %Tablebase{} = tablebase} = FathomBackend.probe(@krk)
@@ -82,4 +88,37 @@ defmodule Chesstrainer.Endgames.Tablebase.Backend.FathomTest do
assert {:ok, %Tablebase{category: :draw}} = FathomBackend.probe(@krk)
end
end
describe "probe_local_detail/1" do
test "returns the raw root WDL, DTZ and every legal move" do
assert {:ok, %{wdl: :win, dtz: dtz, moves: moves}} =
FathomBackend.probe_local_detail(@krk)
assert is_integer(dtz)
assert length(moves) == 15
assert Enum.all?(moves, &(elem(&1, 0) in [:win, :cursed_win, :draw, :blessed_loss, :loss]))
end
test "returns terminal for a finished position" do
assert {:error, :terminal} = FathomBackend.probe_local_detail(@mate)
end
test "returns unsupported when the local files cannot answer the position" do
assert {:error, :unsupported} = FathomBackend.probe_local_detail(@kpk)
end
test "never calls the remote backend" do
previous = Application.get_env(:chesstrainer, :tablebase_fallback)
on_exit(fn ->
if previous,
do: Application.put_env(:chesstrainer, :tablebase_fallback, previous),
else: Application.delete_env(:chesstrainer, :tablebase_fallback)
end)
Application.put_env(:chesstrainer, :tablebase_fallback, __MODULE__.ExplodingFallback)
assert {:error, :unsupported} = FathomBackend.probe_local_detail(@kpk)
end
end
end