katago-analysis 0.1.2

A wrapper around KataGo's analysis engine.
Documentation
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
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
use std::{
    collections::HashMap,
    ops::{ControlFlow, Deref},
    sync::Arc,
};

use tokio::{
    process::{Child, ChildStderr},
    sync::{Notify, RwLock, RwLockReadGuard},
};
use tokio_stream::StreamExt;

use crate::{
    engine::{Engine, EngineStdin, EngineStdout},
    *,
};

/// An instance of the KataGo analysis engine, launched as a child process.
///
/// Drop this to close the engine's stdin and request KataGo to exit.
/// Responses will continue to be processed until the engine actually exits.
pub struct Analyzer<W: WarningHandling = WarningsAsErrors> {
    stdin: EngineStdin,

    /// The analysis engine's stderr output, if available.
    pub stderr: Option<ChildStderr>,

    /// The engine process.
    pub child_process: Child,

    next_id: u32,
    pending_requests: Arc<RwLock<PendingRequests<W>>>,
}

impl<W: WarningHandling> Analyzer<W> {
    /// Analyzes the final position in the game and returns a single result.
    pub async fn analyze(
        &mut self,
        request: AnalysisRequest,
    ) -> WarningResult<Option<AnalysisResult>, W> {
        self.start_analyze(request).await?.finish().await
    }

    /// Analyzes a specific position in the game and returns a single result.
    pub async fn analyze_position(
        &mut self,
        request: AnalysisRequest,
        position: usize,
    ) -> WarningResult<Option<AnalysisResult>, W> {
        self.start_analyze_position(request, position)
            .await?
            .finish()
            .await
    }

    /// Analyzes all moves in the game and returns a collection of results, one for each position.
    pub async fn analyze_game(
        &mut self,
        request: AnalysisRequest,
    ) -> WarningResult<HashMap<usize, AnalysisResult>, W> {
        self.start_analyze_game(request).await?.finish().await
    }

    /// Analyzes the specified positions in the game and returns a collection of results, one for each position.
    pub async fn analyze_positions(
        &mut self,
        request: AnalysisRequest,
        analyze_turns: Vec<usize>,
    ) -> WarningResult<HashMap<usize, AnalysisResult>, W> {
        self.start_analyze_positions(request, analyze_turns)
            .await?
            .finish()
            .await
    }

    /// Starts analyzing the final position in the game and returns a progress object which can be polled for updates.
    pub async fn start_analyze(&mut self, request: AnalysisRequest) -> Result<AnalysisProgress<W>> {
        let position = request.moves.len();
        self.start_analyze_position(request, position).await
    }

    /// Starts analyzing a specific position in the game and returns a progress object which can be polled for updates.
    pub async fn start_analyze_position(
        &mut self,
        request: AnalysisRequest,
        position: usize,
    ) -> Result<AnalysisProgress<W>> {
        Ok(self
            .start_analyze_positions(request, vec![position])
            .await?
            .into_positions()
            .remove(&position)
            .expect("position analysis should be available"))
    }

    /// Starts analyzing all moves in the game and returns a collection of progress objects.
    pub async fn start_analyze_game(
        &mut self,
        request: AnalysisRequest,
    ) -> Result<GameAnalysisProgress<W>> {
        let positions = (0..=request.moves.len()).collect();
        self.start_analyze_positions(request, positions).await
    }

    /// Starts analyzing the specified positions in the game and returns a collection of progress objects.
    pub async fn start_analyze_positions(
        &mut self,
        request: AnalysisRequest,
        analyze_turns: Vec<usize>,
    ) -> Result<GameAnalysisProgress<W>> {
        self.start_analyze_positions_impl(request, analyze_turns, None)
            .await
    }

