asupersync 0.3.1

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
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
//! Opportunity scoring and performance PR gate logic.
//!
//! Implements the opportunity matrix scoring formula from
//! `docs/benchmarking.md` as a Rust type so the gate logic can be
//! unit-tested and reused by CLI tooling.
//!
//! # Scoring Formula
//!
//! ```text
//! Score = (Impact × Confidence) / Effort
//! ```
//!
//! | Factor | Range | Description |
//! |--------|-------|-------------|
//! | Impact | 1–5 | Expected improvement (1 = <5%, 5 = >50%) |
//! | Confidence | 0.2–1.0 | Evidence level (0.2 = speculative, 1.0 = certain) |
//! | Effort | 1–5 | Implementation cost (1 = trivial, 5 = major) |
//!
//! # Gate Decision
//!
//! Performance PRs must satisfy:
//!
//! 1. Score ≥ 2.0 (the "implement" threshold)
//! 2. Isomorphism proof section present
//! 3. Baseline metrics present (p50, p99)
//! 4. One Lever Rule documented
//!
//! See `.github/workflows/perf-pr-check.yml` for the CI enforcement.

/// Opportunity score factors for a performance optimization proposal.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct OpportunityScore {
    /// Expected performance improvement magnitude (1–5).
    pub impact: f64,
    /// Confidence in the estimate based on evidence (0.2–1.0).
    pub confidence: f64,
    /// Implementation effort (1–5).
    pub effort: f64,
}

/// Score threshold for "implement" decision.
pub const SCORE_THRESHOLD: f64 = 2.0;

/// Decision from the perf gate.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GateDecision {
    /// Score meets threshold — implement the optimization.
    Implement,
    /// Score is promising but below threshold — gather more evidence.
    NeedsEvidence,
    /// Score is too low — not worthwhile.
    Reject,
}

/// Structured gate result with decision path for CI logging.
#[derive(Debug, Clone, PartialEq)]
pub struct GateResult {
    /// The computed opportunity score.
    pub score: f64,
    /// The gate decision.
    pub decision: GateDecision,
    /// Structured reasons for the decision.
    pub reasons: Vec<&'static str>,
}

/// Validation errors for opportunity score inputs.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ScoreError {
    /// Impact must be in [1, 5].
    ImpactOutOfRange,
    /// Confidence must be in [0.2, 1.0].
    ConfidenceOutOfRange,
    /// Effort must be in [1, 5].
    EffortOutOfRange,
    /// Effort must not be zero (division by zero).
    ZeroEffort,
}

impl OpportunityScore {
    /// Creates a new score, validating inputs.
    pub fn new(impact: f64, confidence: f64, effort: f64) -> Result<Self, ScoreError> {
        if effort == 0.0 {
            return Err(ScoreError::ZeroEffort);
        }
        if !(1.0..=5.0).contains(&impact) {
            return Err(ScoreError::ImpactOutOfRange);
        }
        if !(0.2..=1.0).contains(&confidence) {
            return Err(ScoreError::ConfidenceOutOfRange);
        }
        if !(1.0..=5.0).contains(&effort) {
            return Err(ScoreError::EffortOutOfRange);
        }
        Ok(Self {
            impact,
            confidence,
            effort,
        })
    }

    /// Computes the opportunity score: `(Impact × Confidence) / Effort`.
    #[must_use]
    pub fn score(&self) -> f64 {
        (self.impact * self.confidence) / self.effort
    }

