feat: coloured search output for a terminal

Started from a terminal, Rigby now shows its search for a human:
coloured progress lines (depth/seldepth, playouts, speed, time, a tree
fill bar, score in pawns, the PV fading with depth) at 125, 250, 500 ms
and then every second, and at the end a table of the most visited root
moves with a bar for their share of the visits, their score and prior.
Tree reuse and collections are shown as notes, and bestmove is
highlighted.

The first "uci" command switches to plain UCI output for good. GUIs and
match runners send it first and read stdout through a pipe, so they
never see the coloured output. NO_COLOR turns it off. On Windows the
console gets ANSI processing and UTF-8 output.

Checked: the UCI output of a scripted session (two searches with tree
reuse, a stalemate root) is identical to main apart from nps and time;
go wtime, go infinite with stop, and quit work; the pretty mode was
driven in a real console, including the switch to UCI. Search is
untouched, so the bench is unchanged.

Bench: 546018

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
rektdieandclaude committed 2026-11-15 14:15:49 +01:00
1 parent 1754753a6e
commit 6490286852
6 files changed
+319 -17

No files matched your search

+52 -16
View File
@@ -7,6 +7,7 @@
#include <sstream>
#include "eval.h"
#include "pretty.h"
#include "uci_io.h"
namespace rigby {
@@ -412,26 +413,45 @@ void Searcher::print_info() const {
const i64 ms = elapsed_ms();
const u32 best = best_child(Tree::root());
std::ostringstream out;
out << "info depth " << (iterations_ ? std::max<u64>(1, depth_sum_ / iterations_) : 0) << " seldepth "
<< seldepth_;
pretty::Progress p;
p.depth = int(iterations_ ? std::max<u64>(1, depth_sum_ / iterations_) : 0);
p.seldepth = seldepth_;
p.cp = eval::q_to_cp(best && tree_[best].visits ? tree_[best].q : -root.q);
p.playouts = iterations_;
p.nps = ms ? iterations_ * 1000 / u64(ms) : iterations_;
p.ms = ms;
p.hashfull = int(tree_.used() * 1000 / tree_.capacity());
const float q = best && tree_[best].visits ? tree_[best].q : -root.q;
out << " score cp " << eval::q_to_cp(q);
out << " nodes " << iterations_ << " nps " << (ms ? iterations_ * 1000 / u64(ms) : iterations_) << " time " << ms;
out << " hashfull " << (tree_.used() * 1000 / tree_.capacity());
out << " pv";
u32 idx = Tree::root();
for (int ply = 0; ply < 64; ply++) {
const u32 c = best_child(idx);
if (!c || !tree_[c].visits) break;
out << ' ' << Move::from_raw(tree_[c].move).to_uci();
p.pv.push_back(Move::from_raw(tree_[c].move).to_uci());
idx = c;
}
if (pretty) {
uci_io::send(pretty::progress(p));
return;
}
std::ostringstream out;
out << "info depth " << p.depth << " seldepth " << p.seldepth << " score cp " << p.cp << " nodes " << p.playouts
<< " nps " << p.nps << " time " << p.ms << " hashfull " << p.hashfull << " pv";
for (const std::string& m : p.pv) out << ' ' << m;
uci_io::send(out.str());
}
void Searcher::print_root_moves() const {
const Node& root = tree_[Tree::root()];
std::vector<pretty::RootMove> moves;
for (u32 i = 0; i < root.num_children; i++) {
const Node& child = tree_[root.first_child + i];
moves.push_back({Move::from_raw(child.move).to_uci(), child.visits, eval::q_to_cp(child.q), child.prior()});
}
uci_io::send(pretty::root_table(std::move(moves), 8));
}
SearchResult Searcher::search(const Position& root, const std::vector<u64>& history, const SearchLimits& limits,
bool print) {
start_time_ = Clock::now();
@@ -453,7 +473,7 @@ SearchResult Searcher::search(const Position& root, const std::vector<u64>& hist
#ifdef RIGBY_VERIFY_TREE
if (reused) verify_tree();
#endif
if (print && reused) uci_io::send("info string reused " + std::to_string(reused) + " nodes");
if (print && !pretty && reused) uci_io::send("info string reused " + std::to_string(reused) + " nodes");
iterations_ = 0;
depth_sum_ = 0;
seldepth_ = 0;
@@ -467,10 +487,18 @@ SearchResult Searcher::search(const Position& root, const std::vector<u64>& hist
generate_legal(root, root_moves);
if (root_moves.empty()) {
tree_valid_ = false;
if (print) uci_io::send("info depth 0 score " + std::string(root.in_check() ? "mate 0" : "cp 0"));
if (print && pretty)
uci_io::send(pretty::note(root.in_check() ? "\n checkmate, no legal moves" : "\n stalemate, no legal moves"));
else if (print)
uci_io::send("info depth 0 score " + std::string(root.in_check() ? "mate 0" : "cp 0"));
return result;
}
if (print && pretty) {
if (reused) uci_io::send(pretty::reused(reused));
uci_io::send(pretty::header());
}
// The root needs its children before the first playout, so that even a
// single playout leaves a move to play.
if (tree_[Tree::root()].state == UNEXPANDED && !expand(Tree::root(), root)) {
@@ -479,7 +507,9 @@ SearchResult Searcher::search(const Position& root, const std::vector<u64>& hist
expand(Tree::root(), root);
}
i64 last_info = 0;
// UCI reports once a second. A human gets lines at 125, 250, 500 ms and so
// on, so short searches still show how the search developed.
i64 next_info = pretty ? 125 : 1000;
do {
playout();
if (gc_pending_) {
@@ -492,8 +522,8 @@ SearchResult Searcher::search(const Position& root, const std::vector<u64>& hist
}
if (print && (iterations_ & 1023) == 0) {
const i64 ms = elapsed_ms();
if (ms - last_info >= 1000) {
last_info = ms;
if (ms >= next_info) {
next_info = pretty && next_info < 1000 ? next_info * 2 : ms + 1000;
print_info();
}
}
@@ -501,7 +531,13 @@ SearchResult Searcher::search(const Position& root, const std::vector<u64>& hist
if (print) {
print_info();
if (gc_runs_) uci_io::send("info string tree collected " + std::to_string(gc_runs_) + " times");
if (pretty) {
print_root_moves();
if (gc_runs_) uci_io::send(pretty::note("\n tree memory was full " + std::to_string(gc_runs_) +
"x: the least visited branches were collected"));
} else if (gc_runs_) {
uci_io::send("info string tree collected " + std::to_string(gc_runs_) + " times");
}
}
const u32 best = best_child(Tree::root());