    /// Analyzes all moves in the game with the given priorities and returns a collection of results, one for each
    /// position.
    ///
    /// `priorities` must have length equal to one more than the number of moves in the game.
    pub async fn analyze_game_prioritized(
        &mut self,
        request: AnalysisRequest,
        priorities: Vec<i32>,
    ) -> WarningResult<HashMap<usize, AnalysisResult>, W> {
        self.start_analyze_game_prioritized(request, priorities)
            .await?
            .finish()
            .await
    }

    /// Analyzes the specified positions in the game with the given priorities and returns a collection of results,
    /// one for each position.
    ///
    /// `priorities` must have the same length as `analyze_turns`.
    pub async fn analyze_positions_prioritized(
        &mut self,
        request: AnalysisRequest,
        analyze_turns: Vec<usize>,
        priorities: Vec<i32>,
    ) -> WarningResult<HashMap<usize, AnalysisResult>, W> {
        self.start_analyze_positions_prioritized(request, analyze_turns, priorities)
            .await?
            .finish()
            .await
    }

    /// Starts analyzing all moves in the game with the given priorities and returns a collection of progress objects.
    ///
    /// `priorities` must have length equal to one more than the number of moves in the game.
    pub async fn start_analyze_game_prioritized(
        &mut self,
        request: AnalysisRequest,
        priorities: Vec<i32>,
    ) -> Result<GameAnalysisProgress<W>> {
        let positions = (0..=request.moves.len()).collect();
        self.start_analyze_positions_prioritized(request, positions, priorities)
            .await
    }

    /// Starts analyzing the specified positions in the game with the given priorities and returns a collection of
    /// progress objects.
    ///
    /// `priorities` must have the same length as `analyze_turns`.
    pub async fn start_analyze_positions_prioritized(
        &mut self,
        request: AnalysisRequest,
        analyze_turns: Vec<usize>,
        priorities: Vec<i32>,
    ) -> Result<GameAnalysisProgress<W>> {
        self.start_analyze_positions_impl(request, analyze_turns, Some(priorities))
            .await
    }

    async fn start_analyze_positions_impl(
        &mut self,
        request: AnalysisRequest,
        analyze_turns: Vec<usize>,
        priorities: Option<Vec<i32>>,
    ) -> Result<GameAnalysisProgress<W>> {
        let id = self.generate_id();
        let mut senders = HashMap::new();
        let mut positions = HashMap::new();
        for position in &analyze_turns {
            let (sender, receiver) = channel(W::ok(None));
            senders.insert(*position, sender);
            positions.insert(
                *position,
                AnalysisProgress::<W> {
                    receiver,
                    id: id.clone(),
                    turn_number: *position,
                },
            );
        }

        let pending_request = PendingRequest::<W> {
            positions: senders,
            width: request.board_x_size,
            height: request.board_y_size,
        };

        let mut pending = self.pending_requests.write().await;
        self.stdin
            .send(&engine::Request::Analyze(request.into_engine_request(
                id.clone(),
                analyze_turns,
                priorities,
            )))
            .await?;
        pending.requests.insert(id.clone(), pending_request);
        Ok(GameAnalysisProgress::<W> { id, positions })
    }

    /// Requests KataGo's version information.
    pub async fn query_version(&mut self) -> WarningResult<VersionInfo, W> {
        let id = self.generate_id();
        let (sender, receiver) = channel(W::ok(VersionInfo {
            version: String::new(),
            git_hash: String::new(),
        }));

        let mut pending = self.pending_requests.write().await;
        self.stdin
            .send(&engine::Request::QueryVersion { id: id.clone() })
            .await?;
        pending.query_version_requests.insert(id, sender);
        drop(pending);

        receiver.finish().await
    }

    /// Clears the neural network cache.
    pub async fn clear_cache(&mut self) -> WarningResult<(), W> {
        let id = self.generate_id();
        let (sender, receiver) = channel(W::ok(()));

        let mut pending = self.pending_requests.write().await;
        self.stdin
            .send(&engine::Request::ClearCache { id: id.clone() })
            .await?;
        pending.clear_cache_requests.insert(id, sender);
        drop(pending);

        receiver.finish().await
    }

