asupersync 0.3.6

Spec-first, cancel-correct, capability-secure async runtime for Rust.
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
//! Conformal exploration-budget estimates for schedule search.
//!
//! This module converts DPOR or seed-sweep novelty observations into a
//! deterministic stopping signal. The bound is intentionally scoped: it is a
//! finite-sample upper confidence bound on the binary novelty proportion under
//! exchangeability, not a proof that every reachable schedule class has been
//! enumerated.

use crate::lab::explorer::RunResult;
use serde::Serialize;

/// Configuration for conformal exploration-budget estimates.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ExplorationBudgetConfig {
    /// Target miscoverage rate for the conformal novelty bound.
    pub alpha: f64,
    /// Desired coverage confidence, expressed as `1 - residual novelty`.
    pub target_coverage: f64,
    /// Minimum observations before a stop recommendation may be issued.
    pub min_samples: usize,
    /// Maximum additional existing-class runs considered for the recommendation.
    pub max_additional_runs: usize,
}

impl Default for ExplorationBudgetConfig {
    fn default() -> Self {
        Self {
            alpha: 0.05,
            target_coverage: 0.95,
            min_samples: 20,
            max_additional_runs: 1_000,
        }
    }
}

impl ExplorationBudgetConfig {
    /// Create a budget config with a target miscoverage and coverage level.
    #[must_use]
    pub fn new(alpha: f64, target_coverage: f64) -> Self {
        assert_valid_probability("alpha", alpha);
        assert_valid_probability("target_coverage", target_coverage);
        Self {
            alpha,
            target_coverage,
            ..Self::default()
        }
    }

    /// Set the minimum sample count required before stop recommendations.
    #[must_use]
    pub fn min_samples(mut self, samples: usize) -> Self {
        assert!(samples > 0, "min_samples must be greater than zero");
        self.min_samples = samples;
        self
    }

    /// Set the maximum additional run count considered by recommendations.
    #[must_use]
    pub fn max_additional_runs(mut self, runs: usize) -> Self {
        self.max_additional_runs = runs;
        self
    }
}

/// Explicit assumptions behind an exploration-budget estimate.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
pub struct ExplorationBudgetAssumptions {
    /// Observed runs are treated as exchangeable for conformal calibration.
    pub exchangeable_runs: bool,
    /// The novelty score is binary: `1.0` for a new class and `0.0` otherwise.
    pub binary_novelty_score: bool,
    /// Additional-run recommendations assume future added runs hit known classes.
    pub additional_runs_assume_existing_classes: bool,
}

impl Default for ExplorationBudgetAssumptions {
    fn default() -> Self {
        Self {
            exchangeable_runs: true,
            binary_novelty_score: true,
            additional_runs_assume_existing_classes: true,
        }
    }
}

/// Deterministic exploration-budget estimate.
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct ExplorationBudgetEstimate {
    /// Total novelty observations consumed.
    pub total_runs: usize,
    /// Number of observations that discovered a new equivalence class.
    pub discoveries: usize,
    /// Empirical residual discovery rate, `discoveries / total_runs`, or `1.0`
    /// before the first calibration sample.
    pub residual_discovery_rate: f64,
    /// Finite-sample upper confidence bound for the residual discovery rate.
    pub conformal_upper_bound: f64,
    /// Target residual discovery rate, `1 - target_coverage`.
    pub target_residual_rate: f64,
    /// Requested coverage confidence.
    pub target_coverage: f64,
    /// Number of samples used by the conformal bound.
    pub calibration_samples: usize,
    /// Additional runs recommended before the target should be re-evaluated.
    pub recommended_additional_runs: usize,
    /// True when the conformal bound is at or below the target residual rate.
    pub target_met: bool,
    /// True when the recommendation hit `max_additional_runs` before the target.
    pub exhausted_recommendation: bool,
    /// Assumptions that scope this estimate.
    pub assumptions: ExplorationBudgetAssumptions,
}

/// Pure estimator for exploration-budget reports.
#[derive(Debug, Clone, Copy)]
pub struct ExplorationBudget;

impl ExplorationBudget {
    /// Estimate residual novelty from an ordered novelty-observation series.
    ///
    /// Each `true` value represents a run that discovered a new equivalence
    /// class; each `false` value represents an existing-class hit.
    #[must_use]
    pub fn estimate_from_novelty<I>(
        novelty: I,
        config: ExplorationBudgetConfig,
    ) -> ExplorationBudgetEstimate
    where
        I: IntoIterator<Item = bool>,
    {
        Self::estimate_from_flags(novelty.into_iter().collect(), config)
    }

