mctrust 0.4.0

Universal search & planning toolkit — MCTS, bandit search, pluggable evaluators, tree reuse, DAG transpositions, root parallelism. Define an Environment, search handles the rest.
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
//! Bandit-based MCTS — explore/exploit search for flat action spaces with RAVE.
//!
//! Unlike [`crate::TreeSearch`] which builds a search tree, [`BanditSearch`]
//! operates on a pre-enumerated set of "arms" grouped by category.
//! It uses UCT + RAVE to decide which arm to pull next, and you
//! feed back rewards.
//!
//! # Use Cases
//!
//! - **Fuzzing**: arms are test inputs, groups are mutation categories
//! - **Hyperparameter search**: arms are configurations, groups are strategy families
//! - **Security testing**: arms are probes, groups are methods/endpoints
//! - **A/B testing**: arms are variants, groups are features

pub mod config;
mod node;

#[cfg(test)]
mod tests;

#[allow(unused_imports)] // Re-exported for downstream consumers
pub use config::{BanditConfig, BanditConfigBuilder, Scalarizer};
use node::BanditNode;
pub use node::GroupStats;

use std::collections::HashMap;

use rand::seq::SliceRandom;
use rand::SeedableRng;
use rand_chacha::ChaCha8Rng;

///
/// Arms are grouped by category. The engine maintains a two-level tree:
/// root → group nodes → arm selection within groups.
///
/// # Usage Flow
///
/// 1. Create with [`BanditSearch::new`].
/// 2. Register arms via [`add_arm`](BanditSearch::add_arm).
/// 3. Loop: call [`next_arm`](BanditSearch::next_arm) → evaluate → [`observe`](BanditSearch::observe).
/// 4. Query statistics via [`group_stats`](BanditSearch::group_stats).
pub struct BanditSearch {
    config: BanditConfig,

    /// Flat node arena. Index 0 is root.
    nodes: Vec<BanditNode>,

    /// Group ID → node index mapping.
    group_to_node: HashMap<u32, u32>,

    /// Arm ID → (node index) for backpropagation.
    arm_to_node: HashMap<u64, u32>,

    /// Total pulls executed.
    pulls_executed: u64,

    /// Fast, deterministic, `no_std`-compatible RNG for tie-breaking.
    rng: ChaCha8Rng,
}

/// Serializable checkpoint for restoring bandit search progress.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct BanditSearchCheckpoint {
    /// Search configuration at checkpoint time.
    pub(crate) config: BanditConfig,
    /// Full two-level tree snapshot.
    nodes: Vec<BanditNode>,
    /// Group mapping.
    group_to_node: HashMap<u32, u32>,
    /// Arm mapping.
    arm_to_node: HashMap<u64, u32>,
    /// Total pulls executed.
    pulls_executed: u64,
}

impl BanditSearch {
    /// Creates a new bandit search engine with an entropy-seeded RNG.
    ///
    /// # Parameters
    ///
    /// - `config`: Search hyperparameters controlling exploration, RAVE, and pull budget.
    ///
    /// # Returns
    ///
    /// Returns an empty [`BanditSearch`] with only the root node initialized.
    ///
    /// # Panics
    ///
    /// This function does not panic.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use mctrust::{BanditConfig, BanditSearch};
    ///
    /// let search = BanditSearch::new(BanditConfig::default());
    /// assert_eq!(search.total_pulls(), 0);
    /// ```
    pub fn new(config: BanditConfig) -> Self {
        Self::with_seed(config, entropy_rng())
    }

    /// Creates a new bandit search with a deterministic RNG seed.
    ///
    /// # Parameters
    ///
    /// - `config`: Search hyperparameters.
    /// - `seed`: Seed used to initialize the RNG.
    ///
    /// # Returns
    ///
    /// Returns an empty [`BanditSearch`] with deterministic tie-breaking behavior.
    ///
    /// # Panics
    ///
    /// This function does not panic.
    pub fn new_seeded(config: BanditConfig, seed: u64) -> Self {
        Self::with_seed(config, ChaCha8Rng::seed_from_u64(seed))
    }

    fn with_seed(config: BanditConfig, rng: ChaCha8Rng) -> Self {
        // Create root node.
        let root = BanditNode {
            visits: 0,
            reward: 0.0,
            rave_visits: 0,
            rave_reward: 0.0,
            group_id: u32::MAX,
            bias: 0.0,
            children: Vec::new(),
            arms: Vec::new(),
            next_untried: 0,
        };

        Self {
            config,
            nodes: vec![root],
            group_to_node: HashMap::new(),
            arm_to_node: HashMap::new(),
            pulls_executed: 0,
            rng,
        }
    }

