0049. Game review data: PGN-first plies, stored engine scores, honest gaps
Source:
docs/decisions/0049-game-review-data.md. Generated at build time; edit the source file.
Date: 2026-11-22. Status: accepted.
Context
Section titled “Context”The single-game review page (M5a Task 10) replays one game move by move with an eval bar, an eval graph, judgements, best-move arrows and clocks. The TS worker has already stored everything it knows: the PGN in games, one moves row per ply (mover-POV win% before and after, judgement, best move, phase, time spent), and position_evals (White-POV engine scores keyed by fenKey and protocol). The loader must read this without inventing anything, show a game whose analysis is not done, and never confirm that another user’s game id exists.
Decision
Section titled “Decision”- Loader.
getGameReview(db, userId, gameId)inapps/web/src/server/games/review.tsreturns aReviewGameor null. It checksgameIdagainst a uuid pattern before any query, then runs inreadOnlyand selects the gamewhere id = $1 and account_id in scopedAccountIds(user)(ADR 0046). A malformed id, a random uuid, another user’s game and a game of an account nobody links all give null, which the page turns intonotFound(). - PGN-first plies.
pliesalways come fromparsePgn(game.pgn), so every game can be replayed, whatever its analysis status. A PGN thatparsePgnrejects (a variant, a custom start) gives no plies rather than an error. - Mismatch rule.
analysisis attached only whenanalysis_status = 'done'and themovesrows line up with the PGN: the same count, and row i has ply i, the same colour and the sameuci. Otherwise everyanalysisis null,analysedis false,momentsis empty,startScoreis null andphasesis{}. A partial or stale analysis is shown as no analysis, never as a half-filled graph. Accuracies are likewise shown only fordonegames, because a re-queued game keeps its old values. - R7, engine scores.
score(White’s point of view) is theposition_evalsrow forfenKey(fenAfter)under the game’sanalysis_protocol, fetched for the start and every position in onefen_key = any($keys)query. When the row is missing, the score is derived from the stored win%:scoreFromWin(winWhite)invertswinningChancesexactly, rounds to an integer and clamps to ±1000 (the capwinningChancesapplies), and the score is markedscoreSource: "derived", which the page shows with “≈”. The final position of a checkmate scores{ mate: 0 }(sourceengine; it is a fact, not an estimate).startScorefollows the same rule, deriving from the first move’swinBefore. - White’s point of view.
moves.win_afteris the mover’s win%;winWhiteis it for White’s plies and100 − win_afterfor Black’s, matchingbuildMoveRecordsin@ci/chess-core. - R7, classification. Only what is stored:
blunder,mistakeandinaccuracyfrommoves.judgement, andbestwhen the playeduciequals the storedbestUciand the move carries no judgement. There is no!or!!, because nothing stored supports brilliance. - Moments. The player’s own judged plies (
moves.color = games.player_color), worst first bydeltaW, ties broken by the lower ply. They drive the review’s opening ply and the reveal’s climax (ADR 0052). - Clocks.
clockAfteris the mover’s[%clk]after the move, from the PGN;hasClocksis true when any ply has one. - Source-URL allow-list.
sourceUrl(platform, headers)readsLinkfor chess.com andSitefor Lichess, and accepts only a plainhttps:URL onwww.chess.comorlichess.orgrespectively, with no port and no credentials. Anything else (javascript:, look-alike hosts,http:) gives null, so a crafted PGN header can never become a link.
Extension points
Section titled “Extension points”ReviewPly.analysisgains R2’s motif tags (fork, pin, hanging piece, …) as an extra field; consumers that ignore it keep working.moments(game id plus ply) are the anchors for R3’s “Ask your coach” entry points.
Consequences
Section titled “Consequences”The review never shows a number the worker did not store or that is not an exact inverse of one it did, and a derived score is always marked. A game whose analysis is re-run shows as pending until the new rows land, rather than mixing old and new analyses. The one extra query per review (the eval lookup) is bounded by the game’s length.