    /// Terminates the analysis for a single position.
    ///
    /// `progress` may still be used to wait for the final result.
    pub async fn terminate(&mut self, progress: &AnalysisProgress) -> WarningResult<(), W> {
        self.terminate_impl(progress.id.clone(), Some(vec![progress.turn_number]))
            .await
    }

    /// Terminates the analysis for all positions in a game.
    ///
    /// `progress` may still be used to wait for the final results.
    pub async fn terminate_game(
        &mut self,
        progress: &GameAnalysisProgress,
    ) -> WarningResult<(), W> {
        self.terminate_impl(progress.id.clone(), None).await
    }

    /// Terminates the analysis for the specified positions in a game.
    ///
    /// `progress` may still be used to wait for the final results.
    pub async fn terminate_positions(
        &mut self,
        progress: &GameAnalysisProgress,
        turn_numbers: Vec<usize>,
    ) -> WarningResult<(), W> {
        self.terminate_impl(progress.id.clone(), Some(turn_numbers))
            .await
    }

    async fn terminate_impl(
        &mut self,
        terminate_id: String,
        turn_numbers: Option<Vec<usize>>,
    ) -> WarningResult<(), W> {
        let id = self.generate_id();
        let (sender, receiver) = channel(W::ok(()));

        let mut pending = self.pending_requests.write().await;
        self.stdin
            .send(&engine::Request::Terminate {
                id: id.clone(),
                terminate_id,
                turn_numbers,
            })
            .await?;
        pending.terminate_requests.insert(id, sender);
        drop(pending);

        receiver.finish().await
    }

    /// Terminates all pending analysis requests.
    pub async fn terminate_all(&mut self) -> WarningResult<(), W> {
        self.terminate_all_impl(None).await
    }

    /// Terminates all pending analysis requests for the specified positions.
    pub async fn terminate_all_positions(
        &mut self,
        turn_numbers: Vec<usize>,
    ) -> WarningResult<(), W> {
        self.terminate_all_impl(Some(turn_numbers)).await
    }

    async fn terminate_all_impl(
        &mut self,
        turn_numbers: Option<Vec<usize>>,
    ) -> WarningResult<(), W> {
        let id = self.generate_id();
        let (sender, receiver) = channel(W::ok(()));

        let mut pending = self.pending_requests.write().await;
        self.stdin
            .send(&engine::Request::TerminateAll {
                id: id.clone(),
                turn_numbers,
            })
            .await?;
        pending.terminate_all_requests.insert(id, sender);
        drop(pending);

        receiver.finish().await
    }

    /// Requests information about the available neural network models.
    pub async fn query_models(&mut self) -> WarningResult<Vec<Model>, W> {
        let id = self.generate_id();
        let (sender, receiver) = channel(W::ok(vec![]));

        let mut pending = self.pending_requests.write().await;
        self.stdin
            .send(&engine::Request::QueryModels { id: id.clone() })
            .await?;
        pending.query_models_requests.insert(id, sender);
        drop(pending);

        receiver.finish().await
    }

    fn generate_id(&mut self) -> String {
        let id = self.next_id.to_string();
        self.next_id += 1;
        id
    }
}

impl<W: WarningHandling + Default + Clone + 'static> From<Engine> for Analyzer<W>
where
    W::OkType<Option<AnalysisResult>>: Send + Sync,
    W::OkType<VersionInfo>: Send + Sync,
    W::OkType<()>: Send + Sync,
    W::OkType<Vec<Model>>: Send + Sync,
{
    fn from(engine: Engine) -> Self {
        let client = Self {
            stdin: engine.stdin,
            stderr: engine.stderr,
            child_process: engine.child_process,
            next_id: 1,
            pending_requests: Arc::default(),
        };

        tokio::spawn(handle_responses(
            engine.stdout,
            client.pending_requests.clone(),
        ));

        client
    }
}