    /// Estimate residual novelty directly from explorer run results.
    #[must_use]
    pub fn estimate_from_runs(
        runs: &[RunResult],
        config: ExplorationBudgetConfig,
    ) -> ExplorationBudgetEstimate {
        Self::estimate_from_novelty(runs.iter().map(|run| run.is_new_class), config)
    }

    /// Estimate residual novelty from aggregate counts.
    ///
    /// This is useful for serialized coverage reports that preserve discovery
    /// counts but not individual run order. The binary proportion bound is
    /// order-insensitive, so the aggregate path produces the same bound as a
    /// per-run series with the same counts.
    #[must_use]
    pub fn estimate_from_counts(
        total_runs: usize,
        discoveries: usize,
        config: ExplorationBudgetConfig,
    ) -> ExplorationBudgetEstimate {
        assert!(
            discoveries <= total_runs,
            "discoveries must not exceed total_runs"
        );
        let existing_hits = total_runs - discoveries;
        let mut novelty = Vec::with_capacity(total_runs);
        novelty.extend(std::iter::repeat_n(true, discoveries));
        novelty.extend(std::iter::repeat_n(false, existing_hits));
        Self::estimate_from_flags(novelty, config)
    }

    fn estimate_from_flags(
        novelty: Vec<bool>,
        config: ExplorationBudgetConfig,
    ) -> ExplorationBudgetEstimate {
        assert_valid_config(config);

        let total_runs = novelty.len();
        let discoveries = novelty.iter().filter(|&&is_new| is_new).count();
        let residual_discovery_rate = ratio(discoveries, total_runs);
        let target_residual_rate = 1.0 - config.target_coverage;
        let conformal_upper_bound =
            binary_novelty_upper_bound(&novelty, config.alpha, config.min_samples);
        let target_met = conformal_upper_bound <= target_residual_rate;
        let recommended_additional_runs = if target_met {
            0
        } else {
            recommended_existing_class_runs(&novelty, config)
        };
        let exhausted_recommendation = !target_met
            && recommended_additional_runs == config.max_additional_runs
            && !target_reached_after_existing_hits(&novelty, config, recommended_additional_runs);

        ExplorationBudgetEstimate {
            total_runs,
            discoveries,
            residual_discovery_rate,
            conformal_upper_bound,
            target_residual_rate,
            target_coverage: config.target_coverage,
            calibration_samples: total_runs,
            recommended_additional_runs,
            target_met,
            exhausted_recommendation,
            assumptions: ExplorationBudgetAssumptions::default(),
        }
    }
}

fn assert_valid_config(config: ExplorationBudgetConfig) {
    assert_valid_probability("alpha", config.alpha);
    assert_valid_probability("target_coverage", config.target_coverage);
    assert!(
        config.min_samples > 0,
        "min_samples must be greater than zero"
    );
}

fn assert_valid_probability(name: &str, value: f64) {
    assert!(
        value.is_finite() && value > 0.0 && value < 1.0,
        "{name} must be finite and in (0, 1)"
    );
}

fn ratio(numerator: usize, denominator: usize) -> f64 {
    if denominator == 0 {
        return 1.0;
    }
    numerator as f64 / denominator as f64
}

fn binary_novelty_upper_bound(novelty: &[bool], alpha: f64, min_samples: usize) -> f64 {
    if novelty.len() < min_samples {
        return 1.0;
    }
    let discoveries = novelty.iter().filter(|&&is_new| is_new).count();
    let empirical_rate = ratio(discoveries, novelty.len());
    let sample_count = novelty.len() as f64;
    let radius = ((1.0 / alpha).ln() / (2.0 * sample_count)).sqrt();
    (empirical_rate + radius).min(1.0)
}

fn recommended_existing_class_runs(novelty: &[bool], config: ExplorationBudgetConfig) -> usize {
    for additional in 0..=config.max_additional_runs {
        if target_reached_after_existing_hits(novelty, config, additional) {
            return additional;
        }
    }
    config.max_additional_runs
}

fn target_reached_after_existing_hits(
    novelty: &[bool],
    config: ExplorationBudgetConfig,
    additional: usize,
) -> bool {
    let mut projected = Vec::with_capacity(novelty.len() + additional);
    projected.extend_from_slice(novelty);
    projected.extend(std::iter::repeat_n(false, additional));
    binary_novelty_upper_bound(&projected, config.alpha, config.min_samples)
        <= 1.0 - config.target_coverage
}

#[cfg(test)]
mod tests {
    #![allow(clippy::pedantic, clippy::nursery, clippy::float_cmp)]

    use super::*;
    use crate::lab::runtime::InvariantViolation;

