1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
//! Sekirei — USI (Universal Shogi Interface) engine binary.
//!
//! Run: `cargo run --release -p sekirei`
//! Then paste USI commands on stdin.
use std::io::{self, BufRead, Write};
use std::path::Path;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::thread::JoinHandle;
use std::time::Duration;
use sekirei_core::{
board::Board,
color::Color,
nnue::load_weights,
search::{SearchConfig, SpeculativeSearcher},
sfen::{board_to_sfen, move_to_usi, parse_position_cmd},
tt::Tt,
};
mod book;
mod invariant;
use book::Book;
use invariant::DiagCtx;
// ---- Engine identity ----
const ENGINE_NAME: &str = "Sekirei";
const ENGINE_AUTHOR: &str = "ke.tanabe@gmail.com";
const DEFAULT_HASH_MB: usize = 64;
const DEFAULT_BOOK_FILE: &str = "data/opening_book.jsonl";
// Dedicated speculative-search pool size. Was hardcoded in make_searcher()
// with no USI option (issue #9); this is that same value now exposed as
// the SpecTopN option's default, so not setting it changes nothing.
const DEFAULT_SPEC_TOP_N: usize = 3;
// ---- Main loop ----
/// Abort and join any in-flight search thread before mutating shared search
/// state. Shared by `go`, `usinewgame`, and every `setoption` branch that
/// rebuilds `searcher` (`Hash`, `SpecTopN`) -- extracted after `setoption
/// Hash` was found missing this exact sequence (issue #10; `SpecTopN`,
/// issue #9, needs the identical sequence for the same reason: rebuilding
/// the searcher's dedicated pool out from under an in-flight search thread
/// is safe for the search itself, which holds its own `Arc` clone, but
/// leaves nothing blocking the main loop from answering the next command
/// before that thread's `bestmove` has actually been printed).
fn abort_and_join_inflight_search(
search_abort: &mut Option<Arc<AtomicBool>>,
search_handle: &mut Option<JoinHandle<()>>,
) {
if let Some(a) = search_abort.take() {
a.store(true, Ordering::Relaxed);
}
if let Some(h) = search_handle.take() {
h.join().ok();
}
}
fn main() {
// Optional: load NNUE weights from first command-line argument
// Usage: cargo run --release -p usi -- weights.bin
let mut weight_path = String::new();
let mut weight_hash: Option<u64> = None;
if let Some(path) = std::env::args().nth(1) {
match load_weights(Path::new(&path)) {
Ok(()) => eprintln!("info string NNUE weights loaded from {path}"),
Err(e) => eprintln!("info string weight load failed ({path}): {e}"),
}
weight_hash = invariant::hash_file(&path);
weight_path = path;
}
let binary_hash = std::env::current_exe()
.ok()
.and_then(|p| invariant::hash_file(p.to_str()?));
let stdin = io::stdin();
let stdout = io::stdout();
let mut hash_mb = DEFAULT_HASH_MB;
let mut spec_top_n = DEFAULT_SPEC_TOP_N;
let mut searcher = make_searcher(hash_mb, spec_top_n);
let mut eval_file: Option<String> = None;
let mut move_overhead_ms: u64 = 50;
let mut multi_pv: u32 = 1;
let mut use_book = true;
let mut book_max_ply: usize = 30;
let mut book_min_confidence: f64 = 0.20;
let mut book_file = DEFAULT_BOOK_FILE.to_string();
let mut book: Option<Book> = None;
let mut book_loaded_path: Option<String> = None;
// Current board position (updated by "position" commands)
let mut board = Board::startpos();
// Ply reached by the last "position" command's move list (0 = startpos) --
// used only to gate book lookups to the opening phase (BookMaxPly).
let mut current_ply: usize = 0;
// ---- invariant-check bookkeeping (crates/sekirei-usi/src/invariant.rs) ----
// Incremented on every "usinewgame" -- carried into a bestmove-illegal
// diagnostic dump so a failure can be tied back to a specific game in a
// long-lived process, the way sprint_gate.sh's per-game logs are.
let mut game_counter: u64 = 0;
// Raw body of the last "position" command, for the same reason.
let mut last_position_cmd = String::from("startpos");
// Mirrors the "Threads" setoption value (0 = unset/rayon default).
let mut threads: u32 = 0;
// Abort flag and handle for the currently running search (None if no search in flight)
let mut search_abort: Option<Arc<AtomicBool>> = None;
let mut search_handle: Option<JoinHandle<()>> = None;
// Set true before aborting a ponder search so the dying thread skips bestmove output
let suppress_bm: Arc<AtomicBool> = Arc::new(AtomicBool::new(false));
// Saved args from `go ponder ...` so ponderhit can restart with real time limits
let mut ponder_go_args: Option<String> = None;
for raw in stdin.lock().lines() {
let Ok(line) = raw else { break };
let line = line.trim().to_string();
if line.is_empty() {
continue;
}
let (cmd, rest) = line
.split_once(' ')
.map(|(c, r)| (c, r.trim()))
.unwrap_or((&line, ""));
match cmd {
"usi" => {
println!("id name {ENGINE_NAME}");
println!("id author {ENGINE_AUTHOR}");
println!("option name Hash type spin default {DEFAULT_HASH_MB} min 1 max 2048");
println!("option name Threads type spin default 0 min 0 max 512");
println!(
"option name SpecTopN type spin default {DEFAULT_SPEC_TOP_N} min 0 max 512"
);
println!("option name MoveOverhead type spin default 50 min 0 max 5000");
println!("option name Ponder type check default false");
println!("option name MultiPV type spin default 1 min 1 max 256");
println!("option name EvalFile type string default ");
println!("option name UseBook type check default true");
println!("option name BookMaxPly type spin default 30 min 0 max 200");
println!("option name BookMinConfidence type string default 0.20");
println!("option name BookFile type string default {DEFAULT_BOOK_FILE}");
println!("usiok");
stdout.lock().flush().ok();
}
"isready" => {
if let Some(ref path) = eval_file
&& !sekirei_core::nnue::weights_active()
{
match sekirei_core::nnue::load_weights(Path::new(path)) {
Ok(()) => {
println!("info string NNUE weights loaded from {path}");
// `board` (constructed at startup, before this load) has a
// stale accumulator baked from the pre-load fallback weights.
board.refresh_acc();
}
Err(e) => println!("info string weight load failed: {e}"),
}
}
if use_book && book_loaded_path.as_deref() != Some(book_file.as_str()) {
match Book::load(&book_file) {
Ok(b) => {
println!(
"info string opening book loaded from {book_file} ({} positions)",
b.len()
);
book = Some(b);
book_loaded_path = Some(book_file.clone());
}
Err(e) => {
println!("info string opening book load failed ({book_file}): {e}");
book_loaded_path = Some(book_file.clone()); // don't retry every isready
}
}
}
println!("readyok");
stdout.lock().flush().ok();
}
"setoption" => {
// "setoption name <Name> value <Value>"
let parts: Vec<&str> = rest.split_whitespace().collect();
if parts.get(1) == Some(&"Hash")
&& let Some(mb) = parts.get(3).and_then(|s| s.parse().ok())
{
abort_and_join_inflight_search(&mut search_abort, &mut search_handle);
hash_mb = mb;
searcher = make_searcher(hash_mb, spec_top_n);
} else if parts.get(1) == Some(&"SpecTopN")
&& let Some(n) = parts.get(3).and_then(|s| s.parse().ok())
{
// Same abort+join requirement as Hash just above, and for the
// identical reason: this rebuilds the searcher's dedicated
// speculative-search pool, which the in-flight search's own
// Arc<SpeculativeSearcher> clone is unaffected by, but nothing
// else here would otherwise block the main loop from moving on
// to the next command before that search's bestmove is printed.
abort_and_join_inflight_search(&mut search_abort, &mut search_handle);
spec_top_n = n;
searcher = make_searcher(hash_mb, spec_top_n);
} else if parts.get(1) == Some(&"Threads") {
if let Some(n) = parts.get(3).and_then(|s| s.parse::<usize>().ok()) {
threads = n as u32;
// ponytail: build_global silently fails if already init'd; that's fine
let _ = rayon::ThreadPoolBuilder::new()
.num_threads(n)
.build_global();
}
} else if parts.get(1) == Some(&"MoveOverhead") {
if let Some(n) = parts.get(3).and_then(|s| s.parse().ok()) {
move_overhead_ms = n;
}
} else if parts.get(1) == Some(&"MultiPV") {
if let Some(n) = parts.get(3).and_then(|s| s.parse::<u32>().ok()) {
multi_pv = n.max(1);
}
} else if parts.get(1) == Some(&"EvalFile") {
// value may contain spaces (e.g. paths with spaces)
if let Some(val) = rest.split_once("value ").map(|(_, v)| v.trim())
&& !val.is_empty()
{
eval_file = Some(val.to_string());
}
} else if parts.get(1) == Some(&"UseBook") {
if let Some(v) = parts.get(3) {
use_book = *v == "true";
}
} else if parts.get(1) == Some(&"BookMaxPly") {
if let Some(n) = parts.get(3).and_then(|s| s.parse().ok()) {
book_max_ply = n;
}
} else if parts.get(1) == Some(&"BookMinConfidence") {
if let Some(n) = parts.get(3).and_then(|s| s.parse().ok()) {
book_min_confidence = n;
}
} else if parts.get(1) == Some(&"BookFile")
&& let Some(val) = rest.split_once("value ").map(|(_, v)| v.trim())
&& !val.is_empty()
{
book_file = val.to_string();
}
}
"usinewgame" => {
abort_and_join_inflight_search(&mut search_abort, &mut search_handle);
board = Board::startpos();
searcher.clear_tt();
game_counter += 1;
}
"position" => match parse_position_cmd(rest) {
Ok(b) => {
board = b;
// Only gates book lookups (BookMaxPly) -- doesn't need to
// handle every conceivable "position" form, just the
// "startpos moves ..." shape this project's own tooling
// always sends.
current_ply = rest
.split_whitespace()
.skip_while(|&t| t != "moves")
.skip(1)
.count();
last_position_cmd = rest.to_string();
// Must run before any search on this position -- an
// already-desynced board must never be allowed to
// search at all, since its bestmove would be answering
// the wrong question. Panics (stderr diagnostics) on
// mismatch instead of returning, by design. Replays the
// move list independently against a shadow board (not
// just the base SFEN) -- see invariant.rs's
// verify_position_replay doc comment for why.
invariant::verify_position_replay(
rest,
&board,
&invariant::ReplayDiagCtx {
game_counter,
weight_path: weight_path.clone(),
weight_hash,
binary_hash,
},
);
}
Err(e) => eprintln!("position error: {e}"),
},
"go" => {
// Abort any in-flight search and join before starting a new one.
// suppress_bm stays false so the dying thread still emits bestmove.
abort_and_join_inflight_search(&mut search_abort, &mut search_handle);
let pondering = rest.split_whitespace().any(|t| t == "ponder");
if pondering {
ponder_go_args = Some(rest.to_string());
} else {
ponder_go_args = None;
}
// Reset suppress flag now that the previous thread has joined.
suppress_bm.store(false, Ordering::Relaxed);
// Book lookup: skip search entirely for a known opening
// position, within BookMaxPly. Not applied while pondering --
// that has its own ponderhit/new-position protocol flow that
// an instant book bestmove would short-circuit incorrectly.
if !pondering
&& use_book
&& current_ply < book_max_ply
&& let Some(b) = &book
&& let Some(mv) = b.lookup(&board_to_sfen(&board), &board, book_min_confidence)
{
println!("info string book move");
invariant::assert_legal_bestmove(
&board,
mv,
&DiagCtx {
game_counter,
last_position_cmd: last_position_cmd.clone(),
weight_path: weight_path.clone(),
weight_hash,
threads,
board_hash_at_search_start: board.hash(),
accumulator_hash_at_search_start: invariant::hash_accumulator(
&board.acc,
),
},
);
println!("bestmove {}", move_to_usi(mv));
stdout.lock().flush().ok();
continue;
}
let config = parse_go(
rest,
board.side_to_move,
move_overhead_ms,
pondering,
multi_pv,
);
let abort = searcher.abort_flag();
search_abort = Some(abort);
let searcher2 = Arc::clone(&searcher);
let mut board2 = board.clone();
let suppress2 = Arc::clone(&suppress_bm);
let diag_ctx = DiagCtx {
game_counter,
last_position_cmd: last_position_cmd.clone(),
weight_path: weight_path.clone(),
weight_hash,
threads,
board_hash_at_search_start: board.hash(),
accumulator_hash_at_search_start: invariant::hash_accumulator(&board.acc),
};
search_handle = Some(std::thread::spawn(move || {
let info = searcher2.search(&mut board2, config);
if suppress2.load(Ordering::Relaxed) {
return; // ponderhit aborted this search; caller starts a new one
}
let elapsed_ms = info.elapsed.as_millis().max(1) as u64;
let nps = info.nodes.saturating_mul(1000) / elapsed_ms;
if info.pv_list.len() > 1 {
for (i, &(mv, score)) in info.pv_list.iter().enumerate() {
println!(
"info multipv {} depth {} score cp {} nodes {} nps {} time {} hashfull {} pv {}",
i + 1,
info.depth,
score,
info.nodes,
nps,
elapsed_ms,
info.hashfull,
move_to_usi(mv)
);
}
} else if let Some(m) = info.best_move {
println!(
"info depth {} score cp {} nodes {} nps {} time {} hashfull {} pv {}",
info.depth,
info.score,
info.nodes,
nps,
elapsed_ms,
info.hashfull,
move_to_usi(m)
);
}
let best = info
.best_move
.map(move_to_usi)
.unwrap_or_else(|| "resign".to_string());
// Probe TT for predicted opponent reply to offer GUI a ponder move.
let ponder_token = info.best_move.and_then(|m| {
let token = board2.do_move(m);
let pm = searcher2.probe_tt(board2.hash());
board2.undo_move(token);
pm
});
// "resign" (info.best_move == None) is a special
// response, not a move -- excluded from the legality
// check by construction.
if let Some(mv) = info.best_move {
invariant::assert_legal_bestmove(&board2, mv, &diag_ctx);
}
if let Some(pm) = ponder_token {
println!("bestmove {best} ponder {}", move_to_usi(pm));
} else {
println!("bestmove {best}");
}
io::stdout().lock().flush().ok();
}));
}
"stop" => {
abort_and_join_inflight_search(&mut search_abort, &mut search_handle);
}
"ponderhit" => {
// Suppress bestmove from dying ponder thread, abort it, then restart
// with the original go-ponder time args (opponent's clock hasn't ticked).
suppress_bm.store(true, Ordering::Relaxed);
abort_and_join_inflight_search(&mut search_abort, &mut search_handle);
// Reset suppress before launching the real timed search.
suppress_bm.store(false, Ordering::Relaxed);
if let Some(ref args) = ponder_go_args.take() {
let config =
parse_go(args, board.side_to_move, move_overhead_ms, false, multi_pv);
let abort = searcher.abort_flag();
search_abort = Some(abort);
let searcher2 = Arc::clone(&searcher);
let mut board2 = board.clone();
let suppress2 = Arc::clone(&suppress_bm);
let diag_ctx = DiagCtx {
game_counter,
last_position_cmd: last_position_cmd.clone(),
weight_path: weight_path.clone(),
weight_hash,
threads,
board_hash_at_search_start: board.hash(),
accumulator_hash_at_search_start: invariant::hash_accumulator(&board.acc),
};
search_handle = Some(std::thread::spawn(move || {
let info = searcher2.search(&mut board2, config);
if suppress2.load(Ordering::Relaxed) {
return;
}
let elapsed_ms = info.elapsed.as_millis().max(1) as u64;
let nps = info.nodes.saturating_mul(1000) / elapsed_ms;
if let Some(m) = info.best_move {
println!(
"info depth {} score cp {} nodes {} nps {} time {} hashfull {} pv {}",
info.depth,
info.score,
info.nodes,
nps,
elapsed_ms,
info.hashfull,
move_to_usi(m)
);
}
let best = info
.best_move
.map(move_to_usi)
.unwrap_or_else(|| "resign".to_string());
let ponder_token = info.best_move.and_then(|m| {
let token = board2.do_move(m);
let pm = searcher2.probe_tt(board2.hash());
board2.undo_move(token);
pm
});
if let Some(mv) = info.best_move {
invariant::assert_legal_bestmove(&board2, mv, &diag_ctx);
}
if let Some(pm) = ponder_token {
println!("bestmove {best} ponder {}", move_to_usi(pm));
} else {
println!("bestmove {best}");
}
io::stdout().lock().flush().ok();
}));
}
}
"gameover" => {}
"quit" => {
abort_and_join_inflight_search(&mut search_abort, &mut search_handle);
break;
}
_ => {
eprintln!("unknown command: '{cmd}'");
}
}
}
}
// ---- Helpers ----
fn make_searcher(hash_mb: usize, spec_top_n: usize) -> Arc<SpeculativeSearcher> {
Arc::new(SpeculativeSearcher::new(Tt::new(hash_mb), spec_top_n))
}
// ---- Go command time-control parsing ----
fn parse_go(
args: &str,
side: Color,
overhead_ms: u64,
pondering: bool,
multi_pv: u32,
) -> SearchConfig {
let mut btime: Option<u64> = None;
let mut wtime: Option<u64> = None;
let mut byoyomi: Option<u64> = None;
let mut binc: Option<u64> = None;
let mut winc: Option<u64> = None;
let mut movestogo: Option<u64> = None;
let mut movetime: Option<u64> = None;
let mut depth: Option<u32> = None;
let mut infinite = false;
let tokens: Vec<&str> = args.split_whitespace().collect();
let mut i = 0;
while i < tokens.len() {
match tokens[i] {
"btime" => {
i += 1;
btime = tokens.get(i).and_then(|s| s.parse().ok());
}
"wtime" => {
i += 1;
wtime = tokens.get(i).and_then(|s| s.parse().ok());
}
"byoyomi" => {
i += 1;
byoyomi = tokens.get(i).and_then(|s| s.parse().ok());
}
"binc" => {
i += 1;
binc = tokens.get(i).and_then(|s| s.parse().ok());
}
"winc" => {
i += 1;
winc = tokens.get(i).and_then(|s| s.parse().ok());
}
"movestogo" => {
i += 1;
movestogo = tokens.get(i).and_then(|s| s.parse().ok());
}
"movetime" => {
i += 1;
movetime = tokens.get(i).and_then(|s| s.parse().ok());
}
"depth" => {
i += 1;
depth = tokens.get(i).and_then(|s| s.parse().ok());
}
"infinite" => {
infinite = true;
}
_ => {}
}
i += 1;
}
let has_clock = btime.is_some() || wtime.is_some() || byoyomi.is_some() || movetime.is_some();
let (time_limit, soft_limit) = if infinite || pondering {
(None, None)
} else if let Some(mt) = movetime {
(
Some(Duration::from_millis(
mt.saturating_sub(overhead_ms).max(50),
)),
None,
)
} else if depth.is_some() && !has_clock {
(None, None) // pure depth search — no time cap
} else if has_clock {
let our_time = match side {
Color::Black => btime.unwrap_or(0),
Color::White => wtime.unwrap_or(0),
};
let increment = match side {
Color::Black => binc.unwrap_or(0),
Color::White => winc.unwrap_or(0),
};
let byo_ms = byoyomi.unwrap_or(0);
let effective_time = our_time.saturating_add(increment);
let moves_left = movestogo.unwrap_or(30).max(1);
let from_main = effective_time / moves_left;
let from_byo = byo_ms * 13 / 20;
// Panic mode: if under 5 s and byoyomi exists, lean on byoyomi only
let panic = our_time < 5_000 && byo_ms > 0;
let base = if panic {
from_byo
} else {
from_main.max(from_byo)
};
let base = base.saturating_sub(overhead_ms).max(50);
// Cap hard limit at byoyomi - overhead to avoid time-loss on byoyomi clocks
let byo_safe = byo_ms.saturating_sub(overhead_ms).max(50);
let hard_ms = if byo_ms > 0 {
(base * 3 / 2).min(byo_safe)
} else {
base * 3 / 2
}
.max(50);
let soft_ms = base * 4 / 5;
let hard = Some(Duration::from_millis(hard_ms));
let soft = if !panic {
Some(Duration::from_millis(soft_ms))
} else {
None
};
(hard, soft)
} else {
(None, None) // bare `go` with no args → infinite
};
SearchConfig {
max_depth: depth.unwrap_or(50),
time_limit,
soft_limit,
multi_pv,
}
}
#[cfg(test)]
mod tests {
use super::*;
use sekirei_core::color::Color;
#[test]
fn parse_go_binc_winc() {
// Black has 60s + 1s increment: effective_time = 61000, moves_left=30
// base = 61000/30 = 2033, hard = 2033*3/2 = 3049, soft = 2033*4/5 = 1626
let cfg = parse_go(
"btime 60000 wtime 60000 binc 1000 winc 1000",
Color::Black,
0,
false,
1,
);
assert!(cfg.time_limit.is_some(), "hard limit should be set");
assert!(cfg.soft_limit.is_some(), "soft limit should be set");
let hard = cfg.time_limit.unwrap().as_millis();
let soft = cfg.soft_limit.unwrap().as_millis();
assert!(soft < hard, "soft_limit must be less than hard time_limit");
}
#[test]
fn parse_go_movestogo() {
// 60s, movestogo=20 → from_main = 60000/20 = 3000
let cfg = parse_go(
"btime 60000 wtime 60000 movestogo 20",
Color::Black,
0,
false,
1,
);
let hard = cfg.time_limit.unwrap().as_millis();
// base = 3000, hard = 4500
assert!((hard as i64 - 4500).abs() < 100, "hard={hard}");
}
#[test]
fn parse_go_byoyomi_only() {
// byoyomi 5000, no main time → panic mode, no soft limit
// byo_safe = 5000, base = 3250 - 0 = 3250, hard = min(4875, 5000) = 4875
let cfg = parse_go("byoyomi 5000", Color::Black, 0, false, 1);
assert!(cfg.time_limit.is_some());
assert!(cfg.soft_limit.is_none(), "panic mode: no soft limit");
let hard = cfg.time_limit.unwrap().as_millis();
assert!(hard <= 5000, "hard={hard} must not exceed byoyomi");
}
#[test]
fn parse_go_soft_less_than_hard() {
// Normal case: ample time, no panic
let cfg = parse_go("btime 120000 wtime 120000", Color::Black, 0, false, 1);
let hard = cfg.time_limit.unwrap().as_millis();
let soft = cfg.soft_limit.unwrap().as_millis();
assert!(soft < hard, "soft={soft} hard={hard}");
}
#[test]
fn byoyomi_hard_within_overhead() {
// byoyomi 5000, overhead 300 → hard must be <= byo - overhead = 4700
let cfg = parse_go("byoyomi 5000", Color::Black, 300, false, 1);
let hard = cfg.time_limit.unwrap().as_millis();
assert!(hard <= 4700, "hard={hard} exceeds byoyomi - overhead");
}
#[test]
fn pondering_no_limits() {
let cfg = parse_go("btime 60000 wtime 60000 ponder", Color::Black, 50, true, 1);
assert!(cfg.time_limit.is_none());
assert!(cfg.soft_limit.is_none());
}
#[test]
fn movetime_overhead_deducted() {
// movetime 1000, overhead 50 → hard = 950
let cfg = parse_go("movetime 1000", Color::Black, 50, false, 1);
let hard = cfg.time_limit.unwrap().as_millis();
assert!(hard <= 950, "hard={hard}");
assert!(cfg.soft_limit.is_none());
}
}