async fn handle_responses<W: WarningHandling>(
    mut stdout: EngineStdout,
    pending: Arc<RwLock<PendingRequests<W>>>,
) {
    while let Some(response) = stdout.next().await {
        let response = match response {
            Ok(response) => response,
            Err(e) => {
                pending.write().await.poison_all(e).await;
                continue;
            }
        };
        match response {
            engine::Response::Analyze(response) => {
                let id = response.id.clone();
                let turn_number = response.turn_number;
                let is_during_search = response.is_during_search;
                let mut pending = pending.write().await;
                if let Some(request) = pending.requests.get_mut(&id) {
                    if let Some(sender) = request.positions.get(&turn_number) {
                        let result = Some(AnalysisResult::from_engine_response(
                            response,
                            request.width,
                            request.height,
                        ));
                        sender.send_modify(|r| W::set_result(r, result)).await;

                        if !is_during_search {
                            request.positions.remove(&turn_number);
                        }
                    }
                    if request.positions.is_empty() {
                        pending.requests.remove(&id);
                    }
                }
            }
            engine::Response::NoResults { id, turn_number } => {
                let mut pending = pending.write().await;
                if let Some(request) = pending.requests.get_mut(&id) {
                    request.positions.remove(&turn_number);
                    if request.positions.is_empty() {
                        pending.requests.remove(&id);
                    }
                }
            }
            engine::Response::QueryVersion {
                id,
                version,
                git_hash,
            } => {
                if let Some(sender) = pending.write().await.query_version_requests.remove(&id) {
                    sender
                        .send_modify(|r| W::set_result(r, VersionInfo { version, git_hash }))
                        .await;
                }
            }
            engine::Response::ClearCache { id } => {
                if let Some(sender) = pending.write().await.clear_cache_requests.remove(&id) {
                    sender.send_modify(|r| W::set_result(r, ())).await;
                }
            }
            engine::Response::Terminate { id, .. } => {
                if let Some(sender) = pending.write().await.terminate_requests.remove(&id) {
                    sender.send_modify(|r| W::set_result(r, ())).await;
                }
            }
            engine::Response::TerminateAll { id, .. } => {
                if let Some(sender) = pending.write().await.terminate_all_requests.remove(&id) {
                    sender.send_modify(|r| W::set_result(r, ())).await;
                }
            }
            engine::Response::QueryModels { id, models } => {
                if let Some(sender) = pending.write().await.query_models_requests.remove(&id) {
                    sender.send_modify(|r| W::set_result(r, models)).await;
                }
            }
            engine::Response::GeneralError { error } => {
                pending
                    .write()
                    .await
                    .poison_all(Error::KataGoGeneralError { error })
                    .await;
            }
            engine::Response::FieldError { id, error, field } => {
                pending
                    .write()
                    .await
                    .poison(&id, Error::KataGoFieldError { error, field })
                    .await;
            }
            engine::Response::FieldWarning { id, warning, field } => {
                pending
                    .write()
                    .await
                    .add_warning(&id, Warning { warning, field })
                    .await;
            }
        };
    }
    let mut pending = pending.write().await;
    pending.requests.clear();
    pending.query_version_requests.clear();
    pending.clear_cache_requests.clear();
    pending.terminate_requests.clear();
    pending.terminate_all_requests.clear();
    pending.query_models_requests.clear();
}

impl<W: WarningHandling> std::fmt::Debug for Analyzer<W>
where
    W::OkType<Option<AnalysisResult>>: std::fmt::Debug,
    W::OkType<VersionInfo>: std::fmt::Debug,
    W::OkType<()>: std::fmt::Debug,
    W::OkType<Vec<Model>>: std::fmt::Debug,
{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Analyzer")
            .field("stdin", &self.stdin)
            .field("stderr", &self.stderr)
            .field("child_process", &self.child_process)
            .field("next_id", &self.next_id)
            .field("pending_requests", &self.pending_requests)
            .finish()
    }
}

