Weight puzzle selection and stop repeats inside a session (#183)

Selection took a hard rating band (medium = user rating ± 100), removed
everything attempted in the last 30 days, and drew uniformly from what was
left. Because the bulk importer gives every endgame in a batch the same
rating, the band held either the whole library or — once a user's rating
drifted past it — only the endgames that user had already played. With
`fresh` empty, the fallback re-drew from the same band ignoring recency, so
recently played puzzles came straight back. An empty band used a
deterministic nearest-match, alternating between the same two puzzles.

Difficulty is now a bias rather than a filter: candidates are weighted by
`exp(-|rating - target| / spread)` and drawn with an exponential race, so
the whole library stays reachable and a thin region around the user's rating
can never empty the pool. `easy` and `hard` keep one-sided guards so the
labels stay honest. When every candidate is excluded, selection serves the
least recently attempted endgame instead of a random recent one.

Sessions now carry the ids they have served, which fixes the observed
in-session repeat (only the current puzzle used to be excluded), and guest
training gets the same treatment. The 30-day window is configurable as the
repeat window and is now named in GLOSSARY.md, distinct from the rating
window and from the tablebase cooldown.

Difficulty copy is qualitative, since the band numbers no longer describe
how selection works.
This commit is contained in:
2026-11-17 14:28:24 +01:00
committed by GitHub
parent fa1fef7b62
commit e0b446bbfb
8 changed files with 212 additions and 115 deletions
+60 -27
View File
@@ -7,6 +7,7 @@ defmodule Chesstrainer.PuzzlesTest do
alias Chesstrainer.Accounts.Scope
alias Chesstrainer.Attempts
alias Chesstrainer.Puzzles
alias Chesstrainer.Repo
# The shared `endgame_fixture/1` picks one of six FENs at random-ish, which
# can collide when one test creates several endgames under async load. Pin
@@ -22,71 +23,103 @@ defmodule Chesstrainer.PuzzlesTest do
endgame_fixture(fen: Enum.fetch!(@fens, index), rating: rating)
end
describe "difficulty_range/2" do
test "is centred on the rating and open-ended at the extremes" do
assert Puzzles.difficulty_range(1500, :medium) == {1400, 1600}
assert Puzzles.difficulty_range(1500, :easy) == {nil, 1400}
assert Puzzles.difficulty_range(1500, :hard) == {1600, nil}
defp attempt_days_ago(user, endgame, days) do
{:ok, attempt} = Attempts.record_attempt(Scope.for_user(user), endgame, :solved)
attempt
|> Ecto.Changeset.change(
inserted_at: DateTime.utc_now() |> DateTime.add(-days, :day) |> DateTime.truncate(:second)
)
|> Repo.update!()
end
describe "difficulty_target/2" do
test "centres on the rating and offsets easy and hard" do
assert Puzzles.difficulty_target(1500, :medium) == 1500
assert Puzzles.difficulty_target(1500, :easy) == 1300
assert Puzzles.difficulty_target(1500, :hard) == 1700
end
end
describe "selection settings" do
test "uses the configured spread and repeat window" do
assert Puzzles.spread() == 150
assert Puzzles.repeat_window_days() == 30
end
end
describe "next_endgame/3" do
test "medium picks an endgame within the band" do
test "draws far more often from near the user's rating" do
user = user_fixture()
in_band = endgame_at(1550, 0)
_out_of_band = endgame_at(1900, 1)
near = endgame_at(1500, 0)
_far = endgame_at(4000, 1)
assert Puzzles.next_endgame(user, :medium).id == in_band.id
for _ <- 1..50 do
assert Puzzles.next_endgame(user, :medium).id == near.id
end
end
test "easy picks an endgame below the user's rating" do
test "easy never serves above the user's rating plus the spread" do
user = user_fixture()
easy = endgame_at(1300, 0)
_medium = endgame_at(1500, 1)
_too_hard = endgame_at(3000, 1)
assert Puzzles.next_endgame(user, :easy).id == easy.id
end
test "hard picks an endgame above the user's rating" do
test "hard never serves below the user's rating minus the spread" do
user = user_fixture()
hard = endgame_at(1700, 0)
_medium = endgame_at(1500, 1)
_too_easy = endgame_at(100, 1)
assert Puzzles.next_endgame(user, :hard).id == hard.id
end
test "falls back to the nearest endgame when the band is empty" do
user = user_fixture()
only = endgame_at(2200, 0)
assert Puzzles.next_endgame(user, :medium).id == only.id
end
test "prefers endgames not attempted in the last 30 days" do
test "prefers endgames not attempted inside the repeat window" do
user = user_fixture()
attempted = endgame_at(1500, 0)
fresh = endgame_at(1500, 1)
{:ok, _} = Attempts.record_attempt(Scope.for_user(user), attempted, :solved)
attempt_days_ago(user, attempted, 1)
assert Puzzles.next_endgame(user, :medium).id == fresh.id
end
test "falls back to a recent endgame once the fresh pool is empty" do
test "serves an endgame again once its attempt is outside the repeat window" do
user = user_fixture()
only = endgame_at(1500, 0)
{:ok, _} = Attempts.record_attempt(Scope.for_user(user), only, :solved)
attempt_days_ago(user, only, 31)
assert Puzzles.next_endgame(user, :medium).id == only.id
end
test "skips excluded ids" do
test "falls back to the least recently attempted endgame when all are excluded" do
user = user_fixture()
excluded = endgame_at(1500, 0)
older = endgame_at(1500, 0)
newer = endgame_at(1500, 1)
attempt_days_ago(user, older, 20)
attempt_days_ago(user, newer, 10)
assert Puzzles.next_endgame(user, :medium).id == older.id
end
test "skips endgames already served in the session" do
user = user_fixture()
served = endgame_at(1500, 0)
other = endgame_at(1500, 1)
assert Puzzles.next_endgame(user, :medium, exclude_ids: [excluded.id]).id == other.id
assert Puzzles.next_endgame(user, :medium, exclude_ids: [served.id]).id == other.id
end
test "returns nil when every endgame has been served and attempted" do
user = user_fixture()
only = endgame_at(1500, 0)
attempt_days_ago(user, only, 1)
assert Puzzles.next_endgame(user, :medium, exclude_ids: [only.id]) == nil
end
test "returns nil when no endgames exist" do
@@ -170,6 +170,18 @@ defmodule ChesstrainerWeb.EndgameLive.TrainTest do
assert Enum.any?(endgames, &(&1.id == second.id))
end
test "does not serve the same puzzle twice in a session", %{conn: conn} do
{view, [only]} = start_medium_session(conn, 1)
solve_current(view)
view |> element("#next-puzzle-button") |> render_click()
assigns = :sys.get_state(view.pid).socket.assigns
assert has_element?(view, "#pool-exhausted")
assert assigns.endgame.id == only.id
assert assigns.session.served_ids == [only.id]
end
test "keeps next puzzle available while reviewing", %{conn: conn} do
{view, endgames} = start_medium_session(conn, 2)
first = :sys.get_state(view.pid).socket.assigns.endgame