    #[test]
    fn empty_series_reports_uncalibrated_upper_bound() {
        let estimate = ExplorationBudget::estimate_from_novelty(
            [],
            ExplorationBudgetConfig::new(0.05, 0.95)
                .min_samples(5)
                .max_additional_runs(3),
        );

        assert_eq!(estimate.total_runs, 0);
        assert_eq!(estimate.discoveries, 0);
        assert_eq!(estimate.residual_discovery_rate, 1.0);
        assert_eq!(estimate.conformal_upper_bound, 1.0);
        assert_eq!(estimate.recommended_additional_runs, 3);
        assert!(estimate.exhausted_recommendation);
        assert!(!estimate.target_met);
    }

    #[test]
    fn existing_class_hits_can_satisfy_target_after_min_samples() {
        let estimate = ExplorationBudget::estimate_from_novelty(
            [false; 25],
            ExplorationBudgetConfig::new(0.20, 0.80).min_samples(5),
        );

        assert_eq!(estimate.total_runs, 25);
        assert!(estimate.conformal_upper_bound <= estimate.target_residual_rate);
        assert_eq!(estimate.recommended_additional_runs, 0);
        assert!(estimate.target_met);
        assert!(!estimate.exhausted_recommendation);
    }

    #[test]
    fn binary_bound_fails_closed_before_min_samples() {
        let estimate = ExplorationBudget::estimate_from_novelty(
            [false; 18],
            ExplorationBudgetConfig::new(0.05, 0.95)
                .min_samples(20)
                .max_additional_runs(3),
        );

        assert_eq!(estimate.conformal_upper_bound, 1.0);
        assert!(!estimate.target_met);
        assert_eq!(estimate.recommended_additional_runs, 3);
    }

    #[test]
    fn target_coverage_changes_decision_for_same_observations() {
        let relaxed = ExplorationBudget::estimate_from_novelty(
            [false; 25],
            ExplorationBudgetConfig::new(0.20, 0.80)
                .min_samples(5)
                .max_additional_runs(400),
        );
        let strict = ExplorationBudget::estimate_from_novelty(
            [false; 25],
            ExplorationBudgetConfig::new(0.20, 0.95)
                .min_samples(5)
                .max_additional_runs(400),
        );

        assert_eq!(relaxed.conformal_upper_bound, strict.conformal_upper_bound);
        assert!(relaxed.target_met);
        assert!(!strict.target_met);
        assert_eq!(relaxed.recommended_additional_runs, 0);
        assert!(strict.recommended_additional_runs > 0);
    }

    #[test]
    fn discoveries_keep_conformal_bound_conservative() {
        let no_discoveries = ExplorationBudget::estimate_from_novelty(
            [false; 25],
            ExplorationBudgetConfig::new(0.20, 0.80).min_samples(5),
        );
        let with_discoveries = ExplorationBudget::estimate_from_novelty(
            [
                true, true, true, true, true, false, false, false, false, false, false, false,
                false, false, false, false, false, false, false, false, false, false, false, false,
                false,
            ],
            ExplorationBudgetConfig::new(0.20, 0.80).min_samples(5),
        );

        assert!(with_discoveries.conformal_upper_bound >= no_discoveries.conformal_upper_bound);
        assert!(with_discoveries.recommended_additional_runs > 0);
    }

    #[test]
    fn counts_match_same_size_novelty_series() {
        let config = ExplorationBudgetConfig::new(0.20, 0.80).min_samples(5);
        let from_counts = ExplorationBudget::estimate_from_counts(10, 2, config);
        let from_series = ExplorationBudget::estimate_from_novelty(
            [
                true, false, false, true, false, false, false, false, false, false,
            ],
            config,
        );

        assert_eq!(
            from_counts.conformal_upper_bound,
            from_series.conformal_upper_bound
        );
        assert_eq!(
            from_counts.residual_discovery_rate,
            from_series.residual_discovery_rate
        );
    }

    #[test]
    fn run_results_feed_budget_estimator() {
        let runs = [
            RunResult {
                seed: 1,
                steps: 10,
                fingerprint: 101,
                is_new_class: true,
                violations: Vec::<InvariantViolation>::new(),
                certificate_hash: 1_001,
            },
            RunResult {
                seed: 2,
                steps: 8,
                fingerprint: 101,
                is_new_class: false,
                violations: Vec::<InvariantViolation>::new(),
                certificate_hash: 1_001,
            },
        ];

        let estimate = ExplorationBudget::estimate_from_runs(
            &runs,
            ExplorationBudgetConfig::new(0.20, 0.80).min_samples(2),
        );

        assert_eq!(estimate.total_runs, 2);
        assert_eq!(estimate.discoveries, 1);
        assert_eq!(estimate.residual_discovery_rate, 0.5);
    }
}