#[derive(Default)]
struct PendingRequests<W: WarningHandling = WarningsAsErrors> {
    requests: HashMap<String, PendingRequest<W>>,
    query_version_requests: HashMap<String, Sender<WarningResult<VersionInfo, W>>>,
    clear_cache_requests: HashMap<String, Sender<WarningResult<(), W>>>,
    terminate_requests: HashMap<String, Sender<WarningResult<(), W>>>,
    terminate_all_requests: HashMap<String, Sender<WarningResult<(), W>>>,
    query_models_requests: HashMap<String, Sender<WarningResult<Vec<Model>, W>>>,
}

impl<W: WarningHandling> PendingRequests<W> {
    async fn poison_all(&mut self, error: Error) {
        for (_, request) in self.requests.drain() {
            for sender in request.positions.values() {
                sender.send_err(error.clone()).await;
            }
        }

        for (_, sender) in self.query_version_requests.drain() {
            sender.send_err(error.clone()).await;
        }

        for (_, sender) in self.clear_cache_requests.drain() {
            sender.send_err(error.clone()).await;
        }

        for (_, sender) in self.terminate_requests.drain() {
            sender.send_err(error.clone()).await;
        }

        for (_, sender) in self.terminate_all_requests.drain() {
            sender.send_err(error.clone()).await;
        }

        for (_, sender) in self.query_models_requests.drain() {
            sender.send_err(error.clone()).await;
        }
    }

    async fn poison(&mut self, id: &str, error: Error) {
        if let Some(request) = self.requests.remove(id) {
            for sender in request.positions.values() {
                sender.send_err(error.clone()).await;
            }
        }

        if let Some(sender) = self.query_version_requests.remove(id) {
            sender.send_err(error.clone()).await;
        }

        if let Some(sender) = self.clear_cache_requests.remove(id) {
            sender.send_err(error.clone()).await;
        }

        if let Some(sender) = self.terminate_requests.remove(id) {
            sender.send_err(error.clone()).await;
        }

        if let Some(sender) = self.terminate_all_requests.remove(id) {
            sender.send_err(error.clone()).await;
        }

        if let Some(sender) = self.query_models_requests.remove(id) {
            sender.send_err(error.clone()).await;
        }
    }

    async fn add_warning(&mut self, id: &str, warning: Warning) {
        if let Some(request) = self.requests.get(id) {
            for sender in request.positions.values() {
                sender
                    .send_modify(|r| W::add_warning(r, warning.clone()))
                    .await;
            }
        }

        if let Some(sender) = self.query_version_requests.get(id) {
            sender
                .send_modify(|r| W::add_warning(r, warning.clone()))
                .await;
        }

        if let Some(sender) = self.clear_cache_requests.get(id) {
            sender
                .send_modify(|r| W::add_warning(r, warning.clone()))
                .await;
        }

        if let Some(sender) = self.terminate_requests.get(id) {
            sender
                .send_modify(|r| W::add_warning(r, warning.clone()))
                .await;
        }

        if let Some(sender) = self.terminate_all_requests.get(id) {
            sender
                .send_modify(|r| W::add_warning(r, warning.clone()))
                .await;
        }

        if let Some(sender) = self.query_models_requests.get(id) {
            sender
                .send_modify(|r| W::add_warning(r, warning.clone()))
                .await;
        }
    }
}

impl<W: WarningHandling> std::fmt::Debug for PendingRequests<W>
where
    W::OkType<Option<AnalysisResult>>: std::fmt::Debug,
    W::OkType<VersionInfo>: std::fmt::Debug,
    W::OkType<()>: std::fmt::Debug,
    W::OkType<Vec<Model>>: std::fmt::Debug,
{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("PendingRequests")
            .field("requests", &self.requests)
            .field("query_version_requests", &self.query_version_requests)
            .field("clear_cache_requests", &self.clear_cache_requests)
            .field("terminate_requests", &self.terminate_requests)
            .field("terminate_all_requests", &self.terminate_all_requests)
            .field("query_models_requests", &self.query_models_requests)
            .finish()
    }
}