    /// Creates a serializable checkpoint of the current bandit search state.
    ///
    /// # Parameters
    ///
    /// This function takes no additional parameters.
    ///
    /// # Returns
    ///
    /// Returns a [`BanditSearchCheckpoint`] containing nodes, mappings, and budget state.
    ///
    /// # Panics
    ///
    /// This function does not panic.
    pub fn checkpoint(&self) -> BanditSearchCheckpoint {
        BanditSearchCheckpoint {
            config: self.config.clone(),
            nodes: self.nodes.clone(),
            group_to_node: self.group_to_node.clone(),
            arm_to_node: self.arm_to_node.clone(),
            pulls_executed: self.pulls_executed,
        }
    }

    /// Restores a bandit search from a checkpoint using a fresh entropy-seeded RNG.
    ///
    /// # Parameters
    ///
    /// - `checkpoint`: Previously captured search state.
    ///
    /// # Returns
    ///
    /// Returns a [`BanditSearch`] resumed from the checkpoint.
    ///
    /// # Panics
    ///
    /// This function does not panic.
    pub fn restore(checkpoint: BanditSearchCheckpoint) -> Self {
        Self {
            config: checkpoint.config,
            nodes: checkpoint.nodes,
            group_to_node: checkpoint.group_to_node,
            arm_to_node: checkpoint.arm_to_node,
            pulls_executed: checkpoint.pulls_executed,
            rng: entropy_rng(),
        }
    }

    /// Restores a bandit search from a checkpoint with a deterministic RNG seed.
    ///
    /// # Parameters
    ///
    /// - `checkpoint`: Previously captured search state.
    /// - `seed`: Seed used for deterministic tie-breaking after restore.
    ///
    /// # Returns
    ///
    /// Returns a [`BanditSearch`] resumed from the checkpoint.
    ///
    /// # Panics
    ///
    /// This function does not panic.
    pub fn restore_with_seed(checkpoint: BanditSearchCheckpoint, seed: u64) -> Self {
        Self {
            config: checkpoint.config,
            nodes: checkpoint.nodes,
            group_to_node: checkpoint.group_to_node,
            arm_to_node: checkpoint.arm_to_node,
            pulls_executed: checkpoint.pulls_executed,
            rng: ChaCha8Rng::seed_from_u64(seed),
        }
    }

    /// Registers an arm with the given group.
    ///
    /// Must be called before the first [`next_arm`](Self::next_arm).
    /// Arms within the same group share RAVE statistics.
    /// # Parameters
    ///
    /// - `arm_id`: Unique identifier for the arm to register.
    /// - `group_id`: Group identifier used for RAVE sharing and group selection.
    ///
    /// # Returns
    ///
    /// This function returns no value.
    ///
    /// # Panics
    ///
    /// This function does not panic.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use mctrust::{BanditConfig, BanditSearch};
    ///
    /// let mut search = BanditSearch::new_seeded(BanditConfig::default(), 7);
    /// search.add_arm(10, 1);
    /// assert_eq!(search.next_arm(), Some(10));
    /// ```
    pub fn add_arm(&mut self, arm_id: u64, group_id: u32) {
        if self.arm_to_node.contains_key(&arm_id) {
            return;
        }

        if self.nodes.len() == u32::MAX as usize {
            return;
        }

        // Lazily create the group node.
        let node_idx = if let Some(&idx) = self.group_to_node.get(&group_id) {
            idx
        } else {
            // Safety: conversion is validated below to avoid truncation.
            let Ok(idx) = u32::try_from(self.nodes.len()) else {
                return;
            };
            self.nodes.push(BanditNode {
                visits: 0,
                reward: 0.0,
                rave_visits: 0,
                rave_reward: 0.0,
                group_id,
                bias: 0.0,
                children: Vec::new(),
                arms: Vec::new(),
                next_untried: 0,
            });
            self.nodes[0].children.push(idx);

            self.group_to_node.insert(group_id, idx);
            idx
        };

        self.nodes[node_idx as usize].arms.push(arm_id);
        self.arm_to_node.insert(arm_id, node_idx);
    }