    /// Evaluates the perf gate and returns a structured decision.
    #[must_use]
    pub fn evaluate(&self) -> GateResult {
        let score = self.score();
        let mut reasons = Vec::new();

        let decision = if score >= SCORE_THRESHOLD {
            reasons.push("score meets threshold (>= 2.0)");
            if self.confidence >= 0.8 {
                reasons.push("high confidence from profiling evidence");
            }
            if self.effort <= 2.0 {
                reasons.push("low implementation effort");
            }
            GateDecision::Implement
        } else if score >= 1.0 {
            reasons.push("score below threshold but promising (1.0–2.0)");
            if self.confidence < 0.6 {
                reasons.push("needs profiling data to increase confidence");
            }
            if self.impact >= 3.0 {
                reasons.push("high potential impact justifies further investigation");
            }
            GateDecision::NeedsEvidence
        } else {
            reasons.push("score below 1.0 — not worthwhile");
            if self.impact <= 2.0 {
                reasons.push("low expected impact");
            }
            if self.effort >= 4.0 {
                reasons.push("high implementation effort relative to gain");
            }
            GateDecision::Reject
        };

        GateResult {
            score,
            decision,
            reasons,
        }
    }
}

impl core::fmt::Display for OpportunityScore {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(
            f,
            "Impact={:.1} × Confidence={:.1} / Effort={:.1} = {:.2}",
            self.impact,
            self.confidence,
            self.effort,
            self.score()
        )
    }
}

impl core::fmt::Display for GateDecision {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            Self::Implement => write!(f, "IMPLEMENT"),
            Self::NeedsEvidence => write!(f, "NEEDS_EVIDENCE"),
            Self::Reject => write!(f, "REJECT"),
        }
    }
}