struct PendingRequest<W: WarningHandling = WarningsAsErrors> {
    positions: HashMap<usize, Sender<WarningResult<Option<AnalysisResult>, W>>>,
    width: u8,
    height: u8,
}

impl<W: WarningHandling> std::fmt::Debug for PendingRequest<W>
where
    W::OkType<Option<AnalysisResult>>: std::fmt::Debug,
{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("PendingRequest")
            .field("positions", &self.positions)
            .field("height", &self.height)
            .finish()
    }
}

#[derive(Debug)]
struct NotifyOnDrop(Arc<Notify>);

impl Drop for NotifyOnDrop {
    fn drop(&mut self) {
        self.0.notify_one();
    }
}

impl Deref for NotifyOnDrop {
    type Target = Arc<Notify>;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

/// The sender half of a single-producer single-consumer watch channel.
///
/// When dropped, is guaranteed to notify the receiver after the last value is sent.
#[derive(Debug)]
struct Sender<T> {
    value: Arc<RwLock<T>>,
    notify: NotifyOnDrop,
}

impl<T> Sender<T> {
    async fn send_modify(&self, f: impl FnOnce(&mut T)) {
        f(&mut *self.value.write().await);
        self.notify.notify_one();
    }
}

impl<T, E> Sender<std::result::Result<T, E>> {
    async fn send_err(&self, value: E) {
        self.send_modify(|r| *r = Err(value)).await;
    }
}

/// The receiver half of a single-producer single-consumer watch channel.
#[derive(Debug)]
struct Receiver<T> {
    value: Arc<RwLock<T>>,
    notify: Arc<Notify>,
}

impl<T> Receiver<T> {
    async fn finish(mut self) -> T {
        loop {
            match self.poll().await {
                ControlFlow::Break(value) => return value,
                ControlFlow::Continue(s) => self = s,
            };
        }
    }

    async fn poll(self) -> ControlFlow<T, Self> {
        self.notify.notified().await;
        match Arc::try_unwrap(self.value) {
            Ok(value) => ControlFlow::Break(value.into_inner()),
            Err(arc) => ControlFlow::Continue(Self { value: arc, ..self }),
        }
    }

    async fn read(&self) -> RwLockReadGuard<'_, T> {
        self.value.read().await
    }
}

/// Creates a single-producer single-consumer watch channel with the given initial value.
fn channel<T>(value: T) -> (Sender<T>, Receiver<T>) {
    let receiver = Receiver {
        value: Arc::new(RwLock::new(value)),
        notify: Arc::new(Notify::new()),
    };
    let sender = Sender {
        value: receiver.value.clone(),
        notify: NotifyOnDrop(receiver.notify.clone()),
    };
    (sender, receiver)
}

/// A collection of in-progress analysis operations for multiple positions in a single game.
pub struct GameAnalysisProgress<W: WarningHandling = WarningsAsErrors> {
    id: String,
    positions: HashMap<usize, AnalysisProgress<W>>,
}

impl<W: WarningHandling> GameAnalysisProgress<W> {
    /// Waits for all positions to finish analyzing and returns the results.
    ///
    /// Positions that were terminated before any search was performed will not be included in the results.
    pub async fn finish(self) -> WarningResult<HashMap<usize, AnalysisResult>, W> {
        let mut results = W::ok(HashMap::new());
        for (position, progress) in self.into_positions().into_iter() {
            let result = progress.finish().await;
            results = W::merge(results, result, |mut results, result| {
                if let Some(result) = result {
                    results.insert(position, result);
                }
                results
            });
        }
        results
    }

    /// Returns a reference to the raw collection of in-progress analysis operations for each position.
    pub fn positions(&self) -> &HashMap<usize, AnalysisProgress<W>> {
        &self.positions
    }