    /// Selects the next arm to pull using UCT + RAVE.
    ///
    /// Returns `None` when the budget is exhausted or all arms have been tried.
    /// # Parameters
    ///
    /// This function takes no additional parameters.
    ///
    /// # Returns
    ///
    /// Returns the next arm identifier to evaluate, or `None` when the pull budget is
    /// exhausted or every registered arm has been tried.
    ///
    /// # Panics
    ///
    /// This function does not panic.
    pub fn next_arm(&mut self) -> Option<u64> {
        // Budget check.
        if self.config.max_pulls > 0 && self.pulls_executed >= self.config.max_pulls {
            return None;
        }

        // No groups registered.
        let available_groups: Vec<u32> = self
            .nodes
            .first()
            .map(|root| {
                root.children
                    .iter()
                    .copied()
                    .filter(|idx| self.nodes[*idx as usize].has_untried())
                    .collect()
            })
            .unwrap_or_default();
        let mut available_groups = available_groups;
        available_groups.shuffle(&mut self.rng);

        let group_idx = available_groups.iter().copied().max_by(|&a, &b| {
            let sa = self.nodes[a as usize].score(self.nodes[0].visits, &self.config);
            let sb = self.nodes[b as usize].score(self.nodes[0].visits, &self.config);
            let ord = sa.partial_cmp(&sb).unwrap_or(std::cmp::Ordering::Equal);
            if ord == std::cmp::Ordering::Equal {
                self.nodes[a as usize]
                    .bias
                    .partial_cmp(&self.nodes[b as usize].bias)
                    .unwrap_or(std::cmp::Ordering::Equal)
            } else {
                ord
            }
        })?;

        // Expand one untried arm from selected group.
        let node = &mut self.nodes[group_idx as usize];
        if node.next_untried < node.arms.len() {
            let arm_id = node.arms[node.next_untried];
            node.next_untried += 1;
            self.pulls_executed += 1;
            Some(arm_id)
        } else {
            None
        }
    }

    /// Selects up to `n` arms by repeatedly calling [`next_arm`](Self::next_arm).
    ///
    /// This method intentionally provides no atomicity guarantees; it is a best-effort
    /// batch collection for callers that want multiple pull candidates at once.
    ///
    /// # Parameters
    ///
    /// - `n`: Maximum number of arms to select.
    ///
    /// # Returns
    ///
    /// Returns a vector of arm identifiers in selection order.
    ///
    /// # Panics
    ///
    /// This function does not panic.
    pub fn next_arms(&mut self, n: usize) -> Vec<u64> {
        // Compute available arms to cap capacity allocation safely.
        let available_arms = if let Some(root) = self.nodes.first() {
            root.children
                .iter()
                .map(|&idx| {
                    let node = &self.nodes[idx as usize];
                    node.arms.len() - node.next_untried
                })
                .sum::<usize>()
        } else {
            0
        };

        // If config.max_pulls is set, the remaining budget also bounds capacity.
        let remaining_budget = if self.config.max_pulls > 0 {
            // max_pulls is u64, but n is usize, so we cast pulls_executed carefully
            usize::try_from(self.config.max_pulls.saturating_sub(self.pulls_executed))
                .unwrap_or(usize::MAX)
        } else {
            usize::MAX
        };

        let cap = n.min(available_arms).min(remaining_budget);
        let mut arms = Vec::with_capacity(cap);

        for _ in 0..n {
            if let Some(arm_id) = self.next_arm() {
                arms.push(arm_id);
            } else {
                break;
            }
        }
        arms
    }

    /// Reports the reward for a previously pulled arm.
    ///
    /// Triggers backpropagation and RAVE cross-group updates.
    /// # Parameters
    ///
    /// - `arm_id`: Previously selected arm identifier.
    /// - `reward`: Observed reward to backpropagate.
    ///
    /// # Returns
    ///
    /// This function returns no value.
    ///
    /// # Panics
    ///
    /// This function does not panic.
    pub fn observe(&mut self, arm_id: u64, reward: f64) {
        // Guard: reject non-finite rewards to prevent NaN poisoning of UCT scores.
        if !reward.is_finite() {
            return;
        }

        let Some(node_idx) = self.arm_to_node.get(&arm_id) else {
            return;
        };

        // Backpropagate: group node → root.
        let node_idx = *node_idx;
        let group_node = &mut self.nodes[node_idx as usize];
        group_node.visits += 1;
        group_node.reward += reward;

        {
            let root = &mut self.nodes[0];
            root.visits += 1;
            root.reward += reward;
        }

        // RAVE: boost sibling groups with decayed reward.
        // Skip entirely when rave_bias is zero (RAVE disabled).
        if self.config.rave_bias > 0.0 {
            let sibling_groups: Vec<u32> = self.nodes[0].children.clone();
            for sibling_idx in sibling_groups {
                if sibling_idx == node_idx {
                    continue;
                }

                let sibling = &mut self.nodes[sibling_idx as usize];
                sibling.rave_visits += 1;
                sibling.rave_reward += reward;
            }
        }
    }

