Android: game report — mistakes, blunders and accuracy, as on the desktop

The game analysis of the desktop (evaluations of the main line, judgements, accuracy,
cache and the engine job) moves into a shared crate, game-review, so both apps give the
same numbers. The desktop uses it unchanged in behaviour.

On Android, ⋮ → «Game analysis» runs Stockfish over the main line in its own process
(the live engine waits): an evaluation graph and a status row between the board and the
notation, marks in the notation, the better line of an error on the board, a report sheet
with both players side by side. Games with [%eval] and games analysed before show their
report at once. The analysis stops with the screen and goes on after it, also after the
phone is turned, and is cached as it goes.

Reviewed by an independent tester and designer (docs/design/android-ui.md §21, §22).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
vrubelroman 2026-10-23 14:10:58 +03:00
parent 7fcc75c7af
commit 1661e238d8
31 changed files with 3016 additions and 567 deletions

View file

@ -209,7 +209,7 @@ impl GameView {
MoveKind::Null => return None,
};
if self.review.visible
&& let Some(mark) = self.review.marks.get(&self.cursor)
&& let Some(mark) = self.review.analysis.marks.get(&self.cursor)
{
return Some(board::Badge {
square,
@ -853,7 +853,7 @@ impl GameView {
game: &self.game,
cursor: self.cursor,
scroll_to_cursor: self.scroll_to_cursor,
marks: self.review.visible.then_some(&self.review.marks),
marks: self.review.visible.then_some(&self.review.analysis.marks),
preview: self
.preview
.as_ref()
@ -865,7 +865,7 @@ impl GameView {
self.goto(id);
}
if let Some((id, shown)) = resp.best
&& let Some(mark) = self.review.marks.get(&id)
&& let Some(mark) = self.review.analysis.marks.get(&id)
&& let Some(root) = self.game.position(mark.base)
&& !mark.best.is_empty()
{
@ -1240,7 +1240,7 @@ impl GameView {
.game
.mainline()
.into_iter()
.find_map(|id| self.review.marks.get(&id).cloned());
.find_map(|id| self.review.analysis.marks.get(&id).cloned());
if let Some(mark) = mark
&& let Some(root) = self.game.position(mark.base)
&& !mark.best.is_empty()

View file

@ -5889,7 +5889,6 @@ mod tests {
include_str!("online_ui.rs"),
include_str!("update.rs"),
include_str!("update_ui.rs"),
include_str!("report.rs"),
include_str!("board.rs"),
include_str!("engine_download.rs"),
include_str!("main.rs"),
@ -5961,7 +5960,6 @@ mod tests {
include_str!("online_ui.rs"),
include_str!("update.rs"),
include_str!("update_ui.rs"),
include_str!("report.rs"),
include_str!("board.rs"),
include_str!("engine_download.rs"),
include_str!("main.rs"),

View file

@ -20,7 +20,6 @@ mod notation;
mod online;
mod online_ui;
mod reference;
mod report;
mod review;
mod san;
mod settings;

View file

@ -1,569 +0,0 @@
//! Game review metrics: win percentage, move and game accuracy, inaccuracy/mistake/blunder
//! judgements, average centipawn loss and game phases.
//!
//! The method follows Lichess (lila `WinPercent`, `AccuracyPercent`, `Advice`, and the
//! scalachess `Divider`, see <https://lichess.org/page/accuracy>); this is an independent
//! reimplementation of the published formulas.
use chess_core::shakmaty::{Bitboard, Chess, Color, Position, Rank};
use serde::{Deserialize, Serialize};
/// Engine evaluation from White's point of view.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum Eval {
Cp(i32),
/// Forced mate; `moves == 0` in a checkmate position.
Mate {
white: bool,
moves: u32,
},
}
/// Centipawn ceiling: larger advantages (and mates) count as this much.
const CP_CEILING: i32 = 1000;
impl Eval {
pub fn from_white_eval(e: uci::WhiteEval) -> Eval {
match e {
uci::WhiteEval::Cp(cp) => Eval::Cp(cp),
uci::WhiteEval::Mate { winner, moves } => Eval::Mate {
white: winner == uci::Side::White,
moves,
},
}
}
/// Centipawns for `side`, mates folded into the ceiling.
fn cp_for(self, side: Color) -> i32 {
let white = match self {
Eval::Cp(cp) => cp.clamp(-CP_CEILING, CP_CEILING),
Eval::Mate { white: true, .. } => CP_CEILING,
Eval::Mate { white: false, .. } => -CP_CEILING,
};
if side == Color::White { white } else { -white }
}
/// `Some(true)` if `side` has a forced mate, `Some(false)` if it is being mated.
fn mate_for(self, side: Color) -> Option<bool> {
match self {
Eval::Cp(_) => None,
Eval::Mate { white, .. } => Some(white == (side == Color::White)),
}
}
/// Chance of winning for `side`, 0..100.
pub fn win_percent(self, side: Color) -> f64 {
50.0 + 50.0 * winning_chances(self.cp_for(side))
}
/// "+0.35", "−1.20", "#3", "#-2", "1-0".
pub fn format(self) -> String {
match self {
Eval::Cp(cp) => format!("{:+.2}", f64::from(cp) / 100.0),
Eval::Mate { white, moves: 0 } => if white { "1-0" } else { "0-1" }.into(),
Eval::Mate { white: true, moves } => format!("#{moves}"),
Eval::Mate {
white: false,
moves,
} => format!("#-{moves}"),
}
}
/// Value for a PGN `[%eval …]` command (pawns or `#n`).
pub fn pgn(self) -> String {
match self {
Eval::Cp(cp) => format!("{:.2}", f64::from(cp) / 100.0),
Eval::Mate { white, moves } => {
format!("#{}{moves}", if white { "" } else { "-" })
}
}
}
/// Parses a PGN `[%eval …]` payload ("0.35", "-1.2", "#3", "#-2").
pub fn parse_pgn(s: &str) -> Option<Eval> {
let s = s.split([',', ' ']).next()?.trim();
if let Some(m) = s.strip_prefix('#') {
let n: i32 = m.parse().ok()?;
return Some(Eval::Mate {
white: !m.starts_with('-'),
moves: n.unsigned_abs(),
});
}
let pawns: f64 = s.parse().ok()?;
pawns
.is_finite()
.then(|| Eval::Cp((pawns * 100.0).round().clamp(-100_000.0, 100_000.0) as i32))
}
}
/// Lichess winning chances, −1..1.
fn winning_chances(cp: i32) -> f64 {
2.0 / (1.0 + (-0.003_682_08 * f64::from(cp)).exp()) - 1.0
}
/// Accuracy of a move given the mover's win percentage before and after it, 0..100.
fn move_accuracy(before: f64, after: f64) -> f64 {
if after >= before {
return 100.0;
}
let raw = 103.166_810_071_164_9 * (-0.043_544_153_867_539_51 * (before - after)).exp()
- 3.166_924_740_191_411;
// +1: uncertainty bonus for imperfect analysis, as on Lichess.
(raw + 1.0).clamp(0.0, 100.0)
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Judgement {
Inaccuracy,
Mistake,
Blunder,
}
/// Judgement of a move from the evaluations before and after it.
fn judge(before: Eval, after: Eval, mover: Color) -> Option<Judgement> {
use Judgement::*;
let (mb, ma) = (before.mate_for(mover), after.mate_for(mover));
if ma == Some(false) && mb != Some(false) {
// The move allows a forced mate.
let cp = before.cp_for(mover);
return Some(if cp < -999 {
Inaccuracy
} else if cp < -700 {
Mistake
} else {
Blunder
});
}
if mb == Some(true) && ma != Some(true) {
// A forced mate was let go.
let cp = after.cp_for(mover);
return Some(if cp > 999 {
Inaccuracy
} else if cp > 700 {
Mistake
} else {
Blunder
});
}
if mb.is_some() || ma.is_some() {
return None;
}
let delta = winning_chances(before.cp_for(mover)) - winning_chances(after.cp_for(mover));
if delta >= 0.3 {
Some(Blunder)
} else if delta >= 0.2 {
Some(Mistake)
} else if delta >= 0.1 {
Some(Inaccuracy)
} else {
None
}
}
/// Game phases as indices of the first position of each phase.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct Phases {
pub middlegame: Option<usize>,
pub endgame: Option<usize>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Phase {
Opening,
Middlegame,
Endgame,
}
impl Phases {
/// Splits a sequence of positions: the middlegame starts once few pieces are left or
/// a back rank is sparse; the endgame once at most six queens, rooks and minor pieces remain.
pub fn divide(positions: &[Chess]) -> Phases {
let majors_minors = |p: &Chess| {
let b = p.board();
(b.knights() | b.bishops() | b.rooks() | b.queens()).count()
};
let backrank_sparse = |p: &Chess| {
let b = p.board();
(b.by_color(Color::White) & Bitboard::from_rank(Rank::First)).count() < 4
|| (b.by_color(Color::Black) & Bitboard::from_rank(Rank::Eighth)).count() < 4
};
let middlegame = positions
.iter()
.position(|p| majors_minors(p) <= 10 || backrank_sparse(p));
let endgame = middlegame.and_then(|m| {
positions[m..]
.iter()
.position(|p| majors_minors(p) <= 6)
.map(|i| i + m)
});
Phases {
middlegame,
endgame,
}
}
pub fn of(self, position: usize) -> Phase {
if self.endgame.is_some_and(|e| position >= e) {
Phase::Endgame
} else if self.middlegame.is_some_and(|m| position >= m) {
Phase::Middlegame
} else {
Phase::Opening
}
}
}
/// A move of the analysed line.
#[derive(Clone, Copy, Debug)]
pub struct Ply {
pub mover: Color,
/// Teaching null move `--`: not graded.
pub null: bool,
/// The move played equals the engine's best move (never judged as an error).
pub best: bool,
}
#[derive(Clone, Debug, Default, PartialEq)]
pub struct SideStats {
pub inaccuracies: u32,
pub mistakes: u32,
pub blunders: u32,
/// Average centipawn loss.
pub acpl: Option<u32>,
pub accuracy: Option<f64>,
/// Opening, middlegame, endgame.
pub phase_accuracy: [Option<f64>; 3],
}
#[derive(Clone, Debug, Default)]
pub struct Report {
/// Per ply of the line.
pub judgements: Vec<Option<Judgement>>,
pub white: SideStats,
pub black: SideStats,
pub phases: Phases,
/// All positions are evaluated.
pub complete: bool,
}
impl Report {
pub fn side(&self, c: Color) -> &SideStats {
match c {
Color::White => &self.white,
Color::Black => &self.black,
}
}
/// `evals[i]` belongs to the position before ply `i`; `evals.len() == plies.len() + 1`.
pub fn build(evals: &[Option<Eval>], plies: &[Ply], phases: Phases) -> Report {
let n = plies.len().min(evals.len().saturating_sub(1));
let mut report = Report {
judgements: vec![None; plies.len()],
phases,
complete: evals.iter().take(n + 1).all(Option::is_some),
..Report::default()
};
let mut losses: [Vec<i32>; 2] = [Vec::new(), Vec::new()];
for i in 0..n {
let ply = plies[i];
let (Some(before), Some(after)) = (evals[i], evals[i + 1]) else {
continue;
};
if ply.null {
continue;
}
let j = if ply.best {
None
} else {
judge(before, after, ply.mover)
};
report.judgements[i] = j;
let stats = match ply.mover {
Color::White => &mut report.white,
Color::Black => &mut report.black,
};
match j {
Some(Judgement::Inaccuracy) => stats.inaccuracies += 1,
Some(Judgement::Mistake) => stats.mistakes += 1,
Some(Judgement::Blunder) => stats.blunders += 1,
None => {}
}
let loss = (before.cp_for(ply.mover) - after.cp_for(ply.mover)).max(0);
losses[ply.mover as usize].push(loss);
}
for color in [Color::White, Color::Black] {
let l = &losses[color as usize];
if !l.is_empty() {
let avg = l.iter().map(|x| i64::from(*x)).sum::<i64>() as f64 / l.len() as f64;
let stats = match color {
Color::White => &mut report.white,
Color::Black => &mut report.black,
};
stats.acpl = Some(avg.round() as u32);
}
}
if report.complete && n > 0 {
let wp: Vec<f64> = evals[..=n]
.iter()
.map(|e| e.map_or(50.0, |e| e.win_percent(Color::White)))
.collect();
report.fill_accuracy(&wp, &plies[..n]);
}
report
}
/// Lichess game accuracy: mean of the volatility-weighted mean and the harmonic mean
/// of move accuracies.
fn fill_accuracy(&mut self, wp: &[f64], plies: &[Ply]) {
let window = (wp.len() / 10).clamp(2, 8).min(wp.len());
let mut windows: Vec<&[f64]> = vec![&wp[..window]; window.saturating_sub(2)];
windows.extend(wp.windows(window));
let weights: Vec<f64> = windows
.iter()
.map(|w| std_dev(w).clamp(0.5, 12.0))
.collect();
// (color, phase, accuracy, weight)
let mut moves = Vec::new();
for (i, ply) in plies.iter().enumerate() {
if ply.null {
continue;
}
let (before, after) = (wp[i], wp[i + 1]);
let (before, after) = match ply.mover {
Color::White => (before, after),
Color::Black => (100.0 - before, 100.0 - after),
};
let weight = weights.get(i).copied().unwrap_or(0.5);
moves.push((
ply.mover,
self.phases.of(i),
move_accuracy(before, after),
weight,
));
}
for color in [Color::White, Color::Black] {
let all: Vec<(f64, f64)> = moves
.iter()
.filter(|m| m.0 == color)
.map(|m| (m.2, m.3))
.collect();
let phase = |p: Phase| {
let v: Vec<(f64, f64)> = moves
.iter()
.filter(|m| m.0 == color && m.1 == p)
.map(|m| (m.2, m.3))
.collect();
// Too few moves in a phase say nothing about it.
if v.len() >= 3 { mixed_mean(&v) } else { None }
};
let stats = match color {
Color::White => &mut self.white,
Color::Black => &mut self.black,
};
stats.accuracy = mixed_mean(&all);
stats.phase_accuracy = [
phase(Phase::Opening),
phase(Phase::Middlegame),
phase(Phase::Endgame),
];
}
}
}
fn std_dev(xs: &[f64]) -> f64 {
if xs.is_empty() {
return 0.0;
}
let mean = xs.iter().sum::<f64>() / xs.len() as f64;
(xs.iter().map(|x| (x - mean).powi(2)).sum::<f64>() / xs.len() as f64).sqrt()
}
/// Average of the weighted mean and the harmonic mean of `(accuracy, weight)`.
fn mixed_mean(v: &[(f64, f64)]) -> Option<f64> {
if v.is_empty() {
return None;
}
let wsum: f64 = v.iter().map(|x| x.1).sum();
let weighted = v.iter().map(|x| x.0 * x.1).sum::<f64>() / wsum.max(f64::EPSILON);
let harmonic = v.len() as f64 / v.iter().map(|x| 1.0 / x.0.max(1.0)).sum::<f64>();
Some((weighted + harmonic) / 2.0)
}
#[cfg(test)]
mod tests {
use super::*;
fn ply(mover: Color) -> Ply {
Ply {
mover,
null: false,
best: false,
}
}
fn alternating(n: usize) -> Vec<Ply> {
(0..n)
.map(|i| {
ply(if i % 2 == 0 {
Color::White
} else {
Color::Black
})
})
.collect()
}
#[test]
fn win_percent_matches_lichess_curve() {
assert!((Eval::Cp(0).win_percent(Color::White) - 50.0).abs() < 1e-9);
let w = Eval::Cp(100).win_percent(Color::White);
assert!((w - 59.1).abs() < 0.1, "{w}");
assert!((Eval::Cp(100).win_percent(Color::Black) - (100.0 - w)).abs() < 1e-9);
// Mates count as the centipawn ceiling.
let m = Eval::Mate {
white: true,
moves: 3,
};
assert_eq!(
m.win_percent(Color::White),
Eval::Cp(5000).win_percent(Color::White)
);
}
#[test]
fn move_accuracy_bounds() {
assert_eq!(move_accuracy(50.0, 60.0), 100.0);
assert_eq!(move_accuracy(50.0, 50.0), 100.0);
let small = move_accuracy(60.0, 55.0);
let big = move_accuracy(60.0, 20.0);
assert!(small > 75.0 && small < 100.0, "{small}");
assert!(big < 20.0, "{big}");
assert_eq!(move_accuracy(100.0, 0.0), 0.0);
}
#[test]
fn judgements_follow_win_chance_drop() {
let w = Color::White;
assert_eq!(judge(Eval::Cp(0), Eval::Cp(-30), w), None);
assert_eq!(
judge(Eval::Cp(0), Eval::Cp(-60), w),
Some(Judgement::Inaccuracy)
);
assert_eq!(
judge(Eval::Cp(0), Eval::Cp(-150), w),
Some(Judgement::Mistake)
);
assert_eq!(
judge(Eval::Cp(0), Eval::Cp(-500), w),
Some(Judgement::Blunder)
);
// Black's point of view: White's eval going up is Black's loss.
assert_eq!(
judge(Eval::Cp(0), Eval::Cp(500), Color::Black),
Some(Judgement::Blunder)
);
// Already winning by a lot: losing a pawn changes nothing.
assert_eq!(judge(Eval::Cp(900), Eval::Cp(800), w), None);
let mated = Eval::Mate {
white: false,
moves: 4,
};
let mating = Eval::Mate {
white: true,
moves: 4,
};
assert_eq!(judge(Eval::Cp(0), mated, w), Some(Judgement::Blunder));
assert_eq!(
judge(Eval::Cp(-1200), mated, w),
Some(Judgement::Inaccuracy)
);
assert_eq!(judge(mating, Eval::Cp(300), w), Some(Judgement::Blunder));
assert_eq!(
judge(mating, Eval::Cp(1500), w),
Some(Judgement::Inaccuracy)
);
assert_eq!(judge(mating, mating, w), None);
assert_eq!(judge(mated, mated, w), None);
assert_eq!(judge(Eval::Cp(0), mating, w), None);
}
#[test]
fn report_counts_and_accuracy() {
// White blunders on ply 2; everything else is quiet.
let cps = [20, 25, 20, -400, -390, -400, -395];
let evals: Vec<Option<Eval>> = cps.iter().map(|c| Some(Eval::Cp(*c))).collect();
let plies = alternating(cps.len() - 1);
let r = Report::build(&evals, &plies, Phases::default());
assert!(r.complete);
assert_eq!(r.judgements[2], Some(Judgement::Blunder));
assert_eq!(r.white.blunders, 1);
assert_eq!(
r.black.blunders + r.black.mistakes + r.black.inaccuracies,
0
);
let (wa, ba) = (r.white.accuracy.expect("w"), r.black.accuracy.expect("b"));
assert!(wa < ba, "white {wa} black {ba}");
assert!(ba > 90.0);
assert!(r.white.acpl.expect("acpl") > r.black.acpl.expect("acpl"));
}
#[test]
fn partial_and_special_plies() {
let evals = vec![Some(Eval::Cp(0)), Some(Eval::Cp(-500)), None, None];
let mut plies = alternating(3);
let r = Report::build(&evals, &plies, Phases::default());
assert!(!r.complete);
assert_eq!(r.white.blunders, 1);
assert_eq!(r.white.accuracy, None);
// The engine's own best move is never an error; null moves are not graded.
plies[0].best = true;
let r = Report::build(&evals, &plies, Phases::default());
assert_eq!(r.white.blunders, 0);
plies[0].best = false;
plies[0].null = true;
let r = Report::build(&evals, &plies, Phases::default());
assert_eq!(r.white.blunders, 0);
assert_eq!(r.white.acpl, None);
}
#[test]
fn pgn_eval_roundtrip() {
for e in [
Eval::Cp(35),
Eval::Cp(-120),
Eval::Mate {
white: true,
moves: 3,
},
Eval::Mate {
white: false,
moves: 2,
},
] {
assert_eq!(Eval::parse_pgn(&e.pgn()), Some(e));
}
assert_eq!(Eval::parse_pgn("0.17,22"), Some(Eval::Cp(17)));
assert_eq!(Eval::parse_pgn("abc"), None);
}
#[test]
fn phases_of_a_real_game() {
// The Opera Game: queens and rooks stay until the very end, so no endgame.
let game = chess_core::parse_str(
"1. e4 e5 2. Nf3 d6 3. d4 Bg4 4. dxe5 Bxf3 5. Qxf3 dxe5 6. Bc4 Nf6 7. Qb3 Qe7 \
8. Nc3 c6 9. Bg5 b5 10. Nxb5 cxb5 11. Bxb5+ Nbd7 12. O-O-O Rd8 13. Rxd7 Rxd7 \
14. Rd1 Qe6 15. Bxd7+ Nxd7 16. Qb8+ Nxb8 17. Rd8# 1-0",
)
.game;
let positions: Vec<Chess> = std::iter::once(chess_core::NodeId::ROOT)
.chain(game.mainline())
.filter_map(|n| game.position(n).cloned())
.collect();
assert_eq!(positions.len(), 34);
let p = Phases::divide(&positions);
let mid = p.middlegame.expect("middlegame");
assert!(mid > 4 && mid < 25, "{mid}");
assert_eq!(p.of(0), Phase::Opening);
assert_eq!(p.of(mid), Phase::Middlegame);
}
}

View file

@ -1,5 +1,7 @@
//! Whole-game analysis: a background engine job over the main line, cached results,
//! the evaluation graph under the board, the report tab and the launch dialog.
//! Whole-game analysis on the desktop: the background engine job over the main line,
//! the evaluation graph under the board, the report tab and the launch dialog. The
//! evaluations, judgements, cache and the job itself are the `game-review` crate's,
//! shared with the Android app.
//!
//! The job uses its own engine process and never touches the game; writing the results
//! into the game is a separate, undoable action ([`Review::write_to_game`]).
@ -8,63 +10,18 @@ use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::mpsc::{Receiver, Sender, channel};
use std::time::{Duration, Instant};
use std::sync::mpsc::{Receiver, channel};
use chess_core::shakmaty::san::SanPlus;
use chess_core::shakmaty::uci::UciMove;
use chess_core::shakmaty::{Chess, Color, Move, Position};
use chess_core::{Annotation, CommentPart, Game, Nag, NodeId, parse_comment};
use chess_core::shakmaty::{Color, Move};
use chess_core::{Game, Nag, NodeId};
use egui::{Color32, RichText, Sense, Stroke, Ui, pos2, vec2};
use serde::{Deserialize, Serialize};
use uci::{EngineEvent, EnginePosition, ScoreBound, SearchId, SearchLimits, Side};
use game_review::{Eval, GameAnalysis, JobEvent, JobFailure, JobSettings, Judgement, ReviewMeta};
pub use game_review::Mark;
use crate::i18n::{tr, trf};
use crate::report::{Eval, Judgement, Phases, Ply, Report};
use crate::settings::{EngineConfig, REVIEW_QUALITY, ReviewSettings, Settings};
/// Engine result for one position of the main line.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct PosEval {
pub eval: Eval,
/// Principal variation in UCI; the first move is the engine's best move.
#[serde(default)]
pub pv: Vec<String>,
#[serde(default)]
pub depth: u32,
}
/// Where the evaluations came from.
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct ReviewMeta {
pub engine: String,
/// Nodes per position; 0 = read from `[%eval]` comments of the PGN.
pub nodes: u64,
}
#[derive(Serialize, Deserialize)]
struct CacheFile {
version: u32,
meta: ReviewMeta,
evals: Vec<(String, PosEval)>,
}
const CACHE_VERSION: u32 = 1;
/// A node-limited search should never take this long; stop it if it does.
const SEARCH_GUARD: Duration = Duration::from_secs(300);
/// An engine assessment of a main-line move, shown over the notation.
#[derive(Clone, Debug)]
pub struct Mark {
pub judgement: Judgement,
/// Node before the move (where the better line starts).
pub base: NodeId,
pub best: Vec<Move>,
pub best_san: Vec<String>,
/// "23…Nf6".
pub best_label: Option<String>,
}
pub enum ReportAction {
Goto(NodeId),
Write,
@ -78,19 +35,8 @@ pub enum DialogAction {
OpenEngines,
}
struct Target {
key: String,
pos: EnginePosition,
turn: Side,
}
enum JobMsg {
Engine(String),
Eval {
key: String,
eval: PosEval,
nps: u64,
},
Event(JobEvent),
Done,
Failed(String),
}
@ -110,8 +56,8 @@ impl Drop for Job {
}
pub struct Review {
evals: HashMap<String, PosEval>,
pub meta: Option<ReviewMeta>,
/// Evaluations of the main line and the report built from them.
pub analysis: GameAnalysis,
job: Option<Job>,
pub error: Option<String>,
/// Graph and report tab are shown.
@ -119,51 +65,31 @@ pub struct Review {
pub dialog_open: bool,
/// Measured engine speed per thread, for time estimates.
nps_per_thread: Option<f64>,
built_rev: Option<u64>,
dirty: bool,
/// Root and main-line nodes: the analysed positions.
line: Vec<NodeId>,
keys: Vec<String>,
/// Side making each move of the line.
movers: Vec<Color>,
/// Evaluation per position (engine result or a terminal position).
values: Vec<Option<Eval>>,
pub report: Report,
pub marks: HashMap<NodeId, Mark>,
/// Report tab: index of the last visited error per side and kind.
cycle: HashMap<(Color, Judgement), usize>,
}
/// The folder of the cached analyses.
fn cache_dir() -> Option<PathBuf> {
Settings::cache_dir().map(|d| d.join("analysis"))
}
impl Review {
pub fn new(game: &Game) -> Self {
let mut r = Review {
evals: HashMap::new(),
meta: None,
let analysis = GameAnalysis::new(game, cache_dir().as_deref());
Review {
visible: analysis.has_data(),
analysis,
job: None,
error: None,
visible: false,
dialog_open: false,
nps_per_thread: None,
built_rev: None,
dirty: false,
line: Vec::new(),
keys: Vec::new(),
movers: Vec::new(),
values: Vec::new(),
report: Report::default(),
marks: HashMap::new(),
cycle: HashMap::new(),
};
r.rebuild(game);
if r.load_cache() || r.load_pgn_evals(game) {
r.rebuild(game);
r.visible = true;
}
r
}
pub fn has_data(&self) -> bool {
!self.evals.is_empty()
self.analysis.has_data()
}
pub fn running(&self) -> bool {
@ -176,78 +102,12 @@ impl Review {
/// Positions of the current main line without an evaluation.
pub fn missing(&self) -> usize {
self.values.iter().filter(|v| v.is_none()).count()
self.analysis.missing()
}
/// Re-derives the report when the game or the evaluations changed. Cheap otherwise.
pub fn refresh(&mut self, game: &Game) {
if self.dirty || self.built_rev != Some(game.revision()) {
self.rebuild(game);
}
}
fn rebuild(&mut self, game: &Game) {
self.line = std::iter::once(NodeId::ROOT)
.chain(game.mainline())
.collect();
let positions: Vec<Chess> = self
.line
.iter()
.filter_map(|id| game.position(*id).cloned())
.collect();
self.keys = self
.line
.iter()
.map(|id| position_key(game, *id).map(|k| k.0).unwrap_or_default())
.collect();
self.values = (0..self.line.len())
.map(|i| {
self.evals
.get(&self.keys[i])
.map(|e| e.eval)
.or_else(|| positions.get(i).and_then(terminal_eval))
})
.collect();
let mut plies = Vec::with_capacity(self.line.len().saturating_sub(1));
for i in 1..self.line.len() {
let mv = game.game_move(self.line[i]);
let best = self.evals.get(&self.keys[i - 1]).and_then(|e| e.pv.first());
plies.push(Ply {
mover: positions.get(i - 1).map_or(Color::White, |p| p.turn()),
null: mv.is_none_or(|m| m.is_null()),
best: best.is_some() && mv.and_then(|m| m.uci()).as_ref() == best,
});
}
self.movers = plies.iter().map(|p| p.mover).collect();
self.report = Report::build(&self.values, &plies, Phases::divide(&positions));
self.marks.clear();
for (i, j) in self.report.judgements.iter().enumerate() {
let Some(judgement) = *j else { continue };
let (base, node) = (self.line[i], self.line[i + 1]);
let (best, best_san) = match (self.evals.get(&self.keys[i]), positions.get(i)) {
(Some(e), Some(root)) => parse_pv(root, &e.pv),
_ => (Vec::new(), Vec::new()),
};
let best_label = best_san.first().and_then(|san| {
let (num, color) = game.move_number(node)?;
Some(match color {
Color::White => format!("{num}.{san}"),
Color::Black => format!("{num}…{san}"),
})
});
self.marks.insert(
node,
Mark {
judgement,
base,
best,
best_san,
best_label,
},
);
}
self.built_rev = Some(game.revision());
self.dirty = false;
self.analysis.refresh(game);
}
/// Starts the engine job. `full` discards earlier results; otherwise only positions
@ -266,41 +126,46 @@ impl Review {
self.error = Some(tr("The built-in engine is not part of this build").into());
return false;
};
if full {
self.evals.clear();
self.dirty = true;
}
self.meta = Some(ReviewMeta {
let meta = ReviewMeta {
engine: engine.name.clone(),
nodes: rs.nodes(),
});
self.rebuild(game);
let targets: Vec<Target> = (0..self.line.len())
.filter(|i| self.values[*i].is_none())
.filter_map(|i| {
let (key, pos) = position_key(game, self.line[i])?;
let turn = match game.position(self.line[i])?.turn() {
Color::White => Side::White,
Color::Black => Side::Black,
};
Some(Target { key, pos, turn })
})
.collect();
};
let targets = self.analysis.begin(game, meta, full);
if targets.is_empty() {
return false;
}
let (tx, rx) = channel();
let cancel = Arc::new(AtomicBool::new(false));
let total = targets.len();
let (path, threads, hash, nodes) = (path, rs.threads.max(1), rs.hash_mb, rs.nodes());
// Filling in keeps the quality of the earlier run (see `GameAnalysis::begin`).
let nodes = self.analysis.meta.as_ref().map_or(rs.nodes(), |m| m.nodes);
let settings = JobSettings {
threads: rs.threads.max(1),
hash_mb: rs.hash_mb,
nodes,
eval_file: None,
};
let threads = settings.threads;
let (cancel_w, ctx_w) = (cancel.clone(), ctx.clone());
let spawned = std::thread::Builder::new()
.name("game-review".into())
.spawn(move || {
let msg = match run_job(path, threads, hash, nodes, targets, &tx, &cancel_w, &ctx_w)
{
let result = game_review::run(
uci::EngineConfig::new(&path),
&settings,
targets,
&cancel_w,
&mut |e| {
let _ = tx.send(JobMsg::Event(e));
ctx_w.request_repaint();
},
);
let msg = match result {
Ok(()) => JobMsg::Done,
Err(e) => JobMsg::Failed(e),
Err(JobFailure::Start(e) | JobFailure::Engine(e)) => JobMsg::Failed(e),
Err(JobFailure::Terminated(reason)) => {
JobMsg::Failed(crate::analysis::termination_message(&reason))
}
};
let _ = tx.send(msg);
ctx_w.request_repaint();
@ -334,18 +199,14 @@ impl Review {
let mut ended = false;
while let Ok(msg) = job.rx.try_recv() {
match msg {
JobMsg::Engine(name) => {
if let Some(m) = &mut self.meta {
m.engine = name;
JobMsg::Event(e) => {
if let JobEvent::Eval { nps, .. } = &e {
job.done += 1;
if *nps > 0 {
self.nps_per_thread = Some(*nps as f64 / f64::from(job.threads));
}
}
}
JobMsg::Eval { key, eval, nps } => {
job.done += 1;
if nps > 0 {
self.nps_per_thread = Some(nps as f64 / f64::from(job.threads));
}
self.evals.insert(key, eval);
self.dirty = true;
self.analysis.apply(e);
}
JobMsg::Done => ended = true,
JobMsg::Failed(e) => {
@ -369,138 +230,17 @@ impl Review {
}
/// After an inaccuracy, mistake or blunder: how the opponent punishes it and how
/// the player best answers, the first two moves of the engine line from the position
/// after the move (shown as arrows, as on Lichess).
/// the player best answers (shown as arrows, as on Lichess).
pub fn refutation(&self, node: NodeId, game: &Game) -> Vec<Move> {
if !self.marks.contains_key(&node) {
return Vec::new();
}
let (Some(i), Some(pos)) = (
self.line.iter().position(|n| *n == node),
game.position(node),
) else {
return Vec::new();
};
let Some(e) = self.keys.get(i).and_then(|k| self.evals.get(k)) else {
return Vec::new();
};
let (mut moves, _) = parse_pv(pos, &e.pv);
moves.truncate(2);
moves
}
// ----- cache -----------------------------------------------------------------
fn cache_path(&self) -> Option<PathBuf> {
let mut h: u64 = 0xcbf2_9ce4_8422_2325;
for k in &self.keys {
for b in k.bytes().chain(std::iter::once(b'\n')) {
h ^= u64::from(b);
h = h.wrapping_mul(0x0100_0000_01b3);
}
}
Some(
Settings::cache_dir()?
.join("analysis")
.join(format!("{h:016x}.json")),
)
self.analysis.refutation(node, game)
}
fn save_cache(&self) {
let Some(meta) = self.meta.clone().filter(|m| m.nodes > 0) else {
return;
};
if self.evals.is_empty() {
return;
if let Some(dir) = cache_dir()
&& let Err(e) = self.analysis.save_cache(&dir)
{
eprintln!("review cache: {}: {e}", dir.display());
}
let Some(path) = self.cache_path() else {
return;
};
let file = CacheFile {
version: CACHE_VERSION,
meta,
evals: self
.evals
.iter()
.map(|(k, v)| (k.clone(), v.clone()))
.collect(),
};
match serde_json::to_vec(&file) {
Ok(bytes) => {
if let Err(e) = crate::settings::write_atomic(&path, &bytes) {
eprintln!("review cache: {}: {e}", path.display());
}
}
Err(e) => eprintln!("review cache: {e}"),
}
}
fn load_cache(&mut self) -> bool {
let Some(bytes) = self.cache_path().and_then(|p| std::fs::read(p).ok()) else {
return false;
};
let Ok(file) = serde_json::from_slice::<CacheFile>(&bytes) else {
return false;
};
if file.version != CACHE_VERSION || file.evals.is_empty() {
return false;
}
self.meta = Some(file.meta);
self.evals = file.evals.into_iter().collect();
true
}
/// Uses `[%eval]` comments (Lichess exports, or our own written analysis) when every
/// main-line move has one.
fn load_pgn_evals(&mut self, game: &Game) -> bool {
let mut found = Vec::new();
for (i, id) in self.line.iter().enumerate().skip(1) {
let Some(node) = game.node(*id) else {
return false;
};
let e = node
.comments_after()
.iter()
.flat_map(|c| parse_comment(c))
.find_map(|p| match p {
CommentPart::Annotation(Annotation::Other { name, payload })
if name == "eval" =>
{
Eval::parse_pgn(&payload)
}
_ => None,
});
match e {
Some(e) => found.push((i, e)),
None if self.values[i].is_some() => {}
None => return false,
}
}
let Some(&(_, first)) = found.first() else {
return false;
};
// The start position carries no comment: Lichess assumes +0.15 for the initial one.
let root = if game.start().is_standard() {
Eval::Cp(15)
} else {
first
};
found.insert(0, (0, root));
for (i, eval) in found {
self.evals.insert(
self.keys[i].clone(),
PosEval {
eval,
pv: Vec::new(),
depth: 0,
},
);
}
self.meta = Some(ReviewMeta {
engine: "PGN".into(),
nodes: 0,
});
true
}
// ----- writing into the game -------------------------------------------------
@ -511,8 +251,8 @@ impl Review {
pub fn write_to_game(&self, game: &mut Game) -> usize {
game.edit_group(|g| {
let mut errors = 0;
for i in 1..self.line.len() {
let id = self.line[i];
for i in 1..self.analysis.line().len() {
let id = self.analysis.line()[i];
let Some(node) = g.node(id) else { continue };
let mut nags = node.nags().to_vec();
let mut comments: Vec<String> = node
@ -521,12 +261,12 @@ impl Review {
.map(|c| strip_eval(c))
.filter(|c| !c.trim().is_empty())
.collect();
if let Some(e) = self.values[i]
if let Some(e) = self.analysis.values()[i]
&& !matches!(e, Eval::Mate { moves: 0, .. })
{
comments.insert(0, format!("[%eval {}]", e.pgn()));
}
if let Some(mark) = self.marks.get(&id) {
if let Some(mark) = self.analysis.marks.get(&id) {
errors += 1;
if !nags.iter().any(|n| n.is_move_assessment()) {
nags.push(match mark.judgement {
@ -571,7 +311,7 @@ impl Review {
let painter = ui.painter_at(rect);
let visuals = ui.visuals();
painter.rect_filled(rect, 4.0, visuals.extreme_bg_color);
let n = self.values.len();
let n = self.analysis.values().len();
if n < 2 {
return None;
}
@ -580,7 +320,8 @@ impl Review {
let y_of = |wp: f64| inner.bottom() - inner.height() * (wp / 100.0) as f32;
let mid = y_of(50.0);
let wp: Vec<Option<f64>> = self
.values
.analysis
.values()
.iter()
.map(|v| v.map(|e| e.win_percent(Color::White)))
.collect();
@ -609,12 +350,18 @@ impl Review {
visuals.weak_text_color(),
);
};
if self.report.phases.middlegame.is_some_and(|m| m > 0) {
if self
.analysis
.report
.phases
.middlegame
.is_some_and(|m| m > 0)
{
label(inner.left(), tr("Opening"));
}
for (idx, name) in [
(self.report.phases.middlegame, tr("Middlegame")),
(self.report.phases.endgame, tr("Endgame")),
(self.analysis.report.phases.middlegame, tr("Middlegame")),
(self.analysis.report.phases.endgame, tr("Endgame")),
] {
if let Some(i) = idx {
let x = x_of(i);
@ -673,7 +420,7 @@ impl Review {
if run.len() > 1 {
painter.add(egui::Shape::line(run, curve));
}
for (i, j) in self.report.judgements.iter().enumerate() {
for (i, j) in self.analysis.report.judgements.iter().enumerate() {
if let (Some(j), Some(w)) = (j, wp[i + 1]) {
painter.circle_filled(pos2(x_of(i + 1), y_of(w)), 3.5, judgement_color(*j));
}
@ -707,12 +454,14 @@ impl Review {
let label = if i == 0 {
tr("Start position").to_string()
} else {
crate::san::show(&game.move_label(self.line[i]).unwrap_or_default()).into_owned()
crate::san::show(&game.move_label(self.analysis.line()[i]).unwrap_or_default())
.into_owned()
};
let eval = self.values[i]
let eval = self.analysis.values()[i]
.map(Eval::format)
.unwrap_or_else(|| "…".into());
let mark = self
.analysis
.report
.judgements
.get(i.wrapping_sub(1))
@ -731,7 +480,7 @@ impl Review {
if (resp.clicked() || resp.dragged())
&& let Some(p) = resp.interact_pointer_pos()
{
let target = self.line[index_at(p.x)];
let target = self.analysis.line()[index_at(p.x)];
if target != cursor {
return Some(target);
}
@ -741,13 +490,7 @@ impl Review {
/// Index in the main line of `cursor`, or of the main-line move a variation leaves from.
fn line_index(&self, cursor: NodeId, game: &Game) -> Option<usize> {
let mut id = cursor;
loop {
if let Some(i) = self.line.iter().position(|n| *n == id) {
return Some(i);
}
id = game.parent(id)?;
}
self.analysis.line_index(cursor, game)
}
/// Report tab: per-player statistics in the Lichess layout.
@ -761,7 +504,7 @@ impl Review {
ui.add_space(10.0);
}
ui.separator();
if let Some(meta) = &self.meta {
if let Some(meta) = &self.analysis.meta {
let source = if meta.nodes == 0 {
tr("Evaluations from the PGN ([%eval])").to_string()
} else {
@ -785,7 +528,7 @@ impl Review {
RichText::new(trf("Positions without evaluation: {n}", &[("n", &missing)]))
.color(ui.visuals().warn_fg_color),
);
let can_update = self.meta.as_ref().is_some_and(|m| m.nodes > 0);
let can_update = self.analysis.meta.as_ref().is_some_and(|m| m.nodes > 0);
if can_update && ui.button(tr("Update")).clicked() {
action = Some(ReportAction::Update);
}
@ -842,7 +585,7 @@ impl Review {
}
ui.label(RichText::new(name).strong().size(16.0));
});
let s = self.report.side(color).clone();
let s = self.analysis.report.side(color).clone();
egui::Grid::new(("review_side", color as u8))
.num_columns(2)
.spacing(vec2(10.0, 3.0))
@ -919,14 +662,7 @@ impl Review {
/// Next main-line move of `color` with judgement `j` (cycles).
fn next_error(&mut self, color: Color, j: Judgement) -> Option<NodeId> {
let hits: Vec<NodeId> = self
.report
.judgements
.iter()
.enumerate()
.filter(|(i, x)| **x == Some(j) && self.movers.get(*i) == Some(&color))
.map(|(i, _)| self.line[i + 1])
.collect();
let hits = self.analysis.errors(color, j);
if hits.is_empty() {
return None;
}
@ -1020,11 +756,7 @@ impl Review {
ui.end_row();
});
ui.add_space(6.0);
let positions = self
.values
.iter()
.filter(|v| !matches!(v, Some(Eval::Mate { moves: 0, .. })))
.count();
let positions = self.analysis.positions();
let per_thread = self.nps_per_thread.unwrap_or(700_000.0);
let secs = positions as f64 * rs.nodes() as f64 / (per_thread * f64::from(rs.threads));
ui.label(trf(
@ -1076,136 +808,6 @@ impl Drop for Review {
}
}
#[allow(clippy::too_many_arguments)]
fn run_job(
path: PathBuf,
threads: u32,
hash_mb: u32,
nodes: u64,
targets: Vec<Target>,
tx: &Sender<JobMsg>,
cancel: &AtomicBool,
ctx: &egui::Context,
) -> Result<(), String> {
let handle = uci::Engine::spawn_with(uci::EngineConfig::new(&path), Box::new(|| {}))
.map_err(|e| e.to_string())?;
if let Some(name) = &handle.info().name {
let _ = tx.send(JobMsg::Engine(name.clone()));
}
// Unknown options are not fatal: some engines lack Threads/Hash/MultiPV.
let _ = handle.set_threads(threads);
let _ = handle.set_hash_mb(hash_mb);
let _ = handle.set_multipv(1);
let _ = handle.new_game();
for (i, t) in targets.into_iter().enumerate() {
let id = SearchId(i as u64 + 1);
handle
.search(t.pos.clone(), id, SearchLimits::Nodes(nodes))
.map_err(|e| e.to_string())?;
let started = Instant::now();
let mut stopping = false;
let mut last: Option<uci::SearchInfo> = None;
loop {
if cancel.load(Ordering::Relaxed) {
handle.quit();
return Ok(());
}
if !stopping && started.elapsed() > SEARCH_GUARD {
let _ = handle.stop();
stopping = true;
}
match handle.recv_timeout(Duration::from_millis(100)) {
Some(EngineEvent::Info { search_id, info }) if search_id == id => {
if info.multipv_index() == 1 && info.score.is_some() {
let exact = info.score.is_some_and(|s| s.bound == ScoreBound::Exact);
if exact || last.is_none() {
last = Some(info);
}
}
}
Some(EngineEvent::BestMove {
search_id, best, ..
}) if search_id == id => {
if let Some(info) = last.take()
&& let Some(score) = info.score
{
let eval = Eval::from_white_eval(score.to_white(t.turn).eval);
let mut pv: Vec<String> = info.pv.iter().take(12).cloned().collect();
if let Some(b) = best
&& pv.first() != Some(&b)
{
pv = vec![b];
}
let _ = tx.send(JobMsg::Eval {
key: t.key,
eval: PosEval {
eval,
pv,
depth: info.depth.unwrap_or(0),
},
nps: info.nps.unwrap_or(0),
});
ctx.request_repaint();
}
break;
}
Some(EngineEvent::Terminated(reason)) => {
return Err(crate::analysis::termination_message(&reason));
}
_ => {}
}
}
}
handle.quit();
Ok(())
}
/// Cache key and UCI position of a node (full history, see [`Game::engine_history`]).
fn position_key(game: &Game, id: NodeId) -> Option<(String, EnginePosition)> {
let h = game.engine_history(id)?;
let key = format!(
"{}|{}",
h.start_fen.as_deref().unwrap_or("startpos"),
h.moves.join(" ")
);
let pos = match h.start_fen {
None => EnginePosition::startpos(h.moves),
Some(fen) => EnginePosition::fen(fen, h.moves),
};
Some((key, pos))
}
/// Evaluation of a position without legal moves.
fn terminal_eval(pos: &Chess) -> Option<Eval> {
if pos.is_checkmate() {
Some(Eval::Mate {
white: pos.turn() == Color::Black,
moves: 0,
})
} else if pos.is_stalemate() {
Some(Eval::Cp(0))
} else {
None
}
}
fn parse_pv(root: &Chess, pv: &[String]) -> (Vec<Move>, Vec<String>) {
let mut pos = root.clone();
let (mut moves, mut san) = (Vec::new(), Vec::new());
for u in pv {
let Some(m) = u
.parse::<UciMove>()
.ok()
.and_then(|um| um.to_move(&pos).ok())
else {
break;
};
san.push(SanPlus::from_move_and_play_unchecked(&mut pos, m).to_string());
moves.push(m);
}
(moves, san)
}
/// Removes `[%eval …]` commands from a comment.
fn strip_eval(c: &str) -> String {
let mut out = String::new();
@ -1327,17 +929,17 @@ mod tests {
let mut r = Review::new(&game);
assert!(r.has_data() && r.visible);
assert_eq!(r.missing(), 0);
assert!(r.report.complete);
assert!(r.analysis.report.complete);
// 2…Ke7 throws the game away; 3.Qxe5# is a terminal position.
let ke7 = game.mainline()[3];
assert_eq!(
r.marks.get(&ke7).map(|m| m.judgement),
r.analysis.marks.get(&ke7).map(|m| m.judgement),
Some(Judgement::Blunder)
);
assert_eq!(r.report.black.blunders, 1);
assert_eq!(r.analysis.report.black.blunders, 1);
let errors = r.write_to_game(&mut game);
assert_eq!(errors, r.marks.len());
assert_eq!(errors, r.analysis.marks.len());
let node = game.node(ke7).expect("node");
assert!(node.nags().contains(&Nag::BLUNDER));
assert!(node.comments_after()[0].starts_with("[%eval 6.50]"));