impl core::fmt::Display for GateResult {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "score={:.2} decision={}", self.score, self.decision)?;
        for reason in &self.reasons {
            write!(f, " reason=\"{reason}\"")?;
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    // =========================================================================
    // Scoring Formula Tests
    // =========================================================================

    #[test]
    fn score_basic_formula() {
        // Impact=3, Confidence=0.8, Effort=1 → 3×0.8/1 = 2.4
        let s = OpportunityScore::new(3.0, 0.8, 1.0).unwrap();
        let score = s.score();
        assert!((score - 2.4).abs() < 1e-9, "expected 2.4, got {score}");
    }

    #[test]
    fn score_examples_from_docs() {
        // Pre-size BinaryHeap lanes: 3 × 0.8 / 1 = 2.4
        let s = OpportunityScore::new(3.0, 0.8, 1.0).unwrap();
        assert!((s.score() - 2.4).abs() < 1e-9);

        // Arena-backed task nodes: 4 × 0.6 / 3 = 0.8
        let s = OpportunityScore::new(4.0, 0.6, 3.0).unwrap();
        assert!((s.score() - 0.8).abs() < 1e-9);

        // Intrusive queues: 4 × 0.6 / 4 = 0.6
        let s = OpportunityScore::new(4.0, 0.6, 4.0).unwrap();
        assert!((s.score() - 0.6).abs() < 1e-9);

        // Reuse steal_batch Vec: 2 × 1.0 / 1 = 2.0
        let s = OpportunityScore::new(2.0, 1.0, 1.0).unwrap();
        assert!((s.score() - 2.0).abs() < 1e-9);

        // SIMD for RaptorQ GF ops: 5 × 0.4 / 3 ≈ 0.667
        let s = OpportunityScore::new(5.0, 0.4, 3.0).unwrap();
        assert!((s.score() - 2.0 / 3.0).abs() < 1e-9);
    }

    // =========================================================================
    // Gate Decision Tests
    // =========================================================================

    #[test]
    fn gate_implement_when_above_threshold() {
        let s = OpportunityScore::new(3.0, 0.8, 1.0).unwrap();
        let result = s.evaluate();
        assert_eq!(result.decision, GateDecision::Implement);
        assert!(result.score >= SCORE_THRESHOLD);
    }

    #[test]
    fn gate_implement_at_exact_threshold() {
        // 2 × 1.0 / 1 = 2.0 — exactly at threshold
        let s = OpportunityScore::new(2.0, 1.0, 1.0).unwrap();
        let result = s.evaluate();
        assert_eq!(result.decision, GateDecision::Implement);
    }

    #[test]
    fn gate_reject_below_needs_evidence_threshold() {
        // 4 × 0.6 / 3 = 0.8 — below 1.0 - not worthwhile
        let s = OpportunityScore::new(4.0, 0.6, 3.0).unwrap();
        let result = s.evaluate();
        assert_eq!(result.decision, GateDecision::Reject);
    }

    #[test]
    fn gate_needs_evidence_mid_range() {
        // 3 × 0.5 / 1 = 1.5 — between 1.0 and 2.0
        let s = OpportunityScore::new(3.0, 0.5, 1.0).unwrap();
        let result = s.evaluate();
        assert_eq!(result.decision, GateDecision::NeedsEvidence);
    }

    #[test]
    fn gate_reject_low_score() {
        // 1 × 0.2 / 5 = 0.04
        let s = OpportunityScore::new(1.0, 0.2, 5.0).unwrap();
        let result = s.evaluate();
        assert_eq!(result.decision, GateDecision::Reject);
        assert!(result.score < 1.0);
    }

    // =========================================================================
    // Decision Path (Structured Logging) Tests
    // =========================================================================

    #[test]
    fn gate_result_includes_reasons() {
        let s = OpportunityScore::new(3.0, 0.9, 1.0).unwrap();
        let result = s.evaluate();
        assert!(!result.reasons.is_empty());
        assert!(result.reasons.contains(&"score meets threshold (>= 2.0)"));
        assert!(
            result
                .reasons
                .contains(&"high confidence from profiling evidence")
        );
        assert!(result.reasons.contains(&"low implementation effort"));
    }

    #[test]
    fn gate_result_needs_evidence_reasons() {
        let s = OpportunityScore::new(4.0, 0.4, 1.0).unwrap();
        // 4 × 0.4 / 1 = 1.6 → NeedsEvidence
        let result = s.evaluate();
        assert_eq!(result.decision, GateDecision::NeedsEvidence);
        assert!(
            result
                .reasons
                .contains(&"needs profiling data to increase confidence")
        );
        assert!(
            result
                .reasons
                .contains(&"high potential impact justifies further investigation")
        );
    }

    #[test]
    fn gate_result_reject_reasons() {
        let s = OpportunityScore::new(2.0, 0.3, 4.0).unwrap();
        // 2 × 0.3 / 4 = 0.15 → Reject
        let result = s.evaluate();
        assert_eq!(result.decision, GateDecision::Reject);
        assert!(result.reasons.contains(&"low expected impact"));
        assert!(
            result
                .reasons
                .contains(&"high implementation effort relative to gain")
        );
    }

    #[test]
    fn gate_result_display_is_structured() {
        let s = OpportunityScore::new(3.0, 0.8, 1.0).unwrap();
        let result = s.evaluate();
        let display = format!("{result}");
        assert!(display.contains("score=2.40"));
        assert!(display.contains("decision=IMPLEMENT"));
        assert!(display.contains("reason="));
    }

    // =========================================================================
    // Input Validation Tests
    // =========================================================================

    #[test]
    fn rejects_impact_out_of_range() {
        assert_eq!(
            OpportunityScore::new(0.5, 0.5, 1.0),
            Err(ScoreError::ImpactOutOfRange)
        );
        assert_eq!(
            OpportunityScore::new(6.0, 0.5, 1.0),
            Err(ScoreError::ImpactOutOfRange)
        );
    }

    #[test]
    fn rejects_confidence_out_of_range() {
        assert_eq!(
            OpportunityScore::new(3.0, 0.1, 1.0),
            Err(ScoreError::ConfidenceOutOfRange)
        );
        assert_eq!(
            OpportunityScore::new(3.0, 1.1, 1.0),
            Err(ScoreError::ConfidenceOutOfRange)
        );
    }

    #[test]
    fn rejects_effort_out_of_range() {
        assert_eq!(
            OpportunityScore::new(3.0, 0.5, 0.5),
            Err(ScoreError::EffortOutOfRange)
        );
        assert_eq!(
            OpportunityScore::new(3.0, 0.5, 6.0),
            Err(ScoreError::EffortOutOfRange)
        );
    }

    #[test]
    fn rejects_zero_effort() {
        assert_eq!(
            OpportunityScore::new(3.0, 0.5, 0.0),
            Err(ScoreError::ZeroEffort)
        );
    }

    #[test]
    fn rejects_tiny_nonzero_effort_as_out_of_range() {
        assert_eq!(
            OpportunityScore::new(3.0, 0.5, f64::EPSILON / 2.0),
            Err(ScoreError::EffortOutOfRange)
        );
        assert_eq!(
            OpportunityScore::new(3.0, 0.5, -f64::EPSILON / 2.0),
            Err(ScoreError::EffortOutOfRange)
        );
    }

    // =========================================================================
    // Monotonicity Properties
    // =========================================================================

    #[test]
    fn score_increases_with_impact() {
        let lo = OpportunityScore::new(1.0, 0.8, 2.0).unwrap();
        let hi = OpportunityScore::new(5.0, 0.8, 2.0).unwrap();
        assert!(hi.score() > lo.score());
    }

    #[test]
    fn score_increases_with_confidence() {
        let lo = OpportunityScore::new(3.0, 0.2, 2.0).unwrap();
        let hi = OpportunityScore::new(3.0, 1.0, 2.0).unwrap();
        assert!(hi.score() > lo.score());
    }

    #[test]
    fn score_decreases_with_effort() {
        let lo = OpportunityScore::new(3.0, 0.8, 1.0).unwrap();
        let hi = OpportunityScore::new(3.0, 0.8, 5.0).unwrap();
        assert!(lo.score() > hi.score());
    }

    // =========================================================================
    // Boundary Cases
    // =========================================================================

    #[test]
    fn max_score() {
        // 5 × 1.0 / 1 = 5.0
        let s = OpportunityScore::new(5.0, 1.0, 1.0).unwrap();
        assert!((s.score() - 5.0).abs() < 1e-9);
        assert_eq!(s.evaluate().decision, GateDecision::Implement);
    }

    #[test]
    fn min_score() {
        // 1 × 0.2 / 5 = 0.04
        let s = OpportunityScore::new(1.0, 0.2, 5.0).unwrap();
        assert!(s.score() < 0.05);
        assert_eq!(s.evaluate().decision, GateDecision::Reject);
    }

    #[test]
    fn opportunity_score_debug_clone_copy_eq() {
        let s = OpportunityScore::new(3.0, 0.8, 2.0).unwrap();
        let dbg = format!("{s:?}");
        assert!(dbg.contains("OpportunityScore"), "{dbg}");
        let copied: OpportunityScore = s;
        let cloned = s;
        assert_eq!(copied, cloned);
    }

    #[test]
    fn gate_decision_debug_clone_copy_eq() {
        let d = GateDecision::Implement;
        let dbg = format!("{d:?}");
        assert!(dbg.contains("Implement"), "{dbg}");
        let copied: GateDecision = d;
        let cloned = d;
        assert_eq!(copied, cloned);
        assert_ne!(d, GateDecision::Reject);
    }

    #[test]
    fn gate_result_debug_clone_eq() {
        let s = OpportunityScore::new(4.0, 0.9, 1.0).unwrap();
        let r = s.evaluate();
        let dbg = format!("{r:?}");
        assert!(dbg.contains("GateResult"), "{dbg}");
        let cloned = r.clone();
        assert_eq!(r, cloned);
    }

    #[test]
    fn score_error_debug_clone_copy_eq() {
        let e = ScoreError::ImpactOutOfRange;
        let dbg = format!("{e:?}");
        assert!(dbg.contains("ImpactOutOfRange"), "{dbg}");
        let copied: ScoreError = e;
        let cloned = e;
        assert_eq!(copied, cloned);
        assert_ne!(e, ScoreError::ZeroEffort);
    }
}