    /// Returns a mutable reference to the raw collection of in-progress analysis operations for each position.
    pub fn positions_mut(&mut self) -> &mut HashMap<usize, AnalysisProgress<W>> {
        &mut self.positions
    }

    /// Extracts the collection of in-progress analysis operations for each position and consumes this object.
    pub fn into_positions(self) -> HashMap<usize, AnalysisProgress<W>> {
        self.positions
    }
}

impl<W: WarningHandling> std::fmt::Debug for GameAnalysisProgress<W>
where
    W::OkType<Option<AnalysisResult>>: std::fmt::Debug,
{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("GameAnalysisProgress")
            .field("id", &self.id)
            .field("positions", &self.positions)
            .finish()
    }
}

/// An in-progress analysis operation for a single position.
pub struct AnalysisProgress<W: WarningHandling = WarningsAsErrors> {
    receiver: Receiver<WarningResult<Option<AnalysisResult>, W>>,
    id: String,
    turn_number: usize,
}

impl<W: WarningHandling> AnalysisProgress<W> {
    /// Waits for the analysis to finish and returns the result.
    ///
    /// If the analysis was terminated before any search was performed, returns `Ok(None)`.
    pub async fn finish(self) -> WarningResult<Option<AnalysisResult>, W> {
        self.receiver.finish().await
    }

    /// Waits for an analysis update.
    ///
    /// This is mainly useful when using [`AnalysisRequest::report_during_search_every`]. Otherwise, it's simpler to
    /// just call [`finish`](Self::finish) to wait for the final result.
    ///
    /// If the analysis is finished, it consumes this object and returns [`ControlFlow::Break`] containing the final
    /// result. If a partial result is available, it returns [`ControlFlow::Continue`] containing this object again,
    /// which can be read using [`read`](Self::read) or polled again for the next update.
    ///
    /// This method (in combination with [`read`](Self::read)) is conceptually similar to Tokio's
    /// [`watch`](tokio::sync::watch) channel. It provides a way to follow the latest information as it becomes
    /// available without any danger of falling behind.
    ///
    /// # Example
    ///
    /// ```
    /// # use katago_analysis::*;
    /// # use std::ops::ControlFlow;
    /// # async fn example(mut progress: AnalysisProgress) {
    /// loop {
    ///     match progress.poll().await {
    ///         ControlFlow::Break(result) => {
    ///             match result {
    ///                 Ok(Some(result)) => {
    ///                     println!("Winrate: {:.1}%", result.root_info.winrate * 100.0);
    ///                 }
    ///                 Ok(None) => println!("No results"),
    ///                 Err(e) => println!("Error: {e}"),
    ///             }
    ///             break;
    ///         }
    ///         ControlFlow::Continue(p) => {
    ///             progress = p;
    ///             if let Ok(Some(result)) = progress.read().await.as_ref() {
    ///                 println!(
    ///                     "Winrate: {:.1}% Visits: {}",
    ///                     result.root_info.winrate * 100.0,
    ///                     result.root_info.visits
    ///                 );
    ///             }
    ///         }
    ///     };
    /// }
    /// # }
    /// ```
    pub async fn poll(self) -> ControlFlow<WarningResult<Option<AnalysisResult>, W>, Self> {
        self.receiver.poll().await.map_continue(|r| Self {
            receiver: r,
            ..self
        })
    }

    /// Reads the latest analysis result available.
    ///
    /// See also: [`poll`](Self::poll)
    pub async fn read(&self) -> RwLockReadGuard<'_, WarningResult<Option<AnalysisResult>, W>> {
        self.receiver.read().await
    }
}

impl<W: WarningHandling> std::fmt::Debug for AnalysisProgress<W>
where
    W::OkType<Option<AnalysisResult>>: std::fmt::Debug,
{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("AnalysisProgress")
            .field("receiver", &self.receiver)
            .field("id", &self.id)
            .field("turn_number", &self.turn_number)
            .finish()
    }
}