    /// Scalarizes named signals using the configured scalarizer and records the result.
    ///
    /// # Parameters
    ///
    /// - `arm_id`: Previously selected arm identifier.
    /// - `signals`: Named signal values to convert into a scalar reward.
    ///
    /// # Returns
    ///
    /// This function returns no value.
    ///
    /// # Panics
    ///
    /// This function does not panic.
    pub fn observe_with_signals(&mut self, arm_id: u64, signals: &[(&str, f64)]) {
        let reward = self.config.scalarizer.scalarize(signals);
        self.observe(arm_id, reward);
    }

    /// Updates signal weights used by the configured scalarizer for online re-weighting.
    ///
    /// # Parameters
    ///
    /// - `updates`: Named signal weights to overwrite.
    ///
    /// # Returns
    ///
    /// This function returns no value.
    ///
    /// # Panics
    ///
    /// This function does not panic.
    pub fn reweight_signals(&mut self, updates: &[(&str, f64)]) {
        for (name, weight) in updates {
            if weight.is_finite() {
                self.config
                    .scalarizer
                    .signal_weights
                    .insert(name.to_string(), *weight);
            }
        }
    }

    /// Sets an external bias on a group node.
    ///
    /// Use this to inject domain-specific priors (e.g., from a mixture-of-experts
    /// gating network, from prior scan results, or from static analysis).
    /// # Parameters
    ///
    /// - `group_id`: Target group identifier.
    /// - `bias`: Bias to apply during group tie-breaking.
    ///
    /// # Returns
    ///
    /// This function returns no value.
    ///
    /// # Panics
    ///
    /// This function does not panic.
    pub fn set_group_bias(&mut self, group_id: u32, bias: f64) {
        if let Some(&node_idx) = self.group_to_node.get(&group_id) {
            self.nodes[node_idx as usize].bias = bias;
        }
    }

    /// Returns a snapshot of per-group search statistics.
    ///
    /// # Parameters
    ///
    /// This function takes no additional parameters.
    ///
    /// # Returns
    ///
    /// Returns one `GroupStats` record for each registered group.
    ///
    /// # Panics
    ///
    /// This function does not panic.
    pub fn group_stats(&self) -> Vec<GroupStats> {
        self.nodes[0]
            .children
            .iter()
            .map(|&idx| {
                let node = &self.nodes[idx as usize];
                let avg = if node.visits > 0 {
                    node.reward / f64::from(node.visits)
                } else {
                    0.0
                };
                GroupStats {
                    group_id: node.group_id,
                    visits: node.visits,
                    average_reward: avg,
                    total_arms: node.arms.len(),
                    explored_arms: node.next_untried,
                    rave_visits: node.rave_visits,
                }
            })
            .collect()
    }

    /// Returns the total number of pulls executed so far.
    ///
    /// # Parameters
    ///
    /// This function takes no additional parameters.
    ///
    /// # Returns
    ///
    /// Returns the cumulative pull count.
    ///
    /// # Panics
    ///
    /// This function does not panic.
    pub fn total_pulls(&self) -> u64 {
        self.pulls_executed
    }

    /// Reports a multi-signal observation for an arm, using the maximum signal value
    /// as the scalar reward.
    ///
    /// # Parameters
    ///
    /// - `arm_id`: Previously selected arm identifier.
    /// - `signals`: Named signal values (e.g., coverage, crashes, sinks).
    pub fn observe_multi(&mut self, arm_id: u64, signals: &[(&str, f64)]) {
        let scalar = signals
            .iter()
            .map(|(_, v)| *v)
            .fold(f64::NEG_INFINITY, f64::max);
        self.observe(arm_id, scalar);
    }

    /// Returns statistics for a specific group.
    ///
    /// # Parameters
    ///
    /// - `group_id`: Target group identifier.
    ///
    /// # Returns
    ///
    /// Returns [`GroupStats`](crate::GroupStats) for the group if it exists, otherwise `None`.
    pub fn group_stat(&self, group_id: u32) -> Option<GroupStats> {
        let idx = *self.group_to_node.get(&group_id)?;
        let node = &self.nodes[idx as usize];
        let avg = if node.visits > 0 {
            node.reward / f64::from(node.visits)
        } else {
            0.0
        };
        Some(GroupStats {
            group_id: node.group_id,
            visits: node.visits,
            average_reward: avg,
            total_arms: node.arms.len(),
            explored_arms: node.next_untried,
            rave_visits: node.rave_visits,
        })
    }
}

fn entropy_rng() -> ChaCha8Rng {
    match ChaCha8Rng::try_from_rng(&mut rand::rngs::SysRng) {
        Ok(rng) => rng,
        Err(error) => panic!("failed to seed ChaCha8Rng from system RNG: {error}"),
    }
}